/**
  * Logs message to the system log.
  * This should be implemented around the source code, including the Core and both frontend and backend, logging serious errors.
  * If you want to implement the sysLog in your applications, simply add lines like:
  *		 tx_div2007_div::sysLog('[write message in English here]', 'extension_key', 'severity');
  *
  * @param string $msg Message (in English).
  * @param string $extKey Extension key (from which extension you are calling the log) or "Core"
  * @param integer $severity Severity: 0 is info, 1 is notice, 2 is warning, 3 is error, 4 is fatal error
  * @return void
  */
 public static function sysLog($msg, $extKey, $severity = 0)
 {
     $severity = tx_div2007_core::intInRange($severity, 0, 4);
     // is message worth logging?
     if (intval($GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLogLevel']) > $severity) {
         return;
     }
     // initialize logging
     if (!$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit']) {
         self::initSysLog();
     }
     // do custom logging
     if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'])) {
         $params = array('msg' => $msg, 'extKey' => $extKey, 'backTrace' => debug_backtrace(), 'severity' => $severity);
         $fakeThis = FALSE;
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'] as $hookMethod) {
             self::callUserFunction($hookMethod, $params, $fakeThis);
         }
     }
     // TYPO3 logging enabled?
     if (!$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLog']) {
         return;
     }
     $dateFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'];
     $timeFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
     // use all configured logging options
     foreach (explode(';', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLog'], 2) as $log) {
         list($type, $destination, $level) = explode(',', $log, 4);
         // is message worth logging for this log type?
         if (intval($level) > $severity) {
             continue;
         }
         $msgLine = ' - ' . $extKey . ': ' . $msg;
         // write message to a file
         if ($type == 'file') {
             $lockObject = self::makeInstance('t3lib_lock', $destination, $GLOBALS['TYPO3_CONF_VARS']['SYS']['lockingMode']);
             /** @var t3lib_lock $lockObject */
             $lockObject->setEnableLogging(FALSE);
             $lockObject->acquire();
             $file = fopen($destination, 'a');
             if ($file) {
                 fwrite($file, date($dateFormat . ' ' . $timeFormat) . $msgLine . LF);
                 fclose($file);
                 self::fixPermissions($destination);
             }
             $lockObject->release();
         } elseif ($type == 'mail') {
             list($to, $from) = explode('/', $destination);
             if (!self::validEmail($from)) {
                 $from = t3lib_utility_Mail::getSystemFrom();
             }
             /** @var $mail t3lib_mail_Message */
             $mail = self::makeInstance('t3lib_mail_Message');
             $mail->setTo($to)->setFrom($from)->setSubject('Warning - error in TYPO3 installation')->setBody('Host: ' . $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] . LF . 'Extension: ' . $extKey . LF . 'Severity: ' . $severity . LF . LF . $msg);
             $mail->send();
         } elseif ($type == 'error_log') {
             error_log($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] . $msgLine, 0);
         } elseif ($type == 'syslog') {
             $priority = array(LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT);
             syslog($priority[(int) $severity], $msgLine);
         }
     }
 }
    /**
     * Sends a warning to $email if there has been a certain amount of failed logins during a period.
     * If a login fails, this function is called. It will look up the sys_log to see if there has been more than $max failed logins the last $secondsBack seconds (default 3600). If so, an email with a warning is sent to $email.
     *
     * @param	string		Email address
     * @param	integer		Number of sections back in time to check. This is a kind of limit for how many failures an hour for instance.
     * @param	integer		Max allowed failures before a warning mail is sent
     * @return	void
     * @access private
     */
    function checkLogFailures($email, $secondsBack = 3600, $max = 3)
    {
        if ($email) {
            // get last flag set in the log for sending
            $theTimeBack = $GLOBALS['EXEC_TIME'] - $secondsBack;
            $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tstamp', 'sys_log', 'type=255 AND action=4 AND tstamp>' . intval($theTimeBack), '', 'tstamp DESC', '1');
            if ($testRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                $theTimeBack = $testRow['tstamp'];
            }
            // Check for more than $max number of error failures with the last period.
            $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_log', 'type=255 AND action=3 AND error!=0 AND tstamp>' . intval($theTimeBack), '', 'tstamp');
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > $max) {
                // OK, so there were more than the max allowed number of login failures - so we will send an email then.
                $subject = 'TYPO3 Login Failure Warning (at ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . ')';
                $email_body = 'There have been some attempts (' . $GLOBALS['TYPO3_DB']->sql_num_rows($res) . ') to login at the TYPO3
site "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '" (' . t3lib_div::getIndpEnv('HTTP_HOST') . ').

This is a dump of the failures:

';
                while ($testRows = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                    $theData = unserialize($testRows['log_data']);
                    $email_body .= date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'], $testRows['tstamp']) . ':  ' . @sprintf($testRows['details'], '' . $theData[0], '' . $theData[1], '' . $theData[2]);
                    $email_body .= LF;
                }
                $from = t3lib_utility_Mail::getSystemFrom();
                /** @var $mail t3lib_mail_Message */
                $mail = t3lib_div::makeInstance('t3lib_mail_Message');
                $mail->setTo($email)->setFrom($from)->setSubject($subject)->setBody($email_body);
                $mail->send();
                $this->writelog(255, 4, 0, 3, 'Failure warning (%s failures within %s seconds) sent by email to %s', array($GLOBALS['TYPO3_DB']->sql_num_rows($res), $secondsBack, $email));
                // Logout written to log
            }
        }
    }
 /**
  * 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;
             }
         }
     }
     $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;
     $from = t3lib_utility_Mail::getSystemFrom();
     $mail = t3lib_div::makeInstance('t3lib_mail_Message');
     $mail->setFrom($from);
     $mail->setTo($this->notificationEmail);
     $mail->setSubject($subject);
     $mail->setBody($message);
     $mail->send();
 }
 /**
  * Will send an email notification to warning_email_address/the login users email address when a login session is just started.
  * Depends on various parameters whether mails are send and to whom.
  *
  * @return	void
  * @access private
  */
 function emailAtLogin()
 {
     if ($this->loginSessionStarted) {
         // Send notify-mail
         $subject = 'At "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '"' . ' from ' . t3lib_div::getIndpEnv('REMOTE_ADDR') . (t3lib_div::getIndpEnv('REMOTE_HOST') ? ' (' . t3lib_div::getIndpEnv('REMOTE_HOST') . ')' : '');
         $msg = sprintf('User "%s" logged in from %s (%s) at "%s" (%s)', $this->user['username'], t3lib_div::getIndpEnv('REMOTE_ADDR'), t3lib_div::getIndpEnv('REMOTE_HOST'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], t3lib_div::getIndpEnv('HTTP_HOST'));
         // Warning email address
         if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
             $warn = 0;
             $prefix = '';
             if (intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 1) {
                 // first bit: All logins
                 $warn = 1;
                 $prefix = $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
             }
             if ($this->isAdmin() && intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 2) {
                 // second bit: Only admin-logins
                 $warn = 1;
                 $prefix = '[AdminLoginWarning]';
             }
             if ($warn) {
                 $from = t3lib_utility_Mail::getSystemFrom();
                 /** @var $mail t3lib_mail_Message */
                 $mail = t3lib_div::makeInstance('t3lib_mail_Message');
                 $mail->setTo($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'])->setFrom($from)->setSubject($prefix . ' ' . $subject)->setBody($msg);
                 $mail->send();
             }
         }
         // If An email should be sent to the current user, do that:
         if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) {
             $from = t3lib_utility_Mail::getSystemFrom();
             /** @var $mail t3lib_mail_Message */
             $mail = t3lib_div::makeInstance('t3lib_mail_Message');
             $mail->setTo($this->user['email'])->setFrom($from)->setSubject($subject)->setBody($msg);
             $mail->send();
         }
     }
 }