OXID eShop CE  4.9.11
 All Classes Files Functions Variables Pages
oxutilsurl.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsUrl extends oxSuperCfg
7 {
8 
14  protected $_aAddUrlParams = null;
15 
21  protected $_aHosts = null;
22 
28  public function getBaseAddUrlParams()
29  {
30  $aAddUrlParams = array();
31 
32  return $aAddUrlParams;
33  }
34 
40  public function getAddUrlParams()
41  {
42  if ($this->_aAddUrlParams === null) {
43  $this->_aAddUrlParams = $this->getBaseAddUrlParams();
44 
45  // appending currency
46  if (($iCur = $this->getConfig()->getShopCurrency())) {
47  $this->_aAddUrlParams['cur'] = $iCur;
48  }
49  }
50 
51  return $this->_aAddUrlParams;
52  }
53 
63  public function prepareUrlForNoSession($sUrl)
64  {
66  $oStr = getStr();
67 
68  // cleaning up session id..
69  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
70  $sUrl = $oStr->preg_replace('/(&amp;|\?)$/', '', $sUrl);
71 
72  if (oxRegistry::getUtils()->seoIsActive()) {
73  return $sUrl;
74  }
75 
76  if ($qpos = $oStr->strpos($sUrl, '?')) {
77  if ($qpos == $oStr->strlen($sUrl) - 1) {
78  $sSep = '';
79  } else {
80  $sSep = '&amp;';
81  }
82  } else {
83  $sSep = '?';
84  }
85 
86  if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
87  $sUrl .= "{$sSep}lang=" . oxRegistry::getLang()->getBaseLanguage();
88  $sSep = '&amp;';
89  }
90 
91  $oConfig = $this->getConfig();
92  if (!$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
93  $iCur = (int) $oConfig->getShopCurrency();
94  if ($iCur) {
95  $sUrl .= "{$sSep}cur=" . $iCur;
96  $sSep = '&amp;';
97  }
98  }
99 
100 
101  return $sUrl;
102  }
103 
112  public function prepareCanonicalUrl($sUrl)
113  {
114  $oConfig = $this->getConfig();
116  $oStr = getStr();
117 
118  // cleaning up session id..
119  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
120  $sUrl = $oStr->preg_replace('/(&amp;|\?)$/', '', $sUrl);
121  $sSep = ($oStr->strpos($sUrl, '?') === false) ? '?' : '&amp;';
122 
123 
124  if (!oxRegistry::getUtils()->seoIsActive()) {
125  // non seo url has no language identifier..
126  $iLang = oxRegistry::getLang()->getBaseLanguage();
127  if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl) &&
128  $iLang != $oConfig->getConfigParam('sDefaultLang')
129  ) {
130  $sUrl .= "{$sSep}lang=" . $iLang;
131  }
132  }
133 
134  return $sUrl;
135  }
136 
146  public function appendUrl($sUrl, $aAddParams, $blFinalUrl = false)
147  {
149  $oStr = getStr();
150 
151  $sSeparator = $this->_getUrlParametersSeparator($sUrl);
152 
153  if (is_array($aAddParams)) {
154  foreach ($aAddParams as $sName => $sValue) {
155  if (isset($sValue) && !$oStr->preg_match("/\?(.*&(amp;)?)?" . preg_quote($sName) . "=/", $sUrl)) {
156  $sUrl .= $sSeparator . $sName . "=" . $sValue;
157  $sSeparator = '&amp;';
158  }
159  }
160  }
161 
162  if ($sUrl && !$blFinalUrl) {
163  $sUrl .= $sSeparator;
164  }
165 
166  return $sUrl;
167  }
168 
177  public function cleanUrl($sUrl, $aParams = null)
178  {
180  $oStr = getStr();
181  if (is_array($aParams)) {
182  foreach ($aParams as $sParam) {
183  $sUrl = $oStr->preg_replace(
184  '/(\?|&(amp;)?)' . preg_quote($sParam) . '=[a-z0-9\.]+&?(amp;)?/i',
185  '\1',
186  $sUrl
187  );
188  }
189  } else {
190  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?).+/i', '\1', $sUrl);
191  }
192 
193  return trim($sUrl, "?");
194  }
195 
196 
204  public function addShopHost($sUrl)
205  {
206  if (!preg_match("#^https?://#i", $sUrl)) {
207  $sShopUrl = $this->getConfig()->getSslShopUrl();
208  $sUrl = $sShopUrl . $sUrl;
209  }
210 
211  return $sUrl;
212  }
213 
224  public function processUrl($sUrl, $blFinalUrl = true, $aParams = null, $iLang = null)
225  {
226  $sUrl = $this->appendUrl($sUrl, $aParams, $blFinalUrl);
227 
228  if ($this->isCurrentShopHost($sUrl)) {
229  $sUrl = $this->processShopUrl($sUrl, $blFinalUrl, $iLang);
230  }
231 
232  return $sUrl;
233  }
234 
244  public function processShopUrl($sUrl, $blFinalUrl = true, $iLang = null)
245  {
246  $aAddParams = $this->getAddUrlParams();
247 
248  $sUrl = $this->appendUrl($sUrl, $aAddParams, $blFinalUrl);
249  $sUrl = oxRegistry::getLang()->processUrl($sUrl, $iLang);
250  $sUrl = oxRegistry::getSession()->processUrl($sUrl);
251 
252  if ($blFinalUrl) {
253  $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
254  }
255 
256  return $sUrl;
257  }
258 
266  public function isCurrentShopHost($sUrl)
267  {
268  $blCurrent = false;
269  $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
270  // checks if it is relative url.
271  if (is_null($sUrlHost)) {
272  $blCurrent = true;
273  } else {
274  $aHosts = $this->_getHosts();
275 
276  foreach ($aHosts as $sHost) {
277  if ($sHost === $sUrlHost) {
278  $blCurrent = true;
279  break;
280  }
281  }
282  }
283 
284  return $blCurrent;
285  }
286 
294  public function processSeoUrl($sUrl)
295  {
296 
297  if (!$this->isAdmin()) {
298  $sUrl = $this->getSession()->processUrl($this->appendUrl($sUrl, $this->getAddUrlParams()));
299  }
300 
301  $sUrl = $this->cleanUrlParams($sUrl);
302 
303  return getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
304  }
305 
314  public function cleanUrlParams($sUrl, $sConnector = '&amp;')
315  {
316  $aUrlParts = explode('?', $sUrl);
317 
318  // check for params part
319  if (!is_array($aUrlParts) || count($aUrlParts) != 2) {
320  return $sUrl;
321  }
322 
323  $sUrl = $aUrlParts[0];
324  $sUrlParams = $aUrlParts[1];
325 
327  $oStrUtils = getStr();
328  $sUrlParams = $oStrUtils->preg_replace(
329  array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
330  array('&', '&', '?'),
331  $sUrlParams
332  );
333 
334  // remove duplicate entries
335  parse_str($sUrlParams, $aUrlParams);
336  $sUrl .= '?' . http_build_query($aUrlParams, '', $sConnector);
337 
338  // replace brackets
339  $sUrl = str_replace(
340  array('%5B', '%5D'),
341  array('[', ']'),
342  $sUrl
343  );
344 
345  return $sUrl;
346  }
347 
355  public function appendParamSeparator($sUrl)
356  {
358  $oStr = getStr();
359  if ($oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl)) {
360  // it is already ok
361  return $sUrl;
362  }
363  if ($oStr->strpos($sUrl, '?') === false) {
364  return $sUrl . '?';
365  }
366 
367  return $sUrl . '&amp;';
368  }
369 
375  public function getCurrentUrl()
376  {
377  $oUtilsServer = oxRegistry::get("oxUtilsServer");
378 
379  $aServerParams["HTTPS"] = $oUtilsServer->getServerVar("HTTPS");
380  $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar("HTTP_X_FORWARDED_PROTO");
381  $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar("HTTP_HOST");
382  $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar("REQUEST_URI");
383 
384  $sProtocol = "http://";
385 
386  if (isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
387  || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
388  ) {
389  $sProtocol = 'https://';
390  }
391 
392  $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
393 
394  return $sUrl;
395  }
396 
406  public function stringToParamsArray($sValue)
407  {
408  // url building
409  // replace possible ampersands, explode, and filter out empty values
410  $sValue = str_replace("&amp;", "&", $sValue);
411  $aNavParams = explode("&", $sValue);
412  $aNavParams = array_filter($aNavParams);
413  $aParams = array();
414  foreach ($aNavParams as $sValue) {
415  $exp = explode("=", $sValue);
416  $aParams[$exp[0]] = $exp[1];
417  }
418 
419  return $aParams;
420  }
421 
428  protected function _addHost($sUrl, &$aHosts)
429  {
430  if ($sUrl && ($sHost = @parse_url($sUrl, PHP_URL_HOST))) {
431  if (!in_array($sHost, $aHosts)) {
432  $aHosts[] = $sHost;
433  }
434  }
435  }
436 
443  protected function _addLanguageHost($aLanguageUrls, & $aHosts)
444  {
445  $iLanguageId = oxRegistry::getLang()->getBaseLanguage();
446 
447  if (isset($aLanguageUrls[$iLanguageId])) {
448  $this->_addHost($aLanguageUrls[$iLanguageId], $aHosts);
449  }
450  }
451 
457  protected function _getHosts()
458  {
459  if ($this->_aHosts === null) {
460  $this->_aHosts = array();
461  $oConfig = $this->getConfig();
462 
463  $this->_addMallHosts($this->_aHosts);
464 
465  // language url
466  $this->_addLanguageHost($oConfig->getConfigParam('aLanguageURLs'), $this->_aHosts);
467  $this->_addLanguageHost($oConfig->getConfigParam('aLanguageSSLURLs'), $this->_aHosts);
468 
469  // current url
470  $this->_addHost($oConfig->getConfigParam("sShopURL"), $this->_aHosts);
471  $this->_addHost($oConfig->getConfigParam("sSSLShopURL"), $this->_aHosts);
472 
473  if ($this->isAdmin()) {
474  $this->_addHost($oConfig->getConfigParam("sAdminSSLURL"), $this->_aHosts);
475  }
476  }
477 
478  return $this->_aHosts;
479  }
480 
486  protected function _addMallHosts(& $aHosts)
487  {
488  }
489 
497  private function _getUrlParametersSeparator($sUrl)
498  {
500  $oStr = getStr();
501 
502  $sSeparator = '';
503  if ($oStr->strpos($sUrl, '?') === false) {
504  $sSeparator = '?';
505  } else {
506  if ($sSeparator === '' && !$oStr->preg_match("/(\?|&(amp;)?)$/", $sUrl)) {
507  $sSeparator = '&amp;';
508  }
509  }
510 
511  return $sSeparator;
512  }
513 }