[TASK] Add verbose output to report mail for failed tests
[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\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\MailUtility;
19 use TYPO3\CMS\Reports\Status;
20 use TYPO3\CMS\Scheduler\Task\AbstractTask;
21
22 /**
23 * A task that should be run regularly to determine the system's status.
24 *
25 * @author Ingo Renner <ingo@typo3.org>
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 * Executes the System Status Update task, determining the highest severity of
39 * status reports and saving that to the registry to be displayed at login
40 * if necessary.
41 *
42 * @see \TYPO3\CMS\Scheduler\Task\AbstractTask::execute()
43 */
44 public function execute() {
45 /** @var $registry \TYPO3\CMS\Core\Registry */
46 $registry = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
47 /** @var $statusReport \TYPO3\CMS\Reports\Report\Status\Status */
48 $statusReport = GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Report\\Status\\Status');
49 $systemStatus = $statusReport->getDetailedSystemStatus();
50 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
51 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
52 if ($highestSeverity > Status::OK) {
53 $this->sendNotificationEmail($systemStatus);
54 }
55 return TRUE;
56 }
57
58 /**
59 * Gets the notification email addresses.
60 *
61 * @return string Notification email addresses.
62 */
63 public function getNotificationEmail() {
64 return $this->notificationEmail;
65 }
66
67 /**
68 * Sets the notification email address.
69 *
70 * @param string $notificationEmail Notification email address.
71 * @return void
72 */
73 public function setNotificationEmail($notificationEmail) {
74 $this->notificationEmail = $notificationEmail;
75 }
76
77 /**
78 * Sends a notification email, reporting system issues.
79 *
80 * @param Status[] $systemStatus Array of statuses
81 * @return void
82 */
83 protected function sendNotificationEmail(array $systemStatus) {
84 $systemIssues = array();
85 foreach ($systemStatus as $statusProvider) {
86 /** @var Status $status */
87 foreach ($statusProvider as $status) {
88 if ($status->getSeverity() > Status::OK) {
89 $systemIssues[] = (string)$status . CRLF . $status->getMessage() . CRLF . CRLF;
90 }
91 }
92 }
93 $notificationEmails = GeneralUtility::trimExplode(LF, $this->notificationEmail, TRUE);
94 $sendEmailsTo = array();
95 foreach ($notificationEmails as $notificationEmail) {
96 $sendEmailsTo[] = $notificationEmail;
97 }
98 $subject = sprintf($GLOBALS['LANG']->getLL('status_updateTask_email_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
99 $message = sprintf($GLOBALS['LANG']->getLL('status_problemNotification'), '', '');
100 $message .= CRLF . CRLF;
101 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_site') . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
102 $message .= CRLF . CRLF;
103 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_issues') . ': ' . CRLF;
104 $message .= implode(CRLF, $systemIssues);
105 $message .= CRLF . CRLF;
106 $from = MailUtility::getSystemFrom();
107 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
108 $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
109 $mail->setFrom($from);
110 $mail->setTo($sendEmailsTo);
111 $mail->setSubject($subject);
112 $mail->setBody($message);
113 $mail->send();
114 }
115
116 }