From 9a0f68701baa295b2546aa234704f50b8355ddcf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 29 Sep 2012 12:52:18 +0000 Subject: [PATCH] Added missing email-functions.php :( --- .gitattributes | 1 + inc/config-global.php | 2 +- inc/email-functions.php | 227 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 inc/email-functions.php diff --git a/.gitattributes b/.gitattributes index 0600d9c829..e0acc3d335 100644 --- a/.gitattributes +++ b/.gitattributes @@ -130,6 +130,7 @@ inc/debug/client/.htaccess svneol=native#text/plain inc/debug/relay/.htaccess svneol=native#text/plain inc/debug/request_ svneol=native#text/plain inc/debug/server/.htaccess svneol=native#text/plain +inc/email-functions.php -text inc/expression-functions.php svneol=native#text/plain inc/extensions-functions.php svneol=native#text/plain inc/extensions/.htaccess svneol=native#text/plain diff --git a/inc/config-global.php b/inc/config-global.php index f2ef6fd7fc..6fba83c71e 100644 --- a/inc/config-global.php +++ b/inc/config-global.php @@ -63,7 +63,7 @@ if (function_exists('import_request_variables')) { $path = str_replace(chr(92), '/', substr(dirname(__FILE__), 0, -3)); // Some very important function includes -foreach (array('config', 'wrapper', 'template', 'module', 'inc', 'stats', 'http', 'xml', 'callback', 'referral') as $inc) { +foreach (array('config', 'wrapper', 'template', 'module', 'inc', 'stats', 'http', 'xml', 'callback', 'referral', 'email') as $inc) { include($path . 'inc/' . $inc . '-functions.php'); } // END - foreach diff --git a/inc/email-functions.php b/inc/email-functions.php new file mode 100644 index 0000000000..ced06aa037 --- /dev/null +++ b/inc/email-functions.php @@ -0,0 +1,227 @@ + 0)) { + // Does the user exist? + if ((isExtensionActive('user')) && (fetchUserData($toEmail))) { + // Get the email + $toEmail = getUserData('email'); + } else { + // Set webmaster + $toEmail = getWebmaster(); + } + } elseif ($toEmail == '0') { + // Is the webmaster! + $toEmail = getWebmaster(); + } + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'TO=' . $toEmail); + + // Check for PHPMailer or debug-mode + if ((!isPhpMailerConfigured()) || (isDebugModeEnabled())) { + // Prefix is '' for text mails + $prefix = ''; + + // Is HTML? + if ($isHtml == 'Y') { + // Set prefix + $prefix = 'html_'; + } // END - if + + // Load email header template + $mailHeader = loadEmailTemplate($prefix . 'header', $mailHeader); + } // END - if + + // Debug mode enabled? + if (isDebugModeEnabled()) { + // In debug mode we want to display the mail instead of sending it away so we can debug this part + outputHtml('
+Headers : ' . htmlentities(trim($mailHeader)) . '
+To      : ' . htmlentities($toEmail) . '
+Subject : ' . htmlentities($subject) . '
+Message(' . strlen($message) . ') : ' . htmlentities($message) . '
+
'); + + // This is always fine + return true; + } elseif (!empty($toEmail)) { + // Send Mail away + return sendRawEmail($toEmail, $subject, $message, $mailHeader); + } elseif ($isHtml != 'Y') { + // Problem detected while sending a mail, forward it to admin + return sendRawEmail(getWebmaster(), '[PROBLEM:]' . $subject, $message, $mailHeader); + } + + // Why did we end up here? This should not happen + reportBug(__FUNCTION__, __LINE__, 'Ending up: template=' . $template); +} + +/** + * Check to use whether plain mail() command or PHPMailer class + * @TODO Rewrite this to an extension 'smtp' + * @private + */ +function isPhpMailerConfigured () { + return ((getConfig('SMTP_HOSTNAME') != '') && (getConfig('SMTP_USER') != '')); +} + +// Send out a raw email with PHPMailer class or plain mail() command +function sendRawEmail ($toEmail, $subject, $message, $headers) { + // Just compile all to put out all configs, etc. + $eval = '$toEmail = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($toEmail), false)) . '"); '; + $eval .= '$subject = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($subject), false)) . '"); '; + $eval .= '$headers = decodeEntities("' . escapeQuotes(doFinalCompilation(compileRawCode($headers), false)) . '"); '; + + // Do not decode entities in the message because we also send HTML mails through this function + $eval .= '$message = "' . escapeQuotes(doFinalCompilation(compileRawCode($message), false)) . '";'; + + // Run the final eval() command + eval($eval); + + // Shall we use PHPMailer class or plain mail() command? + if (isPhpMailerConfigured()) { + // Use PHPMailer class with SMTP enabled + loadIncludeOnce('inc/phpmailer/class.phpmailer.php'); + loadIncludeOnce('inc/phpmailer/class.smtp.php'); + + // get new instance + $mail = new PHPMailer(); + + // Set charset to UTF-8 + $mail->CharSet = 'UTF-8'; + + // Path for PHPMailer + $mail->PluginDir = sprintf("%sinc/phpmailer/", getPath()); + + $mail->IsSMTP(); + $mail->SMTPAuth = true; + $mail->Host = getConfig('SMTP_HOSTNAME'); + $mail->Port = 25; + $mail->Username = getConfig('SMTP_USER'); + $mail->Password = getConfig('SMTP_PASSWORD'); + if (empty($headers)) { + $mail->From = getWebmaster(); + } else { + $mail->From = $headers; + } + $mail->FromName = getMainTitle(); + $mail->Subject = $subject; + if ((isExtensionActive('html_mail')) && (secureString($message) != $message)) { + $mail->Body = $message; + $mail->AltBody = decodeEntities($message); + $mail->WordWrap = 70; + $mail->IsHTML(true); + } else { + $mail->Body = decodeEntities(strip_tags($message)); + } + + $mail->AddAddress($toEmail, ''); + $mail->AddReplyTo(getWebmaster(), getMainTitle()); + $mail->AddCustomHeader('Errors-To: ' . getWebmaster()); + $mail->AddCustomHeader('X-Loop: ' . getWebmaster()); + $mail->AddCustomHeader('Bounces-To: ' . getWebmaster()); + $mail->Send(); + + // Has an error occured? + if (!empty($mail->ErrorInfo)) { + // Log message + logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo); + + // Raise an error + return false; + } else { + // All fine! + return true; + } + } else { + // Use plain mail() command + return mail($toEmail, $subject, decodeEntities($message), $headers); + } +} + +// Send notification to admin +function sendAdminNotification ($subject, $templateName, $content = array(), $userid = NULL) { + if ((isExtensionInstalledAndNewer('admins', '0.4.1')) && (function_exists('sendAdminsEmails'))) { + // Send new way + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=Y,subject=' . $subject . ',templateName=' . $templateName . ' - OKAY!'); + sendAdminsEmails($subject, $templateName, $content, $userid); + } else { + // Send out-dated way + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'admins=N,subject=' . $subject . ',templateName=' . $templateName . ' - OUT-DATED!'); + $message = loadEmailTemplate($templateName, $content, $userid); + sendAdminEmails($subject, $message, ($templateName == 'admin_report_bug')); + } +} + +// ---------------------------------------------------------------------------- +// Template helper functions +// ---------------------------------------------------------------------------- + +// Helper function to add extra headers to text mails +function doTemplateAddExtraTextMailHeaders ($templateName, $clear, $extraHeaders = '') { + // Run the header through the filter + $extraHeaders = runFilterChain('add_extra_text_mail_headers', $extraHeaders); + + // And return it + return $extraHeaders; +} + +// Helper function to add extra headers to HTML mails +function doTemplateAddExtraHtmlMailHeaders ($templateName, $clear, $extraHeaders = '') { + // Run the header through the filter + $extraHeaders = runFilterChain('add_extra_html_mail_headers', $extraHeaders); + + // And return it + return $extraHeaders; +} + +// [EOF] +?> -- 2.39.2