connect( get_settings('mailserver_url'),
get_settings('mailserver_port'),
get_settings('mailserver_login'),
get_settings('mailserver_pass') ) )
{
echo "ERROR: Ooops $mbox->ERROR
\n";
exit;
} else {
echo "
Connection Success!
";
}
if ($debug) {
echo "(Debugging is ON)
";
}
$count = $mbox->get_count();
if (0 == $count) die(_("There doesn't seem to be any new mail."));
echo "Received $count messages
\n";
for ($i=1; $i <= $count; $i++) {
//variables
$content = '';
$url_image = '';
//$url_thumbs = array();
$header = $mbox->parse_header($i);
$body = $mbox->parse_content($i);
if ($debug) {
walk('headers',$header);
walk('body',$body);
}
/* See notes above regarding actual message deletion */
if(!$mbox->delete_msg($i)) {
echo 'Oops '.$mbox->ERROR.'
';
$mbox->reset();
exit;
} else {
echo "Mission complete, message $i deleted.
";
}
$from = $header['fromaddress'];
$subject = $header['subject'];
$ddate = $header['date'];
if (preg_match('/^[^<>]+<([^<>]+)>$/',$from,$matches))
{
$from = $matches[1];
}
for ($j=1; $j <= $body['parts']; $j++)
{
$content = $content . get_content($body, $j);
}
//date reformating
$post_date = date('Y-m-d H:i:s', time($ddate)); // + $time_difference);
$post_date_gmt = gmdate('Y-m-d H:i:s', time($ddate) );
//filter content
$search = array (
'/ (\n|\r\n|\r)/',
'/(\n|\r\n|\r)/'
);
$replace = array (
' ',
"\n"
);
// strip extra line breaks
$content = preg_replace($search,$replace,trim($content));
$post_categories = array();
// if subject line begins with text enclosed in brackets, use that text as
// the category (i.e. "Subject: [food] funkyness" --> Category: food)
if ( preg_match( '#^\[(.+)\](.+)$#', $subject, $matches ) )
{
$post_categories[0] = $matches[1];
$subject = trim($matches[2]);
}
// always put it in the moblog category.
// change this as you please
if ( !isset( $post_categories[0] ) ||
strcmp( $post_categories[0], "moblog" ) != 0 )
{
array_push( $post_categories, moblog );
}
// to force into other categories, add below line for each category
// (don't put category name in quotes; see above example)
//array_push( $post_categories, CATEGORY_NAME );
// 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 . '
'."\n";
$sql = 'SELECT id FROM '.$tableusers.' WHERE user_email=\'' . addslashes($from) . '\'';
// from: email address must be registered in wordpress and with valid access
// levels (I think '4' allows for posting)
if (!$poster = $wpdb->get_var($sql))
{
echo 'INVALID SENDER: ' . htmlentities($from) . "
\n";
continue;
}
$post_name = sanitize_title( $subject );
$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' => $post_name
);
//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;
if ($debug) {
echo "
".$sql."
";
if ($result) {echo "SUCCESS
";}
else{"FAIL
";}
}
// sanitize_title makes nasty post_name for UTF-8, so replace with post_ID
$sql = 'UPDATE '.$tableposts.' SET post_name='.$post_ID.' WHERE ID='.$post_ID;
$result = $wpdb->query($sql);
if ($debug) {
echo "
".$sql."
";
if ($result) {echo "SUCCESS
";}
else{"FAIL
";}
}
do_action('publish_post', $post_ID);
do_action('publish_phone', $post_ID);
pingback($content, $post_ID); // probably not needed, but keep it anyways
// Store image meta data
$sql = "INSERT INTO $tablepostmeta (post_id,meta_key,meta_value) VALUES ('$post_ID','url_image','$url_image')";
$result = $wpdb->query($sql);
if ($debug) {
echo "
".$sql."
";
if ($result) {echo "SUCCESS
";}
else{"FAIL
";}
}
/* not using thumbnails for now */
// $sql = "INSERT INTO $tablepostmeta (post_id,meta_key,meta_value) VALUES ('$post_ID','url_thumb','$url_thumb')";
//$result = $wpdb->query($sql);
// echo "SQL query: ".$sql."
";
// what exactly does this do? undocumented in the codex, but seems important
add_meta($post_ID);
// Store into respective categories
foreach ($post_categories as $post_category) {
// MLC, if category is a string changeover to id
if ( !preg_match( '#[0-9]+#', $post_category ) ) {
$catIdSql = "
SELECT cat_ID
FROM $tablecategories
WHERE 1 = 1
AND cat_name = '$post_category'";
$id = $wpdb->get_var( $catIdSql );
// grab whichever is valid
$post_category = ( !is_null( $id ) ) ? $id : $post_categories[0];
}
$post_category = intval($post_category);
// Double check it's not already there
$sql = "SELECT * FROM $tablepost2cat WHERE post_id = $post_ID AND category_id = $post_category";
if ($debug) {echo "SQL query: ".$sql."
";}
$exists = $wpdb->get_row($sql);
if ($debug) {
echo "
".$sql."
";
if ($exists) {echo "EXISTS
";}
else{"DOES NOT EXIST
";}
}
if (!$exists && $result) {
$sql = "INSERT INTO $tablepost2cat (post_id, category_id) VALUES ($post_ID, $post_category)";
$result = $wpdb->query($sql);
if ($debug) {
echo "
".$sql."
";
if ($result) {echo "SUCCESS
";}
else{"FAIL
";}
}
}
} // end foreach for post_categories
} // end looping over messages
if (!$mbox->quit()) {
echo "ERROR: quiting failed (!)";
} else {
echo "
Connection Closed