2 require(dirname(__FILE__) . '/wp-config.php');
4 require_once(ABSPATH.WPINC.'/class-pop3.php');
8 $time_difference = get_option('gmt_offset') * 3600;
14 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port')))
15 wp_die(wp_specialchars($pop3->ERROR));
17 if (!$pop3->user(get_option('mailserver_login')))
18 wp_die(wp_specialchars($pop3->ERROR));
20 $count = $pop3->pass(get_option('mailserver_pass'));
22 wp_die(wp_specialchars($pop3->ERROR));
24 echo "<p>There doesn't seem to be any new mail.</p>\n"; // will fall-through to end of for loop
26 for ($i=1; $i <= $count; $i++) :
28 $message = $pop3->get($i);
32 $content_transfer_encoding = '';
36 $author_found = false;
37 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
38 foreach ($message as $line) :
39 if (strlen($line) < 3) $bodysignal = 1;
44 if (preg_match('/Content-Type: /i', $line)) {
45 $content_type = trim($line);
46 $content_type = substr($content_type, 14, strlen($content_type)-14);
47 $content_type = explode(';', $content_type);
48 $content_type = $content_type[0];
50 if (preg_match('/Content-Transfer-Encoding: /i', $line)) {
51 $content_transfer_encoding = trim($line);
52 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding)-14);
53 $content_transfer_encoding = explode(';', $content_transfer_encoding);
54 $content_transfer_encoding = $content_transfer_encoding[0];
56 if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
57 $boundary = trim($line);
58 $boundary = explode('"', $boundary);
59 $boundary = $boundary[1];
61 if (preg_match('/Subject: /i', $line)) {
62 $subject = trim($line);
63 $subject = substr($subject, 9, strlen($subject)-9);
64 $subject = wp_iso_descrambler($subject);
65 // Captures any text in the subject before $phone_delim as the subject
66 $subject = explode($phone_delim, $subject);
67 $subject = $subject[0];
70 // Set the author using the email address (From or Reply-To, the last used)
71 // otherwise use the site admin
72 if ( preg_match('/(From|Reply-To): /', $line) ) {
73 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
74 $author = $matches[0];
76 $author = trim($line);
77 $author = sanitize_email($author);
78 if ( is_email($author) ) {
79 echo "Author = {$author} <p>";
80 $userdata = get_user_by_email($author);
83 $author_found = false;
85 $post_author = $userdata->ID;
90 $author_found = false;
94 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
96 $ddate = str_replace('Date: ', '', $ddate);
97 if (strpos($ddate, ',')) {
98 $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
100 $date_arr = explode(' ', $ddate);
101 $date_time = explode(':', $date_arr[3]);
103 $ddate_H = $date_time[0];
104 $ddate_i = $date_time[1];
105 $ddate_s = $date_time[2];
107 $ddate_m = $date_arr[1];
108 $ddate_d = $date_arr[0];
109 $ddate_Y = $date_arr[2];
110 for ($j=0; $j<12; $j++) {
111 if ($ddate_m == $dmonths[$j]) {
116 $time_zn = intval($date_arr[4]) * 36;
117 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
118 $ddate_U = $ddate_U - $time_zn;
119 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
120 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
125 // Set $post_status based on $author_found and on author's publish_posts capability
127 $user = new WP_User($post_author);
128 if ($user->has_cap('publish_posts'))
129 $post_status = 'publish';
131 $post_status = 'pending';
133 // Author not found in DB, set status to pending. Author already set to admin.
134 $post_status = 'pending';
137 $subject = trim($subject);
139 if ($content_type == 'multipart/alternative') {
140 $content = explode('--'.$boundary, $content);
141 $content = $content[2];
142 $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
143 $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
145 $content = trim($content);
147 if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
148 $content = quoted_printable_decode($content);
151 // Captures any text in the body after $phone_delim as the body
152 $content = explode($phone_delim, $content);
153 $content[1] ? $content = $content[1] : $content = $content[0];
155 $content = trim($content);
157 $post_content = apply_filters('phone_content', $content);
159 $post_title = xmlrpc_getposttitle($content);
161 if ($post_title == '') $post_title = $subject;
163 if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
165 $post_category = $post_categories;
167 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
168 $post_data = add_magic_quotes($post_data);
170 $post_ID = wp_insert_post($post_data);
171 if ( is_wp_error( $post_ID ) )
172 echo "\n" . $post_ID->get_error_message();
175 // we couldn't post, for whatever reason. better move forward to the next email
179 do_action('publish_phone', $post_ID);
181 echo "\n<p><b>Author:</b> " . wp_specialchars($post_author) . "</p>";
182 echo "\n<p><b>Posted title:</b> " . wp_specialchars($post_title) . "<br />";
184 if(!$pop3->delete($i)) {
185 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
189 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";