00001 <?php
00002
00006 class oxUtilsUrl extends oxSuperCfg
00007 {
00008
00014 protected $_aAddUrlParams = null;
00015
00021 protected $_aHosts = null;
00022
00028 public function getBaseAddUrlParams()
00029 {
00030 $aAddUrlParams = array();
00031
00032 return $aAddUrlParams;
00033 }
00034
00040 public function getAddUrlParams()
00041 {
00042 if ($this->_aAddUrlParams === null) {
00043 $this->_aAddUrlParams = $this->getBaseAddUrlParams();
00044
00045
00046 if (($iCur = $this->getConfig()->getShopCurrency())) {
00047 $this->_aAddUrlParams['cur'] = $iCur;
00048 }
00049 }
00050
00051 return $this->_aAddUrlParams;
00052 }
00053
00063 public function prepareUrlForNoSession($sUrl)
00064 {
00066 $oStr = getStr();
00067
00068
00069 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00070 $sUrl = $oStr->preg_replace('/(&|\?)$/', '', $sUrl);
00071
00072 if (oxRegistry::getUtils()->seoIsActive()) {
00073 return $sUrl;
00074 }
00075
00076 if ($qpos = $oStr->strpos($sUrl, '?')) {
00077 if ($qpos == $oStr->strlen($sUrl) - 1) {
00078 $sSep = '';
00079 } else {
00080 $sSep = '&';
00081 }
00082 } else {
00083 $sSep = '?';
00084 }
00085
00086 if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00087 $sUrl .= "{$sSep}lang=" . oxRegistry::getLang()->getBaseLanguage();
00088 $sSep = '&';
00089 }
00090
00091 $oConfig = $this->getConfig();
00092 if (!$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00093 $iCur = (int) $oConfig->getShopCurrency();
00094 if ($iCur) {
00095 $sUrl .= "{$sSep}cur=" . $iCur;
00096 $sSep = '&';
00097 }
00098 }
00099
00100
00101 return $sUrl;
00102 }
00103
00112 public function prepareCanonicalUrl($sUrl)
00113 {
00114 $oConfig = $this->getConfig();
00116 $oStr = getStr();
00117
00118
00119 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00120 $sUrl = $oStr->preg_replace('/(&|\?)$/', '', $sUrl);
00121 $sSep = ($oStr->strpos($sUrl, '?') === false) ? '?' : '&';
00122
00123
00124 if (!oxRegistry::getUtils()->seoIsActive()) {
00125
00126 $iLang = oxRegistry::getLang()->getBaseLanguage();
00127 if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl) &&
00128 $iLang != $oConfig->getConfigParam('sDefaultLang')
00129 ) {
00130 $sUrl .= "{$sSep}lang=" . $iLang;
00131 }
00132 }
00133
00134 return $sUrl;
00135 }
00136
00146 public function appendUrl($sUrl, $aAddParams, $blFinalUrl = false)
00147 {
00149 $oStr = getStr();
00150
00151 $sSeparator = $this->_getUrlParametersSeparator($sUrl);
00152
00153 if (is_array($aAddParams)) {
00154 foreach ($aAddParams as $sName => $sValue) {
00155 if (isset($sValue) && !$oStr->preg_match("/\?(.*&(amp;)?)?" . preg_quote($sName) . "=/", $sUrl)) {
00156 $sUrl .= $sSeparator . $sName . "=" . $sValue;
00157 $sSeparator = '&';
00158 }
00159 }
00160 }
00161
00162 if ($sUrl && !$blFinalUrl) {
00163 $sUrl .= $sSeparator;
00164 }
00165
00166 return $sUrl;
00167 }
00168
00177 public function cleanUrl($sUrl, $aParams = null)
00178 {
00180 $oStr = getStr();
00181 if (is_array($aParams)) {
00182 foreach ($aParams as $sParam) {
00183 $sUrl = $oStr->preg_replace(
00184 '/(\?|&(amp;)?)' . preg_quote($sParam) . '=[a-z0-9\.]+&?(amp;)?/i',
00185 '\1',
00186 $sUrl
00187 );
00188 }
00189 } else {
00190 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?).+/i', '\1', $sUrl);
00191 }
00192
00193 return trim($sUrl, "?");
00194 }
00195
00196
00204 public function addShopHost($sUrl)
00205 {
00206 if (!preg_match("#^https?://#i", $sUrl)) {
00207 $sShopUrl = $this->getConfig()->getSslShopUrl();
00208 $sUrl = $sShopUrl . $sUrl;
00209 }
00210
00211 return $sUrl;
00212 }
00213
00224 public function processUrl($sUrl, $blFinalUrl = true, $aParams = null, $iLang = null)
00225 {
00226 $sUrl = $this->appendUrl($sUrl, $aParams, $blFinalUrl);
00227
00228 if ($this->isCurrentShopHost($sUrl)) {
00229 $sUrl = $this->processShopUrl($sUrl, $blFinalUrl, $iLang);
00230 }
00231
00232 return $sUrl;
00233 }
00234
00244 public function processShopUrl($sUrl, $blFinalUrl = true, $iLang = null)
00245 {
00246 $aAddParams = $this->getAddUrlParams();
00247
00248 $sUrl = $this->appendUrl($sUrl, $aAddParams, $blFinalUrl);
00249 $sUrl = oxRegistry::getLang()->processUrl($sUrl, $iLang);
00250 $sUrl = oxRegistry::getSession()->processUrl($sUrl);
00251
00252 if ($blFinalUrl) {
00253 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
00254 }
00255
00256 return $sUrl;
00257 }
00258
00266 public function isCurrentShopHost($sUrl)
00267 {
00268 $blCurrent = false;
00269 $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
00270
00271 if (is_null($sUrlHost)) {
00272 $blCurrent = true;
00273 } else {
00274 $aHosts = $this->_getHosts();
00275 if (is_null($aHosts)) {
00276 $aHosts = array($this->_getShopHostName());
00277 }
00278
00279 foreach ($aHosts as $sHost) {
00280 if ($sHost === $sUrlHost) {
00281 $blCurrent = true;
00282 break;
00283 }
00284 }
00285 }
00286
00287 return $blCurrent;
00288 }
00289
00297 public function processSeoUrl($sUrl)
00298 {
00299
00300 if (!$this->isAdmin()) {
00301 $sUrl = $this->getSession()->processUrl($this->appendUrl($sUrl, $this->getAddUrlParams()));
00302 }
00303
00304 $sUrl = $this->cleanUrlParams($sUrl);
00305
00306 return getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
00307 }
00308
00317 public function cleanUrlParams($sUrl, $sConnector = '&')
00318 {
00319 $aUrlParts = explode('?', $sUrl);
00320
00321
00322 if (!is_array($aUrlParts) || count($aUrlParts) != 2) {
00323 return $sUrl;
00324 }
00325
00326 $sUrl = $aUrlParts[0];
00327 $sUrlParams = $aUrlParts[1];
00328
00330 $oStrUtils = getStr();
00331 $sUrlParams = $oStrUtils->preg_replace(
00332 array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00333 array('&', '&', '?'),
00334 $sUrlParams
00335 );
00336
00337
00338 parse_str($sUrlParams, $aUrlParams);
00339 $sUrl .= '?' . http_build_query($aUrlParams, '', $sConnector);
00340
00341
00342 $sUrl = str_replace(
00343 array('%5B', '%5D'),
00344 array('[', ']'),
00345 $sUrl
00346 );
00347
00348 return $sUrl;
00349 }
00350
00358 public function appendParamSeparator($sUrl)
00359 {
00361 $oStr = getStr();
00362 if ($oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl)) {
00363
00364 return $sUrl;
00365 }
00366 if ($oStr->strpos($sUrl, '?') === false) {
00367 return $sUrl . '?';
00368 }
00369
00370 return $sUrl . '&';
00371 }
00372
00378 public function getCurrentUrl()
00379 {
00380 $oUtilsServer = oxRegistry::get("oxUtilsServer");
00381
00382 $aServerParams["HTTPS"] = $oUtilsServer->getServerVar("HTTPS");
00383 $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar("HTTP_X_FORWARDED_PROTO");
00384 $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar("HTTP_HOST");
00385 $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar("REQUEST_URI");
00386
00387 $sProtocol = "http://";
00388
00389 if (isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
00390 || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
00391 ) {
00392 $sProtocol = 'https://';
00393 }
00394
00395 $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
00396
00397 return $sUrl;
00398 }
00399
00409 public function stringToParamsArray($sValue)
00410 {
00411
00412
00413 $sValue = str_replace("&", "&", $sValue);
00414 $aNavParams = explode("&", $sValue);
00415 $aNavParams = array_filter($aNavParams);
00416 $aParams = array();
00417 foreach ($aNavParams as $sValue) {
00418 $exp = explode("=", $sValue);
00419 $aParams[$exp[0]] = $exp[1];
00420 }
00421
00422 return $aParams;
00423 }
00424
00425
00431 protected function _getHosts()
00432 {
00433
00434 return $this->_aHosts;
00435 }
00436
00444 private function _getUrlParametersSeparator($sUrl)
00445 {
00447 $oStr = getStr();
00448
00449 $sSeparator = '';
00450 if ($oStr->strpos($sUrl, '?') === false) {
00451 $sSeparator = '?';
00452 } else {
00453 if ($sSeparator === '' && !$oStr->preg_match("/(\?|&(amp;)?)$/", $sUrl)) {
00454 $sSeparator = '&';
00455 }
00456 }
00457
00458 return $sSeparator;
00459 }
00460
00466 private function _getShopHostName()
00467 {
00468 return @parse_url($this->getConfig()->getShopUrl(), PHP_URL_HOST);
00469 }
00470 }