Source for file web.php

Documentation is available at web.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Installation
  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! Installation Application class
  14.  *
  15.  * @package     Joomla.Installation
  16.  * @subpackage  Application
  17.  * @since       3.1
  18.  */
  19. {
  20.     /**
  21.      * Class constructor.
  22.      *
  23.      * @since   3.1
  24.      */
  25.     public function __construct()
  26.     {
  27.         // Run the parent constructor
  28.         parent::__construct();
  29.  
  30.         // Load and set the dispatcher
  31.         $this->loadDispatcher();
  32.  
  33.         // Enable sessions by default.
  34.         if (is_null($this->config->get('session')))
  35.         {
  36.             $this->config->set('session'true);
  37.         }
  38.  
  39.         // Set the session default name.
  40.         if (is_null($this->config->get('session_name')))
  41.         {
  42.             $this->config->set('session_name''installation');
  43.         }
  44.  
  45.         // Create the session if a session name is passed.
  46.         if ($this->config->get('session'!== false)
  47.         {
  48.             $this->loadSession();
  49.  
  50.             // Register the session with JFactory
  51.             JFactory::$session $this->getSession();
  52.         }
  53.  
  54.         // Store the debug value to config based on the JDEBUG flag
  55.         $this->config->set('debug'JDEBUG);
  56.  
  57.         // Register the config to JFactory
  58.         JFactory::$config $this->config;
  59.  
  60.         // Register the application to JFactory
  61.         JFactory::$application $this;
  62.  
  63.         // Register the application name
  64.         $this->_name = 'installation';
  65.  
  66.         // Register the client ID
  67.         $this->_clientId = 2;
  68.  
  69.         // Set the root in the URI one level up.
  70.         $parts explode('/'JUri::base(true));
  71.         array_pop($parts);
  72.         JUri::root(nullimplode('/'$parts));
  73.     }
  74.  
  75.     /**
  76.      * Method to display errors in language parsing
  77.      *
  78.      * @return  string  Language debug output
  79.      *
  80.      * @since   3.1
  81.      */
  82.     public function debugLanguage()
  83.     {
  84.         $lang   JFactory::getLanguage();
  85.         $output '<h4>' JText::_('JDEBUG_LANGUAGE_FILES_IN_ERROR''</h4>';
  86.  
  87.         $errorfiles $lang->getErrorFiles();
  88.  
  89.         if (count($errorfiles))
  90.         {
  91.             $output .= '<ul>';
  92.  
  93.             foreach ($errorfiles as $error)
  94.             {
  95.                 $output .= "<li>$error</li>";
  96.             }
  97.  
  98.             $output .= '</ul>';
  99.         }
  100.         else
  101.         {
  102.             $output .= '<pre>' JText::_('JNONE''</pre>';
  103.         }
  104.  
  105.         $output .= '<h4>' JText::_('JDEBUG_LANGUAGE_UNTRANSLATED_STRING''</h4>';
  106.         $output .= '<pre>';
  107.         $orphans $lang->getOrphans();
  108.  
  109.         if (count($orphans))
  110.         {
  111.             ksort($orphansSORT_STRING);
  112.  
  113.             $guesses array();
  114.             foreach ($orphans as $key => $occurance)
  115.             {
  116.                 $guess str_replace('_'' '$key);
  117.  
  118.                 $parts explode(' '$guess);
  119.                 if (count($parts1)
  120.                 {
  121.                     array_shift($parts);
  122.                     $guess implode(' '$parts);
  123.                 }
  124.  
  125.                 $guess trim($guess);
  126.  
  127.                 $key trim(strtoupper($key));
  128.                 $key preg_replace('#\s+#''_'$key);
  129.                 $key preg_replace('#\W#'''$key);
  130.  
  131.                 // Prepare the text
  132.                 $guesses[$key '="' $guess '"';
  133.             }
  134.  
  135.             $output .= implode("\n"$guesses);
  136.         }
  137.         else
  138.         {
  139.             $output .= '<pre>' JText::_('JNONE''</pre>';
  140.         }
  141.  
  142.         $output .= '</pre>';
  143.  
  144.         return $output;
  145.     }
  146.  
  147.     /**
  148.      * Dispatch the application
  149.      *
  150.      * @return  void 
  151.      *
  152.      * @since   3.1
  153.      */
  154.     public function dispatch()
  155.     {
  156.         try
  157.         {
  158.             // Load the document to the API
  159.             $this->loadDocument();
  160.  
  161.             // Set up the params
  162.             $document $this->getDocument();
  163.  
  164.             // Register the document object with JFactory
  165.             JFactory::$document $document;
  166.  
  167.             if ($document->getType(== 'html')
  168.             {
  169.                 // Set metadata
  170.                 $document->setTitle(JText::_('INSTL_PAGE_TITLE'));
  171.             }
  172.  
  173.             // Define component path
  174.             define('JPATH_COMPONENT'JPATH_BASE);
  175.             define('JPATH_COMPONENT_SITE'JPATH_SITE);
  176.             define('JPATH_COMPONENT_ADMINISTRATOR'JPATH_ADMINISTRATOR);
  177.  
  178.             // Execute the task.
  179.             try
  180.             {
  181.                 $controller $this->fetchController($this->input->getCmd('task'));
  182.                 $contents   $controller->execute();
  183.             }
  184.             catch (RuntimeException $e)
  185.             {
  186.                 echo $e->getMessage();
  187.                 $this->close($e->getCode());
  188.             }
  189.  
  190.             // If debug language is set, append its output to the contents
  191.             if ($this->config->get('debug_lang'))
  192.             {
  193.                 $contents .= $this->debugLanguage();
  194.             }
  195.  
  196.             $document->setBuffer($contents'component');
  197.             $document->setTitle(JText::_('INSTL_PAGE_TITLE'));
  198.         }
  199.  
  200.         // Mop up any uncaught exceptions.
  201.         catch (Exception $e)
  202.         {
  203.             echo $e->getMessage();
  204.             $this->close($e->getCode());
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Method to run the Web application routines.
  210.      *
  211.      * @return  void 
  212.      *
  213.      * @since   3.1
  214.      */
  215.     protected function doExecute()
  216.     {
  217.         // Initialise the application
  218.         $this->initialiseApp();
  219.  
  220.         // Dispatch the application
  221.         $this->dispatch();
  222.     }
  223.  
  224.     /**
  225.      * Method to load a PHP configuration class file based on convention and return the instantiated data object.  You
  226.      * will extend this method in child classes to provide configuration data from whatever data source is relevant
  227.      * for your specific application.
  228.      *
  229.      * @param   string  $file   The path and filename of the configuration file. If not provided, configuration.php
  230.      *                           in JPATH_BASE will be used.
  231.      * @param   string  $class  The class name to instantiate.
  232.      *
  233.      * @return  mixed   Either an array or object to be loaded into the configuration object.
  234.      *
  235.      * @since   11.3
  236.      * @throws  RuntimeException
  237.      */
  238.     protected function fetchConfigurationData($file ''$class 'JConfig')
  239.     {
  240.         return array();
  241.     }
  242.  
  243.     /**
  244.      * Method to get a controller object.
  245.      *
  246.      * @param   string  $task  The task being executed
  247.      *
  248.      * @return  JController 
  249.      *
  250.      * @since   3.1
  251.      * @throws  RuntimeException
  252.      */
  253.     protected function fetchController($task)
  254.     {
  255.         if (is_null($task))
  256.         {
  257.             $task 'default';
  258.         }
  259.  
  260.         // Set the controller class name based on the task
  261.         $class 'InstallationController' ucfirst($task);
  262.  
  263.         // If the requested controller exists let's use it.
  264.         if (class_exists($class))
  265.         {
  266.             return new $class;
  267.         }
  268.  
  269.         // Nothing found. Panic.
  270.         throw new RuntimeException('Class ' $class ' not found');
  271.     }
  272.  
  273.     /**
  274.      * Returns the language code and help url set in the localise.xml file.
  275.      * Used for forcing a particular language in localised releases.
  276.      *
  277.      * @return  mixed  False on failure, array on success.
  278.      *
  279.      * @since   3.1
  280.      */
  281.     public function getLocalise()
  282.     {
  283.         $xml simplexml_load_file(JPATH_INSTALLATION '/localise.xml');
  284.  
  285.         if (!$xml)
  286.         {
  287.             return false;
  288.         }
  289.  
  290.         // Check that it's a localise file
  291.         if ($xml->getName(!= 'localise')
  292.         {
  293.             return false;
  294.         }
  295.  
  296.         $ret array();
  297.  
  298.         $ret['language'= (string) $xml->forceLang;
  299.         $ret['helpurl'= (string) $xml->helpurl;
  300.         $ret['debug'= (string) $xml->debug;
  301.         $ret['sampledata'= (string) $xml->sampledata;
  302.  
  303.         return $ret;
  304.     }
  305.  
  306.     /**
  307.      * Returns the installed language files in the administrative and
  308.      * front-end area.
  309.      *
  310.      * @param   mixed  $db  JDatabaseDriver instance
  311.      *
  312.      * @return  array  Array with installed language packs in admin and site area
  313.      *
  314.      * @since   3.1
  315.      */
  316.     public function getLocaliseAdmin($db false)
  317.     {
  318.         // Read the files in the admin area
  319.         $path JLanguage::getLanguagePath(JPATH_ADMINISTRATOR);
  320.         $langfiles['admin'JFolder::folders($path);
  321.  
  322.         // Read the files in the site area
  323.         $path JLanguage::getLanguagePath(JPATH_SITE);
  324.         $langfiles['site'JFolder::folders($path);
  325.  
  326.         if ($db)
  327.         {
  328.             $langfiles_disk $langfiles;
  329.             $langfiles array();
  330.             $langfiles['admin'array();
  331.             $langfiles['site'array();
  332.             $query $db->getQuery(true)
  333.                 ->select($db->quoteName(array('element','client_id')))
  334.                 ->from($db->quoteName('#__extensions'))
  335.                 ->where($db->quoteName('type'' = ' $db->quote('language'));
  336.             $db->setQuery($query);
  337.             $langs $db->loadObjectList();
  338.  
  339.             foreach ($langs as $lang)
  340.             {
  341.                 switch ($lang->client_id)
  342.                 {
  343.                     // Site
  344.                     case 0:
  345.                         if (in_array($lang->element$langfiles_disk['site']))
  346.                         {
  347.                             $langfiles['site'][$lang->element;
  348.                         }
  349.  
  350.                         break;
  351.  
  352.                     // Administrator
  353.                     case 1:
  354.                         if (in_array($lang->element$langfiles_disk['admin']))
  355.                         {
  356.                             $langfiles['admin'][$lang->element;
  357.                         }
  358.  
  359.                         break;
  360.                 }
  361.             }
  362.         }
  363.  
  364.         return $langfiles;
  365.     }
  366.  
  367.     /**
  368.      * Gets the name of the current template.
  369.      *
  370.      * @param   boolean  $params  True to return the template parameters
  371.      *
  372.      * @return  string  The name of the template.
  373.      *
  374.      * @since   3.1
  375.      */
  376.     public function getTemplate($params false)
  377.     {
  378.         if ($params)
  379.         {
  380.             $template new stdClass;
  381.             $template->template 'template';
  382.             $template->params new JRegistry;
  383.             return $template;
  384.         }
  385.  
  386.         return 'template';
  387.     }
  388.  
  389.     /**
  390.      * Initialise the application.
  391.      *
  392.      * @param   array  $options  An optional associative array of configuration settings.
  393.      *
  394.      * @return  void 
  395.      *
  396.      * @since   3.1
  397.      */
  398.     protected function initialiseApp($options array())
  399.     {
  400.         // Get the localisation information provided in the localise.xml file.
  401.         $forced $this->getLocalise();
  402.  
  403.         // Check the request data for the language.
  404.         if (empty($options['language']))
  405.         {
  406.             $requestLang $this->input->getCmd('lang'null);
  407.             if (!is_null($requestLang))
  408.             {
  409.                 $options['language'$requestLang;
  410.             }
  411.         }
  412.  
  413.         // Check the session for the language.
  414.         if (empty($options['language']))
  415.         {
  416.             $sessionOptions $this->getSession()->get('setup.options');
  417.  
  418.             if (isset($sessionOptions['language']))
  419.             {
  420.                 $options['language'$sessionOptions['language'];
  421.             }
  422.         }
  423.  
  424.         // This could be a first-time visit - try to determine what the client accepts.
  425.         if (empty($options['language']))
  426.         {
  427.             if (!empty($forced['language']))
  428.             {
  429.                 $options['language'$forced['language'];
  430.             }
  431.             else
  432.             {
  433.                 $options['language'JLanguageHelper::detectLanguage();
  434.  
  435.                 if (empty($options['language']))
  436.                 {
  437.                     $options['language''en-GB';
  438.                 }
  439.             }
  440.         }
  441.  
  442.         // Give the user English
  443.         if (empty($options['language']))
  444.         {
  445.             $options['language''en-GB';
  446.         }
  447.  
  448.         // Set the language in the class
  449.         $this->config->set('language'$options['language']);
  450.         $this->config->set('debug_lang'$forced['debug']);
  451.         $this->config->set('sampledata'$forced['sampledata']);
  452.     }
  453.  
  454.     /**
  455.      * Allows the application to load a custom or default document.
  456.      *
  457.      * The logic and options for creating this object are adequately generic for default cases
  458.      * but for many applications it will make sense to override this method and create a document,
  459.      * if required, based on more specific needs.
  460.      *
  461.      * @param   JDocument  $document  An optional document object. If omitted, the factory document is created.
  462.      *
  463.      * @return  InstallationApplicationWeb This method is chainable.
  464.      *
  465.      * @since   3.2
  466.      */
  467.     public function loadDocument(JDocument $document null)
  468.     {
  469.         if ($document === null)
  470.         {
  471.             $lang JFactory::getLanguage();
  472.  
  473.             $type $this->input->get('format''html''word');
  474.  
  475.             $attributes array(
  476.                 'charset' => 'utf-8',
  477.                 'lineend' => 'unix',
  478.                 'tab' => '  ',
  479.                 'language' => $lang->getTag(),
  480.                 'direction' => $lang->isRTL('rtl' 'ltr'
  481.             );
  482.  
  483.             $document JDocument::getInstance($type$attributes);
  484.  
  485.             // Register the instance to JFactory
  486.             JFactory::$document $document;
  487.         }
  488.  
  489.         $this->document = $document;
  490.  
  491.         return $this;
  492.     }
  493.  
  494.     /**
  495.      * Allows the application to load a custom or default session.
  496.      *
  497.      * The logic and options for creating this object are adequately generic for default cases
  498.      * but for many applications it will make sense to override this method and create a session,
  499.      * if required, based on more specific needs.
  500.      *
  501.      * @param   JSession  $session  An optional session object. If omitted, the session is created.
  502.      *
  503.      * @return  InstallationApplicationWeb  This method is chainable.
  504.      *
  505.      * @since   3.1
  506.      */
  507.     public function loadSession(JSession $session null)
  508.     {
  509.         // Generate a session name.
  510.         $name md5($this->get('secret'$this->get('session_name'get_class($this)));
  511.  
  512.         // Calculate the session lifetime.
  513.         $lifetime (($this->get('lifetime')) $this->get('lifetime'60 900);
  514.  
  515.         // Get the session handler from the configuration.
  516.         $handler $this->get('session_handler''none');
  517.  
  518.         // Initialize the options for JSession.
  519.         $options array(
  520.             'name' => $name,
  521.             'expire' => $lifetime,
  522.             'force_ssl' => $this->get('force_ssl')
  523.         );
  524.  
  525.         // Instantiate the session object.
  526.         $session JSession::getInstance($handler$options);
  527.         $session->initialise($this->input$this->dispatcher);
  528.  
  529.         if ($session->getState(== 'expired')
  530.         {
  531.             $session->restart();
  532.         }
  533.         else
  534.         {
  535.             $session->start();
  536.         }
  537.  
  538.         if (!$session->get('registry'instanceof JRegistry)
  539.         {
  540.             // Registry has been corrupted somehow
  541.             $session->set('registry'new JRegistry('session'));
  542.         }
  543.  
  544.         // Set the session object.
  545.         $this->session = $session;
  546.  
  547.         return $this;
  548.     }
  549.  
  550.     /**
  551.      * Rendering is the process of pushing the document buffers into the template
  552.      * placeholders, retrieving data from the document and pushing it into
  553.      * the application response buffer.
  554.      *
  555.      * @return  void 
  556.      *
  557.      * @since   3.1
  558.      */
  559.     public function render()
  560.     {
  561.         $file $this->input->getCmd('tmpl''index');
  562.  
  563.         $options array(
  564.             'template' => 'template',
  565.             'file' => $file '.php',
  566.             'directory' => JPATH_THEMES,
  567.             'params' => '{}'
  568.         );
  569.  
  570.         // Parse the document.
  571.         $this->document->parse($options);
  572.  
  573.         // Render the document.
  574.         $data $this->document->render($this->get('cache_enabled')$options);
  575.  
  576.         // Set the application output data.
  577.         $this->setBody($data);
  578.     }
  579.  
  580.     /**
  581.      * Method to send a JSON response. The data parameter
  582.      * can be a Exception object for when an error has occurred or
  583.      * a stdClass for a good response.
  584.      *
  585.      * @param   mixed  $response  stdClass on success, Exception on failure.
  586.      *
  587.      * @return  void 
  588.      *
  589.      * @since   3.1
  590.      */
  591.     public function sendJsonResponse($response)
  592.     {
  593.         // Check if we need to send an error code.
  594.         if ($response instanceof Exception)
  595.         {
  596.             // Send the appropriate error code response.
  597.             $this->setHeader('status'$response->getCode());
  598.             $this->setHeader('Content-Type''application/json; charset=utf-8');
  599.             $this->sendHeaders();
  600.         }
  601.  
  602.         // Send the JSON response.
  603.         echo json_encode(new InstallationResponseJson($response));
  604.  
  605.         // Close the application.
  606.         $this->close();
  607.     }
  608.  
  609.     /**
  610.      * Set configuration values
  611.      *
  612.      * @param   array   $vars       Array of configuration values
  613.      * @param   string  $namespace  The namespace
  614.      *
  615.      * @return  void 
  616.      *
  617.      * @since   3.1
  618.      */
  619.     public function setCfg(array $vars array()$namespace 'config')
  620.     {
  621.         $this->config->loadArray($vars$namespace);
  622.     }
  623. }

Documentation generated on Tue, 19 Nov 2013 15:18:23 +0100 by phpDocumentor 1.4.3