install.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. <?php
  2. /**
  3. * @file
  4. * API functions for installing modules and themes.
  5. */
  6. use Drupal\Component\Utility\Unicode;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Drupal\Component\Utility\Crypt;
  9. use Drupal\Component\Utility\OpCodeCache;
  10. use Drupal\Component\Utility\UrlHelper;
  11. use Drupal\Core\Extension\ExtensionDiscovery;
  12. use Drupal\Core\Site\Settings;
  13. /**
  14. * Requirement severity -- Informational message only.
  15. */
  16. const REQUIREMENT_INFO = -1;
  17. /**
  18. * Requirement severity -- Requirement successfully met.
  19. */
  20. const REQUIREMENT_OK = 0;
  21. /**
  22. * Requirement severity -- Warning condition; proceed but flag warning.
  23. */
  24. const REQUIREMENT_WARNING = 1;
  25. /**
  26. * Requirement severity -- Error condition; abort installation.
  27. */
  28. const REQUIREMENT_ERROR = 2;
  29. /**
  30. * File permission check -- File exists.
  31. */
  32. const FILE_EXIST = 1;
  33. /**
  34. * File permission check -- File is readable.
  35. */
  36. const FILE_READABLE = 2;
  37. /**
  38. * File permission check -- File is writable.
  39. */
  40. const FILE_WRITABLE = 4;
  41. /**
  42. * File permission check -- File is executable.
  43. */
  44. const FILE_EXECUTABLE = 8;
  45. /**
  46. * File permission check -- File does not exist.
  47. */
  48. const FILE_NOT_EXIST = 16;
  49. /**
  50. * File permission check -- File is not readable.
  51. */
  52. const FILE_NOT_READABLE = 32;
  53. /**
  54. * File permission check -- File is not writable.
  55. */
  56. const FILE_NOT_WRITABLE = 64;
  57. /**
  58. * File permission check -- File is not executable.
  59. */
  60. const FILE_NOT_EXECUTABLE = 128;
  61. /**
  62. * Loads .install files for installed modules to initialize the update system.
  63. */
  64. function drupal_load_updates() {
  65. foreach (drupal_get_installed_schema_version(NULL, FALSE, TRUE) as $module => $schema_version) {
  66. if ($schema_version > -1) {
  67. module_load_install($module);
  68. }
  69. }
  70. }
  71. /**
  72. * Loads the installation profile, extracting its defined distribution name.
  73. *
  74. * @return
  75. * The distribution name defined in the profile's .info.yml file. Defaults to
  76. * "Drupal" if none is explicitly provided by the installation profile.
  77. *
  78. * @see install_profile_info()
  79. */
  80. function drupal_install_profile_distribution_name() {
  81. // During installation, the profile information is stored in the global
  82. // installation state (it might not be saved anywhere yet).
  83. $info = [];
  84. if (drupal_installation_attempted()) {
  85. global $install_state;
  86. if (isset($install_state['profile_info'])) {
  87. $info = $install_state['profile_info'];
  88. }
  89. }
  90. // At all other times, we load the profile via standard methods.
  91. else {
  92. $profile = drupal_get_profile();
  93. $info = system_get_info('module', $profile);
  94. }
  95. return isset($info['distribution']['name']) ? $info['distribution']['name'] : 'Drupal';
  96. }
  97. /**
  98. * Loads the installation profile, extracting its defined version.
  99. *
  100. * @return string
  101. * Distribution version defined in the profile's .info.yml file.
  102. * Defaults to \Drupal::VERSION if no version is explicitly provided by the
  103. * installation profile.
  104. *
  105. * @see install_profile_info()
  106. */
  107. function drupal_install_profile_distribution_version() {
  108. // During installation, the profile information is stored in the global
  109. // installation state (it might not be saved anywhere yet).
  110. if (drupal_installation_attempted()) {
  111. global $install_state;
  112. return isset($install_state['profile_info']['version']) ? $install_state['profile_info']['version'] : \Drupal::VERSION;
  113. }
  114. // At all other times, we load the profile via standard methods.
  115. else {
  116. $profile = drupal_get_profile();
  117. $info = system_get_info('module', $profile);
  118. return $info['version'];
  119. }
  120. }
  121. /**
  122. * Detects all supported databases that are compiled into PHP.
  123. *
  124. * @return
  125. * An array of database types compiled into PHP.
  126. */
  127. function drupal_detect_database_types() {
  128. $databases = drupal_get_database_types();
  129. foreach ($databases as $driver => $installer) {
  130. $databases[$driver] = $installer->name();
  131. }
  132. return $databases;
  133. }
  134. /**
  135. * Returns all supported database driver installer objects.
  136. *
  137. * @return \Drupal\Core\Database\Install\Tasks[]
  138. * An array of available database driver installer objects.
  139. */
  140. function drupal_get_database_types() {
  141. $databases = [];
  142. $drivers = [];
  143. // The internal database driver name is any valid PHP identifier.
  144. $mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '$/';
  145. $files = file_scan_directory(DRUPAL_ROOT . '/core/lib/Drupal/Core/Database/Driver', $mask, ['recurse' => FALSE]);
  146. if (is_dir(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database')) {
  147. $files += file_scan_directory(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database/', $mask, ['recurse' => FALSE]);
  148. }
  149. foreach ($files as $file) {
  150. if (file_exists($file->uri . '/Install/Tasks.php')) {
  151. $drivers[$file->filename] = $file->uri;
  152. }
  153. }
  154. foreach ($drivers as $driver => $file) {
  155. $installer = db_installer_object($driver);
  156. if ($installer->installable()) {
  157. $databases[$driver] = $installer;
  158. }
  159. }
  160. // Usability: unconditionally put the MySQL driver on top.
  161. if (isset($databases['mysql'])) {
  162. $mysql_database = $databases['mysql'];
  163. unset($databases['mysql']);
  164. $databases = ['mysql' => $mysql_database] + $databases;
  165. }
  166. return $databases;
  167. }
  168. /**
  169. * Replaces values in settings.php with values in the submitted array.
  170. *
  171. * This function replaces values in place if possible, even for
  172. * multidimensional arrays. This way the old settings do not linger,
  173. * overridden and also the doxygen on a value remains where it should be.
  174. *
  175. * @param $settings
  176. * An array of settings that need to be updated. Multidimensional arrays
  177. * are dumped up to a stdClass object. The object can have value, required
  178. * and comment properties.
  179. * @code
  180. * $settings['config_directories'] = array(
  181. * CONFIG_SYNC_DIRECTORY => (object) array(
  182. * 'value' => 'config_hash/sync',
  183. * 'required' => TRUE,
  184. * ),
  185. * );
  186. * @endcode
  187. * gets dumped as:
  188. * @code
  189. * $config_directories['sync'] = 'config_hash/sync'
  190. * @endcode
  191. */
  192. function drupal_rewrite_settings($settings = [], $settings_file = NULL) {
  193. if (!isset($settings_file)) {
  194. $settings_file = \Drupal::service('site.path') . '/settings.php';
  195. }
  196. // Build list of setting names and insert the values into the global namespace.
  197. $variable_names = [];
  198. $settings_settings = [];
  199. foreach ($settings as $setting => $data) {
  200. if ($setting != 'settings') {
  201. _drupal_rewrite_settings_global($GLOBALS[$setting], $data);
  202. }
  203. else {
  204. _drupal_rewrite_settings_global($settings_settings, $data);
  205. }
  206. $variable_names['$' . $setting] = $setting;
  207. }
  208. $contents = file_get_contents($settings_file);
  209. if ($contents !== FALSE) {
  210. // Initialize the contents for the settings.php file if it is empty.
  211. if (trim($contents) === '') {
  212. $contents = "<?php\n";
  213. }
  214. // Step through each token in settings.php and replace any variables that
  215. // are in the passed-in array.
  216. $buffer = '';
  217. $state = 'default';
  218. foreach (token_get_all($contents) as $token) {
  219. if (is_array($token)) {
  220. list($type, $value) = $token;
  221. }
  222. else {
  223. $type = -1;
  224. $value = $token;
  225. }
  226. // Do not operate on whitespace.
  227. if (!in_array($type, [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) {
  228. switch ($state) {
  229. case 'default':
  230. if ($type === T_VARIABLE && isset($variable_names[$value])) {
  231. // This will be necessary to unset the dumped variable.
  232. $parent = &$settings;
  233. // This is the current index in parent.
  234. $index = $variable_names[$value];
  235. // This will be necessary for descending into the array.
  236. $current = &$parent[$index];
  237. $state = 'candidate_left';
  238. }
  239. break;
  240. case 'candidate_left':
  241. if ($value == '[') {
  242. $state = 'array_index';
  243. }
  244. if ($value == '=') {
  245. $state = 'candidate_right';
  246. }
  247. break;
  248. case 'array_index':
  249. if (_drupal_rewrite_settings_is_array_index($type, $value)) {
  250. $index = trim($value, '\'"');
  251. $state = 'right_bracket';
  252. }
  253. else {
  254. // $a[foo()] or $a[$bar] or something like that.
  255. throw new Exception('invalid array index');
  256. }
  257. break;
  258. case 'right_bracket':
  259. if ($value == ']') {
  260. if (isset($current[$index])) {
  261. // If the new settings has this index, descend into it.
  262. $parent = &$current;
  263. $current = &$parent[$index];
  264. $state = 'candidate_left';
  265. }
  266. else {
  267. // Otherwise, jump back to the default state.
  268. $state = 'wait_for_semicolon';
  269. }
  270. }
  271. else {
  272. // $a[1 + 2].
  273. throw new Exception('] expected');
  274. }
  275. break;
  276. case 'candidate_right':
  277. if (_drupal_rewrite_settings_is_simple($type, $value)) {
  278. $value = _drupal_rewrite_settings_dump_one($current);
  279. // Unsetting $current would not affect $settings at all.
  280. unset($parent[$index]);
  281. // Skip the semicolon because _drupal_rewrite_settings_dump_one() added one.
  282. $state = 'semicolon_skip';
  283. }
  284. else {
  285. $state = 'wait_for_semicolon';
  286. }
  287. break;
  288. case 'wait_for_semicolon':
  289. if ($value == ';') {
  290. $state = 'default';
  291. }
  292. break;
  293. case 'semicolon_skip':
  294. if ($value == ';') {
  295. $value = '';
  296. $state = 'default';
  297. }
  298. else {
  299. // If the expression was $a = 1 + 2; then we replaced 1 and
  300. // the + is unexpected.
  301. throw new Exception('Unexpected token after replacing value.');
  302. }
  303. break;
  304. }
  305. }
  306. $buffer .= $value;
  307. }
  308. foreach ($settings as $name => $setting) {
  309. $buffer .= _drupal_rewrite_settings_dump($setting, '$' . $name);
  310. }
  311. // Write the new settings file.
  312. if (file_put_contents($settings_file, $buffer) === FALSE) {
  313. throw new Exception(t('Failed to modify %settings. Verify the file permissions.', ['%settings' => $settings_file]));
  314. }
  315. else {
  316. // In case any $settings variables were written, import them into the
  317. // Settings singleton.
  318. if (!empty($settings_settings)) {
  319. $old_settings = Settings::getAll();
  320. new Settings($settings_settings + $old_settings);
  321. }
  322. // The existing settings.php file might have been included already. In
  323. // case an opcode cache is enabled, the rewritten contents of the file
  324. // will not be reflected in this process. Ensure to invalidate the file
  325. // in case an opcode cache is enabled.
  326. OpCodeCache::invalidate(DRUPAL_ROOT . '/' . $settings_file);
  327. }
  328. }
  329. else {
  330. throw new Exception(t('Failed to open %settings. Verify the file permissions.', ['%settings' => $settings_file]));
  331. }
  332. }
  333. /**
  334. * Helper for drupal_rewrite_settings().
  335. *
  336. * Checks whether this token represents a scalar or NULL.
  337. *
  338. * @param int $type
  339. * The token type.
  340. * @param string $value
  341. * The value of the token.
  342. *
  343. * @return bool
  344. * TRUE if this token represents a scalar or NULL.
  345. *
  346. * @see token_name()
  347. */
  348. function _drupal_rewrite_settings_is_simple($type, $value) {
  349. $is_integer = $type == T_LNUMBER;
  350. $is_float = $type == T_DNUMBER;
  351. $is_string = $type == T_CONSTANT_ENCAPSED_STRING;
  352. $is_boolean_or_null = $type == T_STRING && in_array(strtoupper($value), ['TRUE', 'FALSE', 'NULL']);
  353. return $is_integer || $is_float || $is_string || $is_boolean_or_null;
  354. }
  355. /**
  356. * Helper for drupal_rewrite_settings().
  357. *
  358. * Checks whether this token represents a valid array index: a number or a
  359. * string.
  360. *
  361. * @param int $type
  362. * The token type.
  363. *
  364. * @return bool
  365. * TRUE if this token represents a number or a string.
  366. *
  367. * @see token_name()
  368. */
  369. function _drupal_rewrite_settings_is_array_index($type) {
  370. $is_integer = $type == T_LNUMBER;
  371. $is_float = $type == T_DNUMBER;
  372. $is_string = $type == T_CONSTANT_ENCAPSED_STRING;
  373. return $is_integer || $is_float || $is_string;
  374. }
  375. /**
  376. * Helper for drupal_rewrite_settings().
  377. *
  378. * Makes the new settings global.
  379. *
  380. * @param array|null $ref
  381. * A reference to a nested index in $GLOBALS.
  382. * @param array|object $variable
  383. * The nested value of the setting being copied.
  384. */
  385. function _drupal_rewrite_settings_global(&$ref, $variable) {
  386. if (is_object($variable)) {
  387. $ref = $variable->value;
  388. }
  389. else {
  390. foreach ($variable as $k => $v) {
  391. _drupal_rewrite_settings_global($ref[$k], $v);
  392. }
  393. }
  394. }
  395. /**
  396. * Helper for drupal_rewrite_settings().
  397. *
  398. * Dump the relevant value properties.
  399. *
  400. * @param array|object $variable
  401. * The container for variable values.
  402. * @param string $variable_name
  403. * Name of variable.
  404. * @return string
  405. * A string containing valid PHP code of the variable suitable for placing
  406. * into settings.php.
  407. */
  408. function _drupal_rewrite_settings_dump($variable, $variable_name) {
  409. $return = '';
  410. if (is_object($variable)) {
  411. if (!empty($variable->required)) {
  412. $return .= _drupal_rewrite_settings_dump_one($variable, "$variable_name = ", "\n");
  413. }
  414. }
  415. else {
  416. foreach ($variable as $k => $v) {
  417. $return .= _drupal_rewrite_settings_dump($v, $variable_name . "['" . $k . "']");
  418. }
  419. }
  420. return $return;
  421. }
  422. /**
  423. * Helper for drupal_rewrite_settings().
  424. *
  425. * Dump the value of a value property and adds the comment if it exists.
  426. *
  427. * @param object $variable
  428. * A stdClass object with at least a value property.
  429. * @param string $prefix
  430. * A string to prepend to the variable's value.
  431. * @param string $suffix
  432. * A string to append to the variable's value.
  433. * @return string
  434. * A string containing valid PHP code of the variable suitable for placing
  435. * into settings.php.
  436. */
  437. function _drupal_rewrite_settings_dump_one(\stdClass $variable, $prefix = '', $suffix = '') {
  438. $return = $prefix . var_export($variable->value, TRUE) . ';';
  439. if (!empty($variable->comment)) {
  440. $return .= ' // ' . $variable->comment;
  441. }
  442. $return .= $suffix;
  443. return $return;
  444. }
  445. /**
  446. * Creates the config directory and ensures it is operational.
  447. *
  448. * @see install_settings_form_submit()
  449. * @see update_prepare_d8_bootstrap()
  450. */
  451. function drupal_install_config_directories() {
  452. global $config_directories;
  453. // Add a randomized config directory name to settings.php, unless it was
  454. // manually defined in the existing already.
  455. if (empty($config_directories[CONFIG_SYNC_DIRECTORY])) {
  456. $config_directories[CONFIG_SYNC_DIRECTORY] = \Drupal::service('site.path') . '/files/config_' . Crypt::randomBytesBase64(55) . '/sync';
  457. $settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [
  458. 'value' => $config_directories[CONFIG_SYNC_DIRECTORY],
  459. 'required' => TRUE,
  460. ];
  461. // Rewrite settings.php, which also sets the value as global variable.
  462. drupal_rewrite_settings($settings);
  463. }
  464. // This should never fail, since if the config directory was specified in
  465. // settings.php it will have already been created and verified earlier, and
  466. // if it wasn't specified in settings.php, it is created here inside the
  467. // public files directory, which has already been verified to be writable
  468. // itself. But if it somehow fails anyway, the installation cannot proceed.
  469. // Bail out using a similar error message as in system_requirements().
  470. if (!file_prepare_directory($config_directories[CONFIG_SYNC_DIRECTORY], FILE_CREATE_DIRECTORY)
  471. && !file_exists($config_directories[CONFIG_SYNC_DIRECTORY])) {
  472. throw new Exception(t('The directory %directory could not be created. To proceed with the installation, either create the directory or ensure that the installer has the permissions to create it automatically. For more information, see the <a href=":handbook_url">online handbook</a>.', [
  473. '%directory' => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
  474. ':handbook_url' => 'https://www.drupal.org/server-permissions',
  475. ]));
  476. }
  477. elseif (is_writable($config_directories[CONFIG_SYNC_DIRECTORY])) {
  478. // Put a README.txt into the sync config directory. This is required so that
  479. // they can later be added to git. Since this directory is auto-created, we
  480. // have to write out the README rather than just adding it to the drupal core
  481. // repo.
  482. $text = 'This directory contains configuration to be imported into your Drupal site. To make this configuration active, visit admin/config/development/configuration/sync.' . ' For information about deploying configuration between servers, see https://www.drupal.org/documentation/administer/config';
  483. file_put_contents(config_get_config_directory(CONFIG_SYNC_DIRECTORY) . '/README.txt', $text);
  484. }
  485. }
  486. /**
  487. * Ensures that the config directory exists and is writable, or can be made so.
  488. *
  489. * @param string $type
  490. * Type of config directory to return. Drupal core provides 'sync'.
  491. *
  492. * @return bool
  493. * TRUE if the config directory exists and is writable.
  494. *
  495. * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.x. Use
  496. * config_get_config_directory() and file_prepare_directory() instead.
  497. *
  498. * @see https://www.drupal.org/node/2501187
  499. */
  500. function install_ensure_config_directory($type) {
  501. @trigger_error('install_ensure_config_directory() is deprecated in Drupal 8.1.0 and will be removed before Drupal 9.0.0. Use config_get_config_directory() and file_prepare_directory() instead. See https://www.drupal.org/node/2501187.', E_USER_DEPRECATED);
  502. // The config directory must be defined in settings.php.
  503. global $config_directories;
  504. if (!isset($config_directories[$type])) {
  505. return FALSE;
  506. }
  507. // The logic here is similar to that used by system_requirements() for other
  508. // directories that the installer creates.
  509. else {
  510. $config_directory = config_get_config_directory($type);
  511. return file_prepare_directory($config_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  512. }
  513. }
  514. /**
  515. * Verifies that all dependencies are met for a given installation profile.
  516. *
  517. * @param $install_state
  518. * An array of information about the current installation state.
  519. *
  520. * @return
  521. * The list of modules to install.
  522. */
  523. function drupal_verify_profile($install_state) {
  524. $profile = $install_state['parameters']['profile'];
  525. $info = $install_state['profile_info'];
  526. // Get the list of available modules for the selected installation profile.
  527. $listing = new ExtensionDiscovery(\Drupal::root());
  528. $present_modules = [];
  529. foreach ($listing->scan('module') as $present_module) {
  530. $present_modules[] = $present_module->getName();
  531. }
  532. // The installation profile is also a module, which needs to be installed
  533. // after all the other dependencies have been installed.
  534. $present_modules[] = $profile;
  535. // Verify that all of the profile's required modules are present.
  536. $missing_modules = array_diff($info['dependencies'], $present_modules);
  537. $requirements = [];
  538. if ($missing_modules) {
  539. $build = [
  540. '#theme' => 'item_list',
  541. '#context' => ['list_style' => 'comma-list'],
  542. ];
  543. foreach ($missing_modules as $module) {
  544. $build['#items'][] = ['#markup' => '<span class="admin-missing">' . Unicode::ucfirst($module) . '</span>'];
  545. }
  546. $modules_list = \Drupal::service('renderer')->renderPlain($build);
  547. $requirements['required_modules'] = [
  548. 'title' => t('Required modules'),
  549. 'value' => t('Required modules not found.'),
  550. 'severity' => REQUIREMENT_ERROR,
  551. 'description' => t('The following modules are required but were not found. Move them into the appropriate modules subdirectory, such as <em>/modules</em>. Missing modules: @modules', ['@modules' => $modules_list]),
  552. ];
  553. }
  554. return $requirements;
  555. }
  556. /**
  557. * Installs the system module.
  558. *
  559. * Separated from the installation of other modules so core system
  560. * functions can be made available while other modules are installed.
  561. *
  562. * @param array $install_state
  563. * An array of information about the current installation state. This is used
  564. * to set the default language.
  565. */
  566. function drupal_install_system($install_state) {
  567. // Remove the service provider of the early installer.
  568. unset($GLOBALS['conf']['container_service_providers']['InstallerServiceProvider']);
  569. $request = \Drupal::request();
  570. // Reboot into a full production environment to continue the installation.
  571. /** @var \Drupal\Core\Installer\InstallerKernel $kernel */
  572. $kernel = \Drupal::service('kernel');
  573. $kernel->shutdown();
  574. // Have installer rebuild from the disk, rather then building from scratch.
  575. $kernel->rebuildContainer(FALSE);
  576. $kernel->prepareLegacyRequest($request);
  577. // Install base system configuration.
  578. \Drupal::service('config.installer')->installDefaultConfig('core', 'core');
  579. // Store the installation profile in configuration to populate the
  580. // 'install_profile' container parameter.
  581. \Drupal::configFactory()->getEditable('core.extension')
  582. ->set('profile', $install_state['parameters']['profile'])
  583. ->save();
  584. // Install System module and rebuild the newly available routes.
  585. $kernel->getContainer()->get('module_installer')->install(['system'], FALSE);
  586. \Drupal::service('router.builder')->rebuild();
  587. // Ensure default language is saved.
  588. if (isset($install_state['parameters']['langcode'])) {
  589. \Drupal::configFactory()->getEditable('system.site')
  590. ->set('langcode', (string) $install_state['parameters']['langcode'])
  591. ->set('default_langcode', (string) $install_state['parameters']['langcode'])
  592. ->save(TRUE);
  593. }
  594. }
  595. /**
  596. * Verifies the state of the specified file.
  597. *
  598. * @param $file
  599. * The file to check for.
  600. * @param $mask
  601. * An optional bitmask created from various FILE_* constants.
  602. * @param $type
  603. * The type of file. Can be file (default), dir, or link.
  604. *
  605. * @return
  606. * TRUE on success or FALSE on failure. A message is set for the latter.
  607. */
  608. function drupal_verify_install_file($file, $mask = NULL, $type = 'file') {
  609. $return = TRUE;
  610. // Check for files that shouldn't be there.
  611. if (isset($mask) && ($mask & FILE_NOT_EXIST) && file_exists($file)) {
  612. return FALSE;
  613. }
  614. // Verify that the file is the type of file it is supposed to be.
  615. if (isset($type) && file_exists($file)) {
  616. $check = 'is_' . $type;
  617. if (!function_exists($check) || !$check($file)) {
  618. $return = FALSE;
  619. }
  620. }
  621. // Verify file permissions.
  622. if (isset($mask)) {
  623. $masks = [FILE_EXIST, FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
  624. foreach ($masks as $current_mask) {
  625. if ($mask & $current_mask) {
  626. switch ($current_mask) {
  627. case FILE_EXIST:
  628. if (!file_exists($file)) {
  629. if ($type == 'dir') {
  630. drupal_install_mkdir($file, $mask);
  631. }
  632. if (!file_exists($file)) {
  633. $return = FALSE;
  634. }
  635. }
  636. break;
  637. case FILE_READABLE:
  638. if (!is_readable($file) && !drupal_install_fix_file($file, $mask)) {
  639. $return = FALSE;
  640. }
  641. break;
  642. case FILE_WRITABLE:
  643. if (!is_writable($file) && !drupal_install_fix_file($file, $mask)) {
  644. $return = FALSE;
  645. }
  646. break;
  647. case FILE_EXECUTABLE:
  648. if (!is_executable($file) && !drupal_install_fix_file($file, $mask)) {
  649. $return = FALSE;
  650. }
  651. break;
  652. case FILE_NOT_READABLE:
  653. if (is_readable($file) && !drupal_install_fix_file($file, $mask)) {
  654. $return = FALSE;
  655. }
  656. break;
  657. case FILE_NOT_WRITABLE:
  658. if (is_writable($file) && !drupal_install_fix_file($file, $mask)) {
  659. $return = FALSE;
  660. }
  661. break;
  662. case FILE_NOT_EXECUTABLE:
  663. if (is_executable($file) && !drupal_install_fix_file($file, $mask)) {
  664. $return = FALSE;
  665. }
  666. break;
  667. }
  668. }
  669. }
  670. }
  671. return $return;
  672. }
  673. /**
  674. * Creates a directory with the specified permissions.
  675. *
  676. * @param $file
  677. * The name of the directory to create;
  678. * @param $mask
  679. * The permissions of the directory to create.
  680. * @param $message
  681. * (optional) Whether to output messages. Defaults to TRUE.
  682. *
  683. * @return
  684. * TRUE/FALSE whether or not the directory was successfully created.
  685. */
  686. function drupal_install_mkdir($file, $mask, $message = TRUE) {
  687. $mod = 0;
  688. $masks = [FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
  689. foreach ($masks as $m) {
  690. if ($mask & $m) {
  691. switch ($m) {
  692. case FILE_READABLE:
  693. $mod |= 0444;
  694. break;
  695. case FILE_WRITABLE:
  696. $mod |= 0222;
  697. break;
  698. case FILE_EXECUTABLE:
  699. $mod |= 0111;
  700. break;
  701. }
  702. }
  703. }
  704. if (@drupal_mkdir($file, $mod)) {
  705. return TRUE;
  706. }
  707. else {
  708. return FALSE;
  709. }
  710. }
  711. /**
  712. * Attempts to fix file permissions.
  713. *
  714. * The general approach here is that, because we do not know the security
  715. * setup of the webserver, we apply our permission changes to all three
  716. * digits of the file permission (i.e. user, group and all).
  717. *
  718. * To ensure that the values behave as expected (and numbers don't carry
  719. * from one digit to the next) we do the calculation on the octal value
  720. * using bitwise operations. This lets us remove, for example, 0222 from
  721. * 0700 and get the correct value of 0500.
  722. *
  723. * @param $file
  724. * The name of the file with permissions to fix.
  725. * @param $mask
  726. * The desired permissions for the file.
  727. * @param $message
  728. * (optional) Whether to output messages. Defaults to TRUE.
  729. *
  730. * @return
  731. * TRUE/FALSE whether or not we were able to fix the file's permissions.
  732. */
  733. function drupal_install_fix_file($file, $mask, $message = TRUE) {
  734. // If $file does not exist, fileperms() issues a PHP warning.
  735. if (!file_exists($file)) {
  736. return FALSE;
  737. }
  738. $mod = fileperms($file) & 0777;
  739. $masks = [FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
  740. // FILE_READABLE, FILE_WRITABLE, and FILE_EXECUTABLE permission strings
  741. // can theoretically be 0400, 0200, and 0100 respectively, but to be safe
  742. // we set all three access types in case the administrator intends to
  743. // change the owner of settings.php after installation.
  744. foreach ($masks as $m) {
  745. if ($mask & $m) {
  746. switch ($m) {
  747. case FILE_READABLE:
  748. if (!is_readable($file)) {
  749. $mod |= 0444;
  750. }
  751. break;
  752. case FILE_WRITABLE:
  753. if (!is_writable($file)) {
  754. $mod |= 0222;
  755. }
  756. break;
  757. case FILE_EXECUTABLE:
  758. if (!is_executable($file)) {
  759. $mod |= 0111;
  760. }
  761. break;
  762. case FILE_NOT_READABLE:
  763. if (is_readable($file)) {
  764. $mod &= ~0444;
  765. }
  766. break;
  767. case FILE_NOT_WRITABLE:
  768. if (is_writable($file)) {
  769. $mod &= ~0222;
  770. }
  771. break;
  772. case FILE_NOT_EXECUTABLE:
  773. if (is_executable($file)) {
  774. $mod &= ~0111;
  775. }
  776. break;
  777. }
  778. }
  779. }
  780. // chmod() will work if the web server is running as owner of the file.
  781. if (@chmod($file, $mod)) {
  782. return TRUE;
  783. }
  784. else {
  785. return FALSE;
  786. }
  787. }
  788. /**
  789. * Sends the user to a different installer page.
  790. *
  791. * This issues an on-site HTTP redirect. Messages (and errors) are erased.
  792. *
  793. * @param $path
  794. * An installer path.
  795. */
  796. function install_goto($path) {
  797. global $base_url;
  798. $headers = [
  799. // Not a permanent redirect.
  800. 'Cache-Control' => 'no-cache',
  801. ];
  802. $response = new RedirectResponse($base_url . '/' . $path, 302, $headers);
  803. $response->send();
  804. }
  805. /**
  806. * Returns the URL of the current script, with modified query parameters.
  807. *
  808. * This function can be called by low-level scripts (such as install.php and
  809. * update.php) and returns the URL of the current script. Existing query
  810. * parameters are preserved by default, but new ones can optionally be merged
  811. * in.
  812. *
  813. * This function is used when the script must maintain certain query parameters
  814. * over multiple page requests in order to work correctly. In such cases (for
  815. * example, update.php, which requires the 'continue=1' parameter to remain in
  816. * the URL throughout the update process if there are any requirement warnings
  817. * that need to be bypassed), using this function to generate the URL for links
  818. * to the next steps of the script ensures that the links will work correctly.
  819. *
  820. * @param $query
  821. * (optional) An array of query parameters to merge in to the existing ones.
  822. *
  823. * @return
  824. * The URL of the current script, with query parameters modified by the
  825. * passed-in $query. The URL is not sanitized, so it still needs to be run
  826. * through \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be
  827. * used as an HTML attribute value.
  828. *
  829. * @see drupal_requirements_url()
  830. * @see Drupal\Component\Utility\UrlHelper::filterBadProtocol()
  831. */
  832. function drupal_current_script_url($query = []) {
  833. $uri = $_SERVER['SCRIPT_NAME'];
  834. $query = array_merge(UrlHelper::filterQueryParameters(\Drupal::request()->query->all()), $query);
  835. if (!empty($query)) {
  836. $uri .= '?' . UrlHelper::buildQuery($query);
  837. }
  838. return $uri;
  839. }
  840. /**
  841. * Returns a URL for proceeding to the next page after a requirements problem.
  842. *
  843. * This function can be called by low-level scripts (such as install.php and
  844. * update.php) and returns a URL that can be used to attempt to proceed to the
  845. * next step of the script.
  846. *
  847. * @param $severity
  848. * The severity of the requirements problem, as returned by
  849. * drupal_requirements_severity().
  850. *
  851. * @return
  852. * A URL for attempting to proceed to the next step of the script. The URL is
  853. * not sanitized, so it still needs to be run through
  854. * \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be used
  855. * as an HTML attribute value.
  856. *
  857. * @see drupal_current_script_url()
  858. * @see \Drupal\Component\Utility\UrlHelper::filterBadProtocol()
  859. */
  860. function drupal_requirements_url($severity) {
  861. $query = [];
  862. // If there are no errors, only warnings, append 'continue=1' to the URL so
  863. // the user can bypass this screen on the next page load.
  864. if ($severity == REQUIREMENT_WARNING) {
  865. $query['continue'] = 1;
  866. }
  867. return drupal_current_script_url($query);
  868. }
  869. /**
  870. * Checks an installation profile's requirements.
  871. *
  872. * @param string $profile
  873. * Name of installation profile to check.
  874. *
  875. * @return array
  876. * Array of the installation profile's requirements.
  877. */
  878. function drupal_check_profile($profile) {
  879. $info = install_profile_info($profile);
  880. // Collect requirement testing results.
  881. $requirements = [];
  882. // Performs an ExtensionDiscovery scan as the system module is unavailable and
  883. // we don't yet know where all the modules are located.
  884. // @todo Remove as part of https://www.drupal.org/node/2186491
  885. $drupal_root = \Drupal::root();
  886. $module_list = (new ExtensionDiscovery($drupal_root))->scan('module');
  887. foreach ($info['dependencies'] as $module) {
  888. // If the module is in the module list we know it exists and we can continue
  889. // including and registering it.
  890. // @see \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory()
  891. if (isset($module_list[$module])) {
  892. $function = $module . '_requirements';
  893. $module_path = $module_list[$module]->getPath();
  894. $install_file = "$drupal_root/$module_path/$module.install";
  895. if (is_file($install_file)) {
  896. require_once $install_file;
  897. }
  898. drupal_classloader_register($module, $module_path);
  899. if (function_exists($function)) {
  900. $requirements = array_merge($requirements, $function('install'));
  901. }
  902. }
  903. }
  904. return $requirements;
  905. }
  906. /**
  907. * Extracts the highest severity from the requirements array.
  908. *
  909. * @param $requirements
  910. * An array of requirements, in the same format as is returned by
  911. * hook_requirements().
  912. *
  913. * @return
  914. * The highest severity in the array.
  915. */
  916. function drupal_requirements_severity(&$requirements) {
  917. $severity = REQUIREMENT_OK;
  918. foreach ($requirements as $requirement) {
  919. if (isset($requirement['severity'])) {
  920. $severity = max($severity, $requirement['severity']);
  921. }
  922. }
  923. return $severity;
  924. }
  925. /**
  926. * Checks a module's requirements.
  927. *
  928. * @param $module
  929. * Machine name of module to check.
  930. *
  931. * @return
  932. * TRUE or FALSE, depending on whether the requirements are met.
  933. */
  934. function drupal_check_module($module) {
  935. module_load_install($module);
  936. // Check requirements
  937. $requirements = \Drupal::moduleHandler()->invoke($module, 'requirements', ['install']);
  938. if (is_array($requirements) && drupal_requirements_severity($requirements) == REQUIREMENT_ERROR) {
  939. // Print any error messages
  940. foreach ($requirements as $requirement) {
  941. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  942. $message = $requirement['description'];
  943. if (isset($requirement['value']) && $requirement['value']) {
  944. $message = t('@requirements_message (Currently using @item version @version)', ['@requirements_message' => $requirement['description'], '@item' => $requirement['title'], '@version' => $requirement['value']]);
  945. }
  946. drupal_set_message($message, 'error');
  947. }
  948. }
  949. return FALSE;
  950. }
  951. return TRUE;
  952. }
  953. /**
  954. * Retrieves information about an installation profile from its .info.yml file.
  955. *
  956. * The information stored in a profile .info.yml file is similar to that stored
  957. * in a normal Drupal module .info.yml file. For example:
  958. * - name: The real name of the installation profile for display purposes.
  959. * - description: A brief description of the profile.
  960. * - dependencies: An array of shortnames of other modules that this install
  961. * profile requires.
  962. *
  963. * Additional, less commonly-used information that can appear in a
  964. * profile.info.yml file but not in a normal Drupal module .info.yml file
  965. * includes:
  966. *
  967. * - distribution: Existence of this key denotes that the installation profile
  968. * is intended to be the only eligible choice in a distribution and will be
  969. * auto-selected during installation, whereas the installation profile
  970. * selection screen will be skipped. If more than one distribution profile is
  971. * found then the first one discovered will be selected.
  972. * The following subproperties may be set:
  973. * - name: The name of the distribution that is being installed, to be shown
  974. * throughout the installation process. If omitted,
  975. * drupal_install_profile_distribution_name() defaults to 'Drupal'.
  976. * - install: Optional parameters to override the installer:
  977. * - theme: The machine name of a theme to use in the installer instead of
  978. * Drupal's default installer theme.
  979. *
  980. * Note that this function does an expensive file system scan to get info file
  981. * information for dependencies. If you only need information from the info
  982. * file itself, use system_get_info().
  983. *
  984. * Example of .info.yml file:
  985. * @code
  986. * name: Minimal
  987. * description: Start fresh, with only a few modules enabled.
  988. * dependencies:
  989. * - block
  990. * - dblog
  991. * @endcode
  992. *
  993. * @param $profile
  994. * Name of profile.
  995. * @param $langcode
  996. * Language code (if any).
  997. *
  998. * @return
  999. * The info array.
  1000. */
  1001. function install_profile_info($profile, $langcode = 'en') {
  1002. $cache = &drupal_static(__FUNCTION__, []);
  1003. if (!isset($cache[$profile][$langcode])) {
  1004. // Set defaults for module info.
  1005. $defaults = [
  1006. 'dependencies' => [],
  1007. 'themes' => ['stark'],
  1008. 'description' => '',
  1009. 'version' => NULL,
  1010. 'hidden' => FALSE,
  1011. 'php' => DRUPAL_MINIMUM_PHP,
  1012. ];
  1013. $profile_file = drupal_get_path('profile', $profile) . "/$profile.info.yml";
  1014. $info = \Drupal::service('info_parser')->parse($profile_file);
  1015. $info += $defaults;
  1016. // drupal_required_modules() includes the current profile as a dependency.
  1017. // Remove that dependency, since a module cannot depend on itself.
  1018. $required = array_diff(drupal_required_modules(), [$profile]);
  1019. $locale = !empty($langcode) && $langcode != 'en' ? ['locale'] : [];
  1020. $info['dependencies'] = array_unique(array_merge($required, $info['dependencies'], $locale));
  1021. $cache[$profile][$langcode] = $info;
  1022. }
  1023. return $cache[$profile][$langcode];
  1024. }
  1025. /**
  1026. * Returns a database installer object.
  1027. *
  1028. * @param $driver
  1029. * The name of the driver.
  1030. *
  1031. * @return \Drupal\Core\Database\Install\Tasks
  1032. * A class defining the requirements and tasks for installing the database.
  1033. */
  1034. function db_installer_object($driver) {
  1035. // We cannot use Database::getConnection->getDriverClass() here, because
  1036. // the connection object is not yet functional.
  1037. $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
  1038. if (class_exists($task_class)) {
  1039. return new $task_class();
  1040. }
  1041. else {
  1042. $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
  1043. return new $task_class();
  1044. }
  1045. }