wp-mail.php 8.31 KB
Newer Older
godog's avatar
godog committed
1 2 3 4 5 6 7 8 9 10
<?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. */
agata's avatar
agata committed
11
require __DIR__ . '/wp-load.php';
godog's avatar
godog committed
12

lucha's avatar
lucha committed
13
/** This filter is documented in wp-admin/options.php */
samba's avatar
samba committed
14
if ( ! apply_filters( 'enable_post_by_email_configuration', true ) ) {
lucha's avatar
lucha committed
15
	wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
samba's avatar
samba committed
16
}
godog's avatar
godog committed
17

lucha's avatar
lucha committed
18 19 20 21 22 23
$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 );
}

lucha's avatar
lucha committed
24 25 26 27 28
/**
 * Fires to allow a plugin to do a complete takeover of Post by Email.
 *
 * @since 2.9.0
 */
agata's avatar
agata committed
29
do_action( 'wp-mail.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
godog's avatar
godog committed
30 31

/** Get the POP3 class with which to access the mailbox. */
agata's avatar
agata committed
32
require_once ABSPATH . WPINC . '/class-pop3.php';
godog's avatar
godog committed
33 34

/** Only check at this interval for new messages. */
samba's avatar
samba committed
35
if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
agata's avatar
agata committed
36
	define( 'WP_MAIL_INTERVAL', 5 * MINUTE_IN_SECONDS );
samba's avatar
samba committed
37
}
godog's avatar
godog committed
38

samba's avatar
samba committed
39
$last_checked = get_transient( 'mailserver_last_checked' );
godog's avatar
godog committed
40

samba's avatar
samba committed
41 42 43
if ( $last_checked ) {
	wp_die( __( 'Slow down cowboy, no need to check for new mails so often!' ) );
}
godog's avatar
godog committed
44

samba's avatar
samba committed
45
set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
godog's avatar
godog committed
46

samba's avatar
samba committed
47
$time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
godog's avatar
godog committed
48 49 50 51 52

$phone_delim = '::';

$pop3 = new POP3();

samba's avatar
samba committed
53
if ( ! $pop3->connect( get_option( 'mailserver_url' ), get_option( 'mailserver_port' ) ) || ! $pop3->user( get_option( 'mailserver_login' ) ) ) {
godog's avatar
godog committed
54
	wp_die( esc_html( $pop3->ERROR ) );
samba's avatar
samba committed
55
}
godog's avatar
godog committed
56

samba's avatar
samba committed
57
$count = $pop3->pass( get_option( 'mailserver_pass' ) );
godog's avatar
godog committed
58

samba's avatar
samba committed
59
if ( false === $count ) {
godog's avatar
godog committed
60
	wp_die( esc_html( $pop3->ERROR ) );
samba's avatar
samba committed
61
}
godog's avatar
godog committed
62

samba's avatar
samba committed
63
if ( 0 === $count ) {
godog's avatar
godog committed
64
	$pop3->quit();
samba's avatar
samba committed
65
	wp_die( __( 'There doesn&#8217;t seem to be any new mail.' ) );
godog's avatar
godog committed
66 67 68 69
}

for ( $i = 1; $i <= $count; $i++ ) {

samba's avatar
samba committed
70
	$message = $pop3->get( $i );
godog's avatar
godog committed
71

samba's avatar
samba committed
72 73 74 75 76
	$bodysignal                = false;
	$boundary                  = '';
	$charset                   = '';
	$content                   = '';
	$content_type              = '';
godog's avatar
godog committed
77
	$content_transfer_encoding = '';
samba's avatar
samba committed
78 79 80
	$post_author               = 1;
	$author_found              = false;
	foreach ( $message as $line ) {
lucha's avatar
lucha committed
81
		// Body signal.
samba's avatar
samba committed
82
		if ( strlen( $line ) < 3 ) {
godog's avatar
godog committed
83
			$bodysignal = true;
samba's avatar
samba committed
84
		}
godog's avatar
godog committed
85 86 87
		if ( $bodysignal ) {
			$content .= $line;
		} else {
samba's avatar
samba committed
88 89 90 91
			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 );
godog's avatar
godog committed
92
				if ( ! empty( $content_type[1] ) ) {
samba's avatar
samba committed
93 94
					$charset = explode( '=', $content_type[1] );
					$charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : '';
godog's avatar
godog committed
95 96 97
				}
				$content_type = $content_type[0];
			}
samba's avatar
samba committed
98 99 100 101
			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 );
godog's avatar
godog committed
102 103
				$content_transfer_encoding = $content_transfer_encoding[0];
			}
agata's avatar
agata committed
104
			if ( ( 'multipart/alternative' === $content_type ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' === $boundary ) ) {
samba's avatar
samba committed
105 106
				$boundary = trim( $line );
				$boundary = explode( '"', $boundary );
godog's avatar
godog committed
107 108
				$boundary = $boundary[1];
			}
samba's avatar
samba committed
109 110 111
			if ( preg_match( '/Subject: /i', $line ) ) {
				$subject = trim( $line );
				$subject = substr( $subject, 9, strlen( $subject ) - 9 );
agata's avatar
agata committed
112
				// Captures any text in the subject before $phone_delim as the subject.
samba's avatar
samba committed
113 114
				if ( function_exists( 'iconv_mime_decode' ) ) {
					$subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
godog's avatar
godog committed
115
				} else {
samba's avatar
samba committed
116
					$subject = wp_iso_descrambler( $subject );
godog's avatar
godog committed
117
				}
samba's avatar
samba committed
118
				$subject = explode( $phone_delim, $subject );
godog's avatar
godog committed
119 120 121
				$subject = $subject[0];
			}

lucha's avatar
lucha committed
122 123 124 125
			/*
			 * Set the author using the email address (From or Reply-To, the last used)
			 * otherwise use the site admin.
			 */
lechuck's avatar
lechuck committed
126
			if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
samba's avatar
samba committed
127
				if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches ) ) {
godog's avatar
godog committed
128
					$author = $matches[0];
samba's avatar
samba committed
129 130 131 132 133
				} else {
					$author = trim( $line );
				}
				$author = sanitize_email( $author );
				if ( is_email( $author ) ) {
agata's avatar
agata committed
134
					/* translators: %s: Post author email address. */
samba's avatar
samba committed
135 136
					echo '<p>' . sprintf( __( 'Author is %s' ), $author ) . '</p>';
					$userdata = get_user_by( 'email', $author );
lechuck's avatar
lechuck committed
137
					if ( ! empty( $userdata ) ) {
samba's avatar
samba committed
138
						$post_author  = $userdata->ID;
godog's avatar
godog committed
139 140 141 142 143
						$author_found = true;
					}
				}
			}

agata's avatar
agata committed
144 145 146 147 148 149 150
			if ( preg_match( '/Date: /i', $line ) ) { // Of the form '20 Mar 2002 20:32:37 +0100'.
				$ddate = str_replace( 'Date: ', '', trim( $line ) );
				// Remove parenthesised timezone string if it exists, as this confuses strtotime().
				$ddate           = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate );
				$ddate_timestamp = strtotime( $ddate );
				$post_date       = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference );
				$post_date_gmt   = gmdate( 'Y-m-d H:i:s', $ddate_timestamp );
godog's avatar
godog committed
151 152 153 154
			}
		}
	}

agata's avatar
agata committed
155
	// Set $post_status based on $author_found and on author's publish_posts capability.
godog's avatar
godog committed
156
	if ( $author_found ) {
samba's avatar
samba committed
157 158
		$user        = new WP_User( $post_author );
		$post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
godog's avatar
godog committed
159
	} else {
lechuck's avatar
lechuck committed
160
		// Author not found in DB, set status to pending. Author already set to admin.
godog's avatar
godog committed
161 162 163
		$post_status = 'pending';
	}

samba's avatar
samba committed
164
	$subject = trim( $subject );
godog's avatar
godog committed
165

agata's avatar
agata committed
166
	if ( 'multipart/alternative' === $content_type ) {
samba's avatar
samba committed
167
		$content = explode( '--' . $boundary, $content );
godog's avatar
godog committed
168
		$content = $content[2];
lucha's avatar
lucha committed
169 170

		// Match case-insensitive content-transfer-encoding.
samba's avatar
samba committed
171 172
		if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
			$content = explode( $delim[0], $content );
godog's avatar
godog committed
173 174
			$content = $content[1];
		}
samba's avatar
samba committed
175
		$content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
godog's avatar
godog committed
176
	}
samba's avatar
samba committed
177
	$content = trim( $content );
godog's avatar
godog committed
178

lucha's avatar
lucha committed
179
	/**
lucha's avatar
lucha committed
180
	 * Filters the original content of the email.
lucha's avatar
lucha committed
181 182 183 184 185 186 187 188 189
	 *
	 * 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 );
godog's avatar
godog committed
190

samba's avatar
samba committed
191 192
	if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) {
		$content = quoted_printable_decode( $content );
godog's avatar
godog committed
193 194
	}

samba's avatar
samba committed
195 196
	if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
		$content = iconv( $charset, get_option( 'blog_charset' ), $content );
godog's avatar
godog committed
197 198
	}

agata's avatar
agata committed
199
	// Captures any text in the body after $phone_delim as the body.
samba's avatar
samba committed
200
	$content = explode( $phone_delim, $content );
godog's avatar
godog committed
201 202
	$content = empty( $content[1] ) ? $content[0] : $content[1];

samba's avatar
samba committed
203
	$content = trim( $content );
godog's avatar
godog committed
204

lucha's avatar
lucha committed
205
	/**
lucha's avatar
lucha committed
206
	 * Filters the content of the post submitted by email before saving.
lucha's avatar
lucha committed
207 208 209 210 211 212
	 *
	 * @since 1.2.0
	 *
	 * @param string $content The email content.
	 */
	$post_content = apply_filters( 'phone_content', $content );
godog's avatar
godog committed
213

samba's avatar
samba committed
214
	$post_title = xmlrpc_getposttitle( $content );
godog's avatar
godog committed
215

agata's avatar
agata committed
216
	if ( '' === trim( $post_title ) ) {
samba's avatar
samba committed
217 218
		$post_title = $subject;
	}
godog's avatar
godog committed
219

samba's avatar
samba committed
220
	$post_category = array( get_option( 'default_email_category' ) );
godog's avatar
godog committed
221

samba's avatar
samba committed
222 223
	$post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
	$post_data = wp_slash( $post_data );
godog's avatar
godog committed
224

samba's avatar
samba committed
225 226
	$post_ID = wp_insert_post( $post_data );
	if ( is_wp_error( $post_ID ) ) {
godog's avatar
godog committed
227
		echo "\n" . $post_ID->get_error_message();
samba's avatar
samba committed
228
	}
godog's avatar
godog committed
229 230

	// We couldn't post, for whatever reason. Better move forward to the next email.
samba's avatar
samba committed
231
	if ( empty( $post_ID ) ) {
godog's avatar
godog committed
232
		continue;
samba's avatar
samba committed
233
	}
godog's avatar
godog committed
234

lucha's avatar
lucha committed
235 236 237 238 239 240 241 242
	/**
	 * 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 );
godog's avatar
godog committed
243

lechuck's avatar
lechuck committed
244 245
	echo "\n<p><strong>" . __( 'Author:' ) . '</strong> ' . esc_html( $post_author ) . '</p>';
	echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
godog's avatar
godog committed
246

samba's avatar
samba committed
247
	if ( ! $pop3->delete( $i ) ) {
lechuck's avatar
lechuck committed
248
		echo '<p>' . sprintf(
agata's avatar
agata committed
249
			/* translators: %s: POP3 error. */
lechuck's avatar
lechuck committed
250 251 252
			__( 'Oops: %s' ),
			esc_html( $pop3->ERROR )
		) . '</p>';
godog's avatar
godog committed
253 254 255
		$pop3->reset();
		exit;
	} else {
lechuck's avatar
lechuck committed
256
		echo '<p>' . sprintf(
agata's avatar
agata committed
257
			/* translators: %s: The message ID. */
lechuck's avatar
lechuck committed
258 259 260
			__( 'Mission complete. Message %s deleted.' ),
			'<strong>' . $i . '</strong>'
		) . '</p>';
godog's avatar
godog committed
261 262 263 264
	}
}

$pop3->quit();