Added feature #17033: [reports] Notification Emails for system status updates
authorIngo Renner <ingo.renner@typo3.org>
Mon, 17 Jan 2011 10:56:55 +0000 (10:56 +0000)
committerIngo Renner <ingo.renner@typo3.org>
Mon, 17 Jan 2011 10:56:55 +0000 (10:56 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@10098 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
typo3/sysext/reports/ChangeLog
typo3/sysext/reports/ext_autoload.php
typo3/sysext/reports/ext_localconf.php
typo3/sysext/reports/reports/locallang.xml
typo3/sysext/reports/reports/status/class.tx_reports_reports_status_status.php
typo3/sysext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetask.php
typo3/sysext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php [new file with mode: 0644]

index 24de9fe..27d093f 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2011-01-17  Ingo Renner  <ingo@typo3.org>
+
+       * Added feature #17033: [reports] Notification Emails for system status updates
+
 2011-01-17  Steffen Kamper  <steffen@typo3.org>
 
        * Fixed bug #17073: Final Integration of the pagetree (Thanks to Stefan Galinski)
index 4e18968..94cdbdf 100644 (file)
@@ -1,3 +1,7 @@
+2011-01-17  Ingo Renner  <ingo@typo3.org>
+
+       * Added feature #17033: Notification Emails for system status updates
+
 2011-01-13  Ingo Renner  <ingo@typo3.org>
 
        * Fixed issue #17012: Use javascript goToModule() function in the warning message
index 76c885b..352194c 100644 (file)
@@ -16,5 +16,6 @@ return array(
        'tx_reports_reports_status_configurationstatus' => $extensionPath . 'reports/status/class.tx_reports_reports_status_configurationstatus.php',
        'tx_reports_reports_status_status' => $extensionPath . 'reports/status/class.tx_reports_reports_status_status.php',
        'tx_reports_tasks_systemstatusupdatetask' => $extensionPath . 'tasks/class.tx_reports_tasks_systemstatusupdatetask.php',
+       'tx_reports_tasks_systemstatusupdatetasknotificationemailfield' => $extensionPath . 'tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php',
 );
 ?>
\ No newline at end of file
index 3141a2c..7a79e83 100644 (file)
@@ -6,9 +6,10 @@ if (!defined ('TYPO3_MODE')) {
 }
 
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_reports_tasks_SystemStatusUpdateTask'] = array(
-       'extension'   => $_EXTKEY,
-       'title'       => 'LLL:EXT:' . $_EXTKEY . '/reports/locallang.xml:status_updateTaskTitle',
-       'description' => 'LLL:EXT:' . $_EXTKEY . '/reports/locallang.xml:status_updateTaskDescription'
+       'extension'        => $_EXTKEY,
+       'title'            => 'LLL:EXT:' . $_EXTKEY . '/reports/locallang.xml:status_updateTaskTitle',
+       'description'      => 'LLL:EXT:' . $_EXTKEY . '/reports/locallang.xml:status_updateTaskDescription',
+       'additionalFields' => 'tx_reports_tasks_SystemStatusUpdateTaskNotificationEmailField'
 );
 
 $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['displayWarningMessages']['tx_reports_WarningMessagePostProcessor'] = 'EXT:reports/reports/status/class.tx_reports_reports_status_warningmessagepostprocessor.php:tx_reports_reports_status_WarningMessagePostProcessor';
index b567d5f..2b957a7 100644 (file)
                        <label index="status_configuration_DeprecationLogSize">Your deprecation log file currently takes up %s.</label>
                        <label index="status_updateTaskTitle">System Status Update</label>
                        <label index="status_updateTaskDescription">Runs a system status check and sends notifications if problems have been found.</label>
+                       <label index="status_updateTaskField_notificationEmail">Notification Email Address</label>
+                       <label index="status_updateTaskField_notificationEmail_invlaid">Empty or invalid notification email address.</label>
+                       <label index="status_updateTask_email_subject">System Status Notification for site %s</label>
+                       <label index="status_updateTask_email_site">Site</label>
+                       <label index="status_updateTask_email_issues">Issues</label>
                </languageKey>
        </data>
 </T3locallang>
\ No newline at end of file
index db0e4b8..91969c8 100644 (file)
@@ -99,6 +99,29 @@ class tx_reports_reports_status_Status {
        public function getSeverity() {
                return $this->severity;
        }
+
+       /**
+        * Creates a string representation of a status.
+        *
+        * @return      string  String representation of this status.
+        */
+       public function __toString() {
+               $severity = array(
+                       self::NOTICE  => 'NOTE',
+                       self::INFO    => 'INFO',
+                       self::OK      => 'OK',
+                       self::WARNING => 'WARN',
+                       self::ERROR   => 'ERR',
+               );
+
+                       // max length 80 characters
+               $stringRepresentation =
+                       str_pad('[' . $severity[$this->severity] . ']', 7) .
+                       str_pad($this->title, 40) . ' - ' .
+                       substr($this->value, 0, 30);
+
+               return $stringRepresentation;
+       }
 }
 
 
index fdeb489..b7957f6 100644 (file)
 class tx_reports_tasks_SystemStatusUpdateTask extends tx_scheduler_Task {
 
        /**
+        * Email address to send email notification to in case we find problems with
+        * the system.
+        *
+        * @var string
+        */
+       protected $notificationEmail = NULL;
+
+       /**
         * Executes the System Status Update task, determing the highest severity of
         * status reports and saving that to the registry to be displayed at login
         * if necessary.
@@ -48,8 +56,109 @@ class tx_reports_tasks_SystemStatusUpdateTask extends tx_scheduler_Task {
 
                $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
 
+               if ($highestSeverity > tx_reports_reports_status_Status::OK) {
+                       $this->sendNotificationEmail($systemStatus);
+               }
+
                return true;
        }
+
+       /**
+        * Gets the notification email address.
+        *
+        * @return      string  Notification email address.
+        */
+       public function getNotificationEmail() {
+               return $this->notificationEmail;
+       }
+
+       /**
+        * Sets the notification email address.
+        *
+        * @param       string  $notificationEmail Notification email address.
+        */
+       public function setNotificationEmail($notificationEmail) {
+               $this->notificationEmail = $notificationEmail;
+       }
+
+       /**
+        * Sends a notification email, reporting system issues.
+        *
+        * @param       array   $systemStatus Array of statuses
+        */
+       protected function sendNotificationEmail(array $systemStatus) {
+               $systemIssues = array();
+
+               foreach ($systemStatus as $statusProvider) {
+                       foreach ($statusProvider as $status) {
+                               if ($status->getSeverity() > tx_reports_reports_status_Status::OK) {
+                                       $systemIssues[] = (string) $status;
+                               }
+                       }
+               }
+
+               $fromEmail = $this->getFromAddress();
+
+               $subject = sprintf(
+                       $GLOBALS['LANG']->getLL('status_updateTask_email_subject'),
+                       $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']
+               );
+
+               $message = sprintf(
+                       $GLOBALS['LANG']->getLL('status_problemNotification'),
+                       '',
+                       ''
+               );
+               $message .= CRLF . CRLF;
+               $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_site')
+                       . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
+               $message .= CRLF . CRLF;
+               $message .= $GLOBALS['LANG']->getLL('status_updateTask_email_issues')
+                       . ': ' .CRLF;
+               $message .= implode(CRLF, $systemIssues);
+               $message .= CRLF . CRLF;
+
+               $mail = t3lib_div::makeInstance('t3lib_mail_Message');
+               $mail->setFrom(array($fromEmail => 'TYPO3 CMS'));
+               $mail->setTo($this->notificationEmail);
+               $mail->setSubject($subject);
+               $mail->setBody($message);
+
+               $mail->send();
+       }
+
+       /**
+        * Tries to generate an email address to use for the From field.
+        *
+        * @return      string  email address
+        */
+       protected function getFromAddress() {
+               $user = 'no-reply';
+               $host = php_uname('n');
+
+                       // just get us a domain record we can use
+               $domainRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
+                       'domainName',
+                       'sys_domain',
+                       'hidden = 0',
+                       '',
+                       'pid ASC, sorting ASC'
+               );
+
+               if (!empty($domainRecord['domainName'])) {
+                       $tempUrl = $domainRecord['domainName'];
+
+                       if (!t3lib_div::isFirstPartOfStr($tempUrl, 'http')) {
+                                       // shouldn't be the case anyways, but you never know
+                                       // ... there're crazy people out there
+                               $tempUrl = 'http://' .$tempUrl;
+                       }
+
+                       $host = parse_url($tempUrl, PHP_URL_HOST);
+               }
+
+               return $user . '@' . $host;
+       }
 }
 
 
diff --git a/typo3/sysext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php b/typo3/sysext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php
new file mode 100644 (file)
index 0000000..db8f277
--- /dev/null
@@ -0,0 +1,147 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2010 Ingo Renner <ingo@typo3.org>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+
+/**
+ * Additional field to set the notification email address(es) for system health
+ * issue notifications.
+ *
+ * @author     Ingo Renner <ingo@typo3.org>
+ * @package    TYPO3
+ * @subpackage reports
+ */
+class tx_reports_tasks_SystemStatusUpdateTaskNotificationEmailField implements tx_scheduler_AdditionalFieldProvider {
+
+
+       /**
+        * Additional fields
+        *
+        * @var array
+        */
+       protected $fields = array('notificationEmail');
+
+       /**
+        * Field prefix.
+        *
+        * @var string
+        */
+       protected $fieldPrefix = 'SystemStatusUpdate';
+
+       /**
+        * Gets additional fields to render in the form to add/edit a task
+        *
+        * @param       array   $taskInfo Values of the fields from the add/edit task form
+        * @param       tx_scheduler_Task       $task The task object being eddited. Null when adding a task!
+        * @param       tx_scheduler_Module     $schedulerModule Reference to the scheduler backend module
+        * @return      array   A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
+        */
+       public function getAdditionalFields(array &$taskInfo, $task, tx_scheduler_Module $schedulerModule) {
+               $fields = array('notificationEmail');
+
+               if ($schedulerModule->CMD == 'edit') {
+                       $taskInfo[$this->fieldPrefix . 'NotificationEmail'] = $task->getNotificationEmail();
+               }
+
+               $additionalFields = array();
+               foreach ($fields as $field) {
+                       $fieldName = $this->getFullFieldName($field);
+                       $fieldId   = 'task_' . $fieldName;
+                       $fieldHtml = '<input type="text" '
+                               . 'name="tx_scheduler[' . $fieldName . ']" '
+                               . 'id="' . $fieldId . '" '
+                               . 'value="' . $taskInfo[$fieldName] . '" />';
+
+                       $additionalFields[$fieldId] = array(
+                               'code'     => $fieldHtml,
+                               'label'    => 'LLL:EXT:reports/reports/locallang.xml:status_updateTaskField_' . $field,
+                               'cshKey'   => '',
+                               'cshLabel' => $fieldId
+                       );
+               }
+
+               return $additionalFields;
+       }
+
+       /**
+        * Validates the additional fields' values
+        *
+        * @param       array   $submittedData An array containing the data submitted by the add/edit task form
+        * @param       tx_scheduler_Module     $schedulerModule Reference to the scheduler backend module
+        * @return      boolean True if validation was ok (or selected class is not relevant), false otherwise
+        */
+       public function validateAdditionalFields(array &$submittedData, tx_scheduler_Module $schedulerModule) {
+               $validInput = TRUE;
+               $submittedData[$this->fieldPrefix . 'NotificationEmail'] = trim($submittedData[$this->fieldPrefix . 'NotificationEmail']);
+
+               if (
+                       empty($submittedData[$this->fieldPrefix . 'NotificationEmail'])
+                       || !filter_var($submittedData[$this->fieldPrefix . 'NotificationEmail'], FILTER_VALIDATE_EMAIL)
+               ) {
+                       $schedulerModule->addMessage(
+                               $GLOBALS['LANG']->sL('LLL:EXT:reports/reports/locallang.xml:status_updateTaskField_notificationEmail_invalid'),
+                               t3lib_FlashMessage::ERROR
+                       );
+                       $validInput = FALSE;
+               }
+
+               return $validInput;
+       }
+
+       /**
+        * Takes care of saving the additional fields' values in the task's object
+        *
+        * @param       array   $submittedData An array containing the data submitted by the add/edit task form
+        * @param       tx_scheduler_Task       $task Reference to the scheduler backend module
+        * @return      void
+        */
+       public function saveAdditionalFields(array $submittedData, tx_scheduler_Task $task) {
+
+               if (!($task instanceof tx_reports_tasks_SystemStatusUpdateTask)) {
+                       throw new InvalidArgumentException(
+                               'Expected a task of type tx_reports_tasks_SystemStatusUpdateTask, but got ' . get_class($task),
+                               1295012802
+                       );
+               }
+
+               $task->setNotificationEmail($submittedData[$this->fieldPrefix . 'NotificationEmail']);
+       }
+
+       /**
+        * Constructs the full field name which can be used in HTML markup.
+        *
+        * @param       string  $fieldName A raw field name
+        * @return      string Field name ready to use in HTML markup
+        */
+       protected function getFullFieldName($fieldName) {
+               return $this->fieldPrefix . ucfirst($fieldName);
+       }
+
+}
+
+
+if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php'])) {
+       include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/tasks/class.tx_reports_tasks_systemstatusupdatetasknotificationemailfield.php']);
+}
+
+?>
\ No newline at end of file