array( 'proxy' => 'tcp://127.0.0.1:3128', 'request_fulluri' => true, ), )); $stcxHEAD = stream_context_create(array( 'http' => array( 'method' => 'HEAD', 'proxy' => 'tcp://127.0.0.1:3128', 'request_fulluri' => true, ), )); $source = file_get_contents("http://www.homestarrunner.com/rando.xml", false, $stcx); $source = str_replace("&", "&", $source); $source = preg_replace('/\?>/', "?>\n", $source) . "\n"; try { $db = new PDO('sqlite:homestarrunner.db'); } catch( PDOException $exception ) { die($exception->getMessage()); } $db->exec('CREATE TABLE last_mods (URL varchar(255), value int(10))'); function get_timestamp($url) { global $db; $stmt = $db->prepare('SELECT value FROM last_mods WHERE url = :url'); $stmt->bindParam('url', $url); $stmt->execute(); if ($result = $stmt->fetch()) { return $result['value']; } global $stcxHEAD; file_get_contents($url, false, $stcxHEAD); $timestamp = 0; foreach ($http_response_header as $line) { $matches = null; if (preg_match('/Last-Modified: (.*)/i', $line, $matches)) { $timestamp = strtotime($matches[1]); break; } } $stmt = $db->prepare('INSERT INTO last_mods (URL, value) VALUES (:url, :value)'); $stmt->bindParam(':url', $url); $stmt->bindParam(':value', $timestamp); $stmt->execute(); return $timestamp; } $data = array(); $counts = array(); $names = array(); $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "xmlStartElement", "xmlEndElement"); xml_set_default_handler($xml_parser, "xmlDefault"); if (!xml_parse($xml_parser, $source, true)) { header('Content-Type: text/plain'); 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); function xmlDefault($parser, $data) { $data = trim($data); if (!$data) return; global $names; $matches = null; if (preg_match('/^$/', $data, $matches)) { $key = $matches[3]; $value = $matches[1]; if ($value == 'sbemail') { $value = 'Strongbad Email'; } if (strtolower($value) == $value) { $value = ucwords($value); } $names[$key] = $value; } else { header('Content-Type: text/plain'); die("Unexpected data: " . $data); } } function xmlStartElement($parser, $name, $attrs) { global $counts, $data, $names; $name = strtolower($name); if ($name != "root") { if (!array_key_exists($name, $counts)) { $counts[$name] = 0; } $counts[$name]++; if (!array_key_exists($name, $names)) { header('Content-Type: text/plain'); die('Unknown category type: ' . $name); } $item = array(); $item['category'] = $name; $item['title'] = $names[$name] . ' ' . $counts[$name] . ': ' . $attrs['N']; $item['url'] = 'http://www.homestarrunner.com/' . ( array_key_exists('U', $attrs) ? $attrs['U'] : ("sbemail".$counts[$name].".html") ); $swf = preg_replace('/\.html$/', '.swf', $item['url']); $item['timestamp'] = get_timestamp($swf); $data[] = $item; } } function xmlEndElement($parser, $name) { } //uasort($data, 'by_timestamp_desc'); require("_lib/xmlwriterclass.php"); require("_lib/rss_writer_class.php"); $rss_writer_object = &new rss_writer_class; $rss_writer_object->specification='2.0'; $rss_writer_object->about=$_SERVER['SCRIPT_URI']; $rss_writer_object->inputencoding="utf-8"; $properties=array(); $properties['description']=''; $properties['link']='http://www.homestarrunner.com/'; $properties['title']='Homestar Runner'; $properties['ttl']=1; $rss_writer_object->addchannel($properties); foreach ($data as $item) { $properties=array(); $properties['description']=''; $properties['link']=$item['url']; $properties['author']='Homestar Runner'; $properties['title']=$item['title']; $properties['pubDate']=date('r', $item['timestamp']); $properties['guid']=$item['url']; $properties['category'] = $item['category']; $rss_writer_object->additem($properties); } if($rss_writer_object->writerss($output)) { $timestamp = 0; foreach ($data as $item) { $timestamp = max($timestamp, $item['timestamp']); } last_modified_check($timestamp); expire_in(0); header('Content-Type: text/xml; charset="'.$rss_writer_object->outputencoding.'"'); header('Content-Length: '.strval(strlen($output))); echo $output; } else { header('Content-Type: text/plain'); echo ('Error: '.$rss_writer_object->error); } ?>