<?php if (!defined('PmWiki')) exit();

##############################
# twitterposts.php
# code gathered and modified for PmWiki by Adam Overton, July 2009
# original code was gathered from Gareth Rushgrove's website,
# http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# this code allows an admin to easily post updates to a Twitter account.
# call the function with the appropriate username, password, and the message:
# 		twitterPostMsg_func($twitter_username, $twitter_password, $twitterMsg, $verbose);
# ... and that's it.
#
# this author is currently using it in coordination with a cron job that calls the code daily,
# and sends tweets listing any newly created pages. (by updating at the end of the day, as opposed to 
# immediately after the page has been posted, visitors are less likely to be directed to a page still 
# under construction, and allows the editor to set up PTV's to be included in the tweet, i.e. {$:Description}, etc)
#
# there are many more twitter api libraries out there that allow all sort of interaction with Twitter,
# but this was the simplest, most straightforward code for simply sending updates that i found online.
# feel free to explore those as well, and feel free to improve this code ...
##############################

$RecipeInfo['TwitterPost']['Version'] = '2009-08-03';

// Set username and password
# $twitter_username = 'username';
# $twitter_password = 'password';
# $twitterMsg = "test using twitterPost function at $Now";

function twitterPostMsg_func ($username, $password, $message, $verbose=false) {
	# must supply all three arguments!
	if (!$username || !$password || !$message) {
		echo "Twitter post failed: <br />";
		if (!$username) echo "No username provided!<br />";
		if (!$password) echo "No password provided!<br />";
		if (!$message) echo "No message provided!<br />";
		return false;
	}

	// The twitter API address
	//$url = 'http://twitter.com/statuses/update.xml';
	// Alternative JSON version
	$url = 'http://twitter.com/statuses/update.json';

	// Set up and execute the curl process
	$curl_handle = curl_init();
	curl_setopt($curl_handle, CURLOPT_URL, "$url");
	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_handle, CURLOPT_POST, 1);
	curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
	curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
	$buffer = curl_exec($curl_handle);
	curl_close($curl_handle);

	// check for success or failure
	if (empty($buffer)) {
		if ($verbose) {
			echo 'TWITTER POST FAILED<br />';
			echo "$buffer<br />";
		}
		return false;  # if it failed
	} else {
		if ($verbose) {
			echo 'TWITTER POST SUCCESSFUL<br />';
			//echo "success $buffer";
		}
		return true;
	}

}