PHP Code:
#!/usr/bin/php -q
<?php
-= Begin Program =-
**/
//Load up some usefull libraries
$ROOTDIR = dirname(__FILE__); //area where wordpress is, i.e. /var/www/wordpress
require($ROOTDIR . '/wp-config.php');
require_once ($ROOTDIR . '/mimedecode.php');
/* USER VARIABLES */
//global vars you can change, but dont unless you know what you're doing. :)
$PHOTOSDIR = '/wp-photos/'; //relative directory, must include extra slashes
$FILESDIR = '/wp-filez/';
//if you don't want to add a subject in the email then you assign a default
$DEFAULT_TITLE = 'Emailed Post';
//What protocol are we using? Just leave it default if you don't know
$input_protocol = '0'; //0 = pop3, 1 = smtp
//Delete mail on successful download? Setting FALSE for debuging
$delete_email = TRUE;
//Time difference - tweak to suit, ie (10 for Brisbane, Australia)
$time_difference = get_settings('gmt_offset');
//drop Sigature, Everything after -- is deleted in the text msg.
$DROP_SIGNATURE = TRUE;
// Use thumbnails? will be used in ver 0.4
// $THUMBNAILS = TRUE;
// Photo maximum thumbnail size for Height
// $PHOTO_MAXHEIGHT = 200; // maximum height
/* END OF USER VARIABLES */
//some variables
error_reporting(2037);
//Retreive emails
switch ( $input_protocol ) {
case '1': //smtp - direct
$fd = fopen("php://stdin", "r");
$input = "";
while (!feof($fd)) {
$input .= fread($fd, 1024);
}
fclose($fd);
$emails[0] = $input;
break;
default: //pop3
$emails = pop3_retr( $delete_email );
}
//loop through messages
foreach ($emails as $email)
{
//sanity check to see if there is any info in the message
if ($email == NULL ) { print 'Dang, message is empty!'; die; }
//decode the mime
$params['include_bodies'] = true;
$params['decode_bodies'] = false;
$params['decode_headers'] = true;
$params['input'] = $email;
$structure = Mail_mimeDecode::decode($params);
# print_r ($structure);
//assign the default title/subject
if ( $structure->headers['subject'] == NULL )
{
$subject = $DEFAULT_TITLE;
} else {
$subject = htmlentities($structure->headers['subject']);
}
$ddate = trim($structure->headers['date']);
$from = trim($structure->headers['from']);
//default ping/comment status for sanity
$comment_status = get_settings('default_comment_status');
$ping_status = get_settings('default_ping_status');
//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
if ( $DROP_SIGNATURE ) { $content = removesig($content); }
//decode ubb
$content = ubb2html($content);
//filter new lines
$content = filternewlines($content);
//try and determine category
if ( preg_match('/^\[(.+)\] (.*)/', $subject, $matches) )
{
$post_categories[0] = trim($matches[1]);
$subject = trim($matches[2]);
//Work on the category search to see if we can determine the cat_id
if (empty($post_categories)) {
//empty(post_categories) is empty so use default cat
$post_categories[] = get_settings('default_category');
} else {
//check the database to see if their is a category similar
$sql_name = 'SELECT cat_ID FROM ' . $tablecategories . ' WHERE cat_name=\'' . addslashes($post_categories[0]) . '\'';
$sql_id = 'SELECT cat_ID FROM ' . $tablecategories . ' WHERE cat_ID=\'' . addslashes($post_categories[0]) . '\'';
if ( $post_categories[0] = $wpdb->get_var($sql_name) ) {
//then category is a named and found
} elseif ( $post_categories[0] = $wpdb->get_var($sql_id) ) {
//then cateogry was an ID and found
} else {
//then there was no found category so use default
$post_categories[0] = get_settings('default_category');
}
}
}
// Report
// print '<p><b>Mail Format</b>: ' . $mailformat . '</p>' . "\n";
print '<p><b>From</b>: ' . $from . '<br />' . "\n";
print '<b>Date</b>: ' . $post_date . '<br />' . "\n";
print '<b>Date GMT</b>: ' . $post_date_gmt . '<br />' . "\n";
print '<b>Category</b>: ' . $post_categories[0] . '<br />' . "\n";
print '<b>Subject</b>: ' . $subject . '<br />' . "\n";
print '<b>Posted content:</b></p><hr />' . $content . '<hr />';
$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,
'ping_status' => $ping_status,
'post_name' => $subject
);
//generate sql for insertion
$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
/* END PROGRAM */
/** FUNCTIONS **/
//retrieve POP3 mail
function pop3_retr ( $delete_email=TRUE )
{
require_once(ABSPATH.WPINC.'/class-pop3.php');
$pop3 = new POP3();
if (!$pop3->connect(get_settings('mailserver_url'), get_settings('mailserver_port'))) :
echo "Ooops $pop3->ERROR <br />\n";
exit;
endif;
//Check to see if there is any mail, if not die
$msg_count = $pop3->login(get_settings('mailserver_login'), get_settings('mailserver_pass'));
if (0 == $msg_count)
{
$pop3->quit();
die(__('There does not seem to be any new mail.'));
}
// loop through messages
for ($i=1; $i <= $msg_count; $i++)
{
$emails[$i] = implode ('',$pop3->get($i));
if ( $delete_email == TRUE && checkuserposter($emails[$i]) ) {
if( !$pop3->delete($i) ) {
echo '<p>Oops '.$pop3->ERROR.'</p></div>';
$pop3->reset();
exit;
} else {
echo "Mission complete, message <strong>$i</strong> deleted.";
}
}
}
//clean up
$pop3->quit();
return $emails;
}
//tear apart the meta part for useful information
function get_content ($part)
{
global $PHOTOSDIR,$FILESDIR,$THUMBNAILS,$ROOTDIR;
$URLPHOTOSDIR = get_settings('siteurl') . $PHOTOSDIR;
$REALPHOTOSDIR = $ROOTDIR . $PHOTOSDIR;
$URLFILESDIR = get_settings('siteurl') . $FILESDIR;
$REALFILESDIR = $ROOTDIR . $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':
//go through each sub-section
if ($part->ctype_secondary=='enriched')
{
//convert enriched text to html
$meta_return = etf2html($part->body ) . "\n";
} elseif ($part->ctype_secondary=='html') //check for html encoding
{
//strip excess html
$meta_return = html2html($part->body ) . "\n";
} else {
//regular text, so just strip the pgp signature
$meta_return = htmlentities( $part->body ) . "\n";
$meta_return = strip_pgp($meta_return);
}
break;
case 'image':
$filename = rand() . '.' . $part->ctype_secondary;
$file = $REALPHOTOSDIR . $filename;
$fp = fopen($file, 'w');
fwrite($fp, $part->body);
fclose($fp);
[at]exec ('chmod 755 ' . $file);
$meta_return .= '<img src="' . $URLPHOTOSDIR . $filename . '" alt="' . $part->ctype_parameters['name'] . '" />' . "\n";
break;
case 'application':
//pgp signature - then forget it
if ( $part->ctype_secondary == 'pgp-signature' ) {break;}
//other attachments save to $FILESDIR
$filename = $part->ctype_parameters['name'];
$file = $REALFILESDIR . $filename;
$fp = fopen($file, 'w');
fwrite($fp, $part->body );
fclose($fp);
[at]exec ('chmod 755 ' . $file);
$meta_return .= '<a href="' . $URLFILESDIR . $filename . '">' . $part->ctype_parameters['name'] . '</a>' . "\n";
break;
}
return $meta_return;
}
function ubb2html($text)
{
// Array of tags with opening and closing
$tagArray['img'] = array('open'=>'<img src="','close'=>'">');
$tagArray['b'] = array('open'=>'<b>','close'=>'</b>');
$tagArray['i'] = array('open'=>'<i>','close'=>'</i>');
$tagArray['u'] = array('open'=>'<u>','close'=>'</u>');
$tagArray['url'] = array('open'=>'<a&nbs