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 $_aSeoCache = array();
00065 
00071     protected $_aFixedCache = array();
00072 
00078     public static function getInstance()
00079     {
00080         if ( defined( 'OXID_PHP_UNIT' ) ) {
00081             self::$_instance = modInstances::getMod( __CLASS__ );
00082         }
00083 
00084         if (!self::$_instance) {
00085             self::$_instance = oxNew("oxSeoEncoder");
00086             if ( defined( 'OXID_PHP_UNIT' ) ) {
00087                 modInstances::addMod( __CLASS__, self::$_instance);
00088             }
00089         }
00090 
00091         if ( defined( 'OXID_PHP_UNIT' ) ) {
00092             // resetting cache
00093             self::$_instance->_aSeoCache = array();
00094         }
00095 
00096         return self::$_instance;
00097 
00098     }
00099 
00108     public function addLanguageParam( $sSeoUrl, $iLang )
00109     {
00110         $iLang    = (int) $iLang;
00111         $iDefLang = (int) $this->getConfig()->getConfigParam( 'iDefSeoLang' );
00112         $aLangIds = oxLang::getInstance()->getLanguageIds();
00113 
00114         if ( $iLang != $iDefLang && isset( $aLangIds[$iLang] ) && getStr()->strpos( $sSeoUrl, $aLangIds[$iLang] . '/' ) !== 0 ) {
00115             $sSeoUrl = $aLangIds[$iLang] . '/'.$sSeoUrl;
00116         }
00117 
00118         return $sSeoUrl;
00119     }
00120 
00133     protected function _processSeoUrl( $sSeoUrl, $sObjectId = null, $iLang = null, $blExclude = false )
00134     {
00135         return $this->_getUniqueSeoUrl( $blExclude ? $sSeoUrl : $this->addLanguageParam( $sSeoUrl, $iLang ), $sObjectId, $iLang );
00136     }
00137 
00143     public function resetCache()
00144     {
00145         $this->_aSeoCache = array();
00146     }
00147 
00151     public function __construct()
00152     {
00153         $myConfig = $this->getConfig();
00154         if (!self::$_sSeparator) {
00155             $this->setSeparator( $myConfig->getConfigParam( 'sSEOSeparator' ) );
00156         }
00157         if (!self::$_sPrefix) {
00158             $this->setPrefix( $myConfig->getConfigParam( 'sSEOuprefix' ) );
00159         }
00160         $this->setReservedWords( $myConfig->getConfigParam( 'aSEOReservedWords' ) );
00161     }
00162 
00174     protected function _copyToHistory( $sId, $iShopId, $iLang, $sType = null, $sNewId = null )
00175     {
00176         $oDb = oxDb::getDb();
00177         $sObjectid = $sNewId?$oDb->quote( $sNewId ):'oxobjectid';
00178         $sType     = $sType?"oxtype =".$oDb->quote( $sType )." and":'';
00179         $iLang     = (int) $iLang;
00180 
00181         // moving
00182         $sSub = "select $sObjectid, MD5( LOWER( oxseourl ) ), oxshopid, oxlang, now() from oxseo
00183                  where {$sType} oxobjectid = ".$oDb->quote( $sId )." and oxshopid = ".$oDb->quote( $iShopId )." and
00184                  oxlang = {$iLang} limit 1";
00185         $sQ   = "replace oxseohistory ( oxobjectid, oxident, oxshopid, oxlang, oxinsert ) {$sSub}";
00186         $oDb->execute( $sQ );
00187     }
00188 
00197     protected function _getAddParams()
00198     {
00199         // performance
00200         if ( $this->_sAddParams === null ) {
00201             $this->_sAddParams = $this->_getAddParamsFnc( oxConfig::getParameter('currency'), $this->getConfig()->getShopId() );
00202         }
00203         return $this->_sAddParams;
00204     }
00205 
00217     protected function _getAddParamsFnc( $iCur, $iActShop )
00218     {
00219         $sParams = '';
00220         if ( count( $aParams = oxUtilsUrl::getInstance()->getAddUrlParams() ) ) {
00221             foreach ( $aParams as $sName => $sValue ) {
00222                 if ( $sValue ) {
00223                     if ( $sParams ) {
00224                         $sParams .= "&amp;";
00225                     }
00226                     $sParams .= $sName . "=" . $sValue;
00227                 }
00228             }
00229             if ( $sParams ) {
00230                 $sParams = '?'.$sParams;
00231             }
00232         }
00233         return $sParams;
00234     }
00235 
00246     protected function _getDynamicObjectId( $iShopId, $sStdUrl )
00247     {
00248         return $this->getDynamicObjectId( $iShopId, $sStdUrl );
00249     }
00250 
00259     public function getDynamicObjectId( $iShopId, $sStdUrl )
00260     {
00261         return $this->_getStaticObjectId( $iShopId, $sStdUrl );
00262     }
00263 
00273     protected function _getDynamicUri( $sStdUrl, $sSeoUrl, $iLang )
00274     {
00275         $iShopId = $this->getConfig()->getShopId();
00276 
00277         $sStdUrl   = $this->_trimUrl( $sStdUrl );
00278         $sObjectId = $this->getDynamicObjectId( $iShopId, $sStdUrl );
00279         $sSeoUrl   = $this->_prepareUri( $sSeoUrl );
00280 
00281         //load details link from DB
00282         $sOldSeoUrl = $this->_loadFromDb( 'dynamic', $sObjectId, $iLang );
00283         if ( $sOldSeoUrl === $sSeoUrl ) {
00284             $sSeoUrl = $sOldSeoUrl;
00285         } else {
00286 
00287             if ( $sOldSeoUrl ) {
00288                 // old must be transferred to history
00289                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, 'dynamic' );
00290             }
00291 
00292             // creating unique
00293             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00294 
00295             // inserting
00296             $this->_saveToDb( 'dynamic', $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId );
00297         }
00298 
00299         return $sSeoUrl;
00300     }
00301 
00311     protected function _getFullUrl( $sSeoUrl, $iLang = null, $blSsl = false )
00312     {
00313         $sFullUrl = ( $blSsl ? $this->getConfig()->getSslShopUrl( $iLang ) : $this->getConfig()->getShopUrl( $iLang ) ) . $sSeoUrl;
00314         $sProcessedUrl =  oxUtilsUrl::getInstance()->processSeoUrl( $sFullUrl );
00315         return $sProcessedUrl;
00316     }
00317 
00328     protected function _getSeoIdent( $sSeoUrl, $iLang = null )
00329     {
00330         return md5( strtolower( $sSeoUrl ) );
00331     }
00332 
00342     protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00343     {
00344         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00345         return $this->_loadFromDb( 'static', $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00346     }
00347 
00353     protected function _getUrlExtension()
00354     {
00355         return;
00356     }
00357 
00370     protected function _getUniqueSeoUrl( $sSeoUrl, $sObjectId = null, $iObjectLang = null )
00371     {
00372         $oStr = getStr();
00373         $sConstEnd = $this->_getUrlExtension();
00374         if ($sConstEnd === null) {
00375             $aMatched = array();
00376             if ( $oStr->preg_match( '/\.html?$/i', $sSeoUrl, $aMatched ) ) {
00377                 $sConstEnd = $aMatched[0];
00378             } else {
00379                 if ($sSeoUrl{$oStr->strlen($sSeoUrl)-1} != '/') {
00380                     $sSeoUrl .= '/';
00381                 }
00382                 $sConstEnd = '/';
00383             }
00384         }
00385 
00386         // fix for not having url, which executes through /other/ script then seo decoder
00387         $sAdd = ' ';
00388         if ('/' != self::$_sSeparator) {
00389             $sAdd = self::$_sSeparator . self::$_sPrefix;
00390         } else {
00391             $sAdd = '_' . self::$_sPrefix;
00392         }
00393         $sSeoUrl = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")/#i", "\$1\$2$sAdd/", $sSeoUrl );
00394 
00395         $sBaseSeoUrl = $sSeoUrl;
00396         if ( $sConstEnd && $oStr->substr( $sSeoUrl, 0 - $oStr->strlen( $sConstEnd ) ) == $sConstEnd ) {
00397             $sBaseSeoUrl = $oStr->substr( $sSeoUrl, 0, $oStr->strlen( $sSeoUrl ) - $oStr->strlen( $sConstEnd ) );
00398         }
00399 
00400         $oDb = oxDb::getDb();
00401         $iShopId = $this->getConfig()->getShopId();
00402         $iCnt = 0;
00403         $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00404         $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00405 
00406         // skipping self
00407         if ( $sObjectId && isset($iObjectLang) ) {
00408             $iObjectLang = (int) $iObjectLang;
00409             $sQ .= " and not (oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = $iObjectLang)";
00410         }
00411 
00412         while ( $oDb->getOne( $sQ ." and oxident= " . $oDb->quote( $this->_getSeoIdent( $sCheckSeoUrl ) ) ) ) {
00413             $sAdd = '';
00414             if ( self::$_sPrefix ) {
00415                 $sAdd = self::$_sSeparator . self::$_sPrefix;
00416             }
00417             if ( $iCnt ) {
00418                 $sAdd .= self::$_sSeparator . $iCnt;
00419             }
00420             ++$iCnt;
00421 
00422             $sSeoUrl = $sBaseSeoUrl . $sAdd . $sConstEnd;
00423             $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00424         }
00425         return $sSeoUrl;
00426     }
00427 
00442     protected function _isFixed( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00443     {
00444         if ( $iShopId === null ) {
00445             $iShopId = $this->getConfig()->getShopId();
00446         }
00447         $iLang = (int) $iLang;
00448 
00449         if ( !isset( $this->_aFixedCache[$sType][$sShopId][$sId][$iLang] ) ) {
00450             $oDb = oxDb::getDb( true );
00451 
00452             $sQ = "select oxfixed from oxseo where oxtype = ".$oDb->quote( $sType )."
00453                    and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00454 
00455             $sParams = $sParams ? $oDb->quote( $sParams ) : "''";
00456             if ( $sParams && $blStrictParamsCheck ) {
00457                 $sQ .= " and oxparams = {$sParams}";
00458             } else {
00459                 $sQ .= " order by oxparams = {$sParams} desc";
00460             }
00461             $sQ .= " limit 1";
00462 
00463             $this->_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00464         }
00465         return $this->_aFixedCache[$sType][$sShopId][$sId][$iLang];
00466     }
00467 
00483     protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00484     {
00485         $oDb = oxDb::getDb( true );
00486         if ( $iShopId === null ) {
00487             $iShopId = $this->getConfig()->getShopId();
00488         }
00489 
00490         $iLang = (int) $iLang;
00491 
00492         $sQ = "select oxfixed, oxseourl, oxexpired, oxtype from oxseo where oxtype = ".$oDb->quote( $sType )."
00493                and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00494 
00495         $sParams = $sParams ? $sParams : '';
00496         if ( $sParams && $blStrictParamsCheck ) {
00497             $sQ .= " and oxparams = '{$sParams}'";
00498         } else {
00499             $sQ .= " order by oxparams = '{$sParams}' desc";
00500         }
00501         $sQ .= " limit 1";
00502 
00503         // caching to avoid same queries..
00504         $sIdent = md5($sQ);
00505         if ( isset( $this->_aSeoCache[$sIdent] ) ) {
00506             return $this->_aSeoCache[$sIdent];
00507         }
00508 
00509         $sSeoUrl = false;
00510         $oRs = $oDb->execute( $sQ );
00511         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00512             // moving expired static urls to history ..
00513             if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00514                 // if expired - copying to history, marking as not expired
00515                 $this->_copyToHistory( $sId, $iShopId, $iLang );
00516                 $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00517                 $sSeoUrl = $oRs->fields['oxseourl'];
00518             } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00519                 // if seo url is available and is valid
00520                 $sSeoUrl = $oRs->fields['oxseourl'];
00521             }
00522 
00523             // store cache
00524             $this->_aSeoCache[$sIdent] = $sSeoUrl;
00525         }
00526         return $sSeoUrl;
00527     }
00528 
00535     protected function _getReservedEntryKeys()
00536     {
00537         if ( !isset( self::$_aReservedEntryKeys ) && !is_array( self::$_aReservedEntryKeys ) ) {
00538             $sDir = getShopBasePath();
00539             self::$_aReservedEntryKeys = array();
00540             $oStr = getStr();
00541             foreach ( glob( "$sDir/*" ) as $sFile ) {
00542                 if ( $oStr->preg_match( '/^(.+)\.php[0-9]*$/i', basename( $sFile ), $aMatches ) ) {
00543                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[0] );
00544                     self::$_aReservedEntryKeys[] = preg_quote( $aMatches[1] );
00545                 } elseif ( is_dir( $sFile ) ) {
00546                     self::$_aReservedEntryKeys[] = preg_quote( basename( $sFile ) );
00547                 }
00548             }
00549         }
00550         return self::$_aReservedEntryKeys;
00551     }
00552 
00560     protected function _prepareUri( $sUri )
00561     {
00562         // decoding entities
00563         $sUri = $this->encodeString( $sUri );
00564 
00565         // basic string preparation
00566         $sUri = strip_tags( $sUri );
00567         $oStr = getStr();
00568 
00569         $sSeparator = self::$_sSeparator;
00570         $sPrefix    = self::$_sPrefix;
00571         // 'fixing' reserved words
00572         foreach ( self::$_aReservedWords as $sWord ) {
00573             $sWord = preg_quote( $sWord );
00574             $sUri = $oStr->preg_replace( array( "/(\s{$sWord})$/i", "/^({$sWord}\s)/i", "/(\s{$sWord}\s)/i", "/^({$sWord})$/i",
00575                                          "/(\/{$sWord})$/i", "/^({$sWord}\/)/i", "/(\/{$sWord}\/)/i"),
00576                                          " \$1{$sSeparator}{$sPrefix}{$sSeparator} ", $sUri );
00577         }
00578 
00579         // if found ".html" at the end - removing it temporary
00580         $sExt = '';
00581         $oStr = getStr();
00582         $aMatched = array();
00583         if ( $oStr->preg_match( '/\.html?$/i', $sUri, $aMatched ) ) {
00584             $sExt   = $oStr->substr( $sUri, 0 - $oStr->strlen( $aMatched[0] ) );
00585             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $aMatched[0] ) );
00586         }
00587 
00588         // removing any special characters
00589         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00590         $sUri  = trim( $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri ), self::$_sSeparator );
00591 
00592         // SEO id is empty ?
00593         if ( !$sUri ) {
00594             $sUri = $this->_prepareUri( self::$_sPrefix );
00595         }
00596 
00597         // binding ".html" back
00598         $sUri .= $sExt;
00599 
00600         // cleaning
00601         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00602                              array( '/', self::$_sSeparator ), $sUri );
00603     }
00604 
00613     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00614     {
00615         $sTitle = $this->_prepareUri( str_replace( "/", self::$_sSeparator, $sTitle ) );
00616 
00617         $oStr = getStr();
00618         // smart truncate
00619         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00620             if ( ( $iFirstSpace = $oStr->strstr( $oStr->substr( $sTitle, $this->_iIdLength ), self::$_sSeparator ) !== false ) ) {
00621                 $sTitle = trim( $oStr->substr( $sTitle, 0, $this->_iIdLength + $iFirstSpace ), self::$_sSeparator );
00622             }
00623         }
00624 
00625         // cleaning
00626         return $sTitle;
00627     }
00628 
00629 
00646     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sParams = null )
00647     {
00648         $oDb = oxDb::getDb( true );
00649         if ( $iShopId === null ) {
00650             $iShopId = $this->getConfig()->getShopId();
00651         }
00652 
00653         $iLang = (int) $iLang;
00654 
00655         $sStdUrl = $this->_trimUrl( $sStdUrl );
00656         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00657         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00658 
00659         // transferring old url, thus current url will be regenerated
00660         $sQtedObjectId = $oDb->quote( $sObjectId );
00661         $iQtedShopId   = $oDb->quote( $iShopId );
00662         $sQtedType     = $oDb->quote( $sType );
00663         $sQtedSeoUrl   = $oDb->quote( $sSeoUrl );
00664         $sQtedStdUrl   = $oDb->quote( $sStdUrl );
00665         $sQtedParams   = $oDb->quote( $sParams );
00666         $sQtedIdent    = $oDb->quote( $sIdent );
00667 
00668         // transferring old url, thus current url will be regenerated
00669         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like {$sQtedStdUrl} ) as samestdurl,
00670                 oxseourl like {$sQtedSeoUrl} as sameseourl from oxseo where oxtype = {$sQtedType} and
00671                 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId}  and oxlang = {$iLang} ";
00672 
00673         $sQ .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00674         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00675         $sQ .= "limit 1";
00676 
00677         $oRs = $oDb->execute( $sQ );
00678         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00679             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00680                 // fixed state change
00681                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00682                 // nothing was changed - setting expired status back to 0
00683                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = {$sQtedType} and
00684                           oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00685                 $sSql .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00686                 $sSql .= " limit 1";
00687 
00688                 return $oDb->execute( $sSql );
00689             } elseif ( $oRs->fields['oxexpired'] ) {
00690                 // copy to history
00691                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00692             }
00693         }
00694 
00695         // inserting new or updating
00696         $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00697         $blFixed = (int) $blFixed;
00698 
00699         $sQ  = "insert into oxseo
00700                     (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxparams)
00701                 values
00702                     ( {$sQtedObjectId}, {$sQtedIdent}, {$iQtedShopId}, {$iLang}, {$sQtedStdUrl}, {$sQtedSeoUrl}, {$sQtedType}, '$blFixed', '0', {$sParams} )
00703                 on duplicate key update
00704                     oxident = {$sQtedIdent}, oxstdurl = {$sQtedStdUrl}, oxseourl = {$sQtedSeoUrl}, oxfixed = '$blFixed', oxexpired = '0'";
00705 
00706         return $oDb->execute( $sQ );
00707     }
00708 
00719     protected function _trimUrl( $sUrl, $iLang = null )
00720     {
00721         $myConfig = $this->getConfig();
00722         $oStr = getStr();
00723         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00724         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00725         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00726         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00727         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00728         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)+$/i', '', $sUrl );
00729         return $sUrl;
00730     }
00731 
00740     public function encodeString( $sString, $blReplaceChars = true )
00741     {
00742         // decoding entities
00743         $sString = getStr()->html_entity_decode( $sString );
00744 
00745         if ( $blReplaceChars ) {
00746             $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' );
00747             $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00748         }
00749 
00750         // special chars
00751         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00752         return str_replace( $aReplaceWhat, '', $sString );
00753     }
00754 
00762     public function setSeparator( $sSeparator = null )
00763     {
00764         self::$_sSeparator = $sSeparator;
00765         if ( !self::$_sSeparator ) {
00766             self::$_sSeparator = '-';
00767         }
00768     }
00769 
00777     public function setPrefix( $sPrefix )
00778     {
00779         if ($sPrefix) {
00780             self::$_sPrefix = $sPrefix;
00781         } else {
00782             self::$_sPrefix = 'oxid';
00783         }
00784     }
00785 
00793     public function setIdLength( $iIdlength = null )
00794     {
00795         if ( isset( $iIdlength ) ) {
00796             $this->_iIdLength = $iIdlength;
00797         }
00798     }
00799 
00807     public function setReservedWords( $aReservedWords )
00808     {
00809         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00810     }
00811 
00812 
00824     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00825     {
00826         $oDb = oxDb::getDb();
00827         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00828         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00829         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00830         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00831 
00832         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00833         $oDb->execute( $sQ );
00834     }
00835 
00849     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00850     {
00851         if ( !isset( $iLang ) ) {
00852             $iLang = $oObject->getLanguage();
00853         }
00854         $iShopId = $this->getConfig()->getShopId();
00855 
00856         //load page link from DB
00857         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00858         if ( !$sOldSeoUrl ) {
00859             // generating new..
00860             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00861             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, $sParams );
00862         } else {
00863             // using old
00864             $sSeoUrl = $sOldSeoUrl;
00865         }
00866         return $sSeoUrl;
00867     }
00868 
00877     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00878     {
00879         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00880     }
00881 
00891     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00892     {
00893         $oDb = oxDb::getDb();
00894         $sValues = '';
00895         $sOldObjectId = null;
00896 
00897         // standard url
00898         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00899         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00900 
00901         if ( !$sObjectId || $sObjectId == '-1' ) {
00902             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00903         } else {
00904             // marking entry as needs to move to history
00905             $sOldObjectId = $sObjectId;
00906 
00907             // if std url does not match old
00908             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00909                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00910             }
00911         }
00912 
00913         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00914 
00915             $iLang = (int) $iLang;
00916 
00917             // generating seo url
00918             if ( ( $sSeoUrl = trim( $sSeoUrl ) ) ) {
00919                 $sSeoUrl = $this->_prepareUri( $this->_trimUrl( $sSeoUrl ) );
00920                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00921             }
00922 
00923 
00924             if ( $sOldObjectId ) {
00925                 // move changed records to history
00926                 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}' " ) ) {
00927                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00928                 }
00929             }
00930 
00931             if ( !$sSeoUrl || !$sStdUrl ) {
00932                 continue;
00933             }
00934 
00935             $sIdent = $this->_getSeoIdent( $sSeoUrl );
00936 
00937             if ( $sValues ) {
00938                 $sValues .= ', ';
00939             }
00940 
00941             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
00942         }
00943 
00944         // must delete old before insert/update
00945         if ( $sOldObjectId ) {
00946             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
00947         }
00948 
00949         // (re)inserting
00950         if ( $sValues ) {
00951 
00952             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
00953             $oDb->execute( $sQ );
00954         }
00955 
00956         return $sObjectId;
00957     }
00958 
00966     public function copyStaticUrls( $iShopId )
00967     {
00968         $iBaseShopId = $this->getConfig()->getBaseShopId();
00969         if ( $iShopId != $iBaseShopId ) {
00970             $oDb = oxDb::getDb();
00971             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
00972                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
00973                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
00974                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
00975                 $oDb->execute( $sQ );
00976             }
00977         }
00978     }
00979 
00989     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
00990     {
00991         if (!isset($iShopId)) {
00992             $iShopId = $this->getConfig()->getShopId();
00993         }
00994         if (!isset($iLang)) {
00995             $iLang   = oxLang::getInstance()->getEditLanguage();
00996         }
00997 
00998         if ( isset($this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId])) {
00999             return $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId];
01000         }
01001 
01002         $sFullUrl = '';
01003         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01004             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01005         }
01006 
01007 
01008         $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId] = $sFullUrl;
01009 
01010         return $sFullUrl;
01011     }
01012 
01031     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false, $sAltObjectId = null )
01032     {
01033         $sSeoUrl = $this->_processSeoUrl( $this->_prepareUri( $this->_trimUrl( $sSeoUrl ? $sSeoUrl : $this->_getAltUri( $sAltObjectId ? $sAltObjectId : $sObjectId, $iLang ) ) ), $sObjectId, $iLang, $blExclude );
01034         if ( $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sParams ) ) {
01035 
01036             $oDb = oxDb::getDb( true );
01037 
01038             //
01039             $sQtedObjectId = $oDb->quote( $sAltObjectId ? $sAltObjectId : $sObjectId );
01040             $iQtedShopId   = $oDb->quote( $iShopId );
01041 
01042             $oStr = getStr();
01043             if ( $sKeywords !== false ) {
01044                 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
01045             }
01046 
01047             if ( $sDescription !== false ) {
01048                 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
01049             }
01050 
01051             $sQ = "insert into oxobject2seodata
01052                        ( oxobjectid, oxshopid, oxlang, oxkeywords, oxdescription )
01053                    values
01054                        ( {$sQtedObjectId}, {$iQtedShopId}, {$iLang}, ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" )." )
01055                    on duplicate key update
01056                        oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
01057             $oDb->execute( $sQ );
01058         }
01059     }
01060 
01069     protected function _getAltUri( $sObjectId, $iLang )
01070     {
01071     }
01072 
01083     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01084     {
01085         $oDb = oxDb::getDb();
01086         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01087         oxDb::getDb()->execute( $sQ );
01088     }
01089 
01100     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
01101     {
01102         $oDb = oxDb::getDb();
01103 
01104         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01105         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getObjectTplLanguage():((int) $iLang);
01106         return $oDb->getOne( "select {$sMetaType} from oxobject2seodata where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}'" );
01107     }
01108 
01122     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01123     {
01124         startProfile("getDynamicUrl");
01125         $sDynUrl = $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01126         stopProfile("getDynamicUrl");
01127         return $sDynUrl;
01128     }
01129 
01138     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01139     {
01140         $oDb = oxDb::getDb( true );
01141         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01142         $sSeoUrl   = false;
01143 
01144         $sShopId = $this->getConfig()->getShopId();
01145 
01146         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01147         $oRs = $oDb->execute( $sQ );
01148         if ( !$oRs->EOF ) {
01149             $sSeoUrl = $oRs->fields['oxseourl'];
01150         }
01151 
01152         return $sSeoUrl;
01153     }
01154 }