Quick Search:

View

Revision:

Diff

Diff from 1299 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/database.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
15
3   $Id: database.php 1299 2007-03-02 01:45:18Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1290
8   Copyright (c) 2007 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
13   class osC_Database {
14     var $is_connected = false,
15         $link,
hpdl
280
16         $error_reporting = true,
17         $error = false,
hpdl
1
18         $error_number,
hpdl
280
19         $error_query,
hpdl
1
20         $server,
21         $username,
22         $password,
23         $debug = false,
24         $number_of_queries = 0,
hpdl
1290
25         $time_of_queries = 0,
26         $nextID = null,
27         $logging_transaction = false,
28         $logging_transaction_action = false;
hpdl
1
29
hpdl
1036
30     function &connect($server, $username, $password, $type = DB_DATABASE_CLASS) {
hpdl
1
31       require('database/' . $type . '.php');
32
33       $class = 'osC_Database_' . $type;
hpdl
322
34       $object = new $class($server, $username, $password);
hpdl
1
35
hpdl
322
36       return $object;
hpdl
1
37     }
38
39     function setConnected($boolean) {
40       if ($boolean === true) {
41         $this->is_connected = true;
42       } else {
43         $this->is_connected = false;
44       }
45     }
46
47     function isConnected() {
48       if ($this->is_connected === true) {
49         return true;
50       } else {
51         return false;
52       }
53     }
54
55     function &query($query) {
56       $osC_Database_Result =& new osC_Database_Result($this);
57       $osC_Database_Result->setQuery($query);
58
59       return $osC_Database_Result;
60     }
61
hpdl
234
62     function setError($error, $error_number = '', $query = '') {
hpdl
1
63       global $messageStack;
64
65       if ($this->error_reporting === true) {
66         $this->error = $error;
67         $this->error_number = $error_number;
hpdl
280
68         $this->error_query = $query;
hpdl
1
69
70         if (isset($messageStack)) {
hpdl
280
71           $messageStack->add('debug', $this->getError());
hpdl
1
72         }
73       }
74     }
75
76     function isError() {
77       if ($this->error === false) {
78         return false;
79       } else {
80         return true;
81       }
82     }
83
hpdl
280
84     function getError() {
hpdl
1
85       if ($this->isError()) {
86         $error = '';
87
88         if (!empty($this->error_number)) {
89           $error .= $this->error_number . ': ';
90         }
91
92         $error .= $this->error;
93
hpdl
280
94         if (!empty($this->error_query)) {
95           $error .= '; ' . htmlentities($this->error_query);
hpdl
234
96         }
97
hpdl
1
98         return $error;
99       } else {
100         return false;
101       }
102     }
103
104     function setErrorReporting($boolean) {
105       if ($boolean === true) {
106         $this->error_reporting = true;
107       } else {
108         $this->error_reporting = false;
109       }
110     }
111
112     function setDebug($boolean) {
113       if ($boolean === true) {
114         $this->debug = true;
115       } else {
116         $this->debug = false;
117       }
118     }
119
120     function importSQL($sql_file, $database, $table_prefix = -1) {
121       if ($this->selectDatabase($database)) {
122         if (file_exists($sql_file)) {
123           $fd = fopen($sql_file, 'rb');
124           $import_queries = fread($fd, filesize($sql_file));
125           fclose($fd);
126         } else {
127           $this->setError(sprintf(ERROR_SQL_FILE_NONEXISTENT, $sql_file));
128
129           return false;
130         }
131
132         if (!get_cfg_var('safe_mode')) {
133           @set_time_limit(0);
134         }
135
136         $sql_queries = array();
137         $sql_length = strlen($import_queries);
138         $pos = strpos($import_queries, ';');
139
140         for ($i=$pos; $i<$sql_length; $i++) {
141 // remove comments
142           if ($import_queries[0] == '#') {
143             $import_queries = ltrim(substr($import_queries, strpos($import_queries, "\n")));
144             $sql_length = strlen($import_queries);
145             $i = strpos($import_queries, ';')-1;
146             continue;
147           }
148
149           if ($import_queries[($i+1)] == "\n") {
hpdl
660
150             $next = '';
151
hpdl
1
152             for ($j=($i+2); $j<$sql_length; $j++) {
153               if (!empty($import_queries[$j])) {
154                 $next = substr($import_queries, $j, 6);
155
156                 if ($next[0] == '#') {
157 // find out where the break position is so we can remove this line (#comment line)
158                   for ($k=$j; $k<$sql_length; $k++) {
159                     if ($import_queries[$k] == "\n") {
160                       break;
161                     }
162                   }
163
164                   $query = substr($import_queries, 0, $i+1);
165
166                   $import_queries = substr($import_queries, $k);
167
168 // join the query before the comment appeared, with the rest of the dump
169                   $import_queries = $query . $import_queries;
170                   $sql_length = strlen($import_queries);
171                   $i = strpos($import_queries, ';')-1;
172                   continue 2;
173                 }
174
175                 break;
176               }
177             }
178
179             if (empty($next)) { // get the last insert query
180               $next = 'insert';
181             }
182
183             if ((strtoupper($next) == 'DROP T') || (strtoupper($next) == 'CREATE') || (strtoupper($next) == 'INSERT')) {
184               $next = '';
185
186               $sql_query = substr($import_queries, 0, $i);
187
188               if ($table_prefix !== -1) {
189                 if (strtoupper(substr($sql_query, 0, 25)) == 'DROP TABLE IF EXISTS OSC_') {
190                   $sql_query = 'DROP TABLE IF EXISTS ' . $table_prefix . substr($sql_query, 25);
191                 } elseif (strtoupper(substr($sql_query, 0, 17)) == 'CREATE TABLE OSC_') {
192                   $sql_query = 'CREATE TABLE ' . $table_prefix . substr($sql_query, 17);
193                 } elseif (strtoupper(substr($sql_query, 0, 16)) == 'INSERT INTO OSC_') {
194                   $sql_query = 'INSERT INTO ' . $table_prefix . substr($sql_query, 16);
195                 }
196               }
197
hpdl
280
198               $sql_queries[] = trim($sql_query);
hpdl
1
199
200               $import_queries = ltrim(substr($import_queries, $i+1));
201               $sql_length = strlen($import_queries);
202               $i = strpos($import_queries, ';')-1;
203             }
204           }
205         }
206
207         for ($i=0, $n=sizeof($sql_queries); $i<$n; $i++) {
208           $this->simpleQuery($sql_queries[$i]);
hpdl
280
209
210           if ($this->isError()) {
211             break;
212           }
hpdl
1
213         }
214       }
215
216       if ($this->isError()) {
217         return false;
218       } else {
219         return true;
220       }
221     }
222
223     function hasCreatePermission($database) {
224       $db_created = false;
225
226       if (empty($database)) {
227         $this->setError(ERROR_DB_NO_DATABASE_SELECTED);
228
229         return false;
230       }
231
232       $this->setErrorReporting(false);
233
234       if ($this->selectDatabase($database) === false) {
235         $this->setErrorReporting(true);
236
237         if ($this->simpleQuery('create database ' . $database)) {
238           $db_created = true;
239         }
240       }
241
242       $this->setErrorReporting(true);
243
244       if ($this->isError() === false) {
245         if ($this->selectDatabase($database)) {
246           if ($this->simpleQuery('create table osCommerceTestTable1536f ( temp_id int )')) {
247             if ($db_created === true) {
248               $this->simpleQuery('drop database ' . $database);
249             } else {
250               $this->simpleQuery('drop table osCommerceTestTable1536f');
251             }
252           }
253         }
254       }
255
256       if ($this->isError()) {
257         return false;
258       } else {
259         return true;
260       }
261     }
262
263     function numberOfQueries() {
264       return $this->number_of_queries;
265     }
266
267     function timeOfQueries() {
268       return $this->time_of_queries;
269     }
270
271     function getMicroTime() {
272       list($usec, $sec) = explode(' ', microtime());
273
274       return ((float)$usec + (float)$sec);
275     }
276   }
277
278   class osC_Database_Result {
279     var $db_class,
280         $sql_query,
281         $query_handler,
282         $result,
283         $rows,
284         $affected_rows,
285         $cache_key,
286         $cache_expire,
287         $cache_data,
288         $cache_read = false,
289         $debug = false,
290         $batch_query = false,
291         $batch_number,
292         $batch_rows,
293         $batch_size,
294         $batch_to,
295         $batch_from,
hpdl
1290
296         $batch_select_field,
297         $logging = false,
298         $logging_module,
hpdl
1293
299         $logging_module_id,
hpdl
1290
300         $logging_fields = array(),
301         $logging_changed = array();
hpdl
1
302
303     function osC_Database_Result(&$db_class) {
304       $this->db_class =& $db_class;
305     }
306
307     function setQuery($query) {
308       $this->sql_query = $query;
309     }
310
311     function appendQuery($query) {
312       $this->sql_query .= ' ' . $query;
313     }
314
315     function getQuery() {
316       return $this->sql_query;
317     }
318
319     function setDebug($boolean) {
320       if ($boolean === true) {
321         $this->debug = true;
322       } else {
323         $this->debug = false;
324       }
325     }
326
327     function valueMixed($column, $type = 'string') {
328       if (!isset($this->result)) {
329         $this->next();
330       }
331
332       switch ($type) {
333         case 'protected':
hpdl
725
334           return osc_output_string_protected($this->result[$column]);
hpdl
1
335           break;
336         case 'int':
337           return (int)$this->result[$column];
338           break;
339         case 'decimal':
340           return (float)$this->result[$column];
341           break;
342         case 'string':
343         default:
344           return $this->result[$column];
345       }
346     }
347
348     function value($column) {
349       return $this->valueMixed($column, 'string');
350     }
351
352     function valueProtected($column) {
353       return $this->valueMixed($column, 'protected');
354     }
355
356     function valueInt($column) {
357       return $this->valueMixed($column, 'int');
358     }
359
360     function valueDecimal($column) {
361       return $this->valueMixed($column, 'decimal');
362     }
363
hpdl
1290
364     function bindValueMixed($place_holder, $value, $type = 'string', $log = true) {
365       if ($log === true) {
366         $this->logging_fields[substr($place_holder, 1)] = $value;
367       }
368
hpdl
1
369       switch ($type) {
370         case 'int':
371           $value = intval($value);
372           break;
hpdl
219
373         case 'float':
374           $value = floatval($value);
375           break;
hpdl
1
376         case 'raw':
377           break;
378         case 'string':
379         default:
hpdl
1118
380           $value = "'" . $this->db_class->parseString(trim($value)) . "'";
hpdl
1
381       }
382
383       $this->bindReplace($place_holder, $value);
384     }
385
386     function bindReplace($place_holder, $value) {
387       $pos = strpos($this->sql_query, $place_holder);
388
389       if ($pos !== false) {
390         $length = strlen($place_holder);
391         $character_after_place_holder = substr($this->sql_query, $pos+$length, 1);
392
393         if (($character_after_place_holder === false) || ereg('[ ,)"]', $character_after_place_holder)) {
394           $this->sql_query = substr_replace($this->sql_query, $value, $pos, $length);
395         }
396       }
397     }
398
399     function bindValue($place_holder, $value) {
400       $this->bindValueMixed($place_holder, $value, 'string');
401     }
402
403     function bindInt($place_holder, $value) {
404       $this->bindValueMixed($place_holder, $value, 'int');
405     }
406
hpdl
219
407     function bindFloat($place_holder, $value) {
408       $this->bindValueMixed($place_holder, $value, 'float');
409     }
410
hpdl
1
411     function bindRaw($place_holder, $value) {
412       $this->bindValueMixed($place_holder, $value, 'raw');
413     }
414
415     function bindTable($place_holder, $value) {
hpdl
1290
416       $this->bindValueMixed($place_holder, $value, 'raw', false);
hpdl
1
417     }
418
419     function next() {
420       if ($this->cache_read === true) {
421         list(, $this->result) = each($this->cache_data);
422       } else {
423         if (!isset($this->query_handler)) {
424           $this->execute();
425         }
426
427         $this->result = $this->db_class->next($this->query_handler);
428
429         if (isset($this->cache_key)) {
430           $this->cache_data[] = $this->result;
431         }
432       }
433
434       return $this->result;
435     }
436
437     function freeResult() {
438       global $osC_Cache;
439
440       if ($this->cache_read === false) {
441         if (eregi('^SELECT', $this->sql_query)) {
442           $this->db_class->freeResult($this->query_handler);
443         }
444
445         if (isset($this->cache_key)) {
446           $osC_Cache->write($this->cache_key, $this->cache_data);
447         }
448       }
449
450       unset($this);
451     }
452
453     function numberOfRows() {
454       if (!isset($this->rows)) {
455         if (!isset($this->query_handler)) {
456           $this->execute();
457         }
458
459         if (isset($this->cache_key) && ($this->cache_read === true)) {
460           $this->rows = sizeof($this->cache_data);
461         } else {
462           $this->rows = $this->db_class->numberOfRows($this->query_handler);
463         }
464       }
465
466       return $this->rows;
467     }
468
469     function affectedRows() {
470       if (!isset($this->affected_rows)) {
471         if (!isset($this->query_handler)) {
472           $this->execute();
473         }
474
475         $this->affected_rows = $this->db_class->affectedRows();
476       }
477
478       return $this->affected_rows;
479     }
480
481     function execute() {
482       global $osC_Cache;
483
484       if (isset($this->cache_key)) {
485         if ($osC_Cache->read($this->cache_key, $this->cache_expire)) {
486           $this->cache_data = $osC_Cache->cached_data;
487
488           $this->cache_read = true;
489         }
490       }
491
492       if ($this->cache_read === false) {
hpdl
1290
493         if ($this->logging === true) {
494           $this->logging_action = substr($this->sql_query, 0, strpos($this->sql_query, ' '));
495
496           if ($this->logging_action == 'update') {
497             $db = split(' ', $this->sql_query, 3);
498             $this->logging_database = $db[1];
499
500             $test = $this->db_class->simpleQuery('select ' . implode(', ', array_keys($this->logging_fields)) . ' from ' . $this->logging_database . substr($this->sql_query, osc_strrpos_string($this->sql_query, ' where ')));
501
502             while ($result = $this->db_class->next($test)) {
503               foreach ($this->logging_fields as $key => $value) {
504                 if ($result[$key] != $value) {
505                   $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => $result[$key], 'new' => $value);
506                 }
507               }
508             }
509           } elseif ($this->logging_action == 'insert') {
510             $db = split(' ', $this->sql_query, 4);
511             $this->logging_database = $db[2];
512
513             foreach ($this->logging_fields as $key => $value) {
514               $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => '', 'new' => $value);
515             }
516           } elseif ($this->logging_action == 'delete') {
517             $db = split(' ', $this->sql_query, 4);
518             $this->logging_database = $db[2];
519
520             $del = $this->db_class->simpleQuery('select * from ' . $this->logging_database . ' ' . $db[3]);
521             while ($result = $this->db_class->next($del)) {
522               foreach ($result as $key => $value) {
523                 $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => $value, 'new' => '');
524               }
525             }
526           }
527         }
528
hpdl
1
529         $this->query_handler = $this->db_class->simpleQuery($this->sql_query, $this->debug);
530
hpdl
1290
531         if ($this->logging === true) {
532           if ($this->db_class->logging_transaction_action === false) {
533             $this->db_class->logging_transaction_action = $this->logging_action;
534           }
535
536           if ($this->affectedRows($this->query_handler) > 0) {
537             if (!empty($this->logging_changed)) {
hpdl
1293
538               if ( ($this->logging_action == 'insert') && !is_numeric($this->logging_module_id) ) {
hpdl
1290
539                 $this->logging_module_id = $this->db_class->nextID();
540                 $this->setNextID($this->logging_module_id);
541               }
542
543               if ( class_exists('osC_AdministratorsLog') ) {
544                 osC_AdministratorsLog::insert($this->logging_module, $this->db_class->logging_transaction_action, $this->logging_module_id, $this->logging_action, $this->logging_changed, $this->db_class->logging_transaction);
545               }
546             }
547           }
548         }
549
hpdl
1
550         if ($this->batch_query === true) {
hpdl
298
551           $this->getBatchSize();
hpdl
1
552
553           $this->batch_to = ($this->batch_rows * $this->batch_number);
554           if ($this->batch_to > $this->batch_size) {
555             $this->batch_to = $this->batch_size;
556           }
557
558           $this->batch_from = ($this->batch_rows * ($this->batch_number - 1));
559           if ($this->batch_to == 0) {
560             $this->batch_from = 0;
561           } else {
562             $this->batch_from++;
563           }
564         }
565
566         return $this->query_handler;
567       }
568     }
569
570     function executeRandom() {
571       return $this->query_handler = $this->db_class->randomQuery($this->sql_query);
572     }
573
574     function executeRandomMulti() {
575       return $this->query_handler = $this->db_class->randomQueryMulti($this->sql_query);
576     }
577
578     function setCache($key, $expire = 0) {
579       $this->cache_key = $key;
580       $this->cache_expire = $expire;
581     }
582
hpdl
1293
583     function setLogging($module, $id = null) {
hpdl
1290
584       $this->logging = true;
585       $this->logging_module = $module;
586       $this->logging_module_id = $id;
587     }
588
589     function setNextID($id) {
590       $this->db_class->nextID = $id;
591     }
592
hpdl
1
593     function toArray() {
594       if (!isset($this->result)) {
595         $this->next();
596       }
597
598       return $this->result;
599     }
600
hpdl
220
601     function prepareSearch($keywords, $columns, $embedded = false) {
602       if ($embedded === true) {
603         $this->sql_query .= ' and ';
604       }
605
606       $keywords_array = explode(' ', $keywords);
607
608       if ($this->db_class->use_fulltext === true) {
609         if ($this->db_class->use_fulltext_boolean === true) {
610           $keywords = '';
611
612           foreach ($keywords_array as $keyword) {
613             if ((substr($keyword, 0, 1) != '-') && (substr($keyword, 0, 1) != '+')) {
614               $keywords .= '+';
615             }
hpdl
234
616
hpdl
220
617             $keywords .= $keyword . ' ';
618           }
619
620           $keywords = substr($keywords, 0, -1);
621         }
622
623         $this->sql_query .= $this->db_class->prepareSearch($columns);
624         $this->bindValue(':keywords', $keywords);
625       } else {
626         foreach ($keywords_array as $keyword) {
627           $this->sql_query .= $this->db_class->prepareSearch($columns);
628
629           foreach ($columns as $column) {
630             $this->bindValue(':keyword', '%' . $keyword . '%');
631           }
632
633           $this->sql_query .= ' and ';
634         }
635
636         $this->sql_query = substr($this->sql_query, 0, -5);
637       }
638     }
639
hpdl
56
640     function setBatchLimit($batch_number = 1, $maximum_rows = 20, $select_field = '') {
hpdl
1
641       $this->batch_query = true;
hpdl
55
642       $this->batch_number = (is_numeric($batch_number) ? $batch_number : 1);
hpdl
1
643       $this->batch_rows = $maximum_rows;
644       $this->batch_select_field = (empty($select_field) ? '*' : $select_field);
645
hpdl
444
646       $from = max(($this->batch_number * $maximum_rows) - $maximum_rows, 0);
hpdl
1
647
648       $this->sql_query = $this->db_class->setBatchLimit($this->sql_query, $from, $maximum_rows);
649
650     }
651
hpdl
298
652     function getBatchSize() {
hpdl
1
653       global $osC_Database;
654
655       if (!isset($this->batch_size)) {
hpdl
298
656         $this->batch_size = $this->db_class->getBatchSize($this->sql_query, $this->batch_select_field);
hpdl
1
657       }
658
659       return $this->batch_size;
660     }
661
662     function displayBatchLinksTotal($text) {
663       return sprintf($text, $this->batch_from, $this->batch_to, $this->batch_size);
664     }
665
666     function displayBatchLinksPullDown($batch_keyword = 'page', $parameters = '') {
hpdl
387
667       global $osC_Language;
668
hpdl
1
669       $number_of_pages = ceil($this->batch_size / $this->batch_rows);
670
671       if ($number_of_pages > 1) {
672         $pages_array = array();
673         for ($i=1; $i<=$number_of_pages; $i++) {
674           $pages_array[] = array('id' => $i, 'text' => $i);
675         }
676
677         $get_parameter = '';
678         $hidden_parameter = '';
hpdl
1026
679
hpdl
1
680         if (!empty($parameters)) {
hpdl
685
681           $parameters = explode('&', $parameters);
hpdl
1026
682
hpdl
1
683           foreach ($parameters as $parameter) {
hpdl
1026
684             $keys = explode('=', $parameter, 2);
hpdl
1
685
hpdl
1026
686             if ($keys[0] != $batch_keyword) {
687               $get_parameter .= $keys[0] . (isset($keys[1]) ? '=' . $keys[1] : '') . '&';
688               $hidden_parameter .= osc_draw_hidden_field($keys[0], (isset($keys[1]) ? $keys[1] : ''));
hpdl
1
689             }
690           }
691         }
692
hpdl
1299
693         if ( defined('OSC_IN_ADMIN') && ( OSC_IN_ADMIN === true ) ) {
694           $back_string = osc_icon('nav_back.png');
695           $back_grey_string = osc_icon('nav_back_grey.png');
696           $forward_string = osc_icon('nav_forward.png');
697           $forward_grey_string = osc_icon('nav_forward_grey.png');
698         } else {
699           $back_string = $osC_Language->get('result_set_previous_page');
700           $back_grey_string = $osC_Language->get('result_set_previous_page');
701           $forward_string = $osC_Language->get('result_set_next_page');
702           $forward_grey_string = $osC_Language->get('result_set_next_page');
703         }
704
hpdl
1026
705         $display_links = '<form action="' . osc_href_link(basename($_SERVER['SCRIPT_FILENAME'])) . '" action="get">' . $hidden_parameter;
hpdl
1
706
707         if ($this->batch_number > 1) {
hpdl
1299
708           $display_links .= osc_link_object(osc_href_link(basename($_SERVER['SCRIPT_FILENAME']), $get_parameter . $batch_keyword . '=' . ($this->batch_number - 1)), $back_string, 'class="splitPageLink"');
hpdl
1
709         } else {
hpdl
1299
710           $display_links .= $back_grey_string;
hpdl
1
711         }
712
hpdl
387
713         $display_links .= '&nbsp;&nbsp;' . sprintf($osC_Language->get('result_set_current_page'), osc_draw_pull_down_menu($batch_keyword, $pages_array, $this->batch_number, 'onchange="this.form.submit();"'), $number_of_pages) . '&nbsp;&nbsp;';
hpdl
1
714
715         if (($this->batch_number < $number_of_pages) && ($number_of_pages != 1)) {
hpdl
1299
716           $display_links .= osc_link_object(osc_href_link(basename($_SERVER['SCRIPT_FILENAME']), $get_parameter . $batch_keyword . '=' . ($this->batch_number + 1)), $forward_string, 'class="splitPageLink"');
hpdl
1
717         } else {
hpdl
1299
718           $display_links .= $forward_grey_string;
hpdl
1
719         }
720
hpdl
1026
721         $display_links .= osc_draw_hidden_session_id_field() . '</form>';
hpdl
1
722       } else {
hpdl
387
723         $display_links = sprintf($osC_Language->get('result_set_current_page'), 1, 1);
hpdl
1
724       }
725
726       return $display_links;
727     }
728
729     function isBatchQuery() {
730       if ($this->batch_query === true) {
731         return true;
732       }
733
734       return false;
735     }
736   }
737 ?>