web/wp-mail.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     8  */
     8  */
     9 
     9 
    10 /** Make sure that the WordPress bootstrap has run before continuing. */
    10 /** Make sure that the WordPress bootstrap has run before continuing. */
    11 require(dirname(__FILE__) . '/wp-load.php');
    11 require(dirname(__FILE__) . '/wp-load.php');
    12 
    12 
       
    13 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
       
    14 	wp_die( __( 'This action has been disabled by the administrator.' ) );
       
    15 
    13 /** Allow a plugin to do a complete takeover of Post by Email **/
    16 /** Allow a plugin to do a complete takeover of Post by Email **/
    14 do_action('wp-mail.php');
    17 do_action('wp-mail.php');
    15 
    18 
    16 /** Get the POP3 class with which to access the mailbox. */
    19 /** Get the POP3 class with which to access the mailbox. */
    17 require_once( ABSPATH . WPINC . '/class-pop3.php' );
    20 require_once( ABSPATH . WPINC . '/class-pop3.php' );
    30 $time_difference = get_option('gmt_offset') * 3600;
    33 $time_difference = get_option('gmt_offset') * 3600;
    31 
    34 
    32 $phone_delim = '::';
    35 $phone_delim = '::';
    33 
    36 
    34 $pop3 = new POP3();
    37 $pop3 = new POP3();
    35 $count = 0;
    38 
    36 
    39 if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
    37 if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) ||
    40 	wp_die( esc_html( $pop3->ERROR ) );
    38 	! $pop3->user(get_option('mailserver_login')) ||
    41 
    39 	( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) {
    42 $count = $pop3->pass( get_option('mailserver_pass') );
    40 		$pop3->quit();
    43 
    41 		wp_die( ( 0 === $count ) ? __('There doesn’t seem to be any new mail.') : esc_html($pop3->ERROR) );
    44 if( false === $count )
       
    45 	wp_die( esc_html( $pop3->ERROR ) );
       
    46 
       
    47 if( 0 === $count ) {
       
    48 	$pop3->quit();
       
    49 	wp_die( __('There doesn’t seem to be any new mail.') );
    42 }
    50 }
    43 
    51 
    44 for ( $i = 1; $i <= $count; $i++ ) {
    52 for ( $i = 1; $i <= $count; $i++ ) {
    45 
    53 
    46 	$message = $pop3->get($i);
    54 	$message = $pop3->get($i);
    95 				$subject = $subject[0];
   103 				$subject = $subject[0];
    96 			}
   104 			}
    97 
   105 
    98 			// Set the author using the email address (From or Reply-To, the last used)
   106 			// Set the author using the email address (From or Reply-To, the last used)
    99 			// otherwise use the site admin
   107 			// otherwise use the site admin
   100 			if ( preg_match('/(From|Reply-To): /', $line) )  {
   108 			if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
   101 				if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
   109 				if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
   102 					$author = $matches[0];
   110 					$author = $matches[0];
   103 				else
   111 				else
   104 					$author = trim($line);
   112 					$author = trim($line);
   105 				$author = sanitize_email($author);
   113 				$author = sanitize_email($author);
   106 				if ( is_email($author) ) {
   114 				if ( is_email($author) ) {
   107 					echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
   115 					echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
   108 					$userdata = get_user_by_email($author);
   116 					$userdata = get_user_by('email', $author);
   109 					if ( empty($userdata) ) {
   117 					if ( ! empty( $userdata ) ) {
   110 						$author_found = false;
       
   111 					} else {
       
   112 						$post_author = $userdata->ID;
   118 						$post_author = $userdata->ID;
   113 						$author_found = true;
   119 						$author_found = true;
   114 					}
   120 					}
   115 				} else {
       
   116 					$author_found = false;
       
   117 				}
   121 				}
   118 			}
   122 			}
   119 
   123 
   120 			if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
   124 			if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
   121 				$ddate = trim($line);
   125 				$ddate = trim($line);
   151 	// Set $post_status based on $author_found and on author's publish_posts capability
   155 	// Set $post_status based on $author_found and on author's publish_posts capability
   152 	if ( $author_found ) {
   156 	if ( $author_found ) {
   153 		$user = new WP_User($post_author);
   157 		$user = new WP_User($post_author);
   154 		$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
   158 		$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
   155 	} else {
   159 	} else {
   156 		// Author not found in DB, set status to pending.  Author already set to admin.
   160 		// Author not found in DB, set status to pending. Author already set to admin.
   157 		$post_status = 'pending';
   161 		$post_status = 'pending';
   158 	}
   162 	}
   159 
   163 
   160 	$subject = trim($subject);
   164 	$subject = trim($subject);
   161 
   165 
   216 	if(!$pop3->delete($i)) {
   220 	if(!$pop3->delete($i)) {
   217 		echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>';
   221 		echo '<p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . '</p>';
   218 		$pop3->reset();
   222 		$pop3->reset();
   219 		exit;
   223 		exit;
   220 	} else {
   224 	} else {
   221 		echo '<p>' . sprintf(__('Mission complete.  Message <strong>%s</strong> deleted.'), $i) . '</p>';
   225 		echo '<p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . '</p>';
   222 	}
   226 	}
   223 
   227 
   224 }
   228 }
   225 
   229 
   226 $pop3->quit();
   230 $pop3->quit();
   227 
       
   228 ?>