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

Documentation generated on Mon, 26 May 2008 19:42:08 +0200 by phpDocumentor 1.4.0