update.fetch.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * @file
  4. * Code required only when fetching information about available updates.
  5. */
  6. use Drupal\update\UpdateManagerInterface;
  7. /**
  8. * Performs any notifications that should be done once cron fetches new data.
  9. *
  10. * This method checks the status of the site using the new data and, depending
  11. * on the configuration of the site, notifies administrators via email if there
  12. * are new releases or missing security updates.
  13. *
  14. * @see update_requirements()
  15. */
  16. function _update_cron_notify() {
  17. $update_config = \Drupal::config('update.settings');
  18. module_load_install('update');
  19. $status = update_requirements('runtime');
  20. $params = [];
  21. $notify_all = ($update_config->get('notification.threshold') == 'all');
  22. foreach (['core', 'contrib'] as $report_type) {
  23. $type = 'update_' . $report_type;
  24. if (isset($status[$type]['severity'])
  25. && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UpdateManagerInterface::NOT_CURRENT))) {
  26. $params[$report_type] = $status[$type]['reason'];
  27. }
  28. }
  29. if (!empty($params)) {
  30. $notify_list = $update_config->get('notification.emails');
  31. if (!empty($notify_list)) {
  32. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
  33. foreach ($notify_list as $target) {
  34. if ($target_user = user_load_by_mail($target)) {
  35. $target_langcode = $target_user->getPreferredLangcode();
  36. }
  37. else {
  38. $target_langcode = $default_langcode;
  39. }
  40. $message = \Drupal::service('plugin.manager.mail')->mail('update', 'status_notify', $target, $target_langcode, $params);
  41. // Track when the last mail was successfully sent to avoid sending
  42. // too many emails.
  43. if ($message['result']) {
  44. \Drupal::state()->set('update.last_email_notification', REQUEST_TIME);
  45. }
  46. }
  47. }
  48. }
  49. }