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