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 ( 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 = 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             foreach (glob("$sDir/*") as $file) {
00540                 if (preg_match('/^(.+)\.php[0-9]*$/i', basename($file), $m)) {
00541                     self::$_aReservedEntryKeys[] = $m[0];
00542                     self::$_aReservedEntryKeys[] = $m[1];
00543                 } elseif (is_dir($file)) {
00544                     self::$_aReservedEntryKeys[] = basename($file);
00545                 }
00546             }
00547         }
00548         return self::$_aReservedEntryKeys;
00549     }
00550 
00558     protected function _prepareUri( $sUri )
00559     {
00560         // decoding entities
00561         $sUri = $this->encodeString( $sUri );
00562 
00563         // basic string preparation
00564         $sUri = strip_tags( $sUri );
00565 
00566         $sSeparator = self::$_sSeparator;
00567         $sPrefix    = self::$_sPrefix;
00568         // 'fixing' reserved words
00569         foreach ( self::$_aReservedWords as $sWord ) {
00570             // this probably possible to do in one regexp
00571             $sUri = preg_replace( array( "/(\s$sWord)$/i", "/^($sWord\s)/i", "/(\s$sWord\s)/i", "/^($sWord)$/i",
00572                                          "/(\/$sWord)$/i", "/^($sWord\/)/i", "/(\/$sWord\/)/i"),
00573                                          " \$1{$sSeparator}{$sPrefix}{$sSeparator} ", $sUri );
00574         }
00575 
00576         // if found ".html" at the end - removing it temporary
00577         $sExt = '';
00578         $oStr = getStr();
00579         $aMatched = array();
00580         if ( preg_match( '/\.html?$/i', $sUri, $aMatched ) ) {
00581             $sExt   = $oStr->substr( $sUri, 0 - $oStr->strlen( $aMatched[0] ) );
00582             $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $aMatched[0] ) );
00583         }
00584 
00585         // removing any special characters
00586         $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00587         $sUri  = trim( $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri ), self::$_sSeparator );
00588 
00589         // SEO id is empty ?
00590         if ( !$sUri ) {
00591             $sUri = $this->_prepareUri( self::$_sPrefix );
00592         }
00593 
00594         // binding ".html" back
00595         $sUri .= $sExt;
00596 
00597         // cleaning
00598         return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00599                              array( '/', self::$_sSeparator ), $sUri );
00600     }
00601 
00610     protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00611     {
00612         $sTitle = $this->_prepareUri( str_replace( "/", self::$_sSeparator, $sTitle ) );
00613 
00614         $oStr = getStr();
00615         // smart truncate
00616         if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00617             if ( ( $iFirstSpace = $oStr->strstr( $oStr->substr( $sTitle, $this->_iIdLength ), self::$_sSeparator ) !== false ) ) {
00618                 $sTitle = trim( $oStr->substr( $sTitle, 0, $this->_iIdLength + $iFirstSpace ), self::$_sSeparator );
00619             }
00620         }
00621 
00622         // cleaning
00623         return $sTitle;
00624     }
00625 
00626 
00645     protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sKeywords = false, $sDescription = false, $sParams = null )
00646     {
00647         $oDb = oxDb::getDb( true );
00648         if ( $iShopId === null ) {
00649             $iShopId = $this->getConfig()->getShopId();
00650         }
00651 
00652         $iLang = (int) $iLang;
00653 
00654         $sStdUrl = $this->_trimUrl( $sStdUrl );
00655         $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00656         $sIdent  = $this->_getSeoIdent( $sSeoUrl );
00657 
00658         // transferring old url, thus current url will be regenerated
00659         $sQ  = "select oxfixed, oxexpired, ( oxstdurl like ".$oDb->quote( $sStdUrl )." ) as samestdurl,
00660                 oxseourl like ".$oDb->quote( $sSeoUrl )." as sameseourl from oxseo where oxtype = ".$oDb->quote( $sType )." and
00661                 oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId )."  and oxlang = {$iLang} ";
00662 
00663         $sQ .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00664         $sQ .= ( $sKeywords !== false ) ? " and oxkeywords = " . $oDb->quote( $sKeywords ) . " " : '';
00665         $sQ .= ( $sDescription !== false ) ? " and oxdescription = " . $oDb->quote( $sDescription ) . " " : '';
00666         //$sQ .= isset( $blFixed ) ? " and oxfixed = " . ( (int) $blFixed ) . " " : '';
00667         $sQ .= "limit 1";
00668 
00669         $oRs = $oDb->execute( $sQ );
00670         if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00671             if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00672                 // fixed state change
00673                 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00674                 // nothing was changed - setting expired status back to 0
00675                 $sSql  = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = ".$oDb->quote( $sType )." and
00676                           oxobjectid = ".$oDb->quote( $sObjectId )." and oxshopid = ".$oDb->quote( $iShopId ) ." and oxlang = {$iLang} ";
00677                 $sSql .= $sParams ? " and oxparams = " . $oDb->quote( $sParams ) : '';
00678                 $sSql .= " limit 1";
00679 
00680                 return $oDb->execute( $sSql );
00681             } elseif ( $oRs->fields['oxexpired'] ) {
00682                 // copy to history
00683                 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00684             }
00685         }
00686         $oStr = getStr();
00687         if ( $sKeywords !== false ) {
00688             $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false ) ) );
00689         }
00690 
00691         if ( $sDescription !== false ) {
00692             $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
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, oxkeywords, oxdescription, oxparams)
00701                 values
00702                     ( ".$oDb->quote( $sObjectId ).", ".$oDb->quote( $sIdent ).", ".$oDb->quote( $iShopId ).", {$iLang}, ".$oDb->quote( $sStdUrl ).", ".$oDb->quote( $sSeoUrl ).", ".$oDb->quote( $sType ).", '$blFixed', '0',
00703                     ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" ).", $sParams )
00704                 on duplicate key update oxident = ".$oDb->quote( $sIdent ).", oxstdurl = ".$oDb->quote( $sStdUrl ).", oxseourl = ".$oDb->quote( $sSeoUrl ).", oxfixed = '$blFixed', oxexpired = '0',
00705                     oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
00706 
00707         return $oDb->execute( $sQ );
00708     }
00709 
00720     protected function _trimUrl( $sUrl, $iLang = null )
00721     {
00722         $myConfig = $this->getConfig();
00723         $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00724         $sUrl = preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00725         $sUrl = preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00726         $sUrl = preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00727         $sUrl = preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00728         return $sUrl;
00729     }
00730 
00739     public function encodeString( $sString, $blReplaceChars = true )
00740     {
00741         // decoding entities
00742         $sString = getStr()->html_entity_decode( $sString );
00743 
00744         if ( $blReplaceChars ) {
00745             $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' );
00746             $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00747         }
00748 
00749         // special chars
00750         $aReplaceWhat = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
00751         return str_replace( $aReplaceWhat, '', $sString );
00752     }
00753 
00761     public function setSeparator( $sSeparator = null )
00762     {
00763         self::$_sSeparator = $sSeparator;
00764         if ( !self::$_sSeparator ) {
00765             self::$_sSeparator = '-';
00766         }
00767     }
00768 
00776     public function setPrefix( $sPrefix )
00777     {
00778         if ($sPrefix) {
00779             self::$_sPrefix = $sPrefix;
00780         } else {
00781             self::$_sPrefix = 'oxid';
00782         }
00783     }
00784 
00792     public function setIdLength( $iIdlength = null )
00793     {
00794         if ( isset( $iIdlength ) ) {
00795             $this->_iIdLength = $iIdlength;
00796         }
00797     }
00798 
00806     public function setReservedWords( $aReservedWords )
00807     {
00808         self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00809     }
00810 
00811 
00823     public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00824     {
00825         $oDb = oxDb::getDb();
00826         $sWhere  = $sId ? "where oxobjectid =  " . $oDb->quote( $sId ) : '';
00827         $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00828         $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00829         $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00830 
00831         $sQ = "update oxseo set oxexpired =  " . $oDb->quote( $iExpStat ) . " $sWhere ";
00832         $oDb->execute( $sQ );
00833     }
00834 
00848     protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00849     {
00850         if ( !isset( $iLang ) ) {
00851             $iLang = $oObject->getLanguage();
00852         }
00853         $iShopId = $this->getConfig()->getShopId();
00854 
00855         //load page link from DB
00856         $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00857         if ( !$sOldSeoUrl ) {
00858             // generating new..
00859             $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00860             $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, false, false, $sParams );
00861         } else {
00862             // using old
00863             $sSeoUrl = $sOldSeoUrl;
00864         }
00865         return $sSeoUrl;
00866     }
00867 
00876     protected function _getStaticObjectId( $iShopId, $sStdUrl )
00877     {
00878         return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00879     }
00880 
00890     public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00891     {
00892         $oDb = oxDb::getDb();
00893         $sValues = '';
00894         $sOldObjectId = null;
00895 
00896         // standard url
00897         $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00898         $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00899 
00900         if ( !$sObjectId || $sObjectId == '-1' ) {
00901             $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00902         } else {
00903             // marking entry as needs to move to history
00904             $sOldObjectId = $sObjectId;
00905 
00906             // if std url does not match old
00907             if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00908                 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00909             }
00910         }
00911 
00912         foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00913 
00914             $iLang = (int) $iLang;
00915 
00916             // generating seo url
00917             if ( ( $sSeoUrl = trim( $sSeoUrl ) ) ) {
00918                 $sSeoUrl = $this->_prepareUri( $this->_trimUrl( $sSeoUrl ) );
00919                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00920             }
00921 
00922 
00923             if ( $sOldObjectId ) {
00924                 // move changed records to history
00925                 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}' " ) ) {
00926                     $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
00927                 }
00928             }
00929 
00930             if ( !$sSeoUrl || !$sStdUrl ) {
00931                 continue;
00932             }
00933 
00934             $sIdent = $this->_getSeoIdent( $sSeoUrl );
00935 
00936             if ( $sValues ) {
00937                 $sValues .= ', ';
00938             }
00939 
00940             $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
00941         }
00942 
00943         // must delete old before insert/update
00944         if ( $sOldObjectId ) {
00945             $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
00946         }
00947 
00948         // (re)inserting
00949         if ( $sValues ) {
00950 
00951             $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
00952             $oDb->execute( $sQ );
00953         }
00954 
00955         return $sObjectId;
00956     }
00957 
00965     public function copyStaticUrls( $iShopId )
00966     {
00967         $iBaseShopId = $this->getConfig()->getBaseShopId();
00968         if ( $iShopId != $iBaseShopId ) {
00969             $oDb = oxDb::getDb();
00970             foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
00971                 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
00972                        select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
00973                        " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
00974                 $oDb->execute( $sQ );
00975             }
00976         }
00977     }
00978 
00988     public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
00989     {
00990         if (!isset($iShopId)) {
00991             $iShopId = $this->getConfig()->getShopId();
00992         }
00993         if (!isset($iLang)) {
00994             $iLang   = oxLang::getInstance()->getEditLanguage();
00995         }
00996 
00997         $sFullUrl = '';
00998         if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
00999             $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01000         }
01001         return $sFullUrl;
01002     }
01003 
01021     public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false )
01022     {
01023         $sSeoUrl = $this->_processSeoUrl( $this->_prepareUri( $this->_trimUrl( $sSeoUrl ) ), $sObjectId, $iLang, $blExclude );
01024         $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sKeywords, $sDescription, $sParams );
01025     }
01026 
01037     public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01038     {
01039         $oDb = oxDb::getDb();
01040         $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01041         oxDb::getDb()->execute( $sQ );
01042     }
01043 
01055     public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null, $sParams = null )
01056     {
01057         $oDb = oxDb::getDb();
01058 
01059         $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01060         $iLang   = ( !isset( $iLang ) ) ? oxLang::getInstance()->getTplLanguage():((int) $iLang);
01061         $sParams = ( !isset( $sParams ) ) ? "order by oxparams" : "and oxparams=".$oDb->quote( $sParams );
01062 
01063         return $oDb->getOne( "select {$sMetaType} from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}' {$sParams}" );
01064     }
01065 
01079     public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01080     {
01081         return $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01082     }
01083 
01092     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01093     {
01094         $oDb = oxDb::getDb( true );
01095         $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxLang::getInstance()->getBaseLanguage();
01096         $sSeoUrl   = false;
01097 
01098         $sShopId = $this->getConfig()->getShopId();
01099 
01100         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = ".$oDb->quote( $sStdUrl )." and oxlang = '$iLanguage' and oxshopid = '$sShopId' limit 1";
01101         $oRs = $oDb->execute( $sQ );
01102         if ( !$oRs->EOF ) {
01103             $sSeoUrl = $oRs->fields['oxseourl'];
01104         }
01105 
01106         return $sSeoUrl;
01107     }
01108 }

Generated by  doxygen 1.6.2