oxutilsurl.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsUrl extends oxSuperCfg
00007 {
00008 
00014     protected $_aAddUrlParams = null;
00015 
00021     protected $_aHosts = null;
00022 
00028     public function getBaseAddUrlParams()
00029     {
00030         $aAddUrlParams = array();
00031 
00032         return $aAddUrlParams;
00033     }
00034 
00040     public function getAddUrlParams()
00041     {
00042         if ($this->_aAddUrlParams === null) {
00043             $this->_aAddUrlParams = $this->getBaseAddUrlParams();
00044 
00045             // appending currency
00046             if (($iCur = $this->getConfig()->getShopCurrency())) {
00047                 $this->_aAddUrlParams['cur'] = $iCur;
00048             }
00049         }
00050 
00051         return $this->_aAddUrlParams;
00052     }
00053 
00063     public function prepareUrlForNoSession($sUrl)
00064     {
00066         $oStr = getStr();
00067 
00068         // cleaning up session id..
00069         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00070         $sUrl = $oStr->preg_replace('/(&amp;|\?)$/', '', $sUrl);
00071 
00072         if (oxRegistry::getUtils()->seoIsActive()) {
00073             return $sUrl;
00074         }
00075 
00076         if ($qpos = $oStr->strpos($sUrl, '?')) {
00077             if ($qpos == $oStr->strlen($sUrl) - 1) {
00078                 $sSep = '';
00079             } else {
00080                 $sSep = '&amp;';
00081             }
00082         } else {
00083             $sSep = '?';
00084         }
00085 
00086         if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl)) {
00087             $sUrl .= "{$sSep}lang=" . oxRegistry::getLang()->getBaseLanguage();
00088             $sSep = '&amp;';
00089         }
00090 
00091         $oConfig = $this->getConfig();
00092         if (!$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00093             $iCur = (int) $oConfig->getShopCurrency();
00094             if ($iCur) {
00095                 $sUrl .= "{$sSep}cur=" . $iCur;
00096                 $sSep = '&amp;';
00097             }
00098         }
00099 
00100 
00101         return $sUrl;
00102     }
00103 
00112     public function prepareCanonicalUrl($sUrl)
00113     {
00114         $oConfig = $this->getConfig();
00116         $oStr = getStr();
00117 
00118         // cleaning up session id..
00119         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00120         $sUrl = $oStr->preg_replace('/(&amp;|\?)$/', '', $sUrl);
00121         $sSep = ($oStr->strpos($sUrl, '?') === false) ? '?' : '&amp;';
00122 
00123 
00124         if (!oxRegistry::getUtils()->seoIsActive()) {
00125             // non seo url has no language identifier..
00126             $iLang = oxRegistry::getLang()->getBaseLanguage();
00127             if (!$oStr->preg_match('/[&?](amp;)?lang=[0-9]+/i', $sUrl) &&
00128                 $iLang != $oConfig->getConfigParam('sDefaultLang')
00129             ) {
00130                 $sUrl .= "{$sSep}lang=" . $iLang;
00131             }
00132         }
00133 
00134         return $sUrl;
00135     }
00136 
00146     public function appendUrl($sUrl, $aAddParams, $blFinalUrl = false)
00147     {
00149         $oStr = getStr();
00150 
00151         $sSeparator = $this->_getUrlParametersSeparator($sUrl);
00152 
00153         if (is_array($aAddParams)) {
00154             foreach ($aAddParams as $sName => $sValue) {
00155                 if (isset($sValue) && !$oStr->preg_match("/\?(.*&(amp;)?)?" . preg_quote($sName) . "=/", $sUrl)) {
00156                     $sUrl .= $sSeparator . $sName . "=" . $sValue;
00157                     $sSeparator = '&amp;';
00158                 }
00159             }
00160         }
00161 
00162         if ($sUrl && !$blFinalUrl) {
00163             $sUrl .= $sSeparator;
00164         }
00165 
00166         return $sUrl;
00167     }
00168 
00177     public function cleanUrl($sUrl, $aParams = null)
00178     {
00180         $oStr = getStr();
00181         if (is_array($aParams)) {
00182             foreach ($aParams as $sParam) {
00183                 $sUrl = $oStr->preg_replace(
00184                     '/(\?|&(amp;)?)' . preg_quote($sParam) . '=[a-z0-9\.]+&?(amp;)?/i',
00185                     '\1',
00186                     $sUrl
00187                 );
00188             }
00189         } else {
00190             $sUrl = $oStr->preg_replace('/(\?|&(amp;)?).+/i', '\1', $sUrl);
00191         }
00192 
00193         return trim($sUrl, "?");
00194     }
00195 
00196 
00204     public function addShopHost($sUrl)
00205     {
00206         if (!preg_match("#^https?://#i", $sUrl)) {
00207             $sShopUrl = $this->getConfig()->getSslShopUrl();
00208             $sUrl = $sShopUrl . $sUrl;
00209         }
00210 
00211         return $sUrl;
00212     }
00213 
00224     public function processUrl($sUrl, $blFinalUrl = true, $aParams = null, $iLang = null)
00225     {
00226         $sUrl = $this->appendUrl($sUrl, $aParams, $blFinalUrl);
00227 
00228         if ($this->isCurrentShopHost($sUrl)) {
00229             $sUrl = $this->processShopUrl($sUrl, $blFinalUrl, $iLang);
00230         }
00231 
00232         return $sUrl;
00233     }
00234 
00244     public function processShopUrl($sUrl, $blFinalUrl = true, $iLang = null)
00245     {
00246         $aAddParams = $this->getAddUrlParams();
00247 
00248         $sUrl = $this->appendUrl($sUrl, $aAddParams, $blFinalUrl);
00249         $sUrl = oxRegistry::getLang()->processUrl($sUrl, $iLang);
00250         $sUrl = oxRegistry::getSession()->processUrl($sUrl);
00251 
00252         if ($blFinalUrl) {
00253             $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
00254         }
00255 
00256         return $sUrl;
00257     }
00258 
00266     public function isCurrentShopHost($sUrl)
00267     {
00268         $blCurrent = false;
00269         $sUrlHost = @parse_url($sUrl, PHP_URL_HOST);
00270         // checks if it is relative url.
00271         if (is_null($sUrlHost)) {
00272             $blCurrent = true;
00273         } else {
00274             $aHosts = $this->_getHosts();
00275 
00276             foreach ($aHosts as $sHost) {
00277                 if ($sHost === $sUrlHost) {
00278                     $blCurrent = true;
00279                     break;
00280                 }
00281             }
00282         }
00283 
00284         return $blCurrent;
00285     }
00286 
00294     public function processSeoUrl($sUrl)
00295     {
00296 
00297         if (!$this->isAdmin()) {
00298             $sUrl = $this->getSession()->processUrl($this->appendUrl($sUrl, $this->getAddUrlParams()));
00299         }
00300 
00301         $sUrl = $this->cleanUrlParams($sUrl);
00302 
00303         return getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $sUrl);
00304     }
00305 
00314     public function cleanUrlParams($sUrl, $sConnector = '&amp;')
00315     {
00316         $aUrlParts = explode('?', $sUrl);
00317 
00318         // check for params part
00319         if (!is_array($aUrlParts) || count($aUrlParts) != 2) {
00320             return $sUrl;
00321         }
00322 
00323         $sUrl = $aUrlParts[0];
00324         $sUrlParams = $aUrlParts[1];
00325 
00327         $oStrUtils = getStr();
00328         $sUrlParams = $oStrUtils->preg_replace(
00329             array('@(\&(amp;){1,})@ix', '@\&{1,}@', '@\?&@x'),
00330             array('&', '&', '?'),
00331             $sUrlParams
00332         );
00333 
00334         // remove duplicate entries
00335         parse_str($sUrlParams, $aUrlParams);
00336         $sUrl .= '?' . http_build_query($aUrlParams, '', $sConnector);
00337 
00338         // replace brackets
00339         $sUrl = str_replace(
00340             array('%5B', '%5D'),
00341             array('[', ']'),
00342             $sUrl
00343         );
00344 
00345         return $sUrl;
00346     }
00347 
00355     public function appendParamSeparator($sUrl)
00356     {
00358         $oStr = getStr();
00359         if ($oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl)) {
00360             // it is already ok
00361             return $sUrl;
00362         }
00363         if ($oStr->strpos($sUrl, '?') === false) {
00364             return $sUrl . '?';
00365         }
00366 
00367         return $sUrl . '&amp;';
00368     }
00369 
00375     public function getCurrentUrl()
00376     {
00377         $oUtilsServer = oxRegistry::get("oxUtilsServer");
00378 
00379         $aServerParams["HTTPS"] = $oUtilsServer->getServerVar("HTTPS");
00380         $aServerParams["HTTP_X_FORWARDED_PROTO"] = $oUtilsServer->getServerVar("HTTP_X_FORWARDED_PROTO");
00381         $aServerParams["HTTP_HOST"] = $oUtilsServer->getServerVar("HTTP_HOST");
00382         $aServerParams["REQUEST_URI"] = $oUtilsServer->getServerVar("REQUEST_URI");
00383 
00384         $sProtocol = "http://";
00385 
00386         if (isset($aServerParams['HTTPS']) && (($aServerParams['HTTPS'] == 'on' || $aServerParams['HTTPS'] == 1))
00387             || (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] == 'https')
00388         ) {
00389             $sProtocol = 'https://';
00390         }
00391 
00392         $sUrl = $sProtocol . $aServerParams['HTTP_HOST'] . $aServerParams['REQUEST_URI'];
00393 
00394         return $sUrl;
00395     }
00396 
00406     public function stringToParamsArray($sValue)
00407     {
00408         // url building
00409         // replace possible ampersands, explode, and filter out empty values
00410         $sValue = str_replace("&amp;", "&", $sValue);
00411         $aNavParams = explode("&", $sValue);
00412         $aNavParams = array_filter($aNavParams);
00413         $aParams = array();
00414         foreach ($aNavParams as $sValue) {
00415             $exp = explode("=", $sValue);
00416             $aParams[$exp[0]] = $exp[1];
00417         }
00418 
00419         return $aParams;
00420     }
00421 
00428     protected function _addHost($sUrl, &$aHosts)
00429     {
00430         if ($sUrl && ($sHost = @parse_url($sUrl, PHP_URL_HOST))) {
00431             if (!in_array($sHost, $aHosts)) {
00432                 $aHosts[] = $sHost;
00433             }
00434         }
00435     }
00436     
00443     protected function _addLanguageHost($aLanguageUrls, & $aHosts)
00444     {
00445         $iLanguageId = oxRegistry::getLang()->getBaseLanguage();
00446 
00447         if (isset($aLanguageUrls[$iLanguageId])) {
00448             $this->_addHost($aLanguageUrls[$iLanguageId], $aHosts);
00449         }
00450     }
00451 
00457     protected function _getHosts()
00458     {
00459         if ($this->_aHosts === null) {
00460             $this->_aHosts = array();
00461             $oConfig = $this->getConfig();
00462 
00463             $this->_addMallHosts($this->_aHosts);
00464 
00465             // language url
00466             $this->_addLanguageHost($oConfig->getConfigParam('aLanguageURLs'), $this->_aHosts);
00467             $this->_addLanguageHost($oConfig->getConfigParam('aLanguageSSLURLs'), $this->_aHosts);
00468 
00469             // current url
00470             $this->_addHost($oConfig->getConfigParam("sShopURL"), $this->_aHosts);
00471             $this->_addHost($oConfig->getConfigParam("sSSLShopURL"), $this->_aHosts);
00472 
00473             if ($this->isAdmin()) {
00474                 $this->_addHost($oConfig->getConfigParam("sAdminSSLURL"), $this->_aHosts);
00475             }
00476         }
00477 
00478         return $this->_aHosts;
00479     }
00480 
00486     protected function _addMallHosts(& $aHosts)
00487     {
00488     }
00489 
00497     private function _getUrlParametersSeparator($sUrl)
00498     {
00500         $oStr = getStr();
00501 
00502         $sSeparator = '';
00503         if ($oStr->strpos($sUrl, '?') === false) {
00504             $sSeparator = '?';
00505         } else {
00506             if ($sSeparator === '' && !$oStr->preg_match("/(\?|&(amp;)?)$/", $sUrl)) {
00507                 $sSeparator = '&amp;';
00508             }
00509         }
00510 
00511         return $sSeparator;
00512     }
00513 }