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.
9 /** Make sure that the WordPress bootstrap has ran before continuing. */
10 require(dirname(__FILE__) . '/wp-load.php');
12 /** Get the POP3 class for which to access the mailbox. */
13 require_once(ABSPATH.WPINC.'/class-pop3.php');
15 // WTF is this? Use constants instead.
16 error_reporting(2037);
18 $time_difference = get_option('gmt_offset') * 3600;
24 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port')))
25 wp_die(wp_specialchars($pop3->ERROR));
27 if (!$pop3->user(get_option('mailserver_login')))
28 wp_die(wp_specialchars($pop3->ERROR));
30 $count = $pop3->pass(get_option('mailserver_pass'));
32 wp_die(wp_specialchars($pop3->ERROR));
34 echo "<p>There doesn't seem to be any new mail.</p>\n"; // will fall-through to end of for loop
36 for ($i=1; $i <= $count; $i++) :
38 $message = $pop3->get($i);
42 $content_transfer_encoding = '';
46 $author_found = false;
47 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
48 foreach ($message as $line) :
49 if (strlen($line) < 3) $bodysignal = 1;
54 if (preg_match('/Content-Type: /i', $line)) {
55 $content_type = trim($line);
56 $content_type = substr($content_type, 14, strlen($content_type)-14);
57 $content_type = explode(';', $content_type);
58 $content_type = $content_type[0];
60 if (preg_match('/Content-Transfer-Encoding: /i', $line)) {
61 $content_transfer_encoding = trim($line);
62 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding)-14);
63 $content_transfer_encoding = explode(';', $content_transfer_encoding);
64 $content_transfer_encoding = $content_transfer_encoding[0];
66 if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
67 $boundary = trim($line);
68 $boundary = explode('"', $boundary);
69 $boundary = $boundary[1];
71 if (preg_match('/Subject: /i', $line)) {
72 $subject = trim($line);
73 $subject = substr($subject, 9, strlen($subject)-9);
74 $subject = wp_iso_descrambler($subject);
75 // Captures any text in the subject before $phone_delim as the subject
76 $subject = explode($phone_delim, $subject);
77 $subject = $subject[0];
80 // Set the author using the email address (From or Reply-To, the last used)
81 // otherwise use the site admin
82 if ( preg_match('/(From|Reply-To): /', $line) ) {
83 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
84 $author = $matches[0];
86 $author = trim($line);
87 $author = sanitize_email($author);
88 if ( is_email($author) ) {
89 echo "Author = {$author} <p>";
90 $userdata = get_user_by_email($author);
93 $author_found = false;
95 $post_author = $userdata->ID;
100 $author_found = false;
104 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
105 $ddate = trim($line);
106 $ddate = str_replace('Date: ', '', $ddate);
107 if (strpos($ddate, ',')) {
108 $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
110 $date_arr = explode(' ', $ddate);
111 $date_time = explode(':', $date_arr[3]);
113 $ddate_H = $date_time[0];
114 $ddate_i = $date_time[1];
115 $ddate_s = $date_time[2];
117 $ddate_m = $date_arr[1];
118 $ddate_d = $date_arr[0];
119 $ddate_Y = $date_arr[2];
120 for ($j=0; $j<12; $j++) {
121 if ($ddate_m == $dmonths[$j]) {
126 $time_zn = intval($date_arr[4]) * 36;
127 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
128 $ddate_U = $ddate_U - $time_zn;
129 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
130 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
135 // Set $post_status based on $author_found and on author's publish_posts capability
137 $user = new WP_User($post_author);
138 if ($user->has_cap('publish_posts'))
139 $post_status = 'publish';
141 $post_status = 'pending';
143 // Author not found in DB, set status to pending. Author already set to admin.
144 $post_status = 'pending';
147 $subject = trim($subject);
149 if ($content_type == 'multipart/alternative') {
150 $content = explode('--'.$boundary, $content);
151 $content = $content[2];
152 $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
153 $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
155 $content = trim($content);
157 if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
158 $content = quoted_printable_decode($content);
161 // Captures any text in the body after $phone_delim as the body
162 $content = explode($phone_delim, $content);
163 $content[1] ? $content = $content[1] : $content = $content[0];
165 $content = trim($content);
167 $post_content = apply_filters('phone_content', $content);
169 $post_title = xmlrpc_getposttitle($content);
171 if ($post_title == '') $post_title = $subject;
173 if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
175 $post_category = $post_categories;
177 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
178 $post_data = add_magic_quotes($post_data);
180 $post_ID = wp_insert_post($post_data);
181 if ( is_wp_error( $post_ID ) )
182 echo "\n" . $post_ID->get_error_message();
185 // we couldn't post, for whatever reason. better move forward to the next email
189 do_action('publish_phone', $post_ID);
191 echo "\n<p><b>Author:</b> " . wp_specialchars($post_author) . "</p>";
192 echo "\n<p><b>Posted title:</b> " . wp_specialchars($post_title) . "<br />";
194 if(!$pop3->delete($i)) {
195 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
199 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";