wp-mail.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 __DIR__ . '/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', 5 * MINUTE_IN_SECONDS );
  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 does not seem to be any new mail.' ) );
  49. }
  50. // Always run as an unauthenticated user.
  51. wp_set_current_user( 0 );
  52. for ( $i = 1; $i <= $count; $i++ ) {
  53. $message = $pop3->get( $i );
  54. $bodysignal = false;
  55. $boundary = '';
  56. $charset = '';
  57. $content = '';
  58. $content_type = '';
  59. $content_transfer_encoding = '';
  60. $post_author = 1;
  61. $author_found = false;
  62. $post_date = null;
  63. $post_date_gmt = null;
  64. foreach ( $message as $line ) {
  65. // Body signal.
  66. if ( strlen( $line ) < 3 ) {
  67. $bodysignal = true;
  68. }
  69. if ( $bodysignal ) {
  70. $content .= $line;
  71. } else {
  72. if ( preg_match( '/Content-Type: /i', $line ) ) {
  73. $content_type = trim( $line );
  74. $content_type = substr( $content_type, 14, strlen( $content_type ) - 14 );
  75. $content_type = explode( ';', $content_type );
  76. if ( ! empty( $content_type[1] ) ) {
  77. $charset = explode( '=', $content_type[1] );
  78. $charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : '';
  79. }
  80. $content_type = $content_type[0];
  81. }
  82. if ( preg_match( '/Content-Transfer-Encoding: /i', $line ) ) {
  83. $content_transfer_encoding = trim( $line );
  84. $content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
  85. $content_transfer_encoding = explode( ';', $content_transfer_encoding );
  86. $content_transfer_encoding = $content_transfer_encoding[0];
  87. }
  88. if ( ( 'multipart/alternative' === $content_type ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' === $boundary ) ) {
  89. $boundary = trim( $line );
  90. $boundary = explode( '"', $boundary );
  91. $boundary = $boundary[1];
  92. }
  93. if ( preg_match( '/Subject: /i', $line ) ) {
  94. $subject = trim( $line );
  95. $subject = substr( $subject, 9, strlen( $subject ) - 9 );
  96. // Captures any text in the subject before $phone_delim as the subject.
  97. if ( function_exists( 'iconv_mime_decode' ) ) {
  98. $subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
  99. } else {
  100. $subject = wp_iso_descrambler( $subject );
  101. }
  102. $subject = explode( $phone_delim, $subject );
  103. $subject = $subject[0];
  104. }
  105. /*
  106. * Set the author using the email address (From or Reply-To, the last used)
  107. * otherwise use the site admin.
  108. */
  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. }
  115. $author = sanitize_email( $author );
  116. if ( is_email( $author ) ) {
  117. $userdata = get_user_by( 'email', $author );
  118. if ( ! empty( $userdata ) ) {
  119. $post_author = $userdata->ID;
  120. $author_found = true;
  121. }
  122. }
  123. }
  124. if ( preg_match( '/Date: /i', $line ) ) { // Of the form '20 Mar 2002 20:32:37 +0100'.
  125. $ddate = str_replace( 'Date: ', '', trim( $line ) );
  126. // Remove parenthesised timezone string if it exists, as this confuses strtotime().
  127. $ddate = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );
  128. $ddate_timestamp = strtotime( $ddate );
  129. $post_date = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference );
  130. $post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_timestamp );
  131. }
  132. }
  133. }
  134. // Set $post_status based on $author_found and on author's publish_posts capability.
  135. if ( $author_found ) {
  136. $user = new WP_User( $post_author );
  137. $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
  138. } else {
  139. // Author not found in DB, set status to pending. Author already set to admin.
  140. $post_status = 'pending';
  141. }
  142. $subject = trim( $subject );
  143. if ( 'multipart/alternative' === $content_type ) {
  144. $content = explode( '--' . $boundary, $content );
  145. $content = $content[2];
  146. // Match case-insensitive content-transfer-encoding.
  147. if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
  148. $content = explode( $delim[0], $content );
  149. $content = $content[1];
  150. }
  151. $content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
  152. }
  153. $content = trim( $content );
  154. /**
  155. * Filters the original content of the email.
  156. *
  157. * Give Post-By-Email extending plugins full access to the content, either
  158. * the raw content, or the content of the last quoted-printable section.
  159. *
  160. * @since 2.8.0
  161. *
  162. * @param string $content The original email content.
  163. */
  164. $content = apply_filters( 'wp_mail_original_content', $content );
  165. if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) {
  166. $content = quoted_printable_decode( $content );
  167. }
  168. if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
  169. $content = iconv( $charset, get_option( 'blog_charset' ), $content );
  170. }
  171. // Captures any text in the body after $phone_delim as the body.
  172. $content = explode( $phone_delim, $content );
  173. $content = empty( $content[1] ) ? $content[0] : $content[1];
  174. $content = trim( $content );
  175. /**
  176. * Filters the content of the post submitted by email before saving.
  177. *
  178. * @since 1.2.0
  179. *
  180. * @param string $content The email content.
  181. */
  182. $post_content = apply_filters( 'phone_content', $content );
  183. $post_title = xmlrpc_getposttitle( $content );
  184. if ( '' === trim( $post_title ) ) {
  185. $post_title = $subject;
  186. }
  187. $post_category = array( get_option( 'default_email_category' ) );
  188. $post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
  189. $post_data = wp_slash( $post_data );
  190. $post_ID = wp_insert_post( $post_data );
  191. if ( is_wp_error( $post_ID ) ) {
  192. echo "\n" . $post_ID->get_error_message();
  193. }
  194. // We couldn't post, for whatever reason. Better move forward to the next email.
  195. if ( empty( $post_ID ) ) {
  196. continue;
  197. }
  198. /**
  199. * Fires after a post submitted by email is published.
  200. *
  201. * @since 1.2.0
  202. *
  203. * @param int $post_ID The post ID.
  204. */
  205. do_action( 'publish_phone', $post_ID );
  206. echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
  207. echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
  208. if ( ! $pop3->delete( $i ) ) {
  209. echo '<p>' . sprintf(
  210. /* translators: %s: POP3 error. */
  211. __( 'Oops: %s' ),
  212. esc_html( $pop3->ERROR )
  213. ) . '</p>';
  214. $pop3->reset();
  215. exit;
  216. } else {
  217. echo '<p>' . sprintf(
  218. /* translators: %s: The message ID. */
  219. __( 'Mission complete. Message %s deleted.' ),
  220. '<strong>' . $i . '</strong>'
  221. ) . '</p>';
  222. }
  223. }
  224. $pop3->quit();