Chris@0: $schema_version) {
Chris@0: if ($schema_version > -1) {
Chris@0: module_load_install($module);
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Loads the installation profile, extracting its defined distribution name.
Chris@0: *
Chris@0: * @return
Chris@0: * The distribution name defined in the profile's .info.yml file. Defaults to
Chris@0: * "Drupal" if none is explicitly provided by the installation profile.
Chris@0: *
Chris@0: * @see install_profile_info()
Chris@0: */
Chris@0: function drupal_install_profile_distribution_name() {
Chris@0: // During installation, the profile information is stored in the global
Chris@0: // installation state (it might not be saved anywhere yet).
Chris@0: $info = [];
Chris@0: if (drupal_installation_attempted()) {
Chris@0: global $install_state;
Chris@0: if (isset($install_state['profile_info'])) {
Chris@0: $info = $install_state['profile_info'];
Chris@0: }
Chris@0: }
Chris@0: // At all other times, we load the profile via standard methods.
Chris@0: else {
Chris@0: $profile = drupal_get_profile();
Chris@0: $info = system_get_info('module', $profile);
Chris@0: }
Chris@0: return isset($info['distribution']['name']) ? $info['distribution']['name'] : 'Drupal';
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Loads the installation profile, extracting its defined version.
Chris@0: *
Chris@0: * @return string
Chris@0: * Distribution version defined in the profile's .info.yml file.
Chris@0: * Defaults to \Drupal::VERSION if no version is explicitly provided by the
Chris@0: * installation profile.
Chris@0: *
Chris@0: * @see install_profile_info()
Chris@0: */
Chris@0: function drupal_install_profile_distribution_version() {
Chris@0: // During installation, the profile information is stored in the global
Chris@0: // installation state (it might not be saved anywhere yet).
Chris@0: if (drupal_installation_attempted()) {
Chris@0: global $install_state;
Chris@0: return isset($install_state['profile_info']['version']) ? $install_state['profile_info']['version'] : \Drupal::VERSION;
Chris@0: }
Chris@0: // At all other times, we load the profile via standard methods.
Chris@0: else {
Chris@0: $profile = drupal_get_profile();
Chris@0: $info = system_get_info('module', $profile);
Chris@0: return $info['version'];
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Detects all supported databases that are compiled into PHP.
Chris@0: *
Chris@0: * @return
Chris@0: * An array of database types compiled into PHP.
Chris@0: */
Chris@0: function drupal_detect_database_types() {
Chris@0: $databases = drupal_get_database_types();
Chris@0:
Chris@0: foreach ($databases as $driver => $installer) {
Chris@0: $databases[$driver] = $installer->name();
Chris@0: }
Chris@0:
Chris@0: return $databases;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Returns all supported database driver installer objects.
Chris@0: *
Chris@0: * @return \Drupal\Core\Database\Install\Tasks[]
Chris@0: * An array of available database driver installer objects.
Chris@0: */
Chris@0: function drupal_get_database_types() {
Chris@0: $databases = [];
Chris@0: $drivers = [];
Chris@0:
Chris@0: // The internal database driver name is any valid PHP identifier.
Chris@0: $mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '$/';
Chris@0: $files = file_scan_directory(DRUPAL_ROOT . '/core/lib/Drupal/Core/Database/Driver', $mask, ['recurse' => FALSE]);
Chris@0: if (is_dir(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database')) {
Chris@0: $files += file_scan_directory(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database/', $mask, ['recurse' => FALSE]);
Chris@0: }
Chris@0: foreach ($files as $file) {
Chris@0: if (file_exists($file->uri . '/Install/Tasks.php')) {
Chris@0: $drivers[$file->filename] = $file->uri;
Chris@0: }
Chris@0: }
Chris@0: foreach ($drivers as $driver => $file) {
Chris@0: $installer = db_installer_object($driver);
Chris@0: if ($installer->installable()) {
Chris@0: $databases[$driver] = $installer;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Usability: unconditionally put the MySQL driver on top.
Chris@0: if (isset($databases['mysql'])) {
Chris@0: $mysql_database = $databases['mysql'];
Chris@0: unset($databases['mysql']);
Chris@0: $databases = ['mysql' => $mysql_database] + $databases;
Chris@0: }
Chris@0:
Chris@0: return $databases;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Replaces values in settings.php with values in the submitted array.
Chris@0: *
Chris@0: * This function replaces values in place if possible, even for
Chris@0: * multidimensional arrays. This way the old settings do not linger,
Chris@0: * overridden and also the doxygen on a value remains where it should be.
Chris@0: *
Chris@0: * @param $settings
Chris@0: * An array of settings that need to be updated. Multidimensional arrays
Chris@0: * are dumped up to a stdClass object. The object can have value, required
Chris@0: * and comment properties.
Chris@0: * @code
Chris@0: * $settings['config_directories'] = array(
Chris@0: * CONFIG_SYNC_DIRECTORY => (object) array(
Chris@0: * 'value' => 'config_hash/sync',
Chris@0: * 'required' => TRUE,
Chris@0: * ),
Chris@0: * );
Chris@0: * @endcode
Chris@0: * gets dumped as:
Chris@0: * @code
Chris@0: * $config_directories['sync'] = 'config_hash/sync'
Chris@0: * @endcode
Chris@0: */
Chris@0: function drupal_rewrite_settings($settings = [], $settings_file = NULL) {
Chris@0: if (!isset($settings_file)) {
Chris@0: $settings_file = \Drupal::service('site.path') . '/settings.php';
Chris@0: }
Chris@0: // Build list of setting names and insert the values into the global namespace.
Chris@0: $variable_names = [];
Chris@0: $settings_settings = [];
Chris@0: foreach ($settings as $setting => $data) {
Chris@0: if ($setting != 'settings') {
Chris@0: _drupal_rewrite_settings_global($GLOBALS[$setting], $data);
Chris@0: }
Chris@0: else {
Chris@0: _drupal_rewrite_settings_global($settings_settings, $data);
Chris@0: }
Chris@0: $variable_names['$' . $setting] = $setting;
Chris@0: }
Chris@0: $contents = file_get_contents($settings_file);
Chris@0: if ($contents !== FALSE) {
Chris@0: // Initialize the contents for the settings.php file if it is empty.
Chris@0: if (trim($contents) === '') {
Chris@0: $contents = " $setting) {
Chris@0: $buffer .= _drupal_rewrite_settings_dump($setting, '$' . $name);
Chris@0: }
Chris@0:
Chris@0: // Write the new settings file.
Chris@0: if (file_put_contents($settings_file, $buffer) === FALSE) {
Chris@0: throw new Exception(t('Failed to modify %settings. Verify the file permissions.', ['%settings' => $settings_file]));
Chris@0: }
Chris@0: else {
Chris@0: // In case any $settings variables were written, import them into the
Chris@0: // Settings singleton.
Chris@0: if (!empty($settings_settings)) {
Chris@0: $old_settings = Settings::getAll();
Chris@0: new Settings($settings_settings + $old_settings);
Chris@0: }
Chris@0: // The existing settings.php file might have been included already. In
Chris@0: // case an opcode cache is enabled, the rewritten contents of the file
Chris@0: // will not be reflected in this process. Ensure to invalidate the file
Chris@0: // in case an opcode cache is enabled.
Chris@0: OpCodeCache::invalidate(DRUPAL_ROOT . '/' . $settings_file);
Chris@0: }
Chris@0: }
Chris@0: else {
Chris@0: throw new Exception(t('Failed to open %settings. Verify the file permissions.', ['%settings' => $settings_file]));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Helper for drupal_rewrite_settings().
Chris@0: *
Chris@0: * Checks whether this token represents a scalar or NULL.
Chris@0: *
Chris@0: * @param int $type
Chris@0: * The token type.
Chris@0: * @param string $value
Chris@0: * The value of the token.
Chris@0: *
Chris@0: * @return bool
Chris@0: * TRUE if this token represents a scalar or NULL.
Chris@0: *
Chris@0: * @see token_name()
Chris@0: */
Chris@0: function _drupal_rewrite_settings_is_simple($type, $value) {
Chris@0: $is_integer = $type == T_LNUMBER;
Chris@0: $is_float = $type == T_DNUMBER;
Chris@0: $is_string = $type == T_CONSTANT_ENCAPSED_STRING;
Chris@0: $is_boolean_or_null = $type == T_STRING && in_array(strtoupper($value), ['TRUE', 'FALSE', 'NULL']);
Chris@0: return $is_integer || $is_float || $is_string || $is_boolean_or_null;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Helper for drupal_rewrite_settings().
Chris@0: *
Chris@0: * Checks whether this token represents a valid array index: a number or a
Chris@0: * string.
Chris@0: *
Chris@0: * @param int $type
Chris@0: * The token type.
Chris@0: *
Chris@0: * @return bool
Chris@0: * TRUE if this token represents a number or a string.
Chris@0: *
Chris@0: * @see token_name()
Chris@0: */
Chris@0: function _drupal_rewrite_settings_is_array_index($type) {
Chris@0: $is_integer = $type == T_LNUMBER;
Chris@0: $is_float = $type == T_DNUMBER;
Chris@0: $is_string = $type == T_CONSTANT_ENCAPSED_STRING;
Chris@0: return $is_integer || $is_float || $is_string;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Helper for drupal_rewrite_settings().
Chris@0: *
Chris@0: * Makes the new settings global.
Chris@0: *
Chris@0: * @param array|null $ref
Chris@0: * A reference to a nested index in $GLOBALS.
Chris@0: * @param array|object $variable
Chris@0: * The nested value of the setting being copied.
Chris@0: */
Chris@0: function _drupal_rewrite_settings_global(&$ref, $variable) {
Chris@0: if (is_object($variable)) {
Chris@0: $ref = $variable->value;
Chris@0: }
Chris@0: else {
Chris@0: foreach ($variable as $k => $v) {
Chris@0: _drupal_rewrite_settings_global($ref[$k], $v);
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Helper for drupal_rewrite_settings().
Chris@0: *
Chris@0: * Dump the relevant value properties.
Chris@0: *
Chris@0: * @param array|object $variable
Chris@0: * The container for variable values.
Chris@0: * @param string $variable_name
Chris@0: * Name of variable.
Chris@0: * @return string
Chris@0: * A string containing valid PHP code of the variable suitable for placing
Chris@0: * into settings.php.
Chris@0: */
Chris@0: function _drupal_rewrite_settings_dump($variable, $variable_name) {
Chris@0: $return = '';
Chris@0: if (is_object($variable)) {
Chris@0: if (!empty($variable->required)) {
Chris@0: $return .= _drupal_rewrite_settings_dump_one($variable, "$variable_name = ", "\n");
Chris@0: }
Chris@0: }
Chris@0: else {
Chris@0: foreach ($variable as $k => $v) {
Chris@0: $return .= _drupal_rewrite_settings_dump($v, $variable_name . "['" . $k . "']");
Chris@0: }
Chris@0: }
Chris@0: return $return;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Helper for drupal_rewrite_settings().
Chris@0: *
Chris@0: * Dump the value of a value property and adds the comment if it exists.
Chris@0: *
Chris@0: * @param object $variable
Chris@0: * A stdClass object with at least a value property.
Chris@0: * @param string $prefix
Chris@0: * A string to prepend to the variable's value.
Chris@0: * @param string $suffix
Chris@0: * A string to append to the variable's value.
Chris@0: * @return string
Chris@0: * A string containing valid PHP code of the variable suitable for placing
Chris@0: * into settings.php.
Chris@0: */
Chris@0: function _drupal_rewrite_settings_dump_one(\stdClass $variable, $prefix = '', $suffix = '') {
Chris@0: $return = $prefix . var_export($variable->value, TRUE) . ';';
Chris@0: if (!empty($variable->comment)) {
Chris@0: $return .= ' // ' . $variable->comment;
Chris@0: }
Chris@0: $return .= $suffix;
Chris@0: return $return;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Creates the config directory and ensures it is operational.
Chris@0: *
Chris@0: * @see install_settings_form_submit()
Chris@0: * @see update_prepare_d8_bootstrap()
Chris@0: */
Chris@0: function drupal_install_config_directories() {
Chris@17: global $config_directories, $install_state;
Chris@0:
Chris@17: // If settings.php does not contain a config sync directory name we need to
Chris@17: // configure one.
Chris@0: if (empty($config_directories[CONFIG_SYNC_DIRECTORY])) {
Chris@17: if (empty($install_state['config_install_path'])) {
Chris@17: // Add a randomized config directory name to settings.php
Chris@17: $config_directories[CONFIG_SYNC_DIRECTORY] = \Drupal::service('site.path') . '/files/config_' . Crypt::randomBytesBase64(55) . '/sync';
Chris@17: }
Chris@17: else {
Chris@17: // Install profiles can contain a config sync directory. If they do,
Chris@17: // 'config_install_path' is a path to the directory.
Chris@17: $config_directories[CONFIG_SYNC_DIRECTORY] = $install_state['config_install_path'];
Chris@17: }
Chris@0: $settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [
Chris@0: 'value' => $config_directories[CONFIG_SYNC_DIRECTORY],
Chris@0: 'required' => TRUE,
Chris@0: ];
Chris@0: // Rewrite settings.php, which also sets the value as global variable.
Chris@0: drupal_rewrite_settings($settings);
Chris@0: }
Chris@0:
Chris@0: // This should never fail, since if the config directory was specified in
Chris@0: // settings.php it will have already been created and verified earlier, and
Chris@0: // if it wasn't specified in settings.php, it is created here inside the
Chris@0: // public files directory, which has already been verified to be writable
Chris@0: // itself. But if it somehow fails anyway, the installation cannot proceed.
Chris@0: // Bail out using a similar error message as in system_requirements().
Chris@18: if (!\Drupal::service('file_system')->prepareDirectory($config_directories[CONFIG_SYNC_DIRECTORY], FileSystemInterface::CREATE_DIRECTORY)
Chris@0: && !file_exists($config_directories[CONFIG_SYNC_DIRECTORY])) {
Chris@0: 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 online handbook.', [
Chris@0: '%directory' => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
Chris@0: ':handbook_url' => 'https://www.drupal.org/server-permissions',
Chris@0: ]));
Chris@0: }
Chris@0: elseif (is_writable($config_directories[CONFIG_SYNC_DIRECTORY])) {
Chris@0: // Put a README.txt into the sync config directory. This is required so that
Chris@0: // they can later be added to git. Since this directory is auto-created, we
Chris@0: // have to write out the README rather than just adding it to the drupal core
Chris@0: // repo.
Chris@0: $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';
Chris@18: file_put_contents(config_get_config_directory(CONFIG_SYNC_DIRECTORY) . '/README.txt', "$text\n");
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Ensures that the config directory exists and is writable, or can be made so.
Chris@0: *
Chris@0: * @param string $type
Chris@0: * Type of config directory to return. Drupal core provides 'sync'.
Chris@0: *
Chris@0: * @return bool
Chris@0: * TRUE if the config directory exists and is writable.
Chris@0: *
Chris@0: * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.x. Use
Chris@18: * config_get_config_directory() and
Chris@18: * \Drupal\Core\File\FileSystemInterface::prepareDirectory() instead.
Chris@0: *
Chris@0: * @see https://www.drupal.org/node/2501187
Chris@0: */
Chris@0: function install_ensure_config_directory($type) {
Chris@18: @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 \Drupal\Core\File\FileSystemInterface::prepareDirectory() instead. See https://www.drupal.org/node/2501187.', E_USER_DEPRECATED);
Chris@0: // The config directory must be defined in settings.php.
Chris@0: global $config_directories;
Chris@0: if (!isset($config_directories[$type])) {
Chris@0: return FALSE;
Chris@0: }
Chris@0: // The logic here is similar to that used by system_requirements() for other
Chris@0: // directories that the installer creates.
Chris@0: else {
Chris@0: $config_directory = config_get_config_directory($type);
Chris@18: return \Drupal::service('file_system')->prepareDirectory($config_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Verifies that all dependencies are met for a given installation profile.
Chris@0: *
Chris@0: * @param $install_state
Chris@0: * An array of information about the current installation state.
Chris@0: *
Chris@0: * @return
Chris@0: * The list of modules to install.
Chris@0: */
Chris@0: function drupal_verify_profile($install_state) {
Chris@0: $profile = $install_state['parameters']['profile'];
Chris@0: $info = $install_state['profile_info'];
Chris@0:
Chris@0: // Get the list of available modules for the selected installation profile.
Chris@0: $listing = new ExtensionDiscovery(\Drupal::root());
Chris@0: $present_modules = [];
Chris@0: foreach ($listing->scan('module') as $present_module) {
Chris@0: $present_modules[] = $present_module->getName();
Chris@0: }
Chris@0:
Chris@0: // The installation profile is also a module, which needs to be installed
Chris@0: // after all the other dependencies have been installed.
Chris@0: $present_modules[] = $profile;
Chris@0:
Chris@0: // Verify that all of the profile's required modules are present.
Chris@17: $missing_modules = array_diff($info['install'], $present_modules);
Chris@0:
Chris@0: $requirements = [];
Chris@0:
Chris@0: if ($missing_modules) {
Chris@0: $build = [
Chris@0: '#theme' => 'item_list',
Chris@0: '#context' => ['list_style' => 'comma-list'],
Chris@0: ];
Chris@0:
Chris@0: foreach ($missing_modules as $module) {
Chris@0: $build['#items'][] = ['#markup' => '' . Unicode::ucfirst($module) . ''];
Chris@0: }
Chris@0:
Chris@0: $modules_list = \Drupal::service('renderer')->renderPlain($build);
Chris@0: $requirements['required_modules'] = [
Chris@0: 'title' => t('Required modules'),
Chris@0: 'value' => t('Required modules not found.'),
Chris@0: 'severity' => REQUIREMENT_ERROR,
Chris@0: 'description' => t('The following modules are required but were not found. Move them into the appropriate modules subdirectory, such as /modules. Missing modules: @modules', ['@modules' => $modules_list]),
Chris@0: ];
Chris@0: }
Chris@0: return $requirements;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Installs the system module.
Chris@0: *
Chris@0: * Separated from the installation of other modules so core system
Chris@0: * functions can be made available while other modules are installed.
Chris@0: *
Chris@0: * @param array $install_state
Chris@0: * An array of information about the current installation state. This is used
Chris@0: * to set the default language.
Chris@0: */
Chris@0: function drupal_install_system($install_state) {
Chris@0: // Remove the service provider of the early installer.
Chris@0: unset($GLOBALS['conf']['container_service_providers']['InstallerServiceProvider']);
Chris@17: // Add the normal installer service provider.
Chris@17: $GLOBALS['conf']['container_service_providers']['InstallerServiceProvider'] = 'Drupal\Core\Installer\NormalInstallerServiceProvider';
Chris@0:
Chris@0: $request = \Drupal::request();
Chris@0: // Reboot into a full production environment to continue the installation.
Chris@0: /** @var \Drupal\Core\Installer\InstallerKernel $kernel */
Chris@0: $kernel = \Drupal::service('kernel');
Chris@0: $kernel->shutdown();
Chris@0: // Have installer rebuild from the disk, rather then building from scratch.
Chris@0: $kernel->rebuildContainer(FALSE);
Chris@0: $kernel->prepareLegacyRequest($request);
Chris@0:
Chris@17: // Before having installed the system module and being able to do a module
Chris@17: // rebuild, prime the \Drupal\Core\Extension\ModuleExtensionList static cache
Chris@17: // with the module's location.
Chris@17: // @todo Try to install system as any other module, see
Chris@17: // https://www.drupal.org/node/2719315.
Chris@17: \Drupal::service('extension.list.module')->setPathname('system', 'core/modules/system/system.info.yml');
Chris@17:
Chris@0: // Install base system configuration.
Chris@0: \Drupal::service('config.installer')->installDefaultConfig('core', 'core');
Chris@0:
Chris@0: // Store the installation profile in configuration to populate the
Chris@0: // 'install_profile' container parameter.
Chris@0: \Drupal::configFactory()->getEditable('core.extension')
Chris@0: ->set('profile', $install_state['parameters']['profile'])
Chris@0: ->save();
Chris@0:
Chris@0: // Install System module and rebuild the newly available routes.
Chris@0: $kernel->getContainer()->get('module_installer')->install(['system'], FALSE);
Chris@0: \Drupal::service('router.builder')->rebuild();
Chris@0:
Chris@0: // Ensure default language is saved.
Chris@0: if (isset($install_state['parameters']['langcode'])) {
Chris@0: \Drupal::configFactory()->getEditable('system.site')
Chris@0: ->set('langcode', (string) $install_state['parameters']['langcode'])
Chris@0: ->set('default_langcode', (string) $install_state['parameters']['langcode'])
Chris@0: ->save(TRUE);
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Verifies the state of the specified file.
Chris@0: *
Chris@0: * @param $file
Chris@0: * The file to check for.
Chris@0: * @param $mask
Chris@0: * An optional bitmask created from various FILE_* constants.
Chris@0: * @param $type
Chris@0: * The type of file. Can be file (default), dir, or link.
Chris@17: * @param bool $autofix
Chris@17: * (optional) Determines whether to attempt fixing the permissions according
Chris@17: * to the provided $mask. Defaults to TRUE.
Chris@0: *
Chris@0: * @return
Chris@0: * TRUE on success or FALSE on failure. A message is set for the latter.
Chris@0: */
Chris@17: function drupal_verify_install_file($file, $mask = NULL, $type = 'file', $autofix = TRUE) {
Chris@0: $return = TRUE;
Chris@0: // Check for files that shouldn't be there.
Chris@0: if (isset($mask) && ($mask & FILE_NOT_EXIST) && file_exists($file)) {
Chris@0: return FALSE;
Chris@0: }
Chris@0: // Verify that the file is the type of file it is supposed to be.
Chris@0: if (isset($type) && file_exists($file)) {
Chris@0: $check = 'is_' . $type;
Chris@0: if (!function_exists($check) || !$check($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Verify file permissions.
Chris@0: if (isset($mask)) {
Chris@0: $masks = [FILE_EXIST, FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
Chris@0: foreach ($masks as $current_mask) {
Chris@0: if ($mask & $current_mask) {
Chris@0: switch ($current_mask) {
Chris@0: case FILE_EXIST:
Chris@0: if (!file_exists($file)) {
Chris@17: if ($type == 'dir' && $autofix) {
Chris@0: drupal_install_mkdir($file, $mask);
Chris@0: }
Chris@0: if (!file_exists($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: }
Chris@0: break;
Chris@0: case FILE_READABLE:
Chris@17: if (!is_readable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_WRITABLE:
Chris@17: if (!is_writable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_EXECUTABLE:
Chris@17: if (!is_executable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_READABLE:
Chris@17: if (is_readable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_WRITABLE:
Chris@17: if (is_writable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_EXECUTABLE:
Chris@17: if (is_executable($file)) {
Chris@0: $return = FALSE;
Chris@0: }
Chris@0: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0: }
Chris@17: if (!$return && $autofix) {
Chris@17: return drupal_install_fix_file($file, $mask);
Chris@17: }
Chris@0: return $return;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Creates a directory with the specified permissions.
Chris@0: *
Chris@0: * @param $file
Chris@0: * The name of the directory to create;
Chris@0: * @param $mask
Chris@0: * The permissions of the directory to create.
Chris@0: * @param $message
Chris@0: * (optional) Whether to output messages. Defaults to TRUE.
Chris@0: *
Chris@0: * @return
Chris@0: * TRUE/FALSE whether or not the directory was successfully created.
Chris@0: */
Chris@0: function drupal_install_mkdir($file, $mask, $message = TRUE) {
Chris@0: $mod = 0;
Chris@0: $masks = [FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
Chris@0: foreach ($masks as $m) {
Chris@0: if ($mask & $m) {
Chris@0: switch ($m) {
Chris@0: case FILE_READABLE:
Chris@0: $mod |= 0444;
Chris@0: break;
Chris@0: case FILE_WRITABLE:
Chris@0: $mod |= 0222;
Chris@0: break;
Chris@0: case FILE_EXECUTABLE:
Chris@0: $mod |= 0111;
Chris@0: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@18: if (@\Drupal::service('file_system')->mkdir($file, $mod)) {
Chris@0: return TRUE;
Chris@0: }
Chris@0: else {
Chris@0: return FALSE;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Attempts to fix file permissions.
Chris@0: *
Chris@0: * The general approach here is that, because we do not know the security
Chris@0: * setup of the webserver, we apply our permission changes to all three
Chris@0: * digits of the file permission (i.e. user, group and all).
Chris@0: *
Chris@0: * To ensure that the values behave as expected (and numbers don't carry
Chris@0: * from one digit to the next) we do the calculation on the octal value
Chris@0: * using bitwise operations. This lets us remove, for example, 0222 from
Chris@0: * 0700 and get the correct value of 0500.
Chris@0: *
Chris@0: * @param $file
Chris@0: * The name of the file with permissions to fix.
Chris@0: * @param $mask
Chris@0: * The desired permissions for the file.
Chris@0: * @param $message
Chris@0: * (optional) Whether to output messages. Defaults to TRUE.
Chris@0: *
Chris@0: * @return
Chris@0: * TRUE/FALSE whether or not we were able to fix the file's permissions.
Chris@0: */
Chris@0: function drupal_install_fix_file($file, $mask, $message = TRUE) {
Chris@0: // If $file does not exist, fileperms() issues a PHP warning.
Chris@0: if (!file_exists($file)) {
Chris@0: return FALSE;
Chris@0: }
Chris@0:
Chris@0: $mod = fileperms($file) & 0777;
Chris@0: $masks = [FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE];
Chris@0:
Chris@0: // FILE_READABLE, FILE_WRITABLE, and FILE_EXECUTABLE permission strings
Chris@0: // can theoretically be 0400, 0200, and 0100 respectively, but to be safe
Chris@0: // we set all three access types in case the administrator intends to
Chris@0: // change the owner of settings.php after installation.
Chris@0: foreach ($masks as $m) {
Chris@0: if ($mask & $m) {
Chris@0: switch ($m) {
Chris@0: case FILE_READABLE:
Chris@0: if (!is_readable($file)) {
Chris@0: $mod |= 0444;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_WRITABLE:
Chris@0: if (!is_writable($file)) {
Chris@0: $mod |= 0222;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_EXECUTABLE:
Chris@0: if (!is_executable($file)) {
Chris@0: $mod |= 0111;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_READABLE:
Chris@0: if (is_readable($file)) {
Chris@0: $mod &= ~0444;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_WRITABLE:
Chris@0: if (is_writable($file)) {
Chris@0: $mod &= ~0222;
Chris@0: }
Chris@0: break;
Chris@0: case FILE_NOT_EXECUTABLE:
Chris@0: if (is_executable($file)) {
Chris@0: $mod &= ~0111;
Chris@0: }
Chris@0: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // chmod() will work if the web server is running as owner of the file.
Chris@0: if (@chmod($file, $mod)) {
Chris@0: return TRUE;
Chris@0: }
Chris@0: else {
Chris@0: return FALSE;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Sends the user to a different installer page.
Chris@0: *
Chris@0: * This issues an on-site HTTP redirect. Messages (and errors) are erased.
Chris@0: *
Chris@0: * @param $path
Chris@0: * An installer path.
Chris@0: */
Chris@0: function install_goto($path) {
Chris@0: global $base_url;
Chris@0: $headers = [
Chris@0: // Not a permanent redirect.
Chris@0: 'Cache-Control' => 'no-cache',
Chris@0: ];
Chris@0: $response = new RedirectResponse($base_url . '/' . $path, 302, $headers);
Chris@0: $response->send();
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Returns the URL of the current script, with modified query parameters.
Chris@0: *
Chris@0: * This function can be called by low-level scripts (such as install.php and
Chris@0: * update.php) and returns the URL of the current script. Existing query
Chris@0: * parameters are preserved by default, but new ones can optionally be merged
Chris@0: * in.
Chris@0: *
Chris@0: * This function is used when the script must maintain certain query parameters
Chris@0: * over multiple page requests in order to work correctly. In such cases (for
Chris@0: * example, update.php, which requires the 'continue=1' parameter to remain in
Chris@0: * the URL throughout the update process if there are any requirement warnings
Chris@0: * that need to be bypassed), using this function to generate the URL for links
Chris@0: * to the next steps of the script ensures that the links will work correctly.
Chris@0: *
Chris@0: * @param $query
Chris@0: * (optional) An array of query parameters to merge in to the existing ones.
Chris@0: *
Chris@0: * @return
Chris@0: * The URL of the current script, with query parameters modified by the
Chris@0: * passed-in $query. The URL is not sanitized, so it still needs to be run
Chris@0: * through \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be
Chris@0: * used as an HTML attribute value.
Chris@0: *
Chris@0: * @see drupal_requirements_url()
Chris@0: * @see Drupal\Component\Utility\UrlHelper::filterBadProtocol()
Chris@0: */
Chris@0: function drupal_current_script_url($query = []) {
Chris@0: $uri = $_SERVER['SCRIPT_NAME'];
Chris@0: $query = array_merge(UrlHelper::filterQueryParameters(\Drupal::request()->query->all()), $query);
Chris@0: if (!empty($query)) {
Chris@0: $uri .= '?' . UrlHelper::buildQuery($query);
Chris@0: }
Chris@0: return $uri;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Returns a URL for proceeding to the next page after a requirements problem.
Chris@0: *
Chris@0: * This function can be called by low-level scripts (such as install.php and
Chris@0: * update.php) and returns a URL that can be used to attempt to proceed to the
Chris@0: * next step of the script.
Chris@0: *
Chris@0: * @param $severity
Chris@0: * The severity of the requirements problem, as returned by
Chris@0: * drupal_requirements_severity().
Chris@0: *
Chris@0: * @return
Chris@0: * A URL for attempting to proceed to the next step of the script. The URL is
Chris@0: * not sanitized, so it still needs to be run through
Chris@0: * \Drupal\Component\Utility\UrlHelper::filterBadProtocol() if it will be used
Chris@0: * as an HTML attribute value.
Chris@0: *
Chris@0: * @see drupal_current_script_url()
Chris@0: * @see \Drupal\Component\Utility\UrlHelper::filterBadProtocol()
Chris@0: */
Chris@0: function drupal_requirements_url($severity) {
Chris@0: $query = [];
Chris@0: // If there are no errors, only warnings, append 'continue=1' to the URL so
Chris@0: // the user can bypass this screen on the next page load.
Chris@0: if ($severity == REQUIREMENT_WARNING) {
Chris@0: $query['continue'] = 1;
Chris@0: }
Chris@0: return drupal_current_script_url($query);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks an installation profile's requirements.
Chris@0: *
Chris@0: * @param string $profile
Chris@0: * Name of installation profile to check.
Chris@0: *
Chris@0: * @return array
Chris@0: * Array of the installation profile's requirements.
Chris@0: */
Chris@0: function drupal_check_profile($profile) {
Chris@0: $info = install_profile_info($profile);
Chris@0: // Collect requirement testing results.
Chris@0: $requirements = [];
Chris@0: // Performs an ExtensionDiscovery scan as the system module is unavailable and
Chris@0: // we don't yet know where all the modules are located.
Chris@0: // @todo Remove as part of https://www.drupal.org/node/2186491
Chris@0: $drupal_root = \Drupal::root();
Chris@0: $module_list = (new ExtensionDiscovery($drupal_root))->scan('module');
Chris@0:
Chris@17: foreach ($info['install'] as $module) {
Chris@0: // If the module is in the module list we know it exists and we can continue
Chris@0: // including and registering it.
Chris@0: // @see \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory()
Chris@0: if (isset($module_list[$module])) {
Chris@0: $function = $module . '_requirements';
Chris@0: $module_path = $module_list[$module]->getPath();
Chris@0: $install_file = "$drupal_root/$module_path/$module.install";
Chris@0:
Chris@0: if (is_file($install_file)) {
Chris@0: require_once $install_file;
Chris@0: }
Chris@0:
Chris@0: drupal_classloader_register($module, $module_path);
Chris@0:
Chris@0: if (function_exists($function)) {
Chris@0: $requirements = array_merge($requirements, $function('install'));
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@17: // Add the profile requirements.
Chris@17: $function = $profile . '_requirements';
Chris@17: if (function_exists($function)) {
Chris@17: $requirements = array_merge($requirements, $function('install'));
Chris@17: }
Chris@17:
Chris@0: return $requirements;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Extracts the highest severity from the requirements array.
Chris@0: *
Chris@0: * @param $requirements
Chris@0: * An array of requirements, in the same format as is returned by
Chris@0: * hook_requirements().
Chris@0: *
Chris@0: * @return
Chris@0: * The highest severity in the array.
Chris@0: */
Chris@0: function drupal_requirements_severity(&$requirements) {
Chris@0: $severity = REQUIREMENT_OK;
Chris@0: foreach ($requirements as $requirement) {
Chris@0: if (isset($requirement['severity'])) {
Chris@0: $severity = max($severity, $requirement['severity']);
Chris@0: }
Chris@0: }
Chris@0: return $severity;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks a module's requirements.
Chris@0: *
Chris@0: * @param $module
Chris@0: * Machine name of module to check.
Chris@0: *
Chris@0: * @return
Chris@0: * TRUE or FALSE, depending on whether the requirements are met.
Chris@0: */
Chris@0: function drupal_check_module($module) {
Chris@0: module_load_install($module);
Chris@0: // Check requirements
Chris@0: $requirements = \Drupal::moduleHandler()->invoke($module, 'requirements', ['install']);
Chris@0: if (is_array($requirements) && drupal_requirements_severity($requirements) == REQUIREMENT_ERROR) {
Chris@0: // Print any error messages
Chris@0: foreach ($requirements as $requirement) {
Chris@0: if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
Chris@0: $message = $requirement['description'];
Chris@0: if (isset($requirement['value']) && $requirement['value']) {
Chris@0: $message = t('@requirements_message (Currently using @item version @version)', ['@requirements_message' => $requirement['description'], '@item' => $requirement['title'], '@version' => $requirement['value']]);
Chris@0: }
Chris@17: \Drupal::messenger()->addError($message);
Chris@0: }
Chris@0: }
Chris@0: return FALSE;
Chris@0: }
Chris@0: return TRUE;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Retrieves information about an installation profile from its .info.yml file.
Chris@0: *
Chris@0: * The information stored in a profile .info.yml file is similar to that stored
Chris@0: * in a normal Drupal module .info.yml file. For example:
Chris@0: * - name: The real name of the installation profile for display purposes.
Chris@0: * - description: A brief description of the profile.
Chris@0: * - dependencies: An array of shortnames of other modules that this install
Chris@0: * profile requires.
Chris@17: * - install: An array of shortname of other modules to install that are not
Chris@17: * required by this install profile.
Chris@0: *
Chris@0: * Additional, less commonly-used information that can appear in a
Chris@0: * profile.info.yml file but not in a normal Drupal module .info.yml file
Chris@0: * includes:
Chris@0: *
Chris@0: * - distribution: Existence of this key denotes that the installation profile
Chris@0: * is intended to be the only eligible choice in a distribution and will be
Chris@0: * auto-selected during installation, whereas the installation profile
Chris@0: * selection screen will be skipped. If more than one distribution profile is
Chris@0: * found then the first one discovered will be selected.
Chris@0: * The following subproperties may be set:
Chris@0: * - name: The name of the distribution that is being installed, to be shown
Chris@0: * throughout the installation process. If omitted,
Chris@0: * drupal_install_profile_distribution_name() defaults to 'Drupal'.
Chris@0: * - install: Optional parameters to override the installer:
Chris@0: * - theme: The machine name of a theme to use in the installer instead of
Chris@0: * Drupal's default installer theme.
Chris@17: * - finish_url: A destination to visit after the installation of the
Chris@17: * distribution is finished
Chris@0: *
Chris@0: * Note that this function does an expensive file system scan to get info file
Chris@0: * information for dependencies. If you only need information from the info
Chris@0: * file itself, use system_get_info().
Chris@0: *
Chris@0: * Example of .info.yml file:
Chris@0: * @code
Chris@0: * name: Minimal
Chris@0: * description: Start fresh, with only a few modules enabled.
Chris@17: * install:
Chris@0: * - block
Chris@0: * - dblog
Chris@0: * @endcode
Chris@0: *
Chris@0: * @param $profile
Chris@0: * Name of profile.
Chris@0: * @param $langcode
Chris@0: * Language code (if any).
Chris@0: *
Chris@0: * @return
Chris@0: * The info array.
Chris@0: */
Chris@0: function install_profile_info($profile, $langcode = 'en') {
Chris@0: $cache = &drupal_static(__FUNCTION__, []);
Chris@0:
Chris@0: if (!isset($cache[$profile][$langcode])) {
Chris@0: // Set defaults for module info.
Chris@0: $defaults = [
Chris@0: 'dependencies' => [],
Chris@17: 'install' => [],
Chris@0: 'themes' => ['stark'],
Chris@0: 'description' => '',
Chris@0: 'version' => NULL,
Chris@0: 'hidden' => FALSE,
Chris@0: 'php' => DRUPAL_MINIMUM_PHP,
Chris@17: 'config_install_path' => NULL,
Chris@0: ];
Chris@17: $profile_path = drupal_get_path('profile', $profile);
Chris@17: $info = \Drupal::service('info_parser')->parse("$profile_path/$profile.info.yml");
Chris@0: $info += $defaults;
Chris@0:
Chris@18: $dependency_name_function = function ($dependency) {
Chris@18: return Dependency::createFromString($dependency)->getName();
Chris@18: };
Chris@17: // Convert dependencies in [project:module] format.
Chris@18: $info['dependencies'] = array_map($dependency_name_function, $info['dependencies']);
Chris@17:
Chris@17: // Convert install key in [project:module] format.
Chris@18: $info['install'] = array_map($dependency_name_function, $info['install']);
Chris@17:
Chris@0: // drupal_required_modules() includes the current profile as a dependency.
Chris@0: // Remove that dependency, since a module cannot depend on itself.
Chris@0: $required = array_diff(drupal_required_modules(), [$profile]);
Chris@0:
Chris@0: $locale = !empty($langcode) && $langcode != 'en' ? ['locale'] : [];
Chris@0:
Chris@17: // Merge dependencies, required modules and locale into install list and
Chris@17: // remove any duplicates.
Chris@17: $info['install'] = array_unique(array_merge($info['install'], $required, $info['dependencies'], $locale));
Chris@0:
Chris@17: // If the profile has a config/sync directory use that to install drupal.
Chris@17: if (is_dir($profile_path . '/config/sync')) {
Chris@17: $info['config_install_path'] = $profile_path . '/config/sync';
Chris@17: }
Chris@0: $cache[$profile][$langcode] = $info;
Chris@0: }
Chris@0: return $cache[$profile][$langcode];
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Returns a database installer object.
Chris@0: *
Chris@0: * @param $driver
Chris@0: * The name of the driver.
Chris@0: *
Chris@0: * @return \Drupal\Core\Database\Install\Tasks
Chris@0: * A class defining the requirements and tasks for installing the database.
Chris@0: */
Chris@0: function db_installer_object($driver) {
Chris@0: // We cannot use Database::getConnection->getDriverClass() here, because
Chris@0: // the connection object is not yet functional.
Chris@0: $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
Chris@0: if (class_exists($task_class)) {
Chris@0: return new $task_class();
Chris@0: }
Chris@0: else {
Chris@0: $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
Chris@0: return new $task_class();
Chris@0: }
Chris@0: }