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 front_content.php URL
  8.  *
  9.  * Requirements:
  10.  * @con_php_req 5.0
  11.  *
  12.  * @package     Contenido
  13.  * @subpackage  ModRewrite
  14.  * @version     0.2
  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. if(!defined('CON_FRAMEWORK')) {
  24.     die('Illegal call');
  25. }
  26.  
  27.  
  28. cInclude('classes''UrlBuilder/Contenido_UrlBuilder.class.php');
  29. cInclude('classes''UrlBuilder/Contenido_UrlBuilderFactory.class.php');
  30.  
  31.  
  32. /**
  33.  * Class to build urls frontend urls for Advandced Mod Rewrite plugin.
  34.  * Extends abstract Contenido_UrlBuilder class and implements singleton pattern.
  35.  *
  36.  * Usage:
  37.  * <code>
  38.  * $url = 'front_content.php?idart=123';
  39.  * cInclude('plugins', 'mod_rewrite/classes/Contenido_UrlBuilder_MR.class.php');
  40.  * $mrUrlBuilder = Contenido_UrlBuilder_MR::getInstance();
  41.  * $mrUrlBuilder->buildUrl(array($url));
  42.  * $newUrl = $mrUrlBuilder->getUrl();
  43.  * </code>
  44.  *
  45.  * @author      Murat Purc <murat@purc.de>
  46.  * @copyright   © Murat Purc 2008
  47.  * @package     Contenido
  48.  * @subpackage  ModRewrite
  49.  */
  50. class Contenido_UrlBuilder_MR extends Contenido_UrlBuilder {
  51.  
  52.     /**
  53.      * @var  Contenido_UrlBuilder_MR  Self instance
  54.      */
  55.     static private $_instance;
  56.  
  57.     /**
  58.      * @var  string  Ampersant used for composong several parameter
  59.      */
  60.     private $_sAmp = '&amp;';
  61.  
  62.     /**
  63.      * @var  bool  Is XHTML output?
  64.      */
  65.     private $_bIsXHTML = '&amp;';
  66.  
  67.     /**
  68.      * @var  bool  Is mod rewrite enabled?
  69.      */
  70.     private $_bMREnabled = false;
  71.  
  72.  
  73.     /**
  74.      * @var  array  Mod Rewrite configuration
  75.      */
  76.     private $_aMrCfg = null;
  77.  
  78.  
  79.     /**
  80.      * Constructor, tries to set some member variables.
  81.      */
  82.     private function __construct({
  83.         $this->sHttpBasePath '';
  84.         if ($GLOBALS['cfg']['mod_rewrite']['use'== 1{
  85.             $this->_aMrCfg     = $GLOBALS['cfg']['mod_rewrite'];
  86.             $this->_bMREnabled = true;
  87.             $this->_bIsXHTML   = (getEffectiveSetting('generator''xhtml''false'== 'false'false true;
  88.             $this->_sAmp       = ($this->_bIsXHTML'&' '&amp;';
  89.         }
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Returns a instance of Contenido_UrlBuilder_MR
  95.      *
  96.      * @return  Contenido_UrlBuilder_MR 
  97.      */
  98.     public static function getInstance({
  99.         if (self::$_instance == null{
  100.             self::$_instance new Contenido_UrlBuilder_MR();
  101.         }
  102.         return self::$_instance;
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Builds a URL in front_content.php style.
  108.      *
  109.      * @param   array    $params  Parameter array, provides only following parameters:
  110.      *  <code>
  111.      *  $params[0] = 'front_content.php?idart=123...'
  112.      *  </code>
  113.      * @param   boolean  $bUseAbsolutePath  Flag to use absolute path, not used at the moment
  114.      * @return  string  new build url
  115.      */
  116.     public function buildUrl(array $params$bUseAbsolutePath=false{
  117.  
  118.         $url (isset($params[0])) $params[0'';
  119.  
  120.         $urlDebug['in'$url;
  121.  
  122.         // cleanup any existing & entities
  123.         $url str_replace('&amp;''&'$url);
  124.  
  125.         if ($this->_bMREnabled{
  126.  
  127.             // now a little hack to replace occuring index_controller.php against
  128.             // front_content.php. This happens using $auth->url()/$auth->purl()
  129.             // which composes the URL using $PHP_SELF
  130.             if (strpos($url'index_controller.php'!== false{
  131.                 $url str_replace('index_controller.php''front_content.php'$url);
  132.  
  133.                 // self_url returns url including absoute path from root (/cms/foobar.php)
  134.                 // modify this...
  135.                 $path $GLOBALS['cfgClient'][$GLOBALS['client']]['path']['htmlpath'];
  136.                 $comp @parse_url($path);
  137.                 if (isset($comp['path']&& (strpos($url$comp['path']=== 0)) {
  138.                     $url substr($urlstrlen($comp['path']));
  139.                 }
  140.             }
  141.  
  142.             $aUrl ModRewrite::get_client_full_url_parts($url);
  143.             if (preg_match("/^front_content\.php(.*|.+?)/i"$aUrl['url']$arr_hits== 1{
  144.                 $url $aUrl['htmlpath'self::_buildUrl($arr_hits[1]);
  145.             else {
  146.                 $GLOBALS['mpDebug']->addDebug($url'Contenido_UrlBuilder_MR::buildUrl() nomatch');
  147.             }
  148.         }
  149.  
  150.         // replace & against entity, if xhtml is to use
  151.         if ($this->_bIsXHTML{
  152.             $url str_replace('&''&amp;'$url);
  153.         }
  154.  
  155.         $urlDebug['out'$url;
  156.         $GLOBALS['mpDebug']->addDebug($urlDebug'Contenido_UrlBuilder_MR::buildUrl() in -> out');
  157.  
  158.         $this->sUrl $url;
  159.  
  160.     }
  161.  
  162.  
  163.     /**
  164.      * Private function to build the url by analyzing passed arguments (parameter value pairs)
  165.      *
  166.      * TODO: Too much code in one function -> is to redesign...
  167.      *
  168.      * @param   string  $args  Arguments
  169.      * @return  string  New build url
  170.      */
  171.     private function _buildUrl($args=''{
  172.  
  173.         $sNewUrl        '';
  174.         $sCategories    '';
  175.         $sArticle       '';
  176.         $aArgs          array();
  177.         $sJoinParameter '/';
  178.         $sFileExtension '';
  179.         $aParts         array();
  180.  
  181.         // only html files part
  182.             $sJoinParameter $this->_aMrCfg['category_seperator'];
  183.             $sFileExtension $this->_aMrCfg['file_extension'];
  184.         }
  185.  
  186.         // check arguments ... and split
  187.         $sAnchor '';
  188.         // check for anchor
  189.         $aArgs explode("#"$args);
  190.         if (is_array($aArgs&& count($aArgs1{
  191.             $args $aArgs[0];
  192.             $sAnchor '#' urlencode(urldecode($aArgs[1]));
  193.         }
  194.  
  195.         $args str_replace('?'''$args);
  196.         $args str_replace('&amp;''&'$args);
  197.  
  198.         // extract arguments into current scope
  199.         parse_str($args);
  200.  
  201.         // some preparations to avoid too much checks in further code blocks
  202.         $idcat        (isset($idcat)) ? (int) $idcat 0;
  203.         $idart        (isset($idart)) ? (int) $idart 0;
  204.         $idcatart     (isset($idcatart)) ? (int) $idcatart 0;
  205.         $idartlang    (isset($idartlang)) ? (int) $idartlang 0;
  206.         $changelang   (isset($changelang)) ? (int) $changelang 0;
  207.         $changeclient (isset($changeclient)) ? (int) $changeclient 0;
  208.  
  209.         // get additional non contenido parameters
  210.         $sAdditionalParams '';
  211.         $aAdditionalParams array();
  212.         $aParamPairs       split('&'$args);
  213.  
  214.         $aIgnoredParams array(
  215.             'idcat''idart''lang''client''idcatart''changelang''changeclient''idartlang'
  216.         );
  217.  
  218.         foreach ($aParamPairs as $sPair{
  219.             $bFoundBad false;
  220.             $arr_param explode('='$sPair);
  221.  
  222.             foreach ($aIgnoredParams as $sKey{
  223.                 if ($sKey == strtolower(trim($arr_param[0]))) {
  224.                     $bFoundBad true;
  225.                     break;
  226.                 }
  227.             }
  228.  
  229.             if ($bFoundBad == false{
  230.                 $aAdditionalParams[urlencode(urldecode($arr_param[0])) '=' urlencode(urldecode($arr_param[1]));
  231.             }
  232.         }
  233.  
  234.         if (count($aAdditionalParams0{
  235.             $sAdditionalParams '?' implode($this->_sAmp$aAdditionalParams);
  236.         }
  237.  
  238.         $idlang   ($changelang 0$changelang $GLOBALS['lang'];
  239.         $idclient ($changeclient 0$changeclient $GLOBALS['client'];
  240.  
  241.         // set client?
  242.         if ($this->_aMrCfg['use_client'== 1{
  243.             if ($this->_aMrCfg['use_client_name'== 1{
  244.                 $aParts[urlencode(ModRewrite::get_client_name($idclient));
  245.             else {
  246.                 $aParts[$idclient;
  247.             }
  248.         }
  249.  
  250.         // set language?
  251.         if ($this->_aMrCfg['use_language'== 1{
  252.             if ($this->_aMrCfg['use_language_name'== 1{
  253.                 $aParts[urlencode(ModRewrite::get_language_name($idlang));
  254.             else {
  255.                 $aParts[$idlang;
  256.             }
  257.         }
  258.  
  259.         // define rootpath ...
  260.         $sNewUrl $this->_aMrCfg['rootdir'];
  261.  
  262.         // handle idcatart
  263.         if ($idcatart && $idcat == && $idart == && $idartlang == 0{
  264.             $sql "SELECT idcat, idart FROM " $GLOBALS['cfg']['tab']['cat_art'" WHERE idcatart='$idcatart'";
  265.             if ($aData mr_query_n_next_record($sql)) {
  266.                 $idcat $aData['idcat'];
  267.                 $idart $aData['idart'];
  268.             }
  269.         }
  270.  
  271.         // check if article id is given and set article url
  272.         if ($idart && $idartlang == 0{
  273.             $sArticle ModRewrite::get_art_websafename($idart$idlang);
  274.         elseif ($idartlang && $idart == 0{
  275.             // handle idartlang
  276.             $sArticle ModRewrite::get_art_lang_websafename($idartlang);
  277.         }
  278.  
  279.         // check if only article id is given, cat id have to rebuild
  280.         if ($idart && $idcat == && $idartlang == 0{
  281.             $sql "SELECT idcat FROM " $GLOBALS['cfg']['tab']['cat_art'" WHERE idart='$idart'";
  282.             if ($aData mr_query_n_next_record($sql)) {
  283.                 $idcat $aData['idcat'];
  284.             }
  285.         }
  286.  
  287.         // idartlang is given
  288.         if ($idartlang && $idcat == && $idart == 0{
  289.             $sql "SELECT ca.idcat
  290.                     FROM " $GLOBALS['cfg']['tab']['art_lang'" al
  291.                     LEFT JOIN " $GLOBALS['cfg']['tab']['cat_art'" ca ON al.idart = ca.idart
  292.                     WHERE al.idartlang='$idartlang'";
  293.             if ($aData mr_query_n_next_record($sql)) {
  294.                 $idcat $aData['idcat'];
  295.             }
  296.         }
  297.  
  298.         // we have category but no article name and start article name should be added to url
  299.         if ($idcat && $sArticle == '' && $this->_aMrCfg['add_startart_name_to_url']{
  300.             if ($this->_aMrCfg['default_startart_name'== ''{
  301.  
  302.                 // no default article name is configured get startarticle
  303.                 cInclude('classes''class.article.php');
  304.                 $artColl new ArticleCollection(array('idcat' => $idcat'start' => 1));
  305.                 if ($artItem $artColl->startArticle()) {
  306.                     $sArticle ModRewrite::get_art_websafename($artItem->get('idart')$idlang);
  307.                 }
  308.  
  309.             else {
  310.  
  311.                 // use default start article name
  312.                 $sArticle $this->_aMrCfg['default_startart_name'];
  313.             }
  314.         }
  315.  
  316.         if ($sArticle{
  317.             $sFileExtension $this->_aMrCfg['file_extension'];
  318.         }
  319.  
  320.         // ok build dir list, if idcat found ...
  321.         if ($idcat 0{
  322.  
  323.             $sCategories ModRewrite::build_recursiv_path ($idcat$idlang);
  324.  
  325.             // check start directory settings
  326.             if ($this->_aMrCfg['startfromroot'== 0{
  327.                 // splitt string in array
  328.                 $aCategories explode($sJoinParameter$sCategories);
  329.  
  330.                 // remove first category
  331.                 $sFirstCat array_shift($aCategories);
  332.  
  333.                 // implode array with categories to new string
  334.                 $sCategories implode($sJoinParameter$aCategories);
  335.             }
  336.  
  337.             if (strlen($sCategories0{
  338.                 $aParts[$sCategories;
  339.             }
  340.         }
  341.         $sParts implode($sJoinParameter$aParts);
  342.  
  343.         // only html files part
  344.         if (strlen($sParts&& strlen($sArticle0
  345.             && ModRewrite::validate_setting_categories_as_html()) {
  346.             $sParts .= $this->_aMrCfg['article_seperator'];
  347.         elseif ((int)$this->_aMrCfg['use_categories_as_html_file'!= && strlen($sParts0{
  348.             $sParts .= $sJoinParameter;
  349.         }
  350.  
  351.         // using lowercase uri?
  352.         if ($this->_aMrCfg['use_lowercase_uri'== 1{
  353.             $sFullUrl $sNewUrl strtolower($sParts $sArticle $sFileExtension$sAdditionalParams $sAnchor;
  354.         else {
  355.             $sFullUrl $sNewUrl $sParts $sArticle $sFileExtension $sAdditionalParams $sAnchor;
  356.         }
  357.  
  358.         // remove double slashes
  359.         while (eregi($sJoinParameter.$sJoinParameter$sFullUrl)) {
  360.             $sFullUrl eregi_replace($sJoinParameter $sJoinParameter$sJoinParameter$sFullUrl);
  361.         }
  362.  
  363.         if (substr($sFullUrl-2== '?='{
  364.             $sFullUrl substr_replace($sFullUrl''-2);
  365.         }
  366.  
  367.         return $sFullUrl;
  368.  
  369.     }
  370.  
  371. }

Documentation generated on Sun, 03 Aug 2008 22:21:58 +0200 by phpDocumentor 1.4.0