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 {
00126                 // try something else
00127                 $sRequest = $_SERVER['SCRIPT_URI'];
00128             }
00129         }
00130 
00131         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00132         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00133 
00134             // in case SEO url is actual
00135             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00136                 $_GET = array_merge( $aGet, $_GET );
00137                 oxLang::getInstance()->resetBaseLanguage();
00138             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00139                 // in case SEO url was changed - redirecting to new location
00140                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00141             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00142                 // old type II seo urls
00143                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00144             } else {
00145                 // unrecognized url
00146                 error_404_handler( $sParams );
00147             }
00148         }
00149     }
00150 
00159     protected function _decodeSimpleUrl( $sParams )
00160     {
00161         $oStr = getStr();
00162         $sLastParam = rtrim( $sParams, '/' );
00163         $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00164         $sLastParam = trim( $sParams, '/' );
00165 
00166         // active object id
00167         $sUrl = null;
00168 
00169         if ( $sLastParam ) {
00170 
00171             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00172 
00173             // article ?
00174             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00175                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00176             } else {
00177 
00178                 // category ?
00179                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00180                     // maybe manufacturer ?
00181                     if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00182                         // then maybe vendor ?
00183                         $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00184                     }
00185                 }
00186             }
00187         }
00188 
00189         return $sUrl;
00190     }
00191 
00202     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00203     {
00204         $oDb     = oxDb::getDb();
00205         $sTable  = getViewName( $sTable );
00206         $sField  = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00207         $sSeoUrl = null;
00208 
00209         // first checking of field exists at all
00210         if ( $oDb->getOne( "show columns from {$sTable} where field = '{$sField}'" ) ) {
00211             // if field exists - searching for object id
00212             if ( $sObjectId = $oDb->getOne( "select oxid from {$sTable} where {$sField} = ".$oDb->quote( $sSeoId ) ) ) {
00213                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = " . $oDb->quote( $sType ) . " and oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = " . $oDb->quote( $iLanguage ) . " " );
00214             }
00215         }
00216 
00217         return $sSeoUrl;
00218     }
00219 
00228     protected function _getParams( $sRequest, $sPath )
00229     {
00230         $sParams = preg_replace( '/\?.*/', '', $sRequest );
00231         $sPath   = preg_quote($sPath, '/');
00232         $sParams = preg_replace( "/^$sPath/", '', $sParams );
00233 
00234         // this should not happen on most cases, because this redirect is handled by .htaccess
00235         if ( $sParams && !preg_match( '/\.html$/', $sParams ) && !preg_match( '/\/$/', $sParams ) ) {
00236             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00237         }
00238 
00239         return $sParams;
00240     }
00241 
00242 }

Generated by  doxygen 1.6.2