]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-mail.php
Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-mail.php
1 <?php
2 /**
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.
5  *
6  * @package WordPress
7  */
8
9 /** Make sure that the WordPress bootstrap has ran before continuing. */
10 require(dirname(__FILE__) . '/wp-load.php');
11
12 /** Get the POP3 class for which to access the mailbox. */
13 require_once(ABSPATH.WPINC.'/class-pop3.php');
14
15 // WTF is this? Use constants instead.
16 error_reporting(2037);
17
18 $time_difference = get_option('gmt_offset') * 3600;
19
20 $phone_delim = '::';
21
22 $pop3 = new POP3();
23
24 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port')))
25         wp_die(wp_specialchars($pop3->ERROR));
26
27 if (!$pop3->user(get_option('mailserver_login')))
28         wp_die(wp_specialchars($pop3->ERROR));
29
30 $count = $pop3->pass(get_option('mailserver_pass'));
31 if (false === $count)
32         wp_die(wp_specialchars($pop3->ERROR));
33 if (0 == $count)
34         echo "<p>There doesn't seem to be any new mail.</p>\n"; // will fall-through to end of for loop
35
36 for ($i=1; $i <= $count; $i++) :
37
38         $message = $pop3->get($i);
39
40         $content = '';
41         $content_type = '';
42         $content_transfer_encoding = '';
43         $boundary = '';
44         $bodysignal = 0;
45         $post_author = 1;
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;
50
51                 if ($bodysignal) {
52                         $content .= $line;
53                 } else {
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];
59                         }
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];
65                         }
66                         if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
67                                 $boundary = trim($line);
68                                 $boundary = explode('"', $boundary);
69                                 $boundary = $boundary[1];
70                         }
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];
78                         }
79
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];
85                                 else
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);
91                                         if (!$userdata) {
92                                                 $post_author = 1;
93                                                 $author_found = false;
94                                         } else {
95                                                 $post_author = $userdata->ID;
96                                                 $author_found = true;
97                                         }
98                                 } else {
99                                         $post_author = 1;
100                                         $author_found = false;
101                                 }
102                         }
103
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)));
109                                 }
110                                 $date_arr = explode(' ', $ddate);
111                                 $date_time = explode(':', $date_arr[3]);
112
113                                 $ddate_H = $date_time[0];
114                                 $ddate_i = $date_time[1];
115                                 $ddate_s = $date_time[2];
116
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]) {
122                                                 $ddate_m = $j+1;
123                                         }
124                                 }
125
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);
131                         }
132                 }
133         endforeach;
134
135         // Set $post_status based on $author_found and on author's publish_posts capability
136         if ($author_found) {
137                 $user = new WP_User($post_author);
138                 if ($user->has_cap('publish_posts'))
139                         $post_status = 'publish';
140                 else
141                         $post_status = 'pending';
142         } else {
143                 // Author not found in DB, set status to pending.  Author already set to admin.
144                 $post_status = 'pending';
145         }
146
147         $subject = trim($subject);
148
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>');
154         }
155         $content = trim($content);
156
157         if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
158                 $content = quoted_printable_decode($content);
159         }
160
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];
164
165         $content = trim($content);
166
167         $post_content = apply_filters('phone_content', $content);
168
169         $post_title = xmlrpc_getposttitle($content);
170
171         if ($post_title == '') $post_title = $subject;
172
173         if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
174
175         $post_category = $post_categories;
176
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);
179
180         $post_ID = wp_insert_post($post_data);
181         if ( is_wp_error( $post_ID ) )
182                 echo "\n" . $post_ID->get_error_message();
183
184         if (!$post_ID) {
185                 // we couldn't post, for whatever reason. better move forward to the next email
186                 continue;
187         }
188
189         do_action('publish_phone', $post_ID);
190
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 />";
193
194         if(!$pop3->delete($i)) {
195                 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
196                 $pop3->reset();
197                 exit;
198         } else {
199                 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";
200         }
201
202 endfor;
203
204 $pop3->quit();
205
206 ?>