Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/application/application.php

Documentation is available at application.php

  1. <?php
  2. /**
  3. @version        $Id: application.php 9910 2008-01-08 00:10:44Z jinx $
  4. @package        Joomla.Framework
  5. @subpackage    Application
  6. @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19. * Base class for a Joomla! application.
  20. *
  21. * Acts as a Factory class for application specific objects and provides many
  22. * supporting API functions. Derived clases should supply the route(), dispatch()
  23. * and render() functions.
  24. *
  25. @abstract
  26. @package        Joomla.Framework
  27. @subpackage    Application
  28. @since        1.5
  29. */
  30.  
  31. class JApplication extends JObject
  32. {
  33.     /**
  34.      * The client identifier.
  35.      *
  36.      * @var        integer 
  37.      * @access    protected
  38.      * @since    1.5
  39.      */
  40.     var $_clientId = null;
  41.  
  42.     /**
  43.      * The application message queue.
  44.      *
  45.      * @var        array 
  46.      * @access    protected
  47.      */
  48.     var $_messageQueue = array();
  49.  
  50.     /**
  51.      * The name of the application
  52.      *
  53.      * @var        array 
  54.      * @access    protected
  55.      */
  56.     var $_name = null;
  57.     
  58.     /**
  59.      * The scope of the application
  60.      *
  61.      * @var        string 
  62.      * @access    public
  63.      */
  64.     var $scope = null;
  65.  
  66.     /**
  67.     * Class constructor.
  68.     *
  69.     * @param    integer    A client identifier.
  70.     */
  71.     function __construct($config array())
  72.     {
  73.         jimport('joomla.utilities.utility');
  74.  
  75.         //set the view name
  76.         $this->_name        = $this->getName();
  77.         $this->_clientId    = $config['clientId'];
  78.  
  79.         //Enable sessions by default
  80.         if(!isset($config['session'])) {
  81.             $config['session'true;
  82.         }
  83.  
  84.         //Set the session default name
  85.         if(!isset($config['session_name'])) {
  86.              $config['session_name'$this->_name;
  87.         }
  88.  
  89.         //Set the default configuration file
  90.         if(!isset($config['config_file'])) {
  91.             $config['config_file''configuration.php';
  92.         }
  93.  
  94.         //create the configuration object
  95.         $this->_createConfiguration(JPATH_CONFIGURATION.DS.$config['config_file']);
  96.  
  97.         //create the session if a session name is passed
  98.         if($config['session'!== false{
  99.             $this->_createSession(JUtility::getHash($config['session_name']));
  100.         }
  101.  
  102.         $this->set'requestTime'gmdate('Y-m-d H:i') );
  103.     }
  104.  
  105.     /**
  106.      * Returns a reference to the global JApplication object, only creating it if it
  107.      * doesn't already exist.
  108.      *
  109.      * This method must be invoked as:
  110.      *         <pre>  $menu = &JApplication::getInstance();</pre>
  111.      *
  112.      * @access    public
  113.      * @param    mixed    $id         A client identifier or name.
  114.      * @param    array    $config     An optional associative array of configuration settings.
  115.      * @return    JApplication    The appliction object.
  116.      * @since    1.5
  117.      */
  118.     function &getInstance($client$config array()$prefix 'J')
  119.     {
  120.         static $instances;
  121.  
  122.         if (!isset$instances )) {
  123.             $instances array();
  124.         }
  125.  
  126.         if (empty($instances[$client]))
  127.         {
  128.             //Load the router object
  129.             jimport('joomla.application.helper');
  130.             $info =JApplicationHelper::getClientInfo($clienttrue);
  131.  
  132.             $path $info->path.DS.'includes'.DS.'application.php';
  133.             if(file_exists($path))
  134.             {
  135.                 require_once $path;
  136.  
  137.                 // Create a JRouter object
  138.                 $classname $prefix.ucfirst($client);
  139.                 $instance new $classname($config);
  140.             }
  141.             else
  142.             {
  143.                 $error JError::raiseError(500'Unable to load application: '.$client);
  144.                 return $error;
  145.             }
  146.  
  147.             $instances[$client=$instance;
  148.         }
  149.  
  150.         return $instances[$client];
  151.     }
  152.  
  153.     /**
  154.     * Initialise the application.
  155.     *
  156.     * @param    array An optional associative array of configuration settings.
  157.     * @access    public
  158.     */
  159.     function initialise($options array())
  160.     {
  161.         jimport('joomla.plugin.helper');
  162.  
  163.         //Set the language in the class
  164.         $config =JFactory::getConfig();
  165.  
  166.         // Check that we were given a language in the array (since by default may be blank)
  167.         if(isset($options['language'])) {
  168.             $config->setValue('config.language'$options['language']);
  169.         }
  170.  
  171.         // Set user specific editor
  172.         $user     =JFactory::getUser();
  173.         $editor     $user->getParam('editor'$this->getCfg('editor'));
  174.         $editor JPluginHelper::isEnabled('editors'$editor$editor $this->getCfg('editor');
  175.         $config->setValue('config.editor'$editor);
  176.     }
  177.  
  178.     /**
  179.     * Route the application.
  180.     *
  181.     * Routing is the process of examining the request environment to determine which
  182.     * component should receive the request. The component optional parameters
  183.     * are then set in the request object to be processed when the application is being
  184.     * dispatched.
  185.     *
  186.     * @abstract
  187.     * @access    public
  188.     */
  189.     function route()
  190.      {
  191.         // get the full request URI
  192.         $uri clone(JURI::getInstance());
  193.  
  194.         $router =$this->getRouter();
  195.         $result $router->parse($uri);
  196.  
  197.         JRequest::set($result'get'false );
  198.      }
  199.  
  200.      /**
  201.     * Dispatch the applicaiton.
  202.     *
  203.     * Dispatching is the process of pulling the option from the request object and
  204.     * mapping them to a component. If the component does not exist, it handles
  205.     * determining a default component to dispatch.
  206.     *
  207.     * @abstract
  208.     * @access    public
  209.     */
  210.      function dispatch($component)
  211.      {
  212.         $document =JFactory::getDocument();
  213.  
  214.         $document->setTitle$this->getCfg('sitename' )' - ' .JText::_'Administration' ));
  215.         $document->setDescription$this->getCfg('MetaDesc') );
  216.  
  217.         $contents JComponentHelper::renderComponent($component);
  218.         $document->setBuffer($contents'component');
  219.      }
  220.  
  221.     /**
  222.     * Render the application.
  223.     *
  224.     * Rendering is the process of pushing the document buffers into the template
  225.     * placeholders, retrieving data from the document and pushing it into
  226.     * the JResponse buffer.
  227.     *
  228.     * @abstract
  229.     * @access    public
  230.     */
  231.     function render()
  232.     {
  233.         $params array(
  234.             'template'     => $this->getTemplate(),
  235.             'file'        => 'index.php',
  236.             'directory'    => JPATH_THEMES
  237.         );
  238.  
  239.         $document =JFactory::getDocument();
  240.         $data $document->render($this->getCfg('caching')$params );
  241.         JResponse::setBody($data);
  242.     }
  243.  
  244.     /**
  245.     * Exit the application.
  246.     *
  247.     * @access    public
  248.     * @param    int    Exit code
  249.     */
  250.     function close$code {
  251.         exit($code);
  252.     }
  253.  
  254.     /**
  255.      * Redirect to another URL.
  256.      *
  257.      * Optionally enqueues a message in the system message queue (which will be displayed
  258.      * the next time a page is loaded) using the enqueueMessage method. If the headers have
  259.      * not been sent the redirect will be accomplished using a "301 Moved Permanently"
  260.      * code in the header pointing to the new location. If the headers have already been
  261.      * sent this will be accomplished using a JavaScript statement.
  262.      *
  263.      * @access    public
  264.      * @param    string    $url    The URL to redirect to.
  265.      * @param    string    $msg    An optional message to display on redirect.
  266.      * @param    string  $msgType An optional message type.
  267.      * @return    none; calls exit().
  268.      * @since    1.5
  269.      * @see        JApplication::enqueueMessage()
  270.      */
  271.     function redirect$url$msg=''$msgType='message' )
  272.     {
  273.         // check for relative internal links
  274.         if (preg_match'#^index[2]?.php#'$url )) {
  275.             $url JURI::base($url;
  276.         }
  277.  
  278.         // Strip out any line breaks
  279.         $url preg_split("/[\r\n]/"$url);
  280.         $url $url[0];
  281.  
  282.         // If the message exists, enqueue it
  283.         if (trim$msg )) {
  284.             $this->enqueueMessage($msg$msgType);
  285.         }
  286.  
  287.         // Persist messages if they exist
  288.         if (count($this->_messageQueue))
  289.         {
  290.             $session =JFactory::getSession();
  291.             $session->set('application.queue'$this->_messageQueue);
  292.         }
  293.  
  294.         /*
  295.          * If the headers have been sent, then we cannot send an additional location header
  296.          * so we will output a javascript redirect statement.
  297.          */
  298.         if (headers_sent()) {
  299.             echo "<script>document.location.href='$url';</script>\n";
  300.         else {
  301.             //@ob_end_clean(); // clear output buffer
  302.             header'HTTP/1.1 301 Moved Permanently' );
  303.             header'Location: ' $url );
  304.         }
  305.         $this->close();
  306.     }
  307.  
  308.     /**
  309.      * Enqueue a system message.
  310.      *
  311.      * @access    public
  312.      * @param    string     $msg     The message to enqueue.
  313.      * @param    string    $type    The message type.
  314.      * @return    void 
  315.      * @since    1.5
  316.      */
  317.     function enqueueMessage$msg$type 'message' )
  318.     {
  319.         // For empty queue, if messages exists in the session, enqueue them first
  320.         if (!count($this->_messageQueue))
  321.         {
  322.             $session =JFactory::getSession();
  323.             $sessionQueue $session->get('application.queue');
  324.             if (count($sessionQueue)) {
  325.                 $this->_messageQueue = $sessionQueue;
  326.                 $session->set('application.queue'null);
  327.             }
  328.         }
  329.         // Enqueue the message
  330.         $this->_messageQueue[array('message' => $msg'type' => strtolower($type));
  331.     }
  332.  
  333.     /**
  334.      * Get the system message queue.
  335.      *
  336.      * @access    public
  337.      * @return    The system message queue.
  338.      * @since    1.5
  339.      */
  340.     function getMessageQueue()
  341.     {
  342.         // For empty queue, if messages exists in the session, enqueue them
  343.         if (!count($this->_messageQueue))
  344.         {
  345.             $session =JFactory::getSession();
  346.             $sessionQueue $session->get('application.queue');
  347.             if (count($sessionQueue)) {
  348.                 $this->_messageQueue = $sessionQueue;
  349.                 $session->set('application.queue'null);
  350.             }
  351.         }
  352.         return $this->_messageQueue;
  353.     }
  354.  
  355.      /**
  356.      * Gets a configuration value.
  357.      *
  358.      * @access    public
  359.      * @param    string    The name of the value to get.
  360.      * @return    mixed    The user state.
  361.      * @example    application/japplication-getcfg.php Getting a configuration value
  362.      */
  363.     function getCfg$varname )
  364.     {
  365.         $config =JFactory::getConfig();
  366.         return $config->getValue('config.' $varname);
  367.     }
  368.  
  369.     /**
  370.      * Method to get the application name
  371.      *
  372.      * The dispatcher name by default parsed using the classname, or it can be set
  373.      * by passing a $config['name'] in the class constructor
  374.      *
  375.      * @access    public
  376.      * @return    string The name of the dispatcher
  377.      * @since    1.5
  378.      */
  379.     function getName()
  380.     {
  381.         $name $this->_name;
  382.  
  383.         if (empty$name ))
  384.         {
  385.             $r null;
  386.             if !preg_match'/J(.*)/i'get_class$this )$r ) ) {
  387.                 JError::raiseError(500"JApplication::getName() : Can\'t get or parse class name.");
  388.             }
  389.             $name strtolower$r[1);
  390.         }
  391.  
  392.         return $name;
  393.     }
  394.  
  395.     /**
  396.      * Gets a user state.
  397.      *
  398.      * @access    public
  399.      * @param    string    The path of the state.
  400.      * @return    mixed    The user state.
  401.      */
  402.     function getUserState$key )
  403.     {
  404.         $session    =JFactory::getSession();
  405.         $registry    =$session->get('registry');
  406.         if(!is_null($registry)) {
  407.             return $registry->getValue($key);
  408.         }
  409.         return null;
  410.     }
  411.  
  412.     /**
  413.     * Sets the value of a user state variable.
  414.     *
  415.     * @access    public
  416.     * @param    string    The path of the state.
  417.     * @param    string    The value of the variable.
  418.     * @return    mixed    The previous state, if one existed.
  419.     */
  420.     function setUserState$key$value )
  421.     {
  422.         $session    =JFactory::getSession();
  423.         $registry    =$session->get('registry');
  424.         if(!is_null($registry)) {
  425.             return $registry->setValue($key$value);
  426.         }
  427.         return null;
  428.     }
  429.  
  430.     /**
  431.      * Gets the value of a user state variable.
  432.      *
  433.      * @access    public
  434.      * @param    string    The key of the user state variable.
  435.      * @param    string    The name of the variable passed in a request.
  436.      * @param    string    The default value for the variable if not found. Optional.
  437.      * @param    string    Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
  438.      * @return    The request user state.
  439.      */
  440.     function getUserStateFromRequest$key$request$default null$type 'none' )
  441.     {
  442.         $old_state $this->getUserState$key );
  443.         $cur_state (!is_null($old_state)) $old_state $default;
  444.         $new_state JRequest::getVar($requestnull'default'$type);
  445.  
  446.         // Save the new value only if it was set in this request
  447.         if ($new_state !== null{
  448.             $this->setUserState($key$new_state);
  449.         else {
  450.             $new_state $cur_state;
  451.         }
  452.  
  453.         return $new_state;
  454.     }
  455.  
  456.     /**
  457.      * Registers a handler to a particular event group.
  458.      *
  459.      * @static
  460.      * @param    string    The event name.
  461.      * @param    mixed    The handler, a function or an instance of a event object.
  462.      * @return    void 
  463.      * @since    1.5
  464.      */
  465.     function registerEvent($event$handler)
  466.     {
  467.         $dispatcher =JDispatcher::getInstance();
  468.         $dispatcher->register($event$handler);
  469.     }
  470.  
  471.     /**
  472.      * Calls all handlers associated with an event group.
  473.      *
  474.      * @static
  475.      * @param    string    The event name.
  476.      * @param    array    An array of arguments.
  477.      * @return    array    An array of results from each function call.
  478.      * @since    1.5
  479.      */
  480.     function triggerEvent($event$args=null)
  481.     {
  482.         $dispatcher =JDispatcher::getInstance();