From 9c70b48184fc9dc34b9725daa6b3b050fd49e6d0 Mon Sep 17 00:00:00 2001 From: Joe Presbrey Date: Fri, 9 Sep 2005 08:33:50 +0000 Subject: [PATCH] check quotas function dont let overquota'd users create databases BASE_URL points just above ~/do/ git-svn-id: svn://presbrey.mit.edu/sql/mitsql@55 a142d4bd-2cfb-0310-9673-cb33a7e74f58 --- admin/main.php | 2 +- global.act.php | 4 ++++ lib/dbaccess.lib.php | 28 ++++++++++++++++++++++++++++ lib/display.lib.php | 30 +++++++++++++++++++++++------- lib/security.lib.php | 6 +++++- main.php | 9 +++++---- mitsql.cfg.php | 2 +- test.php | 13 ------------- tpl/head.php | 3 ++- tpl/main.php | 15 ++++++++++----- tpl/menu.php | 19 ++++++++++--------- 11 files changed, 89 insertions(+), 42 deletions(-) diff --git a/admin/main.php b/admin/main.php index 4a7a749..5f6535b 100755 --- a/admin/main.php +++ b/admin/main.php @@ -17,7 +17,7 @@ $users = fetchRows(DBSelect($sql),'UserId'); include 'tpl/head.php'; foreach($users as $id=>$user) { - echo '',$user['Username'],' (',$user['Name'],')
'; + echo '',$user['Username'],' (',$user['Name'],')
'; } include 'tpl/foot.php'; diff --git a/global.act.php b/global.act.php index 8fe4a4c..94e662d 100644 --- a/global.act.php +++ b/global.act.php @@ -44,4 +44,8 @@ if (isSSL() || !isLoggedIn()) { unset($_SESSION['LoginSSL']); } +if (isPost() || isset($i_refresh)) { + checkQuotas($UserId); + isset($i_refresh) && redirect('main'); +} ?> diff --git a/lib/dbaccess.lib.php b/lib/dbaccess.lib.php index bcbc894..d58bef8 100644 --- a/lib/dbaccess.lib.php +++ b/lib/dbaccess.lib.php @@ -49,4 +49,32 @@ function calcDBSize($tdb) { } } +function checkQuotas($userId=null) { + if (empty($userId)) { + $sql = 'SELECT DatabaseId,Name FROM DB WHERE bEnabled=1'; + } else { + $sql = sprintf("SELECT DB.DatabaseId,Name FROM DB INNER JOIN DBOwner ON DB.DatabaseId = DBOwner.DatabaseId WHERE bEnabled=1 AND UserId = '%s'", mysql_escape_string($userId)); + } + $databases = fetchRows(DBSelect($sql),'Name'); + foreach($databases as $db) { + $DBId = $db['DatabaseId']; + $arr['dLastCheck'] = 'NOW()'; + $arr['nBytes'] = calcDBSize($db['Name']); + $sql = sprintf("UPDATE DB %s WHERE DatabaseId = '%s'", + buildSQLSet($arr), + mysql_escape_string($DBId)); + DBUpdate($sql); + } + $sql = "UPDATE UserStat SET nBytes = ( + SELECT SUM(nBytes) + FROM DB + INNER JOIN DBOwner ON DBOwner.DatabaseId = DB.DatabaseId + WHERE DBOwner.UserId = UserStat.UserId + AND DB.bEnabled=1 + GROUP BY UserId + ), dLastCheck = NOW()"; + if (!empty($userId)) $sql .= sprintf(" WHERE UserId = '%s'", mysql_escape_string($userId)); + DBUpdate($sql); +} + ?> diff --git a/lib/display.lib.php b/lib/display.lib.php index ecb4e49..4dfb81c 100644 --- a/lib/display.lib.php +++ b/lib/display.lib.php @@ -25,23 +25,39 @@ function printBar($percent, $txt1 = '', $txt2 = '') { $color2 = 'white'; $color3 = 'white'; $color4 = 'black'; - if ($percent>1) { + /* if ($percent>1) { $percent = $percent/100; - } /* + } } elseif (!is_integer($percent) && $percent<2) { $percent = $percent*100; } elseif ($percent == 1) { $percent = 100; } */ - $per1 = $per2 = ceil($percent*100); - $per2 = 100 - $per2; + $per1 = $per2 = floor($percent*100); + if ($per1>100) { + $per1 = 100; + $per2 = 0; + } else { + $per2 = 100 - $per2; + } $per1 .= "%"; $per2 .= "%"; - return ' +/* return '
- -
 '.$txt1.' 
 
 
'.$txt2.' '.$per1.'
'; +
 
'.$txt2.' '.$per1.'
+ ';*/ + return ' + + + + +
+ + + + +
'.$txt1.'
'.$txt2.'  '.$per1.'
'; } ?> diff --git a/lib/security.lib.php b/lib/security.lib.php index 03121c4..fca7a0f 100644 --- a/lib/security.lib.php +++ b/lib/security.lib.php @@ -87,7 +87,7 @@ class User { var $dblist; function User($userId) { $this->userId = $userId; - $sql = sprintf("SELECT User.UserId, Username, Password, Name, Email, UL, bEnabled, nBytesSoft, nBytesHard, nBytes, nDatabases, nDatabasesHard + $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 INNER JOIN UserQuota ON User.UserId = UserQuota.UserId INNER JOIN UserStat ON User.UserId = UserStat.UserId @@ -121,6 +121,9 @@ 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']; @@ -195,6 +198,7 @@ class User { mysql_escape_string($this->getUserId())); // $r = fetchRows(DBSelect($sql),'DatabaseId'); $r = fetchRows(DBSelect($sql),'Name'); + ksort($r); return $r; } } diff --git a/main.php b/main.php index 6d0e19b..90bf2ea 100644 --- a/main.php +++ b/main.php @@ -10,10 +10,11 @@ $User = new User($Login->getUserID()); if (isPost()) { if (isset($i_newdb)) { $dbname = $User->getUsername().DELIMETER.$i_newdb; - if ($User->addDB($dbname)==false) { + if ($User->isOverQuota()) { + $err[] = 'You are over your quota. You may not add databases.'; + } elseif ($User->addDB($dbname)==false) { $err[] = mysql_error(); } else { - $User->refresh(); $msg[] = 'Database `'.$dbname.'` created.'; } } @@ -26,9 +27,9 @@ if (isPost()) { $err[] = mysql_error(); } } - if (!count($err)) - $User->refresh(); } + if (!count($err)) + $User->refresh(); } $myDBs = $User->getDBList(); diff --git a/mitsql.cfg.php b/mitsql.cfg.php index 0d37ea8..79c60fa 100755 --- a/mitsql.cfg.php +++ b/mitsql.cfg.php @@ -27,7 +27,7 @@ defined('ADMINPASS') || define('ADMINPASS', base64_decode('TXlCZWF0c1Bvc3RA')); defined('ADMINDB') || define('ADMINDB', 'mitsql'); $BASE_PATH = dirname(__FILE__).'/'; -$BASE_URL = BASE_URL; +$BASE_URL = 'http://'.$_SERVER['SERVER_NAME'].BASE_URL; //$BASE_URL = isset($_SERVER['SCRIPT_NAME'])?dirname($_SERVER['SCRIPT_NAME']).'/':''; set_time_limit(0); diff --git a/test.php b/test.php index 6109019..7876ba8 100755 --- a/test.php +++ b/test.php @@ -20,19 +20,6 @@ function getManagedDBs($owner=null) { return $owners; } -function checkQuotas() { - $databases = fetchRows(DBSelect('SELECT DatabaseId,Name FROM DB WHERE bEnabled=1'),'Name'); - foreach($databases as $db) { - $DBId = $db['DatabaseId']; - $arr['dLastCheck'] = 'NOW()'; - $arr['nBytes'] = calcDBSize($db['Name']); - $sql = sprintf("UPDATE DB %s WHERE DatabaseId = '%s'", - buildSQLSet($arr), - mysql_escape_string($DBId)); - DBUpdate($sql); - } -} - /* $g = getManagedDBs(); print_r($g); diff --git a/tpl/head.php b/tpl/head.php index ee20655..1341f61 100644 --- a/tpl/head.php +++ b/tpl/head.php @@ -4,10 +4,11 @@ MIT SQL Services for Athena +
-
MIT SQL ServicesContact/Help
+
MIT SQL ServicesContact/Help
diff --git a/tpl/main.php b/tpl/main.php index be19499..2c019d4 100644 --- a/tpl/main.php +++ b/tpl/main.php @@ -27,20 +27,25 @@ if (isset($i_dropask)) { $total = $bytes['nBytesHard']; foreach($myDBs as $db) { echo ''; - $usage += $db['nBytes']; if ($total>0) $percentage = $db['nBytes']/$total; else $percentage = 0; - echo printBar($percentage, $db['Name'], sprintSize($db['nBytes'])); + echo printBar($percentage, $db['Name'], str_replace(' ', ' ', sprintSize($db['nBytes']))); echo ''; echo ''; echo ''; } - if ($total>0) + if ($total>0) { $percentage = $usage/$total; - else + } else { $percentage = 0; - echo printBar($percentage, 'TOTAL', sprintSize($usage).' / '.sprintSize($total)); + } + echo ''; + echo '
'; + echo ''; + echo ''; + echo printBar($percentage, 'TOTAL USED', str_replace(' ', ' ', sprintSize($usage).' of '.sprintSize($total))); + echo ''; ?> diff --git a/tpl/menu.php b/tpl/menu.php index 1ab1ded..177f8f9 100644 --- a/tpl/menu.php +++ b/tpl/menu.php @@ -1,24 +1,25 @@
-Main | -Logout +Main | +Refresh | + +Admin | + +Logout
  getUsername(); if (isImpersonating()) { - $loggedInText = ''.$loggedInText.''; + $loggedInText = ''.$loggedInText.''; } ?> Logged in as on
-Home | - -Admin | - -Signup | -Login via SQL or SSL +Home | +Signup | +Login via SQL or SSL
 
-- 2.45.0