Imported drupal-5.5
[drupal.git] / install.php
blobe94f763bc2ddfd9bbf751e9c681fc623781d6b93
1 <?php
2 // $Id: install.php,v 1.34.2.4 2007/11/07 08:10:16 drumm Exp $
4 require_once './includes/install.inc';
6 /**
7 * The Drupal installation happens in a series of steps. We begin by verifying
8 * that the current environment meets our minimum requirements. We then go
9 * on to verify that settings.php is properly configured. From there we
10 * connect to the configured database and verify that it meets our minimum
11 * requirements. Finally we can allow the user to select an installation
12 * profile and complete the installation process.
14 * @param $phase
15 * The installation phase we should proceed to.
17 function install_main() {
18 require_once './includes/bootstrap.inc';
19 drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
20 // This must go after drupal_bootstrap(), which unsets globals!
21 global $profile, $install_locale;
22 require_once './modules/system/system.install';
23 require_once './includes/file.inc';
25 // Ensure correct page headers are sent (e.g. caching)
26 drupal_page_header();
28 // Check existing settings.php.
29 $verify = install_verify_settings();
31 // Drupal may already be installed.
32 if ($verify) {
33 // Establish a connection to the database.
34 require_once './includes/database.inc';
35 db_set_active();
36 // Check if Drupal is installed.
37 if (install_verify_drupal()) {
38 install_already_done_error();
42 // Load module basics (needed for hook invokes).
43 include_once './includes/module.inc';
44 $module_list['system']['filename'] = 'modules/system/system.module';
45 $module_list['filter']['filename'] = 'modules/filter/filter.module';
46 module_list(TRUE, FALSE, FALSE, $module_list);
47 drupal_load('module', 'system');
48 drupal_load('module', 'filter');
50 // Decide which profile to use.
51 if (!empty($_GET['profile'])) {
52 $profile = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['profile']);
54 elseif ($profile = install_select_profile()) {
55 install_goto("install.php?profile=$profile");
57 else {
58 install_no_profile_error();
61 // Locale selection
62 if (!empty($_GET['locale'])) {
63 $install_locale = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['locale']);
65 elseif (($install_locale = install_select_locale($profile)) !== FALSE) {
66 install_goto("install.php?profile=$profile&locale=$install_locale");
69 // Load the profile.
70 require_once "./profiles/$profile/$profile.profile";
72 // Check the installation requirements for Drupal and this profile.
73 install_check_requirements($profile);
75 // Change the settings.php information if verification failed earlier.
76 // Note: will trigger a redirect if database credentials change.
77 if (!$verify) {
78 install_change_settings($profile, $install_locale);
81 // Verify existence of all required modules.
82 $modules = drupal_verify_profile($profile, $install_locale);
83 if (!$modules) {
84 install_missing_modules_error($profile);
87 // Perform actual installation defined in the profile.
88 drupal_install_profile($profile, $modules);
90 // Warn about settings.php permissions risk
91 $settings_file = './'. conf_path() .'/settings.php';
92 if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE)) {
93 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');
95 else {
96 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)));
99 // Show end page.
100 install_complete($profile);
104 * Verify if Drupal is installed.
106 function install_verify_drupal() {
107 $result = @db_query("SELECT name FROM {system} WHERE name = 'system'");
108 return $result && db_result($result) == 'system';
112 * Verify existing settings.php
114 function install_verify_settings() {
115 global $db_prefix, $db_type, $db_url;
117 // Verify existing settings (if any).
118 if ($_SERVER['REQUEST_METHOD'] == 'GET' && $db_url != 'mysql://username:password@localhost/databasename') {
119 // We need this because we want to run form_get_errors.
120 include_once './includes/form.inc';
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';
130 _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file);
131 if (!form_get_errors()) {
132 return TRUE;
135 return FALSE;
139 * Configure and rewrite settings.php.
141 function install_change_settings($profile = 'default', $install_locale = '') {
142 global $db_url, $db_type, $db_prefix;
144 $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
145 $db_user = urldecode($url['user']);
146 $db_pass = urldecode($url['pass']);
147 $db_host = urldecode($url['host']);
148 $db_port = isset($url['port']) ? urldecode($url['port']) : '';
149 $db_path = ltrim(urldecode($url['path']), '/');
150 $settings_file = './'. conf_path() .'/settings.php';
152 // We always need this because we want to run form_get_errors.
153 include_once './includes/form.inc';
154 drupal_maintenance_theme();
156 // The existing database settings are not working, so we need write access
157 // to settings.php to change them.
158 if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_WRITABLE)) {
159 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');
161 drupal_set_title(st('Drupal database setup'));
162 print theme('install_page', '');
163 exit;
166 // Don't fill in placeholders
167 if ($db_url == 'mysql://username:password@localhost/databasename') {
168 $db_user = $db_pass = $db_path = '';
170 elseif (!empty($db_url)) {
171 // Do not install over a configured settings.php.
172 install_already_done_error();
174 $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);
175 drupal_set_title(st('Database configuration'));
176 print theme('install_page', $output);
177 exit;
182 * Form API array definition for install_settings.
184 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) {
185 $db_types = drupal_detect_database_types();
186 if (count($db_types) == 0) {
187 $form['no_db_types'] = array(
188 '#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')),
191 else {
192 $form['basic_options'] = array(
193 '#type' => 'fieldset',
194 '#title' => st('Basic options'),
195 '#description' => '<p>'. st('To set up your @drupal database, enter the following information.', array('@drupal' => drupal_install_profile_name())) .'</p>',
198 if (count($db_types) > 1) {
199 // Database type
200 $db_types = drupal_detect_database_types();
201 $form['basic_options']['db_type'] = array(
202 '#type' => 'radios',
203 '#title' => st('Database type'),
204 '#required' => TRUE,
205 '#options' => $db_types,
206 '#default_value' => ($db_type ? $db_type : current($db_types)),
207 '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_name())),
209 $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()));
211 else {
212 if (count($db_types) == 1) {
213 $db_types = array_values($db_types);
214 $form['basic_options']['db_type'] = array(
215 '#type' => 'hidden',
216 '#value' => $db_types[0],
218 $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()));
222 // Database name
223 $form['basic_options']['db_path'] = array(
224 '#type' => 'textfield',
225 '#title' => st('Database name'),
226 '#default_value' => $db_path,
227 '#size' => 45,
228 '#maxlength' => 45,
229 '#required' => TRUE,
230 '#description' => $db_path_description
233 // Database username
234 $form['basic_options']['db_user'] = array(
235 '#type' => 'textfield',
236 '#title' => st('Database username'),
237 '#default_value' => $db_user,
238 '#size' => 45,
239 '#maxlength' => 45,
240 '#required' => TRUE,
243 // Database username
244 $form['basic_options']['db_pass'] = array(
245 '#type' => 'password',
246 '#title' => st('Database password'),
247 '#default_value' => $db_pass,
248 '#size' => 45,
249 '#maxlength' => 45,
252 $form['advanced_options'] = array(
253 '#type' => 'fieldset',
254 '#title' => st('Advanced options'),
255 '#collapsible' => TRUE,
256 '#collapsed' => TRUE,
257 '#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.")
260 // Database host
261 $form['advanced_options']['db_host'] = array(
262 '#type' => 'textfield',
263 '#title' => st('Database host'),
264 '#default_value' => $db_host,
265 '#size' => 45,
266 '#maxlength' => 45,
267 '#required' => TRUE,
268 '#description' => st('If your database is located on a different server, change this.'),
271 // Database port
272 $form['advanced_options']['db_port'] = array(
273 '#type' => 'textfield',
274 '#title' => st('Database port'),
275 '#default_value' => $db_port,
276 '#size' => 45,
277 '#maxlength' => 45,
278 '#description' => st('If your database server is listening to a non-standard port, enter its number.'),
281 // Table prefix
282 $form['advanced_options']['db_prefix'] = array(
283 '#type' => 'textfield',
284 '#title' => st('Table prefix'),
285 '#default_value' => $db_prefix,
286 '#size' => 45,
287 '#maxlength' => 45,
288 '#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())),
291 $form['save'] = array(
292 '#type' => 'submit',
293 '#value' => st('Save configuration'),
296 $form['errors'] = array();
297 $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
298 $form['_db_url'] = array('#type' => 'value');
299 $form['#action'] = "install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : '');
300 $form['#redirect'] = NULL;
302 return $form;
305 * Form API validate for install_settings form.
307 function install_settings_form_validate($form_id, $form_values, $form) {
308 global $db_url;
309 _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);
313 * Helper function for install_settings_validate.
315 function _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file, $form = NULL) {
316 global $db_url;
318 // Check for default username/password
319 if ($db_user == 'username' && $db_pass == 'password') {
320 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())));
323 // Verify the table prefix
324 if (!empty($db_prefix) && is_string($db_prefix) && !preg_match('/^[A-Za-z0-9_]+$/', $db_prefix)) {
325 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');
328 if (!empty($db_port) && !is_numeric($db_port)) {
329 form_set_error('db_port', st('Database port must be a number.'));
332 // Check database type
333 if (!isset($form)) {
334 $_db_url = is_array($db_url) ? $db_url['default'] : $db_url;
335 $db_type = substr($_db_url, 0, strpos($_db_url, '://'));
337 $databases = drupal_detect_database_types();
338 if (!in_array($db_type, $databases)) {
339 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)));
341 else {
342 // Verify
343 $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);
344 if (isset($form)) {
345 form_set_value($form['_db_url'], $db_url);
347 $success = array();
349 $function = 'drupal_test_'. $db_type;
350 if (!$function($db_url, $success)) {
351 if (isset($success['CONNECT'])) {
352 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, ', '))));
354 else {
355 form_set_error('db_type', '');
362 * Form API submit for install_settings form.
364 function install_settings_form_submit($form_id, $form_values) {
365 global $profile, $install_locale;
367 // Update global settings array and save
368 $settings['db_url'] = array(
369 'value' => $form_values['_db_url'],
370 'required' => TRUE,
372 $settings['db_prefix'] = array(
373 'value' => $form_values['db_prefix'],
374 'required' => TRUE,
376 drupal_rewrite_settings($settings);
378 // Continue to install profile step
379 install_goto("install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : ''));
383 * Find all .profile files and allow admin to select which to install.
385 * @return
386 * The selected profile.
388 function install_select_profile() {
389 include_once './includes/form.inc';
391 $profiles = file_scan_directory('./profiles', '\.profile$', array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
392 // Don't need to choose profile if only one available.
393 if (sizeof($profiles) == 1) {
394 $profile = array_pop($profiles);
395 require_once $profile->filename;
396 return $profile->name;
398 elseif (sizeof($profiles) > 1) {
399 foreach ($profiles as $profile) {
400 if ($_POST['profile'] == $profile->name) {
401 return $profile->name;
405 drupal_maintenance_theme();
407 drupal_set_title(st('Select an installation profile'));
408 print theme('install_page', drupal_get_form('install_select_profile_form', $profiles));
409 exit;
413 function install_select_profile_form($profiles) {
414 foreach ($profiles as $profile) {
415 include_once($profile->filename);
416 // Load profile details.
417 $function = $profile->name .'_profile_details';
418 if (function_exists($function)) {
419 $details = $function();
421 // If set, used defined name. Otherwise use file name.
422 $name = isset($details['name']) ? $details['name'] : $profile->name;
423 $form['profile'][$name] = array(
424 '#type' => 'radio',
425 '#value' => 'default',
426 '#return_value' => $profile->name,
427 '#title' => $name,
428 '#description' => isset($details['description']) ? $details['description'] : '',
429 '#parents' => array('profile'),
432 $form['submit'] = array(
433 '#type' => 'submit',
434 '#value' => st('Save configuration'),
436 return $form;
440 * Find all .po files for the current profile and allow admin to select which to use.
442 * @return
443 * The selected language.
445 function install_select_locale($profilename) {
446 include_once './includes/file.inc';
447 include_once './includes/form.inc';
449 // Collect possible locales, add default
450 $locales = file_scan_directory('./profiles/' . $profilename, '\.po$', array('.', '..', 'CVS'), 0, FALSE);
451 array_unshift($locales, (object) array('name' => 'en'));
453 // Don't need to choose locale if only one (English) is available.
454 if (sizeof($locales) == 1) {
455 return FALSE;
456 } else {
457 foreach ($locales as $locale) {
458 if ($_POST['locale'] == $locale->name) {
459 return $locale->name;
463 drupal_maintenance_theme();
465 drupal_set_title(st('Choose your preferred language'));
466 print theme('install_page', drupal_get_form('install_select_locale_form', $locales));
467 exit;
471 function install_select_locale_form($locales) {
472 include_once './includes/locale.inc';
473 $languages = _locale_get_iso639_list();
474 foreach ($locales as $locale) {
475 // Try to use verbose locale name
476 $name = $locale->name;
477 if (isset($languages[$name])) {
478 $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' '. st('(@language)', array('@language' => $languages[$name][1])) : '');
480 $form['locale'][$locale->name] = array(
481 '#type' => 'radio',
482 '#return_value' => $locale->name,
483 '#default_value' => ($locale->name == 'en' ? TRUE : FALSE),
484 '#title' => $name . ($locale->name == 'en' ? ' '. st('(built-in)') : ''),
485 '#parents' => array('locale')
488 $form['submit'] = array(
489 '#type' => 'submit',
490 '#value' => st('Save configuration'),
492 return $form;
496 * Show an error page when there are no profiles available.
498 function install_no_profile_error() {
499 drupal_maintenance_theme();
500 drupal_set_title(st('No profiles available'));
501 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>');
502 exit;
507 * Show an error page when Drupal has already been installed.
509 function install_already_done_error() {
510 global $base_url;
512 drupal_maintenance_theme();
513 drupal_set_title(st('Drupal already installed'));
514 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)));
515 exit;
519 * Show an error page when Drupal is missing required modules.
521 function install_missing_modules_error($profile) {
522 global $base_url;
524 drupal_maintenance_theme();
525 drupal_set_title(st('Modules missing'));
526 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>');
527 exit;
531 * Page displayed when the installation is complete. Called from install.php.
533 function install_complete($profile) {
534 global $base_url;
535 $output = '';
536 // Store install profile for later use.
537 variable_set('install_profile', $profile);
539 // Bootstrap newly installed Drupal, while preserving existing messages.
540 $messages = $_SESSION['messages'];
541 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
542 $_SESSION['messages'] = $messages;
544 // Build final page.
545 drupal_maintenance_theme();
546 drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())));
547 $output .= '<p>'. st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) .'</p>';
549 // Show profile finalization info.
550 $function = $profile .'_profile_final';
551 if (function_exists($function)) {
552 // More steps required
553 $profile_message = $function();
556 // If the profile returned a welcome message, use that instead of default.
557 if (isset($profile_message)) {
558 $output .= $profile_message;
560 else {
561 // No more steps
562 $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>';
564 // Output page.
565 print theme('maintenance_page', $output);
569 * Page to check installation requirements and report any errors.
571 function install_check_requirements($profile) {
572 $requirements = drupal_check_profile($profile);
573 $severity = drupal_requirements_severity($requirements);
575 // If there are issues, report them.
576 if ($severity == REQUIREMENT_ERROR) {
577 drupal_maintenance_theme();
579 foreach ($requirements as $requirement) {
580 if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
581 drupal_set_message($requirement['description'] .' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')', 'error');
585 drupal_set_title(st('Incompatible environment'));
586 print theme('install_page', '');
587 exit;
591 install_main();