00001 <?php
00002
00007 class oxSeoEncoder extends oxSuperCfg
00008 {
00015 protected static $_aReservedWords = array( 'admin' );
00016
00022 protected static $_aReservedEntryKeys = null;
00023
00029 protected static $_sSeparator = null;
00030
00036 protected $_iIdLength = 255;
00037
00043 protected static $_sPrefix = null;
00044
00050 protected $_sAddParams = null;
00051
00057 protected static $_instance = null;
00058
00064 protected $_aSeoCache = array();
00065
00071 public static function getInstance()
00072 {
00073 if (!self::$_instance) {
00074 self::$_instance = oxNew("oxSeoEncoder");
00075 }
00076
00077 if ( defined( 'OXID_PHP_UNIT' ) ) {
00078
00079 self::$_instance->_aSeoCache = array();
00080 }
00081
00082 return self::$_instance;
00083 }
00084
00093 public function addLanguageParam( $sSeoUrl, $iLang )
00094 {
00095 $iLang = (int) $iLang;
00096 $iDefLang = (int) $this->getConfig()->getConfigParam( 'iDefSeoLang' );
00097 $aLangIds = oxLang::getInstance()->getLanguageIds();
00098
00099 if ( $iLang != $iDefLang && isset( $aLangIds[$iLang] ) && getStr()->strpos( $sSeoUrl, $aLangIds[$iLang] . '/' ) !== 0 ) {
00100 $sSeoUrl = $aLangIds[$iLang] . '/'.$sSeoUrl;
00101 }
00102
00103 return $sSeoUrl;
00104 }
00105
00118 protected function _processSeoUrl( $sSeoUrl, $sObjectId = null, $iLang = null, $blExclude = false )
00119 {
00120 return $this->_getUniqueSeoUrl( $blExclude ? $sSeoUrl : $this->addLanguageParam( $sSeoUrl, $iLang ), $sObjectId, $iLang );
00121 }
00122
00128 public function resetCache()
00129 {
00130 $this->_aSeoCache = array();
00131 }
00132
00136 public function __construct()
00137 {
00138 $myConfig = $this->getConfig();
00139 if (!self::$_sSeparator) {
00140 $this->setSeparator( $myConfig->getConfigParam( 'sSEOSeparator' ) );
00141 }
00142 if (!self::$_sPrefix) {
00143 $this->setPrefix( $myConfig->getConfigParam( 'sSEOuprefix' ) );
00144 }
00145 $this->setReservedWords( $myConfig->getConfigParam( 'aSEOReservedWords' ) );
00146 }
00147
00159 protected function _copyToHistory( $sId, $iShopId, $iLang, $sType = null, $sNewId = null )
00160 {
00161 $oDb = oxDb::getDb();
00162 $sObjectid = $sNewId?$oDb->quote( $sNewId ):'oxobjectid';
00163 $sType = $sType?"oxtype =".$oDb->quote( $sType )." and":'';
00164 $iLang = (int) $iLang;
00165
00166
00167 $sSub = "select $sObjectid, MD5( LOWER( oxseourl ) ), oxshopid, oxlang, now() from oxseo
00168 where {$sType} oxobjectid = ".$oDb->quote( $sId )." and oxshopid = ".$oDb->quote( $iShopId )." and
00169 oxlang = {$iLang} limit 1";
00170 $sQ = "replace oxseohistory ( oxobjectid, oxident, oxshopid, oxlang, oxinsert ) {$sSub}";
00171 $oDb->execute( $sQ );
00172 }
00173
00180 protected function _getAddParams()
00181 {
00182
00183 if ( $this->_sAddParams === null ) {
00184 $this->_sAddParams = $this->_getAddParamsFnc( oxConfig::getParameter('currency'), $this->getConfig()->getShopId() );
00185 }
00186 return $this->_sAddParams;
00187 }
00188
00198 protected function _getAddParamsFnc( $iCur, $iActShop )
00199 {
00200
00201 $this->_sAddParams = '';
00202 $sSep = '?';
00203 if ( $iCur ) {
00204 $this->_sAddParams .= $sSep . 'cur=' . $iCur;
00205 $sSep = '&';
00206 }
00207
00208
00209 return $this->_sAddParams;
00210 }
00211
00222 protected function _getDynamicObjectId( $iShopId, $sStdUrl )
00223 {
00224 return $this->getDynamicObjectId( $iShopId, $sStdUrl );
00225 }
00226
00235 public function getDynamicObjectId( $iShopId, $sStdUrl )
00236 {
00237 return $this->_getStaticObjectId( $iShopId, $sStdUrl );
00238 }
00239
00249 protected function _getDynamicUri( $sStdUrl, $sSeoUrl, $iLang )
00250 {
00251 $iShopId = $this->getConfig()->getShopId();
00252
00253 $sStdUrl = $this->_trimUrl( $sStdUrl );
00254 $sObjectId = $this->getDynamicObjectId( $iShopId, $sStdUrl );
00255 $sSeoUrl = $this->_prepareTitle( $sSeoUrl );
00256
00257
00258 $sOldSeoUrl = $this->_loadFromDb( 'dynamic', $sObjectId, $iLang );
00259 if ( $sOldSeoUrl === $sSeoUrl ) {
00260 $sSeoUrl = $sOldSeoUrl;
00261 } else {
00262
00263 if ( $sOldSeoUrl ) {
00264 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, 'dynamic' );
00265 }
00266
00267
00268 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00269
00270
00271 $this->_saveToDb( 'dynamic', $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId );
00272 }
00273
00274 return $sSeoUrl;
00275 }
00276
00285 protected function _getFullUrl( $sSeoUrl, $iLang = null)
00286 {
00287 $sFullUrl = $this->getConfig()->getShopUrl( $iLang ) . $sSeoUrl . $this->_getAddParams();
00288 return $this->getSession()->processUrl( $sFullUrl );
00289 }
00290
00301 protected function _getSeoIdent( $sSeoUrl, $iLang = null )
00302 {
00303 return md5( strtolower( $sSeoUrl ) );
00304 }
00305
00315 protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00316 {
00317 $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00318 return $this->_loadFromDb( 'static', $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00319 }
00320
00326 protected function _getUrlExtension()
00327 {
00328 return;
00329 }
00330
00343 protected function _getUniqueSeoUrl( $sSeoUrl, $sObjectId = null, $iObjectLang = null )
00344 {
00345 $oStr = getStr();
00346 $sConstEnd = $this->_getUrlExtension();
00347 if ($sConstEnd === null) {
00348 $aMatched = array();
00349 if ( preg_match('/\.html?$/i', $sSeoUrl, $aMatched ) ) {
00350 $sConstEnd = $aMatched[0];
00351 } else {
00352 if ($sSeoUrl{$oStr->strlen($sSeoUrl)-1} != '/') {
00353 $sSeoUrl .= '/';
00354 }
00355 $sConstEnd = '/';
00356 }
00357 }
00358
00359
00360 $sAdd = ' ';
00361 if ('/' != self::$_sSeparator) {
00362 $sAdd = self::$_sSeparator . self::$_sPrefix;
00363 } else {
00364 $sAdd = '_' . self::$_sPrefix;
00365 }
00366 $sSeoUrl = preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")/#i", "\$1\$2$sAdd/", $sSeoUrl );
00367
00368 $sBaseSeoUrl = $sSeoUrl;
00369 if ( $sConstEnd && $oStr->substr( $sSeoUrl, 0 - $oStr->strlen( $sConstEnd ) ) == $sConstEnd ) {
00370 $sBaseSeoUrl = $oStr->substr( $sSeoUrl, 0, $oStr->strlen( $sSeoUrl ) - $oStr->strlen( $sConstEnd ) );
00371 }
00372
00373 $oDb = oxDb::getDb();
00374 $iShopId = $this->getConfig()->getShopId();
00375 $iCnt = 0;
00376 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00377 $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00378
00379
00380 if ( $sObjectId && isset($iObjectLang) ) {
00381 $iObjectLang = (int) $iObjectLang;
00382 $sQ .= " and not (oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = $iObjectLang)";
00383 }
00384
00385 while ( $oDb->getOne( $sQ ." and oxident= " . $oDb->quote( $this->_getSeoIdent( $sCheckSeoUrl ) ) ) ) {
00386 $sAdd = '';
00387 if ( self::$_sPrefix ) {
00388 $sAdd = self::$_sSeparator . self::$_sPrefix;
00389 }
00390 if ( $iCnt ) {
00391 $sAdd .= self::$_sSeparator . $iCnt;
00392 }
00393 ++$iCnt;
00394
00395 $sSeoUrl = $sBaseSeoUrl . $sAdd . $sConstEnd;
00396 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00397 }
00398 return $sSeoUrl;
00399 }
00400
00416 protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00417 {
00418 $oDb = oxDb::getDb( true );
00419 if ( $iShopId === null ) {
00420 $iShopId = $this->getConfig()->getShopId();
00421 }
00422
00423 $iLang = (int) $iLang;
00424
00425 $sQ = "select oxfixed, oxseourl, oxexpired, oxtype from oxseo where oxtype = ".$oDb->quote( $sType )."
00426 and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00427
00428 $sParams = $sParams ? $sParams : '';
00429 if ( $sParams && $blStrictParamsCheck ) {
00430 $sQ .= " and oxparams = '{$sParams}'";
00431 } else {
00432 $sQ .= " order by oxparams = '{$sParams}' desc";
00433 }
00434 $sQ .= " limit 1";
00435
00436
00437 $sIdent = md5($sQ);
00438 if ( isset( $this->_aSeoCache[$sIdent] ) ) {
00439 return $this->_aSeoCache[$sIdent];
00440 }
00441
00442 $sSeoUrl = false;
00443 $oRs = $oDb->execute( $sQ );
00444 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00445
00446 if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00447
00448 $this->_copyToHistory( $sId, $iShopId, $iLang );
00449 $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00450 $sSeoUrl = $oRs->fields['oxseourl'];
00451 } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00452
00453 $sSeoUrl = $oRs->fields['oxseourl'];
00454 }
00455
00456
00457 $this->_aSeoCache[$sIdent] = $sSeoUrl;
00458 }
00459 return $sSeoUrl;
00460 }
00461
00468 protected function _getReservedEntryKeys()
00469 {
00470 if (!isset(self::$_aReservedEntryKeys) && !is_array(self::$_aReservedEntryKeys)) {
00471 $sDir = getShopBasePath();
00472 self::$_aReservedEntryKeys = array();
00473 foreach (glob("$sDir/*") as $file) {
00474 if (preg_match('/^(.+)\.php[0-9]*$/i', basename($file), $m)) {
00475 self::$_aReservedEntryKeys[] = $m[0];
00476 self::$_aReservedEntryKeys[] = $m[1];
00477 } elseif (is_dir($file)) {
00478 self::$_aReservedEntryKeys[] = basename($file);
00479 }
00480 }
00481 }
00482 return self::$_aReservedEntryKeys;
00483 }
00484
00493 protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00494 {
00495
00496 $sTitle = $this->encodeString( $sTitle );
00497
00498
00499 $sTitle = strip_tags( $sTitle );
00500 $sSeparator = self::$_sSeparator;
00501 $sPrefix = self::$_sPrefix;
00502
00503 foreach ( self::$_aReservedWords as $sWord ) {
00504
00505 $sTitle = preg_replace( array( "/(\s$sWord)$/i", "/^($sWord\s)/i", "/(\s$sWord\s)/i", "/^($sWord)$/i",
00506 "/(\/$sWord)$/i", "/^($sWord\/)/i", "/(\/$sWord\/)/i"),
00507 " $1{$sSeparator}{$sPrefix}{$sSeparator} ", $sTitle );
00508 }
00509
00510
00511 $sExt = '';
00512 $oStr = getStr();
00513 $aMatched = array();
00514 if ( preg_match( '/\.html?$/i', $sTitle, $aMatched ) ) {
00515 $sExt = $oStr->substr( $sTitle, 0 - $oStr->strlen( $aMatched[0] ) );
00516 $sTitle = $oStr->substr( $sTitle, 0, $oStr->strlen( $sTitle ) - $oStr->strlen( $aMatched[0] ) );
00517 }
00518
00519
00520 if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00521
00522 if ( ( $iFirstSpace = $oStr->strstr( $oStr->substr( $sTitle, $this->_iIdLength ), ' ' ) !== false ) ) {
00523 $sTitle = $oStr->substr( $sTitle, 0, $this->_iIdLength + $iFirstSpace );
00524 }
00525 }
00526
00527
00528 $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').'\/]+/';
00529 $sTitle = trim( $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sTitle ), self::$_sSeparator );
00530
00531
00532 if ( !$sTitle ) {
00533 $sTitle = $this->_prepareTitle( self::$_sPrefix );
00534 }
00535
00536
00537 $sTitle .= $sExt;
00538
00539
00540 return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00541 array( '/', self::$_sSeparator ), $sTitle );
00542 }
00543
00544
00563 protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sKeywords = false, $sDescription = false, $sParams = null )
00564 {
00565 $oDb = oxDb::getDb( true );
00566 if ( $iShopId === null ) {
00567 $iShopId = $this->getConfig()->getShopId();
00568 }
00569
00570 $iLang = (int) $iLang;
00571
00572 $sStdUrl = $this->_trimUrl( $sStdUrl );
00573 $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00574 $sIdent = $this->_getSeoIdent( $sSeoUrl );
00575
00576
00577 $sQ = "select oxfixed, oxexpired, ( oxstdurl like ".$oDb->quote( $sStdUrl )." and oxexpired != 2 ) as samestdurl,
00578 oxseourl like ".$oDb->quote( $sSeoUrl )." as sameseourl from oxseo where oxtype = ".$oDb->quote( $sType )." and
00579 oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = {$iLang} ";
00580
00581 $sQ .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00582 $sQ .= ( $sKeywords !== false ) ? " and oxkeywords = " . $oDb->quote( $sKeywords ) . " " : '';
00583 $sQ .= ( $sDescription !== false ) ? " and oxdescription = " . $oDb->quote( $sDescription ) . " " : '';
00584
00585 $sQ .= "limit 1";
00586
00587 $oRs = $oDb->execute( $sQ );
00588 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00589 if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00590
00591 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00592
00593 $sSql = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = ".$oDb->quote( $sType )." and
00594 oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId ) ." and oxlang = {$iLang} ";
00595 $sSql .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00596 $sSql .= " limit 1";
00597
00598 return $oDb->execute( $sSql );
00599 } elseif ( $oRs->fields['oxexpired'] ) {
00600
00601 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00602 }
00603 }
00604 $oStr = getStr();
00605 if ( $sKeywords !== false ) {
00606 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
00607 }
00608
00609 if ( $sDescription !== false ) {
00610 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
00611 }
00612
00613
00614 $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00615 $blFixed = (int) $blFixed;
00616
00617 $sQ = "insert into oxseo
00618 (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxkeywords, oxdescription, oxparams)
00619 values
00620 ( ".$oDb->quote( $sObjectId ).", ".$oDb->quote( $sIdent ).", ".$oDb->quote( $iShopId ).", {$iLang}, ".$oDb->quote( $sStdUrl ).", ".$oDb->quote( $sSeoUrl ).", ".$oDb->quote( $sType ).", '$blFixed', '0',
00621 ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" ).", $sParams )
00622 on duplicate key update oxident = ".$oDb->quote( $sIdent ).", oxstdurl = ".$oDb->quote( $sStdUrl ).", oxseourl = ".$oDb->quote( $sSeoUrl ).", oxfixed = '$blFixed', oxexpired = '0',
00623 oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
00624
00625 return $oDb->execute( $sQ );
00626 }
00627
00638 protected function _trimUrl( $sUrl, $iLang = null )
00639 {
00640 $sUrl = str_replace( $this->getConfig()->getShopURL( $iLang ), '', $sUrl );
00641 return preg_replace( '/(force_)?sid=[a-z0-9\.]+&?(amp;)?/i', '', $sUrl );
00642 }
00643
00652 public function encodeString( $sString, $blReplaceChars = true )
00653 {
00654
00655 $sString = getStr()->html_entity_decode( $sString );
00656
00657 if ( $blReplaceChars ) {
00658 $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' );
00659 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00660 }
00661
00662
00663 $aReplaceWhat = array( '&', '"', ''', '<', '>' );
00664 return str_replace( $aReplaceWhat, '', $sString );
00665 }
00666
00674 public function setSeparator( $sSeparator = null )
00675 {
00676 self::$_sSeparator = $sSeparator;
00677 if ( !self::$_sSeparator ) {
00678 self::$_sSeparator = '-';
00679 }
00680 }
00681
00689 public function setPrefix( $sPrefix )
00690 {
00691 if ($sPrefix) {
00692 self::$_sPrefix = $sPrefix;
00693 } else {
00694 self::$_sPrefix = 'oxid';
00695 }
00696 }
00697
00705 public function setIdLength( $iIdlength = null )
00706 {
00707 if ( isset( $iIdlength ) ) {
00708 $this->_iIdLength = $iIdlength;
00709 }
00710 }
00711
00719 public function setReservedWords( $aReservedWords )
00720 {
00721 self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00722 }
00723
00724
00736 public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00737 {
00738 $oDb = oxDb::getDb();
00739 $sWhere = $sId ? "where oxobjectid = " . $oDb->quote( $sId ) : '';
00740 $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00741 $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00742 $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00743
00744 $sQ = "update oxseo set oxexpired = " . $oDb->quote( $iExpStat ) . " $sWhere ";
00745 $oDb->execute( $sQ );
00746 }
00747
00761 protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00762 {
00763 if (!isset($iLang)) {
00764 $iLang = $oObject->getLanguage();
00765 }
00766 $iShopId = $this->getConfig()->getShopId();
00767
00768
00769 if ( ( $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams ) ) ) {
00770 if ( $sOldSeoUrl === $sSeoUrl ) {
00771 return $sSeoUrl;
00772 } else {
00773 $this->_copyToHistory( $oObject->getId(), $iShopId, $iLang, $sType );
00774 }
00775 }
00776
00777 $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, false, false, $sParams );
00778
00779 return $sSeoUrl;
00780 }
00781
00790 protected function _getStaticObjectId( $iShopId, $sStdUrl )
00791 {
00792 return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00793 }
00794
00804 public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00805 {
00806 $oDb = oxDb::getDb();
00807 $sValues = '';
00808 $sOldObjectId = null;
00809
00810
00811 $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00812 $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00813
00814 if ( !$sObjectId || $sObjectId == '-1' ) {
00815 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00816 } else {
00817
00818 $sOldObjectId = $sObjectId;
00819
00820
00821 if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00822 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00823 }
00824 }
00825
00826 foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00827
00828 $iLang = (int) $iLang;
00829
00830
00831 if ( ( $sSeoUrl = trim( $sSeoUrl ) ) ) {
00832 $sSeoUrl = $this->_prepareTitle( $this->_trimUrl( $sSeoUrl ) );
00833 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00834 }
00835
00836
00837 if ( $sOldObjectId ) {
00838
00839 if ( !$oDb->getOne( "select (" . $oDb->quote( $sSeoUrl ) . " like oxseourl) & (" . $oDb->quote( $sStdUrl ) . " like oxstdurl) from oxseo where oxobjectid = ".$oDb->quote( $sOldObjectId )." and oxshopid = '{$iShopId}' and oxlang = '{$iLang}' " ) ) {
00840 $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00841 }
00842 }
00843
00844 if ( !$sSeoUrl || !$sStdUrl ) {
00845 continue;
00846 }
00847
00848 $sIdent = $this->_getSeoIdent( $sSeoUrl );
00849
00850 if ( $sValues ) {
00851 $sValues .= ', ';
00852 }
00853
00854 $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
00855 }
00856
00857
00858 if ( $sOldObjectId ) {
00859 $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
00860 }
00861
00862
00863 if ( $sValues ) {
00864
00865 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
00866 $oDb->execute( $sQ );
00867 }
00868
00869 return $sObjectId;
00870 }
00871
00879 public function copyStaticUrls( $iShopId )
00880 {
00881 $iBaseShopId = $this->getConfig()->getBaseShopId();
00882 if ( $iShopId != $iBaseShopId ) {
00883 $oDb = oxDb::getDb();
00884 foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
00885 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
00886 select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
00887 " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
00888 $oDb->execute( $sQ );
00889 }
00890 }
00891 }
00892
00902 public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
00903 {
00904 if (!isset($iShopId)) {
00905 $iShopId = $this->getConfig()->getShopId();
00906 }
00907 if (!isset($iLang)) {
00908 $iLang = oxLang::getInstance()->getEditLanguage();
00909 }
00910
00911 $sFullUrl = '';
00912 if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
00913 $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
00914 }
00915 return $sFullUrl;
00916 }
00917
00935 public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false )
00936 {
00937 $sSeoUrl = $this->_processSeoUrl( $this->_prepareTitle( $this->_trimUrl( $sSeoUrl ) ), $sObjectId, $iLang, $blExclude );
00938 $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sKeywords, $sDescription, $sParams );
00939 }
00940
00951 public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
00952 {
00953 $oDb = oxDb::getDb();
00954 $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
00955 oxDb::getDb()->execute( $sQ );
00956 }
00957
00968 public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
00969 {
00970 $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
00971 $iLang = ( !isset( $iLang ) ) ? oxLang::getInstance()->getTplLanguage():((int)$iLang);
00972
00973 $oDb = oxDb::getDb();
00974 return $oDb->getOne( "select {$sMetaType} from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}' order by oxparams" );
00975 }
00976
00990 public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
00991 {
00992 return $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ) );
00993 }
00994
01003 public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01004 {
01005 $oDb = oxDb::getDb( true );
01006 $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01007 $sSeoUrl = false;
01008
01009 $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' limit 1";
01010 $oRs = $oDb->execute( $sQ );
01011 if ( !$oRs->EOF ) {
01012 $sSeoUrl = $oRs->fields['oxseourl'];
01013 }
01014
01015 return $sSeoUrl;
01016 }
01017 }