Source for file cms.php
Documentation is available at cms.php
* @package Joomla.Libraries
* @subpackage Application
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Joomla! CMS Application class
* @package Joomla.Libraries
* @subpackage Application
* Array of options for the JDocument object
* Application instances container.
protected static $instances =
array();
* The scope of the application.
* @deprecated 4.0 Will be renamed $clientId
* The application message queue.
* @deprecated 4.0 Will be renamed $messageQueue
* The name of the application.
* @deprecated 4.0 Will be renamed $name
* Currently active template
* Indicates that strong encryption should be used.
* @note Default has been changed as of 3.2. If salted md5 is required, it must be explictly set.
* @param mixed $input An optional argument to provide dependency injection for the application's
* input object. If the argument is a JInput object that object will become
* the application's input object, otherwise a default input object is created.
* @param mixed $config An optional argument to provide dependency injection for the application's
* config object. If the argument is a JRegistry object that object will become
* the application's config object, otherwise a default config object is created.
* @param mixed $client An optional argument to provide dependency injection for the application's
* client object. If the argument is a JApplicationWebClient object that object will become
* the application's client object, otherwise a default client object is created.
public function __construct(JInput $input =
null, JRegistry $config =
null, JApplicationWebClient $client =
null)
// Load and set the dispatcher
// If JDEBUG is defined, load the profiler instance
// Enable sessions by default.
$this->config->set('session', true);
// Set the session default name.
// Create the session if a session name is passed.
if ($this->config->get('session') !==
false)
* After the session has been started we need to populate it with some default values.
$session->set('registry', new JRegistry('session'));
$session->set('user', new JUser);
* Checks the user session.
* If the session record doesn't exist, initialise it.
* If session is new, create session variables
$query =
$db->getQuery(true)
->select($db->quoteName('session_id'))
->from($db->quoteName('#__session'))
->where($db->quoteName('session_id') .
' = ' .
$db->quote($session->getId()));
$db->setQuery($query, 0, 1);
$exists =
$db->loadResult();
// If the session record doesn't exist initialise it.
$query->insert($db->quoteName('#__session'))
->columns($db->quoteName('session_id') .
', ' .
$db->quoteName('client_id') .
', ' .
$db->quoteName('time'))
->values($db->quote($session->getId()) .
', ' . (int)
$this->getClientId() .
', ' .
$db->quote((int)
time()));
$query->insert($db->quoteName('#__session'))
$db->quoteName('session_id') .
', ' .
$db->quoteName('client_id') .
', ' .
$db->quoteName('guest') .
', ' .
$db->quoteName('time') .
', ' .
$db->quoteName('userid') .
', ' .
$db->quoteName('username')
$db->quote($session->getId()) .
', ' . (int)
$this->getClientId() .
', ' . (int)
$user->get('guest') .
', ' .
$db->quote((int)
$session->get('session.timer.start')) .
', ' . (int)
$user->get('id') .
', ' .
$db->quote($user->get('username'))
// If the insert failed, exit the application.
catch
(RuntimeException $e)
* Enqueue a system message.
* @param string $msg The message to enqueue.
* @param string $type The message type. Default is message.
// For empty queue, if messages exists in the session, enqueue them first.
$sessionQueue =
$session->get('application.queue');
if (count($sessionQueue))
$session->set('application.queue', null);
* Execute the application.
// Perform application routines.
// If we have an application document object, render it.
// Render the application output.
// If gzip compression is enabled in configuration and the server is compliant, compress the output.
if ($this->get('gzip') &&
!ini_get('zlib.output_compression') &&
(ini_get('output_handler') !=
'ob_gzhandler'))
// Trigger the onAfterCompress event.
// Send the application response.
// Trigger the onAfterRespond event.
* Gets a configuration value.
* @param string $varname The name of the value to get.
* @param string $default Default value to return
* @return mixed The user state.
* @deprecated 4.0 Use get() instead
public function getCfg($varname, $default =
null)
return $this->get($varname, $default);
* Gets the client id of the current running application.
* @return integer A client identifier.
* Returns a reference to the global JApplicationCms object, only creating it if it doesn't already exist.
* This method must be invoked as: $web = JApplicationCms::getInstance();
* @param string $name The name (optional) of the JApplicationCms class to instantiate.
* @return JApplicationCms
* @throws RuntimeException
if (empty(static::$instances[$name]))
// Create a JApplicationCms object.
$classname =
'JApplication' .
ucfirst($name);
if (!class_exists($classname))
throw
new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $name), 500);
static::$instances[$name] =
new $classname;
return static::$instances[$name];
* Returns the application JMenu object.
* @param string $name The name of the application/client.
* @param array $options An optional associative array of configuration settings.
public function getMenu($name =
null, $options =
array())
* Get the system message queue.
* @return array The system message queue.
// For empty queue, if messages exists in the session, enqueue them.
$sessionQueue =
$session->get('application.queue');
if (count($sessionQueue))
$session->set('application.queue', null);
* Gets the name of the current running application.
* @return string The name of the application.
* Returns the application JPathway object.
* @param string $name The name of the application.
* @param array $options An optional associative array of configuration settings.
public function getPathway($name =
null, $options =
array())
* Returns the application JRouter object.
* @param string $name The name of the application.
* @param array $options An optional associative array of configuration settings.
public static function getRouter($name =
null, array $options =
array())
* Gets the name of the current template.
* @param boolean $params An optional associative array of configuration settings
* @return mixed System is the fallback.
$template =
new stdClass;
* @param string $key The path of the state.
* @param mixed $default Optional default value, returned if the internal value is null.
* @return mixed The user state or null.
$registry =
$session->get('registry');
return $registry->get($key, $default);
* Gets the value of a user state variable.
* @param string $key The key of the user state variable.
* @param string $request The name of the variable passed in a request.
* @param string $default The default value for the variable if not found. Optional.
* @param string $type Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
* @return object The request user state.
$new_state =
$this->input->get($request, null, $type);
// Save the new value only if it was set in this request.
* Initialise the application.
* @param array $options An optional associative array of configuration settings.
// Set the configuration in the API.
// Check that we were given a language in the array (since by default may be blank).
if (isset
($options['language']))
$this->set('language', $options['language']);
// Set user specific editor.
$editor =
$user->getParam('editor', $this->get('editor'));
$editor =
$this->get('editor');
$this->set('editor', $editor);
* Set the encryption to use. The availability of strong encryption must always be checked separately.
* Use JCrypt::hasStrongPasswordSupport() to check PHP for this support.
$userPluginParams->loadString($userPlugin->params);
$useStrongEncryption =
$userPluginParams->get('strong_passwords', 0);
$this->config->set('useStrongEncryption', $useStrongEncryption);
// Trigger the onAfterInitialise event.
* @return boolean True if this application is administrator.
* @return boolean True if this application is site.
* Allows the application to load a custom or default session.
* The logic and options for creating this object are adequately generic for default cases
* but for many applications it will make sense to override this method and create a session,
* if required, based on more specific needs.
* @param JSession $session An optional session object. If omitted, the session is created.
* @return JApplicationCms This method is chainable.
// Generate a session name.
// Calculate the session lifetime.
$lifetime =
(($this->get('lifetime')) ?
$this->get('lifetime') *
60 :
900);
// Initialize the options for JSession.
if ($this->get('force_ssl') ==
2)
$options['force_ssl'] =
true;
if ($this->get('force_ssl') >=
1)
$options['force_ssl'] =
true;
$this->registerEvent('onAfterSessionStart', array($this, 'afterSessionStart'));
// There's an internal coupling to the session object being present in JFactory, need to deal with this at some point
// TODO: At some point we need to get away from having session data always in the db.
// Remove expired sessions from the database.
// The modulus introduces a little entropy, making the flushing less accurate
// but fires the query less than half the time.
$query =
$db->getQuery(true)
->delete($db->quoteName('#__session'))
->where($db->quoteName('time') .
' < ' .
$db->quote((int)
($time -
$session->getExpire())));
// Get the session handler from the configuration.
$handler =
$this->get('session_handler', 'none');
if (($handler !=
'database' &&
($time %
2 ||
$session->isNew()))
||
($handler ==
'database' &&
$session->isNew()))
// Set the session object.
* Login authentication function.
* Username and encoded password are passed the onUserLogin event which
* is responsible for the user validation. A successful validation updates
* the current session record with the user's details.
* Username and encoded password are sent as credentials (along with other
* possibilities) to each observer (authentication plugin) for user
* validation. Successful validation will update the current session with
* @param array $credentials Array('username' => string, 'password' => string)
* @param array $options Array('remember' => boolean)
* @return boolean True on success.
public function login($credentials, $options =
array())
// Get the global JAuthentication object.
jimport('joomla.user.authentication');
$response =
$authenticate->authenticate($credentials, $options);
* Validate that the user should be able to login (different to being authenticated).
* This permits authentication plugins blocking the user.
$authorisations =
$authenticate->authorise($response, $options);
foreach ($authorisations as $authorisation)
if (in_array($authorisation->status, $denied_states))
// Trigger onUserAuthorisationFailure Event.
$this->triggerEvent('onUserAuthorisationFailure', array((array)
$authorisation));
// If silent is set, just return false.
if (isset
($options['silent']) &&
$options['silent'])
switch ($authorisation->status)
// Import the user plugin group.
// OK, the credentials are authenticated and user is authorised. Let's fire the onLogin event.
$results =
$this->triggerEvent('onUserLogin', array((array)
$response, $options));
* If any of the user plugins did not successfully complete the login routine
* then the whole method fails.
* Any errors raised should be done in the plugin as this provides the ability
* to provide much more information about why the routine may have failed.
if ($response->type ==
'Cookie')
$user->set('cookieLogin', true);
if (in_array(false, $results, true) ==
false)
$options['user'] =
$user;
$options['responseType'] =
$response->type;
if (isset
($response->length) && isset
($response->secure) && isset
($response->lifetime))
$options['length'] =
$response->length;
$options['secure'] =
$response->secure;
$options['lifetime'] =
$response->lifetime;
// The user is successfully logged in. Run the after login events
// Trigger onUserLoginFailure Event.
$this->triggerEvent('onUserLoginFailure', array((array)
$response));
// If silent is set, just return false.
if (isset
($options['silent']) &&
$options['silent'])
// If status is success, any error will have been raised by the user plugin
JLog::add($response->error_message, JLog::WARNING, 'jerror');
* Logout authentication function.
* Passed the current user information to the onUserLogout event and reverts the current
* session record back to 'anonymous' parameters.
* If any of the authentication plugins did not successfully complete
* the logout routine then the whole method fails. Any errors raised
* should be done in the plugin as this provides the ability to give
* much more information about why the routine may have failed.
* @param integer $userid The user to load - Can be an integer or string - If string, it is converted to ID automatically
* @param array $options Array('clientid' => array of client id's)
* @return boolean True on success
public function logout($userid =
null, $options =
array())
// Get a user object from the JApplication.
// Build the credentials array.
$parameters['username'] =
$user->get('username');
$parameters['id'] =
$user->get('id');
// Set clientid in the options array if it hasn't been set already.
if (!isset
($options['clientid']))
// Import the user plugin group.
// OK, the credentials are built. Lets fire the onLogout event.
$results =
$this->triggerEvent('onUserLogout', array($parameters, $options));
// Check if any of the plugins failed. If none did, success.
$options['username'] =
$user->get('username');
// Trigger onUserLoginFailure Event.
$this->triggerEvent('onUserLogoutFailure', array($parameters));
* Redirect to another URL.
* If the headers have not been sent the redirect will be accomplished using a "301 Moved Permanently"
* or "303 See Other" code in the header pointing to the new location. If the headers have already been
* sent this will be accomplished using a JavaScript statement.
* @param string $url The URL to redirect to. Can only be http/https URL
* @param boolean $moved True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
public function redirect($url, $moved =
false)
// Persist messages if they exist.
// Handle B/C by checking if a message was passed to the method, will be removed at 4.0
* Do some checks on the $args array, values below correspond to legacy redirect() method
* $args[1] = Message to enqueue
* $args[2] = Message type
if (isset
($args[1]) &&
!empty($args[1]) &&
!is_bool($args[1]))
// Log that passing the message to the function is deprecated
'Passing a message and message type to JFactory::getApplication()->redirect() is deprecated. '
.
'Please set your message via JFactory::getApplication()->enqueueMessage() prior to calling redirect().',
// Set the message type if present
if (isset
($args[2]) &&
!empty($args[2]))
// Reset the $moved variable
$moved = isset
($args[3]) ? (boolean)
$args[3] :
false;
// Hand over processing to the parent now
* Rendering is the process of pushing the document buffers into the template
* placeholders, retrieving data from the document and pushing it into
* the application response buffer.
// Setup the document options.
$this->docOptions['file'] =
$this->get('themeFile', 'index.php');
if ($this->get('themes.base'))
// Fall back to constants.
// Trigger the onBeforeRender event.
// Set the application output data.
// Trigger the onAfterRender event.
// Mark afterRender in the profiler.
* Routing is the process of examining the request environment to determine which
* component should receive the request. The component optional parameters
* are then set in the request object to be processed when the application is being
protected function route()
// Get the full request URI.
$result =
$router->parse($uri);
foreach ($result as $key =>
$value)
$this->input->def($key, $value);
// Trigger the onAfterRoute event.
* Sets the value of a user state variable.
* @param string $key The path of the state.
* @param string $value The value of the variable.
* @return mixed The previous state, if one existed.
$registry =
$session->get('registry');
return $registry->set($key, $value);
* Sends all headers prior to returning the string
* @param boolean $compress If true, compress the data
public function toString($compress =
false)
// Don't compress something if the server is going to do it anyway. Waste of time.
if ($compress &&
!ini_get('zlib.output_compression') &&
ini_get('output_handler') !=
'ob_gzhandler')
$this->setHeader('Cache-Control', 'no-cache', false);
Documentation generated on Tue, 19 Nov 2013 14:55:59 +0100 by phpDocumentor 1.4.3