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