OSDN Git Service

Continued enhancement of the sql_* api and handler files. Moved startUpError() from...
authorshizuki <shizuki@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Thu, 25 Jun 2009 03:49:32 +0000 (03:49 +0000)
committershizuki <shizuki@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Thu, 25 Jun 2009 03:49:32 +0000 (03:49 +0000)
git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/nucleus-jp/trunk@1019 1ca29b6e-896d-4ea0-84a5-967f57386b96

utf8/install.php
utf8/nucleus/libs/globalfunctions.php
utf8/nucleus/libs/sql/mysql.php
utf8/nucleus/libs/sql/pdo.php

index 7060b23..0da4ff3 100755 (executable)
@@ -73,11 +73,13 @@ if (phpversion() >= '4.1.0') {
 // include core classes that are needed for login & plugin handling\r
 // added for 3.5 sql_* wrapper\r
 global $MYSQL_HANDLER;\r
+//set the handler if different from mysql (or mysqli)\r
+//$MYSQL_HANDLER = array('pdo','mysql');\r
 if (!isset($MYSQL_HANDLER))\r
        $MYSQL_HANDLER = array('mysql','');\r
-include_once(str_replace("install.php", "", __FILE__) . 'nucleus/libs/sql/'.$MYSQL_HANDLER[0].'.php');\r
+include_once('nucleus/libs/sql/'.$MYSQL_HANDLER[0].'.php');\r
 // end new for 3.5 sql_* wrapper\r
-include_once(str_replace("install.php", "", __FILE__) . '/nucleus/libs/mysql.php');\r
+include_once('nucleus/libs/mysql.php');\r
 \r
 // check if mysql support is installed\r
 // this check may not make sense, as is, in a version past 3.5x\r
@@ -170,13 +172,14 @@ function showInstallForm() {
 \r
 <?php\r
        // note: this piece of code is taken from phpMyAdmin\r
-       $result = @sql_query('SELECT VERSION() AS version');\r
+       $conn   = sql_connect_args('localhost','','');\r
+       $result = @sql_query('SELECT VERSION() AS version', $conn);\r
 \r
        if ($result != FALSE && @sql_num_rows($result) > 0) {\r
                $row   = sql_fetch_array($result);\r
                $match = explode('.', $row['version']);\r
        } else {\r
-               $result = @sql_query('SHOW VARIABLES LIKE \'version\'');\r
+               $result = @sql_query('SHOW VARIABLES LIKE \'version\'', $conn);\r
 \r
                if ($result != FALSE && @sql_num_rows($result) > 0) {\r
                        $row   = sql_fetch_row($result);\r
@@ -195,8 +198,9 @@ function showInstallForm() {
                }\r
        }\r
 \r
+       sql_disconnect($conn);\r
        $mysqlVersion = implode($match, '.');\r
-       $minVersion = '3.23';\r
+       $minVersion   = '3.23';\r
 \r
        if ($mysqlVersion == '0.0.0') {\r
                echo _NOTIFICATION1;\r
index 7490c21..c3be4b3 100755 (executable)
@@ -624,8 +624,9 @@ function sendContentType($contenttype, $pagetype = '', $charset = _CHARSET) {
 }\r
 \r
 /**\r
- * Errors before the database connection has been made\r
+ * Errors before the database connection has been made - moved to\r
  */\r
+/* moved to $DIR_LIBS/sql/*.php handler files\r
 function startUpError($msg, $title) {\r
        if (!defined('_CHARSET')) define('_CHARSET', 'iso-8859-1');\r
        header('Content-Type: text/html; charset=' . _CHARSET);\r
@@ -639,7 +640,7 @@ function startUpError($msg, $title) {
                </body>\r
        </html>\r
        <?php   exit;\r
-}\r
+}*/\r
 \r
 /**\r
   * disconnects from SQL server\r
index 58ca3b1..3338d54 100644 (file)
  *
  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
  */
+
+
 $MYSQL_CONN = 0;
 
 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
 {
        /**
+        *Errors before the database connection has been made
+        */
+       function startUpError($msg, $title) {
+               ?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+       <head><title><?php echo htmlspecialchars($title)?></title></head>
+       <body>
+               <h1><?php echo htmlspecialchars($title)?></h1>
+               <?php echo $msg?>
+       </body>
+</html>
+<?php
+               exit;
+       }
+
+       /**
          * Connects to mysql server with arguments
          */
-       function sql_connect_args($mysql_host, $mysql_user, $mysql_password) {
+       function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
                
-               $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password); 
+               $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
+               if ($mysql_database) mysql_select_db($mysql_database,$CONN);
 
                return $CONN;
        }
@@ -53,7 +71,7 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
                if ($mySqlVer >= '5.0.7' && phpversion() >= '5.2.3') {
                        mysql_set_charset($charset);
                } elseif ($mySqlVer >= '4.1.0') {
-                       sql_query("SET NAMES " . $charset);
+                       sql_query("SET CHARACTER SET " . $charset);
                }
 // </add for garble measure>*/
 
@@ -63,60 +81,67 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * disconnects from SQL server
          */
-       function sql_disconnect() {
+       function sql_disconnect($conn = false) {
                global $MYSQL_CONN;
-               @mysql_close($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               @mysql_close($conn);
        }
        
-       function sql_close() {
+       function sql_close($conn = false) {
                global $MYSQL_CONN;
-               @mysql_close($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               @mysql_close($conn);
        }
        
        /**
          * executes an SQL query
          */
-       function sql_query($query) {
+       function sql_query($query, $conn = false) {
                global $SQLCount,$MYSQL_CONN;
+               if (!$conn) $conn = $MYSQL_CONN;
                $SQLCount++;
-               $res = mysql_query($query,$MYSQL_CONN) or print("mySQL error with query $query: " . mysql_error() . '<p />');
+               $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
                return $res;
        }
        
        /**
          * executes an SQL error
          */
-       function sql_error()
+       function sql_error($conn = false)
        {
                global $MYSQL_CONN;
-               return mysql_error($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_error($conn);
        }
        
        /**
          * executes an SQL db select
          */
-       function sql_select_db($db)
+       function sql_select_db($db,$conn = false)
        {
                global $MYSQL_CONN;
-               return mysql_select_db($db,$MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_select_db($db,$conn);
        }
        
        /**
          * executes an SQL real escape 
          */
-       function sql_real_escape_string($val)
+       function sql_real_escape_string($val,$conn = false)
        {
                global $MYSQL_CONN;
-               return mysql_real_escape_string($val,$MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_real_escape_string($val,$conn);
        }
        
        /**
          * executes an SQL insert id
          */
-       function sql_insert_id()
+       function sql_insert_id($conn = false)
        {
                global $MYSQL_CONN;
-               return mysql_insert_id($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_insert_id($conn);
        }
        
        /**
@@ -202,19 +227,21 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * Get current system status (returns string)
          */
-       function sql_stat()
+       function sql_stat($conn=false)
        {
                global $MYSQL_CONN;
-               return mysql_stat();
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_stat($conn);
        }
        
        /**
          * Returns the name of the character set
          */
-       function sql_client_encoding()
+       function sql_client_encoding($conn=false)
        {
                global $MYSQL_CONN;
-               return mysql_client_encoding();
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_client_encoding($conn);
        }
        
        /**
@@ -228,28 +255,31 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * Get SQL server version
          */
-       function sql_get_server_info()
+       function sql_get_server_info($conn=false)
        {
                global $MYSQL_CONN;
-               return mysql_get_server_info($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_get_server_info($conn);
        }
        
        /**
          * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
          */
-       function sql_get_host_info()
+       function sql_get_host_info($conn=false)
        {
                global $MYSQL_CONN;
-               return mysql_get_host_info($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_get_host_info($conn);
        }
        
        /**
          * Returns the SQL protocol on success, or FALSE on failure. 
          */
-       function sql_get_proto_info()
+       function sql_get_proto_info($conn=false)
        {
                global $MYSQL_CONN;
-               return mysql_get_proto_info($MYSQL_CONN);
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_get_proto_info($conn);
        }
 
 }
index 2447f0d..8e21424 100644 (file)
  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
  */
  
+
 $MYSQL_CONN = 0;
 global $SQL_DBH;
 $SQL_DBH = NULL;
 
-if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
+if (!function_exists('sql_fetch_assoc'))
 {
        /**
+        * Errors before the database connection has been made
+        */
+       function startUpError($msg, $title) {
+               ?>
+               <html xmlns="http://www.w3.org/1999/xhtml">
+                       <head><title><?php echo htmlspecialchars($title)?></title></head>
+                       <body>
+                               <h1><?php echo htmlspecialchars($title)?></h1>
+                               <?php echo $msg?>
+                       </body>
+               </html>
+               <?php   exit;
+       }
+       
+       /**
          * Connects to mysql server
          */
-       function sql_connect_args($mysql_host, $mysql_user, $mysql_password) {
+       function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
                global $MYSQL_HANDLER;
                
                try {
@@ -47,14 +63,14 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
                                        $port = '';
                        }
                        
-                       $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port, $mysql_user, $mysql_password);
+                       $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
                                                
                } catch (PDOException $e) {
+                       $DBH =NULL;
                        startUpError('<p>Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
                }
-               
+//echo '<hr />DBH: '.print_r($DBH,true).'<hr />';              
                return $DBH;
-
        }
        
        /**
@@ -62,7 +78,7 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
          */
        function sql_connect() {
                global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
-               
+               $SQL_DBH = NULL;
                try {
                        if (strpos($MYSQL_HOST,':') === false) {
                                $host = $MYSQL_HOST;
@@ -77,49 +93,56 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
                        }
                        
                        $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
-                                               
+/*/ <add for garble measure>
+                       if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
+                               $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
+                               $resource->bindColumn('Value', $charset);
+                               $resource->fetchAll();
+                               $SQL_DBH->exec("SET CHARACTER SET " . $charset);
+                       }
+// </add for garble measure>*/
                } catch (PDOException $e) {
+                       $SQL_DBH = NULL;
                        startUpError('<p>Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
                }
-               
-/*/ <add for garble measure>
-               $resource = sql_query("show variables LIKE 'character_set_database'");
-               $fetchDat = sql_fetch_assoc($resource);
-               $charset  = $fetchDat['Value'];
-               $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info($MYSQL_CONN))));
-               if ($mySqlVer >= '5.0.7' && phpversion() >= '5.2.3') {
-                       mysql_set_charset($charset);
-               } elseif ($mySqlVer >= '4.1.0') {
-                       sql_query("SET NAMES " . $charset);
-               }
-// </add for garble measure>*/
-
-               return $MYSQL_CONN;
+//             echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';             
+               $MYSQL_CONN &= $SQL_DBH;
+               return $SQL_DBH;
 
        }
 
        /**
          * disconnects from SQL server
          */
-       function sql_disconnect() {
+       function sql_disconnect(&$dbh=NULL) {
                global $SQL_DBH;
-               $SQL_DBH = NULL;
+               if (is_null($dbh)) $SQL_DBH = NULL;
+               else $dbh = NULL;
        }
        
-       function sql_close() {
+       function sql_close(&$dbh=NULL) {
                global $SQL_DBH;
-               $SQL_DBH = NULL;
+               if (is_null($dbh)) $SQL_DBH = NULL;
+               else $dbh = NULL;
        }
        
        /**
          * executes an SQL query
          */
-       function sql_query($query) {
+       function sql_query($query,$dbh=NULL) {
                global $SQLCount,$SQL_DBH;
                $SQLCount++;
-               $res = $SQL_DBH->query($query);
+//echo '<hr />SQL_DBH: ';
+//print_r($SQL_DBH);
+//echo '<hr />DBH: ';
+//print_r($dbh);
+//echo '<hr />';
+//echo $query.'<hr />';
+               if (is_null($dbh)) $res = $SQL_DBH->query($query);
+               else $res = $dbh->query($query);
                if ($res->errorCode() != '00000') {
                        $errors = $res->errorInfo();
+                       print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
                }
                
                return $res;
@@ -128,35 +151,48 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * executes an SQL error
          */
-       function sql_error()
+       function sql_error($dbh=NULL)
        {
-               return '';
+               global $SQL_DBH;
+               if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
+               else $error = $dbh->errorInfo();
+               if ($error[0] != '00000') {
+                       return $error[0].'-'.$error[1].' '.$error[2];
+               }
+               else return '';
        }
        
        /**
          * executes an SQL db select
          */
-       function sql_select_db($db)
+       function sql_select_db($db,&$dbh=NULL)
        {
                global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
-               $SQL_DBH = NULL;
-               try {
-                       list($host,$port) = explode(":",$MYSQL_HOST);
-                       if (isset($port)) 
-                               $port = ';port='.trim($port);
-                       else
-                               $port = '';
-                       $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
-                       
-               } catch (PDOException $e) {
-                       startUpError('<p>Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
+               if (is_null($dbh)) { 
+                       try {
+                               $SQL_DBH = NULL;
+                               list($host,$port) = explode(":",$MYSQL_HOST);
+                               if (isset($port)) 
+                                       $port = ';port='.trim($port);
+                               else
+                                       $port = '';
+                               $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
+                               return 1;
+                       } catch (PDOException $e) {
+                               startUpError('<p>Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
+                               return 0;
+                       }
+               }
+               else {
+                       if ($dbh->exec("USE $db") !== false) return 1;
+                       else return 0;
                }
        }
        
        /**
          * executes an SQL real escape 
          */
-       function sql_real_escape_string($val)
+       function sql_real_escape_string($val,$dbh=NULL)
        {
                return addslashes($val);
        }
@@ -164,11 +200,13 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * executes an SQL insert id
          */
-       function sql_insert_id()
+       function sql_insert_id($dbh=NULL)
        {       
                global $SQL_DBH;
-               
-               return $SQL_DBH->lastInsertId();
+               if (is_null($dbh))
+                       return $SQL_DBH->lastInsertId();
+               else
+                       return $dbh->lastInsertId();
        }
        
        /**
@@ -280,19 +318,27 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * Get current system status (returns string)
          */
-       function sql_stat()
+       function sql_stat($dbh=NULL)
        {
                //not implemented
+               global $SQL_DBH;
+               if (is_null($dbh))
+                       return '';
+               else
                return '';
        }
        
        /**
          * Returns the name of the character set
          */
-       function sql_client_encoding()
+       function sql_client_encoding($dbh=NULL)
        {
                //not implemented
-               return '';
+               global $SQL_DBH;
+               if (is_null($dbh))
+                       return '';
+               else
+                       return '';
        }
        
        /**
@@ -307,27 +353,38 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        /**
          * Get SQL server version
          */
-       function sql_get_server_info()
+       function sql_get_server_info($dbh=NULL)
        {
                global $SQL_DBH;
-               return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
+               if (is_null($dbh))
+                       return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
+               else
+                       return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
        }
        
        /**
          * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
          */
-       function sql_get_host_info()
+       function sql_get_host_info($dbh=NULL)
        {
                global $SQL_DBH;
-               return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
+               if (is_null($dbh))
+                       return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
+               else
+                       return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
        }
        
        /**
          * Returns the SQL protocol on success, or FALSE on failure. 
          */
-       function sql_get_proto_info()
+       function sql_get_proto_info($dbh=NULL)
        {
                //not implemented
+               global $SQL_DBH;
+               if (is_null($dbh))
+                       return false;
+               else
+                       return false;
                return '';
        }