<?
error_reporting(E_ALL);
$itemcount = 0;
$maxitems = 0;
$cfile = 0;
$hostname = "";
$uri = "";
$port = 80;
$inchannel = 0;
$initem = 0;
$intitle = 0;
$intxtinput = 0;
$inlink = 0;
$inname = 0;
$chtitle = "";
$churl = "";
$ittitle = "";
$iturl = "";
$tiname = "";
$tiurl = "";
$tititle = "";
function writeHeader($ofile, $title, $url, $rdf)
{
fputs($ofile, "<table cellpadding=0 cellspacing=0 border=0 bgcolor=\"#000000\" width=\"100%\"><tr><td>\n");
fputs($ofile, "<table cellpadding=4 cellspacing=1 border=0 width=\"100%\">\n");
fputs($ofile, "<tr><td bgcolor=\"#000000\" background=\"Images/news_title_background.gif\"><p>\n");
fputs($ofile, "<A HREF=\"$rdf\"><IMG SRC=\"Images/icons/file-text2.gif\" width=10 height=14 BORDER=0 ALT=\"[ Dowload RDF ]\" align=\"right\"></A>");
fputs($ofile, "<A class=\"NBT\" HREF=\"$url\"><font size=\"3\">$title</font></A></p></td>");
fputs($ofile, "<tr><td bgcolor=\"#cccccc\">\n");
}
function writeItem($ofile, $item, $url)
{
global $itemcount;
global $maxitems;
$itemcount++;
if($maxitems > 0 && $itemcount > $maxitems) return;
if($item != "") {
$str = ereg_replace("&*#37;", "'", $item);
$str = ereg_replace("&*#38;", "", $str);
$str = ereg_replace("&*#34;", "\"", $str);
$str = ereg_replace("'", "'", $str);
fputs($ofile, "<A HREF=\"$url\" class=\"NBI\"><font size=\"2\"><IMG SRC=\"/Images/icons/bullet1.gif\" width=8 height=8 border=0 hspace=3 vspace=0>$str</font></A>\n");
}
}
function writeTextInput($ofile, $title, $name, $url)
{
fputs($ofile, "</td></tr><tr><FORM method=\"GET\" action=\"$url\">");
fputs($ofile, "<td bgcolor=\"#cccccc\" align=\"center\">");
fputs($ofile, "<font size=2>$title <input type=\"text\" size=10 name=\"$name\"></font>");
}
function finishNews($ofile, $filedate, $gotti)
{
if($gotti == "") {
fputs($ofile, "</td></tr>");
} else {
fputs($ofile, "</td></form></tr>");
}
fputs($ofile, "<tr><td class=\"NBC\"><font size=\"1\">Update: $filedate");
}
function writeFooter($comment)
{
echo " $comment</font></td></tr>\n</table></td></tr></table>\n";
}
function startElement($parser, $name, $attrs)
{
global $inchannel;
global $initem;
global $intitle;
global $intxtinput;
global $inlink;
global $inname;
global $chtitle;
global $churl;
global $ittitle;
global $iturl;
global $cfile;
global $hostname;
global $uri;
global $port;
if($name == "CHANNEL") {
$inchannel = 1;
} elseif($name == "TITLE") {
$intitle = 1;
} elseif($name == "ITEM") {
if($inchannel) {
$inchannel = 0;
writeHeader($cfile, $chtitle, $churl,
"http://" . $hostname . ":" . $port . $uri);
}
if($initem) writeItem($cfile, $ittitle, $iturl);
$initem = 1;
} elseif($name == "LINK") {
$inlink = 1;
} elseif($name == "NAME") {
$inname = 1;
} elseif($name == "TEXTINPUT") {
$intxtinput = 1;
}
}
function endElement($parser, $name)
{
global $inchannel;
global $initem;
global $intitle;
global $intxtinput;
global $inlink;
global $inname;
global $chtitle;
global $churl;
global $ittitle;
global $iturl;
global $cfile;
global $hostname;
global $uri;
global $port;
if($name == "CHANNEL") {
if($inchannel) writeHeader($cfile, $chtitle, $churl,
"http://" . $hostname . ":" . $port . $uri);
$inchannel = 0;
$initem = 0;
} elseif($name == "TITLE") {
$intitle = 0;
} elseif($name == "ITEM") {
$initem = 0;
writeItem($cfile, $ittitle, $iturl);
} elseif($name == "LINK") {
$inlink = 0;
} elseif($name == "NAME") {
$inname = 0;
} elseif($name == "TEXTINPUT") {
$intxtinput = 0;
}
}
function characterData($parser, $data)
{
global $inchannel;
global $initem;
global $intitle;
global $intxtinput;
global $inlink;
global $inname;
global $chtitle;
global $churl;
global $ittitle;
global $iturl;
global $tiname;
global $tiurl;
global $tititle;
$str = ltrim(Chop($data));
if($str == "") return;
if($intitle) {
if($inchannel) $chtitle = $str;
elseif($initem) $ittitle = $str;
elseif($intxtinput) $tititle = $str;
} elseif($inlink) {
if($inchannel) $churl = $str;
elseif($initem) $iturl = $str;
elseif($intxtinput) $tiurl = $str;
} elseif($inname) {
if($intxtinput) $tiname = $str;
}
}
function defaultHandler($parser, $data)
{
}
function PIHandler($parser, $target, $data)
{
}
function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
$publicId)
{
return true;
}
function parserdf($hn, $ur, $p = 80, $m = 0)
{
global $itemcount;
global $maxitems;
global $cfile;
global $hostname;
global $uri;
global $port;
global $inchannel;
global $initem;
global $intitle;
global $intxtinput;
global $inlink;
global $inname;
global $chtitle;
global $churl;
global $ittitle;
global $iturl;
global $tiname;
global $tiurl;
global $tititle;
$hostname = $hn;
$uri = $ur;
$port = $p;
$maxitems = $m;
$itemcount = 0;
$cfile = 0;
$inchannel = 0;
$initem = 0;
$intitle = 0;
$intxtinput = 0;
$inlink = 0;
$inname = 0;
$chtitle = "";
$churl = "";
$ittitle = "";
$iturl = "";
$tiname = "";
$tiurl = "";
$tititle = "";
$header_passed = 0;
$cache = "cache." . $hostname . ".txt";
$comment = "(cached)";
if(file_exists($cache)) {
$ctime = fileMtime($cache);
if(fileSize($cache) < 20) $ctime = -1;
} else $ctime = -1;
$ntime = Time();
$dtime = $ntime - $ctime;
$uncache = 0;
$linelengh = 1024;
$filedate = "";
if(isset($nocache) || $dtime > $ntime || $dtime > 1800) {
$cfile = fopen($cache, "w");
$fpread = fsockopen("$hostname", $port, &$errno, &$errstr);
if(!$fpread) {
echo "Error connecting to $hostname: $errstr<br>\n";
return;
} else {
fputs($fpread, "GET $uri HTTP/1.0\n");
fputs($fpread, "User-Agent: PHP RDFParser/0.2 mkrus@free.fr\n");
fputs($fpread, "Host: " . $hostname . ":80\n");
fputs($fpread, "Accept: */*\n\n");
$str = fgets($fpread, $linelengh);
$id = strstr($str, "404");
if($id != "") {
echo "Document <A HREF=\"http://$hostname$uri\">http://$hostname$uri</A> not found\n";
return;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_default_handler($xml_parser, "defaultHandler");
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
fputs($fpread, "GET $uri HTTP/1.0\n");
fputs($fpread, "User-Agent: PHP RDFParser/0.2 mkrus@free.fr\n");
fputs($fpread, "Accept: */*\n\n");
while (!feof($fpread) && $header_passed == 0) {
$str = ltrim(Chop(fgets($fpread, $linelengh)));
if ($str == "") $header_passed = 1;
}
while ($data = fread($fpread, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fpread))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
if($tiurl != "") {
writeTextInput($cfile, $tititle, $tiname, $tiurl);
}
if($filedate == "") {
$ctime = fileMtime($cache);
$filedate = Date("G:i m/d/y", $ctime);
}
if($chtitle == "" && $inchannel == 0) {
writeHeader($cfile, $hostname,
"http://" . $hostname . ":" . $port . $uri,
"http://" . $hostname . ":" . $port . $uri);
}
if($itemcount == 0) {
fputs($cfile, "No items found");
$uncache = 1;
}
finishNews($cfile, $filedate, $tiurl);
fclose($fpread);
}
fclose($cfile);
$comment = "";
}
include $cache;
writeFooter($comment);
if($uncache) {
unlink($cache);
}
unset($header_passed);
unset($cache);
unset($comment);
unset($ctime);
unset($ntime);
unset($dtime);
unset($uncache);
unset($linelengh);
unset($filedate);
unset($xml_parser);
}
function getNextToken($ofile)
{
static $char = "";
$token = "";
$intoken = 0;
$intag = 0;
while (1) {
if($char == "") {
$char = fgetc($ofile);
if(feof($ofile)) break;
}
if($char == "\n") {
$char = "";
continue;
}
if(($char == " " || $char == "\t") && $intag != 0) {
$char = "";
continue;
}
if($intoken == 0) {
if($char == "<") $intag = 1;
else $intag = 0;
$intoken = 1;
$token .= $char;
} else {
if($intag == 1) {
$token .= $char;
if($char == ">") {
$char = "";
break;
}
} else {
if($char == "<") break;
$token .= $char;
}
}
$char = "";
}
return ltrim(Chop($token));
}
The syntax highlighted source is automatically generated by PHP from the plaintext script.
If you want to see the source of this page, have
a look here.