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.3
  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 frontend urls for advandced mod rewrite plugin.
  34.  * Extends abstract Contenido_UrlBuilder class and implements singleton pattern.
  35.  *
  36.  * Usage:
  37.  * <code>
  38.  * cInclude('classes', 'UrlBuilder/Contenido_UrlBuilder_MR.class.php');
  39.  * $url = 'front_content.php?idart=123';
  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.      * Self instance
  54.      *
  55.      * @var  Contenido_UrlBuilder_MR 
  56.      */
  57.     static private $_instance;
  58.  
  59.     /**
  60.      * Debugger instance
  61.      *
  62.      * @var  Contenido_mpDebug 
  63.      */
  64.     private $_oDebug;
  65.  
  66.     /**
  67.      * Ampersant used for composing several parameter value pairs
  68.      *
  69.      * @var  string 
  70.      */
  71.     private $_sAmp = '&amp;';
  72.  
  73.     /**
  74.      * Is XHTML output?
  75.      *
  76.      * @var  bool 
  77.      */
  78.     private $_bIsXHTML = false;
  79.  
  80.     /**
  81.      * Is mod rewrite enabled?
  82.      *
  83.      * @var  bool 
  84.      */
  85.     private $_bMREnabled = false;
  86.  
  87.     /**
  88.      * Mod Rewrite configuration
  89.      *
  90.      * @var  array 
  91.      */
  92.     private $_aMrCfg = null;
  93.  
  94.  
  95.     /**
  96.      * Constructor, tries to set some member variables.
  97.      */
  98.     private function __construct() {
  99.         $this->sHttpBasePath = '';
  100.         if (ModRewrite::isEnabled()) {
  101.             $this->_oDebug     = Contenido_mpDebug::getInstance();
  102.             $this->_aMrCfg     = ModRewrite::getConfig();
  103.             $this->_bMREnabled = true;
  104.             $this->_bIsXHTML   = (getEffectiveSetting('generator', 'xhtml', 'false') == 'false') ? false : true;
  105.             $this->_sAmp       = ($this->_bIsXHTML) ? '&' : '&amp;';
  106.         }
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Returns a instance of Contenido_UrlBuilder_MR
  112.      *
  113.      * @return  Contenido_UrlBuilder_MR 
  114.      */
  115.     public static function getInstance() {
  116.         if (self::$_instance == null) {
  117.             self::$_instance = new Contenido_UrlBuilder_MR();
  118.         }
  119.         return self::$_instance;
  120.     }
  121.  
  122.  
  123.     /**
  124.      * Builds a URL based on defined mod rewrite settings.
  125.      *
  126.      * @param   array    $params  Parameter array, provides only following parameters:
  127.      *  <code>
  128.      *  $params[0] = 'front_content.php?idart=123...'
  129.      *  </code>
  130.      * @param   boolean  $bUseAbsolutePath  Flag to use absolute path (not used at the moment)
  131.      * @return  string   New build url
  132.      */
  133.     public function buildUrl(array $params, $bUseAbsolutePath=false) {
  134.         $url = (isset($params[0])) ? $params[0] : '';
  135.  
  136.         if ($this->_bMREnabled) {
  137.  
  138.             $url  = ModRewrite::urlPreClean($url);
  139.             $aUrl = ModRewrite::getClientFullUrlParts($url);
  140.             if (preg_match("/^front_content\.php(.*|.+?)/i", $aUrl['url'], $aHits) == 1) {
  141.                 $url = $aUrl['htmlpath'] . self::_buildUrl($aHits[1]);
  142.             } else {
  143.                 $this->_oDebug->addDebug($url, 'Contenido_UrlBuilder_MR::buildUrl() no match');
  144.             }
  145.         }
  146.  
  147. #        $urlDebug['out'] = $url;
  148. #        $this->_oDebug->addDebug($urlDebug, 'Contenido_UrlBuilder_MR::buildUrl() in -> out');
  149.  
  150.         $this->sUrl = $url;
  151.     }
  152.  
  153.  
  154.     /**
  155.      * Builds the SEO-URL by analyzing passed arguments (parameter value pairs)
  156.      *
  157.      * @param   string  $args  Arguments
  158.      * @return  string  New build pretty url
  159.      */
  160.     private function _buildUrl($args='') {
  161.         // check arguments, try to set anchor, if exists and do some cleanup
  162.         $sAnchor = '';
  163.         $aTmp = explode('#', $args);
  164.         if (count($aTmp) > 1) {
  165.             $args = $aTmp[0];
  166.             $sAnchor = '#' . urlencode(urldecode($aTmp[1]));
  167.         }
  168.         $args = str_replace('?', '', $args);
  169.  
  170.         // extract arguments into arguments array
  171.         $aArgs = array();
  172.         parse_str($args, $aArgs);
  173.  
  174.         // set list of parameter which are to ignore while setting additional parameter
  175.         $aIgnoredParams = array(
  176.             'idcat', 'idart', 'lang', 'client', 'idcatart', 'idartlang'
  177.         );
  178.         if ($this->_aMrCfg['use_language'] == 1) {
  179.             $aIgnoredParams[] = 'changelang';
  180.         }
  181.         if ($this->_aMrCfg['use_client'] == 1) {
  182.             $aIgnoredParams[] = 'changeclient';
  183.         }
  184.  
  185.         // collect additional non contenido related parameters
  186.         $sParams = '';
  187.         foreach ($aArgs as $p => $v) {
  188.             if (!in_array($p, $aIgnoredParams)) {
  189.                 $sParams .= urlencode(urldecode($p)) . '=' . urlencode(urldecode($v)) . $this->_sAmp;
  190.             }
  191.         }
  192.         if (strlen($sParams) > 0) {
  193.             $sParams = '?' . substr($sParams, 0, - strlen($this->_sAmp));
  194.         }
  195.  
  196.         // some presettings of variables
  197.         $aParts = array();
  198.         $iChangeClient = (isset($aArgs['changeclient'])) ? (int) $aArgs['changeclient'] : 0;
  199.         $iChangeLang   = (isset($aArgs['changelang'])) ? (int) $aArgs['changelang'] : 0;
  200.         $iIdCat        = (isset($aArgs['idcat'])) ? (int) $aArgs['idcat'] : 0;
  201.         $iIdCatLang    = (isset($aArgs['idcatlang'])) ? (int) $aArgs['idcatlang'] : 0;
  202.         $iIdCatArt     = (isset($aArgs['idcatart'])) ? (int) $aArgs['idcatart'] : 0;
  203.         $iIdArt        = (isset($aArgs['idart'])) ? (int) $aArgs['idart'] : 0;
  204.         $iIdArtLang    = (isset($aArgs['idartlang'])) ? (int) $aArgs['idartlang'] : 0;
  205.  
  206.         // set client if desired
  207.         if ($this->_aMrCfg['use_client'] == 1) {
  208.             $idclient = ($iChangeClient > 0) ? $iChangeClient : $GLOBALS['client'];
  209.             if ($this->_aMrCfg['use_client_name'] == 1) {
  210.                 $aParts[] = urlencode(ModRewrite::getClientName($idclient));
  211.             } else {
  212.                 $aParts[] = $idclient;
  213.             }
  214.         }
  215.  
  216.         // set language if desired
  217.         if ($this->_aMrCfg['use_language'] == 1) {
  218.             $idlang = ($iChangeLang > 0) ? $iChangeLang : $GLOBALS['lang'];
  219.             if ($this->_aMrCfg['use_language_name'] == 1) {
  220.                 $aParts[] = urlencode(ModRewrite::getLanguageName($idlang));
  221.             } else {
  222.                 $aParts[] = $idlang;
  223.             }
  224.         }
  225.  
  226.         // get pretty url parts
  227.         $oMRUrlStack = ModRewriteUrlStack::getInstance();
  228.         $aPretty = $oMRUrlStack->getPrettyUrlParts('front_content.php?' . $args);
  229.  
  230.         $sPath    = (isset($aPretty['urlpath'])) ? $aPretty['urlpath'] : '';
  231.         $sArticle = (isset($aPretty['urlname'])) ? $aPretty['urlname'] : '';
  232.  
  233.         if (strlen($sPath) > 0) {
  234.             $aParts[] = $sPath;
  235.         }
  236.         $sPath = implode('/', $aParts);
  237.  
  238.         // check start directory settings
  239.         if ($this->_aMrCfg['startfromroot'] == 0) {
  240.             // splitt string in array
  241. ##++##            $aCategories = explode($this->_aMrCfg['category_seperator'], $sPath);
  242.             $aCategories = explode('/', $sPath);
  243.  
  244.             // remove first category
  245.             array_shift($aCategories);
  246.  
  247.             // implode array with categories to new string
  248. ##++##            $sPath = implode($this->_aMrCfg['category_seperator'], $aCategories);
  249.             $sPath = implode('/', $aCategories);
  250.         }
  251.  
  252.         // category id was passed but not article id
  253.         if (($iIdCat > 0 || $iIdCatLang > 0) && $iIdCatArt == 0 && $iIdArt == 0 && $iIdArtLang == 0) {
  254.             $sArticle = '';
  255.             if ($this->_aMrCfg['add_startart_name_to_url']) {
  256.                 if ($this->_aMrCfg['default_startart_name'] !== '') {
  257.                     // use default start article name
  258.                     $sArticle = $this->_aMrCfg['default_startart_name'];
  259.                 }
  260.             } else {
  261.                 // url is to create without article name
  262.                 $sArticle = '';
  263.             }
  264.         }
  265.  
  266.         if ($sArticle !== '') {
  267.             $sFileExt = $this->_aMrCfg['file_extension'];
  268.         } else {
  269.             $sFileExt = '';
  270.         }
  271.  
  272.         // add closing path seperator
  273.         $sPath .= '/';
  274.  
  275.         $sPathAndArticle = $sPath . $sArticle . $sFileExt;
  276.         if ($this->_aMrCfg['use_lowercase_uri'] == 1) {
  277.             // use lowercase url
  278.             $sPathAndArticle = strtolower($sPathAndArticle);
  279.         }
  280.  
  281.         $sFullUrl = $this->_aMrCfg['rootdir'] . $sPathAndArticle . $sParams . $sAnchor;
  282. #echo "<pre>Contenido_UrlBuilder_MR->_buildUrl: \$sFullUrl $sFullUrl</pre>";
  283.  
  284.         // remove double or more join parameter
  285.         $sFullUrl = mr_removeMultipleChars($this->_aMrCfg['category_seperator'], $sFullUrl);
  286.         if (substr($sFullUrl, -2) == '?=') {
  287.             $sFullUrl = substr_replace($sFullUrl, '', -2);
  288.         }
  289. #echo "<pre>Contenido_UrlBuilder_MR->_buildUrl: \$sFullUrl $sFullUrl</pre>";
  290.         // now convert url to an AMR url
  291.         $oMrUrlUtil = ModRewriteUrlUtil::getInstance();
  292.         $sFullUrl = $oMrUrlUtil->toModRewriteUrl($sFullUrl);
  293.  
  294.         return $sFullUrl;
  295.     }
  296.  
  297. }

Documentation generated on Tue, 25 Nov 2008 22:07:34 +0100 by phpDocumentor 1.4.1