da844253f2b71784907202a5753929679a8ddc89
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Task / SystemStatusUpdateTask.php
1 <?php
2 namespace TYPO3\CMS\Reports\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A task that should be run regularly to determine the system's status.
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 */
31 class SystemStatusUpdateTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
32
33 /**
34 * Email addresses to send email notification to in case we find problems with
35 * the system.
36 *
37 * @var string
38 */
39 protected $notificationEmail = NULL;
40
41 /**
42 * Executes the System Status Update task, determining the highest severity of
43 * status reports and saving that to the registry to be displayed at login
44 * if necessary.
45 *
46 * @see \TYPO3\CMS\Scheduler\Task\AbstractTask::execute()
47 */
48 public function execute() {
49 /** @var $registry \TYPO3\CMS\Core\Registry */
50 $registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
51 /** @var $statusReport \TYPO3\CMS\Reports\Report\Status\Status */
52 $statusReport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Report\\Status\\Status');
53 $systemStatus = $statusReport->getSystemStatus();
54 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
55 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
56 if ($highestSeverity > \TYPO3\CMS\Reports\Status::OK) {
57 $this->sendNotificationEmail($systemStatus);
58 }
59 return TRUE;
60 }
61
62 /**
63 * Gets the notification email addresses.
64 *
65 * @return string Notification email addresses.
66 */
67 public function getNotificationEmail() {
68 return $this->notificationEmail;
69 }
70
71 /**
72 * Sets the notification email address.
73 *
74 * @param string $notificationEmail Notification email address.
75 * @return void
76 */
77 public function setNotificationEmail($notificationEmail) {
78 $this->notificationEmail = $notificationEmail;
79 }
80
81 /**
82 * Sends a notification email, reporting system issues.
83 *
84 * @param array $systemStatus Array of statuses
85 * @return void
86 */
87 protected function sendNotificationEmail(array $systemStatus) {
88 $systemIssues = array();
89 foreach ($systemStatus as $statusProvider) {
90 foreach ($statusProvider as $status) {
91 if ($status->getSeverity() > \TYPO3\CMS\Reports\Status::OK) {
92 $systemIssues[] = (string) $status;
93 }
94 }
95 }
96 $notificationEmails = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $this->notificationEmail, TRUE);
97 $sendEmailsTo = array();
98 foreach ($notificationEmails as $notificationEmail) {
99 $sendEmailsTo[] = $notificationEmail;
100 }
101 $subject = sprintf($GLOBALS['LANG']->getLL('status_updateTask_email_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
102 $message = sprintf($GLOBALS['LANG']->getLL('status_problemNotification'), '', '');
103 $message .= CRLF . CRLF;
104 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_site') . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
105 $message .= CRLF . CRLF;
106 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_issues') . ': ' . CRLF;
107 $message .= implode(CRLF, $systemIssues);
108 $message .= CRLF . CRLF;
109 $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
110 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
111 $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
112 $mail->setFrom($from);
113 $mail->setTo($sendEmailsTo);
114 $mail->setSubject($subject);
115 $mail->setBody($message);
116 $mail->send();
117 }
118
119 }