Skip to content

Instantly share code, notes, and snippets.

@nicolish
Created December 22, 2008 08:53
Show Gist options
  • Save nicolish/38931 to your computer and use it in GitHub Desktop.
Save nicolish/38931 to your computer and use it in GitHub Desktop.
<?php
class Config{
const TITLE = 'わたしのほうむぺいじ☆';
const URL = 'http://pc11.2ch.net/test/read.cgi/php/1222645553/';
const DESCRIPTION = 'わたしのさいとのすてきな掲示板です☆ミ';
const GENERATOR = '俺様の書いた最強RSSジェネレーター';
/*
* RSSヘッダの画像ファイル名。
*/
const RSS_IMAGE = 'cat1.gif';
/*
* RSSに載せる件数。
*/
const ITEM_AMOUNT = '40';
/*
* 記事中のアイコン表示と、RSSヘッダの画像を格納するフォルダ。
*/
const IMAGE_DIR_URL = 'http://localhost/ibbs/Icon/';
const LOG_FILE = 'ibbs.dat';
const LOG_DIR = './';
}
class Node{
private $name;
private $content;
private $a_attributes = array();
private $a_children = array();
function appendChild($node){
$this->a_children[] = $node;
}
function asXml($level = 0){
$return = '<' . $this->name . $this->getJoinedAttributes() . '>';
if(null === $this->content){
$return .= "\n";
}
foreach($this->a_children as $child){
for($i=0;$i<$level;$i++){
$return .= ' ';
}
$return .= $child->asXml($level+1);
}
if(null !== $this->content){
$return .= $this->content;
}
$return .= '</' . $this->name . ">\n";
return $return;
}
function __construct($name, $content = null){
$this->name = $name;
$this->content = $content;
}
function setAttribute($key, $name){
$this->a_attributes[$key] = $name;
}
private function getJoinedAttributes(){
if(0 === count($this->a_attributes)){
return '';
}
$return = '';
foreach($this->a_attributes as $k => $v){
$return .= ' ';
$return .= $k . '="' . htmlspecialchars($v, ENT_QUOTES) . '"';
}
return $return;
}
}
class Log{
private $a_entries = array();
private $last_modified;
function __construct($log_filename){
$handle = fopen($log_filename, 'r');
if(false === $handle){
echo 'ログファイルのオープンに失敗';
exit(1);
}
while (!feof($handle)) {
$tmp = explode('<>', trim(mb_convert_encoding(fgets($handle), 'UTF-8', 'SJIS')));
if(count($tmp) < 13){
continue;
}
$this->a_entries[] = new Entry(
$tmp[0],
$tmp[1],
$tmp[2],
$tmp[3],
$tmp[4],
$tmp[5],
$tmp[6],
self::fontColor($tmp[7]),
self::backColor($tmp[7]),
$tmp[8],
$tmp[9],
$tmp[10],
$tmp[11],
$tmp[12]
);
if(count($this->a_entries) > Config::ITEM_AMOUNT){
break;
}
}
fclose($handle);
$this->last_modified = filemtime($log_filename);
}
function entries(){
return $this->a_entries;
}
private static function fontColor($color_string){
$tmp = explode(';', $color_string);
return $tmp[0];
}
private static function backColor($color_string){
$tmp = explode(';', $color_string);
return $tmp[1];
}
function getRssDate(){
return date('D, d M Y H i s O', $this->last_modified);
}
}
class Entry{
private $article_id;
private $time;
private $author_name;
private $author_mail;
private $title;
private $content;
private $url;
private $font_color;
private $back_color;
private $icon_file;
private $parent_article;
private $del_pass;
private $hostname;
private $page;
function __construct($article_id, $time, $author_name, $author_mail, $title, $content, $url,
$font_color, $back_color, $icon_file, $parent_article, $del_pass, $hostname, $page){
$this->article_id = $article_id;
$this->time = self::parseTime($time);
$this->author_name = $author_name;
$this->author_mail = $author_mail;
$this->title = $title;
$this->content = $content;
$this->url = $url;
$this->font_color = $font_color;
$this->back_color = $back_color;
$this->icon_file = $icon_file;
$this->parent_article = $parent_article;
$this->del_pass = $del_pass;
$this->hostname = $hostname;
$this->page = $page;
}
static private function parseTime($str){
return preg_replace('/[^0-9]/', '', $str);
}
function asNode(){
$return = new Node('item');
$return->appendChild(new node('title', $this->title));
$return->appendChild(new Node('link', Config::URL));
$return->appendChild(new Node('guid', Config::URL . '/' . $this->article_id));
$return->appendChild(new Node('pubDate', $this->getRssDate()));
$return->appendChild(new Node('description', $this->getDescription()));
return $return;
}
private function getRssDate(){
$date = mktime(
substr($this->time, 8, 2),
substr($this->time, 10, 2),
substr($this->time, 12, 2),
substr($this->time, 4, 2),
substr($this->time, 6, 2),
substr($this->time, 0, 4)
);
return date('D, d M Y H i s ') . '+0900';
}
private function getDescription(){
$return = "<![CDATA[\n";
$return .= '<div class="icon">' . '<img src="' . Config::IMAGE_DIR_URL . $this->icon_file . '"></div>';
$return .= '<div class="content">' . $this->content . '</div>';
$return .= "\n]]>\n";
return $return;
}
}
$rss = new Node('rss');
$rss->setAttribute('version', '2.0');
$log = new Log(Config::LOG_DIR . Config::LOG_FILE);
$channel = new Node('channel');
$channel->appendChild(new Node('title', Config::TITLE));
$channel->appendChild(new Node('link', Config::URL));
$channel->appendChild(new Node('description', Config::DESCRIPTION));
$channel->appendChild(new Node('pubDate', date('D, d M Y H i s O')));
$channel->appendChild(new Node('lastBuildDate', $log->getRssDate()));
$channel->appendChild(new Node('generator', Config::GENERATOR));
$channel->appendChild(new Node('language', 'ja'));
$channel->appendChild(new Node('docs', 'http://blogs.law.harvard.edu/tech/rss'));
$image = new Node('image');
$image->appendChild(new Node('url', Config::IMAGE_DIR_URL . Config::RSS_IMAGE));
$image->appendChild(new Node('title', Config::TITLE));
$image->appendChild(new Node('link', Config::URL));
$channel->appendChild($image);
foreach($log->entries() as $entry){
$channel->appendChild($entry->asnode());
}
$rss->appendChild($channel);
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo $rss->asXml();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment