MDL-46218 reshuffled access checks in blog and notes
[moodle.git] / blog / rsslib.php
CommitLineData
516194d0 1<?php
2
13d1c9ed
JF
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Blog RSS Management
20 *
21 * @package core_blog
22 * @category rss
23 * @copyright 2010 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
1c7b8b93
NC
26require_once($CFG->dirroot.'/lib/rsslib.php');
27require_once($CFG->dirroot .'/blog/lib.php');
516194d0 28
13d1c9ed
JF
29/**
30 * Build the URL for the RSS feed
31 *
32 * @param int $contextid The context under which the URL should be created
33 * @param int $userid The id of the user requesting the RSS Feed
34 * @param string $filtertype The source of the RSS feed (site/course/group/user)
35 * @param int $filterselect The id of the item defined by $filtertype
36 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
37 * @return string
38 */
e858c368
AD
39function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
40 $componentname = 'blog';
6619a7f4 41
e858c368 42 $additionalargs = null;
1c7b8b93
NC
43 switch ($filtertype) {
44 case 'site':
e858c368 45 $additionalargs = 'site/'.SITEID;
1c7b8b93
NC
46 break;
47 case 'course':
e858c368 48 $additionalargs = 'course/'.$filterselect;
1c7b8b93
NC
49 break;
50 case 'group':
e858c368 51 $additionalargs = 'group/'.$filterselect;
1c7b8b93
NC
52 break;
53 case 'user':
e858c368 54 $additionalargs = 'user/'.$filterselect;
1c7b8b93
NC
55 break;
56 }
6619a7f4 57
1c7b8b93 58 if ($tagid) {
e858c368
AD
59 $additionalargs .= '/'.$tagid;
60 }
61
62 return rss_get_url($contextid, $userid, $componentname, $additionalargs);
63}
64
13d1c9ed
JF
65/**
66 * Print the link for the RSS feed with the correct RSS icon (Theme based)
67 *
68 * @param stdClass $context The context under which the URL should be created
69 * @param string $filtertype The source of the RSS feed (site/course/group/user)
70 * @param int $filterselect The id of the item defined by $filtertype
71 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
72 * @param string $tooltiptext The tooltip to be displayed with the link
73 */
e858c368
AD
74function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
75 global $CFG, $USER, $OUTPUT;
76
77 if (!isloggedin()) {
78 $userid = $CFG->siteguest;
79 } else {
80 $userid = $USER->id;
1c7b8b93 81 }
6619a7f4 82
e858c368 83 $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
b5d0cafc 84 $rsspix = $OUTPUT->pix_url('i/rss');
e858c368
AD
85 print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
86}
516194d0 87
13d1c9ed
JF
88/**
89 * Build the URL for the RSS feed amd add it as a header
90 *
91 * @param stdClass $context The context under which the URL should be created
92 * @param string $title Name for the link to be added to the page header
93 * @param string $filtertype The source of the RSS feed (site/course/group/user)
94 * @param int $filterselect The id of the item defined by $filtertype
95 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
96 */
e858c368
AD
97function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
98 global $PAGE, $USER, $CFG;
516194d0 99
e858c368
AD
100 //$componentname = 'blog';
101 //rss_add_http_header($context, $componentname, $filterselect, $title);
71904f4d 102
e858c368
AD
103 if (!isloggedin()) {
104 $userid = $CFG->siteguest;
105 } else {
106 $userid = $USER->id;
107 }
108
109 $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
110 $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
111}
112
113/**
114 * Utility function to extract parameters needed to generate RSS URLs from the blog filters
13d1c9ed
JF
115 *
116 * @param array $filters filters for the blog
117 * @return array array containing the id of the user/course/group, the relevant context and the filter type: site/user/course/group
e858c368
AD
118 */
119function blog_rss_get_params($filters) {
120 $thingid = $rsscontext = $filtertype = null;
121
41b38360 122 $sitecontext = context_system::instance();
fd7a6fe2 123
e858c368
AD
124 if (!$filters) {
125 $thingid = SITEID;
e858c368
AD
126 $filtertype = 'site';
127 } else if (array_key_exists('course', $filters)) {
128 $thingid = $filters['course'];
e858c368
AD
129 $filtertype = 'course';
130 } else if (array_key_exists('user', $filters)) {
131 $thingid = $filters['user'];
e858c368
AD
132 $filtertype = 'user';
133 } else if (array_key_exists('group', $filters)) {
134 $thingid = $filters['group'];
e858c368
AD
135 $filtertype = 'group';
136 }
137
138 return array($thingid, $rsscontext, $filtertype);
1c7b8b93 139}
516194d0 140
13d1c9ed
JF
141/**
142 * Generate any blog RSS feed via one function
143 *
144 * @param stdClass $context The context of the blog for which the feed it being generated
145 * @param array $args An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
146 */
274f9840 147function blog_rss_get_feed($context, $args) {
1c7b8b93 148 global $CFG, $SITE, $DB;
ebb73f49 149
fe7e411e
DM
150 if (empty($CFG->enableblogs)) {
151 debugging('Blogging disabled on this site, RSS feeds are not available');
152 return null;
153 }
154
274f9840
AD
155 if (empty($CFG->enablerssfeeds)) {
156 debugging('Sorry, RSS feeds are disabled on this site');
157 return '';
158 }
159
21c3b51e
JF
160 if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
161 if (isguestuser()) {
162 debugging(get_string('nopermissiontoshow','error'));
163 return '';
164 }
165 }
166
41b38360 167 $sitecontext = context_system::instance();
274f9840
AD
168 if (!has_capability('moodle/blog:view', $sitecontext)) {
169 return null;
170 }
690aa229 171
5ce534eb
AD
172 $type = clean_param($args[3], PARAM_ALPHA);
173 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
690aa229
AD
174
175 $tagid=0;
176 if ($args[5] != 'rss.xml') {
5ce534eb 177 $tagid = clean_param($args[5], PARAM_INT);
690aa229
AD
178 } else {
179 $tagid = 0;
180 }
181
1c7b8b93 182 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 183
1c7b8b93
NC
184 if (file_exists($filename)) {
185 if (filemtime($filename) + 3600 > time()) {
e858c368 186 return $filename; // It's already done so we return cached version
6619a7f4 187 }
1c7b8b93 188 }
6619a7f4 189
451f1e38
AD
190 $courseid = $groupid = $userid = null;
191 switch ($type) {
192 case 'site':
193 //$siteid = $id;
194 break;
195 case 'course':
196 $courseid = $id;
197 break;
198 case 'group':
199 $groupid = $id;
200 break;
201 case 'user':
202 $userid = $id;
203 break;
204 }
205
e858c368 206 // Get all the entries from the database
e858c368 207 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
208 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
209
e858c368
AD
210 $bloglisting = new blog_listing($blogheaders['filters']);
211 $blogentries = $bloglisting->get_entries();
1c7b8b93 212
e858c368 213 // Now generate an array of RSS items
1c7b8b93
NC
214 if ($blogentries) {
215 $items = array();
216 foreach ($blogentries as $blog_entry) {
217 $item = NULL;
218 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
219 $item->title = $blog_entry->subject;
220 $item->pubdate = $blog_entry->lastmodified;
221 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
28566555
DP
222 $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php',
223 $sitecontext->id, 'blog', 'post', $blog_entry->id);
224 $item->description = format_text($summary, $blog_entry->format);
1c7b8b93
NC
225 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
226 if ($blogtags) {
227 $item->tags = $blogtags;
08577617 228 }
1c7b8b93 229 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 230 }
1c7b8b93 231 $items[] = $item;
516194d0 232 }
1c7b8b93
NC
233 $articles = rss_add_items($items); /// Change structure to XML
234 } else {
235 $articles = '';
236 }
516194d0 237
1c7b8b93 238/// Get header and footer information
dba2764d 239
1c7b8b93
NC
240 switch ($type) {
241 case 'user':
242 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
243 break;
244 case 'course':
245 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
41b38360 246 $info = format_string($info, true, array('context' => context_course::instance($id)));
1c7b8b93
NC
247 break;
248 case 'site':
41b38360 249 $info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
250 break;
251 case 'group':
9250bf81 252 $group = groups_get_group($id);
1c7b8b93
NC
253 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
254 break;
255 default:
256 $info = '';
257 break;
258 }
516194d0 259
1c7b8b93
NC
260 if ($tagid) {
261 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
262 }
cae83708 263
1c7b8b93
NC
264 $header = rss_standard_header(get_string($type.'blog','blog', $info),
265 $CFG->wwwroot.'/blog/index.php',
266 get_string('intro','blog'));
516194d0 267
1c7b8b93 268 $footer = rss_standard_footer();
516194d0 269
e858c368 270 // Save the XML contents to file.
1c7b8b93 271 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
272 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
273 return $filename;
274 } else {
275 return false; // Couldn't find it or make it
516194d0 276 }
1c7b8b93 277}
516194d0 278
13d1c9ed
JF
279/**
280 * Retrieve the location and file name of a cached RSS feed
281 *
282 * @param string $type The source of the RSS feed (site/course/group/user)
283 * @param int $id The id of the item defined by $type
284 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
285 * @return string
286 */
1c7b8b93
NC
287function blog_rss_file_name($type, $id, $tagid=0) {
288 global $CFG;
516194d0 289
1c7b8b93 290 if ($tagid) {
365bec4c 291 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 292 } else {
365bec4c 293 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 294 }
1c7b8b93 295}
cae83708 296
13d1c9ed
JF
297/**
298 * This function saves to file the rss feed specified in the parameters
299 *
300 * @param string $type The source of the RSS feed (site/course/group/user)
301 * @param int $id The id of the item defined by $type
302 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
303 * @param string $contents The contents of the RSS Feed file
304 * @return bool whether the save was successful or not
305 */
1c7b8b93
NC
306function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
307 global $CFG;
71904f4d 308
e858c368 309 $status = true;
516194d0 310
e858c368 311 //blog creates some additional dirs within the rss cache so make sure they all exist
5a87c912
TL
312 make_cache_directory('rss/blog');
313 make_cache_directory('rss/blog/'.$type);
ebb73f49 314
71904f4d
PS
315 $filename = blog_rss_file_name($type, $id, $tagid);
316 $expandfilename = false; //we're supplying a full file path
317 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 318
1c7b8b93
NC
319 return $status;
320}
321
54eb02a4
AD
322/**
323 * Delete the supplied user's cached blog post RSS feed.
324 * Only user blogs are available by RSS.
325 * This doesn't call rss_delete_file() as blog RSS caching uses it's own file structure.
326 *
327 * @param int $userid
328 */
329function blog_rss_delete_file($userid) {
330 $filename = blog_rss_file_name('user', $userid);
331 if (file_exists($filename)) {
332 unlink($filename);
333 }
334}
335