MDL-37106 calendar: Proper capability checks should be done before processing an...
[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;
126 $rsscontext = $sitecontext;
127 $filtertype = 'site';
128 } else if (array_key_exists('course', $filters)) {
129 $thingid = $filters['course'];
130
41b38360 131 $coursecontext = context_course::instance($thingid);
e858c368
AD
132 $rsscontext = $coursecontext;
133
134 $filtertype = 'course';
135 } else if (array_key_exists('user', $filters)) {
136 $thingid = $filters['user'];
137
41b38360 138 $usercontext = context_user::instance($thingid);
e858c368
AD
139 $rsscontext = $usercontext;
140
141 $filtertype = 'user';
142 } else if (array_key_exists('group', $filters)) {
143 $thingid = $filters['group'];
144
145 $rsscontext = $sitecontext; //is this the context we should be using for group blogs?
146 $filtertype = 'group';
147 }
148
149 return array($thingid, $rsscontext, $filtertype);
1c7b8b93 150}
516194d0 151
13d1c9ed
JF
152/**
153 * Generate any blog RSS feed via one function
154 *
155 * @param stdClass $context The context of the blog for which the feed it being generated
156 * @param array $args An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
157 */
274f9840 158function blog_rss_get_feed($context, $args) {
1c7b8b93 159 global $CFG, $SITE, $DB;
ebb73f49 160
274f9840
AD
161 if (empty($CFG->enablerssfeeds)) {
162 debugging('Sorry, RSS feeds are disabled on this site');
163 return '';
164 }
165
41b38360 166 $sitecontext = context_system::instance();
274f9840
AD
167 if (!has_capability('moodle/blog:view', $sitecontext)) {
168 return null;
169 }
690aa229 170
5ce534eb
AD
171 $type = clean_param($args[3], PARAM_ALPHA);
172 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
690aa229
AD
173
174 $tagid=0;
175 if ($args[5] != 'rss.xml') {
5ce534eb 176 $tagid = clean_param($args[5], PARAM_INT);
690aa229
AD
177 } else {
178 $tagid = 0;
179 }
180
1c7b8b93 181 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 182
1c7b8b93
NC
183 if (file_exists($filename)) {
184 if (filemtime($filename) + 3600 > time()) {
e858c368 185 return $filename; // It's already done so we return cached version
6619a7f4 186 }
1c7b8b93 187 }
6619a7f4 188
451f1e38
AD
189 $courseid = $groupid = $userid = null;
190 switch ($type) {
191 case 'site':
192 //$siteid = $id;
193 break;
194 case 'course':
195 $courseid = $id;
196 break;
197 case 'group':
198 $groupid = $id;
199 break;
200 case 'user':
201 $userid = $id;
202 break;
203 }
204
e858c368 205 // Get all the entries from the database
e858c368 206 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
207 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
208
e858c368
AD
209 $bloglisting = new blog_listing($blogheaders['filters']);
210 $blogentries = $bloglisting->get_entries();
1c7b8b93 211
e858c368 212 // Now generate an array of RSS items
1c7b8b93
NC
213 if ($blogentries) {
214 $items = array();
215 foreach ($blogentries as $blog_entry) {
216 $item = NULL;
217 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
218 $item->title = $blog_entry->subject;
219 $item->pubdate = $blog_entry->lastmodified;
220 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
28566555
DP
221 $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php',
222 $sitecontext->id, 'blog', 'post', $blog_entry->id);
223 $item->description = format_text($summary, $blog_entry->format);
1c7b8b93
NC
224 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
225 if ($blogtags) {
226 $item->tags = $blogtags;
08577617 227 }
1c7b8b93 228 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 229 }
1c7b8b93 230 $items[] = $item;
516194d0 231 }
1c7b8b93
NC
232 $articles = rss_add_items($items); /// Change structure to XML
233 } else {
234 $articles = '';
235 }
516194d0 236
1c7b8b93 237/// Get header and footer information
dba2764d 238
1c7b8b93
NC
239 switch ($type) {
240 case 'user':
241 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
242 break;
243 case 'course':
244 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
41b38360 245 $info = format_string($info, true, array('context' => context_course::instance($id)));
1c7b8b93
NC
246 break;
247 case 'site':
41b38360 248 $info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
249 break;
250 case 'group':
9250bf81 251 $group = groups_get_group($id);
1c7b8b93
NC
252 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
253 break;
254 default:
255 $info = '';
256 break;
257 }
516194d0 258
1c7b8b93
NC
259 if ($tagid) {
260 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
261 }
cae83708 262
1c7b8b93
NC
263 $header = rss_standard_header(get_string($type.'blog','blog', $info),
264 $CFG->wwwroot.'/blog/index.php',
265 get_string('intro','blog'));
516194d0 266
1c7b8b93 267 $footer = rss_standard_footer();
516194d0 268
e858c368 269 // Save the XML contents to file.
1c7b8b93 270 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
271 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
272 return $filename;
273 } else {
274 return false; // Couldn't find it or make it
516194d0 275 }
1c7b8b93 276}
516194d0 277
13d1c9ed
JF
278/**
279 * Retrieve the location and file name of a cached RSS feed
280 *
281 * @param string $type The source of the RSS feed (site/course/group/user)
282 * @param int $id The id of the item defined by $type
283 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
284 * @return string
285 */
1c7b8b93
NC
286function blog_rss_file_name($type, $id, $tagid=0) {
287 global $CFG;
516194d0 288
1c7b8b93 289 if ($tagid) {
365bec4c 290 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 291 } else {
365bec4c 292 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 293 }
1c7b8b93 294}
cae83708 295
13d1c9ed
JF
296/**
297 * This function saves to file the rss feed specified in the parameters
298 *
299 * @param string $type The source of the RSS feed (site/course/group/user)
300 * @param int $id The id of the item defined by $type
301 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
302 * @param string $contents The contents of the RSS Feed file
303 * @return bool whether the save was successful or not
304 */
1c7b8b93
NC
305function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
306 global $CFG;
71904f4d 307
e858c368 308 $status = true;
516194d0 309
e858c368 310 //blog creates some additional dirs within the rss cache so make sure they all exist
5a87c912
TL
311 make_cache_directory('rss/blog');
312 make_cache_directory('rss/blog/'.$type);
ebb73f49 313
71904f4d
PS
314 $filename = blog_rss_file_name($type, $id, $tagid);
315 $expandfilename = false; //we're supplying a full file path
316 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 317
1c7b8b93
NC
318 return $status;
319}
320