[BUGFIX] Always use system getFrom() when sending emails
[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\Lang\LanguageService;
21 use TYPO3\CMS\Reports\Status;
22 use TYPO3\CMS\Scheduler\Task\AbstractTask;
23
24 /**
25 * A task that should be run regularly to determine the system's status.
26 */
27 class SystemStatusUpdateTask extends AbstractTask
28 {
29 /**
30 * Email addresses to send email notification to in case we find problems with
31 * the system.
32 *
33 * @var string
34 */
35 protected $notificationEmail = null;
36
37 /**
38 * Checkbox for to send all types of notification, not only problems
39 *
40 * @var bool
41 */
42 protected $notificationAll = false;
43
44 /**
45 * Executes the System Status Update task, determining the highest severity of
46 * status reports and saving that to the registry to be displayed at login
47 * if necessary.
48 *
49 * @see \TYPO3\CMS\Scheduler\Task\AbstractTask::execute()
50 */
51 public function execute()
52 {
53 /** @var Registry $registry */
54 $registry = GeneralUtility::makeInstance(Registry::class);
55 /** @var \TYPO3\CMS\Reports\Report\Status\Status $statusReport */
56 $statusReport = GeneralUtility::makeInstance(\TYPO3\CMS\Reports\Report\Status\Status::class);
57 $systemStatus = $statusReport->getDetailedSystemStatus();
58 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
59 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
60 if (($highestSeverity > Status::OK) || $this->getNotificationAll()) {
61 $this->sendNotificationEmail($systemStatus);
62 }
63 return true;
64 }
65
66 /**
67 * Gets the notification email addresses.
68 *
69 * @return string Notification email addresses.
70 */
71 public function getNotificationEmail()
72 {
73 return $this->notificationEmail;
74 }
75
76 /**
77 * Sets the notification email address.
78 *
79 * @param string $notificationEmail Notification email address.
80 * @return void
81 */
82 public function setNotificationEmail($notificationEmail)
83 {
84 $this->notificationEmail = $notificationEmail;
85 }
86
87 /**
88 * Sends a notification email, reporting system issues.
89 *
90 * @param Status[] $systemStatus Array of statuses
91 * @return void
92 */
93 protected function sendNotificationEmail(array $systemStatus)
94 {
95 $systemIssues = [];
96 foreach ($systemStatus as $statusProvider) {
97 /** @var Status $status */
98 foreach ($statusProvider as $status) {
99 if ($this->getNotificationAll() || ($status->getSeverity() > Status::OK)) {
100 $systemIssues[] = (string)$status . CRLF . $status->getMessage() . CRLF . CRLF;
101 }
102 }
103 }
104 $notificationEmails = GeneralUtility::trimExplode(LF, $this->notificationEmail, true);
105 $sendEmailsTo = [];
106 foreach ($notificationEmails as $notificationEmail) {
107 $sendEmailsTo[] = $notificationEmail;
108 }
109 $subject = sprintf($this->getLanguageService()->getLL('status_updateTask_email_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
110 $message = $this->getNotificationAll() ? $this->getLanguageService()->getLL('status_allNotification') : $this->getLanguageService()->getLL('status_problemNotification');
111 $message .= CRLF . CRLF;
112 $message .= $this->getLanguageService()->getLL('status_updateTask_email_site') . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
113 $message .= CRLF . CRLF;
114 $message .= $this->getLanguageService()->getLL('status_updateTask_email_issues') . ': ' . CRLF;
115 $message .= implode(CRLF, $systemIssues);
116 $message .= CRLF . CRLF;
117 /** @var MailMessage $mail */
118 $mail = GeneralUtility::makeInstance(MailMessage::class);
119 $mail->setTo($sendEmailsTo);
120 $mail->setSubject($subject);
121 $mail->setBody($message);
122 $mail->send();
123 }
124
125 /**
126 * @return bool
127 */
128 public function getNotificationAll(): bool
129 {
130 return $this->notificationAll;
131 }
132
133 /**
134 * @param bool $notificationAll
135 */
136 public function setNotificationAll(bool $notificationAll)
137 {
138 $this->notificationAll = $notificationAll;
139 }
140
141 /**
142 * @return LanguageService
143 */
144 protected function getLanguageService()
145 {
146 return $GLOBALS['LANG'];
147 }
148 }