OXID eShop CE  4.9.5
 All Classes 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 
32  public static $configSet = false;
33 
39  protected static $_instance = null;
40 
46  protected static $_oDB = null;
47 
53  protected static $_aTblDescCache = array();
54 
60  private static $_dbType = '';
61 
67  private static $_dbUser = '';
68 
74  private static $_dbPwd = '';
75 
81  private static $_dbName = '';
82 
88  private static $_dbHost = '';
89 
95  private static $_iDebug = 0;
96 
102  private static $_blLogChangesInAdmin = false;
103 
109  private static $_iUtfMode = 0;
110 
116  private static $_sDefaultDatabaseConnection = null;
117 
123  private static $_aSlaveHosts;
124 
130  private static $_sAdminEmail;
131 
137  private static $_iMasterSlaveBalance;
138 
144  private static $_sLocalTimeFormat;
145 
151  private static $_sLocalDateFormat;
152 
158  public static function setConfig($oConfig)
159  {
160  self::$_dbType = $oConfig->getVar('dbType');
161  self::$_dbUser = $oConfig->getVar('dbUser');
162  self::$_dbPwd = $oConfig->getVar('dbPwd');
163  self::$_dbName = $oConfig->getVar('dbName');
164  self::$_dbHost = $oConfig->getVar('dbHost');
165  self::$_iDebug = $oConfig->getVar('iDebug');
166  self::$_blLogChangesInAdmin = $oConfig->getVar('blLogChangesInAdmin');
167  self::$_iUtfMode = $oConfig->getVar('iUtfMode');
168  self::$_sDefaultDatabaseConnection = $oConfig->getVar('sDefaultDatabaseConnection');
169  self::$_aSlaveHosts = $oConfig->getVar('aSlaveHosts');
170  self::$_iMasterSlaveBalance = $oConfig->getVar('iMasterSlaveBalance');
171  self::$_sAdminEmail = $oConfig->getVar('sAdminEmail');
172  self::$_sLocalTimeFormat = $oConfig->getVar('sLocalTimeFormat');
173  self::$_sLocalDateFormat = $oConfig->getVar('sLocalDateFormat');
174  }
175 
183  protected static function _getConfigParam($sConfigName)
184  {
185  if (isset(self::$$sConfigName)) {
186  return self::$$sConfigName;
187  }
188 
189  return null;
190  }
191 
197  public static function getInstance()
198  {
199  // disable caching for test modules
200  if (defined('OXID_PHP_UNIT')) {
201  self::$_instance = modInstances::getMod(__CLASS__);
202  }
203 
204  if (!self::$_instance instanceof oxDb) {
205 
206  //do not use simple oxNew here as it goes to eternal cycle
207  self::$_instance = new oxDb();
208 
209  if (defined('OXID_PHP_UNIT')) {
210  modInstances::addMod(__CLASS__, self::$_instance);
211  }
212  }
213 
214  return self::$_instance;
215  }
216 
222  protected function isAdmin()
223  {
224  return isAdmin();
225  }
226 
232  protected function _getModules()
233  {
234  $_iDebug = self::_getConfigParam('_iDebug');
235 
237 
238  $sModules = '';
239  if ($_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7) {
240  $sModules = 'perfmon';
241  }
242 
243  // log admin changes ?
244  if ($this->isAdmin() && self::_getConfigParam('_blLogChangesInAdmin')) {
245  $sModules .= ($sModules ? ':' : '') . 'oxadminlog';
246  }
247 
248  return $sModules;
249  }
250 
254  protected function _registerAdoDbExceptionHandler()
255  {
256  global $ADODB_EXCEPTION;
257  $ADODB_EXCEPTION = 'oxAdoDbException';
258 
259  include_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
260  }
261 
267  protected function _setUp($oDb)
268  {
269  $_iDebug = self::_getConfigParam('_iDebug');
270  if ($_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7) {
271  try {
272  $oDb->execute('truncate table adodb_logsql');
273  } catch (ADODB_Exception $e) {
274  // nothing
275  }
276  if (method_exists($oDb, "logSQL")) {
277  $oDb->logSQL(true);
278  }
279  }
280 
281  $oDb->cacheSecs = 60 * 10; // 10 minute caching
282  $oDb->execute('SET @@session.sql_mode = ""');
283 
284  if (self::_getConfigParam('_iUtfMode')) {
285  $oDb->execute('SET NAMES "utf8"');
286  $oDb->execute('SET CHARACTER SET utf8');
287  $oDb->execute('SET CHARACTER_SET_CONNECTION = utf8');
288  $oDb->execute('SET CHARACTER_SET_DATABASE = utf8');
289  $oDb->execute('SET character_set_results = utf8');
290  $oDb->execute('SET character_set_server = utf8');
291  } elseif (($sConn = self::_getConfigParam('_sDefaultDatabaseConnection')) != '') {
292  $oDb->execute('SET NAMES "' . $sConn . '"');
293  }
294  }
295 
305  protected function _sendMail($sEmail, $sSubject, $sBody)
306  {
307  include_once getShopBasePath() . 'core/phpmailer/class.phpmailer.php';
308  $oMailer = new phpmailer();
309  $oMailer->isMail();
310 
311  $oMailer->From = $sEmail;
312  $oMailer->AddAddress($sEmail);
313  $oMailer->Subject = $sSubject;
314  $oMailer->Body = $sBody;
315 
316  return $oMailer->send();
317  }
318 
324  protected function _notifyConnectionErrors($oDb)
325  {
326  // notifying shop owner about connection problems
327  if (($sAdminEmail = self::_getConfigParam('_sAdminEmail'))) {
328  $sFailedShop = isset($_REQUEST['shp']) ? addslashes($_REQUEST['shp']) : 'Base shop';
329 
330  $sDate = date('l dS of F Y h:i:s A');
331  $sScript = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
332  $sReferer = $_SERVER['HTTP_REFERER'];
333 
334  //sending a message to admin
335  $sWarningSubject = 'Offline warning!';
336  $sWarningBody = "
337  Database error in OXID eShop:
338  Date: {$sDate}
339  Shop: {$sFailedShop}
340 
341  mysql error: " . $oDb->errorMsg() . "
342  mysql error no: " . $oDb->errorNo() . "
343 
344  Script: {$sScript}
345  Referer: {$sReferer}";
346 
347  $this->_sendMail($sAdminEmail, $sWarningSubject, $sWarningBody);
348  }
349 
350  //only exception to default construction method
351  $oEx = new oxConnectionException();
352  $oEx->setMessage('EXCEPTION_CONNECTION_NODB');
353  $oEx->setConnectionError(self::_getConfigParam('_dbUser') . 's' . getShopBasePath() . $oDb->errorMsg());
354  throw $oEx;
355  }
356 
363  protected function _onConnectionError($oDb)
364  {
365  $sVerPrefix = '';
366  $sVerPrefix = '_ce';
367 
368 
369  $sConfig = join('', file(getShopBasePath() . 'config.inc.php'));
370 
371  if (strpos($sConfig, '<dbHost' . $sVerPrefix . '>') !== false &&
372  strpos($sConfig, '<dbName' . $sVerPrefix . '>') !== false
373  ) {
374  // pop to setup as there is something wrong
375  //oxRegistry::getUtils()->redirect( "setup/index.php", true, 302 );
376  $sHeaderCode = "HTTP/1.1 302 Found";
377  header($sHeaderCode);
378  header("Location: setup/index.php");
379  header("Connection: close");
380  exit();
381  } else {
382  // notifying about connection problems
383  $this->_notifyConnectionErrors($oDb);
384 
385  }
386  }
387 
388 
396  protected function _getDbInstance($iInstType = false)
397  {
398  $sHost = self::_getConfigParam("_dbHost");
399  $sUser = self::_getConfigParam("_dbUser");
400  $sPwd = self::_getConfigParam("_dbPwd");
401  $sName = self::_getConfigParam("_dbName");
402  $sType = self::_getConfigParam("_dbType");
403 
404  $oDb = ADONewConnection($sType, $this->_getModules());
405 
406 
407  try {
408  $oDb->connect($sHost, $sUser, $sPwd, $sName);
409  } catch (oxAdoDbException $e) {
410  $this->_onConnectionError($oDb);
411  }
412 
413  self::_setUp($oDb);
414 
415  return $oDb;
416  }
417 
427  public static function getDb($iFetchMode = oxDb::FETCH_MODE_NUM)
428  {
429  if (defined('OXID_PHP_UNIT')) {
430  if (isset(modDB::$unitMOD) && is_object(modDB::$unitMOD)) {
431  return modDB::$unitMOD;
432  }
433  }
434 
435  if (self::$_oDB === null) {
436 
437  $oInst = self::getInstance();
438 
439  //setting configuration on the first call
440  $oInst->setConfig(oxRegistry::get("oxConfigFile"));
441 
442  global $ADODB_SESSION_TBL,
443  $ADODB_SESSION_CONNECT,
444  $ADODB_SESSION_DRIVER,
445  $ADODB_SESSION_USER,
446  $ADODB_SESSION_PWD,
447  $ADODB_SESSION_DB,
448  $ADODB_SESS_LIFE,
449  $ADODB_SESS_DEBUG;
450 
451  // session related parameters. don't change.
452 
453  //Tomas
454  //the default setting is 3000 * 60, but actually changing this will give no effect as now redefinition of this constant
455  //appears after OXID custom settings are loaded and $ADODB_SESS_LIFE depends on user settings.
456  //You can find the redefinition of ADODB_SESS_LIFE @ oxconfig.php:: line ~ 390.
457  $ADODB_SESS_LIFE = 3000 * 60;
458  $ADODB_SESSION_TBL = "oxsessions";
459  $ADODB_SESSION_DRIVER = self::_getConfigParam('_dbType');
460  $ADODB_SESSION_USER = self::_getConfigParam('_dbUser');
461  $ADODB_SESSION_PWD = self::_getConfigParam('_dbPwd');
462  $ADODB_SESSION_DB = self::_getConfigParam('_dbName');
463  $ADODB_SESSION_CONNECT = self::_getConfigParam('_dbHost');
464  $ADODB_SESS_DEBUG = false;
465 
466  $oDb = new oxLegacyDb();
467  $oDbInst = $oInst->_getDbInstance();
468  $oDb->setConnection($oDbInst);
469 
470  self::$_oDB = $oDb;
471  }
472 
473  self::$_oDB->setFetchMode($iFetchMode);
474 
475  return self::$_oDB;
476  }
477 
487  public function quoteArray($aStrArray)
488  {
489  return self::getDb()->quoteArray($aStrArray);
490  }
491 
495  public function resetTblDescCache()
496  {
497  self::$_aTblDescCache = array();
498  }
499 
507  public function getTableDescription($sTableName)
508  {
509  // simple cache
510  if (isset(self::$_aTblDescCache[$sTableName])) {
511  return self::$_aTblDescCache[$sTableName];
512  }
513 
514  $aFields = self::getDb()->MetaColumns($sTableName);
515 
516  self::$_aTblDescCache[$sTableName] = $aFields;
517 
518  return $aFields;
519  }
520 
529  public function isValidFieldName($sField)
530  {
531  return ( boolean ) getStr()->preg_match("#^[\w\d\._]*$#", $sField);
532  }
533 
539  protected function _getConnectionId()
540  {
541  return self::getDb()->getDb()->connectionId;
542  }
543 
551  public function escapeString($sString)
552  {
553  if ('mysql' == self::_getConfigParam("_dbType")) {
554  return mysql_real_escape_string($sString, $this->_getConnectionId());
555  } elseif ('mysqli' == self::_getConfigParam("_dbType")) {
556  return mysqli_real_escape_string($this->_getConnectionId(), $sString);
557  } else {
558  return mysql_real_escape_string($sString, $this->_getConnectionId());
559  }
560  }
561 }