oxutilsurl.php

Go to the documentation of this file.
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             // appending currency
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         // cleaning up session id..
00084         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00085         $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $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 = '&amp;';
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 = '&amp;';
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 = '&amp;';
00112             }
00113         }
00114 
00115         return $sUrl;
00116     }
00117 
00126     public function prepareCanonicalUrl( $sUrl )
00127     {
00128         $oConfig = $this->getConfig();
00129         $oStr = getStr();
00130 
00131         // cleaning up session id..
00132         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00133         $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $sUrl );
00134         $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&amp;';
00135 
00136 
00137         if ( !oxRegistry::getUtils()->seoIsActive() ) {
00138             // non seo url has no language identifier..
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 = '&amp;';
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 = '&amp;';
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         $aAddParams = $this->getAddUrlParams();
00210         if ( is_array($aParams) && count( $aParams ) ) {
00211             $aAddParams = array_merge( $aAddParams, $aParams );
00212         }
00213 
00214         $ret = oxRegistry::getSession()->processUrl(
00215             oxRegistry::getLang()->processUrl(
00216                 $this->appendUrl(
00217                     $sUrl,
00218                     $aAddParams
00219                 ),
00220                 $iLang
00221             )
00222         );
00223 
00224         if ($blFinalUrl) {
00225             $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00226         }
00227         return $ret;
00228     }
00229 
00238     protected function _addHost( $sUrl, & $aHosts )
00239     {
00240         if ( $sUrl && ( $sHost = @parse_url( $sUrl, PHP_URL_HOST ) ) ) {
00241             if ( !in_array( $sHost, $aHosts ) ) {
00242                 $aHosts[] = $sHost;
00243             }
00244         }
00245     }
00246 
00252     protected function _getHosts()
00253     {
00254         if ( $this->_aHosts === null ) {
00255             $this->_aHosts = array();
00256             $oConfig = $this->getConfig();
00257 
00258             // mall (ssl) url
00259             $this->_addHost( $oConfig->getConfigParam( "sMallShopURL" ), $this->_aHosts );
00260             $this->_addHost( $oConfig->getConfigParam( "sMallSSLShopURL" ), $this->_aHosts );
00261 
00262             // language url
00263             $this->_addHost( $oConfig->getConfigParam( 'aLanguageURLs' ), $this->_aHosts );
00264             $this->_addHost( $oConfig->getConfigParam( 'aLanguageSSLURLs' ), $this->_aHosts );
00265 
00266             if ( !count( $aHosts ) && !$oConfig->mustAddShopIdToRequest() ) {
00267                 // current url
00268                 $this->_addHost( $oConfig->getConfigParam( "sShopURL" ), $this->_aHosts );
00269                 $this->_addHost( $oConfig->getConfigParam( "sSSLShopURL" ), $this->_aHosts );
00270             }
00271 
00272             if ( $this->isAdmin() ) {
00273                 $this->_addHost( $oConfig->getConfigParam( "sAdminSSLURL" ), $this->_aHosts );
00274             }
00275         }
00276 
00277         return $this->_aHosts;
00278     }
00279 
00287     public function isCurrentShopHost( $sUrl )
00288     {
00289         $blCurrent = false;
00290         if ( $sUrl ) {
00291             // host of given url
00292             $sUrlHost = @parse_url( $sUrl, PHP_URL_HOST );
00293 
00294             // configured hosts
00295             $aHosts = $this->_getHosts();
00296 
00297             foreach ( $aHosts as $sHost ) {
00298                 if ( $sHost === $sUrlHost ) {
00299                     $blCurrent = true;
00300                     break;
00301                 }
00302             }
00303         }
00304 
00305         return $blCurrent;
00306     }
00307 
00315     public function processSeoUrl( $sUrl )
00316     {
00317 
00318         if ( !$this->isAdmin() ) {
00319             $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00320         }
00321 
00322         $sUrl = $this->cleanUrlParams($sUrl);
00323         return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00324     }
00325 
00334     public function cleanUrlParams($sUrl, $sConnector = '&amp;')
00335     {
00336         $aUrlParts = explode('?', $sUrl);
00337 
00338         // check for params part
00339         if ( !is_array($aUrlParts) || count($aUrlParts) != 2) {
00340             return $sUrl;
00341         }
00342 
00343         $sUrl = $aUrlParts[0];
00344         $sUrlParams = $aUrlParts[1];
00345 
00346         $oStrUtils = getStr();
00347         $sUrlParams = $oStrUtils->preg_replace(
00348             array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00349             array('&', '&', '?'),
00350             $sUrlParams
00351         );
00352 
00353         // remove duplicate entries
00354         parse_str($sUrlParams, $aUrlParams);
00355         $sUrl .= '?'.http_build_query($aUrlParams, '', $sConnector);
00356 
00357         // replace brackets
00358         $sUrl = str_replace(
00359             array('%5B', '%5D'),
00360             array('[', ']'),
00361             $sUrl
00362         );
00363 
00364         return $sUrl;
00365     }
00366 
00374     public function appendParamSeparator($sUrl)
00375     {
00376         $oStr = getStr();
00377         if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00378             // it is already ok
00379             return $sUrl;
00380         }
00381         if ( $oStr->strpos($sUrl, '?') === false ) {
00382             return $sUrl.'?';
00383         }
00384         return $sUrl.'&amp;';
00385     }
00386 
00392     function getCurrentUrl()
00393     {
00394         $oUtilsServer = oxRegistry::get( "oxUtilsServer" );
00395 
00396         $aServerParams["HTTPS"]       = $oUtilsServer->getServerVar( "HTTPS" );
00397         $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar( "HTTP_X_FORWARDED_PROTO" );
00398         $aServerParams["HTTP_HOST"]   = $oUtilsServer->getServerVar( "HTTP_HOST" );
00399         $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar( "REQUEST_URI" );
00400 
00401         $sProtocol = "http://";
00402 
00403         if ( isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
00404             || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
00405         ) {
00406             $sProtocol = 'https://';
00407         }
00408 
00409         $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
00410 
00411         return $sUrl;
00412     }
00413 }