]> git.mjollnir.org Git - s9y.git/commitdiff
Commit changes for UTF-8 mails and plugin aPI
authorgarvinhicking <garvinhicking>
Sat, 21 Jan 2006 12:05:17 +0000 (12:05 +0000)
committergarvinhicking <garvinhicking>
Sat, 21 Jan 2006 12:05:17 +0000 (12:05 +0000)
docs/NEWS
include/functions.inc.php

index 9c3a8c40450f1c129da9be0d83f9e346d4399924..60679b82f051765231bb31c7adc2a9cc447047ff 100644 (file)
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,10 @@
 Version 1.0 ()
 ------------------------------------------------------------------------
 
+   * Fix some problems with sending UTF-8 emails on some systems. Allow
+     Plugin API to handle the whole mailflow optionally.
+     (garvinhicking)
+
    * Fixed Safari Browser issue with formatting and inserting media
      items (garvinhicking)
 
index 89feac3683a845ca68ecfb0b4947ad5f806488a1..fce50069ed9d9cd1d28b1730fc7e6050ced52c34 100644 (file)
@@ -76,7 +76,7 @@ function serendipity_serverOffsetHour($timestamp = null, $negative = false) {
  */
 function serendipity_strftime($format, $timestamp = null, $useOffset = true) {
     global $serendipity;
-    
+
     switch($serendipity['calendar']) {
         default:
         case 'gregorian':
@@ -86,7 +86,7 @@ function serendipity_strftime($format, $timestamp = null, $useOffset = true) {
                 $timestamp = serendipity_serverOffsetHour($timestamp);
             }
             return strftime($format, $timestamp);
-        
+
         case 'jalali-utf8':
             if ($timestamp == null) {
                 $timestamp = serendipity_serverOffsetHour();
@@ -188,10 +188,10 @@ function serendipity_fetchTemplateInfo($theme, $abspath = null) {
         $data[$k] = implode("\n", $v);
     }
 
-    if ( $theme != 'default' && $theme != 'default-rtl' 
-      && @is_dir($serendipity['templatePath'] . $theme . '/admin') 
+    if ( $theme != 'default' && $theme != 'default-rtl'
+      && @is_dir($serendipity['templatePath'] . $theme . '/admin')
       && @is_readable($serendipity['templatePath'] . $theme . '/admin/style.css') ) {
-      
+
         $data['custom_admin_interface'] = YES;
     } else {
         $data['custom_admin_interface'] = NO;
@@ -273,7 +273,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
         $query_join   = "LEFT OUTER JOIN {$serendipity['dbPrefix']}entries AS e
                                       ON a.authorid = e.authorid";
     }
-    
+
     if ($is_count || $group != null) {
         if ($serendipity['dbType'] == 'postgres') {
             // Why does PostgreSQL keep doing this to us? :-)
@@ -288,7 +288,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
 
     if ($group === null) {
         $querystring = "SELECT $query_distinct
-                               a.authorid, 
+                               a.authorid,
                                a.realname,
                                a.username,
                                a.password,
@@ -300,7 +300,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
                                $query_select
                           FROM {$serendipity['dbPrefix']}authors AS a
                                $query_join
-                               $where 
+                               $where
                                $query_group
                       ORDER BY a.realname ASC";
     } else {
@@ -314,7 +314,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
         }
 
         $querystring = "SELECT $query_distinct
-                               a.authorid, 
+                               a.authorid,
                                a.realname,
                                a.username,
                                a.password,
@@ -324,7 +324,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
                                a.userlevel,
                                a.right_publish
                                $query_select
-                          FROM {$serendipity['dbPrefix']}authors AS a 
+                          FROM {$serendipity['dbPrefix']}authors AS a
                LEFT OUTER JOIN {$serendipity['dbPrefix']}authorgroups AS ag
                             ON a.authorid = ag.authorid
                LEFT OUTER JOIN {$serendipity['dbPrefix']}groups AS g
@@ -335,7 +335,7 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
                                $query_group
                       ORDER BY a.realname ASC";
     }
-    
+
     return serendipity_db_query($querystring);
 }
 
@@ -355,52 +355,83 @@ function serendipity_fetchUsers($user = '', $group = null, $is_count = false) {
 function serendipity_sendMail($to, $subject, $message, $fromMail, $headers = NULL, $fromName = NULL) {
     global $serendipity;
 
-    if ( !is_null($headers) && !is_array($headers) ) {
+    if (!is_null($headers) && !is_array($headers)) {
         trigger_error(__FUNCTION__ . ': $headers must be either an array or null', E_USER_ERROR);
     }
 
-    if ( is_null($fromName) || empty($fromName)) {
+    if (is_null($fromName) || empty($fromName)) {
         $fromName = $serendipity['blogTitle'];
     }
 
-    if ( is_null($fromMail) || empty($fromMail)) {
+    if (is_null($fromMail) || empty($fromMail)) {
         $fromMail = $to;
     }
+    
+    if (is_null($headers)) {
+        $headers = array();
+    }
 
     // Fix special characters
     $fromName = str_replace(array('"', "\r", "\n"), array("'", '', ''), $fromName);
     $fromMail = str_replace(array("\r","\n"), array('', ''), $fromMail);
 
-    /* Prefix all mail with weblog title */
+    // Prefix all mail with weblog title
     $subject = '['. $serendipity['blogTitle'] . '] '.  $subject;
 
-    /* Append signature to every mail */
+    // Append signature to every mail
     $message .= "\n" . sprintf(SIGNATURE, $serendipity['blogTitle']);
 
+    $maildata = array(
+        'to'       => &$to,
+        'subject'  => &$subject,
+        'fromName' => &$fromName,
+        'fromMail' => &$fromMail,
+        'blogMail' => $serendipity['blogMail'],
+        'version'  => 'Serendipity/' . $serendipity['version'],
+        'legacy'   => true,
+        'headers'  => &$headers,
+        'message'  => &$message
+    );
 
-    /* Check for mb_* function, and use it to encode headers etc. */
-    if ( function_exists('mb_encode_mimeheader') ) {
-        // Funky mb_encode_mimeheader function insertes linebreaks after 74 chars.
-        // Most MTA I've personally spoken with told me they don't like this at all. ;)
-        // Regards to Mark Kronsbein for finding this issue!
-        $subject = str_replace(array("\n", "\r"), array('', ''), mb_encode_mimeheader($subject, LANG_CHARSET));
-        $fromName = str_replace(array("\n", "\r"), array('', ''), mb_encode_mimeheader($fromName, LANG_CHARSET));
-    }
-
-
-    /* Always add these headers */
-    if (!empty($serendipity['blogMail'])) {
-        $headers[] = 'From: "'. $fromName .'" <'. $serendipity['blogMail'] .'>';
+    serendipity_plugin_api('backend_sendmail', $maildata, LANG_CHARSET);
+
+    // This routine can be overridden by a plugin.
+    if ($maildata['legacy']) {
+        // Check for mb_* function, and use it to encode headers etc. */
+        if (function_exists('mb_encode_mimeheader')) {
+            // mb_encode_mimeheader function insertes linebreaks after 74 chars.
+            // Usually this is according to spec, but for us it caused more trouble than
+            // it prevented.
+            // Regards to Mark Kronsbein for finding this issue!
+            $maildata['subject'] = str_replace(array("\n", "\r"), array('', ''), mb_encode_mimeheader($maildata['subject'], LANG_CHARSET));
+            $maildata['fromName'] = str_replace(array("\n", "\r"), array('', ''), mb_encode_mimeheader($maildata['fromName'], LANG_CHARSET));
+        }
+    
+    
+        // Always add these headers
+        if (!empty($maildata['blogMail'])) {
+            $maildata['headers'][] = 'From: "'. $maildata['fromName'] .'" <'. $maildata['blogMail'] .'>';
+        }
+        $maildata['headers'][] = 'Reply-To: "'. $maildata['fromName'] .'" <'. $maildata['fromMail'] .'>';
+        $maildata['headers'][] = 'X-Mailer: ' . $maildata['version'];
+        $maildata['headers'][] = 'X-Engine: PHP/'. phpversion();
+        $maildata['headers'][] = 'Message-ID: <'. md5(microtime() . uniqid(time())) .'@'. $_SERVER['HTTP_HOST'] .'>';
+        $maildata['headers'][] = 'MIME-Version: 1.0';
+        $maildata['headers'][] = 'Precedence: bulk';
+        $maildata['headers'][] = 'Content-Type: text/plain; charset=' . LANG_CHARSET;
+        
+        if (LANG_CHARSET == 'UTF-8') {
+            if (function_exists('imap_8bit')) {
+                $maildata['headers'][] = 'Content-Transfer-Encoding: quoted-printable';
+                $maildata['message']   = imap_8bit($maildata['message']);
+            } else {
+                $maildata['headers'][] = 'Content-Transfer-Encoding: base64';
+                $maildata['message']   = base64_encode($maildata['message']);
+            }
+        }
     }
-    $headers[] = 'Reply-To: "'. $fromName .'" <'. $fromMail .'>';
-    $headers[] = 'X-Mailer: Serendipity/'. $serendipity['version'];
-    $headers[] = 'X-Engine: PHP/'. phpversion();
-    $headers[] = 'Message-ID: <'. md5(microtime() . uniqid(time())) .'@'. $_SERVER['HTTP_HOST'] .'>';
-    $headers[] = 'MIME-Version: 1.0';
-    $headers[] = 'Precedence: bulk';
-    $headers[] = 'Content-Type: text/plain; charset=' . LANG_CHARSET;
 
-    return mail($to, $subject, $message, implode("\n", $headers));
+    return mail($maildata['to'], $maildata['subject'], $maildata['message'], implode("\n", $maildata['headers']));
 }
 
 /**
@@ -462,10 +493,10 @@ function serendipity_rss_getguid($entry, $comments = false) {
     }
 
     $guid = serendipity_archiveURL(
-        $id, 
-        $title, 
-        'baseURL', 
-        true, 
+        $id,
+        $title,
+        'baseURL',
+        true,
         array('timestamp' => $entry['timestamp'])
     );
 
@@ -708,7 +739,7 @@ function serendipity_track_url($list, $url, $entry_id = 0) {
  * @see serendipity_displayTopUrlList()
  * @param  int      Number of referrers to show
  * @param  boolean  Whether to use HTML links for URLs
- * @param  int      Interval for which the top referrers are aggregated 
+ * @param  int      Interval for which the top referrers are aggregated
  * @return string   List of Top referrers
  */
 function serendipity_displayTopReferrers($limit = 10, $use_links = true, $interval = 7) {
@@ -722,7 +753,7 @@ function serendipity_displayTopReferrers($limit = 10, $use_links = true, $interv
  * @see serendipity_displayTopUrlList()
  * @param  int      Number of exits to show
  * @param  boolean  Whether to use HTML links for URLs
- * @param  int      Interval for which the top exits are aggregated 
+ * @param  int      Interval for which the top exits are aggregated
  * @return string   List of Top exits
  */
 function serendipity_displayTopExits($limit = 10, $use_links = true, $interval = 7) {
@@ -737,7 +768,7 @@ function serendipity_displayTopExits($limit = 10, $use_links = true, $interval =
  * @see serendipity_displayTopReferrers()
  * @param   string      Name of the DB table to show data from (exits|referrers)
  * @param  boolean  Whether to use HTML links for URLs
- * @param  int      Interval for which the top exits are aggregated 
+ * @param  int      Interval for which the top exits are aggregated
  * @return
  */
 function serendipity_displayTopUrlList($list, $limit, $use_links = true, $interval = 7) {
@@ -873,13 +904,13 @@ function serendipity_addCategory($name, $desc, $authorid, $icon, $parentid) {
     global $serendipity;
     $query = "INSERT INTO {$serendipity['dbPrefix']}category
                     (category_name, category_description, authorid, category_icon, parentid, category_left, category_right)
-                  VALUES 
-                    ('". serendipity_db_escape_string($name) ."', 
-                     '". serendipity_db_escape_string($desc) ."', 
-                      ". (int)$authorid .", 
-                     '". serendipity_db_escape_string($icon) ."', 
-                      ". (int)$parentid .", 
-                       0, 
+                  VALUES
+                    ('". serendipity_db_escape_string($name) ."',
+                     '". serendipity_db_escape_string($desc) ."',
+                      ". (int)$authorid .",
+                     '". serendipity_db_escape_string($icon) ."',
+                      ". (int)$parentid .",
+                       0,
                        0)";
 
     serendipity_db_query($query);