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         return oxUtilsUrl::getInstance()->processSeoUrl( $sFullUrl );
00315     }
00316 
00327     protected function _getSeoIdent( $sSeoUrl, $iLang = null )
00328     {
00329         return md5( strtolower( $sSeoUrl ) );
00330     }
00331 
00341     protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00342     {
00343         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00344         return $this->_loadFromDb( 'static', $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00345     }
00346 
00352     protected function _getUrlExtension()
00353     {
00354         return;
00355     }
00356 
00369     protected function _getUniqueSeoUrl( $sSeoUrl, $sObjectId = null, $iObjectLang = null )
00370     {
00371         $oStr = getStr();
00372         $sConstEnd = $this->_getUrlExtension();
00373         if ($sConstEnd === null) {
00374             $aMatched = array();
00375             if ( $oStr->preg_match('/\.html?$/i', $sSeoUrl, $aMatched ) ) {
00376                 $sConstEnd = $aMatched[0];
00377             } else {
00378                 if ($sSeoUrl{$oStr->strlen($sSeoUrl)-1} != '/') {
00379                     $sSeoUrl .= '/';
00380                 }
00381                 $sConstEnd = '/';
00382             }
00383         }
00384 
00385         // fix for not having url, which executes through /other/ script then seo decoder
00386         $sAdd = ' ';
00387         if ('/' != self::$_sSeparator) {
00388             $sAdd = self::$_sSeparator . self::$_sPrefix;
00389         } else {
00390             $sAdd = '_' . self::$_sPrefix;
00391         }
00392         $sSeoUrl = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")/#i", "\$1\$2$sAdd/", $sSeoUrl );
00393 
00394         $sBaseSeoUrl = $sSeoUrl;
00395         if ( $sConstEnd && $oStr->substr( $sSeoUrl, 0 - $oStr->strlen( $sConstEnd ) ) == $sConstEnd ) {
00396             $sBaseSeoUrl = $oStr->substr( $sSeoUrl, 0, $oStr->strlen( $sSeoUrl ) - $oStr->strlen( $sConstEnd ) );
00397         }
00398 
00399         $oDb = oxDb::getDb();
00400         $iShopId = $this->getConfig()->getShopId();
00401         $iCnt = 0;
00402         $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00403         $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00404 
00405         // skipping self
00406         if ( $sObjectId && isset($iObjectLang) ) {
00407             $iObjectLang = (int) $iObjectLang;
00408             $sQ .= " and not (oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = $iObjectLang)";
00409         }
00410 
00411         while ( $oDb->getOne( $sQ ." and oxident= " . $oDb->quote( $this->_getSeoIdent( $sCheckSeoUrl ) ) ) ) {
00412             $sAdd = '';
00413             if ( self::$_sPrefix ) {
00414                 $sAdd = self::$_sSeparator . self::$_sPrefix;
00415             }
00416             if ( $iCnt ) {
00417                 $sAdd .= self::$_sSeparator . $iCnt;
00418             }
00419             ++$iCnt;
00420 
00421             $sSeoUrl = $sBaseSeoUrl . $sAdd . $sConstEnd;
00422             $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00423         }
00424         return $sSeoUrl;
00425     }
00426 
00441     protected function _isFixed( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00442     {
00443         if ( $iShopId === null ) {
00444             $iShopId = $this->getConfig()->getShopId();
00445         }
00446         $iLang = (int) $iLang;
00447 
00448         if ( !isset( $this->_aFixedCache[$sType][$sShopId][$sId][$iLang] ) ) {
00449             $oDb = oxDb::getDb( true );
00450 
00451             $sQ = "select oxfixed from oxseo where oxtype = ".$oDb->quote( $sType )."
00452                    and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00453 
00454             $sParams = $sParams ? $oDb->quote( $sParams ) : "''";
00455             if ( $sParams && $blStrictParamsCheck ) {
00456                 $sQ .= " and oxparams = {$sParams}";
00457             } else {
00458                 $sQ .= " order by oxparams = {$sParams} desc";
00459             }
00460             $sQ .= " limit 1";
00461 
00462             $this->_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00463         }
00464         return $this->_aFixedCache[$sType][$sShopId][$sId][$iLang];
00465     }
00466 
00482     protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00483     {
00484         $oDb = oxDb::getDb( true );
00485         if ( $iShopId === null ) {
00486             $iShopId = $this->getConfig()->getShopId();
00487         }
00488 
00489         $iLang = (int) $iLang;
00490 
00491         $sQ = "select oxfixed, oxseourl, oxexpired, oxtype from oxseo where oxtype = ".$oDb->quote( $sType )."
00492                and oxobjectid = ".$oDb->quote( $sId ) ." and oxshopid = ".$oDb->quote( $iShopId )." and oxlang = '{$iLang}'";
00493 
00494         $sParams = $sParams ? $sParams : '';
00495         if ( $sParams && $blStrictParamsCheck ) {
00496             $sQ .= " and oxparams = '{$sParams}'";
00497         } else {
00498             $sQ .= " order by oxparams = '{$sParams}' desc";
00499         }
00500         $sQ .= " limit 1";
00501 
00502         // caching to avoid same queries..
00503         $sIdent = md5($sQ);
00504         if ( isset( $this->_aSeoCache[$sIdent] ) ) {
00505             return $this->_aSeoCache[$sIdent];
00506         }
00507 
00508         $sSeoUrl = false;
00509         $oRs = $oDb->execute( $sQ );
00510         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00511             // moving expired static urls to history ..
00512             if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00513                 // if expired - copying to history, marking as not expired
00514                 $this->_copyToHistory( $sId, $iShopId, $iLang );
00515                 $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00516                 $sSeoUrl = $oRs->fields['oxseourl'];
00517             } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00518                 // if seo url is available and is valid
00519                 $sSeoUrl = $oRs->fields['oxseourl'];
00520             }
00521 
00522             // store cache
00523             $this->_aSeoCache[$sIdent] = $sSeoUrl;
00524         }
00525         return $sSeoUrl;
00526     }
00527 
00534     protected function _getReservedEntryKeys()
00535     {
00536         if (!isset(self::$_aReservedEntryKeys) && !is_array(self::$_aReservedEntryKeys)) {
00537             $sDir = getShopBasePath();
00538             self::$_aReservedEntryKeys = array();
00539             $oStr = getStr();
00540             foreach (glob("$sDir/*") as $file) {
00541                 if ( $oStr->preg_match('/^(.+)\.php[0-9]*$/i', basename($file), $m)) {
00542                     self::$_aReservedEntryKeys[] = $m[0];
00543                     self::$_aReservedEntryKeys[] = $m[1];
00544                 } elseif (is_dir($file)) {
00545                     self::$_aReservedEntryKeys[] = basename($file);
00546                 }
00547             }
00548         }
00549         return self::$_aReservedEntryKeys;
00550     }
00551 
00559     protected function _prepareUri( $sUri )
00560     {
00561         // decoding entities
00562         $sUri = $this->encodeString( $sUri );
00563 
00564         // basic string preparation
00565         $sUri = strip_tags( $sUri );
00566         $oStr = getStr();
00567 
00568         $sSeparator = self::$_sSeparator;
00569         $sPrefix    = self::$_sPrefix;
00570         // 'fixing' reserved words
00571         foreach ( self::$_aReservedWords as $sWord ) {
00572             // this probably possible to do in one regexp
00573             $sUri = $oStr->preg_replace( array( "/(\s$sWord)$/i", "/^($sWord\s)/i", "/(\s$sWord\s)/i", "/^($sWord)$/i",
00574                                          "/(\/$sWord)$/i", "/^($sWord\/)/i", "/(\/$sWord\/)/i"),
00575                                          " \$1{$sSeparator}{$sPrefix}{$sSeparator} ", $sUri );
00576         }
00577 
00578         // if found ".html" at the end - removing it temporary
00579         $sExt = '';
00580         $oStr = getStr();
00581         $aMatched = array();
00582         if ( $oStr->preg_match( '/\.html?$/i', $sUri, $aMatched ) ) {
00583             $sExt   = $oStr->substr( $sUri, 0 - $oStr->strlen( $aMatched[0] ) );
00584             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $aMatched[0] ) );
00585         }
00586 
00587         // removing any special characters
00588         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00589         $sUri  = trim( $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri ), self::$_sSeparator );
00590 
00591         // SEO id is empty ?
00592         if ( !$sUri ) {
00593             $sUri = $this->_prepareUri( self::$_sPrefix );
00594         }
00595 
00596         // binding ".html" back
00597         $sUri .= $sExt;
00598 
00599         // cleaning
00600         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00601                              array( '/', self::$_sSeparator ), $sUri );
00602     }
00603 
00612     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00613     {
00614         $sTitle = $this->_prepareUri( str_replace( "/", self::$_sSeparator, $sTitle ) );
00615 
00616         $oStr = getStr();
00617         // smart truncate
00618         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00619             if ( ( $iFirstSpace = $oStr->strstr( $oStr->substr( $sTitle, $this->_iIdLength ), self::$_sSeparator ) !== false ) ) {
00620                 $sTitle = trim( $oStr->substr( $sTitle, 0, $this->_iIdLength + $iFirstSpace ), self::$_sSeparator );
00621             }
00622         }
00623 
00624         // cleaning
00625         return $sTitle;
00626     }
00627 
00628 
00647     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sKeywords = false, $sDescription = false, $sParams = null )
00648     {
00649         $oDb = oxDb::getDb( true );
00650         if ( $iShopId === null ) {
00651             $iShopId = $this->getConfig()->getShopId();
00652         }
00653 
00654         $iLang = (int) $iLang;
00655 
00656         $sStdUrl = $this->_trimUrl( $sStdUrl );
00657         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00658         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00659 
00660         // transferring old url, thus current url will be regenerated
00661         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like ".$oDb->quote( $sStdUrl )." ) as samestdurl,
00662                 oxseourl like ".$oDb->quote( $sSeoUrl )." as sameseourl from oxseo where oxtype = ".$oDb->quote( $sType )." and
00663                 oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId )."  and oxlang = {$iLang} ";
00664 
00665         $sQ .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00666         $sQ .= ( $sKeywords !== false ) ? " and oxkeywords = " . $oDb->quote( $sKeywords ) . " " : '';
00667         $sQ .= ( $sDescription !== false ) ? " and oxdescription = " . $oDb->quote( $sDescription ) . " " : '';
00668         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00669         $sQ .= "limit 1";
00670 
00671         $oRs = $oDb->execute( $sQ );
00672         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00673             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00674                 // fixed state change
00675                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00676                 // nothing was changed - setting expired status back to 0
00677                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = ".$oDb->quote( $sType )." and
00678                           oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId ) ." and oxlang = {$iLang} ";
00679                 $sSql .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00680                 $sSql .= " limit 1";
00681 
00682                 return $oDb->execute( $sSql );
00683             } elseif ( $oRs->fields['oxexpired'] ) {
00684                 // copy to history
00685                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00686             }
00687         }
00688         $oStr = getStr();
00689         if ( $sKeywords !== false ) {
00690             $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
00691         }
00692 
00693         if ( $sDescription !== false ) {
00694             $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
00695         }
00696 
00697         // inserting new or updating
00698         $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00699         $blFixed = (int) $blFixed;
00700 
00701         $sQ  = "insert into oxseo
00702                     (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxkeywords, oxdescription, oxparams)
00703                 values
00704                     ( ".$oDb->quote( $sObjectId ).", ".$oDb->quote( $sIdent ).", ".$oDb->quote( $iShopId ).", {$iLang}, ".$oDb->quote( $sStdUrl ).", ".$oDb->quote( $sSeoUrl ).", ".$oDb->quote( $sType ).", '$blFixed', '0',
00705                     ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" ).", $sParams )
00706                 on duplicate key update oxident = ".$oDb->quote( $sIdent ).", oxstdurl = ".$oDb->quote( $sStdUrl ).", oxseourl = ".$oDb->quote( $sSeoUrl ).", oxfixed = '$blFixed', oxexpired = '0',
00707                     oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
00708 
00709         return $oDb->execute( $sQ );
00710     }
00711 
00722     protected function _trimUrl( $sUrl, $iLang = null )
00723     {
00724         $myConfig = $this->getConfig();
00725         $oStr = getStr();
00726         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00727         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00728         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00729         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00730         $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00731         return $sUrl;
00732     }
00733 
00742     public function encodeString( $sString, $blReplaceChars = true )
00743     {
00744         // decoding entities
00745         $sString = getStr()->html_entity_decode( $sString );
00746 
00747         if ( $blReplaceChars ) {
00748             $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' );
00749             $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00750         }
00751 
00752         // special chars
00753         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00754         return str_replace( $aReplaceWhat, '', $sString );
00755     }
00756 
00764     public function setSeparator( $sSeparator = null )
00765     {
00766         self::$_sSeparator = $sSeparator;
00767         if ( !self::$_sSeparator ) {
00768             self::$_sSeparator = '-';
00769         }
00770     }
00771 
00779     public function setPrefix( $sPrefix )
00780     {
00781         if ($sPrefix) {
00782             self::$_sPrefix = $sPrefix;
00783         } else {
00784             self::$_sPrefix = 'oxid';
00785         }
00786     }
00787 
00795     public function setIdLength( $iIdlength = null )
00796     {
00797         if ( isset( $iIdlength ) ) {
00798             $this->_iIdLength = $iIdlength;
00799         }
00800     }
00801 
00809     public function setReservedWords( $aReservedWords )
00810     {
00811         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00812     }
00813 
00814 
00826     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00827     {
00828         $oDb = oxDb::getDb();
00829         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00830         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00831         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00832         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00833 
00834         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00835         $oDb->execute( $sQ );
00836     }
00837 
00851     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00852     {
00853         if ( !isset( $iLang ) ) {
00854             $iLang = $oObject->getLanguage();
00855         }
00856         $iShopId = $this->getConfig()->getShopId();
00857 
00858         //load page link from DB
00859         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00860         if ( !$sOldSeoUrl ) {
00861             // generating new..
00862             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00863             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, false, false, $sParams );
00864         } else {
00865             // using old
00866             $sSeoUrl = $sOldSeoUrl;
00867         }
00868         return $sSeoUrl;
00869     }
00870 
00879     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00880     {
00881         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00882     }
00883 
00893     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00894     {
00895         $oDb = oxDb::getDb();
00896         $sValues = '';
00897         $sOldObjectId = null;
00898 
00899         // standard url
00900         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00901         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00902 
00903         if ( !$sObjectId || $sObjectId == '-1' ) {
00904             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00905         } else {
00906             // marking entry as needs to move to history
00907             $sOldObjectId = $sObjectId;
00908 
00909             // if std url does not match old
00910             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00911                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00912             }
00913         }
00914 
00915         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00916 
00917             $iLang = (int) $iLang;
00918 
00919             // generating seo url
00920             if ( ( $sSeoUrl = trim( $sSeoUrl ) ) ) {
00921                 $sSeoUrl = $this->_prepareUri( $this->_trimUrl( $sSeoUrl ) );
00922                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00923             }
00924 
00925 
00926             if ( $sOldObjectId ) {
00927                 // move changed records to history
00928                 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}' " ) ) {
00929                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00930                 }
00931             }
00932 
00933             if ( !$sSeoUrl || !$sStdUrl ) {
00934                 continue;
00935             }
00936 
00937             $sIdent = $this->_getSeoIdent( $sSeoUrl );
00938 
00939             if ( $sValues ) {
00940                 $sValues .= ', ';
00941             }
00942 
00943             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
00944         }
00945 
00946         // must delete old before insert/update
00947         if ( $sOldObjectId ) {
00948             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
00949         }
00950 
00951         // (re)inserting
00952         if ( $sValues ) {
00953 
00954             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
00955             $oDb->execute( $sQ );
00956         }
00957 
00958         return $sObjectId;
00959     }
00960 
00968     public function copyStaticUrls( $iShopId )
00969     {
00970         $iBaseShopId = $this->getConfig()->getBaseShopId();
00971         if ( $iShopId != $iBaseShopId ) {
00972             $oDb = oxDb::getDb();
00973             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
00974                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
00975                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
00976                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
00977                 $oDb->execute( $sQ );
00978             }
00979         }
00980     }
00981 
00991     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
00992     {
00993         if (!isset($iShopId)) {
00994             $iShopId = $this->getConfig()->getShopId();
00995         }
00996         if (!isset($iLang)) {
00997             $iLang   = oxLang::getInstance()->getEditLanguage();
00998         }
00999 
01000         $sFullUrl = '';
01001         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01002             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01003         }
01004         return $sFullUrl;
01005     }
01006 
01024     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false )
01025     {
01026         $sSeoUrl = $this->_processSeoUrl( $this->_prepareUri( $this->_trimUrl( $sSeoUrl ) ), $sObjectId, $iLang, $blExclude );
01027         $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sKeywords, $sDescription, $sParams );
01028     }
01029 
01040     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01041     {
01042         $oDb = oxDb::getDb();
01043         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01044         oxDb::getDb()->execute( $sQ );
01045     }
01046 
01058     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null, $sParams = null )
01059     {
01060         $oDb = oxDb::getDb();
01061 
01062         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01063         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getObjectTplLanguage():((int) $iLang);
01064         $sParams = ( !isset( $sParams ) ) ? "order by oxparams" : "and oxparams=".$oDb->quote( $sParams );
01065 
01066         return $oDb->getOne( "select {$sMetaType} from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}' {$sParams}" );
01067     }
01068 
01082     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01083     {
01084         return $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01085     }
01086 
01095     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01096     {
01097         $oDb = oxDb::getDb( true );
01098         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01099         $sSeoUrl   = false;
01100 
01101         $sShopId = $this->getConfig()->getShopId();
01102 
01103         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01104         $oRs = $oDb->execute( $sQ );
01105         if ( !$oRs->EOF ) {
01106             $sSeoUrl = $oRs->fields['oxseourl'];
01107         }
01108 
01109         return $sSeoUrl;
01110     }
01111 }

Generated by  doxygen 1.6.2