00001 <?php
00002
00007 class oxSeoEncoder extends oxSuperCfg
00008 {
00015 protected static $_aReservedWords = array( 'admin' );
00016
00022 protected static $_aReservedEntryKeys = null;
00023
00029 protected static $_sSeparator = null;
00030
00036 protected $_iIdLength = 255;
00037
00043 protected static $_sPrefix = null;
00044
00050 protected $_sAddParams = null;
00051
00057 protected static $_instance = null;
00058
00064 protected static $_aFixedCache = array();
00065
00070 protected static $_sCacheKey = null;
00071
00076 protected static $_aCache = array();
00077
00082 protected $_iMaxUrlLength = null;
00083
00091 public static function getInstance()
00092 {
00093 return oxRegistry::get("oxSeoEncoder");
00094 }
00095
00104 public function addLanguageParam( $sSeoUrl, $iLang )
00105 {
00106 $iLang = (int) $iLang;
00107 $iDefLang = (int) $this->getConfig()->getConfigParam( 'iDefSeoLang' );
00108 $aLangIds = oxRegistry::getLang()->getLanguageIds();
00109
00110 if ( $iLang != $iDefLang && isset( $aLangIds[$iLang] ) && getStr()->strpos( $sSeoUrl, $aLangIds[$iLang] . '/' ) !== 0 ) {
00111 $sSeoUrl = $aLangIds[$iLang] . '/'.$sSeoUrl;
00112 }
00113
00114 return $sSeoUrl;
00115 }
00116
00129 protected function _processSeoUrl( $sSeoUrl, $sObjectId = null, $iLang = null, $blExclude = false )
00130 {
00131 if (!$blExclude) {
00132 $sSeoUrl = $this->addLanguageParam( $sSeoUrl, $iLang );
00133 }
00134 return $this->_getUniqueSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00135 }
00136
00140 public function __construct()
00141 {
00142 $myConfig = $this->getConfig();
00143 if (!self::$_sSeparator) {
00144 $this->setSeparator( $myConfig->getConfigParam( 'sSEOSeparator' ) );
00145 }
00146 if (!self::$_sPrefix) {
00147 $this->setPrefix( $myConfig->getConfigParam( 'sSEOuprefix' ) );
00148 }
00149 $this->setReservedWords( $myConfig->getConfigParam( 'aSEOReservedWords' ) );
00150 }
00151
00163 protected function _copyToHistory( $sId, $iShopId, $iLang, $sType = null, $sNewId = null )
00164 {
00165 $oDb = oxDb::getDb();
00166 $sObjectid = $sNewId?$oDb->quote( $sNewId ):'oxobjectid';
00167 $sType = $sType?"oxtype =".$oDb->quote( $sType )." and":'';
00168 $iLang = (int) $iLang;
00169
00170
00171 $sSub = "select $sObjectid, MD5( LOWER( oxseourl ) ), oxshopid, oxlang, now() from oxseo
00172 where {$sType} oxobjectid = ".$oDb->quote( $sId )." and oxshopid = ".$oDb->quote( $iShopId )." and
00173 oxlang = {$iLang} and oxexpired = '1'";
00174 $sQ = "replace oxseohistory ( oxobjectid, oxident, oxshopid, oxlang, oxinsert ) {$sSub}";
00175 $oDb->execute( $sQ );
00176 }
00177
00186 public function getDynamicObjectId( $iShopId, $sStdUrl )
00187 {
00188 return $this->_getStaticObjectId( $iShopId, $sStdUrl );
00189 }
00190
00200 protected function _getDynamicUri( $sStdUrl, $sSeoUrl, $iLang )
00201 {
00202 $iShopId = $this->getConfig()->getShopId();
00203
00204 $sStdUrl = $this->_trimUrl( $sStdUrl );
00205 $sObjectId = $this->getDynamicObjectId( $iShopId, $sStdUrl );
00206 $sSeoUrl = $this->_prepareUri( $this->addLanguageParam( $sSeoUrl, $iLang ), $iLang );
00207
00208
00209 $sOldSeoUrl = $this->_loadFromDb( 'dynamic', $sObjectId, $iLang );
00210 if ( $sOldSeoUrl === $sSeoUrl ) {
00211 $sSeoUrl = $sOldSeoUrl;
00212 } else {
00213
00214 if ( $sOldSeoUrl ) {
00215
00216 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, 'dynamic' );
00217 }
00218
00219
00220 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00221
00222
00223 $this->_saveToDb( 'dynamic', $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId );
00224 }
00225
00226 return $sSeoUrl;
00227 }
00228
00238 protected function _getFullUrl( $sSeoUrl, $iLang = null, $blSsl = false )
00239 {
00240 if ( $sSeoUrl ) {
00241 $sFullUrl = ( $blSsl ? $this->getConfig()->getSslShopUrl( $iLang ) : $this->getConfig()->getShopUrl( $iLang, false ) ) . $sSeoUrl;
00242 return oxRegistry::get("oxUtilsUrl")->processSeoUrl( $sFullUrl );
00243 }
00244 return false;
00245 }
00246
00256 protected function _getSeoIdent( $sSeoUrl )
00257 {
00258 return md5( strtolower( $sSeoUrl ) );
00259 }
00260
00270 protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00271 {
00272 $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00273 return $this->_loadFromDb( 'static', $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00274 }
00275
00281 protected function _getUrlExtension()
00282 {
00283 return;
00284 }
00285
00298 protected function _getUniqueSeoUrl( $sSeoUrl, $sObjectId = null, $iObjectLang = null )
00299 {
00300 $sSeoUrl = $this->_prepareUri( $sSeoUrl, $iObjectLang );
00301 $oStr = getStr();
00302 $sExt = '';
00303 if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sSeoUrl, $aMatched ) ) {
00304 $sExt = $aMatched[0];
00305 }
00306 $sBaseSeoUrl = $sSeoUrl;
00307 if ( $sExt && $oStr->substr( $sSeoUrl, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00308 $sBaseSeoUrl = $oStr->substr( $sSeoUrl, 0, $oStr->strlen( $sSeoUrl ) - $oStr->strlen( $sExt ) );
00309 }
00310
00311 $iShopId = $this->getConfig()->getShopId();
00312 $iCnt = 0;
00313 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00314 $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00315
00316 $oDb = oxDb::getDb();
00317
00318 if ( $sObjectId && isset($iObjectLang) ) {
00319 $iObjectLang = (int) $iObjectLang;
00320 $sQ .= " and not (oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = $iObjectLang)";
00321 }
00322
00323 while ( $oDb->getOne( $sQ ." and oxident= " . $oDb->quote( $this->_getSeoIdent( $sCheckSeoUrl ) ) ) ) {
00324 $sAdd = '';
00325 if ( self::$_sPrefix ) {
00326 $sAdd = self::$_sSeparator . self::$_sPrefix;
00327 }
00328 if ( $iCnt ) {
00329 $sAdd .= self::$_sSeparator . $iCnt;
00330 }
00331 ++$iCnt;
00332
00333 $sSeoUrl = $sBaseSeoUrl . $sAdd . $sExt;
00334 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl );
00335 }
00336 return $sSeoUrl;
00337 }
00338
00353 protected function _isFixed( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00354 {
00355 if ( $iShopId === null ) {
00356 $iShopId = $this->getConfig()->getShopId();
00357 }
00358 $iLang = (int) $iLang;
00359
00360 if ( !isset( self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] ) ) {
00361 $oDb = oxDb::getDb();
00362
00363 $sQ = "SELECT `oxfixed`
00364 FROM `oxseo`
00365 WHERE `oxtype` = ".$oDb->quote( $sType )."
00366 AND `oxobjectid` = ".$oDb->quote( $sId ) ."
00367 AND `oxshopid` = ".$oDb->quote( $iShopId )."
00368 AND `oxlang` = '{$iLang}'";
00369
00370 $sParams = $sParams ? $oDb->quote( $sParams ) : "''";
00371 if ( $sParams && $blStrictParamsCheck ) {
00372 $sQ .= " AND `oxparams` = {$sParams}";
00373 } else {
00374 $sQ .= " ORDER BY `oxparams` ASC";
00375 }
00376 $sQ .= " LIMIT 1";
00377
00378 self::$_aFixedCache[$sType][$sShopId][$sId][$iLang] = (bool) $oDb->getOne( $sQ );
00379 }
00380 return self::$_aFixedCache[$sType][$sShopId][$sId][$iLang];
00381 }
00382
00393 protected function _getCacheKey( $sType, $iLang = null, $iShopId = null, $sParams = null )
00394 {
00395 $blAdmin = $this->isAdmin();
00396 if ( !$blAdmin && $sType !== "oxarticle" ) {
00397 return $sType . ( (int) $iLang ) . ( (int) $iShopId ) . "seo";
00398 }
00399
00400
00401 if ( self::$_sCacheKey === null ) {
00402 self::$_sCacheKey = false;
00403 if ( !$blAdmin && ( $oView = $this->getConfig()->getActiveView() ) ) {
00404 self::$_sCacheKey = md5( $oView->getViewId() ) . "seo";
00405 }
00406 }
00407 return self::$_sCacheKey;
00408 }
00409
00421 protected function _loadFromCache( $sCacheIdent, $sType, $iLang = null, $iShopId = null, $sParams = null )
00422 {
00423 if ( !$this->getConfig()->getConfigParam( 'blEnableSeoCache' ) ) {
00424 return false;
00425 }
00426
00427 startProfile( "seoencoder_loadFromCache" );
00428
00429 $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams );
00430 $sCache = false;
00431
00432 if ( $sCacheKey && !isset( self::$_aCache[$sCacheKey] ) ) {
00433 self::$_aCache[$sCacheKey] = oxRegistry::getUtils()->fromFileCache( $sCacheKey );
00434 }
00435
00436 if ( isset( self::$_aCache[$sCacheKey] ) && isset( self::$_aCache[$sCacheKey][$sCacheIdent] ) ) {
00437 $sCache = self::$_aCache[$sCacheKey][$sCacheIdent];
00438 }
00439
00440 stopProfile( "seoencoder_loadFromCache" );
00441 return $sCache;
00442 }
00443
00456 protected function _saveInCache( $sCacheIdent, $sCache, $sType, $iLang = null, $iShopId = null, $sParams = null )
00457 {
00458 if ( !$this->getConfig()->getConfigParam( 'blEnableSeoCache' ) ) {
00459 return false;
00460 }
00461
00462 startProfile( "seoencoder_saveInCache" );
00463
00464 $blSaved = false;
00465 if ( $sCache && ( $sCacheKey = $this->_getCacheKey( $sType, $iLang, $iShopId, $sParams ) ) !== false ) {
00466 self::$_aCache[$sCacheKey][$sCacheIdent] = $sCache;
00467 $blSaved = oxRegistry::getUtils()->toFileCache( $sCacheKey, self::$_aCache[$sCacheKey] );
00468 }
00469
00470 stopProfile( "seoencoder_saveInCache" );
00471 return $blSaved;
00472 }
00473
00489 protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00490 {
00491
00492 if ( $iShopId === null ) {
00493 $iShopId = $this->getConfig()->getShopId();
00494 }
00495
00496 $iLang = (int) $iLang;
00497 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00498
00499 $sQ = "
00500 SELECT
00501 `oxfixed`,
00502 `oxseourl`,
00503 `oxexpired`,
00504 `oxtype`
00505 FROM `oxseo`
00506 WHERE `oxtype` = ".$oDb->quote( $sType )."
00507 AND `oxobjectid` = ".$oDb->quote( $sId ) ."
00508 AND `oxshopid` = ".$oDb->quote( $iShopId )."
00509 AND `oxlang` = '{$iLang}'";
00510
00511 $sParams = $sParams ? $sParams : '';
00512 if ( $sParams && $blStrictParamsCheck ) {
00513 $sQ .= " AND `oxparams` = '{$sParams}'";
00514 } else {
00515 $sQ .= " ORDER BY `oxparams` ASC";
00516 }
00517 $sQ .= " LIMIT 1";
00518
00519
00520
00521 $sIdent = md5( $sQ );
00522
00523
00524 if ( ( $sSeoUrl = $this->_loadFromCache( $sIdent, $sType, $iLang, $iShopId, $sParams ) ) === false ) {
00525 $oRs = $oDb->select( $sQ );
00526
00527 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00528
00529 if ( $oRs->fields['oxexpired'] && ( $oRs->fields['oxtype'] == 'static' || $oRs->fields['oxtype'] == 'dynamic' ) ) {
00530
00531 $this->_copyToHistory( $sId, $iShopId, $iLang );
00532 $oDb->execute( "update oxseo set oxexpired = 0 where oxobjectid = ".$oDb->quote( $sId )." and oxlang = '{$iLang}'" );
00533 $sSeoUrl = $oRs->fields['oxseourl'];
00534 } elseif ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00535
00536 $sSeoUrl = $oRs->fields['oxseourl'];
00537 }
00538
00539
00540 $this->_saveInCache( $sIdent, $sSeoUrl, $sType, $iLang, $iShopId, $sParams );
00541 }
00542 }
00543 return $sSeoUrl;
00544 }
00545
00552 protected function _getReservedEntryKeys()
00553 {
00554 if ( !isset( self::$_aReservedEntryKeys ) || !is_array( self::$_aReservedEntryKeys ) ) {
00555 $sDir = getShopBasePath();
00556 self::$_aReservedEntryKeys = array_map('preg_quote', self::$_aReservedWords, array('#'));
00557 $oStr = getStr();
00558 foreach ( glob( "$sDir/*" ) as $sFile ) {
00559 if ( $oStr->preg_match( '/^(.+)\.php[0-9]*$/i', basename( $sFile ), $aMatches ) ) {
00560 self::$_aReservedEntryKeys[] = preg_quote( $aMatches[0], '#' );
00561 self::$_aReservedEntryKeys[] = preg_quote( $aMatches[1], '#' );
00562 } elseif ( is_dir( $sFile ) ) {
00563 self::$_aReservedEntryKeys[] = preg_quote( basename( $sFile ), '#' );
00564 }
00565 }
00566 self::$_aReservedEntryKeys = array_unique(self::$_aReservedEntryKeys);
00567 }
00568 return self::$_aReservedEntryKeys;
00569 }
00570
00579 protected function _prepareUri( $sUri, $iLang = false )
00580 {
00581
00582 $sUri = $this->encodeString( $sUri, true, $iLang );
00583
00584
00585 $sUri = strip_tags( $sUri );
00586 $oStr = getStr();
00587
00588
00589
00590 $oStr = getStr();
00591 $sExt = $this->_getUrlExtension();
00592 if ($sExt === null) {
00593 $aMatched = array();
00594 if ( $oStr->preg_match( '/(\.html?|\/)$/i', $sUri, $aMatched ) ) {
00595 $sExt = $aMatched[0];
00596 } else {
00597 $sExt = '/';
00598 }
00599 }
00600 if ( $sExt && $oStr->substr( $sUri, 0 - $oStr->strlen( $sExt ) ) == $sExt ) {
00601 $sUri = $oStr->substr( $sUri, 0, $oStr->strlen( $sUri ) - $oStr->strlen( $sExt ) );
00602 }
00603
00604
00605 $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').preg_quote( self::$_sPrefix, '/').'\/]+/';
00606 $sUri = $oStr->preg_replace( array( "/\W*\/\W*/", $sRegExp ), array( "/", self::$_sSeparator ), $sUri );
00607
00608
00609 if ( !$sUri && self::$_sPrefix ) {
00610 $sUri = $this->_prepareUri( self::$_sPrefix, $iLang );
00611 }
00612
00613 $sAdd = '';
00614 if ('/' != self::$_sSeparator) {
00615 $sAdd = self::$_sSeparator . self::$_sPrefix;
00616 $sUri = trim($sUri, self::$_sSeparator);
00617 } else {
00618 $sAdd = '_' . self::$_sPrefix;
00619 }
00620
00621
00622 $sUri .= $sExt;
00623
00624
00625 $sUri = $oStr->preg_replace( "#^(/*)(".implode('|', $this->_getReservedEntryKeys()).")(/|$)#i", "\$1\$2$sAdd\$3", $sUri );
00626
00627
00628 return $oStr->preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00629 array( '/', self::$_sSeparator ), $sUri );
00630 }
00631
00632
00642 protected function _prepareTitle( $sTitle, $blSkipTruncate = false, $iLang = false )
00643 {
00644 $sTitle = $this->encodeString( $sTitle, true, $iLang );
00645 $sSep = self::$_sSeparator;
00646 if (!$sSep || ('/' == $sSep)) {
00647 $sSep = '_';
00648 }
00649
00650 $sRegExp = '/[^A-Za-z0-9\/'.preg_quote( self::$_sPrefix, '/').preg_quote($sSep, '/').']+/';
00651 $sTitle = preg_replace( array("#/+#", $sRegExp, "# +#", "#(".preg_quote($sSep, '/').")+#"), $sSep, $sTitle );
00652
00653 $oStr = getStr();
00654
00655 if ( !$blSkipTruncate && $oStr->strlen( $sTitle ) > $this->_iIdLength ) {
00656 $iFirstSpace = $oStr->strpos( $sTitle, $sSep, $this->_iIdLength);
00657 if ( $iFirstSpace !== false ) {
00658 $sTitle = $oStr->substr( $sTitle, 0, $iFirstSpace );
00659 }
00660 }
00661
00662 $sTitle = trim( $sTitle, $sSep );
00663
00664 if (!$sTitle) {
00665 return self::$_sPrefix;
00666 }
00667
00668 return $sTitle;
00669 }
00670
00671
00688 protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = null, $sParams = null )
00689 {
00690 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00691 if ( $iShopId === null ) {
00692 $iShopId = $this->getConfig()->getShopId();
00693 }
00694
00695 $iLang = (int) $iLang;
00696
00697 $sStdUrl = $this->_trimUrl( $sStdUrl );
00698 $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00699 $sIdent = $this->_getSeoIdent( $sSeoUrl );
00700
00701
00702 $sQtedObjectId = $oDb->quote( $sObjectId );
00703 $iQtedShopId = $oDb->quote( $iShopId );
00704 $sQtedType = $oDb->quote( $sType );
00705 $sQtedSeoUrl = $oDb->quote( $sSeoUrl );
00706 $sQtedStdUrl = $oDb->quote( $sStdUrl );
00707 $sQtedParams = $oDb->quote( $sParams );
00708 $sQtedIdent = $oDb->quote( $sIdent );
00709
00710
00711 $sQ = "select oxfixed, oxexpired, ( oxstdurl like {$sQtedStdUrl} ) as samestdurl,
00712 oxseourl like {$sQtedSeoUrl} as sameseourl from oxseo where oxtype = {$sQtedType} and
00713 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00714
00715 $sQ .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00716 $sQ .= "limit 1";
00717 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00718 $oRs = $oDb->select( $sQ );
00719 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00720 if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00721
00722 $sFixed = isset( $blFixed ) ? ", oxfixed = " . ( (int) $blFixed ) . " " : '';
00723
00724 $sSql = "update oxseo set oxexpired = 0 {$sFixed} where oxtype = {$sQtedType} and
00725 oxobjectid = {$sQtedObjectId} and oxshopid = {$iQtedShopId} and oxlang = {$iLang} ";
00726 $sSql .= $sParams ? " and oxparams = {$sQtedParams} " : '';
00727 $sSql .= " limit 1";
00728
00729 return $oDb->execute( $sSql );
00730 } elseif ( $oRs->fields['oxexpired'] ) {
00731
00732 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00733 }
00734 }
00735
00736
00737 $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00738 $blFixed = (int) $blFixed;
00739
00740 $sQ = "insert into oxseo
00741 (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxparams)
00742 values
00743 ( {$sQtedObjectId}, {$sQtedIdent}, {$iQtedShopId}, {$iLang}, {$sQtedStdUrl}, {$sQtedSeoUrl}, {$sQtedType}, '$blFixed', '0', {$sParams} )
00744 on duplicate key update
00745 oxident = {$sQtedIdent}, oxstdurl = {$sQtedStdUrl}, oxseourl = {$sQtedSeoUrl}, oxfixed = '$blFixed', oxexpired = '0'";
00746
00747 return $oDb->execute( $sQ );
00748 }
00749
00760 protected function _trimUrl( $sUrl, $iLang = null )
00761 {
00762 $myConfig = $this->getConfig();
00763 $oStr = getStr();
00764 $sUrl = str_replace( array( $myConfig->getShopUrl( $iLang, false ), $myConfig->getSslShopUrl( $iLang ) ), '', $sUrl );
00765 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00766 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)shp=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00767 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)lang=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00768 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)cur=[0-9]+&?(amp;)?/i', '\1', $sUrl );
00769 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)stoken=[a-z0-9]+&?(amp;)?/i', '\1', $sUrl );
00770 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)&(amp;)?/i', '\1', $sUrl );
00771 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)+$/i', '', $sUrl );
00772 $sUrl = trim( $sUrl );
00773
00774
00775 $iLength = $this->_getMaxUrlLength();
00776 if ( $oStr->strlen( $sUrl ) > $iLength ) {
00777 $sUrl = $oStr->substr( $sUrl, 0, $iLength );
00778 }
00779
00780 return $sUrl;
00781 }
00782
00788 protected function _getMaxUrlLength()
00789 {
00790 if ( $this->_iMaxUrlLength === null ) {
00791
00792 $this->_iMaxUrlLength = $this->getConfig()->getConfigParam( "iMaxSeoUrlLength" ) ;
00793 if ( !$this->_iMaxUrlLength ) {
00794 $this->_iMaxUrlLength = 2048;
00795 }
00796 }
00797 return $this->_iMaxUrlLength;
00798 }
00799
00809 public function encodeString( $sString, $blReplaceChars = true, $iLang = false )
00810 {
00811
00812 $sString = getStr()->html_entity_decode( $sString );
00813
00814 if ( $blReplaceChars ) {
00815 if ($iLang === false || !is_numeric($iLang)) {
00816 $iLang = oxRegistry::getLang()->getEditLanguage();
00817 }
00818
00819 if ( $aReplaceChars = oxRegistry::getLang()->getSeoReplaceChars($iLang) ) {
00820 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00821 }
00822 }
00823
00824
00825
00826 $aReplaceWhat = array( '&', '"', ''', '<', '>' );
00827 return str_replace( $aReplaceWhat, '', $sString );
00828 }
00829
00837 public function setSeparator( $sSeparator = null )
00838 {
00839 self::$_sSeparator = $sSeparator;
00840 if ( !self::$_sSeparator ) {
00841 self::$_sSeparator = '-';
00842 }
00843 }
00844
00852 public function setPrefix( $sPrefix )
00853 {
00854 if ($sPrefix) {
00855 self::$_sPrefix = $sPrefix;
00856 } else {
00857 self::$_sPrefix = 'oxid';
00858 }
00859 }
00860
00868 public function setIdLength( $iIdlength = null )
00869 {
00870 if ( isset( $iIdlength ) ) {
00871 $this->_iIdLength = $iIdlength;
00872 }
00873 }
00874
00883 public function setReservedWords( $aReservedWords )
00884 {
00885 self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00886 }
00887
00888
00900 public function markAsExpired( $sId, $iShopId = null, $iExpStat = 1, $iLang = null, $sParams = null )
00901 {
00902 $oDb = oxDb::getDb();
00903 $sWhere = $sId ? "where oxobjectid = " . $oDb->quote( $sId ) : '';
00904 $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = ". $oDb->quote( $iShopId ) : "where oxshopid = ". $oDb->quote( $iShopId ) ) : '';
00905 $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00906 $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00907
00908 $sQ = "update oxseo set oxexpired = " . $oDb->quote( $iExpStat ) . " $sWhere ";
00909 $oDb->execute( $sQ );
00910 }
00911
00925 protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00926 {
00927 if ( !isset( $iLang ) ) {
00928 $iLang = $oObject->getLanguage();
00929 }
00930 $iShopId = $this->getConfig()->getShopId();
00931
00932
00933 $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams );
00934 if ( !$sOldSeoUrl ) {
00935
00936 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $oObject->getId(), $iLang );
00937 $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, $sParams );
00938 } else {
00939
00940 $sSeoUrl = $sOldSeoUrl;
00941 }
00942 return $sSeoUrl;
00943 }
00944
00953 protected function _getStaticObjectId( $iShopId, $sStdUrl )
00954 {
00955 return md5( strtolower ( $iShopId . $this->_trimUrl( $sStdUrl ) ) );
00956 }
00957
00967 public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00968 {
00969 $oDb = oxDb::getDb();
00970 $sValues = '';
00971 $sOldObjectId = null;
00972
00973
00974 $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ) );
00975 $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00976
00977 if ( !$sObjectId || $sObjectId == '-1' ) {
00978 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00979 } else {
00980
00981 $sOldObjectId = $sObjectId;
00982
00983
00984 if ( $this->_getStaticObjectId( $iShopId, $sStdUrl ) != $sObjectId ) {
00985 $sObjectId = $this->_getStaticObjectId( $iShopId, $sStdUrl );
00986 }
00987 }
00988
00989 foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00990
00991 $iLang = (int) $iLang;
00992
00993
00994 $sSeoUrl = $this->_trimUrl( $sSeoUrl );
00995 if ( $sSeoUrl ) {
00996 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl, $sObjectId, $iLang );
00997 }
00998
00999
01000 if ( $sOldObjectId ) {
01001
01002 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}' ", false, false) ) {
01003 $this->_copyToHistory( $sOldObjectId, $iShopId, $iLang, 'static', $sObjectId );
01004 }
01005 }
01006
01007 if ( !$sSeoUrl || !$sStdUrl ) {
01008 continue;
01009 }
01010
01011 $sIdent = $this->_getSeoIdent( $sSeoUrl );
01012
01013 if ( $sValues ) {
01014 $sValues .= ', ';
01015 }
01016
01017 $sValues .= "( " . $oDb->quote( $sObjectId ) . ", " . $oDb->quote( $sIdent ) . ", " . $oDb->quote( $iShopId ).", '{$iLang}', " . $oDb->quote( $sStdUrl ) . ", " . $oDb->quote( $sSeoUrl ) . ", 'static' )";
01018 }
01019
01020
01021 if ( $sOldObjectId ) {
01022 $oDb->execute( "delete from oxseo where oxobjectid in ( " . $oDb->quote( $sOldObjectId ) . ", " . $oDb->quote( $sObjectId ) . " )" );
01023 }
01024
01025
01026 if ( $sValues ) {
01027
01028 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
01029 $oDb->execute( $sQ );
01030 }
01031
01032 return $sObjectId;
01033 }
01034
01042 public function copyStaticUrls( $iShopId )
01043 {
01044 $iBaseShopId = $this->getConfig()->getBaseShopId();
01045 if ( $iShopId != $iBaseShopId ) {
01046 $oDb = oxDb::getDb();
01047 foreach (array_keys(oxRegistry::getLang()->getLanguageIds()) as $iLang) {
01048 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
01049 select MD5( LOWER( CONCAT( " . $oDb->quote( $iShopId ) . ", oxstdurl ) ) ), MD5( LOWER( oxseourl ) ),
01050 " . $oDb->quote( $iShopId ) . ", oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
01051 $oDb->execute( $sQ );
01052 }
01053 }
01054 }
01055
01065 public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
01066 {
01067 if (!isset($iShopId)) {
01068 $iShopId = $this->getConfig()->getShopId();
01069 }
01070 if (!isset($iLang)) {
01071 $iLang = oxRegistry::getLang()->getEditLanguage();
01072 }
01073
01074 if ( isset($this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId])) {
01075 return $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId];
01076 }
01077
01078 $sFullUrl = '';
01079 if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
01080 $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang, strpos( $sStdUrl, "https:" ) === 0 );
01081 }
01082
01083
01084 $this->_aStaticUrlCache[$sStdUrl][$iLang][$iShopId] = $sFullUrl;
01085
01086 return $sFullUrl;
01087 }
01088
01107 public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '', $blExclude = false, $sAltObjectId = null )
01108 {
01109 $sSeoUrl = $this->_processSeoUrl( $this->_trimUrl( $sSeoUrl ? $sSeoUrl : $this->_getAltUri( $sAltObjectId ? $sAltObjectId : $sObjectId, $iLang ) ), $sObjectId, $iLang, $blExclude );
01110 if ( $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sParams ) ) {
01111
01112 $oDb = oxDb::getDb();
01113
01114
01115 $sQtedObjectId = $oDb->quote( $sAltObjectId ? $sAltObjectId : $sObjectId );
01116 $iQtedShopId = $oDb->quote( $iShopId );
01117
01118 $oStr = getStr();
01119 if ( $sKeywords !== false ) {
01120 $sKeywords = $oDb->quote( $oStr->htmlspecialchars( $this->encodeString( strip_tags( $sKeywords ), false, $iLang ) ) );
01121 }
01122
01123 if ( $sDescription !== false ) {
01124 $sDescription = $oDb->quote( $oStr->htmlspecialchars( strip_tags( $sDescription ) ) );
01125 }
01126
01127 $sQ = "insert into oxobject2seodata
01128 ( oxobjectid, oxshopid, oxlang, oxkeywords, oxdescription )
01129 values
01130 ( {$sQtedObjectId}, {$iQtedShopId}, {$iLang}, ".( $sKeywords ? $sKeywords : "''" ).", ".( $sDescription ? $sDescription : "''" )." )
01131 on duplicate key update
01132 oxkeywords = ".( $sKeywords ? $sKeywords : "oxkeywords" ).", oxdescription = ".( $sDescription ? $sDescription : "oxdescription" );
01133 $oDb->execute( $sQ );
01134 }
01135 }
01136
01145 protected function _getAltUri( $sObjectId, $iLang )
01146 {
01147 }
01148
01159 public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
01160 {
01161 $oDb = oxDb::getDb();
01162 $sQ = "delete from oxseo where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxlang = " . $oDb->quote( $iLang ) . " and oxtype = " . $oDb->quote( $sType ) . " ";
01163 $oDb->execute( $sQ );
01164 }
01165
01176 public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
01177 {
01178 $oDb = oxDb::getDb();
01179
01180 $iShopId = ( !isset( $iShopId ) ) ? $this->getConfig()->getShopId():$iShopId;
01181 $iLang = ( !isset( $iLang ) ) ? oxRegistry::getLang()->getObjectTplLanguage():((int) $iLang);
01182 return $oDb->getOne( "select {$sMetaType} from oxobject2seodata where oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxshopid = " . $oDb->quote( $iShopId )." and oxlang = '{$iLang}'" );
01183 }
01184
01198 public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
01199 {
01200 startProfile("getDynamicUrl");
01201 $sDynUrl = $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), strpos( $sStdUrl, "https:" ) === 0 );
01202 stopProfile("getDynamicUrl");
01203 return $sDynUrl;
01204 }
01205
01214 public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
01215 {
01216 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01217 $iLanguage = isset( $iLanguage ) ? ( (int) $iLanguage ) : oxRegistry::getLang()->getBaseLanguage();
01218 $sSeoUrl = false;
01219
01220 $sShopId = $this->getConfig()->getShopId();
01221
01222 $sQ = "SELECT `oxseourl`, `oxlang` FROM `oxseo` WHERE `oxstdurl` = " . $oDb->quote( $sStdUrl ) . " AND `oxlang` = '$iLanguage' AND `oxshopid` = '$sShopId' LIMIT 1";
01223
01224 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01225 $oRs = $oDb->select( $sQ );
01226
01227 if ( !$oRs->EOF ) {
01228 $sSeoUrl = $oRs->fields['oxseourl'];
01229 }
01230
01231 return $sSeoUrl;
01232 }
01233 }