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 from oxseo where oxtype = ".$oDb->quote( $sType )."
00374                    and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00375 
00376             $sParams = $sParams ? $oDb->quote( $sParams ) : "''";
00377             if ( $sParams && $blStrictParamsCheck ) {
00378                 $sQ .= " and oxparams = {$sParams}";
00379             } else {
00380                 $sQ .= " order by oxparams = {$sParams} desc";
00381             }
00382             $sQ .= " limit 1";
00383 
00384             self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00385         }
00386         return self::$_aFixedCache[$sType][$sShopId][$sId][$iLang];
00387     }
00388 
00399     protected function _getCacheKey( $sType, $iLang = null, $iShopId = null, $sParams = null )
00400     {
00401         $blAdmin = $this->isAdmin();
00402         if ( !$blAdmin && $sType !== "oxarticle" ) {
00403             return $sType . ( (int) $iLang ) . ( (int) $iShopId ) . "seo";
00404         }
00405 
00406         // use cache in non admin mode
00407         if ( self::$_sCacheKey === null ) {
00408             self::$_sCacheKey = false;
00409             if ( !$blAdmin && ( $oView = $this->getConfig()->getActiveView() ) ) {
00410                 self::$_sCacheKey = md5( $oView->getViewId() ) . "seo";
00411             }
00412         }
00413         return self::$_sCacheKey;
00414     }
00415 
00427     protected function _loadFromCache( $sCacheIdent, $sType, $iLang = null, $iShopId = null, $sParams = null )
00428     {
00429         startProfile( "seoencoder_loadFromCache" );
00430 
00431         $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams );
00432         $sCache = false;
00433 
00434         if ( $sCacheKey && !isset( $sCacheKey, self::$_aCache ) ) {
00435             self::$_aCache[$sCacheKey] = oxUtils::getInstance()->fromFileCache( $sCacheKey );
00436         }
00437 
00438         if ( isset( self::$_aCache[$sCacheKey] ) && isset( self::$_aCache[$sCacheKey][$sCacheIdent] ) ) {
00439             $sCache = self::$_aCache[$sCacheKey][$sCacheIdent];
00440         }
00441 
00442         stopProfile( "seoencoder_loadFromCache" );
00443         return $sCache;
00444     }
00445 
00458     protected function _saveInCache( $sCacheIdent, $sCache, $sType, $iLang = null, $iShopId = null, $sParams = null )
00459     {
00460         startProfile( "seoencoder_saveInCache" );
00461 
00462         $blSaved = false;
00463         if ( $sCache && ( $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams ) ) !== false ) {
00464             self::$_aCache[$sCacheKey][$sCacheIdent] = $sCache;
00465             $blSaved = oxUtils::getInstance()->toFileCache( $sCacheKey, self::$_aCache[$sCacheKey] );
00466         }
00467 
00468         stopProfile( "seoencoder_saveInCache" );
00469         return $blSaved;
00470     }
00471 
00487     protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00488     {
00489         if ( $iShopId === null ) {
00490             $iShopId = $this->getConfig()->getShopId();
00491         }
00492 
00493         $iLang = (int) $iLang;
00494 
00495         $oDb = oxDb::getDb( true );
00496         $sQ = "select oxfixed, oxseourl, oxexpired, oxtype from oxseo where oxtype = ".$oDb->quote( $sType )."
00497                and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00498 
00499         $sParams = $sParams ? $sParams : '';
00500         if ( $sParams && $blStrictParamsCheck ) {
00501             $sQ .= " and oxparams = '{$sParams}'";
00502         } else {
00503             $sQ .= " order by oxparams = '{$sParams}' desc";
00504         }
00505         $sQ .= " limit 1";
00506 
00507         // caching to avoid same queries..
00508         $sIdent = md5( $sQ );
00509 
00510         // looking in cache
00511         if ( ( $sSeoUrl = $this->_loadFromCache( $sIdent, $sType, $iLang, $iShopId, $sParams ) ) === false ) {
00512 
00513             $oRs = $oDb->execute( $sQ );
00514             if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00515                 // moving expired static urls to history ..
00516                 if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00517                     // if expired - copying to history, marking as not expired
00518                     $this->_copyToHistory( $sId, $iShopId, $iLang );
00519                     $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00520                     $sSeoUrl = $oRs->fields['oxseourl'];
00521                 } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00522                     // if seo url is available and is valid
00523                     $sSeoUrl = $oRs->fields['oxseourl'];
00524                 }
00525 
00526                 // storing in cache
00527                 $this->_saveInCache( $sIdent, $sSeoUrl, $sType, $iLang, $iShopId, $sParams );
00528             }
00529         }
00530         return $sSeoUrl;
00531     }
00532 
00539     protected function _getReservedEntryKeys()
00540     {
00541         if ( !isset( self::$_aReservedEntryKeys ) || !is_array( self::$_aReservedEntryKeys ) ) {
00542             $sDir = getShopBasePath();
00543             self::$_aReservedEntryKeys = array_map('preg_quote', self::$_aReservedWords, array('#'));
00544             $oStr = getStr();
00545             foreach ( glob( "$sDir/*" ) as $sFile ) {
00546                 if ( $oStr->preg_match( '/^(.+)\.php[0-9]*$/i', basename( $sFile ), $aMatches ) ) {
00547                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[0], '#' );
00548                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[1], '#' );
00549                 } elseif ( is_dir( $sFile ) ) {
00550                     self::$_aReservedEntryKeys[] = preg_quote( basename( $sFile ), '#' );
00551                 }
00552             }
00553             self::$_aReservedEntryKeys = array_unique(self::$_aReservedEntryKeys);
00554         }
00555         return self::$_aReservedEntryKeys;
00556     }
00557 
00565     protected function _prepareUri( $sUri )
00566     {
00567         // decoding entities
00568         $sUri = $this->encodeString( $sUri );
00569 
00570         // basic string preparation
00571         $sUri = strip_tags( $sUri );
00572         $oStr = getStr();
00573 
00574 
00575         // if found ".html" or "/" at the end - removing it temporary
00576         $oStr = getStr();
00577         $sExt = $this->_getUrlExtension();
00578         if ($sExt === null) {
00579             $aMatched = array();
00580             if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sUri, $aMatched ) ) {
00581                 $sExt = $aMatched[0];
00582             } else {
00583                 $sExt = '/';
00584             }
00585         }
00586         if ( $sExt && $oStr->substr( $sUri, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00587             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $sExt ) );
00588         }
00589 
00590         // removing any special characters
00591         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00592         $sUri  = $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri );
00593 
00594         // SEO id is empty ?
00595         if ( !$sUri && self::$_sPrefix ) {
00596             $sUri = $this->_prepareUri( self::$_sPrefix );
00597         }
00598 
00599         $sAdd = '';
00600         if ('/' != self::$_sSeparator) {
00601             $sAdd = self::$_sSeparator . self::$_sPrefix;
00602             $sUri = trim($sUri, self::$_sSeparator);
00603         } else {
00604             $sAdd = '_' . self::$_sPrefix;
00605         }
00606 
00607         // binding the ending back
00608         $sUri .= $sExt;
00609 
00610         // fix for not having url, which executes through /other/ script then seo decoder
00611         $sUri = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")(/|$)#i", "\$1\$2$sAdd\$3", $sUri );
00612 
00613         // cleaning
00614         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00615                              array( '/', self::$_sSeparator ), $sUri );
00616     }
00617 
00618 
00627     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00628     {
00629         $sTitle = $this->encodeString( $sTitle );
00630         $sSep = self::$_sSeparator;
00631         if (!$sSep || ('/' == $sSep)) {
00632             $sSep = '_';
00633         }
00634 
00635         $sRegExp = '/[^A-Za-z0-9\/'.preg_quote( self::$_sPrefix, '/').preg_quote($sSep, '/').']+/';
00636         $sTitle = preg_replace( array("#/+#", $sRegExp, "# +#", "#(".preg_quote($sSep, '/').")+#"), $sSep, $sTitle );
00637 
00638         $oStr = getStr();
00639         // smart truncate
00640         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00641             $iFirstSpace = $oStr->strpos( $sTitle, $sSep, $this->_iIdLength);
00642             if ( $iFirstSpace !== false ) {
00643                 $sTitle = $oStr->substr( $sTitle, 0, $iFirstSpace );
00644             }
00645         }
00646 
00647         $sTitle = trim( $sTitle, $sSep );
00648 
00649         if (!$sTitle) {
00650             return self::$_sPrefix;
00651         }
00652         // cleaning
00653         return $sTitle;
00654     }
00655 
00656 
00673     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sParams = null )
00674     {
00675         $oDb = oxDb::getDb( true );
00676         if ( $iShopId === null ) {
00677             $iShopId = $this->getConfig()->getShopId();
00678         }
00679 
00680         $iLang = (int) $iLang;
00681 
00682         $sStdUrl = $this->_trimUrl( $sStdUrl );
00683         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00684         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00685 
00686         // transferring old url, thus current url will be regenerated
00687         $sQtedObjectId = $oDb->quote( $sObjectId );
00688         $iQtedShopId   = $oDb->quote( $iShopId );
00689         $sQtedType     = $oDb->quote( $sType );
00690         $sQtedSeoUrl   = $oDb->quote( $sSeoUrl );
00691         $sQtedStdUrl   = $oDb->quote( $sStdUrl );
00692         $sQtedParams   = $oDb->quote( $sParams );
00693         $sQtedIdent    = $oDb->quote( $sIdent );
00694 
00695         // transferring old url, thus current url will be regenerated
00696         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like {$sQtedStdUrl} ) as samestdurl,
00697                 oxseourl like {$sQtedSeoUrl} as sameseourl from oxseo where oxtype = {$sQtedType} and
00698                 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId}  and oxlang = {$iLang} ";
00699 
00700         $sQ .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00701         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00702         $sQ .= "limit 1";
00703 
00704         $oRs = $oDb->execute( $sQ );
00705         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00706             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00707                 // fixed state change
00708                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00709                 // nothing was changed - setting expired status back to 0
00710                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = {$sQtedType} and
00711                           oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00712                 $sSql .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00713                 $sSql .= " limit 1";
00714 
00715                 return $oDb->execute( $sSql );
00716             } elseif ( $oRs->fields['oxexpired'] ) {
00717                 // copy to history
00718                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00719             }
00720         }
00721 
00722         // inserting new or updating
00723         $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00724         $blFixed = (int) $blFixed;
00725 
00726         $sQ  = "insert into oxseo
00727                     (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxparams)
00728                 values
00729                     ( {$sQtedObjectId}, {$sQtedIdent}, {$iQtedShopId}, {$iLang}, {$sQtedStdUrl}, {$sQtedSeoUrl}, {$sQtedType}, '$blFixed', '0', {$sParams} )
00730                 on duplicate key update
00731                     oxident = {$sQtedIdent}, oxstdurl = {$sQtedStdUrl}, oxseourl = {$sQtedSeoUrl}, oxfixed = '$blFixed', oxexpired = '0'";
00732 
00733         return $oDb->execute( $sQ );
00734     }
00735 
00746     protected function _trimUrl( $sUrl, $iLang = null )
00747     {
00748         $myConfig = $this->getConfig();
00749         $oStr = getStr();
00750         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang, false ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00751         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00752         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00753         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00754         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)cur=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00755         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)stoken=[a-z0-9]+&?(amp;)?/i', '\1', $sUrl );
00756         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00757         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)+$/i', '', $sUrl );
00758         $sUrl = trim( $sUrl );
00759 
00760         // max length <= $this->_iMaxUrlLength
00761         $iLength = $this->_getMaxUrlLength();
00762         if ( $oStr->strlen( $sUrl ) > $iLength ) {
00763             $sUrl = $oStr->substr( $sUrl, 0, $iLength );
00764         }
00765 
00766         return $sUrl;
00767     }
00768 
00774     protected function _getMaxUrlLength()
00775     {
00776         if ( $this->_iMaxUrlLength === null ) {
00777             // max length <= 2048 / custom
00778             $this->_iMaxUrlLength = $this->getConfig()->getConfigParam( "iMaxSeoUrlLength" ) ;
00779             if ( !$this->_iMaxUrlLength ) {
00780                 $this->_iMaxUrlLength = 2048;
00781             }
00782         }
00783         return $this->_iMaxUrlLength;
00784     }
00785 
00794     public function encodeString( $sString, $blReplaceChars = true )
00795     {
00796         // decoding entities
00797         $sString = getStr()->html_entity_decode( $sString );
00798 
00799         if ( $blReplaceChars ) {
00800             if ( $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' ) ) {
00801                 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00802             }
00803         }
00804 
00805         // special chars
00806         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00807         return str_replace( $aReplaceWhat, '', $sString );
00808     }
00809 
00817     public function setSeparator( $sSeparator = null )
00818     {
00819         self::$_sSeparator = $sSeparator;
00820         if ( !self::$_sSeparator ) {
00821             self::$_sSeparator = '-';
00822         }
00823     }
00824 
00832     public function setPrefix( $sPrefix )
00833     {
00834         if ($sPrefix) {
00835             self::$_sPrefix = $sPrefix;
00836         } else {
00837             self::$_sPrefix = 'oxid';
00838         }
00839     }
00840 
00848     public function setIdLength( $iIdlength = null )
00849     {
00850         if ( isset( $iIdlength ) ) {
00851             $this->_iIdLength = $iIdlength;
00852         }
00853     }
00854 
00863     public function setReservedWords( $aReservedWords )
00864     {
00865         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00866     }
00867 
00868 
00880     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00881     {
00882         $oDb = oxDb::getDb();
00883         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00884         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00885         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00886         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00887 
00888         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00889         $oDb->execute( $sQ );
00890     }
00891 
00905     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00906     {
00907         if ( !isset( $iLang ) ) {
00908             $iLang = $oObject->getLanguage();
00909         }
00910         $iShopId = $this->getConfig()->getShopId();
00911 
00912         //load page link from DB
00913         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00914         if ( !$sOldSeoUrl ) {
00915             // generating new..
00916             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00917             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, $sParams );
00918         } else {
00919             // using old
00920             $sSeoUrl = $sOldSeoUrl;
00921         }
00922         return $sSeoUrl;
00923     }
00924 
00933     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00934     {
00935         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00936     }
00937 
00947     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00948     {
00949         $oDb = oxDb::getDb();
00950         $sValues = '';
00951         $sOldObjectId = null;
00952 
00953         // standard url
00954         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00955         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00956 
00957         if ( !$sObjectId || $sObjectId == '-1' ) {
00958             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00959         } else {
00960             // marking entry as needs to move to history
00961             $sOldObjectId = $sObjectId;
00962 
00963             // if std url does not match old
00964             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00965                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00966             }
00967         }
00968 
00969         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00970 
00971             $iLang = (int) $iLang;
00972 
00973             // generating seo url
00974             $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00975             if ( $sSeoUrl ) {
00976                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00977             }
00978 
00979 
00980             if ( $sOldObjectId ) {
00981                 // move changed records to history
00982                 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}' " ) ) {
00983                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00984                 }
00985             }
00986 
00987             if ( !$sSeoUrl || !$sStdUrl ) {
00988                 continue;
00989             }
00990 
00991             $sIdent = $this->_getSeoIdent( $sSeoUrl );
00992 
00993             if ( $sValues ) {
00994                 $sValues .= ', ';
00995             }
00996 
00997             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
00998         }
00999 
01000         // must delete old before insert/update
01001         if ( $sOldObjectId ) {
01002             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
01003         }
01004 
01005         // (re)inserting
01006         if ( $sValues ) {
01007 
01008             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
01009             $oDb->execute( $sQ );
01010         }
01011 
01012         return $sObjectId;
01013     }
01014 
01022     public function copyStaticUrls( $iShopId )
01023     {
01024         $iBaseShopId = $this->getConfig()->getBaseShopId();
01025         if ( $iShopId != $iBaseShopId ) {
01026             $oDb = oxDb::getDb();
01027             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
01028                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
01029                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
01030                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
01031                 $oDb->execute( $sQ );
01032             }
01033         }
01034     }
01035 
01045     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
01046     {
01047         if (!isset($iShopId)) {
01048             $iShopId = $this->getConfig()->getShopId();
01049         }
01050         if (!isset($iLang)) {
01051             $iLang   = oxLang::getInstance()->getEditLanguage();
01052         }
01053 
01054         if ( isset($this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId])) {
01055             return $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId];
01056         }
01057 
01058         $sFullUrl = '';
01059         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01060             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01061         }
01062 
01063 
01064         $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId] = $sFullUrl;
01065 
01066         return $sFullUrl;
01067     }
01068 
01087     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false, $sAltObjectId = null )
01088     {
01089         $sSeoUrl = $this->_processSeoUrl( $this->_trimUrl( $sSeoUrl ? $sSeoUrl : $this->_getAltUri( $sAltObjectId ? $sAltObjectId : $sObjectId, $iLang ) ), $sObjectId, $iLang, $blExclude );
01090         if ( $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sParams ) ) {
01091 
01092             $oDb = oxDb::getDb( true );
01093 
01094             //
01095             $sQtedObjectId = $oDb->quote( $sAltObjectId ? $sAltObjectId : $sObjectId );
01096             $iQtedShopId   = $oDb->quote( $iShopId );
01097 
01098             $oStr = getStr();
01099             if ( $sKeywords !== false ) {
01100                 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
01101             }
01102 
01103             if ( $sDescription !== false ) {
01104                 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
01105             }
01106 
01107             $sQ = "insert into oxobject2seodata
01108                        ( oxobjectid, oxshopid, oxlang, oxkeywords, oxdescription )
01109                    values
01110                        ( {$sQtedObjectId}, {$iQtedShopId}, {$iLang}, ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" )." )
01111                    on duplicate key update
01112                        oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
01113             $oDb->execute( $sQ );
01114         }
01115     }
01116 
01125     protected function _getAltUri( $sObjectId, $iLang )
01126     {
01127     }
01128 
01139     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01140     {
01141         $oDb = oxDb::getDb();
01142         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01143         oxDb::getDb()->execute( $sQ );
01144     }
01145 
01156     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
01157     {
01158         $oDb = oxDb::getDb();
01159 
01160         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01161         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getObjectTplLanguage():((int) $iLang);
01162         return $oDb->getOne( "select {$sMetaType} from oxobject2seodata where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}'" );
01163     }
01164 
01178     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01179     {
01180         startProfile("getDynamicUrl");
01181         $sDynUrl = $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01182         stopProfile("getDynamicUrl");
01183         return $sDynUrl;
01184     }
01185 
01194     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01195     {
01196         $oDb = oxDb::getDb( true );
01197         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01198         $sSeoUrl   = false;
01199 
01200         $sShopId = $this->getConfig()->getShopId();
01201 
01202         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01203         $oRs = $oDb->execute( $sQ );
01204         if ( !$oRs->EOF ) {
01205             $sSeoUrl = $oRs->fields['oxseourl'];
01206         }
01207 
01208         return $sSeoUrl;
01209     }
01210 }