2cca962599934cae8bf9be19ca85be15122031d9
[yaffs-website] / web / core / lib / Drupal / Core / Installer / Form / SiteConfigureForm.php
1 <?php
2
3 namespace Drupal\Core\Installer\Form;
4
5 use Drupal\Core\Extension\ModuleInstallerInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Locale\CountryManagerInterface;
9 use Drupal\Core\Site\Settings;
10 use Drupal\Core\State\StateInterface;
11 use Drupal\user\UserStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Provides the site configuration form.
16  *
17  * @internal
18  */
19 class SiteConfigureForm extends ConfigFormBase {
20
21   /**
22    * The site path.
23    *
24    * @var string
25    */
26   protected $sitePath;
27
28   /**
29    * The user storage.
30    *
31    * @var \Drupal\user\UserStorageInterface
32    */
33   protected $userStorage;
34
35   /**
36    * The state service.
37    *
38    * @var \Drupal\Core\State\StateInterface
39    */
40   protected $state;
41
42   /**
43    * The module installer.
44    *
45    * @var \Drupal\Core\Extension\ModuleInstallerInterface
46    */
47   protected $moduleInstaller;
48
49   /**
50    * The country manager.
51    *
52    * @var \Drupal\Core\Locale\CountryManagerInterface
53    */
54   protected $countryManager;
55
56   /**
57    * The app root.
58    *
59    * @var string
60    */
61   protected $root;
62
63   /**
64    * Constructs a new SiteConfigureForm.
65    *
66    * @param string $root
67    *   The app root.
68    * @param string $site_path
69    *   The site path.
70    * @param \Drupal\user\UserStorageInterface $user_storage
71    *   The user storage.
72    * @param \Drupal\Core\State\StateInterface $state
73    *   The state service.
74    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
75    *   The module installer.
76    * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
77    *   The country manager.
78    */
79   public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
80     $this->root = $root;
81     $this->sitePath = $site_path;
82     $this->userStorage = $user_storage;
83     $this->state = $state;
84     $this->moduleInstaller = $module_installer;
85     $this->countryManager = $country_manager;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public static function create(ContainerInterface $container) {
92     return new static(
93       $container->get('app.root'),
94       $container->get('site.path'),
95       $container->get('entity.manager')->getStorage('user'),
96       $container->get('state'),
97       $container->get('module_installer'),
98       $container->get('country_manager')
99     );
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function getFormId() {
106     return 'install_configure_form';
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   protected function getEditableConfigNames() {
113     return [
114       'system.date',
115       'system.site',
116       'update.settings',
117     ];
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function buildForm(array $form, FormStateInterface $form_state) {
124     global $install_state;
125     $form['#title'] = $this->t('Configure site');
126
127     // Warn about settings.php permissions risk
128     $settings_dir = $this->sitePath;
129     $settings_file = $settings_dir . '/settings.php';
130     // Check that $_POST is empty so we only show this message when the form is
131     // first displayed, not on the next page after it is submitted. (We do not
132     // want to repeat it multiple times because it is a general warning that is
133     // not related to the rest of the installation process; it would also be
134     // especially out of place on the last page of the installer, where it would
135     // distract from the message that the Drupal installation has completed
136     // successfully.)
137     $post_params = $this->getRequest()->request->all();
138     if (empty($post_params) && (Settings::get('skip_permissions_hardening') || !drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
139       $this->messenger()->addWarning(t('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, consult the <a href=":handbook_url">online handbook</a>.', ['%dir' => $settings_dir, '%file' => $settings_file, ':handbook_url' => 'https://www.drupal.org/server-permissions']));
140     }
141
142     $form['#attached']['library'][] = 'system/drupal.system';
143     // Add JavaScript time zone detection.
144     $form['#attached']['library'][] = 'core/drupal.timezone';
145     // We add these strings as settings because JavaScript translation does not
146     // work during installation.
147     $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
148
149     $form['site_information'] = [
150       '#type' => 'fieldgroup',
151       '#title' => $this->t('Site information'),
152       '#access' => empty($install_state['config_install_path']),
153     ];
154     $form['site_information']['site_name'] = [
155       '#type' => 'textfield',
156       '#title' => $this->t('Site name'),
157       '#required' => TRUE,
158       '#weight' => -20,
159       '#access' => empty($install_state['config_install_path']),
160     ];
161     $form['site_information']['site_mail'] = [
162       '#type' => 'email',
163       '#title' => $this->t('Site email address'),
164       '#default_value' => ini_get('sendmail_from'),
165       '#description' => $this->t("Automated emails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these emails from being flagged as spam."),
166       '#required' => TRUE,
167       '#weight' => -15,
168       '#access' => empty($install_state['config_install_path']),
169     ];
170
171     $form['admin_account'] = [
172       '#type' => 'fieldgroup',
173       '#title' => $this->t('Site maintenance account'),
174     ];
175     $form['admin_account']['account']['name'] = [
176       '#type' => 'textfield',
177       '#title' => $this->t('Username'),
178       '#maxlength' => USERNAME_MAX_LENGTH,
179       '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
180       '#required' => TRUE,
181       '#attributes' => ['class' => ['username']],
182     ];
183     $form['admin_account']['account']['pass'] = [
184       '#type' => 'password_confirm',
185       '#required' => TRUE,
186       '#size' => 25,
187     ];
188     $form['admin_account']['account']['#tree'] = TRUE;
189     $form['admin_account']['account']['mail'] = [
190       '#type' => 'email',
191       '#title' => $this->t('Email address'),
192       '#required' => TRUE,
193     ];
194
195     $form['regional_settings'] = [
196       '#type' => 'fieldgroup',
197       '#title' => $this->t('Regional settings'),
198       '#access' => empty($install_state['config_install_path']),
199     ];
200     $countries = $this->countryManager->getList();
201     $form['regional_settings']['site_default_country'] = [
202       '#type' => 'select',
203       '#title' => $this->t('Default country'),
204       '#empty_value' => '',
205       '#default_value' => $this->config('system.date')->get('country.default'),
206       '#options' => $countries,
207       '#description' => $this->t('Select the default country for the site.'),
208       '#weight' => 0,
209       '#access' => empty($install_state['config_install_path']),
210     ];
211     $form['regional_settings']['date_default_timezone'] = [
212       '#type' => 'select',
213       '#title' => $this->t('Default time zone'),
214       // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
215       '#default_value' => @date_default_timezone_get(),
216       '#options' => system_time_zones(NULL, TRUE),
217       '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
218       '#weight' => 5,
219       '#attributes' => ['class' => ['timezone-detect']],
220       '#access' => empty($install_state['config_install_path']),
221     ];
222
223     $form['update_notifications'] = [
224       '#type' => 'fieldgroup',
225       '#title' => $this->t('Update notifications'),
226       '#description' => $this->t('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href=":drupal">Drupal.org</a>.', [':drupal' => 'https://www.drupal.org']),
227       '#access' => empty($install_state['config_install_path']),
228     ];
229     $form['update_notifications']['enable_update_status_module'] = [
230       '#type' => 'checkbox',
231       '#title' => $this->t('Check for updates automatically'),
232       '#default_value' => 1,
233       '#access' => empty($install_state['config_install_path']),
234     ];
235     $form['update_notifications']['enable_update_status_emails'] = [
236       '#type' => 'checkbox',
237       '#title' => $this->t('Receive email notifications'),
238       '#default_value' => 1,
239       '#states' => [
240         'visible' => [
241           'input[name="enable_update_status_module"]' => ['checked' => TRUE],
242         ],
243       ],
244       '#access' => empty($install_state['config_install_path']),
245     ];
246
247     $form['actions'] = ['#type' => 'actions'];
248     $form['actions']['submit'] = [
249       '#type' => 'submit',
250       '#value' => $this->t('Save and continue'),
251       '#weight' => 15,
252       '#button_type' => 'primary',
253     ];
254
255     return $form;
256   }
257
258   /**
259    * {@inheritdoc}
260    */
261   public function validateForm(array &$form, FormStateInterface $form_state) {
262     if ($error = user_validate_name($form_state->getValue(['account', 'name']))) {
263       $form_state->setErrorByName('account][name', $error);
264     }
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   public function submitForm(array &$form, FormStateInterface $form_state) {
271     global $install_state;
272
273     if (empty($install_state['config_install_path'])) {
274       $this->config('system.site')
275         ->set('name', (string) $form_state->getValue('site_name'))
276         ->set('mail', (string) $form_state->getValue('site_mail'))
277         ->save(TRUE);
278
279       $this->config('system.date')
280         ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
281         ->set('country.default', (string) $form_state->getValue('site_default_country'))
282         ->save(TRUE);
283     }
284
285     $account_values = $form_state->getValue('account');
286
287     // Enable update.module if this option was selected.
288     $update_status_module = $form_state->getValue('enable_update_status_module');
289     if (empty($install_state['config_install_path']) && $update_status_module) {
290       $this->moduleInstaller->install(['file', 'update'], FALSE);
291
292       // Add the site maintenance account's email address to the list of
293       // addresses to be notified when updates are available, if selected.
294       $email_update_status_emails = $form_state->getValue('enable_update_status_emails');
295       if ($email_update_status_emails) {
296         // Reset the configuration factory so it is updated with the new module.
297         $this->resetConfigFactory();
298         $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
299       }
300     }
301
302     // We precreated user 1 with placeholder values. Let's save the real values.
303     $account = $this->userStorage->load(1);
304     $account->init = $account->mail = $account_values['mail'];
305     $account->roles = $account->getRoles();
306     $account->activate();
307     $account->timezone = $form_state->getValue('date_default_timezone');
308     $account->pass = $account_values['pass'];
309     $account->name = $account_values['name'];
310     $account->save();
311
312     // Record when this install ran.
313     $this->state->set('install_time', $_SERVER['REQUEST_TIME']);
314   }
315
316 }