00001 <?php
00002
00003
00004
00005 require_once getShopBasePath() . 'core/adodblite/adodb.inc.php';
00006
00010 class oxDb extends oxSuperCfg
00011 {
00017 protected static $_instance = null;
00018
00024 protected static $_oDB = null;
00025
00031 protected static $_aTblDescCache = array();
00032
00038 public static function getInstance()
00039 {
00040
00041 if ( defined( 'OXID_PHP_UNIT' ) ) {
00042 self::$_instance = modInstances::getMod( __CLASS__ );
00043 }
00044
00045
00046 if ( !self::$_instance instanceof oxDb ) {
00047
00048
00049 self::$_instance = oxNew( 'oxdb' );
00050
00051 if ( defined( 'OXID_PHP_UNIT' ) ) {
00052 modInstances::addMod( __CLASS__, self::$_instance);
00053 }
00054 }
00055 return self::$_instance;
00056 }
00057
00067 public static function getDb( $blAssoc = false )
00068 {
00069 global $ADODB_FETCH_MODE;
00070
00071 if ( $blAssoc ) {
00072 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00073 } else {
00074 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00075 }
00076
00077 if ( defined( 'OXID_PHP_UNIT' ) ) {
00078 if ( isset( modDB::$unitMOD ) && is_object( modDB::$unitMOD ) ) {
00079 return modDB::$unitMOD;
00080 }
00081 }
00082
00083 if ( self::$_oDB !== null ) {
00084 return self::$_oDB;
00085 }
00086
00087 global $ADODB_CACHE_DIR;
00088 global $ADODB_DRIVER,
00089 $ADODB_SESSION_TBL,
00090 $ADODB_SESSION_CONNECT,
00091 $ADODB_SESSION_DRIVER,
00092 $ADODB_SESSION_USER,
00093 $ADODB_SESSION_PWD,
00094 $ADODB_SESSION_DB,
00095 $ADODB_SESS_LIFE,
00096 $ADODB_SESS_DEBUG;
00097
00098
00099 $myConfig = self::getInstance()->getConfig();
00100 $iDebug = $myConfig->getConfigParam( 'iDebug' );
00101 if ( $iDebug ) {
00102 include_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 $ADODB_SESS_LIFE = 3000 * 60;
00112 $ADODB_SESSION_TBL = "oxsessions";
00113 $ADODB_SESSION_DRIVER = $myConfig->getConfigParam( 'dbType' );
00114 $ADODB_SESSION_USER = $myConfig->getConfigParam( 'dbUser' );
00115 $ADODB_SESSION_PWD = $myConfig->getConfigParam( 'dbPwd' );
00116 $ADODB_SESSION_DB = $myConfig->getConfigParam( 'dbName' );
00117 $ADODB_SESSION_CONNECT = $myConfig->getConfigParam( 'dbHost' );
00118 $ADODB_SESS_DEBUG = false;
00119 $ADODB_CACHE_DIR = $myConfig->getConfigParam( 'sCompileDir' );
00120
00121 $sModules = '';
00122 if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 || $iDebug == 7 ) {
00123 $sModules = 'perfmon';
00124 }
00125
00126
00127 if ( $myConfig->isAdmin() && $myConfig->getConfigParam( 'blLogChangesInAdmin' ) ) {
00128 $sModules = ( $sModules ? ':' : '' ) . 'oxadminlog';
00129 }
00130
00131 self::$_oDB = ADONewConnection( $myConfig->getConfigParam( 'dbType' ), $sModules );
00132
00133 $sVerPrefix = '';
00134 $sVerPrefix = '_ce';
00135
00136 if ( !self::$_oDB->connect( $myConfig->getConfigParam( 'dbHost' ), $myConfig->getConfigParam( 'dbUser' ), $myConfig->getConfigParam( 'dbPwd' ), $myConfig->getConfigParam( 'dbName' ) ) ) {
00137 $sConfig = join( '', file( getShopBasePath().'config.inc.php' ) );
00138 if ( strpos( $sConfig, '<dbHost'.$sVerPrefix.'>' ) !== false &&
00139 strpos( $sConfig, '<dbName'.$sVerPrefix.'>' ) !== false ) {
00140 header( 'location:setup/index.php' );
00141 oxUtils::getInstance()->showMessageAndExit( "" );
00142 } else {
00143
00144
00145 $sFailedShop = isset( $_REQUEST['shp'] )?addslashes( $_REQUEST['shp'] ):'Base shop';
00146
00147 $sDate = date( 'l dS of F Y h:i:s A');
00148 $sScript = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
00149 $sReferer = $_SERVER['HTTP_REFERER'];
00150
00151
00152 $sWarningSubject = 'Offline warning!';
00153 $sWarningBody = "
00154 Database error in OXID eShop:
00155 Date: $sDate
00156 Shop: $sFailedShop
00157
00158 mysql error: ".self::$_oDB->errorMsg()."
00159 mysql error no: ".self::$_oDB->errorNo()."
00160
00161 Script: $sScript
00162 Referer: $sReferer";
00163
00164 if ( ( $sAdminEmail = $myConfig->getConfigParam( 'sAdminEmail' ) ) ) {
00165 include 'core/phpmailer/class.phpmailer.php';
00166
00167 $oMailer = new phpmailer();
00168 $oMailer->isMail();
00169 $oMailer->From = $sAdminEmail;
00170 $oMailer->AddAddress( $sAdminEmail );
00171 $oMailer->Subject = $sWarningSubject;
00172 $oMailer->Body = $sWarningBody;
00173 $oMailer->send();
00174 }
00175
00176
00177 $oEx = new oxConnectionException();
00178 $oEx->setMessage( 'EXCEPTION_CONNECTION_NODB' );
00179 $oEx->setConnectionError( $myConfig->getConfigParam( 'dbUser' ).'s'.getShopBasePath().self::$_oDB->errorMsg() );
00180 throw $oEx;
00181 }
00182 }
00183
00184 if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 || $iDebug == 7 ) {
00185 try {
00186 self::$_oDB->execute('truncate table adodb_logsql;');
00187 } catch (ADODB_Exception $e) {
00188
00189 }
00190 self::$_oDB->logSQL( true );
00191 }
00192
00193 self::$_oDB->cacheSecs = 60 * 10;
00194 self::$_oDB->execute( 'SET @@session.sql_mode = ""' );
00195
00196 if ( $myConfig->isUtf() ) {
00197 self::$_oDB->execute( 'SET NAMES "utf8"' );
00198 self::$_oDB->execute( 'SET CHARACTER SET utf8' );
00199 self::$_oDB->execute( 'SET CHARACTER_SET_CONNECTION = utf8' );
00200 self::$_oDB->execute( 'SET CHARACTER_SET_DATABASE = utf8' );
00201 self::$_oDB->execute( 'SET character_set_results = utf8' );
00202 self::$_oDB->execute( 'SET character_set_server = utf8' );
00203 }
00204
00205 return self::$_oDB;
00206 }
00207
00215 public function getMultiLangFieldName( $sField )
00216 {
00217
00218
00219
00220
00221
00222
00223 return $sField . oxLang::getInstance()->getLanguageTag();
00224 }
00225
00234 public function isQuoteNeeded( $sFieldtype)
00235 {
00236 $aTypesWoQuotes = array('int', 'decimal', 'float', 'tinyint', 'smallint', 'mediumint', 'bigint', 'double');
00237 return !in_array( $sFieldtype, $aTypesWoQuotes);
00238 }
00239
00247 public function quoteArray( $aStrArray)
00248 {
00249 foreach ( $aStrArray as $sKey => $sString ) {
00250 $aStrArray[$sKey] = self::getDb()->quote($sString);
00251 }
00252 return $aStrArray;
00253 }
00254
00262 public function getTableDescription( $sTableName )
00263 {
00264
00265 if ( isset( self::$_aTblDescCache[$sTableName] ) ) {
00266 return self::$_aTblDescCache[$sTableName];
00267 }
00268
00269 $aFields = self::getDb()->MetaColumns( $sTableName );
00270
00271 self::$_aTblDescCache[$sTableName] = $aFields;
00272
00273 return $aFields;
00274 }
00275
00285 public function convertDBDateTime( $oObject, $blToTimeStamp = false, $blOnlyDate = false )
00286 {
00287 $sDate = $oObject->value;
00288
00289
00290 $sLocalDateFormat = $this->_defineAndCheckDefaultDateValues( $blToTimeStamp );
00291 $sLocalTimeFormat = $this->_defineAndCheckDefaultTimeValues( $blToTimeStamp );
00292
00293
00294 $aDefDatePatterns = $this->_defaultDatePattern();
00295
00296
00297 $aDatePatterns = $this->_regexp2ValidateDateInput();
00298 $aTimePatterns = $this->_regexp2ValidateTimeInput();
00299
00300
00301 $aDFormats = $this->_defineDateFormattingRules();
00302 $aTFormats = $this->_defineTimeFormattingRules();
00303
00304
00305 if ( !$sDate) {
00306 $this->_setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00307 return $oObject->value;
00308 }
00309
00310 $blDefDateFound = false;
00311 $oStr = getStr();
00312
00313
00314 foreach ( array_keys( $aDefDatePatterns ) as $sDefDatePattern ) {
00315 if ( $oStr->preg_match( $sDefDatePattern, $sDate)) {
00316 $blDefDateFound = true;
00317 break;
00318 }
00319 }
00320
00321
00322 if ( $blDefDateFound) {
00323 $this->_setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00324 return $oObject->value;
00325 }
00326
00327 $blDateFound = false;
00328 $blTimeFound = false;
00329 $aDateMatches = array();
00330 $aTimeMatches = array();
00331
00332
00333 foreach ( $aDatePatterns as $sPattern => $sType) {
00334 if ( $oStr->preg_match( $sPattern, $sDate, $aDateMatches)) {
00335 $blDateFound = true;
00336
00337
00338 $sDateFormat = $aDFormats[$sLocalDateFormat][0];
00339 $aDFields = $aDFormats[$sType][1];
00340 break;
00341 }
00342 }
00343
00344
00345 if ( !$blDateFound) {
00346 return $sDate;
00347 }
00348
00349 if ( $blOnlyDate) {
00350 $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00351 return $oObject->value;
00352 }
00353
00354
00355 foreach ( $aTimePatterns as $sPattern => $sType) {
00356 if ( $oStr->preg_match( $sPattern, $sDate, $aTimeMatches)) {
00357 $blTimeFound = true;
00358
00359
00360 $sTimeFormat = $aTFormats[$sLocalTimeFormat][0];
00361 $aTFields = $aTFormats[$sType][1];
00362
00363
00364 if ( $sType == "USA" && isset($aTimeMatches[4])) {
00365 $iIntVal = (int) $aTimeMatches[1];
00366 if ( $aTimeMatches[4] == "PM") {
00367 if ( $iIntVal < 13) {
00368 $iIntVal += 12;
00369 }
00370 } elseif ( $aTimeMatches[4] == "AM" && $aTimeMatches[1] == "12") {
00371 $iIntVal = 0;
00372 }
00373
00374 $aTimeMatches[1] = sprintf("%02d", $iIntVal);
00375 }
00376
00377 break;
00378 }
00379 }
00380
00381 if ( !$blTimeFound) {
00382
00383
00384 $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00385 return $oObject->value;
00386 }
00387
00388 $this->_formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields);
00389
00390
00391 if ( !$oObject->fldmax_length) {
00392 return $this->convertDBDateTime( $oObject, $blToTimeStamp, $blOnlyDate);
00393 }
00394 return $oObject->value;
00395 }
00396
00405 public function convertDBTimestamp( $oObject, $blToTimeStamp = false )
00406 {
00407
00408 $sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
00409 $sISOTimeStampPattern = "/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/";
00410 $aMatches = array();
00411 $oStr = getStr();
00412
00413
00414 if ( $blToTimeStamp) {
00415
00416 $this->convertDBDateTime( $oObject, $blToTimeStamp );
00417
00418 if ( $oStr->preg_match( $sISOTimeStampPattern, $oObject->value, $aMatches)) {
00419
00420 $oObject->setValue($aMatches[1].$aMatches[2].$aMatches[3].$aMatches[4].$aMatches[5].$aMatches[6]);
00421 $oObject->fldmax_length = strlen( $oObject->value);
00422 return $oObject->value;
00423 }
00424 } else {
00425
00426
00427
00428 if ( $oStr->preg_match( $sSQLTimeStampPattern, $oObject->value, $aMatches ) ) {
00429 $iTimestamp = mktime( $aMatches[4],
00430 $aMatches[5],
00431 $aMatches[6],
00432 $aMatches[2],
00433 $aMatches[3],
00434 $aMatches[1]);
00435 if ( !$iTimestamp ) {
00436 $iTimestamp = "0";
00437 }
00438
00439 $oObject->setValue(trim( date( "Y-m-d H:i:s", $iTimestamp)));
00440 $oObject->fldmax_length = strlen( $oObject->value);
00441 $this->convertDBDateTime( $oObject, $blToTimeStamp );
00442 return $oObject->value;
00443 }
00444 }
00445 }
00446
00455 public function convertDBDate( $oObject, $blToTimeStamp = false )
00456 {
00457 return $this->convertDBDateTime( $oObject, $blToTimeStamp, true );
00458 }
00459
00467 public function createSQLList( $aArray )
00468 {
00469 $sRet = "";
00470
00471 $blSep = false;
00472 foreach ( $aArray as $aToken) {
00473 if ( !$aToken[0]) {
00474 continue;
00475 }
00476 if ( $blSep) {
00477 $sRet .= ",";
00478 }
00479 $sRet .= "'".$aToken[0]."'";
00480 $blSep = true;
00481 }
00482 return $sRet;
00483 }
00484
00490 static public function startTransaction()
00491 {
00492 self::$_oDB->execute( 'START TRANSACTION' );
00493 }
00494
00500 static public function commitTransaction()
00501 {
00502 self::$_oDB->execute( 'COMMIT' );
00503 }
00504
00510 static public function rollbackTransaction()
00511 {
00512 self::$_oDB->execute( 'ROLLBACK' );
00513 }
00514
00523 static public function setTransactionIsolationLevel( $sLevel = null )
00524 {
00525 $aLevels = array( 'READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ', 'SERIALIZABLE' );
00526
00527 if (in_array(strtoupper($sLevel), $aLevels)) {
00528 self::$_oDB->execute( 'SET TRANSACTION ISOLATION LEVEL ' . $sLevel );
00529 }
00530 }
00531
00532
00541 public function isValidFieldName( $sField )
00542 {
00543 return ( boolean ) getStr()->preg_match( "#^[\w\d\._]*$#", $sField );
00544 }
00545
00557 protected function _setDefaultFormatedValue( $oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00558 {
00559 $aDefTimePatterns = $this->_defaultTimePattern();
00560 $aDFormats = $this->_defineDateFormattingRules();
00561 $aTFormats = $this->_defineTimeFormattingRules();
00562 $oStr = getStr();
00563
00564 foreach ( array_keys( $aDefTimePatterns ) as $sDefTimePattern ) {
00565 if ( $oStr->preg_match( $sDefTimePattern, $sDate ) ) {
00566 $blDefTimeFound = true;
00567 break;
00568 }
00569 }
00570
00571
00572 if ( $blOnlyDate) {
00573 $oObject->setValue(trim( $aDFormats[$sLocalDateFormat][2] ));
00574
00575 $oObject->fldmax_length = strlen( $oObject->value );
00576 return ;
00577 } elseif ( $blDefTimeFound ) {
00578
00579 $oObject->setValue(trim( $aDFormats[$sLocalDateFormat][2] . " " . $aTFormats[$sLocalTimeFormat][2] ));
00580
00581 $oObject->fldmax_length = strlen( $oObject->value );
00582 return ;
00583 }
00584 }
00585
00593 protected function _defineAndCheckDefaultTimeValues( $blToTimeStamp )
00594 {
00595
00596
00597 $sLocalTimeFormat = $this->getConfig()->getConfigParam( 'sLocalTimeFormat' );
00598 if ( !$sLocalTimeFormat || $blToTimeStamp) {
00599 $sLocalTimeFormat = "ISO";
00600 }
00601 return $sLocalTimeFormat;
00602 }
00603
00611 protected function _defineAndCheckDefaultDateValues( $blToTimeStamp )
00612 {
00613
00614
00615 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00616 if ( !$sLocalDateFormat || $blToTimeStamp) {
00617 $sLocalDateFormat = "ISO";
00618 }
00619 return $sLocalDateFormat;
00620 }
00621
00627 protected function _defaultDatePattern()
00628 {
00629
00630 $aDefDatePatterns = array("/^0000-00-00/" => "ISO",
00631 "/^00\.00\.0000/" => "EUR",
00632 "/^00\/00\/0000/" => "USA"
00633 );
00634 return $aDefDatePatterns;
00635 }
00636
00642 protected function _defaultTimePattern()
00643 {
00644
00645 $aDefTimePatterns = array("/00:00:00$/" => "ISO",
00646 "/00\.00\.00$/" => "EUR",
00647 "/00:00:00 AM$/" => "USA"
00648 );
00649 return $aDefTimePatterns;
00650 }
00651
00657 protected function _regexp2ValidateDateInput()
00658 {
00659
00660 $aDatePatterns = array("/^([0-9]{4})-([0-9]{2})-([0-9]{2})/" => "ISO",
00661 "/^([0-9]{2})\.([0-9]{2})\.([0-9]{4})/" => "EUR",
00662 "/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})/" => "USA"
00663 );
00664 return $aDatePatterns;
00665 }
00666
00672 protected function _regexp2ValidateTimeInput()
00673 {
00674
00675 $aTimePatterns = array("/([0-9]{2}):([0-9]{2}):([0-9]{2})$/" => "ISO",
00676 "/([0-9]{2})\.([0-9]{2})\.([0-9]{2})$/" => "EUR",
00677 "/([0-9]{2}):([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/" => "USA"
00678 );
00679 return $aTimePatterns;
00680 }
00681
00687 protected function _defineDateFormattingRules()
00688 {
00689
00690 $aDFormats = array("ISO" => array("Y-m-d", array(2, 3, 1), "0000-00-00"),
00691 "EUR" => array("d.m.Y", array(2, 1, 3), "00.00.0000"),
00692 "USA" => array("m/d/Y", array(1, 2, 3), "00/00/0000")
00693 );
00694 return $aDFormats;
00695 }
00696
00702 protected function _defineTimeFormattingRules()
00703 {
00704
00705 $aTFormats = array("ISO" => array("H:i:s", array(1, 2, 3 ), "00:00:00"),
00706 "EUR" => array("H.i.s", array(1, 2, 3 ), "00.00.00"),
00707 "USA" => array("h:i:s A", array(1, 2, 3 ), "00:00:00 AM")
00708 );
00709 return $aTFormats;
00710 }
00711
00722 protected function _setDefaultDateTimeValue( $oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00723 {
00724 $aDFormats = $this->_defineDateFormattingRules();
00725 $aTFormats = $this->_defineTimeFormattingRules();
00726
00727 $sReturn = $aDFormats[$sLocalDateFormat][2];
00728 if ( !$blOnlyDate) {
00729 $sReturn .= " ".$aTFormats[$sLocalTimeFormat][2];
00730 }
00731
00732 if ($oObject instanceof oxField) {
00733 $oObject->setValue(trim($sReturn));
00734 } else {
00735 $oObject->value = trim($sReturn);
00736 }
00737
00738 $oObject->fldmax_length = strlen( $oObject->value);
00739 }
00740
00751 protected function _setDate( $oObject, $sDateFormat, $aDFields, $aDateMatches )
00752 {
00753
00754 $iTimestamp = mktime( 0, 0, 0, $aDateMatches[$aDFields[0]],
00755 $aDateMatches[$aDFields[1]],
00756 $aDateMatches[$aDFields[2]]);
00757
00758 if ($oObject instanceof oxField) {
00759 $oObject->setValue(@date( $sDateFormat, $iTimestamp ));
00760 } else {
00761 $oObject->value = @date( $sDateFormat, $iTimestamp );
00762 }
00763
00764 $oObject->fldmax_length = strlen( $oObject->value );
00765 }
00766
00780 protected function _formatCorrectTimeValue( $oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields )
00781 {
00782
00783 $iTimestamp = @mktime( (int) $aTimeMatches[$aTFields[0]],
00784 (int) $aTimeMatches[$aTFields[1]],
00785 (int) $aTimeMatches[$aTFields[2]],
00786 (int) $aDateMatches[$aDFields[0]],
00787 (int) $aDateMatches[$aDFields[1]],
00788 (int) $aDateMatches[$aDFields[2]] );
00789
00790 if ($oObject instanceof oxField) {
00791 $oObject->setValue(trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) ));
00792 } else {
00793 $oObject->value = trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) );
00794 }
00795
00796
00797 $oObject->fldmax_length = strlen( $oObject->value );
00798 }
00799
00805 protected function _getConnectionId()
00806 {
00807 if ( self::$_oDB !== null ) {
00808 return self::$_oDB->connectionId;
00809 }
00810
00811 return null;
00812 }
00813
00821 public function escapeString( $sString )
00822 {
00823 return mysql_real_escape_string( $sString, $this->_getConnectionId() );
00824 }
00825
00826 }