PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-mail.php

https://bitbucket.org/dkrzos/phc
PHP | 230 lines | 165 code | 43 blank | 22 comment | 46 complexity | 8643c50bcb9832a1c591ae5b306f425f MD5 | raw file
Possible License(s): GPL-2.0
  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. if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
  12. wp_die( __( 'This action has been disabled by the administrator.' ) );
  13. /** Allow a plugin to do a complete takeover of Post by Email **/
  14. do_action('wp-mail.php');
  15. /** Get the POP3 class with which to access the mailbox. */
  16. require_once( ABSPATH . WPINC . '/class-pop3.php' );
  17. /** Only check at this interval for new messages. */
  18. if ( !defined('WP_MAIL_INTERVAL') )
  19. define('WP_MAIL_INTERVAL', 300); // 5 minutes
  20. $last_checked = get_transient('mailserver_last_checked');
  21. if ( $last_checked )
  22. wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
  23. set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
  24. $time_difference = get_option('gmt_offset') * HOUR_IN_SECONDS;
  25. $phone_delim = '::';
  26. $pop3 = new POP3();
  27. if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
  28. wp_die( esc_html( $pop3->ERROR ) );
  29. $count = $pop3->pass( get_option('mailserver_pass') );
  30. if( false === $count )
  31. wp_die( esc_html( $pop3->ERROR ) );
  32. if( 0 === $count ) {
  33. $pop3->quit();
  34. wp_die( __('There doesn&#8217;t seem to be any new mail.') );
  35. }
  36. for ( $i = 1; $i <= $count; $i++ ) {
  37. $message = $pop3->get($i);
  38. $bodysignal = false;
  39. $boundary = '';
  40. $charset = '';
  41. $content = '';
  42. $content_type = '';
  43. $content_transfer_encoding = '';
  44. $post_author = 1;
  45. $author_found = false;
  46. $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  47. foreach ($message as $line) {
  48. // body signal
  49. if ( strlen($line) < 3 )
  50. $bodysignal = true;
  51. if ( $bodysignal ) {
  52. $content .= $line;
  53. } else {
  54. if ( preg_match('/Content-Type: /i', $line) ) {
  55. $content_type = trim($line);
  56. $content_type = substr($content_type, 14, strlen($content_type) - 14);
  57. $content_type = explode(';', $content_type);
  58. if ( ! empty( $content_type[1] ) ) {
  59. $charset = explode('=', $content_type[1]);
  60. $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
  61. }
  62. $content_type = $content_type[0];
  63. }
  64. if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
  65. $content_transfer_encoding = trim($line);
  66. $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
  67. $content_transfer_encoding = explode(';', $content_transfer_encoding);
  68. $content_transfer_encoding = $content_transfer_encoding[0];
  69. }
  70. if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
  71. $boundary = trim($line);
  72. $boundary = explode('"', $boundary);
  73. $boundary = $boundary[1];
  74. }
  75. if (preg_match('/Subject: /i', $line)) {
  76. $subject = trim($line);
  77. $subject = substr($subject, 9, strlen($subject) - 9);
  78. // Captures any text in the subject before $phone_delim as the subject
  79. if ( function_exists('iconv_mime_decode') ) {
  80. $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
  81. } else {
  82. $subject = wp_iso_descrambler($subject);
  83. }
  84. $subject = explode($phone_delim, $subject);
  85. $subject = $subject[0];
  86. }
  87. // Set the author using the email address (From or Reply-To, the last used)
  88. // otherwise use the site admin
  89. if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
  90. if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
  91. $author = $matches[0];
  92. else
  93. $author = trim($line);
  94. $author = sanitize_email($author);
  95. if ( is_email($author) ) {
  96. echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
  97. $userdata = get_user_by('email', $author);
  98. if ( ! empty( $userdata ) ) {
  99. $post_author = $userdata->ID;
  100. $author_found = true;
  101. }
  102. }
  103. }
  104. if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
  105. $ddate = trim($line);
  106. $ddate = str_replace('Date: ', '', $ddate);
  107. if (strpos($ddate, ',')) {
  108. $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
  109. }
  110. $date_arr = explode(' ', $ddate);
  111. $date_time = explode(':', $date_arr[3]);
  112. $ddate_H = $date_time[0];
  113. $ddate_i = $date_time[1];
  114. $ddate_s = $date_time[2];
  115. $ddate_m = $date_arr[1];
  116. $ddate_d = $date_arr[0];
  117. $ddate_Y = $date_arr[2];
  118. for ( $j = 0; $j < 12; $j++ ) {
  119. if ( $ddate_m == $dmonths[$j] ) {
  120. $ddate_m = $j+1;
  121. }
  122. }
  123. $time_zn = intval($date_arr[4]) * 36;
  124. $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
  125. $ddate_U = $ddate_U - $time_zn;
  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. //Give Post-By-Email extending plugins full access to the content
  152. //Either the raw content or the content of the last quoted-printable section
  153. $content = apply_filters('wp_mail_original_content', $content);
  154. if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
  155. $content = quoted_printable_decode($content);
  156. }
  157. if ( function_exists('iconv') && ! empty( $charset ) ) {
  158. $content = iconv($charset, get_option('blog_charset'), $content);
  159. }
  160. // Captures any text in the body after $phone_delim as the body
  161. $content = explode($phone_delim, $content);
  162. $content = empty( $content[1] ) ? $content[0] : $content[1];
  163. $content = trim($content);
  164. $post_content = apply_filters('phone_content', $content);
  165. $post_title = xmlrpc_getposttitle($content);
  166. if ($post_title == '') $post_title = $subject;
  167. $post_category = array(get_option('default_email_category'));
  168. $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
  169. $post_data = add_magic_quotes($post_data);
  170. $post_ID = wp_insert_post($post_data);
  171. if ( is_wp_error( $post_ID ) )
  172. echo "\n" . $post_ID->get_error_message();
  173. // We couldn't post, for whatever reason. Better move forward to the next email.
  174. if ( empty( $post_ID ) )
  175. continue;
  176. do_action('publish_phone', $post_ID);
  177. echo "\n<p>" . sprintf(__('<strong>Author:</strong> %s'), esc_html($post_author)) . '</p>';
  178. echo "\n<p>" . sprintf(__('<strong>Posted title:</strong> %s'), esc_html($post_title)) . '</p>';
  179. if(!$pop3->delete($i)) {
  180. echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>';
  181. $pop3->reset();
  182. exit;
  183. } else {
  184. echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';
  185. }
  186. }
  187. $pop3->quit();