wp-mail.php 8.3 KB

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