<?php
$username = 'username';
$password = 'password';
$filename = '/path/to/textfile';
$script_home = '/path/to/script/dir';
$desc = 'file has changed:';
$show_contents = true;
// Don't change anything down here
function twitter_update($username, $password, $message) {
$c = curl_init();
$message = str_replace(' ', '+', $message);
$message = str_replace("\n", '+', $message);
curl_setopt($c, CURLOPT_TRANSFERTEXT, true);
curl_setopt($c, CURLOPT_URL, 'http://twitter.com/statuses/update.json');
curl_setopt($c, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'status='.$message);
curl_exec($c);
curl_close($c);
}
if (!file_exists($script_home.'/datafile') || (md5_file($script_home.'/datafile') != md5_file($filename))) {
if (!copy($filename, $script_home.'/datafile')) {
die('Unable to copy file for local storage; '.$script_home.' needs world-writeable permissions');
}
$data = $desc;
if ($show_contents) {
$readlen = 140 - strlen($desc) - 1;
$rsrc = fopen($script_home.'/datafile', 'r');
if ($rsrc === false) die('Unable to open file; check permissions?');
echo 'reading';
$innards = fread($rsrc, $readlen+1);
if (strlen($innards) > $readlen) {
$innards = substr($innards, 0, -4);
$innards .= '...';
}
fclose($rsrc);
$data .= ' '.$innards;
echo $data;
}
twitter_update($username, $password, $data);
}
?>