Source for file cms.php

Documentation is available at cms.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Application
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Joomla! CMS Application class
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Application
  17.  * @since       3.2
  18.  */
  19. {
  20.     /**
  21.      * Array of options for the JDocument object
  22.      *
  23.      * @var    array 
  24.      * @since  3.2
  25.      */
  26.     protected $docOptions = array();
  27.  
  28.     /**
  29.      * Application instances container.
  30.      *
  31.      * @var    array 
  32.      * @since  3.2
  33.      */
  34.     protected static $instances array();
  35.  
  36.     /**
  37.      * The scope of the application.
  38.      *
  39.      * @var    string 
  40.      * @since  3.2
  41.      */
  42.     public $scope = null;
  43.  
  44.     /**
  45.      * The client identifier.
  46.      *
  47.      * @var    integer 
  48.      * @since  3.2
  49.      * @deprecated  4.0  Will be renamed $clientId
  50.      */
  51.     protected $_clientId = null;
  52.  
  53.     /**
  54.      * The application message queue.
  55.      *
  56.      * @var    array 
  57.      * @since  3.2
  58.      * @deprecated  4.0  Will be renamed $messageQueue
  59.      */
  60.     protected $_messageQueue = array();
  61.  
  62.     /**
  63.      * The name of the application.
  64.      *
  65.      * @var    array 
  66.      * @since  3.2
  67.      * @deprecated  4.0  Will be renamed $name
  68.      */
  69.     protected $_name = null;
  70.  
  71.     /**
  72.      * The profiler instance
  73.      *
  74.      * @var    JProfiler 
  75.      * @since  3.2
  76.      */
  77.     protected $profiler = null;
  78.  
  79.     /**
  80.      * Currently active template
  81.      *
  82.      * @var    object 
  83.      * @since  3.2
  84.      */
  85.     protected $template = null;
  86.  
  87.     /**
  88.      * Indicates that strong encryption should be used.
  89.      *
  90.      * @var    boolean 
  91.      * @since  3.2
  92.      * @note   Default has been changed as of 3.2. If salted md5 is required, it must be explictly set.
  93.      */
  94.     protected $useStrongEncryption = false;
  95.  
  96.     /**
  97.      * Class constructor.
  98.      *
  99.      * @param   mixed  $input   An optional argument to provide dependency injection for the application's
  100.      *                           input object.  If the argument is a JInput object that object will become
  101.      *                           the application's input object, otherwise a default input object is created.
  102.      * @param   mixed  $config  An optional argument to provide dependency injection for the application's
  103.      *                           config object.  If the argument is a JRegistry object that object will become
  104.      *                           the application's config object, otherwise a default config object is created.
  105.      * @param   mixed  $client  An optional argument to provide dependency injection for the application's
  106.      *                           client object.  If the argument is a JApplicationWebClient object that object will become
  107.      *                           the application's client object, otherwise a default client object is created.
  108.      *
  109.      * @since   3.2
  110.      */
  111.     public function __construct(JInput $input nullJRegistry $config nullJApplicationWebClient $client null)
  112.     {
  113.         parent::__construct($input$config$client);
  114.  
  115.         // Load and set the dispatcher
  116.         $this->loadDispatcher();
  117.  
  118.         // If JDEBUG is defined, load the profiler instance
  119.         if (defined('JDEBUG'&& JDEBUG)
  120.         {
  121.             $this->profiler = JProfiler::getInstance('Application');
  122.         }
  123.  
  124.         // Enable sessions by default.
  125.         if (is_null($this->config->get('session')))
  126.         {
  127.             $this->config->set('session'true);
  128.         }
  129.  
  130.         // Set the session default name.
  131.         if (is_null($this->config->get('session_name')))
  132.         {
  133.             $this->config->set('session_name'$this->getName());
  134.         }
  135.  
  136.         // Create the session if a session name is passed.
  137.         if ($this->config->get('session'!== false)
  138.         {
  139.             $this->loadSession();
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * After the session has been started we need to populate it with some default values.
  145.      *
  146.      * @return  void 
  147.      *
  148.      * @since   3.2
  149.      */
  150.     public function afterSessionStart()
  151.     {
  152.         $session JFactory::getSession();
  153.  
  154.         if ($session->isNew())
  155.         {
  156.             $session->set('registry'new JRegistry('session'));
  157.             $session->set('user'new JUser);
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Checks the user session.
  163.      *
  164.      * If the session record doesn't exist, initialise it.
  165.      * If session is new, create session variables
  166.      *
  167.      * @return  void 
  168.      *
  169.      * @since   3.2
  170.      */
  171.     public function checkSession()
  172.     {
  173.         $db JFactory::getDbo();
  174.         $session JFactory::getSession();
  175.         $user JFactory::getUser();
  176.  
  177.         $query $db->getQuery(true)
  178.             ->select($db->quoteName('session_id'))
  179.             ->from($db->quoteName('#__session'))
  180.             ->where($db->quoteName('session_id'' = ' $db->quote($session->getId()));
  181.  
  182.         $db->setQuery($query01);
  183.         $exists $db->loadResult();
  184.  
  185.         // If the session record doesn't exist initialise it.
  186.         if (!$exists)
  187.         {
  188.             $query->clear();
  189.  
  190.             if ($session->isNew())
  191.             {
  192.                 $query->insert($db->quoteName('#__session'))
  193.                     ->columns($db->quoteName('session_id'', ' $db->quoteName('client_id'', ' $db->quoteName('time'))
  194.                     ->values($db->quote($session->getId()) ', ' . (int) $this->getClientId(', ' $db->quote((int) time()));
  195.                 $db->setQuery($query);
  196.             }
  197.             else
  198.             {
  199.                 $query->insert($db->quoteName('#__session'))
  200.                     ->columns(
  201.                         $db->quoteName('session_id'', ' $db->quoteName('client_id'', ' $db->quoteName('guest'', ' .
  202.                         $db->quoteName('time'', ' $db->quoteName('userid'', ' $db->quoteName('username')
  203.                     )
  204.                     ->values(
  205.                         $db->quote($session->getId()) ', ' . (int) $this->getClientId(', ' . (int) $user->get('guest'', ' .
  206.                         $db->quote((int) $session->get('session.timer.start')) ', ' . (int) $user->get('id'', ' $db->quote($user->get('username'))
  207.                     );
  208.  
  209.                 $db->setQuery($query);
  210.             }
  211.  
  212.             // If the insert failed, exit the application.
  213.             try
  214.             {
  215.                 $db->execute();
  216.             }
  217.             catch (RuntimeException $e)
  218.             {
  219.                 jexit($e->getMessage());
  220.             }
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Enqueue a system message.
  226.      *
  227.      * @param   string  $msg   The message to enqueue.
  228.      * @param   string  $type  The message type. Default is message.
  229.      *
  230.      * @return  void 
  231.      *
  232.      * @since   3.2
  233.      */
  234.     public function enqueueMessage($msg$type 'message')
  235.     {
  236.         // For empty queue, if messages exists in the session, enqueue them first.
  237.         if (!count($this->_messageQueue))
  238.         {
  239.             $session JFactory::getSession();
  240.             $sessionQueue $session->get('application.queue');
  241.  
  242.             if (count($sessionQueue))
  243.             {
  244.                 $this->_messageQueue = $sessionQueue;
  245.                 $session->set('application.queue'null);
  246.             }
  247.         }
  248.  
  249.         // Enqueue the message.
  250.         $this->_messageQueue[array('message' => $msg'type' => strtolower($type));
  251.     }
  252.  
  253.     /**
  254.      * Execute the application.
  255.      *
  256.      * @return  void 
  257.      *
  258.      * @since   3.2
  259.      */
  260.     public function execute()
  261.     {
  262.         // Perform application routines.
  263.         $this->doExecute();
  264.  
  265.         // If we have an application document object, render it.
  266.         if ($this->document instanceof JDocument)
  267.         {
  268.             // Render the application output.
  269.             $this->render();
  270.         }
  271.  
  272.         // If gzip compression is enabled in configuration and the server is compliant, compress the output.
  273.         if ($this->get('gzip'&& !ini_get('zlib.output_compression'&& (ini_get('output_handler'!= 'ob_gzhandler'))
  274.         {
  275.             $this->compress();
  276.  
  277.             // Trigger the onAfterCompress event.
  278.             $this->triggerEvent('onAfterCompress');
  279.         }
  280.  
  281.         // Send the application response.
  282.         $this->respond();
  283.  
  284.         // Trigger the onAfterRespond event.
  285.         $this->triggerEvent('onAfterRespond');
  286.     }
  287.  
  288.     /**
  289.      * Gets a configuration value.
  290.      *
  291.      * @param   string  $varname  The name of the value to get.
  292.      * @param   string  $default  Default value to return
  293.      *
  294.      * @return  mixed  The user state.
  295.      *
  296.      * @since   3.2
  297.      * @deprecated  4.0  Use get() instead
  298.      */
  299.     public function getCfg($varname$default null)
  300.     {
  301.         return $this->get($varname$default);
  302.     }
  303.  
  304.     /**
  305.      * Gets the client id of the current running application.
  306.      *
  307.      * @return  integer  A client identifier.
  308.      *
  309.      * @since   3.2
  310.      */
  311.     public function getClientId()
  312.     {
  313.         return $this->_clientId;
  314.     }
  315.  
  316.     /**
  317.      * Returns a reference to the global JApplicationCms object, only creating it if it doesn't already exist.
  318.      *
  319.      * This method must be invoked as: $web = JApplicationCms::getInstance();
  320.      *
  321.      * @param   string  $name  The name (optional) of the JApplicationCms class to instantiate.
  322.      *
  323.      * @return  JApplicationCms 
  324.      *
  325.      * @since   3.2
  326.      * @throws  RuntimeException
  327.      */
  328.     public static function getInstance($name null)
  329.     {
  330.         if (empty(static::$instances[$name]))
  331.         {
  332.             // Create a JApplicationCms object.
  333.             $classname 'JApplication' ucfirst($name);
  334.  
  335.             if (!class_exists($classname))
  336.             {
  337.                 throw new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD'$name)500);
  338.             }
  339.  
  340.             static::$instances[$namenew $classname;
  341.         }
  342.  
  343.         return static::$instances[$name];
  344.     }
  345.  
  346.     /**
  347.      * Returns the application JMenu object.
  348.      *
  349.      * @param   string  $name     The name of the application/client.
  350.      * @param   array   $options  An optional associative array of configuration settings.
  351.      *
  352.      * @return  JMenu 
  353.      *
  354.      * @since   3.2
  355.      */
  356.     public function getMenu($name null$options array())
  357.     {
  358.         if (!isset($name))
  359.         {
  360.             $name $this->getName();
  361.         }
  362.  
  363.         try
  364.         {
  365.             $menu JMenu::getInstance($name$options);
  366.         }
  367.         catch (Exception $e)
  368.         {
  369.             return null;
  370.         }
  371.  
  372.         return $menu;
  373.     }
  374.  
  375.     /**
  376.      * Get the system message queue.
  377.      *
  378.      * @return  array  The system message queue.
  379.      *
  380.      * @since   3.2
  381.      */
  382.     public function getMessageQueue()
  383.     {
  384.         // For empty queue, if messages exists in the session, enqueue them.
  385.         if (!count($this->_messageQueue))
  386.         {
  387.             $session JFactory::getSession();
  388.             $sessionQueue $session->get('application.queue');
  389.  
  390.             if (count($sessionQueue))
  391.             {
  392.                 $this->_messageQueue $sessionQueue;
  393.                 $session->set('application.queue'null);
  394.             }
  395.         }
  396.  
  397.         return $this->_messageQueue;
  398.     }
  399.  
  400.     /**
  401.      * Gets the name of the current running application.
  402.      *
  403.      * @return  string  The name of the application.
  404.      *
  405.      * @since   3.2
  406.      */
  407.     public function getName()
  408.     {
  409.         return $this->_name;
  410.     }
  411.  
  412.     /**
  413.      * Returns the application JPathway object.
  414.      *
  415.      * @param   string  $name     The name of the application.
  416.      * @param   array   $options  An optional associative array of configuration settings.
  417.      *
  418.      * @return  JPathway 
  419.      *
  420.      * @since   3.2
  421.      */
  422.     public function getPathway($name null$options array())
  423.     {
  424.         if (!isset($name))
  425.         {
  426.             $name $this->getName();
  427.         }
  428.  
  429.         try
  430.         {
  431.             $pathway JPathway::getInstance($name$options);
  432.         }
  433.         catch (Exception $e)
  434.         {
  435.             return null;
  436.         }
  437.  
  438.         return $pathway;
  439.     }
  440.  
  441.     /**
  442.      * Returns the application JRouter object.
  443.      *
  444.      * @param   string  $name     The name of the application.
  445.      * @param   array   $options  An optional associative array of configuration settings.
  446.      *
  447.      * @return  JRouter 
  448.      *
  449.      * @since   3.2
  450.      */
  451.     public static function getRouter($name nullarray $options array())
  452.     {
  453.         if (!isset($name))
  454.         {
  455.             $app JFactory::getApplication();
  456.             $name $app->getName();
  457.         }
  458.  
  459.         try
  460.         {
  461.             $router JRouter::getInstance($name$options);
  462.         }
  463.         catch (Exception $e)
  464.         {
  465.             return null;
  466.         }
  467.  
  468.         return $router;
  469.     }
  470.  
  471.     /**
  472.      * Gets the name of the current template.
  473.      *
  474.      * @param   boolean  $params  An optional associative array of configuration settings
  475.      *
  476.      * @return  mixed  System is the fallback.
  477.      *
  478.      * @since   3.2
  479.      */
  480.     public function getTemplate($params false)
  481.     {
  482.         $template new stdClass;
  483.  
  484.         $template->template 'system';
  485.         $template->params   new JRegistry;
  486.  
  487.         if ($params)
  488.         {
  489.             return $template;
  490.         }
  491.  
  492.         return $template->template;
  493.     }
  494.  
  495.     /**
  496.      * Gets a user state.
  497.      *
  498.      * @param   string  $key      The path of the state.
  499.      * @param   mixed   $default  Optional default value, returned if the internal value is null.
  500.      *
  501.      * @return  mixed  The user state or null.
  502.      *
  503.      * @since   3.2
  504.      */
  505.     public function getUserState($key$default null)
  506.     {
  507.         $session JFactory::getSession();
  508.         $registry $session->get('registry');
  509.  
  510.         if (!is_null($registry))
  511.         {
  512.             return $registry->get($key$default);
  513.         }
  514.  
  515.         return $default;
  516.     }
  517.  
  518.     /**
  519.      * Gets the value of a user state variable.
  520.      *
  521.      * @param   string  $key      The key of the user state variable.
  522.      * @param   string  $request  The name of the variable passed in a request.
  523.      * @param   string  $default  The default value for the variable if not found. Optional.
  524.      * @param   string  $type     Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
  525.      *
  526.      * @return  object  The request user state.
  527.      *
  528.      * @since   3.2
  529.      */
  530.     public function getUserStateFromRequest($key$request$default null$type 'none')
  531.     {
  532.         $cur_state $this->getUserState($key$default);
  533.         $new_state $this->input->get($requestnull$type);
  534.  
  535.         // Save the new value only if it was set in this request.
  536.         if ($new_state !== null)
  537.         {
  538.             $this->setUserState($key$new_state);
  539.         }
  540.         else
  541.         {
  542.             $new_state $cur_state;
  543.         }
  544.  
  545.         return $new_state;
  546.     }
  547.  
  548.     /**
  549.      * Initialise the application.
  550.      *
  551.      * @param   array  $options  An optional associative array of configuration settings.
  552.      *
  553.      * @return  void 
  554.      *
  555.      * @since   3.2
  556.      */
  557.     protected function initialiseApp($options array())
  558.     {
  559.         // Set the configuration in the API.
  560.         $this->config JFactory::getConfig();
  561.  
  562.         // Check that we were given a language in the array (since by default may be blank).
  563.         if (isset($options['language']))
  564.         {
  565.             $this->set('language'$options['language']);
  566.         }
  567.  
  568.         // Set user specific editor.
  569.         $user JFactory::getUser();
  570.         $editor $user->getParam('editor'$this->get('editor'));
  571.  
  572.         if (!JPluginHelper::isEnabled('editors'$editor))
  573.         {
  574.             $editor $this->get('editor');
  575.  
  576.             if (!JPluginHelper::isEnabled('editors'$editor))
  577.             {
  578.                 $editor 'none';
  579.             }
  580.         }
  581.  
  582.         $this->set('editor'$editor);
  583.  
  584.         /*
  585.          * Set the encryption to use. The availability of strong encryption must always be checked separately.
  586.          * Use JCrypt::hasStrongPasswordSupport() to check PHP for this support.
  587.          */
  588.         if (JPluginHelper::isEnabled('user''joomla'))
  589.         {
  590.             $userPlugin JPluginHelper::getPlugin('user''joomla');
  591.             $userPluginParams new JRegistry;
  592.             $userPluginParams->loadString($userPlugin->params);
  593.             $useStrongEncryption $userPluginParams->get('strong_passwords'0);
  594.  
  595.             $this->config->set('useStrongEncryption'$useStrongEncryption);
  596.         }
  597.  
  598.         // Trigger the onAfterInitialise event.
  599.         JPluginHelper::importPlugin('system');
  600.         $this->triggerEvent('onAfterInitialise');
  601.     }
  602.  
  603.     /**
  604.      * Is admin interface?
  605.      *
  606.      * @return  boolean  True if this application is administrator.
  607.      *
  608.      * @since   3.2
  609.      */
  610.     public function isAdmin()
  611.     {
  612.         return ($this->getClientId(=== 1);
  613.     }
  614.  
  615.     /**
  616.      * Is site interface?
  617.      *
  618.      * @return  boolean  True if this application is site.
  619.      *
  620.      * @since   3.2
  621.      */
  622.     public function isSite()
  623.     {
  624.         return ($this->getClientId(=== 0);
  625.     }
  626.  
  627.     /**
  628.      * Allows the application to load a custom or default session.
  629.      *
  630.      * The logic and options for creating this object are adequately generic for default cases
  631.      * but for many applications it will make sense to override this method and create a session,
  632.      * if required, based on more specific needs.
  633.      *
  634.      * @param   JSession  $session  An optional session object. If omitted, the session is created.
  635.      *
  636.      * @return  JApplicationCms  This method is chainable.
  637.      *
  638.      * @since   3.2
  639.      */
  640.     public function loadSession(JSession $session null)
  641.     {
  642.         if ($session !== null)
  643.         {
  644.             $this->session $session;
  645.  
  646.             return $this;
  647.         }
  648.  
  649.         // Generate a session name.
  650.         $name JApplicationHelper::getHash($this->get('session_name'get_class($this)));
  651.  
  652.         // Calculate the session lifetime.
  653.         $lifetime (($this->get('lifetime')) $this->get('lifetime'60 900);
  654.  
  655.         // Initialize the options for JSession.
  656.         $options array(
  657.             'name'   => $name,
  658.             'expire' => $lifetime
  659.         );
  660.  
  661.         switch ($this->getClientId())
  662.         {
  663.             case 0:
  664.                 if ($this->get('force_ssl'== 2)
  665.                 {
  666.                     $options['force_ssl'true;
  667.                 }
  668.  
  669.                 break;
  670.  
  671.             case 1:
  672.                 if ($this->get('force_ssl'>= 1)
  673.                 {
  674.                     $options['force_ssl'true;
  675.                 }
  676.  
  677.                 break;
  678.         }
  679.  
  680.         $this->registerEvent('onAfterSessionStart'array($this'afterSessionStart'));
  681.  
  682.         // There's an internal coupling to the session object being present in JFactory, need to deal with this at some point
  683.         $session JFactory::getSession($options);
  684.         $session->initialise($this->input$this->dispatcher);
  685.         $session->start();
  686.  
  687.         // TODO: At some point we need to get away from having session data always in the db.
  688.         $db JFactory::getDbo();
  689.  
  690.         // Remove expired sessions from the database.
  691.         $time time();
  692.  
  693.         if ($time 2)
  694.         {
  695.             // The modulus introduces a little entropy, making the flushing less accurate
  696.             // but fires the query less than half the time.
  697.             $query $db->getQuery(true)
  698.                 ->delete($db->quoteName('#__session'))
  699.                 ->where($db->quoteName('time'' < ' $db->quote((int) ($time $session->getExpire())));
  700.  
  701.             $db->setQuery($query);
  702.             $db->execute();
  703.         }
  704.  
  705.         // Get the session handler from the configuration.
  706.         $handler $this->get('session_handler''none');
  707.  
  708.         if (($handler != 'database' && ($time || $session->isNew()))
  709.             || ($handler == 'database' && $session->isNew()))
  710.         {
  711.             $this->checkSession();
  712.         }
  713.  
  714.         // Set the session object.
  715.         $this->session $session;
  716.  
  717.         return $this;
  718.     }
  719.  
  720.     /**
  721.      * Login authentication function.
  722.      *
  723.      * Username and encoded password are passed the onUserLogin event which
  724.      * is responsible for the user validation. A successful validation updates
  725.      * the current session record with the user's details.
  726.      *
  727.      * Username and encoded password are sent as credentials (along with other
  728.      * possibilities) to each observer (authentication plugin) for user
  729.      * validation.  Successful validation will update the current session with
  730.      * the user details.
  731.      *
  732.      * @param   array  $credentials  Array('username' => string, 'password' => string)
  733.      * @param   array  $options      Array('remember' => boolean)
  734.      *
  735.      * @return  boolean  True on success.
  736.      *
  737.      * @since   3.2
  738.      */
  739.     public function login($credentials$options array())
  740.     {
  741.         // Get the global JAuthentication object.
  742.         jimport('joomla.user.authentication');
  743.  
  744.         $authenticate JAuthentication::getInstance();
  745.         $response $authenticate->authenticate($credentials$options);
  746.  
  747.         if ($response->status === JAuthentication::STATUS_SUCCESS)
  748.         {
  749.             /*
  750.              * Validate that the user should be able to login (different to being authenticated).
  751.              * This permits authentication plugins blocking the user.
  752.              */
  753.             $authorisations $authenticate->authorise($response$options);
  754.  
  755.             foreach ($authorisations as $authorisation)
  756.             {
  757.                 $denied_states array(JAuthentication::STATUS_EXPIREDJAuthentication::STATUS_DENIED);
  758.  
  759.                 if (in_array($authorisation->status$denied_states))
  760.                 {
  761.                     // Trigger onUserAuthorisationFailure Event.
  762.                     $this->triggerEvent('onUserAuthorisationFailure'array((array) $authorisation));
  763.  
  764.                     // If silent is set, just return false.
  765.                     if (isset($options['silent']&& $options['silent'])
  766.                     {
  767.                         return false;
  768.                     }
  769.  
  770.                     // Return the error.
  771.                     switch ($authorisation->status)
  772.                     {
  773.                         case JAuthentication::STATUS_EXPIRED:
  774.                             return JError::raiseWarning('102002'JText::_('JLIB_LOGIN_EXPIRED'));
  775.  
  776.                             break;
  777.  
  778.                         case JAuthentication::STATUS_DENIED:
  779.                             return JError::raiseWarning('102003'JText::_('JLIB_LOGIN_DENIED'));
  780.  
  781.                             break;
  782.  
  783.                         default:
  784.                             return JError::raiseWarning('102004'JText::_('JLIB_LOGIN_AUTHORISATION'));
  785.  
  786.                             break;
  787.                     }
  788.                 }
  789.             }
  790.  
  791.             // Import the user plugin group.
  792.             JPluginHelper::importPlugin('user');
  793.  
  794.             // OK, the credentials are authenticated and user is authorised.  Let's fire the onLogin event.
  795.             $results $this->triggerEvent('onUserLogin'array((array) $response$options));
  796.  
  797.             /*
  798.              * If any of the user plugins did not successfully complete the login routine
  799.              * then the whole method fails.
  800.              *
  801.              * Any errors raised should be done in the plugin as this provides the ability
  802.              * to provide much more information about why the routine may have failed.
  803.              */
  804.             $user JFactory::getUser();
  805.  
  806.             if ($response->type == 'Cookie')
  807.             {
  808.                 $user->set('cookieLogin'true);
  809.             }
  810.  
  811.             if (in_array(false$resultstrue== false)
  812.             {
  813.                 $options['user'$user;
  814.                 $options['responseType'$response->type;
  815.  
  816.                 if (isset($response->length&& isset($response->secure&& isset($response->lifetime))
  817.                 {
  818.                     $options['length'$response->length;
  819.                     $options['secure'$response->secure;
  820.                     $options['lifetime'$response->lifetime;
  821.                 }
  822.  
  823.                 // The user is successfully logged in. Run the after login events
  824.                 $this->triggerEvent('onUserAfterLogin'array($options));
  825.             }
  826.  
  827.             return true;
  828.         }
  829.  
  830.         // Trigger onUserLoginFailure Event.
  831.         $this->triggerEvent('onUserLoginFailure'array((array) $response));
  832.  
  833.         // If silent is set, just return false.
  834.         if (isset($options['silent']&& $options['silent'])
  835.         {
  836.             return false;
  837.         }
  838.  
  839.         // If status is success, any error will have been raised by the user plugin
  840.         if ($response->status !== JAuthentication::STATUS_SUCCESS)
  841.         {
  842.             JLog::add($response->error_messageJLog::WARNING'jerror');
  843.         }
  844.  
  845.         return false;
  846.     }
  847.  
  848.     /**
  849.      * Logout authentication function.
  850.      *
  851.      * Passed the current user information to the onUserLogout event and reverts the current
  852.      * session record back to 'anonymous' parameters.
  853.      * If any of the authentication plugins did not successfully complete
  854.      * the logout routine then the whole method fails. Any errors raised
  855.      * should be done in the plugin as this provides the ability to give
  856.      * much more information about why the routine may have failed.
  857.      *
  858.      * @param   integer  $userid   The user to load - Can be an integer or string - If string, it is converted to ID automatically
  859.      * @param   array    $options  Array('clientid' => array of client id's)
  860.      *
  861.      * @return  boolean  True on success
  862.      *
  863.      * @since   3.2
  864.      */
  865.     public function logout($userid null$options array())
  866.     {
  867.         // Get a user object from the JApplication.
  868.         $user JFactory::getUser($userid);
  869.  
  870.         // Build the credentials array.
  871.         $parameters['username'$user->get('username');
  872.         $parameters['id'$user->get('id');
  873.  
  874.         // Set clientid in the options array if it hasn't been set already.
  875.         if (!isset($options['clientid']))
  876.         {
  877.             $options['clientid'$this->getClientId();
  878.         }
  879.  
  880.         // Import the user plugin group.
  881.         JPluginHelper::importPlugin('user');
  882.  
  883.         // OK, the credentials are built. Lets fire the onLogout event.
  884.         $results $this->triggerEvent('onUserLogout'array($parameters$options));
  885.  
  886.         // Check if any of the plugins failed. If none did, success.
  887.         if (!in_array(false$resultstrue))
  888.         {
  889.             $options['username'$user->get('username');
  890.             $this->triggerEvent('onUserAfterLogout'array($options));
  891.  
  892.             return true;
  893.         }
  894.  
  895.         // Trigger onUserLoginFailure Event.
  896.         $this->triggerEvent('onUserLogoutFailure'array($parameters));
  897.  
  898.         return false;
  899.     }
  900.  
  901.     /**
  902.      * Redirect to another URL.
  903.      *
  904.      * If the headers have not been sent the redirect will be accomplished using a "301 Moved Permanently"
  905.      * or "303 See Other" code in the header pointing to the new location. If the headers have already been
  906.      * sent this will be accomplished using a JavaScript statement.
  907.      *
  908.      * @param   string   $url    The URL to redirect to. Can only be http/https URL
  909.      * @param   boolean  $moved  True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
  910.      *
  911.      * @return  void 
  912.      *
  913.      * @since   3.2
  914.      */
  915.     public function redirect($url$moved false)
  916.     {
  917.         // Persist messages if they exist.
  918.         if (count($this->_messageQueue))
  919.         {
  920.             $session JFactory::getSession();
  921.             $session->set('application.queue'$this->_messageQueue);
  922.         }
  923.  
  924.         // Handle B/C by checking if a message was passed to the method, will be removed at 4.0
  925.         if (func_num_args(1)
  926.         {
  927.             $args func_get_args();
  928.  
  929.             /*
  930.              * Do some checks on the $args array, values below correspond to legacy redirect() method
  931.              *
  932.              * $args[0] = $url
  933.              * $args[1] = Message to enqueue
  934.              * $args[2] = Message type
  935.              * $args[3] = $moved
  936.              */
  937.             if (isset($args[1]&& !empty($args[1]&& !is_bool($args[1]))
  938.             {
  939.                 // Log that passing the message to the function is deprecated
  940.                 JLog::add(
  941.                     'Passing a message and message type to JFactory::getApplication()->redirect() is deprecated. '
  942.                     . 'Please set your message via JFactory::getApplication()->enqueueMessage() prior to calling redirect().',
  943.                     JLog::WARNING,
  944.                     'deprecated'
  945.                 );
  946.  
  947.                 $message $args[1];
  948.  
  949.                 // Set the message type if present
  950.                 if (isset($args[2]&& !empty($args[2]))
  951.                 {
  952.                     $type $args[2];
  953.                 }
  954.                 else
  955.                 {
  956.                     $type null;
  957.                 }
  958.  
  959.                 // Enqueue the message
  960.                 $this->enqueueMessage($message$type);
  961.  
  962.                 // Reset the $moved variable
  963.                 $moved = isset($args[3]? (boolean) $args[3false;
  964.             }
  965.         }
  966.  
  967.         // Hand over processing to the parent now
  968.         parent::redirect($url$moved);
  969.     }
  970.  
  971.     /**
  972.      * Rendering is the process of pushing the document buffers into the template
  973.      * placeholders, retrieving data from the document and pushing it into
  974.      * the application response buffer.
  975.      *
  976.      * @return  void 
  977.      *
  978.      * @since   3.2
  979.      */
  980.     protected function render()
  981.     {
  982.         // Setup the document options.
  983.         $this->docOptions['template'$this->get('theme');
  984.         $this->docOptions['file']     $this->get('themeFile''index.php');
  985.         $this->docOptions['params']   $this->get('themeParams');
  986.  
  987.         if ($this->get('themes.base'))
  988.         {
  989.             $this->docOptions['directory'$this->get('themes.base');
  990.         }
  991.         // Fall back to constants.
  992.         else
  993.         {
  994.             $this->docOptions['directory'defined('JPATH_THEMES'JPATH_THEMES (defined('JPATH_BASE'JPATH_BASE : __DIR__'/themes';
  995.         }
  996.  
  997.         // Parse the document.
  998.         $this->document->parse($this->docOptions);
  999.  
  1000.         // Trigger the onBeforeRender event.
  1001.         JPluginHelper::importPlugin('system');
  1002.         $this->triggerEvent('onBeforeRender');
  1003.  
  1004.         $caching false;
  1005.  
  1006.         if ($this->isSite(&& $this->get('caching'&& $this->get('caching'2== && !JFactory::getUser()->get('id'))
  1007.         {
  1008.             $caching true;
  1009.         }
  1010.  
  1011.         // Render the document.
  1012.         $data $this->document->render($caching$this->docOptions);
  1013.  
  1014.         // Set the application output data.
  1015.         $this->setBody($data);
  1016.  
  1017.         // Trigger the onAfterRender event.
  1018.         $this->triggerEvent('onAfterRender');
  1019.  
  1020.         // Mark afterRender in the profiler.
  1021.         JDEBUG $this->profiler->mark('afterRender'null;
  1022.     }
  1023.  
  1024.     /**
  1025.      * Route the application.
  1026.      *
  1027.      * Routing is the process of examining the request environment to determine which
  1028.      * component should receive the request. The component optional parameters
  1029.      * are then set in the request object to be processed when the application is being
  1030.      * dispatched.
  1031.      *
  1032.      * @return  void 
  1033.      *
  1034.      * @since   3.2
  1035.      */
  1036.     protected function route()
  1037.     {
  1038.         // Get the full request URI.
  1039.         $uri clone JUri::getInstance();
  1040.  
  1041.         $router $this->getRouter();
  1042.         $result $router->parse($uri);
  1043.  
  1044.         foreach ($result as $key => $value)
  1045.         {
  1046.             $this->input->def($key$value);
  1047.         }
  1048.  
  1049.         // Trigger the onAfterRoute event.
  1050.         JPluginHelper::importPlugin('system');
  1051.         $this->triggerEvent('onAfterRoute');
  1052.     }
  1053.  
  1054.     /**
  1055.      * Sets the value of a user state variable.
  1056.      *
  1057.      * @param   string  $key    The path of the state.
  1058.      * @param   string  $value  The value of the variable.
  1059.      *
  1060.      * @return  mixed  The previous state, if one existed.
  1061.      *
  1062.      * @since   3.2
  1063.      */
  1064.     public function setUserState($key$value)
  1065.     {
  1066.         $session JFactory::getSession();
  1067.         $registry $session->get('registry');
  1068.  
  1069.         if (!is_null($registry))
  1070.         {
  1071.             return $registry->set($key$value);
  1072.         }
  1073.  
  1074.         return null;
  1075.     }
  1076.  
  1077.     /**
  1078.      * Sends all headers prior to returning the string
  1079.      *
  1080.      * @param   boolean  $compress  If true, compress the data
  1081.      *
  1082.      * @return  string 
  1083.      *
  1084.      * @since   3.2
  1085.      */
  1086.     public function toString($compress false)
  1087.     {
  1088.         // Don't compress something if the server is going to do it anyway. Waste of time.
  1089.         if ($compress && !ini_get('zlib.output_compression'&& ini_get('output_handler'!= 'ob_gzhandler')
  1090.         {
  1091.             $this->compress();
  1092.         }
  1093.  
  1094.         if ($this->allowCache(=== false)
  1095.         {
  1096.             $this->setHeader('Cache-Control''no-cache'false);
  1097.  
  1098.             // HTTP 1.0
  1099.             $this->setHeader('Pragma''no-cache');
  1100.         }
  1101.  
  1102.         $this->sendHeaders();
  1103.  
  1104.         return $this->getBody();
  1105.     }
  1106. }

Documentation generated on Tue, 19 Nov 2013 14:55:59 +0100 by phpDocumentor 1.4.3