author | Dan Fuhry <dan@enanocms.org> |
Thu, 08 Jul 2010 17:52:37 -0400 | |
changeset 4 | 308a696fce01 |
parent 3 | f71442e3a95f |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/* |
|
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
3 |
Plugin Name: RSS Frontend |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
4 |
Plugin URI: http://enanocms.org/Feed_me |
0 | 5 |
Description: Provides the page Special:RSS, which is used to generate RSS feeds of site and page content changes. |
6 |
Author: Dan Fuhry |
|
7 |
Version: 1.0 |
|
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
8 |
Author URI: http://enanocms.org/ |
0 | 9 |
*/ |
10 |
||
11 |
/* |
|
12 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
13 |
* Version 1.0 release candidate 2 |
|
14 |
* Copyright (C) 2006-2007 Dan Fuhry |
|
15 |
* |
|
16 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
17 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
18 |
* |
|
19 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
20 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
21 |
*/ |
|
22 |
||
23 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
24 |
||
25 |
$plugins->attachHook('base_classes_initted', ' |
|
26 |
$paths->add_page(Array( |
|
27 |
\'name\'=>\'RSS Feed - Latest changes\', |
|
28 |
\'urlname\'=>\'RSS\', |
|
29 |
\'namespace\'=>\'Special\', |
|
30 |
\'special\'=>0,\'visible\'=>0,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\', |
|
31 |
)); |
|
32 |
'); |
|
33 |
||
34 |
$plugins->attachHook('session_started', '__enanoRSSAttachHTMLHeaders();'); |
|
35 |
||
36 |
function __enanoRSSAttachHTMLHeaders() |
|
37 |
{ |
|
38 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
39 |
$template->add_header('<link rel="alternate" title="'.getConfig('site_name').' Changes feed" href="'.makeUrlNS('Special', 'RSS/recent', null, true).'" type="application/rss+xml" />'); |
|
40 |
$template->add_header('<link rel="alternate" title="'.getConfig('site_name').' Comments feed" href="'.makeUrlNS('Special', 'RSS/comments', null, true).'" type="application/rss+xml" />'); |
|
41 |
} |
|
42 |
||
43 |
define('ENANO_FEEDBURNER_INCLUDED', true); |
|
44 |
||
45 |
/** |
|
46 |
* Class for easily generating RSS feeds. |
|
47 |
* @package Enano |
|
48 |
* @subpackage Feed Me |
|
49 |
* @license GNU General Public License <http://www.gnu.org/licenses/gpl.html> |
|
50 |
*/ |
|
51 |
||
52 |
class RSS |
|
53 |
{ |
|
54 |
||
55 |
/** |
|
56 |
* List of channels contained in this feed. |
|
57 |
* @var array |
|
58 |
*/ |
|
59 |
||
60 |
var $channels = Array(); |
|
61 |
||
62 |
/** |
|
63 |
* The channel that's currently being operated on |
|
64 |
* @var array |
|
65 |
*/ |
|
66 |
||
67 |
var $this_channel = Array(); |
|
68 |
||
69 |
/** |
|
70 |
* GUID of the current channel |
|
71 |
* @var string |
|
72 |
*/ |
|
73 |
||
74 |
var $this_guid = ''; |
|
75 |
||
76 |
/** |
|
77 |
* List of fully XML-formatted feed entries. |
|
78 |
* @var array |
|
79 |
*/ |
|
80 |
||
81 |
var $items = Array(); |
|
82 |
||
83 |
/** |
|
84 |
* Constructor. |
|
85 |
* @param string Feed title |
|
86 |
* @param string Feed description |
|
87 |
* @param string Linkback |
|
88 |
* @param string Generator |
|
89 |
* @param string E-mail of webmaster |
|
90 |
*/ |
|
91 |
||
92 |
function __construct($title = false, $desc = false, $link = false, $gen = false, $email = false) |
|
93 |
{ |
|
94 |
$this->create_channel($title, $desc, $link, $gen, $email); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Creates a new channel. |
|
99 |
*/ |
|
100 |
||
101 |
function create_channel($title = false, $desc = false, $link = false, $gen = false, $email = false) |
|
102 |
{ |
|
103 |
if ( empty($title) ) |
|
104 |
$title = 'Untitled feed'; |
|
105 |
else |
|
106 |
$title = htmlspecialchars($title); |
|
107 |
if ( empty($desc) ) |
|
108 |
$desc = 'Test feed'; |
|
109 |
else |
|
110 |
$desc = htmlspecialchars($desc); |
|
111 |
if ( empty($link) ) |
|
112 |
$link = 'http' . ( isset($_SERVER['HTTPS']) ? 's' : '' ) . '://'.$_SERVER['HTTP_HOST'] . scriptPath . '/'; |
|
113 |
else |
|
114 |
$link = htmlspecialchars($link); |
|
115 |
if ( !empty($gen) ) |
|
116 |
$gen = htmlspecialchars($gen); |
|
117 |
else |
|
118 |
$gen = 'Enano CMS ' . enano_version(); |
|
119 |
if ( !empty($email) ) |
|
120 |
$email = htmlspecialchars($email); |
|
121 |
else |
|
122 |
$email = getConfig('contact_email'); |
|
123 |
||
124 |
$this->channels = Array(); |
|
125 |
$guid = md5(microtime() . mt_rand()); |
|
126 |
$this->channels[$guid] = Array( |
|
127 |
'title' => $title, |
|
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
128 |
'desc' => $desc, |
0 | 129 |
'link' => $link, |
130 |
'gen' => $gen, |
|
131 |
'email' => $email, |
|
132 |
'lang' => 'en-us', |
|
133 |
'items' => Array() |
|
134 |
); |
|
135 |
$this->this_channel =& $this->channels[$guid]; |
|
136 |
$this->this_guid = $guid; |
|
137 |
return $guid; |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Selects a specific channel to add items to |
|
142 |
* @param string The GUID of the channel |
|
143 |
*/ |
|
144 |
||
145 |
function select_channel($guid) |
|
146 |
{ |
|
147 |
if ( isset($this->channels[$guid]) ) |
|
148 |
{ |
|
149 |
$this->this_channel =& $this->channels[$guid]; |
|
150 |
$this->guid = $guid; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Adds a news item. |
|
156 |
* @param string Title of the feed entry |
|
157 |
* @param string Link to where more information can be found |
|
158 |
* @param string Short description or content area |
|
159 |
* @param string Date the item was published. If this is a UNIX timestamp it will be formatted with date(). |
|
160 |
* @param string A Globally-Unique Identifier (GUID) for this item. Doesn't have to be a 128-bit hash - usually it's a link. If one is not provided, one is generated based on the first three parameters. |
|
161 |
*/ |
|
162 |
||
163 |
function add_item($title, $link, $desc, $pubdate, $guid = false) |
|
164 |
{ |
|
165 |
$title = htmlspecialchars($title); |
|
166 |
$link = htmlspecialchars($link); |
|
167 |
$desc = '<![CDATA[ ' . str_replace(']]>', ']]>', $desc) . ']]>'; |
|
4
308a696fce01
Fixed a few bugs reported by Neal. Removed PHP 4 support.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
168 |
if ( is_int($pubdate) || ( !is_int($pubdate) && preg_match('/^([0-9]+)$/', $pubdate) ) ) |
0 | 169 |
{ |
170 |
$pubdate = date('D, d M Y H:i:s T', intval($pubdate)); |
|
171 |
} |
|
172 |
if ( !$guid ) |
|
173 |
{ |
|
174 |
$guid = md5 ( $title . $link . $desc . $pubdate ); |
|
175 |
$sec1 = substr($guid, 0, 8); |
|
176 |
$sec2 = substr($guid, 8, 4); |
|
177 |
$sec3 = substr($guid, 12, 4); |
|
178 |
$sec4 = substr($guid, 16, 4); |
|
179 |
$sec5 = substr($guid, 20, 12); |
|
180 |
$guid = sprintf('%s-%s-%s-%s-%s', $sec1, $sec2, $sec3, $sec4, $sec5); |
|
181 |
} |
|
182 |
$xml = " <item> |
|
183 |
<title>$title</title> |
|
184 |
<link>$link</link> |
|
185 |
<description> |
|
186 |
$desc |
|
187 |
</description> |
|
188 |
<pubDate>$pubdate</pubDate> |
|
3 | 189 |
<guid>$link</guid> |
0 | 190 |
</item>"; |
191 |
$this->this_channel['items'][] = $xml; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Converts everything into the final RSS feed. |
|
196 |
* @param bool If true, XML headers ("<?xml version="1.0" encoding="utf-8" ?>") are included. Defaults to true. |
|
197 |
* @return string |
|
198 |
*/ |
|
199 |
||
200 |
function render($headers = true) |
|
201 |
{ |
|
202 |
$xml = ''; |
|
203 |
if ( $headers ) |
|
204 |
// The weird quotes are because of a jEdit syntax highlighting bug |
|
205 |
$xml .= "<?xml version=".'"'."1.0".'"'." encoding=".'"'."utf-8".'"'." ?>\n"; |
|
206 |
$xml .= "<rss version=".'"'."2.0".'"'.">\n"; |
|
207 |
foreach ( $this->channels as $channel ) |
|
208 |
{ |
|
209 |
$xml .= " <channel> |
|
210 |
<title>{$channel['title']}</title> |
|
211 |
<link>{$channel['link']}</link> |
|
212 |
<description>{$channel['desc']}</description> |
|
213 |
<generator>{$channel['gen']}</generator> |
|
214 |
<webMaster>{$channel['email']}</webMaster> |
|
215 |
<language>{$channel['lang']}</language> |
|
216 |
||
217 |
"; |
|
218 |
$content = implode("\n", $channel['items']) . "\n"; |
|
219 |
$xml .= $content; |
|
220 |
$xml .= " </channel>"; |
|
221 |
$xml .= " |
|
222 |
</rss>"; |
|
223 |
} |
|
224 |
return $xml; |
|
225 |
} |
|
226 |
||
227 |
} |
|
228 |
||
229 |
// function names are IMPORTANT!!! The name pattern is: page_<namespace ID>_<page URLname, without namespace> |
|
230 |
||
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
231 |
function page_Special_RSS() |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
232 |
{ |
0 | 233 |
global $db, $session, $paths, $template, $plugins; // Common objects |
2
882c793b0f37
Merging a one-line change from nighthawk (wrong content-type if you're interested)
Dan
parents:
1
diff
changeset
|
234 |
header('Content-type: text/xml; charset=utf-8'); //application/rss+xml'); |
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
235 |
global $aggressive_optimize_html; |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
236 |
$aggressive_optimize_html = false; |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
237 |
$session->sid_super = false; |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
238 |
if ( $session->auth_level > USER_LEVEL_MEMBER ) |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
239 |
$session->auth_level = USER_LEVEL_MEMBER; |
0 | 240 |
$mode = $paths->getParam(0); |
241 |
$n = $paths->getParam(1); |
|
242 |
if(!preg_match('#^([0-9]+)$#', $n) || (int)$n > 50) $n = 20; |
|
243 |
else $n = (int)$n; |
|
244 |
switch($mode) |
|
245 |
{ |
|
246 |
case "recent": |
|
247 |
$title = getConfig('site_name') . ' Recent Changes'; |
|
248 |
$desc = getConfig('site_desc'); |
|
249 |
||
250 |
$rss = new RSS($title, $desc); |
|
251 |
||
252 |
$q = $db->sql_query('SELECT * FROM '.table_prefix.'logs WHERE log_type=\'page\' ORDER BY time_id DESC LIMIT '.$n.';'); |
|
253 |
if(!$q) |
|
254 |
{ |
|
255 |
$rss->add_item('ERROR', '', 'Error selecting log data: ' . mysql_error() . '', time()); |
|
256 |
} |
|
257 |
else |
|
258 |
{ |
|
259 |
while($row = $db->fetchrow()) |
|
260 |
{ |
|
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
261 |
$link = makeUrlComplete($row['namespace'], $row['page_id'], "oldid={$row['time_id']}"); // makeUrlComplete($row['namespace'], $row['page_id']); |
4
308a696fce01
Fixed a few bugs reported by Neal. Removed PHP 4 support.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
262 |
$title = get_page_title_ns($row['page_id'], $row['namespace']); // $paths->pages[$paths->nslist[$row['namespace']].$row['page_id']]['name']; |
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
263 |
$desc = "Change by {$row['author']}:<br />"; |
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
264 |
$desc .= ( $row['edit_summary'] != '' ) ? $row['edit_summary'] : 'No edit summary given.'; |
0 | 265 |
$date = $row['time_id']; |
1
327d3c7cd3f3
A few bugfixes and updates. The most recent fix involves $desc being undefined in the add_channel method.
Dan
parents:
0
diff
changeset
|
266 |
$guid = false; |
0 | 267 |
|
268 |
$rss->add_item($title, $link, $desc, $date, $guid); |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
echo $rss->render(); |
|
273 |
break; |
|
274 |
case "comments": |
|
275 |
$title = getConfig('site_name') . ' Latest Comments'; |
|
276 |
$desc = getConfig('site_desc'); |
|
277 |
||
278 |
$rss = new RSS($title, $desc); |
|
279 |
||
280 |
$q = $db->sql_query('SELECT * FROM '.table_prefix.'comments ORDER BY time DESC LIMIT '.$n.';'); |
|
281 |
||
282 |
if(!$q) |
|
283 |
{ |
|
284 |
$rss->add_item('ERROR', '', 'Error selecting log data: ' . mysql_error() . '', time()); |
|
285 |
} |
|
286 |
else |
|
287 |
{ |
|
288 |
$n = $db->numrows(); |
|
289 |
//echo '<!-- Number of rows: '.$n.' -->'; // ."\n<!-- SQL backtrace: \n\n".$db->sql_backtrace().' -->'; |
|
290 |
for ( $j = 0; $j < $n; $j++ ) |
|
291 |
{ |
|
292 |
$row = $db->fetchrow($q); |
|
293 |
if(!is_array($row)) die(__FILE__.':'.__LINE__.' $row is not an array'); |
|
294 |
$link = 'http' . ( isset($_SERVER['HTTPS']) ? 's' : '' ) . '://'.$_SERVER['HTTP_HOST'] . makeUrlNS($row['namespace'], $row['page_id']).'#comments'; |
|
295 |
$page = ( isset($paths->pages[$paths->nslist[$row['namespace']].$row['page_id']]) ) ? $paths->pages[$paths->nslist[$row['namespace']].$row['page_id']]['name'] : $paths->nslist[$row['namespace']].$row['page_id']; |
|
296 |
$title = $row['subject'] . ': Posted on page "'.$page.'" by user '.$row['name']; |
|
297 |
$desc = RenderMan::render($row['comment_data']); |
|
298 |
$date = $row['time']; |
|
299 |
$guid = 'http' . ( isset($_SERVER['HTTPS']) ? 's' : '' ) . '://'.$_SERVER['HTTP_HOST'] . makeUrlNS($row['namespace'], $row['page_id']).'?do=comments&comment='.$row['time']; |
|
300 |
||
301 |
$rss->add_item($title, $link, $desc, $date, $guid); |
|
302 |
} |
|
303 |
} |
|
304 |
echo $rss->render(); |
|
305 |
break; |
|
306 |
default: |
|
307 |
$code = $plugins->setHook('feed_me_request'); |
|
308 |
foreach ( $code as $cmd ) |
|
309 |
{ |
|
310 |
eval($cmd); |
|
311 |
} |
|
312 |
break; |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
?> |