Skip to content
Newer
Older
100644 226 lines (186 sloc) 7.31 KB
b43564c *** empty log message ***
saxmatt authored
1 <?php
a6a1522 File file level phpdoc from jacobsantos. see #7037
ryan authored
2 /**
3 * Gets the email message from the user's mailbox to add as
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
4 * a WordPress post. Mailbox connection information must be
5 * configured under Settings > Writing
a6a1522 File file level phpdoc from jacobsantos. see #7037
ryan authored
6 *
7 * @package WordPress
8 */
9
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
10 /** Make sure that the WordPress bootstrap has run before continuing. */
5b81135 Allow wp-config.php to exist one level up from WordPress root directo…
ryan authored
11 require(dirname(__FILE__) . '/wp-load.php');
b43564c *** empty log message ***
saxmatt authored
12
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
13 /** Get the POP3 class with which to access the mailbox. */
14 require_once( ABSPATH . WPINC . '/class-pop3.php' );
b43564c *** empty log message ***
saxmatt authored
15
1199fd7 Limit wp-mail blog by email checks to every 5 minutes. Fixes #7245 ba…
westi authored
16 /** Only check at this interval for new messages. */
17 if ( !defined('WP_MAIL_INTERVAL') )
18 define('WP_MAIL_INTERVAL', 500); // 5 minutes
19
20 $last_checked = get_transient('mailserver_last_checked');
21
22 if ( $last_checked )
23 wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
24
25 set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
26
47aa41a Don't absint gmt_offset. Props Hirvine. fixes #9162
ryan authored
27 $time_difference = get_option('gmt_offset') * 3600;
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
28
c913685 Massive options cleanup and another step of cleaning up the upgrade/i…
saxmatt authored
29 $phone_delim = '::';
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
30
b43564c *** empty log message ***
saxmatt authored
31 $pop3 = new POP3();
1199fd7 Limit wp-mail blog by email checks to every 5 minutes. Fixes #7245 ba…
westi authored
32 $count = 0;
b43564c *** empty log message ***
saxmatt authored
33
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
34 if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) ||
35 ! $pop3->user(get_option('mailserver_login')) ||
36 ( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) {
37 $pop3->quit();
119b39c deprecate wp_specialchars() in favor of esc_html(). Encode quotes for…
markjaquith authored
38 wp_die( ( 0 === $count ) ? __('There doesn&#8217;t seem to be any new mail.') : esc_html($pop3->ERROR) );
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
39 }
b43564c *** empty log message ***
saxmatt authored
40
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
41 for ( $i = 1; $i <= $count; $i++ ) {
b43564c *** empty log message ***
saxmatt authored
42
a0ad8e4 New mail functionality.
saxmatt authored
43 $message = $pop3->get($i);
b43564c *** empty log message ***
saxmatt authored
44
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
45 $bodysignal = false;
46 $boundary = '';
47 $charset = '';
b43564c *** empty log message ***
saxmatt authored
48 $content = '';
49 $content_type = '';
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Enco…
rob1n authored
50 $content_transfer_encoding = '';
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
51 $post_author = 1;
52 $author_found = false;
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Enco…
rob1n authored
53 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
54 foreach ($message as $line) {
55 // body signal
56 if ( strlen($line) < 3 )
57 $bodysignal = true;
58 if ( $bodysignal ) {
b43564c *** empty log message ***
saxmatt authored
59 $content .= $line;
60 } else {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
61 if ( preg_match('/Content-Type: /i', $line) ) {
b43564c *** empty log message ***
saxmatt authored
62 $content_type = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
63 $content_type = substr($content_type, 14, strlen($content_type) - 14);
b43564c *** empty log message ***
saxmatt authored
64 $content_type = explode(';', $content_type);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
65 if ( ! empty( $content_type[1] ) ) {
66 $charset = explode('=', $content_type[1]);
67 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
68 }
b43564c *** empty log message ***
saxmatt authored
69 $content_type = $content_type[0];
70 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
71 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Enco…
rob1n authored
72 $content_transfer_encoding = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
73 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Enco…
rob1n authored
74 $content_transfer_encoding = explode(';', $content_transfer_encoding);
75 $content_transfer_encoding = $content_transfer_encoding[0];
76 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
77 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
b43564c *** empty log message ***
saxmatt authored
78 $boundary = trim($line);
79 $boundary = explode('"', $boundary);
80 $boundary = $boundary[1];
81 }
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
82 if (preg_match('/Subject: /i', $line)) {
b43564c *** empty log message ***
saxmatt authored
83 $subject = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
84 $subject = substr($subject, 9, strlen($subject) - 9);
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
85 // Captures any text in the subject before $phone_delim as the subject
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
86 if ( function_exists('iconv_mime_decode') ) {
87 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
88 } else {
89 $subject = wp_iso_descrambler($subject);
90 }
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
91 $subject = explode($phone_delim, $subject);
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
92 $subject = $subject[0];
b43564c *** empty log message ***
saxmatt authored
93 }
cb96ccc Have WP mailed post come from author if email matches author in DB. H…
saxmatt authored
94
a92c487 Fix email address parsing in wp-mail.php. Props Bobcat. fixes #5169
ryan authored
95 // Set the author using the email address (From or Reply-To, the last used)
cb96ccc Have WP mailed post come from author if email matches author in DB. H…
saxmatt authored
96 // otherwise use the site admin
88735ac Strip From/Reply-To from e-mail before sanitizing in wp-mail.php. Pro…
markjaquith authored
97 if ( preg_match('/(From|Reply-To): /', $line) ) {
a92c487 Fix email address parsing in wp-mail.php. Props Bobcat. fixes #5169
ryan authored
98 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
99 $author = $matches[0];
100 else
101 $author = trim($line);
102 $author = sanitize_email($author);
1b7798d Properly sanitize e-mail addresses in wp-mail.php. Props giannis1. fi…
markjaquith authored
103 if ( is_email($author) ) {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
104 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
105 $userdata = get_user_by_email($author);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
106 if ( empty($userdata) ) {
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
107 $author_found = false;
108 } else {
109 $post_author = $userdata->ID;
110 $author_found = true;
111 }
112 } else {
113 $author_found = false;
114 }
cb96ccc Have WP mailed post come from author if email matches author in DB. H…
saxmatt authored
115 }
116
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
117 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
b43564c *** empty log message ***
saxmatt authored
118 $ddate = trim($line);
119 $ddate = str_replace('Date: ', '', $ddate);
120 if (strpos($ddate, ',')) {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
121 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
b43564c *** empty log message ***
saxmatt authored
122 }
123 $date_arr = explode(' ', $ddate);
124 $date_time = explode(':', $date_arr[3]);
dd202ce Death to trailing tabs. Props Mark J. fixes #2405
ryan authored
125
b43564c *** empty log message ***
saxmatt authored
126 $ddate_H = $date_time[0];
127 $ddate_i = $date_time[1];
128 $ddate_s = $date_time[2];
dd202ce Death to trailing tabs. Props Mark J. fixes #2405
ryan authored
129
b43564c *** empty log message ***
saxmatt authored
130 $ddate_m = $date_arr[1];
131 $ddate_d = $date_arr[0];
132 $ddate_Y = $date_arr[2];
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
133 for ( $j = 0; $j < 12; $j++ ) {
134 if ( $ddate_m == $dmonths[$j] ) {
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
135 $ddate_m = $j+1;
b43564c *** empty log message ***
saxmatt authored
136 }
137 }
84435bf big leap to GMT dates
michelvaldrighi authored
138
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
139 $time_zn = intval($date_arr[4]) * 36;
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
140 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
141 $ddate_U = $ddate_U - $time_zn;
142 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
b8df8fb now storing emailed posts as localtime+gmt
michelvaldrighi authored
143 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
b43564c *** empty log message ***
saxmatt authored
144 }
145 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
146 }
b43564c *** empty log message ***
saxmatt authored
147
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
148 // Set $post_status based on $author_found and on author's publish_posts capability
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
149 if ( $author_found ) {
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
150 $user = new WP_User($post_author);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
151 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
a7c0525 When posting by email, publish only if user has publish caps, otherwi…
ryan authored
152 } else {
153 // Author not found in DB, set status to pending. Author already set to admin.
154 $post_status = 'pending';
155 }
156
e8b6fe7 Trim trailing whitespace
ryan authored
157 $subject = trim($subject);
158
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
159 if ( $content_type == 'multipart/alternative' ) {
a0ad8e4 New mail functionality.
saxmatt authored
160 $content = explode('--'.$boundary, $content);
161 $content = $content[2];
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
162 // match case-insensitive content-transfer-encoding
163 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
164 $content = explode($delim[0], $content);
165 $content = $content[1];
166 }
167 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
b43564c *** empty log message ***
saxmatt authored
168 }
a0ad8e4 New mail functionality.
saxmatt authored
169 $content = trim($content);
2f09416 Trim empty lines. Nothing but newline.
ryan authored
170
48f9d53 Add a filter to allow post-by-email plugins better control over the c…
westi authored
171 //Give Post-By-Email extending plugins full access to the content
172 //Either the raw content or the content of the last quoted-printable section
173 $content = apply_filters('wp_mail_original_content', $content);
e8b6fe7 Trim trailing whitespace
ryan authored
174
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
175 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Enco…
rob1n authored
176 $content = quoted_printable_decode($content);
177 }
178
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
179 if ( function_exists('iconv') && ! empty( $charset ) ) {
180 $content = iconv($charset, get_option('blog_charset'), $content);
181 }
e89192b Strip trailing whitespace
ryan authored
182
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
183 // Captures any text in the body after $phone_delim as the body
184 $content = explode($phone_delim, $content);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
185 $content = empty( $content[1] ) ? $content[0] : $content[1];
b43564c *** empty log message ***
saxmatt authored
186
a0ad8e4 New mail functionality.
saxmatt authored
187 $content = trim($content);
b43564c *** empty log message ***
saxmatt authored
188
ed34ca0 Set GUID when posting.
saxmatt authored
189 $post_content = apply_filters('phone_content', $content);
b43564c *** empty log message ***
saxmatt authored
190
a0ad8e4 New mail functionality.
saxmatt authored
191 $post_title = xmlrpc_getposttitle($content);
b43564c *** empty log message ***
saxmatt authored
192
a0ad8e4 New mail functionality.
saxmatt authored
193 if ($post_title == '') $post_title = $subject;
b43564c *** empty log message ***
saxmatt authored
194
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
195 $post_category = array(get_option('default_email_category'));
b43564c *** empty log message ***
saxmatt authored
196
e75e000 set emailed posts to be published. won't do no good moblogging if in …
michelvaldrighi authored
197 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
df89b36 Give wp-mail a little love.
ryan authored
198 $post_data = add_magic_quotes($post_data);
b43564c *** empty log message ***
saxmatt authored
199
3d21925 Ryan slays the dragon. (Some of these return WP_Error objects.)
matt authored
200 $post_ID = wp_insert_post($post_data);
b7f907b Escape pop3 error messages. Props xknown. fixes #5484
ryan authored
201 if ( is_wp_error( $post_ID ) )
9bfccd6 Add checks for WP_Error. Props filosofo. see #4809
ryan authored
202 echo "\n" . $post_ID->get_error_message();
b43564c *** empty log message ***
saxmatt authored
203
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
204 // We couldn't post, for whatever reason. Better move forward to the next email.
205 if ( empty( $post_ID ) )
65950b5 moving the pop3->delete call to the end of the loop so that emails ar…
michelvaldrighi authored
206 continue;
207
2f4218a Added filter and action hooks.
saxmatt authored
208 do_action('publish_phone', $post_ID);
5624ca0 Fix ups.
saxmatt authored
209
119b39c deprecate wp_specialchars() in favor of esc_html(). Encode quotes for…
markjaquith authored
210 echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), esc_html($post_author)) . '</p>';
211 echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), esc_html($post_title)) . '</p>';
5624ca0 Fix ups.
saxmatt authored
212
65950b5 moving the pop3->delete call to the end of the loop so that emails ar…
michelvaldrighi authored
213 if(!$pop3->delete($i)) {
119b39c deprecate wp_specialchars() in favor of esc_html(). Encode quotes for…
markjaquith authored
214 echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>';
65950b5 moving the pop3->delete call to the end of the loop so that emails ar…
michelvaldrighi authored
215 $pop3->reset();
216 exit;
217 } else {
0f33f00 Localization for wp-mail. Props filosofo. fixes #8051
ryan authored
218 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';
b43564c *** empty log message ***
saxmatt authored
219 }
220
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
221 }
b43564c *** empty log message ***
saxmatt authored
222
a0ad8e4 New mail functionality.
saxmatt authored
223 $pop3->quit();
b43564c *** empty log message ***
saxmatt authored
224
0f33f00 Localization for wp-mail. Props filosofo. fixes #8051
ryan authored
225 ?>
Something went wrong with that request. Please try again.