#!/usr/bin/php -q

<?

/* AUTOPOST
* AUTOPOST is a script that reads an external RSS file, parses the items, formats them
* as blog entries and finally posts them to a blog via XML-RPC calls to metaWeblog API 
* procedures.
*/

// This version (autopost2.php) has been modified to work with the Blogger API
// implementation on a pMachine install 

/* LIBRARIES
* You need these external libraries for this script to work. You'll also need to
* change these paths below to point to where the libraries live on your server.
*/
// XML-RPC library 
include("/var/www/html/phplib/xmlrpc-1.0.99.2/xmlrpc.inc");
// Magpie is an RSS parser for PHP
require_once('/var/www/html/phplib/magpierss-0.5.2/rss_fetch.inc');
// If you have a new enough version of PHP, you can just comment this line out
// If you have an older version (like me) you'll have to roll your own function for
// file_get_contents (you can find them on php.net to cut n' paste).
require('/var/www/html/phplib/file_get_contents.inc');

/* USERNAME/PASSWORD
* Change these variables to the appropriate values for the blog you wish to post to.
* You might want to create a new username, or you could just have the script post 
* as "you", using your own username.
*/
$username "autopost";
$password "pass123";

// RSS INFO
// Change '$url' to the URL of the RSS feed you want to grab
$url 'http://www.searchenginelowdown.com/rss/seo_news_feed';

// Change local to the full path of the file to which you want to write
// the raw grabbed RSS. This file MUST already exist ("touch" it to create)
// Also, you need to make sure that the script has the permission to write
// to this file (take the easy way out and do a 'chmod a+w file.xml')
$local '/var/www/html/autopost/rsscache/searchengine.xml';

// OPEN LOGFILE
// The log file MUST exist too. See above to create.
// Change the path to point to the file you create.
$logfilename '/var/www/html/autopost/log';
$log fopen($logfilename"a");
$logtimestamp date('Y-m-d H:i:s');
fwrite($log"- - - - -\n");
fwrite($log"START SESSION: $logtimestamp\n");
// IMPORTANT: For log file clarity, you want to change this line below so
// you know what RSS file is being referred to
fwrite($log"RSS ID: Search Engine Lowdown\n");

// RSS FILE COMPARE TO CACHE
$current file_get_contents($url);
$cache file_get_contents($local);

$freshtest strcmp($current$cache);
if (
$freshtest == 0) { // files are the same, nothing new to post
    
fwrite($log"CACHE: remote and local files are the same\nEXIT: cache freshness\n");
    
fwrite($log"- - - - -\n");
    
fclose($log);
    die; 
// so we exit here if there's nothing new
}

// otherwise, the current file is newer, so we have to
// update the cached version

// UPDATE CACHE

$handle fopen($local"w");
fwrite($handle$current);

// RSS PARSING
// num_items represents the number of items you want from the RSS file. If set to 1, you get 
// the first item in the RSS files. That's what you'll usually want (since it'll be the 
// newest)
$num_items 1;
$rss fetch_rss($url);

$blogname $rss->channel['title'];
$bloglink $rss->channel['link'];

$items array_slice($rss->items0$num_items);

foreach (
$items as $item ) {
    
$posttitle $item[title];
    
$posturl   $item[link];
    
// this doesn't work RSS 2.0 $date = $item[pubDate];
    
$postdesc "<title>$posttitle</title>";
    
$postdesc .= "<body>From <a href=$bloglink>$blogname</a>:<p>$item[description]... ";
    
$postdesc .= "[<a href=$posturl>read the full post</a>]";
    
$postdesc .= "</body>";
    
$postdesc .= "<category>Search Engine Marketing</category>";    


    
// DEBUG
    //echo "<p><a href=$posturl>$posttitle</a><br>\n";
    //echo "$postdesc<br><br>\n";
    //echo "From <a href=$bloglink>$blogname</a>\n";
}    

// grab the timestamp and format it for ISO8601
$timestamp date("Ymd\TH:i:s");

// do we need an appkey? Yes we do! Nice of pMachine to let us know! </sarcasm>
//$appkey = "C6CE3FFB3174106584CBB250C0B0519BF4E294";
$appkey "NETSCIAUTOPOSTINGSYSTEM000000000000000";

// XML-RPC TIME (ouch!)
// marshalling data into XML-RPC types
$xappkey = new xmlrpcval($appkey$xmlrpcString);
$xblogid = new xmlrpcval("1"$xmlrpcString);
$xusername = new xmlrpcval($username$xmlrpcString);
$xpassword = new xmlrpcval($password$xmlrpcString);
$xcontent = new xmlrpcval($postdesc$xmlrpcString);
//$xcontent = new xmlrpcval(array(
//    "title" => new xmlrpcval($posttitle, $xmlrpcString),
//    "description" => new xmlrpcval($postdesc, $xmlrpcString),
//    "dateCreated" => new xmlrpcval($timestamp, $xmlrpcDateTime)), $xmlrpcStruct);
$xpublish = new xmlrpcval("1"$xmlrpcBoolean);

// instantiate the client
$client = new xmlrpc_client("/blog/pm/pmserver.php""www.ablog.com""80");

// prepare the invocation
$msg = new xmlrpcmsg("blogger.newPost", array($xappkey$xblogid$xusername$xpassword$xcontent$xpublish));

// send that sucker
$result $client->send($msg);

// error checking of result
if ($result) { // no level error occured
    
if ($result->value()) { // no xmlrpc error has occurred
        // use shortcut function to lazily decode a scalar
        
$val=xmlrpc_decode($result->value());
        
// LOG: SUCCESS print it to logfile instead
        
fwrite($log"XML-RPC: successful communication with server - post id ${val}\n");
        
// HTML print "Got this result: ${val}<br />\n";
    
} else {
        
// xmlrpc error handling
        // LOG: ERROR print it to logfile instead
        
$xmlfaultcode $result->faultCode();
        
$xmlfaultstring $result->faultString();
        
fwrite($log"XML-RPC: error code $xmlfaultcode: $xmlfaultstring\n");
        
//print "A fault has occured, code number: " .
        //$result->faultCode() . ", explanation: " .
        //$result->faultString() . "<br />\n";
    
}
} else {
    
// low level error handling
    // LOG: ERROR print it to logfile instead
    
$lowlevelerrno $client->errno;
    
$lowlevelerrstr $client->errstr;
    
fwrite($log"XML-RPC: low level error code $lowlevelerrno: $lowlevelerrstr\n");
    
fwrite($log"EXIT: low level error\n- - - - -\n");
    
//print "Help! A low level error occured. Error # " .
    //$client->errno . " : " . $client->errstr . "<br />\n";
    
fclose($log);
    exit(
0);
}
fwrite($log"EXIT\n- - - - -\n");
fclose($log);


?>