2013-10-24 18:58:23 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Gets the email message from the user's mailbox to add as
|
|
|
|
* a WordPress post. Mailbox connection information must be
|
|
|
|
* configured under Settings > Writing
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Make sure that the WordPress bootstrap has run before continuing. */
|
2017-11-30 18:11:00 -05:00
|
|
|
require( dirname( __FILE__ ) . '/wp-load.php' );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2013-10-24 18:59:20 -04:00
|
|
|
/** This filter is documented in wp-admin/options.php */
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! apply_filters( 'enable_post_by_email_configuration', true ) ) {
|
2016-08-23 10:02:39 -04:00
|
|
|
wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-01-11 00:13:43 -05:00
|
|
|
$mailserver_url = get_option( 'mailserver_url' );
|
|
|
|
|
|
|
|
if ( 'mail.example.com' === $mailserver_url || empty( $mailserver_url ) ) {
|
|
|
|
wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
|
|
|
|
}
|
|
|
|
|
2013-11-13 06:58:12 -05:00
|
|
|
/**
|
|
|
|
* Fires to allow a plugin to do a complete takeover of Post by Email.
|
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*/
|
2019-07-04 21:45:56 -04:00
|
|
|
do_action( 'wp-mail.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2016-08-31 12:31:29 -04:00
|
|
|
/** Get the POP3 class with which to access the mailbox. */
|
|
|
|
require_once( ABSPATH . WPINC . '/class-pop3.php' );
|
|
|
|
|
2013-10-24 18:58:23 -04:00
|
|
|
/** Only check at this interval for new messages. */
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
|
|
|
|
define( 'WP_MAIL_INTERVAL', 300 ); // 5 minutes
|
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$last_checked = get_transient( 'mailserver_last_checked' );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $last_checked ) {
|
|
|
|
wp_die( __( 'Slow down cowboy, no need to check for new mails so often!' ) );
|
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
|
2013-10-24 18:58:23 -04:00
|
|
|
|
|
|
|
$phone_delim = '::';
|
|
|
|
|
|
|
|
$pop3 = new POP3();
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $pop3->connect( get_option( 'mailserver_url' ), get_option( 'mailserver_port' ) ) || ! $pop3->user( get_option( 'mailserver_login' ) ) ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
wp_die( esc_html( $pop3->ERROR ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$count = $pop3->pass( get_option( 'mailserver_pass' ) );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( false === $count ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
wp_die( esc_html( $pop3->ERROR ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( 0 === $count ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
$pop3->quit();
|
2017-11-30 18:11:00 -05:00
|
|
|
wp_die( __( 'There doesn’t seem to be any new mail.' ) );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( $i = 1; $i <= $count; $i++ ) {
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$message = $pop3->get( $i );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$bodysignal = false;
|
|
|
|
$boundary = '';
|
|
|
|
$charset = '';
|
|
|
|
$content = '';
|
|
|
|
$content_type = '';
|
2013-10-24 18:58:23 -04:00
|
|
|
$content_transfer_encoding = '';
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_author = 1;
|
|
|
|
$author_found = false;
|
|
|
|
foreach ( $message as $line ) {
|
2014-07-17 05:12:16 -04:00
|
|
|
// Body signal.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( strlen( $line ) < 3 ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
$bodysignal = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
if ( $bodysignal ) {
|
|
|
|
$content .= $line;
|
|
|
|
} else {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( preg_match( '/Content-Type: /i', $line ) ) {
|
|
|
|
$content_type = trim( $line );
|
|
|
|
$content_type = substr( $content_type, 14, strlen( $content_type ) - 14 );
|
|
|
|
$content_type = explode( ';', $content_type );
|
2013-10-24 18:58:23 -04:00
|
|
|
if ( ! empty( $content_type[1] ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$charset = explode( '=', $content_type[1] );
|
|
|
|
$charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : '';
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
$content_type = $content_type[0];
|
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( preg_match( '/Content-Transfer-Encoding: /i', $line ) ) {
|
|
|
|
$content_transfer_encoding = trim( $line );
|
|
|
|
$content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
|
|
|
|
$content_transfer_encoding = explode( ';', $content_transfer_encoding );
|
2013-10-24 18:58:23 -04:00
|
|
|
$content_transfer_encoding = $content_transfer_encoding[0];
|
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' == $boundary ) ) {
|
|
|
|
$boundary = trim( $line );
|
|
|
|
$boundary = explode( '"', $boundary );
|
2013-10-24 18:58:23 -04:00
|
|
|
$boundary = $boundary[1];
|
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( preg_match( '/Subject: /i', $line ) ) {
|
|
|
|
$subject = trim( $line );
|
|
|
|
$subject = substr( $subject, 9, strlen( $subject ) - 9 );
|
2013-10-24 18:58:23 -04:00
|
|
|
// Captures any text in the subject before $phone_delim as the subject
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'iconv_mime_decode' ) ) {
|
|
|
|
$subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
|
2013-10-24 18:58:23 -04:00
|
|
|
} else {
|
2017-11-30 18:11:00 -05:00
|
|
|
$subject = wp_iso_descrambler( $subject );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
$subject = explode( $phone_delim, $subject );
|
2013-10-24 18:58:23 -04:00
|
|
|
$subject = $subject[0];
|
|
|
|
}
|
|
|
|
|
2014-07-17 05:12:16 -04:00
|
|
|
/*
|
|
|
|
* Set the author using the email address (From or Reply-To, the last used)
|
|
|
|
* otherwise use the site admin.
|
|
|
|
*/
|
2013-10-24 18:58:23 -04:00
|
|
|
if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches ) ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
$author = $matches[0];
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
|
|
|
$author = trim( $line );
|
|
|
|
}
|
|
|
|
$author = sanitize_email( $author );
|
|
|
|
if ( is_email( $author ) ) {
|
2016-11-20 21:46:30 -05:00
|
|
|
/* translators: Post author email address */
|
2017-11-30 18:11:00 -05:00
|
|
|
echo '<p>' . sprintf( __( 'Author is %s' ), $author ) . '</p>';
|
|
|
|
$userdata = get_user_by( 'email', $author );
|
2013-10-24 18:58:23 -04:00
|
|
|
if ( ! empty( $userdata ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_author = $userdata->ID;
|
2013-10-24 18:58:23 -04:00
|
|
|
$author_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 10:07:24 -04:00
|
|
|
if ( preg_match( '/Date: /i', $line ) ) { // of the form '20 Mar 2002 20:32:37 +0100'
|
2017-11-30 18:11:00 -05:00
|
|
|
$ddate = str_replace( 'Date: ', '', trim( $line ) );
|
|
|
|
$ddate = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate ); // remove parenthesised timezone string if it exists, as this confuses strtotime
|
|
|
|
$ddate_U = strtotime( $ddate );
|
|
|
|
$post_date = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
|
2015-10-06 10:07:24 -04:00
|
|
|
$post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set $post_status based on $author_found and on author's publish_posts capability
|
|
|
|
if ( $author_found ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$user = new WP_User( $post_author );
|
|
|
|
$post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
|
2013-10-24 18:58:23 -04:00
|
|
|
} else {
|
|
|
|
// Author not found in DB, set status to pending. Author already set to admin.
|
|
|
|
$post_status = 'pending';
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$subject = trim( $subject );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
|
|
|
if ( $content_type == 'multipart/alternative' ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$content = explode( '--' . $boundary, $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
$content = $content[2];
|
2014-07-17 05:12:16 -04:00
|
|
|
|
|
|
|
// Match case-insensitive content-transfer-encoding.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
|
|
|
|
$content = explode( $delim[0], $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
$content = $content[1];
|
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
$content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
$content = trim( $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2013-11-13 06:58:12 -05:00
|
|
|
/**
|
2016-05-23 12:44:27 -04:00
|
|
|
* Filters the original content of the email.
|
2013-11-13 06:58:12 -05:00
|
|
|
*
|
|
|
|
* Give Post-By-Email extending plugins full access to the content, either
|
|
|
|
* the raw content, or the content of the last quoted-printable section.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param string $content The original email content.
|
|
|
|
*/
|
|
|
|
$content = apply_filters( 'wp_mail_original_content', $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) {
|
|
|
|
$content = quoted_printable_decode( $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
|
|
|
|
$content = iconv( $charset, get_option( 'blog_charset' ), $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Captures any text in the body after $phone_delim as the body
|
2017-11-30 18:11:00 -05:00
|
|
|
$content = explode( $phone_delim, $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
$content = empty( $content[1] ) ? $content[0] : $content[1];
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$content = trim( $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2013-11-13 06:58:12 -05:00
|
|
|
/**
|
2016-05-23 12:44:27 -04:00
|
|
|
* Filters the content of the post submitted by email before saving.
|
2013-11-13 06:58:12 -05:00
|
|
|
*
|
|
|
|
* @since 1.2.0
|
|
|
|
*
|
|
|
|
* @param string $content The email content.
|
|
|
|
*/
|
|
|
|
$post_content = apply_filters( 'phone_content', $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_title = xmlrpc_getposttitle( $content );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $post_title == '' ) {
|
|
|
|
$post_title = $subject;
|
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_category = array( get_option( 'default_email_category' ) );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
|
|
|
|
$post_data = wp_slash( $post_data );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$post_ID = wp_insert_post( $post_data );
|
|
|
|
if ( is_wp_error( $post_ID ) ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
echo "\n" . $post_ID->get_error_message();
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
|
|
|
// We couldn't post, for whatever reason. Better move forward to the next email.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $post_ID ) ) {
|
2013-10-24 18:58:23 -04:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2013-11-13 06:58:12 -05:00
|
|
|
/**
|
|
|
|
* Fires after a post submitted by email is published.
|
|
|
|
*
|
|
|
|
* @since 1.2.0
|
|
|
|
*
|
|
|
|
* @param int $post_ID The post ID.
|
|
|
|
*/
|
|
|
|
do_action( 'publish_phone', $post_ID );
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2015-09-19 22:53:25 -04:00
|
|
|
echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
|
|
|
|
echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
|
2013-10-24 18:58:23 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $pop3->delete( $i ) ) {
|
2015-09-19 22:53:25 -04:00
|
|
|
echo '<p>' . sprintf(
|
|
|
|
/* translators: %s: POP3 error */
|
|
|
|
__( 'Oops: %s' ),
|
|
|
|
esc_html( $pop3->ERROR )
|
|
|
|
) . '</p>';
|
2013-10-24 18:58:23 -04:00
|
|
|
$pop3->reset();
|
|
|
|
exit;
|
|
|
|
} else {
|
2015-09-19 22:53:25 -04:00
|
|
|
echo '<p>' . sprintf(
|
|
|
|
/* translators: %s: the message ID */
|
|
|
|
__( 'Mission complete. Message %s deleted.' ),
|
|
|
|
'<strong>' . $i . '</strong>'
|
|
|
|
) . '</p>';
|
2013-10-24 18:58:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pop3->quit();
|