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         if ( oxRegistry::getUtils()->seoIsActive() ) {
00082             return $sUrl;
00083         }
00084 
00085         $oStr = getStr();
00086         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00087 
00088         if ($qpos = $oStr->strpos($sUrl, '?')) {
00089             if ($qpos == $oStr->strlen($sUrl)-1) {
00090                 $sSep = '';
00091             } else {
00092                 $sSep = '&amp;';
00093             }
00094         } else {
00095             $sSep = '?';
00096         }
00097 
00098         if ( !$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00099             $sUrl .= "{$sSep}lang=".oxRegistry::getLang()->getBaseLanguage();
00100             $sSep = '&amp;';
00101         }
00102 
00103         $oConfig = $this->getConfig();
00104         if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00105             $iCur = (int) $oConfig->getShopCurrency();
00106             if ( $iCur ) {
00107                 $sUrl .= "{$sSep}cur=".$iCur;
00108                 $sSep = '&amp;';
00109             }
00110         }
00111 
00112         return $sUrl;
00113     }
00114 
00123     public function prepareCanonicalUrl( $sUrl )
00124     {
00125         $oConfig = $this->getConfig();
00126         $oStr = getStr();
00127 
00128         // cleaning up session id..
00129         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00130         $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $sUrl );
00131         $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&amp;';
00132 
00133 
00134         if ( !oxRegistry::getUtils()->seoIsActive() ) {
00135             // non seo url has no language identifier..
00136             $iLang = oxRegistry::getLang()->getBaseLanguage();
00137             if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
00138                 $sUrl .= "{$sSep}lang=".$iLang;
00139             }
00140         }
00141 
00142         return $sUrl;
00143     }
00144 
00153     public function appendUrl( $sUrl, $aAddParams )
00154     {
00155         $oStr = getStr();
00156         $sSep = '&amp;';
00157         if ( $oStr->strpos( $sUrl, '?' ) === false ) {
00158             $sSep = '?';
00159         }
00160 
00161         if ( count( $aAddParams ) ) {
00162             foreach ( $aAddParams as $sName => $sValue ) {
00163                 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00164                     $sUrl .= $sSep . $sName . "=" . $sValue;
00165                     $sSep = '&amp;';
00166                 }
00167             }
00168         }
00169         return $sUrl ? $sUrl.$sSep : '';
00170     }
00171 
00180     public function cleanUrl( $sUrl, $aParams = null )
00181     {
00182         $oStr = getStr();
00183         if ( is_array( $aParams ) ) {
00184             foreach ( $aParams as $sParam ) {
00185                 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00186             }
00187         } else {
00188             $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00189         }
00190 
00191         return trim( $sUrl, "?" );
00192     }
00193 
00204     public function processUrl( $sUrl, $blFinalUrl = true, $aParams = null, $iLang = null )
00205     {
00206         $aAddParams = $this->getAddUrlParams();
00207         if ( is_array($aParams) && count( $aParams ) ) {
00208             $aAddParams = array_merge( $aAddParams, $aParams );
00209         }
00210 
00211         $ret = oxRegistry::getSession()->processUrl(
00212                     oxRegistry::getLang()->processUrl(
00213                         $this->appendUrl(
00214                                 $sUrl,
00215                                 $aAddParams
00216                         ),
00217                         $iLang
00218                     )
00219                 );
00220 
00221         if ($blFinalUrl) {
00222             $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00223         }
00224         return $ret;
00225     }
00226 
00235     protected function _addHost( $sUrl, & $aHosts )
00236     {
00237         if ( $sUrl && ( $sHost = @parse_url( $sUrl, PHP_URL_HOST ) ) ) {
00238             if ( !in_array( $sHost, $aHosts ) ) {
00239                 $aHosts[] = $sHost;
00240             }
00241         }
00242     }
00243 
00249     protected function _getHosts()
00250     {
00251         if ( $this->_aHosts === null ) {
00252             $this->_aHosts = array();
00253             $oConfig = $this->getConfig();
00254 
00255             // mall (ssl) url
00256             $this->_addHost( $oConfig->getConfigParam( "sMallShopURL" ), $this->_aHosts );
00257             $this->_addHost( $oConfig->getConfigParam( "sMallSSLShopURL" ), $this->_aHosts );
00258 
00259             // language url
00260             $this->_addHost( $oConfig->getConfigParam( 'aLanguageURLs' ), $this->_aHosts );
00261             $this->_addHost( $oConfig->getConfigParam( 'aLanguageSSLURLs' ), $this->_aHosts );
00262 
00263             if ( !count( $aHosts ) && !$oConfig->mustAddShopIdToRequest() ) {
00264                 // current url
00265                 $this->_addHost( $oConfig->getConfigParam( "sShopURL" ), $this->_aHosts );
00266                 $this->_addHost( $oConfig->getConfigParam( "sSSLShopURL" ), $this->_aHosts );
00267             }
00268 
00269             if ( $this->isAdmin() ) {
00270                 $this->_addHost( $oConfig->getConfigParam( "sAdminSSLURL" ), $this->_aHosts );
00271             }
00272         }
00273 
00274         return $this->_aHosts;
00275     }
00276 
00284     public function isCurrentShopHost( $sUrl )
00285     {
00286         $blCurrent = false;
00287         if ( $sUrl ) {
00288             // host of given url
00289             $sUrlHost = @parse_url( $sUrl, PHP_URL_HOST );
00290 
00291             // configured hosts
00292             $aHosts = $this->_getHosts();
00293 
00294             foreach ( $aHosts as $sHost ) {
00295                 if ( $sHost === $sUrlHost ) {
00296                     $blCurrent = true;
00297                     break;
00298                 }
00299             }
00300         }
00301 
00302         return $blCurrent;
00303     }
00304 
00312     public function processSeoUrl( $sUrl )
00313     {
00314 
00315         if ( !$this->isAdmin() ) {
00316             $sUrl = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00317         }
00318 
00319         $sUrl = $this->cleanUrlParams($sUrl);
00320         return getStr()->preg_replace( '/(\?|&(amp;)?)$/', '', $sUrl );
00321     }
00322 
00331     public function cleanUrlParams($sUrl, $sConnector = '&amp;')
00332     {
00333         $aUrlParts = explode('?', $sUrl);
00334 
00335         // check for params part
00336         if (
00337             !is_array($aUrlParts)
00338             || count($aUrlParts) != 2
00339         ) {
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 dublicate 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 }