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 = oxConfig::getParameter( 'currency' ) ) ) {
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         if ( !$oStr->preg_match('/[&?](amp;)?cur=[0-9]+/i', $sUrl)) {
00107             $iCur = (int) oxConfig::getParameter('currency');
00108             if ( $iCur ) {
00109                 $sUrl .= "{$sSep}cur=".$iCur;
00110                 $sSep = '&amp;';
00111             }
00112         }
00113 
00114         return $sUrl;
00115     }
00116 
00125     public function prepareCanonicalUrl( $sUrl )
00126     {
00127         $oConfig = $this->getConfig();
00128         $oStr = getStr();
00129 
00130         // cleaning up session id..
00131         $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)(force_)?(admin_)?sid=[a-z0-9\._]+&?(amp;)?/i', '\1', $sUrl);
00132         $sUrl = $oStr->preg_replace( '/(&amp;|\?)$/', '', $sUrl );
00133         $sSep = ( $oStr->strpos( $sUrl, '?' ) === false ) ? '?' : '&amp;';
00134 
00135 
00136         if ( !oxUtils::getInstance()->seoIsActive() ) {
00137             // non seo url has no language identifier..
00138             $iLang = oxLang::getInstance()->getBaseLanguage();
00139             if ( !$oStr->preg_match( '/[&?](amp;)?lang=[0-9]+/i', $sUrl ) && $iLang != $oConfig->getConfigParam( 'sDefaultLang' ) ) {
00140                 $sUrl .= "{$sSep}lang=".$iLang;
00141             }
00142         }
00143 
00144         return $sUrl;
00145     }
00146 
00155     public function appendUrl( $sUrl, $aAddParams )
00156     {
00157         $oStr = getStr();
00158         $sSep = '&amp;';
00159         if ( $oStr->strpos( $sUrl, '?' ) === false ) {
00160             $sSep = '?';
00161         }
00162 
00163         if ( count( $aAddParams ) ) {
00164             foreach ( $aAddParams as $sName => $sValue ) {
00165                 if ( isset( $sValue ) && !$oStr->preg_match("/\?(.*&(amp;)?)?".preg_quote( $sName )."=/", $sUrl ) ) {
00166                     $sUrl .= $sSep . $sName . "=" . $sValue;
00167                     $sSep = '&amp;';
00168                 }
00169             }
00170         }
00171         return $sUrl ? $sUrl.$sSep : '';
00172     }
00173 
00185     protected function _appendUrl( $sUrl, $aAddParams )
00186     {
00187         return $this->appendUrl( $sUrl, $aAddParams );
00188     }
00189 
00198     public function cleanUrl( $sUrl, $aParams = null )
00199     {
00200         $oStr = getStr();
00201         if ( is_array( $aParams ) ) {
00202             foreach ( $aParams as $sParam ) {
00203                 $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?)'.preg_quote( $sParam ).'=[a-z0-9\.]+&?(amp;)?/i', '\1', $sUrl );
00204             }
00205         } else {
00206             $sUrl = $oStr->preg_replace( '/(\?|&(amp;)?).+/i', '\1', $sUrl );
00207         }
00208 
00209         return trim( $sUrl, "?" );
00210     }
00211 
00222     public function processUrl( $sUrl, $blFinalUrl = true, $aParams = null, $iLang = null )
00223     {
00224         $aAddParams = $this->getAddUrlParams();
00225         if ( is_array($aParams) && count( $aParams ) ) {
00226             $aAddParams = array_merge( $aAddParams, $aParams );
00227         }
00228 
00229         $ret = oxSession::getInstance()->processUrl(
00230                     oxLang::getInstance()->processUrl(
00231                         $this->appendUrl(
00232                                 $sUrl,
00233                                 $aAddParams
00234                         ),
00235                         $iLang
00236                     )
00237                 );
00238 
00239         if ($blFinalUrl) {
00240             $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00241         }
00242         return $ret;
00243     }
00244 
00252     public function processSeoUrl( $sUrl )
00253     {
00254         $ret = $this->getSession()->processUrl( $this->appendUrl( $sUrl, $this->getAddUrlParams() ) );
00255         $ret = getStr()->preg_replace('/(\?|&(amp;)?)$/', '', $ret);
00256         return $ret;
00257     }
00258 
00273     public function processStdUrl( $sUrl, $aParams, $iLang, $blAddLangParam )
00274     {
00275         return $this->processUrl($sUrl, true, $aParams, $iLang);
00276     }
00277 
00285     public function appendParamSeparator($sUrl)
00286     {
00287         $oStr = getStr();
00288         if ( $oStr->preg_match('/(\?|&(amp;)?)$/i', $sUrl ) ) {
00289             // it is already ok
00290             return $sUrl;
00291         }
00292         if ( $oStr->strpos($sUrl, '?') === false ) {
00293             return $sUrl.'?';
00294         }
00295         return $sUrl.'&amp;';
00296     }
00297 }