Updated for better reporting when loading from remote url fails. Now displays error...
[moodle.git] / rss / rsslib.php
CommitLineData
b2ddd9d7 1<?php // $Id$
8adcb49f 2 // This file contains all the common stuff to be used in RSS System
3
65f1df62 4//This function returns the icon (from theme) with the link to rss/file.php
5function rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
8adcb49f 6
7 global $CFG, $THEME, $USER;
8
9 static $pixpath = '';
10 static $rsspath = '';
11
b2ddd9d7 12 //In site course, if no logged (userid), use admin->id. Bug 2048.
13 if ($courseid == SITEID and empty($userid)) {
14 $admin = get_admin();
15 $userid = $admin->id;
16 }
17
8adcb49f 18 if ($CFG->slasharguments) {
19 $rsspath = "$CFG->wwwroot/rss/file.php/$courseid/$userid/$modulename/$id/rss.xml";
20 } else {
21 $rsspath = "$CFG->wwwroot/rss/file.php?file=/$courseid/$userid/$modulename/$id/rss.xml";
22 }
23
24 if (empty($pixpath)) {
25 if (empty($THEME->custompix)) {
26 $pixpath = "$CFG->wwwroot/pix";
27 } else {
28 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
29 }
30 }
31
32 $rsspix = $pixpath."/i/rss.gif";
33
839f2456 34 return "<a href=\"".$rsspath."\"><img src=\"$rsspix\" title=\"$tooltiptext\" alt=\"\" /></a>";
8adcb49f 35
36}
37
65f1df62 38//This function prints the icon (from theme) with the link to rss/file.php
39function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
40
41 echo rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext);
42
43}
8adcb49f 44//This function iterates over each module in the server to see if
45//it supports generating rss feeds, searching for a MODULENAME_rss_feeds()
46//function and invoking it foreach activity as necessary
47function cron_rss_feeds () {
48
49 global $CFG;
50
51 $status = true;
52
b65a50e5 53 mtrace(" Generating rssfeeds...");
8adcb49f 54
2f29ee13 55 //Check for required functions...
56 if(!function_exists('utf8_encode')) {
b65a50e5 57 mtrace(" ERROR: You need to add XML support to your PHP installation!");
2f29ee13 58 return true;
59 }
60
8adcb49f 61 if ($allmods = get_records("modules") ) {
62 foreach ($allmods as $mod) {
b65a50e5 63 mtrace(' '.$mod->name.': ', '');
8adcb49f 64 $modname = $mod->name;
65 $modfile = "$CFG->dirroot/mod/$modname/rsslib.php";
66 //If file exists and we have selected to restore that type of module
67 if (file_exists($modfile)) {
68 include_once($modfile);
69 $generaterssfeeds = $modname.'_rss_feeds';
70 if (function_exists($generaterssfeeds)) {
71 if ($status) {
b65a50e5 72 mtrace('generating ', '');;
8adcb49f 73 $status = $generaterssfeeds();
74 if (!empty($status)) {
b65a50e5 75 mtrace("...OK");
8adcb49f 76 } else {
b65a50e5 77 mtrace("...FAILED");
8adcb49f 78 }
79 } else {
b65a50e5 80 mtrace("...SKIPPED (failed above)");
8adcb49f 81 }
82 } else {
b65a50e5 83 mtrace("...NOT SUPPORTED (function)");
8adcb49f 84 }
85 } else {
b65a50e5 86 mtrace("...NOT SUPPORTED (file)");
8adcb49f 87 }
88 }
89 }
b65a50e5 90 mtrace(" Ending rssfeeds...", '');
8adcb49f 91 if (!empty($status)) {
b65a50e5 92 mtrace("...OK");
8adcb49f 93 } else {
b65a50e5 94 mtrace("...FAILED");
8adcb49f 95 }
96
97 return $status;
98}
99
100//This function saves to file the rss feed specified in the parameters
101function rss_save_file ($modname,$mod,$result) {
102
103 global $CFG;
104
105 $status = true;
106
bc706aeb 107 if (! $basedir = make_upload_directory ("rss/".$modname)) {
108 //Cannot be created, so error
109 $status = false;
110 }
8adcb49f 111
112 if ($status) {
6069e206 113 $file = rss_file_name($modname, $mod);
8adcb49f 114 $rss_file = fopen($file,"w");
115 if ($rss_file) {
116 $status = fwrite ($rss_file,$result);
117 fclose($rss_file);
bc706aeb 118 } else {
119 $status = false;
8adcb49f 120 }
121 }
122 return $status;
123}
124
6069e206 125
126function rss_file_name($modname, $mod) {
127 global $CFG;
128
129 return "$CFG->dataroot/rss/$modname/$mod->id.xml";
130}
131
8adcb49f 132//This function return all the common headers for every rss feed in the site
133function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
134
1adf511c 135 global $CFG, $THEME, $USER;
8adcb49f 136
137 static $pixpath = '';
138
139 $status = true;
140 $result = "";
141
142 if (!$site = get_site()) {
143 $status = false;
144 }
145
146 if ($status) {
147
148 //Calculate title, link and description
149 if (empty($title)) {
150 $title = $site->fullname;
151 }
152 if (empty($link)) {
153 $link = $CFG->wwwroot;
154 }
155 if (empty($description)) {
156 $description = $site->summary;
157 }
158
159 //xml headers
160 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
161 $result .= "<rss version=\"2.0\">\n";
162
163 //open the channel
164 $result .= rss_start_tag("channel",1,true);
165
166 //write channel info
167 $result .= rss_full_tag("title",2,false,$title);
168 $result .= rss_full_tag("link",2,false,$link);
169 $result .= rss_full_tag("description",2,false,$description);
ec6dc3f6 170 if (!empty($USER->lang)) {
171 $result .= rss_full_tag("language",2,false,substr($USER->lang,0,2));
172 }
8adcb49f 173 $today = getdate();
174 $result .= rss_full_tag("copyright",2,false,"&copy; ".$today['year']." ".$site->fullname);
ec6dc3f6 175 if (!empty($USER->email)) {
176 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
177 $result .= rss_full_tag("webMaster",2,false,$USER->email);
178 }
8adcb49f 179
180 //write image info
181 //Calculate the origin
182 if (empty($pixpath)) {
183 if (empty($THEME->custompix)) {
184 $pixpath = "$CFG->wwwroot/pix";
185 } else {
186 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
187 }
188 }
189 $rsspix = $pixpath."/i/rsssitelogo.gif";
190
191 //write the info
192 $result .= rss_start_tag("image",2,true);
193 $result .= rss_full_tag("url",3,false,$rsspix);
194 $result .= rss_full_tag("title",3,false,"moodle");
195 $result .= rss_full_tag("link",3,false,$CFG->wwwroot);
196 $result .= rss_full_tag("width",3,false,"140");
197 $result .= rss_full_tag("height",3,false,"35");
198 $result .= rss_end_tag("image",2,true);
199 }
200
201 if (!$status) {
202 return false;
203 } else {
204 return $result;
205 }
206}
207
208//This function returns the rss XML code for every item passed in the array
209//item->title: The title of the item
ca87cbe1 210//item->author: The author of the item. Optional !!
8adcb49f 211//item->pubdate: The pubdate of the item
212//item->link: The link url of the item
213//item->description: The content of the item
214function rss_add_items($items) {
215
216 global $CFG;
217
218 $result = "";
219
220 if (!empty($items)) {
221 foreach ($items as $item) {
222 $result .= rss_start_tag("item",2,true);
223 $result .= rss_full_tag("title",3,false,$item->title);
224 $result .= rss_full_tag("link",3,false,$item->link);
225 $result .= rss_full_tag("pubDate",3,false,date("r",$item->pubdate));
ca87cbe1 226 //Include the author if exists
227 if (isset($item->author)) {
228 $item->description = get_string("byname","",$item->author)."<p>".$item->description;
229 }
8adcb49f 230 $result .= rss_full_tag("description",3,false,$item->description);
231 $result .= rss_end_tag("item",2,true);
232
233 }
234 } else {
235 $result = false;
236 }
237 return $result;
238}
239
240//This function return all the common footers for every rss feed in the site
241function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
242
243 global $CFG, $USER;
244
245 $status = true;
246 $result = "";
247
248 //Close the chanel
249 $result .= rss_end_tag("channel",1,true);
250 ////Close the rss tag
251 $result .= "</rss>";
252
253 return $result;
254}
255
ec6dc3f6 256//This function return an error xml file (string)
257//to be sent when a rss is required (file.php)
258//and something goes wrong
259function rss_geterrorxmlfile() {
260
261 global $CFG;
262
263 $return = '';
264
265 //XML Header
266 $return = rss_standard_header();
267
268 //XML item
269 if ($return) {
270 $item->title = "RSS Error";
271 $item->link = $CFG->wwwroot;
272 $item->pubdate = time();
273 $item->description = get_string("rsserror");
274 $return .= rss_add_items(array($item));
275 }
276
277 //XML Footer
278 if ($return) {
279 $return .= rss_standard_footer();
280 }
281
282 return $return;
283}
284
8adcb49f 285// ===== This function are used to write XML tags =========
286// [stronk7]: They are similar to the glossary export and backup generation
287// but I've replicated them here because they have some minor
288// diferences. Someday all they should go to a common place.
289
290//Return the xml start tag
291function rss_start_tag($tag,$level=0,$endline=false) {
292 if ($endline) {
293 $endchar = "\n";
294 } else {
295 $endchar = "";
296 }
297 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
298}
299
300//Return the xml end tag
301function rss_end_tag($tag,$level=0,$endline=true) {
302 if ($endline) {
303 $endchar = "\n";
304 } else {
305 $endchar = "";
306 }
307 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
308}
309
310//Return the start tag, the contents and the end tag
311function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
312 //Here we encode absolute links
313 $st = rss_start_tag($tag,$level,$endline);
314 $co="";
315 if ($to_utf) {
316 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
317 } else {
318 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
319 }
320 $et = rss_end_tag($tag,0,true);
321 return $st.$co.$et;
322}
323
324?>