00001 <?php
00002
00008 class oxSeoEncoder extends oxSuperCfg
00009 {
00016 protected static $_aReservedWords = array( 'admin' );
00017
00023 protected static $_sSeparator = null;
00024
00030 protected $_iIdLength = 255;
00031
00037 protected static $_sPrefix = null;
00038
00044 protected $_sAddParams = null;
00045
00049 protected static $_instance = null;
00050
00056 public static function getInstance()
00057 {
00058 if (!self::$_instance) {
00059 self::$_instance = oxNew("oxSeoEncoder");
00060 }
00061 return self::$_instance;
00062 }
00063
00067 public function __construct()
00068 {
00069 $myConfig = $this->getConfig();
00070 if (!self::$_sSeparator) {
00071 $this->setSeparator( $myConfig->getConfigParam( 'sSEOSeparator' ) );
00072 }
00073 if (!self::$_sPrefix) {
00074 $this->setPrefix( $myConfig->getConfigParam( 'sSEOuprefix' ) );
00075 }
00076 $this->setReservedWords( $myConfig->getConfigParam( 'aSEOReservedWords' ) );
00077 }
00078
00090 protected function _copyToHistory( $sId, $iShopId, $iLang, $sType = null, $sNewId = null )
00091 {
00092 $sObjectid = $sNewId?"'$sNewId'":'oxobjectid';
00093 $sType = $sType?"oxtype = {$sType} and":'';
00094
00095
00096 $sSub = "select {$sObjectid}, oxident, oxshopid, oxlang, now() from oxseo where {$sType} oxobjectid = {$sId} and oxshopid = {$iShopId} and oxlang = {$iLang} limit 1";
00097 $sQ = "replace oxseohistory ( oxobjectid, oxident, oxshopid, oxlang, oxinsert ) {$sSub}";
00098 oxDb::getDb()->execute( $sQ );
00099 }
00100
00107 protected function _getAddParams()
00108 {
00109
00110 if ( $this->_sAddParams ) {
00111 return $this->_sAddParams;
00112 }
00113
00114 $myConfig = $this->getConfig();
00115 $iCur = oxConfig::getParameter('currency');
00116 $sActShop = $myConfig->getShopId();
00117
00118 return $this->_getAddParamsFnc( $iCur, $sActShop );
00119 }
00120
00130 protected function _getAddParamsFnc( $iCur, $iActShop )
00131 {
00132
00133 $this->_sAddParams = '';
00134 $sSep = '?';
00135 if ( $iCur ) {
00136 $this->_sAddParams .= $sSep . 'cur=' . $iCur;
00137 $sSep = '&';
00138 }
00139
00140
00141 return $this->_sAddParams;
00142 }
00143
00153 protected function _getDynamicUri( $sStdUrl, $sSeoUrl, $iLang )
00154 {
00155 $iShopId = $this->getConfig()->getShopId();
00156
00157 $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00158 $sObjectId = md5( strtolower( $iShopId . $sStdUrl ) );
00159 $sSeoUrl = $this->_prepareTitle( $sSeoUrl );
00160
00161
00162 $sOldSeoUrl = $this->_loadFromDb( 'dynamic', $sObjectId, $iLang );
00163 if ( $sOldSeoUrl === $sSeoUrl ) {
00164 $sSeoUrl = $sOldSeoUrl;
00165 } else {
00166
00167 if ( $sOldSeoUrl ) {
00168 $oDb = oxDb::getDb();
00169 $this->_copyToHistory( $oDb->quote( $sObjectId ), $oDb->quote( $iShopId ), $iLang, $oDb->quote( 'dynamic' ) );
00170 }
00171
00172
00173 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl, null, $sObjectId );
00174
00175
00176 $this->_saveToDb( 'dynamic', $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId );
00177 }
00178
00179 return $sSeoUrl;
00180 }
00181
00189 public function getLanguageParam( $iObjectLang )
00190 {
00191 $iDefLang = (int) $this->getConfig()->getConfigParam( 'iDefSeoLang' );
00192 $aLangIds = oxLang::getInstance()->getLanguageIds();
00193 $sLang = '';
00194
00195 if ( $iObjectLang != $iDefLang && isset( $aLangIds[$iObjectLang] ) ) {
00196
00197 $sLang = $aLangIds[$iObjectLang] . '/';
00198 }
00199 return $sLang;
00200 }
00201
00210 protected function _getFullUrl( $sSeoUrl, $iLang )
00211 {
00212 return $this->getConfig()->getShopURL() . $this->getLanguageParam( (int) $iLang ) . $sSeoUrl . $this->_getAddParams();
00213 }
00214
00225 protected function _getSeoIdent( $sSeoUrl, $iLang )
00226 {
00227 return md5( strtolower( $this->getLanguageParam( $iLang ) . $sSeoUrl ) );
00228 }
00229
00239 protected function _getStaticUri( $sStdUrl, $iShopId, $iLang )
00240 {
00241 $sIdent = md5( strtolower( $iShopId . $this->_trimUrl( $sStdUrl, $iLang ) ) );
00242 return $this->_loadFromDb( 'static', $sIdent, $iLang );
00243 }
00244
00257 protected function _getUniqueSeoUrl( $sSeoUrl, $sConstEnd = null, $sObjectId = null )
00258 {
00259 if ($sConstEnd === null) {
00260 $aMatched = array();
00261 if ( preg_match('/\.html?$/i', $sSeoUrl, $aMatched ) ) {
00262 $sConstEnd = $aMatched[0];
00263 } else {
00264 if ($sSeoUrl{strlen($sSeoUrl)-1} != '/') {
00265 $sSeoUrl .= '/';
00266 }
00267 $sConstEnd = '/';
00268 }
00269 }
00270
00271 $sBaseSeoUrl = $sSeoUrl;
00272 if ( $sConstEnd && substr( $sSeoUrl, 0 - strlen( $sConstEnd ) ) == $sConstEnd ) {
00273 $sBaseSeoUrl = substr( $sSeoUrl, 0, strlen( $sSeoUrl ) - strlen( $sConstEnd ) );
00274 }
00275
00276 $oDb = oxDb::getDb();
00277 $iShopId = $this->getConfig()->getShopId();
00278 $iCnt = 0;
00279 $iLang = oxLang::getInstance()->getBaseLanguage();
00280 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl, $iLang );
00281 $sQ = "select 1 from oxseo where oxshopid = '{$iShopId}'";
00282
00283
00284 if ( $sObjectId ) {
00285 $sQ .= " and oxobjectid != '{$sObjectId}'";
00286 }
00287
00288 while ( $oDb->getOne( $sQ ." and oxident='".$this->_getSeoIdent( $sCheckSeoUrl, $iLang )."' " ) ) {
00289
00290 $sAdd = self::$_sSeparator . self::$_sPrefix;
00291 if ( $iCnt ) {
00292 $sAdd .= self::$_sSeparator . $iCnt;
00293 }
00294 ++$iCnt;
00295
00296 $sSeoUrl = $sBaseSeoUrl . $sAdd . $sConstEnd;
00297 $sCheckSeoUrl = $this->_trimUrl( $sSeoUrl, $iLang );
00298 }
00299 return $sSeoUrl;
00300 }
00301
00317 protected function _loadFromDb( $sType, $sId, $iLang, $iShopId = null, $sParams = null, $blStrictParamsCheck = true)
00318 {
00319 $oDb = oxDb::getDb( true );
00320 if ( $iShopId === null ) {
00321 $iShopId = $this->getConfig()->getShopId();
00322 }
00323
00324 $iShopId = $oDb->quote( $iShopId );
00325 $sId = $oDb->quote( $sId );
00326 $sType = $oDb->quote( $sType );
00327 $iLang = (int) $iLang;
00328
00329 $sQ = "select oxfixed, oxseourl, oxexpired from oxseo where oxtype = {$sType}
00330 and oxobjectid = {$sId} and oxshopid = {$iShopId} and oxlang = {$iLang}";
00331
00332 if ($sParams) {
00333 if ($blStrictParamsCheck) {
00334 $sQ .= " and oxparams = '{$sParams}'";
00335 } else {
00336 $sQ .= " order by oxparams = '{$sParams}' desc";
00337 }
00338 }
00339 $sQ .= " limit 1";
00340
00341 $sSeoUrl = false;
00342 $oRs = $oDb->execute( $sQ );
00343 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00344
00345 if ( !$oRs->fields['oxexpired'] || $oRs->fields['oxfixed'] ) {
00346 $sSeoUrl = $oRs->fields['oxseourl'];
00347 }
00348 }
00349 return $sSeoUrl;
00350 }
00351
00360 protected function _prepareTitle( $sTitle, $blSkipTruncate = false )
00361 {
00362
00363 $sTitle = $this->encodeString( $sTitle );
00364
00365
00366 $sTitle = strip_tags( $sTitle );
00367 $sSeparator = self::$_sSeparator;
00368 $sPrefix = self::$_sPrefix;
00369
00370 foreach ( self::$_aReservedWords as $sWord ) {
00371
00372 $sTitle = preg_replace( array( "/(\s$sWord)$/i", "/^($sWord\s)/i", "/(\s$sWord\s)/i", "/^($sWord)$/i",
00373 "/(\/$sWord)$/i", "/^($sWord\/)/i", "/(\/$sWord\/)/i"),
00374 " $1{$sSeparator}{$sPrefix}{$sSeparator} ", $sTitle );
00375 }
00376
00377
00378 $sExt = '';
00379 $aMatched = array();
00380 if ( preg_match( '/\.html?$/i', $sTitle, $aMatched ) ) {
00381 $sExt = substr( $sTitle, 0 - strlen( $aMatched[0] ) );
00382 $sTitle = substr( $sTitle, 0, strlen( $sTitle ) - strlen( $aMatched[0] ) );
00383 }
00384
00385
00386 if ( !$blSkipTruncate && strlen( $sTitle ) > $this->_iIdLength ) {
00387
00388 if ( ( $iFirstSpace = strstr( substr( $sTitle, $this->_iIdLength ), ' ' ) ) ) {
00389 $sTitle = substr( $sTitle, 0, $this->_iIdLength + $iFirstSpace );
00390 }
00391 }
00392
00393
00394 $sRegExp = '/[^A-Za-z0-9'.preg_quote( self::$_sSeparator, '/').'\/]+/';
00395 $sTitle = trim( preg_replace( $sRegExp, self::$_sSeparator, $sTitle ), self::$_sSeparator );
00396
00397
00398 $sTitle .= $sExt;
00399
00400
00401 if ( !$sTitle ) {
00402 $sTitle = $this->_prepareTitle( self::$_sPrefix );
00403 }
00404
00405
00406 return preg_replace( array( '|//+|', '/' . preg_quote( self::$_sSeparator . self::$_sSeparator, '/' ) .'+/' ),
00407 array( '/', self::$_sSeparator ), $sTitle );
00408 }
00409
00410
00429 protected function _saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId = null, $blFixed = 0, $sKeywords = '', $sDescription = '', $sParams = null )
00430 {
00431 $oDb = oxDb::getDb( true );
00432 if ( $iShopId === null ) {
00433 $iShopId = $this->getConfig()->getShopId();
00434 }
00435
00436 $iShopId = $oDb->quote( $iShopId );
00437
00438 $sObjectId = $oDb->quote( $sObjectId );
00439 $sType = $oDb->quote( $sType );
00440
00441 $iLang = (int) $iLang;
00442
00443 $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00444 $sSeoUrl = $this->_trimUrl( $sSeoUrl, $iLang );
00445
00446 $sIdent = $this->_getSeoIdent( $sSeoUrl, $iLang );
00447
00448 $sStdUrl = $oDb->quote( $sStdUrl );
00449 $sSeoUrl = $oDb->quote( $sSeoUrl );
00450
00451 $blFixed = (int) $blFixed;
00452
00453 $sQ = "select oxfixed, oxexpired, oxstdurl like {$sStdUrl} as samestdurl, oxseourl like {$sSeoUrl} as sameseourl from oxseo where oxtype = {$sType} and oxobjectid = {$sObjectId} and oxshopid = {$iShopId} and oxlang = {$iLang} ";
00454 $sQ .= $sParams?" and oxparams = '{$sParams}' " : '';
00455 $sQ .= "limit 1";
00456
00457 $oRs = $oDb->execute( $sQ );
00458 if ( $oRs && $oRs->recordCount() > 0 && !$oRs->EOF ) {
00459
00460 if ( $oRs->fields['samestdurl'] && $oRs->fields['sameseourl'] && $oRs->fields['oxexpired'] ) {
00461
00462 return $oDb->execute( "update oxseo set oxexpired = 0 where oxtype = {$sType} and oxobjectid = {$sObjectId} and oxshopid = {$iShopId} and oxlang = {$iLang} limit 1" );
00463 } elseif ( $oRs->fields['oxexpired'] && !$oRs->fields['oxfixed'] ) {
00464
00465 $this->_copyToHistory( $sObjectId, $iShopId, $iLang, $sType );
00466 }
00467 }
00468
00469 $sKeywords = $sKeywords?$oDb->quote( htmlentities( $this->encodeString( strip_tags( $sKeywords ) ) ) ):"''";
00470 $sDescription = $sDescription?$oDb->quote( htmlentities( strip_tags( $sDescription ) ) ):"''";
00471
00472
00473 $sParams = $sParams ? $oDb->quote( $sParams ) :'""';
00474 $sQ = "replace into oxseo
00475 (oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype, oxfixed, oxexpired, oxkeywords, oxdescription, oxparams)
00476 values
00477 ( {$sObjectId}, '$sIdent', {$iShopId}, {$iLang}, {$sStdUrl}, {$sSeoUrl}, {$sType}, '$blFixed', '0', {$sKeywords}, {$sDescription}, $sParams )";
00478 return $oDb->execute( $sQ );
00479 }
00480
00491 protected function _trimUrl( $sUrl, $iLang )
00492 {
00493 $sUrl = str_replace( $this->getConfig()->getShopURL(), '', $sUrl );
00494
00495 if ( ( $sLangParam = $this->getLanguageParam( $iLang ) ) ) {
00496 $sUrl = preg_replace( "'^".preg_quote( $sLangParam, "'")."'", '', $sUrl );
00497 }
00498
00499 return preg_replace( '/sid=[a-z0-9\.]+&?(amp;)?/i', '', $sUrl );
00500 }
00501
00509 public function encodeString( $sString )
00510 {
00511
00512 $sString = html_entity_decode( $sString );
00513
00514 $aReplaceChars = $this->getConfig()->getConfigParam( 'aSeoReplaceChars' );
00515 $sString = str_replace( array_keys( $aReplaceChars ), array_values( $aReplaceChars ), $sString );
00516
00517
00518 $aReplaceWhat = array( '/&/', '/"/', '/'/', '/</', '/>/' );
00519 return str_replace( $aReplaceWhat, '', $sString );
00520 }
00521
00529 public function setSeparator( $sSeparator = null )
00530 {
00531 self::$_sSeparator = $sSeparator;
00532 if ( !self::$_sSeparator ) {
00533 self::$_sSeparator = '-';
00534 }
00535 }
00536
00544 public function setPrefix( $sPrefix )
00545 {
00546 if ($sPrefix) {
00547 self::$_sPrefix = $sPrefix;
00548 } else {
00549 self::$_sPrefix = 'oxid';
00550 }
00551 }
00552
00560 public function setIdLength( $iIdlength = null )
00561 {
00562 if ( isset( $iIdlength ) ) {
00563 $this->_iIdLength = $iIdlength;
00564 }
00565 }
00566
00574 public function setReservedWords( $aReservedWords )
00575 {
00576 self::$_aReservedWords = array_merge( self::$_aReservedWords, $aReservedWords );
00577 }
00578
00579
00590 public function markAsExpired( $sId, $iShopId = null, $iLang = null, $sParams = null )
00591 {
00592 $sWhere = $sId ? "where oxobjectid = '{$sId}'" : '';
00593 $sWhere .= isset( $iShopId ) ? ( $sWhere ? " and oxshopid = '{$iShopId}'" : "where oxshopid = '{$iShopId}'" ) : '';
00594 $sWhere .= $iLang ? ( $sWhere ? " and oxlang = '{$iLang}'" : "where oxlang = '{$iLang}'" ) : '';
00595 $sWhere .= $sParams ? ( $sWhere ? " and {$sParams}" : "where {$sParams}" ) : '';
00596
00597 $sQ = "update oxseo set oxexpired = '1' $sWhere ";
00598 oxDb::getDb()->execute( $sQ );
00599 }
00600
00614 protected function _getPageUri( $oObject, $sType, $sStdUrl, $sSeoUrl, $sParams, $iLang = null, $blFixed = false )
00615 {
00616 if (!isset($iLang)) {
00617 $iLang = $oObject->getLanguage();
00618 }
00619 $iShopId = $this->getConfig()->getShopId();
00620
00621
00622 if ( ( $sOldSeoUrl = $this->_loadFromDb( $sType, $oObject->getId(), $iLang, $iShopId, $sParams ) ) ) {
00623 if ( $sOldSeoUrl === $sSeoUrl ) {
00624 return $sSeoUrl;
00625 } else {
00626 $oDb = oxDb::getDb();
00627 $this->_copyToHistory( $oDb->quote( $oObject->getId() ), $oDb->quote( $iShopId ), $iLang, $oDb->quote( $sType ) );
00628 }
00629 }
00630
00631 $this->_saveToDb( $sType, $oObject->getId(), $sStdUrl, $sSeoUrl, $iLang, $iShopId, (int) $blFixed, '', '', $sParams );
00632
00633 return $sSeoUrl;
00634 }
00635
00645 public function encodeStaticUrls( $aStaticUrl, $iShopId, $iLang )
00646 {
00647 $oDb = oxDb::getDb();
00648 $sValues = '';
00649 $sOldObjectId = null;
00650
00651
00652 $sStdUrl = $this->_trimUrl( trim( $aStaticUrl['oxseo__oxstdurl'] ), $iLang );
00653 $sObjectId = $aStaticUrl['oxseo__oxobjectid'];
00654
00655 if ( !$sObjectId || $sObjectId == '-1' ) {
00656 $sObjectId = md5( strtolower ( $iShopId.$sStdUrl ) );
00657 } else {
00658
00659 $sOldObjectId = $sObjectId;
00660
00661
00662 if ( md5( strtolower ( $iShopId.$sStdUrl ) ) != $sObjectId ) {
00663 $sObjectId = md5( strtolower ( $iShopId.$sStdUrl ) );
00664 }
00665 }
00666
00667 foreach ( $aStaticUrl['oxseo__oxseourl'] as $iLang => $sSeoUrl ) {
00668
00669
00670 if ( ( $sSeoUrl = trim( $sSeoUrl ) ) ) {
00671 $sSeoUrl = $this->_prepareTitle( $this->_trimUrl( $sSeoUrl, $iLang ) );
00672 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl, null, $sObjectId );
00673 }
00674
00675 if ( $sOldObjectId ) {
00676
00677 if ( !$oDb->getOne( "select ('{$sSeoUrl}' like oxseourl) & ('{$sStdUrl}' like oxstdurl) from oxseo where oxobjectid = '{$sOldObjectId}' and oxshopid = '{$iShopId}' and oxlang = '{$iLang}' " ) ) {
00678 $this->_copyToHistory( $oDb->quote( $sOldObjectId ), $iShopId, $iLang, $oDb->quote( 'static' ), $sObjectId );
00679 }
00680 }
00681
00682 if ( !$sSeoUrl || !$sStdUrl ) {
00683 continue;
00684 }
00685
00686 $sIdent = $this->_getSeoIdent( $sSeoUrl, $iLang );
00687
00688 if ( $sValues ) {
00689 $sValues .= ', ';
00690 }
00691
00692 $sValues .= "( '{$sObjectId}', '{$sIdent}', '{$iShopId}', '{$iLang}', '$sStdUrl', '$sSeoUrl', 'static' )";
00693 }
00694
00695
00696 if ( $sOldObjectId ) {
00697 $oDb->execute( "delete from oxseo where oxobjectid in ( '{$sOldObjectId}', '{$sObjectId}' )" );
00698 }
00699
00700
00701 if ( $sValues ) {
00702
00703 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype ) values {$sValues} ";
00704 $oDb->execute( $sQ );
00705 }
00706
00707 return $sObjectId;
00708 }
00709
00717 public function copyStaticUrls( $iShopId )
00718 {
00719 $iBaseShopId = $this->getConfig()->getBaseShopId();
00720 if ( $iShopId != $iBaseShopId ) {
00721 foreach (array_keys(oxLang::getInstance()->getLanguageIds()) as $iLang) {
00722 $iLang = (int) $iLang;
00723 $sPrfx = $this->getLanguageParam($iLang);
00724 $sQ = "insert into oxseo ( oxobjectid, oxident, oxshopid, oxlang, oxstdurl, oxseourl, oxtype )
00725 select MD5( LOWER( CONCAT( '{$iShopId}', oxstdurl ) ) ), MD5( LOWER( CONCAT( '{$sPrfx}', oxseourl ) ) ),
00726 '$iShopId', oxlang, oxstdurl, oxseourl, oxtype from oxseo where oxshopid = '{$iBaseShopId}' and oxtype = 'static' and oxlang='$iLang' ";
00727 oxDb::getDb()->execute( $sQ );
00728 }
00729 }
00730 }
00731
00741 public function getStaticUrl( $sStdUrl, $iLang = null, $iShopId = null )
00742 {
00743 if (!isset($iShopId)) {
00744 $iShopId = oxConfig::getInstance()->getShopId();
00745 }
00746 if (!isset($iLang)) {
00747 $iLang = oxLang::getInstance()->getEditLanguage();
00748 }
00749
00750 $sFullUrl = '';
00751 if ( ( $sSeoUrl = $this->_getStaticUri( $sStdUrl, $iShopId, $iLang ) ) ) {
00752 $sFullUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
00753 }
00754 return $sFullUrl;
00755 }
00756
00773 public function addSeoEntry( $sObjectId, $iShopId, $iLang, $sStdUrl, $sSeoUrl, $sType, $blFixed = 1, $sKeywords = '', $sDescription = '', $sParams = '' )
00774 {
00775 $sSeoUrl = $this->_getUniqueSeoUrl( $this->_prepareTitle( $this->_trimUrl( $sSeoUrl, $iLang ) ), null, $sObjectId );
00776 $this->_saveToDb( $sType, $sObjectId, $sStdUrl, $sSeoUrl, $iLang, $iShopId, $blFixed, $sKeywords, $sDescription, $sParams );
00777 }
00778
00789 public function deleteSeoEntry( $sObjectId, $iShopId, $iLang, $sType )
00790 {
00791 $sQ = "delete from oxseo where oxobjectid = '{$sObjectId}' and oxshopid = '{$iShopId}' and oxlang = '{$iLang}' and oxtype = '{$sType}' ";
00792 oxDb::getDb()->execute( $sQ );
00793 }
00794
00805 public function getMetaData( $sObjectId, $sMetaType, $iShopId = null, $iLang = null )
00806 {
00807 $iShopId = ( !isset( $iShopId ) ) ? oxConfig::getInstance()->getShopId():$iShopId;
00808 $iLang = ( !isset( $iLang ) ) ? oxLang::getInstance()->getTplLanguage():$iLang;
00809
00810 return oxDb::getDb()->getOne( "select {$sMetaType} from oxseo where oxobjectid = '{$sObjectId}' and oxshopid = '{$iShopId}' and oxlang = '{$iLang}'" );
00811 }
00812
00826 public function getDynamicUrl( $sStdUrl, $sSeoUrl, $iLang )
00827 {
00828 return $this->_getFullUrl( $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang ), $iLang );
00829 }
00830
00859 }