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 
00045     public function decodeUrl( $sSeoUrl )
00046     {
00047         $sBaseUrl = $this->getConfig()->getShopURL();
00048         if ( strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00049             $sSeoUrl = substr( $sSeoUrl, strlen( $sBaseUrl ) );
00050         }
00051         $iShopId = $this->getConfig()->getShopId();
00052         $sSeoUrl = rawurldecode($sSeoUrl);
00053 
00054         $sKey = md5( strtolower( $sSeoUrl ) );
00055 
00056         $rs = oxDb::getDb(true)->Execute( "select oxstdurl, oxlang from oxseo where oxident='$sKey' and oxshopid='$iShopId' limit 1");
00057         if (!$rs->EOF) {
00058             $sStdUrl = $rs->fields['oxstdurl'];
00059             $iLang   = $rs->fields['oxlang'];
00060             $aRet = $this->parseStdUrl($sStdUrl);
00061             $aRet['lang'] = $iLang;
00062             return $aRet;
00063         }
00064         return false;
00065     }
00066 
00076     protected function _decodeOldUrl( $sSeoUrl )
00077     {
00078         $oDb = oxDb::getDb(true);
00079         $sBaseUrl = $this->getConfig()->getShopURL();
00080         if ( strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00081             $sSeoUrl = substr( $sSeoUrl, strlen( $sBaseUrl ) );
00082         }
00083         $iShopId = $this->getConfig()->getShopId();
00084         $sSeoUrl = rawurldecode($sSeoUrl);
00085         $sKey = md5( strtolower( $sSeoUrl ) );
00086 
00087         $sUrl = false;
00088         $rs = $oDb->execute( "select oxobjectid, oxlang from oxseohistory where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1");
00089         if ( !$rs->EOF ) {
00090             // updating hit info (oxtimestamp field will be updated automatically)
00091             $oDb->execute( "update oxseohistory set oxhits = oxhits + 1 where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1" );
00092 
00093             // fetching new url
00094             $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid = '{$rs->fields['oxobjectid']}' and oxlang = '{$rs->fields['oxlang']}' and oxshopid = '{$iShopId}' " );
00095         }
00096         return $sUrl;
00097     }
00098 
00108     public function processSeoCall( $sRequest = null, $sPath = null )
00109     {
00110         // first - collect needed parameters
00111         if ( !$sRequest ) {
00112             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00113                 $sRequest = $_SERVER['REQUEST_URI'];
00114             } else {    // try something else
00115                 $sRequest = $_SERVER['SCRIPT_URI'];
00116             }
00117         }
00118 
00119         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00120         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00121 
00122             // in case SEO url is actual
00123             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00124                 $_GET = array_merge( $aGet, $_GET );
00125                 oxLang::getInstance()->resetBaseLanguage();
00126             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00127                 // in case SEO url was changed - redirecting to new location
00128                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00129             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00130                 // old type II seo urls
00131                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00132             } else { // unrecognized url
00133                 error_404_handler( $sParams );
00134             }
00135         }
00136     }
00137 
00146     protected function _decodeSimpleUrl( $sParams )
00147     {
00148         $sLastParam = rtrim( $sParams, '/' );
00149         $sLastParam = substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( strlen( $sLastParam ) ) );
00150         $sLastParam = trim( $sParams, '/' );
00151 
00152         // active object id
00153         $sUrl = null;
00154 
00155         if ( $sLastParam ) {
00156 
00157             $sLastParam = oxDb::getDb()->quote( $sLastParam );
00158             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00159 
00160             // article ?
00161             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00162                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00163             } else {
00164 
00165                 // category ?
00166                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00167                     // then maybe vendor ?
00168                     $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00169                 }
00170             }
00171         }
00172 
00173         return $sUrl;
00174     }
00175 
00186     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00187     {
00188         $oDb     = oxDb::getDb();
00189         $sTable  = getViewName( $sTable );
00190         $sField  = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00191         $sSeoUrl = null;
00192 
00193         try {
00194             if ( $sObjectId = $oDb->getOne( "select oxid from $sTable where $sField = $sSeoId" ) ) {
00195                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = '$sType' and oxobjectid = '$sObjectId' and oxlang = '$iLanguage' " );
00196             }
00197         } catch ( Exception $oEx ) {
00198             // in case field does not exist must catch db exception
00199         }
00200 
00201         return $sSeoUrl;
00202     }
00203 
00212     protected function _getParams( $sRequest, $sPath )
00213     {
00214         $sParams = preg_replace( '/\?.*/', '', $sRequest );
00215         $sPath   = preg_quote($sPath, '/');
00216         $sParams = preg_replace( "/^$sPath/", '', $sParams );
00217 
00218         // this should not happen on most cases, because this redirect is handled by .htaccess
00219         if ( $sParams && !ereg( '\.html$', $sParams ) && !ereg( '\/$', $sParams ) ) {
00220             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00221         }
00222 
00223         return $sParams;
00224     }
00225 
00234     public function fetchSeoUrl( $sStdUrl, $iLanguage = null )
00235     {
00236         $oDb = oxDb::getDb( true );
00237         $sStdUrl = $oDb->quote( $sStdUrl );
00238         $iLanguage = isset( $iLanguage ) ? $iLanguage : oxLang::getInstance()->getBaseLanguage();
00239 
00240         $sSeoUrl = false;
00241 
00242         $sQ = "select oxseourl, oxlang from oxseo where oxstdurl = $sStdUrl and oxlang = '$iLanguage' limit 1";
00243         $oRs = $oDb->execute( $sQ );
00244         if ( !$oRs->EOF ) {
00245             $sSeoUrl = oxSeoEncoder::getInstance()->getLanguageParam( $oRs->fields['oxlang'] ).$oRs->fields['oxseourl'];
00246         }
00247 
00248         return $sSeoUrl;
00249     }
00250 }

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5