annotate core/modules/update/update.module @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Handles updates of Drupal core and contributed projects.
Chris@0 6 *
Chris@0 7 * The module checks for available updates of Drupal core and any installed
Chris@0 8 * contributed modules and themes. It warns site administrators if newer
Chris@0 9 * releases are available via the system status report (admin/reports/status),
Chris@0 10 * the module and theme pages, and optionally via email. It also provides the
Chris@0 11 * ability to install contributed modules and themes via an user interface.
Chris@0 12 */
Chris@0 13
Chris@0 14 use Drupal\Core\Url;
Chris@0 15 use Drupal\Core\Form\FormStateInterface;
Chris@0 16 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 17 use Drupal\Core\Site\Settings;
Chris@0 18
Chris@0 19 // These are internally used constants for this code, do not modify.
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Project is missing security update(s).
Chris@0 23 *
Chris@0 24 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 25 * Use \Drupal\update\UpdateManagerInterface::NOT_SECURE instead.
Chris@0 26 *
Chris@0 27 * @see https://www.drupal.org/node/2831620
Chris@0 28 */
Chris@0 29 const UPDATE_NOT_SECURE = 1;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Current release has been unpublished and is no longer available.
Chris@0 33 *
Chris@0 34 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 35 * Use \Drupal\update\UpdateManagerInterface::REVOKED instead.
Chris@0 36 *
Chris@0 37 * @see https://www.drupal.org/node/2831620
Chris@0 38 */
Chris@0 39 const UPDATE_REVOKED = 2;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Current release is no longer supported by the project maintainer.
Chris@0 43 *
Chris@0 44 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 45 * Use \Drupal\update\UpdateManagerInterface::NOT_SUPPORTED instead.
Chris@0 46 *
Chris@0 47 * @see https://www.drupal.org/node/2831620
Chris@0 48 */
Chris@0 49 const UPDATE_NOT_SUPPORTED = 3;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Project has a new release available, but it is not a security release.
Chris@0 53 *
Chris@0 54 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 55 * Use \Drupal\update\UpdateManagerInterface::NOT_CURRENT instead.
Chris@0 56 *
Chris@0 57 * @see https://www.drupal.org/node/2831620
Chris@0 58 */
Chris@0 59 const UPDATE_NOT_CURRENT = 4;
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Project is up to date.
Chris@0 63 *
Chris@0 64 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 65 * Use \Drupal\update\UpdateManagerInterface::CURRENT instead.
Chris@0 66 *
Chris@0 67 * @see https://www.drupal.org/node/2831620
Chris@0 68 */
Chris@0 69 const UPDATE_CURRENT = 5;
Chris@0 70
Chris@0 71 /**
Chris@0 72 * Project's status cannot be checked.
Chris@0 73 *
Chris@0 74 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 75 * Use \Drupal\update\UpdateFetcherInterface::NOT_CHECKED instead.
Chris@0 76 *
Chris@0 77 * @see https://www.drupal.org/node/2831620
Chris@0 78 */
Chris@0 79 const UPDATE_NOT_CHECKED = -1;
Chris@0 80
Chris@0 81 /**
Chris@0 82 * No available update data was found for project.
Chris@0 83 *
Chris@0 84 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 85 * Use \Drupal\update\UpdateFetcherInterface::UNKNOWN instead.
Chris@0 86 *
Chris@0 87 * @see https://www.drupal.org/node/2831620
Chris@0 88 */
Chris@0 89 const UPDATE_UNKNOWN = -2;
Chris@0 90
Chris@0 91 /**
Chris@0 92 * There was a failure fetching available update data for this project.
Chris@0 93 *
Chris@0 94 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 95 * Use \Drupal\update\UpdateFetcherInterface::NOT_FETCHED instead.
Chris@0 96 *
Chris@0 97 * @see https://www.drupal.org/node/2831620
Chris@0 98 */
Chris@0 99 const UPDATE_NOT_FETCHED = -3;
Chris@0 100
Chris@0 101 /**
Chris@0 102 * We need to (re)fetch available update data for this project.
Chris@0 103 *
Chris@0 104 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 105 * Use \Drupal\update\UpdateFetcherInterface::FETCH_PENDING instead.
Chris@0 106 *
Chris@0 107 * @see https://www.drupal.org/node/2831620
Chris@0 108 */
Chris@0 109 const UPDATE_FETCH_PENDING = -4;
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Implements hook_help().
Chris@0 113 */
Chris@0 114 function update_help($route_name, RouteMatchInterface $route_match) {
Chris@0 115 switch ($route_name) {
Chris@0 116 case 'help.page.update':
Chris@0 117 $output = '';
Chris@0 118 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 119 $output .= '<p>' . 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 <a href=":modules">Extend page</a>; if you do so, functionality that depends on the Update Manager system will not work. For more information, see the <a href=":update">online documentation for the Update Manager module</a>.', [':update' => 'https://www.drupal.org/documentation/modules/update', ':modules' => \Drupal::url('system.modules_list')]) . '</p>';
Chris@0 120 // Only explain the Update manager if it has not been disabled.
Chris@0 121 if (_update_manager_access()) {
Chris@0 122 $output .= '<p>' . t('The Update Manager also allows administrators to update and install modules and themes through the administration interface.') . '</p>';
Chris@0 123 }
Chris@0 124 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 125 $output .= '<dl>';
Chris@0 126 $output .= '<dt>' . t('Checking for available updates') . '</dt>';
Chris@0 127 $output .= '<dd>' . t('The <a href=":update-report">Available updates report</a> displays core, contributed modules, and themes for which there are new releases available for download. On the report page, you can also check manually for updates. You can configure the frequency of update checks, which are performed during cron runs, and whether notifications are sent on the <a href=":update-settings">Update Manager settings page</a>.', [':update-report' => \Drupal::url('update.status'), ':update-settings' => \Drupal::url('update.settings')]) . '</dd>';
Chris@0 128 // Only explain the Update manager if it has not been disabled.
Chris@0 129 if (_update_manager_access()) {
Chris@0 130 $output .= '<dt>' . t('Performing updates through the Update page') . '</dt>';
Chris@0 131 $output .= '<dd>' . t('The Update Manager module allows administrators to perform updates directly from the <a href=":update-page">Update page</a>. It lists all available updates, and you can confirm whether you want to download them. If you don\'t have sufficient access rights to your web server, you could be prompted for your FTP/SSH password. Afterwards the files are transferred into your site installation, overwriting your old files. Direct links to the Update page are also displayed on the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>.', [':modules_page' => \Drupal::url('system.modules_list'), ':themes_page' => \Drupal::url('system.themes_page'), ':update-page' => \Drupal::url('update.report_update')]) . '</dd>';
Chris@0 132 $output .= '<dt>' . t('Installing new modules and themes through the Install page') . '</dt>';
Chris@0 133 $output .= '<dd>' . t('You can also install new modules and themes in the same fashion, through the <a href=":install">Install page</a>, or by clicking the <em>Install new module/theme</em> links at the top of the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>. In this case, you are prompted to provide either the URL to the download, or to upload a packaged release file from your local computer.', [':modules_page' => \Drupal::url('system.modules_list'), ':themes_page' => \Drupal::url('system.themes_page'), ':install' => \Drupal::url('update.report_install')]) . '</dd>';
Chris@0 134 }
Chris@0 135 $output .= '</dl>';
Chris@0 136 return $output;
Chris@0 137
Chris@0 138 case 'update.status':
Chris@0 139 return '<p>' . 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.') . '</p>';
Chris@0 140
Chris@0 141 case 'system.modules_list':
Chris@0 142 if (_update_manager_access()) {
Chris@0 143 $output = '<p>' . t('Regularly review and install <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => \Drupal::url('system.db_update'), ':updates' => \Drupal::url('update.status')]) . '</p>';
Chris@0 144 }
Chris@0 145 else {
Chris@0 146 $output = '<p>' . t('Regularly review <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => \Drupal::url('system.db_update'), ':updates' => \Drupal::url('update.status')]) . '</p>';
Chris@0 147 }
Chris@0 148 return $output;
Chris@0 149 }
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * Implements hook_page_top().
Chris@0 154 */
Chris@0 155 function update_page_top() {
Chris@0 156 /** @var \Drupal\Core\Routing\AdminContext $admin_context */
Chris@0 157 $admin_context = \Drupal::service('router.admin_context');
Chris@0 158 $route_match = \Drupal::routeMatch();
Chris@0 159 if ($admin_context->isAdminRoute($route_match->getRouteObject()) && \Drupal::currentUser()->hasPermission('administer site configuration')) {
Chris@0 160 $route_name = \Drupal::routeMatch()->getRouteName();
Chris@0 161 switch ($route_name) {
Chris@0 162 // These pages don't need additional nagging.
Chris@0 163 case 'update.theme_update':
Chris@0 164 case 'system.theme_install':
Chris@0 165 case 'update.module_update':
Chris@0 166 case 'update.module_install':
Chris@0 167 case 'update.status':
Chris@0 168 case 'update.report_update':
Chris@0 169 case 'update.report_install':
Chris@0 170 case 'update.settings':
Chris@0 171 case 'system.status':
Chris@0 172 case 'update.confirmation_page':
Chris@0 173 return;
Chris@0 174
Chris@0 175 // If we are on the appearance or modules list, display a detailed report
Chris@0 176 // of the update status.
Chris@0 177 case 'system.themes_page':
Chris@0 178 case 'system.modules_list':
Chris@0 179 $verbose = TRUE;
Chris@0 180 break;
Chris@0 181
Chris@0 182 }
Chris@0 183 module_load_install('update');
Chris@0 184 $status = update_requirements('runtime');
Chris@0 185 foreach (['core', 'contrib'] as $report_type) {
Chris@0 186 $type = 'update_' . $report_type;
Chris@0 187 // hook_requirements() supports render arrays therefore we need to render
Chris@17 188 // them before using
Chris@17 189 // \Drupal\Core\Messenger\MessengerInterface::addStatus().
Chris@0 190 if (isset($status[$type]['description']) && is_array($status[$type]['description'])) {
Chris@0 191 $status[$type]['description'] = \Drupal::service('renderer')->renderPlain($status[$type]['description']);
Chris@0 192 }
Chris@0 193 if (!empty($verbose)) {
Chris@0 194 if (isset($status[$type]['severity'])) {
Chris@0 195 if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
Chris@17 196 \Drupal::messenger()->addError($status[$type]['description']);
Chris@0 197 }
Chris@0 198 elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
Chris@17 199 \Drupal::messenger()->addWarning($status[$type]['description']);
Chris@0 200 }
Chris@0 201 }
Chris@0 202 }
Chris@0 203 // Otherwise, if we're on *any* admin page and there's a security
Chris@0 204 // update missing, print an error message about it.
Chris@0 205 else {
Chris@0 206 if (isset($status[$type])
Chris@0 207 && isset($status[$type]['reason'])
Chris@0 208 && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
Chris@17 209 \Drupal::messenger()->addError($status[$type]['description']);
Chris@0 210 }
Chris@0 211 }
Chris@0 212 }
Chris@0 213 }
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Resolves if the current user can access updater menu items.
Chris@0 218 *
Chris@0 219 * It both enforces the 'administer software updates' permission and the global
Chris@0 220 * kill switch for the authorize.php script.
Chris@0 221 *
Chris@0 222 * @return
Chris@0 223 * TRUE if the current user can access the updater menu items; FALSE
Chris@0 224 * otherwise.
Chris@0 225 */
Chris@0 226 function _update_manager_access() {
Chris@0 227 return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
Chris@0 228 }
Chris@0 229
Chris@0 230 /**
Chris@0 231 * Implements hook_theme().
Chris@0 232 */
Chris@0 233 function update_theme() {
Chris@0 234 return [
Chris@0 235 'update_last_check' => [
Chris@0 236 'variables' => ['last' => 0],
Chris@0 237 ],
Chris@0 238 'update_report' => [
Chris@0 239 'variables' => ['data' => NULL],
Chris@0 240 'file' => 'update.report.inc',
Chris@0 241 ],
Chris@0 242 'update_project_status' => [
Chris@0 243 'variables' => ['project' => []],
Chris@0 244 'file' => 'update.report.inc',
Chris@0 245 ],
Chris@0 246 // We are using template instead of '#type' => 'table' here to keep markup
Chris@0 247 // out of preprocess and allow for easier changes to markup.
Chris@0 248 'update_version' => [
Chris@0 249 'variables' => ['version' => NULL, 'title' => NULL, 'attributes' => []],
Chris@0 250 'file' => 'update.report.inc',
Chris@0 251 ],
Chris@0 252 ];
Chris@0 253 }
Chris@0 254
Chris@0 255 /**
Chris@0 256 * Implements hook_cron().
Chris@0 257 */
Chris@0 258 function update_cron() {
Chris@0 259 $update_config = \Drupal::config('update.settings');
Chris@0 260 $frequency = $update_config->get('check.interval_days');
Chris@0 261 $interval = 60 * 60 * 24 * $frequency;
Chris@0 262 $last_check = \Drupal::state()->get('update.last_check') ?: 0;
Chris@0 263 if ((REQUEST_TIME - $last_check) > $interval) {
Chris@0 264 // If the configured update interval has elapsed, we want to invalidate
Chris@0 265 // the data for all projects, attempt to re-fetch, and trigger any
Chris@0 266 // configured notifications about the new status.
Chris@0 267 update_refresh();
Chris@0 268 update_fetch_data();
Chris@0 269 }
Chris@0 270 else {
Chris@0 271 // Otherwise, see if any individual projects are now stale or still
Chris@0 272 // missing data, and if so, try to fetch the data.
Chris@0 273 update_get_available(TRUE);
Chris@0 274 }
Chris@0 275 $last_email_notice = \Drupal::state()->get('update.last_email_notification') ?: 0;
Chris@0 276 if ((REQUEST_TIME - $last_email_notice) > $interval) {
Chris@0 277 // If configured time between notifications elapsed, send email about
Chris@0 278 // updates possibly available.
Chris@0 279 module_load_include('inc', 'update', 'update.fetch');
Chris@0 280 _update_cron_notify();
Chris@0 281 }
Chris@0 282
Chris@0 283 // Clear garbage from disk.
Chris@0 284 update_clear_update_disk_cache();
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Implements hook_themes_installed().
Chris@0 289 *
Chris@0 290 * If themes are installed, we invalidate the information of available updates.
Chris@0 291 */
Chris@0 292 function update_themes_installed($themes) {
Chris@0 293 // Clear all update module data.
Chris@0 294 update_storage_clear();
Chris@0 295 }
Chris@0 296
Chris@0 297 /**
Chris@0 298 * Implements hook_themes_uninstalled().
Chris@0 299 *
Chris@0 300 * If themes are uninstalled, we invalidate the information of available updates.
Chris@0 301 */
Chris@0 302 function update_themes_uninstalled($themes) {
Chris@0 303 // Clear all update module data.
Chris@0 304 update_storage_clear();
Chris@0 305 }
Chris@0 306
Chris@0 307 /**
Chris@0 308 * Implements hook_form_FORM_ID_alter() for system_modules().
Chris@0 309 *
Chris@0 310 * Adds a form submission handler to the system modules form, so that if a site
Chris@0 311 * admin saves the form, we invalidate the information of available updates.
Chris@0 312 *
Chris@0 313 * @see _update_cache_clear()
Chris@0 314 */
Chris@0 315 function update_form_system_modules_alter(&$form, FormStateInterface $form_state) {
Chris@0 316 $form['#submit'][] = 'update_storage_clear_submit';
Chris@0 317 }
Chris@0 318
Chris@0 319 /**
Chris@0 320 * Form submission handler for system_modules().
Chris@0 321 *
Chris@0 322 * @see update_form_system_modules_alter()
Chris@0 323 */
Chris@0 324 function update_storage_clear_submit($form, FormStateInterface $form_state) {
Chris@0 325 // Clear all update module data.
Chris@0 326 update_storage_clear();
Chris@0 327 }
Chris@0 328
Chris@0 329 /**
Chris@0 330 * Returns a warning message when there is no data about available updates.
Chris@0 331 */
Chris@0 332 function _update_no_data() {
Chris@0 333 $destination = \Drupal::destination()->getAsArray();
Chris@0 334 return t('No update information available. <a href=":run_cron">Run cron</a> or <a href=":check_manually">check manually</a>.', [
Chris@0 335 ':run_cron' => \Drupal::url('system.run_cron', [], ['query' => $destination]),
Chris@0 336 ':check_manually' => \Drupal::url('update.manual_status', [], ['query' => $destination]),
Chris@0 337 ]);
Chris@0 338 }
Chris@0 339
Chris@0 340 /**
Chris@0 341 * Tries to get update information and refreshes it when necessary.
Chris@0 342 *
Chris@0 343 * In addition to checking the lifetime, this function also ensures that
Chris@0 344 * there are no .info.yml files for enabled modules or themes that have a newer
Chris@0 345 * modification timestamp than the last time we checked for available update
Chris@0 346 * data. If any .info.yml file was modified, it almost certainly means a new
Chris@0 347 * version of something was installed. Without fresh available update data, the
Chris@0 348 * logic in update_calculate_project_data() will be wrong and produce confusing,
Chris@0 349 * bogus results.
Chris@0 350 *
Chris@0 351 * @param $refresh
Chris@0 352 * (optional) Boolean to indicate if this method should refresh automatically
Chris@0 353 * if there's no data. Defaults to FALSE.
Chris@0 354 *
Chris@0 355 * @return
Chris@0 356 * Array of data about available releases, keyed by project shortname.
Chris@0 357 *
Chris@0 358 * @see update_refresh()
Chris@0 359 * @see \Drupal\Update\UpdateManager::getProjects()
Chris@0 360 */
Chris@0 361 function update_get_available($refresh = FALSE) {
Chris@0 362 module_load_include('inc', 'update', 'update.compare');
Chris@0 363 $needs_refresh = FALSE;
Chris@0 364
Chris@0 365 // Grab whatever data we currently have.
Chris@0 366 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
Chris@0 367 $projects = \Drupal::service('update.manager')->getProjects();
Chris@0 368 foreach ($projects as $key => $project) {
Chris@0 369 // If there's no data at all, we clearly need to fetch some.
Chris@0 370 if (empty($available[$key])) {
Chris@0 371 // update_create_fetch_task($project);
Chris@0 372 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 373 $needs_refresh = TRUE;
Chris@0 374 continue;
Chris@0 375 }
Chris@0 376
Chris@0 377 // See if the .info.yml file is newer than the last time we checked for
Chris@0 378 // data, and if so, mark this project's data as needing to be re-fetched.
Chris@0 379 // Any time an admin upgrades their local installation, the .info.yml file
Chris@0 380 // will be changed, so this is the only way we can be sure we're not showing
Chris@0 381 // bogus information right after they upgrade.
Chris@0 382 if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) {
Chris@0 383 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
Chris@0 384 }
Chris@0 385
Chris@0 386 // If we have project data but no release data, we need to fetch. This
Chris@0 387 // can be triggered when we fail to contact a release history server.
Chris@0 388 if (empty($available[$key]['releases']) && !$available[$key]['last_fetch']) {
Chris@0 389 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
Chris@0 390 }
Chris@0 391
Chris@0 392 // If we think this project needs to fetch, actually create the task now
Chris@0 393 // and remember that we think we're missing some data.
Chris@0 394 if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) {
Chris@0 395 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 396 $needs_refresh = TRUE;
Chris@0 397 }
Chris@0 398 }
Chris@0 399
Chris@0 400 if ($needs_refresh && $refresh) {
Chris@0 401 // Attempt to drain the queue of fetch tasks.
Chris@0 402 update_fetch_data();
Chris@0 403 // After processing the queue, we've (hopefully) got better data, so pull
Chris@0 404 // the latest data again and use that directly.
Chris@0 405 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
Chris@0 406 }
Chris@0 407
Chris@0 408 return $available;
Chris@0 409 }
Chris@0 410
Chris@0 411 /**
Chris@17 412 * Identifies equivalent security releases with a hardcoded list.
Chris@17 413 *
Chris@17 414 * Generally, only the latest minor version of Drupal 8 is supported. However,
Chris@17 415 * when security fixes are backported to an old branch, and the site owner
Chris@17 416 * updates to the release containing the backported fix, they should not
Chris@17 417 * see "Security update required!" again if the only other security releases
Chris@17 418 * are releases for the same advisories.
Chris@17 419 *
Chris@17 420 * @return string[]
Chris@17 421 * A list of security release numbers that are equivalent to this release
Chris@17 422 * (i.e. covered by the same advisory), for backported security fixes only.
Chris@17 423 *
Chris@17 424 * @internal
Chris@17 425 *
Chris@17 426 * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use the
Chris@17 427 * 'Insecure' release type tag in update XML provided by Drupal.org to
Chris@17 428 * determine if releases are insecure.
Chris@17 429 */
Chris@17 430 function _update_equivalent_security_releases() {
Chris@17 431 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 432 switch (\Drupal::VERSION) {
Chris@17 433 case '8.3.8':
Chris@17 434 return ['8.4.5', '8.5.0-rc1'];
Chris@17 435 case '8.3.9':
Chris@17 436 return ['8.4.6', '8.5.1'];
Chris@17 437 case '8.4.5':
Chris@17 438 return ['8.5.0-rc1'];
Chris@17 439 case '8.4.6':
Chris@17 440 return ['8.5.1'];
Chris@17 441 case '8.4.7':
Chris@17 442 return ['8.5.2'];
Chris@17 443 case '8.4.8':
Chris@17 444 return ['8.5.3'];
Chris@17 445 }
Chris@17 446
Chris@17 447 return [];
Chris@17 448 }
Chris@17 449
Chris@17 450 /**
Chris@0 451 * Adds a task to the queue for fetching release history data for a project.
Chris@0 452 *
Chris@0 453 * We only create a new fetch task if there's no task already in the queue for
Chris@0 454 * this particular project (based on 'update_fetch_task' key-value collection).
Chris@0 455 *
Chris@0 456 * @param $project
Chris@0 457 * Associative array of information about a project as created by
Chris@0 458 * \Drupal\Update\UpdateManager::getProjects(), including keys such as 'name'
Chris@0 459 * (short name), and the 'info' array with data from a .info.yml file for the
Chris@0 460 * project.
Chris@0 461 *
Chris@0 462 * @see \Drupal\update\UpdateFetcher::createFetchTask()
Chris@0 463 */
Chris@0 464 function update_create_fetch_task($project) {
Chris@0 465 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 466 }
Chris@0 467
Chris@0 468 /**
Chris@0 469 * Refreshes the release data after loading the necessary include file.
Chris@0 470 */
Chris@0 471 function update_refresh() {
Chris@0 472 \Drupal::service('update.manager')->refreshUpdateData();
Chris@0 473 }
Chris@0 474
Chris@0 475 /**
Chris@0 476 * Attempts to fetch update data after loading the necessary include file.
Chris@0 477 *
Chris@0 478 * @see \Drupal\update\UpdateProcessor::fetchData()
Chris@0 479 */
Chris@0 480 function update_fetch_data() {
Chris@0 481 \Drupal::service('update.processor')->fetchData();
Chris@0 482 }
Chris@0 483
Chris@0 484 /**
Chris@0 485 * Batch callback: Performs actions when all fetch tasks have been completed.
Chris@0 486 *
Chris@0 487 * @param $success
Chris@0 488 * TRUE if the batch operation was successful; FALSE if there were errors.
Chris@0 489 * @param $results
Chris@0 490 * An associative array of results from the batch operation, including the key
Chris@0 491 * 'updated' which holds the total number of projects we fetched available
Chris@0 492 * update data for.
Chris@0 493 */
Chris@0 494 function update_fetch_data_finished($success, $results) {
Chris@0 495 if ($success) {
Chris@0 496 if (!empty($results)) {
Chris@0 497 if (!empty($results['updated'])) {
Chris@17 498 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.'));
Chris@0 499 }
Chris@0 500 if (!empty($results['failures'])) {
Chris@17 501 \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 502 }
Chris@0 503 }
Chris@0 504 }
Chris@0 505 else {
Chris@17 506 \Drupal::messenger()->addError(t('An error occurred trying to get available update data.'), 'error');
Chris@0 507 }
Chris@0 508 }
Chris@0 509
Chris@0 510 /**
Chris@0 511 * Implements hook_mail().
Chris@0 512 *
Chris@0 513 * Constructs the email notification message when the site is out of date.
Chris@0 514 *
Chris@0 515 * @param $key
Chris@0 516 * Unique key to indicate what message to build, always 'status_notify'.
Chris@0 517 * @param $message
Chris@0 518 * Reference to the message array being built.
Chris@0 519 * @param $params
Chris@0 520 * Array of parameters to indicate what kind of text to include in the message
Chris@0 521 * body. This is a keyed array of message type ('core' or 'contrib') as the
Chris@0 522 * keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for the
Chris@0 523 * values.
Chris@0 524 *
Chris@0 525 * @see \Drupal\Core\Mail\MailManagerInterface::mail()
Chris@0 526 * @see _update_cron_notify()
Chris@0 527 * @see _update_message_text()
Chris@0 528 */
Chris@0 529 function update_mail($key, &$message, $params) {
Chris@0 530 $langcode = $message['langcode'];
Chris@0 531 $language = \Drupal::languageManager()->getLanguage($langcode);
Chris@0 532 $message['subject'] .= t('New release(s) available for @site_name', ['@site_name' => \Drupal::config('system.site')->get('name')], ['langcode' => $langcode]);
Chris@0 533 foreach ($params as $msg_type => $msg_reason) {
Chris@0 534 $message['body'][] = _update_message_text($msg_type, $msg_reason, $langcode);
Chris@0 535 }
Chris@0 536 $message['body'][] = t('See the available updates page for more information:', [], ['langcode' => $langcode]) . "\n" . \Drupal::url('update.status', [], ['absolute' => TRUE, 'language' => $language]);
Chris@0 537 if (_update_manager_access()) {
Chris@0 538 $message['body'][] = t('You can automatically install your missing updates using the Update manager:', [], ['langcode' => $langcode]) . "\n" . \Drupal::url('update.report_update', [], ['absolute' => TRUE, 'language' => $language]);
Chris@0 539 }
Chris@0 540 $settings_url = \Drupal::url('update.settings', [], ['absolute' => TRUE]);
Chris@0 541 if (\Drupal::config('update.settings')->get('notification.threshold') == 'all') {
Chris@0 542 $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 543 }
Chris@0 544 else {
Chris@0 545 $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 546 }
Chris@0 547 }
Chris@0 548
Chris@0 549 /**
Chris@0 550 * Returns the appropriate message text when site is out of date or not secure.
Chris@0 551 *
Chris@0 552 * These error messages are shared by both update_requirements() for the
Chris@0 553 * site-wide status report at admin/reports/status and in the body of the
Chris@0 554 * notification email messages generated by update_cron().
Chris@0 555 *
Chris@0 556 * @param $msg_type
Chris@0 557 * String to indicate what kind of message to generate. Can be either 'core'
Chris@0 558 * or 'contrib'.
Chris@0 559 * @param $msg_reason
Chris@0 560 * Integer constant specifying why message is generated.
Chris@0 561 * @param $langcode
Chris@0 562 * (optional) A language code to use. Defaults to NULL.
Chris@0 563 *
Chris@0 564 * @return
Chris@0 565 * The properly translated error message for the given key.
Chris@0 566 */
Chris@0 567 function _update_message_text($msg_type, $msg_reason, $langcode = NULL) {
Chris@0 568 $text = '';
Chris@0 569 switch ($msg_reason) {
Chris@0 570 case UPDATE_NOT_SECURE:
Chris@0 571 if ($msg_type == 'core') {
Chris@0 572 $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 573 }
Chris@0 574 else {
Chris@0 575 $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 576 }
Chris@0 577 break;
Chris@0 578
Chris@0 579 case UPDATE_REVOKED:
Chris@0 580 if ($msg_type == 'core') {
Chris@0 581 $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
Chris@0 582 }
Chris@0 583 else {
Chris@0 584 $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 585 }
Chris@0 586 break;
Chris@0 587
Chris@0 588 case UPDATE_NOT_SUPPORTED:
Chris@0 589 if ($msg_type == 'core') {
Chris@0 590 $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
Chris@0 591 }
Chris@0 592 else {
Chris@0 593 $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 594 }
Chris@0 595 break;
Chris@0 596
Chris@0 597 case UPDATE_NOT_CURRENT:
Chris@0 598 if ($msg_type == 'core') {
Chris@0 599 $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 600 }
Chris@0 601 else {
Chris@0 602 $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 603 }
Chris@0 604 break;
Chris@0 605
Chris@0 606 case UPDATE_UNKNOWN:
Chris@0 607 case UPDATE_NOT_CHECKED:
Chris@0 608 case UPDATE_NOT_FETCHED:
Chris@0 609 case UPDATE_FETCH_PENDING:
Chris@0 610 if ($msg_type == 'core') {
Chris@0 611 $text = t('There was a problem checking <a href=":update-report">available updates</a> for Drupal.', [':update-report' => \Drupal::url('update.status')], ['langcode' => $langcode]);
Chris@0 612 }
Chris@0 613 else {
Chris@0 614 $text = t('There was a problem checking <a href=":update-report">available updates</a> for your modules or themes.', [':update-report' => \Drupal::url('update.status')], ['langcode' => $langcode]);
Chris@0 615 }
Chris@0 616 break;
Chris@0 617 }
Chris@0 618
Chris@0 619 return $text;
Chris@0 620 }
Chris@0 621
Chris@0 622 /**
Chris@0 623 * Orders projects based on their status.
Chris@0 624 *
Chris@0 625 * Callback for uasort() within update_requirements().
Chris@0 626 */
Chris@0 627 function _update_project_status_sort($a, $b) {
Chris@0 628 // The status constants are numerically in the right order, so we can
Chris@0 629 // usually subtract the two to compare in the order we want. However,
Chris@0 630 // negative status values should be treated as if they are huge, since we
Chris@0 631 // always want them at the bottom of the list.
Chris@0 632 $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
Chris@0 633 $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
Chris@0 634 return $a_status - $b_status;
Chris@0 635 }
Chris@0 636
Chris@0 637 /**
Chris@0 638 * Prepares variables for last time update data was checked templates.
Chris@0 639 *
Chris@0 640 * Default template: update-last-check.html.twig.
Chris@0 641 *
Chris@0 642 * In addition to properly formatting the given timestamp, this function also
Chris@0 643 * provides a "Check manually" link that refreshes the available update and
Chris@0 644 * redirects back to the same page.
Chris@0 645 *
Chris@0 646 * @param $variables
Chris@0 647 * An associative array containing:
Chris@0 648 * - last: The timestamp when the site last checked for available updates.
Chris@0 649 *
Chris@0 650 * @see theme_update_report()
Chris@0 651 */
Chris@0 652 function template_preprocess_update_last_check(&$variables) {
Chris@0 653 $variables['time'] = \Drupal::service('date.formatter')->formatTimeDiffSince($variables['last']);
Chris@0 654 $variables['link'] = \Drupal::l(t('Check manually'), new Url('update.manual_status', [], ['query' => \Drupal::destination()->getAsArray()]));
Chris@0 655 }
Chris@0 656
Chris@0 657 /**
Chris@0 658 * Implements hook_verify_update_archive().
Chris@0 659 *
Chris@0 660 * First, we ensure that the archive isn't a copy of Drupal core, which the
Chris@0 661 * update manager does not yet support. See https://www.drupal.org/node/606592.
Chris@0 662 *
Chris@0 663 * Then, we make sure that at least one module included in the archive file has
Chris@0 664 * an .info.yml file which claims that the code is compatible with the current
Chris@0 665 * version of Drupal core.
Chris@0 666 *
Chris@0 667 * @see \Drupal\Core\Extension\ExtensionDiscovery
Chris@0 668 */
Chris@0 669 function update_verify_update_archive($project, $archive_file, $directory) {
Chris@0 670 $errors = [];
Chris@0 671
Chris@0 672 // Make sure this isn't a tarball of Drupal core.
Chris@0 673 if (
Chris@0 674 file_exists("$directory/$project/index.php")
Chris@0 675 && file_exists("$directory/$project/core/install.php")
Chris@0 676 && file_exists("$directory/$project/core/includes/bootstrap.inc")
Chris@0 677 && file_exists("$directory/$project/core/modules/node/node.module")
Chris@0 678 && file_exists("$directory/$project/core/modules/system/system.module")
Chris@0 679 ) {
Chris@0 680 return [
Chris@0 681 'no-core' => t('Automatic updating of Drupal core is not supported. See the <a href=":upgrade-guide">upgrade guide</a> for information on how to update Drupal core manually.', [':upgrade-guide' => 'https://www.drupal.org/upgrade']),
Chris@0 682 ];
Chris@0 683 }
Chris@0 684
Chris@0 685 // Parse all the .info.yml files and make sure at least one is compatible with
Chris@0 686 // this version of Drupal core. If one is compatible, then the project as a
Chris@0 687 // whole is considered compatible (since, for example, the project may ship
Chris@0 688 // with some out-of-date modules that are not necessary for its overall
Chris@0 689 // functionality).
Chris@0 690 $compatible_project = FALSE;
Chris@0 691 $incompatible = [];
Chris@0 692 $files = file_scan_directory("$directory/$project", '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', ['key' => 'name', 'min_depth' => 0]);
Chris@0 693 foreach ($files as $file) {
Chris@0 694 // Get the .info.yml file for the module or theme this file belongs to.
Chris@0 695 $info = \Drupal::service('info_parser')->parse($file->uri);
Chris@0 696
Chris@0 697 // If the module or theme is incompatible with Drupal core, set an error.
Chris@0 698 if (empty($info['core']) || $info['core'] != \Drupal::CORE_COMPATIBILITY) {
Chris@0 699 $incompatible[] = !empty($info['name']) ? $info['name'] : t('Unknown');
Chris@0 700 }
Chris@0 701 else {
Chris@0 702 $compatible_project = TRUE;
Chris@0 703 break;
Chris@0 704 }
Chris@0 705 }
Chris@0 706
Chris@0 707 if (empty($files)) {
Chris@0 708 $errors[] = t('%archive_file does not contain any .info.yml files.', ['%archive_file' => drupal_basename($archive_file)]);
Chris@0 709 }
Chris@0 710 elseif (!$compatible_project) {
Chris@0 711 $errors[] = \Drupal::translation()->formatPlural(
Chris@0 712 count($incompatible),
Chris@0 713 '%archive_file contains a version of %names that is not compatible with Drupal @version.',
Chris@0 714 '%archive_file contains versions of modules or themes that are not compatible with Drupal @version: %names',
Chris@0 715 ['@version' => \Drupal::CORE_COMPATIBILITY, '%archive_file' => drupal_basename($archive_file), '%names' => implode(', ', $incompatible)]
Chris@0 716 );
Chris@0 717 }
Chris@0 718
Chris@0 719 return $errors;
Chris@0 720 }
Chris@0 721
Chris@0 722 /**
Chris@0 723 * Invalidates stored data relating to update status.
Chris@0 724 */
Chris@0 725 function update_storage_clear() {
Chris@0 726 \Drupal::keyValueExpirable('update')->deleteAll();
Chris@0 727 \Drupal::keyValueExpirable('update_available_release')->deleteAll();
Chris@0 728 }
Chris@0 729
Chris@0 730 /**
Chris@0 731 * Returns a short unique identifier for this Drupal installation.
Chris@0 732 *
Chris@0 733 * @return
Chris@0 734 * An eight character string uniquely identifying this Drupal installation.
Chris@0 735 */
Chris@0 736 function _update_manager_unique_identifier() {
Chris@0 737 $id = &drupal_static(__FUNCTION__, '');
Chris@0 738 if (empty($id)) {
Chris@0 739 $id = substr(hash('sha256', Settings::getHashSalt()), 0, 8);
Chris@0 740 }
Chris@0 741 return $id;
Chris@0 742 }
Chris@0 743
Chris@0 744 /**
Chris@0 745 * Returns the directory where update archive files should be extracted.
Chris@0 746 *
Chris@0 747 * @param $create
Chris@0 748 * (optional) Whether to attempt to create the directory if it does not
Chris@0 749 * already exist. Defaults to TRUE.
Chris@0 750 *
Chris@0 751 * @return
Chris@0 752 * The full path to the temporary directory where update file archives should
Chris@0 753 * be extracted.
Chris@0 754 */
Chris@0 755 function _update_manager_extract_directory($create = TRUE) {
Chris@0 756 $directory = &drupal_static(__FUNCTION__, '');
Chris@0 757 if (empty($directory)) {
Chris@0 758 $directory = 'temporary://update-extraction-' . _update_manager_unique_identifier();
Chris@0 759 if ($create && !file_exists($directory)) {
Chris@0 760 mkdir($directory);
Chris@0 761 }
Chris@0 762 }
Chris@0 763 return $directory;
Chris@0 764 }
Chris@0 765
Chris@0 766 /**
Chris@0 767 * Returns the directory where update archive files should be cached.
Chris@0 768 *
Chris@0 769 * @param $create
Chris@0 770 * (optional) Whether to attempt to create the directory if it does not
Chris@0 771 * already exist. Defaults to TRUE.
Chris@0 772 *
Chris@0 773 * @return
Chris@0 774 * The full path to the temporary directory where update file archives should
Chris@0 775 * be cached.
Chris@0 776 */
Chris@0 777 function _update_manager_cache_directory($create = TRUE) {
Chris@0 778 $directory = &drupal_static(__FUNCTION__, '');
Chris@0 779 if (empty($directory)) {
Chris@0 780 $directory = 'temporary://update-cache-' . _update_manager_unique_identifier();
Chris@0 781 if ($create && !file_exists($directory)) {
Chris@0 782 mkdir($directory);
Chris@0 783 }
Chris@0 784 }
Chris@0 785 return $directory;
Chris@0 786 }
Chris@0 787
Chris@0 788 /**
Chris@0 789 * Clears the temporary files and directories based on file age from disk.
Chris@0 790 */
Chris@0 791 function update_clear_update_disk_cache() {
Chris@0 792 // List of update module cache directories. Do not create the directories if
Chris@0 793 // they do not exist.
Chris@0 794 $directories = [
Chris@0 795 _update_manager_cache_directory(FALSE),
Chris@0 796 _update_manager_extract_directory(FALSE),
Chris@0 797 ];
Chris@0 798
Chris@0 799 // Search for files and directories in base folder only without recursion.
Chris@0 800 foreach ($directories as $directory) {
Chris@0 801 file_scan_directory($directory, '/.*/', ['callback' => 'update_delete_file_if_stale', 'recurse' => FALSE]);
Chris@0 802 }
Chris@0 803 }
Chris@0 804
Chris@0 805 /**
Chris@0 806 * Deletes stale files and directories from the update manager disk cache.
Chris@0 807 *
Chris@0 808 * Files and directories older than 6 hours and development snapshots older than
Chris@0 809 * 5 minutes are considered stale. We only cache development snapshots for 5
Chris@0 810 * minutes since otherwise updated snapshots might not be downloaded as
Chris@0 811 * expected.
Chris@0 812 *
Chris@0 813 * When checking file ages, we need to use the ctime, not the mtime
Chris@0 814 * (modification time) since many (all?) tar implementations go out of their way
Chris@0 815 * to set the mtime on the files they create to the timestamps recorded in the
Chris@0 816 * tarball. We want to see the last time the file was changed on disk, which is
Chris@0 817 * left alone by tar and correctly set to the time the archive file was
Chris@0 818 * unpacked.
Chris@0 819 *
Chris@0 820 * @param $path
Chris@0 821 * A string containing a file path or (streamwrapper) URI.
Chris@0 822 */
Chris@0 823 function update_delete_file_if_stale($path) {
Chris@0 824 if (file_exists($path)) {
Chris@0 825 $filectime = filectime($path);
Chris@0 826 $max_age = \Drupal::config('system.file')->get('temporary_maximum_age');
Chris@0 827
Chris@0 828 if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) {
Chris@0 829 file_unmanaged_delete_recursive($path);
Chris@0 830 }
Chris@0 831 }
Chris@0 832 }