OSDN Git Service

The variable for 'defined' function need to be packed with quotation.
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / sql / pdo.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2011 The Nucleus Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  */
13 /**
14  * @license http://nucleuscms.org/license.txt GNU General Public License
15  * @copyright Copyright (C) 2002-2011 The Nucleus Group
16  * @version $Id$
17  */
18  
19 /*
20  * complete sql_* wrappers for mysql functions
21  *
22  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
23  */
24  
25
26 $MYSQL_CONN = 0;
27 global $SQL_DBH;
28 $SQL_DBH = NULL;
29
30 if (!function_exists('sql_fetch_assoc'))
31 {
32 /**
33  * Errors before the database connection has been made
34  */
35         function startUpError($msg, $title) {
36                 if (!defined('_CHARSET')) {
37                         define('_CHARSET', 'UTF-8');
38                 }
39                 if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
40                         define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
41                 }
42                 sendContentType('text/html','',_CHARSET);
43                 ?>
44 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
45         <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
46         <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
47         <body>
48                 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
49                 <?php echo $msg?> 
50         </body>
51 </html>
52 <?php
53                 exit;
54         }
55         
56 /**
57  * Connects to mysql server
58  */
59         function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
60                 global $MYSQL_HANDLER;
61                 
62                 try {
63                         if (strpos($mysql_host,':') === false) {
64                                 $host = $mysql_host;
65                                 $port = '';
66                                 $portnum = '';
67                         }
68                         else {
69                                 list($host,$port) = explode(":",$mysql_host);
70                                 if (isset($port)) {
71                                         $portnum = $port;
72                                         $port = ';port='.trim($port);
73                                 }
74                                 else {
75                                         $port = '';
76                                         $portnum = '';
77                                 }
78                         }
79                         
80                         switch ($MYSQL_HANDLER[1]) {
81                                 case 'sybase':
82                                 case 'dblib':
83                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
84                                         else $port = '';
85                                         $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
86                                 break;
87                                 case 'mssql':
88                                         if (is_numeric($portnum)) $port = ','.intval($portnum);
89                                         else $port = '';
90                                         $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
91                                 break;
92                                 case 'oci':
93                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
94                                         else $port = '';
95                                         $DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$mysql_database, $mysql_user, $mysql_password);
96                                 break;
97                                 case 'odbc':
98                                         if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
99                                         else $port = '';
100                                         $DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$mysql_database.';PROTOCOL=TCPIP;UID='.$mysql_user.';PWD='.$mysql_password);
101
102                                 break;
103                                 case 'pgsql':
104                                         if (is_numeric($portnum)) $port = ';port='.intval($portnum);
105                                         else $port = '';
106                                         $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
107                                 break;
108                                 case 'sqlite':
109                                 case 'sqlite2':
110                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
111                                         else $port = '';
112                                         $DBH = new PDO($MYSQL_HANDLER[1].':'.$mysql_database, $mysql_user, $mysql_password);
113                                 break;
114                                 default:
115                                         //mysql
116                                         $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
117                                 break;
118                         }
119         
120                         
121                                                 
122                 } catch (PDOException $e) {
123                         $DBH =NULL;
124                         startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
125                 }
126 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />';               
127                 return $DBH;
128         }
129         
130 /**
131  * Connects to mysql server
132  */
133         function sql_connect() {
134                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
135                 $SQL_DBH = NULL;
136                 try {
137                         if (strpos($MYSQL_HOST,':') === false) {
138                                 $host = $MYSQL_HOST;
139                                 $port = '';
140                         }
141                         else {
142                                 list($host,$port) = explode(":",$MYSQL_HOST);
143                                 if (isset($port)) {
144                                         $portnum = $port;
145                                         $port = ';port='.trim($port);
146                                 }
147                                 else {
148                                         $port = '';
149                                         $portnum = '';
150                                 }
151                         }
152                         
153                         switch ($MYSQL_HANDLER[1]) {
154                                 case 'sybase':
155                                 case 'dblib':
156                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
157                                         else $port = '';
158                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
159                                 break;
160                                 case 'mssql':
161                                         if (is_numeric($portnum)) $port = ','.intval($portnum);
162                                         else $port = '';
163                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
164                                 break;
165                                 case 'oci':
166                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
167                                         else $port = '';
168                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
169                                 break;
170                                 case 'odbc':
171                                         if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
172                                         else $port = '';
173                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$MYSQL_DATABASE.';PROTOCOL=TCPIP;UID='.$MYSQL_USER.';PWD='.$MYSQL_PASSWORD);
174
175                                 break;
176                                 case 'pgsql':
177                                         if (is_numeric($portnum)) $port = ';port='.intval($portnum);
178                                         else $port = '';
179                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
180                                 break;
181                                 case 'sqlite':
182                                 case 'sqlite2':
183                                         if (is_numeric($portnum)) $port = ':'.intval($portnum);
184                                         else $port = '';
185                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
186                                 break;
187                                 default:
188                                         //mysql
189                                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
190                                 break;
191                         }
192
193                         //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
194                         
195 // <add for garble measure>
196                         if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
197                                 if (defined('_CHARSET')){
198                                         $charset  = _CHARSET;
199                                 }else{
200                                         $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
201                                         $resource->bindColumn('Value', $charset);
202                                         $resource->fetchAll();
203                                         // in trouble of encoding,uncomment the following line.
204                                         // $charset = "ujis";
205                                         // $charset = "utf8";
206                                 }
207                                 sql_set_charset_jp($charset);
208                         }
209 // </add for garble measure>*/
210                 } catch (PDOException $e) {
211                         $SQL_DBH = NULL;
212                         startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
213                 }
214 //              echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';             
215                 $MYSQL_CONN &= $SQL_DBH;
216                 return $SQL_DBH;
217
218         }
219
220 /**
221  * disconnects from SQL server
222  */
223         function sql_disconnect(&$dbh=NULL) {
224                 global $SQL_DBH;
225                 if (is_null($dbh)) $SQL_DBH = NULL;
226                 else $dbh = NULL;
227         }
228         
229         function sql_close(&$dbh=NULL) {
230                 global $SQL_DBH;
231                 if (is_null($dbh)) $SQL_DBH = NULL;
232                 else $dbh = NULL;
233         }
234         
235 /**
236  * executes an SQL query
237  */
238         function sql_query($query,$dbh=NULL) {
239                 global $SQLCount,$SQL_DBH;
240                 $SQLCount++;
241 //echo '<hr />SQL_DBH: ';
242 //print_r($SQL_DBH);
243 //echo '<hr />DBH: ';
244 //print_r($dbh);
245 //echo '<hr />';
246 //echo $query.'<hr />';
247                 if (is_null($dbh)) $res = $SQL_DBH->query($query);
248                 else $res = $dbh->query($query);
249                 if ($res->errorCode() != '00000') {
250                         $errors = $res->errorInfo();
251                         print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
252                 }
253                 
254                 return $res;
255         }
256         
257 /**
258  * executes an SQL error
259  */
260         function sql_error($dbh=NULL)
261         {
262                 global $SQL_DBH;
263                 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
264                 else $error = $dbh->errorInfo();
265                 if ($error[0] != '00000') {
266                         return $error[0].'-'.$error[1].' '.$error[2];
267                 }
268                 else return '';
269         }
270         
271 /**
272  * executes an SQL db select
273  */
274         function sql_select_db($db,&$dbh=NULL)
275         {
276                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
277 //echo '<hr />'.print_r($dbh,true).'<hr />';
278 //exit;
279                 if (is_null($dbh)) { 
280                         try {
281                                 $SQL_DBH = NULL;
282                                 list($host,$port) = explode(":",$MYSQL_HOST);
283                                 if (isset($port)) {
284                                         $portnum = $port;
285                                         $port = ';port='.trim($port);
286                                 }
287                                 else {
288                                         $port = '';
289                                         $portnum = '';
290                                 }
291                                 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
292                                 //$SQL_DBH = sql_connect();
293                                 switch ($MYSQL_HANDLER[1]) {
294                                         case 'sybase':
295                                         case 'dblib':
296                                                 if (is_numeric($portnum)) $port = ':'.intval($portnum);
297                                                 else $port = '';
298                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
299                                         break;
300                                         case 'mssql':
301                                                 if (is_numeric($portnum)) $port = ','.intval($portnum);
302                                                 else $port = '';
303                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
304                                         break;
305                                         case 'oci':
306                                                 if (is_numeric($portnum)) $port = ':'.intval($portnum);
307                                                 else $port = '';
308                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
309                                         break;
310                                         case 'odbc':
311                                                 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
312                                                 else $port = '';
313                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$db.';PROTOCOL=TCPIP;UID='.$MYSQL_USER.';PWD='.$MYSQL_PASSWORD);
314
315                                         break;
316                                         case 'pgsql':
317                                                 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
318                                                 else $port = '';
319                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
320                                         break;
321                                         case 'sqlite':
322                                         case 'sqlite2':
323                                                 if (is_numeric($portnum)) $port = ':'.intval($portnum);
324                                                 else $port = '';
325                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
326                                         break;
327                                         default:
328                                                 //mysql
329                                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
330                                         break;
331                                 }
332                                 return 1;
333                         } catch (PDOException $e) {
334                                 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
335                                 return 0;
336                         }
337                 }
338                 else {
339                         if ($dbh->exec("USE $db") !== false) return 1;
340                         else return 0;
341                 }
342         }
343         
344 /**
345  * executes an SQL real escape 
346  */
347         function sql_real_escape_string($val,$dbh=NULL)
348         {
349                 return addslashes($val);
350         }
351         
352 /**
353  * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 
354  */
355         function sql_quote_string($val,$dbh=NULL) {
356                 global $SQL_DBH;
357                 if (is_null($dbh))
358                         return $SQL_DBH->quote($val);
359                 else
360                         return $dbh->quote($val);
361         }
362         
363 /**
364  * executes an SQL insert id
365  */
366         function sql_insert_id($dbh=NULL)
367         {   
368                 global $SQL_DBH;
369                 if (is_null($dbh))
370                         return $SQL_DBH->lastInsertId();
371                 else
372                         return $dbh->lastInsertId();
373         }
374         
375 /**
376  * executes an SQL result request
377  */
378         function sql_result($res, $row = 0, $col = 0)
379         {
380                 $results = array();
381                 if (intval($row) < 1) {
382                         $results = $res->fetch(PDO::FETCH_BOTH);
383                         return $results[$col];
384                 }
385                 else {
386                         for ($i = 0; $i < intval($row); $i++) {
387                                 $results = $res->fetch(PDO::FETCH_BOTH);
388                         }
389                         $results = $res->fetch(PDO::FETCH_BOTH);
390                         return $results[$col];
391                 }
392         }
393         
394 /**
395  * frees sql result resources
396  */
397         function sql_free_result($res)
398         {
399                 $res = NULL;
400                 return true;
401         }
402         
403 /**
404  * returns number of rows in SQL result
405  */
406         function sql_num_rows($res)
407         {
408                 return $res->rowCount();
409         }
410         
411 /**
412  * returns number of rows affected by SQL query
413  */
414         function sql_affected_rows($res)
415         {
416                 return $res->rowCount();
417         }
418         
419 /**
420  * Get number of fields in result
421  */
422         function sql_num_fields($res)
423         {
424                 return $res->columnCount();
425         }
426         
427 /**
428  * fetches next row of SQL result as an associative array
429  */
430         function sql_fetch_assoc($res)
431         {
432                 $results = array();
433                 $results = $res->fetch(PDO::FETCH_ASSOC);   
434                 return $results;
435         }
436         
437 /**
438  * Fetch a result row as an associative array, a numeric array, or both
439  */
440         function sql_fetch_array($res)
441         {
442                 $results = array();
443                 $results = $res->fetch(PDO::FETCH_BOTH);
444                 return $results;
445         }
446         
447 /**
448  * fetches next row of SQL result as an object
449  */
450         function sql_fetch_object($res)
451         {
452                 $results = NULL;
453                 $results = $res->fetchObject(); 
454                 return $results;
455         }
456         
457 /**
458  * Get a result row as an enumerated array
459  */
460         function sql_fetch_row($res)
461         {
462                 $results = array();
463                 $results = $res->fetch(PDO::FETCH_NUM); 
464                 return $results;
465         }
466         
467 /**
468  * Get column information from a result and return as an object
469  */
470         function sql_fetch_field($res,$offset = 0)
471         {
472                 $results = array();
473                 $obj = NULL;
474                 $results = $res->getColumnMeta($offset);
475                 foreach($results as $key=>$value) {
476                         $obj->$key = $value;
477                 }
478                 return $obj;
479         }
480         
481 /**
482  * Get current system status (returns string)
483  */
484         function sql_stat($dbh=NULL)
485         {
486                 //not implemented
487                 global $SQL_DBH;
488                 if (is_null($dbh))
489                         return '';
490                 else
491                 return '';
492         }
493         
494 /**
495  * Returns the name of the character set
496  */
497         function sql_client_encoding($dbh=NULL)
498         {
499                 //not implemented
500                 global $SQL_DBH;
501                 if (is_null($dbh))
502                         return '';
503                 else
504                         return '';
505         }
506         
507 /**
508  * Get SQL client version
509  */
510         function sql_get_client_info()
511         {
512                 global $SQL_DBH;
513                 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
514         }
515         
516 /**
517  * Get SQL server version
518  */
519         function sql_get_server_info($dbh=NULL)
520         {
521                 global $SQL_DBH;
522                 if (is_null($dbh))
523                         return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
524                 else
525                         return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
526         }
527         
528 /**
529  * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
530  */
531         function sql_get_host_info($dbh=NULL)
532         {
533                 global $SQL_DBH;
534                 if (is_null($dbh))
535                         return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
536                 else
537                         return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
538         }
539         
540 /**
541  * Returns the SQL protocol on success, or FALSE on failure. 
542  */
543         function sql_get_proto_info($dbh=NULL)
544         {
545                 //not implemented
546                 global $SQL_DBH;
547                 if (is_null($dbh))
548                         return false;
549                 else
550                         return false;
551         }
552
553 /**
554  * Get the name of the specified field in a result
555  */
556         function sql_field_name($res, $offset = 0)
557         {
558                 $column = $res->getColumnMeta($offset);
559                 if ($column) {
560                         return $column['name'];
561                 }
562                 return false;
563         }
564
565 /**************************************************************************
566         Unimplemented mysql_* functions
567         
568 # mysql_ data_ seek (maybe useful)
569 # mysql_ errno (maybe useful)
570 # mysql_ fetch_ lengths (maybe useful)
571 # mysql_ field_ flags (maybe useful)
572 # mysql_ field_ len (maybe useful)
573 # mysql_ field_ name (maybe useful)
574 # mysql_ field_ seek (maybe useful)
575 # mysql_ field_ table (maybe useful)
576 # mysql_ field_ type (maybe useful)
577 # mysql_ info (maybe useful)
578 # mysql_ list_ processes (maybe useful)
579 # mysql_ ping (maybe useful)
580 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
581 # mysql_ thread_ id (maybe useful)
582
583 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
584 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
585
586 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
587 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
588
589 # mysql_ change_ user (deprecated)
590 # mysql_ create_ db (deprecated)
591 # mysql_ db_ query (deprecated)
592 # mysql_ drop_ db (deprecated)
593 # mysql_ escape_ string (deprecated)
594 # mysql_ list_ fields (deprecated)
595 # mysql_ list_ tables (deprecated)
596 # mysql_ tablename (deprecated)
597
598 *******************************************************************/
599
600         /*
601          * for preventing I/O strings from auto-detecting the charactor encodings by MySQL
602          * since 3.62_beta-jp
603          * Jan.20, 2011 by kotorisan and cacher
604          * refering to their conversation below,
605          * http://japan.nucleuscms.org/bb/viewtopic.php?p=26581
606          * 
607          * NOTE:        shift_jis is only supported for output. Using shift_jis in DB is prohibited.
608          * NOTE:        iso-8859-x,windows-125x if _CHARSET is unset.
609          */
610         function sql_set_charset_jp($charset) {
611                 global $MYSQL_HANDLER,$SQL_DBH;
612                 if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
613                         switch(strtolower($charset)){\r
614                                 case 'utf-8':
615                                 case 'utf8':\r
616                                         $charset = 'utf8';\r
617                                         break;\r
618                                 case 'euc-jp':
619                                 case 'ujis':\r
620                                         $charset = 'ujis';\r
621                                         break;\r
622                                 case 'gb2312':\r
623                                         $charset = 'gb2312';\r
624                                         break;
625                                 /*\r
626                                 case 'shift_jis':
627                                 case 'sjis':\r
628                                         $charset = 'sjis';\r
629                                         break;
630                                 */\r
631                                 default:
632                                         $charset = 'latin1';\r
633                                         break;\r
634                         }
635                         $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info())));
636                         if (version_compare($mySqlVer, '4.1.0', '>=')) {
637                                 $res = $SQL_DBH->exec("SET CHARACTER SET " . $charset);
638                         }
639                 }
640                 return $res;
641         }
642 }
643 ?>