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/'; //$subject = ''; require(dirname(__FILE__) . '/wp-config.php'); require_once(ABSPATH.WPINC.'/class-pop3.php'); require_once(ABSPATH.WPINC.'/functions-formatting.php'); require_once (dirname(__FILE__) . '/mimedecode.php'); error_reporting(2037); $time_difference = get_settings('gmt_offset'); $theworldisending = false; //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.')); } //$count = 1; //loop through messages for ($i=1; $i <= $count; $i++) { //variables $content_type = ''; $boundary = ''; $bodysignal = 0; $theworldisending = false; $input = implode ('',$pop3->get($i)); // $input = file_get_contents('./email.txt'); // $input = implode('', file('/home/pug/picshare.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 = gmdate('Y-m-d H:i:s', time($ddate) + ($time_difference * 3600)); // JCJ - make this a gmdate too to fix doubledating $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 $from = "pug@ibu02.com"; $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, 'post_name' => sanitize_title($subject) ); //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 $theworldisending; 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); if ($theworldisending) return $meta_return; } break; case 'text': //Take care of Sprint PCS Vision picture phones if (strpos($part->body, "pictures.sprintpcs.com") !== false) { if (strpos($part->body, "-->") === false) break; /* Skip the plaintext run */ return $meta_return = sprint_parse($part->body, $photosdir); /* Stop now or we'll go and dump the images in the message */ break; } //dump the non-Sprint enriched stuff if ($part->ctype_secondary=='enriched') { $meta_return = etf2html($part->body ) . "\n"; } else { $meta_return = htmlentities( $part->body ) . "\n"; } break; case 'image': $filename = $photosdir . rand() . '.' . $part->ctype_secondary; $fp = fopen($filename, 'w'); fwrite($fp, $part->body); fclose($fp); $meta_return .= '' . $part->ctype_parameters['name'] . '' . "\n"; break; case 'application': //pgp signature if ( $part->ctype_secondary == 'pgp-signature' ) {break;} //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); for ($i = 0; $i<=count($arrcontent); $i++) { $line = $arrcontent[$i]; $nextline = $arrcontent[$i+1]; // check for this sequence for Sprint phones: "------------------" if (strpos(trim($line), "------------------")) { break; } if ( preg_match('/^--$/',trim($line)) ) { break; } $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; } function sprint_parse($message, $photosdir) { global $subject, $theworldisending; preg_match("/(http:\/\/.*getMessageMedia.*)&lt/i", $message, $matches); $url=$matches[1]; preg_match("/messageText>(.*)</i", $message, $matches); $subject = $text = $matches[1]; if (!$subject) $subject = "No description... yet."; echo "Opening picture located at [".$url."]\n"; echo "Entry title is \"".$subject."\"\n"; if (!$url) die("No URL found in the message. Maybe it's malformed, or Spring hates you."); $cnt = ""; do { $webname = $photosdir . date('YmdHis') . $cnt . '.jpg'; $filename = $_SERVER['DOCUMENT_ROOT']."/".$webname; if($cnt=="") $cnt=1; else $cnt++; } while (is_file($filename)); if (!$pic = fopen($url, "rb")) { die("Could not open the picture off the web from ".$url); return; } if (!$fp = fopen($filename, 'w')) { die("Could not open ".$filename." for writing"); } while (!feof($pic)) { fwrite($fp, fread($pic, 8192)); } fclose($pic); fclose($fp); if (!is_file($filename)) die("File was not written: ".$filename); list($wo, $ho) = getimagesize($filename); /* $maxx is a variable which determines the maximum width of the resulting image you would like on your blog. If the image is too large, it will be smartly scaled to the proper size. */ $maxx = 320; if ($wo > $maxx) { $w = $maxx; $h = $ho * ($maxx/$wo); $image_r = imagecreatetruecolor($w, $h); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_r, $image, 0, 0, 0, 0, $w, $h, $wo, $ho); imagejpeg($image_r, $filename, 70); } else { $h = $ho; $w = $wo; } $meta_return = 'Some picture from a call phone camera' . "\n"; echo "Image size is ".$w."x".$h."\n"; $theworldisending = true; echo "Run is done."; return $meta_return; } // end of script ?>