OSDN Git Service

Refering to http://sourceforge.jp/projects/nucleus-jp/lists/archive/developers/2011...
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / sql / mysql.php
old mode 100644 (file)
new mode 100755 (executable)
index a3d5a55..8ae98b7
@@ -2,7 +2,7 @@
 
 /*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2007 The Nucleus Group
+ * Copyright (C) 2002-2011 The Nucleus Group
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -12,8 +12,8 @@
  */
 /**
  * @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2007 The Nucleus Group
- * @version $Id: mysql.php 1279 2008-10-23 08:18:26Z shizuki $
+ * @copyright Copyright (C) 2002-2011 The Nucleus Group
+ * @version $Id$
  */
  
 /*
@@ -31,12 +31,20 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
         *Errors before the database connection has been made
         */
        function startUpError($msg, $title) {
+               if (!defined('_CHARSET')) {
+                       define('_CHARSET', 'UTF-8');
+               }
+               if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
+                       define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
+               }
+               sendContentType('text/html','',_CHARSET);
                ?>
-<html xmlns="http://www.w3.org/1999/xhtml">
-       <head><title><?php echo htmlspecialchars($title)?></title></head>
+<html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
+       <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
+       <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
        <body>
-               <h1><?php echo htmlspecialchars($title)?></h1>
-               <?php echo $msg?>
+               <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
+               <?php echo $msg?> 
        </body>
 </html>
 <?php
@@ -63,16 +71,18 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
                $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
                mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</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 CHARACTER SET " . $charset);
+// <add for garble measure>
+               if (defined(_CHARSET)){
+                       $charset  = _CHARSET;
+               }else{
+                       $resource = sql_query("show variables LIKE 'character_set_database'");
+                       $fetchDat = sql_fetch_assoc($resource);
+                       $charset  = $fetchDat['Value'];
+                       // in trouble of encoding,uncomment the following line.
+                       // $charset = "ujis";
+                       // $charset = "utf8";
                }
+               sql_set_charset_jp($charset);
 // </add for garble measure>*/
 
                return $MYSQL_CONN;
@@ -170,19 +180,21 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
        }
        
        /**
-         * returns number of rows in SQL result
-         */
+        * returns number of rows in SQL result
+        */
        function sql_num_rows($res)
        {
                return mysql_num_rows($res);
        }
        
        /**
-         * returns number of rows affected by SQL query
-         */
-       function sql_affected_rows($res)
+        * returns number of rows affected by SQL query
+        */
+       function sql_affected_rows($conn = false)
        {
-               return mysql_affected_rows($res);
+               global $MYSQL_CONN;
+               if (!$conn) $conn = $MYSQL_CONN;
+               return mysql_affected_rows($conn);
        }
        
        /**
@@ -291,39 +303,90 @@ if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
                return mysql_get_proto_info($conn);
        }
 
-}
+       /**
+        * Get the name of the specified field in a result
+        */
+       function sql_field_name($res, $offset = 0)
+       {
+               return mysql_field_name($res, $offset);
+       }
 
 /**************************************************************************
        Unimplemented mysql_* functions
        
-# mysql_ change_ user
-# mysql_ create_ db
-# mysql_ data_ seek
-# mysql_ db_ name
-# mysql_ db_ query
-# mysql_ drop_ db
-# mysql_ errno
-# mysql_ escape_ string
-# mysql_ fetch_ lengths
-# mysql_ field_ flags
-# mysql_ field_ len
-# mysql_ field_ name
-# mysql_ field_ seek
-# mysql_ field_ table
-# mysql_ field_ type
-# mysql_ info
-# mysql_ list_ dbs
-# mysql_ list_ fields
-# mysql_ list_ processes
-# mysql_ list_ tables
-# mysql_ pconnect
-# mysql_ ping
-# mysql_ set_ charset
-# mysql_ tablename
-# mysql_ thread_ id
-# mysql_ unbuffered_ query
-*******************************************************************/
+# mysql_ data_ seek (maybe useful)
+# mysql_ errno (maybe useful)
+# mysql_ fetch_ lengths (maybe useful)
+# mysql_ field_ flags (maybe useful)
+# mysql_ field_ len (maybe useful)
+# mysql_ field_ seek (maybe useful)
+# mysql_ field_ table (maybe useful)
+# mysql_ field_ type (maybe useful)
+# mysql_ info (maybe useful)
+# mysql_ list_ processes (maybe useful)
+# mysql_ ping (maybe useful)
+# mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
+# mysql_ thread_ id (maybe useful)
+
+# mysql_ db_ name (useful only if working on multiple dbs which we do not do)
+# mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
 
+# mysql_ pconnect (probably not useful and could cause some unintended performance issues)
+# mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
 
+# mysql_ change_ user (deprecated)
+# mysql_ create_ db (deprecated)
+# mysql_ db_ query (deprecated)
+# mysql_ drop_ db (deprecated)
+# mysql_ escape_ string (deprecated)
+# mysql_ list_ fields (deprecated)
+# mysql_ list_ tables (deprecated)
+# mysql_ tablename (deprecated)
+
+*******************************************************************/
 
-?>
\ No newline at end of file
+       /*
+        * for preventing I/O strings from auto-detecting the charactor encodings by MySQL
+        * since 3.62_beta-jp
+        * Jan.20, 2011 by kotorisan and cacher
+        * refering to their conversation below,
+        * http://japan.nucleuscms.org/bb/viewtopic.php?p=26581
+        * 
+        * NOTE:        shift_jis is only supported for output. Using shift_jis in DB is prohibited.
+        * NOTE:        iso-8859-x,windows-125x if _CHARSET is unset.
+        */
+       function sql_set_charset_jp($charset,$mode = NULL) {
+               switch(strtolower($charset)){\r
+                       case 'utf-8':
+                       case 'utf8':\r
+                               $charset = 'utf8';\r
+                               break;\r
+                       case 'euc-jp':
+                       case 'ujis':\r
+                               $charset = 'ujis';\r
+                               break;\r
+                       case 'gb2312':\r
+                               $charset = 'gb2312';\r
+                               break;
+                       /*\r
+                       case 'shift_jis':
+                       case 'sjis':\r
+                               $charset = 'sjis';\r
+                               break;
+                       */\r
+                       default:
+                               $charset = 'latin1';\r
+                               break;\r
+               }
+               $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info())));
+               if (version_compare($mySqlVer, '5.0.7', '>=') && function_exists('mysql_set_charset')) {
+                       $res = mysql_set_charset($charset);
+               } elseif (version_compare($mySqlVer, '4.1.0', '>=')) {
+                       $res = ($mode == 'NAMES')
+                               ?sql_query("SET NAMES " . $charset)
+                               :sql_query("SET CHARACTER SET " . $charset)
+                       ;
+               }
+               return $res;
+       }
+}
\ No newline at end of file