Wordpress 2.5.1-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 if (!$pop3->user(get_option('mailserver_login')))
18         wp_die(wp_specialchars($pop3->ERROR));
19
20 $count = $pop3->pass(get_option('mailserver_pass'));
21 if (false === $count)
22         wp_die(wp_specialchars($pop3->ERROR));
23 if (0 == $count)
24         echo "<p>There doesn't seem to be any new mail.</p>\n"; // will fall-through to end of for loop
25
26 for ($i=1; $i <= $count; $i++) :
27
28         $message = $pop3->get($i);
29
30         $content = '';
31         $content_type = '';
32         $content_transfer_encoding = '';
33         $boundary = '';
34         $bodysignal = 0;
35         $post_author = 1;
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;
40
41                 if ($bodysignal) {
42                         $content .= $line;
43                 } else {
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];
49                         }
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];
55                         }
56                         if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
57                                 $boundary = trim($line);
58                                 $boundary = explode('"', $boundary);
59                                 $boundary = $boundary[1];
60                         }
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];
68                         }
69
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];
75                                 else
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);
81                                         if (!$userdata) {
82                                                 $post_author = 1;
83                                                 $author_found = false;
84                                         } else {
85                                                 $post_author = $userdata->ID;
86                                                 $author_found = true;
87                                         }
88                                 } else {
89                                         $post_author = 1;
90                                         $author_found = false;
91                                 }
92                         }
93
94                         if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
95                                 $ddate = trim($line);
96                                 $ddate = str_replace('Date: ', '', $ddate);
97                                 if (strpos($ddate, ',')) {
98                                         $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
99                                 }
100                                 $date_arr = explode(' ', $ddate);
101                                 $date_time = explode(':', $date_arr[3]);
102
103                                 $ddate_H = $date_time[0];
104                                 $ddate_i = $date_time[1];
105                                 $ddate_s = $date_time[2];
106
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]) {
112                                                 $ddate_m = $j+1;
113                                         }
114                                 }
115
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);
121                         }
122                 }
123         endforeach;
124
125         // Set $post_status based on $author_found and on author's publish_posts capability
126         if ($author_found) {
127                 $user = new WP_User($post_author);
128                 if ($user->has_cap('publish_posts'))
129                         $post_status = 'publish';
130                 else
131                         $post_status = 'pending';
132         } else {
133                 // Author not found in DB, set status to pending.  Author already set to admin.
134                 $post_status = 'pending';
135         }
136
137         $subject = trim($subject);
138
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>');
144         }
145         $content = trim($content);
146
147         if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
148                 $content = quoted_printable_decode($content);
149         }
150
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];
154
155         $content = trim($content);
156
157         $post_content = apply_filters('phone_content', $content);
158
159         $post_title = xmlrpc_getposttitle($content);
160
161         if ($post_title == '') $post_title = $subject;
162
163         if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
164
165         $post_category = $post_categories;
166
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);
169
170         $post_ID = wp_insert_post($post_data);
171         if ( is_wp_error( $post_ID ) )
172                 echo "\n" . $post_ID->get_error_message();
173
174         if (!$post_ID) {
175                 // we couldn't post, for whatever reason. better move forward to the next email
176                 continue;
177         }
178
179         do_action('publish_phone', $post_ID);
180
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 />";
183
184         if(!$pop3->delete($i)) {
185                 echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
186                 $pop3->reset();
187                 exit;
188         } else {
189                 echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";
190         }
191
192 endfor;
193
194 $pop3->quit();
195
196 ?>