svn.filsa.org wordpress_dev

Compare Revisions

Ignore whitespace Rev 16 → Rev 17

/trunk/plugins/mail2wordpress/mail2wp.php
1,12 → 1,10
<?php
/*
 
 
 
*/
 
/** Make sure that the WordPress bootstrap has run before continuing. */
require(dirname(__FILE__) . '/../../../wp-load.php');
include_once( ABSPATH . 'wp-admin/includes/admin.php' );
 
/** Get the POP3 class with which to access the mailbox. */
require_once( ABSPATH . WPINC . '/class-pop3.php' );
21,6 → 19,7
var $image_id = 1;
var $upload_dir;
var $blog_gmt_offset;
var $options;
/**
* Process one mail message, and execute the post-by-mail.
45,7 → 44,7
//$parseparams['SkipBody'] = '1';
//var_dump($parseparams);
$result['status'] = 'INCOMPLETE';
// --------------------- PARSE with mime_parser ----------------------------
if(!$parser->Decode($parseparams, $decoded)) {
$result['error'] = 'MIME message decoding error: '.$mime->error.' at position '.$mime->error_position. 'Aborting this message.' . "\n";
$this->log($result['error'], 'ERR');
59,6 → 58,7
return False;
}
$this->log(' - Parsing data from message titled: ' . $analyzed['Subject']);
// ------- DO AUTH CHECK -- at this point we have enough info to figure this out
//var_dump($analyzed);
//get headers
// 'post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status'
68,30 → 68,16
$post_date_gmt = $this->_parse_rfc822date($analyzed['Date'], True);
//$post_title = $this->_decode_text($msgparse['Headers']['subject:']); // using decoded
$post_title = $this->_decode_text($analyzed['Subject']);
$post_status = 'pending';
$post_content = $analyzed['Data'];; // content, unprocessed, for now.
$post_status = 'draft';
if (empty($post_author)) {
$post_author=1; // dumb but easy assumption that user 1, admin, will always exist.
} else {
$user = new WP_User($post_author);
$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
}
// process body
$post_content = $this->process_analyzed_body($analyzed); // process via analyzed
$post_content = apply_filters('phone_content', $post_content);// WP filter hook
 
if (function_exists('mb_convert_encoding') &&
$analyzed['Encoding'] == 'iso-2022-jp') {
$this->log(' ! Detected iso-2022-jp encoding. Making adjustments.');
$post_title = mb_convert_encoding($post_title, "UTF-8", "ISO-2022-JP");
$post_content = mb_convert_encoding($post_content, "UTF-8", "ISO-2022-JP");
}
// prep post
$post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
//var_dump($post_data);
$post_data = add_magic_quotes($post_data);
// send post to WP
$post_ID = wp_insert_post($post_data);
103,11 → 89,33
if ( empty( $post_ID ) )
return False;
 
// process mail body into post content
$post_content = $this->process_analyzed_body($analyzed, $post_ID); // process via analyzed
$post_content = apply_filters('phone_content', $post_content);// WP filter hook
if (function_exists('mb_convert_encoding') &&
$analyzed['Encoding'] == 'iso-2022-jp') {
$this->log(' ! Detected iso-2022-jp encoding. Making adjustments.');
$post_title = mb_convert_encoding($post_title, "UTF-8", "ISO-2022-JP");
$post_content = mb_convert_encoding($post_content, "UTF-8", "ISO-2022-JP");
}
$user = new WP_User($post_author);
$post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
// update post content and title
$update = compact('post_content','post_title', 'post_status');
$update['ID'] = $post_ID;
$update_post_id = wp_update_post($update);
if ( 0 == $update_post_id ) {
$this->log(' WP Post failed after content update. Check for a new draft with no content.');
return False;
}
do_action('publish_phone', $post_ID); // WP hook
$this->log(" * Posted title: $post_title, Author id: $post_author, status: $post_status.");
//var_dump($post_data);
//echo 'POST_CONTENT:<br/>' . $post_content . '<br />';
return True;
return true;
}
116,14 → 124,16
The body is modified based on the attachments or related fileparts that are included with the message.
@param $mail is an array returned from the $mime_parser_class->Analyze() function.
*/
function process_analyzed_body($mail) {
function process_analyzed_body($mail, $post_id) {
$result = '';
$attach_text = '';
if ($mail['Type'] == 'text') $result = $this->_decode_text($mail['Data']);
 
//handle Type=html and Related
// Related images are already in the $result text.
if ($mail['Type'] == 'html') {
$this->log (' - HTML message type.');
$result = $this->_decode_text($mail['Data']);
$result = $mail['Data'];
if (!empty($mail['Related'])) {
$this->log(' - Processing related items.');
foreach ($mail['Related'] as $attachment) {
130,7 → 140,10
if (! 'image' == $attachment['Type']) continue; //skip non-images for now
$fname = $this->_get_safe_filename($attachment['FileName'], $attachment['SubType']);
if ($this->_save_file($fname, $attachment['Data'])) {
$urlpath = $this->upload_dir['url'] . '/' . $fname;
$mimetype = $attachment['Type'] . '/' . $attachment['SubType'];
$attachname = $this->_insert_attachment($fname, $post_id, $mimetype, false);
 
$urlpath = $this->upload_dir['url'] . '/' . $attachname['file'];
$cid = 'cid:' . $attachment['ContentID'];
$result = str_replace($cid, $urlpath, $result);
}
141,21 → 154,73
// handle attachments
if (!empty($mail['Attachments'])) {
$this->log(' - Processing attachments.');
$result .= "\n<div class='attachments'>\n";
$attach_text .= "\n<div class='attachments'>\n";
foreach ($mail['Attachments'] as $attachment) {
if (! 'image' == $attachment['Type']) continue; //skip non-images for now
$fname = $this->_get_safe_filename($attachment['FileName'], $attachment['SubType']);
if ($this->_save_file($fname, $attachment['Data'])) {
$urlpath = $this->upload_dir['url'] . '/' . $fname;
$result .= '<img src="' . $urlpath .'" class="photomail attachments"/>'. "\n";
$mimetype = $attachment['Type'] . '/' . $attachment['SubType'];
$attachname = $this->_insert_attachment($fname, $post_id, $mimetype);
$urlpath = $this->upload_dir['url'] . '/' . $attachname['file'];
$attach_text .= '<img src="' . $urlpath .'" class="photomail attachments"/>'. "\n";
}
}
$result .= "\n</div>\n";
$attach_text .= "\n</div>\n";
$result = $attach_text . $result;
}
return $result;
}
/*******
* registers image as an attachment, so it shows in media browser and is associated with the post
* creates thumbnails and other images sizes
* @param $filename - name of the file (from $this->_get_safe_filename() )
* @param $parent_post_id - the post id of the mail message
* @param $resize - resize the image to no larger than the configured size. defaults to true.
* @returns An array with keys "file, height,width"
*/
function _insert_attachment($fname, $parent_post_id, $mimetype, $resize=true ) {
$result = array('file'=>$fname);
$fpath = $this->upload_dir['path'] . '/'. $fname;
$att_params = array();
$att_params['post_title'] = $fname;
$att_params['post_status'] = 'inherit';
$att_params['post_content'] = '';
$att_params['post_mime_type'] = $mimetype;
$att_params['guid'] = $this->upload_dir['url'] . '/'. $fname;
$this->log(' - _insert_attachment: ' . $fname .',path:'.$fpath.', postid:'. $parent_post_id);
$attach_id = wp_insert_attachment( $att_params, $fpath, $parent_post_id );
$this->log(' - got attachment_id=' . $attach_id);
$attach_data = wp_generate_attachment_metadata( $attach_id, $fpath );
//var_dump($attach_data);
wp_update_attachment_metadata( $attach_id, $attach_data );
$result['height'] = $attach_data['height'];
$result['width'] = $attach_data['width'];
// return a resized image filename if the attachment image is larger than a $good_size image.
// @@TODO - make this a config option.
$good_size = 'medium';
$this->log(' - _insert_attachment: h=' . $attach_data['height'] .', w='.$attach_data['width'] );
 
if ($resize) {
if ( ($attach_data['width'] > get_option("{$good_size}_size_w")) ||
($attach_data['height'] > get_option("{$good_size}_size_h")) ) {
if (isset($attach_data['sizes'][$good_size])) {
$this->log(' - _insert_attachment inside resize: good-size=' . $good_size);
//var_dump($attach_data['sizes'][$good_size]);
$result = $attach_data['sizes'][$good_size];
}
}
}
$this->log(' - _insert_attachment is done, returning:' . $result['file']);
 
return $result;
}
/**
Returns a string, that represents a safe filename.
- safe filename is not encoded in a weird way
165,7 → 230,7
$fext = array('jpeg'=>'.jpg', 'gif'=>'.gif', 'png'=>'.png');
// fix encoded filenames - just generate a safe name.
if ($f != $this->_decode_text($f)) {
$f = $this->run_id . '-' . $this->image_id++ . $fext[$subtype];
$f = 'pic_'.$this->run_id . '-' . $this->image_id++ . $fext[$subtype];
$f = $this->_decode_text($f);
}
$f = str_replace(' ', '_', $f); // change spaces to underscores.
204,7 → 269,8
}
/**
Easy logging
*Easy logging
* @@TODO: log to a file, implement log levels that work.
*/
function log($s, $level='INFO') {
$t = date('Y-m-d H:i:s');
243,7 → 309,7
}
}
//$pop3->quit();
$pop3->quit(); // comment out to prevent deletion of messages.
$this->shutdown();
 
}
251,6 → 317,7
$this->run_id = dechex(time());
$this->upload_dir = wp_upload_dir(date('Y/m'));
$this->blog_gmt_offset = absint(get_option('gmt_offset')) * 3600;
$this->options = get_option( 'm2wp_options' );
}
function shutdown() {
346,7 → 413,7
$photomail = new mail2wp();
 
$is_active = get_option('m2wp_enabled');
if (! $is_active) {
if (false /*! $is_active*/) {
$photomail->log("Deactivated. Activate the mail2wordpress plugin to start processing emails.");
return true;
}