OXID eShop CE  4.9.9
 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  include_once getShopBasePath() . 'core/phpmailer/class.smtp.php';
309  $oMailer = new phpmailer();
310  $oMailer->isMail();
311 
312  $oMailer->From = $sEmail;
313  $oMailer->AddAddress($sEmail);
314  $oMailer->Subject = $sSubject;
315  $oMailer->Body = $sBody;
316 
317  return $oMailer->send();
318  }
319 
325  protected function _notifyConnectionErrors($oDb)
326  {
327  // notifying shop owner about connection problems
328  if (($sAdminEmail = self::_getConfigParam('_sAdminEmail'))) {
329  $sFailedShop = isset($_REQUEST['shp']) ? addslashes($_REQUEST['shp']) : 'Base shop';
330 
331  $sDate = date('l dS of F Y h:i:s A');
332  $sScript = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
333  $sReferer = $_SERVER['HTTP_REFERER'];
334 
335  //sending a message to admin
336  $sWarningSubject = 'Offline warning!';
337  $sWarningBody = "
338  Database error in OXID eShop:
339  Date: {$sDate}
340  Shop: {$sFailedShop}
341 
342  mysql error: " . $oDb->errorMsg() . "
343  mysql error no: " . $oDb->errorNo() . "
344 
345  Script: {$sScript}
346  Referer: {$sReferer}";
347 
348  $this->_sendMail($sAdminEmail, $sWarningSubject, $sWarningBody);
349  }
350 
351  //only exception to default construction method
352  $oEx = new oxConnectionException();
353  $oEx->setMessage('EXCEPTION_CONNECTION_NODB');
354  $oEx->setConnectionError(self::_getConfigParam('_dbUser') . 's' . getShopBasePath() . $oDb->errorMsg());
355  throw $oEx;
356  }
357 
364  protected function _onConnectionError($oDb)
365  {
366  $sVerPrefix = '';
367  $sVerPrefix = '_ce';
368 
369 
370  $sConfig = join('', file(getShopBasePath() . 'config.inc.php'));
371 
372  if (strpos($sConfig, '<dbHost' . $sVerPrefix . '>') !== false &&
373  strpos($sConfig, '<dbName' . $sVerPrefix . '>') !== false
374  ) {
375  // pop to setup as there is something wrong
376  //oxRegistry::getUtils()->redirect( "setup/index.php", true, 302 );
377  $sHeaderCode = "HTTP/1.1 302 Found";
378  header($sHeaderCode);
379  header("Location: setup/index.php");
380  header("Connection: close");
381  exit();
382  } else {
383  // notifying about connection problems
384  $this->_notifyConnectionErrors($oDb);
385 
386  }
387  }
388 
389 
397  protected function _getDbInstance($iInstType = false)
398  {
399  $sHost = self::_getConfigParam("_dbHost");
400  $sUser = self::_getConfigParam("_dbUser");
401  $sPwd = self::_getConfigParam("_dbPwd");
402  $sName = self::_getConfigParam("_dbName");
403  $sType = self::_getConfigParam("_dbType");
404 
405  $oDb = ADONewConnection($sType, $this->_getModules());
406 
407 
408  try {
409  $oDb->connect($sHost, $sUser, $sPwd, $sName);
410  } catch (oxAdoDbException $e) {
411  $this->_onConnectionError($oDb);
412  }
413 
414  self::_setUp($oDb);
415 
416  return $oDb;
417  }
418 
428  public static function getDb($iFetchMode = oxDb::FETCH_MODE_NUM)
429  {
430  if (defined('OXID_PHP_UNIT')) {
431  if (isset(modDB::$unitMOD) && is_object(modDB::$unitMOD)) {
432  return modDB::$unitMOD;
433  }
434  }
435 
436  if (self::$_oDB === null) {
437 
438  $oInst = self::getInstance();
439 
440  //setting configuration on the first call
441  $oInst->setConfig(oxRegistry::get("oxConfigFile"));
442 
443  global $ADODB_SESSION_TBL,
444  $ADODB_SESSION_CONNECT,
445  $ADODB_SESSION_DRIVER,
446  $ADODB_SESSION_USER,
447  $ADODB_SESSION_PWD,
448  $ADODB_SESSION_DB,
449  $ADODB_SESS_LIFE,
450  $ADODB_SESS_DEBUG;
451 
452  // session related parameters. don't change.
453 
454  //Tomas
455  //the default setting is 3000 * 60, but actually changing this will give no effect as now redefinition of this constant
456  //appears after OXID custom settings are loaded and $ADODB_SESS_LIFE depends on user settings.
457  //You can find the redefinition of ADODB_SESS_LIFE @ oxconfig.php:: line ~ 390.
458  $ADODB_SESS_LIFE = 3000 * 60;
459  $ADODB_SESSION_TBL = "oxsessions";
460  $ADODB_SESSION_DRIVER = self::_getConfigParam('_dbType');
461  $ADODB_SESSION_USER = self::_getConfigParam('_dbUser');
462  $ADODB_SESSION_PWD = self::_getConfigParam('_dbPwd');
463  $ADODB_SESSION_DB = self::_getConfigParam('_dbName');
464  $ADODB_SESSION_CONNECT = self::_getConfigParam('_dbHost');
465  $ADODB_SESS_DEBUG = false;
466 
467  $oDb = new oxLegacyDb();
468  $oDbInst = $oInst->_getDbInstance();
469  $oDb->setConnection($oDbInst);
470 
471  self::$_oDB = $oDb;
472  }
473 
474  self::$_oDB->setFetchMode($iFetchMode);
475 
476  return self::$_oDB;
477  }
478 
488  public function quoteArray($aStrArray)
489  {
490  return self::getDb()->quoteArray($aStrArray);
491  }
492 
496  public function resetTblDescCache()
497  {
498  self::$_aTblDescCache = array();
499  }
500 
508  public function getTableDescription($sTableName)
509  {
510  // simple cache
511  if (isset(self::$_aTblDescCache[$sTableName])) {
512  return self::$_aTblDescCache[$sTableName];
513  }
514 
515  $aFields = self::getDb()->MetaColumns($sTableName);
516 
517  self::$_aTblDescCache[$sTableName] = $aFields;
518 
519  return $aFields;
520  }
521 
530  public function isValidFieldName($sField)
531  {
532  return ( boolean ) getStr()->preg_match("#^[\w\d\._]*$#", $sField);
533  }
534 
540  protected function _getConnectionId()
541  {
542  return self::getDb()->getDb()->connectionId;
543  }
544 
552  public function escapeString($sString)
553  {
554  if ('mysql' == self::_getConfigParam("_dbType")) {
555  return mysql_real_escape_string($sString, $this->_getConnectionId());
556  } elseif ('mysqli' == self::_getConfigParam("_dbType")) {
557  return mysqli_real_escape_string($this->_getConnectionId(), $sString);
558  } else {
559  return mysql_real_escape_string($sString, $this->_getConnectionId());
560  }
561  }
562 }