136
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Gets the email message from the user's mailbox to add as |
|
4 |
* a WordPress post. Mailbox connection information must be |
|
5 |
* configured under Settings > Writing |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
*/ |
|
9 |
|
|
10 |
/** Make sure that the WordPress bootstrap has run before continuing. */ |
|
11 |
require(dirname(__FILE__) . '/wp-load.php'); |
|
12 |
|
|
13 |
/** Allow a plugin to do a complete takeover of Post by Email **/ |
|
14 |
do_action('wp-mail.php'); |
|
15 |
|
|
16 |
/** Get the POP3 class with which to access the mailbox. */ |
|
17 |
require_once( ABSPATH . WPINC . '/class-pop3.php' ); |
|
18 |
|
|
19 |
/** Only check at this interval for new messages. */ |
|
20 |
if ( !defined('WP_MAIL_INTERVAL') ) |
|
21 |
define('WP_MAIL_INTERVAL', 300); // 5 minutes |
|
22 |
|
|
23 |
$last_checked = get_transient('mailserver_last_checked'); |
|
24 |
|
|
25 |
if ( $last_checked ) |
|
26 |
wp_die(__('Slow down cowboy, no need to check for new mails so often!')); |
|
27 |
|
|
28 |
set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL); |
|
29 |
|
|
30 |
$time_difference = get_option('gmt_offset') * 3600; |
|
31 |
|
|
32 |
$phone_delim = '::'; |
|
33 |
|
|
34 |
$pop3 = new POP3(); |
|
35 |
$count = 0; |
|
36 |
|
|
37 |
if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) || |
|
38 |
! $pop3->user(get_option('mailserver_login')) || |
|
39 |
( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) { |
|
40 |
$pop3->quit(); |
|
41 |
wp_die( ( 0 === $count ) ? __('There doesn’t seem to be any new mail.') : esc_html($pop3->ERROR) ); |
|
42 |
} |
|
43 |
|
|
44 |
for ( $i = 1; $i <= $count; $i++ ) { |
|
45 |
|
|
46 |
$message = $pop3->get($i); |
|
47 |
|
|
48 |
$bodysignal = false; |
|
49 |
$boundary = ''; |
|
50 |
$charset = ''; |
|
51 |
$content = ''; |
|
52 |
$content_type = ''; |
|
53 |
$content_transfer_encoding = ''; |
|
54 |
$post_author = 1; |
|
55 |
$author_found = false; |
|
56 |
$dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); |
|
57 |
foreach ($message as $line) { |
|
58 |
// body signal |
|
59 |
if ( strlen($line) < 3 ) |
|
60 |
$bodysignal = true; |
|
61 |
if ( $bodysignal ) { |
|
62 |
$content .= $line; |
|
63 |
} else { |
|
64 |
if ( preg_match('/Content-Type: /i', $line) ) { |
|
65 |
$content_type = trim($line); |
|
66 |
$content_type = substr($content_type, 14, strlen($content_type) - 14); |
|
67 |
$content_type = explode(';', $content_type); |
|
68 |
if ( ! empty( $content_type[1] ) ) { |
|
69 |
$charset = explode('=', $content_type[1]); |
|
70 |
$charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : ''; |
|
71 |
} |
|
72 |
$content_type = $content_type[0]; |
|
73 |
} |
|
74 |
if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) { |
|
75 |
$content_transfer_encoding = trim($line); |
|
76 |
$content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27); |
|
77 |
$content_transfer_encoding = explode(';', $content_transfer_encoding); |
|
78 |
$content_transfer_encoding = $content_transfer_encoding[0]; |
|
79 |
} |
|
80 |
if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) { |
|
81 |
$boundary = trim($line); |
|
82 |
$boundary = explode('"', $boundary); |
|
83 |
$boundary = $boundary[1]; |
|
84 |
} |
|
85 |
if (preg_match('/Subject: /i', $line)) { |
|
86 |
$subject = trim($line); |
|
87 |
$subject = substr($subject, 9, strlen($subject) - 9); |
|
88 |
// Captures any text in the subject before $phone_delim as the subject |
|
89 |
if ( function_exists('iconv_mime_decode') ) { |
|
90 |
$subject = iconv_mime_decode($subject, 2, get_option('blog_charset')); |
|
91 |
} else { |
|
92 |
$subject = wp_iso_descrambler($subject); |
|
93 |
} |
|
94 |
$subject = explode($phone_delim, $subject); |
|
95 |
$subject = $subject[0]; |
|
96 |
} |
|
97 |
|
|
98 |
// Set the author using the email address (From or Reply-To, the last used) |
|
99 |
// otherwise use the site admin |
|
100 |
if ( preg_match('/(From|Reply-To): /', $line) ) { |
|
101 |
if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) ) |
|
102 |
$author = $matches[0]; |
|
103 |
else |
|
104 |
$author = trim($line); |
|
105 |
$author = sanitize_email($author); |
|
106 |
if ( is_email($author) ) { |
|
107 |
echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>'; |
|
108 |
$userdata = get_user_by_email($author); |
|
109 |
if ( empty($userdata) ) { |
|
110 |
$author_found = false; |
|
111 |
} else { |
|
112 |
$post_author = $userdata->ID; |
|
113 |
$author_found = true; |
|
114 |
} |
|
115 |
} else { |
|
116 |
$author_found = false; |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37' |
|
121 |
$ddate = trim($line); |
|
122 |
$ddate = str_replace('Date: ', '', $ddate); |
|
123 |
if (strpos($ddate, ',')) { |
|
124 |
$ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate))); |
|
125 |
} |
|
126 |
$date_arr = explode(' ', $ddate); |
|
127 |
$date_time = explode(':', $date_arr[3]); |
|
128 |
|
|
129 |
$ddate_H = $date_time[0]; |
|
130 |
$ddate_i = $date_time[1]; |
|
131 |
$ddate_s = $date_time[2]; |
|
132 |
|
|
133 |
$ddate_m = $date_arr[1]; |
|
134 |
$ddate_d = $date_arr[0]; |
|
135 |
$ddate_Y = $date_arr[2]; |
|
136 |
for ( $j = 0; $j < 12; $j++ ) { |
|
137 |
if ( $ddate_m == $dmonths[$j] ) { |
|
138 |
$ddate_m = $j+1; |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
$time_zn = intval($date_arr[4]) * 36; |
|
143 |
$ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y); |
|
144 |
$ddate_U = $ddate_U - $time_zn; |
|
145 |
$post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference); |
|
146 |
$post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U); |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
// Set $post_status based on $author_found and on author's publish_posts capability |
|
152 |
if ( $author_found ) { |
|
153 |
$user = new WP_User($post_author); |
|
154 |
$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending'; |
|
155 |
} else { |
|
156 |
// Author not found in DB, set status to pending. Author already set to admin. |
|
157 |
$post_status = 'pending'; |
|
158 |
} |
|
159 |
|
|
160 |
$subject = trim($subject); |
|
161 |
|
|
162 |
if ( $content_type == 'multipart/alternative' ) { |
|
163 |
$content = explode('--'.$boundary, $content); |
|
164 |
$content = $content[2]; |
|
165 |
// match case-insensitive content-transfer-encoding |
|
166 |
if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) { |
|
167 |
$content = explode($delim[0], $content); |
|
168 |
$content = $content[1]; |
|
169 |
} |
|
170 |
$content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>'); |
|
171 |
} |
|
172 |
$content = trim($content); |
|
173 |
|
|
174 |
//Give Post-By-Email extending plugins full access to the content |
|
175 |
//Either the raw content or the content of the last quoted-printable section |
|
176 |
$content = apply_filters('wp_mail_original_content', $content); |
|
177 |
|
|
178 |
if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) { |
|
179 |
$content = quoted_printable_decode($content); |
|
180 |
} |
|
181 |
|
|
182 |
if ( function_exists('iconv') && ! empty( $charset ) ) { |
|
183 |
$content = iconv($charset, get_option('blog_charset'), $content); |
|
184 |
} |
|
185 |
|
|
186 |
// Captures any text in the body after $phone_delim as the body |
|
187 |
$content = explode($phone_delim, $content); |
|
188 |
$content = empty( $content[1] ) ? $content[0] : $content[1]; |
|
189 |
|
|
190 |
$content = trim($content); |
|
191 |
|
|
192 |
$post_content = apply_filters('phone_content', $content); |
|
193 |
|
|
194 |
$post_title = xmlrpc_getposttitle($content); |
|
195 |
|
|
196 |
if ($post_title == '') $post_title = $subject; |
|
197 |
|
|
198 |
$post_category = array(get_option('default_email_category')); |
|
199 |
|
|
200 |
$post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status'); |
|
201 |
$post_data = add_magic_quotes($post_data); |
|
202 |
|
|
203 |
$post_ID = wp_insert_post($post_data); |
|
204 |
if ( is_wp_error( $post_ID ) ) |
|
205 |
echo "\n" . $post_ID->get_error_message(); |
|
206 |
|
|
207 |
// We couldn't post, for whatever reason. Better move forward to the next email. |
|
208 |
if ( empty( $post_ID ) ) |
|
209 |
continue; |
|
210 |
|
|
211 |
do_action('publish_phone', $post_ID); |
|
212 |
|
|
213 |
echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), esc_html($post_author)) . '</p>'; |
|
214 |
echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), esc_html($post_title)) . '</p>'; |
|
215 |
|
|
216 |
if(!$pop3->delete($i)) { |
|
217 |
echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>'; |
|
218 |
$pop3->reset(); |
|
219 |
exit; |
|
220 |
} else { |
|
221 |
echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>'; |
|
222 |
} |
|
223 |
|
|
224 |
} |
|
225 |
|
|
226 |
$pop3->quit(); |
|
227 |
|
|
228 |
?> |