OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxseodecoder.php
Go to the documentation of this file.
1 <?php
2 
7 class oxSeoDecoder extends oxSuperCfg
8 {
9 
18  public function parseStdUrl($sUrl)
19  {
20  $oStr = getStr();
21  $aRet = array();
22  $sUrl = $oStr->html_entity_decode($sUrl);
23 
24  if (($iPos = strpos($sUrl, '?')) !== false) {
25  parse_str($oStr->substr($sUrl, $iPos + 1), $aRet);
26  }
27 
28  return $aRet;
29  }
30 
39  protected function _getIdent($sSeoUrl, $blIgnore = false)
40  {
41  return md5(strtolower($sSeoUrl));
42  }
43 
53  public function decodeUrl($sSeoUrl)
54  {
55  $oStr = getStr();
56  $sBaseUrl = $this->getConfig()->getShopURL();
57  if ($oStr->strpos($sSeoUrl, $sBaseUrl) === 0) {
58  $sSeoUrl = $oStr->substr($sSeoUrl, $oStr->strlen($sBaseUrl));
59  }
60  $sSeoUrl = rawurldecode($sSeoUrl);
61  $iShopId = $this->getConfig()->getShopId();
62 
63  $sKey = $this->_getIdent($sSeoUrl);
64  $aRet = false;
65 
67  $oRs = $oDb->select("select oxstdurl, oxlang from oxseo where oxident=" . $oDb->quote($sKey) . " and oxshopid='$iShopId' limit 1");
68  if (!$oRs->EOF) {
69  // primary seo language changed ?
70  $aRet = $this->parseStdUrl($oRs->fields['oxstdurl']);
71  $aRet['lang'] = $oRs->fields['oxlang'];
72  }
73 
74  return $aRet;
75  }
76 
86  protected function _decodeOldUrl($sSeoUrl)
87  {
88  $oStr = getStr();
90  $sBaseUrl = $this->getConfig()->getShopURL();
91  if ($oStr->strpos($sSeoUrl, $sBaseUrl) === 0) {
92  $sSeoUrl = $oStr->substr($sSeoUrl, $oStr->strlen($sBaseUrl));
93  }
94  $iShopId = $this->getConfig()->getShopId();
95  $sSeoUrl = rawurldecode($sSeoUrl);
96 
97  $sKey = $this->_getIdent($sSeoUrl, true);
98 
99  $sUrl = false;
100  $oRs = $oDb->select("select oxobjectid, oxlang from oxseohistory where oxident = " . $oDb->quote($sKey) . " and oxshopid = '{$iShopId}' limit 1");
101  if (!$oRs->EOF) {
102  // updating hit info (oxtimestamp field will be updated automatically)
103  $oDb->execute("update oxseohistory set oxhits = oxhits + 1 where oxident = " . $oDb->quote($sKey) . " and oxshopid = '{$iShopId}' limit 1");
104 
105  // fetching new url
106  $sUrl = $this->_getSeoUrl($oRs->fields['oxobjectid'], $oRs->fields['oxlang'], $iShopId);
107 
108  // appending with $_SERVER["QUERY_STRING"]
109  $sUrl = $this->_addQueryString($sUrl);
110  }
111 
112  return $sUrl;
113  }
114 
122  protected function _addQueryString($sUrl)
123  {
124  if (($sQ = $_SERVER["QUERY_STRING"])) {
125  $sUrl = rtrim($sUrl, "&?");
126  $sQ = ltrim($sQ, "&?");
127 
128  $sUrl .= (strpos($sUrl, '?') === false) ? "?" : "&";
129  $sUrl .= $sQ;
130  }
131 
132  return $sUrl;
133  }
134 
145  protected function _getSeoUrl($sObjectId, $iLang, $iShopId)
146  {
148  $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");
149  if ('oxarticle' == $aInfo['oxtype']) {
150  $sMainCatId = $oDb->getOne("select oxcatnid from " . getViewName("oxobject2category") . " where oxobjectid = " . $oDb->quote($sObjectId) . " order by oxtime");
151  if ($sMainCatId) {
152  $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");
153  if ($sUrl) {
154  return $sUrl;
155  }
156  }
157  }
158 
159  return $aInfo['oxseourl'];
160  }
161 
170  public function processSeoCall($sRequest = null, $sPath = null)
171  {
172  // first - collect needed parameters
173  if (!$sRequest) {
174  if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
175  $sRequest = $_SERVER['REQUEST_URI'];
176  } else {
177  // try something else
178  $sRequest = $_SERVER['SCRIPT_URI'];
179  }
180  }
181 
182  $sPath = $sPath ? $sPath : str_replace('oxseo.php', '', $_SERVER['SCRIPT_NAME']);
183  if (($sParams = $this->_getParams($sRequest, $sPath))) {
184 
185  // in case SEO url is actual
186  if (is_array($aGet = $this->decodeUrl($sParams))) {
187  $_GET = array_merge($aGet, $_GET);
188  oxRegistry::getLang()->resetBaseLanguage();
189  } elseif (($sRedirectUrl = $this->_decodeOldUrl($sParams))) {
190  // in case SEO url was changed - redirecting to new location
191  oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sRedirectUrl, false, 301);
192  } elseif (($sRedirectUrl = $this->_decodeSimpleUrl($sParams))) {
193  // old type II seo urls
194  oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sRedirectUrl, false, 301);
195  } else {
196  oxRegistry::getSession()->start();
197  // unrecognized url
198  error_404_handler($sParams);
199  }
200  }
201  }
202 
211  protected function _decodeSimpleUrl($sParams)
212  {
213  $oStr = getStr();
214  $sLastParam = rtrim($sParams, '/');
215  $sLastParam = $oStr->substr($sLastParam, (( int ) strrpos($sLastParam, '/')) - ($oStr->strlen($sLastParam)));
216  $sLastParam = trim($sParams, '/');
217 
218  // active object id
219  $sUrl = null;
220 
221  if ($sLastParam) {
222 
223  $iLanguage = oxRegistry::getLang()->getBaseLanguage();
224 
225  // article ?
226  if (strpos($sLastParam, '.htm') !== false) {
227  $sUrl = $this->_getObjectUrl($sLastParam, 'oxarticles', $iLanguage, 'oxarticle');
228  } else {
229 
230  // category ?
231  if (!($sUrl = $this->_getObjectUrl($sLastParam, 'oxcategories', $iLanguage, 'oxcategory'))) {
232  // maybe manufacturer ?
233  if (!($sUrl = $this->_getObjectUrl($sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer'))) {
234  // then maybe vendor ?
235  $sUrl = $this->_getObjectUrl($sLastParam, 'oxvendor', $iLanguage, 'oxvendor');
236  }
237  }
238  }
239  }
240 
241  return $sUrl;
242  }
243 
254  protected function _getObjectUrl($sSeoId, $sTable, $iLanguage, $sType)
255  {
256  $oDb = oxDb::getDb();
257  $sTable = getViewName($sTable, $iLanguage);
258  $sSeoUrl = null;
259 
260  // first checking of field exists at all
261  if ($oDb->getOne("show columns from {$sTable} where field = 'oxseoid'")) {
262  // if field exists - searching for object id
263  if ($sObjectId = $oDb->getOne("select oxid from {$sTable} where oxseoid = " . $oDb->quote($sSeoId))) {
264  $sSeoUrl = $oDb->getOne("select oxseourl from oxseo where oxtype = " . $oDb->quote($sType) . " and oxobjectid = " . $oDb->quote($sObjectId) . " and oxlang = " . $oDb->quote($iLanguage) . " ");
265  }
266  }
267 
268  return $sSeoUrl;
269  }
270 
279  protected function _getParams($sRequest, $sPath)
280  {
281  $oStr = getStr();
282 
283  $sParams = $oStr->preg_replace('/\?.*/', '', $sRequest);
284  $sPath = preg_quote($sPath, '/');
285  $sParams = $oStr->preg_replace("/^$sPath/", '', $sParams);
286 
287  // this should not happen on most cases, because this redirect is handled by .htaccess
288  if ($sParams && !$oStr->preg_match('/\.html$/', $sParams) && !$oStr->preg_match('/\/$/', $sParams)) {
289  oxRegistry::getUtils()->redirect($this->getConfig()->getShopURL() . $sParams . '/', false);
290  }
291 
292  return $sParams;
293  }
294 }