00001 <?php
00002
00006 class oxDb extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 private static $_oDB = null;
00021
00027 protected static $_aTblDescCache = array();
00028
00034 public static function getInstance()
00035 {
00036
00037 if ( defined( 'OXID_PHP_UNIT' ) ) {
00038 static $inst = array();
00039 self::$_instance = $inst[oxClassCacheKey()];
00040 }
00041
00042
00043 if ( !self::$_instance instanceof oxDb ) {
00044
00045
00046 self::$_instance = oxNew( 'oxdb' );
00047
00048 if ( defined( 'OXID_PHP_UNIT' ) ) {
00049 $inst[oxClassCacheKey()] = self::$_instance;
00050 }
00051 }
00052 return self::$_instance;
00053 }
00054
00064 public static function getDb( $blAssoc = false )
00065 {
00066 if ( defined( 'OXID_PHP_UNIT' ) ) {
00067 if ( isset( modDB::$unitMOD ) && is_object( modDB::$unitMOD ) ) {
00068 return modDB::$unitMOD;
00069 }
00070 }
00071
00072 global $ADODB_FETCH_MODE;
00073
00074 if ( $blAssoc )
00075 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00076 else
00077 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00078
00079 if ( self::$_oDB !== null ) {
00080 return self::$_oDB;
00081 }
00082
00083 global $ADODB_CACHE_DIR;
00084 global $ADODB_DRIVER,
00085 $ADODB_SESSION_TBL,
00086 $ADODB_SESSION_CONNECT,
00087 $ADODB_SESSION_DRIVER,
00088 $ADODB_SESSION_USER,
00089 $ADODB_SESSION_PWD,
00090 $ADODB_SESSION_DB,
00091 $ADODB_SESS_LIFE,
00092 $ADODB_SESS_DEBUG;
00093
00094
00095 $myConfig = self::getInstance()->getConfig();
00096 $iDebug = $myConfig->getConfigParam( 'iDebug' );
00097 if ( $iDebug ) {
00098 require_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 $ADODB_SESS_LIFE = 3000 * 60;
00108 $ADODB_SESSION_TBL = "oxsessions";
00109
00110
00111 $ADODB_SESSION_DRIVER = "mysql";
00112 $ADODB_SESSION_USER = $myConfig->getConfigParam( 'dbUser' );
00113 $ADODB_SESSION_PWD = $myConfig->getConfigParam( 'dbPwd' );
00114 $ADODB_SESSION_DB = $myConfig->getConfigParam( 'dbName' );
00115 $ADODB_SESSION_CONNECT = $myConfig->getConfigParam( 'dbHost' );
00116 $ADODB_SESS_DEBUG = false;
00117 $ADODB_CACHE_DIR = $myConfig->getConfigParam( 'sCompileDir' );
00118
00119 $sModules = '';
00120 if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 || $iDebug == 7 ) {
00121 $sModules = 'perfmon';
00122 }
00123
00124
00125 if ( $myConfig->isAdmin() && $myConfig->getConfigParam( 'blLogChangesInAdmin' ) ) {
00126 $sModules = ( $sModules ? ':' : '' ) . 'oxadminlog';
00127 }
00128
00129 self::$_oDB = ADONewConnection( $myConfig->getConfigParam( 'dbType' ), $sModules );
00130
00131 $sVerPrefix = '';
00132 $sVerPrefix = '_ce';
00133
00134 if ( !self::$_oDB->connect( $myConfig->getConfigParam( 'dbHost' ), $myConfig->getConfigParam( 'dbUser' ), $myConfig->getConfigParam( 'dbPwd' ), $myConfig->getConfigParam( 'dbName' ) ) ) {
00135 $sConfig = join( '', file( getShopBasePath().'config.inc.php' ) );
00136 if ( strpos( $sConfig, '<dbHost'.$sVerPrefix.'>' ) !== false &&
00137 strpos( $sConfig, '<dbName'.$sVerPrefix.'>' ) !== false ) {
00138 header( 'location:setup/index.php' );
00139 exit();
00140 } else {
00141
00142
00143 $sFailedShop = isset( $_REQUEST['shp'] )?addslashes( $_REQUEST['shp'] ):'Base shop';
00144
00145 $sDate = date( 'l dS of F Y h:i:s A');
00146 $sScript = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
00147 $sReferer = $_SERVER['HTTP_REFERER'];
00148
00149
00150 $sWarningSubject = 'Offline warning!';
00151 $sWarningBody = "
00152 Database error in OXID eShop:
00153 Date: $sDate
00154 Shop: $sFailedShop
00155
00156 mysql error: ".self::$_oDB->errorMsg()."
00157 mysql error no: ".self::$_oDB->errorNo()."
00158
00159 Script: $sScript
00160 Referer: $sReferer";
00161
00162 if ( ( $sAdminEmail = $myConfig->getConfigParam( 'sAdminEmail' ) ) ) {
00163 include 'core/phpmailer/class.phpmailer.php';
00164
00165 $oMailer = new phpmailer();
00166 $oMailer->isMail();
00167 $oMailer->From = $sAdminEmail;
00168 $oMailer->AddAddress( $sAdminEmail );
00169 $oMailer->Subject = $sWarningSubject;
00170 $oMailer->Body = $sWarningBody;
00171 $oMailer->send();
00172 }
00173
00174
00175 $oEx = new oxConnectionException();
00176 $oEx->setMessage( 'EXCEPTION_CONNECTION_NODB' );
00177 $oEx->setConnectionError( $myConfig->getConfigParam( 'dbUser' ).'s'.getShopBasePath().self::$_oDB->errorMsg() );
00178 throw $oEx;
00179 }
00180 }
00181
00182 if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 || $iDebug == 7 ) {
00183 try {
00184 self::$_oDB->execute('truncate table adodb_logsql;');
00185 } catch (ADODB_Exception $e) {
00186
00187 }
00188 self::$_oDB->logSQL( true );
00189 }
00190
00191 self::$_oDB->cacheSecs = 60 * 10;
00192 self::$_oDB->execute( 'SET @@session.sql_mode = ""' );
00193
00194 if ( $myConfig->isUtf() ) {
00195 self::$_oDB->execute( 'SET NAMES "utf8"' );
00196 self::$_oDB->execute( 'SET CHARACTER SET utf8' );
00197 self::$_oDB->execute( 'SET CHARACTER_SET_CONNECTION = utf8' );
00198 self::$_oDB->execute( 'SET CHARACTER_SET_DATABASE = utf8' );
00199 self::$_oDB->execute( 'SET character_set_results = utf8' );
00200 self::$_oDB->execute( 'SET character_set_server = utf8' );
00201 }
00202
00203 return self::$_oDB;
00204 }
00205
00213 public function getMultiLangFieldName( $sField )
00214 {
00215
00216
00217
00218
00219
00220
00221 return $sField . oxLang::getInstance()->getLanguageTag();
00222 }
00223
00232 public function isQuoteNeeded( $sFieldtype)
00233 {
00234 $aTypesWoQuotes = array('int', 'decimal', 'float', 'tinyint', 'smallint', 'mediumint', 'bigint', 'double');
00235 return !in_array( $sFieldtype, $aTypesWoQuotes);
00236 }
00237
00245 public function getTableDescription( $sTableName )
00246 {
00247
00248 if ( isset( self::$_aTblDescCache[$sTableName] ) ) {
00249 return self::$_aTblDescCache[$sTableName];
00250 }
00251
00252 $aFields = self::getDb()->MetaColumns( $sTableName );
00253
00254 self::$_aTblDescCache[$sTableName] = $aFields;
00255
00256 return $aFields;
00257 }
00258
00268 public function convertDBDateTime( $oObject, $blToTimeStamp = false, $blOnlyDate = false )
00269 {
00270 $sDate = $oObject->value;
00271
00272
00273 $sLocalDateFormat = $this->_defineAndCheckDefaultDateValues( $blToTimeStamp );
00274 $sLocalTimeFormat = $this->_defineAndCheckDefaultTimeValues( $blToTimeStamp );
00275
00276
00277 $aDefDatePatterns = $this->_defaultDatePattern();
00278
00279
00280 $aDatePatterns = $this->_regexp2ValidateDateInput();
00281 $aTimePatterns = $this->_regexp2ValidateTimeInput();
00282
00283
00284 $aDFormats = $this->_defineDateFormattingRules();
00285 $aTFormats = $this->_defineTimeFormattingRules();
00286
00287
00288 if ( !$sDate) {
00289 $this->_setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00290 return $oObject->value;
00291 }
00292
00293 $blDefDateFound = false;
00294
00295
00296 foreach ( array_keys( $aDefDatePatterns ) as $sDefDatePattern ) {
00297 if ( preg_match( $sDefDatePattern, $sDate)) {
00298 $blDefDateFound = true;
00299 break;
00300 }
00301 }
00302
00303
00304 if ( $blDefDateFound) {
00305 $this->_setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00306 return $oObject->value;
00307 }
00308
00309 $blDateFound = false;
00310 $blTimeFound = false;
00311 $aDateMatches = array();
00312 $aTimeMatches = array();
00313
00314
00315 foreach ( $aDatePatterns as $sPattern => $sType) {
00316 if ( preg_match( $sPattern, $sDate, $aDateMatches)) {
00317 $blDateFound = true;
00318
00319
00320 $sDateFormat = $aDFormats[$sLocalDateFormat][0];
00321 $aDFields = $aDFormats[$sType][1];
00322 break;
00323 }
00324 }
00325
00326
00327 if ( !$blDateFound) {
00328 return $sDate;
00329 }
00330
00331 if ( $blOnlyDate) {
00332 $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00333 return $oObject->value;
00334 }
00335
00336
00337 foreach ( $aTimePatterns as $sPattern => $sType) {
00338 if ( preg_match( $sPattern, $sDate, $aTimeMatches)) {
00339 $blTimeFound = true;
00340
00341
00342 $sTimeFormat = $aTFormats[$sLocalTimeFormat][0];
00343 $aTFields = $aTFormats[$sType][1];
00344
00345
00346 if ( $sType == "USA" && isset($aTimeMatches[4])) {
00347 $iIntVal = (int) $aTimeMatches[1];
00348 if ( $aTimeMatches[4] == "PM") {
00349 if ( $iIntVal < 13) {
00350 $iIntVal += 12;
00351 }
00352 } elseif ( $aTimeMatches[4] == "AM" && $aTimeMatches[1] == "12") {
00353 $iIntVal = 0;
00354 }
00355
00356 $aTimeMatches[1] = sprintf("%02d", $iIntVal);
00357 }
00358
00359 break;
00360 }
00361 }
00362
00363 if ( !$blTimeFound) {
00364
00365
00366 $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00367 return $oObject->value;
00368 }
00369
00370 $this->_formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields);
00371
00372
00373 if ( !$oObject->fldmax_length) {
00374 return $this->convertDBDateTime( $oObject, $blToTimeStamp, $blOnlyDate);
00375 }
00376 return $oObject->value;
00377 }
00378
00387 public function convertDBTimestamp( $oObject, $blToTimeStamp = false )
00388 {
00389
00390 $sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
00391 $sISOTimeStampPattern = "/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/";
00392 $aMatches = array();
00393
00394
00395 if ( $blToTimeStamp) {
00396
00397 $this->convertDBDateTime( $oObject, $blToTimeStamp );
00398
00399 if ( preg_match( $sISOTimeStampPattern, $oObject->value, $aMatches)) {
00400
00401 $oObject->setValue($aMatches[1].$aMatches[2].$aMatches[3].$aMatches[4].$aMatches[5].$aMatches[6]);
00402 $oObject->fldmax_length = strlen( $oObject->value);
00403 return $oObject->value;
00404 }
00405 } else {
00406
00407
00408
00409 if ( preg_match( $sSQLTimeStampPattern, $oObject->value, $aMatches ) ) {
00410 $iTimestamp = mktime( $aMatches[4],
00411 $aMatches[5],
00412 $aMatches[6],
00413 $aMatches[2],
00414 $aMatches[3],
00415 $aMatches[1]);
00416 if ( !$iTimestamp ) {
00417 $iTimestamp = "0";
00418 }
00419
00420 $oObject->setValue(trim( date( "Y-m-d H:i:s", $iTimestamp)));
00421 $oObject->fldmax_length = strlen( $oObject->value);
00422 $this->convertDBDateTime( $oObject, $blToTimeStamp );
00423 return $oObject->value;
00424 }
00425 }
00426 }
00427
00436 public function convertDBDate( $oObject, $blToTimeStamp = false )
00437 {
00438 return $this->convertDBDateTime( $oObject, $blToTimeStamp, true );
00439 }
00440
00448 public function createSQLList( $aArray )
00449 {
00450 $sRet = "";
00451
00452 $blSep = false;
00453 foreach ( $aArray as $aToken) {
00454 if ( !$aToken[0]) {
00455 continue;
00456 }
00457 if ( $blSep) {
00458 $sRet .= ",";
00459 }
00460 $sRet .= "'".$aToken[0]."'";
00461 $blSep = true;
00462 }
00463 return $sRet;
00464 }
00465
00471 static public function startTransaction()
00472 {
00473 self::$_oDB->execute( 'START TRANSACTION' );
00474 }
00475
00481 static public function commitTransaction()
00482 {
00483 self::$_oDB->execute( 'COMMIT' );
00484 }
00485
00491 static public function rollbackTransaction()
00492 {
00493 self::$_oDB->execute( 'ROLLBACK' );
00494 }
00495
00504 static public function setTransactionIsolationLevel( $sLevel = null )
00505 {
00506 $aLevels = array( 'READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ', 'SERIALIZABLE' );
00507
00508 if (in_array(strtoupper($sLevel), $aLevels)) {
00509 self::$_oDB->execute( 'SET TRANSACTION ISOLATION LEVEL ' . $sLevel );
00510 }
00511 }
00512
00513
00522 public function isValidFieldName( $sField )
00523 {
00524 return ( boolean ) preg_match( "#^[\w\d\._]*$#", $sField );
00525 }
00526
00538 protected function _setDefaultFormatedValue( $oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00539 {
00540 $aDefTimePatterns = $this->_defaultTimePattern();
00541 $aDFormats = $this->_defineDateFormattingRules();
00542 $aTFormats = $this->_defineTimeFormattingRules();
00543
00544 foreach ( array_keys( $aDefTimePatterns ) as $sDefTimePattern ) {
00545 if ( preg_match( $sDefTimePattern, $sDate ) ) {
00546 $blDefTimeFound = true;
00547 break;
00548 }
00549 }
00550
00551
00552 if ( $blOnlyDate) {
00553 $oObject->setValue(trim( $aDFormats[$sLocalDateFormat][2] ));
00554
00555 $oObject->fldmax_length = strlen( $oObject->value );
00556 return ;
00557 } elseif ( $blDefTimeFound ) {
00558
00559 $oObject->setValue(trim( $aDFormats[$sLocalDateFormat][2] . " " . $aTFormats[$sLocalTimeFormat][2] ));
00560
00561 $oObject->fldmax_length = strlen( $oObject->value );
00562 return ;
00563 }
00564 }
00565
00573 protected function _defineAndCheckDefaultTimeValues( $blToTimeStamp )
00574 {
00575
00576
00577 $sLocalTimeFormat = $this->getConfig()->getConfigParam( 'sLocalTimeFormat' );
00578 if ( !$sLocalTimeFormat || $blToTimeStamp) {
00579 $sLocalTimeFormat = "ISO";
00580 }
00581 return $sLocalTimeFormat;
00582 }
00583
00591 protected function _defineAndCheckDefaultDateValues( $blToTimeStamp )
00592 {
00593
00594
00595 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00596 if ( !$sLocalDateFormat || $blToTimeStamp) {
00597 $sLocalDateFormat = "ISO";
00598 }
00599 return $sLocalDateFormat;
00600 }
00601
00607 protected function _defaultDatePattern()
00608 {
00609
00610 $aDefDatePatterns = array("/^0000-00-00/" => "ISO",
00611 "/^00\.00\.0000/" => "EUR",
00612 "/^00\/00\/0000/" => "USA"
00613 );
00614 return $aDefDatePatterns;
00615 }
00616
00622 protected function _defaultTimePattern()
00623 {
00624
00625 $aDefTimePatterns = array("/00:00:00$/" => "ISO",
00626 "/00\.00\.00$/" => "EUR",
00627 "/00:00:00 AM$/" => "USA"
00628 );
00629 return $aDefTimePatterns;
00630 }
00631
00637 protected function _regexp2ValidateDateInput()
00638 {
00639
00640 $aDatePatterns = array("/^([0-9]{4})-([0-9]{2})-([0-9]{2})/" => "ISO",
00641 "/^([0-9]{2})\.([0-9]{2})\.([0-9]{4})/" => "EUR",
00642 "/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})/" => "USA"
00643 );
00644 return $aDatePatterns;
00645 }
00646
00652 protected function _regexp2ValidateTimeInput()
00653 {
00654
00655 $aTimePatterns = array("/([0-9]{2}):([0-9]{2}):([0-9]{2})$/" => "ISO",
00656 "/([0-9]{2})\.([0-9]{2})\.([0-9]{2})$/" => "EUR",
00657 "/([0-9]{2}):([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/" => "USA"
00658 );
00659 return $aTimePatterns;
00660 }
00661
00667 protected function _defineDateFormattingRules()
00668 {
00669
00670 $aDFormats = array("ISO" => array("Y-m-d", array(2, 3, 1), "0000-00-00"),
00671 "EUR" => array("d.m.Y", array(2, 1, 3), "00.00.0000"),
00672 "USA" => array("m/d/Y", array(1, 2, 3), "00/00/0000")
00673 );
00674 return $aDFormats;
00675 }
00676
00682 protected function _defineTimeFormattingRules()
00683 {
00684
00685 $aTFormats = array("ISO" => array("H:i:s", array(1, 2, 3 ), "00:00:00"),
00686 "EUR" => array("H.i.s", array(1, 2, 3 ), "00.00.00"),
00687 "USA" => array("h:i:s A", array(1, 2, 3 ), "00:00:00 AM")
00688 );
00689 return $aTFormats;
00690 }
00691
00702 protected function _setDefaultDateTimeValue( $oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate )
00703 {
00704 $aDFormats = $this->_defineDateFormattingRules();
00705 $aTFormats = $this->_defineTimeFormattingRules();
00706
00707 $sReturn = $aDFormats[$sLocalDateFormat][2];
00708 if ( !$blOnlyDate) {
00709 $sReturn .= " ".$aTFormats[$sLocalTimeFormat][2];
00710 }
00711
00712 if ($oObject instanceof oxField) {
00713 $oObject->setValue(trim($sReturn));
00714 } else {
00715 $oObject->value = trim($sReturn);
00716 }
00717
00718 $oObject->fldmax_length = strlen( $oObject->value);
00719 }
00720
00731 protected function _setDate( $oObject, $sDateFormat, $aDFields, $aDateMatches )
00732 {
00733
00734 $iTimestamp = mktime( 0, 0, 0, $aDateMatches[$aDFields[0]],
00735 $aDateMatches[$aDFields[1]],
00736 $aDateMatches[$aDFields[2]]);
00737
00738 if ($oObject instanceof oxField) {
00739 $oObject->setValue(@date( $sDateFormat, $iTimestamp ));
00740 } else {
00741 $oObject->value = @date( $sDateFormat, $iTimestamp );
00742 }
00743
00744 $oObject->fldmax_length = strlen( $oObject->value );
00745 }
00746
00760 protected function _formatCorrectTimeValue( $oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields )
00761 {
00762
00763 $iTimestamp = @mktime( (int) $aTimeMatches[$aTFields[0]],
00764 (int) $aTimeMatches[$aTFields[1]],
00765 (int) $aTimeMatches[$aTFields[2]],
00766 (int) $aDateMatches[$aDFields[0]],
00767 (int) $aDateMatches[$aDFields[1]],
00768 (int) $aDateMatches[$aDFields[2]] );
00769
00770 if ($oObject instanceof oxField) {
00771 $oObject->setValue(trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) ));
00772 } else {
00773 $oObject->value = trim( @date( $sDateFormat." ".$sTimeFormat, $iTimestamp ) );
00774 }
00775
00776
00777 $oObject->fldmax_length = strlen( $oObject->value );
00778 }
00779
00780 }