1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 defined('FOF_INCLUDED') or die;
14
15 16 17 18 19 20
21 jimport('joomla.database.driver');
22 jimport('joomla.database.driver.mysqli');
23 class_exists('JDatabaseDriver', true);
24
25 26 27
28 class FOFDatabaseDriverJoomla extends FOFDatabase implements FOFDatabaseInterface
29 {
30
31 private $dbo;
32
33 34 35 36 37 38 39
40 protected $nameQuote = '';
41
42 43 44 45 46
47 public static function isSupported()
48 {
49 return true;
50 }
51
52 53 54 55 56
57 public function __construct($options = array())
58 {
59
60 $this->dbo = JFactory::getDbo();
61
62 $reflection = new ReflectionClass($this->dbo);
63
64 try
65 {
66 $refProp = $reflection->getProperty('nameQuote');
67 $refProp->setAccessible(true);
68 $this->nameQuote = $refProp->getValue($this->dbo);
69 }
70 catch (Exception $e)
71 {
72 $this->nameQuote = '`';
73 }
74 }
75
76 public function close()
77 {
78 if (method_exists($this->dbo, 'close'))
79 {
80 $this->dbo->close();
81 }
82 elseif (method_exists($this->dbo, 'disconnect'))
83 {
84 $this->dbo->disconnect();
85 }
86 }
87
88 public function disconnect()
89 {
90 $this->close();
91 }
92
93 public function open()
94 {
95 if (method_exists($this->dbo, 'open'))
96 {
97 $this->dbo->open();
98 }
99 elseif (method_exists($this->dbo, 'connect'))
100 {
101 $this->dbo->connect();
102 }
103 }
104
105 public function connect()
106 {
107 $this->open();
108 }
109
110 public function connected()
111 {
112 if (method_exists($this->dbo, 'connected'))
113 {
114 return $this->dbo->connected();
115 }
116
117 return true;
118 }
119
120 public function escape($text, $extra = false)
121 {
122 return $this->dbo->escape($text, $extra);
123 }
124
125 public function execute()
126 {
127 if (method_exists($this->dbo, 'execute'))
128 {
129 return $this->dbo->execute();
130 }
131
132 return $this->dbo->query();
133 }
134
135 public function getAffectedRows()
136 {
137 if (method_exists($this->dbo, 'getAffectedRows'))
138 {
139 return $this->dbo->getAffectedRows();
140 }
141
142 return 0;
143 }
144
145 public function getCollation()
146 {
147 if (method_exists($this->dbo, 'getCollation'))
148 {
149 return $this->dbo->getCollation();
150 }
151
152 return 'utf8_general_ci';
153 }
154
155 public function getConnection()
156 {
157 if (method_exists($this->dbo, 'getConnection'))
158 {
159 return $this->dbo->getConnection();
160 }
161
162 return null;
163 }
164
165 public function getCount()
166 {
167 if (method_exists($this->dbo, 'getCount'))
168 {
169 return $this->dbo->getCount();
170 }
171
172 return 0;
173 }
174
175 public function getDateFormat()
176 {
177 if (method_exists($this->dbo, 'getDateFormat'))
178 {
179 return $this->dbo->getDateFormat();
180 }
181
182 return 'Y-m-d H:i:s';;
183 }
184
185 public function getMinimum()
186 {
187 if (method_exists($this->dbo, 'getMinimum'))
188 {
189 return $this->dbo->getMinimum();
190 }
191
192 return '5.0.40';
193 }
194
195 public function getNullDate()
196 {
197 if (method_exists($this->dbo, 'getNullDate'))
198 {
199 return $this->dbo->getNullDate();
200 }
201
202 return '0000-00-00 00:00:00';
203 }
204
205 public function getNumRows($cursor = null)
206 {
207 if (method_exists($this->dbo, 'getNumRows'))
208 {
209 return $this->dbo->getNumRows($cursor);
210 }
211
212 return 0;
213 }
214
215 public function getQuery($new = false)
216 {
217 if (method_exists($this->dbo, 'getQuery'))
218 {
219 return $this->dbo->getQuery($new);
220 }
221
222 return null;
223 }
224
225 public function getTableColumns($table, $typeOnly = true)
226 {
227 if (method_exists($this->dbo, 'getTableColumns'))
228 {
229 return $this->dbo->getTableColumns($table, $typeOnly);
230 }
231
232 $result = $this->dbo->getTableFields(array($table), $typeOnly);
233
234 return $result[$table];
235 }
236
237 public function getTableKeys($tables)
238 {
239 if (method_exists($this->dbo, 'getTableKeys'))
240 {
241 return $this->dbo->getTableKeys($tables);
242 }
243
244 return array();
245 }
246
247 public function getTableList()
248 {
249 if (method_exists($this->dbo, 'getTableList'))
250 {
251 return $this->dbo->getTableList();
252 }
253
254 return array();
255 }
256
257 public function getVersion()
258 {
259 if (method_exists($this->dbo, 'getVersion'))
260 {
261 return $this->dbo->getVersion();
262 }
263
264 return '5.0.40';
265 }
266
267 public function insertid()
268 {
269 if (method_exists($this->dbo, 'insertid'))
270 {
271 return $this->dbo->insertid();
272 }
273
274 return null;
275 }
276
277 public function insertObject($table, &$object, $key = null)
278 {
279 if (method_exists($this->dbo, 'insertObject'))
280 {
281 return $this->dbo->insertObject($table, $object, $key);
282 }
283
284 return null;
285 }
286
287 public function loadAssoc()
288 {
289 if (method_exists($this->dbo, 'loadAssoc'))
290 {
291 return $this->dbo->loadAssoc();
292 }
293
294 return null;
295 }
296
297 public function loadAssocList($key = null, $column = null)
298 {
299 if (method_exists($this->dbo, 'loadAssocList'))
300 {
301 return $this->dbo->loadAssocList($key, $column);
302 }
303
304 return null;
305 }
306
307 public function loadObject($class = 'stdClass')
308 {
309 if (method_exists($this->dbo, 'loadObject'))
310 {
311 return $this->dbo->loadObject($class);
312 }
313
314 return null;
315 }
316
317 public function loadObjectList($key = '', $class = 'stdClass')
318 {
319 if (method_exists($this->dbo, 'loadObjectList'))
320 {
321 return $this->dbo->loadObjectList($key, $class);
322 }
323
324 return null;
325 }
326
327 public function loadResult()
328 {
329 if (method_exists($this->dbo, 'loadResult'))
330 {
331 return $this->dbo->loadResult();
332 }
333
334 return null;
335 }
336
337 public function loadRow()
338 {
339 if (method_exists($this->dbo, 'loadRow'))
340 {
341 return $this->dbo->loadRow();
342 }
343
344 return null;
345 }
346
347 public function loadRowList($key = null)
348 {
349 if (method_exists($this->dbo, 'loadRowList'))
350 {
351 return $this->dbo->loadRowList($key);
352 }
353
354 return null;
355 }
356
357 public function lockTable($tableName)
358 {
359 if (method_exists($this->dbo, 'lockTable'))
360 {
361 return $this->dbo->lockTable($this);
362 }
363
364 return $this;
365 }
366
367 public function quote($text, $escape = true)
368 {
369 if (method_exists($this->dbo, 'quote'))
370 {
371 return $this->dbo->quote($text, $escape);
372 }
373
374 return $text;
375 }
376
377 public function select($database)
378 {
379 if (method_exists($this->dbo, 'select'))
380 {
381 return $this->dbo->select($database);
382 }
383
384 return false;
385 }
386
387 public function setQuery($query, $offset = 0, $limit = 0)
388 {
389 if (method_exists($this->dbo, 'setQuery'))
390 {
391 return $this->dbo->setQuery($query, $offset, $limit);
392 }
393
394 return false;
395 }
396
397 public function transactionCommit($toSavepoint = false)
398 {
399 if (method_exists($this->dbo, 'transactionCommit'))
400 {
401 $this->dbo->transactionCommit($toSavepoint);
402 }
403 }
404
405 public function transactionRollback($toSavepoint = false)
406 {
407 if (method_exists($this->dbo, 'transactionRollback'))
408 {
409 $this->dbo->transactionRollback($toSavepoint);
410 }
411 }
412
413 public function transactionStart($asSavepoint = false)
414 {
415 if (method_exists($this->dbo, 'transactionStart'))
416 {
417 $this->dbo->transactionStart($asSavepoint);
418 }
419 }
420
421 public function unlockTables()
422 {
423 if (method_exists($this->dbo, 'unlockTables'))
424 {
425 return $this->dbo->unlockTables();
426 }
427
428 return $this;
429 }
430
431 public function updateObject($table, &$object, $key, $nulls = false)
432 {
433 if (method_exists($this->dbo, 'updateObject'))
434 {
435 return $this->dbo->updateObject($table, $object, $key, $nulls);
436 }
437
438 return false;
439 }
440
441 public function getLog()
442 {
443 if (method_exists($this->dbo, 'getLog'))
444 {
445 return $this->dbo->getLog();
446 }
447
448 return array();
449 }
450
451 public function dropTable($table, $ifExists = true)
452 {
453 if (method_exists($this->dbo, 'dropTable'))
454 {
455 return $this->dbo->dropTable($table, $ifExists);
456 }
457
458 return $this;
459 }
460
461 public function getTableCreate($tables)
462 {
463 if (method_exists($this->dbo, 'getTableCreate'))
464 {
465 return $this->dbo->getTableCreate($tables);
466 }
467
468 return array();
469 }
470
471 public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
472 {
473 if (method_exists($this->dbo, 'renameTable'))
474 {
475 return $this->dbo->renameTable($oldTable, $newTable, $backup, $prefix);
476 }
477
478 return $this;
479 }
480
481 public function setUtf()
482 {
483 if (method_exists($this->dbo, 'setUtf'))
484 {
485 return $this->dbo->setUtf();
486 }
487
488 return false;
489 }
490
491
492 protected function freeResult($cursor = null)
493 {
494 return false;
495 }
496
497 498 499 500 501 502 503 504 505 506 507
508 public function loadColumn($offset = 0)
509 {
510 if (method_exists($this->dbo, 'loadColumn'))
511 {
512 return $this->dbo->loadColumn($offset);
513 }
514
515 return $this->dbo->loadResultArray($offset);
516 }
517
518 519 520 521 522 523 524 525 526 527 528 529 530
531 public function quoteName($name, $as = null)
532 {
533 if (is_string($name))
534 {
535 $quotedName = $this->quoteNameStr(explode('.', $name));
536
537 $quotedAs = '';
538
539 if (!is_null($as))
540 {
541 settype($as, 'array');
542 $quotedAs .= ' AS ' . $this->quoteNameStr($as);
543 }
544
545 return $quotedName . $quotedAs;
546 }
547 else
548 {
549 $fin = array();
550
551 if (is_null($as))
552 {
553 foreach ($name as $str)
554 {
555 $fin[] = $this->quoteName($str);
556 }
557 }
558 elseif (is_array($name) && (count($name) == count($as)))
559 {
560 $count = count($name);
561
562 for ($i = 0; $i < $count; $i++)
563 {
564 $fin[] = $this->quoteName($name[$i], $as[$i]);
565 }
566 }
567
568 return $fin;
569 }
570 }
571
572 573 574 575 576 577 578 579 580
581 protected function quoteNameStr($strArr)
582 {
583 $parts = array();
584 $q = $this->nameQuote;
585
586 foreach ($strArr as $part)
587 {
588 if (is_null($part))
589 {
590 continue;
591 }
592
593 if (strlen($q) == 1)
594 {
595 $parts[] = $q . $part . $q;
596 }
597 else
598 {
599 $parts[] = $q{0} . $part . $q{1};
600 }
601 }
602
603 return implode('.', $parts);
604 }
605
606 607 608 609 610 611 612 613 614
615 public function getErrorMsg($escaped = false)
616 {
617 if (method_exists($this->dbo, 'getErrorMsg'))
618 {
619 $errorMessage = $this->dbo->getErrorMsg();
620 }
621 else
622 {
623 $errorMessage = $this->errorMsg;
624 }
625
626 if ($escaped)
627 {
628 return addslashes($errorMessage);
629 }
630
631 return $errorMessage;
632 }
633
634 635 636 637 638 639 640 641
642 public function getErrorNum()
643 {
644 if (method_exists($this->dbo, 'getErrorNum'))
645 {
646 $errorNum = $this->dbo->getErrorNum();
647 }
648 else
649 {
650 $errorNum = $this->getErrorNum;
651 }
652
653 return $errorNum;
654 }
655
656 657 658 659 660 661 662
663 public function stderr($showSQL = false)
664 {
665 if (method_exists($this->dbo, 'stderr'))
666 {
667 return $this->dbo->stderr($showSQL);
668 }
669
670 return parent::stderr($showSQL);
671 }
672
673 674 675
676 public function __call($name, array $arguments)
677 {
678 if (is_null($this->dbo))
679 {
680 throw new Exception('FOF database driver is not loaded');
681 }
682
683 if (method_exists($this->dbo, $name) || in_array($name, array('q', 'nq', 'qn', 'query')))
684 {
685 switch ($name)
686 {
687 case 'execute':
688 $name = 'query';
689 break;
690
691 case 'q':
692 $name = 'quote';
693 break;
694
695 case 'qn':
696 case 'nq':
697 switch (count($arguments))
698 {
699 case 0 :
700 $result = $this->quoteName();
701 break;
702 case 1 :
703 $result = $this->quoteName($arguments[0]);
704 break;
705 case 2:
706 default:
707 $result = $this->quoteName($arguments[0], $arguments[1]);
708 break;
709 }
710 return $result;
711
712 break;
713 }
714
715 switch (count($arguments))
716 {
717 case 0 :
718 $result = $this->dbo->$name();
719 break;
720 case 1 :
721 $result = $this->dbo->$name($arguments[0]);
722 break;
723 case 2:
724 $result = $this->dbo->$name($arguments[0], $arguments[1]);
725 break;
726 case 3:
727 $result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2]);
728 break;
729 case 4:
730 $result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
731 break;
732 case 5:
733 $result = $this->dbo->$name($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
734 break;
735 default:
736
737 $result = call_user_func_array(array($this->dbo, $name), $arguments);
738 }
739
740 if (class_exists('JDatabase') && is_object($result) && ($result instanceof JDatabase))
741 {
742 return $this;
743 }
744
745 return $result;
746 }
747 else
748 {
749 throw new \Exception('Method ' . $name . ' not found in FOFDatabase');
750 }
751 }
752
753 public function __get($name)
754 {
755 if (isset($this->dbo->$name) || property_exists($this->dbo, $name))
756 {
757 return $this->dbo->$name;
758 }
759 else
760 {
761 $this->dbo->$name = null;
762 user_error('Database driver does not support property ' . $name);
763 }
764 }
765
766 public function __set($name, $value)
767 {
768 if (isset($this->dbo->name) || property_exists($this->dbo, $name))
769 {
770 $this->dbo->$name = $value;
771 }
772 else
773 {
774 $this->dbo->$name = null;
775 user_error('Database driver not support property ' . $name);
776 }
777 }
778 }
779