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