Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (187 sloc) 7.723 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 directory....
ryan authored
11 require(dirname(__FILE__) . '/wp-load.php');
b43564c *** empty log message ***
saxmatt authored
12
3e4ce6e Switch from POST_BY_EMAIL constant to enable post-by-email when multisit...
westi authored
13 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
442d412 Tweak new strings for better typography. Props demetris. Fixes #12962
dd32 authored
14 wp_die( __( 'This action has been disabled by the administrator.' ) );
9f1aa91 Integrate maybe_cancel_post_by_email() into wp-mail.php. see #11644
ryan authored
15
46ffb5b Allow a plugin to do a complete takeover of Post by Email
westi authored
16 /** Allow a plugin to do a complete takeover of Post by Email **/
ad83f73 Make the action name for post-by-email takeover actually unique so the p...
westi authored
17 do_action('wp-mail.php');
46ffb5b Allow a plugin to do a complete takeover of Post by Email
westi authored
18
8772775 Post by Email plugins might want to use there own POP3 class so move the...
westi authored
19 /** Get the POP3 class with which to access the mailbox. */
20 require_once( ABSPATH . WPINC . '/class-pop3.php' );
21
1199fd7 Limit wp-mail blog by email checks to every 5 minutes. Fixes #7245 based...
westi authored
22 /** Only check at this interval for new messages. */
23 if ( !defined('WP_MAIL_INTERVAL') )
45ec67d Set the interval to 5 minutes like I meant. See #7245 props aaroncampbel...
westi authored
24 define('WP_MAIL_INTERVAL', 300); // 5 minutes
1199fd7 Limit wp-mail blog by email checks to every 5 minutes. Fixes #7245 based...
westi authored
25
26 $last_checked = get_transient('mailserver_last_checked');
27
28 if ( $last_checked )
29 wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
30
31 set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
32
557d931 Andrew Nacin Introduce constants to allow for easier expression of time periods in se...
nacin authored
33 $time_difference = get_option('gmt_offset') * HOUR_IN_SECONDS;
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
34
c913685 Massive options cleanup and another step of cleaning up the upgrade/inst...
saxmatt authored
35 $phone_delim = '::';
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
36
b43564c *** empty log message ***
saxmatt authored
37 $pop3 = new POP3();
38
1a552dd Strip trailing whitespace
ryan authored
39 if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
1d8718e Adjust POP3 error checks in wp-mail.php. props solarissmoke, fixes #1316...
nacin authored
40 wp_die( esc_html( $pop3->ERROR ) );
41
42 $count = $pop3->pass( get_option('mailserver_pass') );
43
1a552dd Strip trailing whitespace
ryan authored
44 if( false === $count )
1d8718e Adjust POP3 error checks in wp-mail.php. props solarissmoke, fixes #1316...
nacin authored
45 wp_die( esc_html( $pop3->ERROR ) );
1a552dd Strip trailing whitespace
ryan authored
46
1d8718e Adjust POP3 error checks in wp-mail.php. props solarissmoke, fixes #1316...
nacin authored
47 if( 0 === $count ) {
48 $pop3->quit();
49 wp_die( __('There doesn&#8217;t seem to be any new mail.') );
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
50 }
b43564c *** empty log message ***
saxmatt authored
51
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
52 for ( $i = 1; $i <= $count; $i++ ) {
b43564c *** empty log message ***
saxmatt authored
53
a0ad8e4 New mail functionality.
saxmatt authored
54 $message = $pop3->get($i);
b43564c *** empty log message ***
saxmatt authored
55
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
56 $bodysignal = false;
57 $boundary = '';
58 $charset = '';
b43564c *** empty log message ***
saxmatt authored
59 $content = '';
60 $content_type = '';
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Encodin...
rob1n authored
61 $content_transfer_encoding = '';
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
62 $post_author = 1;
63 $author_found = false;
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Encodin...
rob1n authored
64 $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
65 foreach ($message as $line) {
66 // body signal
67 if ( strlen($line) < 3 )
68 $bodysignal = true;
69 if ( $bodysignal ) {
b43564c *** empty log message ***
saxmatt authored
70 $content .= $line;
71 } else {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
72 if ( preg_match('/Content-Type: /i', $line) ) {
b43564c *** empty log message ***
saxmatt authored
73 $content_type = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
74 $content_type = substr($content_type, 14, strlen($content_type) - 14);
b43564c *** empty log message ***
saxmatt authored
75 $content_type = explode(';', $content_type);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
76 if ( ! empty( $content_type[1] ) ) {
77 $charset = explode('=', $content_type[1]);
78 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
79 }
b43564c *** empty log message ***
saxmatt authored
80 $content_type = $content_type[0];
81 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
82 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Encodin...
rob1n authored
83 $content_transfer_encoding = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
84 $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-Encodin...
rob1n authored
85 $content_transfer_encoding = explode(';', $content_transfer_encoding);
86 $content_transfer_encoding = $content_transfer_encoding[0];
87 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
88 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
b43564c *** empty log message ***
saxmatt authored
89 $boundary = trim($line);
90 $boundary = explode('"', $boundary);
91 $boundary = $boundary[1];
92 }
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
93 if (preg_match('/Subject: /i', $line)) {
b43564c *** empty log message ***
saxmatt authored
94 $subject = trim($line);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
95 $subject = substr($subject, 9, strlen($subject) - 9);
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
96 // Captures any text in the subject before $phone_delim as the subject
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
97 if ( function_exists('iconv_mime_decode') ) {
98 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
99 } else {
100 $subject = wp_iso_descrambler($subject);
101 }
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
102 $subject = explode($phone_delim, $subject);
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
103 $subject = $subject[0];
b43564c *** empty log message ***
saxmatt authored
104 }
cb96ccc Have WP mailed post come from author if email matches author in DB. Hat ...
saxmatt authored
105
a92c487 Fix email address parsing in wp-mail.php. Props Bobcat. fixes #5169
ryan authored
106 // 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. Hat ...
saxmatt authored
107 // otherwise use the site admin
95ab5d3 Anchor a regular expression in wp-mail.php to ensure we are matching the...
nacin authored
108 if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
a92c487 Fix email address parsing in wp-mail.php. Props Bobcat. fixes #5169
ryan authored
109 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
110 $author = $matches[0];
111 else
112 $author = trim($line);
113 $author = sanitize_email($author);
1b7798d Properly sanitize e-mail addresses in wp-mail.php. Props giannis1. fix...
markjaquith authored
114 if ( is_email($author) ) {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
115 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
ec4ccf2 Deprecate get_userdatabylogin() and get_user_by_email(). Props scribu. f...
ryan authored
116 $userdata = get_user_by('email', $author);
95ab5d3 Anchor a regular expression in wp-mail.php to ensure we are matching the...
nacin authored
117 if ( ! empty( $userdata ) ) {
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
118 $post_author = $userdata->ID;
119 $author_found = true;
120 }
121 }
cb96ccc Have WP mailed post come from author if email matches author in DB. Hat ...
saxmatt authored
122 }
123
5b0fcdb Timezone fix from Kimmo Suominen.
saxmatt authored
124 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
b43564c *** empty log message ***
saxmatt authored
125 $ddate = trim($line);
126 $ddate = str_replace('Date: ', '', $ddate);
127 if (strpos($ddate, ',')) {
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
128 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
b43564c *** empty log message ***
saxmatt authored
129 }
130 $date_arr = explode(' ', $ddate);
131 $date_time = explode(':', $date_arr[3]);
dd202ce Death to trailing tabs. Props Mark J. fixes #2405
ryan authored
132
b43564c *** empty log message ***
saxmatt authored
133 $ddate_H = $date_time[0];
134 $ddate_i = $date_time[1];
135 $ddate_s = $date_time[2];
dd202ce Death to trailing tabs. Props Mark J. fixes #2405
ryan authored
136
b43564c *** empty log message ***
saxmatt authored
137 $ddate_m = $date_arr[1];
138 $ddate_d = $date_arr[0];
139 $ddate_Y = $date_arr[2];
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
140 for ( $j = 0; $j < 12; $j++ ) {
141 if ( $ddate_m == $dmonths[$j] ) {
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
142 $ddate_m = $j+1;
b43564c *** empty log message ***
saxmatt authored
143 }
144 }
84435bf big leap to GMT dates
michelvaldrighi authored
145
87812da Queue, SMS, timezone, and permalink fixes for wp-mail from MooKitty.
rboren authored
146 $time_zn = intval($date_arr[4]) * 36;
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
147 $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
148 $ddate_U = $ddate_U - $time_zn;
149 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
b8df8fb now storing emailed posts as localtime+gmt
michelvaldrighi authored
150 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
b43564c *** empty log message ***
saxmatt authored
151 }
152 }
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
153 }
b43564c *** empty log message ***
saxmatt authored
154
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
155 // 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
156 if ( $author_found ) {
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
157 $user = new WP_User($post_author);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
158 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
159 } else {
07ff8b2 Use one space, not two, after trailing punctuation. fixes #19537
ryan authored
160 // Author not found in DB, set status to pending. Author already set to admin.
a7c0525 When posting by email, publish only if user has publish caps, otherwise ...
ryan authored
161 $post_status = 'pending';
162 }
163
e8b6fe7 Trim trailing whitespace
ryan authored
164 $subject = trim($subject);
165
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
166 if ( $content_type == 'multipart/alternative' ) {
a0ad8e4 New mail functionality.
saxmatt authored
167 $content = explode('--'.$boundary, $content);
168 $content = $content[2];
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
169 // match case-insensitive content-transfer-encoding
170 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
171 $content = explode($delim[0], $content);
172 $content = $content[1];
173 }
174 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
b43564c *** empty log message ***
saxmatt authored
175 }
a0ad8e4 New mail functionality.
saxmatt authored
176 $content = trim($content);
2f09416 Trim empty lines. Nothing but newline.
ryan authored
177
48f9d53 Add a filter to allow post-by-email plugins better control over the cont...
westi authored
178 //Give Post-By-Email extending plugins full access to the content
179 //Either the raw content or the content of the last quoted-printable section
180 $content = apply_filters('wp_mail_original_content', $content);
e8b6fe7 Trim trailing whitespace
ryan authored
181
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
182 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
7068240 Add support to wp-mail.php for quoted-printable Content-Transfer-Encodin...
rob1n authored
183 $content = quoted_printable_decode($content);
184 }
185
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
186 if ( function_exists('iconv') && ! empty( $charset ) ) {
187 $content = iconv($charset, get_option('blog_charset'), $content);
188 }
e89192b Strip trailing whitespace
ryan authored
189
3e1304d More wp-mail timezone and delimiter fixes from Kitten.
rboren authored
190 // Captures any text in the body after $phone_delim as the body
191 $content = explode($phone_delim, $content);
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
192 $content = empty( $content[1] ) ? $content[0] : $content[1];
b43564c *** empty log message ***
saxmatt authored
193
a0ad8e4 New mail functionality.
saxmatt authored
194 $content = trim($content);
b43564c *** empty log message ***
saxmatt authored
195
ed34ca0 Set GUID when posting.
saxmatt authored
196 $post_content = apply_filters('phone_content', $content);
b43564c *** empty log message ***
saxmatt authored
197
a0ad8e4 New mail functionality.
saxmatt authored
198 $post_title = xmlrpc_getposttitle($content);
b43564c *** empty log message ***
saxmatt authored
199
a0ad8e4 New mail functionality.
saxmatt authored
200 if ($post_title == '') $post_title = $subject;
b43564c *** empty log message ***
saxmatt authored
201
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
202 $post_category = array(get_option('default_email_category'));
b43564c *** empty log message ***
saxmatt authored
203
e75e000 set emailed posts to be published. won't do no good moblogging if in the...
michelvaldrighi authored
204 $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
205 $post_data = add_magic_quotes($post_data);
b43564c *** empty log message ***
saxmatt authored
206
3d21925 Ryan slays the dragon. (Some of these return WP_Error objects.)
matt authored
207 $post_ID = wp_insert_post($post_data);
b7f907b Escape pop3 error messages. Props xknown. fixes #5484
ryan authored
208 if ( is_wp_error( $post_ID ) )
9bfccd6 Add checks for WP_Error. Props filosofo. see #4809
ryan authored
209 echo "\n" . $post_ID->get_error_message();
b43564c *** empty log message ***
saxmatt authored
210
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
211 // We couldn't post, for whatever reason. Better move forward to the next email.
212 if ( empty( $post_ID ) )
65950b5 moving the pop3->delete call to the end of the loop so that emails are d...
michelvaldrighi authored
213 continue;
214
2f4218a Added filter and action hooks.
saxmatt authored
215 do_action('publish_phone', $post_ID);
5624ca0 Fix ups.
saxmatt authored
216
119b39c deprecate wp_specialchars() in favor of esc_html(). Encode quotes for es...
markjaquith authored
217 echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), esc_html($post_author)) . '</p>';
218 echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), esc_html($post_title)) . '</p>';
5624ca0 Fix ups.
saxmatt authored
219
65950b5 moving the pop3->delete call to the end of the loop so that emails are d...
michelvaldrighi authored
220 if(!$pop3->delete($i)) {
119b39c deprecate wp_specialchars() in favor of esc_html(). Encode quotes for es...
markjaquith authored
221 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 are d...
michelvaldrighi authored
222 $pop3->reset();
223 exit;
224 } else {
07ff8b2 Use one space, not two, after trailing punctuation. fixes #19537
ryan authored
225 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';
b43564c *** empty log message ***
saxmatt authored
226 }
227
8486731 Improve wp-mail utf8 support. Fixes #6788 props filosofo.
westi authored
228 }
b43564c *** empty log message ***
saxmatt authored
229
a0ad8e4 New mail functionality.
saxmatt authored
230 $pop3->quit();
Something went wrong with that request. Please try again.