-
Notifications
You must be signed in to change notification settings - Fork 3
/
RssGenerator.php
103 lines (86 loc) · 3.28 KB
/
RssGenerator.php
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
<?php
class RssGenerator
{
var $_encoding = 'UTF-8';
var $_title = 'YOUR_WEBSITE_TITLE';
var $_language = 'zh-tw';
var $_description = 'YOUR_DESCRIPTION';
var $_link = 'http://example.com/';
var $_ttl = 10; // minutes
var $_generator = 'YOUR_NAME';
var $_version = '2.0';
function RssGenerator()
{
}
function set($name, $value)
{
if ($name == 'encoding')
$this->_encoding = stripslashes($value);
if ($name == 'title')
$this->_title = stripslashes($value);
if ($name == 'language')
$this->_language = stripslashes($value);
if ($name == 'description')
$this->_description = stripslashes($value);
if ($name == 'generator')
$this->_generator = stripslashes($value);
if ($name == 'link')
$this->_link = stripslashes($value);
if ($name == 'ttl')
$this->_ttl = stripslashes($value);
}
/**
* Make an xml document of the rss stream
* @param: items: n row of associative array with theses field:
* @param: title: title of the item
* @param: description: short description of the item
* @param: link: url to show the item
* @param: pubData: publication timestamp of the item
* @res: xml document of rss
*/
function get($items)
{
$res='';
// header
$res .= "<?xml version=\"1.0\" encoding=\"" . $this->_encoding . "\"?>\n";
$res .= "<rss version=\"2.0\">\n";
$res .= "\t<channel>\n";
$res .= "\t\t<title><![CDATA[" . $this->_title . "]]></title>\n";
$res .= "\t\t<description><![CDATA[" . $this->_description . "]]></description>\n";
$res .= "\t\t<link><![CDATA[" . $this->_link . "]]></link>\n";
$res .= "\t\t<language>" . $this->_language . "</language>\n";
$res .= "\t\t<lastBuildDate>" . date(DATE_RSS) . "</lastBuildDate>\n";
$res .= "\t\t<ttl>" . $this->_ttl . "</ttl>\n";
$res .= "\t\t<generator><![CDATA[" . $this->_generator . "]]></generator>\n";
// items
foreach ($items as $item) {
$res.="\t\t<item>\n";
foreach ($item as $key => $val) {
switch($key) {
case 'title':
$res .= "\t\t\t<title><![CDATA[" . stripslashes($val) . "]]></title>\n";
break;
case 'description':
$res .= "\t\t\t<description><![CDATA[" . stripslashes($val) . "]]></description>\n";
break;
case 'link':
if (!empty($val))
$res .= "\t\t\t<link><![CDATA[" . stripslashes($val) . "]]></link>\n";
break;
case 'pubDate':
if (!empty($val))
$res .= "\t\t\t<pubDate>" . date(DATE_RSS, strtotime($val)) . "</pubDate>\n";
break;
default:
$res .= "\t\t\t<$key><![CDATA[" . stripslashes($val) . "]]></$key>\n";
break;
}
}
$res .= "\t\t</item>\n";
}
$res .= "\t</channel>\n";
$res .= "</rss>\n";
return $res;
}
}
?>