[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> wp-mail.php (source)

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


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7