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.         $sFullUrl $this->_aMrCfg['rootdir'$sPathAndArticle $sQuery;
  214.  
  215.         // remove double or more join parameter
  216.         $sFullUrl mr_removeMultipleChars('/'$sFullUrl);
  217.         if (substr($sFullUrl-2== '?='{
  218.             $sFullUrl substr_replace($sFullUrl''-2);
  219.         }
  220.  
  221.         // now convert Contenido url to an AMR url
  222.         $sFullUrl ModRewriteUrlUtil::getInstance()->toModRewriteUrl($sFullUrl);
  223.  
  224.         return $sFullUrl;
  225.     }
  226.  
  227.  
  228.     /**
  229.      * Loops thru passed parameter array and creates the query part of the URL.
  230.      * All non Contenido related parameter will be excluded from composition.
  231.      *
  232.      * @param   array  $aArgs  Assoziative parameter array
  233.      * @return  string  Composed query part for the URL like '?foo=bar&amp;param=value'
  234.      */
  235.     private function _createUrlQueryPart(array $aArgs){
  236.         // set list of parameter which are to ignore while setting additional parameter
  237.         $aIgnoredParams array(
  238.             'idcat''idart''lang''client''idcatart''idartlang'
  239.         );
  240.         if ($this->_aMrCfg['use_language'== 1{
  241.             $aIgnoredParams['changelang';
  242.         }
  243.         if ($this->_aMrCfg['use_client'== 1{
  244.             $aIgnoredParams['changeclient';
  245.         }
  246.  
  247.         // collect additional non contenido related parameters
  248.         $sQuery '';
  249.         foreach ($aArgs as $p => $v{
  250.             if (!in_array($p$aIgnoredParams)) {
  251.                 $sQuery .= urlencode(urldecode($p)) '=' urlencode(urldecode($v)) $this->_sAmp;
  252.             }
  253.         }
  254.         if (strlen($sQuery0{
  255.             $sQuery '?' substr($sQuery0strlen($this->_sAmp));
  256.         }
  257.         return $sQuery;
  258.     }
  259.  
  260.  
  261.     /**
  262.      * Returns client id or name depending on settings.
  263.      *
  264.      * @param   array   $aArgs     Additional arguments
  265.      * @return  mixed   Client id, client name or null
  266.      */
  267.     private function _getClientParameter(array $aArgs{
  268.         // set client if desired
  269.         if ($this->_aMrCfg['use_client'== 1{
  270.             $iChangeClient (isset($aArgs['changeclient'])) ? (int) $aArgs['changeclient'0;
  271.             $idclient      ($iChangeClient 0$iChangeClient $GLOBALS['client'];
  272.             if ($this->_aMrCfg['use_client_name'== 1{
  273.                 return urlencode(ModRewrite::getClientName($idclient));
  274.             else {
  275.                 return $idclient;
  276.             }
  277.         }
  278.         return null;
  279.     }
  280.  
  281.  
  282.     /**
  283.      * Returns language id or name depending on settings.
  284.      *
  285.      * @param   array   $aArgs     Additional arguments
  286.      * @return  mixed   Language id, language name or null
  287.      */
  288.     private function _getLanguageParameter(array $aArgs{
  289.         // set language if desired
  290.         if ($this->_aMrCfg['use_language'== 1{
  291.             $iChangeLang (isset($aArgs['changelang'])) ? (int) $aArgs['changelang'0;
  292.             $idlang      ($iChangeLang 0$iChangeLang $GLOBALS['lang'];
  293.             if ($this->_aMrCfg['use_language_name'== 1{
  294.                 return urlencode(ModRewrite::getLanguageName($idlang));
  295.             else {
  296.                 return $idlang;
  297.             }
  298.         }
  299.         return null;
  300.     }
  301.  
  302.  
  303.     /**
  304.      * Returns composed path of url (normally the category structure)
  305.      *
  306.      * @param   array   $aPretty   Pretty url array
  307.      * @return  string  Path
  308.      */
  309.     private function _getPath(array $aPretty{
  310.         $sPath (isset($aPretty['urlpath'])) $aPretty['urlpath''';
  311.  
  312.         // check start directory settings
  313.         if ($this->_aMrCfg['startfromroot'== && (strlen($sPath0)) {
  314.             // splitt string in array
  315.             $aCategories explode('/'$sPath);
  316.  
  317.             // remove first category
  318.             array_shift($aCategories);
  319.  
  320.             // implode array with categories to new string
  321.             $sPath implode('/'$aCategories);
  322.         }
  323.  
  324.         return $sPath;
  325.     }
  326.  
  327.  
  328.     /**
  329.      * Returns articlename depending on current setting
  330.      *
  331.      * @param   array   $aPretty   Pretty url array
  332.      * @param   array   $aArgs     Additional arguments
  333.      * @return  string  Articlename
  334.      */
  335.     private function _getArticleName(array $aPrettyarray $aArgs{
  336.  
  337.         $sArticle   (isset($aPretty['urlname'])) $aPretty['urlname''';
  338.  
  339.         $iIdCat     (isset($aArgs['idcat'])) ? (int) $aArgs['idcat'0;
  340.         $iIdCatLang (isset($aArgs['idcatlang'])) ? (int) $aArgs['idcatlang'0;
  341.         $iIdCatArt  (isset($aArgs['idcatart'])) ? (int) $aArgs['idcatart'0;
  342.         $iIdArt     (isset($aArgs['idart'])) ? (int) $aArgs['idart'0;
  343.         $iIdArtLang (isset($aArgs['idartlang'])) ? (int) $aArgs['idartlang'0;
  344.  
  345.         // category id was passed but not article id
  346.         if (($iIdCat || $iIdCatLang 0&& $iIdCatArt == && $iIdArt == && $iIdArtLang == 0{
  347.             $sArticle '';
  348.             if ($this->_aMrCfg['add_startart_name_to_url']{
  349.                 if ($this->_aMrCfg['default_startart_name'!== ''{
  350.                     // use default start article name
  351.                     $sArticle $this->_aMrCfg['default_startart_name'];
  352.                 else {
  353.                     $sArticle (isset($aPretty['urlname'])) $aPretty['urlname''';
  354.                 }
  355.             else {
  356.                 // url is to create without article name
  357.                 $sArticle '';
  358.             }
  359.         }
  360.  
  361.         return $sArticle;
  362.     }
  363.  
  364.  
  365. }

Documentation generated on Sun, 18 Jan 2009 22:21:30 +0100 by phpDocumentor 1.4.1