OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxdb.php
Go to the documentation of this file.
1 <?php
2 
3 
4 // Including main ADODB include
5 require_once getShopBasePath() . 'core/adodblite/adodb.inc.php';
6 
10 class oxDb
11 {
12 
18  const FETCH_MODE_NUM = ADODB_FETCH_NUM;
19 
25  const FETCH_MODE_ASSOC = ADODB_FETCH_ASSOC;
26 
34  public static $configSet = false;
35 
43  protected static $_instance = null;
44 
52  protected static $_oDB = null;
53 
61  protected static $_aTblDescCache = array();
62 
70  private static $_dbType = '';
71 
79  private static $_dbUser = '';
80 
88  private static $_dbPwd = '';
89 
97  private static $_dbName = '';
98 
106  private static $_dbHost = '';
107 
115  private static $_iDebug = 0;
116 
124  private static $_blLogChangesInAdmin = false;
125 
133  private static $_iUtfMode = 0;
134 
142  private static $_sDefaultDatabaseConnection = null;
143 
151  private static $_aSlaveHosts;
152 
160  private static $_sAdminEmail;
161 
171  private static $_iMasterSlaveBalance;
172 
180  private static $_sLocalTimeFormat;
181 
189  private static $_sLocalDateFormat;
190 
198  public static function setConfig($oConfig)
199  {
200  self::$_dbType = $oConfig->getVar('dbType');
201  self::$_dbUser = $oConfig->getVar('dbUser');
202  self::$_dbPwd = $oConfig->getVar('dbPwd');
203  self::$_dbName = $oConfig->getVar('dbName');
204  self::$_dbHost = $oConfig->getVar('dbHost');
205  self::$_iDebug = $oConfig->getVar('iDebug');
206  self::$_blLogChangesInAdmin = $oConfig->getVar('blLogChangesInAdmin');
207  self::$_iUtfMode = $oConfig->getVar('iUtfMode');
208  self::$_sDefaultDatabaseConnection = $oConfig->getVar('sDefaultDatabaseConnection');
209  self::$_aSlaveHosts = $oConfig->getVar('aSlaveHosts');
210  //deprecated since v5.3.0 (2016-06-17). Different solution in 6.0.
211  self::$_iMasterSlaveBalance = $oConfig->getVar('iMasterSlaveBalance');
212  //end deprecated
213  self::$_sAdminEmail = $oConfig->getVar('sAdminEmail');
214  self::$_sLocalTimeFormat = $oConfig->getVar('sLocalTimeFormat');
215  self::$_sLocalDateFormat = $oConfig->getVar('sLocalDateFormat');
216  }
217 
227  protected static function _getConfigParam($sConfigName)
228  {
229  if (isset(self::$$sConfigName)) {
230  return self::$$sConfigName;
231  }
232 
233  return null;
234  }
235 
241  public static function getInstance()
242  {
243  // disable caching for test modules
244  if (defined('OXID_PHP_UNIT')) {
245  self::$_instance = modInstances::getMod(__CLASS__);
246  }
247 
248  if (!self::$_instance instanceof oxDb) {
249 
250  //do not use simple oxNew here as it goes to eternal cycle
251  self::$_instance = new oxDb();
252 
253  if (defined('OXID_PHP_UNIT')) {
254  modInstances::addMod(__CLASS__, self::$_instance);
255  }
256  }
257 
258  return self::$_instance;
259  }
260 
266  protected function isAdmin()
267  {
268  return isAdmin();
269  }
270 
278  protected function _getModules()
279  {
280  $_iDebug = self::_getConfigParam('_iDebug');
281 
283 
284  $sModules = '';
285  if ($_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7) {
286  $sModules = 'perfmon';
287  }
288 
289  // log admin changes ?
290  if ($this->isAdmin() && self::_getConfigParam('_blLogChangesInAdmin')) {
291  $sModules .= ($sModules ? ':' : '') . 'oxadminlog';
292  }
293 
294  return $sModules;
295  }
296 
302  protected function _registerAdoDbExceptionHandler()
303  {
304  global $ADODB_EXCEPTION;
305  $ADODB_EXCEPTION = 'oxAdoDbException';
306 
307  include_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
308  }
309 
317  protected function _setUp($oDb)
318  {
319  // @deprecated since v5.3.0 (2016-06-07).
320  // The SQL logging feature is deprecated. This feature will be removed.
321  $_iDebug = self::_getConfigParam('_iDebug');
322  if ($_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7) {
323  try {
324  $oDb->execute('truncate table adodb_logsql');
325  } catch (ADODB_Exception $e) {
326  // nothing
327  }
328  if (method_exists($oDb, "logSQL")) {
329  $oDb->logSQL(true);
330  }
331  }
332  // END deprecated
333 
334  $oDb->cacheSecs = 60 * 10; // 10 minute caching
335  $oDb->execute('SET @@session.sql_mode = ""');
336 
337  if (self::_getConfigParam('_iUtfMode')) {
338  $oDb->execute('SET NAMES "utf8"');
339  $oDb->execute('SET CHARACTER SET utf8');
340  $oDb->execute('SET CHARACTER_SET_CONNECTION = utf8');
341  $oDb->execute('SET CHARACTER_SET_DATABASE = utf8');
342  $oDb->execute('SET character_set_results = utf8');
343  $oDb->execute('SET character_set_server = utf8');
344  } elseif (($sConn = self::_getConfigParam('_sDefaultDatabaseConnection')) != '') {
345  $oDb->execute('SET NAMES "' . $sConn . '"');
346  }
347  }
348 
360  protected function _sendMail($sEmail, $sSubject, $sBody)
361  {
362  include_once getShopBasePath() . 'core/phpmailer/class.phpmailer.php';
363  include_once getShopBasePath() . 'core/phpmailer/class.smtp.php';
364  $oMailer = new phpmailer();
365  $oMailer->isMail();
366 
367  $oMailer->From = $sEmail;
368  $oMailer->AddAddress($sEmail);
369  $oMailer->Subject = $sSubject;
370  $oMailer->Body = $sBody;
371 
372  return $oMailer->send();
373  }
374 
382  protected function _notifyConnectionErrors($oDb)
383  {
384  // notifying shop owner about connection problems
385  if (($sAdminEmail = self::_getConfigParam('_sAdminEmail'))) {
386  $sFailedShop = isset($_REQUEST['shp']) ? addslashes($_REQUEST['shp']) : 'Base shop';
387 
388  $sDate = date('l dS of F Y h:i:s A');
389  $sScript = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
390  $sReferer = $_SERVER['HTTP_REFERER'];
391 
392  //sending a message to admin
393  $sWarningSubject = 'Offline warning!';
394  $sWarningBody = "
395  Database error in OXID eShop:
396  Date: {$sDate}
397  Shop: {$sFailedShop}
398 
399  mysql error: " . $oDb->errorMsg() . "
400  mysql error no: " . $oDb->errorNo() . "
401 
402  Script: {$sScript}
403  Referer: {$sReferer}";
404 
405  $this->_sendMail($sAdminEmail, $sWarningSubject, $sWarningBody);
406  }
407 
408  //only exception to default construction method
409  $oEx = new oxConnectionException();
410  $oEx->setMessage('EXCEPTION_CONNECTION_NODB');
411  $oEx->setConnectionError(self::_getConfigParam('_dbUser') . 's' . getShopBasePath() . $oDb->errorMsg());
412  throw $oEx;
413  }
414 
423  protected function _onConnectionError($oDb)
424  {
425  $sVerPrefix = '';
426  $sVerPrefix = '_ce';
427 
428 
429  $sConfig = join('', file(getShopBasePath() . 'config.inc.php'));
430 
431  if (strpos($sConfig, '<dbHost' . $sVerPrefix . '>') !== false &&
432  strpos($sConfig, '<dbName' . $sVerPrefix . '>') !== false
433  ) {
434  // pop to setup as there is something wrong
435  //oxRegistry::getUtils()->redirect( "setup/index.php", true, 302 );
436  $sHeaderCode = "HTTP/1.1 302 Found";
437  header($sHeaderCode);
438  header("Location: setup/index.php");
439  header("Connection: close");
440  exit();
441  } else {
442  // notifying about connection problems
443  $this->_notifyConnectionErrors($oDb);
444 
445  }
446  }
447 
448 
458  protected function _getDbInstance($iInstType = false)
459  {
460  $sHost = self::_getConfigParam("_dbHost");
461  $sUser = self::_getConfigParam("_dbUser");
462  $sPwd = self::_getConfigParam("_dbPwd");
463  $sName = self::_getConfigParam("_dbName");
464  $sType = self::_getConfigParam("_dbType");
465 
466  $oDb = ADONewConnection($sType, $this->_getModules());
467 
468 
469  try {
470  $oDb->connect($sHost, $sUser, $sPwd, $sName);
471  } catch (oxAdoDbException $e) {
472  $this->_onConnectionError($oDb);
473  }
474 
475  self::_setUp($oDb);
476 
477  return $oDb;
478  }
479 
489  public static function getDb($iFetchMode = oxDb::FETCH_MODE_NUM)
490  {
491  if (defined('OXID_PHP_UNIT')) {
492  if (isset(modDB::$unitMOD) && is_object(modDB::$unitMOD)) {
493  return modDB::$unitMOD;
494  }
495  }
496 
497  if (self::$_oDB === null) {
498 
499  $oInst = self::getInstance();
500 
501  //setting configuration on the first call
502  $oInst->setConfig(oxRegistry::get("oxConfigFile"));
503 
504  // @deprecated since v5.3.0 (2016-06-07).
505  // As of v6.0 it is not longer possible to use the database for PHP session handling.
506  global $ADODB_SESSION_TBL,
507  $ADODB_SESSION_CONNECT,
508  $ADODB_SESSION_DRIVER,
509  $ADODB_SESSION_USER,
510  $ADODB_SESSION_PWD,
511  $ADODB_SESSION_DB,
512  $ADODB_SESS_LIFE,
513  $ADODB_SESS_DEBUG;
514 
515  // session related parameters. don't change.
516 
517  //Tomas
518  //the default setting is 3000 * 60, but actually changing this will give no effect as now redefinition of this constant
519  //appears after OXID custom settings are loaded and $ADODB_SESS_LIFE depends on user settings.
520  //You can find the redefinition of ADODB_SESS_LIFE @ oxconfig.php:: line ~ 390.
521  $ADODB_SESS_LIFE = 3000 * 60;
522  $ADODB_SESSION_TBL = "oxsessions";
523  $ADODB_SESSION_DRIVER = self::_getConfigParam('_dbType');
524  $ADODB_SESSION_USER = self::_getConfigParam('_dbUser');
525  $ADODB_SESSION_PWD = self::_getConfigParam('_dbPwd');
526  $ADODB_SESSION_DB = self::_getConfigParam('_dbName');
527  $ADODB_SESSION_CONNECT = self::_getConfigParam('_dbHost');
528  $ADODB_SESS_DEBUG = false;
529  // END deprecated
530 
531 
532  $oDb = new oxLegacyDb();
533  $oDbInst = $oInst->_getDbInstance();
534  $oDb->setConnection($oDbInst);
535 
536  self::$_oDB = $oDb;
537  }
538 
539  self::$_oDB->setFetchMode($iFetchMode);
540 
541  return self::$_oDB;
542  }
543 
553  public function quoteArray($aStrArray)
554  {
555  return self::getDb()->quoteArray($aStrArray);
556  }
557 
563  public function resetTblDescCache()
564  {
565  self::$_aTblDescCache = array();
566  }
567 
575  public function getTableDescription($sTableName)
576  {
577  // simple cache
578  if (isset(self::$_aTblDescCache[$sTableName])) {
579  return self::$_aTblDescCache[$sTableName];
580  }
581 
582  $aFields = self::getDb()->MetaColumns($sTableName);
583 
584  self::$_aTblDescCache[$sTableName] = $aFields;
585 
586  return $aFields;
587  }
588 
599  public function isValidFieldName($sField)
600  {
601  return ( boolean ) getStr()->preg_match("#^[\w\d\._]*$#", $sField);
602  }
603 
611  protected function _getConnectionId()
612  {
613  return self::getDb()->getDb()->connectionId;
614  }
615 
626  public function escapeString($sString)
627  {
628  if ('mysql' == self::_getConfigParam("_dbType")) {
629  return mysql_real_escape_string($sString, $this->_getConnectionId());
630  } elseif ('mysqli' == self::_getConfigParam("_dbType")) {
631  return mysqli_real_escape_string($this->_getConnectionId(), $sString);
632  } else {
633  return mysql_real_escape_string($sString, $this->_getConnectionId());
634  }
635  }
636 }