00001 <?php
00002
00006 class oxUtilsUrl extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 protected $_aAddUrlParams = null;
00021
00026 protected $_aHosts = null;
00027
00035 public static function getInstance()
00036 {
00037 return oxRegistry::get("oxUtilsUrl");
00038 }
00039
00045 public function getBaseAddUrlParams()
00046 {
00047 $aAddUrlParams = array();
00048
00049 return $aAddUrlParams;
00050 }
00051
00057 public function getAddUrlParams()
00058 {
00059 if ( $this->_aAddUrlParams === null ) {
00060 $this->_aAddUrlParams = $this->getBaseAddUrlParams();
00061
00062
00063 if ( ( $iCur = $this->getConfig()->getShopCurrency() ) ) {
00064 $this->_aAddUrlParams['cur'] = $iCur;
00065 }
00066 }
00067 return $this->_aAddUrlParams;
00068 }
00069
00079 public function prepareUrlForNoSession( $sUrl )
00080 {
00081 $oStr = getStr();
00082
00083
00084 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00085 $sUrl = $oStr->preg_replace( '/(&|\?)$/', '', $sUrl );
00086
00087 if ( oxRegistry::getUtils()->seoIsActive() ) {
00088 return $sUrl;
00089 }
00090
00091 if ($qpos = $oStr->strpos($sUrl, '?')) {
00092 if ($qpos == $oStr->strlen($sUrl)-1) {
00093 $sSep = '';
00094 } else {
00095 $sSep = '&';
00096 }
00097 } else {
00098 $sSep = '?';
00099 }
00100
00101 if ( !$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00102 $sUrl .= "{$sSep}lang=".oxRegistry::getLang()->getBaseLanguage();
00103 $sSep = '&';
00104 }
00105
00106 $oConfig = $this->getConfig();
00107 if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00108 $iCur = (int) $oConfig->getShopCurrency();
00109 if ( $iCur ) {
00110 $sUrl .= "{$sSep}cur=".$iCur;
00111 $sSep = '&';
00112 }
00113 }
00114
00115 return $sUrl;
00116 }
00117
00126 public function prepareCanonicalUrl( $sUrl )
00127 {
00128 $oConfig = $this->getConfig();
00129 $oStr = getStr();
00130
00131
00132 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00133 $sUrl = $oStr->preg_replace( '/(&|\?)$/', '', $sUrl );
00134 $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&';
00135
00136
00137 if ( !oxRegistry::getUtils()->seoIsActive() ) {
00138
00139 $iLang = oxRegistry::getLang()->getBaseLanguage();
00140 if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
00141 $sUrl .= "{$sSep}lang=".$iLang;
00142 }
00143 }
00144
00145 return $sUrl;
00146 }
00147
00157 public function appendUrl($sUrl, $aAddParams, $blFinalUrl = false)
00158 {
00159 $oStr = getStr();
00160
00161 $sSeparator = $this->_getUrlParametersSeparator($sUrl);
00162
00163 if (is_array($aAddParams)) {
00164 foreach ( $aAddParams as $sName => $sValue ) {
00165 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00166 $sUrl .= $sSeparator . $sName . "=" . $sValue;
00167 $sSeparator = '&';
00168 }
00169 }
00170 }
00171
00172 if ($sUrl && !$blFinalUrl) {
00173 $sUrl .= $sSeparator;
00174 }
00175
00176 return $sUrl;
00177 }
00178
00187 public function cleanUrl( $sUrl, $aParams = null )
00188 {
00189 $oStr = getStr();
00190 if ( is_array( $aParams ) ) {
00191 foreach ( $aParams as $sParam ) {
00192 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00193 }
00194 } else {
00195 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00196 }
00197
00198 return trim( $sUrl, "?" );
00199 }
00200
00201
00209 public function addShopHost($sUrl)
00210 {
00211 if (!preg_match("#^https?://#i", $sUrl)) {
00212 $sShopUrl = $this->getConfig()->getSslShopUrl();
00213 $sUrl = $sShopUrl . $sUrl;
00214 }
00215
00216 return $sUrl;
00217 }
00218
00229 public function processUrl($sUrl, $blFinalUrl = true, $aParams = null, $iLang = null)
00230 {
00231 $sUrl = $this->appendUrl($sUrl, $aParams, $blFinalUrl);
00232
00233 if ($this->isCurrentShopHost($sUrl)) {
00234 $sUrl = $this->processShopUrl($sUrl, $blFinalUrl, $iLang);
00235 }
00236
00237 return $sUrl;
00238 }
00239
00249 public function processShopUrl($sUrl, $blFinalUrl = true, $iLang = null)
00250 {
00251 $aAddParams = $this->getAddUrlParams();
00252
00253 $sUrl = $this->appendUrl($sUrl, $aAddParams, $blFinalUrl);
00254 $sUrl = oxRegistry::getLang()->processUrl($sUrl, $iLang);
00255 $sUrl = oxRegistry::getSession()->processUrl($sUrl);
00256
00257 if ($blFinalUrl) {
00258 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
00259 }
00260
00261 return $sUrl;
00262 }
00263
00271 public function isCurrentShopHost( $sUrl )
00272 {
00273 $blCurrent = false;
00274 $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
00275
00276 if (is_null($sUrlHost)) {
00277 $blCurrent = true;
00278 } else {
00279 $aHosts = $this->_getHosts();
00280 if (empty($aHosts)) {
00281 $aHosts = array($this->_getShopHostName());
00282 }
00283
00284 foreach ( $aHosts as $sHost ) {
00285 if ( $sHost === $sUrlHost ) {
00286 $blCurrent = true;
00287 break;
00288 }
00289 }
00290 }
00291
00292 return $blCurrent;
00293 }
00294
00302 public function processSeoUrl( $sUrl )
00303 {
00304
00305 if ( !$this->isAdmin() ) {
00306 $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00307 }
00308
00309 $sUrl = $this->cleanUrlParams($sUrl);
00310 return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00311 }
00312
00321 public function cleanUrlParams($sUrl, $sConnector = '&')
00322 {
00323 $aUrlParts = explode('?', $sUrl);
00324
00325
00326 if ( !is_array($aUrlParts) || count($aUrlParts) != 2) {
00327 return $sUrl;
00328 }
00329
00330 $sUrl = $aUrlParts[0];
00331 $sUrlParams = $aUrlParts[1];
00332
00333 $oStrUtils = getStr();
00334 $sUrlParams = $oStrUtils->preg_replace(
00335 array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00336 array('&', '&', '?'),
00337 $sUrlParams
00338 );
00339
00340
00341 parse_str($sUrlParams, $aUrlParams);
00342 $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
00343
00344
00345 $sUrl = str_replace(
00346 array('%5B', '%5D'),
00347 array('[', ']'),
00348 $sUrl
00349 );
00350
00351 return $sUrl;
00352 }
00353
00361 public function appendParamSeparator($sUrl)
00362 {
00363 $oStr = getStr();
00364 if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00365
00366 return $sUrl;
00367 }
00368 if ( $oStr->strpos($sUrl, '?') === false ) {
00369 return $sUrl.'?';
00370 }
00371 return $sUrl.'&';
00372 }
00373
00379 public function getCurrentUrl()
00380 {
00381 $oUtilsServer = oxRegistry::get( "oxUtilsServer" );
00382
00383 $aServerParams["HTTPS"] = $oUtilsServer->getServerVar( "HTTPS" );
00384 $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar( "HTTP_X_FORWARDED_PROTO" );
00385 $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar( "HTTP_HOST" );
00386 $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar( "REQUEST_URI" );
00387
00388 $sProtocol = "http://";
00389
00390 if ( isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
00391 || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
00392 ) {
00393 $sProtocol = 'https://';
00394 }
00395
00396 $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
00397
00398 return $sUrl;
00399 }
00400
00410 public function stringToParamsArray( $sValue )
00411 {
00412
00413
00414 $sValue = str_replace( "&", "&", $sValue );
00415 $aNavParams = explode( "&", $sValue );
00416 $aNavParams = array_filter( $aNavParams );
00417 $aParams = array();
00418 foreach ( $aNavParams as $sValue ) {
00419 $exp = explode( "=", $sValue );
00420 $aParams[$exp[0]] = $exp[1];
00421 }
00422
00423 return $aParams;
00424 }
00425
00432 protected function _addHost($sUrl, & $aHosts)
00433 {
00434 if ($sUrl && ($sHost = @parse_url($sUrl, PHP_URL_HOST))) {
00435 if (!in_array($sHost, $aHosts)) {
00436 $aHosts[] = $sHost;
00437 }
00438 }
00439 }
00440
00447 protected function _addLanguageHost($aLanguageUrls, & $aHosts)
00448 {
00449 $iLanguageId = oxRegistry::getLang()->getBaseLanguage();
00450
00451 if (isset($aLanguageUrls[$iLanguageId])) {
00452 $this->_addHost($aLanguageUrls[$iLanguageId], $aHosts);
00453 }
00454 }
00455
00461 protected function _getHosts()
00462 {
00463 if ($this->_aHosts === null) {
00464 $this->_aHosts = array();
00465 $oConfig = $this->getConfig();
00466
00467 $this->_addMallHosts($this->_aHosts);
00468
00469
00470 $this->_addLanguageHost($oConfig->getConfigParam('aLanguageURLs'), $this->_aHosts);
00471 $this->_addLanguageHost($oConfig->getConfigParam('aLanguageSSLURLs'), $this->_aHosts);
00472
00473
00474 $this->_addHost($oConfig->getConfigParam("sShopURL"), $this->_aHosts);
00475 $this->_addHost($oConfig->getConfigParam("sSSLShopURL"), $this->_aHosts);
00476
00477 if ($this->isAdmin()) {
00478 $this->_addHost($oConfig->getConfigParam("sAdminSSLURL"), $this->_aHosts);
00479 }
00480 }
00481
00482 return $this->_aHosts;
00483 }
00484
00490 protected function _addMallHosts(& $aHosts)
00491 {
00492 }
00493
00501 private function _getUrlParametersSeparator($sUrl)
00502 {
00504 $oStr = getStr();
00505
00506 $sSeparator = '';
00507 if ($oStr->strpos($sUrl, '?') === false) {
00508 $sSeparator = '?';
00509 } else {
00510 if ($sSeparator === '' && !$oStr->preg_match("/(\?|&(amp;)?)$/", $sUrl)) {
00511 $sSeparator = '&';
00512 }
00513 }
00514
00515 return $sSeparator;
00516 }
00517
00523 private function _getShopHostName()
00524 {
00525 return @parse_url($this->getConfig()->getShopUrl(), PHP_URL_HOST);
00526 }
00527 }