raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Gets the email message from the user's mailbox to add as
mp-wp_genesis 4 * a WordPress post. Mailbox connection information must be
mp-wp_genesis 5 * configured under Settings > Writing
mp-wp_genesis 6 *
mp-wp_genesis 7 * @package WordPress
mp-wp_genesis 8 */
mp-wp_genesis 9
mp-wp_genesis 10 /** Make sure that the WordPress bootstrap has run before continuing. */
mp-wp_genesis 11 require(dirname(__FILE__) . '/wp-load.php');
mp-wp_genesis 12
mp-wp_genesis 13 /** Get the POP3 class with which to access the mailbox. */
mp-wp_genesis 14 require_once( ABSPATH . WPINC . '/class-pop3.php' );
mp-wp_genesis 15
mp-wp_genesis 16 $time_difference = absint(get_option('gmt_offset')) * 3600;
mp-wp_genesis 17
mp-wp_genesis 18 $phone_delim = '::';
mp-wp_genesis 19
mp-wp_genesis 20 $pop3 = new POP3();
mp-wp_genesis 21
mp-wp_genesis 22 if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) ||
mp-wp_genesis 23 ! $pop3->user(get_option('mailserver_login')) ||
mp-wp_genesis 24 ( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) {
mp-wp_genesis 25 $pop3->quit();
mp-wp_genesis 26 wp_die( ( 0 === $count ) ? __("There doesn't seem to be any new mail.") : wp_specialchars($pop3->ERROR) );
mp-wp_genesis 27 }
mp-wp_genesis 28
mp-wp_genesis 29 for ( $i = 1; $i <= $count; $i++ ) {
mp-wp_genesis 30
mp-wp_genesis 31 $message = $pop3->get($i);
mp-wp_genesis 32
mp-wp_genesis 33 $bodysignal = false;
mp-wp_genesis 34 $boundary = '';
mp-wp_genesis 35 $charset = '';
mp-wp_genesis 36 $content = '';
mp-wp_genesis 37 $content_type = '';
mp-wp_genesis 38 $content_transfer_encoding = '';
mp-wp_genesis 39 $post_author = 1;
mp-wp_genesis 40 $author_found = false;
mp-wp_genesis 41 $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
mp-wp_genesis 42 foreach ($message as $line) {
mp-wp_genesis 43 // body signal
mp-wp_genesis 44 if ( strlen($line) < 3 )
mp-wp_genesis 45 $bodysignal = true;
mp-wp_genesis 46 if ( $bodysignal ) {
mp-wp_genesis 47 $content .= $line;
mp-wp_genesis 48 } else {
mp-wp_genesis 49 if ( preg_match('/Content-Type: /i', $line) ) {
mp-wp_genesis 50 $content_type = trim($line);
mp-wp_genesis 51 $content_type = substr($content_type, 14, strlen($content_type) - 14);
mp-wp_genesis 52 $content_type = explode(';', $content_type);
mp-wp_genesis 53 if ( ! empty( $content_type[1] ) ) {
mp-wp_genesis 54 $charset = explode('=', $content_type[1]);
mp-wp_genesis 55 $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
mp-wp_genesis 56 }
mp-wp_genesis 57 $content_type = $content_type[0];
mp-wp_genesis 58 }
mp-wp_genesis 59 if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
mp-wp_genesis 60 $content_transfer_encoding = trim($line);
mp-wp_genesis 61 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
mp-wp_genesis 62 $content_transfer_encoding = explode(';', $content_transfer_encoding);
mp-wp_genesis 63 $content_transfer_encoding = $content_transfer_encoding[0];
mp-wp_genesis 64 }
mp-wp_genesis 65 if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
mp-wp_genesis 66 $boundary = trim($line);
mp-wp_genesis 67 $boundary = explode('"', $boundary);
mp-wp_genesis 68 $boundary = $boundary[1];
mp-wp_genesis 69 }
mp-wp_genesis 70 if (preg_match('/Subject: /i', $line)) {
mp-wp_genesis 71 $subject = trim($line);
mp-wp_genesis 72 $subject = substr($subject, 9, strlen($subject) - 9);
mp-wp_genesis 73 // Captures any text in the subject before $phone_delim as the subject
mp-wp_genesis 74 if ( function_exists('iconv_mime_decode') ) {
mp-wp_genesis 75 $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
mp-wp_genesis 76 } else {
mp-wp_genesis 77 $subject = wp_iso_descrambler($subject);
mp-wp_genesis 78 }
mp-wp_genesis 79 $subject = explode($phone_delim, $subject);
mp-wp_genesis 80 $subject = $subject[0];
mp-wp_genesis 81 }
mp-wp_genesis 82
mp-wp_genesis 83 // Set the author using the email address (From or Reply-To, the last used)
mp-wp_genesis 84 // otherwise use the site admin
mp-wp_genesis 85 if ( preg_match('/(From|Reply-To): /', $line) ) {
mp-wp_genesis 86 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
mp-wp_genesis 87 $author = $matches[0];
mp-wp_genesis 88 else
mp-wp_genesis 89 $author = trim($line);
mp-wp_genesis 90 $author = sanitize_email($author);
mp-wp_genesis 91 if ( is_email($author) ) {
mp-wp_genesis 92 echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
mp-wp_genesis 93 $userdata = get_user_by_email($author);
mp-wp_genesis 94 if ( empty($userdata) ) {
mp-wp_genesis 95 $author_found = false;
mp-wp_genesis 96 } else {
mp-wp_genesis 97 $post_author = $userdata->ID;
mp-wp_genesis 98 $author_found = true;
mp-wp_genesis 99 }
mp-wp_genesis 100 } else {
mp-wp_genesis 101 $author_found = false;
mp-wp_genesis 102 }
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
mp-wp_genesis 106 $ddate = trim($line);
mp-wp_genesis 107 $ddate = str_replace('Date: ', '', $ddate);
mp-wp_genesis 108 if (strpos($ddate, ',')) {
mp-wp_genesis 109 $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
mp-wp_genesis 110 }
mp-wp_genesis 111 $date_arr = explode(' ', $ddate);
mp-wp_genesis 112 $date_time = explode(':', $date_arr[3]);
mp-wp_genesis 113
mp-wp_genesis 114 $ddate_H = $date_time[0];
mp-wp_genesis 115 $ddate_i = $date_time[1];
mp-wp_genesis 116 $ddate_s = $date_time[2];
mp-wp_genesis 117
mp-wp_genesis 118 $ddate_m = $date_arr[1];
mp-wp_genesis 119 $ddate_d = $date_arr[0];
mp-wp_genesis 120 $ddate_Y = $date_arr[2];
mp-wp_genesis 121 for ( $j = 0; $j < 12; $j++ ) {
mp-wp_genesis 122 if ( $ddate_m == $dmonths[$j] ) {
mp-wp_genesis 123 $ddate_m = $j+1;
mp-wp_genesis 124 }
mp-wp_genesis 125 }
mp-wp_genesis 126
mp-wp_genesis 127 $time_zn = intval($date_arr[4]) * 36;
mp-wp_genesis 128 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
mp-wp_genesis 129 $ddate_U = $ddate_U - $time_zn;
mp-wp_genesis 130 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
mp-wp_genesis 131 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
mp-wp_genesis 132 }
mp-wp_genesis 133 }
mp-wp_genesis 134 }
mp-wp_genesis 135
mp-wp_genesis 136 // Set $post_status based on $author_found and on author's publish_posts capability
mp-wp_genesis 137 if ( $author_found ) {
mp-wp_genesis 138 $user = new WP_User($post_author);
mp-wp_genesis 139 $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
mp-wp_genesis 140 } else {
mp-wp_genesis 141 // Author not found in DB, set status to pending. Author already set to admin.
mp-wp_genesis 142 $post_status = 'pending';
mp-wp_genesis 143 }
mp-wp_genesis 144
mp-wp_genesis 145 $subject = trim($subject);
mp-wp_genesis 146
mp-wp_genesis 147 if ( $content_type == 'multipart/alternative' ) {
mp-wp_genesis 148 $content = explode('--'.$boundary, $content);
mp-wp_genesis 149 $content = $content[2];
mp-wp_genesis 150 // match case-insensitive content-transfer-encoding
mp-wp_genesis 151 if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
mp-wp_genesis 152 $content = explode($delim[0], $content);
mp-wp_genesis 153 $content = $content[1];
mp-wp_genesis 154 }
mp-wp_genesis 155 $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
mp-wp_genesis 156 }
mp-wp_genesis 157 $content = trim($content);
mp-wp_genesis 158
mp-wp_genesis 159 if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
mp-wp_genesis 160 $content = quoted_printable_decode($content);
mp-wp_genesis 161 }
mp-wp_genesis 162
mp-wp_genesis 163 if ( function_exists('iconv') && ! empty( $charset ) ) {
mp-wp_genesis 164 $content = iconv($charset, get_option('blog_charset'), $content);
mp-wp_genesis 165 }
mp-wp_genesis 166
mp-wp_genesis 167 // Captures any text in the body after $phone_delim as the body
mp-wp_genesis 168 $content = explode($phone_delim, $content);
mp-wp_genesis 169 $content = empty( $content[1] ) ? $content[0] : $content[1];
mp-wp_genesis 170
mp-wp_genesis 171 $content = trim($content);
mp-wp_genesis 172
mp-wp_genesis 173 $post_content = apply_filters('phone_content', $content);
mp-wp_genesis 174
mp-wp_genesis 175 $post_title = xmlrpc_getposttitle($content);
mp-wp_genesis 176
mp-wp_genesis 177 if ($post_title == '') $post_title = $subject;
mp-wp_genesis 178
mp-wp_genesis 179 $post_category = array(get_option('default_email_category'));
mp-wp_genesis 180
mp-wp_genesis 181 $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
mp-wp_genesis 182 $post_data = add_magic_quotes($post_data);
mp-wp_genesis 183
mp-wp_genesis 184 $post_ID = wp_insert_post($post_data);
mp-wp_genesis 185 if ( is_wp_error( $post_ID ) )
mp-wp_genesis 186 echo "\n" . $post_ID->get_error_message();
mp-wp_genesis 187
mp-wp_genesis 188 // We couldn't post, for whatever reason. Better move forward to the next email.
mp-wp_genesis 189 if ( empty( $post_ID ) )
mp-wp_genesis 190 continue;
mp-wp_genesis 191
mp-wp_genesis 192 do_action('publish_phone', $post_ID);
mp-wp_genesis 193
mp-wp_genesis 194 echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), wp_specialchars($post_author)) . '</p>';
mp-wp_genesis 195 echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), wp_specialchars($post_title)) . '</p>';
mp-wp_genesis 196
mp-wp_genesis 197 if(!$pop3->delete($i)) {
mp-wp_genesis 198 echo '<p>' . sprintf(__('Oops: %s'), wp_specialchars($pop3->ERROR)) . '</p>';
mp-wp_genesis 199 $pop3->reset();
mp-wp_genesis 200 exit;
mp-wp_genesis 201 } else {
mp-wp_genesis 202 echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';
mp-wp_genesis 203 }
mp-wp_genesis 204
mp-wp_genesis 205 }
mp-wp_genesis 206
mp-wp_genesis 207 $pop3->quit();
mp-wp_genesis 208
mp-wp_genesis 209 ?>