Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / tasks / class.tx_reports_tasks_systemstatusupdatetask.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
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 * @package TYPO3
31 * @subpackage reports
32 */
33 class tx_reports_tasks_SystemStatusUpdateTask extends tx_scheduler_Task {
34
35 /**
36 * Email address to send email notification to in case we find problems with
37 * the system.
38 *
39 * @var string
40 */
41 protected $notificationEmail = NULL;
42
43 /**
44 * Executes the System Status Update task, determing the highest severity of
45 * status reports and saving that to the registry to be displayed at login
46 * if necessary.
47 *
48 * @see typo3/sysext/scheduler/tx_scheduler_Task::execute()
49 */
50 public function execute() {
51 $registry = t3lib_div::makeInstance('t3lib_Registry');
52 $statusReport = t3lib_div::makeInstance('tx_reports_reports_Status');
53
54 $systemStatus = $statusReport->getSystemStatus();
55 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
56
57 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
58
59 if ($highestSeverity > tx_reports_reports_status_Status::OK) {
60 $this->sendNotificationEmail($systemStatus);
61 }
62
63 return true;
64 }
65
66 /**
67 * Gets the notification email address.
68 *
69 * @return string Notification email address.
70 */
71 public function getNotificationEmail() {
72 return $this->notificationEmail;
73 }
74
75 /**
76 * Sets the notification email address.
77 *
78 * @param string $notificationEmail Notification email address.
79 */
80 public function setNotificationEmail($notificationEmail) {
81 $this->notificationEmail = $notificationEmail;
82 }
83
84 /**
85 * Sends a notification email, reporting system issues.
86 *
87 * @param array $systemStatus Array of statuses
88 */
89 protected function sendNotificationEmail(array $systemStatus) {
90 $systemIssues = array();
91
92 foreach ($systemStatus as $statusProvider) {
93 foreach ($statusProvider as $status) {
94 if ($status->getSeverity() > tx_reports_reports_status_Status::OK) {
95 $systemIssues[] = (string) $status;
96 }
97 }
98 }
99
100 $fromEmail = $this->getFromAddress();
101
102 $subject = sprintf(
103 $GLOBALS['LANG']->getLL('status_updateTask_email_subject'),
104 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']
105 );
106
107 $message = sprintf(
108 $GLOBALS['LANG']->getLL('status_problemNotification'),
109 '',
110 ''
111 );
112 $message .= CRLF . CRLF;
113 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_site')
114 . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
115 $message .= CRLF . CRLF;
116 $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_issues')
117 . ': ' .CRLF;
118 $message .= implode(CRLF, $systemIssues);
119 $message .= CRLF . CRLF;
120
121 $mail = t3lib_div::makeInstance('t3lib_mail_Message');
122 $mail->setFrom(array($fromEmail => 'TYPO3 CMS'));
123 $mail->setTo($this->notificationEmail);
124 $mail->setSubject($subject);
125 $mail->setBody($message);
126
127 $mail->send();
128 }
129
130 /**
131 * Tries to generate an email address to use for the From field.
132 *
133 * @return string email address
134 */
135 protected function getFromAddress() {
136 $user = 'no-reply';
137 $host = php_uname('n');
138
139 // just get us a domain record we can use
140 $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
141 'domainName',
142 'sys_domain',
143 'hidden = 0',
144 '',
145 'pid ASC, sorting ASC'
146 );
147
148 if (!empty($domainRecord['domainName'])) {
149 $tempUrl = $domainRecord['domainName'];
150
151 if (!t3lib_div::isFirstPartOfStr($tempUrl, 'http')) {
152 // shouldn't be the case anyways, but you never know
153 // ... there're crazy people out there
154 $tempUrl = 'http://' .$tempUrl;
155 }
156
157 $host = parse_url($tempUrl, PHP_URL_HOST);
158 }
159
160 return $user . '@' . $host;
161 }
162 }
163
164
165 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetask.php'])) {
166 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetask.php']);
167 }
168
169 ?>