wp-mail.php 8.21 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. */
samba's avatar
samba committed
11
require( dirname( __FILE__ ) . '/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 29
/**
 * Fires to allow a plugin to do a complete takeover of Post by Email.
 *
 * @since 2.9.0
 */
do_action( 'wp-mail.php' );
godog's avatar
godog committed
30 31 32 33 34

/** Get the POP3 class with which to access the mailbox. */
require_once( ABSPATH . WPINC . '/class-pop3.php' );

/** Only check at this interval for new messages. */
samba's avatar
samba committed
35 36 37
if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
	define( 'WP_MAIL_INTERVAL', 300 ); // 5 minutes
}
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];
			}
samba's avatar
samba committed
104 105 106
			if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' == $boundary ) ) {
				$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 );
godog's avatar
godog 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 ) ) {
lucha's avatar
lucha committed
134
					/* translators: 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;
					}
				}
			}

lechuck's avatar
lechuck committed
144
			if ( preg_match( '/Date: /i', $line ) ) { // of the form '20 Mar 2002 20:32:37 +0100'
samba's avatar
samba committed
145 146 147 148
				$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 );
lechuck's avatar
lechuck committed
149
				$post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
godog's avatar
godog committed
150 151 152 153 154 155
			}
		}
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

samba's avatar
samba committed
221 222
	$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
223

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

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

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

lechuck's avatar
lechuck committed
243 244
	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
245

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

$pop3->quit();