wp-mail.php 7.9 KB

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