[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/ -> install.php (source)

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


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics