Source for file dispatcher.php
Documentation is available at dispatcher.php
* @package FrameworkOnFramework
* @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
// Protect from unauthorized access
* FrameworkOnFramework dispatcher class
* FrameworkOnFramework is a set of classes whcih extend Joomla! 1.5 and later's
* MVC framework with features making maintaining complex software much easier,
* without tedious repetitive copying of the same code over and over again.
* @package FrameworkOnFramework
/** @var array Configuration variables */
/** @var FOFInput Input variables */
/** @var string The name of the default view, in case none is specified */
// Variables for FOF's transparent user authentication. You can override them
// in your Dispatcher's __construct() method.
/** @var int The Time Step for the TOTP used in FOF's transparent user authentication */
/** @var string The key for the TOTP, Base32 encoded (watch out; Base32, NOT Base64!) */
/** @var array Which formats to be handled by transparent authentication */
* Should I logout the transparently authenticated user on logout?
* Recommended to leave it on in order to avoid crashing the sessions table.
/** @var array Which methods to use to fetch authentication credentials and in which order */
/* HTTP Basic Authentication using encrypted information protected
* with a TOTP (the username must be "_fof_auth") */
/* Encrypted information protected with a TOTP passed in the
* _fofauthentication query string parameter */
/* HTTP Basic Authentication using a username and password pair in plain text */
'HTTPBasicAuth_Plaintext',
/* Plaintext, JSON-encoded username and password pair passed in the
* _fofauthentication query string parameter */
/* Plaintext username and password in the _fofauthentication_username
* and _fofauthentication_username query string parameters */
'SplitQueryString_Plaintext',
/** @var bool Did we successfully and transparently logged in a user? */
private $_fofAuth_isLoggedIn =
false;
/** @var string The calculated encryption key for the _TOTP methods, used if we have to encrypt the reply */
private $_fofAuth_CryptoKey =
'';
* Get a static (Singleton) instance of a particular Dispatcher
* @param string $option The component name
* @param string $view The View name
* @param array $config Configuration data
* @staticvar array $instances Holds the array of Dispatchers FOF knows about
public static function &getAnInstance($option =
null, $view =
null, $config =
array())
static $instances =
array();
if (!array_key_exists($hash, $instances))
$instances[$hash] =
self::getTmpInstance($option, $view, $config);
return $instances[$hash];
* Gets a temporary instance of a Dispatcher
* @param string $option The component name
* @param string $view The View name
* @param array $config Configuration data
public static function &getTmpInstance($option =
null, $view =
null, $config =
array())
if ($config['input'] instanceof
FOFInput)
$input =
$config['input'];
$config['input'] = (array)
$config['input'];
$config['input'] =
array_merge($_REQUEST, $config['input']);
$input =
new FOFInput($config['input']);
$config['option'] =
!is_null($option) ?
$option :
$input->getCmd('option', 'com_foobar');
$config['view'] =
!is_null($view) ?
$view :
$input->getCmd('view', '');
$input->set('option', $config['option']);
$input->set('view', $config['view']);
$config['input'] =
$input;
$componentPaths['main'] .
'/dispatchers',
$componentPaths['admin'],
$componentPaths['admin'] .
'/dispatchers'
$searchPaths, 'dispatcher.php'
$className =
'FOFDispatcher';
$instance =
new $className($config);
* @param array $config The configuration variables
// Get the input for this MVC triad
$this->input =
$config['input'];
// Get the default values for the component name
$this->component =
$this->input->getCmd('option', 'com_foobar');
// Load the component's fof.xml configuration file
// Get the default values for the view name
$this->view =
$this->input->getCmd('view', null);
// Do we have a task formatted as controller.task?
$task =
$this->input->getCmd('task', '');
if (!empty($task) &&
(strstr($task, '.') !==
false))
list
($this->view, $task) =
explode('.', $task, 2);
$this->layout =
$this->input->getCmd('layout', null);
// Overrides from the config
$this->component =
$config['option'];
$this->view =
empty($config['view']) ?
$this->view :
$config['view'];
$this->layout =
$config['layout'];
$this->input->set('option', $this->component);
$this->input->set('layout', $this->layout);
* The main code of the Dispatcher. It spawns the necessary controller and
throw
new Exception(JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
// Merge English and local translations
throw
new Exception(JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
// Get and execute the controller
$option =
$this->input->getCmd('option', 'com_foobar');
$task =
$this->input->getCmd('task', null);
// Pluralise/sungularise the view name for typical tasks
if (in_array($task, array('edit', 'add', 'read')))
elseif (in_array($task, array('browse')))
$config['input'] =
$this->input;
$status =
$controller->execute($task);
throw
new Exception(JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
$format =
$this->input->get('format', 'html', 'cmd');
$format =
empty($format) ?
'html' :
$format;
// In HTML views perform a redirection
// In non-HTML views just exit the application with the proper HTTP headers
if ($controller->hasRedirect())
* Tries to guess the controller task to execute based on the view name and
* the HTTP request method.
* @param string $view The name of the view
* @return string The best guess of the task to execute
// Get a default task based on plural/singular view
$request_task =
$this->input->getCmd('task', null);
// Get a potential ID, we might need it later
$id =
$this->input->get('id', null, 'int');
$ids =
$this->input->get('ids', array(), 'array');
// Check the request method
if (!isset
($_SERVER['REQUEST_METHOD']))
$_SERVER['REQUEST_METHOD'] =
'GET';
$requestMethod =
strtoupper($_SERVER['REQUEST_METHOD']);
// If it's an edit without an ID or ID=0, it's really an add
if (($task ==
'edit') &&
($id ==
0))
// If it's an edit in the frontend, it's really a read
* Executes right before the dispatcher tries to instantiate and run the
* @return boolean Return false to abort
* Sets up some environment variables, so we can work as usually on CLI, too.
* @return boolean Return false to abort
// Trick to create a valid url used by JURI
$this->_originalPhpScript =
'';
// We have no Application Helper (there is no Application!), so I have to define these constants manually
$option =
$this->input->get('option', '', 'cmd');
define('JPATH_COMPONENT', $componentPaths['main']);
if (!defined('JPATH_COMPONENT_SITE'))
define('JPATH_COMPONENT_SITE', $componentPaths['site']);
if (!defined('JPATH_COMPONENT_ADMINISTRATOR'))
define('JPATH_COMPONENT_ADMINISTRATOR', $componentPaths['admin']);
* Executes right after the dispatcher runs the controller.
* @return boolean Return false to abort
// If we have to log out the user, please do so now
* Transparently authenticates a user
// Only run when there is no logged in user
// @todo Check the format
$format =
$this->input->getCmd('format', 'html');
// If we're already logged in, don't bother
if ($this->_fofAuth_isLoggedIn)
// This will hold our authentication data array (username, password)
case 'HTTPBasicAuth_TOTP':
if (!isset
($_SERVER['PHP_AUTH_USER']))
if (!isset
($_SERVER['PHP_AUTH_PW']))
if ($_SERVER['PHP_AUTH_USER'] !=
'_fof_auth')
$encryptedData =
$_SERVER['PHP_AUTH_PW'];
$authInfo =
$this->_decryptWithTOTP($encryptedData);
$encryptedData =
$this->input->get('_fofauthentication', '', 'raw');
if (empty($encryptedData))
$authInfo =
$this->_decryptWithTOTP($encryptedData);
case 'HTTPBasicAuth_Plaintext':
if (!isset
($_SERVER['PHP_AUTH_USER']))
if (!isset
($_SERVER['PHP_AUTH_PW']))
'username' =>
$_SERVER['PHP_AUTH_USER'],
'password' =>
$_SERVER['PHP_AUTH_PW']
case 'QueryString_Plaintext':
$jsonencoded =
$this->input->get('_fofauthentication', '', 'raw');
case 'SplitQueryString_Plaintext':
'username' =>
$this->input->get('_fofauthentication_username', '', 'raw'),
'password' =>
$this->input->get('_fofauthentication_password', '', 'raw'),
if (empty($authInfo['username']))
// No point trying unless we have a username and password
* Decrypts a transparent authentication message using a TOTP
* @param string $encryptedData The encrypted data
* @return array The decrypted data
private function _decryptWithTOTP($encryptedData)
$this->_fofAuth_CryptoKey =
null;
$period =
$totp->getPeriod();
for ($i =
0; $i <=
2; $i++
)
$aes =
new FOFEncryptAes($this->_fofAuth_CryptoKey);
$ret =
$aes->decryptString($encryptedData);
$ret =
rtrim($ret, "\000");
// Successful decryption!
// Obviously if we're here we could not decrypt anything. Bail out.
$this->_fofAuth_CryptoKey =
null;
* Creates a decryption key for use with the TOTP decryption method
* @param integer $time The timestamp used for TOTP calculation, leave empty to use current timestamp
* @return string THe encryption key
private function _createDecryptionKey($time =
null)
* Main function to detect if we're running in a CLI environment and we're admin
* @return array isCLI and isAdmin. It's not an associtive array, so we can use list.
if (is_null($isCLI) &&
is_null($isAdmin))
$isCLI =
FOFPlatform::getInstance()->isCli();
$isAdmin =
FOFPlatform::getInstance()->isBackend();
return array($isCLI, $isAdmin);
Documentation generated on Tue, 19 Nov 2013 15:01:34 +0100 by phpDocumentor 1.4.3