PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/_installation/models/setup.php

https://github.com/rietn/minima
PHP | 415 lines | 231 code | 68 blank | 116 comment | 28 complexity | 647130c71622be602dd43b547300c1a1 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: setup.php 20196 2011-01-09 02:40:25Z ian $
  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. jimport('joomla.application.component.model');
  10. /**
  11. * Setup model for the Joomla Core Installer.
  12. *
  13. * @package Joomla.Installation
  14. * @since 1.6
  15. */
  16. class JInstallationModelSetup extends JModel
  17. {
  18. /**
  19. * Get the current setup options from the session.
  20. *
  21. * @return array
  22. * @since 1.6
  23. */
  24. public function getOptions()
  25. {
  26. $session = JFactory::getSession();
  27. $options = $session->get('setup.options', array());
  28. return $options;
  29. }
  30. /**
  31. * Store the current setup options in the session.
  32. *
  33. * @return array
  34. * @since 1.6
  35. */
  36. public function storeOptions($options)
  37. {
  38. // Get the current setup options from the session.
  39. $session = JFactory::getSession();
  40. $old = $session->get('setup.options', array());
  41. // Merge the new setup options into the current ones and store in the session.
  42. $options = array_merge($old, (array)$options);
  43. $session->set('setup.options', $options);
  44. // If the setup language is set in the options, set it separately in the session.
  45. if (!empty($options['language'])) {
  46. $session->set('setup.language', $options['language']);
  47. }
  48. return $options;
  49. }
  50. /**
  51. * Method to get the link form.
  52. *
  53. * @return mixed JForm object on success, false on failure.
  54. * @since 1.6
  55. */
  56. public function getForm($view = null)
  57. {
  58. // Initialise variables.
  59. $false = false;
  60. if (!$view) {
  61. $view = JRequest::getWord('view', 'language');
  62. }
  63. // Get the form.
  64. jimport('joomla.form.form');
  65. JForm::addFormPath(JPATH_COMPONENT.'/models/forms');
  66. JForm::addFieldPath(JPATH_COMPONENT.'/models/fields');
  67. try {
  68. $form = JForm::getInstance('jform', $view, array('control' => 'jform'));
  69. } catch (Exception $e) {
  70. $this->setError($e->getMessage());
  71. return false;
  72. }
  73. // Check the session for previously entered form data.
  74. $data = (array) $this->getOptions();
  75. // Bind the form data if present.
  76. if (!empty($data)) {
  77. $form->bind($data);
  78. }
  79. return $form;
  80. }
  81. /**
  82. * @since 1.6
  83. */
  84. public function getDboptions()
  85. {
  86. // Initialise variables.
  87. $options = array();
  88. // Create an array of known database connect functions.
  89. $map = array(
  90. 'MySQL' => 'mysql_connect',
  91. 'MySQLi' => 'mysqli_connect',
  92. );
  93. // Iterate over the options, building an array.
  94. $found = false;
  95. foreach ($map as $k => $v) {
  96. // Only list available options.
  97. if (!function_exists($v)) {
  98. continue;
  99. }
  100. // Create the option object.
  101. $option = new stdClass;
  102. $option->text = $k;
  103. $option->value = strtolower($k);
  104. // Select the first available.
  105. if (!$found) {
  106. $option->selected = ' selected="selected"';
  107. $found = true;
  108. }
  109. $options[] = $option;
  110. }
  111. return $options;
  112. }
  113. /**
  114. * Generate a panel of language choices for the user to select their language
  115. *
  116. * @return boolean True if successful
  117. * @since 1.6
  118. */
  119. public function getLanguages()
  120. {
  121. // Initialise variables.
  122. $app = JFactory::getApplication();
  123. // Detect the native language.
  124. jimport('joomla.language.helper');
  125. $native = JLanguageHelper::detectLanguage();
  126. if (empty($native)) {
  127. $native = 'en-GB';
  128. }
  129. // Get a forced language if it exists.
  130. $forced = $app->getLocalise();
  131. if (!empty($forced['language'])) {
  132. $native = $forced['language'];
  133. }
  134. // Get the list of available languages.
  135. $list = JLanguageHelper::createLanguageList($native);
  136. if (!$list || JError::isError($list)) {
  137. $list = array();
  138. }
  139. return $list;
  140. }
  141. /**
  142. * Checks the availability of the parse_ini_file and parse_ini_string functions.
  143. *
  144. * @return boolean
  145. * @since 1.6
  146. */
  147. public function getIniParserAvailability()
  148. {
  149. $disabled_functions = ini_get('disable_functions');
  150. if (!empty($disabled_functions)) {
  151. // Attempt to detect them in the disable_functions black list
  152. $disabled_functions = explode(',', trim($disabled_functions));
  153. $number_of_disabled_functions = count($disabled_functions);
  154. for($i = 0; $i < $number_of_disabled_functions; $i++) {
  155. $disabled_functions[$i] = trim($disabled_functions[$i]);
  156. }
  157. if (phpversion() >= '5.3.0') {
  158. $result = !in_array('parse_ini_string', $disabled_functions);
  159. } else {
  160. $result = !in_array('parse_ini_file', $disabled_functions);
  161. }
  162. } else {
  163. // Attempt to detect their existence; even pure PHP implementation of them will trigger a positive response, though.
  164. if( phpversion() >= '5.3.0' ) {
  165. $result = function_exists('parse_ini_string');
  166. } else {
  167. $result = function_exists('parse_ini_file');
  168. }
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Gets PHP options.
  174. *
  175. * @return array
  176. * @since 1.6
  177. */
  178. public function getPhpOptions()
  179. {
  180. // Initialise variables.
  181. $options = array();
  182. // Check the PHP Version.
  183. $option = new stdClass;
  184. $option->label = JText::_('INSTL_PHP_VERSION').' >= 5.2.4';
  185. $option->state = version_compare(PHP_VERSION, '5.2.4', '>=');
  186. $option->notice = null;
  187. $options[] = $option;
  188. // Check for zlib support.
  189. $option = new stdClass;
  190. $option->label = JText::_('INSTL_ZLIB_COMPRESSION_SUPPORT');
  191. $option->state = extension_loaded('zlib');
  192. $option->notice = null;
  193. $options[] = $option;
  194. // Check for XML support.
  195. $option = new stdClass;
  196. $option->label = JText::_('INSTL_XML_SUPPORT');
  197. $option->state = extension_loaded('xml');
  198. $option->notice = null;
  199. $options[] = $option;
  200. // Check for MySQL support.
  201. $option = new stdClass;
  202. $option->label = JText::_('INSTL_MYSQL_SUPPORT');
  203. $option->state = (function_exists('mysql_connect') || function_exists('mysqli_connect'));
  204. $option->notice = null;
  205. $options[] = $option;
  206. // Check for mbstring options.
  207. if (extension_loaded('mbstring')) {
  208. // Check for default MB language.
  209. $option = new stdClass;
  210. $option->label = JText::_('INSTL_MB_LANGUAGE_IS_DEFAULT');
  211. $option->state = (strtolower(ini_get('mbstring.language')) == 'neutral');
  212. $option->notice = ($option->state) ? null : JText::_('INSTL_NOTICEMBLANGNOTDEFAULT');
  213. $options[] = $option;
  214. // Check for MB function overload.
  215. $option = new stdClass;
  216. $option->label = JText::_('INSTL_MB_STRING_OVERLOAD_OFF');
  217. $option->state = (ini_get('mbstring.func_overload') == 0);
  218. $option->notice = ($option->state) ? null : JText::_('INSTL_NOTICEMBSTRINGOVERLOAD');
  219. $options[] = $option;
  220. }
  221. // Check for a missing native parse_ini_file implementation
  222. $option = new stdClass;
  223. $option->label = JText::_('INSTL_PARSE_INI_FILE_AVAILABLE');
  224. $option->state = $this->getIniParserAvailability();
  225. $option->notice = null;
  226. $options[] = $option;
  227. // Check for missing native json_encode / json_decode support
  228. $option = new stdClass;
  229. $option->label = JText::_('INSTL_JSON_SUPPORT_AVAILABLE');
  230. $option->state = function_exists('json_encode') && function_exists('json_decode');
  231. $option->notice = null;
  232. $options[] = $option;
  233. // Check for configuration file writeable.
  234. $option = new stdClass;
  235. $option->label = 'configuration.php '.JText::_('INSTL_WRITABLE');
  236. $option->state = (is_writable('../configuration.php') || (!file_exists('../configuration.php') && is_writable('../')));
  237. $option->notice = ($option->state) ? null : JText::_('INSTL_NOTICEYOUCANSTILLINSTALL');
  238. $options[] = $option;
  239. return $options;
  240. }
  241. /**
  242. * Checks if all of the mandatory PHP options are met
  243. *
  244. * @return boolean
  245. * @since 1.6
  246. */
  247. public function getPhpOptionsSufficient()
  248. {
  249. $result = true;
  250. $options = $this->getPhpOptions();
  251. foreach($options as $option) {
  252. if (is_null($option->notice)) {
  253. $result = ($result && $option->state);
  254. }
  255. }
  256. return $result;
  257. }
  258. /**
  259. * Gets PHP Settings.
  260. *
  261. * @return array
  262. * @since 1.6
  263. */
  264. public function getPhpSettings()
  265. {
  266. // Initialise variables.
  267. $settings = array();
  268. // Check for safe mode.
  269. $setting = new stdClass;
  270. $setting->label = JText::_('INSTL_SAFE_MODE');
  271. $setting->state = (bool) ini_get('safe_mode');
  272. $setting->recommended = false;
  273. $settings[] = $setting;
  274. // Check for display errors.
  275. $setting = new stdClass;
  276. $setting->label = JText::_('INSTL_DISPLAY_ERRORS');
  277. $setting->state = (bool) ini_get('display_errors');
  278. $setting->recommended = false;
  279. $settings[] = $setting;
  280. // Check for file uploads.
  281. $setting = new stdClass;
  282. $setting->label = JText::_('INSTL_FILE_UPLOADS');
  283. $setting->state = (bool) ini_get('file_uploads');
  284. $setting->recommended = true;
  285. $settings[] = $setting;
  286. // Check for magic quotes.
  287. $setting = new stdClass;
  288. $setting->label = JText::_('INSTL_MAGIC_QUOTES_RUNTIME');
  289. $setting->state = (bool) ini_get('magic_quotes_runtime');
  290. $setting->recommended = false;
  291. $settings[] = $setting;
  292. // Check for register globals.
  293. $setting = new stdClass;
  294. $setting->label = JText::_('INSTL_REGISTER_GLOBALS');
  295. $setting->state = (bool) ini_get('register_globals');
  296. $setting->recommended = false;
  297. $settings[] = $setting;
  298. // Check for output buffering.
  299. $setting = new stdClass;
  300. $setting->label = JText::_('INSTL_OUTPUT_BUFFERING');
  301. $setting->state = (bool) ini_get('output_buffering');
  302. $setting->recommended = false;
  303. $settings[] = $setting;
  304. // Check for session auto-start.
  305. $setting = new stdClass;
  306. $setting->label = JText::_('INSTL_SESSION_AUTO_START');
  307. $setting->state = (bool) ini_get('session.auto_start');
  308. $setting->recommended = false;
  309. $settings[] = $setting;
  310. return $settings;
  311. }
  312. /**
  313. * Method to validate the form data.
  314. *
  315. * @param array $data The form data.
  316. * @param string $view The view.
  317. *
  318. * @return mixed Array of filtered data if valid, false otherwise.
  319. * @since 1.6
  320. */
  321. public function validate($data, $view = null)
  322. {
  323. // Get the form.
  324. $form = $this->getForm($view);
  325. // Check for an error.
  326. if ($form === false) {
  327. return false;
  328. }
  329. // Filter and validate the form data.
  330. $data = $form->filter($data);
  331. $return = $form->validate($data);
  332. // Check for an error.
  333. if (JError::isError($return)) {
  334. $this->setError($return->getMessage());
  335. return false;
  336. }
  337. // Check the validation results.
  338. if ($return === false) {
  339. // Get the validation messages from the form.
  340. foreach ($form->getErrors() as $message) {
  341. $this->setError($message);
  342. }
  343. return false;
  344. }
  345. return $data;
  346. }
  347. }