[ Index ]

PHP Cross Reference of phpwcms V1.5.0 _r431 (28.01.12)

title

Body

[close]

/include/inc_lib/ -> dbcon.inc.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2012 Oliver Georgi <oliver@phpwcms.de> // All rights reserved.
   6   
   7     This script is part of PHPWCMS. The PHPWCMS web content management system is
   8     free software; you can redistribute it and/or modify it under the terms of
   9     the GNU General Public License as published by the Free Software Foundation;
  10     either version 2 of the License, or (at your option) any later version.
  11    
  12     The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
  13     A copy is found in the textfile GPL.txt and important notices to the license 
  14     from the author is found in LICENSE.txt distributed with these scripts.
  15    
  16     This script is distributed in the hope that it will be useful, but WITHOUT ANY 
  17     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  18     PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  19   
  20     This copyright notice MUST APPEAR in all copies of the script!
  21  *************************************************************************************/
  22  
  23  // ----------------------------------------------------------------
  24  // obligate check for phpwcms constants
  25  if (!defined('PHPWCMS_ROOT')) {
  26     die("You Cannot Access This Script Directly, Have a Nice Day.");
  27  }
  28  // ----------------------------------------------------------------
  29  
  30  // build the database table prepend part
  31  define ('DB_PREPEND', $phpwcms["db_prepend"] ? $phpwcms["db_prepend"].'_' : '');
  32  
  33  // open the connection to MySQL database
  34  $is_mysql_error = false;
  35  
  36  if($phpwcms["db_pers"] == 1) {
  37      $db = @mysql_pconnect($phpwcms["db_host"], $phpwcms["db_user"], $phpwcms["db_pass"]) or ($is_mysql_error = true);
  38  } else {
  39      $db = @mysql_connect($phpwcms["db_host"], $phpwcms["db_user"], $phpwcms["db_pass"]) or ($is_mysql_error = true);
  40  }
  41  
  42  $is_mysql_error = _dbSelect() ? false : true;
  43  
  44  if($is_mysql_error) {
  45      header('Location: '.PHPWCMS_URL.'dbdown.php');
  46      exit();
  47  
  48  }
  49  
  50  // set DB to compatible mode
  51  // for compatibility issues try to check for MySQL version and charset
  52  $phpwcms['db_version'] = _dbInitialize();
  53  define('PHPWCMS_DB_VERSION', $phpwcms['db_version']);
  54  
  55  if(!function_exists('mysql_real_escape_string')) {
  56      if(function_exists('mysql_escape_string')) {
  57  		function mysql_real_escape_string($string) {
  58              return mysql_escape_string( $string );
  59          }
  60      } else {
  61  		function mysql_real_escape_string($string) {
  62              return str_replace("'", "''", str_replace("\\", "\\\\", $string) );
  63          }
  64      }
  65  }
  66  // deprecated function for escaping db items
  67  function aporeplace($value='') {
  68      // ToDo: Check if _dbEscape($value, false) might better replacement
  69      return mysql_real_escape_string($value);
  70  }
  71  
  72  function _dbSelect($db_table='') {
  73      
  74      if(empty($db_table)) {
  75          $db_table = $GLOBALS['phpwcms']["db_table"];
  76      }
  77      
  78      if(isset($GLOBALS['phpwcms']["db_table_selected"]) && $GLOBALS['phpwcms']["db_table_selected"] == $db_table) {
  79          return true;
  80      }
  81      
  82      // Set current selected DB Table
  83      $GLOBALS['phpwcms']["db_table_selected"] = $db_table;
  84  
  85      return @mysql_select_db($db_table, $GLOBALS['db']);
  86  
  87  }
  88  
  89  function _dbQuery($query='', $_queryMode='ASSOC') {
  90  
  91      if(empty($query)) return false;
  92      
  93      global $db;
  94      $queryResult    = array();
  95      $queryCount        = 0;
  96      
  97      if($result = @mysql_query($query, $db)) {
  98      
  99          switch($_queryMode) {
 100  
 101              // INSERT, UPDATE, DELETE
 102              case 'INSERT':    $queryResult['INSERT_ID']        = mysql_insert_id($db);
 103              case 'DELETE':    
 104              case 'UPDATE':    
 105                              $queryResult['AFFECTED_ROWS']    = mysql_affected_rows($db);
 106                              return $queryResult;
 107                              break;
 108              
 109              // INSERT ... ON DUPLICATE KEY
 110              case 'ON_DUPLICATE':
 111                              $queryResult['AFFECTED_ROWS']    = mysql_affected_rows($db);
 112                              $queryResult['INSERT_ID']        = mysql_insert_id($db);
 113                              if($queryResult['AFFECTED_ROWS'] == 2) {
 114                                  $queryResult['INSERT_ID']        = 0;
 115                                  $queryResult['AFFECTED_ROWS']    = 1;
 116                              }
 117                              return $queryResult;
 118                              break;                
 119  
 120              // SELECT Queries    
 121              case 'ROW':        $_queryMode = 'mysql_fetch_row';    break;
 122              case 'ARRAY':    $_queryMode = 'mysql_fetch_array';    break;
 123              
 124              // COUNT
 125              case 'COUNT':    // first check if SQL COUNT() is used
 126                              $query = strtoupper($query);
 127                              if(strpos($query, 'SELECT COUNT(') !== false) {
 128                                  $row = mysql_fetch_row($result);
 129                                  return $row ? (int) $row[0] : 0;
 130                              } else {
 131                                  return mysql_num_rows($result);
 132                              }
 133                              break;
 134              
 135              // SET, CREATE, ALTER, DROP, RENAME
 136              case 'RENAME':
 137              case 'DROP':
 138              case 'ALTER':
 139              case 'SET':
 140              case 'CREATE':    return true;
 141                              break;
 142              
 143              // send SHOW query and count results
 144              case 'COUNT_SHOW':
 145                              return mysql_num_rows($result);
 146                              break;
 147              
 148              default:         $_queryMode = 'mysql_fetch_assoc';
 149      
 150          }
 151      
 152          while($row = $_queryMode($result)) {
 153              
 154              $queryResult[$queryCount] = $row;
 155              $queryCount++;
 156  
 157          }
 158          mysql_free_result($result);
 159      
 160          return $queryResult;
 161      
 162      } else {
 163          return false;
 164      }
 165  
 166  }
 167  
 168  function _dbCount($query='') {
 169      return _dbQuery($query, 'COUNT');
 170  }
 171  
 172  // function for simplified insert
 173  function _dbInsert($table='', $data=array(), $special='', $prefix=NULL) {
 174      
 175      if(empty($table)) return false;
 176      if(!is_array($data) || !count($data)) return false;
 177      
 178      $table    = (is_string($prefix) ? $prefix : DB_PREPEND).$table;
 179      $fields    = array();
 180      $values    = array();
 181      $x        = 0;
 182      
 183      foreach($data as $key => $value) {
 184          $fields[$x]    = '`'.$key.'`';
 185          $values[$x]    = _dbEscape($value);
 186          $x++;
 187      }
 188      
 189      if($special) {
 190          $special = strtoupper(trim($special));
 191          if($special != 'LOW_PRIORITY' || $special != 'DELAYED') {
 192              $special = 'DELAYED';
 193          }
 194          $special .= ' ';
 195      }
 196      
 197      $query  = 'INSERT '.$special.'INTO ' . $table . ' (';
 198      $query .= implode(',', $fields) . ') VALUES (' . implode(',', $values) . ')';
 199      
 200      return _dbQuery($query, 'INSERT');
 201  
 202  }
 203  
 204  function _dbInsertOrUpdate($table='', $data=array(), $where='', $prefix=NULL) {
 205  
 206      // INSERT ... ON DUPLICATE KEY UPDATE is available for MySQL >= 4.1.0
 207      // $where is necessary OR if $where is empty first array $data element
 208      // have to be the primary OR a unique key otherwise this will fail
 209      
 210      global $phpwcms;
 211      
 212      if(empty($table)) return false;
 213      if(!is_array($data) || !count($data)) return false;
 214      
 215      $table    = (is_string($prefix) ? $prefix : DB_PREPEND).$table;
 216      $fields    = array();
 217      $values    = array();
 218      $set    = array();
 219      $x        = 0;
 220      
 221      foreach($data as $key => $value) {
 222          $fields[$x]    = '`'.$key.'`';
 223          $values[$x]    = _dbEscape($value);
 224          $set[$x]    = $fields[$x].'='.$values[$x];
 225          $x++;
 226      }
 227      
 228      $insert  = 'INSERT INTO ' . $table . ' (';
 229      $insert .= implode(',', $fields) . ') VALUES (' . implode(',', $values) . ')';
 230      
 231      if($phpwcms['db_version'] < 40100) {
 232          // the old way
 233          
 234          // 1st send INSERT
 235          $result = _dbQuery($insert, 'INSERT');
 236          
 237          if($result === false) {
 238          
 239              // INSERT was false, now try UPDATE
 240              $update  = 'UPDATE ' . $table . ' SET ';
 241              $update .= implode(',', $set) . ' WHERE ';
 242              if($where === '' || strpos($where, '=') === false) {
 243                  reset($data);
 244                  $key    = key($data);
 245                  $value     = current($data);
 246                  $update .= '`'.$key.'`=';
 247                  $update .= _dbEscape($value);
 248              } else {
 249                  $update .= trim($where);
 250              }
 251              
 252              return _dbQuery($update, 'UPDATE');
 253  
 254          } else {
 255          
 256              return $result;
 257          }
 258      
 259      } else {
 260          // the new way
 261          $insert .= ' ON DUPLICATE KEY UPDATE ';
 262          $insert .= implode(',', $set);
 263          
 264          return _dbQuery($insert, 'ON_DUPLICATE');
 265      }
 266      
 267      return false;
 268  
 269  }
 270  
 271  // simplified db select
 272  function _dbGet($table='', $select='*', $where='', $group_by='', $order_by='', $limit='', $prefix=NULL) {
 273      
 274      if(empty($table)) return false;
 275      
 276      $table        = (is_string($prefix) ? $prefix : DB_PREPEND).$table;
 277      $sets        = array();
 278      $select        = trim($select);
 279      $limit        = trim($limit);
 280      $group_by    = trim($group_by);
 281      $order_by    = trim($order_by);
 282      
 283      if($select === '') {
 284          $select = '*';
 285      }
 286      if($limit !== '') {
 287          if(is_int($limit)) {
 288              $limit = ' LIMIT ' . $limit;
 289          } else {
 290              $limit = explode(',', $limit);
 291              $limit[0] = intval(trim($limit[0]));
 292              $limit[1] = isset($limit[1]) ? intval(trim($limit[1])) : 0;
 293              if($limit[0] && $limit[1]) {
 294                  $limit = ' LIMIT ' . $limit[0] . ',' . $limit[1];
 295              } elseif($limit[0] === 0 && $limit[1]) {
 296                  $limit = ' LIMIT ' . $limit[1];
 297              } elseif($limit[0]) {
 298                  $limit = ' LIMIT ' . $limit[0];
 299              } else {
 300                  $limit = '';
 301              }
 302          }
 303      }
 304      if($group_by !== '') {
 305          $group_by = ' GROUP BY '.aporeplace($group_by);
 306      } else {
 307          $group_by = '';
 308      }
 309      
 310      if($order_by !== '') {
 311          $order_by = ' ORDER BY '.aporeplace($order_by);
 312      } else {
 313          $order_by = '';
 314      }
 315      
 316      if($where != '') {
 317          $where = trim($where);
 318          if( substr(strtoupper($where), 0, 5) !== 'WHERE' ) {
 319              $where = 'WHERE '.$where;
 320          }
 321          $where = ' '.$where;
 322      }
 323  
 324      $query = trim( 'SELECT ' . $select . ' FROM ' . $table . $where . $group_by . $order_by . $limit);
 325  
 326      return _dbQuery($query);
 327  }
 328  
 329  // function for simplified update
 330  function _dbUpdate($table='', $data=array(), $where='', $special='', $prefix=NULL) {
 331      
 332      if(empty($table)) return false;
 333      if(!is_array($data) || !count($data)) return false;
 334      
 335      $table    = (is_string($prefix) ? $prefix : DB_PREPEND).$table;
 336      $sets    = array();
 337      
 338      foreach($data as $key => $value) {
 339          $sets[]    = '`'.$key.'`=' . _dbEscape($value);
 340      }
 341      
 342      if($special) {
 343          $special = strtoupper(trim($special));
 344          if($special != 'LOW_PRIORITY') $special = 'LOW_PRIORITY';
 345          $special .= ' ';
 346      }
 347      
 348      if($where != '') {
 349          $where = trim($where);
 350          if( substr(strtoupper($where), 0, 5) !== 'WHERE' ) {
 351              $where = 'WHERE '.$where;
 352          }
 353      }
 354  
 355      $query = trim( 'UPDATE ' . $special . $table . ' SET ' . implode(',', $sets) . ' ' . $where );
 356  
 357      return _dbQuery($query, 'UPDATE');
 358  
 359  }
 360  
 361  function _dbGetCreateCharsetCollation() {
 362      global $phpwcms;
 363      $value = '';
 364      if($phpwcms['db_version'] > 40100 && $phpwcms['db_charset']) {
 365          $value .= ' DEFAULT';
 366          $value .= ' CHARACTER SET '.$phpwcms['db_charset'];
 367          if(!empty($phpwcms['db_collation'])) {
 368              $value .= ' COLLATE '.$phpwcms['db_collation'];
 369          }
 370      }
 371      return $value;
 372  }
 373  
 374  function _report_error($error_type='DB', $query='') {
 375      global $db;
 376      $error = mysql_error($db);
 377      if($query) {
 378          $query  = str_replace(',', ",\n", $query);
 379          $error .= '<pre>' . $query .'</pre>';
 380      }
 381      return $error;
 382  }
 383  
 384  function _dbInitialize() {
 385  
 386      global $phpwcms;
 387  
 388      // check if mysql version is set
 389      if(empty($phpwcms['db_version'])) {
 390          $version = _dbQuery('SELECT VERSION()', 'ROW');
 391          if(isset($version[0][0])) {
 392              $version = explode('.', $version[0][0]);
 393              $version[0] = intval($version[0]);
 394              $version[1] = empty($version[1]) ? 0 : intval($version[1]);
 395              $version[2] = empty($version[2]) ? 0 : intval($version[2]);
 396              $phpwcms["db_version"] = (int)sprintf('%d%02d%02d', $version[0], $version[1], $version[2]);
 397          } else {
 398              return 0;
 399          }
 400      }
 401      if($phpwcms['db_version'] > 40000) {
 402          
 403          if(empty($phpwcms['db_charset'])) {
 404              $mysql_charset_map = array(    'big5'         => 'big5',    'cp-866'       => 'cp866',    'euc-jp'       => 'ujis',
 405                                          'euc-kr'       => 'euckr',    'gb2312'       => 'gb2312',    'gbk'          => 'gbk',
 406                                          'iso-8859-1'   => 'latin1',    'iso-8859-2'   => 'latin2',    'iso-8859-7'   => 'greek',
 407                                          'iso-8859-8'   => 'hebrew',    'iso-8859-8-i' => 'hebrew',    'iso-8859-9'   => 'latin5',
 408                                          'iso-8859-13'  => 'latin7',    'iso-8859-15'  => 'latin1',    'koi8-r'       => 'koi8r',
 409                                          'shift_jis'    => 'sjis',    'tis-620'      => 'tis620',    'utf-8'        => 'utf8',
 410                                          'windows-1250' => 'cp1250',    'windows-1251' => 'cp1251',    'windows-1252' => 'latin1',
 411                                          'windows-1256' => 'cp1256',    'windows-1257' => 'cp1257'   );
 412              $phpwcms['db_charset'] = $mysql_charset_map[ strtolower($phpwcms['charset']) ];
 413          }
 414          
 415          // Send charset used in phpwcms for every query
 416          $sql = "SET NAMES "._dbEscape($phpwcms['db_charset']);
 417          if($phpwcms['db_version'] > 40100 && !empty($phpwcms['db_collation'])) {
 418              $sql .= " COLLATE "._dbEscape($phpwcms['db_collation']);
 419          }
 420          if(!empty($phpwcms['db_timezone'])) {
 421              $sql .= ", time_zone = "._dbEscape($phpwcms['db_timezone']);
 422          }
 423          _dbQuery($sql, 'SET');
 424      }
 425      
 426      return $phpwcms['db_version'];
 427  }
 428  
 429  // duplicate a DB record based on 1 unique column
 430  function _dbDuplicateRow($table='', $unique_field='', $id_value=0, $exception=array(), $prefix=NULL) {
 431  
 432      // use exceptions to define duplicate values: 'field_name' => 'value' (INT/STRING)
 433      // to avoid problems with UNIQUE/auto increment columns set 'field_name' => '--UNIQUE--'
 434      // to overwrite a unique value use excpetions 'unique_field_name' => 'new_value'
 435      // to use simple SQL functions for exceptions define it like 'field_name' => 'SQL:NOW()'
 436      // for simple string operations use '--SELF--' like 'field_name' => 'Copy --SELF--'
 437      // --SELF-- will be replaced by current value of the field
 438  
 439      if(empty($table) || empty($unique_field) || empty($id_value)) return false;
 440      if(!is_array($exception)) $exception = array();
 441      
 442      $table    = (is_string($prefix) ? $prefix : DB_PREPEND).$table;
 443      
 444      $where_value = is_string($id_value) ? "'".aporeplace($id_value)."'" : $id_value;
 445      $row = _dbQuery('SELECT * FROM '.$table.' WHERE '.$unique_field.'='.$where_value.' LIMIT 1');
 446  
 447      // check against result
 448      if(isset($row[0]) && is_array($row[0]) && count($row[0])) {
 449          $row = $row[0];
 450          unset($row[$unique_field]);
 451      } else {
 452          return false;
 453      }
 454          
 455      // check eceptions
 456      foreach($exception as $key => $value) {
 457          if(isset($row[$key])) {
 458              if($value === '--UNIQUE--') {
 459                  unset($row[$key]);
 460              } else {
 461                  if(is_string($value) && strpos($value, '--SELF--') !== false) {
 462                      $value = str_replace('--SELF--', $row[$key], $value);
 463                  }
 464                  $row[$key] = $value;
 465              }
 466          }
 467      }
 468      
 469      $_VALUE    = array();
 470      $_SET    = array();
 471      $c        = 0;
 472      
 473      // build INSERT query
 474      foreach($row as $key => $value) {
 475          $_VALUE[$c]    = $key;
 476          if(is_string($value)) {
 477              if(strpos($value, 'SQL:') === 0) {
 478                  $_SET[$c] = str_replace('SQL:', '', $value);
 479              } else {
 480                  $_SET[$c] = _dbEscape($value);
 481              }
 482          } else {
 483              $_SET[$c] = _dbEscape($value);
 484          }
 485          $c++;
 486      }
 487      
 488      $sql  = 'INSERT INTO '.$table.' (';
 489      $sql .= implode(', ', $_VALUE);
 490      $sql .= ') VALUES (';
 491      $sql .= implode(', ', $_SET);
 492      $sql .= ')';
 493  
 494      $new_id = _dbQuery($sql, 'INSERT');
 495  
 496      if(!empty($new_id['INSERT_ID'])) {
 497  
 498          // fine - auto increment returns new ID
 499          return $new_id['INSERT_ID'];
 500  
 501      } elseif(isset($new_id['INSERT_ID']) && $new_id['INSERT_ID'] === 0) {
 502  
 503          // hm - maybe no auto increment - but insert was done
 504          // so lets check against $unique_field and its possible new value
 505          if(!empty($exception[$unique_field]) && $exception[$unique_field] != '__UNIQUE__') {
 506              return $exception[$unique_field];
 507          }
 508  
 509      }
 510      return false;
 511      
 512  }
 513  
 514  /*
 515   * Set Config - store given key/value in config database
 516   *
 517   * 2008/03/13 Thiemo Mättig, fixed for MySQL 4.0, use _dbInsertOrUpdate()
 518   */
 519  function _setConfig($key, $value=NULL, $group='', $status=1) {
 520  
 521      $time        = now();
 522      $group        = trim($group);
 523      $status     = intval($status);
 524  
 525      if (! is_array($key)) {
 526          $key = array($key => $value);
 527      }
 528  
 529      foreach($key as $k => $value) {
 530  
 531          if( is_string($value) ) {
 532              $vartype = 'string';
 533          } elseif( is_int($value) ) {
 534              $vartype = 'int';
 535          } elseif( is_float($value) ) {
 536              $vartype = 'float';
 537          } elseif( is_bool($value) ) {
 538              $vartype = 'bool';
 539          } elseif( is_array($value) ) {
 540               $vartype = 'array';
 541              $value   = serialize($value);
 542          } elseif( is_object($value) ) {
 543              $vartype = 'object';
 544              $value   = serialize($value);
 545          } else {
 546              $vartype = '';
 547              $value   = '';
 548          }
 549          
 550          $data = array(    'sysvalue_key'            => $k,
 551                          'sysvalue_group'        => $group,
 552                          'sysvalue_lastchange'    => $time,
 553                          'sysvalue_status'        => $status,
 554                          'sysvalue_vartype'        => $vartype,
 555                          'sysvalue_value'        => $value     );
 556  
 557          if ( ! _dbInsertOrUpdate('phpwcms_sysvalue', $data) ) {
 558              $mysql_error = trim( @mysql_error() );
 559              trigger_error("_setConfig failed".(empty($mysql_error) ? '' : ' with MySQL error: '.$mysql_error), E_USER_WARNING);
 560          }
 561  
 562      }
 563  
 564      return true;
 565  }
 566  
 567  function _dbEscape($value='', $quoted=TRUE) {
 568      if(!is_string($value) && !is_numeric($value)) {
 569          if(is_array($value) || is_object($value)) {
 570              $value = serialize($value);
 571          } elseif(is_bool($value)) {
 572              return $value ? 'true' : 'false';
 573          } elseif(is_null($value)) {
 574              return 'NULL';
 575          } else {
 576              $value = strval($value);
 577          }
 578      }
 579      $value = mysql_real_escape_string($value);
 580      return $quoted === TRUE ? "'".$value."'" : $value;
 581  }
 582  
 583  /*
 584   * Get Config - retrieve Config value from database
 585   *
 586   * If $key is string, single value will be returned.
 587   * If $key given as array - array containing values will be returned.
 588   * If $set_global is set config value will be registered in $GLOBALS[$set_global],
 589   * set $set_global = FALSE and var will not be registered in $GLOBALS
 590   */
 591  function _getConfig($key, $set_global='phpwcms') {
 592      $return = 'array';
 593      $string = '';
 594      if(is_string($key)) {
 595          if($set_global && isset($GLOBALS[$set_global][$key])) {
 596              return $GLOBALS[$set_global][$key];
 597          }
 598          $return = 'value';
 599          $string = $key;
 600          $key = array($key);
 601      }
 602      if(is_array($key) && count($key)) {
 603          $result = array();
 604          foreach($key as $value) {
 605              if($set_global && isset($GLOBALS[$set_global][$value])) {
 606                  $result[ $value ] = $GLOBALS[$set_global][$value];
 607                  continue;
 608              }
 609              $sql = 'SELECT * FROM '.DB_PREPEND."phpwcms_sysvalue WHERE sysvalue_status=1 AND sysvalue_key='".mysql_real_escape_string($value)."'";
 610              $row = _dbQuery($sql);
 611              if(isset($row[0])) {
 612                  switch($row[0]['sysvalue_vartype']) {
 613                      case 'string':    $result[ $value ] = (string) $row[0]['sysvalue_value'];                    break;
 614                      case 'int':        $result[ $value ] = (int) $row[0]['sysvalue_value'];                    break;
 615                      case 'float':    $result[ $value ] = (float) $row[0]['sysvalue_value'];                    break;
 616                      case 'bool':    $result[ $value ] = (bool) $row[0]['sysvalue_value'];                    break;
 617                      case 'array':    $result[ $value ] = (array) @unserialize($row[0]['sysvalue_value']);    break;
 618                      case 'object':    $result[ $value ] = (object) @unserialize($row[0]['sysvalue_value']);    break;
 619                      default:        $result[ $value ] = $row[0]['sysvalue_value'];
 620                  }
 621              }
 622          }
 623          if($set_global && count($result)) {
 624              foreach($result as $key => $value) {
 625                  $GLOBALS[$set_global][$key] = $result[$key];
 626              }
 627          }
 628          if($return === 'array')    {
 629              return $result;
 630          } elseif(isset($result[$string])) {
 631              return $result[$string];
 632          }        
 633      }
 634      return false;
 635  }
 636  
 637  /**
 638   * Set MySQL variable
 639   *
 640   * An often seen default value is just 1M for that MySQL variable
 641   * Serialized data or text can be much bigger than this and
 642   * MySQL connection can get lost. This fixes this and set it
 643   * to a global default value of 16M
 644   */
 645  function _dbSetVar($var='', $value=NULL, $compare=FALSE) {
 646      
 647      $var = trim($var);
 648      
 649      // stop if this was set yet. can be defined as 
 650      // additional  config value in conf.inc.php
 651      
 652      if(!is_string($var) || !$var || $value === NULL) {
 653      
 654          return FALSE;
 655          
 656      } elseif(isset($GLOBALS['phpwcms']['mysql_'.$var]) && $GLOBALS['phpwcms']['mysql_'.$var] == $value) {
 657          
 658          return TRUE;
 659      
 660      }
 661      
 662      // check if it is a valid MySQL var
 663      $_var        = _dbEscape($var, FALSE);
 664      $result        = _dbQuery('SELECT @@'.$_var.' AS mysqlvar');
 665      $default    = NULL;
 666      
 667      if(isset($result[0]['mysqlvar'])) {
 668          
 669          // check if the given MySQL var exists
 670          $default = $result[0]['mysqlvar'];
 671          
 672          if($default !== NULL) {
 673              
 674              $GLOBALS['phpwcms']['mysql_'.$var] = $default;
 675              
 676              switch($compare) {
 677                  
 678                  case '>':
 679                      $set = $default > $value ? TRUE : FALSE;
 680                      break;
 681                  
 682                  case '<':
 683                      $set = $default < $value ? TRUE : FALSE;
 684                      break;
 685                  
 686                  case '!=':
 687                      $set = $default != $value ? TRUE : FALSE;
 688                      break;
 689                  
 690                  default:
 691                      $set = FALSE;
 692              
 693              }
 694              
 695              // change MySQL var setting
 696              if($set) {
 697                  
 698                  $value    = _dbEscape($value, is_numeric($default) ? FALSE : TRUE);
 699                  
 700                  // try SET SESSION first
 701                  if(!_dbQuery('SET @@'.$_var.'='.$value, 'SET')) {
 702                      
 703                      if(!_dbQuery('SET @@session.'.$_var.'='.$value, 'SET')) {
 704                      
 705                          if(!_dbQuery('SET @@global.'.$_var.'='.$value, 'SET')) {
 706                              
 707                              return FALSE;
 708                              
 709                          }
 710  
 711                      }
 712                  
 713                  }
 714                  
 715                  $GLOBALS['phpwcms']['mysql_'.$var] = $value;
 716                  return TRUE;
 717                  
 718              }
 719              
 720          }
 721          
 722      }
 723      
 724      return FALSE;
 725  }
 726  
 727  ?>


Generated: Sun Jan 29 16:31:14 2012 Cross-referenced by PHPXref 0.7.1