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 $count = $pop3->login(get_option('mailserver_login'), get_option('mailserver_pass'));
18 if (0 == $count) wp_die(__('There doesn’t seem to be any new mail.'));
21 for ($i=1; $i <= $count; $i++) :
23 $message = $pop3->get($i);
27 $content_transfer_encoding = '';
30 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
31 foreach ($message as $line) :
32 if (strlen($line) < 3) $bodysignal = 1;
37 if (preg_match('/Content-Type: /i', $line)) {
38 $content_type = trim($line);
39 $content_type = substr($content_type, 14, strlen($content_type)-14);
40 $content_type = explode(';', $content_type);
41 $content_type = $content_type[0];
43 if (preg_match('/Content-Transfer-Encoding: /i', $line)) {
44 $content_transfer_encoding = trim($line);
45 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding)-14);
46 $content_transfer_encoding = explode(';', $content_transfer_encoding);
47 $content_transfer_encoding = $content_transfer_encoding[0];
49 if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
50 $boundary = trim($line);
51 $boundary = explode('"', $boundary);
52 $boundary = $boundary[1];
54 if (preg_match('/Subject: /i', $line)) {
55 $subject = trim($line);
56 $subject = substr($subject, 9, strlen($subject)-9);
57 $subject = wp_iso_descrambler($subject);
58 // Captures any text in the subject before $phone_delim as the subject
59 $subject = explode($phone_delim, $subject);
60 $subject = $subject[0];
63 // Set the author using the email address (From or Reply-To, the last used)
64 // otherwise use the site admin
65 if ( preg_match('/(From|Reply-To): /', $line) ) {
66 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
67 $author = $matches[0];
69 $author = trim($line);
70 $author = sanitize_email($author);
71 if ( is_email($author) ) {
72 echo "Author = {$author} <p>";
73 $author = $wpdb->escape($author);
74 $result = $wpdb->get_row("SELECT ID FROM $wpdb->users WHERE user_email='$author' LIMIT 1");
78 $post_author = $result->ID;
83 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
85 $ddate = str_replace('Date: ', '', $ddate);
86 if (strpos($ddate, ',')) {
87 $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
89 $date_arr = explode(' ', $ddate);
90 $date_time = explode(':', $date_arr[3]);
92 $ddate_H = $date_time[0];
93 $ddate_i = $date_time[1];
94 $ddate_s = $date_time[2];
96 $ddate_m = $date_arr[1];
97 $ddate_d = $date_arr[0];
98 $ddate_Y = $date_arr[2];
99 for ($j=0; $j<12; $j++) {
100 if ($ddate_m == $dmonths[$j]) {
105 $time_zn = intval($date_arr[4]) * 36;
106 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
107 $ddate_U = $ddate_U - $time_zn;
108 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
109 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
114 $subject = trim($subject);
116 if ($content_type == 'multipart/alternative') {
117 $content = explode('--'.$boundary, $content);
118 $content = $content[2];
119 $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
120 $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
122 $content = trim($content);
124 if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
125 $content = quoted_printable_decode($content);
128 // Captures any text in the body after $phone_delim as the body
129 $content = explode($phone_delim, $content);
130 $content[1] ? $content = $content[1] : $content = $content[0];
132 $content = trim($content);
134 $post_content = apply_filters('phone_content', $content);
136 $post_title = xmlrpc_getposttitle($content);
138 if ($post_title == '') $post_title = $subject;
140 if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
142 $post_category = $post_categories;
144 // or maybe we should leave the choice to email drafts? propose a way
145 $post_status = 'publish';
147 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
148 $post_data = add_magic_quotes($post_data);
150 $post_ID = wp_insert_post($post_data);
151 if ( is_wp_error( $post_ID ) )
152 echo "\n" . $post_ID->get_error_message();
155 // we couldn't post, for whatever reason. better move forward to the next email
159 do_action('publish_phone', $post_ID);
161 echo "\n<p><b>Author:</b> " . wp_specialchars($post_author) . "</p>";
162 echo "\n<p><b>Posted title:</b> " . wp_specialchars($post_title) . "<br />";
164 if(!$pop3->delete($i)) {
165 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
169 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";