install_7.inc 5.86 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 *  @file
 *    Rebuild all the caches
 */

7 8 9 10
$GLOBALS['url'] = d()->uri;
$GLOBALS['profile'] = d()->profile;
$GLOBALS['install_locale'] = d()->language;
$GLOBALS['base_url'] = provision_get_base_url();
11

12
require_once 'install.inc';  // From Provision, not the Drupal platform.
13
define('MAINTENANCE_MODE', 'install');
14

15
function install_send_welcome_mail($url, $account, $language, $client_email, $onetime) {
16
  global $base_url;
17 18 19 20 21
  if ($client_email) {
    // Mail one time login URL and instructions.
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    $mail_params['variables'] = array(
      '!username' => $account->name, '!site' => variable_get('site_name', 'Drupal'), '!login_url' => $onetime,
omega8cc's avatar
omega8cc committed
22 23
      '!uri' => $base_url, '!uri_brief' => preg_replace('!^https?://!', '', $base_url), '!mailto' => $account->mail,
      '!date' => format_date(time()), '!login_uri' => url('user', array('absolute' => TRUE)),
24 25 26 27 28
      '!edit_uri' => url('user/'. $account->uid .'/edit', array('absolute' => TRUE)));

    $mail_success = drupal_mail('install', 'welcome-admin', $account->mail, user_preferred_language($account), $mail_params, $from, TRUE);

    if ($mail_success) {
29
      drush_log(dt('Sent welcome mail to @client', array('@client' => $client_email)), 'success');
30 31
    }
    else {
32
      drush_log(dt('Could not send welcome mail to @client', array('@client' => $client_email)), 'warning');
33 34 35 36
    }
  }
}

37 38

function install_mail($key, &$message, $params) {
39
  global $profile;
40 41 42 43 44 45 46
  switch ($key) {
    case 'welcome-admin':
      // allow the profile to override welcome email text
      if (file_exists("./profiles/$profile/provision_welcome_mail.inc")) {
        require_once "./profiles/$profile/provision_welcome_mail.inc";
        $custom = TRUE;
      }
omega8cc's avatar
omega8cc committed
47
      elseif (file_exists(dirname(__FILE__) . '/../provision_welcome_mail.inc')) {
48 49 50 51 52 53 54 55 56 57 58 59 60
        /** use the module provided welcome email
         * We can not use drupal_get_path here,
         * as we are connected to the provisioned site's database
         */
        require_once dirname(__FILE__) . '/../provision_welcome_mail.inc';
        $custom = TRUE;
      }
      else {
        // last resort use the user-pass mail text
        $custom = FALSE;
      }

      if ($custom) {
61 62
        $message['subject'] = dt($mail['subject'], $params['variables']);
        $message['body'][] = dt($mail['body'], $params['variables']);
63 64 65 66 67 68 69 70 71 72 73
      }
      else {
        $message['subject'] = _user_mail_text('pass_subject', $params['variables']);
        $message['body'][] = _user_mail_text('pass_body', $params['variables']);
      }

      break;
    }
}


74
function install_main() {
75 76
  global $profile, $install_locale, $conf, $url, $base_url;

77
  require_once DRUPAL_ROOT . '/includes/install.core.inc';
78
  require_once DRUPAL_ROOT . '/includes/entity.inc';
79
  require_once DRUPAL_ROOT . '/includes/unicode.inc';
80
  require_once DRUPAL_ROOT . '/modules/user/user.module';
81

82 83 84
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);

  // We have an existing settings.php.
ergonlogic's avatar
ergonlogic committed
85 86

  switch (substr(drush_core_version(), 0, 1)) {
87
    case '9':
helmo's avatar
helmo committed
88
    case '8':
ergonlogic's avatar
ergonlogic committed
89 90 91 92 93 94 95 96 97 98
    case '7':
      $sql = drush_get_class('Drush\Sql\Sql', array(), array(drush_drupal_major_version()));
      $db_spec = $sql->get_db_spec();
      break;
    case '6':
    case '5':
      $db_spec = _drush_sql_get_db_spec();
      break;
    default:
      drush_set_error('DRUSH_UNSUPPORTED_VERSION', dt('Drush !version is not supported'));
99 100
  }

101 102 103 104 105 106 107 108 109 110 111
  $db_spec['db_prefix'] = $GLOBALS['db_prefix'];

  if ($db_spec['driver'] == 'mysqli') {
    $db_spec['driver'] = 'mysql';
  }


  unset($conf['site_name']);
  unset($conf['site_mail']);
  unset($GLOBALS['db_url']);

112
  $account_pass = provision_password();
113 114
  $name = drush_get_option('admin_user', PROVISION_DEFAULT_ADMIN_USER);
  if ($error = user_validate_name($name)) {
115
    drush_log(dt('Error setting UID username to `!user`: ', array('!user' => $name)) . $error . dt(' Using default (!default) instead.', array('!default' => PROVISION_DEFAULT_ADMIN_USER)) , 'warning');
116
    $name = PROVISION_DEFAULT_ADMIN_USER;
117
  }
118
  $client_email = install_validate_client_email(drush_get_option('client_email', FALSE));
119 120 121 122 123
  $settings = array(
    'parameters' => array(
      'profile' => $profile,
      'locale' => $install_locale,
    ),
124
    'settings_verified' => TRUE,
125
    'forms' => array(
126
      'install_settings_form' => $db_spec,
127 128
      'install_configure_form' => array(
        'site_name' => $url,
129
        'site_mail' => $client_email,
130
        'account' => array(
131
          'name' => $name,
132
          'mail' => $client_email,
133 134 135 136 137 138 139 140 141 142 143 144 145
          'pass' => array(
            'pass1' => $account_pass,
            'pass2' => $account_pass,
          ),
        ),
        'update_status_module' => array(
          1 => TRUE,
          2 => TRUE,
        ),
        'clean_url' => drush_get_option('clean_url', TRUE),
      ),
    ),
  );
146 147 148 149

  // Allow other commands to alter the installation settings.
  drush_command_invoke_all_ref('provision_drupal_install_settings_alter', $settings, $url);

150 151
  try {
    install_drupal($settings);
152
  }
153
  catch (Exception $e) {
154
    drush_set_error('DRUPAL_INSTALL_FAILED');
155
    drush_log('Site installation caused an exception: ' . $e->getMessage(), 'error');
156
    dlm($e);
157
  }
158
  _provision_drupal_create_directories();
159

160
  $account = user_load(1);
161 162 163 164 165 166 167 168 169 170

  // If a redirect is defined, the symlink to the alias needs to exist before
  // we generate the login link, below.
  _provision_drupal_maintain_aliases();

  // Store the one time login link in an option so the front end can direct the
  // user to their new site.
  $onetime = provision_generate_login_reset();
  drush_set_option('login_link', $onetime);
  drush_log(dt('Login url: !onetime', array('!onetime' => $onetime)), 'success');
171

172
  if (drush_get_option('client_email', FALSE)) {
173
    install_send_welcome_mail($url, $account, $install_locale, $client_email, $onetime);
174
  }
175
}
176
install_main();
177 178 179 180

function install_exception_handler() {
  dlm(func_get_args());
}