PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php

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