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