8cb9513a50ff6fed8198a4ae5439bf9ddab2dae5
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Task / SystemStatusUpdateTask.php
1 <?php
2 namespace TYPO3\CMS\Reports\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Mail\MailMessage;
18 use TYPO3\CMS\Core\Registry;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MailUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22 use TYPO3\CMS\Reports\Status;
23 use TYPO3\CMS\Scheduler\Task\AbstractTask;
24
25 /**
26 * A task that should be run regularly to determine the system's status.
27 */
28 class SystemStatusUpdateTask extends AbstractTask
29 {
30 /**
31 * Email addresses to send email notification to in case we find problems with
32 * the system.
33 *
34 * @var string
35 */
36 protected $notificationEmail = null;
37
38 /**
39 * Checkbox for to send all types of notification, not only problems
40 *
41 * @var bool
42 */
43 protected $notificationAll = false;
44
45 /**
46 * Executes the System Status Update task, determining the highest severity of
47 * status reports and saving that to the registry to be displayed at login
48 * if necessary.
49 *
50 * @see \TYPO3\CMS\Scheduler\Task\AbstractTask::execute()
51 */
52 public function execute()
53 {
54 /** @var Registry $registry */
55 $registry = GeneralUtility::makeInstance(Registry::class);
56 /** @var \TYPO3\CMS\Reports\Report\Status\Status $statusReport */
57 $statusReport = GeneralUtility::makeInstance(\TYPO3\CMS\Reports\Report\Status\Status::class);
58 $systemStatus = $statusReport->getDetailedSystemStatus();
59 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
60 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
61 if (($highestSeverity > Status::OK) || $this->getNotificationAll()) {
62 $this->sendNotificationEmail($systemStatus);
63 }
64 return true;
65 }
66
67 /**
68 * Gets the notification email addresses.
69 *
70 * @return string Notification email addresses.
71 */
72 public function getNotificationEmail()
73 {
74 return $this->notificationEmail;
75 }
76
77 /**
78 * Sets the notification email address.
79 *
80 * @param string $notificationEmail Notification email address.
81 * @return void
82 */
83 public function setNotificationEmail($notificationEmail)
84 {
85 $this->notificationEmail = $notificationEmail;
86 }
87
88 /**
89 * Sends a notification email, reporting system issues.
90 *
91 * @param Status[] $systemStatus Array of statuses
92 * @return void
93 */
94 protected function sendNotificationEmail(array $systemStatus)
95 {
96 $systemIssues = [];
97 foreach ($systemStatus as $statusProvider) {
98 /** @var Status $status */
99 foreach ($statusProvider as $status) {
100 if ($this->getNotificationAll() || ($status->getSeverity() > Status::OK)) {
101 $systemIssues[] = (string)$status . CRLF . $status->getMessage() . CRLF . CRLF;
102 }
103 }
104 }
105 $notificationEmails = GeneralUtility::trimExplode(LF, $this->notificationEmail, true);
106 $sendEmailsTo = [];
107 foreach ($notificationEmails as $notificationEmail) {
108 $sendEmailsTo[] = $notificationEmail;
109 }
110 $subject = sprintf($this->getLanguageService()->getLL('status_updateTask_email_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
111 $message = $this->getNotificationAll() ? $this->getLanguageService()->getLL('status_allNotification') : $this->getLanguageService()->getLL('status_problemNotification');
112 $message .= CRLF . CRLF;
113 $message .= $this->getLanguageService()->getLL('status_updateTask_email_site') . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
114 $message .= CRLF . CRLF;
115 $message .= $this->getLanguageService()->getLL('status_updateTask_email_issues') . ': ' . CRLF;
116 $message .= implode(CRLF, $systemIssues);
117 $message .= CRLF . CRLF;
118 $from = MailUtility::getSystemFrom();
119 /** @var MailMessage $mail */
120 $mail = GeneralUtility::makeInstance(MailMessage::class);
121 $mail->setFrom($from);
122 $mail->setTo($sendEmailsTo);
123 $mail->setSubject($subject);
124 $mail->setBody($message);
125 $mail->send();
126 }
127
128 /**
129 * @return bool
130 */
131 public function getNotificationAll(): bool
132 {
133 return $this->notificationAll;
134 }
135
136 /**
137 * @param bool $notificationAll
138 */
139 public function setNotificationAll(bool $notificationAll)
140 {
141 $this->notificationAll = $notificationAll;
142 }
143
144 /**
145 * @return LanguageService
146 */
147 protected function getLanguageService()
148 {
149 return $GLOBALS['LANG'];
150 }
151 }