/** * @test */ public function mailCallsHook() { $to = '*****@*****.**'; $subject = 'Good news everybody!'; $messageBody = 'The hooks works!'; $additionalHeaders = 'Reply-to: jane@example.com'; $additionalParameters = '-f postmaster@example.com'; $mockMailer = $this->getMock('mockMailer', array('mail')); $mockMailer->expects($this->once())->method('mail')->with(array('to' => $to, 'subject' => $subject, 'messageBody' => $messageBody, 'additionalHeaders' => $additionalHeaders, 'additionalParameters' => $additionalParameters), FALSE); $GLOBALS['T3_VAR']['callUserFunction']['mockMailer->mail'] = array('obj' => $mockMailer, 'method' => 'mail'); $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] = array('mockMailer->mail'); t3lib_utility_Mail::mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters); }
/** * Simple substitute for the PHP function mail() which allows you to specify encoding and character set * The fifth parameter ($encoding) will allow you to specify 'base64' encryption for the output (set $encoding=base64) * Further the output has the charset set to UTF-8 by default. * * @param string $email Email address to send to. (see PHP function mail()) * @param string $subject Subject line, non-encoded. (see PHP function mail()) * @param string $message Message content, non-encoded. (see PHP function mail()) * @param string $headers Headers, separated by LF * @param string $encoding Encoding type: "base64", "quoted-printable", "8bit". Default value is "quoted-printable". * @param string $charset Charset used in encoding-headers (only if $encoding is set to a valid value which produces such a header) * @param boolean $dontEncodeHeader If set, the header content will not be encoded. * @return boolean TRUE if mail was accepted for delivery, FALSE otherwise */ public static function plainMailEncoded($email, $subject, $message, $headers = '', $encoding = 'quoted-printable', $charset = '', $dontEncodeHeader = FALSE) { if (!$charset) { $charset = 'utf-8'; } $email = self::normalizeMailAddress($email); if (!$dontEncodeHeader) { // Mail headers must be ASCII, therefore we convert the whole header to either base64 or quoted_printable $newHeaders = array(); foreach (explode(LF, $headers) as $line) { // Split the header in lines and convert each line separately $parts = explode(': ', $line, 2); // Field tags must not be encoded if (count($parts) == 2) { if (0 == strcasecmp($parts[0], 'from')) { $parts[1] = self::normalizeMailAddress($parts[1]); } $parts[1] = self::encodeHeader($parts[1], $encoding, $charset); $newHeaders[] = implode(': ', $parts); } else { $newHeaders[] = $line; // Should never happen - is such a mail header valid? Anyway, just add the unchanged line... } } $headers = implode(LF, $newHeaders); unset($newHeaders); $email = self::encodeHeader($email, $encoding, $charset); // Email address must not be encoded, but it could be appended by a name which should be so (e.g. "Kasper Skårhøj <*****@*****.**>") $subject = self::encodeHeader($subject, $encoding, $charset); } switch ((string) $encoding) { case 'base64': $headers = trim($headers) . LF . 'Mime-Version: 1.0' . LF . 'Content-Type: text/plain; charset="' . $charset . '"' . LF . 'Content-Transfer-Encoding: base64'; $message = trim(chunk_split(base64_encode($message . LF))) . LF; // Adding LF because I think MS outlook 2002 wants it... may be removed later again. break; case '8bit': $headers = trim($headers) . LF . 'Mime-Version: 1.0' . LF . 'Content-Type: text/plain; charset=' . $charset . LF . 'Content-Transfer-Encoding: 8bit'; break; case 'quoted-printable': default: $headers = trim($headers) . LF . 'Mime-Version: 1.0' . LF . 'Content-Type: text/plain; charset=' . $charset . LF . 'Content-Transfer-Encoding: quoted-printable'; $message = self::quoted_printable($message); break; } // Headers must be separated by CRLF according to RFC 2822, not just LF. // But many servers (Gmail, for example) behave incorrectly and want only LF. // So we stick to LF in all cases. $headers = trim(implode(LF, self::trimExplode(LF, $headers, TRUE))); // Make sure no empty lines are there. return t3lib_utility_Mail::mail($email, $subject, $message, $headers); }
/** * 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: * t3lib_div::sysLog('[write message in English here]', 'extension_key', 'severity'); * * @param string Message (in English). * @param string Extension key (from which extension you are calling the log) or "Core" * @param integer 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) { global $TYPO3_CONF_VARS; $severity = self::intInRange($severity, 0, 4); // is message worth logging? if (intval($TYPO3_CONF_VARS['SYS']['systemLogLevel']) > $severity) { return; } // initialize logging if (!$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit']) { self::initSysLog(); } // do custom logging if (isset($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog']) && is_array($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 ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'] as $hookMethod) { self::callUserFunction($hookMethod, $params, $fakeThis); } } // TYPO3 logging enabled? if (!$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(';', $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') { $file = fopen($destination, 'a'); if ($file) { flock($file, LOCK_EX); // try locking, but ignore if not available (eg. on NFS and FAT) fwrite($file, date($dateFormat . ' ' . $timeFormat) . $msgLine . LF); flock($file, LOCK_UN); // release the lock fclose($file); } } elseif ($type == 'mail') { list($to, $from) = explode('/', $destination); t3lib_utility_Mail::mail($to, 'Warning - error in TYPO3 installation', 'Host: ' . $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] . LF . 'Extension: ' . $extKey . LF . 'Severity: ' . $severity . LF . LF . $msg, $from ? 'From: ' . $from : ''); } elseif ($type == 'error_log') { error_log($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 the mail by calling the mail() function in php. On Linux systems this will invoke the MTA * defined in php.ini (sendmail -t -i by default), on Windows a SMTP must be specified in the sys.ini. * Most common MTA's on Linux has a Sendmail interface, including Postfix and Exim. * For setting the return-path correctly, the parameter -f has to be added to the system call to sendmail. * This obviously does not have any effect on Windows, but on Sendmail compliant systems this works. If safe mode * is enabled, then extra parameters is not allowed, so a safe mode check is made before the mail() command is * invoked. When using the -f parameter, some MTA's will put an X-AUTHENTICATION-WARNING saying that * the return path was modified manually with the -f flag. To disable this warning make sure that the user running * Apache is in the /etc/mail/trusted-users table. * * POSTFIX: With postfix version below 2.0 there is a problem that the -f parameter can not be used in conjunction * with -t. Postfix will give an error in the maillog: * * cannot handle command-line recipients with -t * * The -f parameter is only enabled if the parameter forceReturnPath is enabled in the install tool. * * This whole problem of return-path turns out to be quite tricky. If you have a solution that works better, on all * standard MTA's then we are very open for suggestions. * * With time this function should be made such that several ways of sending the mail is possible (local MTA, smtp other). * * @return boolean Returns whether the mail was sent (successfully accepted for delivery) */ public function sendTheMail() { $mailWasSent = FALSE; // Sending the mail requires the recipient and message to be set. if (!trim($this->recipient) || !trim($this->message)) { return FALSE; } // On windows the -f flag is not used (specific for Sendmail and Postfix), // but instead the php.ini parameter sendmail_from is used. $returnPath = $this->forceReturnPath && strlen($this->returnPath) > 0 ? '-f ' . escapeshellarg($this->returnPath) : ''; if (TYPO3_OS == 'WIN' && $this->returnPath) { @ini_set('sendmail_from', t3lib_div::normalizeMailAddress($this->returnPath)); } $recipient = t3lib_div::normalizeMailAddress($this->recipient); // If safe mode is on, the fifth parameter to mail is not allowed, so the fix wont work on unix with safe_mode=On $returnPathPossible = !t3lib_utility_PhpOptions::isSafeModeEnabled() && $this->forceReturnPath; if ($returnPathPossible) { $mailWasSent = t3lib_utility_Mail::mail($recipient, $this->subject, $this->message, $this->headers, $returnPath); } else { $mailWasSent = t3lib_utility_Mail::mail($recipient, $this->subject, $this->message, $this->headers); } // Auto response if ($this->auto_respond_msg) { $theParts = explode('/', $this->auto_respond_msg, 2); $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]); $theParts[1] = str_replace("/", LF, $theParts[1]); $theParts[1] = str_replace("###MESSAGE###", $this->getContent('plain'), $theParts[1]); if ($returnPathPossible) { $mailWasSent = t3lib_utility_Mail::mail($this->from_email, $theParts[0], $theParts[1], 'From: ' . $recipient . $this->linebreak . $this->plain_text_header, $returnPath); } else { $mailWasSent = t3lib_utility_Mail::mail($this->from_email, $theParts[0], $theParts[1], 'From: ' . $recipient . $this->linebreak . $this->plain_text_header); } } if ($this->returnPath) { ini_restore('sendmail_from'); } return $mailWasSent; }
/** * 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) { t3lib_utility_Mail::mail($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'], $prefix . ' ' . $subject, $msg, $this->notifyHeader); } } // If An email should be sent to the current user, do that: if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) { t3lib_utility_Mail::mail($this->user['email'], $subject, $msg, $this->notifyHeader); } } }
/** * 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 has been numerous 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; } t3lib_utility_Mail::mail($email, $subject, $email_body, 'From: TYPO3 Login WARNING<>'); $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 } } }