author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
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. */ |
|
16 | 11 |
require __DIR__ . '/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 |
*/ |
|
16 | 29 |
do_action( 'wp-mail.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
0 | 30 |
|
31 |
/** Get the POP3 class with which to access the mailbox. */ |
|
16 | 32 |
require_once ABSPATH . WPINC . '/class-pop3.php'; |
0 | 33 |
|
34 |
/** Only check at this interval for new messages. */ |
|
9 | 35 |
if ( ! defined( 'WP_MAIL_INTERVAL' ) ) { |
16 | 36 |
define( 'WP_MAIL_INTERVAL', 5 * MINUTE_IN_SECONDS ); |
9 | 37 |
} |
0 | 38 |
|
9 | 39 |
$last_checked = get_transient( 'mailserver_last_checked' ); |
0 | 40 |
|
9 | 41 |
if ( $last_checked ) { |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
42 |
wp_die( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
43 |
sprintf( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
44 |
// translators: %s human readable rate limit. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
45 |
__( 'Email checks are rate limited to once every %s.' ), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
46 |
human_time_diff( time() - WP_MAIL_INTERVAL, time() ) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
47 |
), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
48 |
__( 'Slow down, no need to check for new mails so often!' ), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
49 |
429 |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
50 |
); |
9 | 51 |
} |
0 | 52 |
|
9 | 53 |
set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL ); |
0 | 54 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
55 |
$time_difference = (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
0 | 56 |
|
57 |
$phone_delim = '::'; |
|
58 |
||
59 |
$pop3 = new POP3(); |
|
60 |
||
9 | 61 |
if ( ! $pop3->connect( get_option( 'mailserver_url' ), get_option( 'mailserver_port' ) ) || ! $pop3->user( get_option( 'mailserver_login' ) ) ) { |
0 | 62 |
wp_die( esc_html( $pop3->ERROR ) ); |
9 | 63 |
} |
0 | 64 |
|
9 | 65 |
$count = $pop3->pass( get_option( 'mailserver_pass' ) ); |
0 | 66 |
|
9 | 67 |
if ( false === $count ) { |
0 | 68 |
wp_die( esc_html( $pop3->ERROR ) ); |
9 | 69 |
} |
0 | 70 |
|
9 | 71 |
if ( 0 === $count ) { |
0 | 72 |
$pop3->quit(); |
19 | 73 |
wp_die( __( 'There does not seem to be any new mail.' ) ); |
0 | 74 |
} |
75 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
// Always run as an unauthenticated user. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
wp_set_current_user( 0 ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
|
0 | 79 |
for ( $i = 1; $i <= $count; $i++ ) { |
80 |
||
9 | 81 |
$message = $pop3->get( $i ); |
0 | 82 |
|
9 | 83 |
$bodysignal = false; |
84 |
$boundary = ''; |
|
85 |
$charset = ''; |
|
86 |
$content = ''; |
|
87 |
$content_type = ''; |
|
0 | 88 |
$content_transfer_encoding = ''; |
9 | 89 |
$post_author = 1; |
90 |
$author_found = false; |
|
19 | 91 |
$post_date = null; |
92 |
$post_date_gmt = null; |
|
93 |
||
9 | 94 |
foreach ( $message as $line ) { |
5 | 95 |
// Body signal. |
9 | 96 |
if ( strlen( $line ) < 3 ) { |
0 | 97 |
$bodysignal = true; |
9 | 98 |
} |
0 | 99 |
if ( $bodysignal ) { |
100 |
$content .= $line; |
|
101 |
} else { |
|
9 | 102 |
if ( preg_match( '/Content-Type: /i', $line ) ) { |
103 |
$content_type = trim( $line ); |
|
104 |
$content_type = substr( $content_type, 14, strlen( $content_type ) - 14 ); |
|
105 |
$content_type = explode( ';', $content_type ); |
|
0 | 106 |
if ( ! empty( $content_type[1] ) ) { |
9 | 107 |
$charset = explode( '=', $content_type[1] ); |
108 |
$charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : ''; |
|
0 | 109 |
} |
110 |
$content_type = $content_type[0]; |
|
111 |
} |
|
9 | 112 |
if ( preg_match( '/Content-Transfer-Encoding: /i', $line ) ) { |
113 |
$content_transfer_encoding = trim( $line ); |
|
114 |
$content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 ); |
|
115 |
$content_transfer_encoding = explode( ';', $content_transfer_encoding ); |
|
0 | 116 |
$content_transfer_encoding = $content_transfer_encoding[0]; |
117 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
if ( 'multipart/alternative' === $content_type && str_contains( $line, 'boundary="' ) && '' === $boundary ) { |
9 | 119 |
$boundary = trim( $line ); |
120 |
$boundary = explode( '"', $boundary ); |
|
0 | 121 |
$boundary = $boundary[1]; |
122 |
} |
|
9 | 123 |
if ( preg_match( '/Subject: /i', $line ) ) { |
124 |
$subject = trim( $line ); |
|
125 |
$subject = substr( $subject, 9, strlen( $subject ) - 9 ); |
|
16 | 126 |
// Captures any text in the subject before $phone_delim as the subject. |
9 | 127 |
if ( function_exists( 'iconv_mime_decode' ) ) { |
128 |
$subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) ); |
|
0 | 129 |
} else { |
9 | 130 |
$subject = wp_iso_descrambler( $subject ); |
0 | 131 |
} |
9 | 132 |
$subject = explode( $phone_delim, $subject ); |
0 | 133 |
$subject = $subject[0]; |
134 |
} |
|
135 |
||
5 | 136 |
/* |
137 |
* Set the author using the email address (From or Reply-To, the last used) |
|
138 |
* otherwise use the site admin. |
|
139 |
*/ |
|
0 | 140 |
if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) { |
9 | 141 |
if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches ) ) { |
0 | 142 |
$author = $matches[0]; |
9 | 143 |
} else { |
144 |
$author = trim( $line ); |
|
145 |
} |
|
146 |
$author = sanitize_email( $author ); |
|
147 |
if ( is_email( $author ) ) { |
|
148 |
$userdata = get_user_by( 'email', $author ); |
|
0 | 149 |
if ( ! empty( $userdata ) ) { |
9 | 150 |
$post_author = $userdata->ID; |
0 | 151 |
$author_found = true; |
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
16 | 156 |
if ( preg_match( '/Date: /i', $line ) ) { // Of the form '20 Mar 2002 20:32:37 +0100'. |
157 |
$ddate = str_replace( 'Date: ', '', trim( $line ) ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
// Remove parenthesized timezone string if it exists, as this confuses strtotime(). |
16 | 159 |
$ddate = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate ); |
160 |
$ddate_timestamp = strtotime( $ddate ); |
|
161 |
$post_date = gmdate( 'Y-m-d H:i:s', $ddate_timestamp + $time_difference ); |
|
162 |
$post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_timestamp ); |
|
0 | 163 |
} |
164 |
} |
|
165 |
} |
|
166 |
||
16 | 167 |
// Set $post_status based on $author_found and on author's publish_posts capability. |
0 | 168 |
if ( $author_found ) { |
9 | 169 |
$user = new WP_User( $post_author ); |
170 |
$post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending'; |
|
0 | 171 |
} else { |
172 |
// Author not found in DB, set status to pending. Author already set to admin. |
|
173 |
$post_status = 'pending'; |
|
174 |
} |
|
175 |
||
9 | 176 |
$subject = trim( $subject ); |
0 | 177 |
|
16 | 178 |
if ( 'multipart/alternative' === $content_type ) { |
9 | 179 |
$content = explode( '--' . $boundary, $content ); |
0 | 180 |
$content = $content[2]; |
5 | 181 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
182 |
// Match case-insensitive Content-Transfer-Encoding. |
9 | 183 |
if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) { |
184 |
$content = explode( $delim[0], $content ); |
|
0 | 185 |
$content = $content[1]; |
186 |
} |
|
9 | 187 |
$content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' ); |
0 | 188 |
} |
9 | 189 |
$content = trim( $content ); |
0 | 190 |
|
5 | 191 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
* Filters the original content of the email. |
5 | 193 |
* |
194 |
* Give Post-By-Email extending plugins full access to the content, either |
|
195 |
* the raw content, or the content of the last quoted-printable section. |
|
196 |
* |
|
197 |
* @since 2.8.0 |
|
198 |
* |
|
199 |
* @param string $content The original email content. |
|
200 |
*/ |
|
201 |
$content = apply_filters( 'wp_mail_original_content', $content ); |
|
0 | 202 |
|
9 | 203 |
if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) { |
204 |
$content = quoted_printable_decode( $content ); |
|
0 | 205 |
} |
206 |
||
9 | 207 |
if ( function_exists( 'iconv' ) && ! empty( $charset ) ) { |
208 |
$content = iconv( $charset, get_option( 'blog_charset' ), $content ); |
|
0 | 209 |
} |
210 |
||
16 | 211 |
// Captures any text in the body after $phone_delim as the body. |
9 | 212 |
$content = explode( $phone_delim, $content ); |
0 | 213 |
$content = empty( $content[1] ) ? $content[0] : $content[1]; |
214 |
||
9 | 215 |
$content = trim( $content ); |
0 | 216 |
|
5 | 217 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* Filters the content of the post submitted by email before saving. |
5 | 219 |
* |
220 |
* @since 1.2.0 |
|
221 |
* |
|
222 |
* @param string $content The email content. |
|
223 |
*/ |
|
224 |
$post_content = apply_filters( 'phone_content', $content ); |
|
0 | 225 |
|
9 | 226 |
$post_title = xmlrpc_getposttitle( $content ); |
0 | 227 |
|
16 | 228 |
if ( '' === trim( $post_title ) ) { |
9 | 229 |
$post_title = $subject; |
230 |
} |
|
0 | 231 |
|
9 | 232 |
$post_category = array( get_option( 'default_email_category' ) ); |
0 | 233 |
|
9 | 234 |
$post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' ); |
235 |
$post_data = wp_slash( $post_data ); |
|
0 | 236 |
|
9 | 237 |
$post_ID = wp_insert_post( $post_data ); |
238 |
if ( is_wp_error( $post_ID ) ) { |
|
0 | 239 |
echo "\n" . $post_ID->get_error_message(); |
9 | 240 |
} |
0 | 241 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
242 |
// The post wasn't inserted or updated, for whatever reason. Better move forward to the next email. |
9 | 243 |
if ( empty( $post_ID ) ) { |
0 | 244 |
continue; |
9 | 245 |
} |
0 | 246 |
|
5 | 247 |
/** |
248 |
* Fires after a post submitted by email is published. |
|
249 |
* |
|
250 |
* @since 1.2.0 |
|
251 |
* |
|
252 |
* @param int $post_ID The post ID. |
|
253 |
*/ |
|
254 |
do_action( 'publish_phone', $post_ID ); |
|
0 | 255 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
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
|
257 |
echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>'; |
0 | 258 |
|
9 | 259 |
if ( ! $pop3->delete( $i ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
echo '<p>' . sprintf( |
16 | 261 |
/* translators: %s: POP3 error. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
__( 'Oops: %s' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
esc_html( $pop3->ERROR ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
) . '</p>'; |
0 | 265 |
$pop3->reset(); |
266 |
exit; |
|
267 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
echo '<p>' . sprintf( |
16 | 269 |
/* translators: %s: The message ID. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
__( 'Mission complete. Message %s deleted.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
'<strong>' . $i . '</strong>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
) . '</p>'; |
0 | 273 |
} |
274 |
} |
|
275 |
||
276 |
$pop3->quit(); |