  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: database.php 1319 2007-03-05 20:43:07Z hpdl $ |
| |
| 3 | + | $Id: database.php 1372 2007-03-05 22:58:51Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
7 | 7 | | |
  |
8 | | - | Copyright (c) 2006 osCommerce |
| |
| 8 | + | Copyright (c) 2007 osCommerce |
|
9 | 9 | | |
| |
10 | 10 | | Released under the GNU General Public License |
| |
11 | 11 | | */ |
| |
|
|
 |
… |
|
22 | 22 | | $password, |
| |
23 | 23 | | $debug = false, |
| |
24 | 24 | | $number_of_queries = 0, |
  |
25 | | - | $time_of_queries = 0; |
| |
| 25 | + | $time_of_queries = 0, |
| |
| 26 | + | $nextID = null, |
| |
| 27 | + | $logging_transaction = false, |
| |
| 28 | + | $logging_transaction_action = false; |
|
26 | 29 | | |
| |
27 | 30 | | function &connect($server, $username, $password, $type = DB_DATABASE_CLASS) { |
| |
28 | 31 | | require('database/' . $type . '.php'); |
| |
|
|
 |
… |
|
290 | 293 | | $batch_size, |
| |
291 | 294 | | $batch_to, |
| |
292 | 295 | | $batch_from, |
  |
293 | | - | $batch_select_field; |
| |
| 296 | + | $batch_select_field, |
| |
| 297 | + | $logging = false, |
| |
| 298 | + | $logging_module, |
| |
| 299 | + | $logging_module_id, |
| |
| 300 | + | $logging_fields = array(), |
| |
| 301 | + | $logging_changed = array(); |
|
294 | 302 | | |
| |
295 | 303 | | function osC_Database_Result(&$db_class) { |
| |
296 | 304 | | $this->db_class =& $db_class; |
| |
|
|
 |
… |
|
353 | 361 | | return $this->valueMixed($column, 'decimal'); |
| |
354 | 362 | | } |
| |
355 | 363 | | |
  |
356 | | - | function bindValueMixed($place_holder, $value, $type = 'string') { |
| |
| 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 | + | |
|
357 | 369 | | switch ($type) { |
| |
358 | 370 | | case 'int': |
| |
359 | 371 | | $value = intval($value); |
| |
|
|
 |
… |
|
401 | 413 | | } |
| |
402 | 414 | | |
| |
403 | 415 | | function bindTable($place_holder, $value) { |
  |
404 | | - | $this->bindValueMixed($place_holder, $value, 'raw'); |
| |
| 416 | + | $this->bindValueMixed($place_holder, $value, 'raw', false); |
|
405 | 417 | | } |
| |
406 | 418 | | |
| |
407 | 419 | | function next() { |
| |
|
|
 |
… |
|
478 | 490 | | } |
| |
479 | 491 | | |
| |
480 | 492 | | if ($this->cache_read === false) { |
  |
| 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 | + | |
|
481 | 529 | | $this->query_handler = $this->db_class->simpleQuery($this->sql_query, $this->debug); |
| |
482 | 530 | | |
  |
| 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)) { |
| |
| 538 | + | if ( ($this->logging_action == 'insert') && !is_numeric($this->logging_module_id) ) { |
| |
| 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 | + | |
|
483 | 550 | | if ($this->batch_query === true) { |
| |
484 | 551 | | $this->getBatchSize(); |
| |
485 | 552 | | |
| |
|
|
 |
… |
|
513 | 580 | | $this->cache_expire = $expire; |
| |
514 | 581 | | } |
| |
515 | 582 | | |
  |
| 583 | + | function setLogging($module, $id = null) { |
| |
| 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 | + | |
  |
516 | 593 | | function toArray() { |
| |
517 | 594 | | if (!isset($this->result)) { |
| |
518 | 595 | | $this->next(); |