// lib blather v0.3
// a set of PHP functions to seemlessly integrate with blather.newdream.net
// by user24 - www.puremango.co.uk
/*
supports:
-reading blathes
-adding blathes
-recent
-who
-go
-check if word exists
all functionality supports both red and blue editions
// version history:
0.1 - first working version
0.2 - added detection for blathes
0.3 - added detection for blathes in red (0.2 broke red)
*/
define(BLATHER_LOCATION,"blather.newdream.net");
function http_request($host,$port,$out,$return_array=false)
{
// used internally to send and receive http headers/server response
// initialise some variables
$headersEnded = 0;
// open connection
$fsock = fsockopen($host, $port,$errno, $errstr,10);
if(!$fsock)
{
//echo "$errstr ($errno)
";
return false;
} else {
// send headers to server
fputs($fsock, $out);
while(!feof($fsock))
{
// grab data
$returned_data = fgets($fsock, 256);
//echo "rawdat:[".htmlspecialchars($returned_data)."]
";
// is it the first blank line we've received?
// (if so, that's the end of the returned headers)
if($headersEnded == 0 && ($returned_data=="\r\n" || $returned_data=="\n" || $returned_data=="\r"))
{
$returned_data = "";
$headersEnded = 1;
}
if($headersEnded == 1)
{
// store returned data as one string, for later output
$data .= $returned_data;
} else {
$headers .= $returned_data;
}
}
// close connection
fclose($fsock);
if($return_array)
{
return Array($headers,$data);
} else {
return $data;
}
}
}
function make_blathe($word,$entry,$name,$email="",$edition="")
{
// asks blather to create a new blathe or new blathe entry
// eg: make_blathe("fish","fish are nice to eat","fishman","fishman@yahoo.com");
// eg: make_blathe("fish","fish are nice to eat","fishman");
// eg: make_blathe("fish","fish are nice to eat","fishman","","red");
// email address MUST be valid or blather won't allow it.
// error check:
if(empty($word) || empty($entry) || empty($name))
{
return false;
}
if($edition=="red")
{
$edition = "/edition=red";
} else {
$edition = "";
}
// prepare headers to send
$out = 'POST /cgi-bin/blather'.$edition.'?add HTTP/1.0'."\r\n";
$out .= 'Accept-Encoding: '."\r\n";
$out .= 'X-Script: lib_blather0.3 from puremango.co.uk'."\r\n";
$out .= 'Host: '.BLATHER_LOCATION."\r\n";
$out .= 'Referer: http://'.BLATHER_LOCATION."/cgi-bin/blather".$edition."?addform;word=".$word."\r\n";
$out .= 'Connection: close'."\r\n";
$out .= 'Content-Type: application/x-www-form-urlencoded'."\r\n";
$postData = 'word='.$word.'&entry='.$entry.'&name='.$name.'&email='.$email;
$out .= 'Content-Length: '.strlen($postData)."\r\n";
$out .= ''."\r\n";
$out .= $postData."\r\n";
// terminate headers
$out .= "\r\n";
http_request(BLATHER_LOCATION,80,$out);
return true;
}
function who_blathes($who,$email="",$edition="")
{
// returns array of files, indexed by page name that $who has blathed to
// eg: who("u24");
// eg: who("u24","email@address.com");
// eg: who("u24","email@address.com","red");
// eg: who("u24","","red");
if($edition=="red")
{
$edition = "/edition=red";
} else {
$edition = "";
}
if(!empty($email))
{
$email = ";email=".urlencode($email);
} else {
$email = "";
}
$who_blathes = file("http://".BLATHER_LOCATION."/cgi-bin/blather".$edition."?who;name=".urlencode($who).$email);
$lines = Array();
if(!is_array($who_blathes))
{
return false;
}
foreach($who_blathes as $line)
{
if(strpos($line,''));
// and the name of the page
$page_name = substr($line,strrpos($line,'/')+1);
$page_name = substr($page_name,0,strpos($page_name,'.html'));
// and assign the array space.
$lines[$page_name] = $line;
}
}
return $lines;
}
function recent($edition="")
{
if($edition=="red")
{
$edition = "/edition=red";
} else {
$edition = "";
}
$recent = file("http://".BLATHER_LOCATION."/cgi-bin/blather".$edition."?recent");
$lines = Array();
$index = "today";
foreach($recent as $line)
{
if($index=="today" && trim($line)=="