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. defined('CON_FRAMEWORK'or die('Illegal call');
  13.  
  14.  
  15. /**
  16.  * Mod Rewrite controller class. Extracts url parts and sets some necessary globals like:
  17.  * - $idart
  18.  * - $idcat
  19.  * - $client
  20.  * - $changeclient
  21.  * - $lang
  22.  * - $changelang
  23.  *
  24.  * @author      Murat Purc <murat@purc.de>
  25.  * @date        16.04.2008
  26.  * @package     Contenido
  27.  * @subpackage  ModRewrite
  28.  */
  29.  
  30.     /**
  31.      * Extracted request uri path parts by path separator '/'
  32.      *
  33.      * @var array 
  34.      */
  35.     private $_aParts;
  36.  
  37.     /**
  38.      * Extracted article name from request uri
  39.      *
  40.      * @var string 
  41.      */
  42.     private $_sArtName;
  43.  
  44.     /**
  45.      * Remaining path for path resolver (see $GLOBALS['path'])
  46.      *
  47.      * @var string 
  48.      */
  49.     private $_sPath;
  50.  
  51.     /**
  52.      * Incomming URL
  53.      *
  54.      * @var string 
  55.      */
  56.     private $_sIncommingUrl;
  57.  
  58.     /**
  59.      * Resolved URL
  60.      *
  61.      * @var string 
  62.      */
  63.     private $_sResolvedUrl;
  64.  
  65.     /**
  66.      * Client id used by this class
  67.      *
  68.      * @var int 
  69.      */
  70.     private $_iClientMR;
  71.  
  72.     /**
  73.      * Flag about occured errors
  74.      *
  75.      * @var bool 
  76.      */
  77.     private $_bError = false;
  78.  
  79.     /**
  80.      * Flag about found routing definition
  81.      *
  82.      * @var bool 
  83.      */
  84.     private $_bRoutingFound = false;
  85.  
  86.  
  87.     /**
  88.      * Constructor, sets several properties.
  89.      *
  90.      * @param  string  $incommingUrl  Incomming URL
  91.      */
  92.     public function __construct($incommingUrl{
  93.         parent::initialize();
  94.         $this->_sIncommingUrl = $incommingUrl;
  95.         $this->_aParts        = array();
  96.     }
  97.  
  98.  
  99.     /**
  100.      * Getter for overwritten client id (see $GLOBALS['client'])
  101.      *
  102.      * @return  int  Client id
  103.      */
  104.     public function getClient({
  105.         return parent::$_oGlobals->get('client');
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Getter for overwritten change client id (see $GLOBALS['changeclient'])
  111.      *
  112.      * @return  int  Change client id
  113.      */
  114.     public function getChangeClient({
  115.         return parent::$_oGlobals->get('changeclient');
  116.     }
  117.  
  118.  
  119.     /**
  120.      * Getter for article id (see $GLOBALS['idart'])
  121.      *
  122.      * @return  int  Article id
  123.      */
  124.     public function getIdArt({
  125.         return parent::$_oGlobals->get('idart');
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Getter for category id (see $GLOBALS['idcat'])
  131.      *
  132.      * @return  int  Category id
  133.      */
  134.     public function getIdCat({
  135.         return parent::$_oGlobals->get('idcat');
  136.     }
  137.  
  138.  
  139.     /**
  140.      * Getter for language id (see $GLOBALS['lang'])
  141.      *
  142.      * @return  int  Language id
  143.      */
  144.     public function getLang({
  145.         return parent::$_oGlobals->get('lang');
  146.     }
  147.  
  148.  
  149.     /**
  150.      * Getter for change language id (see $GLOBALS['change_lang'])
  151.      *
  152.      * @return  int  Change language id
  153.      */
  154.     public function getChangeLang({
  155.         return parent::$_oGlobals->get('changelang');
  156.     }
  157.  
  158.  
  159.     /**
  160.      * Getter for path (see $GLOBALS['path'])
  161.      *
  162.      * @return  string  Path, used by path resolver
  163.      */
  164.     public function getPath({
  165.         return $this->_sPath;
  166.     }
  167.  
  168.  
  169.     /**
  170.      * Getter for resolved url
  171.      *
  172.      * @return  string  Resolved url
  173.      */
  174.     public function getResolvedUrl({
  175.         return $this->_sResolvedUrl;
  176.     }
  177.  
  178.  
  179.     /**
  180.      * Returns a flag about found routing definition
  181.      *
  182.      * return  bool  Flag about found routing
  183.      */
  184.     public function getRoutingFoundState({
  185.         return $this->_bRoutingFound;
  186.     }
  187.  
  188.  
  189.     /**
  190.      * Getter for occured error state
  191.      *
  192.      * @return  bool  Flag for occured error
  193.      */
  194.     public function errorOccured(){
  195.         return $this->_bError;
  196.     }
  197.  
  198.  
  199.     /**
  200.      * Main function to call for mod rewrite related preprocessing jobs.
  201.      *
  202.      * Executes some private functions to extract request URI and to set needed membervariables
  203.      * (client, language, article id, category id, etc.)
  204.      */
  205.     public function execute({
  206.         if (parent::isEnabled(== false{
  207.             return;
  208.         }
  209.  
  210.         $this->_extractRequestUri();
  211.  
  212.         $this->_initializeClientId();
  213.  
  214.         $this->_setClientId();
  215.  
  216.         mr_loadConfiguration($this->_iClientMR);
  217.  
  218.         $this->_setLanguageId();
  219.  
  220.         // second call after setting client and language
  221.         $this->_extractRequestUri(true);
  222.  
  223.         $this->_setPathresolverSetting();
  224.  
  225.         $this->_setIdart();
  226.  
  227.         parent::$_oDebug->addDebug($this->_aParts'ModRewriteController::execute() _setIdart');
  228.  
  229.         $this->_postValidation();
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Extracts request URI and sets member variables $this->_sArtName and $this->_aParts
  235.      *
  236.      * @param  bool $secondCall  Flag about secont call of this function, is needed
  237.      *                            to re extract url if a routing definition was found
  238.      */
  239.     private function _extractRequestUri($secondCall=false{
  240.         // check for defined rootdir
  241.         if (parent::getConfig('rootdir'!== '/' && strpos($_SERVER['REQUEST_URI']$this->_sIncommingUrl=== 0{
  242.             $this->_sIncommingUrl = str_replace(parent::getConfig('rootdir')'/'$this->_sIncommingUrl);
  243.         }
  244.  
  245.         $aUrlComponents $this->_parseUrl($this->_sIncommingUrl);
  246.  
  247.         if (isset($aUrlComponents['path'])) {
  248.  
  249.             if ($secondCall == true{
  250.  
  251. #        parent::$_oDebug->addDebug($aUrlComponents, 'ModRewriteController::_extractRequestUri() 2. call $aUrlComponents');
  252.                 // @todo: implement real redirect of old front_content.php style urls
  253.  
  254.                 // check for routing definition
  255.                 $routings parent::getConfig('routing');
  256.                 if (is_array($routings&& isset($routings[$aUrlComponents['path']])) {
  257.                     $aUrlComponents['path'$routings[$aUrlComponents['path']];
  258.                     if (strpos($aUrlComponents['path']'front_content.php'!== false{
  259.                         // routing destination contains front_content.php
  260.  
  261.                         $this->_bRoutingFound = true;
  262.  
  263.                         // set client language, if not set before
  264.                         mr_setClientLanguageId(parent::$_oGlobals->get('client'));
  265.  
  266.                         //rebuild URL
  267.                         $url mr_buildNewUrl($aUrlComponents['path']);
  268.  
  269.                         $aUrlComponents $this->_parseUrl($url);
  270.  
  271.                         $this->_aParts = array();
  272.                     }
  273.                 else {
  274.                     return;
  275.                 }
  276.             }
  277.  
  278.             $aPaths explode('/'$aUrlComponents['path']);
  279.             foreach ($aPaths as $p => $item{
  280.                 if (!empty($item)) {
  281.                     // pathinfo would also work
  282.                     $arr   explode('.'$item);
  283.                     $count count($arr);
  284.                     if ($count && '.' strtolower($arr[$count-1]== parent::getConfig('file_extension')) {
  285.                         array_pop($arr);
  286.                         $this->_sArtName = implode('.'$arr);
  287.                     else {
  288.                         $this->_aParts[$item;
  289.                     }
  290.                 }
  291.             }
  292.  
  293.             if ($secondCall == true{
  294.                 // reprocess extracting client and language
  295.                 $this->_setClientId();
  296.                 mr_loadConfiguration($this->_iClientMR);
  297.                 $this->_setLanguageId();
  298.             }
  299.  
  300.         }
  301.         parent::$_oDebug->addDebug($this->_aParts'ModRewriteController::_extractRequestUri() $this->_aParts');
  302.  
  303.         // loop parts array and remove existing 'front_content.php'
  304.         if ($this->_hasPartArrayItems()) {
  305.             foreach($this->_aParts as $p => $item{
  306.                 if ($item == 'front_content.php'{
  307.                     unset($this->_aParts[$p]);
  308.                 }
  309.             }
  310.         }
  311.  
  312.         // set parts property top null, if needed
  313.         if ($this->_hasPartArrayItems(== false{
  314.             $this->_aParts = null;
  315.         }
  316.  
  317.         // set artname to null if needed
  318.         if (!isset($this->_sArtName|| empty($this->_sArtName|| strlen($this->_sArtName== 0{
  319.             $this->_sArtName = null;
  320.         }
  321.  
  322.     }
  323.  
  324.  
  325.     /**
  326.      * Tries to initialize the client id
  327.      */
  328.     private function _initializeClientId({
  329.         $client       parent::$_oGlobals->get('client'0);
  330.         $changeclient parent::$_oGlobals->get('changeclient'0);
  331.  
  332.         $this->_iClientMR = 0;
  333.         if ($client && $changeclient == 0{
  334.             $this->_iClientMR = $client;
  335.         elseif ($changeclient 0{
  336.             $this->_iClientMR = $changeclient;
  337.         else {
  338.             $this->_iClientMR = parent::$_oGlobals->get('load_client');
  339.         }
  340.  
  341.         if ((int) $this->_iClientMR > 0{
  342.             // set global client variable
  343.             parent::$_oGlobals->set('client'(int) $this->_iClientMR);
  344.         }
  345.     }
  346.  
  347.  
  348.     /**
  349.      * Sets client id
  350.      */
  351.     private function _setClientId({
  352.         if ($this->_hasPartArrayItems(== false || parent::getConfig('use_client'!== 1{
  353.             return;
  354.         }
  355.  
  356.         $client       parent::$_oGlobals->get('client'0);
  357.         $changeclient parent::$_oGlobals->get('changeclient'0);
  358.  
  359.         if (parent::getConfig('use_client_name'== 1{
  360.             $changeclient     ModRewrite::getClientId(array_shift($this->_aParts));
  361.             $this->_iClientMR = $changeclient;
  362.         else {
  363.             $changeclient     = (int) array_shift($this->_aParts);
  364.             $this->_iClientMR = $changeclient;
  365.         }
  366.  
  367.         if (empty($changeclient|| (int) $changeclient == 0{
  368.             $changeclient parent::$_oGlobals->get('load_client');
  369.         }
  370.         if ($client && $changeclient !== $client{
  371.             // overwrite existing client variable
  372.             $this->_iClientMR = $changeclient;
  373.             parent::$_oGlobals->set('client'$changeclient);
  374.         }
  375.  
  376.         parent::$_oGlobals->set('changeclient'$changeclient);
  377.     }
  378.  
  379.  
  380.     /**
  381.      * Sets language id
  382.      */
  383.     private function _setLanguageId({
  384.         if ($this->_hasPartArrayItems(== false || parent::getConfig('use_language'!== 1{
  385.             return;
  386.         }
  387.  
  388.         if (parent::getConfig('use_language_name'== 1{
  389.             // thanks to Nicolas Dickinson for multi Client/Language BugFix
  390.             $changelang ModRewrite::getLanguageId(array_shift($this->_aParts$this->_iClientMR);
  391.         else {
  392.             $changelang = (int) array_shift($this->_aParts);
  393.         }
  394.  
  395.         if ((int) $changelang 0{
  396.             parent::$_oGlobals->set('lang'$changelang);
  397.             parent::$_oGlobals->set('changelang'$changelang);
  398.         }
  399.     }
  400.  
  401.  
  402.     /**
  403.      * Sets path resolver and category id
  404.      */
  405.     private function _setPathresolverSetting({
  406.         if ($this->_hasPartArrayItems(== false{
  407.             return;
  408.         }
  409.  
  410.         $this->_sPath = '/' implode('/'$this->_aParts'/';
  411.  
  412.         $lang parent::$_oGlobals->get('lang');
  413.  
  414.         if ($lang == null{
  415.             if (parent::$_oGlobals->get('load_lang'00{
  416.                 // load_client is set in frontend/config.php
  417.  
  418.                 $lang parent::$_oGlobals->get('load_lang');
  419.             else {
  420.                 // get client id from table
  421.                 cInclude('classes''contenido/class.clientslang.php');
  422.                 $clCol new cApiClientLanguageCollection();
  423.                 $clCol->setWhere('idclient'parent::$_oGlobals->get('client'));
  424.                 $clCol->query();
  425.                 if ($clItem $clCol->next()) {
  426.                     $lang $clItem->get('idlang');
  427.                 }
  428.             }
  429.             parent::$_oGlobals->set('lang'$lang);
  430.         }
  431.  
  432.         $idcat = (int) ModRewrite::getCatIdByUrlPath($this->_sPath);
  433.  
  434.         if ($idcat == 0{
  435.             // category couldn't resolved
  436.             $this->_bError = true;
  437.             $idcat null;
  438.         else {
  439.             // unset $this->_sPath if $idcat could set, otherwhise it would be resolved again.
  440.             unset($this->_sPath);
  441.         }
  442.  
  443.         parent::$_oGlobals->set('idcat'$idcat);
  444.  
  445.         parent::$_oDebug->addDebug($idcat'ModRewriteController->_setPathresolverSetting $idcat');
  446.         parent::$_oDebug->addDebug($this->_sPath'ModRewriteController->_setPathresolverSetting $this->_sPath');
  447.     }
  448.  
  449.  
  450.     /**
  451.      * Sets article id
  452.      */
  453.     private function _setIdart({
  454.         // startarticle name in url
  455.         if (parent::getConfig('add_startart_name_to_url'&& isset($this->_sArtName)) {
  456.             if ($this->_sArtName == parent::getConfig('default_startart_name')) {
  457.                 // stored articlename is the default one, remove it ModRewrite::getArtIdByWebsafeName()
  458.                 // will find the real article name
  459.                 $this->_sArtName = null;
  460.             }
  461.         }
  462.  
  463.         $idcat parent::$_oGlobals->get('idcat');
  464.         $idart parent::$_oGlobals->get('idart');
  465.  
  466.         if ($idcat !== null && $this->_sArtName && $idart == null{
  467.             // existing idcat with article name and no idart
  468.             $idart ModRewrite::getArtIdByWebsafeName($this->_sArtName$idcat);
  469.         elseif ($idcat && $this->_sArtName == null && $idart == null{
  470.  
  471.             if (parent::getConfig('add_startart_name_to_url'&& parent::getConfig('default_startart_name'== ''{
  472.  
  473.                 // existing idcat without article name and idart
  474.                 cInclude('classes''class.article.php');
  475.                 $artColl new ArticleCollection(array('idcat' => $idcat'start' => 1));
  476.                 if ($artItem $artColl->startArticle()) {
  477.                     $idart $artItem->get('idart');
  478.                 }
  479.  
  480.             }
  481.  
  482.         elseif ($idcat == null && $idart == null && isset($this->_sArtName)) {
  483.             // no idcat and idart but article name
  484.             $idart ModRewrite::getArtIdByWebsafeName($this->_sArtName);
  485.         }
  486.  
  487.         if ($idart !== null && (!$idart || (int) $idart == 0)) {
  488.             if (parent::getConfig('redirect_invalid_article_to_errorsite'== 1{
  489.                 $this->_bError = true;
  490.                 $idart null;
  491.             }
  492.         }
  493.  
  494.         parent::$_oGlobals->set('idart'$idart);
  495.  
  496.         parent::$_oDebug->addDebug($idart'ModRewriteController->_setIdart $idart');
  497.     }
  498.  
  499.  
  500.     /**
  501.      * Does post validation of the extracted data.
  502.      *
  503.      * One main goal of this function is to prevent duplicated content, which could happen, if
  504.      * the configuration 'startfromroot' is activated.
  505.      */
  506.     private function _postValidation({
  507.         if ($this->_bError || $this->_bRoutingFound || !$this->_hasPartArrayItems()) {
  508.             return;
  509.         }
  510.  
  511.         if (parent::getConfig('startfromroot'== && parent::getConfig('prevent_duplicated_content'== 1{
  512.  
  513.             // prevention of duplicated content if '/firstcat/' is directly requested!
  514.  
  515.             $idcat parent::$_oGlobals->get('idcat');
  516.             $idart parent::$_oGlobals->get('idart');
  517.  
  518.             // compose new parameter
  519.             $param '';
  520.             if ($idcat{
  521.                 $param .= 'idcat=' . (int) $idcat;
  522.             }
  523.             if ($idart{
  524.                 $param .= ($param !== '''&idart=' . (int) $idart 'idart=' . (int) $idart;
  525.             }
  526.  
  527.             if ($param == ''{
  528.                 return;
  529.             }
  530.  
  531.             // set client language, if not set before
  532.             mr_setClientLanguageId(parent::$_oGlobals->get('client'));
  533.  
  534.             //rebuild url
  535.             $url mr_buildNewUrl('front_content.php?' $param);
  536.  
  537.             $aUrlComponents @parse_url($this->_sIncommingUrl);
  538.             $incommingUrl   (isset($aUrlComponents['path'])) $aUrlComponents['path''';
  539.  
  540.             parent::$_oDebug->addDebug($url'ModRewriteController->_postValidation validate url');
  541.             parent::$_oDebug->addDebug($incommingUrl'ModRewriteController->_postValidation incommingUrl');
  542.  
  543.             // now the new generated uri should be identical with the request uri
  544.             if ($incommingUrl !== $url{
  545.                 $this->_bError = true;
  546.                 parent::$_oGlobals->set('idcat'null);
  547.             }
  548.         }
  549.     }
  550.  
  551.  
  552.     /**
  553.      * Parses the url using defined separators
  554.      *
  555.      * @param   string  $url  Incoming url
  556.      * @return  string  Parsed url
  557.      */
  558.     private function _parseUrl($url{
  559.         $this->_sResolvedUrl = $url;
  560.  
  561.         $oMrUrlUtil ModRewriteUrlUtil::getInstance();
  562.         $url $oMrUrlUtil->toContenidoUrl($url);
  563.  
  564.         return @parse_url($url);
  565.     }
  566.  
  567.  
  568.     /**
  569.      * Returns state of parts property.
  570.      *
  571.      * @return  bool  True if $this->_aParts propery is an array and contains items
  572.      * @access  private
  573.      */
  574.     function _hasPartArrayItems({
  575.         if (is_array($this->_aParts&& count($this->_aParts0{
  576.             return true;
  577.         else {
  578.             return false;
  579.         }
  580.     }
  581.  
  582. }

Documentation generated on Sun, 21 Dec 2008 21:42:35 +0100 by phpDocumentor 1.4.1