summaryrefslogtreecommitdiff
path: root/utils/rss.php
blob: 6f909d30fbb1ea44423537f57fbda0659d2fdbac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

include_once("convert.php");
include_once($UTIL_DIR . "/markdown.php");

class RSSEntry {
	public $title;
	public $time;
	public $description;
	public $category;

	/*
	public function show()
	{
		echo "<div class=\"news_entry\">\n";
		echo "  <div class=\"news_title\">" .
			htmlspecialchars_decode($this->title, ENT_QUOTES) . "</div>\n";
		echo "  <div class=\"news_time\">" . date("D M jS Y G:i", $this->time) . "</div>\n";
		echo "  <div class=\"news_description\">" .
			htmlspecialchars_decode($this->description, ENT_QUOTES) . "</div>\n";
		echo "</div>\n";
	}
	*/
	public function RSSEntry($title, $time, $category, $description)
	{
		$this->title = $title;
		$this->time = $time;
		$this->category = $category;
		$this->description = $description;
	}
}

class RSS {

	private $newsfile;
	private $rssfile;
	private $news = array();

	public function add($newsentry) {
		$key = $newsentry->time;
		$this->news[$key] = $newsentry;
	}

	private function date($time) {
		return date("r", $time);
	}

	public function write()
	{
		global $RSS_TITLE;
		global $RSS_URL;
		global $RSS_DESCRIPTION;
		global $RSS_EDITOR;
		global $RSS_WEBMASTER;
		global $RSS_TARGET_PAGE;

		$fp = fopen($this->rssfile, "w");
		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		fwrite($fp, "<rss version=\"2.0\">\n");
		fwrite($fp, "   <channel>\n");
		fwrite($fp, "      <title>".$RSS_TITLE."</title>\n");
		fwrite($fp, "      <link>".$RSS_URL."</link>\n");
		fwrite($fp, "      <description>".$RSS_DESCRIPTION."</description>\n");
		fwrite($fp, "      <language>en-us</language>\n");
		fwrite($fp, "      <pubDate>".$this->date(time())."</pubDate>\n");
		fwrite($fp, "      <lastBuildDate>".$this->date(time())."</lastBuildDate>\n");
		fwrite($fp, "      <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n");
		fwrite($fp, "      <generator>ExecutionRoom CMS</generator>\n");
		fwrite($fp, "      <managingEditor>".$RSS_EDITOR."</managingEditor>\n");
		fwrite($fp, "      <webMaster>".$RSS_WEBMASTER."</webMaster>\n");

		$i = 0;
		foreach($this->news as $newsentry) {
			fwrite($fp, "      <item>\n");
			fwrite($fp, "         <title>".$newsentry->title."</title>\n");
			fwrite($fp, "         <link>".$RSS_URL."/?page=".$RSS_TARGET_PAGE."&amp;newsid=".$newsentry->time."</link>\n");
			$content = Markdown(htmlspecialchars_decode($newsentry->description));
			fwrite($fp, "         <description>".$content."</description>\n");
			fwrite($fp, "         <pubDate>".$this->date($newsentry->time)."</pubDate>\n");
			fwrite($fp, "         <guid>".$RSS_URL."/?page=".$RSS_TARGET_PAGE."&amp;newsid=".$newsentry->time."</guid>\n");
			fwrite($fp, "      </item>\n");
			$i++;
			if($i > 6) break;
		}

		fwrite($fp, "   </channel>\n");
		fwrite($fp, "</rss>\n");

		fclose($fp);
	}
	
	private function read()
	{

		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->newsfile);
		$params = $dom->getElementsByTagName('newsentry');

		foreach ($params as $param) {
			$rssentry = new RSSEntry($param->getAttribute('title'),
															 $param->getAttribute('time'),
															 $param->getAttribute('category'),
															 $param->getAttribute('description'));
			$this->add($rssentry);
		}
		
		// Key sort
		krsort($this->news);
	}

	public function RSS($newsfile, $rssfile)
	{
		$this->newsfile =  $newsfile;
		$this->rssfile =  $rssfile;
		$this->read();
	}

}

?>