]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_mdb2.php
e44826e956980134b54eabce2e9f8d24ee38f2f2
[roundcube.git] / program / include / rcube_mdb2.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_mdb2.php                                        |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   PEAR:DB wrapper class that implements PEAR MDB2 functions           |
13  |   See http://pear.php.net/package/MDB2                                |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Lukas Kahwe Smith <smith@pooteeweet.org>                      |
17  +-----------------------------------------------------------------------+
18
19  $Id: rcube_mdb2.php 4810 2011-05-27 11:02:51Z alec $
20
21 */
22
23
24 /**
25  * Database independent query interface
26  *
27  * This is a wrapper for the PEAR::MDB2 class
28  *
29  * @package    Database
30  * @author     David Saez Padros <david@ols.es>
31  * @author     Thomas Bruederli <roundcube@gmail.com>
32  * @author     Lukas Kahwe Smith <smith@pooteeweet.org>
33  * @version    1.18
34  * @link       http://pear.php.net/package/MDB2
35  */
36 class rcube_mdb2
37 {
38     var $db_dsnw;               // DSN for write operations
39     var $db_dsnr;               // DSN for read operations
40     var $db_connected = false;  // Already connected ?
41     var $db_mode = '';          // Connection mode
42     var $db_handle = 0;         // Connection handle
43     var $db_error = false;
44     var $db_error_msg = '';
45
46     private $debug_mode = false;
47     private $write_failure = false;
48     private $a_query_results = array('dummy');
49     private $last_res_id = 0;
50     private $tables;
51
52
53     /**
54      * Object constructor
55      *
56      * @param  string $db_dsnw DSN for read/write operations
57      * @param  string $db_dsnr Optional DSN for read only operations
58      */
59     function __construct($db_dsnw, $db_dsnr='', $pconn=false)
60     {
61         if ($db_dsnr == '')
62             $db_dsnr = $db_dsnw;
63
64         $this->db_dsnw = $db_dsnw;
65         $this->db_dsnr = $db_dsnr;
66         $this->db_pconn = $pconn;
67
68         $dsn_array = MDB2::parseDSN($db_dsnw);
69         $this->db_provider = $dsn_array['phptype'];
70     }
71
72
73     /**
74      * Connect to specific database
75      *
76      * @param  string $dsn  DSN for DB connections
77      * @return MDB2 PEAR database handle
78      * @access private
79      */
80     private function dsn_connect($dsn)
81     {
82         // Use persistent connections if available
83         $db_options = array(
84             'persistent'       => $this->db_pconn,
85             'emulate_prepared' => $this->debug_mode,
86             'debug'            => $this->debug_mode,
87             'debug_handler'    => array($this, 'debug_handler'),
88             'portability'      => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
89
90         if ($this->db_provider == 'pgsql') {
91             $db_options['disable_smart_seqname'] = true;
92             $db_options['seqname_format'] = '%s';
93         }
94
95         $dbh = MDB2::connect($dsn, $db_options);
96
97         if (MDB2::isError($dbh)) {
98             $this->db_error = true;
99             $this->db_error_msg = $dbh->getMessage();
100
101             raise_error(array('code' => 500, 'type' => 'db',
102                 'line' => __LINE__, 'file' => __FILE__,
103                 'message' => $dbh->getUserInfo()), true, false);
104         }
105         else if ($this->db_provider == 'sqlite') {
106             $dsn_array = MDB2::parseDSN($dsn);
107             if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
108                 $this->_sqlite_create_database($dbh, $this->sqlite_initials);
109         }
110         else if ($this->db_provider!='mssql' && $this->db_provider!='sqlsrv')
111             $dbh->setCharset('utf8');
112
113         return $dbh;
114     }
115
116
117     /**
118      * Connect to appropiate database depending on the operation
119      *
120      * @param  string $mode Connection mode (r|w)
121      * @access public
122      */
123     function db_connect($mode)
124     {
125         // Already connected
126         if ($this->db_connected) {
127             // connected to read-write db, current connection is ok
128             if ($this->db_mode == 'w' && !$this->write_failure)
129                 return;
130
131             // no replication, current connection is ok for read and write
132             if (empty($this->db_dsnr) || $this->db_dsnw == $this->db_dsnr) {
133                 $this->db_mode = 'w';
134                 return;
135             }
136
137             // Same mode, current connection is ok
138             if ($this->db_mode == $mode)
139                 return;
140         }
141
142         $dsn = ($mode == 'r') ? $this->db_dsnr : $this->db_dsnw;
143
144         $this->db_handle = $this->dsn_connect($dsn);
145         $this->db_connected = !PEAR::isError($this->db_handle);
146
147         if ($this->db_connected)
148           $this->db_mode = $mode;
149     }
150
151
152     /**
153      * Activate/deactivate debug mode
154      *
155      * @param boolean $dbg True if SQL queries should be logged
156      * @access public
157      */
158     function set_debug($dbg = true)
159     {
160         $this->debug_mode = $dbg;
161         if ($this->db_connected) {
162             $this->db_handle->setOption('debug', $dbg);
163             $this->db_handle->setOption('emulate_prepared', $dbg);
164         }
165     }
166
167
168     /**
169      * Getter for error state
170      *
171      * @param  boolean  True on error
172      * @access public
173      */
174     function is_error()
175     {
176         return $this->db_error ? $this->db_error_msg : false;
177     }
178
179
180     /**
181      * Connection state checker
182      *
183      * @param  boolean  True if in connected state
184      * @access public
185      */
186     function is_connected()
187     {
188         return PEAR::isError($this->db_handle) ? false : $this->db_connected;
189     }
190
191
192     /**
193      * Is database replication configured?
194      * This returns true if dsnw != dsnr
195      */
196     function is_replicated()
197     {
198       return !empty($this->db_dsnr) && $this->db_dsnw != $this->db_dsnr;
199     }
200
201
202     /**
203      * Execute a SQL query
204      *
205      * @param  string  SQL query to execute
206      * @param  mixed   Values to be inserted in query
207      * @return number  Query handle identifier
208      * @access public
209      */
210     function query()
211     {
212         $params = func_get_args();
213         $query = array_shift($params);
214
215         // Support one argument of type array, instead of n arguments
216         if (count($params) == 1 && is_array($params[0]))
217             $params = $params[0];
218
219         return $this->_query($query, 0, 0, $params);
220     }
221
222
223     /**
224      * Execute a SQL query with limits
225      *
226      * @param  string  SQL query to execute
227      * @param  number  Offset for LIMIT statement
228      * @param  number  Number of rows for LIMIT statement
229      * @param  mixed   Values to be inserted in query
230      * @return number  Query handle identifier
231      * @access public
232      */
233     function limitquery()
234     {
235         $params  = func_get_args();
236         $query   = array_shift($params);
237         $offset  = array_shift($params);
238         $numrows = array_shift($params);
239
240         return $this->_query($query, $offset, $numrows, $params);
241     }
242
243
244     /**
245      * Execute a SQL query with limits
246      *
247      * @param  string $query   SQL query to execute
248      * @param  number $offset  Offset for LIMIT statement
249      * @param  number $numrows Number of rows for LIMIT statement
250      * @param  array  $params  Values to be inserted in query
251      * @return number  Query handle identifier
252      * @access private
253      */
254     private function _query($query, $offset, $numrows, $params)
255     {
256         // Read or write ?
257         $mode = (strtolower(substr(trim($query),0,6)) == 'select') ? 'r' : 'w';
258
259         // don't event attempt to connect if previous write-operation failed
260         if ($this->write_failure && $mode == 'w')
261             return false;
262
263         $this->db_connect($mode);
264
265         // check connection before proceeding
266         if (!$this->is_connected())
267             return null;
268
269         if ($this->db_provider == 'sqlite')
270             $this->_sqlite_prepare();
271
272         if ($numrows || $offset)
273             $result = $this->db_handle->setLimit($numrows,$offset);
274
275         if (empty($params))
276             $result = $mode == 'r' ? $this->db_handle->query($query) : $this->db_handle->exec($query);
277         else {
278             $params = (array)$params;
279             $q = $this->db_handle->prepare($query, null, $mode=='w' ? MDB2_PREPARE_MANIP : null);
280             if ($this->db_handle->isError($q)) {
281                 $this->db_error = true;
282                 $this->db_error_msg = $q->userinfo;
283
284                 raise_error(array('code' => 500, 'type' => 'db',
285                     'line' => __LINE__, 'file' => __FILE__,
286                     'message' => $this->db_error_msg), true, false);
287                 
288                 $result = false;
289             }
290             else {
291                 $result = $q->execute($params);
292                 $q->free();
293             }
294         }
295
296         // remember that write-operation failed
297         if ($mode == 'w' && ($result === false || PEAR::isError($result)))
298             $this->write_failure = true;
299
300         // add result, even if it's an error
301         return $this->_add_result($result);
302     }
303
304
305     /**
306      * Get number of rows for a SQL query
307      * If no query handle is specified, the last query will be taken as reference
308      *
309      * @param  number $res_id  Optional query handle identifier
310      * @return mixed   Number of rows or false on failure
311      * @access public
312      */
313     function num_rows($res_id=null)
314     {
315         if (!$this->db_connected)
316             return false;
317
318         if ($result = $this->_get_result($res_id))
319             return $result->numRows();
320         else
321             return false;
322     }
323
324
325     /**
326      * Get number of affected rows for the last query
327      *
328      * @param  number $res_id Optional query handle identifier
329      * @return mixed   Number of rows or false on failure
330      * @access public
331      */
332     function affected_rows($res_id = null)
333     {
334         if (!$this->db_connected)
335             return false;
336
337         return $this->_get_result($res_id);
338     }
339
340
341     /**
342      * Get last inserted record ID
343      * For Postgres databases, a sequence name is required
344      *
345      * @param  string $table  Table name (to find the incremented sequence)
346      * @return mixed   ID or false on failure
347      * @access public
348      */
349     function insert_id($table = '')
350     {
351         if (!$this->db_connected || $this->db_mode == 'r')
352             return false;
353
354         if ($table) {
355             if ($this->db_provider == 'pgsql')
356                 // find sequence name
357                 $table = get_sequence_name($table);
358             else
359                 // resolve table name
360                 $table = get_table_name($table);
361         }
362
363         $id = $this->db_handle->lastInsertID($table);
364
365         return $this->db_handle->isError($id) ? null : $id;
366     }
367
368
369     /**
370      * Get an associative array for one row
371      * If no query handle is specified, the last query will be taken as reference
372      *
373      * @param  number $res_id Optional query handle identifier
374      * @return mixed   Array with col values or false on failure
375      * @access public
376      */
377     function fetch_assoc($res_id=null)
378     {
379         $result = $this->_get_result($res_id);
380         return $this->_fetch_row($result, MDB2_FETCHMODE_ASSOC);
381     }
382
383
384     /**
385      * Get an index array for one row
386      * If no query handle is specified, the last query will be taken as reference
387      *
388      * @param  number $res_id  Optional query handle identifier
389      * @return mixed   Array with col values or false on failure
390      * @access public
391      */
392     function fetch_array($res_id=null)
393     {
394         $result = $this->_get_result($res_id);
395         return $this->_fetch_row($result, MDB2_FETCHMODE_ORDERED);
396     }
397
398
399     /**
400      * Get col values for a result row
401      *
402      * @param  MDB2_Result_Common Query $result result handle
403      * @param  number                   $mode   Fetch mode identifier
404      * @return mixed   Array with col values or false on failure
405      * @access private
406      */
407     private function _fetch_row($result, $mode)
408     {
409         if ($result === false || PEAR::isError($result) || !$this->is_connected())
410             return false;
411
412         return $result->fetchRow($mode);
413     }
414
415
416     /**
417      * Wrapper for the SHOW TABLES command
418      *
419      * @return array List of all tables of the current database
420      * @access public
421      * @since 0.4-beta
422      */
423     function list_tables()
424     {
425         // get tables if not cached
426         if (!$this->tables) {
427             $this->db_handle->loadModule('Manager');
428             if (!PEAR::isError($result = $this->db_handle->listTables()))
429                 $this->tables = $result;
430             else
431                 $this->tables = array();
432         }
433
434         return $this->tables;
435     }
436
437
438     /**
439      * Wrapper for SHOW COLUMNS command
440      *
441      * @param string Table name
442      * @return array List of table cols
443      */
444     function list_cols($table)
445     {
446         $this->db_handle->loadModule('Manager');
447         if (!PEAR::isError($result = $this->db_handle->listTableFields($table))) {
448             return $result;
449         }
450         
451         return null;
452     }
453
454
455     /**
456      * Formats input so it can be safely used in a query
457      *
458      * @param  mixed  $input  Value to quote
459      * @param  string $type   Type of data
460      * @return string  Quoted/converted string for use in query
461      * @access public
462      */
463     function quote($input, $type = null)
464     {
465         // handle int directly for better performance
466         if ($type == 'integer')
467             return intval($input);
468
469         // create DB handle if not available
470         if (!$this->db_handle)
471             $this->db_connect('r');
472
473         return $this->db_connected ? $this->db_handle->quote($input, $type) : addslashes($input);
474     }
475
476
477     /**
478      * Quotes a string so it can be safely used as a table or column name
479      *
480      * @param  string $str Value to quote
481      * @return string  Quoted string for use in query
482      * @deprecated     Replaced by rcube_MDB2::quote_identifier
483      * @see            rcube_mdb2::quote_identifier
484      * @access public
485      */
486     function quoteIdentifier($str)
487     {
488         return $this->quote_identifier($str);
489     }
490
491
492     /**
493      * Quotes a string so it can be safely used as a table or column name
494      *
495      * @param  string $str Value to quote
496      * @return string  Quoted string for use in query
497      * @access public
498      */
499     function quote_identifier($str)
500     {
501         if (!$this->db_handle)
502             $this->db_connect('r');
503
504         return $this->db_connected ? $this->db_handle->quoteIdentifier($str) : $str;
505     }
506
507
508     /**
509      * Escapes a string
510      *
511      * @param  string $str The string to be escaped
512      * @return string  The escaped string
513      * @access public
514      * @since  0.1.1
515      */
516     function escapeSimple($str)
517     {
518         if (!$this->db_handle)
519             $this->db_connect('r');
520
521         return $this->db_handle->escape($str);
522     }
523
524
525     /**
526      * Return SQL function for current time and date
527      *
528      * @return string SQL function to use in query
529      * @access public
530      */
531     function now()
532     {
533         switch($this->db_provider) {
534             case 'mssql':
535             case 'sqlsrv':
536                 return "getdate()";
537
538             default:
539                 return "now()";
540         }
541     }
542
543
544     /**
545      * Return list of elements for use with SQL's IN clause
546      *
547      * @param  array  $arr  Input array
548      * @param  string $type Type of data
549      * @return string Comma-separated list of quoted values for use in query
550      * @access public
551      */
552     function array2list($arr, $type = null)
553     {
554         if (!is_array($arr))
555             return $this->quote($arr, $type);
556
557         foreach ($arr as $idx => $item)
558             $arr[$idx] = $this->quote($item, $type);
559
560         return implode(',', $arr);
561     }
562
563
564     /**
565      * Return SQL statement to convert a field value into a unix timestamp
566      *
567      * This method is deprecated and should not be used anymore due to limitations
568      * of timestamp functions in Mysql (year 2038 problem)
569      *
570      * @param  string $field Field name
571      * @return string  SQL statement to use in query
572      * @deprecated
573      */
574     function unixtimestamp($field)
575     {
576         switch($this->db_provider) {
577             case 'pgsql':
578                 return "EXTRACT (EPOCH FROM $field)";
579
580             case 'mssql':
581             case 'sqlsrv':
582                 return "DATEDIFF(second, '19700101', $field) + DATEDIFF(second, GETDATE(), GETUTCDATE())";
583
584             default:
585                 return "UNIX_TIMESTAMP($field)";
586         }
587     }
588
589
590     /**
591      * Return SQL statement to convert from a unix timestamp
592      *
593      * @param  string $timestamp Field name
594      * @return string  SQL statement to use in query
595      * @access public
596      */
597     function fromunixtime($timestamp)
598     {
599         return date("'Y-m-d H:i:s'", $timestamp);
600     }
601
602
603     /**
604      * Return SQL statement for case insensitive LIKE
605      *
606      * @param  string $column  Field name
607      * @param  string $value   Search value
608      * @return string  SQL statement to use in query
609      * @access public
610      */
611     function ilike($column, $value)
612     {
613         // TODO: use MDB2's matchPattern() function
614         switch($this->db_provider) {
615             case 'pgsql':
616                 return $this->quote_identifier($column).' ILIKE '.$this->quote($value);
617             default:
618                 return $this->quote_identifier($column).' LIKE '.$this->quote($value);
619         }
620     }
621
622     /**
623      * Abstract SQL statement for value concatenation
624      *
625      * @return string SQL statement to be used in query
626      * @access public
627      */
628     function concat(/* col1, col2, ... */)
629     {
630         $func = '';
631         $args = func_get_args();
632
633         switch($this->db_provider) {
634             case 'mysql':
635             case 'mysqli':
636                 $func = 'CONCAT';
637                 $delim = ', ';
638                 break;
639             case 'mssql':
640             case 'sqlsrv':
641                 $delim = ' + ';
642                 break;
643             default:
644                 $delim = ' || ';
645         }
646
647         return $func . '(' . join($delim, $args) . ')';
648     }
649
650
651     /**
652      * Encodes non-UTF-8 characters in string/array/object (recursive)
653      *
654      * @param  mixed  $input Data to fix
655      * @return mixed  Properly UTF-8 encoded data
656      * @access public
657      */
658     function encode($input)
659     {
660         if (is_object($input)) {
661             foreach (get_object_vars($input) as $idx => $value)
662                 $input->$idx = $this->encode($value);
663             return $input;
664         }
665         else if (is_array($input)) {
666             foreach ($input as $idx => $value)
667                 $input[$idx] = $this->encode($value);
668             return $input;      
669         }
670
671         return utf8_encode($input);
672     }
673
674
675     /**
676      * Decodes encoded UTF-8 string/object/array (recursive)
677      *
678      * @param  mixed $input Input data
679      * @return mixed  Decoded data
680      * @access public
681      */
682     function decode($input)
683     {
684         if (is_object($input)) {
685             foreach (get_object_vars($input) as $idx => $value)
686                 $input->$idx = $this->decode($value);
687             return $input;
688         }
689         else if (is_array($input)) {
690             foreach ($input as $idx => $value)
691                 $input[$idx] = $this->decode($value);
692             return $input;      
693         }
694
695         return utf8_decode($input);
696     }
697
698
699     /**
700      * Adds a query result and returns a handle ID
701      *
702      * @param  object $res Query handle
703      * @return mixed   Handle ID
704      * @access private
705      */
706     private function _add_result($res)
707     {
708         // sql error occured
709         if (PEAR::isError($res)) {
710             $this->db_error = true;
711             $this->db_error_msg = $res->getMessage();
712             raise_error(array('code' => 500, 'type' => 'db',
713                 'line' => __LINE__, 'file' => __FILE__,
714                 'message' => $res->getMessage() . " Query: " 
715                 . substr(preg_replace('/[\r\n]+\s*/', ' ', $res->userinfo), 0, 512)),
716                 true, false);
717         }
718
719         $res_id = sizeof($this->a_query_results);
720         $this->last_res_id = $res_id;
721         $this->a_query_results[$res_id] = $res;
722         return $res_id;
723     }
724
725
726     /**
727      * Resolves a given handle ID and returns the according query handle
728      * If no ID is specified, the last resource handle will be returned
729      *
730      * @param  number $res_id Handle ID
731      * @return mixed   Resource handle or false on failure
732      * @access private
733      */
734     private function _get_result($res_id = null)
735     {
736         if ($res_id == null)
737             $res_id = $this->last_res_id;
738
739         if (isset($this->a_query_results[$res_id]))
740             if (!PEAR::isError($this->a_query_results[$res_id]))
741                 return $this->a_query_results[$res_id];
742
743         return false;
744     }
745
746
747     /**
748      * Create a sqlite database from a file
749      *
750      * @param  MDB2   $dbh       SQLite database handle
751      * @param  string $file_name File path to use for DB creation
752      * @access private
753      */
754     private function _sqlite_create_database($dbh, $file_name)
755     {
756         if (empty($file_name) || !is_string($file_name))
757             return;
758
759         $data = file_get_contents($file_name);
760
761         if (strlen($data))
762             if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh)) 
763                 raise_error(array('code' => 500, 'type' => 'db',
764                     'line' => __LINE__, 'file' => __FILE__,
765                     'message' => $error), true, false); 
766     }
767
768
769     /**
770      * Add some proprietary database functions to the current SQLite handle
771      * in order to make it MySQL compatible
772      *
773      * @access private
774      */
775     private function _sqlite_prepare()
776     {
777         include_once(INSTALL_PATH . 'program/include/rcube_sqlite.inc');
778
779         // we emulate via callback some missing MySQL function
780         sqlite_create_function($this->db_handle->connection,
781             'from_unixtime', 'rcube_sqlite_from_unixtime');
782         sqlite_create_function($this->db_handle->connection,
783             'unix_timestamp', 'rcube_sqlite_unix_timestamp');
784         sqlite_create_function($this->db_handle->connection,
785             'now', 'rcube_sqlite_now');
786         sqlite_create_function($this->db_handle->connection,
787             'md5', 'rcube_sqlite_md5');
788     }
789
790
791     /**
792      * Debug handler for the MDB2
793      */
794     function debug_handler(&$db, $scope, $message, $context = array())
795     {
796         if ($scope != 'prepare') {
797             $debug_output = sprintf('%s(%d): %s;',
798                 $scope, $db->db_index, rtrim($message, ';'));
799             write_log('sql', $debug_output);
800         }
801     }
802
803 }  // end class rcube_db