Wordpress 2.3.2-scripts
[autoinstalls/wordpress.git] / wp-mail.php
1 <?php
2 require(dirname(__FILE__) . '/wp-config.php');
3
4 require_once(ABSPATH.WPINC.'/class-pop3.php');
5
6 error_reporting(2037);
7
8 $time_difference = get_option('gmt_offset') * 3600;
9
10 $phone_delim = '::';
11
12 $pop3 = new POP3();
13
14 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port')))
15         wp_die(wp_specialchars($pop3->ERROR));
16
17 $count = $pop3->login(get_option('mailserver_login'), get_option('mailserver_pass'));
18 if (0 == $count) wp_die(__('There doesn&#8217;t seem to be any new mail.'));
19
20
21 for ($i=1; $i <= $count; $i++) :
22
23         $message = $pop3->get($i);
24
25         $content = '';
26         $content_type = '';
27         $content_transfer_encoding = '';
28         $boundary = '';
29         $bodysignal = 0;
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;
33
34                 if ($bodysignal) {
35                         $content .= $line;
36                 } else {
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];
42                         }
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];
48                         }
49                         if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
50                                 $boundary = trim($line);
51                                 $boundary = explode('"', $boundary);
52                                 $boundary = $boundary[1];
53                         }
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];
61                         }
62
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];
68                                 else
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");
75                                         if (!$result)
76                                                 $post_author = 1;
77                                         else
78                                                 $post_author = $result->ID;
79                                 } else
80                                         $post_author = 1;
81                         }
82
83                         if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
84                                 $ddate = trim($line);
85                                 $ddate = str_replace('Date: ', '', $ddate);
86                                 if (strpos($ddate, ',')) {
87                                         $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
88                                 }
89                                 $date_arr = explode(' ', $ddate);
90                                 $date_time = explode(':', $date_arr[3]);
91
92                                 $ddate_H = $date_time[0];
93                                 $ddate_i = $date_time[1];
94                                 $ddate_s = $date_time[2];
95
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]) {
101                                                 $ddate_m = $j+1;
102                                         }
103                                 }
104
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);
110                         }
111                 }
112         endforeach;
113
114         $subject = trim($subject);
115
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>');
121         }
122         $content = trim($content);
123
124         if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
125                 $content = quoted_printable_decode($content);
126         }
127
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];
131
132         $content = trim($content);
133
134         $post_content = apply_filters('phone_content', $content);
135
136         $post_title = xmlrpc_getposttitle($content);
137
138         if ($post_title == '') $post_title = $subject;
139
140         if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
141
142         $post_category = $post_categories;
143
144         // or maybe we should leave the choice to email drafts? propose a way
145         $post_status = 'publish';
146
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);
149
150         $post_ID = wp_insert_post($post_data);
151         if ( is_wp_error( $post_ID ) ) 
152                 echo "\n" . $post_ID->get_error_message();
153
154         if (!$post_ID) {
155                 // we couldn't post, for whatever reason. better move forward to the next email
156                 continue;
157         }
158
159         do_action('publish_phone', $post_ID);
160
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 />";
163
164         if(!$pop3->delete($i)) {
165                 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
166                 $pop3->reset();
167                 exit;
168         } else {
169                 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";
170         }
171
172 endfor;
173
174 $pop3->quit();
175
176 ?>