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
00156 public function appendUrl( $sUrl, $aAddParams )
00157 {
00158 $oStr = getStr();
00159 $sSep = '&';
00160 if ( $oStr->strpos( $sUrl, '?' ) === false ) {
00161 $sSep = '?';
00162 }
00163
00164 if ( count( $aAddParams ) ) {
00165 foreach ( $aAddParams as $sName => $sValue ) {
00166 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00167 $sUrl .= $sSep . $sName . "=" . $sValue;
00168 $sSep = '&';
00169 }
00170 }
00171 }
00172 return $sUrl ? $sUrl.$sSep : '';
00173 }
00174
00183 public function cleanUrl( $sUrl, $aParams = null )
00184 {
00185 $oStr = getStr();
00186 if ( is_array( $aParams ) ) {
00187 foreach ( $aParams as $sParam ) {
00188 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00189 }
00190 } else {
00191 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00192 }
00193
00194 return trim( $sUrl, "?" );
00195 }
00196
00207 public function processUrl( $sUrl, $blFinalUrl = true, $aParams = null, $iLang = null )
00208 {
00209 $aParams = is_array($aParams)? $aParams : array();
00210
00211 $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
00212 $sShopHost = @parse_url($this->getConfig()->getShopUrl(), PHP_URL_HOST);
00213 $isCurrentShop = is_null($sUrlHost) || $sShopHost == $sUrlHost;
00214
00215 if ( $isCurrentShop ) {
00216 $aParams = array_merge($this->getAddUrlParams(), $aParams);
00217 }
00218 $sUrl = $this->appendUrl( $sUrl, $aParams );
00219
00220 if ( $isCurrentShop ) {
00221 $sUrl = oxRegistry::getSession()->processUrl(
00222 oxRegistry::getLang()->processUrl(
00223 $sUrl,
00224 $iLang
00225 )
00226 );
00227 }
00228
00229 if ($blFinalUrl) {
00230 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)+$/', '', $sUrl);
00231 }
00232 return $sUrl;
00233 }
00234
00243 protected function _addHost( $sUrl, & $aHosts )
00244 {
00245 if ( $sUrl && ( $sHost = @parse_url( $sUrl, PHP_URL_HOST ) ) ) {
00246 if ( !in_array( $sHost, $aHosts ) ) {
00247 $aHosts[] = $sHost;
00248 }
00249 }
00250 }
00251
00257 protected function _getHosts()
00258 {
00259 if ( $this->_aHosts === null ) {
00260 $this->_aHosts = array();
00261 $oConfig = $this->getConfig();
00262
00263
00264 $this->_addHost( $oConfig->getConfigParam( "sMallShopURL" ), $this->_aHosts );
00265 $this->_addHost( $oConfig->getConfigParam( "sMallSSLShopURL" ), $this->_aHosts );
00266
00267
00268 $this->_addHost( $oConfig->getConfigParam( 'aLanguageURLs' ), $this->_aHosts );
00269 $this->_addHost( $oConfig->getConfigParam( 'aLanguageSSLURLs' ), $this->_aHosts );
00270
00271 if ( !count( $aHosts ) && !$oConfig->mustAddShopIdToRequest() ) {
00272
00273 $this->_addHost( $oConfig->getConfigParam( "sShopURL" ), $this->_aHosts );
00274 $this->_addHost( $oConfig->getConfigParam( "sSSLShopURL" ), $this->_aHosts );
00275 }
00276
00277 if ( $this->isAdmin() ) {
00278 $this->_addHost( $oConfig->getConfigParam( "sAdminSSLURL" ), $this->_aHosts );
00279 }
00280 }
00281
00282 return $this->_aHosts;
00283 }
00284
00292 public function isCurrentShopHost( $sUrl )
00293 {
00294 $blCurrent = false;
00295 if ( $sUrl ) {
00296
00297 $sUrlHost = @parse_url( $sUrl, PHP_URL_HOST );
00298
00299
00300 $aHosts = $this->_getHosts();
00301
00302 foreach ( $aHosts as $sHost ) {
00303 if ( $sHost === $sUrlHost ) {
00304 $blCurrent = true;
00305 break;
00306 }
00307 }
00308 }
00309
00310 return $blCurrent;
00311 }
00312
00320 public function processSeoUrl( $sUrl )
00321 {
00322
00323 if ( !$this->isAdmin() ) {
00324 $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00325 }
00326
00327 $sUrl = $this->cleanUrlParams($sUrl);
00328 return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00329 }
00330
00339 public function cleanUrlParams($sUrl, $sConnector = '&')
00340 {
00341 $aUrlParts = explode('?', $sUrl);
00342
00343
00344 if ( !is_array($aUrlParts) || count($aUrlParts) != 2) {
00345 return $sUrl;
00346 }
00347
00348 $sUrl = $aUrlParts[0];
00349 $sUrlParams = $aUrlParts[1];
00350
00351 $oStrUtils = getStr();
00352 $sUrlParams = $oStrUtils->preg_replace(
00353 array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00354 array('&', '&', '?'),
00355 $sUrlParams
00356 );
00357
00358
00359 parse_str($sUrlParams, $aUrlParams);
00360 $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
00361
00362
00363 $sUrl = str_replace(
00364 array('%5B', '%5D'),
00365 array('[', ']'),
00366 $sUrl
00367 );
00368
00369 return $sUrl;
00370 }
00371
00379 public function appendParamSeparator($sUrl)
00380 {
00381 $oStr = getStr();
00382 if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00383
00384 return $sUrl;
00385 }
00386 if ( $oStr->strpos($sUrl, '?') === false ) {
00387 return $sUrl.'?';
00388 }
00389 return $sUrl.'&';
00390 }
00391
00397 function getCurrentUrl()
00398 {
00399 $oUtilsServer = oxRegistry::get( "oxUtilsServer" );
00400
00401 $aServerParams["HTTPS"] = $oUtilsServer->getServerVar( "HTTPS" );
00402 $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar( "HTTP_X_FORWARDED_PROTO" );
00403 $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar( "HTTP_HOST" );
00404 $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar( "REQUEST_URI" );
00405
00406 $sProtocol = "http://";
00407
00408 if ( isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
00409 || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
00410 ) {
00411 $sProtocol = 'https://';
00412 }
00413
00414 $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
00415
00416 return $sUrl;
00417 }
00418 }