install.php 37.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php
// $Id$

require_once './includes/install.inc';

/**
 * 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() {
18
  global $profile, $install_locale, $conf;
19 20
  require_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
21

22
  require_once './modules/system/system.install';
23
  require_once './includes/file.inc';
24

25 26 27
  // Ensure correct page headers are sent (e.g. caching)
  drupal_page_header();

28 29 30 31
  // Check existing settings.php.
  $verify = install_verify_settings();

  if ($verify) {
32 33 34 35 36 37
    // 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.
    require_once './includes/cache.inc';
    $conf['cache_inc'] = './includes/cache.inc';

38 39 40
    // Establish a connection to the database.
    require_once './includes/database.inc';
    db_set_active();
41

42
    // Check if Drupal is installed.
43 44
    $task = install_verify_drupal();
    if ($task == 'done') {
45 46 47
      install_already_done_error();
    }
  }
48
  else {
49 50 51 52 53 54 55
    // 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.
    require_once './includes/cache-install.inc';
    $conf['cache_inc'] = './includes/cache-install.inc';

56 57
    $task = NULL;
  }
58 59 60 61 62 63 64 65 66 67 68 69 70 71

  // Load module basics (needed for hook invokes).
  include_once './includes/module.inc';
  $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');

  // Decide which profile to use.
  if (!empty($_GET['profile'])) {
    $profile = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['profile']);
  }
  elseif ($profile = install_select_profile()) {
72
    install_goto("install.php?profile=$profile");
73 74
  }
  else {
75
    install_no_profile_error();
76
  }
drumm's avatar
drumm committed
77

78 79 80
  // Load the profile.
  require_once "./profiles/$profile/$profile.profile";

drumm's avatar
drumm committed
81 82 83 84 85 86 87 88
  // Locale selection
  if (!empty($_GET['locale'])) {
    $install_locale = preg_replace('/[^a-zA-Z_0-9]/', '', $_GET['locale']);
  }
  elseif (($install_locale = install_select_locale($profile)) !== FALSE) {
    install_goto("install.php?profile=$profile&locale=$install_locale");
  }

89 90 91 92
  // Tasks come after the database is set up
  if (!$task) {
    // Check the installation requirements for Drupal and this profile.
    install_check_requirements($profile);
93

94 95 96 97 98
    // Verify existence of all required modules.
    $modules = drupal_verify_profile($profile, $install_locale);
    if (!$modules) {
      install_missing_modules_error($profile);
    }
99

100 101 102 103 104
    // 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);
    }
105

106 107
    // Perform actual installation defined in the profile.
    drupal_install_profile($profile, $modules);
108

109
    // Warn about settings.php permissions risk
110 111 112 113
    $settings_dir = './'. conf_path();
    $settings_file = $settings_dir .'/settings.php';
    if (!drupal_verify_install_file($settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file($settings_dir, FILE_NOT_WRITABLE, 'dir')) {
      drupal_set_message(st('All necessary changes to %dir and %file have been made, so you should now remove write permissions to them. Failure to remove write permissions to them is a security risk.', array('%dir' => $settings_dir, '%file' => $settings_file)), 'error');
114 115
    }
    else {
116
      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)));
117
    }
118
  }
119

120 121
  // The database is set up, turn to further tasks.
  install_tasks($profile, $task);
122 123 124 125 126 127
}

/**
 * Verify if Drupal is installed.
 */
function install_verify_drupal() {
128
  // Read the variable manually using the @ so we don't trigger an error if it fails.
129
  $result = @db_query("SELECT value FROM {variable} WHERE name = '%s'", 'install_task');
130 131 132
  if ($result) {
    return unserialize(db_result($result));
  }
133 134 135 136 137 138 139 140 141
}

/**
 * Verify existing settings.php
 */
function install_verify_settings() {
  global $db_prefix, $db_type, $db_url;

  // Verify existing settings (if any).
142
  if (!empty($db_url)) {
143 144 145
    // We need this because we want to run form_get_errors.
    include_once './includes/form.inc';

146
    $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
147 148 149
    $db_user = urldecode($url['user']);
    $db_pass = urldecode($url['pass']);
    $db_host = urldecode($url['host']);
150
    $db_port = isset($url['port']) ? urldecode($url['port']) : '';
151 152 153
    $db_path = ltrim(urldecode($url['path']), '/');
    $settings_file = './'. conf_path() .'/settings.php';

154 155
    $form_state = array();
    _install_settings_form_validate($db_prefix, $db_type, $db_user, $db_pass, $db_host, $db_port, $db_path, $settings_file, $form_state);
156 157 158 159 160 161 162 163 164 165
    if (!form_get_errors()) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Configure and rewrite settings.php.
 */
166 167
function install_change_settings($profile = 'default', $install_locale = '') {
  global $db_url, $db_type, $db_prefix;
168

169
  $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
170 171 172
  $db_user = isset($url['user']) ? urldecode($url['user']) : '';
  $db_pass = isset($url['pass']) ? urldecode($url['pass']) : '';
  $db_host = isset($url['host']) ? urldecode($url['host']) : '';
173
  $db_port = isset($url['port']) ? urldecode($url['port']) : '';
174
  $db_path = ltrim(urldecode($url['path']), '/');
175 176
  $conf_path = './'. conf_path();
  $settings_file = $conf_path .'/settings.php';
177 178 179

  // We always need this because we want to run form_get_errors.
  include_once './includes/form.inc';
180
  drupal_maintenance_theme();
181
  install_task_list('database');
182 183 184

  // The existing database settings are not working, so we need write access
  // to settings.php to change them.
185
  $writable = FALSE;
186 187 188 189 190
  $file = $conf_path;
  // Verify the directory exists.
  if (drupal_verify_install_file($conf_path, FILE_EXIST, 'dir')) {
    // Check to see if a settings.php already exists
    if (drupal_verify_install_file($settings_file, FILE_EXIST)) {
191 192
      // If it does, make sure it is writable
      $writable = drupal_verify_install_file($settings_file, FILE_READABLE|FILE_WRITABLE);
193 194 195 196
      $file = $settings_file;
    }
    else {
      // If not, makes sure the directory is.
197
      $writable = drupal_verify_install_file($conf_path, FILE_READABLE|FILE_WRITABLE, 'dir');
198 199 200
    }
  }

201
  if (!$writable) {
202
    drupal_set_message(st('The @drupal installer requires write permissions to %file during the installation process.', array('@drupal' => drupal_install_profile_name(), '%file' => $file)), 'error');
203

drumm's avatar
drumm committed
204
    drupal_set_title(st('Drupal database setup'));
205 206 207 208
    print theme('install_page', '');
    exit;
  }

209
  $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);
drumm's avatar
drumm committed
210
  drupal_set_title(st('Database configuration'));
211 212 213
  print theme('install_page', $output);
  exit;
}
214

215

216 217 218
/**
 * Form API array definition for install_settings.
 */
219
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) {
220 221 222
  if (empty($db_host)) {
    $db_host = 'localhost';
  }
223
  $db_types = drupal_detect_database_types();
224 225 226 227 228 229

  // If both 'mysql' and 'mysqli' are available, we disable 'mysql':
  if (isset($db_types['mysqli'])) {
    unset($db_types['mysql']);
  }

230 231
  if (count($db_types) == 0) {
    $form['no_db_types'] = array(
drumm's avatar
drumm committed
232
      '#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')),
233 234 235 236 237
    );
  }
  else {
    $form['basic_options'] = array(
      '#type' => 'fieldset',
drumm's avatar
drumm committed
238
      '#title' => st('Basic options'),
239
      '#description' => '<p>'. st('To set up your @drupal database, enter the following information.', array('@drupal' => drupal_install_profile_name())) .'</p>',
240 241 242 243 244
    );

    if (count($db_types) > 1) {
      $form['basic_options']['db_type'] = array(
        '#type' => 'radios',
drumm's avatar
drumm committed
245
        '#title' => st('Database type'),
246
        '#required' => TRUE,
247 248
        '#options' => $db_types,
        '#default_value' => ($db_type ? $db_type : current($db_types)),
249
        '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_name())),
250
      );
251
      $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()));
252 253 254
    }
    else {
      if (count($db_types) == 1) {
255
        $db_types = array_values($db_types);
256 257 258 259
        $form['basic_options']['db_type'] = array(
          '#type' => 'hidden',
          '#value' => $db_types[0],
        );
260
        $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()));
261 262 263 264 265 266
      }
    }

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

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

    // Database username
    $form['basic_options']['db_pass'] = array(
      '#type' => 'password',
drumm's avatar
drumm committed
288
      '#title' => st('Database password'),
289 290 291 292 293 294 295
      '#default_value' => $db_pass,
      '#size' => 45,
      '#maxlength' => 45,
    );

    $form['advanced_options'] = array(
      '#type' => 'fieldset',
drumm's avatar
drumm committed
296
      '#title' => st('Advanced options'),
297 298
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
299
      '#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.")
300 301 302 303 304
    );

    // Database host
    $form['advanced_options']['db_host'] = array(
      '#type' => 'textfield',
drumm's avatar
drumm committed
305
      '#title' => st('Database host'),
306 307 308 309
      '#default_value' => $db_host,
      '#size' => 45,
      '#maxlength' => 45,
      '#required' => TRUE,
drumm's avatar
drumm committed
310
      '#description' => st('If your database is located on a different server, change this.'),
311 312
    );

313 314 315 316 317 318 319 320 321 322
    // Database port
    $form['advanced_options']['db_port'] = array(
      '#type' => 'textfield',
      '#title' => st('Database port'),
      '#default_value' => $db_port,
      '#size' => 45,
      '#maxlength' => 45,
      '#description' => st('If your database server is listening to a non-standard port, enter its number.'),
    );

323
    // Table prefix
324 325
    $form['advanced_options']['db_prefix'] = array(
      '#type' => 'textfield',
326
      '#title' => st('Table prefix'),
327 328 329
      '#default_value' => $db_prefix,
      '#size' => 45,
      '#maxlength' => 45,
330
      '#description' => st('If more than one @drupal website will be sharing this database, enter a table prefix for your @drupal site here.', array('@drupal' => drupal_install_profile_name())),
331 332 333 334
    );

    $form['save'] = array(
      '#type' => 'submit',
drumm's avatar
drumm committed
335
      '#value' => st('Save configuration'),
336 337 338 339 340
    );

    $form['errors'] = array();
    $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
    $form['_db_url'] = array('#type' => 'value');
drumm's avatar
drumm committed
341
    $form['#action'] = "install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : '');
342
    $form['#redirect'] = FALSE;
343
  }
344
  return $form;
345
}
346

347 348 349
/**
 * Form API validate for install_settings form.
 */
350
function install_settings_form_validate($form, &$form_state) {
351
  global $db_url;
352
  _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);
353 354 355 356 357
}

/**
 * Helper function for install_settings_validate.
 */
358
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) {
359 360
  global $db_url;

361
  // Verify the table prefix
362 363
  if (!empty($db_prefix) && is_string($db_prefix) && !preg_match('/^[A-Za-z0-9_.]+$/', $db_prefix)) {
    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, underscores or dots.', array('%db_prefix' => $db_prefix)), 'error');
364 365
  }

366 367 368 369
  if (!empty($db_port) && !is_numeric($db_port)) {
    form_set_error('db_port', st('Database port must be a number.'));
  }

370 371
  // Check database type
  if (!isset($form)) {
372 373
    $_db_url = is_array($db_url) ? $db_url['default'] : $db_url;
    $db_type = substr($_db_url, 0, strpos($_db_url, '://'));
374 375 376
  }
  $databases = drupal_detect_database_types();
  if (!in_array($db_type, $databases)) {
377
    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)));
378 379 380
  }
  else {
    // Verify
381
    $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);
382
    if (isset($form)) {
383
      form_set_value($form['_db_url'], $db_url, $form_state);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    }
    $success = array();

    $function = 'drupal_test_'. $db_type;
    if (!$function($db_url, $success)) {
      if (isset($success['CONNECT'])) {
        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, ', '))));
      }
      else {
        form_set_error('db_type', '');
      }
    }
  }
}

/**
 * Form API submit for install_settings form.
 */
402
function install_settings_form_submit($form, &$form_state) {
drumm's avatar
drumm committed
403
  global $profile, $install_locale;
404 405 406

  // Update global settings array and save
  $settings['db_url'] = array(
407
    'value'    => $form_state['values']['_db_url'],
408 409 410
    'required' => TRUE,
  );
  $settings['db_prefix'] = array(
411
    'value'    => $form_state['values']['db_prefix'],
412 413 414 415 416
    'required' => TRUE,
  );
  drupal_rewrite_settings($settings);

  // Continue to install profile step
drumm's avatar
drumm committed
417
  install_goto("install.php?profile=$profile" . ($install_locale ? "&locale=$install_locale" : ''));
418 419 420
}

/**
421 422 423
 * Find all .profile files.
 */
function install_find_profiles() {
Dries's avatar
Dries committed
424
  return file_scan_directory('./profiles', '\.profile$', array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
425 426 427 428
}

/**
 * Allow admin to select which profile to install.
429 430 431 432 433 434 435
 *
 * @return
 *   The selected profile.
 */
function install_select_profile() {
  include_once './includes/form.inc';

436
  $profiles = install_find_profiles();
437 438 439 440 441 442 443 444
  // 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) {
445
      if ($_POST['profile'] == $profile->name) {
446 447 448
        return $profile->name;
      }
    }
449 450

    drupal_maintenance_theme();
451
    install_task_list('profile-select');
452

drumm's avatar
drumm committed
453
    drupal_set_title(st('Select an installation profile'));
454
    print theme('install_page', drupal_get_form('install_select_profile_form', $profiles));
455 456 457 458
    exit;
  }
}

459
function install_select_profile_form(&$form_state, $profiles) {
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  foreach ($profiles as $profile) {
    include_once($profile->filename);
    // Load profile details.
    $function = $profile->name .'_profile_details';
    if (function_exists($function)) {
      $details = $function();
    }
    // If set, used defined name. Otherwise use file name.
    $name = isset($details['name']) ? $details['name'] : $profile->name;
    $form['profile'][$name] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $profile->name,
      '#title' => $name,
      '#description' => isset($details['description']) ? $details['description'] : '',
      '#parents' => array('profile'),
    );
  }
  $form['submit'] =  array(
    '#type' => 'submit',
drumm's avatar
drumm committed
480 481 482 483 484 485
    '#value' => st('Save configuration'),
  );
  return $form;
}

/**
486 487 488
 * Find all .po files for the current profile.
 */
function install_find_locales($profilename) {
489
  $locales = file_scan_directory('./profiles/'. $profilename .'/translations', '\.po$', array('.', '..', 'CVS'), 0, FALSE);
490 491 492 493 494 495
  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
496 497 498 499 500 501 502 503
 *
 * @return
 *   The selected language.
 */
function install_select_locale($profilename) {
  include_once './includes/file.inc';
  include_once './includes/form.inc';

504 505
  // Find all available locales.
  $locales = install_find_locales($profilename);
drumm's avatar
drumm committed
506

507 508 509 510 511 512 513
  // 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') {
      drupal_maintenance_theme();
514 515 516
      install_task_list('locale-select');
      drupal_set_title(st('Choose language'));
      if (!empty($_GET['localize'])) {
517
        $output = '<p>'. st('With the addition of an appropriate language pack, this installer is capable of proceeding in another language of your choice. To install and use Drupal in a language other than English:') .'</p>';
518 519 520
        $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 pack; each translation pack 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 language pack 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 language packs.') .'</li>';
521 522 523
        $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>';
524 525
      }
      else {
526
        $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 .'&localize=true">'. st('Learn how to install Drupal in other languages') .'</a></li></ul>';
527
      }
528 529 530 531 532
      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
533
    return FALSE;
534 535
  }
  else {
drumm's avatar
drumm committed
536 537 538 539 540 541 542
    foreach ($locales as $locale) {
      if ($_POST['locale'] == $locale->name) {
        return $locale->name;
      }
    }

    drupal_maintenance_theme();
543
    install_task_list('locale-select');
drumm's avatar
drumm committed
544

545
    drupal_set_title(st('Choose language'));
drumm's avatar
drumm committed
546 547 548 549 550
    print theme('install_page', drupal_get_form('install_select_locale_form', $locales));
    exit;
  }
}

551
function install_select_locale_form(&$form_state, $locales) {
drumm's avatar
drumm committed
552
  include_once './includes/locale.inc';
553
  $languages = _locale_get_predefined_list();
drumm's avatar
drumm committed
554 555 556 557
  foreach ($locales as $locale) {
    // Try to use verbose locale name
    $name = $locale->name;
    if (isset($languages[$name])) {
558
      $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' '. st('(@language)', array('@language' => $languages[$name][1])) : '');
drumm's avatar
drumm committed
559 560 561 562 563
    }
    $form['locale'][$locale->name] = array(
      '#type' => 'radio',
      '#return_value' => $locale->name,
      '#default_value' => ($locale->name == 'en' ? TRUE : FALSE),
564
      '#title' => $name . ($locale->name == 'en' ? ' '. st('(built-in)') : ''),
drumm's avatar
drumm committed
565 566 567 568 569
      '#parents' => array('locale')
    );
  }
  $form['submit'] =  array(
    '#type' => 'submit',
570
    '#value' => st('Select language'),
571 572 573 574
  );
  return $form;
}

575 576 577 578 579
/**
 * Show an error page when there are no profiles available.
 */
function install_no_profile_error() {
  drupal_maintenance_theme();
580
  install_task_list('profile-select');
drumm's avatar
drumm committed
581
  drupal_set_title(st('No profiles available'));
582
  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>');
583 584 585 586 587 588 589 590
  exit;
}


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

593
  drupal_maintenance_theme();
drumm's avatar
drumm committed
594
  drupal_set_title(st('Drupal already installed'));
595
  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)));
596 597 598
  exit;
}

599 600 601 602 603 604 605
/**
 * Show an error page when Drupal is missing required modules.
 */
function install_missing_modules_error($profile) {
  global $base_url;

  drupal_maintenance_theme();
606
  install_task_list('requirements');
607 608 609 610 611
  drupal_set_title(st('Modules missing'));
  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>');
  exit;
}

612
/**
613
 * Tasks performed after the database is initialized. Called from install.php.
614
 */
615
function install_tasks($profile, $task) {
616
  global $base_url, $install_locale;
617
  $output = '';
618 619

  // Bootstrap newly installed Drupal, while preserving existing messages.
620
  $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : '';
621 622 623
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  $_SESSION['messages'] = $messages;

624
  // Build a page for a final task.
625
  drupal_maintenance_theme();
626 627 628
  if (empty($task)) {
    variable_set('install_task', 'configure');
    $task = 'configure';
629 630
  }

631 632 633
  // We are using a list of if constructs here to allow for
  // passing from one task to the other in the same request.

634 635 636 637 638
  if ($task == 'configure') {
    drupal_set_title(st('Configure site'));

    // We break the form up so we can tell when it's been successfully
    // submitted.
639 640 641

    $form_state = array('storage' => NULL, 'submitted' => FALSE);

642
    $form = drupal_retrieve_form('install_configure_form', $form_state);
643 644 645
    $form_build_id = md5(mt_rand());
    $form['#build_id'] = $form_build_id;
    drupal_prepare_form('install_configure_form', $form, $form_state);
646 647 648 649

    // In order to find out if the form was successfully submitted or not,
    // we do a little song and dance to set the form to 'programmed' and check
    // to make sure this is really the form being submitted. It'd better be.
650
    if (!empty($_POST) && $_POST['form_id'] == 'install_configure_form') {
651 652 653
      $form['#programmed'] = TRUE;
      $form['#post'] = $_POST;
    }
654 655 656
    else {
      $form['#post'] = array();
    }
657

658 659
    drupal_process_form('install_configure_form', $form, $form_state);
    if (empty($form_state['redirect'])) {
660 661
      // Build menu to allow clean URL check.
      menu_rebuild();
662
      $output = drupal_render_form('install_configure_form', $form);
663 664 665
    }
    else {
      $task = 'profile';
666
    }
667
  }
668

669 670 671 672 673
  // If found an unknown task or the 'profile-custom' task, which is
  // 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())) {
    $function = $profile .'_profile_tasks';
674
    if (function_exists($function)) {
675
      // The profile needs to run more code, maybe even more tasks.
676 677 678 679
      // $task is sent through as a reference and may be changed!
      $output = $function($task);
    }

680 681 682 683 684
    // If the profile doesn't move on to a new task we assume
    // that it is done: we let the installer regain control and
    // proceed with the locale import.
    if ($task == 'profile') {
      $task = 'locale-import';
685
    }
686
  }
687

688 689 690 691 692 693 694 695
  // Import interface translations for the enabled modules, after
  // any changes made by the profile through the profile forms.
  if ($task == 'locale-import') {
    if (!empty($install_locale) && ($install_locale != 'en')) {
      include_once 'includes/locale.inc';
      // 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.
696
      $batch = locale_batch_by_language($install_locale);
697 698 699 700 701 702 703 704
      if (!empty($batch)) {
        // Start a batch, switch to 'locale-batch' task. We need to
        // set the variable here, because batch_process() redirects.
        variable_set('install_task', 'locale-batch');
        batch_set($batch);
        $path = $base_url .'/install.php?locale='. $install_locale .'&profile='. $profile;
        batch_process($path, $path);
      }
705
    }
706
    // Found nothing to import or not foreign language, go to next task.
707
    $task = 'finished';
708
  }
709

710 711 712 713 714 715 716 717 718
  // We are running a batch import of interface translation files.
  // 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 'finished'.
  if ($task == 'locale-batch') {
    include_once 'includes/batch.inc';
    include_once 'includes/locale.inc';
    $output .= _batch_page();
  }
719

720 721 722 723 724 725 726 727
  // Display a 'finished' page to user.
  if ($task == 'finished') {
    drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())));
    $output = '<p>'. st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) .'</p>';
    $messages = drupal_set_message();
    $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>';
    $task = 'done';
  }
728

729 730 731 732 733
  // The end of the install process. Remember profile used.
  if ($task == 'done') {
    // Rebuild menu to get content type links registered by the profile,
    // and possibly any other menu items created through the tasks.
    menu_rebuild();
734 735 736 737

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

738
    variable_set('install_profile', $profile);
739
  }
740 741 742 743 744

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

745 746 747 748
  // Output page.
  print theme('maintenance_page', $output);
}

749 750 751 752 753 754 755
/**
 * The list of reserved tasks to run in the installer.
 */
function install_reserved_tasks() {
  return array('configure', 'locale-import', 'locale-batch', 'finished', 'done');
}

756 757 758 759 760 761 762 763 764 765
/**
 * Page to check installation requirements and report any errors.
 */
function install_check_requirements($profile) {
  $requirements = drupal_check_profile($profile);
  $severity = drupal_requirements_severity($requirements);

  // If there are issues, report them.
  if ($severity == REQUIREMENT_ERROR) {
    drupal_maintenance_theme();
766
    install_task_list('requirements');
767 768 769 770 771 772 773

    foreach ($requirements as $requirement) {
      if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
        drupal_set_message($requirement['description'] .' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')', 'error');
      }
    }

774
    drupal_set_title(st('Incompatible environment'));
775 776 777 778 779
    print theme('install_page', '');
    exit;
  }
}

780 781 782 783 784 785
/**
 * Add the installation task list to the current page.
 */
function install_task_list($active = NULL) {
  // Default list of tasks.
  $tasks = array(
786 787 788 789 790
    'profile-select' => st('Choose profile'),
    'locale-select'  => st('Choose language'),
    'requirements'   => st('Verify requirements'),
    'database'       => st('Setup database'),
    'configure'      => st('Configure site'),
791
  );
Dries's avatar
Dries committed
792

793 794
  $profiles = install_find_profiles();
  $profile = isset($_GET['profile']) && isset($profiles[$_GET['profile']]) ? $_GET['profile'] : '.';
795 796
  $locales = install_find_locales($profile);

797 798 799 800 801
  // Remove select profile if we have only one.
  if (count($profiles) == 1) {
    unset($tasks['profile-select']);
  }

802
  // Add tasks defined by the profile.
803 804 805 806 807 808 809 810 811 812
  if ($profile) {
    $function = $profile .'_profile_task_list';
    if (function_exists($function)) {
      $result = $function();
      if (is_array($result)) {
        $tasks += $result;
      }
    }
  }

813 814 815 816 817 818 819
  // If necessary, add translation import to the task list.
  if (count($locales) > 1 && !empty($_GET['locale']) && $_GET['locale'] != 'en') {
    $tasks += array(
      'locale-batch' => st('Import translations'),
    );
  }

820
  // Add finished step as the last task.
821 822 823 824
  $tasks += array(
    'finished'     => st('Finished')
  );

825 826 827 828 829
  // Let the theming function know that 'finished' and 'done'
  // include everything, so every step is completed.
  if (in_array($active, array('finished', 'done'))) {
    $active = NULL;
  }
830 831 832
  drupal_set_content('left', theme_task_list($tasks, $active));
}

833 834 835 836
function install_configure_form() {
  // This is necessary to add the task to the $_GET args so the install
  // system will know that it is done and we've taken over.

837 838
  _user_password_dynamic_validation();

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  $form['intro'] = array(
    '#value' => st('To configure your web site, please provide the following information.'),
    '#weight' => -10,
  );
  $form['site_information'] = array(
    '#type' => 'fieldset',
    '#title' => st('Site information'),
    '#collapsible' => FALSE,
  );
  $form['site_information']['site_name'] = array(
    '#type' => 'textfield',
    '#title' => st('Site name'),
    '#default_value' => variable_get('site_name', 'Drupal'),
    '#required' => TRUE,
    '#weight' => -20,
  );
  $form['site_information']['site_mail'] = array(
    '#type' => 'textfield',
    '#title' => st('Site e-mail address'),
    '#default_value' => variable_get('site_mail', ini_get('sendmail_from')),
    '#description' => st('A valid e-mail address to be used as the "From" address by the auto-mailer during registration, new password requests, notifications, etc.  To lessen the likelihood of e-mail being marked as spam, this e-mail address should use the same domain as the website.'),
    '#required' => TRUE,
    '#weight' => -15,
  );
  $form['admin_account'] = array(
    '#type' => 'fieldset',
    '#title' => st('Administrator account'),
    '#collapsible' => FALSE,
  );
  $form['admin_account']['account']['#tree'] = TRUE;
  $form['admin_account']['markup'] = array(
    '#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>',
    '#weight' => -10,
  );

  $form['admin_account']['account']['name'] = array('#type' => 'textfield',
    '#title' => st('Username'),
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#description' => st('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
    '#required' => TRUE,
    '#weight' => -10,
  );

  $form['admin_account']['account']['mail'] = array('#type' => 'textfield',
    '#title' => st('E-mail address'),
    '#maxlength' => EMAIL_MAX_LENGTH,
    '#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.'),
    '#required' => TRUE,
    '#weight' => -5,
  );
  $form['admin_account']['account']['pass'] = array(
    '#type' => 'password_confirm',
    '#required' => TRUE,
    '#size' => 25,
    '#weight' => 0,
  );

  $zones = _system_zonelist();

  $form['server_settings'] = array(
    '#type' => 'fieldset',
    '#title' => st('Server settings'),
    '#collapsible' => FALSE,
  );
  $form['server_settings']['date_default_timezone'] = array(
    '#type' => 'select',
    '#title' => st('Default time zone'),
    '#default_value' => variable_get('date_default_timezone', 0),
    '#options' => $zones,
    '#description' => st('By default, dates in this site will be displayed in the chosen time zone.'),
    '#weight' => 5,
  );

  drupal_add_js(drupal_get_path('module', 'system') .'/system.js', 'module');
913 914
  // We add these strings as settings because JavaScript translation does not
  // work on install time.
915 916 917 918 919 920
  drupal_add_js(array('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();
921
    Drupal.setDefaultTimezone();
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
  });
}', 'inline');

  $form['server_settings']['clean_url'] = array(
    '#type' => 'radios',
    '#title' => st('Clean URLs'),
    '#default_value' => variable_get('clean_url', 0),
    '#options' => array(st('Disabled'), st('Enabled')),
    '#description' => st('This option makes Drupal emit "clean" URLs (i.e. without <code>?q=</code> in the URL).'),
    '#disabled' => TRUE,
    '#prefix' => '<div id="clean-url" class="install">',
    '#suffix' => '</div>',
    '#weight' => 10,
  );

  $form['submit'] = array(
    '#type' => 'submit',
939
    '#value' => st('Save'),
940 941 942 943
    '#weight' => 15,
  );
  $hook_form_alter = $_GET['profile'] .'_form_alter';
  if (function_exists($hook_form_alter)) {
944
    $hook_form_alter($form, 'install_configure');
945 946 947 948
  }
  return $form;
}

949 950
function install_configure_form_validate($form, &$form_state) {
  if ($error = user_validate_name($form_state['values']['account']['name'])) {
951 952
    form_error($form['admin_account']['account']['name'], $error);
  }
953
  if ($error = user_validate_mail($form_state['values']['account']['mail'])) {
954 955
    form_error($form['admin_account']['account']['mail'], $error);
  }
956
  if ($error = user_validate_mail($form_state['values']['site_mail'])) {
957 958 959 960
    form_error($form['site_information']['site_mail'], $error);
  }
}

961
function install_configure_form_submit($form, &$form_state) {
962 963
  global $user;

964 965 966 967
  variable_set('site_name', $form_state['values']['site_name']);
  variable_set('site_mail', $form_state['values']['site_mail']);
  variable_set('date_default_timezone', $form_state['values']['date_default_timezone']);

968 969
  // Turn this off temporarily so that we can pass a password through.
  variable_set('user_email_verification', FALSE);
970 971
  $form_state['old_values'] = $form_state['values'];
  $form_state['values'] = $form_state['values']['account'];
972 973 974 975 976 977 978

  // We precreated user 1 with placeholder values. Let's save the real values.
  $account = user_load(1);
  $merge_data = array('init' => $form_state['values']['mail'], 'roles' => array(), 'status' => 1);
  user_save($account, array_merge($form_state['values'], $merge_data));
  // Log in the first user.
  user_authenticate($form_state['values']['name'], trim($form_state['values']['pass']));
979 980
  $form_state['values'] = $form_state['old_values'];
  unset($form_state['old_values']);
981
  variable_set('user_email_verification', TRUE);
982 983 984

  if (isset($form_state['values']['clean_url'])) {
    variable_set('clean_url', $form_state['values']['clean_url']);
985
  }
986 987 988
  // The user is now logged in, but has no session ID yet, which
  // would be required later in the request, so remember it.
  $user->sid = session_id();
989

990
  $form_state['redirect'] = 'finished';
991 992
}

993
install_main();