install.php

  1. 7.x drupal-7.x/install.php
  2. 6.x drupal-6.x/install.php
  3. 6.x documentation-6.x/developer/hooks/install.php

File

drupal-6.x/install.php
View source
  1. <?php
  2. require_once './includes/install.inc';
  3. define('MAINTENANCE_MODE', 'install');
  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, $conf;
  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. // Set up $language, so t() caller functions will still work.
  25. drupal_init_language();
  26. // Load module basics (needed for hook invokes).
  27. include_once './includes/module.inc';
  28. $module_list['system']['filename'] = 'modules/system/system.module';
  29. $module_list['filter']['filename'] = 'modules/filter/filter.module';
  30. module_list(TRUE, FALSE, FALSE, $module_list);
  31. drupal_load('module', 'system');
  32. drupal_load('module', 'filter');
  33. // Install profile chosen, set the global immediately.
  34. // This needs to be done before the theme cache gets
  35. // initialized in drupal_maintenance_theme().
  36. if (!empty($_GET['profile'])) {
  37. $profile = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['profile']);
  38. }
  39. // Set up theme system for the maintenance page.
  40. drupal_maintenance_theme();
  41. // Check existing settings.php.
  42. $verify = install_verify_settings();
  43. if ($verify) {
  44. // Since we have a database connection, we use the normal cache system.
  45. // This is important, as the installer calls into the Drupal system for
  46. // the clean URL checks, so we should maintain the cache properly.
  47. require_once './includes/cache.inc';
  48. $conf['cache_inc'] = './includes/cache.inc';
  49. // Establish a connection to the database.
  50. require_once './includes/database.inc';
  51. db_set_active();
  52. // Check if Drupal is installed.
  53. $task = install_verify_drupal();
  54. if ($task == 'done') {
  55. install_already_done_error();
  56. }
  57. }
  58. else {
  59. // Since no persistent storage is available yet, and functions that check
  60. // for cached data will fail, we temporarily replace the normal cache
  61. // system with a stubbed-out version that short-circuits the actual
  62. // caching process and avoids any errors.
  63. require_once './includes/cache-install.inc';
  64. $conf['cache_inc'] = './includes/cache-install.inc';
  65. $task = NULL;
  66. }
  67. // No profile was passed in GET, ask the user.
  68. if (empty($_GET['profile'])) {
  69. if ($profile = install_select_profile()) {
  70. install_goto("install.php?profile=$profile");
  71. }
  72. else {
  73. install_no_profile_error();
  74. }
  75. }
  76. // Load the profile.
  77. require_once "./profiles/$profile/$profile.profile";
  78. // Locale selection
  79. if (!empty($_GET['locale'])) {
  80. $install_locale = preg_replace('/[^a-zA-Z_0-9\-]/', '', $_GET['locale']);
  81. }
  82. elseif (($install_locale = install_select_locale($profile)) !== FALSE) {
  83. install_goto("install.php?profile=$profile&locale=$install_locale");
  84. }
  85. // Tasks come after the database is set up
  86. if (!$task) {
  87. global $db_url;
  88. if (!$verify && !empty($db_url)) {
  89. // Do not install over a configured settings.php.
  90. install_already_done_error();
  91. }
  92. // Check the installation requirements for Drupal and this profile.
  93. install_check_requirements($profile, $verify);
  94. // Verify existence of all required modules.
  95. $modules = drupal_verify_profile($profile, $install_locale);
  96. // If any error messages are set now, it means a requirement problem.
  97. $messages = drupal_set_message();
  98. if (!empty($messages['error'])) {
  99. install_task_list('requirements');
  100. drupal_set_title(st('Requirements problem'));
  101. print theme('install_page', '');
  102. exit;
  103. }
  104. // Change the settings.php information if verification failed earlier.
  105. // Note: will trigger a redirect if database credentials change.
  106. if (!$verify) {
  107. install_change_settings($profile, $install_locale);
  108. }
  109. // The default lock implementation uses a database table,
  110. // so we cannot use it for install, but we still need
  111. // the API functions available.
  112. require_once './includes/lock-install.inc';
  113. $conf['lock_inc'] = './includes/lock-install.inc';
  114. lock_init();
  115. // Install system.module.
  116. drupal_install_system();
  117. // Ensure that all of Drupal's standard directories have appropriate
  118. // .htaccess files. These directories will have already been created by
  119. // this point in the installer, since Drupal creates them during the
  120. // install_check_requirements() task. Note that we cannot create them any
  121. // earlier than this, since the code below relies on system.module in order
  122. // to work.
  123. file_create_htaccess(file_directory_path());
  124. file_create_htaccess(file_directory_temp());
  125. // Save the list of other modules to install for the 'profile-install'
  126. // task. variable_set() can be used now that system.module is installed
  127. // and drupal is bootstrapped.
  128. variable_set('install_profile_modules', array_diff($modules, array('system')));
  129. }
  130. // The database is set up, turn to further tasks.
  131. install_tasks($profile, $task);
  132. }
  133. /**
  134. * Verify if Drupal is installed.
  135. */
  136. function install_verify_drupal() {
  137. // Read the variable manually using the @ so we don't trigger an error if it fails.
  138. $result = @db_query("SELECT value FROM {variable} WHERE name = '%s'", 'install_task');
  139. if ($result) {
  140. return unserialize(db_result($result));
  141. }
  142. }
  143. /**
  144. * Verify existing settings.php
  145. */
  146. function install_verify_settings() {
  147. global $db_prefix, $db_type, $db_url;
  148. // Verify existing settings (if any).
  149. if (!empty($db_url)) {
  150. // We need this because we want to run form_get_errors.
  151. include_once './includes/form.inc';
  152. $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  153. $db_user = urldecode($url['user']);
  154. $db_pass = isset($url['pass']) ? urldecode($url['pass']) : NULL;
  155. $db_host = urldecode($url['host']);
  156. $db_port = isset($url['port']) ? urldecode($url['port']) : '';
  157. $db_path = ltrim(urldecode($url['path']), '/');
  158. $settings_file = './'. conf_path(FALSE, TRUE) .'/settings.php';
  159. $form_state = array();
  160. _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file, $form_state);
  161. if (!form_get_errors()) {
  162. return TRUE;
  163. }
  164. }
  165. return FALSE;
  166. }
  167. /**
  168. * Configure and rewrite settings.php.
  169. */
  170. function install_change_settings($profile = 'default', $install_locale = '') {
  171. global $db_url, $db_type, $db_prefix;
  172. $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  173. $db_user = isset($url['user']) ? urldecode($url['user']) : '';
  174. $db_pass = isset($url['pass']) ? urldecode($url['pass']) : '';
  175. $db_host = isset($url['host']) ? urldecode($url['host']) : '';
  176. $db_port = isset($url['port']) ? urldecode($url['port']) : '';
  177. $db_path = ltrim(urldecode($url['path']), '/');
  178. $conf_path = './'. conf_path(FALSE, TRUE);
  179. $settings_file = $conf_path .'/settings.php';
  180. // We always need this because we want to run form_get_errors.
  181. include_once './includes/form.inc';
  182. install_task_list('database');
  183. $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);
  184. drupal_set_title(st('Database configuration'));
  185. print theme('install_page', $output);
  186. exit;
  187. }
  188. /**
  189. * Form API array definition for install_settings.
  190. */
  191. function install_settings_form(&$form_state, $profile, $install_locale, $settings_file, $db_url, $db_type, $db_prefix, $db_user, $db_pass, $db_host, $db_port, $db_path) {
  192. if (empty($db_host)) {
  193. $db_host = 'localhost';
  194. }
  195. $db_types = drupal_detect_database_types();
  196. // If both 'mysql' and 'mysqli' are available, we disable 'mysql':
  197. if (isset($db_types['mysqli'])) {
  198. unset($db_types['mysql']);
  199. }
  200. if (count($db_types) == 0) {
  201. $form['no_db_types'] = array(
  202. '#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')),
  203. );
  204. }
  205. else {
  206. $form['basic_options'] = array(
  207. '#type' => 'fieldset',
  208. '#title' => st('Basic options'),
  209. '#description' => '<p>'. st('To set up your @drupal database, enter the following information.', array('@drupal' => drupal_install_profile_name())) .'</p>',
  210. );
  211. if (count($db_types) > 1) {
  212. $form['basic_options']['db_type'] = array(
  213. '#type' => 'radios',
  214. '#title' => st('Database type'),
  215. '#required' => TRUE,
  216. '#options' => $db_types,
  217. '#default_value' => ($db_type ? $db_type : current($db_types)),
  218. '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_name())),
  219. );
  220. $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()));
  221. }
  222. else {
  223. if (count($db_types) == 1) {
  224. $db_types = array_values($db_types);
  225. $form['basic_options']['db_type'] = array(
  226. '#type' => 'hidden',
  227. '#value' => $db_types[0],
  228. );
  229. $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()));
  230. }
  231. }
  232. // Database name
  233. $form['basic_options']['db_path'] = array(
  234. '#type' => 'textfield',
  235. '#title' => st('Database name'),
  236. '#default_value' => $db_path,
  237. '#size' => 45,
  238. '#required' => TRUE,
  239. '#description' => $db_path_description
  240. );
  241. // Database username
  242. $form['basic_options']['db_user'] = array(
  243. '#type' => 'textfield',
  244. '#title' => st('Database username'),
  245. '#default_value' => $db_user,
  246. '#size' => 45,
  247. '#required' => TRUE,
  248. );
  249. // Database username
  250. $form['basic_options']['db_pass'] = array(
  251. '#type' => 'password',
  252. '#title' => st('Database password'),
  253. '#default_value' => $db_pass,
  254. '#size' => 45,
  255. );
  256. $form['advanced_options'] = array(
  257. '#type' => 'fieldset',
  258. '#title' => st('Advanced options'),
  259. '#collapsible' => TRUE,
  260. '#collapsed' => TRUE,
  261. '#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.")
  262. );
  263. // Database host
  264. $form['advanced_options']['db_host'] = array(
  265. '#type' => 'textfield',
  266. '#title' => st('Database host'),
  267. '#default_value' => $db_host,
  268. '#size' => 45,
  269. // Hostnames can be 255 characters long.
  270. '#maxlength' => 255,
  271. '#required' => TRUE,
  272. '#description' => st('If your database is located on a different server, change this.'),
  273. );
  274. // Database port
  275. $form['advanced_options']['db_port'] = array(
  276. '#type' => 'textfield',
  277. '#title' => st('Database port'),
  278. '#default_value' => $db_port,
  279. '#size' => 45,
  280. // The maximum port number is 65536, 5 digits.
  281. '#maxlength' => 5,
  282. '#description' => st('If your database server is listening to a non-standard port, enter its number.'),
  283. );
  284. // Table prefix
  285. $prefix = ($profile == 'default') ? 'drupal_' : $profile .'_';
  286. $form['advanced_options']['db_prefix'] = array(
  287. '#type' => 'textfield',
  288. '#title' => st('Table prefix'),
  289. '#default_value' => $db_prefix,
  290. '#size' => 45,
  291. '#description' => st('If more than one application will be sharing this database, enter a table prefix such as %prefix for your @drupal site here.', array('@drupal' => drupal_install_profile_name(), '%prefix' => $prefix)),
  292. );
  293. $form['save'] = array(
  294. '#type' => 'submit',
  295. '#value' => st('Save and continue'),
  296. );
  297. $form['errors'] = array();
  298. $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
  299. $form['_db_url'] = array('#type' => 'value');
  300. $form['#action'] = "install.php?profile=$profile". ($install_locale ? "&locale=$install_locale" : '');
  301. $form['#redirect'] = FALSE;
  302. }
  303. return $form;
  304. }
  305. /**
  306. * Form API validate for install_settings form.
  307. */
  308. function install_settings_form_validate($form, &$form_state) {
  309. global $db_url;
  310. _install_settings_form_validate($form_state['values']['db_prefix'], $form_state['values']['db_type'], $form_state['values']['db_user'], $form_state['values']['db_pass'], $form_state['values']['db_host'], $form_state['values']['db_port'], $form_state['values']['db_path'], $form_state['values']['settings_file'], $form_state, $form);
  311. }
  312. /**
  313. * Helper function for install_settings_validate.
  314. */
  315. function _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file, &$form_state, $form = NULL) {
  316. global $db_url;
  317. // Verify the table prefix
  318. if (!empty($db_prefix) && is_string($db_prefix) && !preg_match('/^[A-Za-z0-9_.]+$/', $db_prefix)) {
  319. 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, periods, or underscores.', array('%db_prefix' => $db_prefix)), 'error');
  320. }
  321. if (!empty($db_port) && !is_numeric($db_port)) {
  322. form_set_error('db_port', st('Database port must be a number.'));
  323. }
  324. // Check database type
  325. if (!isset($form)) {
  326. $_db_url = is_array($db_url) ? $db_url['default'] : $db_url;
  327. $db_type = substr($_db_url, 0, strpos($_db_url, '://'));
  328. }
  329. $databases = drupal_detect_database_types();
  330. if (!in_array($db_type, $databases)) {
  331. 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)));
  332. }
  333. else {
  334. // Verify
  335. $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);
  336. if (isset($form)) {
  337. form_set_value($form['_db_url'], $db_url, $form_state);
  338. }
  339. $success = array();
  340. $function = 'drupal_test_'. $db_type;
  341. if (!$function($db_url, $success)) {
  342. if (isset($success['CONNECT'])) {
  343. form_set_error('db_type', st('In order for Drupal to work, and to continue 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, ', '))));
  344. }
  345. else {
  346. form_set_error('db_type', '');
  347. }
  348. }
  349. }
  350. }
  351. /**
  352. * Form API submit for install_settings form.
  353. */
  354. function install_settings_form_submit($form, &$form_state) {
  355. global $profile, $install_locale;
  356. // Update global settings array and save
  357. $settings['db_url'] = array(
  358. 'value' => $form_state['values']['_db_url'],
  359. 'required' => TRUE,
  360. );
  361. $settings['db_prefix'] = array(
  362. 'value' => $form_state['values']['db_prefix'],
  363. 'required' => TRUE,
  364. );
  365. drupal_rewrite_settings($settings);
  366. // Continue to install profile step
  367. install_goto("install.php?profile=$profile". ($install_locale ? "&locale=$install_locale" : ''));
  368. }
  369. /**
  370. * Find all .profile files.
  371. */
  372. function install_find_profiles() {
  373. return file_scan_directory('./profiles', '\.profile$', array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
  374. }
  375. /**
  376. * Allow admin to select which profile to install.
  377. *
  378. * @return
  379. * The selected profile.
  380. */
  381. function install_select_profile() {
  382. include_once './includes/form.inc';
  383. $profiles = install_find_profiles();
  384. // Don't need to choose profile if only one available.
  385. if (sizeof($profiles) == 1) {
  386. $profile = array_pop($profiles);
  387. require_once $profile->filename;
  388. return $profile->name;
  389. }
  390. elseif (sizeof($profiles) > 1) {
  391. foreach ($profiles as $profile) {
  392. if (!empty($_POST['profile']) && ($_POST['profile'] == $profile->name)) {
  393. return $profile->name;
  394. }
  395. }
  396. install_task_list('profile-select');
  397. drupal_set_title(st('Select an installation profile'));
  398. print theme('install_page', drupal_get_form('install_select_profile_form', $profiles));
  399. exit;
  400. }
  401. }
  402. /**
  403. * Form API array definition for the profile selection form.
  404. *
  405. * @param $form_state
  406. * Array of metadata about state of form processing.
  407. * @param $profile_files
  408. * Array of .profile files, as returned from file_scan_directory().
  409. */
  410. function install_select_profile_form(&$form_state, $profile_files) {
  411. $profiles = array();
  412. $names = array();
  413. foreach ($profile_files as $profile) {
  414. include_once($profile->filename);
  415. // Load profile details and store them for later retrieval.
  416. $function = $profile->name .'_profile_details';
  417. if (function_exists($function)) {
  418. $details = $function();
  419. }
  420. $profiles[$profile->name] = $details;
  421. // Determine the name of the profile; default to file name if defined name
  422. // is unspecified.
  423. $name = isset($details['name']) ? $details['name'] : $profile->name;
  424. $names[$profile->name] = $name;
  425. }
  426. // Display radio buttons alphabetically by human-readable name.
  427. natcasesort($names);
  428. foreach ($names as $profile => $name) {
  429. $form['profile'][$name] = array(
  430. '#type' => 'radio',
  431. '#value' => 'default',
  432. '#return_value' => $profile,
  433. '#title' => $name,
  434. '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
  435. '#parents' => array('profile'),
  436. );
  437. }
  438. $form['submit'] = array(
  439. '#type' => 'submit',
  440. '#value' => st('Save and continue'),
  441. );
  442. return $form;
  443. }
  444. /**
  445. * Find all .po files for the current profile.
  446. */
  447. function install_find_locales($profilename) {
  448. $locales = file_scan_directory('./profiles/'. $profilename .'/translations', '\.po$', array('.', '..', 'CVS'), 0, FALSE);
  449. array_unshift($locales, (object) array('name' => 'en'));
  450. return $locales;
  451. }
  452. /**
  453. * Allow admin to select which locale to use for the current profile.
  454. *
  455. * @return
  456. * The selected language.
  457. */
  458. function install_select_locale($profilename) {
  459. include_once './includes/file.inc';
  460. include_once './includes/form.inc';
  461. // Find all available locales.
  462. $locales = install_find_locales($profilename);
  463. // If only the built-in (English) language is available,
  464. // and we are using the default profile, inform the user
  465. // that the installer can be localized. Otherwise we assume
  466. // the user know what he is doing.
  467. if (count($locales) == 1) {
  468. if ($profilename == 'default') {
  469. install_task_list('locale-select');
  470. drupal_set_title(st('Choose language'));
  471. if (!empty($_GET['localize'])) {
  472. $output = '<p>'. st('With the addition of an appropriate translation package, this installer is capable of proceeding in another language of your choice. To install and use Drupal in a language other than English:') .'</p>';
  473. $output .= '<ul><li>'. st('Determine if <a href="@translations" target="_blank">a translation of this Drupal version</a> is available in your language of choice. A translation is provided via a translation package; each translation package enables the display of a specific version of Drupal in a specific language. Not all languages are available for every version of Drupal.', array('@translations' => 'http://localize.drupal.org')) .'</li>';
  474. $output .= '<li>'. st('If an alternative translation package of your choice is available, download and extract its contents to your Drupal root directory.') .'</li>';
  475. $output .= '<li>'. st('Return to choose language using the second link below and select your desired language from the displayed list. Reloading the page allows the list to automatically adjust to the presence of new translation packages.') .'</li>';
  476. $output .= '</ul><p>'. st('Alternatively, to install and use Drupal in English, or to defer the selection of an alternative language until after installation, select the first link below.') .'</p>';
  477. $output .= '<p>'. st('How should the installation continue?') .'</p>';
  478. $output .= '<ul><li><a href="install.php?profile='. $profilename .'&amp;locale=en">'. st('Continue installation in English') .'</a></li><li><a href="install.php?profile='. $profilename .'">'. st('Return to choose a language') .'</a></li></ul>';
  479. }
  480. else {
  481. $output = '<ul><li><a href="install.php?profile='. $profilename .'&amp;locale=en">'. st('Install Drupal in English') .'</a></li><li><a href="install.php?profile='. $profilename .'&amp;localize=true">'. st('Learn how to install Drupal in other languages') .'</a></li></ul>';
  482. }
  483. print theme('install_page', $output);
  484. exit;
  485. }
  486. // One language, but not the default profile, assume
  487. // the user knows what he is doing.
  488. return FALSE;
  489. }
  490. else {
  491. // Allow profile to pre-select the language, skipping the selection.
  492. $function = $profilename .'_profile_details';
  493. if (function_exists($function)) {
  494. $details = $function();
  495. if (isset($details['language'])) {
  496. foreach ($locales as $locale) {
  497. if ($details['language'] == $locale->name) {
  498. return $locale->name;
  499. }
  500. }
  501. }
  502. }
  503. if (!empty($_POST['locale'])) {
  504. foreach ($locales as $locale) {
  505. if ($_POST['locale'] == $locale->name) {
  506. return $locale->name;
  507. }
  508. }
  509. }
  510. install_task_list('locale-select');
  511. drupal_set_title(st('Choose language'));
  512. print theme('install_page', drupal_get_form('install_select_locale_form', $locales));
  513. exit;
  514. }
  515. }
  516. /**
  517. * Form API array definition for language selection.
  518. */
  519. function install_select_locale_form(&$form_state, $locales) {
  520. include_once './includes/locale.inc';
  521. $languages = _locale_get_predefined_list();
  522. foreach ($locales as $locale) {
  523. // Try to use verbose locale name
  524. $name = $locale->name;
  525. if (isset($languages[$name])) {
  526. $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' '. st('(@language)', array('@language' => $languages[$name][1])) : '');
  527. }
  528. $form['locale'][$locale->name] = array(
  529. '#type' => 'radio',
  530. '#return_value' => $locale->name,
  531. '#default_value' => ($locale->name == 'en' ? TRUE : FALSE),
  532. '#title' => $name . ($locale->name == 'en' ? ' '. st('(built-in)') : ''),
  533. '#parents' => array('locale')
  534. );
  535. }
  536. $form['submit'] = array(
  537. '#type' => 'submit',
  538. '#value' => st('Select language'),
  539. );
  540. return $form;
  541. }
  542. /**
  543. * Show an error page when there are no profiles available.
  544. */
  545. function install_no_profile_error() {
  546. install_task_list('profile-select');
  547. drupal_set_title(st('No profiles available'));
  548. 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>');
  549. exit;
  550. }
  551. /**
  552. * Show an error page when Drupal has already been installed.
  553. */
  554. function install_already_done_error() {
  555. global $base_url;
  556. drupal_set_title(st('Drupal already installed'));
  557. print theme('install_page', st('<ul><li>To start over, you must empty your existing database.</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><li>View your <a href="@base-url">existing site</a>.</li></ul>', array('@base-url' => $base_url)));
  558. exit;
  559. }
  560. /**
  561. * Tasks performed after the database is initialized.
  562. */
  563. function install_tasks($profile, $task) {
  564. global $base_url, $install_locale;
  565. // Bootstrap newly installed Drupal, while preserving existing messages.
  566. $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : '';
  567. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  568. $_SESSION['messages'] = $messages;
  569. // URL used to direct page requests.
  570. $url = $base_url .'/install.php?locale='. $install_locale .'&profile='. $profile;
  571. // Build a page for final tasks.
  572. if (empty($task)) {
  573. variable_set('install_task', 'profile-install');
  574. $task = 'profile-install';
  575. }
  576. // We are using a list of if constructs here to allow for
  577. // passing from one task to the other in the same request.
  578. // Install profile modules.
  579. if ($task == 'profile-install') {
  580. $modules = variable_get('install_profile_modules', array());
  581. $files = module_rebuild_cache();
  582. variable_del('install_profile_modules');
  583. $operations = array();
  584. foreach ($modules as $module) {
  585. $operations[] = array('_install_module_batch', array($module, $files[$module]->info['name']));
  586. }
  587. $batch = array(
  588. 'operations' => $operations,
  589. 'finished' => '_install_profile_batch_finished',
  590. 'title' => st('Installing @drupal', array('@drupal' => drupal_install_profile_name())),
  591. 'error_message' => st('The installation has encountered an error.'),
  592. );
  593. // Start a batch, switch to 'profile-install-batch' task. We need to
  594. // set the variable here, because batch_process() redirects.
  595. variable_set('install_task', 'profile-install-batch');
  596. batch_set($batch);
  597. batch_process($url, $url);
  598. }
  599. // We are running a batch install of the profile's modules.
  600. // This might run in multiple HTTP requests, constantly redirecting
  601. // to the same address, until the batch finished callback is invoked
  602. // and the task advances to 'locale-initial-import'.
  603. if ($task == 'profile-install-batch') {
  604. include_once 'includes/batch.inc';
  605. $output = _batch_page();
  606. }
  607. // Import interface translations for the enabled modules.
  608. if ($task == 'locale-initial-import') {
  609. if (!empty($install_locale) && ($install_locale != 'en')) {
  610. include_once 'includes/locale.inc';
  611. // Enable installation language as default site language.
  612. locale_add_language($install_locale, NULL, NULL, NULL, NULL, NULL, 1, TRUE);
  613. // Collect files to import for this language.
  614. $batch = locale_batch_by_language($install_locale, '_install_locale_initial_batch_finished');
  615. if (!empty($batch)) {
  616. // Remember components we cover in this batch set.
  617. variable_set('install_locale_batch_components', $batch['#components']);
  618. // Start a batch, switch to 'locale-batch' task. We need to
  619. // set the variable here, because batch_process() redirects.
  620. variable_set('install_task', 'locale-initial-batch');
  621. batch_set($batch);
  622. batch_process($url, $url);
  623. }
  624. }
  625. // Found nothing to import or not foreign language, go to next task.
  626. $task = 'configure';
  627. }
  628. if ($task == 'locale-initial-batch') {
  629. include_once 'includes/batch.inc';
  630. include_once 'includes/locale.inc';
  631. $output = _batch_page();
  632. }
  633. if ($task == 'configure') {
  634. if (variable_get('site_name', FALSE) || variable_get('site_mail', FALSE)) {
  635. // Site already configured: This should never happen, means re-running
  636. // the installer, possibly by an attacker after the 'install_task' variable
  637. // got accidentally blown somewhere. Stop it now.
  638. install_already_done_error();
  639. }
  640. $form = drupal_get_form('install_configure_form', $url);
  641. if (!variable_get('site_name', FALSE) && !variable_get('site_mail', FALSE)) {
  642. // Not submitted yet: Prepare to display the form.
  643. $output = $form;
  644. drupal_set_title(st('Configure site'));
  645. // Warn about settings.php permissions risk
  646. $settings_dir = './'. conf_path();
  647. $settings_file = $settings_dir .'/settings.php';
  648. if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file($settings_dir, FILE_NOT_WRITABLE, 'dir')) {
  649. drupal_set_message(st('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, please consult the <a href="@handbook_url">on-line handbook</a>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/getting-started')), 'error');
  650. }
  651. else {
  652. drupal_set_message(st('All necessary changes to %dir and %file have been made. They have been set to read-only for security.', array('%dir' => $settings_dir, '%file' => $settings_file)));
  653. }
  654. // Add JavaScript validation.
  655. _user_password_dynamic_validation();
  656. drupal_add_js(drupal_get_path('module', 'system') .'/system.js', 'module');
  657. // We add these strings as settings because JavaScript translation does not
  658. // work on install time.
  659. drupal_add_js(array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail')), 'cleanURL' => array('success' => st('Your server has been successfully tested to support this feature.'), 'failure' => st('Your system configuration does not currently support this feature. The <a href="http://drupal.org/node/15365">handbook page on Clean URLs</a> has additional troubleshooting information.'), 'testing' => st('Testing clean URLs...'))), 'setting');
  660. drupal_add_js('
  661. // Global Killswitch
  662. if (Drupal.jsEnabled) {
  663. $(document).ready(function() {
  664. Drupal.cleanURLsInstallCheck();
  665. Drupal.setDefaultTimezone();
  666. });
  667. }', 'inline');
  668. // Build menu to allow clean URL check.
  669. menu_rebuild();
  670. }
  671. else {
  672. $task = 'profile';
  673. }
  674. }
  675. // If found an unknown task or the 'profile' task, which is
  676. // reserved for profiles, hand over the control to the profile,
  677. // so it can run any number of custom tasks it defines.
  678. if (!in_array($task, install_reserved_tasks())) {
  679. $function = $profile .'_profile_tasks';
  680. if (function_exists($function)) {
  681. // The profile needs to run more code, maybe even more tasks.
  682. // $task is sent through as a reference and may be changed!
  683. $output = $function($task, $url);
  684. }
  685. // If the profile doesn't move on to a new task we assume
  686. // that it is done.
  687. if ($task == 'profile') {
  688. $task = 'profile-finished';
  689. }
  690. }
  691. // Profile custom tasks are done, so let the installer regain
  692. // control and proceed with importing the remaining translations.
  693. if ($task == 'profile-finished') {
  694. if (!empty($install_locale) && ($install_locale != 'en')) {
  695. include_once 'includes/locale.inc';
  696. // Collect files to import for this language. Skip components
  697. // already covered in the initial batch set.
  698. $batch = locale_batch_by_language($install_locale, '_install_locale_remaining_batch_finished', variable_get('install_locale_batch_components', array()));
  699. // Remove temporary variable.
  700. variable_del('install_locale_batch_components');
  701. if (!empty($batch)) {
  702. // Start a batch, switch to 'locale-remaining-batch' task. We need to
  703. // set the variable here, because batch_process() redirects.
  704. variable_set('install_task', 'locale-remaining-batch');
  705. batch_set($batch);
  706. batch_process($url, $url);
  707. }
  708. }
  709. // Found nothing to import or not foreign language, go to next task.
  710. $task = 'finished';
  711. }
  712. if ($task == 'locale-remaining-batch') {
  713. include_once 'includes/batch.inc';
  714. include_once 'includes/locale.inc';
  715. $output = _batch_page();
  716. }
  717. // Display a 'finished' page to user.
  718. if ($task == 'finished') {
  719. drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())));
  720. $messages = drupal_set_message();
  721. $output = '<p>'. st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) .'</p>';
  722. $output .= '<p>'. (isset($messages['error']) ? 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>';
  723. $task = 'done';
  724. }
  725. // The end of the install process. Remember profile used.
  726. if ($task == 'done') {
  727. // Rebuild menu to get content type links registered by the profile,
  728. // and possibly any other menu items created through the tasks.
  729. menu_rebuild();
  730. // Register actions declared by any modules.
  731. actions_synchronize();
  732. // Randomize query-strings on css/js files, to hide the fact that
  733. // this is a new install, not upgraded yet.
  734. _drupal_flush_css_js();
  735. variable_set('install_profile', $profile);
  736. }
  737. // Set task for user, and remember the task in the database.
  738. install_task_list($task);
  739. variable_set('install_task', $task);
  740. // Output page, if some output was required. Otherwise it is possible
  741. // that we are printing a JSON page and theme output should not be there.
  742. if (isset($output)) {
  743. print theme('maintenance_page', $output);
  744. }
  745. }
  746. /**
  747. * Batch callback for batch installation of modules.
  748. */
  749. function _install_module_batch($module, $module_name, &$context) {
  750. _drupal_install_module($module);
  751. // We enable the installed module right away, so that the module will be
  752. // loaded by drupal_bootstrap in subsequent batch requests, and other
  753. // modules possibly depending on it can safely perform their installation
  754. // steps.
  755. module_enable(array($module));
  756. $context['results'][] = $module;
  757. $context['message'] = st('Installed %module module.', array('%module' => $module_name));
  758. }
  759. /**
  760. * Finished callback for the modules install batch.
  761. *
  762. * Advance installer task to language import.
  763. */
  764. function _install_profile_batch_finished($success, $results) {
  765. variable_set('install_task', 'locale-initial-import');
  766. }
  767. /**
  768. * Finished callback for the first locale import batch.
  769. *
  770. * Advance installer task to the configure screen.
  771. */
  772. function _install_locale_initial_batch_finished($success, $results) {
  773. variable_set('install_task', 'configure');
  774. }
  775. /**
  776. * Finished callback for the second locale import batch.
  777. *
  778. * Advance installer task to the finished screen.
  779. */
  780. function _install_locale_remaining_batch_finished($success, $results) {
  781. variable_set('install_task', 'finished');
  782. }
  783. /**
  784. * The list of reserved tasks to run in the installer.
  785. */
  786. function install_reserved_tasks() {
  787. return array('configure', 'profile-install', 'profile-install-batch', 'locale-initial-import', 'locale-initial-batch', 'profile-finished', 'locale-remaining-batch', 'finished', 'done');
  788. }
  789. /**
  790. * Check installation requirements and report any errors.
  791. */
  792. function install_check_requirements($profile, $verify) {
  793. // If Drupal is not set up already, we need to create a settings file.
  794. if (!$verify) {
  795. $writable = FALSE;
  796. $conf_path = './'. conf_path(FALSE, TRUE);
  797. $settings_file = $conf_path .'/settings.php';
  798. $file = $conf_path;
  799. $exists = FALSE;
  800. // Verify that the directory exists.
  801. if (drupal_verify_install_file($conf_path, FILE_EXIST, 'dir')) {
  802. // Check to make sure a settings.php already exists.
  803. $file = $settings_file;
  804. if (drupal_verify_install_file($settings_file, FILE_EXIST)) {
  805. $exists = TRUE;
  806. // If it does, make sure it is writable.
  807. $writable = drupal_verify_install_file($settings_file, FILE_READABLE|FILE_WRITABLE);
  808. }
  809. }
  810. if (!$exists) {
  811. drupal_set_message(st('The @drupal installer requires that you create a settings file as part of the installation process.
  812. <ol>
  813. <li>Copy the %default_file file to %file.</li>
  814. <li>Change file permissions so that it is writable by the web server. If you are unsure how to grant file permissions, please consult the <a href="@handbook_url">on-line handbook</a>.</li>
  815. </ol>
  816. More details about installing Drupal are available in INSTALL.txt.', array('@drupal' => drupal_install_profile_name(), '%file' => $file, '%default_file' => $conf_path .'/default.settings.php', '@handbook_url' => 'http://drupal.org/server-permissions')), 'error');
  817. }
  818. elseif (!$writable) {
  819. drupal_set_message(st('The @drupal installer requires write permissions to %file during the installation process. If you are unsure how to grant file permissions, please consult the <a href="@handbook_url">on-line handbook</a>.', array('@drupal' => drupal_install_profile_name(), '%file' => $file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'error');
  820. }
  821. }
  822. // Check the other requirements.
  823. $requirements = drupal_check_profile($profile);
  824. $severity = drupal_requirements_severity($requirements);
  825. // If there are issues, report them.
  826. if ($severity == REQUIREMENT_ERROR) {
  827. foreach ($requirements as $requirement) {
  828. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  829. $message = $requirement['description'];
  830. if (isset($requirement['value']) && $requirement['value']) {
  831. $message .= ' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')';
  832. }
  833. drupal_set_message($message, 'error');
  834. }
  835. }
  836. }
  837. if ($severity == REQUIREMENT_WARNING) {
  838. foreach ($requirements as $requirement) {
  839. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_WARNING) {
  840. $message = $requirement['description'];
  841. if (isset($requirement['value']) && $requirement['value']) {
  842. $message .= ' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')';
  843. }
  844. drupal_set_message($message, 'warning');
  845. }
  846. }
  847. }
  848. }
  849. /**
  850. * Add the installation task list to the current page.
  851. */
  852. function install_task_list($active = NULL) {
  853. // Default list of tasks.
  854. $tasks = array(
  855. 'profile-select' => st('Choose profile'),
  856. 'locale-select' => st('Choose language'),
  857. 'requirements' => st('Verify requirements'),
  858. 'database' => st('Set up database'),
  859. 'profile-install-batch' => st('Install profile'),
  860. 'locale-initial-batch' => st('Set up translations'),
  861. 'configure' => st('Configure site'),
  862. );
  863. $profiles = install_find_profiles();
  864. $profile = isset($_GET['profile']) && isset($profiles[$_GET['profile']]) ? $_GET['profile'] : '.';
  865. $locales = install_find_locales($profile);
  866. // If we have only one profile, remove 'Choose profile'
  867. // and rename 'Install profile'.
  868. if (count($profiles) == 1) {
  869. unset($tasks['profile-select']);
  870. $tasks['profile-install-batch'] = st('Install site');
  871. }
  872. // Add tasks defined by the profile.
  873. if ($profile) {
  874. $function = $profile .'_profile_task_list';
  875. if (function_exists($function)) {
  876. $result = $function();
  877. if (is_array($result)) {
  878. $tasks += $result;
  879. }
  880. }
  881. }
  882. if (count($locales) < 2 || empty($_GET['locale']) || $_GET['locale'] == 'en') {
  883. // If not required, remove translation import from the task list.
  884. unset($tasks['locale-initial-batch']);
  885. }
  886. else {
  887. // If required, add remaining translations import task.
  888. $tasks += array('locale-remaining-batch' => st('Finish translations'));
  889. }
  890. // Add finished step as the last task.
  891. $tasks += array(
  892. 'finished' => st('Finished')
  893. );
  894. // Let the theming function know that 'finished' and 'done'
  895. // include everything, so every step is completed.
  896. if (in_array($active, array('finished', 'done'))) {
  897. $active = NULL;
  898. }
  899. drupal_set_content('left', theme_task_list($tasks, $active));
  900. }
  901. /**
  902. * Form API array definition for site configuration.
  903. */
  904. function install_configure_form(&$form_state, $url) {
  905. $form['intro'] = array(
  906. '#value' => st('To configure your website, please provide the following information.'),
  907. '#weight' => -10,
  908. );
  909. $form['site_information'] = array(
  910. '#type' => 'fieldset',
  911. '#title' => st('Site information'),
  912. '#collapsible' => FALSE,
  913. );
  914. $form['site_information']['site_name'] = array(
  915. '#type' => 'textfield',
  916. '#title' => st('Site name'),
  917. '#required' => TRUE,
  918. '#weight' => -20,
  919. );
  920. $form['site_information']['site_mail'] = array(
  921. '#type' => 'textfield',
  922. '#title' => st('Site e-mail address'),
  923. '#default_value' => ini_get('sendmail_from'),
  924. '#description' => st("The <em>From</em> address in automated e-mails sent during registration and new password requests, and other notifications. (Use an address ending in your site's domain to help prevent this e-mail being flagged as spam.)"),
  925. '#required' => TRUE,
  926. '#weight' => -15,
  927. );
  928. $form['admin_account'] = array(
  929. '#type' => 'fieldset',
  930. '#title' => st('Administrator account'),
  931. '#collapsible' => FALSE,
  932. );
  933. $form['admin_account']['account']['#tree'] = TRUE;
  934. $form['admin_account']['markup'] = array(
  935. '#value' => '<p class="description">'. st('The administrator account has complete access to the site; it will automatically be granted all permissions and can perform any administrative activity. This will be the only account that can perform certain activities, so keep its credentials safe.') .'</p>',
  936. '#weight' => -10,
  937. );
  938. $form['admin_account']['account']['name'] = array('#type' => 'textfield',
  939. '#title' => st('Username'),
  940. '#maxlength' => USERNAME_MAX_LENGTH,
  941. '#description' => st('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
  942. '#required' => TRUE,
  943. '#weight' => -10,
  944. );
  945. $form['admin_account']['account']['mail'] = array('#type' => 'textfield',
  946. '#title' => st('E-mail address'),
  947. '#maxlength' => EMAIL_MAX_LENGTH,
  948. '#description' => st('All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'),
  949. '#required' => TRUE,
  950. '#weight' => -5,
  951. );
  952. $form['admin_account']['account']['pass'] = array(
  953. '#type' => 'password_confirm',
  954. '#required' => TRUE,
  955. '#size' => 25,
  956. '#weight' => 0,
  957. );
  958. $form['server_settings'] = array(
  959. '#type' => 'fieldset',
  960. '#title' => st('Server settings'),
  961. '#collapsible' => FALSE,
  962. );
  963. $form['server_settings']['date_default_timezone'] = array(
  964. '#type' => 'select',
  965. '#title' => st('Default time zone'),
  966. '#default_value' => 0,
  967. '#options' => _system_zonelist(),
  968. '#description' => st('By default, dates in this site will be displayed in the chosen time zone.'),
  969. '#weight' => 5,
  970. );
  971. $form['server_settings']['clean_url'] = array(
  972. '#type' => 'radios',
  973. '#title' => st('Clean URLs'),
  974. '#default_value' => 0,
  975. '#options' => array(0 => st('Disabled'), 1 => st('Enabled')),
  976. '#description' => st('This option makes Drupal emit "clean" URLs (i.e. without <code>?q=</code> in the URL).'),
  977. '#disabled' => TRUE,
  978. '#prefix' => '<div id="clean-url" class="install">',
  979. '#suffix' => '</div>',
  980. '#weight' => 10,
  981. );
  982. $form['server_settings']['update_status_module'] = array(
  983. '#type' => 'checkboxes',
  984. '#title' => st('Update notifications'),
  985. '#options' => array(1 => st('Check for updates automatically')),
  986. '#default_value' => array(1),
  987. '#description' => st('With this option enabled, Drupal will notify you when new releases are available. This will significantly enhance your site\'s security and is <strong>highly recommended</strong>. This requires your site to periodically send anonymous information on its installed components to <a href="@drupal">drupal.org</a>. For more information please see the <a href="@update">update notification information</a>.', array('@drupal' => 'http://drupal.org', '@update' => 'http://drupal.org/handbook/modules/update')),
  988. '#weight' => 15,
  989. );
  990. $form['submit'] = array(
  991. '#type' => 'submit',
  992. '#value' => st('Save and continue'),
  993. '#weight' => 15,
  994. );
  995. $form['#action'] = $url;
  996. $form['#redirect'] = FALSE;
  997. // Allow the profile to alter this form. $form_state isn't available
  998. // here, but to conform to the hook_form_alter() signature, we pass
  999. // an empty array.
  1000. $hook_form_alter = $_GET['profile'] .'_form_alter';
  1001. if (function_exists($hook_form_alter)) {
  1002. $hook_form_alter($form, array(), 'install_configure');
  1003. }
  1004. return $form;
  1005. }
  1006. /**
  1007. * Form API validate for the site configuration form.
  1008. */
  1009. function install_configure_form_validate($form, &$form_state) {
  1010. if ($error = user_validate_name($form_state['values']['account']['name'])) {
  1011. form_error($form['admin_account']['account']['name'], $error);
  1012. }
  1013. if ($error = user_validate_mail($form_state['values']['account']['mail'])) {
  1014. form_error($form['admin_account']['account']['mail'], $error);
  1015. }
  1016. if ($error = user_validate_mail($form_state['values']['site_mail'])) {
  1017. form_error($form['site_information']['site_mail'], $error);
  1018. }
  1019. }
  1020. /**
  1021. * Form API submit for the site configuration form.
  1022. */
  1023. function install_configure_form_submit($form, &$form_state) {
  1024. global $user;
  1025. variable_set('site_name', $form_state['values']['site_name']);
  1026. variable_set('site_mail', $form_state['values']['site_mail']);
  1027. variable_set('date_default_timezone', $form_state['values']['date_default_timezone']);
  1028. // Enable update.module if this option was selected.
  1029. if ($form_state['values']['update_status_module'][1]) {
  1030. drupal_install_modules(array('update'));
  1031. }
  1032. // Turn this off temporarily so that we can pass a password through.
  1033. variable_set('user_email_verification', FALSE);
  1034. $form_state['old_values'] = $form_state['values'];
  1035. $form_state['values'] = $form_state['values']['account'];
  1036. // We precreated user 1 with placeholder values. Let's save the real values.
  1037. $account = user_load(1);
  1038. $merge_data = array('init' => $form_state['values']['mail'], 'roles' => array(), 'status' => 1);
  1039. user_save($account, array_merge($form_state['values'], $merge_data));
  1040. // Log in the first user.
  1041. user_authenticate($form_state['values']);
  1042. $form_state['values'] = $form_state['old_values'];
  1043. unset($form_state['old_values']);
  1044. variable_set('user_email_verification', TRUE);
  1045. if (isset($form_state['values']['clean_url'])) {
  1046. variable_set('clean_url', $form_state['values']['clean_url']);
  1047. }
  1048. // The user is now logged in, but has no session ID yet, which
  1049. // would be required later in the request, so remember it.
  1050. $user->sid = session_id();
  1051. // Record when this install ran.
  1052. variable_set('install_time', time());
  1053. }
  1054. // Start the installer.
  1055. install_main();