oxseodecoder.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoDecoder extends oxSuperCfg
00008 {
00009 
00018     public function parseStdUrl($sUrl)
00019     {
00020         $oStr = getStr();
00021         $aRet = array();
00022         $sUrl = $oStr->html_entity_decode($sUrl);
00023 
00024         if (($iPos = strpos($sUrl, '?')) !== false) {
00025             parse_str($oStr->substr($sUrl, $iPos + 1), $aRet);
00026         }
00027 
00028         return $aRet;
00029     }
00030 
00039     protected function _getIdent($sSeoUrl, $blIgnore = false)
00040     {
00041         return md5(strtolower($sSeoUrl));
00042     }
00043 
00053     public function decodeUrl($sSeoUrl)
00054     {
00055         $oStr = getStr();
00056         $sBaseUrl = $this->getConfig()->getShopURL();
00057         if ($oStr->strpos($sSeoUrl, $sBaseUrl) === 0) {
00058             $sSeoUrl = $oStr->substr($sSeoUrl, $oStr->strlen($sBaseUrl));
00059         }
00060         $sSeoUrl = rawurldecode($sSeoUrl);
00061         $iShopId = $this->getConfig()->getShopId();
00062 
00063         $sKey = $this->_getIdent($sSeoUrl);
00064         $aRet = false;
00065 
00066         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00067         $oRs = $oDb->select("select oxstdurl, oxlang from oxseo where oxident=" . $oDb->quote($sKey) . " and oxshopid='$iShopId' limit 1");
00068         if (!$oRs->EOF) {
00069             // primary seo language changed ?
00070             $aRet = $this->parseStdUrl($oRs->fields['oxstdurl']);
00071             $aRet['lang'] = $oRs->fields['oxlang'];
00072         }
00073 
00074         return $aRet;
00075     }
00076 
00086     protected function _decodeOldUrl($sSeoUrl)
00087     {
00088         $oStr = getStr();
00089         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00090         $sBaseUrl = $this->getConfig()->getShopURL();
00091         if ($oStr->strpos($sSeoUrl, $sBaseUrl) === 0) {
00092             $sSeoUrl = $oStr->substr($sSeoUrl, $oStr->strlen($sBaseUrl));
00093         }
00094         $iShopId = $this->getConfig()->getShopId();
00095         $sSeoUrl = rawurldecode($sSeoUrl);
00096 
00097         $sKey = $this->_getIdent($sSeoUrl, true);
00098 
00099         $sUrl = false;
00100         $oRs = $oDb->select("select oxobjectid, oxlang from oxseohistory where oxident = " . $oDb->quote($sKey) . " and oxshopid = '{$iShopId}' limit 1");
00101         if (!$oRs->EOF) {
00102             // updating hit info (oxtimestamp field will be updated automatically)
00103             $oDb->execute("update oxseohistory set oxhits = oxhits + 1 where oxident = " . $oDb->quote($sKey) . " and oxshopid = '{$iShopId}' limit 1");
00104 
00105             // fetching new url
00106             $sUrl = $this->_getSeoUrl($oRs->fields['oxobjectid'], $oRs->fields['oxlang'], $iShopId);
00107 
00108             // appending with $_SERVER["QUERY_STRING"]
00109             $sUrl = $this->_addQueryString($sUrl);
00110         }
00111 
00112         return $sUrl;
00113     }
00114 
00122     protected function _addQueryString($sUrl)
00123     {
00124         if (($sQ = $_SERVER["QUERY_STRING"])) {
00125             $sUrl = rtrim($sUrl, "&?");
00126             $sQ = ltrim($sQ, "&?");
00127 
00128             $sUrl .= (strpos($sUrl, '?') === false) ? "?" : "&";
00129             $sUrl .= $sQ;
00130         }
00131 
00132         return $sUrl;
00133     }
00134 
00145     protected function _getSeoUrl($sObjectId, $iLang, $iShopId)
00146     {
00147         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00148         $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");
00149         if ('oxarticle' == $aInfo['oxtype']) {
00150             $sMainCatId = $oDb->getOne("select oxcatnid from " . getViewName("oxobject2category") . " where oxobjectid = " . $oDb->quote($sObjectId) . " order by oxtime");
00151             if ($sMainCatId) {
00152                 $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");
00153                 if ($sUrl) {
00154                     return $sUrl;
00155                 }
00156             }
00157         }
00158 
00159         return $aInfo['oxseourl'];
00160     }
00161 
00170     public function processSeoCall($sRequest = null, $sPath = null)
00171     {
00172         // first - collect needed parameters
00173         if (!$sRequest) {
00174             if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
00175                 $sRequest = $_SERVER['REQUEST_URI'];
00176             } else {
00177                 // try something else
00178                 $sRequest = $_SERVER['SCRIPT_URI'];
00179             }
00180         }
00181 
00182         $sPath = $sPath ? $sPath : str_replace('oxseo.php', '', $_SERVER['SCRIPT_NAME']);
00183         if (($sParams = $this->_getParams($sRequest, $sPath))) {
00184 
00185             // in case SEO url is actual
00186             if (is_array($aGet = $this->decodeUrl($sParams))) {
00187                 $_GET = array_merge($aGet, $_GET);
00188                 oxRegistry::getLang()->resetBaseLanguage();
00189             } elseif (($sRedirectUrl = $this->_decodeOldUrl($sParams))) {
00190                 // in case SEO url was changed - redirecting to new location
00191                 oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sRedirectUrl, false);
00192             } elseif (($sRedirectUrl = $this->_decodeSimpleUrl($sParams))) {
00193                 // old type II seo urls
00194                 oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sRedirectUrl, false);
00195             } else {
00196                 oxRegistry::getSession()->start();
00197                 // unrecognized url
00198                 error_404_handler($sParams);
00199             }
00200         }
00201     }
00202 
00211     protected function _decodeSimpleUrl($sParams)
00212     {
00213         $oStr = getStr();
00214         $sLastParam = rtrim($sParams, '/');
00215         $sLastParam = $oStr->substr($sLastParam, (( int ) strrpos($sLastParam, '/')) - ($oStr->strlen($sLastParam)));
00216         $sLastParam = trim($sParams, '/');
00217 
00218         // active object id
00219         $sUrl = null;
00220 
00221         if ($sLastParam) {
00222 
00223             $iLanguage = oxRegistry::getLang()->getBaseLanguage();
00224 
00225             // article ?
00226             if (strpos($sLastParam, '.htm') !== false) {
00227                 $sUrl = $this->_getObjectUrl($sLastParam, 'oxarticles', $iLanguage, 'oxarticle');
00228             } else {
00229 
00230                 // category ?
00231                 if (!($sUrl = $this->_getObjectUrl($sLastParam, 'oxcategories', $iLanguage, 'oxcategory'))) {
00232                     // maybe manufacturer ?
00233                     if (!($sUrl = $this->_getObjectUrl($sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer'))) {
00234                         // then maybe vendor ?
00235                         $sUrl = $this->_getObjectUrl($sLastParam, 'oxvendor', $iLanguage, 'oxvendor');
00236                     }
00237                 }
00238             }
00239         }
00240 
00241         return $sUrl;
00242     }
00243 
00254     protected function _getObjectUrl($sSeoId, $sTable, $iLanguage, $sType)
00255     {
00256         $oDb = oxDb::getDb();
00257         $sTable = getViewName($sTable, $iLanguage);
00258         $sSeoUrl = null;
00259 
00260         // first checking of field exists at all
00261         if ($oDb->getOne("show columns from {$sTable} where field = 'oxseoid'")) {
00262             // if field exists - searching for object id
00263             if ($sObjectId = $oDb->getOne("select oxid from {$sTable} where oxseoid = " . $oDb->quote($sSeoId))) {
00264                 $sSeoUrl = $oDb->getOne("select oxseourl from oxseo where oxtype = " . $oDb->quote($sType) . " and oxobjectid = " . $oDb->quote($sObjectId) . " and oxlang = " . $oDb->quote($iLanguage) . " ");
00265             }
00266         }
00267 
00268         return $sSeoUrl;
00269     }
00270 
00279     protected function _getParams($sRequest, $sPath)
00280     {
00281         $oStr = getStr();
00282 
00283         $sParams = $oStr->preg_replace('/\?.*/', '', $sRequest);
00284         $sPath = preg_quote($sPath, '/');
00285         $sParams = $oStr->preg_replace("/^$sPath/", '', $sParams);
00286 
00287         // this should not happen on most cases, because this redirect is handled by .htaccess
00288         if ($sParams && !$oStr->preg_match('/\.html$/', $sParams) && !$oStr->preg_match('/\/$/', $sParams)) {
00289             oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sParams . '/', false);
00290         }
00291 
00292         return $sParams;
00293     }
00294 }