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

Documentation generated on Tue, 25 Nov 2008 22:07:25 +0100 by phpDocumentor 1.4.1