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 
00107         return $sUrl;
00108     }
00109 
00120     protected function _getSeoUrl($sObjectId, $iLang, $iShopId)
00121     {
00122         $oDb = oxDb::getDb(true);
00123         $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" );
00124         if ('oxarticle' == $aInfo['oxtype']) {
00125             $sMainCatId = $oDb->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = " . $oDb->quote( $sObjectId ) . " order by oxtime" );
00126             if ($sMainCatId) {
00127                 $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 ) );
00128                 if ($sUrl) {
00129                     return $sUrl;
00130                 }
00131             }
00132         }
00133 
00134         return $aInfo['oxseourl'];
00135     }
00136 
00146     public function processSeoCall( $sRequest = null, $sPath = null )
00147     {
00148         // first - collect needed parameters
00149         if ( !$sRequest ) {
00150             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00151                 $sRequest = $_SERVER['REQUEST_URI'];
00152             } else {
00153                 // try something else
00154                 $sRequest = $_SERVER['SCRIPT_URI'];
00155             }
00156         }
00157 
00158         $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00159         if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00160 
00161             // in case SEO url is actual
00162             if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00163                 $_GET = array_merge( $aGet, $_GET );
00164                 oxLang::getInstance()->resetBaseLanguage();
00165             } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00166                 // in case SEO url was changed - redirecting to new location
00167                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00168             } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00169                 // old type II seo urls
00170                 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00171             } else {
00172                 // unrecognized url
00173                 error_404_handler( $sParams );
00174             }
00175         }
00176     }
00177 
00186     protected function _decodeSimpleUrl( $sParams )
00187     {
00188         $oStr = getStr();
00189         $sLastParam = rtrim( $sParams, '/' );
00190         $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00191         $sLastParam = trim( $sParams, '/' );
00192 
00193         // active object id
00194         $sUrl = null;
00195 
00196         if ( $sLastParam ) {
00197 
00198             $iLanguage  = oxLang::getInstance()->getBaseLanguage();
00199 
00200             // article ?
00201             if ( strpos( $sLastParam, '.htm' ) !== false ) {
00202                 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00203             } else {
00204 
00205                 // category ?
00206                 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00207                     // maybe manufacturer ?
00208                     if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00209                         // then maybe vendor ?
00210                         $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00211                     }
00212                 }
00213             }
00214         }
00215 
00216         return $sUrl;
00217     }
00218 
00229     protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00230     {
00231         $oDb     = oxDb::getDb();
00232         $sTable  = getViewName( $sTable );
00233         $sField  = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00234         $sSeoUrl = null;
00235 
00236         // first checking of field exists at all
00237         if ( $oDb->getOne( "show columns from {$sTable} where field = '{$sField}'" ) ) {
00238             // if field exists - searching for object id
00239             if ( $sObjectId = $oDb->getOne( "select oxid from {$sTable} where {$sField} = ".$oDb->quote( $sSeoId ) ) ) {
00240                 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = " . $oDb->quote( $sType ) . " and oxobjectid = " . $oDb->quote( $sObjectId ) . " and oxlang = " . $oDb->quote( $iLanguage ) . " " );
00241             }
00242         }
00243 
00244         return $sSeoUrl;
00245     }
00246 
00255     protected function _getParams( $sRequest, $sPath )
00256     {
00257         $oStr = getStr();
00258 
00259         $sParams = $oStr->preg_replace( '/\?.*/', '', $sRequest );
00260         $sPath   = preg_quote($sPath, '/');
00261         $sParams = $oStr->preg_replace( "/^$sPath/", '', $sParams );
00262 
00263         // this should not happen on most cases, because this redirect is handled by .htaccess
00264         if ( $sParams && !$oStr->preg_match( '/\.html$/', $sParams ) && !$oStr->preg_match( '/\/$/', $sParams ) ) {
00265             oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00266         }
00267 
00268         return $sParams;
00269     }
00270 
00271 }