Ticket #6788: wp-mail.iconv.diff
File wp-mail.iconv.diff, 8.5 KB (added by , 17 years ago) |
---|
-
wp-mail.php
1 1 <?php 2 2 /** 3 3 * Gets the email message from the user's mailbox to add as 4 * a WordPress post. Will only run if this is setup and enabled. 4 * a WordPress post. Mailbox connection information must be 5 * configured under Settings > Writing 5 6 * 6 7 * @package WordPress 7 8 */ 8 9 9 /** Make sure that the WordPress bootstrap has r an before continuing. */10 /** Make sure that the WordPress bootstrap has run before continuing. */ 10 11 require(dirname(__FILE__) . '/wp-load.php'); 11 12 12 /** Get the POP3 class forwhich to access the mailbox. */13 require_once( ABSPATH.WPINC.'/class-pop3.php');13 /** Get the POP3 class with which to access the mailbox. */ 14 require_once( ABSPATH . WPINC . '/class-pop3.php' ); 14 15 15 $time_difference = get_option('gmt_offset') * 3600;16 $time_difference = intval(get_option('gmt_offset')) * 3600; 16 17 17 18 $phone_delim = '::'; 18 19 19 20 $pop3 = new POP3(); 20 21 21 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port'))) 22 wp_die(wp_specialchars($pop3->ERROR)); 22 if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) || 23 ! $pop3->user(get_option('mailserver_login')) || 24 ( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) { 25 $pop3->quit(); 26 wp_die( ( 0 === $count ) ? __("There doesn't seem to be any new mail.") : wp_specialchars($pop3->ERROR) ); 27 } 23 28 24 if (!$pop3->user(get_option('mailserver_login'))) 25 wp_die(wp_specialchars($pop3->ERROR)); 29 for ( $i = 1; $i <= $count; $i++ ) { 26 30 27 $count = $pop3->pass(get_option('mailserver_pass'));28 if (false === $count)29 wp_die(wp_specialchars($pop3->ERROR));30 if (0 == $count)31 echo '<p>' . __("There doesn't seem to be any new mail.") . "</p>\n"; // will fall-through to end of for loop32 33 for ($i=1; $i <= $count; $i++) :34 35 31 $message = $pop3->get($i); 36 32 33 $bodysignal = false; 34 $boundary = ''; 35 $charset = ''; 37 36 $content = ''; 38 37 $content_type = ''; 39 38 $content_transfer_encoding = ''; 40 $boundary = '';41 $bodysignal = 0;42 39 $post_author = 1; 43 40 $author_found = false; 44 41 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); 45 foreach ($message as $line) : 46 if (strlen($line) < 3) $bodysignal = 1; 47 48 if ($bodysignal) { 42 foreach ($message as $line) { 43 // body signal 44 if ( strlen($line) < 3 ) 45 $bodysignal = true; 46 if ( $bodysignal ) { 49 47 $content .= $line; 50 48 } else { 51 if ( preg_match('/Content-Type: /i', $line)) {49 if ( preg_match('/Content-Type: /i', $line) ) { 52 50 $content_type = trim($line); 53 $content_type = substr($content_type, 14, strlen($content_type) -14);51 $content_type = substr($content_type, 14, strlen($content_type) - 14); 54 52 $content_type = explode(';', $content_type); 53 if ( ! empty( $content_type[1] ) ) { 54 $charset = explode('=', $content_type[1]); 55 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : ''; 56 } 55 57 $content_type = $content_type[0]; 56 58 } 57 if ( preg_match('/Content-Transfer-Encoding: /i', $line)) {59 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) { 58 60 $content_transfer_encoding = trim($line); 59 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) -14);61 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27); 60 62 $content_transfer_encoding = explode(';', $content_transfer_encoding); 61 63 $content_transfer_encoding = $content_transfer_encoding[0]; 62 64 } 63 if ( ($content_type == 'multipart/alternative') && (false !== strpos($line, 'boundary="')) && ($boundary == '')) {65 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) { 64 66 $boundary = trim($line); 65 67 $boundary = explode('"', $boundary); 66 68 $boundary = $boundary[1]; 67 69 } 68 70 if (preg_match('/Subject: /i', $line)) { 69 71 $subject = trim($line); 70 $subject = substr($subject, 9, strlen($subject)-9); 71 $subject = wp_iso_descrambler($subject); 72 $subject = substr($subject, 9, strlen($subject) - 9); 72 73 // Captures any text in the subject before $phone_delim as the subject 74 if ( function_exists('iconv_mime_decode') ) { 75 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset')); 76 } else { 77 $subject = wp_iso_descrambler($subject); 78 } 73 79 $subject = explode($phone_delim, $subject); 74 80 $subject = $subject[0]; 75 81 } … … 83 89 $author = trim($line); 84 90 $author = sanitize_email($author); 85 91 if ( is_email($author) ) { 86 echo "Author = {$author} <p>";92 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>'; 87 93 $userdata = get_user_by_email($author); 88 if (!$userdata) { 89 $post_author = 1; 94 if ( empty($userdata) ) { 90 95 $author_found = false; 91 96 } else { 92 97 $post_author = $userdata->ID; 93 98 $author_found = true; 94 99 } 95 100 } else { 96 $post_author = 1;97 101 $author_found = false; 98 102 } 99 103 } … … 102 106 $ddate = trim($line); 103 107 $ddate = str_replace('Date: ', '', $ddate); 104 108 if (strpos($ddate, ',')) { 105 $ddate = trim(substr($ddate, strpos($ddate, ',') +1, strlen($ddate)));109 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate))); 106 110 } 107 111 $date_arr = explode(' ', $ddate); 108 112 $date_time = explode(':', $date_arr[3]); … … 114 118 $ddate_m = $date_arr[1]; 115 119 $ddate_d = $date_arr[0]; 116 120 $ddate_Y = $date_arr[2]; 117 for ( $j=0; $j<12; $j++) {118 if ( $ddate_m == $dmonths[$j]) {121 for ( $j = 0; $j < 12; $j++ ) { 122 if ( $ddate_m == $dmonths[$j] ) { 119 123 $ddate_m = $j+1; 120 124 } 121 125 } … … 127 131 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U); 128 132 } 129 133 } 130 endforeach;134 } 131 135 132 136 // Set $post_status based on $author_found and on author's publish_posts capability 133 if ( $author_found) {137 if ( $author_found ) { 134 138 $user = new WP_User($post_author); 135 if ($user->has_cap('publish_posts')) 136 $post_status = 'publish'; 137 else 138 $post_status = 'pending'; 139 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending'; 139 140 } else { 140 141 // Author not found in DB, set status to pending. Author already set to admin. 141 142 $post_status = 'pending'; … … 143 144 144 145 $subject = trim($subject); 145 146 146 if ( $content_type == 'multipart/alternative') {147 if ( $content_type == 'multipart/alternative' ) { 147 148 $content = explode('--'.$boundary, $content); 148 149 $content = $content[2]; 149 $content = explode('Content-Transfer-Encoding: quoted-printable', $content); 150 $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>'); 150 // match case-insensitive content-transfer-encoding 151 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) { 152 $content = explode($delim[0], $content); 153 $content = $content[1]; 154 } 155 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>'); 151 156 } 152 157 $content = trim($content); 153 158 154 if ( stripos($content_transfer_encoding, "quoted-printable") !== false) {159 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) { 155 160 $content = quoted_printable_decode($content); 156 161 } 157 162 163 if ( function_exists('iconv') && ! empty( $charset ) ) { 164 $content = iconv($charset, get_option('blog_charset'), $content); 165 } 166 158 167 // Captures any text in the body after $phone_delim as the body 159 168 $content = explode($phone_delim, $content); 160 $content [1] ? $content = $content[1] : $content = $content[0];169 $content = empty( $content[1] ) ? $content[0] : $content[1]; 161 170 162 171 $content = trim($content); 163 172 … … 167 176 168 177 if ($post_title == '') $post_title = $subject; 169 178 170 if (empty($post_categories)) $post_categories[] = get_option('default_email_category');179 $post_category = array(get_option('default_email_category')); 171 180 172 $post_category = $post_categories;173 174 181 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status'); 175 182 $post_data = add_magic_quotes($post_data); 176 183 … … 178 185 if ( is_wp_error( $post_ID ) ) 179 186 echo "\n" . $post_ID->get_error_message(); 180 187 181 if (!$post_ID) {182 // we couldn't post, for whatever reason. better move forward to the next email188 // We couldn't post, for whatever reason. Better move forward to the next email. 189 if ( empty( $post_ID ) ) 183 190 continue; 184 }185 191 186 192 do_action('publish_phone', $post_ID); 187 193 … … 196 202 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>'; 197 203 } 198 204 199 endfor; 205 } 200 206 201 207 $pop3->quit(); 202 208