Source for file actionfactory.php

Documentation is available at actionfactory.php

  1. <?php
  2. /**
  3. * Create Action object that will do whatever category of work is required in this invocation
  4. *
  5. @author     Stuart Prescott
  6. @copyright  Copyright Stuart Prescott
  7. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. @version    $Id$
  9. @package    Bumblebee
  10. @subpackage Actions
  11. *
  12. *  path (bumblebee root)/inc/actions/actionfactory.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. /**  permissions definitions */
  19. require_once 'inc/permissions.php';
  20.  
  21. if (LOAD_ALL_PHP_FILES{
  22.   /**  basic functions (user functions) */
  23.   require_once 'login.php';
  24.   require_once 'logout.php';
  25.   require_once 'view.php';
  26.   require_once 'calendar.php';
  27.   require_once 'book.php';
  28.   require_once 'bookcontact.php';
  29.   require_once 'password.php';
  30.   require_once 'masquerade.php';
  31.  
  32.   // admin functions
  33.     /**  admin functions*/
  34.   require_once 'groups.php';
  35.   require_once 'projects.php';
  36.   require_once 'users.php';
  37.   require_once 'instruments.php';
  38.   require_once 'consumables.php';
  39.   require_once 'consume.php';
  40.   require_once 'deletedbookings.php';
  41.   require_once 'costs.php';
  42.   require_once 'specialcosts.php';
  43.   require_once 'userclass.php';
  44.   require_once 'instrumentclass.php';
  45.   //require_once 'adminconfirm.php';
  46.     require_once 'emaillist.php';
  47.   // require_once 'report.php';
  48.     require_once 'export.php';
  49.   require_once 'billing.php';
  50.   require_once 'backupdatabase.php';
  51.   require_once 'settings.php';
  52. }
  53.  
  54. require_once 'unknownaction.php';
  55. require_once 'inc/typeinfo.php';
  56. require_once 'actions.php';
  57.  
  58. /**
  59. * Factory class for creating Action objects
  60. *
  61. * An Action is a single operation requested by the user. What action is to be performed
  62. * is determined by the action-triage mechanism in the class ActionFactory.
  63. *
  64. * Everything done by an application suite (e.g. edit/create a user) can be reduced to
  65. * broad categories of actions (e.g. edituser) which can be encapsulated within an
  66. * object framework in which every object has the same interface.
  67. *
  68. * The invoking code is thus boiled down to something quite simple:
  69. * <code>
  70. * $action = new ActionFactory($params);
  71. * $action->go();
  72. * </code>
  73. * where ActionFactory does the work of deciding what is to be done on this
  74. * invocation and instantiates the appropriate action object.
  75. *
  76. * The action object then actually performs the tasks desired by the user.
  77. *
  78. * Here, we use the data from the browser (a PATH_INFO variable from the URL in the form
  79. * index.php/user) to triage the transaction.
  80. *
  81. @package    Bumblebee
  82. @subpackage Actions
  83. */
  84. class ActionFactory {
  85.   /** @var string          the user-supplied "verb" (name of the action) e.g. "edituser" */
  86.   var $_verb;
  87.   /** @var string          the actual user-supplied verb... we may pretend to do something else due to permissions  */
  88.   var $_original_verb;
  89.   /**  @var string         Each action has a description associated with it that we will put into the HTML title tag  */
  90.   var $title;
  91.   /**  @var ActionAction   The action object (some descendent of the ActionAction class)   */
  92.   var $_action;
  93.   /**  @var BumblebeeAuth  The user's login credentials object  */
  94.   var $_auth;
  95.   /**  @var array          user-supplied data from the PATH_INFO and GET sections of the URL  */
  96.   var $PDATA;
  97.   /**  @var string         The 'verb' that should follow this current action were a standard workflow being followed   */
  98.   var $nextaction;
  99.   /**  @var array          ActionListing object  */
  100.   var $actionListing;
  101.   /**  @var ActionData     The action data object for this action  */
  102.   var $_actionData;
  103.  
  104.   /**
  105.   * Constructor for the class
  106.   *
  107.   * - Parse the submitted data
  108.   * - work out what action we are supposed to be performing
  109.   * - set up the title tag for the browser
  110.   * - create the ActionAction descendent object that will perform the task
  111.   *
  112.   * @param BumblebeeAuth $auth      user login credentials object
  113.   * @param string        $forceVerb force a particular action
  114.   */
  115.   function ActionFactory($auth$forceVerb=null{
  116.     $this->_auth = $auth;
  117.     #$this->PDATA = $this->_eatPathInfo();
  118.     $this->PDATA = $this->_eatGPCInfo();
  119.     $this->actionListing = new ActionListing();
  120.     $this->_verb = $this->_checkActions($forceVerb);
  121.     $this->_actionData = $this->actionListing->actions[$this->_verb];
  122.     $this->nextaction = $this->_actionData->next_action();
  123.     $this->title = $this->_actionData->title();
  124.     $this->_action = $this->_makeAction();
  125.     $this->_action->readOnly = $this->checkMagic();
  126.   }
  127.  
  128.   /**
  129.   * Fire the action: make things actually happen now
  130.   */
  131.   function go({
  132.     $this->_action->go();
  133.   }
  134.  
  135.   function verb({
  136.     return $this->_verb;
  137.   }
  138.  
  139.   /**
  140.   * Determine what action should be performed.
  141.   *
  142.   * This is done by:
  143.   * - checking that the user is logged in... if not, they *must* login
  144.   * - looking for hints in the user-supplied data for what the correct action is
  145.   * - checking that the user is an admin user if admin functions were requested
  146.   *
  147.   * @param string        $forceVerb  force a particular action
  148.   * @return string                   the name of the action (verb) to be undertaken
  149.   */
  150.   function _checkActions($forceVerb{
  151.     if ($forceVerb !== nullreturn $forceVerb;
  152.  
  153.     $action '';
  154.  
  155.     # first, we need to determine if we are actually logged in or not
  156.     # if we are not logged in, the the action *has* to be 'login'
  157.     if ($this->_auth->isLoggedIn()) return 'login';
  158.  
  159.     #We can have action verbs past to us in three different ways.
  160.     # 1. first PATH_INFO
  161.     # 2. explicit PATH_INFO action=
  162.     # 3. action= form fields
  163.     # Later specifications are used in preference to earlier ones
  164.  
  165.     $explicitaction issetSet($this->PDATA'forceaction');
  166.     $pathaction issetSet($this->PDATA'action');
  167.     $formaction issetSet($_POST'action');
  168.     #$pathaction = $PDATA['action'];
  169.     #$formaction = $_POST['action'];
  170.  
  171.     if ($formaction$action $formaction;
  172.     if ($pathaction$action $pathaction;
  173.     if ($explicitaction$action $explicitaction;
  174.  
  175.     $this->_original_verb = $action;
  176.  
  177.     // dump if unknown action
  178.     if ($this->actionListing->action_exists($action)) {
  179.       return 'unknown';
  180.     }
  181.     // protect admin functions
  182.     if ($this->_auth->permitted($this->actionListing->actions[$action]->permissions)) {
  183.       return 'forbidden!';
  184.     }
  185.  
  186.     # We also need to check to see if we are trying to change privileges
  187.     #if (isset($_POST['changemasq']) && $_POST['changemasq']) return 'masquerade';
  188.  
  189.     return $action;
  190.   }
  191.  
  192.   /**
  193.   * Trigger a restart of the action or a new action
  194.   *
  195.   * Sometimes, an action may need to be restarted or the action changed (e.g. logout => login)
  196.   *
  197.   * @param string $newaction  new verb for the new action
  198.   */
  199.   function _actionRestart($newaction{
  200.     $this->_verb=$newaction;
  201.     $this->_action = $this->_makeAction();
  202.     $this->go();
  203.   }
  204.  
  205.   /**
  206.   * Parse the user-supplied data in PATH_INFO part of URL
  207.   *
  208.   * @returns array  (key => $data)
  209.   */
  210.   function _eatPathInfo({
  211.     $pd array();
  212.     $pathinfo issetSet($_SERVER'PATH_INFO');
  213.     if ($pathinfo{
  214.       $path explode('/'$pathinfo);
  215.       $pd['action'$path[1];
  216.       $actions preg_grep("/^action=/"$path);
  217.       $forceaction array_keys($actions);
  218.       if (isset($forceaction[0])) {
  219.         preg_match("/^action=(.+)/",$path[$forceaction[0]],$m);
  220.         $pd['forceaction'$m[1];
  221.         $max $forceaction[0];
  222.       else {
  223.         $max count($path);
  224.       }
  225.       for($i=2$i<$max$i++{
  226.         $pd[$i-1$path[$i];
  227.       }
  228.     }
  229.     return $pd;
  230.   }
  231.  
  232.   /**
  233.   * Parse the user-supplied data from either the GET or POST data
  234.   *
  235.   * @returns array  (key => $data)
  236.   */
  237.   function _eatGPCInfo({
  238.     //$pd = $this->_eatPathInfo();
  239.     //return array_merge($pd, $_GET, $_POST);
  240.     $data array_merge($_GET$_POST);
  241.     foreach ($data as $k => $v{
  242.       if (preg_match('@^reflection_(.+)$@'$k$matches)) {
  243.         $data[$matches[1]] $v;
  244.         #print "Remapped $matches[1] => $v<br />";
  245.       }
  246.     }
  247.     return $data;
  248.   }
  249.  
  250.   function checkMagic({
  251.     return isset($this->_auth&& $this->_auth->isValidTag(issetSet($_POST'magicTag'NULL));
  252.   }
  253.  
  254.   /**
  255.   * Is it ok to allow the HTML template to dump to the browser from the output buffer?
  256.   *
  257.   * (see BufferedAction descendents)
  258.   *
  259.   * @returns boolean  ok to dump to browser
  260.   */
  261.   function ob_flush_ok({
  262.     return $this->_action->ob_flush_ok;
  263.   }
  264.  
  265.   /**
  266.   * Cause buffered actions to output their data to the browser
  267.   *
  268.   * (see BufferedAction descendents)
  269.   */
  270.   function returnBufferedStream({
  271.     if (method_exists($this->_action'sendBufferedStream')) {
  272.       $this->_action->sendBufferedStream();
  273.     }
  274.   }
  275.  
  276.   /**
  277.   * create the action object (a descendent of ActionAction) for the user-defined verb
  278.   */
  279.   function _makeAction({
  280.     /** to reduce PHP processing overhead, include only the file that is required for this action */
  281.     require_once $this->_actionData->include_file();
  282.     switch ($this->_verb{
  283.       case 'forbidden!':
  284.         return new ActionUnknown($this->_original_verb1);
  285.       case 'unknown':
  286.         return new ActionUnknown($this->_original_verb);
  287.       default:
  288.         $class $this->_actionData->action_class();
  289.         return new $class ($this->_auth$this->PDATA);
  290.     }
  291.   }
  292.  
  293. }
  294.  //class ActionFactory
  295.  
  296. ?>

Documentation generated on Tue, 06 Mar 2007 10:00:28 +0000 by phpDocumentor 1.3.0