Drupal investigation

install.inc 36KB

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