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 
00027     public static function getInstance()
00028     {
00029         // disable caching for test modules
00030         if ( defined( 'OXID_PHP_UNIT' ) ) {
00031             self::$_instance = modInstances::getMod( __CLASS__ );
00032         }
00033 
00034         if ( !(self::$_instance instanceof oxUtilsUrl ) ) {
00035             self::$_instance = oxNew( 'oxUtilsUrl' );
00036             if ( defined( 'OXID_PHP_UNIT' ) ) {
00037                 modInstances::addMod( __CLASS__, self::$_instance);
00038             }
00039         }
00040         return self::$_instance;
00041     }
00042 
00048     public function getBaseAddUrlParams()
00049     {
00050         $aAddUrlParams = array();
00051 
00052         return $aAddUrlParams;
00053     }
00054 
00060     public function getAddUrlParams()
00061     {
00062         if ( $this->_aAddUrlParams === null ) {
00063             $this->_aAddUrlParams = $this->getBaseAddUrlParams();
00064 
00065             // appending currency
00066             if ( ( $iCur = $this->getConfig()->getShopCurrency() ) ) {
00067                 $this->_aAddUrlParams['cur'] = $iCur;
00068             }
00069         }
00070         return $this->_aAddUrlParams;
00071     }
00072 
00082     public function prepareUrlForNoSession( $sUrl )
00083     {
00084         if ( oxUtils::getInstance()->seoIsActive() ) {
00085             return $sUrl;
00086         }
00087 
00088         $oStr = getStr();
00089         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
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=".oxLang::getInstance()->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 ( !oxUtils::getInstance()->seoIsActive() ) {
00138             // non seo url has no language identifier..
00139             $iLang = oxLang::getInstance()->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 = oxSession::getInstance()->processUrl(
00215                     oxLang::getInstance()->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 
00237     public function processSeoUrl( $sUrl )
00238     {
00239         $ret = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00240         $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00241         return $ret;
00242     }
00243 
00251     public function appendParamSeparator($sUrl)
00252     {
00253         $oStr = getStr();
00254         if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00255             // it is already ok
00256             return $sUrl;
00257         }
00258         if ( $oStr->strpos($sUrl, '?') === false ) {
00259             return $sUrl.'?';
00260         }
00261         return $sUrl.'&amp;';
00262     }
00263 }