<?php
/*  
ปรับปรุง : 2552-05-09
rss.php ใช้อ่านแฟ้ม rss แล้วแปลงเป็น html เพื่อแสดงในเว็บเพจ
- ทดสอบกับ RSS.XML เปิดให้ใช้จากเว็บไซต์ต่าง ๆ
- โปรแกรมนี้ใช้กับบาง web server ไม่ได้ เพราะไม่อนุญาติให้ใช้ fopen 
- ยกเลิกการทดสอบใน thaiabc.com เพราะใช้ fopen ไม่ได้ (IIS on godaddy.com)
- แก้ไขภาษาไทยแบบ UTF-8 ของแฟ้ม rss ใน gotoknow.org ทั้ง meta และ xml_parser_create
=======================================
PHP RSS Reader v1.1
By Richard James Kendall
Code updated from : http://www.rjk-hosting.co.uk/programs/prog.php?id=7
*/
set_time_limit(0); // 0 for Unlimied but default for Maximum execution time is 30 seconds 
$far = array();
$i=0;
$far[$i++] = "http://www.manager.co.th/RSS/Entertainment/Entertainment.xml";
$far[$i++] = "http://slashdot.org/index.rss";
$far[$i++] = "http://www.microsite.reuters.co.uk/rss/uk/topNews";
$far[$i++] = "http://www.neowin.net/backend.php?page=main";
$far[$i++] = "http://gotoknow.org/blog/thaiall/rss20.xml?tag=article";
$far[$i++] = "http://gotoknow.org/blog/thaiall/rss20.xml"; 
$far[$i++] = "http://www.rssthai.com/rss/education.xml";
$far[$i++] = "http://www.bangkokbiznews.com/rss/index.xml";
if(isset($_GET["url"])) $file=$_GET["url"]; else $file = $far[0];
if (substr($file,0,19) == "http://gotoknow.org") $charset = "UTF-8"; else $charset = "windows-874";
echo "<html><head><title>rss feeder</title>
<meta http-equiv=content-type content='text/html; charset=". $charset ."'></head><body>";
if (isset($_GET["action"]) && $_GET["action"] == "source") {
  $ar = file("rss.php");
  echo "<body bgcolor=#ffffdd><pre><font face='courier new'>";
  foreach($ar as $v) echo str_replace("<","<",$v);
  exit;
}
// ------------------
echo "<form action='' method=get>
<input name=url size=70 value='http://www.manager.co.th/RSS/Entertainment/Entertainment.xml'>
<input type=submit></form>";
echo "<form action='' method=get><select name=url>";
foreach($far as $v) {     
	echo "<option value=$v ";
    if (isset($_GET["url"]) && $_GET["url"] == $v) echo "selected";
	echo ">$v";
}
echo "<input type=submit></form>";
$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;
function startElement($parser, $name, $attrs) {
  global $rss_channel, $currently_writing, $main;
  switch($name) {
  case "RSS":
  case "RDF:RDF":
  case "ITEMS":
   $currently_writing = "";
   break;
  case "CHANNEL":
   $main = "CHANNEL";
   break;
  case "IMAGE":
   $main = "IMAGE";
   $rss_channel["IMAGE"] = array();
   break;
  case "ITEM":
   $main = "ITEMS";
   break;
  default:
   $currently_writing = $name;
   break;
 }
}
function endElement($parser, $name) {
 global $rss_channel, $currently_writing, $item_counter;
 $currently_writing = "";
 if ($name == "ITEM") {
  $item_counter++;
 }
}
function characterData($parser, $data) {
 global $rss_channel, $currently_writing, $main, $item_counter, $file;
 if ($currently_writing != "") {
  switch($main) {
   case "CHANNEL":
    if (isset($rss_channel[$currently_writing])) {
     $rss_channel[$currently_writing] .= $data;
    } else {
     $rss_channel[$currently_writing] = $data;
    }
    break;
   case "IMAGE":
    if (isset($rss_channel[$main][$currently_writing])) {
     $rss_channel[$main][$currently_writing] .= $data;
    } else {
     $rss_channel[$main][$currently_writing] = $data;
    }
    break;
   case "ITEMS":
    if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
     $rss_channel[$main][$item_counter][$currently_writing] .= $data;
    } else {
     $rss_channel[$main][$item_counter][$currently_writing] = $data;
    }
    break;
   }
 }
}
// old $xml_parser = xml_parser_create();
if ($charset == "UTF-8") $xml_parser = xml_parser_create("UTF-8"); else $xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen("$file", "r"))) { die("could not open XML input"); }
while ($data = fread($fp, 4096)) {
 if (!xml_parse($xml_parser, $data, feof($fp))) {
  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);

// output as HTML
if (isset($rss_channel["ITEMS"])) {
 if (count($rss_channel["ITEMS"]) > 0) {
  for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
    $t = $rss_channel["ITEMS"][$i]["TITLE"];
    $l = $rss_channel["ITEMS"][$i]["LINK"];
    // htmlspecialchars(utf8_encode($string), ENT_QUOTES);
    print ("\n<table width=\"100%\" border=\"0\"><tr><td width=\"100%\">".
    "<font face='microsoft sans serif'><a href=\"". $l ."\" target=\"_blank\">". $t . "</a>");
    print ("" . html_entity_decode($rss_channel["ITEMS"][$i]["DESCRIPTION"]) . "");
    print ("</td></tr></table>");
  }
 } else {
  print ("<b>There are no articles in this feed.</b>");
 }
}
print ("<br><a href=?action=source>.</a></body></html>");
?>