<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2257] 2013/codebykat/post-by-email/trunk: refactored check_email</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2257">2257</a></dd>
<dt>Author</dt> <dd>codebykat</dd>
<dt>Date</dt> <dd>2013-09-06 08:00:20 +0000 (Fri, 06 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>refactored check_email</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013codebykatpostbyemailtrunkclasspostbyemailadminphp">2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkclasspostbyemailphp">2013/codebykat/post-by-email/trunk/class-post-by-email.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkreadmetxt">2013/codebykat/post-by-email/trunk/readme.txt</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013codebykatpostbyemailtrunkclasspostbyemailadminphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php (2256 => 2257)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php 2013-09-06 08:00:18 UTC (rev 2256)
+++ 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php 2013-09-06 08:00:20 UTC (rev 2257)
</span><span class="lines">@@ -111,14 +111,14 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Clear the log file
</del><ins>+ * Clear the log file.
</ins><span class="cx"> *
</span><span class="cx"> * @since 0.9.9
</span><span class="cx"> */
</span><span class="cx"> public function clear_log() {
</span><span class="cx"> check_ajax_referer( 'post-by-email-clear-log', 'security' );
</span><span class="cx"> if( current_user_can( 'manage_options' ) ) {
</span><del>- delete_option( 'post_by_email_log' );
</del><ins>+ update_option( 'post_by_email_log', array() );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> die();
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkclasspostbyemailphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/class-post-by-email.php (2256 => 2257)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/class-post-by-email.php 2013-09-06 08:00:18 UTC (rev 2256)
+++ 2013/codebykat/post-by-email/trunk/class-post-by-email.php 2013-09-06 08:00:20 UTC (rev 2257)
</span><span class="lines">@@ -24,10 +24,10 @@
</span><span class="cx"> *
</span><span class="cx"> * @var string
</span><span class="cx"> */
</span><del>- protected $version = '0.9.7';
</del><ins>+ protected $version = '1.0.0';
</ins><span class="cx">
</span><span class="cx"> /**
</span><del>- * Unique identifier for your plugin.
</del><ins>+ * Unique identifier for the plugin.
</ins><span class="cx"> *
</span><span class="cx"> * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
</span><span class="cx"> * match the Text Domain file header in the main plugin file.
</span><span class="lines">@@ -70,6 +70,14 @@
</span><span class="cx"> 'mailserver_port' => 110,
</span><span class="cx"> 'default_email_category' => '');
</span><span class="cx">
</span><ins>+ /**
+ * Active connection.
+ *
+ * @since 1.0.0
+ *
+ * @var object
+ */
+ protected $connection;
</ins><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Initialize the plugin by setting localization, filters, and administration functions.
</span><span class="lines">@@ -112,7 +120,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
</span><span class="cx"> */
</span><del>- function activate( $network_wide ) {
</del><ins>+ public static function activate( $network_wide ) {
</ins><span class="cx"> // set up plugin options
</span><span class="cx"> $options = get_option( 'post_by_email_options' );
</span><span class="cx">
</span><span class="lines">@@ -130,6 +138,9 @@
</span><span class="cx"> update_option( 'post_by_email_options', $options );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ // if log already exists, this will return false, and that is okay
+ add_option( 'post_by_email_log', array(), '', 'no' );
+
</ins><span class="cx"> // schedule hourly mail checks with wp_cron
</span><span class="cx"> if( ! wp_next_scheduled( 'post-by-email-wp-mail.php' ) ) {
</span><span class="cx"> wp_schedule_event( current_time( 'timestamp', 1 ), 'hourly', 'post-by-email-wp-mail.php' );
</span><span class="lines">@@ -141,7 +152,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 0.9.8
</span><span class="cx"> */
</span><del>- function deactivate() {
</del><ins>+ public static function deactivate() {
</ins><span class="cx"> wp_clear_scheduled_hook( 'post-by-email-wp-mail.php' );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -159,7 +170,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Run "check_email" when not called by wp_cron
</del><ins>+ * Run "check_email" when not called by wp_cron.
</ins><span class="cx"> *
</span><span class="cx"> * @since 0.9.9
</span><span class="cx"> */
</span><span class="lines">@@ -168,7 +179,7 @@
</span><span class="cx"> wp_clear_scheduled_hook( 'post-by-email-wp-mail.php' );
</span><span class="cx"> wp_schedule_event( current_time( 'timestamp', 1 ) + HOUR_IN_SECONDS, 'hourly', 'post-by-email-wp-mail.php' );
</span><span class="cx">
</span><del>- self::check_email();
</del><ins>+ $this->check_email();
</ins><span class="cx">
</span><span class="cx"> wp_safe_redirect( admin_url( 'tools.php?page=post-by-email' ) );
</span><span class="cx"> }
</span><span class="lines">@@ -191,7 +202,7 @@
</span><span class="cx">
</span><span class="cx"> if ( $last_checked && ! WP_DEBUG ) {
</span><span class="cx"> $log_message = __( 'Slow down cowboy, no need to check for new mails so often!', 'post-by-email' );
</span><del>- self::save_log_message( $log_message );
</del><ins>+ $this->save_log_message( $log_message );
</ins><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -201,164 +212,71 @@
</span><span class="cx"> foreach( array( 'mailserver_url', 'mailserver_login', 'mailserver_pass' ) as $optname ) {
</span><span class="cx"> if( ! $options[$optname] || $options[$optname] == $this->default_options[$optname] ) {
</span><span class="cx"> $log_message = __( 'Options not set; skipping.', 'post-by-email' );
</span><del>- self::save_log_message( $log_message );
</del><ins>+ $this->save_log_message( $log_message );
</ins><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- $time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
</del><ins>+ $this->connection = $this->open_mailbox_connection( $options );
</ins><span class="cx">
</span><del>- $phone_delim = '::';
-
- $pop3 = new Horde_Imap_Client_Socket_Pop3( array( 'username' => $options['mailserver_login'],
- 'password' => $options['mailserver_pass'],
- 'hostspec' => $options['mailserver_url'],
- 'port' => $options['mailserver_port'] ) );
- $pop3->_setInit( 'authmethod', 'USER' );
-
- try {
- $pop3->login();
- $test = $pop3->search( 'INBOX' );
- $uids = $test['match'];
- }
- catch( Horde_Imap_Client_Exception $e ) {
- self::save_log_message( __( 'An error occurred: ', 'post-by-email') . $e->getMessage() );
</del><ins>+ if(! $this->connection ) {
</ins><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ $uids = $this->get_messages();
+
</ins><span class="cx"> if( 0 === sizeof( $uids ) ) {
</span><del>- $pop3->shutdown();
- self::save_log_message( __( 'There doesn’t seem to be any new mail.', 'post-by-email' ) );
</del><ins>+ $this->save_log_message( __( 'There doesn’t seem to be any new mail.', 'post-by-email' ) );
+ $this->connection->shutdown();
</ins><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ $log_message = sprintf( _n( 'Found 1 new message.', 'Found %s new messages.', sizeof( $uids ), 'post-by-email' ), sizeof( $uids ) );
</ins><span class="cx">
</span><ins>+ $time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
+ $phone_delim = '::';
+
</ins><span class="cx"> foreach( $uids as $id ) {
</span><del>- $log_message = '';
-
</del><span class="cx"> $uid = new Horde_Imap_Client_Ids( $id );
</span><span class="cx">
</span><span class="cx"> // get headers
</span><del>- $headerquery = new Horde_Imap_Client_Fetch_Query();
- $headerquery->headerText( array() );
- $headerlist = $pop3->fetch( 'INBOX', $headerquery, array(
- 'ids' => $uid
- ));
</del><ins>+ $headers = $this->get_message_headers( $uid );
</ins><span class="cx">
</span><del>- $headers = $headerlist->first()->getHeaderText( 0, Horde_Imap_Client_Data_Fetch::HEADER_PARSE );
-
</del><span class="cx"> /* Subject */
</span><span class="cx"> // Captures any text in the subject before $phone_delim as the subject
</span><span class="cx"> $subject = $headers->getValue( 'Subject' );
</span><span class="cx"> $subject = explode( $phone_delim, $subject );
</span><span class="cx"> $subject = $subject[0];
</span><ins>+ $subject = trim( $subject );
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> /* Author */
</span><del>- $post_author = 1;
- $author_found = false;
</del><ins>+ $from_email = $this->get_message_author( $headers );
</ins><span class="cx">
</span><del>- // Set the author using the email address (From or Reply-To, the last used)
- // otherwise use the site admin
- $author = $headers->getValue( 'From' );
- $replyto = $headers->getValue( 'Reply-To' );
</del><ins>+ $userdata = get_user_by( 'email', $from_email );
+ if ( ! empty( $userdata ) ) {
+ $post_author = $userdata->ID;
</ins><span class="cx">
</span><del>- if ( ! $author_found ) {
- if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $author, $matches ) )
- $author = $matches[0];
- else
- $author = trim( $author );
- $author = sanitize_email( $author );
- if ( is_email( $author ) ) {
- $log_message .= '<p>' . sprintf( __( 'Author is %s', 'post-by-email' ), $author ) . '</p>';
- $userdata = get_user_by( 'email', $author );
- if ( ! empty( $userdata ) ) {
- $post_author = $userdata->ID;
- $author_found = true;
- }
- }
</del><ins>+ // Set $post_status based on author's publish_posts capability
+ $user = new WP_User( $post_author );
+ $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
</ins><span class="cx"> }
</span><ins>+ else {
+ // use admin if no author found
+ $post_author = 1;
+ $post_status = 'pending';
+ }
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> /* Date */
</span><del>- $date = $headers->getValue( 'Date' );
- $dmonths = array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
-
- // of the form '20 Mar 2002 20:32:37'
- $ddate = trim( $date );
- if ( strpos( $ddate, ',' ) ) {
- $ddate = trim( substr( $ddate, strpos( $ddate, ',' ) + 1, strlen( $ddate ) ) );
- }
-
- $date_arr = explode( ' ', $ddate );
- $date_time = explode( ':', $date_arr[3] );
-
- $ddate_H = $date_time[0];
- $ddate_i = $date_time[1];
- $ddate_s = $date_time[2];
-
- $ddate_m = $date_arr[1];
- $ddate_d = $date_arr[0];
- $ddate_Y = $date_arr[2];
-
- for ( $j = 0; $j < 12; $j++ ) {
- if ( $ddate_m == $dmonths[$j] ) {
- $ddate_m = $j+1;
- }
- }
-
- $time_zn = intval( $date_arr[4] ) * 36;
- $ddate_U = gmmktime( $ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y );
- $ddate_U = $ddate_U - $time_zn;
</del><ins>+ $ddate_U = $this->get_message_date( $headers );
</ins><span class="cx"> $post_date = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
</span><span class="cx"> $post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
</span><span class="cx">
</span><span class="cx">
</span><del>-
</del><span class="cx"> /* Message body */
</span><del>- $query = new Horde_Imap_Client_Fetch_Query();
- $query->structure();
</del><ins>+ $content = $this->get_message_body( $uid );
</ins><span class="cx">
</span><del>- $list = $pop3->fetch( 'INBOX', $query, array(
- 'ids' => $uid
- ));
-
- $part = $list->first()->getStructure();
- $id = $part->findBody();
- $body = $part->getPart( $id );
-
- $query2 = new Horde_Imap_Client_Fetch_Query();
- $query2->bodyPart( $id, array(
- 'decode' => true,
- 'peek' => true
- ));
-
- $list2 = $pop3->fetch( 'INBOX', $query2, array(
- 'ids' => $uid
- ));
-
- $message2 = $list2->first();
- $content = $message2->getBodyPart( $id );
- if ( ! $message2->getBodyPartDecode( $id ) ) {
- // Quick way to transfer decode contents
- $body->setContents( $content );
- $content = $body->getContents();
- }
-
-
- // Set $post_status based on $author_found and on author's publish_posts capability
- if ( $author_found ) {
- $user = new WP_User( $post_author );
- $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
- } else {
- // Author not found in DB, set status to pending. Author already set to admin.
- $post_status = 'pending';
- }
-
- $subject = trim( $subject );
-
- $content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
- $content = trim( $content );
-
</del><span class="cx"> //Give Post-By-Email extending plugins full access to the content
</span><span class="cx"> //Either the raw content or the content of the last quoted-printable section
</span><span class="cx"> $content = apply_filters( 'wp_mail_original_content', $content );
</span><span class="lines">@@ -371,19 +289,25 @@
</span><span class="cx">
</span><span class="cx"> $post_content = apply_filters( 'phone_content' , $content );
</span><span class="cx">
</span><ins>+ /* post title */
</ins><span class="cx"> $post_title = xmlrpc_getposttitle( $content );
</span><span class="cx">
</span><span class="cx"> if ( '' == $post_title )
</span><span class="cx"> $post_title = $subject;
</span><span class="cx">
</span><ins>+ /* category */
</ins><span class="cx"> $post_category = array( $options['default_email_category'] );
</span><span class="cx">
</span><del>- $post_data = compact( 'post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status' );
</del><ins>+
+ /* create the post */
+ $post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
</ins><span class="cx"> $post_data = wp_slash( $post_data );
</span><span class="cx">
</span><span class="cx"> $post_ID = wp_insert_post( $post_data );
</span><del>- if ( is_wp_error( $post_ID ) )
</del><ins>+ if ( is_wp_error( $post_ID ) ) {
</ins><span class="cx"> $log_message .= "\n" . $post_ID->get_error_message();
</span><ins>+ $this->save_log_message( $log_message );
+ }
</ins><span class="cx">
</span><span class="cx"> // We couldn't post, for whatever reason. Better move forward to the next email.
</span><span class="cx"> if ( empty( $post_ID ) )
</span><span class="lines">@@ -391,42 +315,241 @@
</span><span class="cx">
</span><span class="cx"> do_action( 'publish_phone', $post_ID );
</span><span class="cx">
</span><del>- $log_message .= "\n<p>" . sprintf( __( 'Author: %s', 'post-by-email' ), esc_html( $post_author ) ) . '</p>';
- $log_message .= "\n<p>" . sprintf( __( 'Posted title: %s', 'post-by-email' ), esc_html( $post_title ) ) . '</p>';
</del><ins>+ // $log_message .= "\n<p>" . sprintf( __( 'Author: %s', 'post-by-email' ), esc_html( $post_author ) ) . '</p>';
+ // $log_message .= "\n<p>" . sprintf( __( 'Posted title: %s', 'post-by-email' ), esc_html( $post_title ) ) . '</p>';
+ $log_message .= "<br />" . __( 'Posted:', 'post-by-email') . ' <a href="' . get_permalink( $post_ID ) . '">' . esc_html( $post_title ) . '</a>';
</ins><span class="cx">
</span><del>- self::save_log_message($log_message);
</del><ins>+ $this->save_log_message( $log_message );
</ins><span class="cx">
</span><span class="cx"> } // end foreach
</span><span class="cx">
</span><span class="cx"> // delete all processed emails
</span><ins>+ if( ! WP_DEBUG )
+ $this->delete_messages( $uids );
+
+ $this->connection->shutdown();
+ }
+
+ /**
+ * Establishes the connection to the mailserver.
+ *
+ * @since 1.0.0
+ *
+ * @param array $options Options array
+ *
+ * @return object
+ */
+ protected function open_mailbox_connection( $options ) {
+ $pop3 = new Horde_Imap_Client_Socket_Pop3( array( 'username' => $options['mailserver_login'],
+ 'password' => $options['mailserver_pass'],
+ 'hostspec' => $options['mailserver_url'],
+ 'port' => $options['mailserver_port'] ) );
+ $pop3->_setInit( 'authmethod', 'USER' );
+
</ins><span class="cx"> try {
</span><del>- $pop3->store( 'INBOX', array(
</del><ins>+ $pop3->login();
+ }
+ catch( Horde_Imap_Client_Exception $e ) {
+ $this->save_log_message( __( 'An error occurred: ', 'post-by-email') . $e->getMessage() );
+ return false;
+ }
+
+ return $pop3;
+ }
+
+ /**
+ * Retrieve the list of new message IDs from the server.
+ *
+ * @since 1.0.0
+ *
+ * @return array Array of message UIDs
+ */
+ protected function get_messages() {
+ if( ! $this->connection )
+ return;
+
+ try {
+ $test = $this->connection->search( 'INBOX' );
+ $uids = $test['match'];
+ }
+ catch( Horde_Imap_Client_Exception $e ) {
+ $this->save_log_message( __( 'An error occurred: ', 'post-by-email') . $e->getMessage() );
+ return false;
+ }
+ return $uids;
+ }
+
+ /**
+ * Retrieve message headers.
+ *
+ * @since 1.0.0
+ *
+ * @param int $uid Message UID
+ *
+ * @return object
+ */
+ protected function get_message_headers( $uid ) {
+ $headerquery = new Horde_Imap_Client_Fetch_Query();
+ $headerquery->headerText( array() );
+ $headerlist = $this->connection->fetch( 'INBOX', $headerquery, array(
+ 'ids' => $uid
+ ));
+
+ $headers = $headerlist->first()->getHeaderText( 0, Horde_Imap_Client_Data_Fetch::HEADER_PARSE );
+ return $headers;
+ }
+
+ /**
+ * Find the email address of the message sender from the headers.
+ *
+ * @since 1.0.0
+ *
+ * @param object $headers Message headers
+ *
+ * @return string|false
+ */
+ protected function get_message_author( $headers ) {
+ // Set the author using the email address (From or Reply-To, the last used)
+ $author = $headers->getValue( 'From' );
+ // $replyto = $headers->getValue( 'Reply-To' ); // this is not used and doesn't make sense
+
+ if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $author, $matches ) )
+ $author = $matches[0];
+ else
+ $author = trim( $author );
+
+ $author = sanitize_email( $author );
+
+ if ( is_email( $author ) ) {
+ return $author;
+ }
+
+ return false; // author not found
+ }
+
+ /**
+ * Get the date of a message from its headers.
+ *
+ * @since 1.0.0
+ *
+ * @param object $headers Message headers
+ *
+ * @return string
+ */
+ protected function get_message_date( $headers ) {
+ $date = $headers->getValue( 'Date' );
+ $dmonths = array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
+
+ // of the form '20 Mar 2002 20:32:37'
+ $ddate = trim( $date );
+ if ( strpos( $ddate, ',' ) ) {
+ $ddate = trim( substr( $ddate, strpos( $ddate, ',' ) + 1, strlen( $ddate ) ) );
+ }
+
+ $date_arr = explode( ' ', $ddate );
+ $date_time = explode( ':', $date_arr[3] );
+
+ $ddate_H = $date_time[0];
+ $ddate_i = $date_time[1];
+ $ddate_s = $date_time[2];
+
+ $ddate_m = $date_arr[1];
+ $ddate_d = $date_arr[0];
+ $ddate_Y = $date_arr[2];
+
+ for ( $j = 0; $j < 12; $j++ ) {
+ if ( $ddate_m == $dmonths[$j] ) {
+ $ddate_m = $j+1;
+ }
+ }
+
+ $time_zn = intval( $date_arr[4] ) * 36;
+ $ddate_U = gmmktime( $ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y );
+ $ddate_U = $ddate_U - $time_zn;
+
+ return $ddate_U;
+ }
+
+ /**
+ * Get the content of a message from the mailserver.
+ *
+ * @since 1.0.0
+ *
+ * @param int Message UID
+ *
+ * @return string Message content
+ */
+ protected function get_message_body( $uid ) {
+ $query = new Horde_Imap_Client_Fetch_Query();
+ $query->structure();
+
+ $list = $this->connection->fetch( 'INBOX', $query, array(
+ 'ids' => $uid
+ ));
+
+ $part = $list->first()->getStructure();
+ $id = $part->findBody();
+ $body = $part->getPart( $id );
+
+ $query2 = new Horde_Imap_Client_Fetch_Query();
+ $query2->bodyPart( $id, array(
+ 'decode' => true,
+ 'peek' => true
+ ));
+
+ $list2 = $this->connection->fetch( 'INBOX', $query2, array(
+ 'ids' => $uid
+ ));
+
+ $message2 = $list2->first();
+ $content = $message2->getBodyPart( $id );
+ if ( ! $message2->getBodyPartDecode( $id ) ) {
+ // Quick way to transfer decode contents
+ $body->setContents( $content );
+ $content = $body->getContents();
+ }
+
+ $content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
+ $content = trim( $content );
+
+ return $content;
+ }
+
+ /**
+ * Delete a list of messages from the server.
+ *
+ * @since 1.0.0
+ *
+ * @param array UIDs of messages to delete
+ */
+ protected function delete_messages( $uids ) {
+ if( ! $this->connection )
+ return;
+
+ try {
+ $this->connection->store( 'INBOX', array(
</ins><span class="cx"> 'add' => array( Horde_Imap_Client::FLAG_DELETED ),
</span><span class="cx"> 'ids' => $uids
</span><span class="cx"> ) );
</span><span class="cx"> }
</span><span class="cx"> catch ( Horde_Imap_Client_Exception $e ) {
</span><del>- self::save_log_message( __( 'An error occurred: ', 'post-by-email') . $e->getMessage() );
</del><ins>+ $this->save_log_message( __( 'An error occurred: ', 'post-by-email' ) . $e->getMessage() );
</ins><span class="cx"> }
</span><del>-
- $pop3->shutdown();
</del><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Save a message to the log file
</del><ins>+ * Save a message to the log file.
</ins><span class="cx"> *
</span><span class="cx"> * @since 0.9.9
</span><span class="cx"> *
</span><del>- * @param string $error Error message to save to the log.
</del><ins>+ * @param string $message Error message to save to the log.
</ins><span class="cx"> */
</span><span class="cx"> protected function save_log_message( $message ) {
</span><span class="cx"> $log = get_option( 'post_by_email_log', array() );
</span><span class="cx">
</span><span class="cx"> array_unshift( $log, array( 'timestamp'=>current_time( 'timestamp' ), 'message'=>$message ) );
</span><span class="cx">
</span><del>- if( ! add_option( 'post_by_email_log', $log, '', 'no' ) ) {
- update_option( 'post_by_email_log', $log );
- }
</del><ins>+ update_option( 'post_by_email_log', $log );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkreadmetxt"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/readme.txt (2256 => 2257)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/readme.txt 2013-09-06 08:00:18 UTC (rev 2256)
+++ 2013/codebykat/post-by-email/trunk/readme.txt 2013-09-06 08:00:20 UTC (rev 2257)
</span><span class="lines">@@ -41,6 +41,10 @@
</span><span class="cx">
</span><span class="cx"> == Changelog ==
</span><span class="cx">
</span><ins>+= 1.0.0 =
+* Refactored check_email function.
+* Fixed a bug that caused the log file to behave inconsistently.
+
</ins><span class="cx"> = 0.9.9 =
</span><span class="cx"> * Better logging, no more wp_die().
</span><span class="cx"> * When email is checked manually, reschedule the next wp_cron check to an hour later.
</span></span></pre>
</div>
</div>
</body>
</html>