Source for file class.modrewritecontroller.php

Documentation is available at class.modrewritecontroller.php

  1. <?php
  2. /**
  3.  * Includes Mod Rewrite controller class.
  4.  *
  5.  * @author      Murat Purc <murat@purc.de>
  6.  * @copyright   © Murat Purc 2008
  7.  * @package     Contenido
  8.  * @subpackage  ModRewrite
  9.  */
  10.  
  11.  
  12. if(!defined('CON_FRAMEWORK')) {
  13.     die('Illegal call');
  14. }
  15.  
  16.  
  17. /**
  18.  * Mod Rewrite controller class. Extracts url parts and sets some necessary globals like:
  19.  * - $idart
  20.  * - $idcat
  21.  * - $client
  22.  * - $changeclient
  23.  * - $lang
  24.  * - $changelang
  25.  *
  26.  * TODO: Adapt to PHP 5 coding standards...
  27.  *
  28.  * @author      Murat Purc <murat@purc.de>
  29.  * @date        16.04.2008
  30.  * @package     Contenido
  31.  * @subpackage  ModRewrite
  32.  */
  33.  
  34.     /**
  35.      * Contenido configuration array (see $GLOBALS['cfg'])
  36.      *
  37.      * @var array 
  38.      */
  39.     var $_aCfg;
  40.  
  41.     /**
  42.      * Mod Rewrite configuration array (see $GLOBALS['cfg']['mod_rewrite'])
  43.      *
  44.      * @var array 
  45.      */
  46.     var $_aCfgMR;
  47.  
  48.     /**
  49.      * Extracted request uri path parts by path separator '/'
  50.      *
  51.      * @var array 
  52.      */
  53.     var $_aParts;
  54.  
  55.     /**
  56.      * Extracted article name from request uri
  57.      *
  58.      * @var string 
  59.      */
  60.     var $_sArtName;
  61.  
  62.     /**
  63.      * Remaining path for path resolver (see $GLOBALS['path'])
  64.      *
  65.      * @var string 
  66.      */
  67.     var $_sPath;
  68.  
  69.     /**
  70.      * Client id used by this class
  71.      *
  72.      * @var int 
  73.      */
  74.     var $_iClientMR;
  75.  
  76.     /**
  77.      * Flag about occured errors
  78.      *
  79.      * @var bool 
  80.      */
  81.     var $_bError = false;
  82.  
  83.     /**
  84.      * Constructor, sets several properties.
  85.      */
  86.     function ModRewriteController({
  87.         $this->_aCfg   = $GLOBALS['cfg'];
  88.         $this->_aCfgMR = $GLOBALS['cfg']['mod_rewrite'];
  89.         $this->_aParts = array();
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Getter for overwritten client id (see $GLOBALS['client'])
  95.      *
  96.      * @return  int  Client id
  97.      */
  98.     function getClient({
  99.         return $GLOBALS['client'];
  100.     }
  101.  
  102.  
  103.     /**
  104.      * Getter for overwritten change client id (see $GLOBALS['changeclient'])
  105.      *
  106.      * @return  int  Change client id
  107.      */
  108.     function getChangeClient({
  109.         return $GLOBALS['changeclient'];
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Getter for article id (see $GLOBALS['idart'])
  115.      *
  116.      * @return  int  Article id
  117.      */
  118.     function getIdArt({
  119.         return $GLOBALS['idart'];
  120.     }
  121.  
  122.  
  123.     /**
  124.      * Getter for category id (see $GLOBALS['idcat'])
  125.      *
  126.      * @return  int  Category id
  127.      */
  128.     function getIdCat({
  129.         return $GLOBALS['idcat'];
  130.     }
  131.  
  132.  
  133.     /**
  134.      * Getter for language id (see $GLOBALS['lang'])
  135.      *
  136.      * @return  int  Language id
  137.      */
  138.     function getLang({
  139.         return $GLOBALS['lang'];
  140.     }
  141.  
  142.  
  143.     /**
  144.      * Getter for change language id (see $GLOBALS['change_lang'])
  145.      *
  146.      * @return  int  Change language id
  147.      */
  148.     function getChangeLang({
  149.         return $GLOBALS['changelang'];
  150.     }
  151.  
  152.  
  153.     /**
  154.      * Getter for path (see $GLOBALS['path'])
  155.      *
  156.      * @return  string  Path, used by path resolver
  157.      */
  158.     function getPath({
  159.         return $this->_sPath;
  160.     }
  161.  
  162.  
  163.     /**
  164.      * Getter for occured error state
  165.      *
  166.      * @return  bool  Flag for occured error
  167.      */
  168.     function errorOccured(){
  169.         return $this->_bError;
  170.     }
  171.  
  172.  
  173.     /**
  174.      * Main function to call for mod rewrite related preprocessing jobs.
  175.      *
  176.      * Executes some private functions to extract request uri and to set needed membervariables
  177.      * (client, language, article id, category id, etc.)
  178.      */
  179.     function execute({
  180.  
  181.         if (ModRewrite::is_enabled(== false{
  182.             return;
  183.         }
  184.  
  185.         $this->_extractRequestUri();
  186.  
  187.         $this->_overrideConfig();
  188.  
  189.         $this->_preprocessVariables();
  190.  
  191.         $this->_setClientId();
  192.         $GLOBALS['mpDebug']->addDebug($GLOBALS['client']'ModRewriteController::execute() $GLOBALS[\'client\']');
  193.  
  194.         mr_load_configuration($this->_iClientMR);
  195.  
  196.         $this->_setLanguageId();
  197.         $GLOBALS['mpDebug']->addDebug($GLOBALS['lang']'ModRewriteController::execute() $GLOBALS[\'lang\']');
  198.  
  199.         // second call after setting client and language
  200.         $this->_extractRequestUri(true);
  201.  
  202.         $this->_setPathresolverSetting();
  203.         $GLOBALS['mpDebug']->addDebug($this->_aParts'ModRewriteController::execute() after _setPathresolverSetting');
  204.  
  205.         $this->_setIdart();
  206.         $GLOBALS['mpDebug']->addDebug($this->_aParts'ModRewriteController::execute() _setIdart');
  207.     }
  208.  
  209.  
  210.     /**
  211.      * Extracts request uri and sets member variables $this->_sArtName and $this->_aParts
  212.      *
  213.      * @access  private
  214.      */
  215.     function _extractRequestUri($secondCall=false{
  216.  
  217.         // parse request uri
  218.         $uri $_SERVER['REQUEST_URI'];
  219.  
  220.         // check for defined rootdir
  221.         if ($this->_aCfgMR['rootdir'!== '/' && strpos($_SERVER['REQUEST_URI']$uri=== 0{
  222.             $uri str_replace($this->_aCfgMR['rootdir']'/'$uri);
  223.         }
  224. #        $GLOBALS['mpDebug']->addDebug($this->_aParts, 'ModRewriteController::_extractRequestUri() $this->_aParts 3.');
  225.  
  226.         $aUrlComponents @parse_url($uri);
  227.         if (isset($aUrlComponents['path'])) {
  228.  
  229.             if ($secondCall == true{
  230.                 // check for routing definition
  231.                 if (is_array($this->_aCfgMR['routing']&& isset($this->_aCfgMR['routing'][$aUrlComponents['path']])) {
  232.                     $aUrlComponents['path'$this->_aCfgMR['routing'][$aUrlComponents['path']];
  233.                     if (strpos($aUrlComponents['path']'front_content.php'!== false{
  234.                         // routing destination contains front_content.php
  235.  
  236.                         // set client language, if not set before
  237.                         mr_set_client_language($GLOBALS['client']);
  238.  
  239.                         //rebuild uri
  240.                         $uri mr_build_new_url($aUrlComponents['path']);
  241.                         $aUrlComponents parse_url($uri);
  242.                         $this->_aParts = array();
  243.                     }
  244.                 else {
  245.                     return;
  246.                 }
  247.             }
  248.  
  249.             $aPaths explode('/'$aUrlComponents['path']);
  250.             foreach ($aPaths as $p => $item{
  251.                 if (!empty($item)) {
  252.                     // pathinfo would also work
  253.                     $arr explode('.'$item);
  254.                     if (count($arr&& '.'.strtolower($arr[count($arr)-1]== $this->_aCfgMR['file_extension']{
  255.                         array_pop($arr);
  256.                         $this->_sArtName = implode('.'$arr);
  257.                     else {
  258.                         $this->_aParts[$item;
  259.                     }
  260.                 }
  261.             }
  262.         }
  263.         $GLOBALS['mpDebug']->addDebug($this->_aParts'ModRewriteController::_extractRequestUri() $this->_aParts');
  264.  
  265.         // loop parts array and remove existing 'front_content.php'
  266.         if ($this->_hasPartArrayItems()) {
  267.             foreach($this->_aParts as $p => $item{
  268.                 if ($item == 'front_content.php'{
  269.                     unset($this->_aParts[$p]);
  270.                 }
  271.             }
  272.         }
  273. #        $GLOBALS['mpDebug']->addDebug($this->_aParts, 'ModRewriteController::_extractRequestUri() $this->_aParts 2.');
  274.  
  275.         // set parts property top null, if needed
  276.         if ($this->_hasPartArrayItems(== false{
  277.             $this->_aParts = null;
  278.         }
  279. #        $GLOBALS['mpDebug']->addDebug($this->_aParts, 'ModRewriteController::_extractRequestUri() $this->_aParts 3.');
  280.  
  281.         // set artname to null if needed
  282.         if (!isset($this->_sArtName|| empty($this->_sArtName|| strlen($this->_sArtName== 0{
  283.             $this->_sArtName = null;
  284.         }
  285. #        $GLOBALS['mpDebug']->addDebug($this->_sArtName, 'ModRewriteController::_extractRequestUri() $this->_sArtName');
  286.  
  287.     }
  288.  
  289.  
  290.     /**
  291.      * Overrides local mod rewrite cfg settings with client settings from database
  292.      *
  293.      * @access  private
  294.      */
  295.     function _overrideConfig({
  296.  
  297.         if ((int) substr($this->_aCfg['version'],0,1substr($this->_aCfg['version'],2,1>= 46{
  298.             $this->_iClientMR = 0;
  299.             if (isset($GLOBALS['client']&& !empty($GLOBALS['client']&& !isset($GLOBALS['changeclient'])) {
  300.                 $this->_iClientMR = $GLOBALS['client'];
  301.             elseif (isset($GLOBALS['changeclient']&& !empty($GLOBALS['changeclient'])) {
  302.                 $this->_iClientMR = $GLOBALS['changeclient'];
  303.             else {
  304.                 $this->_iClientMR = $GLOBALS['load_client'];
  305.             }
  306.  
  307.             if ((int) $this->_iClientMR !== 0{
  308.                 // set global $GLOBALS['client'] variable
  309.                 $GLOBALS['client'= (int) $this->_iClientMR;
  310.             }
  311.         }
  312.     }
  313.  
  314.  
  315.     /**
  316.      * Preprocesses article name and parts list, sets article name and parts list
  317.      * if settings for usage of categories as a html file is active and valid.
  318.      *
  319.      * @access  private
  320.      */
  321.     function _preprocessVariables({
  322.         // check for html file only links and prepare variables
  323.         if ($this->_sArtName && ModRewrite::validate_setting_categories_as_html()) {
  324.             $aParts explode($this->_aCfgMR['article_seperator']$this->_sArtName);
  325.             if (count($aParts0{
  326.                 $this->_aParts   = (isset($aParts[0]&& strlen($aParts[0]0explode$this->_aCfgMR['category_seperator']$aParts[0false;
  327.                 $this->_sArtName = (isset($aParts[1]&& strlen($aParts[1]0$aParts[1false;
  328.             }
  329.         }
  330.     }
  331.  
  332.  
  333.     /**
  334.      * Sets client id
  335.      *
  336.      * @access  private
  337.      */
  338.     function _setClientId({
  339.         if ($this->_hasPartArrayItems(== false || $this->_aCfgMR['use_client'!== 1{
  340.             return;
  341.         }
  342.  
  343.         if ($this->_aCfgMR['use_client_name'== 1{
  344.             $GLOBALS['changeclient'mr_get_client_id(array_shift($this->_aParts));
  345.             $this->_iClientMR        = $GLOBALS['changeclient'];
  346.         else {
  347.             $GLOBALS['changeclient'= (int) array_shift($this->_aParts);
  348.             $this->_iClientMR        = $GLOBALS['changeclient'];
  349.         }
  350.  
  351.         if (empty($GLOBALS['changeclient']|| (int) $GLOBALS['changeclient'== 0{
  352.             $GLOBALS['changeclient'$GLOBALS['load_client'];
  353.         }
  354.         if (isset($GLOBALS['client']&& $GLOBALS['changeclient'!== $GLOBALS['client']{
  355.             // overwrite existing global $GLOBALS['client'] variable
  356.             $GLOBALS['client'$GLOBALS['changeclient'];
  357.         }
  358. #        $GLOBALS['mpDebug']->addDebug($GLOBALS['changeclient'], 'ModRewriteController::_setClientId() $GLOBALS[\'changeclient\']');
  359. #        $GLOBALS['mpDebug']->addDebug($this->_iClientMR, 'ModRewriteController::_setClientId() $this->_iClientMR');
  360.     }
  361.  
  362.  
  363.     /**
  364.      * Sets language id
  365.      *
  366.      * @access  private
  367.      */
  368.     function _setLanguageId({
  369.         if ($this->_hasPartArrayItems(== false || $this->_aCfgMR['use_language'!== 1{
  370.             return;
  371.         }
  372.  
  373.         if ($this->_aCfgMR['use_language_name'== 1{
  374.             // thanks to Nicolas Dickinson for multi Client/Language BugFix
  375.             $GLOBALS['changelang'mr_get_language_id(array_shift($this->_aParts$this->_iClientMR);
  376.         else {
  377.             $GLOBALS['changelang'= (int) array_shift($this->_aParts);
  378.         }
  379. #        $GLOBALS['mpDebug']->addDebug($GLOBALS['changelang'], 'ModRewriteController::_setLanguageId() $GLOBALS[\'changelang\']');
  380.  
  381.         if (empty($GLOBALS['changelang']|| (int) $GLOBALS['changelang'== 0{
  382.             unset($GLOBALS['changelang']);
  383.         else {
  384.             $GLOBALS['lang'$GLOBALS['changelang'];
  385.         }
  386.     }
  387.  
  388.  
  389.     /**
  390.      * Sets path resolver and category id
  391.      *
  392.      * @access  private
  393.      */
  394.     function _setPathresolverSetting({
  395.  
  396.         if ($this->_hasPartArrayItems(== false{
  397.             return;
  398.         }
  399.  
  400.         $this->_aParts = array_filter($this->_aParts);
  401.  
  402.         if ($this->_aCfgMR['startfromroot'== && count($this->_aParts0{
  403.             // remove first category (the main category) to prevent dublicate content.
  404.             // this is just a hack to change '/rootcat/firstcat/' to '/firstcat/', this will
  405.             // prevent duplicated content if '/firstcat/' is directly requested!
  406.             array_shift($this->_aParts);
  407.         }
  408.  
  409.         if (count($this->_aParts0{
  410.             $this->_sPath = '/' .implode('/'$this->_aParts'/';
  411.         }
  412.  
  413.         // TODO: time to get catid is to long, should be cached!!!
  414.         if (!isset($GLOBALS['lang'])) {
  415.             if (!isset($GLOBALS['load_lang'])) {
  416.                 // load_client is set in frontend/config.php
  417.                 $GLOBALS['lang'$GLOBALS['load_lang'];
  418.             else {
  419.                 // get client id from table
  420.                 cInclude('classes''contenido/class.clientslang.php');
  421.                 $clCol new cApiClientLanguageCollection();
  422.                 $clCol->setWhere('idclient'$GLOBALS['client']);
  423.                 $clCol->query();
  424.                 if ($clItem $clCol->next()) {
  425.                     $GLOBALS['lang'$clItem->get('idlang');
  426.                 }
  427.             }
  428.         }
  429.         $GLOBALS['idcat'prResolvePathViaURLNames($this->_sPath);
  430.  
  431.         if (!$GLOBALS['idcat'|| (int) $GLOBALS['idcat'== 0{
  432.             // set this flag, it's used in front_content.php
  433.             $this->_bError = true;
  434.             unset($GLOBALS['idcat']);
  435.         else {
  436.             // unset $this->_sPath if $GLOBALS['idcat'] could set, otherwhise it would be resolved again.
  437.             unset($this->_sPath);
  438.         }
  439.         $GLOBALS['mpDebug']->addDebug($GLOBALS['idcat']'ModRewriteController->_setPathresolverSetting $GLOBALS[\'idcat\']');
  440.         $GLOBALS['mpDebug']->addDebug($this->_sPath'ModRewriteController->_setPathresolverSetting $this->_sPath');
  441.     }
  442.  
  443.  
  444.     /**
  445.      * Sets article id
  446.      *
  447.      * @access  private
  448.      */
  449.     function _setIdart({
  450.  
  451.         // startarticle name in url
  452.         if ($this->_aCfgMR['add_startart_name_to_url'&& isset($this->_sArtName)) {
  453.             if ($this->_sArtName == $this->_aCfgMR['default_startart_name'$this->_aCfgMR['file_extension']{
  454.                 // stored articlename is the default one, remove it mr_get_idart()
  455.                 // will find the real article name
  456.                 $this->_sArtName = null;
  457.             }
  458.         }
  459.  
  460.         if (isset($GLOBALS['idcat']&& isset($this->_sArtName&& !isset($GLOBALS['idart'])) {
  461.             // existing idcat without article name and idart
  462.             $GLOBALS['idart'mr_get_idart($this->_sArtName$GLOBALS['idcat']);
  463.         elseif (!isset($GLOBALS['idcat']&& isset($this->_sArtName&& !isset($GLOBALS['idart'])) {
  464.             // no idcat and idart but article name
  465.             $GLOBALS['idart'mr_get_idart($this->_sArtName);
  466.         }
  467.         if (isset($GLOBALS['idart']&& (!$GLOBALS['idart'|| (int) $GLOBALS['idart'== 0)) {
  468.             if ($this->_aCfgMR['redirect_invalid_article_to_errorsite'== 1{
  469.                 $this->_bError = true;
  470.                 unset($GLOBALS['idart']);
  471.             }
  472.         }
  473.         $GLOBALS['mpDebug']->addDebug($GLOBALS['idart']'ModRewriteController->_setIdart $GLOBALS[\'idart\']');
  474.     }
  475.  
  476.  
  477.     /**
  478.      * Returns state of parts property
  479.      *
  480.      * @return  bool  True if $this->_aParts propery is an array and contains items
  481.      * @access  private
  482.      */
  483.     function _hasPartArrayItems({
  484.         if (is_array($this->_aParts&& count($this->_aParts0{
  485.             return true;
  486.         else {
  487.             return false;
  488.         }
  489.     }
  490.  
  491. }

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