00001 <?php
00002
00003
00004
00005 require_once getShopBasePath() . 'core/adodblite/adodb.inc.php';
00006
00010 class oxDb
00011 {
00016 const FETCH_MODE_NUM = ADODB_FETCH_NUM;
00017
00022 const FETCH_MODE_ASSOC = ADODB_FETCH_ASSOC;
00023
00028 public static $configSet = false;
00029
00035 protected static $_instance = null;
00036
00042 protected static $_oDB = null;
00043
00049 protected static $_aTblDescCache = array();
00050
00055 private static $_dbType = '';
00056
00061 private static $_dbUser = '';
00062
00067 private static $_dbPwd = '';
00068
00073 private static $_dbName = '';
00074
00079 private static $_dbHost = '';
00080
00085 private static $_iDebug = 0;
00086
00091 private static $_blLogChangesInAdmin = false;
00092
00097 private static $_iUtfMode = 0;
00098
00103 private static $_sDefaultDatabaseConnection = null;
00104
00109 private static $_aSlaveHosts;
00110
00115 private static $_sAdminEmail;
00116
00121 private static $_iMasterSlaveBalance;
00122
00127 private static $_sLocalTimeFormat;
00128
00133 private static $_sLocalDateFormat;
00134
00142 public static function setConfig( $oConfig )
00143 {
00144 self::$_dbType = $oConfig->getVar( 'dbType' );
00145 self::$_dbUser = $oConfig->getVar( 'dbUser' );
00146 self::$_dbPwd = $oConfig->getVar( 'dbPwd' );
00147 self::$_dbName = $oConfig->getVar( 'dbName' );
00148 self::$_dbHost = $oConfig->getVar( 'dbHost' );
00149 self::$_iDebug = $oConfig->getVar( 'iDebug' );
00150 self::$_blLogChangesInAdmin = $oConfig->getVar( 'blLogChangesInAdmin' );
00151 self::$_iUtfMode = $oConfig->getVar( 'iUtfMode' );
00152 self::$_sDefaultDatabaseConnection = $oConfig->getVar( 'sDefaultDatabaseConnection' );
00153 self::$_aSlaveHosts = $oConfig->getVar( 'aSlaveHosts' );
00154 self::$_iMasterSlaveBalance = $oConfig->getVar( 'iMasterSlaveBalance' );
00155 self::$_sAdminEmail = $oConfig->getVar( 'sAdminEmail' );
00156 self::$_sLocalTimeFormat = $oConfig->getVar( 'sLocalTimeFormat' );
00157 self::$_sLocalDateFormat = $oConfig->getVar( 'sLocalDateFormat' );
00158 }
00159
00167 protected static function _getConfigParam( $sConfigName )
00168 {
00169 if ( isset( self::$$sConfigName ) ) {
00170 return self::$$sConfigName;
00171 }
00172
00173 return null;
00174 }
00175
00181 public static function getInstance()
00182 {
00183
00184 if ( defined( 'OXID_PHP_UNIT' ) ) {
00185 self::$_instance = modInstances::getMod( __CLASS__ );
00186 }
00187
00188 if ( !self::$_instance instanceof oxDb ) {
00189
00190
00191 self::$_instance = new oxDb();
00192
00193 if ( defined( 'OXID_PHP_UNIT' ) ) {
00194 modInstances::addMod( __CLASS__, self::$_instance);
00195 }
00196 }
00197 return self::$_instance;
00198 }
00199
00205 protected function isAdmin()
00206 {
00207 return isAdmin();
00208 }
00209
00215 protected function _getModules()
00216 {
00217
00218 $_iDebug = self::_getConfigParam( '_iDebug' );
00219
00220 global $ADODB_EXCEPTION;
00221 $ADODB_EXCEPTION = 'oxAdoDbException';
00222 include_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
00223
00224 $sModules = '';
00225 if ( $_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7 ) {
00226 $sModules = 'perfmon';
00227 }
00228
00229
00230 if ( $this->isAdmin() && self::_getConfigParam( '_blLogChangesInAdmin' ) ) {
00231 $sModules .= ( $sModules ? ':' : '' ) . 'oxadminlog';
00232 }
00233
00234 return $sModules;
00235 }
00236
00244 protected function _setUp( $oDb )
00245 {
00246 $_iDebug = self::_getConfigParam( '_iDebug' );
00247 if ( $_iDebug == 2 || $_iDebug == 3 || $_iDebug == 4 || $_iDebug == 7 ) {
00248 try {
00249 $oDb->execute( 'truncate table adodb_logsql' );
00250 } catch ( ADODB_Exception $e ) {
00251
00252 }
00253 if ( method_exists( $oDb, "logSQL" ) ) {
00254 $oDb->logSQL( true );
00255 }
00256 }
00257
00258 $oDb->cacheSecs = 60 * 10;
00259 $oDb->execute( 'SET @@session.sql_mode = ""' );
00260
00261 if ( self::_getConfigParam( '_iUtfMode' ) ) {
00262 $oDb->execute( 'SET NAMES "utf8"' );
00263 $oDb->execute( 'SET CHARACTER SET utf8' );
00264 $oDb->execute( 'SET CHARACTER_SET_CONNECTION = utf8' );
00265 $oDb->execute( 'SET CHARACTER_SET_DATABASE = utf8' );
00266 $oDb->execute( 'SET character_set_results = utf8' );
00267 $oDb->execute( 'SET character_set_server = utf8' );
00268 } elseif ( ( $sConn = self::_getConfigParam('_sDefaultDatabaseConnection') ) != '' ) {
00269 $oDb->execute( 'SET NAMES "' . $sConn . '"' );
00270 }
00271 }
00272
00282 protected function _sendMail( $sEmail, $sSubject, $sBody )
00283 {
00284 include_once getShopBasePath() . 'core/phpmailer/class.phpmailer.php';
00285 $oMailer = new phpmailer();
00286 $oMailer->isMail();
00287
00288 $oMailer->From = $sEmail;
00289 $oMailer->AddAddress( $sEmail );
00290 $oMailer->Subject = $sSubject;
00291 $oMailer->Body = $sBody;
00292 return $oMailer->send();
00293 }
00294
00302 protected function _notifyConnectionErrors( $oDb )
00303 {
00304
00305 if ( ( $sAdminEmail = self::_getConfigParam( '_sAdminEmail' ) ) ) {
00306 $sFailedShop = isset( $_REQUEST['shp'] ) ? addslashes( $_REQUEST['shp'] ) : 'Base shop';
00307
00308 $sDate = date( 'l dS of F Y h:i:s A');
00309 $sScript = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
00310 $sReferer = $_SERVER['HTTP_REFERER'];
00311
00312
00313 $sWarningSubject = 'Offline warning!';
00314 $sWarningBody = "
00315 Database error in OXID eShop:
00316 Date: {$sDate}
00317 Shop: {$sFailedShop}
00318
00319 mysql error: " . $oDb->errorMsg()."
00320 mysql error no: " . $oDb->errorNo()."
00321
00322 Script: {$sScript}
00323 Referer: {$sReferer}";
00324
00325 $this->_sendMail( $sAdminEmail, $sWarningSubject, $sWarningBody );
00326 }
00327
00328
00329 $oEx = new oxConnectionException();
00330 $oEx->setMessage( 'EXCEPTION_CONNECTION_NODB' );
00331 $oEx->setConnectionError( self::_getConfigParam( '_dbUser' ) . 's' . getShopBasePath() . $oDb->errorMsg() );
00332 throw $oEx;
00333 }
00334
00343 protected function _onConnectionError( $oDb )
00344 {
00345 $sVerPrefix = '';
00346 $sVerPrefix = '_ce';
00347
00348
00349
00350 $sConfig = join( '', file( getShopBasePath().'config.inc.php' ) );
00351
00352 if ( strpos( $sConfig, '<dbHost'.$sVerPrefix.'>' ) !== false &&
00353 strpos( $sConfig, '<dbName'.$sVerPrefix.'>' ) !== false ) {
00354
00355
00356 $sHeaderCode = "HTTP/1.1 302 Found";
00357 header( $sHeaderCode );
00358 header( "Location: setup/index.php" );
00359 header( "Connection: close" );
00360 exit();
00361 } else {
00362
00363 $this->_notifyConnectionErrors( $oDb );
00364
00365 }
00366 }
00367
00368
00376 protected function _getDbInstance( $iInstType = false )
00377 {
00378 $sHost = self::_getConfigParam( "_dbHost" );
00379 $sUser = self::_getConfigParam( "_dbUser" );
00380 $sPwd = self::_getConfigParam( "_dbPwd" );
00381 $sName = self::_getConfigParam( "_dbName" );
00382 $sType = self::_getConfigParam( "_dbType" );
00383
00384 $oDb = ADONewConnection( $sType, $this->_getModules() );
00385
00386
00387 if ( !$oDb->connect( $sHost, $sUser, $sPwd, $sName ) ) {
00388 $this->_onConnectionError( $oDb );
00389 }
00390
00391 self::_setUp( $oDb );
00392
00393 return $oDb;
00394 }
00395
00405 public static function getDb( $iFetchMode = oxDb::FETCH_MODE_NUM )
00406 {
00407 if ( defined( 'OXID_PHP_UNIT' ) ) {
00408 if ( isset( modDB::$unitMOD ) && is_object( modDB::$unitMOD ) ) {
00409 return modDB::$unitMOD;
00410 }
00411 }
00412
00413 if ( self::$_oDB === null ) {
00414
00415 $oInst = self::getInstance();
00416
00417
00418 $oInst->setConfig( oxRegistry::get("oxConfigFile") );
00419
00420 global $ADODB_SESSION_TBL,
00421 $ADODB_SESSION_CONNECT,
00422 $ADODB_SESSION_DRIVER,
00423 $ADODB_SESSION_USER,
00424 $ADODB_SESSION_PWD,
00425 $ADODB_SESSION_DB,
00426 $ADODB_SESS_LIFE,
00427 $ADODB_SESS_DEBUG;
00428
00429
00430
00431
00432
00433
00434
00435 $ADODB_SESS_LIFE = 3000 * 60;
00436 $ADODB_SESSION_TBL = "oxsessions";
00437 $ADODB_SESSION_DRIVER = self::_getConfigParam( '_dbType' );
00438 $ADODB_SESSION_USER = self::_getConfigParam( '_dbUser' );
00439 $ADODB_SESSION_PWD = self::_getConfigParam( '_dbPwd' );
00440 $ADODB_SESSION_DB = self::_getConfigParam( '_dbName' );
00441 $ADODB_SESSION_CONNECT = self::_getConfigParam( '_dbHost' );
00442 $ADODB_SESS_DEBUG = false;
00443
00444 $oDb = new oxLegacyDb();
00445 $oDbInst = $oInst->_getDbInstance();
00446 $oDb->setConnection( $oDbInst );
00447
00448 self::$_oDB = $oDb;
00449 }
00450
00451 self::$_oDB->setFetchMode( $iFetchMode );
00452
00453 return self::$_oDB;
00454 }
00455
00463 public function quoteArray( $aStrArray )
00464 {
00465 $oDb = self::getDb();
00466
00467 foreach ( $aStrArray as $sKey => $sString ) {
00468 $aStrArray[$sKey] = $oDb->quote( $sString );
00469 }
00470 return $aStrArray;
00471 }
00472
00478 public function resetTblDescCache()
00479 {
00480 self::$_aTblDescCache = array();
00481 }
00482
00490 public function getTableDescription( $sTableName )
00491 {
00492
00493 if ( isset( self::$_aTblDescCache[$sTableName] ) ) {
00494 return self::$_aTblDescCache[$sTableName];
00495 }
00496
00497 $aFields = self::getDb()->MetaColumns( $sTableName );
00498
00499 self::$_aTblDescCache[$sTableName] = $aFields;
00500
00501 return $aFields;
00502 }
00503
00515 public function convertDBDateTime( $oObject, $blToTimeStamp = false, $blOnlyDate = false )
00516 {
00517 return oxRegistry::get('oxUtilsDate')->convertDBDateTime( $oObject, $blToTimeStamp, $blOnlyDate );
00518 }
00519
00530 public function convertDBTimestamp( $oObject, $blToTimeStamp = false )
00531 {
00532 return oxRegistry::get('oxUtilsDate')->convertDBTimestamp( $oObject, $blToTimeStamp );
00533 }
00534
00545 public function convertDBDate( $oObject, $blToTimeStamp = false )
00546 {
00547 return oxRegistry::get('oxUtilsDate')->convertDBDate( $oObject, $blToTimeStamp );
00548 }
00549
00558 public function isValidFieldName( $sField )
00559 {
00560 return ( boolean ) getStr()->preg_match( "#^[\w\d\._]*$#", $sField );
00561 }
00562
00576 protected function _setDefaultFormatedValue( $oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00577 {
00578 }
00579
00589 protected function _defineAndCheckDefaultTimeValues( $blToTimeStamp )
00590 {
00591 }
00592
00602 protected function _defineAndCheckDefaultDateValues( $blToTimeStamp )
00603 {
00604 }
00605
00613 protected function _defaultDatePattern()
00614 {
00615 }
00616
00624 protected function _defaultTimePattern()
00625 {
00626 }
00627
00635 protected function _regexp2ValidateDateInput()
00636 {
00637 }
00638
00646 protected function _regexp2ValidateTimeInput()
00647 {
00648 }
00649
00657 protected function _defineDateFormattingRules()
00658 {
00659
00660 $aDFormats = array("ISO" => array("Y-m-d", array(2, 3, 1), "0000-00-00"),
00661 "EUR" => array("d.m.Y", array(2, 1, 3), "00.00.0000"),
00662 "USA" => array("m/d/Y", array(1, 2, 3), "00/00/0000")
00663 );
00664 return $aDFormats;
00665 }
00666
00674 protected function _defineTimeFormattingRules()
00675 {
00676
00677 $aTFormats = array("ISO" => array("H:i:s", array(1, 2, 3 ), "00:00:00"),
00678 "EUR" => array("H.i.s", array(1, 2, 3 ), "00.00.00"),
00679 "USA" => array("h:i:s A", array(1, 2, 3 ), "00:00:00 AM")
00680 );
00681 return $aTFormats;
00682 }
00683
00696 protected function _setDefaultDateTimeValue( $oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00697 {
00698 $aDFormats = $this->_defineDateFormattingRules();
00699 $aTFormats = $this->_defineTimeFormattingRules();
00700
00701 $sReturn = $aDFormats[$sLocalDateFormat][2];
00702 if ( !$blOnlyDate) {
00703 $sReturn .= " ".$aTFormats[$sLocalTimeFormat][2];
00704 }
00705
00706 if ($oObject instanceof oxField) {
00707 $oObject->setValue(trim($sReturn));
00708 } else {
00709 $oObject->value = trim($sReturn);
00710 }
00711
00712 $oObject->fldmax_length = strlen( $oObject->value);
00713 }
00714
00727 protected function _setDate( $oObject, $sDateFormat, $aDFields, $aDateMatches )
00728 {
00729
00730 $iTimestamp = mktime( 0, 0, 0, $aDateMatches[$aDFields[0]],
00731 $aDateMatches[$aDFields[1]],
00732 $aDateMatches[$aDFields[2]]);
00733
00734 if ($oObject instanceof oxField) {
00735 $oObject->setValue(@date( $sDateFormat, $iTimestamp ));
00736 } else {
00737 $oObject->value = @date( $sDateFormat, $iTimestamp );
00738 }
00739
00740 $oObject->fldmax_length = strlen( $oObject->value );
00741 }
00742
00758 protected function _formatCorrectTimeValue( $oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields )
00759 {
00760
00761 $iTimestamp = @mktime( (int) $aTimeMatches[$aTFields[0]],
00762 (int) $aTimeMatches[$aTFields[1]],
00763 (int) $aTimeMatches[$aTFields[2]],
00764 (int) $aDateMatches[$aDFields[0]],
00765 (int) $aDateMatches[$aDFields[1]],
00766 (int) $aDateMatches[$aDFields[2]] );
00767
00768 if ($oObject instanceof oxField) {
00769 $oObject->setValue(trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) ));
00770 } else {
00771 $oObject->value = trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) );
00772 }
00773
00774
00775 $oObject->fldmax_length = strlen( $oObject->value );
00776 }
00777
00783 protected function _getConnectionId()
00784 {
00785 return self::getDb()->getDb()->connectionId;
00786 }
00787
00795 public function escapeString( $sString )
00796 {
00797 if ( 'mysql' == self::_getConfigParam( "_dbType" )) {
00798 return mysql_real_escape_string( $sString, $this->_getConnectionId() );
00799 } elseif ( 'mysqli' == self::_getConfigParam( "_dbType" )) {
00800 return mysqli_real_escape_string( $this->_getConnectionId(), $sString );
00801 } else {
00802 return mysql_real_escape_string( $sString, $this->_getConnectionId() );
00803 }
00804 }
00805
00815 public function updateViews( $aTables = null )
00816 {
00817 $oMetaData = oxNew('oxDbMetaDataHandler');
00818 return $oMetaData->updateViews();
00819 }
00820 }