PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/install.php

https://gitlab.com/endomorphosis/fusenews
PHP | 592 lines | 407 code | 74 blank | 111 comment | 64 complexity | c4f21c4f00da8cfcd17a5b15bf38b278 MD5 | raw file
  1. <?php
  2. // $Id: install.php,v 1.34.2.5 2008/07/09 21:48:41 drumm Exp $
  3. require_once './includes/install.inc';
  4. /**
  5. * The Drupal installation happens in a series of steps. We begin by verifying
  6. * that the current environment meets our minimum requirements. We then go
  7. * on to verify that settings.php is properly configured. From there we
  8. * connect to the configured database and verify that it meets our minimum
  9. * requirements. Finally we can allow the user to select an installation
  10. * profile and complete the installation process.
  11. *
  12. * @param $phase
  13. * The installation phase we should proceed to.
  14. */
  15. function install_main() {
  16. require_once './includes/bootstrap.inc';
  17. drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
  18. // This must go after drupal_bootstrap(), which unsets globals!
  19. global $profile, $install_locale;
  20. require_once './modules/system/system.install';
  21. require_once './includes/file.inc';
  22. // Ensure correct page headers are sent (e.g. caching)
  23. drupal_page_header();
  24. // Check existing settings.php.
  25. $verify = install_verify_settings();
  26. // Drupal may already be installed.
  27. if ($verify) {
  28. // Establish a connection to the database.
  29. require_once './includes/database.inc';
  30. db_set_active();
  31. // Check if Drupal is installed.
  32. if (install_verify_drupal()) {
  33. install_already_done_error();
  34. }
  35. }
  36. // Load module basics (needed for hook invokes).
  37. include_once './includes/module.inc';
  38. $module_list['system']['filename'] = 'modules/system/system.module';
  39. $module_list['filter']['filename'] = 'modules/filter/filter.module';
  40. module_list(TRUE, FALSE, FALSE, $module_list);
  41. drupal_load('module', 'system');
  42. drupal_load('module', 'filter');
  43. // Decide which profile to use.
  44. if (!empty($_GET['profile'])) {
  45. $profile = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['profile']);
  46. }
  47. elseif ($profile = install_select_profile()) {
  48. install_goto("install.php?profile=$profile");
  49. }
  50. else {
  51. install_no_profile_error();
  52. }
  53. // Locale selection
  54. if (!empty($_GET['locale'])) {
  55. $install_locale = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['locale']);
  56. }
  57. elseif (($install_locale = install_select_locale($profile)) !== FALSE) {
  58. install_goto("install.php?profile=$profile&locale=$install_locale");
  59. }
  60. // Load the profile.
  61. require_once "./profiles/$profile/$profile.profile";
  62. // Check the installation requirements for Drupal and this profile.
  63. install_check_requirements($profile);
  64. // Change the settings.php information if verification failed earlier.
  65. // Note: will trigger a redirect if database credentials change.
  66. if (!$verify) {
  67. install_change_settings($profile, $install_locale);
  68. }
  69. // Verify existence of all required modules.
  70. $modules = drupal_verify_profile($profile, $install_locale);
  71. if (!$modules) {
  72. install_missing_modules_error($profile);
  73. }
  74. // Perform actual installation defined in the profile.
  75. drupal_install_profile($profile, $modules);
  76. // Warn about settings.php permissions risk
  77. $settings_file = './'. conf_path() .'/settings.php';
  78. if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE)) {
  79. drupal_set_message(st('All necessary changes to %file have been made, so you should now remove write permissions to this file. Failure to remove write permissions to this file is a security risk.', array('%file' => $settings_file)), 'error');
  80. }
  81. else {
  82. drupal_set_message(st('All necessary changes to %file have been made. It has been set to read-only for security.', array('%file' => $settings_file)));
  83. }
  84. // Show end page.
  85. install_complete($profile);
  86. }
  87. /**
  88. * Verify if Drupal is installed.
  89. */
  90. function install_verify_drupal() {
  91. $result = @db_query("SELECT name FROM {system} WHERE name = 'system'");
  92. return $result && db_result($result) == 'system';
  93. }
  94. /**
  95. * Verify existing settings.php
  96. */
  97. function install_verify_settings() {
  98. global $db_prefix, $db_type, $db_url;
  99. // Verify existing settings (if any).
  100. if ($_SERVER['REQUEST_METHOD'] == 'GET' && $db_url != 'mysql://username:password@localhost/databasename') {
  101. // We need this because we want to run form_get_errors.
  102. include_once './includes/form.inc';
  103. $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  104. $db_user = urldecode($url['user']);
  105. $db_pass = urldecode($url['pass']);
  106. $db_host = urldecode($url['host']);
  107. $db_port = isset($url['port']) ? urldecode($url['port']) : '';
  108. $db_path = ltrim(urldecode($url['path']), '/');
  109. $settings_file = './'. conf_path() .'/settings.php';
  110. _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file);
  111. if (!form_get_errors()) {
  112. return TRUE;
  113. }
  114. }
  115. return FALSE;
  116. }
  117. /**
  118. * Configure and rewrite settings.php.
  119. */
  120. function install_change_settings($profile = 'default', $install_locale = '') {
  121. global $db_url, $db_type, $db_prefix;
  122. $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  123. $db_user = urldecode($url['user']);
  124. $db_pass = urldecode($url['pass']);
  125. $db_host = urldecode($url['host']);
  126. $db_port = isset($url['port']) ? urldecode($url['port']) : '';
  127. $db_path = ltrim(urldecode($url['path']), '/');
  128. $settings_file = './'. conf_path() .'/settings.php';
  129. // We always need this because we want to run form_get_errors.
  130. include_once './includes/form.inc';
  131. drupal_maintenance_theme();
  132. // Don't fill in placeholders
  133. if ($db_url == 'mysql://username:password@localhost/databasename') {
  134. $db_user = $db_pass = $db_path = '';
  135. }
  136. elseif (!empty($db_url)) {
  137. // Do not install over a configured settings.php.
  138. install_already_done_error();
  139. }
  140. // The existing database settings are not working, so we need write access
  141. // to settings.php to change them.
  142. if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_WRITABLE)) {
  143. drupal_set_message(st('The @drupal installer requires write permissions to %file during the installation process.', array('@drupal' => drupal_install_profile_name(), '%file' => $settings_file)), 'error');
  144. drupal_set_title(st('Drupal database setup'));
  145. print theme('install_page', '');
  146. exit;
  147. }
  148. $output = drupal_get_form('install_settings_form', $profile, $install_locale, $settings_file, $db_url, $db_type, $db_prefix, $db_user, $db_pass, $db_host, $db_port, $db_path);
  149. drupal_set_title(st('Database configuration'));
  150. print theme('install_page', $output);
  151. exit;
  152. }
  153. /**
  154. * Form API array definition for install_settings.
  155. */
  156. function install_settings_form($profile, $install_locale, $settings_file, $db_url, $db_type, $db_prefix, $db_user, $db_pass, $db_host, $db_port, $db_path) {
  157. $db_types = drupal_detect_database_types();
  158. if (count($db_types) == 0) {
  159. $form['no_db_types'] = array(
  160. '#value' => st('Your web server does not appear to support any common database types. Check with your hosting provider to see if they offer any databases that <a href="@drupal-databases">Drupal supports</a>.', array('@drupal-databases' => 'http://drupal.org/node/270#database')),
  161. );
  162. }
  163. else {
  164. $form['basic_options'] = array(
  165. '#type' => 'fieldset',
  166. '#title' => st('Basic options'),
  167. '#description' => '<p>'. st('To set up your @drupal database, enter the following information.', array('@drupal' => drupal_install_profile_name())) .'</p>',
  168. );
  169. if (count($db_types) > 1) {
  170. // Database type
  171. $db_types = drupal_detect_database_types();
  172. $form['basic_options']['db_type'] = array(
  173. '#type' => 'radios',
  174. '#title' => st('Database type'),
  175. '#required' => TRUE,
  176. '#options' => $db_types,
  177. '#default_value' => ($db_type ? $db_type : current($db_types)),
  178. '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_name())),
  179. );
  180. $db_path_description = st('The name of the database your @drupal data will be stored in. It must exist on your server before @drupal can be installed.', array('@drupal' => drupal_install_profile_name()));
  181. }
  182. else {
  183. if (count($db_types) == 1) {
  184. $db_types = array_values($db_types);
  185. $form['basic_options']['db_type'] = array(
  186. '#type' => 'hidden',
  187. '#value' => $db_types[0],
  188. );
  189. $db_path_description = st('The name of the %db_type database your @drupal data will be stored in. It must exist on your server before @drupal can be installed.', array('%db_type' => $db_types[0], '@drupal' => drupal_install_profile_name()));
  190. }
  191. }
  192. // Database name
  193. $form['basic_options']['db_path'] = array(
  194. '#type' => 'textfield',
  195. '#title' => st('Database name'),
  196. '#default_value' => $db_path,
  197. '#size' => 45,
  198. '#maxlength' => 45,
  199. '#required' => TRUE,
  200. '#description' => $db_path_description
  201. );
  202. // Database username
  203. $form['basic_options']['db_user'] = array(
  204. '#type' => 'textfield',
  205. '#title' => st('Database username'),
  206. '#default_value' => $db_user,
  207. '#size' => 45,
  208. '#maxlength' => 45,
  209. '#required' => TRUE,
  210. );
  211. // Database username
  212. $form['basic_options']['db_pass'] = array(
  213. '#type' => 'password',
  214. '#title' => st('Database password'),
  215. '#default_value' => $db_pass,
  216. '#size' => 45,
  217. '#maxlength' => 45,
  218. );
  219. $form['advanced_options'] = array(
  220. '#type' => 'fieldset',
  221. '#title' => st('Advanced options'),
  222. '#collapsible' => TRUE,
  223. '#collapsed' => TRUE,
  224. '#description' => st("These options are only necessary for some sites. If you're not sure what you should enter here, leave the default settings or check with your hosting provider.")
  225. );
  226. // Database host
  227. $form['advanced_options']['db_host'] = array(
  228. '#type' => 'textfield',
  229. '#title' => st('Database host'),
  230. '#default_value' => $db_host,
  231. '#size' => 45,
  232. '#maxlength' => 45,
  233. '#required' => TRUE,
  234. '#description' => st('If your database is located on a different server, change this.'),
  235. );
  236. // Database port
  237. $form['advanced_options']['db_port'] = array(
  238. '#type' => 'textfield',
  239. '#title' => st('Database port'),
  240. '#default_value' => $db_port,
  241. '#size' => 45,
  242. '#maxlength' => 45,
  243. '#description' => st('If your database server is listening to a non-standard port, enter its number.'),
  244. );
  245. // Table prefix
  246. $form['advanced_options']['db_prefix'] = array(
  247. '#type' => 'textfield',
  248. '#title' => st('Table prefix'),
  249. '#default_value' => $db_prefix,
  250. '#size' => 45,
  251. '#maxlength' => 45,
  252. '#description' => st('If more than one @drupal web site will be sharing this database, enter a table prefix for your @drupal site here.', array('@drupal' => drupal_install_profile_name())),
  253. );
  254. $form['save'] = array(
  255. '#type' => 'submit',
  256. '#value' => st('Save configuration'),
  257. );
  258. $form['errors'] = array();
  259. $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
  260. $form['_db_url'] = array('#type' => 'value');
  261. $form['#action'] = "install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : '');
  262. $form['#redirect'] = NULL;
  263. }
  264. return $form;
  265. }
  266. /**
  267. * Form API validate for install_settings form.
  268. */
  269. function install_settings_form_validate($form_id, $form_values, $form) {
  270. global $db_url;
  271. _install_settings_form_validate($form_values['db_prefix'], $form_values['db_type'], $form_values['db_user'], $form_values['db_pass'], $form_values['db_host'], $form_values['db_port'], $form_values['db_path'], $form_values['settings_file'], $form);
  272. }
  273. /**
  274. * Helper function for install_settings_validate.
  275. */
  276. function _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file, $form = NULL) {
  277. global $db_url;
  278. // Check for default username/password
  279. if ($db_user == 'username' && $db_pass == 'password') {
  280. form_set_error('db_user', st('You have configured @drupal to use the default username and password. This is not allowed for security reasons.', array('@drupal' => drupal_install_profile_name())));
  281. }
  282. // Verify the table prefix
  283. if (!empty($db_prefix) && is_string($db_prefix) && !preg_match('/^[A-Za-z0-9_]+$/', $db_prefix)) {
  284. form_set_error('db_prefix', st('The database table prefix you have entered, %db_prefix, is invalid. The table prefix can only contain alphanumeric characters or underscores.', array('%db_prefix' => $db_prefix)), 'error');
  285. }
  286. if (!empty($db_port) && !is_numeric($db_port)) {
  287. form_set_error('db_port', st('Database port must be a number.'));
  288. }
  289. // Check database type
  290. if (!isset($form)) {
  291. $_db_url = is_array($db_url) ? $db_url['default'] : $db_url;
  292. $db_type = substr($_db_url, 0, strpos($_db_url, '://'));
  293. }
  294. $databases = drupal_detect_database_types();
  295. if (!in_array($db_type, $databases)) {
  296. form_set_error('db_type', st("In your %settings_file file you have configured @drupal to use a %db_type server, however your PHP installation currently does not support this database type.", array('%settings_file' => $settings_file, '@drupal' => drupal_install_profile_name(), '%db_type' => $db_type)));
  297. }
  298. else {
  299. // Verify
  300. $db_url = $db_type .'://'. urlencode($db_user) .($db_pass ? ':'. urlencode($db_pass) : '') .'@'. ($db_host ? urlencode($db_host) : 'localhost'). ($db_port ? ":$db_port" : '') .'/'. urlencode($db_path);
  301. if (isset($form)) {
  302. form_set_value($form['_db_url'], $db_url);
  303. }
  304. $success = array();
  305. $function = 'drupal_test_'. $db_type;
  306. if (!$function($db_url, $success)) {
  307. if (isset($success['CONNECT'])) {
  308. form_set_error('db_type', st('In order for Drupal to work and to proceed with the installation process you must resolve all permission issues reported above. We were able to verify that we have permission for the following commands: %commands. For more help with configuring your database server, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what any of this means you should probably contact your hosting provider.', array('%commands' => implode($success, ', '))));
  309. }
  310. else {
  311. form_set_error('db_type', '');
  312. }
  313. }
  314. }
  315. }
  316. /**
  317. * Form API submit for install_settings form.
  318. */
  319. function install_settings_form_submit($form_id, $form_values) {
  320. global $profile, $install_locale;
  321. // Update global settings array and save
  322. $settings['db_url'] = array(
  323. 'value' => $form_values['_db_url'],
  324. 'required' => TRUE,
  325. );
  326. $settings['db_prefix'] = array(
  327. 'value' => $form_values['db_prefix'],
  328. 'required' => TRUE,
  329. );
  330. drupal_rewrite_settings($settings);
  331. // Continue to install profile step
  332. install_goto("install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : ''));
  333. }
  334. /**
  335. * Find all .profile files and allow admin to select which to install.
  336. *
  337. * @return
  338. * The selected profile.
  339. */
  340. function install_select_profile() {
  341. include_once './includes/form.inc';
  342. $profiles = file_scan_directory('./profiles', '\.profile$', array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
  343. // Don't need to choose profile if only one available.
  344. if (sizeof($profiles) == 1) {
  345. $profile = array_pop($profiles);
  346. require_once $profile->filename;
  347. return $profile->name;
  348. }
  349. elseif (sizeof($profiles) > 1) {
  350. foreach ($profiles as $profile) {
  351. if ($_POST['profile'] == $profile->name) {
  352. return $profile->name;
  353. }
  354. }
  355. drupal_maintenance_theme();
  356. drupal_set_title(st('Select an installation profile'));
  357. print theme('install_page', drupal_get_form('install_select_profile_form', $profiles));
  358. exit;
  359. }
  360. }
  361. function install_select_profile_form($profiles) {
  362. foreach ($profiles as $profile) {
  363. include_once($profile->filename);
  364. // Load profile details.
  365. $function = $profile->name .'_profile_details';
  366. if (function_exists($function)) {
  367. $details = $function();
  368. }
  369. // If set, used defined name. Otherwise use file name.
  370. $name = isset($details['name']) ? $details['name'] : $profile->name;
  371. $form['profile'][$name] = array(
  372. '#type' => 'radio',
  373. '#value' => 'default',
  374. '#return_value' => $profile->name,
  375. '#title' => $name,
  376. '#description' => isset($details['description']) ? $details['description'] : '',
  377. '#parents' => array('profile'),
  378. );
  379. }
  380. $form['submit'] = array(
  381. '#type' => 'submit',
  382. '#value' => st('Save configuration'),
  383. );
  384. return $form;
  385. }
  386. /**
  387. * Find all .po files for the current profile and allow admin to select which to use.
  388. *
  389. * @return
  390. * The selected language.
  391. */
  392. function install_select_locale($profilename) {
  393. include_once './includes/file.inc';
  394. include_once './includes/form.inc';
  395. // Collect possible locales, add default
  396. $locales = file_scan_directory('./profiles/' . $profilename, '\.po$', array('.', '..', 'CVS'), 0, FALSE);
  397. array_unshift($locales, (object) array('name' => 'en'));
  398. // Don't need to choose locale if only one (English) is available.
  399. if (sizeof($locales) == 1) {
  400. return FALSE;
  401. } else {
  402. foreach ($locales as $locale) {
  403. if ($_POST['locale'] == $locale->name) {
  404. return $locale->name;
  405. }
  406. }
  407. drupal_maintenance_theme();
  408. drupal_set_title(st('Choose your preferred language'));
  409. print theme('install_page', drupal_get_form('install_select_locale_form', $locales));
  410. exit;
  411. }
  412. }
  413. function install_select_locale_form($locales) {
  414. include_once './includes/locale.inc';
  415. $languages = _locale_get_iso639_list();
  416. foreach ($locales as $locale) {
  417. // Try to use verbose locale name
  418. $name = $locale->name;
  419. if (isset($languages[$name])) {
  420. $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' '. st('(@language)', array('@language' => $languages[$name][1])) : '');
  421. }
  422. $form['locale'][$locale->name] = array(
  423. '#type' => 'radio',
  424. '#return_value' => $locale->name,
  425. '#default_value' => ($locale->name == 'en' ? TRUE : FALSE),
  426. '#title' => $name . ($locale->name == 'en' ? ' '. st('(built-in)') : ''),
  427. '#parents' => array('locale')
  428. );
  429. }
  430. $form['submit'] = array(
  431. '#type' => 'submit',
  432. '#value' => st('Save configuration'),
  433. );
  434. return $form;
  435. }
  436. /**
  437. * Show an error page when there are no profiles available.
  438. */
  439. function install_no_profile_error() {
  440. drupal_maintenance_theme();
  441. drupal_set_title(st('No profiles available'));
  442. print theme('install_page', '<p>'. st('We were unable to find any installer profiles. Installer profiles tell us what modules to enable and what schema to install in the database. A profile is necessary to continue with the installation process.') .'</p>');
  443. exit;
  444. }
  445. /**
  446. * Show an error page when Drupal has already been installed.
  447. */
  448. function install_already_done_error() {
  449. global $base_url;
  450. drupal_maintenance_theme();
  451. drupal_set_title(st('Drupal already installed'));
  452. print theme('install_page', st('<ul><li>To start over, you must empty your existing database and replace the appropriate <em>settings.php</em> with an unmodified copy.</li><li>To install to a different database, edit the appropriate <em>settings.php</em> file in the <em>sites</em> folder.</li><li>To upgrade an existing installation, proceed to the <a href="@base-url/update.php">update script</a>.</li></ul>', array('@base-url' => $base_url)));
  453. exit;
  454. }
  455. /**
  456. * Show an error page when Drupal is missing required modules.
  457. */
  458. function install_missing_modules_error($profile) {
  459. global $base_url;
  460. drupal_maintenance_theme();
  461. drupal_set_title(st('Modules missing'));
  462. print theme('install_page', '<p>'. st('One or more required modules are missing. Please check the error messages and <a href="!url">try again</a>.', array('!url' => "install.php?profile=$profile")) .'</p>');
  463. exit;
  464. }
  465. /**
  466. * Page displayed when the installation is complete. Called from install.php.
  467. */
  468. function install_complete($profile) {
  469. global $base_url;
  470. $output = '';
  471. // Store install profile for later use.
  472. variable_set('install_profile', $profile);
  473. // Bootstrap newly installed Drupal, while preserving existing messages.
  474. $messages = $_SESSION['messages'];
  475. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  476. $_SESSION['messages'] = $messages;
  477. // Build final page.
  478. drupal_maintenance_theme();
  479. drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())));
  480. $output .= '<p>'. st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) .'</p>';
  481. // Show profile finalization info.
  482. $function = $profile .'_profile_final';
  483. if (function_exists($function)) {
  484. // More steps required
  485. $profile_message = $function();
  486. }
  487. // If the profile returned a welcome message, use that instead of default.
  488. if (isset($profile_message)) {
  489. $output .= $profile_message;
  490. }
  491. else {
  492. // No more steps
  493. $output .= '<p>' . (drupal_set_message() ? st('Please review the messages above before continuing on to <a href="@url">your new site</a>.', array('@url' => url(''))) : st('You may now visit <a href="@url">your new site</a>.', array('@url' => url('')))) . '</p>';
  494. }
  495. // Output page.
  496. print theme('maintenance_page', $output);
  497. }
  498. /**
  499. * Page to check installation requirements and report any errors.
  500. */
  501. function install_check_requirements($profile) {
  502. $requirements = drupal_check_profile($profile);
  503. $severity = drupal_requirements_severity($requirements);
  504. // If there are issues, report them.
  505. if ($severity == REQUIREMENT_ERROR) {
  506. drupal_maintenance_theme();
  507. foreach ($requirements as $requirement) {
  508. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  509. drupal_set_message($requirement['description'] .' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')', 'error');
  510. }
  511. }
  512. drupal_set_title(st('Incompatible environment'));
  513. print theme('install_page', '');
  514. exit;
  515. }
  516. }
  517. install_main();