Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/rsslib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029  function rss_add_http_header($context, $componentname, $componentinstance, $title) {
00030     global $PAGE, $USER;
00031 
00032     $componentid = null;
00033     if (is_object($componentinstance)) {
00034         $componentid = $componentinstance->id;
00035     } else {
00036         $componentid = $componentinstance;
00037     }
00038 
00039     $rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentid);
00040     $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
00041  }
00042 
00049 function rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
00050     global $OUTPUT;
00051 
00052     static $rsspath = '';
00053 
00054     $rsspath = rss_get_url($contextid, $userid, $componentname, $id);
00055     $rsspix = $OUTPUT->pix_url('i/rss');
00056 
00057     return '<a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a>';
00058 }
00059 
00069 function rss_get_url($contextid, $userid, $componentname, $additionalargs) {
00070     global $CFG;
00071     require_once($CFG->libdir.'/filelib.php');
00072     $usertoken = rss_get_token($userid);
00073     return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$additionalargs.'/rss.xml', null, 'rssfile');
00074 }
00075 
00079 function rss_print_link($contextid, $userid, $componentname, $id, $tooltiptext='') {
00080     print rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext);
00081 
00082 }
00083 
00092 function rss_delete_file($componentname, $instance) {
00093     global $CFG;
00094 
00095     $dirpath = "$CFG->cachedir/rss/$componentname";
00096     if (is_dir($dirpath)) {
00097         $dh  = opendir($dirpath);
00098         while (false !== ($filename = readdir($dh))) {
00099             if ($filename!='.' && $filename!='..') {
00100                 if (preg_match("/{$instance->id}_/", $filename)) {
00101                     unlink("$dirpath/$filename");
00102                 }
00103             }
00104         }
00105     }
00106 }
00107 
00114 function rss_enabled_for_mod($modname, $instance=null, $hasrsstype=true, $hasrssarticles=true) {
00115     if ($hasrsstype) {
00116         if (empty($instance->rsstype) || $instance->rsstype==0) {
00117             return false;
00118         }
00119     }
00120 
00121     //have they set the RSS feed to return 0 results?
00122     if ($hasrssarticles) {
00123         if (empty($instance->rssarticles) || $instance->rssarticles==0) {
00124             return false;
00125         }
00126     }
00127 
00128     if (!empty($instance) && !instance_is_visible($modname,$instance)) {
00129         return false;
00130     }
00131 
00132     return true;
00133 }
00134 
00143 function rss_save_file($componentname, $filename, $contents, $expandfilename=true) {
00144     global $CFG;
00145 
00146     $status = true;
00147 
00148     if (! $basedir = make_cache_directory ('rss/'. $componentname)) {
00149         //Cannot be created, so error
00150         $status = false;
00151     }
00152 
00153     if ($status) {
00154         $fullfilename = $filename;
00155         if ($expandfilename) {
00156             $fullfilename = rss_get_file_full_name($componentname, $filename);
00157         }
00158 
00159         $rss_file = fopen($fullfilename, "w");
00160         if ($rss_file) {
00161             $status = fwrite ($rss_file, $contents);
00162             fclose($rss_file);
00163         } else {
00164             $status = false;
00165         }
00166     }
00167     return $status;
00168 }
00169 
00170 
00171 function rss_get_file_full_name($componentname, $filename) {
00172     global $CFG;
00173     return "$CFG->cachedir/rss/$componentname/$filename.xml";
00174 }
00175 
00176 function rss_get_file_name($instance, $sql) {
00177     return $instance->id.'_'.md5($sql);
00178 }
00179 
00186 function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
00187     global $CFG, $USER, $OUTPUT;
00188 
00189     $status = true;
00190     $result = "";
00191 
00192     $site = get_site();
00193 
00194     if ($status) {
00195 
00196         //Calculate title, link and description
00197         if (empty($title)) {
00198             $title = format_string($site->fullname);
00199         }
00200         if (empty($link)) {
00201             $link = $CFG->wwwroot;
00202         }
00203         if (empty($description)) {
00204             $description = $site->summary;
00205         }
00206 
00207         //xml headers
00208         $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00209         $result .= "<rss version=\"2.0\">\n";
00210 
00211         //open the channel
00212         $result .= rss_start_tag('channel', 1, true);
00213 
00214         //write channel info
00215         $result .= rss_full_tag('title', 2, false, strip_tags($title));
00216         $result .= rss_full_tag('link', 2, false, $link);
00217         $result .= rss_full_tag('description', 2, false, $description);
00218         $result .= rss_full_tag('generator', 2, false, 'Moodle');
00219         if (!empty($USER->lang)) {
00220             $result .= rss_full_tag('language', 2, false, substr($USER->lang,0,2));
00221         }
00222         $today = getdate();
00223         $result .= rss_full_tag('copyright', 2, false, '&#169; '. $today['year'] .' '. format_string($site->fullname));
00224         /*
00225        if (!empty($USER->email)) {
00226             $result .= rss_full_tag('managingEditor', 2, false, fullname($USER));
00227             $result .= rss_full_tag('webMaster', 2, false, fullname($USER));
00228         }
00229        */
00230 
00231         //write image info
00232         $rsspix = $OUTPUT->pix_url('i/rsssitelogo');
00233 
00234         //write the info
00235         $result .= rss_start_tag('image', 2, true);
00236         $result .= rss_full_tag('url', 3, false, $rsspix);
00237         $result .= rss_full_tag('title', 3, false, 'moodle');
00238         $result .= rss_full_tag('link', 3, false, $CFG->wwwroot);
00239         $result .= rss_full_tag('width', 3, false, '140');
00240         $result .= rss_full_tag('height', 3, false, '35');
00241         $result .= rss_end_tag('image', 2, true);
00242     }
00243 
00244     if (!$status) {
00245         return false;
00246     } else {
00247         return $result;
00248     }
00249 }
00250 
00251 //This function returns the rss XML code for every item passed in the array
00252 //item->title: The title of the item
00253 //item->author: The author of the item. Optional !!
00254 //item->pubdate: The pubdate of the item
00255 //item->link: The link url of the item
00256 //item->description: The content of the item
00257 function rss_add_items($items) {
00258     global $CFG;
00259 
00260     $result = '';
00261 
00262     if (!empty($items)) {
00263         foreach ($items as $item) {
00264             $result .= rss_start_tag('item',2,true);
00265             //Include the category if exists (some rss readers will use it to group items)
00266             if (isset($item->category)) {
00267                 $result .= rss_full_tag('category',3,false,$item->category);
00268             }
00269             if (isset($item->tags)) {
00270                 $attributes = array();
00271                 if (isset($item->tagscheme)) {
00272                     $attributes['domain'] = s($item->tagscheme);
00273                 }
00274                 foreach ($item->tags as $tag) {
00275                     $result .= rss_full_tag('category', 3, false, $tag, $attributes);
00276                 }
00277             }
00278             $result .= rss_full_tag('title',3,false,strip_tags($item->title));
00279             $result .= rss_full_tag('link',3,false,$item->link);
00280             $result .= rss_add_enclosures($item);
00281             $result .= rss_full_tag('pubDate',3,false,gmdate('D, d M Y H:i:s',$item->pubdate).' GMT');  # MDL-12563
00282             //Include the author if exists
00283             if (isset($item->author) && !empty($item->author)) {
00284                 //$result .= rss_full_tag('author',3,false,$item->author);
00285                 //We put it in the description instead because it's more important
00286                 //for moodle than most other feeds, and most rss software seems to ignore
00287                 //the author field ...
00288                 $item->description = get_string('byname','',$item->author).'. &nbsp;<p>'.$item->description.'</p>';
00289             }
00290             $result .= rss_full_tag('description',3,false,$item->description);
00291             $result .= rss_full_tag('guid',3,false,$item->link,array('isPermaLink' => 'true'));
00292             $result .= rss_end_tag('item',2,true);
00293 
00294         }
00295     } else {
00296         $result = false;
00297     }
00298     return $result;
00299 }
00300 
00304 function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
00305     $status = true;
00306     $result = '';
00307 
00308     //Close the chanel
00309     $result .= rss_end_tag('channel', 1, true);
00311     $result .= '</rss>';
00312 
00313     return $result;
00314 }
00315 
00321 function rss_geterrorxmlfile($errortype = 'rsserror') {
00322     global $CFG;
00323 
00324     $return = '';
00325 
00326     //XML Header
00327     $return = rss_standard_header();
00328 
00329     //XML item
00330     if ($return) {
00331         $item = new stdClass();
00332         $item->title       = "RSS Error";
00333         $item->link        = $CFG->wwwroot;
00334         $item->pubdate     = time();
00335         $item->description = get_string($errortype);
00336         $return .= rss_add_items(array($item));
00337     }
00338 
00339     //XML Footer
00340     if ($return) {
00341         $return .= rss_standard_footer();
00342     }
00343 
00344     return $return;
00345 }
00346 
00347 function rss_get_userid_from_token($token) {
00348     global $DB;
00349     $record = $DB->get_record('user_private_key', array('script'=>'rss','value' => $token), 'userid', IGNORE_MISSING);
00350     if ($record) {
00351         return $record->userid;
00352     }
00353     return null;
00354 }
00355 
00356 function rss_get_token($userid) {
00357     return get_user_key('rss', $userid);
00358 }
00359 
00360 function rss_delete_token($userid) {
00361     delete_user_key('rss', $userid);
00362 }
00363 
00364 // ===== This function are used to write XML tags =========
00365 // [stronk7]: They are similar to the glossary export and backup generation
00366 // but I've replicated them here because they have some minor
00367 // diferences. Someday all they should go to a common place.
00368 
00372 function rss_start_tag($tag,$level=0,$endline=false,$attributes=null) {
00373     if ($endline) {
00374        $endchar = "\n";
00375     } else {
00376        $endchar = "";
00377     }
00378     $attrstring = '';
00379     if (!empty($attributes) && is_array($attributes)) {
00380         foreach ($attributes as $key => $value) {
00381             $attrstring .= " ".$key."=\"".$value."\"";
00382         }
00383     }
00384     return str_repeat(" ",$level*2)."<".$tag.$attrstring.">".$endchar;
00385 }
00386 
00390 function rss_end_tag($tag,$level=0,$endline=true) {
00391     if ($endline) {
00392        $endchar = "\n";
00393     } else {
00394        $endchar = "";
00395     }
00396     return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
00397 }
00398 
00402 function rss_full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
00403     $st = rss_start_tag($tag,$level,$endline,$attributes);
00404     $co="";
00405     $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
00406     $et = rss_end_tag($tag,0,true);
00407 
00408     return $st.$co.$et;
00409 }
00410 
00428 function rss_add_enclosures($item){
00429     global $CFG;
00430 
00431     $returnstring = '';
00432 
00433     // list of media file extensions and their respective mime types
00434     include_once($CFG->libdir.'/filelib.php');
00435     $mediafiletypes = get_mimetypes_array();
00436 
00437     // take into account attachments (e.g. from forum) - with these, we are able to know the file size
00438     if (isset($item->attachments) && is_array($item->attachments)) {
00439         foreach ($item->attachments as $attachment){
00440             $extension = strtolower(substr($attachment->url, strrpos($attachment->url, '.')+1));
00441             if (isset($mediafiletypes[$extension]['type'])) {
00442                 $type = $mediafiletypes[$extension]['type'];
00443             } else {
00444                 $type = 'document/unknown';
00445             }
00446             $returnstring .= "\n<enclosure url=\"$attachment->url\" length=\"$attachment->length\" type=\"$type\" />\n";
00447         }
00448     }
00449 
00450     return $returnstring;
00451 }
 All Data Structures Namespaces Files Functions Variables Enumerations