OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxutilsurl.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsUrl extends oxSuperCfg
7 {
13  private static $_instance = null;
14 
20  protected $_aAddUrlParams = null;
21 
26  protected $_aHosts = null;
27 
35  public static function getInstance()
36  {
37  return oxRegistry::get("oxUtilsUrl");
38  }
39 
45  public function getBaseAddUrlParams()
46  {
47  $aAddUrlParams = array();
48 
49  return $aAddUrlParams;
50  }
51 
57  public function getAddUrlParams()
58  {
59  if ( $this->_aAddUrlParams === null ) {
60  $this->_aAddUrlParams = $this->getBaseAddUrlParams();
61 
62  // appending currency
63  if ( ( $iCur = $this->getConfig()->getShopCurrency() ) ) {
64  $this->_aAddUrlParams['cur'] = $iCur;
65  }
66  }
67  return $this->_aAddUrlParams;
68  }
69 
79  public function prepareUrlForNoSession( $sUrl )
80  {
81  $oStr = getStr();
82 
83  // cleaning up session id..
84  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
85  $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $sUrl );
86 
87  if ( oxRegistry::getUtils()->seoIsActive() ) {
88  return $sUrl;
89  }
90 
91  if ($qpos = $oStr->strpos($sUrl, '?')) {
92  if ($qpos == $oStr->strlen($sUrl)-1) {
93  $sSep = '';
94  } else {
95  $sSep = '&amp;';
96  }
97  } else {
98  $sSep = '?';
99  }
100 
101  if ( !$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
102  $sUrl .= "{$sSep}lang=".oxRegistry::getLang()->getBaseLanguage();
103  $sSep = '&amp;';
104  }
105 
106  $oConfig = $this->getConfig();
107  if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
108  $iCur = (int) $oConfig->getShopCurrency();
109  if ( $iCur ) {
110  $sUrl .= "{$sSep}cur=".$iCur;
111  $sSep = '&amp;';
112  }
113  }
114 
115  return $sUrl;
116  }
117 
126  public function prepareCanonicalUrl( $sUrl )
127  {
128  $oConfig = $this->getConfig();
129  $oStr = getStr();
130 
131  // cleaning up session id..
132  $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
133  $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $sUrl );
134  $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&amp;';
135 
136 
137  if ( !oxRegistry::getUtils()->seoIsActive() ) {
138  // non seo url has no language identifier..
139  $iLang = oxRegistry::getLang()->getBaseLanguage();
140  if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
141  $sUrl .= "{$sSep}lang=".$iLang;
142  }
143  }
144 
145  return $sUrl;
146  }
147 
157  public function appendUrl($sUrl, $aAddParams, $blFinalUrl = false)
158  {
159  $oStr = getStr();
160 
161  $sSeparator = $this->_getUrlParametersSeparator($sUrl);
162 
163  if (is_array($aAddParams)) {
164  foreach ( $aAddParams as $sName => $sValue ) {
165  if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
166  $sUrl .= $sSeparator . $sName . "=" . $sValue;
167  $sSeparator = '&amp;';
168  }
169  }
170  }
171 
172  if ($sUrl && !$blFinalUrl) {
173  $sUrl .= $sSeparator;
174  }
175 
176  return $sUrl;
177  }
178 
187  public function cleanUrl( $sUrl, $aParams = null )
188  {
189  $oStr = getStr();
190  if ( is_array( $aParams ) ) {
191  foreach ( $aParams as $sParam ) {
192  $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
193  }
194  } else {
195  $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
196  }
197 
198  return trim( $sUrl, "?" );
199  }
200 
201 
209  public function addShopHost($sUrl)
210  {
211  if (!preg_match("#^https?://#i", $sUrl)) {
212  $sShopUrl = $this->getConfig()->getSslShopUrl();
213  $sUrl = $sShopUrl . $sUrl;
214  }
215 
216  return $sUrl;
217  }
218 
229  public function processUrl($sUrl, $blFinalUrl = true, $aParams = null, $iLang = null)
230  {
231  $sUrl = $this->appendUrl($sUrl, $aParams, $blFinalUrl);
232 
233  if ($this->isCurrentShopHost($sUrl)) {
234  $sUrl = $this->processShopUrl($sUrl, $blFinalUrl, $iLang);
235  }
236 
237  return $sUrl;
238  }
239 
249  public function processShopUrl($sUrl, $blFinalUrl = true, $iLang = null)
250  {
251  $aAddParams = $this->getAddUrlParams();
252 
253  $sUrl = $this->appendUrl($sUrl, $aAddParams, $blFinalUrl);
254  $sUrl = oxRegistry::getLang()->processUrl($sUrl, $iLang);
255  $sUrl = oxRegistry::getSession()->processUrl($sUrl);
256 
257  if ($blFinalUrl) {
258  $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
259  }
260 
261  return $sUrl;
262  }
263 
271  public function isCurrentShopHost( $sUrl )
272  {
273  $blCurrent = false;
274  $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
275  // checks if it is relative url.
276  if (is_null($sUrlHost)) {
277  $blCurrent = true;
278  } else {
279  $aHosts = $this->_getHosts();
280  if (empty($aHosts)) {
281  $aHosts = array($this->_getShopHostName());
282  }
283 
284  foreach ( $aHosts as $sHost ) {
285  if ( $sHost === $sUrlHost ) {
286  $blCurrent = true;
287  break;
288  }
289  }
290  }
291 
292  return $blCurrent;
293  }
294 
302  public function processSeoUrl( $sUrl )
303  {
304 
305  if ( !$this->isAdmin() ) {
306  $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
307  }
308 
309  $sUrl = $this->cleanUrlParams($sUrl);
310  return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
311  }
312 
321  public function cleanUrlParams($sUrl, $sConnector = '&amp;')
322  {
323  $aUrlParts = explode('?', $sUrl);
324 
325  // check for params part
326  if ( !is_array($aUrlParts) || count($aUrlParts) != 2) {
327  return $sUrl;
328  }
329 
330  $sUrl = $aUrlParts[0];
331  $sUrlParams = $aUrlParts[1];
332 
333  $oStrUtils = getStr();
334  $sUrlParams = $oStrUtils->preg_replace(
335  array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
336  array('&', '&', '?'),
337  $sUrlParams
338  );
339 
340  // remove duplicate entries
341  parse_str($sUrlParams, $aUrlParams);
342  $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
343 
344  // replace brackets
345  $sUrl = str_replace(
346  array('%5B', '%5D'),
347  array('[', ']'),
348  $sUrl
349  );
350 
351  return $sUrl;
352  }
353 
361  public function appendParamSeparator($sUrl)
362  {
363  $oStr = getStr();
364  if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
365  // it is already ok
366  return $sUrl;
367  }
368  if ( $oStr->strpos($sUrl, '?') === false ) {
369  return $sUrl.'?';
370  }
371  return $sUrl.'&amp;';
372  }
373 
379  public function getCurrentUrl()
380  {
381  $oUtilsServer = oxRegistry::get( "oxUtilsServer" );
382 
383  $aServerParams["HTTPS"] = $oUtilsServer->getServerVar( "HTTPS" );
384  $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar( "HTTP_X_FORWARDED_PROTO" );
385  $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar( "HTTP_HOST" );
386  $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar( "REQUEST_URI" );
387 
388  $sProtocol = "http://";
389 
390  if ( isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
391  || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
392  ) {
393  $sProtocol = 'https://';
394  }
395 
396  $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
397 
398  return $sUrl;
399  }
400 
410  public function stringToParamsArray( $sValue )
411  {
412  // url building
413  // replace possible ampersands, explode, and filter out empty values
414  $sValue = str_replace( "&amp;", "&", $sValue );
415  $aNavParams = explode( "&", $sValue );
416  $aNavParams = array_filter( $aNavParams );
417  $aParams = array();
418  foreach ( $aNavParams as $sValue ) {
419  $exp = explode( "=", $sValue );
420  $aParams[$exp[0]] = $exp[1];
421  }
422 
423  return $aParams;
424  }
425 
432  protected function _addHost($sUrl, & $aHosts)
433  {
434  if ($sUrl && ($sHost = @parse_url($sUrl, PHP_URL_HOST))) {
435  if (!in_array($sHost, $aHosts)) {
436  $aHosts[] = $sHost;
437  }
438  }
439  }
440 
447  protected function _addLanguageHost($aLanguageUrls, & $aHosts)
448  {
449  $iLanguageId = oxRegistry::getLang()->getBaseLanguage();
450 
451  if (isset($aLanguageUrls[$iLanguageId])) {
452  $this->_addHost($aLanguageUrls[$iLanguageId], $aHosts);
453  }
454  }
455 
461  protected function _getHosts()
462  {
463  if ($this->_aHosts === null) {
464  $this->_aHosts = array();
465  $oConfig = $this->getConfig();
466 
467  $this->_addMallHosts($this->_aHosts);
468 
469  // language url
470  $this->_addLanguageHost($oConfig->getConfigParam('aLanguageURLs'), $this->_aHosts);
471  $this->_addLanguageHost($oConfig->getConfigParam('aLanguageSSLURLs'), $this->_aHosts);
472 
473  // current url
474  $this->_addHost($oConfig->getConfigParam("sShopURL"), $this->_aHosts);
475  $this->_addHost($oConfig->getConfigParam("sSSLShopURL"), $this->_aHosts);
476 
477  if ($this->isAdmin()) {
478  $this->_addHost($oConfig->getConfigParam("sAdminSSLURL"), $this->_aHosts);
479  }
480  }
481 
482  return $this->_aHosts;
483  }
484 
490  protected function _addMallHosts(& $aHosts)
491  {
492  }
493 
501  private function _getUrlParametersSeparator($sUrl)
502  {
504  $oStr = getStr();
505 
506  $sSeparator = '';
507  if ($oStr->strpos($sUrl, '?') === false) {
508  $sSeparator = '?';
509  } else {
510  if ($sSeparator === '' && !$oStr->preg_match("/(\?|&(amp;)?)$/", $sUrl)) {
511  $sSeparator = '&amp;';
512  }
513  }
514 
515  return $sSeparator;
516  }
517 
523  private function _getShopHostName()
524  {
525  return @parse_url($this->getConfig()->getShopUrl(), PHP_URL_HOST);
526  }
527 }