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 if ( oxUtils::getInstance()->seoIsActive() ) {
00091 return $sUrl;
00092 }
00093
00094 $oStr = getStr();
00095 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00096
00097 if ($qpos = $oStr->strpos($sUrl, '?')) {
00098 if ($qpos == $oStr->strlen($sUrl)-1) {
00099 $sSep = '';
00100 } else {
00101 $sSep = '&';
00102 }
00103 } else {
00104 $sSep = '?';
00105 }
00106
00107 if ( !$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00108 $sUrl .= "{$sSep}lang=".oxLang::getInstance()->getBaseLanguage();
00109 $sSep = '&';
00110 }
00111
00112 $oConfig = $this->getConfig();
00113 if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00114 $iCur = (int) $oConfig->getShopCurrency();
00115 if ( $iCur ) {
00116 $sUrl .= "{$sSep}cur=".$iCur;
00117 $sSep = '&';
00118 }
00119 }
00120
00121 return $sUrl;
00122 }
00123
00132 public function prepareCanonicalUrl( $sUrl )
00133 {
00134 $oConfig = $this->getConfig();
00135 $oStr = getStr();
00136
00137
00138 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00139 $sUrl = $oStr->preg_replace( '/(&|\?)$/', '', $sUrl );
00140 $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&';
00141
00142
00143 if ( !oxUtils::getInstance()->seoIsActive() ) {
00144
00145 $iLang = oxLang::getInstance()->getBaseLanguage();
00146 if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
00147 $sUrl .= "{$sSep}lang=".$iLang;
00148 }
00149 }
00150
00151 return $sUrl;
00152 }
00153
00162 public function appendUrl( $sUrl, $aAddParams )
00163 {
00164 $oStr = getStr();
00165 $sSep = '&';
00166 if ( $oStr->strpos( $sUrl, '?' ) === false ) {
00167 $sSep = '?';
00168 }
00169
00170 if ( count( $aAddParams ) ) {
00171 foreach ( $aAddParams as $sName => $sValue ) {
00172 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00173 $sUrl .= $sSep . $sName . "=" . $sValue;
00174 $sSep = '&';
00175 }
00176 }
00177 }
00178 return $sUrl ? $sUrl.$sSep : '';
00179 }
00180
00189 public function cleanUrl( $sUrl, $aParams = null )
00190 {
00191 $oStr = getStr();
00192 if ( is_array( $aParams ) ) {
00193 foreach ( $aParams as $sParam ) {
00194 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00195 }
00196 } else {
00197 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00198 }
00199
00200 return trim( $sUrl, "?" );
00201 }
00202
00213 public function processUrl( $sUrl, $blFinalUrl = true, $aParams = null, $iLang = null )
00214 {
00215 $aAddParams = $this->getAddUrlParams();
00216 if ( is_array($aParams) && count( $aParams ) ) {
00217 $aAddParams = array_merge( $aAddParams, $aParams );
00218 }
00219
00220 $ret = oxSession::getInstance()->processUrl(
00221 oxLang::getInstance()->processUrl(
00222 $this->appendUrl(
00223 $sUrl,
00224 $aAddParams
00225 ),
00226 $iLang
00227 )
00228 );
00229
00230 if ($blFinalUrl) {
00231 $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00232 }
00233 return $ret;
00234 }
00235
00244 protected function _addHost( $sUrl, & $aHosts )
00245 {
00246 if ( $sUrl && ( $sHost = @parse_url( $sUrl, PHP_URL_HOST ) ) ) {
00247 if ( !in_array( $sHost, $aHosts ) ) {
00248 $aHosts[] = $sHost;
00249 }
00250 }
00251 }
00252
00258 protected function _getHosts()
00259 {
00260 if ( $this->_aHosts === null ) {
00261 $this->_aHosts = array();
00262 $oConfig = $this->getConfig();
00263
00264
00265 $this->_addHost( $oConfig->getConfigParam( "sMallShopURL" ), $this->_aHosts );
00266 $this->_addHost( $oConfig->getConfigParam( "sMallSSLShopURL" ), $this->_aHosts );
00267
00268
00269 $this->_addHost( $oConfig->getConfigParam( 'aLanguageURLs' ), $this->_aHosts );
00270 $this->_addHost( $oConfig->getConfigParam( 'aLanguageSSLURLs' ), $this->_aHosts );
00271
00272 if ( !count( $aHosts ) && !$oConfig->mustAddShopIdToRequest() ) {
00273
00274 $this->_addHost( $oConfig->getConfigParam( "sShopURL" ), $this->_aHosts );
00275 $this->_addHost( $oConfig->getConfigParam( "sSSLShopURL" ), $this->_aHosts );
00276 }
00277
00278 if ( $this->isAdmin() ) {
00279 $this->_addHost( $oConfig->getConfigParam( "sAdminSSLURL" ), $this->_aHosts );
00280 }
00281 }
00282
00283 return $this->_aHosts;
00284 }
00285
00293 public function isCurrentShopHost( $sUrl )
00294 {
00295 $blCurrent = false;
00296 if ( $sUrl ) {
00297
00298 $sUrlHost = @parse_url( $sUrl, PHP_URL_HOST );
00299
00300
00301 $aHosts = $this->_getHosts();
00302
00303 foreach ( $aHosts as $sHost ) {
00304 if ( $sHost === $sUrlHost ) {
00305 $blCurrent = true;
00306 break;
00307 }
00308 }
00309 }
00310
00311 return $blCurrent;
00312 }
00313
00321 public function processSeoUrl( $sUrl )
00322 {
00323
00324 if ( !$this->isAdmin() ) {
00325 $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00326 }
00327
00328 $sUrl = $this->cleanUrlParams($sUrl);
00329 return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00330 }
00331
00340 public function cleanUrlParams($sUrl, $sConnector = '&')
00341 {
00342 $aUrlParts = explode('?', $sUrl);
00343
00344
00345 if (
00346 !is_array($aUrlParts)
00347 || count($aUrlParts) != 2
00348 ) {
00349 return $sUrl;
00350 }
00351
00352 $sUrl = $aUrlParts[0];
00353 $sUrlParams = $aUrlParts[1];
00354
00355 $oStrUtils = getStr();
00356 $sUrlParams = $oStrUtils->preg_replace(
00357 array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00358 array('&', '&', '?'),
00359 $sUrlParams
00360 );
00361
00362
00363 parse_str($sUrlParams, $aUrlParams);
00364 $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
00365
00366
00367 $sUrl = str_replace(
00368 array('%5B', '%5D'),
00369 array('[', ']'),
00370 $sUrl
00371 );
00372
00373 return $sUrl;
00374 }
00375
00383 public function appendParamSeparator($sUrl)
00384 {
00385 $oStr = getStr();
00386 if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00387
00388 return $sUrl;
00389 }
00390 if ( $oStr->strpos($sUrl, '?') === false ) {
00391 return $sUrl.'?';
00392 }
00393 return $sUrl.'&';
00394 }
00395 }