more graceful exit on event on no emails in inbox ($pop3->quit) * - Added work around for email addresses with exta <> in field (ie: instead of blade@lasmash.com * - Added some ===basic=== enriched text support * - Updated readme file for easier install * - Easy modify of globals (such as photosdir and filesdir) * - Cleaned up some pear stuff in install * * Version 0.1 * First release **/ //global vars you can change $photosdir = 'wp-photos/'; $filesdir = 'wp-filez/'; require(dirname(__FILE__) . '/wp-config.php'); require_once(ABSPATH.WPINC.'/class-pop3.php'); require_once (dirname(__FILE__) . '/mimedecode.php'); error_reporting(2037); $time_difference = get_settings('gmt_offset'); //retrieve mail $pop3 = new POP3(); if (!$pop3->connect(get_settings('mailserver_url'), get_settings('mailserver_port'))) : echo "Ooops $pop3->ERROR
\n"; exit; endif; $count = $pop3->login(get_settings('mailserver_login'), get_settings('mailserver_pass')); if (0 == $count) { $pop3->quit(); die(__('There does not seem to be any new mail.')); } //loop through messages for ($i=1; $i <= $count; $i++) { //variables $content_type = ''; $boundary = ''; $bodysignal = 0; $input = implode ('',$pop3->get($i)); // $input = file_get_contents('./email.txt'); if(!$pop3->delete($i)) { echo '

Oops '.$pop3->ERROR.'

'; $pop3->reset(); exit; } else { echo "

Mission complete, message $i deleted.

"; } //decode the mime $params['include_bodies'] = true; $params['decode_bodies'] = false; $params['decode_headers'] = true; $params['input'] = $input; $structure = Mail_mimeDecode::decode($params); //print_r ($structure); $subject = htmlentities($structure->headers['subject']); $ddate = trim($structure->headers['date']); $from = trim($structure->headers['from']); //work around for users with extra <> in email address if (preg_match('/^[^<>]+<([^<>]+)>$/',$from,$matches)) { $from = $matches[1]; } $content = get_content($structure); //date reformating $post_date = date('Y-m-d H:i:s', time($ddate) + ($time_difference * 3600)); $post_date_gmt = gmdate('Y-m-d H:i:s', time($ddate) ); //remove signature $content = removesig($content); //filter new lines $content = filternewlines($content); //try and determine category if ( preg_match('/.*\[(.+)\](.+)/', $subject, $matches) ) { $post_categories[0] = $matches[1]; $subject = $matches[2]; } if (empty($post_categories)) $post_categories[] = get_settings('default_category'); //report // print '

Mail Format: ' . $mailformat . '

' . "\n"; print '

From: ' . $from . '
' . "\n"; print 'Date: ' . $post_date . '
' . "\n"; print 'Date GMT: ' . $post_date_gmt . '
' . "\n"; print 'Category: ' . $post_categories[0] . '
' . "\n"; print 'Subject: ' . $subject . '
' . "\n"; print 'Posted content:


' . $content . '
'; //see if sender is a valid sender from the wp database $sql = 'SELECT id FROM '.$tableusers.' WHERE user_email=\'' . addslashes($from) . '\''; if (!$poster = $wpdb->get_var($sql)) { echo '

Invalid sender: ' . htmlentities($from) . " !

Not adding email!


\n"; continue; } $details = array( 'post_author' => $poster, 'post_date' => $post_date, 'post_date_gmt' => $post_date_gmt, 'post_content' => $content, 'post_title' => $subject, 'post_modified' => $post_date, 'post_modified_gmt' => $post_date_gmt ); //generate sql $sql = 'INSERT INTO '.$tableposts.' ('. implode(',',array_keys($details)) .') VALUES (\''. implode('\',\'',array_map('addslashes',$details)) . '\')'; $result = $wpdb->query($sql); $post_ID = $wpdb->insert_id; do_action('publish_post', $post_ID); do_action('publish_phone', $post_ID); pingback($content, $post_ID); foreach ($post_categories as $post_category) { $post_category = intval($post_category); // Double check it's not there already $exists = $wpdb->get_row("SELECT * FROM $tablepost2cat WHERE post_id = $post_ID AND category_id = $post_category"); if (!$exists && $result) { $wpdb->query(" INSERT INTO $tablepost2cat (post_id, category_id) VALUES ($post_ID, $post_category) "); } } } // end looping over messages $pop3->quit(); /** FUNCTIONS **/ //tear apart the meta part for useful information function get_content ($part) { global $photosdir,$filesdir; //fixes $part = charsetcheck($part); $part = base64check($part); switch ( strtolower($part->ctype_primary) ) { case 'multipart': $meta_return = ''; foreach ($part->parts as $section) { $meta_return .= get_content($section); } break; case 'text': //dump the enriched stuff if ($part->ctype_secondary=='enriched') { $meta_return = etf2html($part->body ) . "\n"; } else { $meta_return = $part->body . "\n"; } break; case 'image': if ($part->ctype_secondary == "pjpeg") { // sometime progressive-jpges comes in with the $bildersorte = "jpg"; // endinmg pjpeg which causes problems in firefox standalone, so i rename it } else { $bildersorte = $part->ctype_secondary; }; $filename = $photosdir . rand() . '.' . $bildersorte; $fp = fopen($filename, 'w'); fwrite($fp, $part->body); fclose($fp); // ############################################## // einfügen der thumbnail-funktion // Update Image Resizing $maxdim = 250; // max withs maximale breite $size=getimagesize($filename); if ($size[0] > $maxdim) { // wenn breite größer als erlaubt $sizemin[0] = $maxdim; // array sizemin 0 auf max breite $sizemin[1] = $maxdim * $size[1]/$size[0]; //neue breite mal höhe durch breite switch($size[2]) { // abklappern der bildtypen case 1: $im = @imagecreatefromgif($filename); break; case 2: $im = @imagecreatefromjpeg($filename); break; case 3: $im = @imagecreatefrompng($filename); break; case 15: $im = @imagecreatefrombmp($filename); break; } $small = imagecreatetruecolor($sizemin[0], $sizemin[1]); // create new image imagecopyresampled($small, $im, 0, 0, 0, 0,$sizemin[0], $sizemin[1], $size[0], $size[1]); $small_filename = $photosdir . rand() . '.' . "jpg"; imagedestroy($im); // free memory // --------- if (ImageJPEG($small,$small_filename,100)) // try to save image { echo "file $path has been written\n"; //success echo "size: $sizemin[0] x $sizemin[1] \n"; } else { echo "fehler beim schreiben des bildes"; } $meta_return .= '' . '' . '' . "\n"; } else { $meta_return .= '' . $part->ctype_parameters['name'] . '' . "\n"; } break; // ende des modifizierten teiles case 'application': //pgp signature if ( $part->ctype_secondary == 'pgp-signature' ) {break;} if ( $part->ctype_secondary == 'smil' ) {break;} // nokia 3650 sends an extra file with mms //other attachments $filename = $filesdir . $part->ctype_parameters['name']; $fp = fopen($filename, 'w'); fwrite($fp, $part->body ); fclose($fp); $meta_return .= '' . $part->ctype_parameters['name'] . '' . "\n"; break; } return $meta_return; } // This function turns Enriched Text into something similar to html // Very basic at the moment, only supports some functionality and dumps the rest function etf2html ( $content ) { $search = array( '//', '/<\/bold>/', '//', '/<\/underline>/', '//', '/<\/italic>/', '/.*<\/param>/', '/<\/fontfamily>/', '//', '/<\/x-tad-bigger>/' ); $replace = array ( '', '', '', '', '', '', '', '', '', '' ); // strip extra line breaks $content = preg_replace($search,$replace,trim($content)); return ($content); } // Keeps content until finds a line with '--' // This effectively removes signatures function removesig ( $content ) { $arrcontent = explode("\n", $content); $append = TRUE; $i = 0; for ($i = 0; $i<=count($arrcontent); $i++) { $line = $arrcontent[$i]; $nextline = $arrcontent[$i+1]; if ( preg_match('/^--$/',trim($line)) ) { $append = FALSE;} if ( $append == TRUE ) { // if ($line != NULL && $nextline == NULL ) //{ $strcontent .= $line ."
\n";; //check if there are null lines \n //} elseif ($line == NULL && $nextline == NULL) { $strcontent .= '
'; //}else { $strcontent .= $line; } } return $strcontent; } //filter content for new lines function filternewlines ( $content ) { $search = array ( '/ (\n|\r\n|\r)/', '/(\n|\r\n|\r)/' ); $replace = array ( ' ', "\n" ); // strip extra line breaks $return = preg_replace($search,$replace,$content); return $return; } // This function checks if the content is base64 function base64check ( $part ) { // if ($part->headers['content-transfer-encoding'] != 'base64') // { if ( strtolower($part->headers['content-transfer-encoding']) == 'base64' ) { $part->body = base64_decode($part->body); } // } return $part; } function charsetcheck ( $part ) { if ( strtolower($part->ctype_parameters['charset'] == 'utf-8') ) { $part->body = utf8_decode($part->body); } return $part; } // end of script ?>