]> andersk Git - sql-web.git/blobdiff - lib/security.lib.php
check quotas function
[sql-web.git] / lib / security.lib.php
index 8cd49db162ffa491f055afe0b9903cc378bd3c18..fca7a0f2279bfd399bc976dc9d2607ff96a0c968 100644 (file)
@@ -3,21 +3,25 @@
 require_once('mitsql.lib.php');
 
 class Login {
-       var $u, $p;
+       var $id, $u, $p;
     var $info;
     function Login($u, $p=null) {
                if (empty($u)) return;
-               $this->u = $u;
+               $this->u = $u;
                $this->p = $p;
-               $opt = sprintf(" Username = '%s'", mysql_escape_string($u));
-        $opt .= (is_null($p)?'':sprintf(" AND Password='%s'", mysql_escape_string(base64_encode($p))));
-               is_numeric($u) && $opt = sprintf(" UserId = '%s'", mysql_escape_string($u));
+               if (is_numeric($u)) {
+                       $this->id = $u;
+                       $opt = sprintf(" UserId = '%s'", mysql_escape_string($u));
+               } else {
+                       $opt = sprintf(" Username = '%s'", mysql_escape_string($u));
+                       $opt .= (is_null($p)?'':sprintf(" AND Password='%s'", mysql_escape_string(base64_encode($p))));
+               }
         $sql = sprintf("SELECT UserId, Username, Name, Email, UL, bEnabled
                         FROM User
                         WHERE %s", $opt);
         $r = fetchRows(DBSelect($sql),'UserId');
-        $this->info = count($r)?array_shift($r):$r;
-    }
+               $this->info = count($r)?array_shift($r):$r;
+       }
     function exists() {
         return count($this->info);
     }
@@ -52,7 +56,11 @@ class Login {
         $this->info = null;
     }
     function refresh() {
-        $this->Login($this->u,$this->p);
+               if (!empty($this->id)) {
+                       $this->Login($this->id);
+               } else {
+                       $this->Login($this->u,$this->p);
+               }
     }
     function update($name=null,$email=null) {
         if (!$this->exists()) return;
@@ -61,11 +69,11 @@ class Login {
                if ($email == $this->getEmail()) $email = null;
         is_null($name) || $arr['Name'] = $name;
         is_null($email) || $arr['Email'] = $email;
-       $upd = buildSQLSet($arr);
+               $upd = buildSQLSet($arr);
         $sql = sprintf("UPDATE User %s WHERE UserId = '%s'",
                         $upd, mysql_escape_string($this->getUserId()));
-       if (!empty($upd) && $upd != 'SET')
-               DBUpdate($sql);
+               if (!empty($upd) && $upd != 'SET')
+                       DBUpdate($sql);
                if (isset($arr['Name']))
                        $this->info['Name'] = $arr['Name'];
                if (isset($arr['Email']))
@@ -79,15 +87,31 @@ class User {
        var $dblist;
     function User($userId) {
                $this->userId = $userId;
-        $sql = sprintf("SELECT UserId, Username, Password, Name, Email, UL, bEnabled
+        $sql = sprintf("SELECT User.UserId, Username, Password, Name, Email, UL, bEnabled, nBytesSoft, nBytesHard, nBytes, nDatabases, nDatabasesHard, IF(nBytes>nBytesHard,1,0) AS bOverQuota
                         FROM User
-                        WHERE UserId = '%s'",
+                                               INNER JOIN UserQuota ON User.UserId = UserQuota.UserId
+                                               INNER JOIN UserStat ON User.UserId = UserStat.UserId
+                        WHERE User.UserId = '%s'",
                         mysql_escape_string($userId));
         $r = fetchRows(DBSelect($sql),'UserId');
         $this->info = count($r)?array_shift($r):$r;
                $this->dblist = $this->getDBList();
 //             $this->pass = base64_decode($this->info['Password']);
     }
+       function refresh() {
+               unset($this->dblist);
+               $this->User($this->userId);
+               /*
+        $sql = sprintf("SELECT UserId, Username, Password, Name, Email, UL, bEnabled
+                        FROM User
+                        WHERE UserId = '%s'",
+                        mysql_escape_string($this->userId));
+        $r = fetchRows(DBSelect($sql),'UserId');
+        $this->info = count($r)?array_shift($r):$r;
+               unset($this->dblist);
+               $this->getDBList();
+               */
+       }
     function exists() {
         return count($this->info);
     }
@@ -96,6 +120,17 @@ class User {
     }
     function getUsername() {
         return $this->exists()?$this->info['Username']:'';
+    }
+    function isOverQuota() {
+        return $this->exists()?($this->info['bOverQuota']>0?true:false):'';
+    }
+    function getBytes() {
+        if($this->exists()) {
+                       $arr['nBytes'] = $this->info['nBytes'];
+                       $arr['nBytesSoft'] = $this->info['nBytesSoft'];
+                       $arr['nBytesHard'] = $this->info['nBytesHard'];
+                       return $arr;
+               }
     }
        function setPassword($pwd) {
                $arr['Password'] = base64_encode($pwd);
@@ -135,8 +170,7 @@ class User {
                $verb = $yes?'GRANT':'REVOKE';
                $prep = $yes?'TO':'FROM';
                if (is_null($db)) {
-                       $this->dblist = $this->getDBList();
-                       $dbs = $this->dblist;
+                       $dbs = $this->getDBList();
                } else {
                        $dbs[] = array('Name'=>$db);
                }
@@ -152,20 +186,34 @@ class User {
                }
        }
        function getDBList() {
-               $sql = sprintf("SELECT *
+               if (isset($this->dblist)) {
+                       return $this->dblist;
+               } else {
+                       //                      LEFT JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId
+                       $sql = sprintf("SELECT *
                                                FROM DBOwner
                                                INNER JOIN DB ON DB.DatabaseId = DBOwner.DatabaseId
-                                               LEFT JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId
-                                               WHERE UserId = '%s' AND DB.bEnabled=1",
+                                               INNER JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId
+                                               WHERE DBOwner.UserId = '%s' AND DB.bEnabled=1",
                                                mysql_escape_string($this->getUserId()));
-               $r = fetchRows(DBSelect($sql),'DatabaseId');
-               return $r;
+//                     $r = fetchRows(DBSelect($sql),'DatabaseId');
+                       $r = fetchRows(DBSelect($sql),'Name');
+                       ksort($r);
+                       return $r;
+               }
        }
        function addDB($name) {
+               if (in_array($name, $this->getDBList())) return false;
                if (!addDB($name, $this->getUserId())) return false;
                $this->setAccess($name);
                return true;
        }
+       function delDB($name) {
+               if (!in_array($name, array_keys($this->getDBList()))) return false;
+               if (!delDB($name)) return false;//, $this->getUserId())) return false;
+               $this->setAccess($name,false);
+               return true;
+       }
 }
 
 
@@ -177,6 +225,38 @@ function isLoggedIn($aLogin=null) {
     return !empty($aLogin) && is_a($aLogin, 'Login') && $aLogin->canLogin();
 }
 
+function isAdmin($aLogin=null) {
+    if (is_null($aLogin)) {
+        global $Login;
+        $aLogin = $Login;
+    }
+    return !empty($aLogin) && is_a($aLogin, 'Login') && $aLogin->getUL()>=100;
+}
+
+function isImpersonating() {
+       return isSess('_UserId') && isSess('UserId');
+}
+
+function impersonate($userId=null) {
+       $wasImpersonating = isImpersonating();
+       if ($wasImpersonating) {
+               if (is_null($userId) || empty($userId)) {
+                       sess('UserId',sess('_UserId'));
+                       sess('_UserId','');
+               } elseif ($userId>0) {
+                       sess('UserId',$userId);
+               } else {
+                       return false;
+               }
+       } elseif (isLoggedIn()) {
+               sess('_UserId',sess('UserId'));
+               sess('UserId',$userId);
+               return true;
+       } else {
+               return false;
+       }
+}
+
 function isSSL() {
        return $_SERVER['SERVER_PORT'] == 443;
 }
@@ -205,6 +285,9 @@ function redirect($target=null,$secure=null) {
     $base = (is_null($target)||substr($target,0,1)=='?')?$_SERVER['REDIRECT_URL']:(dirname($_SERVER['REDIRECT_URL']).'/');
     redirectFull(is_null($target)?$base:($base.$target),$secure);
 }
+function redirectStart() {
+       redirectFull(BASE_URL,null);
+}
 function redirectFull($target,$secure) {
        redirect2((((isSSL()&&is_null($secure))||$secure==true)?'https://':'http://').$_SERVER['SERVER_NAME'].$target);
 }
@@ -242,32 +325,61 @@ function addUser($sslCredentials) {
 }
 
 function addDB($dbname,$userid) {
-    global $_NEW_DB, $_NEW_DBQUOTA, $_NEW_DBOWNER;
+       global $_NEW_DB, $_NEW_DBQUOTA, $_NEW_DBOWNER;
 
        DBCreate(sprintf('CREATE DATABASE `%s`', mysql_escape_string($dbname)));
-       if (mysql_error()) return false;
 
        $newdb['Name'] = $dbname;
-    $arr = array_merge($newdb, $_NEW_DB);
+       $arr = array_merge($newdb, $_NEW_DB);
        $arr['bEnabled'] = 1;
-    $sql = sprintf("INSERT INTO DB %s",
+       $sql = sprintf("INSERT IGNORE INTO DB %s",
                     buildSQLInsert($arr));
-    $DBId = DBInsert($sql);
+       $DBId = DBInsert($sql);
+       if (empty($DBId)) {
+               $sql = sprintf("SELECT DatabaseId FROM DB WHERE Name = '%s'",
+                                               mysql_escape_string($dbname));
+               $r = fetchRows(DBSelect($sql), 'DatabaseId');
+               if (count($r)) {
+                       $r = array_shift($r);
+                       $DBId = $r['DatabaseId'];
+               } else {
+                       return false;
+               }
+               $sql = sprintf("UPDATE DB %s WHERE DB.DatabaseId = '%s'",
+                                               buildSQLSet($arr),
+                                               $DBId);
+               DBUpdate($sql);
+               return $DBId;
+       } else {
+               $arr = $_NEW_DBQUOTA;
+               $arr['DatabaseId'] = $DBId;
+               $sql = sprintf("INSERT IGNORE INTO DBQuota %s",
+                                               buildSQLInsert($arr));
+               DBInsert($sql);
 
-       $arr = $_NEW_DBQUOTA;
-       $arr['DatabaseId'] = $DBId;
-    $sql = sprintf("INSERT INTO DBQuota %s",
-                    buildSQLInsert($arr));
-       DBInsert($sql);
+               $arr = $_NEW_DBOWNER;
+               $arr['DatabaseId'] = $DBId;
+               $arr['UserId'] = $userid;
+               $sql = sprintf("INSERT IGNORE INTO DBOwner %s",
+                                               buildSQLInsert($arr));
+               DBInsert($sql);
 
-       $arr = $_NEW_DBOWNER;
-       $arr['DatabaseId'] = $DBId;
-       $arr['UserId'] = $userid;
-    $sql = sprintf("INSERT INTO DBOwner %s",
-                    buildSQLInsert($arr));
-       DBInsert($sql);
+               return $DBId;
+       }
+}
+
+function delDB($dbname) {
+       global $_NEW_DB, $_NEW_DBQUOTA, $_NEW_DBOWNER;
+
+       DBCreate(sprintf('DROP DATABASE `%s`', mysql_escape_string($dbname)));
+
+       $arr['bEnabled'] = 0;
+       $sql = sprintf("UPDATE DB %s WHERE DB.Name = '%s'",
+                                       buildSQLSet($arr),
+                                       $dbname);
+       DBUpdate($sql);
 
-       return $DBId;
+       return true;
 }
 
-?>
\ No newline at end of file
+?>
This page took 0.042496 seconds and 4 git commands to generate.