install.inc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 = array();
  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 = array();
  141. $drivers = array();
  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, array('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, array('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 = array('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 = array(), $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 = array();
  197. $settings_settings = array();
  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, array(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.', array('%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.', array('%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), array('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 or could not be made writable. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see the <a href=":handbook_url">online handbook</a>.', array(
  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 = array();
  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 = array();
  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'][] = array('#markup' => '<span class="admin-missing">' . Unicode::ucfirst($module) . '</span>');
  541. }
  542. $modules_list = \Drupal::service('renderer')->renderPlain($build);
  543. $requirements['required_modules'] = array(
  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', array('@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. // Install System module and rebuild the newly available routes.
  576. $kernel->getContainer()->get('module_installer')->install(array('system'), FALSE);
  577. \Drupal::service('router.builder')->rebuild();
  578. // Ensure default language is saved.
  579. if (isset($install_state['parameters']['langcode'])) {
  580. \Drupal::configFactory()->getEditable('system.site')
  581. ->set('langcode', (string) $install_state['parameters']['langcode'])
  582. ->set('default_langcode', (string) $install_state['parameters']['langcode'])
  583. ->save(TRUE);
  584. }
  585. }
  586. /**
  587. * Verifies the state of the specified file.
  588. *
  589. * @param $file
  590. * The file to check for.
  591. * @param $mask
  592. * An optional bitmask created from various FILE_* constants.
  593. * @param $type
  594. * The type of file. Can be file (default), dir, or link.
  595. *
  596. * @return
  597. * TRUE on success or FALSE on failure. A message is set for the latter.
  598. */
  599. function drupal_verify_install_file($file, $mask = NULL, $type = 'file') {
  600. $return = TRUE;
  601. // Check for files that shouldn't be there.
  602. if (isset($mask) && ($mask & FILE_NOT_EXIST) && file_exists($file)) {
  603. return FALSE;
  604. }
  605. // Verify that the file is the type of file it is supposed to be.
  606. if (isset($type) && file_exists($file)) {
  607. $check = 'is_' . $type;
  608. if (!function_exists($check) || !$check($file)) {
  609. $return = FALSE;
  610. }
  611. }
  612. // Verify file permissions.
  613. if (isset($mask)) {
  614. $masks = array(FILE_EXIST, FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
  615. foreach ($masks as $current_mask) {
  616. if ($mask & $current_mask) {
  617. switch ($current_mask) {
  618. case FILE_EXIST:
  619. if (!file_exists($file)) {
  620. if ($type == 'dir') {
  621. drupal_install_mkdir($file, $mask);
  622. }
  623. if (!file_exists($file)) {
  624. $return = FALSE;
  625. }
  626. }
  627. break;
  628. case FILE_READABLE:
  629. if (!is_readable($file) && !drupal_install_fix_file($file, $mask)) {
  630. $return = FALSE;
  631. }
  632. break;
  633. case FILE_WRITABLE:
  634. if (!is_writable($file) && !drupal_install_fix_file($file, $mask)) {
  635. $return = FALSE;
  636. }
  637. break;
  638. case FILE_EXECUTABLE:
  639. if (!is_executable($file) && !drupal_install_fix_file($file, $mask)) {
  640. $return = FALSE;
  641. }
  642. break;
  643. case FILE_NOT_READABLE:
  644. if (is_readable($file) && !drupal_install_fix_file($file, $mask)) {
  645. $return = FALSE;
  646. }
  647. break;
  648. case FILE_NOT_WRITABLE:
  649. if (is_writable($file) && !drupal_install_fix_file($file, $mask)) {
  650. $return = FALSE;
  651. }
  652. break;
  653. case FILE_NOT_EXECUTABLE:
  654. if (is_executable($file) && !drupal_install_fix_file($file, $mask)) {
  655. $return = FALSE;
  656. }
  657. break;
  658. }
  659. }
  660. }
  661. }
  662. return $return;
  663. }
  664. /**
  665. * Creates a directory with the specified permissions.
  666. *
  667. * @param $file
  668. * The name of the directory to create;
  669. * @param $mask
  670. * The permissions of the directory to create.
  671. * @param $message
  672. * (optional) Whether to output messages. Defaults to TRUE.
  673. *
  674. * @return
  675. * TRUE/FALSE whether or not the directory was successfully created.
  676. */
  677. function drupal_install_mkdir($file, $mask, $message = TRUE) {
  678. $mod = 0;
  679. $masks = array(FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
  680. foreach ($masks as $m) {
  681. if ($mask & $m) {
  682. switch ($m) {
  683. case FILE_READABLE:
  684. $mod |= 0444;
  685. break;
  686. case FILE_WRITABLE:
  687. $mod |= 0222;
  688. break;
  689. case FILE_EXECUTABLE:
  690. $mod |= 0111;
  691. break;
  692. }
  693. }
  694. }
  695. if (@drupal_mkdir($file, $mod)) {
  696. return TRUE;
  697. }
  698. else {
  699. return FALSE;
  700. }
  701. }
  702. /**
  703. * Attempts to fix file permissions.
  704. *
  705. * The general approach here is that, because we do not know the security
  706. * setup of the webserver, we apply our permission changes to all three
  707. * digits of the file permission (i.e. user, group and all).
  708. *
  709. * To ensure that the values behave as expected (and numbers don't carry
  710. * from one digit to the next) we do the calculation on the octal value
  711. * using bitwise operations. This lets us remove, for example, 0222 from
  712. * 0700 and get the correct value of 0500.
  713. *
  714. * @param $file
  715. * The name of the file with permissions to fix.
  716. * @param $mask
  717. * The desired permissions for the file.
  718. * @param $message
  719. * (optional) Whether to output messages. Defaults to TRUE.
  720. *
  721. * @return
  722. * TRUE/FALSE whether or not we were able to fix the file's permissions.
  723. */
  724. function drupal_install_fix_file($file, $mask, $message = TRUE) {
  725. // If $file does not exist, fileperms() issues a PHP warning.
  726. if (!file_exists($file)) {
  727. return FALSE;
  728. }
  729. $mod = fileperms($file) & 0777;
  730. $masks = array(FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
  731. // FILE_READABLE, FILE_WRITABLE, and FILE_EXECUTABLE permission strings
  732. // can theoretically be 0400, 0200, and 0100 respectively, but to be safe
  733. // we set all three access types in case the administrator intends to
  734. // change the owner of settings.php after installation.
  735. foreach ($masks as $m) {
  736. if ($mask & $m) {
  737. switch ($m) {
  738. case FILE_READABLE:
  739. if (!is_readable($file)) {
  740. $mod |= 0444;
  741. }
  742. break;
  743. case FILE_WRITABLE:
  744. if (!is_writable($file)) {
  745. $mod |= 0222;
  746. }
  747. break;
  748. case FILE_EXECUTABLE:
  749. if (!is_executable($file)) {
  750. $mod |= 0111;
  751. }
  752. break;
  753. case FILE_NOT_READABLE:
  754. if (is_readable($file)) {
  755. $mod &= ~0444;
  756. }
  757. break;
  758. case FILE_NOT_WRITABLE:
  759. if (is_writable($file)) {
  760. $mod &= ~0222;
  761. }
  762. break;
  763. case FILE_NOT_EXECUTABLE:
  764. if (is_executable($file)) {
  765. $mod &= ~0111;
  766. }
  767. break;
  768. }
  769. }
  770. }
  771. // chmod() will work if the web server is running as owner of the file.
  772. if (@chmod($file, $mod)) {
  773. return TRUE;
  774. }
  775. else {
  776. return FALSE;
  777. }
  778. }
  779. /**
  780. * Sends the user to a different installer page.
  781. *
  782. * This issues an on-site HTTP redirect. Messages (and errors) are erased.
  783. *
  784. * @param $path
  785. * An installer path.
  786. */
  787. function install_goto($path) {
  788. global $base_url;
  789. $headers = array(
  790. // Not a permanent redirect.
  791. 'Cache-Control' => 'no-cache',
  792. );
  793. $response = new RedirectResponse($base_url . '/' . $path, 302, $headers);
  794. $response->send();
  795. }
  796. /**
  797. * Returns the URL of the current script, with modified query parameters.
  798. *
  799. * This function can be called by low-level scripts (such as install.php and
  800. * update.php) and returns the URL of the current script. Existing query
  801. * parameters are preserved by default, but new ones can optionally be merged
  802. * in.
  803. *
  804. * This function is used when the script must maintain certain query parameters
  805. * over multiple page requests in order to work correctly. In such cases (for
  806. * example, update.php, which requires the 'continue=1' parameter to remain in
  807. * the URL throughout the update process if there are any requirement warnings
  808. * that need to be bypassed), using this function to generate the URL for links
  809. * to the next steps of the script ensures that the links will work correctly.
  810. *
  811. * @param $query
  812. * (optional) An array of query parameters to merge in to the existing ones.
  813. *
  814. * @return
  815. * The URL of the current script, with query parameters modified by the
  816. * passed-in $query. The URL is not sanitized, so it still needs to be run
  817. * through \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be
  818. * used as an HTML attribute value.
  819. *
  820. * @see drupal_requirements_url()
  821. * @see Drupal\Component\Utility\UrlHelper::filterBadProtocol()
  822. */
  823. function drupal_current_script_url($query = array()) {
  824. $uri = $_SERVER['SCRIPT_NAME'];
  825. $query = array_merge(UrlHelper::filterQueryParameters(\Drupal::request()->query->all()), $query);
  826. if (!empty($query)) {
  827. $uri .= '?' . UrlHelper::buildQuery($query);
  828. }
  829. return $uri;
  830. }
  831. /**
  832. * Returns a URL for proceeding to the next page after a requirements problem.
  833. *
  834. * This function can be called by low-level scripts (such as install.php and
  835. * update.php) and returns a URL that can be used to attempt to proceed to the
  836. * next step of the script.
  837. *
  838. * @param $severity
  839. * The severity of the requirements problem, as returned by
  840. * drupal_requirements_severity().
  841. *
  842. * @return
  843. * A URL for attempting to proceed to the next step of the script. The URL is
  844. * not sanitized, so it still needs to be run through
  845. * \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be used
  846. * as an HTML attribute value.
  847. *
  848. * @see drupal_current_script_url()
  849. * @see \Drupal\Component\Utility\UrlHelper::filterBadProtocol()
  850. */
  851. function drupal_requirements_url($severity) {
  852. $query = array();
  853. // If there are no errors, only warnings, append 'continue=1' to the URL so
  854. // the user can bypass this screen on the next page load.
  855. if ($severity == REQUIREMENT_WARNING) {
  856. $query['continue'] = 1;
  857. }
  858. return drupal_current_script_url($query);
  859. }
  860. /**
  861. * Checks an installation profile's requirements.
  862. *
  863. * @param string $profile
  864. * Name of installation profile to check.
  865. *
  866. * @return array
  867. * Array of the installation profile's requirements.
  868. */
  869. function drupal_check_profile($profile) {
  870. $info = install_profile_info($profile);
  871. // Collect requirement testing results.
  872. $requirements = array();
  873. // Performs an ExtensionDiscovery scan as the system module is unavailable and
  874. // we don't yet know where all the modules are located.
  875. // @todo Remove as part of https://www.drupal.org/node/2186491
  876. $listing = new ExtensionDiscovery(\Drupal::root());
  877. $module_list = $listing->scan('module');
  878. foreach ($info['dependencies'] as $module) {
  879. $file = \Drupal::root() . '/' . $module_list[$module]->getPath() . "/$module.install";
  880. if (is_file($file)) {
  881. require_once $file;
  882. }
  883. $function = $module . '_requirements';
  884. drupal_classloader_register($module, $module_list[$module]->getPath());
  885. if (function_exists($function)) {
  886. $requirements = array_merge($requirements, $function('install'));
  887. }
  888. }
  889. return $requirements;
  890. }
  891. /**
  892. * Extracts the highest severity from the requirements array.
  893. *
  894. * @param $requirements
  895. * An array of requirements, in the same format as is returned by
  896. * hook_requirements().
  897. *
  898. * @return
  899. * The highest severity in the array.
  900. */
  901. function drupal_requirements_severity(&$requirements) {
  902. $severity = REQUIREMENT_OK;
  903. foreach ($requirements as $requirement) {
  904. if (isset($requirement['severity'])) {
  905. $severity = max($severity, $requirement['severity']);
  906. }
  907. }
  908. return $severity;
  909. }
  910. /**
  911. * Checks a module's requirements.
  912. *
  913. * @param $module
  914. * Machine name of module to check.
  915. *
  916. * @return
  917. * TRUE or FALSE, depending on whether the requirements are met.
  918. */
  919. function drupal_check_module($module) {
  920. module_load_install($module);
  921. // Check requirements
  922. $requirements = \Drupal::moduleHandler()->invoke($module, 'requirements', array('install'));
  923. if (is_array($requirements) && drupal_requirements_severity($requirements) == REQUIREMENT_ERROR) {
  924. // Print any error messages
  925. foreach ($requirements as $requirement) {
  926. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  927. $message = $requirement['description'];
  928. if (isset($requirement['value']) && $requirement['value']) {
  929. $message = t('@requirements_message (Currently using @item version @version)', array('@requirements_message' => $requirement['description'], '@item' => $requirement['title'], '@version' => $requirement['value']));
  930. }
  931. drupal_set_message($message, 'error');
  932. }
  933. }
  934. return FALSE;
  935. }
  936. return TRUE;
  937. }
  938. /**
  939. * Retrieves information about an installation profile from its .info.yml file.
  940. *
  941. * The information stored in a profile .info.yml file is similar to that stored
  942. * in a normal Drupal module .info.yml file. For example:
  943. * - name: The real name of the installation profile for display purposes.
  944. * - description: A brief description of the profile.
  945. * - dependencies: An array of shortnames of other modules that this install
  946. * profile requires.
  947. *
  948. * Additional, less commonly-used information that can appear in a
  949. * profile.info.yml file but not in a normal Drupal module .info.yml file
  950. * includes:
  951. *
  952. * - distribution: Existence of this key denotes that the installation profile
  953. * is intended to be the only eligible choice in a distribution and will be
  954. * auto-selected during installation, whereas the installation profile
  955. * selection screen will be skipped. If more than one distribution profile is
  956. * found then the first one discovered will be selected.
  957. * The following subproperties may be set:
  958. * - name: The name of the distribution that is being installed, to be shown
  959. * throughout the installation process. If omitted,
  960. * drupal_install_profile_distribution_name() defaults to 'Drupal'.
  961. * - install: Optional parameters to override the installer:
  962. * - theme: The machine name of a theme to use in the installer instead of
  963. * Drupal's default installer theme.
  964. *
  965. * Note that this function does an expensive file system scan to get info file
  966. * information for dependencies. If you only need information from the info
  967. * file itself, use system_get_info().
  968. *
  969. * Example of .info.yml file:
  970. * @code
  971. * name = Minimal
  972. * description = Start fresh, with only a few modules enabled.
  973. * dependencies[] = block
  974. * dependencies[] = dblog
  975. * @endcode
  976. *
  977. * @param $profile
  978. * Name of profile.
  979. * @param $langcode
  980. * Language code (if any).
  981. *
  982. * @return
  983. * The info array.
  984. */
  985. function install_profile_info($profile, $langcode = 'en') {
  986. $cache = &drupal_static(__FUNCTION__, array());
  987. if (!isset($cache[$profile][$langcode])) {
  988. // Set defaults for module info.
  989. $defaults = array(
  990. 'dependencies' => array(),
  991. 'themes' => array('stark'),
  992. 'description' => '',
  993. 'version' => NULL,
  994. 'hidden' => FALSE,
  995. 'php' => DRUPAL_MINIMUM_PHP,
  996. );
  997. $profile_file = drupal_get_path('profile', $profile) . "/$profile.info.yml";
  998. $info = \Drupal::service('info_parser')->parse($profile_file);
  999. $info += $defaults;
  1000. // drupal_required_modules() includes the current profile as a dependency.
  1001. // Remove that dependency, since a module cannot depend on itself.
  1002. $required = array_diff(drupal_required_modules(), array($profile));
  1003. $locale = !empty($langcode) && $langcode != 'en' ? array('locale') : array();
  1004. $info['dependencies'] = array_unique(array_merge($required, $info['dependencies'], $locale));
  1005. $cache[$profile][$langcode] = $info;
  1006. }
  1007. return $cache[$profile][$langcode];
  1008. }
  1009. /**
  1010. * Returns a database installer object.
  1011. *
  1012. * @param $driver
  1013. * The name of the driver.
  1014. *
  1015. * @return \Drupal\Core\Database\Install\Tasks
  1016. * A class defining the requirements and tasks for installing the database.
  1017. */
  1018. function db_installer_object($driver) {
  1019. // We cannot use Database::getConnection->getDriverClass() here, because
  1020. // the connection object is not yet functional.
  1021. $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
  1022. if (class_exists($task_class)) {
  1023. return new $task_class();
  1024. }
  1025. else {
  1026. $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
  1027. return new $task_class();
  1028. }
  1029. }