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 = $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(true);
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 ) );
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                 oxLang::getInstance()->resetBaseLanguage();
00187             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00188                 // in case SEO url was changed - redirecting to new location
00189                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00190             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00191                 // old type II seo urls
00192                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00193             } else {
00194                 // unrecognized url
00195                 error_404_handler( $sParams );
00196             }
00197         }
00198     }
00199 
00208     protected function _decodeSimpleUrl( $sParams )
00209     {
00210         $oStr = getStr();
00211         $sLastParam = rtrim( $sParams, '/' );
00212         $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00213         $sLastParam = trim( $sParams, '/' );
00214 
00215         // active object id
00216         $sUrl = null;
00217 
00218         if ( $sLastParam ) {
00219 
00220             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00221 
00222             // article ?
00223             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00224                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00225             } else {
00226 
00227                 // category ?
00228                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00229                     // maybe manufacturer ?
00230                     if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00231                         // then maybe vendor ?
00232                         $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00233                     }
00234                 }
00235             }
00236         }
00237 
00238         return $sUrl;
00239     }
00240 
00251     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00252     {
00253         $oDb     = oxDb::getDb();
00254         $sTable  = getViewName( $sTable, $iLanguage );
00255         $sSeoUrl = null;
00256 
00257         // first checking of field exists at all
00258         if ( $oDb->getOne( "show columns from {$sTable} where field = 'oxseoid'" ) ) {
00259             // if field exists - searching for object id
00260             if ( $sObjectId = $oDb->getOne( "select oxid from {$sTable} where oxseoid = ".$oDb->quote( $sSeoId ) ) ) {
00261                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = " . $oDb->quote( $sType ) . " and oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = " . $oDb->quote( $iLanguage ) . " " );
00262             }
00263         }
00264 
00265         return $sSeoUrl;
00266     }
00267 
00276     protected function _getParams( $sRequest, $sPath )
00277     {
00278         $oStr = getStr();
00279 
00280         $sParams = $oStr->preg_replace( '/\?.*/', '', $sRequest );
00281         $sPath   = preg_quote($sPath, '/');
00282         $sParams = $oStr->preg_replace( "/^$sPath/", '', $sParams );
00283 
00284         // this should not happen on most cases, because this redirect is handled by .htaccess
00285         if ( $sParams && !$oStr->preg_match( '/\.html$/', $sParams ) && !$oStr->preg_match( '/\/$/', $sParams ) ) {
00286             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00287         }
00288 
00289         return $sParams;
00290     }
00291 
00292 }