oxseodecoder.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoDecoder extends oxSuperCfg
00008 {
00017     public function parseStdUrl($sUrl)
00018     {
00019         $oStr = getStr();
00020         $aRet = array();
00021         $sUrl = $oStr->html_entity_decode( $sUrl );
00022 
00023         if ( ( $iPos = strpos( $sUrl, '?' ) ) !== false ) {
00024             parse_str( $oStr->substr( $sUrl, $iPos+1 ), $aRet );
00025         }
00026 
00027         return $aRet;
00028     }
00029 
00038     protected function _getIdent( $sSeoUrl, $blIgnore = false )
00039     {
00040         return md5( strtolower( $sSeoUrl ) );
00041     }
00042 
00052     public function decodeUrl( $sSeoUrl )
00053     {
00054         $oStr = getStr();
00055         $sBaseUrl = $this->getConfig()->getShopURL();
00056         if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00057             $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00058         }
00059         $sSeoUrl = rawurldecode( $sSeoUrl );
00060         $iShopId = $this->getConfig()->getShopId();
00061 
00062         $sKey = $this->_getIdent( $sSeoUrl );
00063         $aRet = false;
00064 
00065         $oDb = oxDb::getDb(true);
00066         $oRs = $oDb->Execute( "select oxstdurl, oxlang from oxseo where oxident=" . $oDb->quote( $sKey ) . " and oxshopid='$iShopId' limit 1");
00067         if ( !$oRs->EOF ) {
00068             // primary seo language changed ?
00069             $aRet = $this->parseStdUrl( $oRs->fields['oxstdurl'] );
00070             $aRet['lang'] = $oRs->fields['oxlang'];;
00071         }
00072         return $aRet;
00073     }
00074 
00084     protected function _decodeOldUrl( $sSeoUrl )
00085     {
00086         $oStr = getStr();
00087         $oDb = oxDb::getDb(true);
00088         $sBaseUrl = $this->getConfig()->getShopURL();
00089         if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00090             $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00091         }
00092         $iShopId = $this->getConfig()->getShopId();
00093         $sSeoUrl = rawurldecode($sSeoUrl);
00094 
00095         $sKey = $this->_getIdent( $sSeoUrl, true );
00096 
00097         $sUrl = false;
00098         $oRs = $oDb->execute( "select oxobjectid, oxlang from oxseohistory where oxident = " . $oDb->quote( $sKey ) . " and oxshopid = '{$iShopId}' limit 1");
00099         if ( !$oRs->EOF ) {
00100             // updating hit info (oxtimestamp field will be updated automatically)
00101             $oDb->execute( "update oxseohistory set oxhits = oxhits + 1 where oxident = " . $oDb->quote( $sKey ) . " and oxshopid = '{$iShopId}' limit 1" );
00102 
00103             // fetching new url
00104             $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid =  " . $oDb->quote( $oRs->fields['oxobjectid'] ) . " and oxlang =  " . $oDb->quote( $oRs->fields['oxlang'] ) . " and oxshopid = '{$iShopId}' order by oxparams " );
00105         }
00106 
00107         return $sUrl;
00108     }
00109 
00119     public function processSeoCall( $sRequest = null, $sPath = null )
00120     {
00121         // first - collect needed parameters
00122         if ( !$sRequest ) {
00123             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00124                 $sRequest = $_SERVER['REQUEST_URI'];
00125             } else {    // try something else
00126                 $sRequest = $_SERVER['SCRIPT_URI'];
00127             }
00128         }
00129 
00130         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00131         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00132 
00133             // in case SEO url is actual
00134             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00135                 $_GET = array_merge( $aGet, $_GET );
00136                 oxLang::getInstance()->resetBaseLanguage();
00137             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00138                 // in case SEO url was changed - redirecting to new location
00139                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00140             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00141                 // old type II seo urls
00142                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00143             } else { // unrecognized url
00144                 error_404_handler( $sParams );
00145             }
00146         }
00147     }
00148 
00157     protected function _decodeSimpleUrl( $sParams )
00158     {
00159         $oStr = getStr();
00160         $sLastParam = rtrim( $sParams, '/' );
00161         $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00162         $sLastParam = trim( $sParams, '/' );
00163 
00164         // active object id
00165         $sUrl = null;
00166 
00167         if ( $sLastParam ) {
00168 
00169             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00170 
00171             // article ?
00172             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00173                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00174             } else {
00175 
00176                 // category ?
00177                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00178                     // maybe manufacturer ?
00179                     if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00180                         // then maybe vendor ?
00181                         $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00182                     }
00183                 }
00184             }
00185         }
00186 
00187         return $sUrl;
00188     }
00189 
00200     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00201     {
00202         $oDb     = oxDb::getDb();
00203         $sTable  = getViewName( $sTable );
00204         $sField  = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00205         $sSeoUrl = null;
00206 
00207         try {
00208             if ( $sObjectId = $oDb->getOne( "select oxid from $sTable where $sField = ".$oDb->quote( $sSeoId ) ) ) {
00209                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = " . $oDb->quote( $sType ) . " and oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = " . $oDb->quote( $iLanguage ) . " " );
00210             }
00211         } catch ( Exception $oEx ) {
00212             // in case field does not exist must catch db exception
00213         }
00214 
00215         return $sSeoUrl;
00216     }
00217 
00226     protected function _getParams( $sRequest, $sPath )
00227     {
00228         $sParams = preg_replace( '/\?.*/', '', $sRequest );
00229         $sPath   = preg_quote($sPath, '/');
00230         $sParams = preg_replace( "/^$sPath/", '', $sParams );
00231 
00232         // this should not happen on most cases, because this redirect is handled by .htaccess
00233         if ( $sParams && !preg_match( '/\.html$/', $sParams ) && !preg_match( '/\/$/', $sParams ) ) {
00234             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00235         }
00236 
00237         return $sParams;
00238     }
00239 
00240 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5