oxseodecoder.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxSeoDecoder extends oxSuperCfg
00009 {
00018     public function parseStdUrl($sUrl)
00019     {
00020         $aRet = array();
00021         $sUrl = html_entity_decode($sUrl);
00022         if (($iPos = strpos($sUrl, '?')) !== false) {
00023             $aParams = explode('&', substr($sUrl, $iPos+1));
00024             foreach ($aParams as $sParam) {
00025                 $aP = explode('=', $sParam);
00026                 if (count($aP) == 2) {
00027                     if (($sName = trim($aP[0])) && ($sValue = trim($aP[1]))) {
00028                         $aRet[$sName] = rawurldecode($sValue);
00029                     }
00030                 }
00031             }
00032         }
00033         return $aRet;
00034     }
00035 
00044     protected function _getIdent( $sSeoUrl, $blIgnore = false )
00045     {
00046         $myConfig = $this->getConfig();
00047         $iDefSeoLang = (int) $myConfig->getConfigParam( 'iDefSeoLang' );
00048 
00049         $blLangIsSet   = false;
00050         $iNotSetLangId = $iDefSeoLang;
00051 
00052         $aLangCodes = array_keys( $myConfig->getConfigParam( 'aLanguages' ) );
00053         foreach ( $aLangCodes as $iLangId => $sLangCode ) {
00054             if ( stripos( $sSeoUrl, "$sLangCode/" ) === 0 ) {
00055                 $blLangIsSet   = true;
00056                 $iNotSetLangId = $iLangId;
00057                 break;
00058             }
00059         }
00060 
00061         // this can only be applied for default language
00062         if ( !$blIgnore && $iNotSetLangId == $iDefSeoLang ) {
00063             if ( !$blLangIsSet ) {
00064                 // if language not set - appending default to calculate md5
00065                 $sSeoUrl = $aLangCodes[ $iDefSeoLang ] . '/' . $sSeoUrl;
00066             } else {
00067                 // if language is set - this should block from direct access
00068                 // but must be ignored when checking in history table
00069                 $sSeoUrl = '!' . $sSeoUrl;
00070             }
00071         }
00072 
00073         return md5( strtolower( $sSeoUrl ) );
00074     }
00075 
00083     protected function _removeLangPrefix( $sSeoUrl )
00084     {
00085         $aLangCodes = array_keys( $this->getConfig()->getConfigParam( 'aLanguages' ) );
00086         foreach ( $aLangCodes as $iLangId => $sLangCode ) {
00087             if ( stripos( $sSeoUrl, "$sLangCode/" ) === 0 ) {
00088                 $sSeoUrl = substr( $sSeoUrl, strlen( "$sLangCode/" ) );
00089                 break;
00090             }
00091         }
00092 
00093         return $sSeoUrl;
00094     }
00095 
00105     public function decodeUrl( $sSeoUrl )
00106     {
00107         $sBaseUrl = $this->getConfig()->getShopURL();
00108         if ( strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00109             $sSeoUrl = substr( $sSeoUrl, strlen( $sBaseUrl ) );
00110         }
00111         $sSeoUrl = rawurldecode( $sSeoUrl );
00112         $iShopId = $this->getConfig()->getShopId();
00113 
00114         $sKey = $this->_getIdent( $sSeoUrl );
00115         $aRet = false;
00116 
00117         $oRs = oxDb::getDb(true)->Execute( "select oxstdurl, oxlang from oxseo where oxident='$sKey' and oxshopid='$iShopId' limit 1");
00118         if ( !$oRs->EOF ) {
00119             // primary seo language changed ?
00120             $aRet = $this->parseStdUrl( $oRs->fields['oxstdurl'] );
00121             $aRet['lang'] = $oRs->fields['oxlang'];;
00122         }
00123         return $aRet;
00124     }
00125 
00135     protected function _decodeOldUrl( $sSeoUrl )
00136     {
00137         $oDb = oxDb::getDb(true);
00138         $sBaseUrl = $this->getConfig()->getShopURL();
00139         if ( strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00140             $sSeoUrl = substr( $sSeoUrl, strlen( $sBaseUrl ) );
00141         }
00142         $iShopId = $this->getConfig()->getShopId();
00143         $sSeoUrl = rawurldecode($sSeoUrl);
00144 
00145         $sKey = $this->_getIdent( $this->_removeLangPrefix( $sSeoUrl ), true );
00146 
00147         $sUrl = false;
00148         $oRs = $oDb->execute( "select oxobjectid, oxlang from oxseohistory where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1");
00149         if ( !$oRs->EOF ) {
00150             // updating hit info (oxtimestamp field will be updated automatically)
00151             $oDb->execute( "update oxseohistory set oxhits = oxhits + 1 where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1" );
00152 
00153             // fetching new url
00154             if ( $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid = '{$oRs->fields['oxobjectid']}' and oxlang = '{$oRs->fields['oxlang']}' and oxshopid = '{$iShopId}' " ) ) {
00155                 $sUrl = oxSeoEncoder::getInstance()->getLanguageParam( $oRs->fields['oxlang'] ) . $sUrl;
00156             }
00157         }
00158 
00159         return $sUrl;
00160     }
00161 
00171     public function processSeoCall( $sRequest = null, $sPath = null )
00172     {
00173         // first - collect needed parameters
00174         if ( !$sRequest ) {
00175             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00176                 $sRequest = $_SERVER['REQUEST_URI'];
00177             } else {    // try something else
00178                 $sRequest = $_SERVER['SCRIPT_URI'];
00179             }
00180         }
00181 
00182         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00183         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00184 
00185             // in case SEO url is actual
00186             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00187                 $_GET = array_merge( $aGet, $_GET );
00188                 oxLang::getInstance()->resetBaseLanguage();
00189             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00190                 // in case SEO url was changed - redirecting to new location
00191                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00192             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00193                 // old type II seo urls
00194                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00195             } else { // unrecognized url
00196                 error_404_handler( $sParams );
00197             }
00198         }
00199     }
00200 
00209     protected function _decodeSimpleUrl( $sParams )
00210     {
00211         $sLastParam = rtrim( $sParams, '/' );
00212         $sLastParam = substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( strlen( $sLastParam ) ) );
00213         $sLastParam = trim( $sParams, '/' );
00214 
00215         // active object id
00216         $sUrl = null;
00217 
00218         if ( $sLastParam ) {
00219 
00220             $sLastParam = oxDb::getDb()->quote( $sLastParam );
00221             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00222 
00223             // article ?
00224             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00225                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00226             } else {
00227 
00228                 // category ?
00229                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00230                     // then maybe vendor ?
00231                     $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00232                 }
00233             }
00234         }
00235 
00236         return $sUrl;
00237     }
00238 
00249     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00250     {
00251         $oDb     = oxDb::getDb();
00252         $sTable  = getViewName( $sTable );
00253         $sField  = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00254         $sSeoUrl = null;
00255 
00256         try {
00257             if ( $sObjectId = $oDb->getOne( "select oxid from $sTable where $sField = $sSeoId" ) ) {
00258                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = '$sType' and oxobjectid = '$sObjectId' and oxlang = '$iLanguage' " );
00259             }
00260         } catch ( Exception $oEx ) {
00261             // in case field does not exist must catch db exception
00262         }
00263 
00264         return $sSeoUrl;
00265     }
00266 
00275     protected function _getParams( $sRequest, $sPath )
00276     {
00277         $sParams = preg_replace( '/\?.*/', '', $sRequest );
00278         $sPath   = preg_quote($sPath, '/');
00279         $sParams = preg_replace( "/^$sPath/", '', $sParams );
00280 
00281         // this should not happen on most cases, because this redirect is handled by .htaccess
00282         if ( $sParams && !ereg( '\.html$', $sParams ) && !ereg( '\/$', $sParams ) ) {
00283             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00284         }
00285 
00286         return $sParams;
00287     }
00288 
00297     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
00298     {
00299         $oDb = oxDb::getDb( true );
00300         $sStdUrl = $oDb->quote( $sStdUrl );
00301         $iLanguage = isset( $iLanguage ) ? $iLanguage : oxLang::getInstance()->getBaseLanguage();
00302 
00303         $sSeoUrl = false;
00304 
00305         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = $sStdUrl and oxlang = '$iLanguage' limit 1";
00306         $oRs = $oDb->execute( $sQ );
00307         if ( !$oRs->EOF ) {
00308             $sSeoUrl = oxSeoEncoder::getInstance()->getLanguageParam( $oRs->fields['oxlang'] ).$oRs->fields['oxseourl'];
00309         }
00310 
00311         return $sSeoUrl;
00312     }
00313 }

Generated on Wed Jan 7 14:17:39 2009 for OXID eShop CE by  doxygen 1.5.5