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
00033 public static function getInstance()
00034 {
00035
00036 if ( defined( 'OXID_PHP_UNIT' ) ) {
00037 self::$_instance = modInstances::getMod( __CLASS__ );
00038 }
00039
00040 if ( !(self::$_instance instanceof oxUtilsUrl ) ) {
00041 self::$_instance = oxNew( 'oxUtilsUrl' );
00042 if ( defined( 'OXID_PHP_UNIT' ) ) {
00043 modInstances::addMod( __CLASS__, self::$_instance);
00044 }
00045 }
00046 return self::$_instance;
00047 }
00048
00054 public function getBaseAddUrlParams()
00055 {
00056 $aAddUrlParams = array();
00057
00058 return $aAddUrlParams;
00059 }
00060
00066 public function getAddUrlParams()
00067 {
00068 if ( $this->_aAddUrlParams === null ) {
00069 $this->_aAddUrlParams = $this->getBaseAddUrlParams();
00070
00071
00072 if ( ( $iCur = $this->getConfig()->getShopCurrency() ) ) {
00073 $this->_aAddUrlParams['cur'] = $iCur;
00074 }
00075 }
00076 return $this->_aAddUrlParams;
00077 }
00078
00088 public function prepareUrlForNoSession( $sUrl )
00089 {
00090 $oStr = getStr();
00091
00092
00093 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00094 $sUrl = $oStr->preg_replace( '/(&|\?)$/', '', $sUrl );
00095
00096 if ( oxUtils::getInstance()->seoIsActive() ) {
00097 return $sUrl;
00098 }
00099
00100 if ($qpos = $oStr->strpos($sUrl, '?')) {
00101 if ($qpos == $oStr->strlen($sUrl)-1) {
00102 $sSep = '';
00103 } else {
00104 $sSep = '&';
00105 }
00106 } else {
00107 $sSep = '?';
00108 }
00109
00110 if ( !$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00111 $sUrl .= "{$sSep}lang=".oxLang::getInstance()->getBaseLanguage();
00112 $sSep = '&';
00113 }
00114
00115 $oConfig = $this->getConfig();
00116 if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00117 $iCur = (int) $oConfig->getShopCurrency();
00118 if ( $iCur ) {
00119 $sUrl .= "{$sSep}cur=".$iCur;
00120 $sSep = '&';
00121 }
00122 }
00123
00124 return $sUrl;
00125 }
00126
00135 public function prepareCanonicalUrl( $sUrl )
00136 {
00137 $oConfig = $this->getConfig();
00138 $oStr = getStr();
00139
00140
00141 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00142 $sUrl = $oStr->preg_replace( '/(&|\?)$/', '', $sUrl );
00143 $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&';
00144
00145
00146 if ( !oxUtils::getInstance()->seoIsActive() ) {
00147
00148 $iLang = oxLang::getInstance()->getBaseLanguage();
00149 if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
00150 $sUrl .= "{$sSep}lang=".$iLang;
00151 }
00152 }
00153
00154 return $sUrl;
00155 }
00156
00165 public function appendUrl( $sUrl, $aAddParams )
00166 {
00167 $oStr = getStr();
00168 $sSep = '&';
00169 if ( $oStr->strpos( $sUrl, '?' ) === false ) {
00170 $sSep = '?';
00171 }
00172
00173 if ( count( $aAddParams ) ) {
00174 foreach ( $aAddParams as $sName => $sValue ) {
00175 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00176 $sUrl .= $sSep . $sName . "=" . $sValue;
00177 $sSep = '&';
00178 }
00179 }
00180 }
00181 return $sUrl ? $sUrl.$sSep : '';
00182 }
00183
00192 public function cleanUrl( $sUrl, $aParams = null )
00193 {
00194 $oStr = getStr();
00195 if ( is_array( $aParams ) ) {
00196 foreach ( $aParams as $sParam ) {
00197 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00198 }
00199 } else {
00200 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00201 }
00202
00203 return trim( $sUrl, "?" );
00204 }
00205
00216 public function processUrl( $sUrl, $blFinalUrl = true, $aParams = null, $iLang = null )
00217 {
00218 $aAddParams = $this->getAddUrlParams();
00219 if ( is_array($aParams) && count( $aParams ) ) {
00220 $aAddParams = array_merge( $aAddParams, $aParams );
00221 }
00222
00223 $ret = oxSession::getInstance()->processUrl(
00224 oxLang::getInstance()->processUrl(
00225 $this->appendUrl(
00226 $sUrl,
00227 $aAddParams
00228 ),
00229 $iLang
00230 )
00231 );
00232
00233 if ($blFinalUrl) {
00234 $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00235 }
00236 return $ret;
00237 }
00238
00247 protected function _addHost( $sUrl, & $aHosts )
00248 {
00249 if ( $sUrl && ( $sHost = @parse_url( $sUrl, PHP_URL_HOST ) ) ) {
00250 if ( !in_array( $sHost, $aHosts ) ) {
00251 $aHosts[] = $sHost;
00252 }
00253 }
00254 }
00255
00261 protected function _getHosts()
00262 {
00263 if ( $this->_aHosts === null ) {
00264 $this->_aHosts = array();
00265 $oConfig = $this->getConfig();
00266
00267
00268 $this->_addHost( $oConfig->getConfigParam( "sMallShopURL" ), $this->_aHosts );
00269 $this->_addHost( $oConfig->getConfigParam( "sMallSSLShopURL" ), $this->_aHosts );
00270
00271
00272 $this->_addHost( $oConfig->getConfigParam( 'aLanguageURLs' ), $this->_aHosts );
00273 $this->_addHost( $oConfig->getConfigParam( 'aLanguageSSLURLs' ), $this->_aHosts );
00274
00275 if ( !count( $aHosts ) && !$oConfig->mustAddShopIdToRequest() ) {
00276
00277 $this->_addHost( $oConfig->getConfigParam( "sShopURL" ), $this->_aHosts );
00278 $this->_addHost( $oConfig->getConfigParam( "sSSLShopURL" ), $this->_aHosts );
00279 }
00280
00281 if ( $this->isAdmin() ) {
00282 $this->_addHost( $oConfig->getConfigParam( "sAdminSSLURL" ), $this->_aHosts );
00283 }
00284 }
00285
00286 return $this->_aHosts;
00287 }
00288
00296 public function isCurrentShopHost( $sUrl )
00297 {
00298 $blCurrent = false;
00299 if ( $sUrl ) {
00300
00301 $sUrlHost = @parse_url( $sUrl, PHP_URL_HOST );
00302
00303
00304 $aHosts = $this->_getHosts();
00305
00306 foreach ( $aHosts as $sHost ) {
00307 if ( $sHost === $sUrlHost ) {
00308 $blCurrent = true;
00309 break;
00310 }
00311 }
00312 }
00313
00314 return $blCurrent;
00315 }
00316
00324 public function processSeoUrl( $sUrl )
00325 {
00326
00327 if ( !$this->isAdmin() ) {
00328 $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00329 }
00330
00331 $sUrl = $this->cleanUrlParams($sUrl);
00332 return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00333 }
00334
00343 public function cleanUrlParams($sUrl, $sConnector = '&')
00344 {
00345 $aUrlParts = explode('?', $sUrl);
00346
00347
00348 if (
00349 !is_array($aUrlParts)
00350 || count($aUrlParts) != 2
00351 ) {
00352 return $sUrl;
00353 }
00354
00355 $sUrl = $aUrlParts[0];
00356 $sUrlParams = $aUrlParts[1];
00357
00358 $oStrUtils = getStr();
00359 $sUrlParams = $oStrUtils->preg_replace(
00360 array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00361 array('&', '&', '?'),
00362 $sUrlParams
00363 );
00364
00365
00366 parse_str($sUrlParams, $aUrlParams);
00367 $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
00368
00369
00370 $sUrl = str_replace(
00371 array('%5B', '%5D'),
00372 array('[', ']'),
00373 $sUrl
00374 );
00375
00376 return $sUrl;
00377 }
00378
00386 public function appendParamSeparator($sUrl)
00387 {
00388 $oStr = getStr();
00389 if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00390
00391 return $sUrl;
00392 }
00393 if ( $oStr->strpos($sUrl, '?') === false ) {
00394 return $sUrl.'?';
00395 }
00396 return $sUrl.'&';
00397 }
00398 }