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 
00381             if ( $sParams ) {
00382                 $sQ .= " AND `oxparams` = '{$sParams}'";
00383             } elseif ( ! $blStrictParamsCheck ) {
00384                 $sQ .= " ORDER BY `oxparams` ASC" ;
00385             }
00386 
00387             $sQ .= " LIMIT 1";
00388 
00389             self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00390         }
00391         return self::$_aFixedCache[$sType][$sShopId][$sId][$iLang];
00392     }
00393 
00404     protected function _getCacheKey( $sType, $iLang = null, $iShopId = null, $sParams = null )
00405     {
00406         $blAdmin = $this->isAdmin();
00407         if ( !$blAdmin && $sType !== "oxarticle" ) {
00408             return $sType . ( (int) $iLang ) . ( (int) $iShopId ) . "seo";
00409         }
00410 
00411         // use cache in non admin mode
00412         if ( self::$_sCacheKey === null ) {
00413             self::$_sCacheKey = false;
00414             if ( !$blAdmin && ( $oView = $this->getConfig()->getActiveView() ) ) {
00415                 self::$_sCacheKey = md5( $oView->getViewId() ) . "seo";
00416             }
00417         }
00418         return self::$_sCacheKey;
00419     }
00420 
00432     protected function _loadFromCache( $sCacheIdent, $sType, $iLang = null, $iShopId = null, $sParams = null )
00433     {
00434         startProfile( "seoencoder_loadFromCache" );
00435 
00436         $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams );
00437         $sCache = false;
00438 
00439         if ( $sCacheKey && !isset( self::$_aCache[$sCacheKey] ) ) {
00440             self::$_aCache[$sCacheKey] = oxUtils::getInstance()->fromFileCache( $sCacheKey );
00441         }
00442 
00443         if ( isset( self::$_aCache[$sCacheKey] ) && isset( self::$_aCache[$sCacheKey][$sCacheIdent] ) ) {
00444             $sCache = self::$_aCache[$sCacheKey][$sCacheIdent];
00445         }
00446 
00447         stopProfile( "seoencoder_loadFromCache" );
00448         return $sCache;
00449     }
00450 
00463     protected function _saveInCache( $sCacheIdent, $sCache, $sType, $iLang = null, $iShopId = null, $sParams = null )
00464     {
00465         startProfile( "seoencoder_saveInCache" );
00466 
00467         $blSaved = false;
00468         if ( $sCache && ( $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams ) ) !== false ) {
00469             self::$_aCache[$sCacheKey][$sCacheIdent] = $sCache;
00470             $blSaved = oxUtils::getInstance()->toFileCache( $sCacheKey, self::$_aCache[$sCacheKey] );
00471         }
00472 
00473         stopProfile( "seoencoder_saveInCache" );
00474         return $blSaved;
00475     }
00476 
00492     protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00493     {
00494         if ( $iShopId === null ) {
00495             $iShopId = $this->getConfig()->getShopId();
00496         }
00497 
00498         $iLang = (int) $iLang;
00499 
00500         $sQ = "
00501             SELECT
00502                 `oxfixed`,
00503                 `oxseourl`,
00504                 `oxexpired`,
00505                 `oxtype`
00506             FROM `oxseo`
00507             WHERE `oxtype` = ".oxDb::getDb()->quote( $sType )."
00508                AND `oxobjectid` = ".oxDb::getDb()->quote( $sId ) ."
00509                AND `oxshopid` = ".oxDb::getDb()->quote( $iShopId )."
00510                AND `oxlang` = '{$iLang}'";
00511 
00512         if ( $sParams ) {
00513             $sQ .= " AND `oxparams` = '{$sParams}'";
00514         } elseif ( ! $blStrictParamsCheck) {
00515             $sQ .= " ORDER BY `oxparams` ASC" ;
00516         }
00517 
00518         $sQ .= " LIMIT 1";
00519 
00520         // caching to avoid same queries..
00521         $sIdent = md5( $sQ );
00522 
00523         // looking in cache
00524         if ( ( $sSeoUrl = $this->_loadFromCache( $sIdent, $sType, $iLang, $iShopId, $sParams ) ) === false ) {
00525             $oDb = oxDb::getDb( true );
00526             $oRs = $oDb->execute( $sQ );
00527             if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00528                 // moving expired static urls to history ..
00529                 if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00530                     // if expired - copying to history, marking as not expired
00531                     $this->_copyToHistory( $sId, $iShopId, $iLang );
00532                     $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00533                     $sSeoUrl = $oRs->fields['oxseourl'];
00534                 } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00535                     // if seo url is available and is valid
00536                     $sSeoUrl = $oRs->fields['oxseourl'];
00537                 }
00538 
00539                 // storing in cache
00540                 $this->_saveInCache( $sIdent, $sSeoUrl, $sType, $iLang, $iShopId, $sParams );
00541             }
00542         }
00543         return $sSeoUrl;
00544     }
00545 
00552     protected function _getReservedEntryKeys()
00553     {
00554         if ( !isset( self::$_aReservedEntryKeys ) || !is_array( self::$_aReservedEntryKeys ) ) {
00555             $sDir = getShopBasePath();
00556             self::$_aReservedEntryKeys = array_map('preg_quote', self::$_aReservedWords, array('#'));
00557             $oStr = getStr();
00558             foreach ( glob( "$sDir/*" ) as $sFile ) {
00559                 if ( $oStr->preg_match( '/^(.+)\.php[0-9]*$/i', basename( $sFile ), $aMatches ) ) {
00560                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[0], '#' );
00561                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[1], '#' );
00562                 } elseif ( is_dir( $sFile ) ) {
00563                     self::$_aReservedEntryKeys[] = preg_quote( basename( $sFile ), '#' );
00564                 }
00565             }
00566             self::$_aReservedEntryKeys = array_unique(self::$_aReservedEntryKeys);
00567         }
00568         return self::$_aReservedEntryKeys;
00569     }
00570 
00578     protected function _prepareUri( $sUri )
00579     {
00580         // decoding entities
00581         $sUri = $this->encodeString( $sUri );
00582 
00583         // basic string preparation
00584         $sUri = strip_tags( $sUri );
00585         $oStr = getStr();
00586 
00587 
00588         // if found ".html" or "/" at the end - removing it temporary
00589         $oStr = getStr();
00590         $sExt = $this->_getUrlExtension();
00591         if ($sExt === null) {
00592             $aMatched = array();
00593             if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sUri, $aMatched ) ) {
00594                 $sExt = $aMatched[0];
00595             } else {
00596                 $sExt = '/';
00597             }
00598         }
00599         if ( $sExt && $oStr->substr( $sUri, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00600             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $sExt ) );
00601         }
00602 
00603         // removing any special characters
00604         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00605         $sUri  = $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri );
00606 
00607         // SEO id is empty ?
00608         if ( !$sUri && self::$_sPrefix ) {
00609             $sUri = $this->_prepareUri( self::$_sPrefix );
00610         }
00611 
00612         $sAdd = '';
00613         if ('/' != self::$_sSeparator) {
00614             $sAdd = self::$_sSeparator . self::$_sPrefix;
00615             $sUri = trim($sUri, self::$_sSeparator);
00616         } else {
00617             $sAdd = '_' . self::$_sPrefix;
00618         }
00619 
00620         // binding the ending back
00621         $sUri .= $sExt;
00622 
00623         // fix for not having url, which executes through /other/ script then seo decoder
00624         $sUri = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")(/|$)#i", "\$1\$2$sAdd\$3", $sUri );
00625 
00626         // cleaning
00627         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00628                              array( '/', self::$_sSeparator ), $sUri );
00629     }
00630 
00631 
00640     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00641     {
00642         $sTitle = $this->encodeString( $sTitle );
00643         $sSep = self::$_sSeparator;
00644         if (!$sSep || ('/' == $sSep)) {
00645             $sSep = '_';
00646         }
00647 
00648         $sRegExp = '/[^A-Za-z0-9\/'.preg_quote( self::$_sPrefix, '/').preg_quote($sSep, '/').']+/';
00649         $sTitle = preg_replace( array("#/+#", $sRegExp, "# +#", "#(".preg_quote($sSep, '/').")+#"), $sSep, $sTitle );
00650 
00651         $oStr = getStr();
00652         // smart truncate
00653         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00654             $iFirstSpace = $oStr->strpos( $sTitle, $sSep, $this->_iIdLength);
00655             if ( $iFirstSpace !== false ) {
00656                 $sTitle = $oStr->substr( $sTitle, 0, $iFirstSpace );
00657             }
00658         }
00659 
00660         $sTitle = trim( $sTitle, $sSep );
00661 
00662         if (!$sTitle) {
00663             return self::$_sPrefix;
00664         }
00665         // cleaning
00666         return $sTitle;
00667     }
00668 
00669 
00686     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sParams = null )
00687     {
00688         $oDb = oxDb::getDb( true );
00689         if ( $iShopId === null ) {
00690             $iShopId = $this->getConfig()->getShopId();
00691         }
00692 
00693         $iLang = (int) $iLang;
00694 
00695         $sStdUrl = $this->_trimUrl( $sStdUrl );
00696         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00697         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00698 
00699         // transferring old url, thus current url will be regenerated
00700         $sQtedObjectId = $oDb->quote( $sObjectId );
00701         $iQtedShopId   = $oDb->quote( $iShopId );
00702         $sQtedType     = $oDb->quote( $sType );
00703         $sQtedSeoUrl   = $oDb->quote( $sSeoUrl );
00704         $sQtedStdUrl   = $oDb->quote( $sStdUrl );
00705         $sQtedParams   = $oDb->quote( $sParams );
00706         $sQtedIdent    = $oDb->quote( $sIdent );
00707 
00708         // transferring old url, thus current url will be regenerated
00709         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like {$sQtedStdUrl} ) as samestdurl,
00710                 oxseourl like {$sQtedSeoUrl} as sameseourl from oxseo where oxtype = {$sQtedType} and
00711                 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId}  and oxlang = {$iLang} ";
00712 
00713         $sQ .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00714         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00715         $sQ .= "limit 1";
00716 
00717         $oRs = $oDb->execute( $sQ );
00718         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00719             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00720                 // fixed state change
00721                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00722                 // nothing was changed - setting expired status back to 0
00723                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = {$sQtedType} and
00724                           oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00725                 $sSql .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00726                 $sSql .= " limit 1";
00727 
00728                 return $oDb->execute( $sSql );
00729             } elseif ( $oRs->fields['oxexpired'] ) {
00730                 // copy to history
00731                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00732             }
00733         }
00734 
00735         // inserting new or updating
00736         $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00737         $blFixed = (int) $blFixed;
00738 
00739         $sQ  = "insert into oxseo
00740                     (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxparams)
00741                 values
00742                     ( {$sQtedObjectId}, {$sQtedIdent}, {$iQtedShopId}, {$iLang}, {$sQtedStdUrl}, {$sQtedSeoUrl}, {$sQtedType}, '$blFixed', '0', {$sParams} )
00743                 on duplicate key update
00744                     oxident = {$sQtedIdent}, oxstdurl = {$sQtedStdUrl}, oxseourl = {$sQtedSeoUrl}, oxfixed = '$blFixed', oxexpired = '0'";
00745 
00746         return $oDb->execute( $sQ );
00747     }
00748 
00759     protected function _trimUrl( $sUrl, $iLang = null )
00760     {
00761         $myConfig = $this->getConfig();
00762         $oStr = getStr();
00763         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang, false ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00764         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00765         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00766         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00767         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)cur=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00768         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)stoken=[a-z0-9]+&?(amp;)?/i', '\1', $sUrl );
00769         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00770         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)+$/i', '', $sUrl );
00771         $sUrl = trim( $sUrl );
00772 
00773         // max length <= $this->_iMaxUrlLength
00774         $iLength = $this->_getMaxUrlLength();
00775         if ( $oStr->strlen( $sUrl ) > $iLength ) {
00776             $sUrl = $oStr->substr( $sUrl, 0, $iLength );
00777         }
00778 
00779         return $sUrl;
00780     }
00781 
00787     protected function _getMaxUrlLength()
00788     {
00789         if ( $this->_iMaxUrlLength === null ) {
00790             // max length <= 2048 / custom
00791             $this->_iMaxUrlLength = $this->getConfig()->getConfigParam( "iMaxSeoUrlLength" ) ;
00792             if ( !$this->_iMaxUrlLength ) {
00793                 $this->_iMaxUrlLength = 2048;
00794             }
00795         }
00796         return $this->_iMaxUrlLength;
00797     }
00798 
00807     public function encodeString( $sString, $blReplaceChars = true )
00808     {
00809         // decoding entities
00810         $sString = getStr()->html_entity_decode( $sString );
00811 
00812         if ( $blReplaceChars ) {
00813             if ( $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' ) ) {
00814                 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00815             }
00816         }
00817 
00818 
00819         // special chars
00820         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00821         return str_replace( $aReplaceWhat, '', $sString );
00822     }
00823 
00831     public function setSeparator( $sSeparator = null )
00832     {
00833         self::$_sSeparator = $sSeparator;
00834         if ( !self::$_sSeparator ) {
00835             self::$_sSeparator = '-';
00836         }
00837     }
00838 
00846     public function setPrefix( $sPrefix )
00847     {
00848         if ($sPrefix) {
00849             self::$_sPrefix = $sPrefix;
00850         } else {
00851             self::$_sPrefix = 'oxid';
00852         }
00853     }
00854 
00862     public function setIdLength( $iIdlength = null )
00863     {
00864         if ( isset( $iIdlength ) ) {
00865             $this->_iIdLength = $iIdlength;
00866         }
00867     }
00868 
00877     public function setReservedWords( $aReservedWords )
00878     {
00879         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00880     }
00881 
00882 
00894     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00895     {
00896         $oDb = oxDb::getDb();
00897         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00898         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00899         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00900         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00901 
00902         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00903         $oDb->execute( $sQ );
00904     }
00905 
00919     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00920     {
00921         if ( !isset( $iLang ) ) {
00922             $iLang = $oObject->getLanguage();
00923         }
00924         $iShopId = $this->getConfig()->getShopId();
00925 
00926         //load page link from DB
00927         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00928         if ( !$sOldSeoUrl ) {
00929             // generating new..
00930             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00931             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, $sParams );
00932         } else {
00933             // using old
00934             $sSeoUrl = $sOldSeoUrl;
00935         }
00936         return $sSeoUrl;
00937     }
00938 
00947     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00948     {
00949         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00950     }
00951 
00961     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00962     {
00963         $oDb = oxDb::getDb();
00964         $sValues = '';
00965         $sOldObjectId = null;
00966 
00967         // standard url
00968         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00969         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00970 
00971         if ( !$sObjectId || $sObjectId == '-1' ) {
00972             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00973         } else {
00974             // marking entry as needs to move to history
00975             $sOldObjectId = $sObjectId;
00976 
00977             // if std url does not match old
00978             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00979                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00980             }
00981         }
00982 
00983         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00984 
00985             $iLang = (int) $iLang;
00986 
00987             // generating seo url
00988             $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00989             if ( $sSeoUrl ) {
00990                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00991             }
00992 
00993 
00994             if ( $sOldObjectId ) {
00995                 // move changed records to history
00996                 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}' " ) ) {
00997                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00998                 }
00999             }
01000 
01001             if ( !$sSeoUrl || !$sStdUrl ) {
01002                 continue;
01003             }
01004 
01005             $sIdent = $this->_getSeoIdent( $sSeoUrl );
01006 
01007             if ( $sValues ) {
01008                 $sValues .= ', ';
01009             }
01010 
01011             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
01012         }
01013 
01014         // must delete old before insert/update
01015         if ( $sOldObjectId ) {
01016             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
01017         }
01018 
01019         // (re)inserting
01020         if ( $sValues ) {
01021 
01022             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
01023             $oDb->execute( $sQ );
01024         }
01025 
01026         return $sObjectId;
01027     }
01028 
01036     public function copyStaticUrls( $iShopId )
01037     {
01038         $iBaseShopId = $this->getConfig()->getBaseShopId();
01039         if ( $iShopId != $iBaseShopId ) {
01040             $oDb = oxDb::getDb();
01041             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
01042                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
01043                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
01044                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
01045                 $oDb->execute( $sQ );
01046             }
01047         }
01048     }
01049 
01059     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
01060     {
01061         if (!isset($iShopId)) {
01062             $iShopId = $this->getConfig()->getShopId();
01063         }
01064         if (!isset($iLang)) {
01065             $iLang   = oxLang::getInstance()->getEditLanguage();
01066         }
01067 
01068         if ( isset($this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId])) {
01069             return $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId];
01070         }
01071 
01072         $sFullUrl = '';
01073         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01074             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01075         }
01076 
01077 
01078         $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId] = $sFullUrl;
01079 
01080         return $sFullUrl;
01081     }
01082 
01101     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false, $sAltObjectId = null )
01102     {
01103         $sSeoUrl = $this->_processSeoUrl( $this->_trimUrl( $sSeoUrl ? $sSeoUrl : $this->_getAltUri( $sAltObjectId ? $sAltObjectId : $sObjectId, $iLang ) ), $sObjectId, $iLang, $blExclude );
01104         if ( $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sParams ) ) {
01105 
01106             $oDb = oxDb::getDb( true );
01107 
01108             //
01109             $sQtedObjectId = $oDb->quote( $sAltObjectId ? $sAltObjectId : $sObjectId );
01110             $iQtedShopId   = $oDb->quote( $iShopId );
01111 
01112             $oStr = getStr();
01113             if ( $sKeywords !== false ) {
01114                 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
01115             }
01116 
01117             if ( $sDescription !== false ) {
01118                 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
01119             }
01120 
01121             $sQ = "insert into oxobject2seodata
01122                        ( oxobjectid, oxshopid, oxlang, oxkeywords, oxdescription )
01123                    values
01124                        ( {$sQtedObjectId}, {$iQtedShopId}, {$iLang}, ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" )." )
01125                    on duplicate key update
01126                        oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
01127             $oDb->execute( $sQ );
01128         }
01129     }
01130 
01139     protected function _getAltUri( $sObjectId, $iLang )
01140     {
01141     }
01142 
01153     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01154     {
01155         $oDb = oxDb::getDb();
01156         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01157         oxDb::getDb()->execute( $sQ );
01158     }
01159 
01170     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
01171     {
01172         $oDb = oxDb::getDb();
01173 
01174         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01175         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getObjectTplLanguage():((int) $iLang);
01176         return $oDb->getOne( "select {$sMetaType} from oxobject2seodata where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}'" );
01177     }
01178 
01192     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01193     {
01194         startProfile("getDynamicUrl");
01195         $sDynUrl = $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01196         stopProfile("getDynamicUrl");
01197         return $sDynUrl;
01198     }
01199 
01208     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01209     {
01210         $oDb = oxDb::getDb( true );
01211         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01212         $sSeoUrl   = false;
01213 
01214         $sShopId = $this->getConfig()->getShopId();
01215 
01216         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01217         $oRs = $oDb->execute( $sQ );
01218         if ( !$oRs->EOF ) {
01219             $sSeoUrl = $oRs->fields['oxseourl'];
01220         }
01221 
01222         return $sSeoUrl;
01223     }
01224 }