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

Documentation generated on Mon, 19 May 2008 22:46:57 +0200 by phpDocumentor 1.4.0