]> andersk Git - sql-web.git/commitdiff
git-svn-id: svn://presbrey.mit.edu/php/php@97 a142d4bd-2cfb-0310-9673-cb33a7e74f58
authorJoe Presbrey <presbrey@mit.edu>
Mon, 3 Apr 2006 03:43:18 +0000 (03:43 +0000)
committerJoe Presbrey <presbrey@mit.edu>
Mon, 3 Apr 2006 03:43:18 +0000 (03:43 +0000)
lib/joe/dbaccess.lib.php [new file with mode: 0644]
lib/joe/errors.lib.php [new file with mode: 0644]
lib/joe/joe.lib.php

diff --git a/lib/joe/dbaccess.lib.php b/lib/joe/dbaccess.lib.php
new file mode 100644 (file)
index 0000000..76a3a34
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+
+require_once('joe.lib.php');
+
+function DBMaster($sql) {
+       sessTime($sql);
+       $res = mysql_query($sql);
+       sessTime();
+       return $res;
+}
+function DBSlave($sql) {
+       sessTime($sql);
+       $res = mysql_query($sql);
+       sessTime();
+       if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR);
+       return $res;
+}
+
+function DBSelect($sql) { return DBSlave($sql); }
+function DBInsert($sql) {
+       DBMaster($sql);
+       if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR);
+       return mysql_insert_id();
+}
+function DBUpdate($sql) { DBInsert($sql); }
+function DBDelete($sql) { DBInsert($sql); }
+function DBCreate($sql) { DBMaster($sql); }
+function DBDrop($sql) { DBMaster($sql); }
+function DBGrant($sql) { DBInsert($sql); }
+function DBRevoke($sql) { DBInsert($sql); }
+function DBSet($sql) { DBInsert($sql); }
+function DBShow($sql) { return DBSlave($sql); }
+
+function calcDBSize($tdb) {
+       $sql_result = "SHOW DATABASES LIKE '".mysql_escape_string($tdb)."'";
+       $result = DBShow($sql_result);
+       if (!mysql_num_rows($result)) return null;
+       
+   $sql_result = "SHOW TABLE STATUS FROM `" .mysql_escape_string($tdb)."`";
+   $result = DBShow($sql_result);
+
+   if($result) {
+       $size = 0;
+       while ($data = mysql_fetch_array($result)) {
+             $size += $data["Data_length"] + $data["Index_length"];
+       }
+       mysql_free_result($result);
+       return $size;
+   }
+   else {
+       return null;
+   }
+}
+
+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);
+       $sql = "UPDATE UserStat SET nDatabases = (
+                               SELECT COUNT(*)
+                               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/joe/errors.lib.php b/lib/joe/errors.lib.php
new file mode 100644 (file)
index 0000000..6f760fc
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+
+function ErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
+       $error_halt = true;
+       $error_type = 'Error';
+       $error_msg = " $errstr occured in $errfile on $errline at ".date("D M j G:i:s T Y");
+       $email_to = 'sql@mit.edu';
+       $email_from = 'sql@sql.mit.edu';
+       switch($errno) {
+               case E_USER_NOTICE:
+               case E_NOTICE:
+                       $type = 'Notice';
+                       $error_halt = false;
+                       break;
+               case E_USER_WARNING:
+               case E_COMPILE_WARNING:
+               case E_CORE_WARNING:
+               case E_WARNING:
+                       $type = 'Warning';
+                       $error_halt = false;
+                       break;
+               case E_USER_ERROR:
+               case E_COMPILE_ERROR:
+               case E_CORE_ERROR:
+               case E_ERROR:
+                       $type = 'Fatal Error';
+                       break;
+               case E_PARSE:
+                       $type = 'Parse Error';
+                       break;
+               default:
+                       $type = 'Unknown Error';
+                       break;
+       }
+       $error_bt = ErrorBacktrace(debug_backtrace());
+       $error_msg = $type . ':' . $error_msg . "\n" . $error_bt . "\n\n";
+       if (DEVEL && 0) {
+               echo nl2br($error_msg);
+       } else {
+               $error_msg .= print_r(get_included_files(),1)."\n";
+               $error_msg .= print_r($errcontext,1);
+               error_log($error_msg, 1, $email_to);
+               if ($error_halt) {
+                       while(ob_get_level()) { ob_end_clean(); }
+                       require_once('security.lib.php');
+                       redirect('error');
+                       exit -1;
+               }
+       }
+}
+
+function ErrorBacktrace($debug_backtrace) {
+       array_shift($debug_backtrace);
+       $output = '';
+   foreach ($debug_backtrace as $bt) {
+       $args = '';
+       foreach ($bt['args'] as $a) {
+           if (!empty($args)) {
+               $args .= ', ';
+           }
+           switch (gettype($a)) {
+           case 'integer':
+           case 'double':
+               $args .= $a;
+               break;
+           case 'string':
+               $a = htmlspecialchars(substr($a, 0, 64)).((strlen($a) > 64) ? '...' : '');
+               $args .= "\"$a\"";
+               break;
+           case 'array':
+               //$args .= 'Array('.count($a).')';
+               $args .= print_r($a,1);
+               break;
+           case 'object':
+               $args .= 'Object('.get_class($a).')';
+               break;
+           case 'resource':
+               $args .= 'Resource('.strstr($a, '#').')';
+               break;
+           case 'boolean':
+               $args .= $a ? 'True' : 'False';
+               break;
+           case 'NULL':
+               $args .= 'Null';
+               break;
+           default:
+               $args .= 'Unknown';
+           }
+       }
+          empty($bt['class']) && $bt['class'] = '';
+          empty($bt['type']) && $bt['type'] = '';
+          empty($bt['function']) && $bt['function'] = '';
+          $output .= "\n";
+       $output .= "file: {$bt['line']} - {$bt['file']}\n";
+       $output .= "call: {$bt['class']}{$bt['type']}{$bt['function']}($args)\n";
+   }
+       return $output;
+}
+
+set_error_handler('ErrorHandler');
+
+?>
index 26723218b3064bf77c1b6051e6c84ba15c04b21e..0bf1fb034a21da6178ebc1383827eb6597b024d3 100644 (file)
@@ -3,28 +3,29 @@
     (c) 2005 Joe Presbrey
     joepresbrey@gmail.com
 
-    ATTN:  This library was compiled and completed in its entirety independent of
-    any and all corporate projects and/or work environment.
+    ATTN:  This library was assembled and completed in its entirety independent of
+    any and all corporate projects and/or work environ.
 
     You may NOT use this library elsewhere!
 
 */
 
-function isFormPost() {
+function isPost() {
   if($_SERVER['REQUEST_METHOD'] == 'POST') {
     return true;
   } else {
     return false;
   }
 }
+function isFormPost() { return isPost(); }
 
 function isSess($id) {
   return isset($_SESSION[$id]);
 }
 
-function sess($id,$val=NULL) {
+function sess($id,$val=null) {
   if (is_null($val)) {
-    return (isSess($id)?$_SESSION[$id]:NULL);
+    return (isSess($id)?$_SESSION[$id]:null);
   } elseif (empty($val)) {
     unset($_SESSION[$id]);
   } else {
@@ -45,28 +46,30 @@ function stopSess() {
   @session_destroy();
 
   foreach($sid as $id) {
-    @unlink(SESSIONS_PATH.'/sess_'.$id);
+    @unlink(session_save_path().'/sess_'.$id);
   }
 }
 
 function sessTime($query=null) {
+  global $timingc;
   global $timings;
 
   if(!isset($timings)) {
     $timings = array();
   }
 
-  if (!isSess('TIMING')) {
-    sess('TIMING', '1');    
+  if (!isset($timingc) || empty($timingc)) {
+       $timingc = 1;
   } elseif (!is_null($query)) {
-    $current = sess('TIMING');
-    $current++;
-    sess('TIMING', $current);
+    $current = $timingc;
+       $timingc = ++$current;
   }
-  $key = sess('TIMING');
+  $key = $timingc;
 
   if (is_null($query)) {
     $timings[$key]['time'] = microtime(true)-$timings[$key]['time'];
+       if (mysql_error())
+               $timings[$key]['error'] = mysql_error();
     return true;
   } else {
     $timings[$key] = array();
@@ -104,17 +107,24 @@ function fetchRows($rs, $key = null) {
     }
 }
 
-function printErrors($errArray) {
-    if (isset($errArray) && count($errArray)) {
-        echo '<ul style="color:red;">';
-        foreach($errArray as $err) {
-            echo '<li style="color:red;"><p>',$err,'</p></li>';
+function printErrors($err) { printList('err', $err); }
+function printMsgs($err) { printList('msg', $err); }
+
+function printList($class,$err) {
+    if (is_array($err) && count($err)) {
+        echo '<div class="',$class,'">',(count($err)>1?'<ul>':'');
+        foreach($err as $e) {
+                       if (count($err)>1) {
+                               echo '<li><p>',$e,'</p></li>';
+                       } else {
+                               echo '<p>',$e,'</p>';
+                       }
         }
-        echo '</ul>';
+        echo (count($err)>1?'</ul>':''),'</div>';
     }
 }
 
-function buildSQLSet($fields, $values=NULL) {
+function buildSQLSet($fields, $values=null) {
     $ex = array('NOW()','NULL');
     $sql = 'SET';
     $c = 0;
@@ -136,7 +146,7 @@ function buildSQLSet($fields, $values=NULL) {
     return $sql;
 }
 
-function buildSQLInsert($array, $table=NULL) {
+function buildSQLInsert($array, $table=null) {
     $ex = array('NOW()','NULL');
     $sql = '(';
     $c = 0;
@@ -169,10 +179,13 @@ function build_str($query_array) {
     return join('&', $query_string);
 }
 
-function newQS($key, $val) {
+function newQS($key, $val=null) {
+    /*
     parse_str($_SERVER['QUERY_STRING'], $arr);
     $arr[$key] = $val;
     return '?'.build_str($arr);
+    */
+    return newQSA(array($key=>$val));
 }
 
 function newQSA($array=array()) {
This page took 0.043478 seconds and 5 git commands to generate.