phpDocumentor ZNF
Action
[ class tree: ZNF ] [ index: ZNF ] [ all elements ]

Source for file RequestProcessor.php

Documentation is available at RequestProcessor.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * PHP version 5
  5.  *
  6.  * This source file is subject to version 2.1 of the GNU Lesser General Public
  7.  * License, that is bundled with this package in the file COPYING, available
  8.  * through the world wide web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html.
  10.  *
  11.  * @package    ZNF
  12.  * @subpackage Action
  13.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  14.  * @author     Graziano Liberati <http://www.liberati.org>
  15.  * @author     Tomasz Kuter <evolic@interia.pl>
  16.  * @copyright  2004-2007 The ZNF Development Team
  17.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  18.  * @version    SVN $Id: RequestProcessor.php 44 2007-07-22 23:55:30Z evolic $
  19.  * @since      Release 0.5.0
  20.  * @link       http://www.zeronotice.org
  21.  */
  22.  
  23. require_once('ZNF/Action/Action.php');
  24. require_once('ZNF/Action/ActionErrors.php');
  25. require_once('ZNF/Action/ActionForm.php');
  26. require_once('ZNF/Action/ActionForward.php');
  27. require_once('ZNF/Action/ActionMapping.php');
  28.  
  29. /**
  30.  * <i>ZNF_Action_RequestProcessor</i> contains the processing logic flow.
  31.  *
  32.  * Subclasses can customize the request processing behavior overriding the
  33.  * methods for which they wish to provide modified functionality.
  34.  *
  35.  * @access     public
  36.  * @package    ZNF
  37.  * @subpackage Action
  38.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  39.  * @author     Graziano Liberati <http://www.liberati.org>
  40.  * @author     Tomasz Kuter <evolic@interia.pl>
  41.  * @copyright  2004-2007 The ZNF Development Team
  42.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  43.  * @version    SVN $Id: RequestProcessor.php 44 2007-07-22 23:55:30Z evolic $
  44.  * @since      Release 0.5.0
  45.  * @link       http://www.zeronotice.org
  46.  */
  47. {
  48.  
  49.     /**
  50.      * The configuration of the module in the application configuration file.
  51.      *
  52.      * @access protected
  53.      * @var array $_appModuleConfig 
  54.      */
  55.     protected $_appModuleConfig;
  56.  
  57.     /**
  58.      * Constructs a new <i>ZNF_Action_RequestProcessor</i> object.
  59.      *
  60.      * @access public
  61.      * @param String $module Name of the module requested
  62.      */
  63.     public function __construct($module)
  64.     {
  65.         $appConfig ZNF_Config_AppConfig::getInstance();
  66.  
  67.         // gets the app module config
  68.         $this->_appModuleConfig = $appConfig->findModule($module);
  69.     }
  70.  
  71.     /**
  72.      * Instruction executed before the execution of the process method.
  73.      *
  74.      * @access public
  75.      * @return boolean 
  76.      */
  77.     public function preProcess()
  78.     {
  79.         return true;
  80.     }
  81.  
  82.     /**
  83.      * Instruction executed after the execution of the process method.
  84.      *
  85.      * @access public
  86.      * @return boolean 
  87.      */
  88.     public function postProcess()
  89.     {
  90.         return true;
  91.     }
  92.  
  93.     /**
  94.      * Processes the action requested with the default processing flow.
  95.      *
  96.      * @access public
  97.      * @param string $path 
  98.      * @return boolean 
  99.      */
  100.     public function process($path)
  101.     {
  102.         // constructs the mapping object
  103.         $mapping $this->_processMapping($path);
  104.  
  105.         // validates the user authorization
  106.         if (!$this->_processRoles($mapping)) {
  107.             return false;
  108.         }
  109.  
  110.         // constructs the form object
  111.         $form $this->_processActionForm($mapping);
  112.  
  113.         // populates the form object with the input data
  114.         $this->_processPopulate($form);
  115.  
  116.         // validates the input data if requested by the configuration file
  117.         if (!$this->_processValidate($form$mapping)) {
  118.             return false;
  119.         }
  120.  
  121.         // checks if in the configuration file is specified to forward to another action
  122.         if ($this->_processForward($mapping)) {
  123.             return true;
  124.         }
  125.  
  126.         // constructs the action object
  127.         $action $this->_processActionCreate($mapping);
  128.  
  129.         // performs the action by the execute method of the action class
  130.         $forward $this->_processActionPerform($action$form$mapping);
  131.  
  132.         // finds forward by the result of the action
  133.         $this->_processForwardConfig($forward$form$mapping);
  134.  
  135.         // end of the processing logic flow
  136.         return true;
  137.     }
  138.  
  139.     /**
  140.      * Extracts the mapping of the action requested from the configuration file.
  141.      *
  142.      * @access protected
  143.      * @param string $path 
  144.      * @return ZNF_Action_ActionMapping 
  145.      */
  146.     protected function _processMapping($path)
  147.     {
  148.         // gets the module configuration
  149.         $moduleConfig ZNF_Config_ModulesConfig::getInstance($this->_appModuleConfig);
  150.  
  151.         $actionConfig $moduleConfig->findActionConfig($path);
  152.         $globalForwardsConfig $moduleConfig->getGlobalForwardsConfig();
  153.  
  154.         if ($actionConfig{
  155.             $mapping new ZNF_Action_ActionMapping($actionConfig$globalForwardsConfig);
  156.         else {
  157.             $mapping new ZNF_Action_ActionMapping($moduleConfig->findActionConfig('default')$globalForwardsConfig);
  158.         }
  159.  
  160.         return $mapping;
  161.     }
  162.  
  163.     /**
  164.      * Validates the authorization of the user to access to the action requested.
  165.      *
  166.      * @access protected
  167.      * @param ZNF_Action_ActionMapping $mapping 
  168.      * @return boolean 
  169.      */
  170.     protected function _processRoles($mapping)
  171.     {
  172.         // gets roles
  173.         $roles $mapping->getRoles();
  174.  
  175.         if (!$roles{
  176.             // for this action is not required a role
  177.             return true;
  178.         }
  179.  
  180.         if(isset($_SESSION['znf']['roles'])) {
  181.             $rolesArray explode(','$roles);
  182.  
  183.             foreach ($_SESSION['znf']['roles'as $current{
  184.  
  185.                 if (in_array($current$rolesArray)) {
  186.                     // the user has a role to execute this action
  187.                     return true;
  188.                 }
  189.             }
  190.         }
  191.  
  192.         // there are no default roles associated to anonymous user or the roles
  193.         // are not matched, authentication is required
  194.         if ($_SESSION['znf']['authenticated'== false{
  195.             // force to recreateURL after successfull login
  196.             $_SESSION['znf']['recreateURL'true;
  197.             $_SESSION['znf']['requestURL'$_SERVER['REQUEST_URI'];
  198.  
  199.             $appConfig ZNF_Config_AppConfig::getInstance();
  200.  
  201.             // gets the authentication configuration
  202.             $authConfig $appConfig->findAuthConfig();
  203.  
  204.             if (ereg('//'$authConfig['path'])) {
  205.                 // authentication path is set to an action of another module
  206.  
  207.                 // extract module and action name
  208.                 $moduleAction explode('//'$authConfig['path']);
  209.  
  210.                 $znf new ZNF();
  211.  
  212.                 // update module and action in the request
  213.                 $znf->updateModule($moduleAction[0]);
  214.                 $znf->updateAction($moduleAction[1]);
  215.  
  216.                 $znf->process();
  217.  
  218.             else if (ereg('/'$authConfig['path'])) {
  219.                 // authentication path is set to a view
  220.  
  221.                 $authConfig['path'ZNF::MODULES_DIR '/' $authConfig['path'];
  222.  
  223.                 if (!file_exists($authConfig['path'])) {
  224.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  225.                     throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadView'));
  226.                 }
  227.  
  228.                 require_once($authConfig['path']);
  229.             else {
  230.                 // authentication path is set to an action
  231.                 $this->process($authConfig['path']);
  232.             }
  233.  
  234.             return false;
  235.         else {
  236.             $appConfig ZNF_Config_AppConfig::getInstance();
  237.  
  238.             // gets the wrong authentication configuration
  239.             $wrongAuthConfig $appConfig->findWrongAuthConfig();
  240.  
  241.             if (ereg('//'$wrongAuthConfig['path'])) {
  242.                 // wrong authentication path is set to an action of another module
  243.  
  244.                 // extract module and action name
  245.                 $moduleAction explode('//'$wrongAuthConfig['path']);
  246.  
  247.                 $znf new ZNF();
  248.  
  249.                 // update module and action in the request
  250.                 $znf->updateModule($moduleAction[0]);
  251.                 $znf->updateAction($moduleAction[1]);
  252.  
  253.                 $znf->process();
  254.  
  255.             else if (ereg('/'$wrongAuthConfig['path'])) {
  256.                 // authentication path is set to a view
  257.                 $wrongAuthConfig['path'ZNF::MODULES_DIR '/' $wrongAuthConfig['path'];
  258.  
  259.                 if (!file_exists($wrongAuthConfig['path'])) {
  260.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  261.                     throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadView'));
  262.                 }
  263.  
  264.                 require_once($wrongAuthConfig['path']);
  265.             else {
  266.                 // wrong authentication path is set to an action
  267.                 $this->process($wrongAuthConfig['path']);
  268.             }
  269.  
  270.             return false;
  271.         }
  272.  
  273.         // the user is authenticated and he has not a role to execute this
  274.         // action
  275.         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  276.         throw new ZNF_Action_RequestProcessorException($translation->get('errorAuthorizationRequired'));
  277.     }
  278.  
  279.     /**
  280.      * If the <i>name</i> property of the <i>ZNF_Action_ActionMapping</i> is found constructs a <i>ZNF_Action_ActionForm</i> object to process the input data.
  281.      *
  282.      * If the package of which the <i>ZNF_Action_ActionForm</i> class belongs is
  283.      * not found a <i>ZNF_Action_RequestProcessorException</i> is raised.
  284.      *
  285.      * @access protected
  286.      * @param ZNF_Action_ActionMapping $mapping 
  287.      * @return ZNF_Action_ActionForm 
  288.      */
  289.     protected function _processActionForm($mapping)
  290.     {
  291.         // gets the name of the form
  292.         $name $mapping->getName();
  293.  
  294.         if (!$name{
  295.             // no form name found
  296.             return null;
  297.         }
  298.  
  299.         // gets the module configuration
  300.         $moduleConfig ZNF_Config_ModulesConfig::getInstance($this->_appModuleConfig);
  301.  
  302.         // extracts the configuration of the form
  303.         $formBeanConfig $moduleConfig->findFormBeanConfig($name);
  304.  
  305.         if (!$formBeanConfig{
  306.             // no form configuration found
  307.             return null;
  308.         }
  309.  
  310.         $form null;
  311.         $scope $mapping->getScope();
  312.  
  313.         // try to load a pre-existing form from the request or session
  314.         if ($scope == 'request'{
  315.             if (is_object($_REQUEST['znf']['form'])) {
  316.                 $form $_REQUEST['znf']['form'];
  317.             }
  318.         elseif ($scope == 'session'{
  319.             if (is_object($_SESSION['znf']['form'])) {
  320.                 $form $_SESSION['znf']['form'];
  321.             }
  322.         }
  323.  
  324.         if (is_object($form)) {
  325.             return $form;
  326.         }
  327.  
  328.         // constructs the ZNF_Action_ActionForm object
  329.         $type $formBeanConfig['type'];
  330.         $file ZNF::MODULES_DIR '/' preg_replace('/_/''/'$type'.php';
  331.  
  332.         if (!file_exists($file)) {
  333.             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  334.             throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadActionForm'));
  335.         }
  336.  
  337.         // requires the file of the of the ZNF_Action_ActionForm
  338.         require_once($file);
  339.         $form new $type();
  340.  
  341.         if ($scope == 'request'{
  342.             $_REQUEST['znf']['form'$form;
  343.         elseif ($scope == 'session'{
  344.             $_SESSION['znf']['form'$form;
  345.         }
  346.  
  347.         return $form;
  348.     }
  349.  
  350.     /**
  351.      * Populates internal properties of the <i>ZNF_Action_ActionForm</i> object with the input data.
  352.      *
  353.      * @access protected
  354.      * @param ZNF_Action_ActionForm $form 
  355.      * @return boolean 
  356.      */
  357.     protected function _processPopulate($form)
  358.     {
  359.         if (!$form{
  360.             // form not set
  361.             return true;
  362.         }
  363.  
  364.         return $form->populate();
  365.     }
  366.  
  367.     /**
  368.      * Validates input data.
  369.      *
  370.      * @access protected
  371.      * @param ZNF_Action_ActionForm $form 
  372.      * @param ZNF_Action_ActionMapping $mapping 
  373.      * @return boolean 
  374.      */
  375.     protected function _processValidate($form$mapping)
  376.     {
  377.         if ($form && $mapping->getValidate(== 'true'{
  378.  
  379.             // validates input data
  380.             $errors $form->validate();
  381.  
  382.             if ($errors->getNumErrors()) {
  383.  
  384.                 $_REQUEST['errors'$errors;
  385.                 $input $mapping->getInput();
  386.  
  387.                 if (ereg('//'$input)) {
  388.                     // forward is set to an action of another module
  389.  
  390.                     // extract module and action name
  391.                     $moduleAction explode('//'$input);
  392.  
  393.                     $znf new ZNF();
  394.  
  395.                     // update module and action in the request
  396.                     $znf->updateModule($moduleAction[0]);
  397.                     $znf->updateAction($moduleAction[1]);
  398.  
  399.                     $znf->process();
  400.  
  401.                 else if (ereg('/'$input)) {
  402.                     // forward is set to a view
  403.  
  404.                     $input ZNF::MODULES_DIR '/' $input;
  405.  
  406.                     if (!file_exists($input)) {
  407.                         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  408.                         throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadView'));
  409.                     }
  410.  
  411.                     require_once($input);
  412.                 else {
  413.                     // forward is set to an action
  414.                     $this->process($input);
  415.                 }
  416.                 return false;
  417.             }
  418.  
  419.         }
  420.  
  421.         return true;
  422.     }
  423.  
  424.     /**
  425.      * If the <i>forward</i> attribute for the current action is found calls <i>ZNF_Action_RequestProcessor->process()</i> with the forward found and break the current proccess flow.
  426.      *
  427.      * @access protected
  428.      * @param ZNF_Action_ActionMapping $mapping 
  429.      * @return boolean 
  430.      */
  431.     protected function _processForward($mapping)
  432.     {
  433.         $forward $mapping->getForward();
  434.  
  435.         if (!$forward{
  436.             // forward not set
  437.             return false;
  438.         }
  439.  
  440.         if (ereg('//'$forward)) {
  441.             // forward is set to an action of another module
  442.  
  443.             // extract module and action name
  444.             $moduleAction explode('//'$forward);
  445.  
  446.             $znf new ZNF();
  447.  
  448.             // update module and action in the request
  449.             $znf->updateModule($moduleAction[0]);
  450.             $znf->updateAction($moduleAction[1]);
  451.  
  452.             $znf->process();
  453.  
  454.         else if (ereg('/'$forward)) {
  455.             // forward is set to a view
  456.  
  457.             $forward ZNF::MODULES_DIR '/' $forward;
  458.  
  459.             if (!file_exists($forward)) {
  460.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  461.                 throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadView'));
  462.             }
  463.  
  464.             require_once($forward);
  465.         else {
  466.             // forward is set to an action
  467.             $this->process($forward);
  468.         }
  469.  
  470.         return true;
  471.     }
  472.  
  473.     /**
  474.      * Finds the <i>ZNF_Action_Action</i> class to handle the action.
  475.      *
  476.      * Constructs an instance and returns it to the
  477.      * <i>ZNF_Action_RequestProcessor->process()</i> method.
  478.      *
  479.      * @access protected
  480.      * @param ZNF_Action_ActionMapping $mapping 
  481.      * @return ZNF_Action_Action 
  482.      */
  483.     protected function _processActionCreate($mapping)
  484.     {
  485.         $type $mapping->getType();
  486.         $file ZNF::MODULES_DIR '/' preg_replace('/_/''/'$type'.php';
  487.  
  488.         if (!file_exists($file)) {
  489.             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  490.             throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadAction'));
  491.         }
  492.  
  493.         // requires the file of the controller of the action
  494.         require_once($file);
  495.  
  496.         return new $type();
  497.     }
  498.  
  499.     /**
  500.      * Calls the <i>ZNF_Action_Action->execute()</i> method and returns the result to the <i>ZNF_Action_RequestProcessor->process()</i> method.
  501.      *
  502.      * @access protected
  503.      * @param ZNF_Action_Action $action 
  504.      * @param ZNF_Action_ActionForm $form 
  505.      * @param ZNF_Action_ActionMapping $mapping 
  506.      * @return ZNF_Action_ActionForward 
  507.      */
  508.     protected function _processActionPerform($action$form$mapping)
  509.     {
  510.         return $action->execute($form$mapping);
  511.     }
  512.  
  513.     /**
  514.      * Forwards or redirects to the specified destination, by the specified mechanism.
  515.      *
  516.      * @access protected
  517.      * @param ZNF_Action_ActionForward $forward 
  518.      * @param ZNF_Action_ActionForm $form 
  519.      * @param ZNF_Action_ActionMapping $mapping 
  520.      * @return boolean 
  521.      */
  522.     protected function _processForwardConfig($forward$form$mapping)
  523.     {
  524.         if (!$forward{
  525.             // forward not set
  526.             return false;
  527.         }
  528.  
  529.         $path $forward->getPath();
  530.  
  531.         if (!$path{
  532.             // path not set
  533.             return false;
  534.         }
  535.  
  536.         $pathArray null;
  537.  
  538.         // check if the forward refer to an action of another module
  539.         if (ereg('//'$path)) {
  540.             $pathArray explode('//'$path);
  541.         }
  542.  
  543.         $redirect $forward->getRedirect();
  544.  
  545.         if ($redirect == 'true'{
  546.  
  547.             // checks if https was used
  548.             if (isset($_SERVER['HTTPS'])) {
  549.                 $location 'https://';
  550.             else {
  551.                 $location 'http://';
  552.             }
  553.  
  554.             $location .= "{$_SERVER['SERVER_NAME']}";
  555.  
  556.             if ($_SERVER['SERVER_PORT'!= '80' && $_SERVER['SERVER_PORT'!= '443'{
  557.                 $location .= ":{$_SERVER['SERVER_PORT']}";
  558.             }
  559.  
  560.             $location .= $_SERVER['SCRIPT_NAME'];
  561.  
  562.             // makes the browser reloads the new url
  563.             if ($pathArray{
  564.                 header('Location: ' "{$location}?ZNF::MODULE_INDEX "={$pathArray[0]}"&amp;" ZNF::ACTION_INDEX "={$pathArray[1]}");
  565.             else {
  566.                 header('Location: ' "{$location}?ZNF::ACTION_INDEX "={$path}");
  567.             }
  568.  
  569.         elseif (ereg('//'$path)) {
  570.             // forward is set to an action of another module
  571.  
  572.             $znf new ZNF();
  573.  
  574.             // update module and action in the request
  575.             $znf->updateModule($pathArray[0]);
  576.             $znf->updateAction($pathArray[1]);
  577.  
  578.             $znf->process();
  579.  
  580.         elseif (ereg('/'$path)) {
  581.             // forwards to a view
  582.  
  583.             $path ZNF::MODULES_DIR '/' $path;
  584.  
  585.             if (!file_exists($path)) {
  586.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  587.                 throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadView'));
  588.             }
  589.  
  590.             require_once($path);
  591.         else {
  592.             // forwards to an action
  593.             $this->process($path);
  594.         }
  595.  
  596.         return true;
  597.     }
  598.  
  599.     /**
  600.      * Handles the exception.
  601.      *
  602.      * @access protected
  603.      * @param Exception $e 
  604.      * @param ZNF_Action_ActionForm $form 
  605.      * @param ZNF_Action_ActionMapping $mapping 
  606.      * @return boolean 
  607.      */
  608.     protected function _processException($e$form$mapping)
  609.     {
  610.         $e->getTraceAsString();
  611.         return true;
  612.     }
  613.  
  614.     /**
  615.      * Destroys the <i>ZNF_Action_RequestProcessor</i> object.
  616.      *
  617.      * @access public
  618.      */
  619.     public function __destruct()
  620.     {
  621.     }
  622.  
  623. }
  624.  
  625. /**
  626.  * <i>ZNF_Action_RequestProcessorException</i> is the exception type for the <i>ZNF_Action_RequestProcessor</i> class.
  627.  *
  628.  * <i>ZNF_Action_RequestProcessorException</i> extends the <i>Exception</i> class of PHP5.
  629.  *
  630.  * @access     public
  631.  * @package    ZNF
  632.  * @subpackage Action
  633.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  634.  * @author     Graziano Liberati <http://www.liberati.org>
  635.  * @copyright  2004-2007 The ZNF Development Team
  636.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  637.  * @version    SVN $Id: RequestProcessor.php 44 2007-07-22 23:55:30Z evolic $
  638.  * @since      Release 0.5.0
  639.  * @link       http://www.zeronotice.org
  640.  */
  641. class ZNF_Action_RequestProcessorException extends Exception
  642. {
  643. }
  644.  
  645. ?>

Documentation generated on Wed, 14 Nov 2007 23:47:40 +0100 by phpDocumentor 1.4.0