PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/installation/includes/application.php

https://bitbucket.org/talueses/joomla-cms
PHP | 362 lines | 212 code | 51 blank | 99 comment | 23 complexity | f2da27eac4b730c93cd8e49e88c9a443 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, JSON
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Installation
  5. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. defined('_JEXEC') or die;
  9. /**
  10. * Joomla Application class
  11. *
  12. * Provide many supporting API functions
  13. *
  14. * @package Joomla.Installation
  15. */
  16. class JInstallation extends JApplication
  17. {
  18. /**
  19. * The url of the site
  20. *
  21. * @var string
  22. */
  23. protected $_siteURL = null;
  24. /**
  25. * Class constructor
  26. *
  27. * @param array $config An optional associative array of configuration settings.
  28. * Recognized key values include 'clientId' (this list is not meant to be comprehensive).
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $config = array())
  33. {
  34. $config['clientId'] = 2;
  35. parent::__construct($config);
  36. JError::setErrorHandling(E_ALL, 'Ignore');
  37. $this->_createConfiguration();
  38. // Set the root in the URI based on the application name.
  39. JURI::root(null, str_replace('/'.$this->getName(), '', JURI::base(true)));
  40. }
  41. /**
  42. * Render the application
  43. *
  44. * @return void
  45. */
  46. public function render()
  47. {
  48. $document = JFactory::getDocument();
  49. $config = JFactory::getConfig();
  50. $user = JFactory::getUser();
  51. switch($document->getType())
  52. {
  53. case 'html' :
  54. // Set metadata
  55. $document->setTitle(JText::_('INSTL_PAGE_TITLE'));
  56. break;
  57. default :
  58. break;
  59. }
  60. // Define component path
  61. define('JPATH_COMPONENT', JPATH_BASE);
  62. define('JPATH_COMPONENT_SITE', JPATH_SITE);
  63. define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR);
  64. // Start the output buffer.
  65. ob_start();
  66. // Import the controller.
  67. require_once JPATH_COMPONENT.'/controller.php';
  68. // Execute the task.
  69. $controller = JController::getInstance('JInstallation');
  70. $controller->execute(JRequest::getVar('task'));
  71. $controller->redirect();
  72. // Get output from the buffer and clean it.
  73. $contents = ob_get_contents();
  74. ob_end_clean();
  75. $file = JRequest::getCmd('tmpl', 'index');
  76. $params = array(
  77. 'template' => 'template',
  78. 'file' => $file.'.php',
  79. 'directory' => JPATH_THEMES,
  80. 'params' => '{}'
  81. );
  82. $document->setBuffer($contents, 'installation');
  83. $document->setTitle(JText::_('INSTL_PAGE_TITLE'));
  84. $data = $document->render(false, $params);
  85. JResponse::setBody($data);
  86. if (JFactory::getConfig()->get('debug_lang')) {
  87. $this->debugLanguage();
  88. }
  89. }
  90. /**
  91. * Initialise the application.
  92. *
  93. * @param array $options
  94. *
  95. * @return void
  96. */
  97. public function initialise($options = array())
  98. {
  99. //Get the localisation information provided in the localise.xml file.
  100. $forced = $this->getLocalise();
  101. // Check the request data for the language.
  102. if (empty($options['language'])) {
  103. $requestLang = JRequest::getCmd('lang', null);
  104. if (!is_null($requestLang)) {
  105. $options['language'] = $requestLang;
  106. }
  107. }
  108. // Check the session for the language.
  109. if (empty($options['language'])) {
  110. $sessionLang = JFactory::getSession()->get('setup.language');
  111. if (!is_null($sessionLang)) {
  112. $options['language'] = $sessionLang;
  113. }
  114. }
  115. // This could be a first-time visit - try to determine what the client accepts.
  116. if (empty($options['language'])) {
  117. if (!empty($forced['language'])) {
  118. $options['language'] = $forced['language'];
  119. } else {
  120. $options['language'] = JLanguageHelper::detectLanguage();
  121. if (empty($options['language'])) {
  122. $options['language'] = 'en-GB';
  123. }
  124. }
  125. }
  126. // Give the user English
  127. if (empty($options['language'])) {
  128. $options['language'] = 'en-GB';
  129. }
  130. // Set the language in the class
  131. $conf = JFactory::getConfig();
  132. $conf->set('language', $options['language']);
  133. $conf->set('debug_lang', $forced['debug']);
  134. $conf->set('sampledata', $forced['sampledata']);
  135. }
  136. /**
  137. * @return void
  138. */
  139. public static function debugLanguage()
  140. {
  141. ob_start();
  142. $lang = JFactory::getLanguage();
  143. echo '<h4>Parsing errors in language files</h4>';
  144. $errorfiles = $lang->getErrorFiles();
  145. if (count($errorfiles)) {
  146. echo '<ul>';
  147. foreach ($errorfiles as $file => $error)
  148. {
  149. echo "<li>$error</li>";
  150. }
  151. echo '</ul>';
  152. }
  153. else {
  154. echo '<pre>None</pre>';
  155. }
  156. echo '<h4>Untranslated Strings</h4>';
  157. echo '<pre>';
  158. $orphans = $lang->getOrphans();
  159. if (count($orphans)) {
  160. ksort($orphans, SORT_STRING);
  161. foreach ($orphans as $key => $occurance)
  162. {
  163. $guess = str_replace('_', ' ', $key);
  164. $parts = explode(' ', $guess);
  165. if (count($parts) > 1) {
  166. array_shift($parts);
  167. $guess = implode(' ', $parts);
  168. }
  169. $guess = trim($guess);
  170. $key = trim(strtoupper($key));
  171. $key = preg_replace('#\s+#', '_', $key);
  172. $key = preg_replace('#\W#', '', $key);
  173. // Prepare the text
  174. $guesses[] = $key.'="'.$guess.'"';
  175. }
  176. echo "\n\n# ".$file."\n\n";
  177. echo implode("\n", $guesses);
  178. }
  179. else {
  180. echo 'None';
  181. }
  182. echo '</pre>';
  183. $debug = ob_get_clean();
  184. JResponse::appendBody($debug);
  185. }
  186. /**
  187. * Set configuration values
  188. *
  189. * @param array $vars Array of configuration values
  190. * @param string $namespace The namespace
  191. *
  192. * @return void
  193. */
  194. public function setCfg(array $vars = array(), $namespace = 'config')
  195. {
  196. $this->_registry->loadArray($vars, $namespace);
  197. }
  198. /**
  199. * Create the configuration registry
  200. *
  201. * @return void
  202. */
  203. public function _createConfiguration()
  204. {
  205. // Create the registry with a default namespace of config which is read only
  206. $this->_registry = new JRegistry('config');
  207. }
  208. /**
  209. * Get the template
  210. *
  211. * @return string The template name
  212. */
  213. public function getTemplate($params = false)
  214. {
  215. if ((bool) $params) {
  216. $template = new stdClass();
  217. $template->template = 'template';
  218. $template->params = new JRegistry;
  219. return $template;
  220. }
  221. return 'template';
  222. }
  223. /**
  224. * Create the user session
  225. *
  226. * @param string $name The sessions name
  227. *
  228. * @return object JSession
  229. */
  230. public function & _createSession($name)
  231. {
  232. $options = array();
  233. $options['name'] = $name;
  234. $session = JFactory::getSession($options);
  235. if (!is_a($session->get('registry'), 'JRegistry')) {
  236. // Registry has been corrupted somehow
  237. $session->set('registry', new JRegistry('session'));
  238. }
  239. return $session;
  240. }
  241. /**
  242. * Returns the language code and help url set in the localise.xml file.
  243. * Used for forcing a particular language in localised releases.
  244. *
  245. * @return bool|array False on failure, array on success.
  246. */
  247. public function getLocalise()
  248. {
  249. $xml = JFactory::getXML(JPATH_SITE . '/installation/localise.xml');
  250. if (!$xml) {
  251. return false;
  252. }
  253. // Check that it's a localise file
  254. if ($xml->getName() != 'localise') {
  255. return false;
  256. }
  257. $ret = array();
  258. $ret['language'] = (string)$xml->forceLang;
  259. $ret['helpurl'] = (string)$xml->helpurl;
  260. $ret['debug'] = (string)$xml->debug;
  261. $ret['sampledata'] = (string)$xml->sampledata;
  262. return $ret;
  263. }
  264. /**
  265. * Returns the installed language files in the administrative and
  266. * front-end area.
  267. *
  268. * @param boolean $db
  269. *
  270. * @return array Array with installed language packs in admin and site area
  271. */
  272. public function getLocaliseAdmin($db=false)
  273. {
  274. jimport('joomla.filesystem.folder');
  275. // Read the files in the admin area
  276. $path = JLanguage::getLanguagePath(JPATH_SITE . '/administrator');
  277. $langfiles['admin'] = JFolder::folders($path);
  278. // Read the files in the site area
  279. $path = JLanguage::getLanguagePath(JPATH_SITE);
  280. $langfiles['site'] = JFolder::folders($path);
  281. if ($db) {
  282. $langfiles_disk = $langfiles;
  283. $langfiles = array();
  284. $langfiles['admin'] = array();
  285. $langfiles['site'] = array();
  286. $query = $db->getQuery(true);
  287. $query->select('element,client_id');
  288. $query->from('#__extensions');
  289. $query->where('type = '.$db->quote('language'));
  290. $db->setQuery($query);
  291. $langs = $db->loadObjectList();
  292. foreach ($langs as $lang)
  293. {
  294. switch($lang->client_id)
  295. {
  296. case 0: // site
  297. if (in_array($lang->element, $langfiles_disk['site'])) {
  298. $langfiles['site'][] = $lang->element;
  299. }
  300. break;
  301. case 1: // administrator
  302. if (in_array($lang->element, $langfiles_disk['admin'])) {
  303. $langfiles['admin'][] = $lang->element;
  304. }
  305. break;
  306. }
  307. }
  308. }
  309. return $langfiles;
  310. }
  311. }