Source for file site.php

Documentation is available at site.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! Site Application class
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Application
  17.  * @since       3.2
  18.  */
  19. final class JApplicationSite extends JApplicationCms
  20. {
  21.     /**
  22.      * Option to filter by language
  23.      *
  24.      * @var    boolean 
  25.      * @since  3.2
  26.      * @deprecated  4.0  Will be renamed $language_filter
  27.      */
  28.     protected $_language_filter = false;
  29.  
  30.     /**
  31.      * Option to detect language by the browser
  32.      *
  33.      * @var    boolean 
  34.      * @since  3.2
  35.      * @deprecated  4.0  Will be renamed $detect_browser
  36.      */
  37.     protected $_detect_browser = false;
  38.  
  39.     /**
  40.      * Class constructor.
  41.      *
  42.      * @param   mixed  $input   An optional argument to provide dependency injection for the application's
  43.      *                           input object.  If the argument is a JInput object that object will become
  44.      *                           the application's input object, otherwise a default input object is created.
  45.      * @param   mixed  $config  An optional argument to provide dependency injection for the application's
  46.      *                           config object.  If the argument is a JRegistry object that object will become
  47.      *                           the application's config object, otherwise a default config object is created.
  48.      * @param   mixed  $client  An optional argument to provide dependency injection for the application's
  49.      *                           client object.  If the argument is a JApplicationWebClient object that object will become
  50.      *                           the application's client object, otherwise a default client object is created.
  51.      *
  52.      * @since   3.2
  53.      */
  54.     public function __construct(JInput $input nullJRegistry $config nullJApplicationWebClient $client null)
  55.     {
  56.         // Register the application name
  57.         $this->_name = 'site';
  58.  
  59.         // Register the client ID
  60.         $this->_clientId = 0;
  61.  
  62.         // Execute the parent constructor
  63.         parent::__construct($input$config$client);
  64.     }
  65.  
  66.     /**
  67.      * Check if the user can access the application
  68.      *
  69.      * @param   integer  $itemid  The item ID to check authorisation for
  70.      *
  71.      * @return  void 
  72.      *
  73.      * @since   3.2
  74.      */
  75.     protected function authorise($itemid)
  76.     {
  77.         $menus $this->getMenu();
  78.         $user JFactory::getUser();
  79.  
  80.         if (!$menus->authorise($itemid))
  81.         {
  82.             if ($user->get('id'== 0)
  83.             {
  84.                 // Set the data
  85.                 $this->setUserState('users.login.form.data'array('return' => JUri::getInstance()->toString()));
  86.  
  87.                 $url JRoute::_('index.php?option=com_users&view=login'false);
  88.  
  89.                 $this->enqueueMessage(JText::_('JGLOBAL_YOU_MUST_LOGIN_FIRST'));
  90.                 $this->redirect($url);
  91.             }
  92.             else
  93.             {
  94.                 $this->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR')'error');
  95.             }
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * Dispatch the application
  101.      *
  102.      * @param   string  $component  The component which is being rendered.
  103.      *
  104.      * @return  void 
  105.      *
  106.      * @since   3.2
  107.      */
  108.     public function dispatch($component null)
  109.     {
  110.         // Get the component if not set.
  111.         if (!$component)
  112.         {
  113.             $component $this->input->getCmd('option'null);
  114.         }
  115.  
  116.         // Load the document to the API
  117.         $this->loadDocument();
  118.  
  119.         // Set up the params
  120.         $document $this->getDocument();
  121.         $router   static::getRouter();
  122.         $params   $this->getParams();
  123.  
  124.         // Register the document object with JFactory
  125.         JFactory::$document $document;
  126.  
  127.         switch ($document->getType())
  128.         {
  129.             case 'html':
  130.                 // Get language
  131.                 $lang_code $this->getLanguage()->getTag();
  132.                 $languages JLanguageHelper::getLanguages('lang_code');
  133.  
  134.                 // Set metadata
  135.                 if (isset($languages[$lang_code]&& $languages[$lang_code]->metakey)
  136.                 {
  137.                     $document->setMetaData('keywords'$languages[$lang_code]->metakey);
  138.                 }
  139.                 else
  140.                 {
  141.                     $document->setMetaData('keywords'$this->get('MetaKeys'));
  142.                 }
  143.  
  144.                 $document->setMetaData('rights'$this->get('MetaRights'));
  145.  
  146.                 if ($router->getMode(== JROUTER_MODE_SEF)
  147.                 {
  148.                     $document->setBase(htmlspecialchars(JUri::current()));
  149.                 }
  150.  
  151.                 // Get the template
  152.                 $template $this->getTemplate(true);
  153.  
  154.                 // Store the template and its params to the config
  155.                 $this->set('theme'$template->template);
  156.                 $this->set('themeParams'$template->params);
  157.  
  158.                 break;
  159.  
  160.             case 'feed':
  161.                 $document->setBase(htmlspecialchars(JUri::current()));
  162.                 break;
  163.         }
  164.  
  165.         $document->setTitle($params->get('page_title'));
  166.         $document->setDescription($params->get('page_description'));
  167.  
  168.         // Add version number or not based on global configuration
  169.         if ($this->get('MetaVersion'0))
  170.         {
  171.             $document->setGenerator('Joomla! - Open Source Content Management  - Version ' JVERSION);
  172.         }
  173.         else
  174.         {
  175.             $document->setGenerator('Joomla! - Open Source Content Management');
  176.         }
  177.  
  178.         $contents JComponentHelper::renderComponent($component);
  179.         $document->setBuffer($contents'component');
  180.  
  181.         // Trigger the onAfterDispatch event.
  182.         JPluginHelper::importPlugin('system');
  183.         $this->triggerEvent('onAfterDispatch');
  184.     }
  185.  
  186.     /**
  187.      * Method to run the Web application routines.
  188.      *
  189.      * @return  void 
  190.      *
  191.      * @since   3.2
  192.      */
  193.     protected function doExecute()
  194.     {
  195.         // Initialise the application
  196.         $this->initialiseApp();
  197.  
  198.         // Mark afterInitialise in the profiler.
  199.         JDEBUG $this->profiler->mark('afterInitialise'null;
  200.  
  201.         // Route the application
  202.         $this->route();
  203.  
  204.         // Mark afterRoute in the profiler.
  205.         JDEBUG $this->profiler->mark('afterRoute'null;
  206.  
  207.         // Dispatch the application
  208.         $this->dispatch();
  209.  
  210.         // Mark afterDispatch in the profiler.
  211.         JDEBUG $this->profiler->mark('afterDispatch'null;
  212.     }
  213.  
  214.     /**
  215.      * Return the current state of the detect browser option.
  216.      *
  217.      * @return    boolean 
  218.      *
  219.      * @since    3.2
  220.      */
  221.     public function getDetectBrowser()
  222.     {
  223.         return $this->_detect_browser;
  224.     }
  225.  
  226.     /**
  227.      * Return the current state of the language filter.
  228.      *
  229.      * @return    boolean 
  230.      *
  231.      * @since    3.2
  232.      */
  233.     public function getLanguageFilter()
  234.     {
  235.         return $this->_language_filter;
  236.     }
  237.  
  238.     /**
  239.      * Return a reference to the JMenu object.
  240.      *
  241.      * @param   string  $name     The name of the application/client.
  242.      * @param   array   $options  An optional associative array of configuration settings.
  243.      *
  244.      * @return  JMenu  JMenu object.
  245.      *
  246.      * @since   3.2
  247.      */
  248.     public function getMenu($name 'site'$options array())
  249.     {
  250.         $menu parent::getMenu($name$options);
  251.  
  252.         return $menu;
  253.     }
  254.  
  255.     /**
  256.      * Get the application parameters
  257.      *
  258.      * @param   string  $option  The component option
  259.      *
  260.      * @return  object  The parameters object
  261.      *
  262.      * @since   3.2
  263.      * @deprecated  4.0  Use getParams() instead
  264.      */
  265.     public function getPageParameters($option null)
  266.     {
  267.         return $this->getParams($option);
  268.     }
  269.  
  270.     /**
  271.      * Get the application parameters
  272.      *
  273.      * @param   string  $option  The component option
  274.      *
  275.      * @return  object  The parameters object
  276.      *
  277.      * @since   3.2
  278.      */
  279.     public function getParams($option null)
  280.     {
  281.         static $params array();
  282.  
  283.         $hash '__default';
  284.         if (!empty($option))
  285.         {
  286.             $hash $option;
  287.         }
  288.  
  289.         if (!isset($params[$hash]))
  290.         {
  291.             // Get component parameters
  292.             if (!$option)
  293.             {
  294.                 $option $this->input->getCmd('option'null);
  295.             }
  296.  
  297.             // Get new instance of component global parameters
  298.             $params[$hashclone JComponentHelper::getParams($option);
  299.  
  300.             // Get menu parameters
  301.             $menus $this->getMenu();
  302.             $menu  $menus->getActive();
  303.  
  304.             // Get language
  305.             $lang_code $this->getLanguage()->getTag();
  306.             $languages JLanguageHelper::getLanguages('lang_code');
  307.  
  308.             $title $this->get('sitename');
  309.  
  310.             if (isset($languages[$lang_code]&& $languages[$lang_code]->metadesc)
  311.             {
  312.                 $description $languages[$lang_code]->metadesc;
  313.             }
  314.             else
  315.             {
  316.                 $description $this->get('MetaDesc');
  317.             }
  318.             $rights $this->get('MetaRights');
  319.             $robots $this->get('robots');
  320.  
  321.             // Lets cascade the parameters if we have menu item parameters
  322.             if (is_object($menu))
  323.             {
  324.                 $temp new JRegistry;
  325.                 $temp->loadString($menu->params);
  326.                 $params[$hash]->merge($temp);
  327.                 $title $menu->title;
  328.             }
  329.             else
  330.             {
  331.                 // Get com_menu global settings
  332.                 $temp clone JComponentHelper::getParams('com_menus');
  333.                 $params[$hash]->merge($temp);
  334.  
  335.                 // If supplied, use page title
  336.                 $title $temp->get('page_title'$title);
  337.             }
  338.  
  339.             $params[$hash]->def('page_title'$title);
  340.             $params[$hash]->def('page_description'$description);
  341.             $params[$hash]->def('page_rights'$rights);
  342.             $params[$hash]->def('robots'$robots);
  343.         }
  344.  
  345.         return $params[$hash];
  346.     }
  347.  
  348.     /**
  349.      * Return a reference to the JPathway object.
  350.      *
  351.      * @param   string  $name     The name of the application.
  352.      * @param   array   $options  An optional associative array of configuration settings.
  353.      *
  354.      * @return  JPathway  A JPathway object
  355.      *
  356.      * @since   3.2
  357.      */
  358.     public function getPathway($name 'site'$options array())
  359.     {
  360.         return parent::getPathway($name$options);
  361.     }
  362.  
  363.     /**
  364.      * Return a reference to the JRouter object.
  365.      *
  366.      * @param   string  $name     The name of the application.
  367.      * @param   array   $options  An optional associative array of configuration settings.
  368.      *
  369.      * @return    JRouter 
  370.      *
  371.      * @since    3.2
  372.      */
  373.     public static function getRouter($name 'site'array $options array())
  374.     {
  375.         $config JFactory::getConfig();
  376.         $options['mode'$config->get('sef');
  377.  
  378.         return parent::getRouter($name$options);
  379.     }
  380.  
  381.     /**
  382.      * Gets the name of the current template.
  383.      *
  384.      * @param   boolean  $params  True to return the template parameters
  385.      *
  386.      * @return  string  The name of the template.
  387.      *
  388.      * @since   3.2
  389.      * @throws  InvalidArgumentException
  390.      */
  391.     public function getTemplate($params false)
  392.     {
  393.         if (is_object($this->template))
  394.         {
  395.             if (!file_exists(JPATH_THEMES '/' $this->template->template '/index.php'))
  396.             {
  397.                 throw new InvalidArgumentException(JText::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE'$this->template->template));
  398.             }
  399.  
  400.             if ($params)
  401.             {
  402.                 return $this->template;
  403.             }
  404.  
  405.             return $this->template->template;
  406.         }
  407.  
  408.         // Get the id of the active menu item
  409.         $menu $this->getMenu();
  410.         $item $menu->getActive();
  411.  
  412.         if (!$item)
  413.         {
  414.             $item $menu->getItem($this->input->getInt('Itemid'null));
  415.         }
  416.  
  417.         $id 0;
  418.  
  419.         if (is_object($item))
  420.         {
  421.             // Valid item retrieved
  422.             $id $item->template_style_id;
  423.         }
  424.  
  425.         $tid $this->input->getUint('templateStyle'0);
  426.  
  427.         if (is_numeric($tid&& (int) $tid 0)
  428.         {
  429.             $id = (int) $tid;
  430.         }
  431.  
  432.         $cache JFactory::getCache('com_templates''');
  433.  
  434.         if ($this->_language_filter)
  435.         {
  436.             $tag $this->getLanguage()->getTag();
  437.         }
  438.         else
  439.         {
  440.             $tag '';
  441.         }
  442.  
  443.         if (!$templates $cache->get('templates0' $tag))
  444.         {
  445.             // Load styles
  446.             $db JFactory::getDbo();
  447.             $query $db->getQuery(true)
  448.                 ->select('id, home, template, s.params')
  449.                 ->from('#__template_styles as s')
  450.                 ->where('s.client_id = 0')
  451.                 ->where('e.enabled = 1')
  452.                 ->join('LEFT''#__extensions as e ON e.element=s.template AND e.type=' $db->quote('template'' AND e.client_id=s.client_id');
  453.  
  454.             $db->setQuery($query);
  455.             $templates $db->loadObjectList('id');
  456.  
  457.             foreach ($templates as &$template)
  458.             {
  459.                 $registry new JRegistry;
  460.                 $registry->loadString($template->params);
  461.                 $template->params $registry;
  462.  
  463.                 // Create home element
  464.                 if ($template->home == && !isset($templates[0]|| $this->_language_filter && $template->home == $tag)
  465.                 {
  466.                     $templates[0clone $template;
  467.                 }
  468.             }
  469.  
  470.             $cache->store($templates'templates0' $tag);
  471.         }
  472.  
  473.         if (isset($templates[$id]))
  474.         {
  475.             $template $templates[$id];
  476.         }
  477.         else
  478.         {
  479.             $template $templates[0];
  480.         }
  481.  
  482.         // Allows for overriding the active template from the request
  483.         $template->template $this->input->getCmd('template'$template->template);
  484.  
  485.         // Need to filter the default value as well
  486.         $template->template JFilterInput::getInstance()->clean($template->template'cmd');
  487.  
  488.         // Fallback template
  489.         if (!file_exists(JPATH_THEMES '/' $template->template '/index.php'))
  490.         {
  491.             $this->enqueueMessage(JText::_('JERROR_ALERTNOTEMPLATE')'error');
  492.  
  493.             // Try to find data for 'beez3' template
  494.             $original_tmpl $template->template;
  495.  
  496.             foreach ($templates as $tmpl)
  497.             {
  498.                 if ($tmpl->template == 'beez3')
  499.                 {
  500.                     $template $tmpl;
  501.                     break;
  502.                 }
  503.             }
  504.  
  505.             // Check, the data were found and if template really exists
  506.             if (!file_exists(JPATH_THEMES '/' $template->template '/index.php'))
  507.             {
  508.                 throw new InvalidArgumentException(JText::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE'$original_tmpl));
  509.             }
  510.         }
  511.  
  512.         // Cache the result
  513.         $this->template $template;
  514.  
  515.         if ($params)
  516.         {
  517.             return $template;
  518.         }
  519.  
  520.         return $template->template;
  521.     }
  522.  
  523.     /**
  524.      * Initialise the application.
  525.      *
  526.      * @param   array  $options  An optional associative array of configuration settings.
  527.      *
  528.      * @return  void 
  529.      *
  530.      * @since   3.2
  531.      */
  532.     protected function initialiseApp($options array())
  533.     {
  534.         $user JFactory::getUser();
  535.  
  536.         // If the user is a guest we populate it with the guest user group.
  537.         if ($user->guest)
  538.         {
  539.             $guestUsergroup JComponentHelper::getParams('com_users')->get('guest_usergroup'1);
  540.             $user->groups array($guestUsergroup);
  541.         }
  542.  
  543.         // If a language was specified it has priority, otherwise use user or default language settings
  544.         JPluginHelper::importPlugin('system''languagefilter');
  545.  
  546.         if (empty($options['language']))
  547.         {
  548.             // Detect the specified language
  549.             $lang $this->input->getString('language'null);
  550.  
  551.             // Make sure that the user's language exists
  552.             if ($lang && JLanguage::exists($lang))
  553.             {
  554.                 $options['language'$lang;
  555.             }
  556.         }
  557.  
  558.         if ($this->_language_filter && empty($options['language']))
  559.         {
  560.             // Detect cookie language
  561.             $lang $this->input->cookie->get(md5($this->get('secret''language')null'string');
  562.  
  563.             // Make sure that the user's language exists
  564.             if ($lang && JLanguage::exists($lang))
  565.             {
  566.                 $options['language'$lang;
  567.             }
  568.         }
  569.  
  570.         if (empty($options['language']))
  571.         {
  572.             // Detect user language
  573.             $lang $user->getParam('language');
  574.  
  575.             // Make sure that the user's language exists
  576.             if ($lang && JLanguage::exists($lang))
  577.             {
  578.                 $options['language'$lang;
  579.             }
  580.         }
  581.  
  582.         if ($this->_detect_browser && empty($options['language']))
  583.         {
  584.             // Detect browser language
  585.             $lang JLanguageHelper::detectLanguage();
  586.  
  587.             // Make sure that the user's language exists
  588.             if ($lang && JLanguage::exists($lang))
  589.             {
  590.                 $options['language'$lang;
  591.             }
  592.         }
  593.  
  594.         if (empty($options['language']))
  595.         {
  596.             // Detect default language
  597.             $params JComponentHelper::getParams('com_languages');
  598.             $options['language'$params->get('site'$this->get('language''en-GB'));
  599.         }
  600.  
  601.         // One last check to make sure we have something
  602.         if (!JLanguage::exists($options['language']))
  603.         {
  604.             $lang $this->config->get('language''en-GB');
  605.  
  606.             if (JLanguage::exists($lang))
  607.             {
  608.                 $options['language'$lang;
  609.             }
  610.             else
  611.             {
  612.                 // As a last ditch fail to english
  613.                 $options['language''en-GB';
  614.             }
  615.         }
  616.  
  617.         // Execute the parent initialiseApp method.
  618.         parent::initialiseApp($options);
  619.  
  620.         // Load the language to the API
  621.         $this->loadLanguage();
  622.  
  623.         // Load Library language
  624.         $lang $this->getLanguage();
  625.  
  626.         // Register the language object with JFactory
  627.         JFactory::$language $lang;
  628.  
  629.         /*
  630.          * Try the lib_joomla file in the current language (without allowing the loading of the file in the default language)
  631.          * Fallback to the default language if necessary
  632.          */
  633.         $lang->load('lib_joomla'JPATH_SITEnullfalsetrue)
  634.             || $lang->load('lib_joomla'JPATH_ADMINISTRATORnullfalsetrue);
  635.     }
  636.  
  637.     /**
  638.      * Login authentication function
  639.      *
  640.      * @param   array  $credentials  Array('username' => string, 'password' => string)
  641.      * @param   array  $options      Array('remember' => boolean)
  642.      *
  643.      * @return  boolean  True on success.
  644.      *
  645.      * @since   3.2
  646.      */
  647.     public function login($credentials$options array())
  648.     {
  649.         // Set the application login entry point
  650.         if (!array_key_exists('entry_url'$options))
  651.         {
  652.             $options['entry_url'JUri::base('index.php?option=com_users&task=user.login';
  653.         }
  654.  
  655.         // Set the access control action to check.
  656.         $options['action''core.login.site';
  657.  
  658.         return parent::login($credentials$options);
  659.     }
  660.  
  661.     /**
  662.      * Rendering is the process of pushing the document buffers into the template
  663.      * placeholders, retrieving data from the document and pushing it into
  664.      * the application response buffer.
  665.      *
  666.      * @return  void 
  667.      *
  668.      * @since   3.2
  669.      */
  670.     protected function render()
  671.     {
  672.         switch ($this->document->getType())
  673.         {
  674.             case 'feed':
  675.                 // No special processing for feeds
  676.                 break;
  677.  
  678.             case 'html':
  679.             default:
  680.                 $template $this->getTemplate(true);
  681.                 $file     $this->input->get('tmpl''index');
  682.  
  683.                 if (!$this->get('offline'&& ($file == 'offline'))
  684.                 {
  685.                     $this->set('themeFile''index.php');
  686.                 }
  687.  
  688.                 if ($this->get('offline'&& !JFactory::getUser()->authorise('core.login.offline'))
  689.                 {
  690.                     $this->setUserState('users.login.form.data'array('return' => JUri::getInstance()->toString()));
  691.                     $this->set('themeFile''offline.php');
  692.                     $this->setHeader('Status''503 Service Temporarily Unavailable''true');
  693.                 }
  694.  
  695.                 if (!is_dir(JPATH_THEMES '/' $template->template&& !$this->get('offline'))
  696.                 {
  697.                     $this->set('themeFile''component.php');
  698.                 }
  699.  
  700.                 // Ensure themeFile is set by now
  701.                 if ($this->get('themeFile'== '')
  702.                 {
  703.                     $this->set('themeFile'$file '.php');
  704.                 }
  705.  
  706.                 break;
  707.         }
  708.  
  709.         parent::render();
  710.     }
  711.  
  712.     /**
  713.      * Route the application.
  714.      *
  715.      * Routing is the process of examining the request environment to determine which
  716.      * component should receive the request. The component optional parameters
  717.      * are then set in the request object to be processed when the application is being
  718.      * dispatched.
  719.      *
  720.      * @return  void 
  721.      *
  722.      * @since   3.2
  723.      */
  724.     protected function route()
  725.     {
  726.         // Execute the parent method
  727.         parent::route();
  728.  
  729.         $Itemid $this->input->getInt('Itemid'null);
  730.         $this->authorise($Itemid);
  731.     }
  732.  
  733.     /**
  734.      * Set the current state of the detect browser option.
  735.      *
  736.      * @param   boolean  $state  The new state of the detect browser option
  737.      *
  738.      * @return    boolean     The previous state
  739.      *
  740.      * @since    3.2
  741.      */
  742.     public function setDetectBrowser($state false)
  743.     {
  744.         $old $this->_detect_browser;
  745.         $this->_detect_browser $state;
  746.  
  747.         return $old;
  748.     }
  749.  
  750.     /**
  751.      * Set the current state of the language filter.
  752.      *
  753.      * @param   boolean  $state  The new state of the language filter
  754.      *
  755.      * @return    boolean     The previous state
  756.      *
  757.      * @since    3.2
  758.      */
  759.     public function setLanguageFilter($state false)
  760.     {
  761.         $old $this->_language_filter;
  762.         $this->_language_filter $state;
  763.  
  764.         return $old;
  765.     }
  766.  
  767.     /**
  768.      * Overrides the default template that would be used
  769.      *
  770.      * @param   string  $template     The template name
  771.      * @param   mixed   $styleParams  The template style parameters
  772.      *
  773.      * @return  void 
  774.      *
  775.      * @since   3.2
  776.      */
  777.     public function setTemplate($template$styleParams null)
  778.     {
  779.         if (is_dir(JPATH_THEMES '/' $template))
  780.         {
  781.             $this->template new stdClass;
  782.             $this->template->template $template;
  783.  
  784.             if ($styleParams instanceof JRegistry)
  785.             {
  786.                 $this->template->params $styleParams;
  787.             }
  788.             else
  789.             {
  790.                 $this->template->params new JRegistry($styleParams);
  791.             }
  792.         }
  793.     }
  794. }

Documentation generated on Tue, 19 Nov 2013 15:13:55 +0100 by phpDocumentor 1.4.3