ActionHandler.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: Jason Rust <jrust@codejanitor.com>                          |
00020 // +----------------------------------------------------------------------+
00021 
00022 // }}}
00023 // {{{ requires
00024 
00025 require_once 'PEAR.php';
00026 require_once dirname(__FILE__) . '/ErrorHandler.php';
00027 require_once dirname(__FILE__) . '/../FastFrame.php';
00028 require_once dirname(__FILE__) . '/Request.php';
00029 require_once dirname(__FILE__) . '/Registry.php';
00030 require_once dirname(__FILE__) . '/Auth.php';
00031 require_once dirname(__FILE__) . '/Perms.php';
00032 require_once dirname(__FILE__) . '/Result.php';
00033 require_once dirname(__FILE__) . '/Output.php';
00034 
00035 // }}}
00036 // {{{ constants
00037 
00042 define('ACTION_PROBLEM',        'problem');
00043 define('ACTION_ADD',            'add');
00044 define('ACTION_ADD_SUBMIT',     'add_submit');
00045 define('ACTION_EDIT',           'edit');
00046 define('ACTION_EDIT_SUBMIT',    'edit_submit');
00047 define('ACTION_VIEW',           'view');
00048 define('ACTION_DELETE',         'delete');
00049 define('ACTION_LIST',           'list');
00050 define('ACTION_LOGIN',          'login');
00051 define('ACTION_LOGIN_SUBMIT',   'login_submit');
00052 define('ACTION_LOGOUT',         'logout');
00053 define('ACTION_SELECT',         'select');
00054 define('ACTION_DISPLAY',        'display');
00055 define('ACTION_RELOAD',         'reload');
00056 define('ACTION_TREE',           'tree');
00057 define('ACTION_HOME',           'home');
00058 define('ACTION_DOWNLOAD',       'download');
00059 define('ACTION_CONTACT',        'contact');
00060 define('ACTION_CONTACT_SUBMIT', 'contact_submit');
00061 
00062 // }}}
00063 // {{{ class FF_ActionHandler 
00064 
00079 // }}}
00080 class FF_ActionHandler {
00081     // {{{ properties
00082 
00087     var $actionId;
00088 
00093     var $appId;
00094 
00099     var $moduleId;
00100 
00105     var $moduleConfig;
00106 
00111     var $defaultActionId;
00112 
00117     var $appName;
00118 
00123     var $o_registry;
00124 
00129     var $o_model;
00130 
00136     var $availableActions;
00137 
00142     var $defaultActions = array(
00143         ACTION_PROBLEM    => array('Action/Problem.php', 'FF_Action_Problem'),
00144         ACTION_ADD        => array('Action/Form.php', 'FF_Action_Form'),
00145         ACTION_ADD_SUBMIT => array('Action/FormSubmit.php', 'FF_Action_FormSubmit'),
00146         ACTION_EDIT       => array('Action/Form.php', 'FF_Action_Form'),
00147         ACTION_EDIT_SUBMIT=> array('Action/FormSubmit.php', 'FF_Action_FormSubmit'),
00148         ACTION_DELETE     => array('Action/Delete.php', 'FF_Action_Delete'),
00149         ACTION_LIST       => array('Action/List.php', 'FF_Action_List'),
00150         ACTION_SELECT     => array('Action/List.php', 'FF_Action_List'),
00151         ACTION_DISPLAY    => array('Action/Display.php', 'FF_Action_Display'),
00152         ACTION_TREE       => array('Action/Tree.php', 'FF_Action_Tree'),
00153         ACTION_TREE       => array('Action/Tree.php', 'FF_Action_Tree'),
00154         ACTION_DOWNLOAD   => array('Action/Download.php', 'FF_Action_Download'),
00155         ACTION_CONTACT    => array('Action/Contact.php', 'FF_Action_Contact'),
00156         ACTION_CONTACT_SUBMIT => array('Action/ContactSubmit.php', 'FF_Action_ContactSubmit'),
00157     );
00158 
00159     // }}}
00160     // {{{ constructor
00161 
00168     function FF_ActionHandler()
00169     {
00170         $this->o_registry =& FF_Registry::singleton();
00171         $this->_initializeErrorHandler();
00172         FF_Auth::startSession();
00173         // Always set the locale to a guaranteed language first so that a custom language
00174         // (i.e. en_BIP) will work
00175         FF_Locale::setLang('en_US');
00176 
00177         if (FF_Request::getParam('actionId', 'g') == ACTION_HOME &&
00178             is_array($a_init = FF_Auth::getCredential('initPage'))) {
00179             $this->setActionId(@$a_init['actionId']);
00180             $this->setModuleId(@$a_init['module']);
00181             $this->setAppId($a_init['app']);
00182         }
00183         elseif (FF_Request::getParam('app', 'pg', false) === false) {
00184             foreach ($this->o_registry->getApps() as $s_app) {
00185                 $m_hosts = (array) $this->o_registry->getAppParam('hostnames', array(), $s_app); 
00186                 foreach ($m_hosts as $s_host ) {
00187                     if (!empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == $s_host ) {
00188                         $this->setActionId('');
00189                         $this->setModuleId('');
00190                         $this->setAppId($s_app);
00191                         break;
00192                     }    
00193                 }
00194             }
00195         }
00196         else {
00197             $this->setActionId(FF_Request::getParam('actionId', 'pg'));
00198             $this->setModuleId(FF_Request::getParam('module', 'pg'));
00199             $this->setAppId(FF_Request::getParam('app', 'pg'));
00200         }
00201 
00202         $this->_makeDefaultPathsAbsolute();
00203         $this->_checkProfile();
00204     }
00205 
00206     // }}}
00207     // {{{ singleton()
00208 
00215     function &singleton()
00216     {
00217         static $instance;
00218         if (!isset($instance)) {
00219             $instance = new FF_ActionHandler();
00220         }
00221 
00222         return $instance;
00223     }
00224 
00225     // }}}
00226     // {{{ takeAction()
00227 
00234     function takeAction()
00235     {
00236         $hitLastAction = false;
00237         while (!$hitLastAction) {
00238             $this->loadActions();
00239             $this->_checkActionId();
00240             $this->checkAuth();
00241             $this->moduleConfig->checkPerms();
00242             $pth_actionFile = $this->availableActions[$this->actionId][0];
00243             require_once $pth_actionFile;
00244             $o_action =& new $this->availableActions[$this->actionId][1]($this->o_model);
00245             $o_nextAction = $o_action->run();
00246             if ($o_nextAction->isLastAction()) {
00247                 $hitLastAction = true;
00248             }
00249             else {
00250                 $this->setActionId($o_nextAction->getNextActionId());
00251                 if (!is_null($o_nextAction->getNextAppId())) {
00252                     $this->setAppId($o_nextAction->getNextAppId());
00253                 }
00254 
00255                 if (!is_null($o_nextAction->getNextModuleId())) {
00256                     $this->setModuleId($o_nextAction->getNextModuleId());
00257                 }
00258             }
00259         }
00260     }
00261 
00262     // }}}
00263     // {{{ loadActions()
00264 
00271     function loadActions()
00272     {
00273         static $a_configObjects, $s_lastApp, $s_lastModule;
00274         settype($a_configObjects, 'array');
00275         if (empty($this->appId)) {
00276             $this->appId = $this->o_registry->getConfigParam('general/initial_app');
00277         }
00278 
00279         if ($s_lastApp != $this->appId) {
00280             $this->o_registry->pushApp($this->appId);
00281             // If changing apps then need to check profile again
00282             $this->_checkProfile();
00283         }
00284 
00285         if (empty($this->moduleId)) {
00286             $this->moduleId = $this->o_registry->getConfigParam('general/initial_module');
00287         }
00288 
00289         // no need to reload if not changing module and app 
00290         if ($s_lastApp == $this->appId && $s_lastModule == $this->moduleId) {
00291             return;
00292         }
00293 
00294         $s_lastApp = $this->appId;
00295         $s_lastModule = $this->moduleId;
00296         $s_className = 'FF_ActionHandlerConfig_' . $this->moduleId;
00297         if (!isset($a_configObjects[$s_className])) {
00298             $pth_config= $this->o_registry->getAppFile("ActionHandler/$this->moduleId.php", $this->appId, 'libs'); 
00299             if (file_exists($pth_config)) {
00300                 require_once $pth_config;
00301                 $a_configObjects[$s_className] =& new $s_className($this);
00302             }
00303             else {
00304                 trigger_error('The class file ' . basename($pth_config) . ' for module ' . $this->moduleId . ' does not exist', E_USER_ERROR);
00305             }
00306         }
00307 
00308         $this->availableActions = $this->defaultActions;
00309         $this->moduleConfig =& $a_configObjects[$s_className];
00310         $this->moduleConfig->loadConfig();
00311     }
00312 
00313     // }}}
00314     // {{{ addAction()
00315 
00326     function addAction($in_actionId, $in_classFile, $in_className)
00327     {
00328         $this->availableActions[$in_actionId] = array($in_classFile, $in_className);
00329     }
00330 
00331     // }}}
00332     // {{{ batchModifyActions()
00333 
00350     function batchModifyActions($in_actions, $in_path, $in_classExtension, $in_filePrefix = '') {
00351         foreach ($in_actions as $s_actionId) {
00352             $s_newPath = $in_path . '/' . $in_filePrefix . basename($this->availableActions[$s_actionId][0]);
00353             $s_newClass = $this->availableActions[$s_actionId][1] . $in_classExtension;
00354             $this->addAction($s_actionId, $s_newPath, $s_newClass);
00355         }
00356     }
00357 
00358     // }}}
00359     // {{{ setAppId()
00360 
00369     function setAppId($in_appId)
00370     {
00371         $this->appId = $in_appId;
00372     }
00373 
00374     // }}}
00375     // {{{ getAppId()
00376 
00383     function getAppId()
00384     {
00385         return $this->appId;
00386     }
00387 
00388     // }}}
00389     // {{{ setModuleId()
00390 
00399     function setModuleId($in_moduleId)
00400     {
00401         $this->moduleId = $in_moduleId;
00402     }
00403 
00404     // }}}
00405     // {{{ getModuleId()
00406 
00413     function getModuleId()
00414     {
00415         return $this->moduleId;
00416     }
00417 
00418     // }}}
00419     // {{{ setActionId()
00420 
00429     function setActionId($in_actionId)
00430     {
00431         $this->actionId = $in_actionId;
00432     }
00433 
00434     // }}}
00435     // {{{ getActionId()
00436 
00443     function getActionId()
00444     {
00445         return $this->actionId;
00446     }
00447 
00448     // }}}
00449     // {{{ setDefaultActionId()
00450 
00459     function setDefaultActionId($in_actionId)
00460     {
00461         $this->defaultActionId = $in_actionId;
00462     }
00463 
00464     // }}}
00465     // {{{ checkAuth()
00466 
00478     function checkAuth($in_noModuleCheck = false)
00479     {
00480         if (!$in_noModuleCheck && $this->moduleConfig->hasCheckAuth()) {
00481             $this->moduleConfig->checkAuth();
00482         } 
00483         else {
00484             if (!FF_Auth::checkAuth(true)) {
00485                 $this->setAppId('login');
00486                 $this->setModuleId($this->o_registry->getConfigParam('general/initial_module', null, $this->appId));
00487                 $this->loadActions();
00488                 // If there was no app specified then they have arrived newly at the
00489                 // site.  We don't want to bounce them to the initial app only to have
00490                 // them bounce back to the login page.  So we send them straight to it.
00491                 if (FF_Request::getParam('app', 'gp', false) === false) {
00492                     $this->setActionId($this->defaultActionId);
00493                 }
00494                 else {
00495                     // They were trying to get somewhere but don't have
00496                     // auth, store where they were trying to go so we can
00497                     // bounce back there after login.
00498                     $s_redirectURL = preg_replace('/[?&]' . session_name() . '=[^?&]*/', '', $_SERVER['REQUEST_URI']);
00499                     FF_Request::setParam('loginRedirect', $s_redirectURL, 'p');
00500                     $this->setActionId(ACTION_LOGOUT);
00501                 }
00502             }
00503         }
00504     }
00505 
00506     // }}}
00507     // {{{ _checkActionId()
00508 
00515     function _checkActionId()
00516     {
00517         if (empty($this->actionId) ||
00518             !isset($this->availableActions[$this->actionId])) {
00519             if (isset($this->availableActions[$this->defaultActionId])) {
00520                 $this->setActionId($this->defaultActionId);
00521             }
00522             else {
00523                 $this->setActionId(ACTION_PROBLEM);
00524             }
00525         }
00526     }
00527 
00528     // }}}
00529     // {{{ _makeDefaultPathsAbsolute()
00530 
00537     function _makeDefaultPathsAbsolute()
00538     {
00539         $s_path = dirname(__FILE__) . '/';
00540         foreach ($this->defaultActions as $s_action => $a_vals) {
00541             if (strpos($a_vals[0], '/') !== 0) {
00542                 $this->defaultActions[$s_action][0] = $s_path . $a_vals[0];
00543             }
00544         }
00545     }
00546 
00547     // }}}
00548     // {{{ _initializeErrorHandler()
00549 
00556     function _initializeErrorHandler()
00557     {
00558         // This causes all pear errors to be sent to the error handler
00559         PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE); 
00560         // Don't re-initialize the error handler
00561         if (!isset($GLOBALS['o_error'])) {
00562             $o_error = new FF_ErrorHandler($this->o_registry->getConfigParam('error/reporters', array()));
00563             $GLOBALS['o_error'] =& $o_error;
00564             $o_error->setDateFormat('[Y-m-d H:i:s]');
00565             $o_error->setExcludeObjects(false);
00566             $o_error->setFileUrl($this->o_registry->getConfigParam('error/file_url'));
00567         }
00568     }
00569 
00570     // }}}
00571     // {{{ _checkProfile()
00572 
00580     function _checkProfile()
00581     {
00582         if ($this->o_registry->hasApp('profile') &&
00583             !FF_Auth::isGuest() &&
00584             !FF_Auth::getCredential('isProfileComplete') &&
00585             $this->getActionId() != ACTION_LOGOUT &&
00586             $this->o_registry->getConfigParam('profile/force_complete', false, 'profile')) {
00587             require_once $this->o_registry->getAppFile('ActionHandler/actions.php', 'profile', 'libs');
00588             // Don't redirect if already on the profile page or submitting it
00589             if ($this->getActionId() != ACTION_MYPROFILE && $this->getActionId() != ACTION_EDIT_SUBMIT) {
00590                 FastFrame::redirect(FastFrame::url('index.php', array('app' => 'profile', 'actionId' => ACTION_MYPROFILE)));
00591             }
00592         }
00593     }
00594 
00595     // }}}
00596 }
00597 ?>

Generated on Fri Jun 23 11:38:14 2006 for FastFrame by  doxygen 1.4.4