date manipulation script
[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
bab450a0 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)) {
bab450a0 26 $pixpath = $CFG->wwwroot .'/pix';
8adcb49f 27 } else {
bab450a0 28 $pixpath = $CFG->wwwroot .'/theme/'. $CFG->theme .'/pix';
8adcb49f 29 }
30 }
31
bab450a0 32 $rsspix = $pixpath .'/i/rss.gif';
8adcb49f 33
bab450a0 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
bab450a0 39function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext='') {
65f1df62 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
bab450a0 53 mtrace(' Generating rssfeeds...');
8adcb49f 54
2f29ee13 55 //Check for required functions...
56 if(!function_exists('utf8_encode')) {
bab450a0 57 mtrace(' ERROR: You need to add XML support to your PHP installation!');
2f29ee13 58 return true;
59 }
60
bab450a0 61 if ($allmods = get_records('modules') ) {
8adcb49f 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)) {
bab450a0 75 mtrace('...OK');
8adcb49f 76 } else {
bab450a0 77 mtrace('...FAILED');
8adcb49f 78 }
79 } else {
bab450a0 80 mtrace('...SKIPPED (failed above)');
8adcb49f 81 }
82 } else {
bab450a0 83 mtrace('...NOT SUPPORTED (function)');
8adcb49f 84 }
85 } else {
bab450a0 86 mtrace('...NOT SUPPORTED (file)');
8adcb49f 87 }
88 }
89 }
bab450a0 90 mtrace(' Ending rssfeeds...', '');
8adcb49f 91 if (!empty($status)) {
bab450a0 92 mtrace('...OK');
8adcb49f 93 } else {
bab450a0 94 mtrace('...FAILED');
8adcb49f 95 }
96
97 return $status;
98}
99
100//This function saves to file the rss feed specified in the parameters
bab450a0 101function rss_save_file ($modname, $mod, $result) {
8adcb49f 102
103 global $CFG;
104
105 $status = true;
106
bab450a0 107 if (! $basedir = make_upload_directory ('rss/'. $modname)) {
bc706aeb 108 //Cannot be created, so error
109 $status = false;
110 }
8adcb49f 111
112 if ($status) {
6069e206 113 $file = rss_file_name($modname, $mod);
bab450a0 114 $rss_file = fopen($file, "w");
8adcb49f 115 if ($rss_file) {
bab450a0 116 $status = fwrite ($rss_file, $result);
8adcb49f 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
e8181a56 324//////////////////// LIBRARY FUNCTIONS FOR RSS_CLIENT BLOCK ////////////////
325
2be9581d 326//initialize default config vars for rss_client block if needed
327if (!isset($CFG->block_rss_client_submitters) ) {
e8181a56 328 $CFG->block_rss_client_submitters = 1; //default to admin only
329}
330if (empty($CFG->block_rss_client_num_entries) ) {
331 $CFG->block_rss_client_num_entries = 5; //default to 5 entries per block
332}
2be9581d 333if (!isset($CFG->block_rss_timeout) ) {
e8181a56 334 $CFG->block_rss_timeout = 30;
335}
336
2be9581d 337// Defines for moodle's use of magpierss classes
338define('MAGPIE_DIR', $CFG->dirroot.'/rss/magpie/');
339define('MAGPIE_CACHE_DIR', $CFG->dataroot .'/cache/rsscache/');
340define('MAGPIE_CACHE_ON', true); //should be exposed as an admin config option
341define('MAGPIE_CACHE_FRESH_ONLY', false); //should be exposed as an admin config option
342define('MAGPIE_CACHE_AGE', $CFG->block_rss_timeout);
343if ($CFG->debug) {
344 define('MAGPIE_DEBUG', true);
345} else {
346 define('MAGPIE_DEBUG', false);
e8181a56 347}
348
2be9581d 349// defines for config var block_rss_client_submitters
350define('SUBMITTERS_ALL_ACCOUNT_HOLDERS', 0);
351define('SUBMITTERS_ADMIN_ONLY', 1);
352define('SUBMITTERS_ADMIN_AND_TEACHER', 2);
e8181a56 353
354/**
355 * @param int $rssid .
356 */
357function rss_display_feeds($rssid='none') {
358 global $db, $USER, $CFG, $THEME;
359 global $blogid; //hackish, but if there is a blogid it would be good to preserve it
360
bab450a0 361 static $pixpath = '';
362 if (empty($pixpath)) {
363 if (empty($THEME->custompix)) {
364 $pixpath = $CFG->wwwroot .'/pix';
365 } else {
366 $pixpath = $CFG->wwwroot .'/theme/'. $CFG->theme .'/pix';
367 }
368 }
369
370 $rsspix = $pixpath .'/i/rss.gif';
371
e8181a56 372 $closeTable = false;
373 //Daryl Hawes note: convert this sql statement to a moodle function call
374 if ($rssid != 'none'){
375 $sql = 'SELECT * FROM '. $CFG->prefix .'block_rss_client WHERE id='. $rssid;
376 } else {
377 $sql = 'SELECT * FROM '. $CFG->prefix .'block_rss_client';
378 }
379
380 $res = $db->Execute($sql);
381// print_object($res); //debug
382
383 if ($res->fields){
384 $closeTable = true;
385 ?>
386 <table width="100%">
387 <tr bgcolor="<?php echo $THEME->cellheading;?>" class="forumpostheadertopic">
388 <td><?php print_string('block_rss_feed', 'block_rss_client'); ?></td>
389 <td><?php print_string('edit'); ?></td>
390 <td><?php print_string('delete'); ?></td>
391 </tr>
392 <?php
393 }
394
395 if (isset($res) && $res->fields){
396 while(!$res->EOF) {
397 $editString = '&nbsp;';
398 $deleteString = '&nbsp;';
399 if ($res->fields['userid'] == $USER->id || isadmin()){
400 $editString = '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?act=rss_edit&rssid='. $res->fields['id'] .'&blogid='. $blogid .'">';
401 $editString .= '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. get_string('edit');
2be9581d 402 $editString .= '" title="'. get_string('edit') .'" align="absmiddle" height="16" width="16" border="0" /></a>';
e8181a56 403
404 $deleteString = '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?act=delfeed&rssid='. $res->fields['id'];
405 $deleteString .= '&blogid='. $blogid .'" onClick="return confirm(\''. get_string('block_rss_delete_feed_confirm', 'block_rss_client') .'\');">';
406 $deleteString .= '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. get_string('delete');
2be9581d 407 $deleteString .= '" title="'. get_string('delete') .'" align="absmiddle" border="0" /></a>';
e8181a56 408 }
409 print '<tr bgcolor="'. $THEME->cellcontent .'" class="forumpostmessage"><td><strong><a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?act=view&rssid=';
410 print $res->fields['id'] .'&blogid='. $blogid .'">'. $res->fields['title'] .'</a></strong><br />' ."\n";
411 print $res->fields['description'] .'&nbsp;<br />' ."\n";
bab450a0 412 print $res->fields['url'] .'&nbsp;&nbsp;<a href="'. $res->fields['url'] .'" target=_new><img src="'. $rsspix .'" border="0" /></a>' ."\n";
e8181a56 413 print '<a href="http://feeds.archive.org/validator/check?url='. $res->fields['url'] .'">(Validate)</a>';
414 print '</td><td align="center">'. $editString .'</td>' ."\n";
2be9581d 415 print '<td align="center">'. $deleteString .'</td>' ."\n";
e8181a56 416 print '</tr>'."\n";
417 $res->MoveNext();
418 }
419 }
420 if ($closeTable){
421 print '</table>'."\n";
422 }
423}
424
425/**
426 * @param string $act .
427 * @param string $url .
428 * @param int $rssid .
429 * @param string $rsstype .
430 * @param bool $printnow .
431 */
432function rss_get_form($act, $url, $rssid, $rsstype, $printnow=true) {
433 global $USER, $CFG, $_SERVER, $blockid, $blockaction;
434 global $blogid; //hackish, but if there is a blogid it would be good to preserve it
435
436 $returnstring = '<table><tr><td valign=\"top\">';
437 if ($act == 'rss_edit') {
438 $returnstring .= get_string('edit');
439 } else {
440 $returnstring .= get_string('block_rss_add_new', 'block_rss_client');
441 }
442 $returnstring .= ' '. get_string('block_rss_feed', 'block_rss_client');
443
444 $returnstring .= '</td></tr><tr><td>';
445
446 $returnstring .= '<form action="'. $_SERVER['PHP_SELF'] .'" method=POST name="block_rss">';
447 $returnstring .= 'URL: <input type="text" size="32" maxlength="128" name="url" value="';
448 if ($act == 'rss_edit') {
449 $returnstring .= $url;
450 }
451
2be9581d 452 $returnstring .= '" /><br />';
453 //<select name="rsstype"><option value="R">RSS/RDF</option>
454 //<option value="A"';
455 //if ($act == 'rss_edit' and $rsstype == 'A') {
456 // $returnstring .= ' selected';
457 //}
e8181a56 458
2be9581d 459 //$returnstring .= '>Atom</option></select>';
e8181a56 460
461 $returnstring .= '<input type="hidden" name="act" value="';
462 if ($act == 'rss_edit') {
463 $returnstring .= 'updfeed';
464 } else {
465 $returnstring .= 'addfeed';
466 }
467 $returnstring .= '" />';
468 if ($act == 'rss_edit') {
469 $returnstring .= '<input type="hidden" name="rssid" value="'. $rssid .'" />'. "\n";
470 }
471 $returnstring .= '<input type="hidden" name="blogid" value="'. $blogid .'" />';
472 $returnstring .= '<input type="hidden" name="user" value="'. $USER->id .'" />';
473 $returnstring .= '<input type="submit" value="';
474 if ($act == 'rss_edit') {
475 $returnstring .= get_string('update');
476 } else {
477 $returnstring .= get_string('add');
478 }
479 $returnstring .= '" />&nbsp;</form>';
480
2be9581d 481// $returnstring .= '<ul>' . get_string('block_rss_find_more_feeds', 'block_rss_client');
e8181a56 482// removed as this is possibly out of place here
483// $returnstring .= '<li><a href="http://www.syndic8.com" target="_new">syndic8</a> <li><a href="http://www.newsisfree.com" target="_new">NewsIsFree</A>';
2be9581d 484// $returnstring .= '</ul>';
e8181a56 485 $returnstring .= '</td></tr></table>';
486
487 if ($printnow){
488 print $returnstring;
489 }
490 return $returnstring;
491}
bab450a0 492?>