install.php 46.1 KB
Newer Older
1 2 3
<?php
// $Id$

4 5 6 7 8 9
/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', dirname(realpath(__FILE__)));

require_once DRUPAL_ROOT . '/includes/install.inc';
10

11 12 13
/**
 * Global flag to indicate that site is in installation mode.
 */
14 15
define('MAINTENANCE_MODE', 'install');

16 17 18 19 20 21 22 23 24 25 26 27
/**
 * The Drupal installation happens in a series of steps. We begin by verifying
 * that the current environment meets our minimum requirements. We then go
 * on to verify that settings.php is properly configured. From there we
 * connect to the configured database and verify that it meets our minimum
 * requirements. Finally we can allow the user to select an installation
 * profile and complete the installation process.
 *
 * @param $phase
 *   The installation phase we should proceed to.
 */
function install_main() {
28 29 30 31
  // The user agent header is used to pass a database prefix in the request when
  // running tests.  However, for security reasons, it is imperative that no
  // installation be permitted using such a prefix.
  if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT'])) {
32
    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
33
    exit;
34 35
  }

36
  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
37
  drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
38

39 40 41
  // This must go after drupal_bootstrap(), which unsets globals!
  global $profile, $install_locale, $conf;

42 43
  require_once DRUPAL_ROOT . '/modules/system/system.install';
  require_once DRUPAL_ROOT . '/includes/file.inc';
44

45 46 47
  // Ensure correct page headers are sent (e.g. caching)
  drupal_page_header();

48 49 50
  // Set up $language, so t() caller functions will still work.
  drupal_init_language();

51
  // Load module basics (needed for hook invokes).
52
  include_once DRUPAL_ROOT . '/includes/module.inc';
53 54 55 56 57 58 59 60 61
  $module_list['system']['filename'] = 'modules/system/system.module';
  $module_list['filter']['filename'] = 'modules/filter/filter.module';
  module_list(TRUE, FALSE, FALSE, $module_list);
  drupal_load('module', 'system');
  drupal_load('module', 'filter');

  // Set up theme system for the maintenance page.
  drupal_maintenance_theme();

62 63 64 65
  // Check existing settings.php.
  $verify = install_verify_settings();

  if ($verify) {
66 67 68
    // Since we have a database connection, we use the normal cache system.
    // This is important, as the installer calls into the Drupal system for
    // the clean URL checks, so we should maintain the cache properly.
69 70
    require_once DRUPAL_ROOT . '/includes/cache.inc';
    $conf['cache_inc'] = 'includes/cache.inc';
71

72 73
    // Initialize the database system.  Note that the connection
    // won't be initialized until it is actually requested.
74
    require_once DRUPAL_ROOT . '/includes/database/database.inc';
75

76
    // Check if Drupal is installed.
77 78
    $task = install_verify_drupal();
    if ($task == 'done') {
79 80 81
      install_already_done_error();
    }
  }
82
  else {
83 84 85 86
    // Since no persistent storage is available yet, and functions that check
    // for cached data will fail, we temporarily replace the normal cache
    // system with a stubbed-out version that short-circuits the actual
    // caching process and avoids any errors.
87 88
    require_once DRUPAL_ROOT . '/includes/cache-install.inc';
    $conf['cache_inc'] = 'includes/cache-install.inc';
89

90 91
    $task = NULL;
  }
92 93 94 95 96 97

  // Decide which profile to use.
  if (!empty($_GET['profile'])) {
    $profile = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['profile']);
  }
  elseif ($profile = install_select_profile()) {
98
    install_goto("install.php?profile=$profile");
99 100
  }
  else {
101
    install_no_profile_error();
102
  }
drumm's avatar
drumm committed
103

104
  // Load the profile.
105
  require_once DRUPAL_ROOT . "/profiles/$profile/$profile.profile";
106

drumm's avatar
drumm committed
107 108
  // Locale selection
  if (!empty($_GET['locale'])) {
109
    $install_locale = preg_replace('/[^a-zA-Z_0-9\-]/', '', $_GET['locale']);
drumm's avatar
drumm committed
110 111 112 113 114
  }
  elseif (($install_locale = install_select_locale($profile)) !== FALSE) {
    install_goto("install.php?profile=$profile&locale=$install_locale");
  }

115 116
  // Tasks come after the database is set up
  if (!$task) {
117 118 119 120 121 122 123
    global $db_url;

    if (!$verify && !empty($db_url)) {
      // Do not install over a configured settings.php.
      install_already_done_error();
    }

124
    // Check the installation requirements for Drupal and this profile.
125
    $requirements = install_check_requirements($profile, $verify);
126

127
    // Verify existence of all required modules.
128
    $requirements += drupal_verify_profile($profile, $install_locale);
129

130 131 132 133
    // Check the severity of the requirements reported.
    $severity = drupal_requirements_severity($requirements);

    if ($severity == REQUIREMENT_ERROR) {
134 135
      install_task_list('requirements');
      drupal_set_title(st('Requirements problem'));
136 137 138
      $status_report = theme('status_report', $requirements);
      $status_report .= st('Please check the error messages and <a href="!url">try again</a>.', array('!url' => request_uri()));
      print theme('install_page', $status_report);
139
      exit;
140
    }
141

142 143 144 145 146
    // Change the settings.php information if verification failed earlier.
    // Note: will trigger a redirect if database credentials change.
    if (!$verify) {
      install_change_settings($profile, $install_locale);
    }
147

148 149 150 151 152
    // Install system.module.
    drupal_install_system();
    // Save the list of other modules to install for the 'profile-install'
    // task. variable_set() can be used now that system.module is installed
    // and drupal is bootstrapped.
153
    $modules = drupal_get_profile_modules($profile, $install_locale);
154
    variable_set('install_profile_modules', array_diff($modules, array('system')));
155
  }
156

157 158
  // The database is set up, turn to further tasks.
  install_tasks($profile, $task);
159 160 161 162 163 164
}

/**
 * Verify if Drupal is installed.
 */
function install_verify_drupal() {
165
  // Read the variable manually using the @ so we don't trigger an error if it fails.
166 167 168 169 170 171
  try {
    if ($result = db_query("SELECT value FROM {variable} WHERE name = '%s'", 'install_task')) {
      return unserialize(db_result($result));
    }
  }
  catch (Exception $e) {
172
  }
173 174 175 176 177 178
}

/**
 * Verify existing settings.php
 */
function install_verify_settings() {
179
  global $db_prefix, $databases;
180 181

  // Verify existing settings (if any).
182
  if (!empty($databases)) {
183
    // We need this because we want to run form_get_errors.
184
    include_once DRUPAL_ROOT . '/includes/form.inc';
185

186
    $database = $databases['default']['default'];
187
    $settings_file = './' . conf_path(FALSE, TRUE) . '/settings.php';
188

189
    $form_state = array();
190
    _install_settings_form_validate($database, $settings_file, $form_state);
191 192 193 194 195 196 197 198 199 200
    if (!form_get_errors()) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Configure and rewrite settings.php.
 */
201
function install_change_settings($profile = 'default', $install_locale = '') {
202 203
  global $databases, $db_prefix;

204 205
  $conf_path = './' . conf_path(FALSE, TRUE);
  $settings_file = $conf_path . '/settings.php';
206 207
  $database = isset($databases['default']['default']) ? $databases['default']['default'] : array();

208
  // We always need this because we want to run form_get_errors.
209
  include_once DRUPAL_ROOT . '/includes/form.inc';
210
  install_task_list('database');
211

212
  $output = drupal_get_form('install_settings_form', $profile, $install_locale, $settings_file, $database);
drumm's avatar
drumm committed
213
  drupal_set_title(st('Database configuration'));
214 215 216
  print theme('install_page', $output);
  exit;
}
217

218

219 220 221
/**
 * Form API array definition for install_settings.
 */
222 223
function install_settings_form(&$form_state, $profile, $install_locale, $settings_file, $database) {
  $drivers = drupal_detect_database_types();
224

225 226
  if (!$drivers) {
    $form['no_drivers'] = array(
227
      '#markup' => 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')),
228 229 230 231 232
    );
  }
  else {
    $form['basic_options'] = array(
      '#type' => 'fieldset',
drumm's avatar
drumm committed
233
      '#title' => st('Basic options'),
234
      '#description' => '<p>' . st('To set up your @drupal database, enter the following information.', array('@drupal' => drupal_install_profile_name())) . '</p>',
235 236
    );

237 238 239 240 241 242 243 244 245
    if (count($drivers) == 1) {
      $form['basic_options']['driver'] = array(
        '#type' => 'hidden',
        '#value' => current(array_keys($drivers)),
      );
      $database_description = st('The name of the %driver database your @drupal data will be stored in. It must exist on your server before @drupal can be installed.', array('%driver' => current($drivers), '@drupal' => drupal_install_profile_name()));
    }
    else  {
      $form['basic_options']['driver'] = array(
246
        '#type' => 'radios',
247
        '#title' => st('Database driver'),
248
        '#required' => TRUE,
249
        '#options' => $drivers,
250
        '#default_value' => !empty($database['driver']) ? $database['driver'] : current(array_keys($drivers)),
251
        '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_name())),
252
      );
253
      $database_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()));
254 255 256
    }

    // Database name
257
    $form['basic_options']['database'] = array(
258
      '#type' => 'textfield',
drumm's avatar
drumm committed
259
      '#title' => st('Database name'),
260
      '#default_value' => empty($database['database']) ? '' : $database['database'],
261 262 263
      '#size' => 45,
      '#maxlength' => 45,
      '#required' => TRUE,
264
      '#description' => $database_description,
265 266 267
    );

    // Database username
268
    $form['basic_options']['username'] = array(
269
      '#type' => 'textfield',
drumm's avatar
drumm committed
270
      '#title' => st('Database username'),
271
      '#default_value' => empty($database['username']) ? '' : $database['username'],
272 273 274 275 276 277
      '#size' => 45,
      '#maxlength' => 45,
      '#required' => TRUE,
    );

    // Database username
278
    $form['basic_options']['password'] = array(
279
      '#type' => 'password',
drumm's avatar
drumm committed
280
      '#title' => st('Database password'),
281
      '#default_value' => empty($database['password']) ? '' : $database['password'],
282 283 284 285 286 287
      '#size' => 45,
      '#maxlength' => 45,
    );

    $form['advanced_options'] = array(
      '#type' => 'fieldset',
drumm's avatar
drumm committed
288
      '#title' => st('Advanced options'),
289 290
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
291
      '#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.")
292 293 294
    );

    // Database host
295
    $form['advanced_options']['host'] = array(
296
      '#type' => 'textfield',
drumm's avatar
drumm committed
297
      '#title' => st('Database host'),
298
      '#default_value' => empty($database['host']) ? 'localhost' : $database['host'],
299 300 301
      '#size' => 45,
      '#maxlength' => 45,
      '#required' => TRUE,
drumm's avatar
drumm committed
302
      '#description' => st('If your database is located on a different server, change this.'),
303 304
    );

305
    // Database port
306
    $form['advanced_options']['port'] = array(
307 308
      '#type' => 'textfield',
      '#title' => st('Database port'),
309
      '#default_value' => empty($database['port']) ? '' : $database['port'],
310 311 312 313 314
      '#size' => 45,
      '#maxlength' => 45,
      '#description' => st('If your database server is listening to a non-standard port, enter its number.'),
    );

315
    // Table prefix
316
    $db_prefix = ($profile == 'default') ? 'drupal_' : $profile . '_';
317 318
    $form['advanced_options']['db_prefix'] = array(
      '#type' => 'textfield',
319
      '#title' => st('Table prefix'),
320
      '#default_value' => '',
321 322
      '#size' => 45,
      '#maxlength' => 45,
323
      '#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' => $db_prefix)),
324 325 326 327
    );

    $form['save'] = array(
      '#type' => 'submit',
328
      '#value' => st('Save and continue'),
329 330 331 332
    );

    $form['errors'] = array();
    $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
333
    $form['_database'] = array('#type' => 'value');
334
    $form['#action'] = "install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : '');
335
    $form['#redirect'] = FALSE;
336
  }
337
  return $form;
338
}
339

340 341 342
/**
 * Form API validate for install_settings form.
 */
343
function install_settings_form_validate($form, &$form_state) {
344
  global $db_url;
345
  _install_settings_form_validate($form_state['values'], $form_state['values']['settings_file'], $form_state, $form);
346 347 348 349 350
}

/**
 * Helper function for install_settings_validate.
 */
351 352
function _install_settings_form_validate($database, $settings_file, &$form_state, $form = NULL) {
  global $databases;
353
  // Verify the table prefix
354
  if (!empty($database['prefix']) && is_string($database['prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $database['dprefix'])) {
355
    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');
356 357
  }

358
  if (!empty($database['port']) && !is_numeric($database['port'])) {
359 360 361
    form_set_error('db_port', st('Database port must be a number.'));
  }

362
  // Check database type
363 364 365 366
  $database_types = drupal_detect_database_types();
  $driver = $database['driver'];
  if (!isset($database_types[$driver])) {
    form_set_error('driver', st("In your %settings_file file you have configured @drupal to use a %driver server, however your PHP installation currently does not support this database type.", array('%settings_file' => $settings_file, '@drupal' => drupal_install_profile_name(), '%driver' => $database['driver'])));
367 368 369
  }
  else {
    if (isset($form)) {
370
      form_set_value($form['_database'], $database, $form_state);
371
    }
372 373 374 375 376 377 378
    $class = "DatabaseInstaller_$driver";
    $test = new $class;
    $databases = array('default' => array('default' => $database));
    $return = $test->test();
    if (!$return || $test->error) {
      if (!empty($test->success)) {
        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($test->success, ', '))));
379 380
      }
      else {
381
        form_set_error('driver', '');
382 383 384 385 386 387 388 389
      }
    }
  }
}

/**
 * Form API submit for install_settings form.
 */
390
function install_settings_form_submit($form, &$form_state) {
drumm's avatar
drumm committed
391
  global $profile, $install_locale;
392

393
  $database = array_intersect_key($form_state['values']['_database'], array_flip(array('driver', 'database', 'username', 'password', 'host', 'port')));
394
  // Update global settings array and save
395 396
  $settings['databases'] = array(
    'value'    => array('default' => array('default' => $database)),
397 398 399
    'required' => TRUE,
  );
  $settings['db_prefix'] = array(
400
    'value'    => $form_state['values']['db_prefix'],
401 402 403 404 405
    'required' => TRUE,
  );
  drupal_rewrite_settings($settings);

  // Continue to install profile step
406
  install_goto("install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : ''));
407 408 409
}

/**
410 411 412
 * Find all .profile files.
 */
function install_find_profiles() {
413
  return file_scan_directory('./profiles', '/\.profile$/', array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
414 415 416 417
}

/**
 * Allow admin to select which profile to install.
418 419 420 421 422
 *
 * @return
 *   The selected profile.
 */
function install_select_profile() {
423
  include_once DRUPAL_ROOT . '/includes/form.inc';
424

425
  $profiles = install_find_profiles();
426 427 428 429 430 431 432 433
  // Don't need to choose profile if only one available.
  if (sizeof($profiles) == 1) {
    $profile = array_pop($profiles);
    require_once $profile->filename;
    return $profile->name;
  }
  elseif (sizeof($profiles) > 1) {
    foreach ($profiles as $profile) {
434
      if (!empty($_POST['profile']) && ($_POST['profile'] == $profile->name)) {
435 436 437
        return $profile->name;
      }
    }
438

439
    install_task_list('profile-select');
440

drumm's avatar
drumm committed
441
    drupal_set_title(st('Select an installation profile'));
442
    print theme('install_page', drupal_get_form('install_select_profile_form', $profiles));
443 444 445 446
    exit;
  }
}

447 448
/**
 * Form API array definition for the profile selection form.
449 450 451 452 453
 *
 * @param $form_state
 *   Array of metadata about state of form processing.
 * @param $profile_files
 *   Array of .profile files, as returned from file_scan_directory().
454
 */
455 456 457 458 459
function install_select_profile_form(&$form_state, $profile_files) {
  $profiles = array();
  $names = array();

  foreach ($profile_files as $profile) {
460
    include_once DRUPAL_ROOT . '/' . $profile->filename;
461 462

    // Load profile details and store them for later retrieval.
463
    $function = $profile->name . '_profile_details';
464 465 466
    if (function_exists($function)) {
      $details = $function();
    }
467 468 469 470
    $profiles[$profile->name] = $details;

    // Determine the name of the profile; default to file name if defined name
    // is unspecified.
471
    $name = isset($details['name']) ? $details['name'] : $profile->name;
472 473 474 475 476 477 478
    $names[$profile->name] = $name;
  }

  // Display radio buttons alphabetically by human-readable name.
  natcasesort($names);

  foreach ($names as $profile => $name) {
479 480 481
    $form['profile'][$name] = array(
      '#type' => 'radio',
      '#value' => 'default',
482
      '#return_value' => $profile,
483
      '#title' => $name,
484
      '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
485 486 487 488 489
      '#parents' => array('profile'),
    );
  }
  $form['submit'] =  array(
    '#type' => 'submit',
490
    '#value' => st('Save and continue'),
drumm's avatar
drumm committed
491 492 493 494 495
  );
  return $form;
}

/**
496 497 498
 * Find all .po files for the current profile.
 */
function install_find_locales($profilename) {
499
  $locales = file_scan_directory('./profiles/' . $profilename . '/translations', '/\.po$/', array('.', '..', 'CVS'), 0, FALSE);
500 501 502 503 504 505
  array_unshift($locales, (object) array('name' => 'en'));
  return $locales;
}

/**
 * Allow admin to select which locale to use for the current profile.
drumm's avatar
drumm committed
506 507 508 509 510
 *
 * @return
 *   The selected language.
 */
function install_select_locale($profilename) {
511 512
  include_once DRUPAL_ROOT . '/includes/file.inc';
  include_once DRUPAL_ROOT . '/includes/form.inc';
drumm's avatar
drumm committed
513

514 515
  // Find all available locales.
  $locales = install_find_locales($profilename);
drumm's avatar
drumm committed
516

517 518 519 520 521 522
  // If only the built-in (English) language is available,
  // and we are using the default profile, inform the user
  // that the installer can be localized. Otherwise we assume
  // the user know what he is doing.
  if (count($locales) == 1) {
    if ($profilename == 'default') {
523 524 525
      install_task_list('locale-select');
      drupal_set_title(st('Choose language'));
      if (!empty($_GET['localize'])) {
526 527 528 529 530 531 532
        $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>';
        $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://drupal.org/project/translations')) . '</li>';
        $output .= '<li>' . st('If an alternative translation package of your choice is available, download and extract its contents to your Drupal root directory.') . '</li>';
        $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>';
        $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>';
        $output .= '<p>' . st('How should the installation continue?') . '</p>';
        $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>';
533 534
      }
      else {
535
        $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>';
536
      }
537 538 539 540 541
      print theme('install_page', $output);
      exit;
    }
    // One language, but not the default profile, assume
    // the user knows what he is doing.
drumm's avatar
drumm committed
542
    return FALSE;
543 544
  }
  else {
545
    // Allow profile to pre-select the language, skipping the selection.
546
    $function = $profilename . '_profile_details';
547 548 549 550 551 552 553 554 555 556 557
    if (function_exists($function)) {
      $details = $function();
      if (isset($details['language'])) {
        foreach ($locales as $locale) {
          if ($details['language'] == $locale->name) {
            return $locale->name;
          }
        }
      }
    }

drumm's avatar
drumm committed
558 559 560 561 562 563
    foreach ($locales as $locale) {
      if ($_POST['locale'] == $locale->name) {
        return $locale->name;
      }
    }

564
    install_task_list('locale-select');
drumm's avatar
drumm committed
565

566
    drupal_set_title(st('Choose language'));
drumm's avatar
drumm committed
567 568 569 570 571
    print theme('install_page', drupal_get_form('install_select_locale_form', $locales));
    exit;
  }
}

572 573 574
/**
 * Form API array definition for language selection.
 */
575
function install_select_locale_form(&$form_state, $locales) {
576
  include_once DRUPAL_ROOT . '/includes/locale.inc';
577
  $languages = _locale_get_predefined_list();
drumm's avatar
drumm committed
578 579 580 581
  foreach ($locales as $locale) {
    // Try to use verbose locale name
    $name = $locale->name;
    if (isset($languages[$name])) {
582
      $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' ' . st('(@language)', array('@language' => $languages[$name][1])) : '');
drumm's avatar
drumm committed
583 584 585 586 587
    }
    $form['locale'][$locale->name] = array(
      '#type' => 'radio',
      '#return_value' => $locale->name,
      '#default_value' => ($locale->name == 'en' ? TRUE : FALSE),
588
      '#title' => $name . ($locale->name == 'en' ? ' ' . st('(built-in)') : ''),
drumm's avatar
drumm committed
589 590 591 592 593
      '#parents' => array('locale')
    );
  }
  $form['submit'] =  array(
    '#type' => 'submit',
594
    '#value' => st('Select language'),
595 596 597 598
  );
  return $form;
}

599 600 601 602
/**
 * Show an error page when there are no profiles available.
 */
function install_no_profile_error() {
603
  install_task_list('profile-select');
drumm's avatar
drumm committed
604
  drupal_set_title(st('No profiles available'));
605
  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>');
606 607 608 609 610 611 612 613
  exit;
}


/**
 * Show an error page when Drupal has already been installed.
 */
function install_already_done_error() {
614 615
  global $base_url;

drumm's avatar
drumm committed
616
  drupal_set_title(st('Drupal already installed'));
617
  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)));
618 619 620 621
  exit;
}

/**
622
 * Tasks performed after the database is initialized.
623
 */
624
function install_tasks($profile, $task) {
625
  global $base_url, $install_locale;
626 627

  // Bootstrap newly installed Drupal, while preserving existing messages.
628
  $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : '';
629
  drupal_install_init_database();
630

631 632 633
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  $_SESSION['messages'] = $messages;

634
  // URL used to direct page requests.
635
  $url = $base_url . '/install.php?locale=' . $install_locale . '&profile=' . $profile;
636 637

  // Build a page for final tasks.
638
  if (empty($task)) {
639 640
    variable_set('install_task', 'profile-install');
    $task = 'profile-install';
641 642
  }

643 644 645
  // We are using a list of if constructs here to allow for
  // passing from one task to the other in the same request.

646 647 648 649 650 651 652 653 654 655 656 657
  // Install profile modules.
  if ($task == 'profile-install') {
    $modules = variable_get('install_profile_modules', array());
    $files = module_rebuild_cache();
    variable_del('install_profile_modules');
    $operations = array();
    foreach ($modules as $module) {
      $operations[] = array('_install_module_batch', array($module, $files[$module]->info['name']));
    }
    $batch = array(
      'operations' => $operations,
      'finished' => '_install_profile_batch_finished',
658 659
      'title' => st('Installing @drupal', array('@drupal' => drupal_install_profile_name())),
      'error_message' => st('The installation has encountered an error.'),
660 661 662 663 664 665 666 667 668 669 670 671
    );
    // Start a batch, switch to 'profile-install-batch' task. We need to
    // set the variable here, because batch_process() redirects.
    variable_set('install_task', 'profile-install-batch');
    batch_set($batch);
    batch_process($url, $url);
  }
  // We are running a batch install of the profile's modules.
  // This might run in multiple HTTP requests, constantly redirecting
  // to the same address, until the batch finished callback is invoked
  // and the task advances to 'locale-initial-import'.
  if ($task == 'profile-install-batch') {
672
    include_once DRUPAL_ROOT .'/includes/batch.inc';
673 674 675
    $output = _batch_page();
  }

676 677 678
  // Import interface translations for the enabled modules.
  if ($task == 'locale-initial-import') {
    if (!empty($install_locale) && ($install_locale != 'en')) {
679
      include_once DRUPAL_ROOT . '/includes/locale.inc';
680 681 682 683 684 685
      // Enable installation language as default site language.
      locale_add_language($install_locale, NULL, NULL, NULL, NULL, NULL, 1, TRUE);
      // Collect files to import for this language.
      $batch = locale_batch_by_language($install_locale, '_install_locale_initial_batch_finished');
      if (!empty($batch)) {
        // Remember components we cover in this batch set.
686
        variable_set('install_locale_batch_components', $batch['#components']);
687 688 689 690 691 692 693 694 695 696 697
        // Start a batch, switch to 'locale-batch' task. We need to
        // set the variable here, because batch_process() redirects.
        variable_set('install_task', 'locale-initial-batch');
        batch_set($batch);
        batch_process($url, $url);
      }
    }
    // Found nothing to import or not foreign language, go to next task.
    $task = 'configure';
  }
  if ($task == 'locale-initial-batch') {
698 699
    include_once DRUPAL_ROOT . '/includes/batch.inc';
    include_once DRUPAL_ROOT . '/includes/locale.inc';
700 701
    $output = _batch_page();
  }
702

703 704 705 706 707 708
  if ($task == 'configure') {
    if (variable_get('site_name', FALSE) || variable_get('site_mail', FALSE)) {
      // Site already configured: This should never happen, means re-running
      // the installer, possibly by an attacker after the 'install_task' variable
      // got accidentally blown somewhere. Stop it now.
      install_already_done_error();
709
    }
710 711 712 713 714 715
    $form = drupal_get_form('install_configure_form', $url);

    if (!variable_get('site_name', FALSE) && !variable_get('site_mail', FALSE)) {
      // Not submitted yet: Prepare to display the form.
      $output = $form;
      drupal_set_title(st('Configure site'));
716

717
      // Warn about settings.php permissions risk
718 719
      $settings_dir = './' . conf_path();
      $settings_file = $settings_dir . '/settings.php';
720
      if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file($settings_dir, FILE_NOT_WRITABLE, 'dir')) {
721
        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">online handbook</a>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/getting-started')), 'error');
722 723 724 725 726
      }
      else {
        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)));
      }

727
      // Add JavaScript validation.
728
      _user_password_dynamic_validation();
729
      drupal_add_js(drupal_get_path('module', 'system') . '/system.js', 'module');
730 731 732 733 734 735 736 737 738 739 740
      // We add these strings as settings because JavaScript translation does not
      // work on install time.
      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');
      drupal_add_js('
// Global Killswitch
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    Drupal.cleanURLsInstallCheck();
    Drupal.setDefaultTimezone();
  });
}', 'inline');
741 742
      // Build menu to allow clean URL check.
      menu_rebuild();
743 744 745 746 747 748 749 750 751 752 753

      // Cache a fully-built schema.  This is necessary for any
      // invocation of index.php because: (1) setting cache table
      // entries requires schema information, (2) that occurs during
      // bootstrap before any module are loaded, so (3) if there is no
      // cached schema, drupal_get_schema() will try to generate one
      // but with no loaded modules will return nothing.
      //
      // This logically could be done during task 'done' but the clean
      // URL check requires it now.
      drupal_get_schema(NULL, TRUE);
754
    }
755

756 757
    else {
      $task = 'profile';
758
    }
759
  }
760

761
  // If found an unknown task or the 'profile' task, which is
762 763 764
  // reserved for profiles, hand over the control to the profile,
  // so it can run any number of custom tasks it defines.
  if (!in_array($task, install_reserved_tasks())) {
765
    $function = $profile . '_profile_tasks';
766
    if (function_exists($function)) {
767
      // The profile needs to run more code, maybe even more tasks.
768
      // $task is sent through as a reference and may be changed!
769
      $output = $function($task, $url);
770 771
    }

772
    // If the profile doesn't move on to a new task we assume
773
    // that it is done.
774
    if ($task == 'profile') {
775
      $task = 'profile-finished';
776
    }
777
  }
778

779 780 781
  // Profile custom tasks are done, so let the installer regain
  // control and proceed with importing the remaining translations.
  if ($task == 'profile-finished') {
782
    if (!empty($install_locale) && ($install_locale != 'en')) {
783
      include_once DRUPAL_ROOT . '/includes/locale.inc';
784 785 786 787 788
      // Collect files to import for this language. Skip components
      // already covered in the initial batch set.
      $batch = locale_batch_by_language($install_locale, '_install_locale_remaining_batch_finished', variable_get('install_locale_batch_components', array()));
      // Remove temporary variable.
      variable_del('install_locale_batch_components');
789
      if (!empty($batch)) {
790
        // Start a batch, switch to 'locale-remaining-batch' task. We need to
791
        // set the variable here, because batch_process() redirects.
792
        variable_set('install_task', 'locale-remaining-batch');
793
        batch_set($batch);
794
        batch_process($url, $url);
795
      }
796
    }
797
    // Found nothing to import or not foreign language, go to next task.
798
    $task = 'finished';
799
  }
800
  if ($task == 'locale-remaining-batch') {
801 802
    include_once DRUPAL_ROOT . '/includes/batch.inc';
    include_once DRUPAL_ROOT . '/includes/locale.inc';
803
    $output = _batch_page();
804
  }
805

806 807 808 809
  // Display a 'finished' page to user.
  if ($task == 'finished') {
    drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())));
    $messages = drupal_set_message();
810 811
    $output = '<p>' . st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) . '</p>';
    $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>';
812 813
    $task = 'done';
  }
814

815 816
  // The end of the install process. Remember profile used.
  if ($task == 'done') {
817 818
    // Rebuild menu and registry to get content type links registered by the
    // profile, and possibly any other menu items created through the tasks.
819
    menu_rebuild();
820 821 822 823

    // Register actions declared by any modules.
    actions_synchronize();

824 825 826 827
    // Randomize query-strings on css/js files, to hide the fact that
    // this is a new install, not upgraded yet.
    _drupal_flush_css_js();

828
    variable_set('install_profile', $profile);
829 830 831

    // Cache a fully-built schema.
    drupal_get_schema(NULL, TRUE);
832
  }
833 834 835 836 837

  // Set task for user, and remember the task in the database.
  install_task_list($task);
  variable_set('install_task', $task);

838 839 840 841 842
  // Output page, if some output was required. Otherwise it is possible
  // that we are printing a JSON page and theme output should not be there.
  if (isset($output)) {
    print theme('maintenance_page', $output);
  }
843 844
}

845 846 847 848 849 850 851 852 853 854 855
/**
 * Batch callback for batch installation of modules.
 */
function _install_module_batch($module, $module_name, &$context) {
  _drupal_install_module($module);
  // We enable the installed module right away, so that the module will be
  // loaded by drupal_bootstrap in subsequent batch requests, and other
  // modules possibly depending on it can safely perform their installation
  // steps.
  module_enable(array($module));
  $context['results'][] = $module;
856
  $context['message'] = 'Installed ' . $module_name . ' module.';
857 858 859 860 861