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( oxDb::FETCH_MODE_ASSOC );
00066         $oRs = $oDb->select( "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( oxDb::FETCH_MODE_ASSOC );
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->select( "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 = $this->_getSeoUrl($oRs->fields['oxobjectid'], $oRs->fields['oxlang'], $iShopId);
00105 
00106             // appending with $_SERVER["QUERY_STRING"]
00107             $sUrl = $this->_addQueryString( $sUrl );
00108         }
00109 
00110         return $sUrl;
00111     }
00112 
00120     protected function _addQueryString( $sUrl )
00121     {
00122         if ( ( $sQ = $_SERVER["QUERY_STRING"] ) ) {
00123             $sUrl = rtrim( $sUrl, "&?" );
00124             $sQ   = ltrim( $sQ, "&?" );
00125 
00126             $sUrl .= ( strpos( $sUrl, '?') === false ) ? "?" : "&";
00127             $sUrl .= $sQ;
00128         }
00129         return $sUrl;
00130     }
00131 
00142     protected function _getSeoUrl($sObjectId, $iLang, $iShopId)
00143     {
00144         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00145         $aInfo = $oDb->getRow( "select oxseourl, oxtype from oxseo where oxobjectid =  " . $oDb->quote( $sObjectId ) . " and oxlang =  " . $oDb->quote( $iLang ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " order by oxparams limit 1" );
00146         if ('oxarticle' == $aInfo['oxtype']) {
00147             $sMainCatId = $oDb->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = " . $oDb->quote( $sObjectId ) . " order by oxtime" );
00148             if ($sMainCatId) {
00149                 $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid =  " . $oDb->quote( $sObjectId ) . " and oxlang =  " . $oDb->quote( $iLang ) . " and oxshopid = " . $oDb->quote( $iShopId ) . " and oxparams = " . $oDb->quote( $sMainCatId ) . "  order by oxexpired" );
00150                 if ($sUrl) {
00151                     return $sUrl;
00152                 }
00153             }
00154         }
00155 
00156         return $aInfo['oxseourl'];
00157     }
00158 
00168     public function processSeoCall( $sRequest = null, $sPath = null )
00169     {
00170         // first - collect needed parameters
00171         if ( !$sRequest ) {
00172             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00173                 $sRequest = $_SERVER['REQUEST_URI'];
00174             } else {
00175                 // try something else
00176                 $sRequest = $_SERVER['SCRIPT_URI'];
00177             }
00178         }
00179 
00180         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00181         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00182 
00183             // in case SEO url is actual
00184             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00185                 $_GET = array_merge( $aGet, $_GET );
00186                 oxRegistry::getLang()->resetBaseLanguage();
00187             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00188                 // in case SEO url was changed - redirecting to new location
00189                 oxRegistry::getUtils()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00190             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00191                 // old type II seo urls
00192                 oxRegistry::getUtils()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00193             } else {
00194                 oxRegistry::getSession()->start();
00195                 // unrecognized url
00196                 error_404_handler( $sParams );
00197             }
00198         }
00199     }
00200 
00209     protected function _decodeSimpleUrl( $sParams )
00210     {
00211         $oStr = getStr();
00212         $sLastParam = rtrim( $sParams, '/' );
00213         $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00214         $sLastParam = trim( $sParams, '/' );
00215 
00216         // active object id
00217         $sUrl = null;
00218 
00219         if ( $sLastParam ) {
00220 
00221             $iLanguage  = oxRegistry::getLang()->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                     // maybe manufacturer ?
00231                     if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00232                         // then maybe vendor ?
00233                         $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00234                     }
00235                 }
00236             }
00237         }
00238 
00239         return $sUrl;
00240     }
00241 
00252     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00253     {
00254         $oDb     = oxDb::getDb();
00255         $sTable  = getViewName( $sTable, $iLanguage );
00256         $sSeoUrl = null;
00257 
00258         // first checking of field exists at all
00259         if ( $oDb->getOne( "show columns from {$sTable} where field = 'oxseoid'" ) ) {
00260             // if field exists - searching for object id
00261             if ( $sObjectId = $oDb->getOne( "select oxid from {$sTable} where oxseoid = ".$oDb->quote( $sSeoId ) ) ) {
00262                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = " . $oDb->quote( $sType ) . " and oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = " . $oDb->quote( $iLanguage ) . " " );
00263             }
00264         }
00265 
00266         return $sSeoUrl;
00267     }
00268 
00277     protected function _getParams( $sRequest, $sPath )
00278     {
00279         $oStr = getStr();
00280 
00281         $sParams = $oStr->preg_replace( '/\?.*/', '', $sRequest );
00282         $sPath   = preg_quote($sPath, '/');
00283         $sParams = $oStr->preg_replace( "/^$sPath/", '', $sParams );
00284 
00285         // this should not happen on most cases, because this redirect is handled by .htaccess
00286         if ( $sParams && !$oStr->preg_match( '/\.html$/', $sParams ) && !$oStr->preg_match( '/\/$/', $sParams ) ) {
00287             oxRegistry::getUtils()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00288         }
00289 
00290         return $sParams;
00291     }
00292 
00293 }