Source for file Contenido_UrlBuilder_MR.class.php

Documentation is available at Contenido_UrlBuilder_MR.class.php

  1. <?php
  2. /**
  3.  * Project:
  4.  * Contenido Content Management System
  5.  *
  6.  * Description:
  7.  * Implementation of Contenido_UrlBuilder to build AMR (Advanced Mod Rewrite) Frontend-URLs
  8.  *
  9.  * Requirements:
  10.  * @con_php_req 5.0
  11.  *
  12.  * @package     Contenido
  13.  * @subpackage  ModRewrite
  14.  * @version     0.4
  15.  * @author      Murat Purc
  16.  * @copyright   © Murat Purc 2008
  17.  * @license     http://www.contenido.org/license/LIZENZ.txt
  18.  * @link        http://www.4fb.de
  19.  * @link        http://www.contenido.org
  20.  */
  21.  
  22.  
  23. defined('CON_FRAMEWORK'or die('Illegal call');
  24.  
  25.  
  26. cInclude('classes''UrlBuilder/Contenido_UrlBuilder.class.php');
  27. cInclude('classes''UrlBuilder/Contenido_UrlBuilderFactory.class.php');
  28.  
  29.  
  30. /**
  31.  * Class to build frontend urls for advandced mod rewrite plugin.
  32.  * Extends abstract Contenido_UrlBuilder class and implements singleton pattern.
  33.  *
  34.  * Usage:
  35.  * <code>
  36.  * cInclude('classes', 'UrlBuilder/Contenido_UrlBuilder_MR.class.php');
  37.  * $url = 'front_content.php?idart=123';
  38.  * $mrUrlBuilder = Contenido_UrlBuilder_MR::getInstance();
  39.  * $mrUrlBuilder->buildUrl(array($url));
  40.  * $newUrl = $mrUrlBuilder->getUrl();
  41.  * </code>
  42.  *
  43.  * @todo  Add handling of absolute paths, standardize handling of fragments
  44.  *
  45.  *
  46.  * @author      Murat Purc <murat@purc.de>
  47.  * @copyright   © Murat Purc 2008
  48.  * @package     Contenido
  49.  * @subpackage  ModRewrite
  50.  */
  51. class Contenido_UrlBuilder_MR extends Contenido_UrlBuilder {
  52.  
  53.     /**
  54.      * Self instance
  55.      *
  56.      * @var  Contenido_UrlBuilder_MR 
  57.      */
  58.     static private $_instance;
  59.  
  60.     /**
  61.      * Debugger instance
  62.      *
  63.      * @var  Contenido_mpDebug 
  64.      */
  65.     private $_oDebug;
  66.  
  67.     /**
  68.      * Ampersant used for composing several parameter value pairs
  69.      *
  70.      * @var  string 
  71.      */
  72.     private $_sAmp = '&amp;';
  73.  
  74.     /**
  75.      * Is XHTML output?
  76.      *
  77.      * @var  bool 
  78.      */
  79.     private $_bIsXHTML = false;
  80.  
  81.     /**
  82.      * Is mod rewrite enabled?
  83.      *
  84.      * @var  bool 
  85.      */
  86.     private $_bMREnabled = false;
  87.  
  88.     /**
  89.      * Mod Rewrite configuration
  90.      *
  91.      * @var  array 
  92.      */
  93.     private $_aMrCfg = null;
  94.  
  95.  
  96.     /**
  97.      * Constructor, tries to set some member variables.
  98.      */
  99.     private function __construct({
  100.         $this->sHttpBasePath '';
  101.         if (ModRewrite::isEnabled()) {
  102.             $this->_oDebug     = Contenido_mpDebug::getInstance();
  103.             $this->_aMrCfg     = ModRewrite::getConfig();
  104.             $this->_bMREnabled = true;
  105.             $this->_bIsXHTML   = (getEffectiveSetting('generator''xhtml''false'== 'false'false true;
  106.             $this->_sAmp       = ($this->_bIsXHTML'&' '&amp;';
  107.         }
  108.     }
  109.  
  110.  
  111.     /**
  112.      * Returns a instance of Contenido_UrlBuilder_MR
  113.      *
  114.      * @return  Contenido_UrlBuilder_MR 
  115.      */
  116.     public static function getInstance({
  117.         if (self::$_instance == null{
  118.             self::$_instance new Contenido_UrlBuilder_MR();
  119.         }
  120.         return self::$_instance;
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Builds a URL based on defined mod rewrite settings.
  126.      *
  127.      * @param   array    $params  Parameter array, provides only following parameters:
  128.      *  <code>
  129.      *  $params[0] = 'front_content.php?idart=123...'
  130.      *  </code>
  131.      * @param   boolean  $bUseAbsolutePath  Flag to use absolute path (not used at the moment)
  132.      * @return  string   New build url
  133.      */
  134.     public function buildUrl(array $params$bUseAbsolutePath=false{
  135.         $this->_oDebug->addDebug($params'Contenido_UrlBuilder_MR::buildUrl() $params');
  136.         $urlDebug['in'$params;
  137.  
  138.         $url self::_buildUrl($params);
  139.  
  140.         $urlPrefix '';
  141.         if ($bUseAbsolutePath{
  142.             $cfgPath 'cfgClient/' mpGlobals::getInstance()->get('client''/path/htmlpath';
  143.             $hmlPath mpGlobals::getInstance()->get($cfgPath);
  144.             $aComp   parse_url($hmlPath);
  145.             $urlPrefix $aComp['scheme''://' $aComp['host'];
  146.         }
  147.  
  148.         $this->sUrl $urlPrefix $url;
  149.  
  150.         $urlDebug['out'$this->sUrl;
  151.         $this->_oDebug->addDebug($urlDebug'Contenido_UrlBuilder_MR::buildUrl() in -> out');
  152.     }
  153.  
  154.  
  155.     /**
  156.      * Builds the SEO-URL by analyzing passed arguments (parameter value pairs)
  157.      *
  158.      * @param   array   $aParams  Parameter array
  159.      * @return  string  New build pretty url
  160.      */
  161.     private function _buildUrl(array $aParams{
  162.  
  163.         // language should changed, set lang parameter
  164.         if (isset($aParams['changelang'])) 
  165.             $aParams['lang'$aParams['changelang']
  166.         
  167.  
  168.         // build the query
  169.         $sQuery http_build_query($aParams);
  170.  
  171.         // get pretty url parts
  172.         $oMRUrlStack ModRewriteUrlStack::getInstance();
  173.         $aPretty $oMRUrlStack->getPrettyUrlParts('front_content.php?' $sQuery);
  174.  
  175.         // get all non contenido related query parameter
  176.         $sQuery $this->_createUrlQueryPart($aParams);
  177.  
  178.         // some presettings of variables
  179.         $aParts array();
  180.  
  181.         // add client id/name if desired
  182.         if ($param $this->_getClientParameter($aParams)) {
  183.             $aParts[$param;
  184.         }
  185.  
  186.         // add language id/name if desired
  187.         if ($param $this->_getLanguageParameter($aParams)) {
  188.             $aParts[$param;
  189.         }
  190.  
  191.         // get path part of the url
  192.         $sPath $this->_getPath($aPretty);
  193.         if ($sPath !== ''{
  194.             $aParts[$sPath;
  195.         }
  196.         $sPath implode('/'$aParts'/';
  197.  
  198.         // get pagename part of the url
  199.         $sArticle $this->_getArticleName($aPretty$aParams);
  200.  
  201.         if ($sArticle !== ''{
  202.             $sFileExt $this->_aMrCfg['file_extension'];
  203.         else {
  204.             $sFileExt '';
  205.         }
  206.  
  207.         $sPathAndArticle $sPath $sArticle $sFileExt;
  208.         if ($this->_aMrCfg['use_lowercase_uri'== 1{
  209.             // use lowercase url
  210.             $sPathAndArticle strtolower($sPathAndArticle);
  211.         }
  212.  
  213. //        $sUrl = $this->_aMrCfg['rootdir'] . $sPathAndArticle . $sQuery;
  214.         $sUrl $sPathAndArticle $sQuery;
  215.  
  216.         // remove double or more join parameter
  217.         $sUrl mr_removeMultipleChars('/'$sUrl);
  218.         if (substr($sUrl-2== '?='{
  219.             $sUrl substr_replace($sUrl''-2);
  220.         }
  221.  
  222.         // now convert Contenido url to an AMR url
  223.         $sUrl ModRewriteUrlUtil::getInstance()->toModRewriteUrl($sUrl);
  224.  
  225.         return mr_removeMultipleChars('/'$this->_aMrCfg['rootdir'$sUrl);
  226.     }
  227.  
  228.  
  229.     /**
  230.      * Loops thru passed parameter array and creates the query part of the URL.
  231.      * All non Contenido related parameter will be excluded from composition.
  232.      *
  233.      * @param   array  $aArgs  Assoziative parameter array
  234.      * @return  string  Composed query part for the URL like '?foo=bar&amp;param=value'
  235.      */
  236.     private function _createUrlQueryPart(array $aArgs){
  237.         // set list of parameter which are to ignore while setting additional parameter
  238.         $aIgnoredParams array(
  239.             'idcat''idart''lang''client''idcatart''idartlang'
  240.         );
  241.         if ($this->_aMrCfg['use_language'== 1{
  242.             $aIgnoredParams['changelang';
  243.         }
  244.         if ($this->_aMrCfg['use_client'== 1{
  245.             $aIgnoredParams['changeclient';
  246.         }
  247.  
  248.         // collect additional non contenido related parameters
  249.         $sQuery '';
  250.         foreach ($aArgs as $p => $v{
  251.             if (!in_array($p$aIgnoredParams)) {
  252.                 $sQuery .= urlencode(urldecode($p)) '=' urlencode(urldecode($v)) $this->_sAmp;
  253.             }
  254.         }
  255.         if (strlen($sQuery0{
  256.             $sQuery '?' substr($sQuery0strlen($this->_sAmp));
  257.         }
  258.         return $sQuery;
  259.     }
  260.  
  261.  
  262.     /**
  263.      * Returns client id or name depending on settings.
  264.      *
  265.      * @param   array   $aArgs     Additional arguments
  266.      * @return  mixed   Client id, client name or null
  267.      */
  268.     private function _getClientParameter(array $aArgs{
  269.         // set client if desired
  270.         if ($this->_aMrCfg['use_client'== 1{
  271.             $iChangeClient (isset($aArgs['changeclient'])) ? (int) $aArgs['changeclient'0;
  272.             $idclient      ($iChangeClient 0$iChangeClient $GLOBALS['client'];
  273.             if ($this->_aMrCfg['use_client_name'== 1{
  274.                 return urlencode(ModRewrite::getClientName($idclient));
  275.             else {
  276.                 return $idclient;
  277.             }
  278.         }
  279.         return null;
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Returns language id or name depending on settings.
  285.      *
  286.      * @param   array   $aArgs     Additional arguments
  287.      * @return  mixed   Language id, language name or null
  288.      */
  289.     private function _getLanguageParameter(array $aArgs{
  290.         // set language if desired
  291.         if ($this->_aMrCfg['use_language'== 1{
  292.             $iChangeLang (isset($aArgs['changelang'])) ? (int) $aArgs['changelang'0;
  293.             $idlang      ($iChangeLang 0$iChangeLang $GLOBALS['lang'];
  294.             if ($this->_aMrCfg['use_language_name'== 1{
  295.                 return urlencode(ModRewrite::getLanguageName($idlang));
  296.             else {
  297.                 return $idlang;
  298.             }
  299.         }
  300.         return null;
  301.     }
  302.  
  303.  
  304.     /**
  305.      * Returns composed path of url (normally the category structure)
  306.      *
  307.      * @param   array   $aPretty   Pretty url array
  308.      * @return  string  Path
  309.      */
  310.     private function _getPath(array $aPretty{
  311.         $sPath (isset($aPretty['urlpath'])) $aPretty['urlpath''';
  312.  
  313.         // check start directory settings
  314.         if ($this->_aMrCfg['startfromroot'== && (strlen($sPath0)) {
  315.             // splitt string in array
  316.             $aCategories explode('/'$sPath);
  317.  
  318.             // remove first category
  319.             array_shift($aCategories);
  320.  
  321.             // implode array with categories to new string
  322.             $sPath implode('/'$aCategories);
  323.         }
  324.  
  325.         return $sPath;
  326.     }
  327.  
  328.  
  329.     /**
  330.      * Returns articlename depending on current setting
  331.      *
  332.      * @param   array   $aPretty   Pretty url array
  333.      * @param   array   $aArgs     Additional arguments
  334.      * @return  string  Articlename
  335.      */
  336.     private function _getArticleName(array $aPrettyarray $aArgs{
  337.  
  338.         $sArticle   (isset($aPretty['urlname'])) $aPretty['urlname''';
  339.  
  340.         $iIdCat     (isset($aArgs['idcat'])) ? (int) $aArgs['idcat'0;
  341.         $iIdCatLang (isset($aArgs['idcatlang'])) ? (int) $aArgs['idcatlang'0;
  342.         $iIdCatArt  (isset($aArgs['idcatart'])) ? (int) $aArgs['idcatart'0;
  343.         $iIdArt     (isset($aArgs['idart'])) ? (int) $aArgs['idart'0;
  344.         $iIdArtLang (isset($aArgs['idartlang'])) ? (int) $aArgs['idartlang'0;
  345.  
  346.         // category id was passed but not article id
  347.         if (($iIdCat || $iIdCatLang 0&& $iIdCatArt == && $iIdArt == && $iIdArtLang == 0{
  348.             $sArticle '';
  349.             if ($this->_aMrCfg['add_startart_name_to_url']{
  350.                 if ($this->_aMrCfg['default_startart_name'!== ''{
  351.                     // use default start article name
  352.                     $sArticle $this->_aMrCfg['default_startart_name'];
  353.                 else {
  354.                     $sArticle (isset($aPretty['urlname'])) $aPretty['urlname''';
  355.                 }
  356.             else {
  357.                 // url is to create without article name
  358.                 $sArticle '';
  359.             }
  360.         }
  361.  
  362.         return $sArticle;
  363.     }
  364.  
  365.  
  366. }

Documentation generated on Sun, 08 Feb 2009 22:00:50 +0100 by phpDocumentor 1.4.1