MDL-50268 core: proper deprecation of get_file_url() method
[moodle.git] / lib / rsslib.php
CommitLineData
50fcb1d8 1<?php
2
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 * This file contains all the common stuff to be used in RSS System
20 *
13d1c9ed
JF
21 * @package core_rss
22 * @category rss
78bfb562
PS
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50fcb1d8 25 */
8adcb49f 26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
13d1c9ed
JF
29/**
30 * Build the URL for the RSS feed and add it as a header
31 *
32 * @param stdClass $context The context under which the URL should be created
33 * @param string $componentname The name of the component for which the RSS feed exists
34 * @param stdClass $componentinstance The instance of the component
35 * @param string $title Name for the link to be added to the page header
36 */
37function rss_add_http_header($context, $componentname, $componentinstance, $title) {
9e86f2e7 38 global $PAGE, $USER;
e858c368
AD
39
40 $componentid = null;
41 if (is_object($componentinstance)) {
42 $componentid = $componentinstance->id;
43 } else {
44 $componentid = $componentinstance;
45 }
46
47 $rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentid);
9e86f2e7
AD
48 $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
49 }
50
007b2f04 51/**
13d1c9ed 52 * Print the link for the RSS feed with the correct RSS icon
50fcb1d8 53 *
13d1c9ed
JF
54 * @param stdClass $contextid The id of the context under which the URL should be created
55 * @param int $userid The source of the RSS feed (site/course/group/user)
56 * @param string $componentname The name of the component for which the feed exists
57 * @param string $id The name by which to call the RSS File
58 * @param string $tooltiptext The tooltip to be displayed with the link
59 * @return string HTML output for the RSS link
007b2f04 60 */
43b92251 61function rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
f2a1963c 62 global $OUTPUT;
8adcb49f 63
8adcb49f 64 static $rsspath = '';
65
43b92251 66 $rsspath = rss_get_url($contextid, $userid, $componentname, $id);
b5d0cafc 67 $rsspix = $OUTPUT->pix_url('i/rss');
d4706960 68
c46fce71 69 return '<a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a>';
d4706960 70}
71
007b2f04 72/**
73 * This function returns the URL for the RSS XML file.
50fcb1d8 74 *
13d1c9ed
JF
75 * @param int $contextid the course id
76 * @param int $userid the current user id
77 * @param string $componentname the name of the current component. For example "forum"
e858c368 78 * @param string $additionalargs For modules, module instance id
13d1c9ed 79 * @return string the url of the RSS feed
007b2f04 80 */
e858c368 81function rss_get_url($contextid, $userid, $componentname, $additionalargs) {
d4706960 82 global $CFG;
cd1b845f
DP
83 if (empty($userid)) {
84 $userid = guest_user()->id;
85 }
fcce139a 86 $usertoken = rss_get_token($userid);
11d4bea2
SL
87 $url = '/rss/file.php';
88 return moodle_url::make_file_url($url, '/'.$contextid.'/'.$usertoken.'/'.$componentname.'/'.$additionalargs.'/rss.xml');
8adcb49f 89}
90
007b2f04 91/**
13d1c9ed
JF
92 * Print the link for the RSS feed with the correct RSS icon (Theme based)
93 *
94 * @param stdClass $contextid The id of the context under which the URL should be created
95 * @param int $userid The source of the RSS feed (site/course/group/user)
96 * @param string $componentname The name of the component for which the feed exists
97 * @param string $id The name by which to call the RSS File
98 * @param string $tooltiptext The tooltip to be displayed with the link
007b2f04 99 */
43b92251
AD
100function rss_print_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
101 print rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext);
65f1df62 102
103}
8adcb49f 104
007b2f04 105/**
fcce139a 106 * Given an object, deletes all RSS files associated with it.
50fcb1d8 107 *
13d1c9ed
JF
108 * @param string $componentname the name of the module ie 'forum'. Used to construct the cache path.
109 * @param stdClass $instance An object with an id member variable ie $forum, $glossary.
007b2f04 110 */
43b92251 111function rss_delete_file($componentname, $instance) {
fcce139a 112 global $CFG;
29b71de6 113
365bec4c 114 $dirpath = "$CFG->cachedir/rss/$componentname";
a74bea12 115 if (is_dir($dirpath)) {
80c27aab 116 if (!$dh = opendir($dirpath)) {
7df271d9 117 error_log("Directory permission error. RSS directory store for component '{$componentname}' exists but cannot be opened.", DEBUG_DEVELOPER);
80c27aab
CF
118 return;
119 }
a74bea12
AD
120 while (false !== ($filename = readdir($dh))) {
121 if ($filename!='.' && $filename!='..') {
122 if (preg_match("/{$instance->id}_/", $filename)) {
43b92251 123 unlink("$dirpath/$filename");
a74bea12 124 }
fcce139a
AD
125 }
126 }
127 }
128}
8adcb49f 129
fcce139a
AD
130/**
131 * Are RSS feeds enabled for the supplied module instance?
13d1c9ed
JF
132 *
133 * @param string $modname The name of the module to be checked
134 * @param stdClass $instance An instance of an activity module ie $forum, $glossary.
135 * @param bool $hasrsstype Should there be a rsstype member variable?
136 * @param bool $hasrssarticles Should there be a rssarticles member variable?
137 * @return bool whether or not RSS is enabled for the module
fcce139a 138 */
43b92251 139function rss_enabled_for_mod($modname, $instance=null, $hasrsstype=true, $hasrssarticles=true) {
fcce139a
AD
140 if ($hasrsstype) {
141 if (empty($instance->rsstype) || $instance->rsstype==0) {
142 return false;
143 }
2f29ee13 144 }
145
fcce139a
AD
146 //have they set the RSS feed to return 0 results?
147 if ($hasrssarticles) {
148 if (empty($instance->rssarticles) || $instance->rssarticles==0) {
149 return false;
8adcb49f 150 }
151 }
fcce139a 152
43b92251 153 if (!empty($instance) && !instance_is_visible($modname,$instance)) {
fcce139a 154 return false;
8adcb49f 155 }
156
fcce139a 157 return true;
8adcb49f 158}
159
007b2f04 160/**
161 * This function saves to file the rss feed specified in the parameters
50fcb1d8 162 *
13d1c9ed
JF
163 * @param string $componentname the module name ie forum. Used to create a cache directory.
164 * @param string $filename the name of the file to be created ie "rss.xml"
165 * @param string $contents the data to be written to the file
166 * @param bool $expandfilename whether or not the fullname of the RSS file should be used
167 * @return bool whether the save was successful or not
007b2f04 168 */
e858c368 169function rss_save_file($componentname, $filename, $contents, $expandfilename=true) {
8adcb49f 170 global $CFG;
29b71de6 171
8adcb49f 172 $status = true;
173
8f6e5a8d 174 if (! $basedir = make_cache_directory ('rss/'. $componentname)) {
bc706aeb 175 //Cannot be created, so error
176 $status = false;
177 }
8adcb49f 178
179 if ($status) {
e858c368
AD
180 $fullfilename = $filename;
181 if ($expandfilename) {
182 $fullfilename = rss_get_file_full_name($componentname, $filename);
183 }
184
fcce139a 185 $rss_file = fopen($fullfilename, "w");
8adcb49f 186 if ($rss_file) {
e858c368 187 $status = fwrite ($rss_file, $contents);
8adcb49f 188 fclose($rss_file);
bc706aeb 189 } else {
190 $status = false;
8adcb49f 191 }
192 }
193 return $status;
194}
195
13d1c9ed
JF
196/**
197 * Retrieve the location and file name of a cached RSS feed
198 *
199 * @param string $componentname the name of the component the RSS feed is being created for
200 * @param string $filename the name of the RSS FEED
201 * @return string The full name and path of the RSS file
202 */
43b92251 203function rss_get_file_full_name($componentname, $filename) {
6069e206 204 global $CFG;
365bec4c 205 return "$CFG->cachedir/rss/$componentname/$filename.xml";
fcce139a 206}
6069e206 207
13d1c9ed
JF
208/**
209 * Construct the file name of the RSS File
210 *
211 * @param stdClass $instance the instance of the source of the RSS feed
212 * @param string $sql the SQL used to produce the RSS feed
5fe65fd4 213 * @param array $params the parameters used in the SQL query
13d1c9ed
JF
214 * @return string the name of the RSS file
215 */
5fe65fd4
MN
216function rss_get_file_name($instance, $sql, $params = array()) {
217 if ($params) {
218 // If a parameters array is passed, then we want to
219 // serialize it and then concatenate it with the sql.
220 // The reason for this is to generate a unique filename
221 // for queries using the same sql but different parameters.
8f6e47d9 222 asort($params);
5fe65fd4
MN
223 $serializearray = serialize($params);
224 return $instance->id.'_'.md5($sql . $serializearray);
225 } else {
226 return $instance->id.'_'.md5($sql);
227 }
6069e206 228}
229
007b2f04 230/**
231 * This function return all the common headers for every rss feed in the site
50fcb1d8 232 *
13d1c9ed
JF
233 * @param string $title the title for the RSS Feed
234 * @param string $link the link for the origin of the RSS feed
235 * @param string $description the description of the contents of the RSS feed
236 * @return bool|string the standard header for the RSS feed
007b2f04 237 */
8adcb49f 238function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
9598cecf 239 global $CFG, $USER, $OUTPUT;
8adcb49f 240
8adcb49f 241 $status = true;
242 $result = "";
243
2a250a0b 244 $site = get_site();
8adcb49f 245
246 if ($status) {
247
248 //Calculate title, link and description
249 if (empty($title)) {
268ddd50 250 $title = format_string($site->fullname);
8adcb49f 251 }
252 if (empty($link)) {
253 $link = $CFG->wwwroot;
254 }
255 if (empty($description)) {
256 $description = $site->summary;
257 }
258
259 //xml headers
260 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
261 $result .= "<rss version=\"2.0\">\n";
262
263 //open the channel
48005861 264 $result .= rss_start_tag('channel', 1, true);
8adcb49f 265
266 //write channel info
75e8eaeb 267 $result .= rss_full_tag('title', 2, false, strip_tags($title));
48005861 268 $result .= rss_full_tag('link', 2, false, $link);
269 $result .= rss_full_tag('description', 2, false, $description);
410a24c0 270 $result .= rss_full_tag('generator', 2, false, 'Moodle');
ec6dc3f6 271 if (!empty($USER->lang)) {
48005861 272 $result .= rss_full_tag('language', 2, false, substr($USER->lang,0,2));
ec6dc3f6 273 }
8adcb49f 274 $today = getdate();
d139e464 275 $result .= rss_full_tag('copyright', 2, false, '(c) '. $today['year'] .' '. format_string($site->fullname));
95a86f75 276 /*
6b263129 277 if (!empty($USER->email)) {
91247b89 278 $result .= rss_full_tag('managingEditor', 2, false, fullname($USER));
279 $result .= rss_full_tag('webMaster', 2, false, fullname($USER));
ec6dc3f6 280 }
6b263129 281 */
8adcb49f 282
283 //write image info
b5d0cafc 284 $rsspix = $OUTPUT->pix_url('i/rsssitelogo');
8adcb49f 285
007b2f04 286 //write the info
48005861 287 $result .= rss_start_tag('image', 2, true);
288 $result .= rss_full_tag('url', 3, false, $rsspix);
289 $result .= rss_full_tag('title', 3, false, 'moodle');
290 $result .= rss_full_tag('link', 3, false, $CFG->wwwroot);
291 $result .= rss_full_tag('width', 3, false, '140');
292 $result .= rss_full_tag('height', 3, false, '35');
293 $result .= rss_end_tag('image', 2, true);
8adcb49f 294 }
295
296 if (!$status) {
297 return false;
298 } else {
299 return $result;
300 }
301}
302
13d1c9ed
JF
303
304/**
305 * Generates the rss XML code for every item passed in the array
306 *
307 * item->title: The title of the item
308 * item->author: The author of the item. Optional !!
309 * item->pubdate: The pubdate of the item
310 * item->link: The link url of the item
311 * item->description: The content of the item
312 *
313 * @param array $items an array of item objects
314 * @return bool|string the rss XML code for every item passed in the array
315 */
8adcb49f 316function rss_add_items($items) {
8adcb49f 317 global $CFG;
29b71de6 318
9d575747 319 $result = '';
8adcb49f 320
321 if (!empty($items)) {
322 foreach ($items as $item) {
9d575747 323 $result .= rss_start_tag('item',2,true);
6d6002a0 324 //Include the category if exists (some rss readers will use it to group items)
325 if (isset($item->category)) {
326 $result .= rss_full_tag('category',3,false,$item->category);
327 }
08577617 328 if (isset($item->tags)) {
329 $attributes = array();
330 if (isset($item->tagscheme)) {
5a84cc70 331 $attributes['domain'] = s($item->tagscheme);
08577617 332 }
333 foreach ($item->tags as $tag) {
334 $result .= rss_full_tag('category', 3, false, $tag, $attributes);
335 }
336 }
75e8eaeb 337 $result .= rss_full_tag('title',3,false,strip_tags($item->title));
9d575747 338 $result .= rss_full_tag('link',3,false,$item->link);
069b6daf 339 $result .= rss_add_enclosures($item);
babe6a0a 340 $result .= rss_full_tag('pubDate',3,false,gmdate('D, d M Y H:i:s',$item->pubdate).' GMT'); # MDL-12563
6f5e0852 341 //Include the author if exists
2b04c41c 342 if (isset($item->author) && !empty($item->author)) {
5744be47 343 //$result .= rss_full_tag('author',3,false,$item->author);
007b2f04 344 //We put it in the description instead because it's more important
5744be47 345 //for moodle than most other feeds, and most rss software seems to ignore
346 //the author field ...
1d5a7c4b 347 $item->description = get_string('byname','',$item->author).'. &nbsp;<p>'.$item->description.'</p>';
ca87cbe1 348 }
9d575747 349 $result .= rss_full_tag('description',3,false,$item->description);
1d5a7c4b 350 $result .= rss_full_tag('guid',3,false,$item->link,array('isPermaLink' => 'true'));
9d575747 351 $result .= rss_end_tag('item',2,true);
8adcb49f 352
353 }
354 } else {
355 $result = false;
356 }
357 return $result;
358}
359
007b2f04 360/**
6c90c5ca 361 * This function return all the common footers for every rss feed in the site.
13d1c9ed 362 *
13d1c9ed 363 * @return string
007b2f04 364 */
6c90c5ca 365function rss_standard_footer() {
8adcb49f 366 $status = true;
9d575747 367 $result = '';
8adcb49f 368
48005861 369 $result .= rss_end_tag('channel', 1, true);
48005861 370 $result .= '</rss>';
8adcb49f 371
372 return $result;
373}
374
13d1c9ed 375
007b2f04 376/**
13d1c9ed
JF
377 * This function return an error xml file (string) to be sent when a rss is required (file.php) and something goes wrong
378 *
379 * @param string $errortype Type of error to send, default is rsserror
380 * @return stdClass returns a XML Feed with an error message in it
007b2f04 381 */
fcce139a 382function rss_geterrorxmlfile($errortype = 'rsserror') {
ec6dc3f6 383 global $CFG;
384
385 $return = '';
386
387 //XML Header
388 $return = rss_standard_header();
389
29b71de6 390 //XML item
ec6dc3f6 391 if ($return) {
365a5941 392 $item = new stdClass();
007b2f04 393 $item->title = "RSS Error";
394 $item->link = $CFG->wwwroot;
395 $item->pubdate = time();
fcce139a 396 $item->description = get_string($errortype);
ec6dc3f6 397 $return .= rss_add_items(array($item));
398 }
399
400 //XML Footer
401 if ($return) {
402 $return .= rss_standard_footer();
403 }
29b71de6 404
ec6dc3f6 405 return $return;
406}
407
13d1c9ed
JF
408/**
409 * Get the ID of the user from a given RSS Token
410 *
411 * @param string $token the RSS token you would like to use to find the user id
412 * @return int The user id
413 */
fcce139a
AD
414function rss_get_userid_from_token($token) {
415 global $DB;
b30dc391
DP
416
417 $sql = 'SELECT u.id FROM {user} u
418 JOIN {user_private_key} k ON u.id = k.userid
419 WHERE u.deleted = 0 AND u.confirmed = 1
85aec6a4 420 AND u.suspended = 0 AND k.value = ?';
b30dc391 421 return $DB->get_field_sql($sql, array($token), IGNORE_MISSING);
fcce139a
AD
422}
423
13d1c9ed
JF
424/**
425 * Get the RSS Token from a given user id
426 *
427 * @param int $userid The user id
428 * @return string the RSS token for the user
429 */
fcce139a
AD
430function rss_get_token($userid) {
431 return get_user_key('rss', $userid);
432}
433
13d1c9ed
JF
434/**
435 * Removes the token for the given user from the DB
436 * @param int $userid The user id for the token you wish to delete
437 */
ffa3bfb3
AD
438function rss_delete_token($userid) {
439 delete_user_key('rss', $userid);
440}
fcce139a 441
007b2f04 442/**
443 * Return the xml start tag
13d1c9ed
JF
444 *
445 * @param string $tag the xml tag name
446 * @param int $level the indentation level
447 * @param bool $endline whether or not to start new tags on a new line
448 * @param array $attributes the attributes of the xml tag
449 * @return string the xml start tag
007b2f04 450 */
1d5a7c4b 451function rss_start_tag($tag,$level=0,$endline=false,$attributes=null) {
8adcb49f 452 if ($endline) {
453 $endchar = "\n";
454 } else {
455 $endchar = "";
456 }
1d5a7c4b 457 $attrstring = '';
458 if (!empty($attributes) && is_array($attributes)) {
459 foreach ($attributes as $key => $value) {
460 $attrstring .= " ".$key."=\"".$value."\"";
461 }
462 }
463 return str_repeat(" ",$level*2)."<".$tag.$attrstring.">".$endchar;
8adcb49f 464}
465
007b2f04 466/**
467 * Return the xml end tag
13d1c9ed
JF
468 * @param string $tag the xml tag name
469 * @param int $level the indentation level
470 * @param bool $endline whether or not to start new tags on a new line
471 * @return string the xml end tag
007b2f04 472 */
8adcb49f 473function rss_end_tag($tag,$level=0,$endline=true) {
474 if ($endline) {
475 $endchar = "\n";
476 } else {
477 $endchar = "";
478 }
479 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
480}
481
007b2f04 482/**
13d1c9ed
JF
483 * Return the while xml element, including content
484 *
485 * @param string $tag the xml tag name
486 * @param int $level the indentation level
487 * @param bool $endline whether or not to start new tags on a new line
488 * @param string $content the text to go inside the tag
489 * @param array $attributes the attributes of the xml tag
490 * @return string the whole xml element
007b2f04 491 */
1d5a7c4b 492function rss_full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
1d5a7c4b 493 $st = rss_start_tag($tag,$level,$endline,$attributes);
8adcb49f 494 $co="";
810944af 495 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
8adcb49f 496 $et = rss_end_tag($tag,0,true);
1d5a7c4b 497
8adcb49f 498 return $st.$co.$et;
499}
500
410a24c0 501/**
770b7f6c 502 * Adds RSS Media Enclosures for "podcasting" by including attachments that
503 * are specified in the item->attachments field.
50fcb1d8 504 *
13d1c9ed
JF
505 * @param stdClass $item representing an RSS item
506 * @return string RSS enclosure tags
50fcb1d8 507 */
410a24c0 508function rss_add_enclosures($item){
6db0338e 509 global $CFG;
510
410a24c0 511 $returnstring = '';
29b71de6 512
410a24c0 513 // list of media file extensions and their respective mime types
6db0338e 514 include_once($CFG->libdir.'/filelib.php');
515 $mediafiletypes = get_mimetypes_array();
410a24c0 516
95a86f75 517 // take into account attachments (e.g. from forum) - with these, we are able to know the file size
518 if (isset($item->attachments) && is_array($item->attachments)) {
519 foreach ($item->attachments as $attachment){
909409b5 520 $extension = strtolower(substr($attachment->url, strrpos($attachment->url, '.')+1));
521 if (isset($mediafiletypes[$extension]['type'])) {
522 $type = $mediafiletypes[$extension]['type'];
523 } else {
524 $type = 'document/unknown';
525 }
069b6daf 526 $returnstring .= "\n<enclosure url=\"$attachment->url\" length=\"$attachment->length\" type=\"$type\" />\n";
95a86f75 527 }
528 }
29b71de6 529
410a24c0 530 return $returnstring;
531}