[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref