oxseoencoder.php

Go to the documentation of this file.
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 static $_aFixedCache = array();
00065 
00070     protected static $_sCacheKey = null;
00071 
00076     protected static $_aCache = array();
00077 
00082     protected $_iMaxUrlLength = null;
00083 
00089     public static function getInstance()
00090     {
00091         if ( defined( 'OXID_PHP_UNIT' ) ) {
00092             self::$_instance = modInstances::getMod( __CLASS__ );
00093         }
00094 
00095         if (!self::$_instance) {
00096             self::$_instance = oxNew("oxSeoEncoder");
00097             if ( defined( 'OXID_PHP_UNIT' ) ) {
00098                 modInstances::addMod( __CLASS__, self::$_instance);
00099             }
00100         }
00101 
00102         return self::$_instance;
00103 
00104     }
00105 
00114     public function addLanguageParam( $sSeoUrl, $iLang )
00115     {
00116         $iLang    = (int) $iLang;
00117         $iDefLang = (int) $this->getConfig()->getConfigParam( 'iDefSeoLang' );
00118         $aLangIds = oxLang::getInstance()->getLanguageIds();
00119 
00120         if ( $iLang != $iDefLang && isset( $aLangIds[$iLang] ) && getStr()->strpos( $sSeoUrl, $aLangIds[$iLang] . '/' ) !== 0 ) {
00121             $sSeoUrl = $aLangIds[$iLang] . '/'.$sSeoUrl;
00122         }
00123 
00124         return $sSeoUrl;
00125     }
00126 
00139     protected function _processSeoUrl( $sSeoUrl, $sObjectId = null, $iLang = null, $blExclude = false )
00140     {
00141         if (!$blExclude) {
00142             $sSeoUrl = $this->addLanguageParam( $sSeoUrl, $iLang );
00143         }
00144         return $this->_getUniqueSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00145     }
00146 
00150     public function __construct()
00151     {
00152         $myConfig = $this->getConfig();
00153         if (!self::$_sSeparator) {
00154             $this->setSeparator( $myConfig->getConfigParam( 'sSEOSeparator' ) );
00155         }
00156         if (!self::$_sPrefix) {
00157             $this->setPrefix( $myConfig->getConfigParam( 'sSEOuprefix' ) );
00158         }
00159         $this->setReservedWords( $myConfig->getConfigParam( 'aSEOReservedWords' ) );
00160     }
00161 
00173     protected function _copyToHistory( $sId, $iShopId, $iLang, $sType = null, $sNewId = null )
00174     {
00175         $oDb = oxDb::getDb();
00176         $sObjectid = $sNewId?$oDb->quote( $sNewId ):'oxobjectid';
00177         $sType     = $sType?"oxtype =".$oDb->quote( $sType )." and":'';
00178         $iLang     = (int) $iLang;
00179 
00180         // moving
00181         $sSub = "select $sObjectid, MD5( LOWER( oxseourl ) ), oxshopid, oxlang, now() from oxseo
00182                  where {$sType} oxobjectid = ".$oDb->quote( $sId )." and oxshopid = ".$oDb->quote( $iShopId )." and
00183                  oxlang = {$iLang} and oxexpired = '1'";
00184         $sQ   = "replace oxseohistory ( oxobjectid, oxident, oxshopid, oxlang, oxinsert ) {$sSub}";
00185         $oDb->execute( $sQ );
00186     }
00187 
00196     public function getDynamicObjectId( $iShopId, $sStdUrl )
00197     {
00198         return $this->_getStaticObjectId( $iShopId, $sStdUrl );
00199     }
00200 
00210     protected function _getDynamicUri( $sStdUrl, $sSeoUrl, $iLang )
00211     {
00212         $iShopId = $this->getConfig()->getShopId();
00213 
00214         $sStdUrl   = $this->_trimUrl( $sStdUrl );
00215         $sObjectId = $this->getDynamicObjectId( $iShopId, $sStdUrl );
00216         $sSeoUrl   = $this->_prepareUri( $this->addLanguageParam( $sSeoUrl, $iLang ) );
00217 
00218         //load details link from DB
00219         $sOldSeoUrl = $this->_loadFromDb( 'dynamic', $sObjectId, $iLang );
00220         if ( $sOldSeoUrl === $sSeoUrl ) {
00221             $sSeoUrl = $sOldSeoUrl;
00222         } else {
00223 
00224             if ( $sOldSeoUrl ) {
00225                 // old must be transferred to history
00226                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, 'dynamic' );
00227             }
00228 
00229             // creating unique
00230             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00231 
00232             // inserting
00233             $this->_saveToDb( 'dynamic', $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId );
00234         }
00235 
00236         return $sSeoUrl;
00237     }
00238 
00248     protected function _getFullUrl( $sSeoUrl, $iLang = null, $blSsl = false )
00249     {
00250         if ( $sSeoUrl ) {
00251             $sFullUrl = ( $blSsl ? $this->getConfig()->getSslShopUrl( $iLang ) : $this->getConfig()->getShopUrl( $iLang, false ) ) . $sSeoUrl;
00252             return oxUtilsUrl::getInstance()->processSeoUrl( $sFullUrl );
00253         }
00254         return false;
00255     }
00256 
00266     protected function _getSeoIdent( $sSeoUrl )
00267     {
00268         return md5( strtolower( $sSeoUrl ) );
00269     }
00270 
00280     protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00281     {
00282         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00283         return $this->_loadFromDb( 'static', $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00284     }
00285 
00291     protected function _getUrlExtension()
00292     {
00293         return;
00294     }
00295 
00308     protected function _getUniqueSeoUrl( $sSeoUrl, $sObjectId = null, $iObjectLang = null )
00309     {
00310         $sSeoUrl = $this->_prepareUri( $sSeoUrl );
00311         $oStr = getStr();
00312         $sExt = '';
00313         if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sSeoUrl, $aMatched ) ) {
00314             $sExt = $aMatched[0];
00315         }
00316         $sBaseSeoUrl = $sSeoUrl;
00317         if ( $sExt && $oStr->substr( $sSeoUrl, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00318             $sBaseSeoUrl = $oStr->substr( $sSeoUrl, 0, $oStr->strlen( $sSeoUrl ) - $oStr->strlen( $sExt ) );
00319         }
00320 
00321         $oDb = oxDb::getDb();
00322         $iShopId = $this->getConfig()->getShopId();
00323         $iCnt = 0;
00324         $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00325         $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00326 
00327         // skipping self
00328         if ( $sObjectId && isset($iObjectLang) ) {
00329             $iObjectLang = (int) $iObjectLang;
00330             $sQ .= " and not (oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = $iObjectLang)";
00331         }
00332 
00333         while ( $oDb->getOne( $sQ ." and oxident= " . $oDb->quote( $this->_getSeoIdent( $sCheckSeoUrl ) ) ) ) {
00334             $sAdd = '';
00335             if ( self::$_sPrefix ) {
00336                 $sAdd = self::$_sSeparator . self::$_sPrefix;
00337             }
00338             if ( $iCnt ) {
00339                 $sAdd .= self::$_sSeparator . $iCnt;
00340             }
00341             ++$iCnt;
00342 
00343             $sSeoUrl = $sBaseSeoUrl . $sAdd . $sExt;
00344             $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00345         }
00346         return $sSeoUrl;
00347     }
00348 
00363     protected function _isFixed( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00364     {
00365         if ( $iShopId === null ) {
00366             $iShopId = $this->getConfig()->getShopId();
00367         }
00368         $iLang = (int) $iLang;
00369 
00370         if ( !isset( self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] ) ) {
00371             $oDb = oxDb::getDb( true );
00372 
00373             $sQ = "SELECT `oxfixed`
00374                 FROM `oxseo`
00375                 WHERE `oxtype` = ".$oDb->quote( $sType )."
00376                    AND `oxobjectid` = ".$oDb->quote( $sId ) ."
00377                    AND `oxshopid` = ".$oDb->quote( $iShopId )."
00378                    AND `oxlang` = '{$iLang}'";
00379 
00380             $sParams = $sParams ? $oDb->quote( $sParams ) : "''";
00381             if ( $sParams && $blStrictParamsCheck ) {
00382                 $sQ .= " AND `oxparams` = {$sParams}";
00383             } else {
00384                 $sQ .= " ORDER BY `oxparams` ASC";
00385             }
00386             $sQ .= " LIMIT 1";
00387 
00388             self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00389         }
00390         return self::$_aFixedCache[$sType][$sShopId][$sId][$iLang];
00391     }
00392 
00403     protected function _getCacheKey( $sType, $iLang = null, $iShopId = null, $sParams = null )
00404     {
00405         $blAdmin = $this->isAdmin();
00406         if ( !$blAdmin && $sType !== "oxarticle" ) {
00407             return $sType . ( (int) $iLang ) . ( (int) $iShopId ) . "seo";
00408         }
00409 
00410         // use cache in non admin mode
00411         if ( self::$_sCacheKey === null ) {
00412             self::$_sCacheKey = false;
00413             if ( !$blAdmin && ( $oView = $this->getConfig()->getActiveView() ) ) {
00414                 self::$_sCacheKey = md5( $oView->getViewId() ) . "seo";
00415             }
00416         }
00417         return self::$_sCacheKey;
00418     }
00419 
00431     protected function _loadFromCache( $sCacheIdent, $sType, $iLang = null, $iShopId = null, $sParams = null )
00432     {
00433         if ( !$this->getConfig()->getConfigParam( 'blEnableSeoCache' ) ) {
00434             return false;
00435         }
00436 
00437         startProfile( "seoencoder_loadFromCache" );
00438 
00439         $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams );
00440         $sCache = false;
00441 
00442         if ( $sCacheKey && !isset( self::$_aCache[$sCacheKey] ) ) {
00443             self::$_aCache[$sCacheKey] = oxUtils::getInstance()->fromFileCache( $sCacheKey );
00444         }
00445 
00446         if ( isset( self::$_aCache[$sCacheKey] ) && isset( self::$_aCache[$sCacheKey][$sCacheIdent] ) ) {
00447             $sCache = self::$_aCache[$sCacheKey][$sCacheIdent];
00448         }
00449 
00450         stopProfile( "seoencoder_loadFromCache" );
00451         return $sCache;
00452     }
00453 
00466     protected function _saveInCache( $sCacheIdent, $sCache, $sType, $iLang = null, $iShopId = null, $sParams = null )
00467     {
00468         if ( !$this->getConfig()->getConfigParam( 'blEnableSeoCache' ) ) {
00469             return false;
00470         }
00471 
00472         startProfile( "seoencoder_saveInCache" );
00473 
00474         $blSaved = false;
00475         if ( $sCache && ( $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams ) ) !== false ) {
00476             self::$_aCache[$sCacheKey][$sCacheIdent] = $sCache;
00477             $blSaved = oxUtils::getInstance()->toFileCache( $sCacheKey, self::$_aCache[$sCacheKey] );
00478         }
00479 
00480         stopProfile( "seoencoder_saveInCache" );
00481         return $blSaved;
00482     }
00483 
00499     protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00500     {
00501         if ( $iShopId === null ) {
00502             $iShopId = $this->getConfig()->getShopId();
00503         }
00504 
00505         $iLang = (int) $iLang;
00506 
00507         $sQ = "
00508             SELECT
00509                 `oxfixed`,
00510                 `oxseourl`,
00511                 `oxexpired`,
00512                 `oxtype`
00513             FROM `oxseo`
00514             WHERE `oxtype` = ".oxDb::getDb()->quote( $sType )."
00515                AND `oxobjectid` = ".oxDb::getDb()->quote( $sId ) ."
00516                AND `oxshopid` = ".oxDb::getDb()->quote( $iShopId )."
00517                AND `oxlang` = '{$iLang}'";
00518 
00519         $sParams = $sParams ? $sParams : '';
00520         if ( $sParams && $blStrictParamsCheck ) {
00521             $sQ .= " AND `oxparams` = '{$sParams}'";
00522         } else {
00523             $sQ .= " ORDER BY `oxparams` ASC";
00524         }
00525         $sQ .= " LIMIT 1";
00526 
00527 
00528         // caching to avoid same queries..
00529         $sIdent = md5( $sQ );
00530 
00531         // looking in cache
00532         if ( ( $sSeoUrl = $this->_loadFromCache( $sIdent, $sType, $iLang, $iShopId, $sParams ) ) === false ) {
00533             $oDb = oxDb::getDb( true );
00534             $oRs = $oDb->execute( $sQ );
00535             if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00536                 // moving expired static urls to history ..
00537                 if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00538                     // if expired - copying to history, marking as not expired
00539                     $this->_copyToHistory( $sId, $iShopId, $iLang );
00540                     $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00541                     $sSeoUrl = $oRs->fields['oxseourl'];
00542                 } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00543                     // if seo url is available and is valid
00544                     $sSeoUrl = $oRs->fields['oxseourl'];
00545                 }
00546 
00547                 // storing in cache
00548                 $this->_saveInCache( $sIdent, $sSeoUrl, $sType, $iLang, $iShopId, $sParams );
00549             }
00550         }
00551         return $sSeoUrl;
00552     }
00553 
00560     protected function _getReservedEntryKeys()
00561     {
00562         if ( !isset( self::$_aReservedEntryKeys ) || !is_array( self::$_aReservedEntryKeys ) ) {
00563             $sDir = getShopBasePath();
00564             self::$_aReservedEntryKeys = array_map('preg_quote', self::$_aReservedWords, array('#'));
00565             $oStr = getStr();
00566             foreach ( glob( "$sDir/*" ) as $sFile ) {
00567                 if ( $oStr->preg_match( '/^(.+)\.php[0-9]*$/i', basename( $sFile ), $aMatches ) ) {
00568                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[0], '#' );
00569                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[1], '#' );
00570                 } elseif ( is_dir( $sFile ) ) {
00571                     self::$_aReservedEntryKeys[] = preg_quote( basename( $sFile ), '#' );
00572                 }
00573             }
00574             self::$_aReservedEntryKeys = array_unique(self::$_aReservedEntryKeys);
00575         }
00576         return self::$_aReservedEntryKeys;
00577     }
00578 
00586     protected function _prepareUri( $sUri )
00587     {
00588         // decoding entities
00589         $sUri = $this->encodeString( $sUri );
00590 
00591         // basic string preparation
00592         $sUri = strip_tags( $sUri );
00593         $oStr = getStr();
00594 
00595 
00596         // if found ".html" or "/" at the end - removing it temporary
00597         $oStr = getStr();
00598         $sExt = $this->_getUrlExtension();
00599         if ($sExt === null) {
00600             $aMatched = array();
00601             if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sUri, $aMatched ) ) {
00602                 $sExt = $aMatched[0];
00603             } else {
00604                 $sExt = '/';
00605             }
00606         }
00607         if ( $sExt && $oStr->substr( $sUri, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00608             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $sExt ) );
00609         }
00610 
00611         // removing any special characters
00612         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00613         $sUri  = $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri );
00614 
00615         // SEO id is empty ?
00616         if ( !$sUri && self::$_sPrefix ) {
00617             $sUri = $this->_prepareUri( self::$_sPrefix );
00618         }
00619 
00620         $sAdd = '';
00621         if ('/' != self::$_sSeparator) {
00622             $sAdd = self::$_sSeparator . self::$_sPrefix;
00623             $sUri = trim($sUri, self::$_sSeparator);
00624         } else {
00625             $sAdd = '_' . self::$_sPrefix;
00626         }
00627 
00628         // binding the ending back
00629         $sUri .= $sExt;
00630 
00631         // fix for not having url, which executes through /other/ script then seo decoder
00632         $sUri = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")(/|$)#i", "\$1\$2$sAdd\$3", $sUri );
00633 
00634         // cleaning
00635         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00636                              array( '/', self::$_sSeparator ), $sUri );
00637     }
00638 
00639 
00648     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00649     {
00650         $sTitle = $this->encodeString( $sTitle );
00651         $sSep = self::$_sSeparator;
00652         if (!$sSep || ('/' == $sSep)) {
00653             $sSep = '_';
00654         }
00655 
00656         $sRegExp = '/[^A-Za-z0-9\/'.preg_quote( self::$_sPrefix, '/').preg_quote($sSep, '/').']+/';
00657         $sTitle = preg_replace( array("#/+#", $sRegExp, "# +#", "#(".preg_quote($sSep, '/').")+#"), $sSep, $sTitle );
00658 
00659         $oStr = getStr();
00660         // smart truncate
00661         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00662             $iFirstSpace = $oStr->strpos( $sTitle, $sSep, $this->_iIdLength);
00663             if ( $iFirstSpace !== false ) {
00664                 $sTitle = $oStr->substr( $sTitle, 0, $iFirstSpace );
00665             }
00666         }
00667 
00668         $sTitle = trim( $sTitle, $sSep );
00669 
00670         if (!$sTitle) {
00671             return self::$_sPrefix;
00672         }
00673         // cleaning
00674         return $sTitle;
00675     }
00676 
00677 
00694     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sParams = null )
00695     {
00696         $oDb = oxDb::getDb( true );
00697         if ( $iShopId === null ) {
00698             $iShopId = $this->getConfig()->getShopId();
00699         }
00700 
00701         $iLang = (int) $iLang;
00702 
00703         $sStdUrl = $this->_trimUrl( $sStdUrl );
00704         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00705         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00706 
00707         // transferring old url, thus current url will be regenerated
00708         $sQtedObjectId = $oDb->quote( $sObjectId );
00709         $iQtedShopId   = $oDb->quote( $iShopId );
00710         $sQtedType     = $oDb->quote( $sType );
00711         $sQtedSeoUrl   = $oDb->quote( $sSeoUrl );
00712         $sQtedStdUrl   = $oDb->quote( $sStdUrl );
00713         $sQtedParams   = $oDb->quote( $sParams );
00714         $sQtedIdent    = $oDb->quote( $sIdent );
00715 
00716         // transferring old url, thus current url will be regenerated
00717         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like {$sQtedStdUrl} ) as samestdurl,
00718                 oxseourl like {$sQtedSeoUrl} as sameseourl from oxseo where oxtype = {$sQtedType} and
00719                 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId}  and oxlang = {$iLang} ";
00720 
00721         $sQ .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00722         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00723         $sQ .= "limit 1";
00724 
00725         $oRs = $oDb->execute( $sQ );
00726         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00727             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00728                 // fixed state change
00729                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00730                 // nothing was changed - setting expired status back to 0
00731                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = {$sQtedType} and
00732                           oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00733                 $sSql .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00734                 $sSql .= " limit 1";
00735 
00736                 return $oDb->execute( $sSql );
00737             } elseif ( $oRs->fields['oxexpired'] ) {
00738                 // copy to history
00739                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00740             }
00741         }
00742 
00743         // inserting new or updating
00744         $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00745         $blFixed = (int) $blFixed;
00746 
00747         $sQ  = "insert into oxseo
00748                     (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxparams)
00749                 values
00750                     ( {$sQtedObjectId}, {$sQtedIdent}, {$iQtedShopId}, {$iLang}, {$sQtedStdUrl}, {$sQtedSeoUrl}, {$sQtedType}, '$blFixed', '0', {$sParams} )
00751                 on duplicate key update
00752                     oxident = {$sQtedIdent}, oxstdurl = {$sQtedStdUrl}, oxseourl = {$sQtedSeoUrl}, oxfixed = '$blFixed', oxexpired = '0'";
00753 
00754         return $oDb->execute( $sQ );
00755     }
00756 
00767     protected function _trimUrl( $sUrl, $iLang = null )
00768     {
00769         $myConfig = $this->getConfig();
00770         $oStr = getStr();
00771         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang, false ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00772         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00773         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00774         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00775         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)cur=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00776         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)stoken=[a-z0-9]+&?(amp;)?/i', '\1', $sUrl );
00777         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00778         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)+$/i', '', $sUrl );
00779         $sUrl = trim( $sUrl );
00780 
00781         // max length <= $this->_iMaxUrlLength
00782         $iLength = $this->_getMaxUrlLength();
00783         if ( $oStr->strlen( $sUrl ) > $iLength ) {
00784             $sUrl = $oStr->substr( $sUrl, 0, $iLength );
00785         }
00786 
00787         return $sUrl;
00788     }
00789 
00795     protected function _getMaxUrlLength()
00796     {
00797         if ( $this->_iMaxUrlLength === null ) {
00798             // max length <= 2048 / custom
00799             $this->_iMaxUrlLength = $this->getConfig()->getConfigParam( "iMaxSeoUrlLength" ) ;
00800             if ( !$this->_iMaxUrlLength ) {
00801                 $this->_iMaxUrlLength = 2048;
00802             }
00803         }
00804         return $this->_iMaxUrlLength;
00805     }
00806 
00815     public function encodeString( $sString, $blReplaceChars = true )
00816     {
00817         // decoding entities
00818         $sString = getStr()->html_entity_decode( $sString );
00819 
00820         if ( $blReplaceChars ) {
00821             if ( $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' ) ) {
00822                 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00823             }
00824         }
00825 
00826 
00827         // special chars
00828         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00829         return str_replace( $aReplaceWhat, '', $sString );
00830     }
00831 
00839     public function setSeparator( $sSeparator = null )
00840     {
00841         self::$_sSeparator = $sSeparator;
00842         if ( !self::$_sSeparator ) {
00843             self::$_sSeparator = '-';
00844         }
00845     }
00846 
00854     public function setPrefix( $sPrefix )
00855     {
00856         if ($sPrefix) {
00857             self::$_sPrefix = $sPrefix;
00858         } else {
00859             self::$_sPrefix = 'oxid';
00860         }
00861     }
00862 
00870     public function setIdLength( $iIdlength = null )
00871     {
00872         if ( isset( $iIdlength ) ) {
00873             $this->_iIdLength = $iIdlength;
00874         }
00875     }
00876 
00885     public function setReservedWords( $aReservedWords )
00886     {
00887         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00888     }
00889 
00890 
00902     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00903     {
00904         $oDb = oxDb::getDb();
00905         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00906         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00907         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00908         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00909 
00910         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00911         $oDb->execute( $sQ );
00912     }
00913 
00927     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00928     {
00929         if ( !isset( $iLang ) ) {
00930             $iLang = $oObject->getLanguage();
00931         }
00932         $iShopId = $this->getConfig()->getShopId();
00933 
00934         //load page link from DB
00935         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00936         if ( !$sOldSeoUrl ) {
00937             // generating new..
00938             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00939             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, $sParams );
00940         } else {
00941             // using old
00942             $sSeoUrl = $sOldSeoUrl;
00943         }
00944         return $sSeoUrl;
00945     }
00946 
00955     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00956     {
00957         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00958     }
00959 
00969     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00970     {
00971         $oDb = oxDb::getDb();
00972         $sValues = '';
00973         $sOldObjectId = null;
00974 
00975         // standard url
00976         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00977         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00978 
00979         if ( !$sObjectId || $sObjectId == '-1' ) {
00980             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00981         } else {
00982             // marking entry as needs to move to history
00983             $sOldObjectId = $sObjectId;
00984 
00985             // if std url does not match old
00986             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00987                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00988             }
00989         }
00990 
00991         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00992 
00993             $iLang = (int) $iLang;
00994 
00995             // generating seo url
00996             $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00997             if ( $sSeoUrl ) {
00998                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00999             }
01000 
01001 
01002             if ( $sOldObjectId ) {
01003                 // move changed records to history
01004                 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}' " ) ) {
01005                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
01006                 }
01007             }
01008 
01009             if ( !$sSeoUrl || !$sStdUrl ) {
01010                 continue;
01011             }
01012 
01013             $sIdent = $this->_getSeoIdent( $sSeoUrl );
01014 
01015             if ( $sValues ) {
01016                 $sValues .= ', ';
01017             }
01018 
01019             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
01020         }
01021 
01022         // must delete old before insert/update
01023         if ( $sOldObjectId ) {
01024             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
01025         }
01026 
01027         // (re)inserting
01028         if ( $sValues ) {
01029 
01030             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
01031             $oDb->execute( $sQ );
01032         }
01033 
01034         return $sObjectId;
01035     }
01036 
01044     public function copyStaticUrls( $iShopId )
01045     {
01046         $iBaseShopId = $this->getConfig()->getBaseShopId();
01047         if ( $iShopId != $iBaseShopId ) {
01048             $oDb = oxDb::getDb();
01049             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
01050                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
01051                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
01052                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
01053                 $oDb->execute( $sQ );
01054             }
01055         }
01056     }
01057 
01067     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
01068     {
01069         if (!isset($iShopId)) {
01070             $iShopId = $this->getConfig()->getShopId();
01071         }
01072         if (!isset($iLang)) {
01073             $iLang   = oxLang::getInstance()->getEditLanguage();
01074         }
01075 
01076         if ( isset($this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId])) {
01077             return $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId];
01078         }
01079 
01080         $sFullUrl = '';
01081         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01082             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01083         }
01084 
01085 
01086         $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId] = $sFullUrl;
01087 
01088         return $sFullUrl;
01089     }
01090 
01109     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false, $sAltObjectId = null )
01110     {
01111         $sSeoUrl = $this->_processSeoUrl( $this->_trimUrl( $sSeoUrl ? $sSeoUrl : $this->_getAltUri( $sAltObjectId ? $sAltObjectId : $sObjectId, $iLang ) ), $sObjectId, $iLang, $blExclude );
01112         if ( $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sParams ) ) {
01113 
01114             $oDb = oxDb::getDb( true );
01115 
01116             //
01117             $sQtedObjectId = $oDb->quote( $sAltObjectId ? $sAltObjectId : $sObjectId );
01118             $iQtedShopId   = $oDb->quote( $iShopId );
01119 
01120             $oStr = getStr();
01121             if ( $sKeywords !== false ) {
01122                 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
01123             }
01124 
01125             if ( $sDescription !== false ) {
01126                 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
01127             }
01128 
01129             $sQ = "insert into oxobject2seodata
01130                        ( oxobjectid, oxshopid, oxlang, oxkeywords, oxdescription )
01131                    values
01132                        ( {$sQtedObjectId}, {$iQtedShopId}, {$iLang}, ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" )." )
01133                    on duplicate key update
01134                        oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
01135             $oDb->execute( $sQ );
01136         }
01137     }
01138 
01147     protected function _getAltUri( $sObjectId, $iLang )
01148     {
01149     }
01150 
01161     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01162     {
01163         $oDb = oxDb::getDb();
01164         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01165         oxDb::getDb()->execute( $sQ );
01166     }
01167 
01178     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
01179     {
01180         $oDb = oxDb::getDb();
01181 
01182         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01183         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getObjectTplLanguage():((int) $iLang);
01184         return $oDb->getOne( "select {$sMetaType} from oxobject2seodata where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}'" );
01185     }
01186 
01200     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01201     {
01202         startProfile("getDynamicUrl");
01203         $sDynUrl = $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01204         stopProfile("getDynamicUrl");
01205         return $sDynUrl;
01206     }
01207 
01216     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01217     {
01218         $oDb = oxDb::getDb( true );
01219         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01220         $sSeoUrl   = false;
01221 
01222         $sShopId = $this->getConfig()->getShopId();
01223 
01224         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01225         $oRs = $oDb->execute( $sQ );
01226         if ( !$oRs->EOF ) {
01227             $sSeoUrl = $oRs->fields['oxseourl'];
01228         }
01229 
01230         return $sSeoUrl;
01231     }
01232 }