📄 application.php
字号:
<?php
/**
* @version $Id: application.php 8682 2007-08-31 18:36:45Z jinx $
* @package Joomla.Framework
* @subpackage Application
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
/**
* Base class for a Joomla! application.
*
* Acts as a Factory class for application specific objects and provides many
* supporting API functions. Derived clases should supply the route(), dispatch()
* and render() functions.
*
* @abstract
* @package Joomla.Framework
* @subpackage Application
* @since 1.5
*/
class JApplication extends JObject
{
/**
* The client identifier.
*
* @var integer
* @access protected
* @since 1.5
*/
var $_clientId = null;
/**
* The router object
*
* @var JRouter
* @access protected
*/
var $_router = null;
/**
* The pathway object
*
* @var JPathway
* @access protected
*/
var $_pathway = null;
/**
* The application message queue.
*
* @var array
* @access protected
*/
var $_messageQueue = array();
/**
* The name of the application
*
* @var array
* @access protected
*/
var $_name = null;
/**
* Class constructor.
*
* @param integer A client identifier.
*/
function __construct($config = array())
{
jimport('joomla.utilities.utility');
//set the view name
$this->_name = $this->getName();
$this->_clientId = $config['clientId'];
//Enable sessions by default
if(!isset($config['session'])) {
$config['session'] = true;
}
//Set the session default name
if(!isset($config['session_name'])) {
$config['session_name'] = $this->_name;
}
//Set the default configuration file
if(!isset($config['config_file'])) {
$config['config_file'] = 'configuration.php';
}
//create the configuration object
$this->_createConfiguration(JPATH_CONFIGURATION.DS.$config['config_file']);
//create the session if a session name is passed
if($config['session'] !== false) {
$this->_createSession(JUtility::getHash($config['session_name']));
}
$this->set( 'requestTime', gmdate('Y-m-d H:i') );
}
/**
* Returns a reference to the global JApplication object, only creating it if it
* doesn't already exist.
*
* This method must be invoked as:
* <pre> $menu = &JApplication::getInstance();</pre>
*
* @access public
* @param integer $id A client identifier.
* @param array $config An optional associative array of configuration settings.
* @return JApplication The appliction object.
* @since 1.5
*/
function &getInstance($client, $config = array())
{
static $instances;
if (!isset( $instances )) {
$instances = array();
}
if (empty($instances[$client]))
{
//Load the router object
jimport('joomla.application.helper');
$info =& JApplicationHelper::getClientInfo($client, true);
$path = $info->path.DS.'includes'.DS.'application.php';
if(file_exists($path))
{
require_once $path;
// Create a JRouter object
$classname = 'J'.ucfirst($client);
$instance = new $classname($config);
}
else
{
$error = new JException( E_ERROR, 500, 'Unable to load application: '.$classname);
return $error;
}
$instances[$client] = & $instance;
}
return $instances[$client];
}
/**
* Initialise the application.
*
* @param array An optional associative array of configuration settings.
* @access public
*/
function initialise($options = array())
{
jimport('joomla.event.helper');
//Set the language in the class
$config =& JFactory::getConfig();
// Check that we were given a language in the array (since by default may be blank)
if(isset($options['language'])) {
$config->setValue('config.language', $options['language']);
}
// Set user specific editor
$user =& JFactory::getUser();
$editor = $user->getParam('editor', $this->getCfg('editor'));
$editor = JPLuginHelper::isEnabled('editors', $editor) ? $editor : $this->getCfg('editor');
$config->setValue('config.editor', $editor);
// Set the database debug
$db =& JFactory::getDBO();
$db->debug( $config->get('debug_db'));
}
/**
* Route the applicaiton.
*
* Routing is the process of examining the request environment to determine which
* which component should receive the request. This component optional parameters
* are then set in the request object to be processed when the application is being
* dispatched
*
* @abstract
* @access public
*/
function route()
{
// get the full request URI
$uri =& JURI::getInstance();
$router =& $this->getRouter();
if(!$router->parse($uri)) {
JError::raiseError( 404, JText::_('Unable to route request') );
}
}
/**
* Dispatch the applicaiton.
*
* Dispatching is the process of pulling the option from the request object and
* mapping them to a component. If the component do not exist, it handles
* determining a default component to dispatch
*
* @abstract
* @access public
*/
function dispatch($component)
{
$document =& JFactory::getDocument();
$document->setTitle( $this->getCfg('sitename' ). ' - ' .JText::_( 'Administration' ));
$document->setDescription( $this->getCfg('MetaDesc') );
$contents = JComponentHelper::renderComponent($component);
$document->setBuffer($contents, 'component');
}
/**
* Render the application.
*
* Rendering is the process of pushing the document buffers into the template
* placeholders, retrieving data from the document and pushing it into the into
* the JResponse buffer.
*
* @abstract
* @access public
*/
function render()
{
$params = array(
'template' => $this->getTemplate(),
'file' => 'index.php',
'directory' => JPATH_THEMES
);
$document =& JFactory::getDocument();
$data = $document->render($this->getCfg('caching'), $params );
JResponse::setBody($data);
}
/**
* Exit the application.
*
* @access public
* @param int Exit code
*/
function close( $code = 0 ) {
exit($code);
}
/**
* Redirect to another URL.
*
* Optionally enqueues a message in the system message queue (which will be displayed
* the next time a page is loaded) using the enqueueMessage method. If the headers have
* not been sent the redirect will be accomplished using a "301 Moved Permanently"
* code in the header pointing to the new location. If the headers have already been
* sent this will be accomplished using a JavaScript statement.
*
* @access public
* @param string $url The URL to redirect to.
* @param string $msg An optional message to display on redirect.
* @param string $msgType An optional message type.
* @return none; calls exit().
* @since 1.5
* @see JApplication::enqueueMessage()
*/
function redirect( $url, $msg='', $msgType='message' )
{
// check for relative internal links
if (preg_match( '#^index[2]?.php#', $url )) {
$url = JURI::base() . $url;
}
// Strip out any line breaks
$url = preg_split("/[\r\n]/", $url);
$url = $url[0];
// If the message exists, enqueue it
if (trim( $msg )) {
$this->enqueueMessage($msg, $msgType);
}
// Persist messages if they exist
if (count($this->_messageQueue))
{
$session =& JFactory::getSession();
$session->set('application.queue', $this->_messageQueue);
}
/*
* If the headers have been sent, then we cannot send an additional location header
* so we will output a javascript redirect statement.
*/
if (headers_sent()) {
echo "<script>document.location.href='$url';</script>\n";
} else {
//@ob_end_clean(); // clear output buffer
header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: ' . $url );
}
$this->close();
}
/**
* Enqueue a system message.
*
* @access public
* @param string $msg The message to enqueue.
* @param string $type The message type.
* @return void
* @since 1.5
*/
function enqueueMessage( $msg, $type = 'message' )
{
// For empty queue, if messages exists in the session, enqueue them first
if (!count($this->_messageQueue))
{
$session =& JFactory::getSession();
$sessionQueue = $session->get('application.queue');
if (count($sessionQueue)) {
$this->_messageQueue = $sessionQueue;
$session->set('application.queue', null);
}
}
// Enqueue the message
$this->_messageQueue[] = array('message' => $msg, 'type' => strtolower($type));
}
/**
* Get the system message queue.
*
* @access public
* @return The system message queue.
* @since 1.5
*/
function getMessageQueue()
{
// For empty queue, if messages exists in the session, enqueue them
if (!count($this->_messageQueue))
{
$session =& JFactory::getSession();
$sessionQueue = $session->get('application.queue');
if (count($sessionQueue)) {
$this->_messageQueue = $sessionQueue;
$session->set('application.queue', null);
}
}
return $this->_messageQueue;
}
/**
* Gets a configuration value.
*
* @access public
* @param string The name of the value to get.
* @return mixed The user state.
* @example application/japplication-getcfg.php Getting a configuration value
*/
function getCfg( $varname )
{
$config =& JFactory::getConfig();
return $config->getValue('config.' . $varname);
}
/**
* Method to get the application name
*
* The dispatcher name by default parsed using the classname, or it can be set
* by passing a $config['name'] in the class constructor
*
* @access public
* @return string The name of the dispatcher
* @since 1.5
*/
function getName()
{
$name = $this->_name;
if (empty( $name ))
{
$r = null;
if ( !preg_match( '/J(.*)/i', get_class( $this ), $r ) ) {
JError::raiseError(500, "JApplication::getName() : Can\'t get or parse class name.");
}
$name = strtolower( $r[1] );
}
return $name;
}
/**
* Gets a user state.
*
* @access public
* @param string The path of the state.
* @return mixed The user state.
*/
function getUserState( $key )
{
$session =& JFactory::getSession();
$registry =& $session->get('registry');
if(!is_null($registry)) {
return $registry->getValue($key);
}
return null;
}
/**
* Sets the value of a user state variable.
*
* @access public
* @param string The path of the state.
* @param string The value of the variable.
* @return mixed The previous state, if one existed.
*/
function setUserState( $key, $value )
{
$session =& JFactory::getSession();
$registry =& $session->get('registry');
if(!is_null($registry)) {
return $registry->setValue($key, $value);
}
return null;
}
/**
* Gets the value of a user state variable.
*
* @access public
* @param string The key of the user state variable.
* @param string The name of the variable passed in a request.
* @param string The default value for the variable if not found. Optional.
* @param string Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
* @return The request user state.
*/
function getUserStateFromRequest( $key, $request, $default = null, $type = 'none' )
{
$old_state = $this->getUserState( $key );
$cur_state = (!is_null($old_state)) ? $old_state : $default;
$new_state = JRequest::getVar($request, null, 'default', $type);
// Save the new value only if it was set in this request
if ($new_state !== null) {
$this->setUserState($key, $new_state);
} else {
$new_state = $cur_state;
}
return $new_state;
}
/**
* Registers a handler to a particular event group.
*
* @static
* @param string The event name.
* @param mixed The handler, a function or an instance of a event object.
* @return void
* @since 1.5
*/
function registerEvent($event, $handler)
{
$dispatcher =& JEventDispatcher::getInstance();
$dispatcher->register($event, $handler);
}
/**
* Calls all handlers associated with an event group.
*
* @static
* @param string The event name.
* @param array An array of arguments.
* @return array An array of results from each function call.
* @since 1.5
*/
function triggerEvent($event, $args=null)
{
$dispatcher =& JEventDispatcher::getInstance();
return $dispatcher->trigger($event, $args);
}
/**
* Login authentication function.
*
* Username and encoded password are passed the the onLoginUser event which
* is responsible for the user validation. A successful validation updates
* the current session record with the users 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
* the user details.
*
* @param array Array( 'username' => string, 'password' => string )
* @param array Array( 'remember' => boolean )
* @return boolean True on success.
* @access public
* @since 1.5
*/
function login($credentials, $options = array())
{
// Get the global JAuthentication object
jimport( 'joomla.user.authentication');
$authenticate = & JAuthentication::getInstance();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -