Chris@0: ' . t('About') . ''; Chris@18: $output .= '
' . t('The Update Manager module periodically checks for new versions of your site\'s software (including contributed modules and themes), and alerts administrators to available updates. The Update Manager system is also used by some other modules to manage updates and downloads; for example, the Interface Translation module uses the Update Manager to download translations from the localization server. Note that whenever the Update Manager system is used, anonymous usage statistics are sent to Drupal.org. If desired, you may disable the Update Manager module from the Extend page; if you do so, functionality that depends on the Update Manager system will not work. For more information, see the online documentation for the Update Manager module.', [':update' => 'https://www.drupal.org/documentation/modules/update', ':modules' => Url::fromRoute('system.modules_list')->toString()]) . '
'; Chris@0: // Only explain the Update manager if it has not been disabled. Chris@0: if (_update_manager_access()) { Chris@0: $output .= '' . t('The Update Manager also allows administrators to update and install modules and themes through the administration interface.') . '
'; Chris@0: } Chris@0: $output .= '' . t('Here you can find information about available updates for your installed modules and themes. Note that each module or theme is part of a "project", which may or may not have the same name, and might include multiple modules or themes within it.') . '
'; Chris@0: Chris@0: case 'system.modules_list': Chris@0: if (_update_manager_access()) { Chris@18: $output = '' . t('Regularly review and install available updates to maintain a secure and current site. Always run the update script each time a module is updated.', [':update-php' => Url::fromRoute('system.db_update')->toString(), ':updates' => Url::fromRoute('update.status')->toString()]) . '
'; Chris@0: } Chris@0: else { Chris@18: $output = '' . t('Regularly review available updates to maintain a secure and current site. Always run the update script each time a module is updated.', [':update-php' => Url::fromRoute('system.db_update')->toString(), ':updates' => Url::fromRoute('update.status')->toString()]) . '
'; Chris@0: } Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_page_top(). Chris@0: */ Chris@0: function update_page_top() { Chris@0: /** @var \Drupal\Core\Routing\AdminContext $admin_context */ Chris@0: $admin_context = \Drupal::service('router.admin_context'); Chris@0: $route_match = \Drupal::routeMatch(); Chris@0: if ($admin_context->isAdminRoute($route_match->getRouteObject()) && \Drupal::currentUser()->hasPermission('administer site configuration')) { Chris@0: $route_name = \Drupal::routeMatch()->getRouteName(); Chris@0: switch ($route_name) { Chris@0: // These pages don't need additional nagging. Chris@0: case 'update.theme_update': Chris@0: case 'system.theme_install': Chris@0: case 'update.module_update': Chris@0: case 'update.module_install': Chris@0: case 'update.status': Chris@0: case 'update.report_update': Chris@0: case 'update.report_install': Chris@0: case 'update.settings': Chris@0: case 'system.status': Chris@0: case 'update.confirmation_page': Chris@0: return; Chris@0: Chris@0: // If we are on the appearance or modules list, display a detailed report Chris@0: // of the update status. Chris@0: case 'system.themes_page': Chris@0: case 'system.modules_list': Chris@0: $verbose = TRUE; Chris@0: break; Chris@0: Chris@0: } Chris@0: module_load_install('update'); Chris@0: $status = update_requirements('runtime'); Chris@0: foreach (['core', 'contrib'] as $report_type) { Chris@0: $type = 'update_' . $report_type; Chris@0: // hook_requirements() supports render arrays therefore we need to render Chris@17: // them before using Chris@17: // \Drupal\Core\Messenger\MessengerInterface::addStatus(). Chris@0: if (isset($status[$type]['description']) && is_array($status[$type]['description'])) { Chris@0: $status[$type]['description'] = \Drupal::service('renderer')->renderPlain($status[$type]['description']); Chris@0: } Chris@0: if (!empty($verbose)) { Chris@0: if (isset($status[$type]['severity'])) { Chris@0: if ($status[$type]['severity'] == REQUIREMENT_ERROR) { Chris@17: \Drupal::messenger()->addError($status[$type]['description']); Chris@0: } Chris@0: elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) { Chris@17: \Drupal::messenger()->addWarning($status[$type]['description']); Chris@0: } Chris@0: } Chris@0: } Chris@0: // Otherwise, if we're on *any* admin page and there's a security Chris@0: // update missing, print an error message about it. Chris@0: else { Chris@0: if (isset($status[$type]) Chris@0: && isset($status[$type]['reason']) Chris@0: && $status[$type]['reason'] === UPDATE_NOT_SECURE) { Chris@17: \Drupal::messenger()->addError($status[$type]['description']); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves if the current user can access updater menu items. Chris@0: * Chris@0: * It both enforces the 'administer software updates' permission and the global Chris@0: * kill switch for the authorize.php script. Chris@0: * Chris@0: * @return Chris@0: * TRUE if the current user can access the updater menu items; FALSE Chris@0: * otherwise. Chris@0: */ Chris@0: function _update_manager_access() { Chris@0: return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme(). Chris@0: */ Chris@0: function update_theme() { Chris@0: return [ Chris@0: 'update_last_check' => [ Chris@0: 'variables' => ['last' => 0], Chris@0: ], Chris@0: 'update_report' => [ Chris@0: 'variables' => ['data' => NULL], Chris@0: 'file' => 'update.report.inc', Chris@0: ], Chris@0: 'update_project_status' => [ Chris@0: 'variables' => ['project' => []], Chris@0: 'file' => 'update.report.inc', Chris@0: ], Chris@0: // We are using template instead of '#type' => 'table' here to keep markup Chris@0: // out of preprocess and allow for easier changes to markup. Chris@0: 'update_version' => [ Chris@0: 'variables' => ['version' => NULL, 'title' => NULL, 'attributes' => []], Chris@0: 'file' => 'update.report.inc', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_cron(). Chris@0: */ Chris@0: function update_cron() { Chris@0: $update_config = \Drupal::config('update.settings'); Chris@0: $frequency = $update_config->get('check.interval_days'); Chris@0: $interval = 60 * 60 * 24 * $frequency; Chris@0: $last_check = \Drupal::state()->get('update.last_check') ?: 0; Chris@0: if ((REQUEST_TIME - $last_check) > $interval) { Chris@0: // If the configured update interval has elapsed, we want to invalidate Chris@0: // the data for all projects, attempt to re-fetch, and trigger any Chris@0: // configured notifications about the new status. Chris@0: update_refresh(); Chris@0: update_fetch_data(); Chris@0: } Chris@0: else { Chris@0: // Otherwise, see if any individual projects are now stale or still Chris@0: // missing data, and if so, try to fetch the data. Chris@0: update_get_available(TRUE); Chris@0: } Chris@0: $last_email_notice = \Drupal::state()->get('update.last_email_notification') ?: 0; Chris@0: if ((REQUEST_TIME - $last_email_notice) > $interval) { Chris@0: // If configured time between notifications elapsed, send email about Chris@0: // updates possibly available. Chris@0: module_load_include('inc', 'update', 'update.fetch'); Chris@0: _update_cron_notify(); Chris@0: } Chris@0: Chris@0: // Clear garbage from disk. Chris@0: update_clear_update_disk_cache(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_themes_installed(). Chris@0: * Chris@0: * If themes are installed, we invalidate the information of available updates. Chris@0: */ Chris@0: function update_themes_installed($themes) { Chris@0: // Clear all update module data. Chris@0: update_storage_clear(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_themes_uninstalled(). Chris@0: * Chris@0: * If themes are uninstalled, we invalidate the information of available updates. Chris@0: */ Chris@0: function update_themes_uninstalled($themes) { Chris@0: // Clear all update module data. Chris@0: update_storage_clear(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_form_FORM_ID_alter() for system_modules(). Chris@0: * Chris@0: * Adds a form submission handler to the system modules form, so that if a site Chris@0: * admin saves the form, we invalidate the information of available updates. Chris@0: * Chris@0: * @see _update_cache_clear() Chris@0: */ Chris@0: function update_form_system_modules_alter(&$form, FormStateInterface $form_state) { Chris@0: $form['#submit'][] = 'update_storage_clear_submit'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler for system_modules(). Chris@0: * Chris@0: * @see update_form_system_modules_alter() Chris@0: */ Chris@0: function update_storage_clear_submit($form, FormStateInterface $form_state) { Chris@0: // Clear all update module data. Chris@0: update_storage_clear(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a warning message when there is no data about available updates. Chris@0: */ Chris@0: function _update_no_data() { Chris@0: $destination = \Drupal::destination()->getAsArray(); Chris@0: return t('No update information available. Run cron or check manually.', [ Chris@18: ':run_cron' => Url::fromRoute('system.run_cron', [], ['query' => $destination])->toString(), Chris@18: ':check_manually' => Url::fromRoute('update.manual_status', [], ['query' => $destination])->toString(), Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tries to get update information and refreshes it when necessary. Chris@0: * Chris@0: * In addition to checking the lifetime, this function also ensures that Chris@0: * there are no .info.yml files for enabled modules or themes that have a newer Chris@0: * modification timestamp than the last time we checked for available update Chris@0: * data. If any .info.yml file was modified, it almost certainly means a new Chris@0: * version of something was installed. Without fresh available update data, the Chris@0: * logic in update_calculate_project_data() will be wrong and produce confusing, Chris@0: * bogus results. Chris@0: * Chris@0: * @param $refresh Chris@0: * (optional) Boolean to indicate if this method should refresh automatically Chris@0: * if there's no data. Defaults to FALSE. Chris@0: * Chris@0: * @return Chris@0: * Array of data about available releases, keyed by project shortname. Chris@0: * Chris@0: * @see update_refresh() Chris@0: * @see \Drupal\Update\UpdateManager::getProjects() Chris@0: */ Chris@0: function update_get_available($refresh = FALSE) { Chris@0: module_load_include('inc', 'update', 'update.compare'); Chris@0: $needs_refresh = FALSE; Chris@0: Chris@0: // Grab whatever data we currently have. Chris@0: $available = \Drupal::keyValueExpirable('update_available_releases')->getAll(); Chris@0: $projects = \Drupal::service('update.manager')->getProjects(); Chris@0: foreach ($projects as $key => $project) { Chris@0: // If there's no data at all, we clearly need to fetch some. Chris@0: if (empty($available[$key])) { Chris@0: // update_create_fetch_task($project); Chris@0: \Drupal::service('update.processor')->createFetchTask($project); Chris@0: $needs_refresh = TRUE; Chris@0: continue; Chris@0: } Chris@0: Chris@0: // See if the .info.yml file is newer than the last time we checked for Chris@0: // data, and if so, mark this project's data as needing to be re-fetched. Chris@0: // Any time an admin upgrades their local installation, the .info.yml file Chris@0: // will be changed, so this is the only way we can be sure we're not showing Chris@0: // bogus information right after they upgrade. Chris@0: if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) { Chris@0: $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING; Chris@0: } Chris@0: Chris@0: // If we have project data but no release data, we need to fetch. This Chris@0: // can be triggered when we fail to contact a release history server. Chris@0: if (empty($available[$key]['releases']) && !$available[$key]['last_fetch']) { Chris@0: $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING; Chris@0: } Chris@0: Chris@0: // If we think this project needs to fetch, actually create the task now Chris@0: // and remember that we think we're missing some data. Chris@0: if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) { Chris@0: \Drupal::service('update.processor')->createFetchTask($project); Chris@0: $needs_refresh = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($needs_refresh && $refresh) { Chris@0: // Attempt to drain the queue of fetch tasks. Chris@0: update_fetch_data(); Chris@0: // After processing the queue, we've (hopefully) got better data, so pull Chris@0: // the latest data again and use that directly. Chris@0: $available = \Drupal::keyValueExpirable('update_available_releases')->getAll(); Chris@0: } Chris@0: Chris@0: return $available; Chris@0: } Chris@0: Chris@0: /** Chris@17: * Identifies equivalent security releases with a hardcoded list. Chris@17: * Chris@17: * Generally, only the latest minor version of Drupal 8 is supported. However, Chris@17: * when security fixes are backported to an old branch, and the site owner Chris@17: * updates to the release containing the backported fix, they should not Chris@17: * see "Security update required!" again if the only other security releases Chris@17: * are releases for the same advisories. Chris@17: * Chris@17: * @return string[] Chris@17: * A list of security release numbers that are equivalent to this release Chris@17: * (i.e. covered by the same advisory), for backported security fixes only. Chris@17: * Chris@17: * @internal Chris@17: * Chris@17: * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use the Chris@17: * 'Insecure' release type tag in update XML provided by Drupal.org to Chris@17: * determine if releases are insecure. Chris@17: */ Chris@17: function _update_equivalent_security_releases() { Chris@17: trigger_error("_update_equivalent_security_releases() was a temporary fix and will be removed before 9.0.0. Use the 'Insecure' release type tag in update XML provided by Drupal.org to determine if releases are insecure.", E_USER_DEPRECATED); Chris@17: switch (\Drupal::VERSION) { Chris@17: case '8.3.8': Chris@17: return ['8.4.5', '8.5.0-rc1']; Chris@17: case '8.3.9': Chris@17: return ['8.4.6', '8.5.1']; Chris@17: case '8.4.5': Chris@17: return ['8.5.0-rc1']; Chris@17: case '8.4.6': Chris@17: return ['8.5.1']; Chris@17: case '8.4.7': Chris@17: return ['8.5.2']; Chris@17: case '8.4.8': Chris@17: return ['8.5.3']; Chris@17: } Chris@17: Chris@17: return []; Chris@17: } Chris@17: Chris@17: /** Chris@0: * Adds a task to the queue for fetching release history data for a project. Chris@0: * Chris@0: * We only create a new fetch task if there's no task already in the queue for Chris@0: * this particular project (based on 'update_fetch_task' key-value collection). Chris@0: * Chris@0: * @param $project Chris@0: * Associative array of information about a project as created by Chris@0: * \Drupal\Update\UpdateManager::getProjects(), including keys such as 'name' Chris@0: * (short name), and the 'info' array with data from a .info.yml file for the Chris@0: * project. Chris@0: * Chris@0: * @see \Drupal\update\UpdateFetcher::createFetchTask() Chris@0: */ Chris@0: function update_create_fetch_task($project) { Chris@0: \Drupal::service('update.processor')->createFetchTask($project); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Refreshes the release data after loading the necessary include file. Chris@0: */ Chris@0: function update_refresh() { Chris@0: \Drupal::service('update.manager')->refreshUpdateData(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attempts to fetch update data after loading the necessary include file. Chris@0: * Chris@0: * @see \Drupal\update\UpdateProcessor::fetchData() Chris@0: */ Chris@0: function update_fetch_data() { Chris@0: \Drupal::service('update.processor')->fetchData(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Batch callback: Performs actions when all fetch tasks have been completed. Chris@0: * Chris@0: * @param $success Chris@0: * TRUE if the batch operation was successful; FALSE if there were errors. Chris@0: * @param $results Chris@0: * An associative array of results from the batch operation, including the key Chris@0: * 'updated' which holds the total number of projects we fetched available Chris@0: * update data for. Chris@0: */ Chris@0: function update_fetch_data_finished($success, $results) { Chris@0: if ($success) { Chris@0: if (!empty($results)) { Chris@0: if (!empty($results['updated'])) { Chris@17: \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.')); Chris@0: } Chris@0: if (!empty($results['failures'])) { Chris@17: \Drupal::messenger()->addError(\Drupal::translation()->formatPlural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.')); Chris@0: } Chris@0: } Chris@0: } Chris@0: else { Chris@17: \Drupal::messenger()->addError(t('An error occurred trying to get available update data.'), 'error'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_mail(). Chris@0: * Chris@0: * Constructs the email notification message when the site is out of date. Chris@0: * Chris@0: * @param $key Chris@0: * Unique key to indicate what message to build, always 'status_notify'. Chris@0: * @param $message Chris@0: * Reference to the message array being built. Chris@0: * @param $params Chris@0: * Array of parameters to indicate what kind of text to include in the message Chris@0: * body. This is a keyed array of message type ('core' or 'contrib') as the Chris@0: * keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for the Chris@0: * values. Chris@0: * Chris@0: * @see \Drupal\Core\Mail\MailManagerInterface::mail() Chris@0: * @see _update_cron_notify() Chris@0: * @see _update_message_text() Chris@0: */ Chris@0: function update_mail($key, &$message, $params) { Chris@0: $langcode = $message['langcode']; Chris@0: $language = \Drupal::languageManager()->getLanguage($langcode); Chris@0: $message['subject'] .= t('New release(s) available for @site_name', ['@site_name' => \Drupal::config('system.site')->get('name')], ['langcode' => $langcode]); Chris@0: foreach ($params as $msg_type => $msg_reason) { Chris@0: $message['body'][] = _update_message_text($msg_type, $msg_reason, $langcode); Chris@0: } Chris@18: $message['body'][] = t('See the available updates page for more information:', [], ['langcode' => $langcode]) . "\n" . Url::fromRoute('update.status', [], ['absolute' => TRUE, 'language' => $language])->toString(); Chris@0: if (_update_manager_access()) { Chris@18: $message['body'][] = t('You can automatically install your missing updates using the Update manager:', [], ['langcode' => $langcode]) . "\n" . Url::fromRoute('update.report_update', [], ['absolute' => TRUE, 'language' => $language])->toString(); Chris@0: } Chris@18: $settings_url = Url::fromRoute('update.settings', [], ['absolute' => TRUE])->toString(); Chris@0: if (\Drupal::config('update.settings')->get('notification.threshold') == 'all') { Chris@0: $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, @url.', ['@url' => $settings_url]); Chris@0: } Chris@0: else { Chris@0: $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, @url.', ['@url' => $settings_url]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the appropriate message text when site is out of date or not secure. Chris@0: * Chris@0: * These error messages are shared by both update_requirements() for the Chris@0: * site-wide status report at admin/reports/status and in the body of the Chris@0: * notification email messages generated by update_cron(). Chris@0: * Chris@0: * @param $msg_type Chris@0: * String to indicate what kind of message to generate. Can be either 'core' Chris@0: * or 'contrib'. Chris@0: * @param $msg_reason Chris@0: * Integer constant specifying why message is generated. Chris@0: * @param $langcode Chris@0: * (optional) A language code to use. Defaults to NULL. Chris@0: * Chris@0: * @return Chris@0: * The properly translated error message for the given key. Chris@0: */ Chris@0: function _update_message_text($msg_type, $msg_reason, $langcode = NULL) { Chris@0: $text = ''; Chris@0: switch ($msg_reason) { Chris@0: case UPDATE_NOT_SECURE: Chris@0: if ($msg_type == 'core') { Chris@0: $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]); Chris@0: } Chris@0: else { Chris@0: $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]); Chris@0: } Chris@0: break; Chris@0: Chris@0: case UPDATE_REVOKED: Chris@0: if ($msg_type == 'core') { Chris@0: $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', [], ['langcode' => $langcode]); Chris@0: } Chris@0: else { Chris@0: $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or disabling is strongly recommended!', [], ['langcode' => $langcode]); Chris@0: } Chris@0: break; Chris@0: Chris@0: case UPDATE_NOT_SUPPORTED: Chris@0: if ($msg_type == 'core') { Chris@0: $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', [], ['langcode' => $langcode]); Chris@0: } Chris@0: else { Chris@0: $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended. See the project homepage for more details.', [], ['langcode' => $langcode]); Chris@0: } Chris@0: break; Chris@0: Chris@0: case UPDATE_NOT_CURRENT: Chris@0: if ($msg_type == 'core') { Chris@0: $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]); Chris@0: } Chris@0: else { Chris@0: $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]); Chris@0: } Chris@0: break; Chris@0: Chris@0: case UPDATE_UNKNOWN: Chris@0: case UPDATE_NOT_CHECKED: Chris@0: case UPDATE_NOT_FETCHED: Chris@0: case UPDATE_FETCH_PENDING: Chris@0: if ($msg_type == 'core') { Chris@18: $text = t('There was a problem checking available updates for Drupal.', [':update-report' => Url::fromRoute('update.status')->toString()], ['langcode' => $langcode]); Chris@0: } Chris@0: else { Chris@18: $text = t('There was a problem checking available updates for your modules or themes.', [':update-report' => Url::fromRoute('update.status')->toString()], ['langcode' => $langcode]); Chris@0: } Chris@0: break; Chris@0: } Chris@0: Chris@0: return $text; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Orders projects based on their status. Chris@0: * Chris@0: * Callback for uasort() within update_requirements(). Chris@0: */ Chris@0: function _update_project_status_sort($a, $b) { Chris@0: // The status constants are numerically in the right order, so we can Chris@0: // usually subtract the two to compare in the order we want. However, Chris@0: // negative status values should be treated as if they are huge, since we Chris@0: // always want them at the bottom of the list. Chris@0: $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']); Chris@0: $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']); Chris@0: return $a_status - $b_status; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for last time update data was checked templates. Chris@0: * Chris@0: * Default template: update-last-check.html.twig. Chris@0: * Chris@0: * In addition to properly formatting the given timestamp, this function also Chris@0: * provides a "Check manually" link that refreshes the available update and Chris@0: * redirects back to the same page. Chris@0: * Chris@0: * @param $variables Chris@0: * An associative array containing: Chris@0: * - last: The timestamp when the site last checked for available updates. Chris@0: * Chris@0: * @see theme_update_report() Chris@0: */ Chris@0: function template_preprocess_update_last_check(&$variables) { Chris@0: $variables['time'] = \Drupal::service('date.formatter')->formatTimeDiffSince($variables['last']); Chris@0: $variables['link'] = \Drupal::l(t('Check manually'), new Url('update.manual_status', [], ['query' => \Drupal::destination()->getAsArray()])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_verify_update_archive(). Chris@0: * Chris@0: * First, we ensure that the archive isn't a copy of Drupal core, which the Chris@0: * update manager does not yet support. See https://www.drupal.org/node/606592. Chris@0: * Chris@0: * Then, we make sure that at least one module included in the archive file has Chris@0: * an .info.yml file which claims that the code is compatible with the current Chris@0: * version of Drupal core. Chris@0: * Chris@0: * @see \Drupal\Core\Extension\ExtensionDiscovery Chris@0: */ Chris@0: function update_verify_update_archive($project, $archive_file, $directory) { Chris@0: $errors = []; Chris@0: Chris@0: // Make sure this isn't a tarball of Drupal core. Chris@0: if ( Chris@0: file_exists("$directory/$project/index.php") Chris@0: && file_exists("$directory/$project/core/install.php") Chris@0: && file_exists("$directory/$project/core/includes/bootstrap.inc") Chris@0: && file_exists("$directory/$project/core/modules/node/node.module") Chris@0: && file_exists("$directory/$project/core/modules/system/system.module") Chris@0: ) { Chris@0: return [ Chris@0: 'no-core' => t('Automatic updating of Drupal core is not supported. See the upgrade guide for information on how to update Drupal core manually.', [':upgrade-guide' => 'https://www.drupal.org/upgrade']), Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Parse all the .info.yml files and make sure at least one is compatible with Chris@0: // this version of Drupal core. If one is compatible, then the project as a Chris@0: // whole is considered compatible (since, for example, the project may ship Chris@0: // with some out-of-date modules that are not necessary for its overall Chris@0: // functionality). Chris@0: $compatible_project = FALSE; Chris@0: $incompatible = []; Chris@0: $files = file_scan_directory("$directory/$project", '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', ['key' => 'name', 'min_depth' => 0]); Chris@0: foreach ($files as $file) { Chris@0: // Get the .info.yml file for the module or theme this file belongs to. Chris@0: $info = \Drupal::service('info_parser')->parse($file->uri); Chris@0: Chris@0: // If the module or theme is incompatible with Drupal core, set an error. Chris@0: if (empty($info['core']) || $info['core'] != \Drupal::CORE_COMPATIBILITY) { Chris@0: $incompatible[] = !empty($info['name']) ? $info['name'] : t('Unknown'); Chris@0: } Chris@0: else { Chris@0: $compatible_project = TRUE; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@18: /** @var \Drupal\Core\File\FileSystemInterface $file_system */ Chris@18: $file_system = \Drupal::service('file_system'); Chris@0: if (empty($files)) { Chris@18: $errors[] = t('%archive_file does not contain any .info.yml files.', ['%archive_file' => $file_system->basename($archive_file)]); Chris@0: } Chris@0: elseif (!$compatible_project) { Chris@0: $errors[] = \Drupal::translation()->formatPlural( Chris@0: count($incompatible), Chris@0: '%archive_file contains a version of %names that is not compatible with Drupal @version.', Chris@0: '%archive_file contains versions of modules or themes that are not compatible with Drupal @version: %names', Chris@18: [ Chris@18: '@version' => \Drupal::CORE_COMPATIBILITY, Chris@18: '%archive_file' => $file_system->basename($archive_file), Chris@18: '%names' => implode(', ', $incompatible), Chris@18: ] Chris@0: ); Chris@0: } Chris@0: Chris@0: return $errors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Invalidates stored data relating to update status. Chris@0: */ Chris@0: function update_storage_clear() { Chris@0: \Drupal::keyValueExpirable('update')->deleteAll(); Chris@0: \Drupal::keyValueExpirable('update_available_release')->deleteAll(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a short unique identifier for this Drupal installation. Chris@0: * Chris@0: * @return Chris@0: * An eight character string uniquely identifying this Drupal installation. Chris@0: */ Chris@0: function _update_manager_unique_identifier() { Chris@0: $id = &drupal_static(__FUNCTION__, ''); Chris@0: if (empty($id)) { Chris@0: $id = substr(hash('sha256', Settings::getHashSalt()), 0, 8); Chris@0: } Chris@0: return $id; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the directory where update archive files should be extracted. Chris@0: * Chris@0: * @param $create Chris@0: * (optional) Whether to attempt to create the directory if it does not Chris@0: * already exist. Defaults to TRUE. Chris@0: * Chris@0: * @return Chris@0: * The full path to the temporary directory where update file archives should Chris@0: * be extracted. Chris@0: */ Chris@0: function _update_manager_extract_directory($create = TRUE) { Chris@0: $directory = &drupal_static(__FUNCTION__, ''); Chris@0: if (empty($directory)) { Chris@0: $directory = 'temporary://update-extraction-' . _update_manager_unique_identifier(); Chris@0: if ($create && !file_exists($directory)) { Chris@0: mkdir($directory); Chris@0: } Chris@0: } Chris@0: return $directory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the directory where update archive files should be cached. Chris@0: * Chris@0: * @param $create Chris@0: * (optional) Whether to attempt to create the directory if it does not Chris@0: * already exist. Defaults to TRUE. Chris@0: * Chris@0: * @return Chris@0: * The full path to the temporary directory where update file archives should Chris@0: * be cached. Chris@0: */ Chris@0: function _update_manager_cache_directory($create = TRUE) { Chris@0: $directory = &drupal_static(__FUNCTION__, ''); Chris@0: if (empty($directory)) { Chris@0: $directory = 'temporary://update-cache-' . _update_manager_unique_identifier(); Chris@0: if ($create && !file_exists($directory)) { Chris@0: mkdir($directory); Chris@0: } Chris@0: } Chris@0: return $directory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clears the temporary files and directories based on file age from disk. Chris@0: */ Chris@0: function update_clear_update_disk_cache() { Chris@0: // List of update module cache directories. Do not create the directories if Chris@0: // they do not exist. Chris@0: $directories = [ Chris@0: _update_manager_cache_directory(FALSE), Chris@0: _update_manager_extract_directory(FALSE), Chris@0: ]; Chris@0: Chris@0: // Search for files and directories in base folder only without recursion. Chris@0: foreach ($directories as $directory) { Chris@0: file_scan_directory($directory, '/.*/', ['callback' => 'update_delete_file_if_stale', 'recurse' => FALSE]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes stale files and directories from the update manager disk cache. Chris@0: * Chris@0: * Files and directories older than 6 hours and development snapshots older than Chris@0: * 5 minutes are considered stale. We only cache development snapshots for 5 Chris@0: * minutes since otherwise updated snapshots might not be downloaded as Chris@0: * expected. Chris@0: * Chris@0: * When checking file ages, we need to use the ctime, not the mtime Chris@0: * (modification time) since many (all?) tar implementations go out of their way Chris@0: * to set the mtime on the files they create to the timestamps recorded in the Chris@0: * tarball. We want to see the last time the file was changed on disk, which is Chris@0: * left alone by tar and correctly set to the time the archive file was Chris@0: * unpacked. Chris@0: * Chris@0: * @param $path Chris@0: * A string containing a file path or (streamwrapper) URI. Chris@0: */ Chris@0: function update_delete_file_if_stale($path) { Chris@0: if (file_exists($path)) { Chris@0: $filectime = filectime($path); Chris@0: $max_age = \Drupal::config('system.file')->get('temporary_maximum_age'); Chris@0: Chris@0: if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) { Chris@18: try { Chris@18: \Drupal::service('file_system')->deleteRecursive($path); Chris@18: } Chris@18: catch (FileException $e) { Chris@18: // Ignore failed deletes. Chris@18: } Chris@0: } Chris@0: } Chris@0: }