summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/update/update.fetch.inc
blob: 296dfbd3d06d8e5b34525c464a3804f6aa6ddd59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

/**
 * @file
 */

use Drupal\Core\Hook\Attribute\ProceduralHookScanStop;
use Drupal\update\UpdateManagerInterface;

/**
 * Performs any notifications that should be done once cron fetches new data.
 *
 * This method checks the status of the site using the new data and, depending
 * on the configuration of the site, notifies administrators via email if there
 * are new releases or missing security updates.
 *
 * @see update_requirements()
 */
#[ProceduralHookScanStop]
function _update_cron_notify(): void {
  $update_config = \Drupal::config('update.settings');
  \Drupal::moduleHandler()->loadInclude('update', 'install');
  $status = update_requirements('runtime');
  $params = [];
  $notify_all = ($update_config->get('notification.threshold') == 'all');
  foreach (['core', 'contrib'] as $report_type) {
    $type = 'update_' . $report_type;
    if (isset($status[$type]['severity'])
        && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UpdateManagerInterface::NOT_CURRENT))) {
      $params[$report_type] = $status[$type]['reason'];
    }
  }
  if (!empty($params)) {
    $notify_list = $update_config->get('notification.emails');
    if (!empty($notify_list)) {
      $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
      $request_time = \Drupal::time()->getRequestTime();
      foreach ($notify_list as $target) {
        if ($target_user = user_load_by_mail($target)) {
          $target_langcode = $target_user->getPreferredLangcode();
        }
        else {
          $target_langcode = $default_langcode;
        }
        $message = \Drupal::service('plugin.manager.mail')->mail('update', 'status_notify', $target, $target_langcode, $params);
        // Track when the last mail was successfully sent to avoid sending
        // too many emails.
        if ($message['result']) {
          \Drupal::state()->set('update.last_email_notification', $request_time);
        }
      }
    }
  }
}