wp/wp-mail.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 19 3d72ae0968f4
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  */
     8  */
     9 
     9 
    10 /** Make sure that the WordPress bootstrap has run before continuing. */
    10 /** Make sure that the WordPress bootstrap has run before continuing. */
    11 require( dirname( __FILE__ ) . '/wp-load.php' );
    11 require __DIR__ . '/wp-load.php';
    12 
    12 
    13 /** This filter is documented in wp-admin/options.php */
    13 /** This filter is documented in wp-admin/options.php */
    14 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) ) {
    14 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) ) {
    15 	wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
    15 	wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
    16 }
    16 }
    24 /**
    24 /**
    25  * Fires to allow a plugin to do a complete takeover of Post by Email.
    25  * Fires to allow a plugin to do a complete takeover of Post by Email.
    26  *
    26  *
    27  * @since 2.9.0
    27  * @since 2.9.0
    28  */
    28  */
    29 do_action( 'wp-mail.php' );
    29 do_action( 'wp-mail.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
    30 
    30 
    31 /** Get the POP3 class with which to access the mailbox. */
    31 /** Get the POP3 class with which to access the mailbox. */
    32 require_once( ABSPATH . WPINC . '/class-pop3.php' );
    32 require_once ABSPATH . WPINC . '/class-pop3.php';
    33 
    33 
    34 /** Only check at this interval for new messages. */
    34 /** Only check at this interval for new messages. */
    35 if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
    35 if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
    36 	define( 'WP_MAIL_INTERVAL', 300 ); // 5 minutes
    36 	define( 'WP_MAIL_INTERVAL', 5 * MINUTE_IN_SECONDS );
    37 }
    37 }
    38 
    38 
    39 $last_checked = get_transient( 'mailserver_last_checked' );
    39 $last_checked = get_transient( 'mailserver_last_checked' );
    40 
    40 
    41 if ( $last_checked ) {
    41 if ( $last_checked ) {
    99 				$content_transfer_encoding = trim( $line );
    99 				$content_transfer_encoding = trim( $line );
   100 				$content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
   100 				$content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
   101 				$content_transfer_encoding = explode( ';', $content_transfer_encoding );
   101 				$content_transfer_encoding = explode( ';', $content_transfer_encoding );
   102 				$content_transfer_encoding = $content_transfer_encoding[0];
   102 				$content_transfer_encoding = $content_transfer_encoding[0];
   103 			}
   103 			}
   104 			if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' == $boundary ) ) {
   104 			if ( ( 'multipart/alternative' === $content_type ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' === $boundary ) ) {
   105 				$boundary = trim( $line );
   105 				$boundary = trim( $line );
   106 				$boundary = explode( '"', $boundary );
   106 				$boundary = explode( '"', $boundary );
   107 				$boundary = $boundary[1];
   107 				$boundary = $boundary[1];
   108 			}
   108 			}
   109 			if ( preg_match( '/Subject: /i', $line ) ) {
   109 			if ( preg_match( '/Subject: /i', $line ) ) {
   110 				$subject = trim( $line );
   110 				$subject = trim( $line );
   111 				$subject = substr( $subject, 9, strlen( $subject ) - 9 );
   111 				$subject = substr( $subject, 9, strlen( $subject ) - 9 );
   112 				// Captures any text in the subject before $phone_delim as the subject
   112 				// Captures any text in the subject before $phone_delim as the subject.
   113 				if ( function_exists( 'iconv_mime_decode' ) ) {
   113 				if ( function_exists( 'iconv_mime_decode' ) ) {
   114 					$subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
   114 					$subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
   115 				} else {
   115 				} else {
   116 					$subject = wp_iso_descrambler( $subject );
   116 					$subject = wp_iso_descrambler( $subject );
   117 				}
   117 				}
   129 				} else {
   129 				} else {
   130 					$author = trim( $line );
   130 					$author = trim( $line );
   131 				}
   131 				}
   132 				$author = sanitize_email( $author );
   132 				$author = sanitize_email( $author );
   133 				if ( is_email( $author ) ) {
   133 				if ( is_email( $author ) ) {
   134 					/* translators: Post author email address */
   134 					/* translators: %s: Post author email address. */
   135 					echo '<p>' . sprintf( __( 'Author is %s' ), $author ) . '</p>';
   135 					echo '<p>' . sprintf( __( 'Author is %s' ), $author ) . '</p>';
   136 					$userdata = get_user_by( 'email', $author );
   136 					$userdata = get_user_by( 'email', $author );
   137 					if ( ! empty( $userdata ) ) {
   137 					if ( ! empty( $userdata ) ) {
   138 						$post_author  = $userdata->ID;
   138 						$post_author  = $userdata->ID;
   139 						$author_found = true;
   139 						$author_found = true;
   140 					}
   140 					}
   141 				}
   141 				}
   142 			}
   142 			}
   143 
   143 
   144 			if ( preg_match( '/Date: /i', $line ) ) { // of the form '20 Mar 2002 20:32:37 +0100'
   144 			if ( preg_match( '/Date: /i', $line ) ) { // Of the form '20 Mar 2002 20:32:37 +0100'.
   145 				$ddate         = str_replace( 'Date: ', '', trim( $line ) );
   145 				$ddate = str_replace( 'Date: ', '', trim( $line ) );
   146 				$ddate         = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate ); // remove parenthesised timezone string if it exists, as this confuses strtotime
   146 				// Remove parenthesised timezone string if it exists, as this confuses strtotime().
   147 				$ddate_U       = strtotime( $ddate );
   147 				$ddate           = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );
   148 				$post_date     = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
   148 				$ddate_timestamp = strtotime( $ddate );
   149 				$post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
   149 				$post_date       = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference );
       
   150 				$post_date_gmt   = gmdate( 'Y-m-d H:i:s', $ddate_timestamp );
   150 			}
   151 			}
   151 		}
   152 		}
   152 	}
   153 	}
   153 
   154 
   154 	// Set $post_status based on $author_found and on author's publish_posts capability
   155 	// Set $post_status based on $author_found and on author's publish_posts capability.
   155 	if ( $author_found ) {
   156 	if ( $author_found ) {
   156 		$user        = new WP_User( $post_author );
   157 		$user        = new WP_User( $post_author );
   157 		$post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
   158 		$post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
   158 	} else {
   159 	} else {
   159 		// Author not found in DB, set status to pending. Author already set to admin.
   160 		// Author not found in DB, set status to pending. Author already set to admin.
   160 		$post_status = 'pending';
   161 		$post_status = 'pending';
   161 	}
   162 	}
   162 
   163 
   163 	$subject = trim( $subject );
   164 	$subject = trim( $subject );
   164 
   165 
   165 	if ( $content_type == 'multipart/alternative' ) {
   166 	if ( 'multipart/alternative' === $content_type ) {
   166 		$content = explode( '--' . $boundary, $content );
   167 		$content = explode( '--' . $boundary, $content );
   167 		$content = $content[2];
   168 		$content = $content[2];
   168 
   169 
   169 		// Match case-insensitive content-transfer-encoding.
   170 		// Match case-insensitive content-transfer-encoding.
   170 		if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
   171 		if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
   193 
   194 
   194 	if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
   195 	if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
   195 		$content = iconv( $charset, get_option( 'blog_charset' ), $content );
   196 		$content = iconv( $charset, get_option( 'blog_charset' ), $content );
   196 	}
   197 	}
   197 
   198 
   198 	// Captures any text in the body after $phone_delim as the body
   199 	// Captures any text in the body after $phone_delim as the body.
   199 	$content = explode( $phone_delim, $content );
   200 	$content = explode( $phone_delim, $content );
   200 	$content = empty( $content[1] ) ? $content[0] : $content[1];
   201 	$content = empty( $content[1] ) ? $content[0] : $content[1];
   201 
   202 
   202 	$content = trim( $content );
   203 	$content = trim( $content );
   203 
   204 
   210 	 */
   211 	 */
   211 	$post_content = apply_filters( 'phone_content', $content );
   212 	$post_content = apply_filters( 'phone_content', $content );
   212 
   213 
   213 	$post_title = xmlrpc_getposttitle( $content );
   214 	$post_title = xmlrpc_getposttitle( $content );
   214 
   215 
   215 	if ( $post_title == '' ) {
   216 	if ( '' === trim( $post_title ) ) {
   216 		$post_title = $subject;
   217 		$post_title = $subject;
   217 	}
   218 	}
   218 
   219 
   219 	$post_category = array( get_option( 'default_email_category' ) );
   220 	$post_category = array( get_option( 'default_email_category' ) );
   220 
   221 
   243 	echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
   244 	echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
   244 	echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
   245 	echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
   245 
   246 
   246 	if ( ! $pop3->delete( $i ) ) {
   247 	if ( ! $pop3->delete( $i ) ) {
   247 		echo '<p>' . sprintf(
   248 		echo '<p>' . sprintf(
   248 			/* translators: %s: POP3 error */
   249 			/* translators: %s: POP3 error. */
   249 			__( 'Oops: %s' ),
   250 			__( 'Oops: %s' ),
   250 			esc_html( $pop3->ERROR )
   251 			esc_html( $pop3->ERROR )
   251 		) . '</p>';
   252 		) . '</p>';
   252 		$pop3->reset();
   253 		$pop3->reset();
   253 		exit;
   254 		exit;
   254 	} else {
   255 	} else {
   255 		echo '<p>' . sprintf(
   256 		echo '<p>' . sprintf(
   256 			/* translators: %s: the message ID */
   257 			/* translators: %s: The message ID. */
   257 			__( 'Mission complete. Message %s deleted.' ),
   258 			__( 'Mission complete. Message %s deleted.' ),
   258 			'<strong>' . $i . '</strong>'
   259 			'<strong>' . $i . '</strong>'
   259 		) . '</p>';
   260 		) . '</p>';
   260 	}
   261 	}
   261 }
   262 }