forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsslib.php
330 lines (287 loc) · 11.2 KB
/
rsslib.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blog RSS Management
*
* @package core_blog
* @category rss
* @copyright 2010 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/lib/rsslib.php');
require_once($CFG->dirroot .'/blog/lib.php');
/**
* Build the URL for the RSS feed
*
* @param int $contextid The context under which the URL should be created
* @param int $userid The id of the user requesting the RSS Feed
* @param string $filtertype The source of the RSS feed (site/course/group/user)
* @param int $filterselect The id of the item defined by $filtertype
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
* @return string
*/
function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
$componentname = 'blog';
$additionalargs = null;
switch ($filtertype) {
case 'site':
$additionalargs = 'site/'.SITEID;
break;
case 'course':
$additionalargs = 'course/'.$filterselect;
break;
case 'group':
$additionalargs = 'group/'.$filterselect;
break;
case 'user':
$additionalargs = 'user/'.$filterselect;
break;
}
if ($tagid) {
$additionalargs .= '/'.$tagid;
}
return rss_get_url($contextid, $userid, $componentname, $additionalargs);
}
/**
* Print the link for the RSS feed with the correct RSS icon (Theme based)
*
* @param stdClass $context The context under which the URL should be created
* @param string $filtertype The source of the RSS feed (site/course/group/user)
* @param int $filterselect The id of the item defined by $filtertype
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
* @param string $tooltiptext The tooltip to be displayed with the link
*/
function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
global $CFG, $USER, $OUTPUT;
if (!isloggedin()) {
$userid = $CFG->siteguest;
} else {
$userid = $USER->id;
}
$url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
$rsspix = $OUTPUT->pix_url('i/rss');
print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
}
/**
* Build the URL for the RSS feed amd add it as a header
*
* @param stdClass $context The context under which the URL should be created
* @param string $title Name for the link to be added to the page header
* @param string $filtertype The source of the RSS feed (site/course/group/user)
* @param int $filterselect The id of the item defined by $filtertype
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
*/
function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
global $PAGE, $USER, $CFG;
if (!isloggedin()) {
$userid = $CFG->siteguest;
} else {
$userid = $USER->id;
}
$rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
$PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
}
/**
* Utility function to extract parameters needed to generate RSS URLs from the blog filters
*
* @param array $filters filters for the blog
* @return array array containing the id of the user/course/group, the relevant context and the filter type: site/user/course/group
*/
function blog_rss_get_params($filters) {
$thingid = $rsscontext = $filtertype = null;
$sitecontext = context_system::instance();
if (!$filters) {
$thingid = SITEID;
$filtertype = 'site';
} else if (array_key_exists('course', $filters)) {
$thingid = $filters['course'];
$filtertype = 'course';
} else if (array_key_exists('user', $filters)) {
$thingid = $filters['user'];
$filtertype = 'user';
} else if (array_key_exists('group', $filters)) {
$thingid = $filters['group'];
$filtertype = 'group';
}
return array($thingid, $rsscontext, $filtertype);
}
/**
* Generate any blog RSS feed via one function
*
* @param stdClass $context The context of the blog for which the feed it being generated
* @param array $args An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
*/
function blog_rss_get_feed($context, $args) {
global $CFG, $SITE, $DB;
if (empty($CFG->enableblogs)) {
debugging('Blogging disabled on this site, RSS feeds are not available');
return null;
}
if (empty($CFG->enablerssfeeds)) {
debugging('Sorry, RSS feeds are disabled on this site');
return '';
}
if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
if (isguestuser()) {
debugging(get_string('nopermissiontoshow', 'error'));
return '';
}
}
$sitecontext = context_system::instance();
if (!has_capability('moodle/blog:view', $sitecontext)) {
return null;
}
$type = clean_param($args[3], PARAM_ALPHA);
$id = clean_param($args[4], PARAM_INT); // Could be groupid / courseid / userid depending on $type.
$tagid=0;
if ($args[5] != 'rss.xml') {
$tagid = clean_param($args[5], PARAM_INT);
} else {
$tagid = 0;
}
$filename = blog_rss_file_name($type, $id, $tagid);
if (file_exists($filename)) {
if (filemtime($filename) + 3600 > time()) {
return $filename; // It's already done so we return cached version.
}
}
$courseid = $groupid = $userid = null;
switch ($type) {
case 'site':
break;
case 'course':
$courseid = $id;
break;
case 'group':
$groupid = $id;
break;
case 'user':
$userid = $id;
break;
}
// Get all the entries from the database.
require_once($CFG->dirroot .'/blog/locallib.php');
$blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
$bloglisting = new blog_listing($blogheaders['filters']);
$blogentries = $bloglisting->get_entries();
// Now generate an array of RSS items.
if ($blogentries) {
$items = array();
foreach ($blogentries as $blogentry) {
$item = null;
$item->author = fullname($DB->get_record('user', array('id' => $blogentry->userid))); // TODO: this is slow.
$item->title = $blogentry->subject;
$item->pubdate = $blogentry->lastmodified;
$item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blogentry->id;
$summary = file_rewrite_pluginfile_urls($blogentry->summary, 'pluginfile.php',
$sitecontext->id, 'blog', 'post', $blogentry->id);
$item->description = format_text($summary, $blogentry->format);
if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blogentry->id)) ) {
if ($blogtags) {
$item->tags = $blogtags;
}
$item->tagscheme = $CFG->wwwroot . '/tag';
}
$items[] = $item;
}
$articles = rss_add_items($items); // Change structure to XML.
} else {
$articles = '';
}
// Get header and footer information.
switch ($type) {
case 'user':
$info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
break;
case 'course':
$info = $DB->get_field('course', 'fullname', array('id'=>$id));
$info = format_string($info, true, array('context' => context_course::instance($id)));
break;
case 'site':
$info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
break;
case 'group':
$group = groups_get_group($id);
$info = $group->name; // TODO: $DB->get_field('groups', 'name', array('id'=>$id)).
break;
default:
$info = '';
break;
}
if ($tagid) {
$info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
}
$header = rss_standard_header(get_string($type.'blog', 'blog', $info),
$CFG->wwwroot.'/blog/index.php',
get_string('intro', 'blog'));
$footer = rss_standard_footer();
// Save the XML contents to file.
$rssdata = $header.$articles.$footer;
if (blog_rss_save_file($type, $id, $tagid, $rssdata)) {
return $filename;
} else {
return false; // Couldn't find it or make it.
}
}
/**
* Retrieve the location and file name of a cached RSS feed
*
* @param string $type The source of the RSS feed (site/course/group/user)
* @param int $id The id of the item defined by $type
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
* @return string
*/
function blog_rss_file_name($type, $id, $tagid=0) {
global $CFG;
if ($tagid) {
return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
} else {
return "$CFG->cachedir/rss/blog/$type/$id.xml";
}
}
/**
* This function saves to file the rss feed specified in the parameters
*
* @param string $type The source of the RSS feed (site/course/group/user)
* @param int $id The id of the item defined by $type
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
* @param string $contents The contents of the RSS Feed file
* @return bool whether the save was successful or not
*/
function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
global $CFG;
$status = true;
// Blog creates some additional dirs within the rss cache so make sure they all exist.
make_cache_directory('rss/blog');
make_cache_directory('rss/blog/'.$type);
$filename = blog_rss_file_name($type, $id, $tagid);
$expandfilename = false; // We are supplying a full file path.
$status = rss_save_file('blog', $filename, $contents, $expandfilename);
return $status;
}
/**
* Delete the supplied user's cached blog post RSS feed.
* Only user blogs are available by RSS.
* This doesn't call rss_delete_file() as blog RSS caching uses it's own file structure.
*
* @param int $userid
*/
function blog_rss_delete_file($userid) {
$filename = blog_rss_file_name('user', $userid);
if (file_exists($filename)) {
unlink($filename);
}
}