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