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.             if ((int) $this->_iClientMR !== 0{
  301.                 // set global $GLOBALS['client'] variable
  302.                 $GLOBALS['client'= (int) $this->_iClientMR;
  303.             }
  304.         }
  305.     }
  306.  
  307.  
  308.     /**
  309.      * Preprocesses article name and parts list, sets article name and parts list
  310.      * if settings for usage of categories as a html file is active and valid.
  311.      *
  312.      * @access  private
  313.      */
  314.     function _preprocessVariables({
  315.         // check for html file only links and prepare variables
  316.         if ($this->_sArtName && ModRewrite::validate_setting_categories_as_html()) {
  317.             $aParts explode($this->_aCfgMR['article_seperator']$this->_sArtName);
  318.             if (count($aParts0{
  319.                 $this->_aParts   = (isset($aParts[0]&& strlen($aParts[0]0explode$this->_aCfgMR['category_seperator']$aParts[0false;
  320.                 $this->_sArtName = (isset($aParts[1]&& strlen($aParts[1]0$aParts[1false;
  321.             }
  322.         }
  323.     }
  324.  
  325.  
  326.     /**
  327.      * Sets client id
  328.      *
  329.      * @access  private
  330.      */
  331.     function _setClientId({
  332.         if ($this->_hasPartArrayItems(== false || $this->_aCfgMR['use_client'!== 1{
  333.             return;
  334.         }
  335.  
  336.         if ($this->_aCfgMR['use_client_name'== 1{
  337.             $GLOBALS['changeclient'mr_get_client_id(array_shift($this->_aParts));
  338.             $this->_iClientMR        = $GLOBALS['changeclient'];
  339.         else {
  340.             $GLOBALS['changeclient'= (int) array_shift($this->_aParts);
  341.             $this->_iClientMR        = $GLOBALS['changeclient'];
  342.         }
  343.  
  344.         if (empty($GLOBALS['changeclient']|| (int) $GLOBALS['changeclient'== 0{
  345.             $GLOBALS['changeclient'$GLOBALS['load_client'];
  346.         }
  347.         if (isset($GLOBALS['client']&& $GLOBALS['changeclient'!== $GLOBALS['client']{
  348.             // overwrite existing global $GLOBALS['client'] variable
  349.             $GLOBALS['client'$GLOBALS['changeclient'];
  350.         }
  351. #        $GLOBALS['mpDebug']->addDebug($GLOBALS['changeclient'], 'ModRewriteController::_setClientId() $GLOBALS[\'changeclient\']');
  352. #        $GLOBALS['mpDebug']->addDebug($this->_iClientMR, 'ModRewriteController::_setClientId() $this->_iClientMR');
  353.     }
  354.  
  355.  
  356.     /**
  357.      * Sets language id
  358.      *
  359.      * @access  private
  360.      */
  361.     function _setLanguageId({
  362.         if ($this->_hasPartArrayItems(== false || $this->_aCfgMR['use_language'!== 1{
  363.             return;
  364.         }
  365.  
  366.         if ($this->_aCfgMR['use_language_name'== 1{
  367.             // thanks to Nicolas Dickinson for multi Client/Language BugFix
  368.             $GLOBALS['changelang'mr_get_language_id(array_shift($this->_aParts$this->_iClientMR);
  369.         else {
  370.             $GLOBALS['changelang'= (int) array_shift($this->_aParts);
  371.         }
  372. #        $GLOBALS['mpDebug']->addDebug($GLOBALS['changelang'], 'ModRewriteController::_setLanguageId() $GLOBALS[\'changelang\']');
  373.  
  374.         if (empty($GLOBALS['changelang']|| (int) $GLOBALS['changelang'== 0{
  375.             unset($GLOBALS['changelang']);
  376.         else {
  377.             $GLOBALS['lang'$GLOBALS['changelang'];
  378.         }
  379.     }
  380.  
  381.  
  382.     /**
  383.      * Sets path resolver and category id
  384.      *
  385.      * @access  private
  386.      */
  387.     function _setPathresolverSetting({
  388.  
  389.         if ($this->_hasPartArrayItems(== false{
  390.             return;
  391.         }
  392.  
  393.         $this->_aParts = array_filter($this->_aParts);
  394.  
  395.         if (count($this->_aParts0{
  396.             $this->_sPath = '/' .implode('/'$this->_aParts'/';
  397.         }
  398.  
  399.         // TODO: time to get catid is to long
  400.         if (!isset($GLOBALS['lang'])) {
  401.             if (!isset($GLOBALS['load_lang'])) {
  402.                 // load_client is set in frontend/config.php
  403.                 $GLOBALS['lang'$GLOBALS['load_lang'];
  404.             else {
  405.                 // get client id from table
  406.                 cInclude('classes''contenido/class.clientslang.php');
  407.                 $clCol new cApiClientLanguageCollection();
  408.                 $clCol->setWhere('idclient'$GLOBALS['client']);
  409.                 $clCol->query();
  410.                 if ($clItem $clCol->next()) {
  411.                     $GLOBALS['lang'$clItem->get('idlang');
  412.                 }
  413.             }
  414.         }
  415.         $GLOBALS['idcat'prResolvePathViaURLNames($this->_sPath);
  416.  
  417.         if (!$GLOBALS['idcat'|| (int) $GLOBALS['idcat'== 0{
  418.             // set this flag, it's used in front_content.php
  419.             $this->_bError = true;
  420.             unset($GLOBALS['idcat']);
  421.         else {
  422.             // unset $this->_sPath if $GLOBALS['idcat'] could set, otherwhise it would be resolved again.
  423.             unset($this->_sPath);
  424.         }
  425.         $GLOBALS['mpDebug']->addDebug($GLOBALS['idcat']'ModRewriteController->_setPathresolverSetting $GLOBALS[\'idcat\']');
  426.         $GLOBALS['mpDebug']->addDebug($this->_sPath'ModRewriteController->_setPathresolverSetting $this->_sPath');
  427.     }
  428.  
  429.  
  430.     /**
  431.      * Sets article id
  432.      *
  433.      * @access  private
  434.      */
  435.     function _setIdart({
  436.  
  437.         // startarticle name in url
  438.         if ($this->_aCfgMR['add_startart_name_to_url'&& isset($this->_sArtName)) {
  439.             if ($this->_sArtName == $this->_aCfgMR['default_startart_name'$this->_aCfgMR['file_extension']{
  440.                 // stored articlename is the default one, remove it mr_get_idart()
  441.                 // will find the real article name
  442.                 $this->_sArtName = null;
  443.             }
  444.         }
  445.  
  446.         if (isset($GLOBALS['idcat']&& isset($this->_sArtName&& !isset($GLOBALS['idart'])) {
  447.             // existing idcat without article name and idart
  448.             $GLOBALS['idart'mr_get_idart($this->_sArtName$GLOBALS['idcat']);
  449.         elseif (!isset($GLOBALS['idcat']&& isset($this->_sArtName&& !isset($GLOBALS['idart'])) {
  450.             // no idcat and idart but article name
  451.             $GLOBALS['idart'mr_get_idart($this->_sArtName);
  452.         }
  453.         if (isset($GLOBALS['idart']&& (!$GLOBALS['idart'|| (int) $GLOBALS['idart'== 0)) {
  454.             if ($this->_aCfgMR['redirect_invalid_article_to_errorsite'== 1{
  455.                 $this->_bError = true;
  456.                 unset($GLOBALS['idart']);
  457.             }
  458.         }
  459.         $GLOBALS['mpDebug']->addDebug($GLOBALS['idart']'ModRewriteController->_setIdart $GLOBALS[\'idart\']');
  460.     }
  461.  
  462.  
  463.     /**
  464.      * Returns state of parts property
  465.      *
  466.      * @return  bool  True if $this->_aParts propery is an array and contains items
  467.      * @access  private
  468.      */
  469.     function _hasPartArrayItems({
  470.         if (is_array($this->_aParts&& count($this->_aParts0{
  471.             return true;
  472.         else {
  473.             return false;
  474.         }
  475.     }
  476.  
  477. }

Documentation generated on Sun, 20 Jul 2008 16:26:41 +0200 by phpDocumentor 1.4.0