From: Joe Presbrey Date: Sun, 10 Jul 2005 23:51:07 +0000 (+0000) Subject: git-svn-id: svn://presbrey.mit.edu/sql/mitsql@6 a142d4bd-2cfb-0310-9673-cb33a7e74f58 X-Git-Url: http://andersk.mit.edu/gitweb/sql-web.git/commitdiff_plain/b43ab1a6f74ea71f3a2388d05ab4843f8277349b git-svn-id: svn://presbrey.mit.edu/sql/mitsql@6 a142d4bd-2cfb-0310-9673-cb33a7e74f58 --- b43ab1a6f74ea71f3a2388d05ab4843f8277349b diff --git a/admin/index.php b/admin/index.php new file mode 100755 index 0000000..7af9251 --- /dev/null +++ b/admin/index.php @@ -0,0 +1,41 @@ +fullDBList(); +ksort($dblist); + +echo ''; +$dba->printDBs($dblist); +echo '
'; + +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100755 index 0000000..1cdf976 --- /dev/null +++ b/index.php @@ -0,0 +1,187 @@ +Your SSL certificate could not be verified, please authenticate manually below.

'; +// echo '
'; + echo '

(This will be fixed when I do group locker SQL support)

'; + } else { + $_SESSION['owner'] = getSSLIdent(); + } +} +if (isset($_SESSION['realuser'])) { + $realuser = $_SESSION['realuser']; +} else $realuser = null; + +ob_start(); +include('style.inc'); + +if (isset($_SESSION['owner'])) { + $owner = $_SESSION['owner']; + $dbm = new DBManage($owner); +} else exit; + +?>

SQL Databases:

[ ]

',TEXT_REGPASSTOOSHORT,'

'; + } else { + if (!$dbm->isInit()) { + $dbm->init($i_initpw); + redirectLocal('/'); + } else { + echo '

Your account is already initialized.

'; + } + } + break; + case 'uninit': + echo '

This operation will remove all the databases and accounts for: ',$owner,'. Are you sure?

'; + echo '
'; + exit(); + break; + case 'uninit2': + $dbm->uninit(); + redirectLocal('/'); + break; + case 'adddb': + if ($dbm->getNumDBs() < $dbm->getNumMaxDBs()) { + $dbm->addDB($owner.DELIMETER.$i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_DBLIMIT,'

'; + } + break; + case 'deldb': + if ($dbm->isRegistered($i_name)) { + $dbm->delDB($i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_NOTREG,'

'; + } + break; + case 'createdb': + if ($dbm->isRegistered($i_name)) { + $dbm->createDB($i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_NOTREG,'

'; + } + break; + case 'dropdb': + if ($dbm->isRegistered($i_name)) { + $dbm->dropDB($i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_NOTREG,'

'; + } + break; + case 'grantdb': + if ($dbm->isRegistered($i_name)) { + $dbm->grantDB($i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_NOTREG,'

'; + } + break; + case 'revokedb': + if ($dbm->isRegistered($i_name)) { + $dbm->revokeDB($i_name); + redirectLocal('/'); + } else { + echo '

',TEXT_NOTREG,'

'; + } + break; + } + $dbm = null; + $dbm = new DBManage($owner); +} +?> + + + +getNumDBs()) { + echo ''; + $dbm->printOwnerDBs(); + echo '
'; + } + if ($dbm->isInit()) { + echo '
'; + + $dbm->printOwner(); + echo ''; + echo ''; + } else { + echo '

',TEXT_NOREG,'

'; + echo ''; + echo '
'; + echo ''; + echo ''; + echo '
your SQL server:',HOST,'
your SQL username:',$owner,'
choose a SQL password:
'; + echo ''; + echo ''; + } + echo '
'; + + @mysql_close($cxn); + + if (DEBUG) { + echo ''; + } +?> +
+ +
MIT SQL Service Management v
+Direct comments and bugs to: presbrey@mit.edu
+ diff --git a/mitsql.lib.php b/mitsql.lib.php new file mode 100755 index 0000000..fbdba3c --- /dev/null +++ b/mitsql.lib.php @@ -0,0 +1,463 @@ +username = $username; + $this->load(); + if ($this->fix()) + $this->load(); + } + } + function load() { + $rs = mysql_db_query(MANAGEDB, + sprintf(SQLSELECT1WHERE, + MANAGEOWNERTABLE, + 'name="'.mysql_escape_string($this->username).'"')); + //or exit(mysql_error()); + while($r = mysql_fetch_assoc($rs)) { + $this->r_owner = $r; + } + mysql_free_result($rs); + + $dbs = array(); + + $rs = mysql_db_query(MANAGEDB, + sprintf(SQLSELECTWHERE, + MANAGEDBTABLE, + 'owner="'.mysql_escape_string($this->username).'" ORDER BY name')) or exit(mysql_error()); + while($r = mysql_fetch_assoc($rs)) { + $t = $r; + $t['registered'] = 1; + $t['granted'] = 0; + $dbs[$r['name']] = $t; + } + mysql_free_result($rs); + + $rs = mysql_db_query('mysql', + sprintf(SQLSELECTWHERE, + MANAGEDBTABLE, + 'User="'.mysql_escape_string($this->username).'" ORDER BY Db')); + while($r = mysql_fetch_assoc($rs)) { + if (!isset($dbs[$r['Db']])) { + $dbs[$r['Db']] = array(); + $dbs[$r['Db']]['registered'] = 0; + } + $dbs[$r['Db']]['granted'] = 1; + $dbs[$r['Db']]['name'] = $r['Db']; + } + mysql_free_result($rs); + + foreach($dbs as $d1=>$d2) { + $size = getDBSize($d1); + if (is_null($size)) { + $dbs[$d1]['sizeNow'] = 0; + $dbs[$d1]['exists'] = 0; + } else { + $dbs[$d1]['sizeNow'] = $size; + $this->sizeNow += $size; + $dbs[$d1]['exists'] = 1; + } + } + $this->dbs = $dbs; + } + function isInit() { + return !is_null($this->r_owner); + } + function grantDB($dbname) { + mysql_query('GRANT ALL PRIVILEGES ON ' + .'`'.mysql_escape_string($dbname).'` . * ' + .'TO \''.mysql_escape_string($this->username).'\'@\'%\'') or exit(mysql_error()); + $this->flushPriv(); + } + function flushPriv() { + mysql_query('FLUSH PRIVILEGES') or exit(mysql_error()); + } + function revokeDB($dbname) { + mysql_db_query('mysql', + 'DELETE FROM `db` WHERE ' + .'User = \''.mysql_escape_string($this->username).'\' ' + .'AND Db = \''.mysql_escape_string($dbname).'\'') or exit(mysql_error()); + $this->flushPriv(); + } + function setPassword($password) { + mysql_query('SET PASSWORD FOR '. + '\''.mysql_escape_string($this->username).'\'@\'%\'='. + 'PASSWORD(\''.mysql_escape_string($password).'\')') or exit(mysql_error()); + } + function init($password, $maxDBs = DEFAULT_MAX_DBS, $maxSize = DEFAULT_MAX_SIZE) { + mysql_query('GRANT USAGE ON * . * TO '. + '\''.mysql_escape_string($this->username).'\'@\'%\' '. + 'IDENTIFIED BY \''.mysql_escape_string($password).'\'') or exit(mysql_error()); + $this->flushPriv(); + mysql_db_query(MANAGEDB, + sprintf(SQLINSERT, + MANAGEOWNERTABLE, + sprintf("'%s','%s','%s',NOW(),NOW()", + mysql_escape_string($this->username), + $maxDBs, + $maxSize))) or exit(mysql_error()); + } + function uninit() { + mysql_db_query('mysql', + 'DELETE FROM `user` WHERE ' + .'User = \''.mysql_escape_string($this->username).'\'') or exit(mysql_error()); + mysql_db_query('mysql', + 'DELETE FROM `db` WHERE ' + .'User = \''.mysql_escape_string($this->username).'\'') or exit(mysql_error()); + mysql_db_query('mysql', + 'DELETE FROM `tables_priv` WHERE ' + .'User = \''.mysql_escape_string($this->username).'\'') or exit(mysql_error()); + mysql_db_query('mysql', + 'DELETE FROM `columns_priv` WHERE ' + .'User = \''.mysql_escape_string($this->username).'\'') or exit(mysql_error()); + mysql_query('FLUSH PRIVILEGES') or exit(mysql_error()); + + mysql_db_query(MANAGEDB, + sprintf(SQLDELETE, + MANAGEDBTABLE, + 'owner=\''.mysql_escape_string($this->username).'\'')) or exit(mysql_error()); + + foreach($this->dbs as $db) + $this->dropDB($db['name']); + + mysql_db_query(MANAGEDB, + sprintf(SQLDELETE1, + MANAGEOWNERTABLE, + 'name=\''.mysql_escape_string($this->username).'\'')) or exit(mysql_error()); + } + function registerDB($name, $maxSize = DEFAULT_MAX_DB_SIZE) { + mysql_db_query(MANAGEDB, + sprintf(SQLINSERT, + MANAGEDBTABLE, + sprintf("'%s','%s','%s','',NOW(),NOW()", + mysql_escape_string($name), + mysql_escape_string($this->username), + $maxSize))) or exit(mysql_error()); + } + function unregisterDB($name) { + mysql_db_query(MANAGEDB, + sprintf(SQLDELETE1, + MANAGEDBTABLE, + 'name="'.mysql_escape_string($name).'"')) or exit(mysql_error()); + } + function createDB($name) { + @mysql_create_db($name); + } + function dropDB($name) { + @mysql_drop_db($name); + } + function addDB($name) { + if (!$this->isExists($name)) + $this->createDB($name); + if (!$this->isRegistered($name)) + $this->registerDB($name); + if (!$this->isGranted($name)) + $this->grantDB($name); + } + function delDB($name) { + $this->revokeDB($name); + $this->unregisterDB($name); + $this->dropDB($name); + } + function getNumDBs() { + return count($this->dbs); + } + function getTotalSize() { + return $this->sizeNow; + } + function isRegistered($name) { + if (isset($this->dbs[$name])) + return $this->dbs[$name]['registered']; + else return false; + } + function isExists($name) { + if (isset($this->dbs[$name])) + return $this->dbs[$name]['exists']; + else return false; + } + function isGranted($name) { + if (isset($this->dbs[$name])) + return $this->dbs[$name]['granted']; + else return false; + } + + function printOwnerHeader() { + echo ''; + echo '

'; + echo '

databases:

'; + echo '

storage:

'; + echo '

modified:

'; +// echo '

created:

'; + echo ''; + } + function printOwner() { + $obj_owner = $this->r_owner; + echo ''; + $this->printOwnerHeader(); + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; +// echo ''; + echo ''; + echo '

status:

',$this->getNumDBs(),'

',sprintSize($this->getTotalSize()),'

limits:

',$obj_owner['maxDB'],'

',sprintSize($obj_owner['maxSize']),'

',sprintTS($obj_owner['modified']),'

',sprintTS($obj_owner['created']),'

'; + } + function printOwnerDBs($showOwner = false) { + $rows = $this->dbs; + echo ''; + echo '

name:

'; + echo '

owner:

'; + echo '

last size:

'; + echo '

size:

'; + echo '

modified:

'; +// echo '

created:

'; + echo ''; + foreach($rows as $name=>$r) { + echo ''; + echo '

',$name,'

'; + echo '

',$r['owner'],'

'; + /* echo '

',sprintSize($r['sizeMax']),'

';*/ + echo '

',sprintSize($r['sizeLast']),'

'; + echo '

',sprintSize($r['sizeNow']),'

'; + echo '

',sprintTS($r['modified']),'

'; +// echo '

',sprintTS($r['created']),'

'; + + echo '
'; + echo ''; + echo '
'; + + echo '
'; + echo ''; + echo '
'; + + echo '
'; + echo ''; + flush(); + } + } + function fix() { + $fixed = false; + foreach($this->dbs as $db) { + if (!$db['registered']) { + echo '

',TEXT_FIXREG,$db['name'],'

'; + flush(); + $this->registerDB($db['name']); + $fixed = true; + } + } + return $fixed; + } + function getNumMaxDBs() { + return $this->r_owner['maxDB']; + } +} + +class DBAdmin { + function mysqlDBList() { + $rs = mysql_query('SHOW DATABASES'); + $dbs = array(); + while($r = mysql_fetch_row($rs)) { + $dbs[$r[0]] = array(); + $dbs[$r[0]]['registered'] = 0; + $dbs[$r[0]]['granted'] = 0; + $dbs[$r[0]]['exists'] = 1; + $dbs[$r[0]]['owner'] = ''; + $dbs[$r[0]]['sizeLast'] = 0; + $dbs[$r[0]]['sizeNow'] = ''; + $dbs[$r[0]]['modified'] = ''; + $dbs[$r[0]]['created'] = ''; + } + return $dbs; + } + function fullDBList() { + //$dbs = array(); + $dbs = $this->mysqlDBList(); + + $rs = mysql_db_query(MANAGEDB, + sprintf(SQLSELECT, + MANAGEDBTABLE)) or exit(mysql_error()); + while($r = mysql_fetch_assoc($rs)) { + $t = $r; + $t['registered'] = 1; + $t['granted'] = 0; + $dbs[$r['name']] = $t; + } + mysql_free_result($rs); + + $rs = mysql_db_query('mysql', + sprintf(SQLSELECT, + MANAGEDBTABLE)); + while($r = mysql_fetch_assoc($rs)) { + if (!isset($dbs[$r['Db']])) { + $dbs[$r['Db']] = array(); + $dbs[$r['Db']]['registered'] = 0; + } + $dbs[$r['Db']]['granted'] = 1; + $dbs[$r['Db']]['name'] = $r['Db']; + } + mysql_free_result($rs); + + foreach($dbs as $d1=>$d2) { + $size = getDBSize($d1); + if (is_null($size)) { + $dbs[$d1]['sizeNow'] = 0; + $dbs[$d1]['exists'] = 0; + } else { + $dbs[$d1]['sizeNow'] = $size; + //$this->sizeNow += $size; + $dbs[$d1]['exists'] = 1; + } + } + return $dbs; + } + function printDBs($rows) { + if (ob_get_contents()) ob_end_flush(); + echo ''; + echo '

name:

'; + echo '

owner:

'; + echo '

last size:

'; + echo '

size:

'; + echo '

modified:

'; + echo '

created:

'; + echo ''; + foreach($rows as $name=>$r) { + echo ''; + echo '

',$name,'

'; +// echo '

',$r['owner'],'

'; + echo '

',$r['owner'],'

'; + /* echo '

',sprintSize($r['sizeMax']),'

';*/ + echo '

',sprintSize($r['sizeLast']),'

'; + echo '

',sprintSize($r['sizeNow']),'

'; + echo '

',sprintTS($r['modified']),'

'; + echo '

',sprintTS($r['created']),'

'; + + echo '
';
+			echo $r['exists']?'E':'';
+			echo $r['granted']?'G':'';
+			echo $r['registered']?'R':'';
+			echo '
'; + + echo '
'; + echo ''; + echo '
'; + + echo '
'; + echo ''; + echo '
'; + + echo '
'; + echo ''; + flush(); + } + } +} diff --git a/style.inc b/style.inc new file mode 100644 index 0000000..7b62704 --- /dev/null +++ b/style.inc @@ -0,0 +1,36 @@ + \ No newline at end of file diff --git a/test.php b/test.php new file mode 100755 index 0000000..7eb69e2 --- /dev/null +++ b/test.php @@ -0,0 +1,3 @@ +