2009-05-26 03:23:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file contains all the common stuff to be used in RSS System
|
|
|
|
*
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function returns the icon (from theme) with the link to rss/file.php
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
|
|
|
* @global object
|
|
|
|
* @global object
|
2008-05-30 20:29:51 +00:00
|
|
|
*/
|
2005-01-20 01:35:04 +00:00
|
|
|
function rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext='') {
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2009-07-02 11:09:15 +00:00
|
|
|
global $OUTPUT;
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
static $rsspath = '';
|
|
|
|
|
2004-10-04 22:21:30 +00:00
|
|
|
//In site course, if no logged (userid), use admin->id. Bug 2048.
|
|
|
|
if ($courseid == SITEID and empty($userid)) {
|
|
|
|
$admin = get_admin();
|
|
|
|
$userid = $admin->id;
|
|
|
|
}
|
|
|
|
|
2006-03-03 08:03:45 +00:00
|
|
|
$rsspath = rss_get_url($courseid, $userid, $modulename, $id);
|
2009-12-16 21:50:45 +00:00
|
|
|
$rsspix = $OUTPUT->pix_url('i/rss');
|
2006-03-03 08:03:45 +00:00
|
|
|
|
2007-01-08 12:47:41 +00:00
|
|
|
return '<a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a>';
|
2006-03-03 08:03:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function returns the URL for the RSS XML file.
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
|
|
|
* @global object
|
2008-05-30 20:29:51 +00:00
|
|
|
*/
|
2006-03-03 08:03:45 +00:00
|
|
|
function rss_get_url($courseid, $userid, $modulename, $id) {
|
|
|
|
global $CFG;
|
2008-07-10 09:55:11 +00:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
|
|
|
return get_file_url($courseid.'/'.$userid.'/'.$modulename.'/'.$id.'/rss.xml', null, 'rssfile');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function prints the icon (from theme) with the link to rss/file.php
|
|
|
|
*/
|
2005-01-20 01:35:04 +00:00
|
|
|
function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext='') {
|
2005-01-27 22:45:50 +00:00
|
|
|
print rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext);
|
2004-08-07 16:54:25 +00:00
|
|
|
|
|
|
|
}
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function iterates over each module in the server to see if
|
|
|
|
* it supports generating rss feeds, searching for a MODULENAME_rss_feeds()
|
|
|
|
* function and invoking it foreach activity as necessary
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
|
|
|
* @global object
|
|
|
|
* @global object
|
2008-05-30 20:29:51 +00:00
|
|
|
*/
|
|
|
|
function cron_rss_feeds () {
|
|
|
|
global $CFG, $DB;
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
$status = true;
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace(' Generating rssfeeds...');
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2004-05-20 15:19:19 +00:00
|
|
|
//Check for required functions...
|
|
|
|
if(!function_exists('utf8_encode')) {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace(' ERROR: You need to add XML support to your PHP installation!');
|
2004-05-20 15:19:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
if ($allmods = $DB->get_records('modules') ) {
|
2004-05-02 23:08:19 +00:00
|
|
|
foreach ($allmods as $mod) {
|
2004-08-05 17:25:15 +00:00
|
|
|
mtrace(' '.$mod->name.': ', '');
|
2004-05-02 23:08:19 +00:00
|
|
|
$modname = $mod->name;
|
|
|
|
$modfile = "$CFG->dirroot/mod/$modname/rsslib.php";
|
|
|
|
//If file exists and we have selected to restore that type of module
|
|
|
|
if (file_exists($modfile)) {
|
|
|
|
include_once($modfile);
|
|
|
|
$generaterssfeeds = $modname.'_rss_feeds';
|
|
|
|
if (function_exists($generaterssfeeds)) {
|
|
|
|
if ($status) {
|
2004-08-05 17:25:15 +00:00
|
|
|
mtrace('generating ', '');;
|
2004-05-02 23:08:19 +00:00
|
|
|
$status = $generaterssfeeds();
|
|
|
|
if (!empty($status)) {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...OK');
|
2004-05-02 23:08:19 +00:00
|
|
|
} else {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...FAILED');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...SKIPPED (failed above)');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...NOT SUPPORTED (function)');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...NOT SUPPORTED (file)');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace(' Ending rssfeeds...', '');
|
2004-05-02 23:08:19 +00:00
|
|
|
if (!empty($status)) {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...OK');
|
2004-05-02 23:08:19 +00:00
|
|
|
} else {
|
2005-01-20 01:35:04 +00:00
|
|
|
mtrace('...FAILED');
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function saves to file the rss feed specified in the parameters
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
|
|
|
* @global object
|
2008-05-30 20:29:51 +00:00
|
|
|
*/
|
|
|
|
function rss_save_file($modname, $mod, $result) {
|
2004-05-02 23:08:19 +00:00
|
|
|
global $CFG;
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2004-05-02 23:08:19 +00:00
|
|
|
$status = true;
|
|
|
|
|
2005-01-20 01:35:04 +00:00
|
|
|
if (! $basedir = make_upload_directory ('rss/'. $modname)) {
|
2004-05-26 16:12:58 +00:00
|
|
|
//Cannot be created, so error
|
|
|
|
$status = false;
|
|
|
|
}
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
if ($status) {
|
2004-08-05 18:06:55 +00:00
|
|
|
$file = rss_file_name($modname, $mod);
|
2005-01-20 01:35:04 +00:00
|
|
|
$rss_file = fopen($file, "w");
|
2004-05-02 23:08:19 +00:00
|
|
|
if ($rss_file) {
|
2005-01-20 01:35:04 +00:00
|
|
|
$status = fwrite ($rss_file, $result);
|
2004-05-02 23:08:19 +00:00
|
|
|
fclose($rss_file);
|
2004-05-26 16:12:58 +00:00
|
|
|
} else {
|
|
|
|
$status = false;
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2004-08-05 18:06:55 +00:00
|
|
|
|
|
|
|
function rss_file_name($modname, $mod) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return "$CFG->dataroot/rss/$modname/$mod->id.xml";
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function return all the common headers for every rss feed in the site
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
|
|
|
* @global object
|
|
|
|
* @global object
|
2008-05-30 20:29:51 +00:00
|
|
|
*/
|
2004-05-02 23:08:19 +00:00
|
|
|
function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
|
2009-07-31 21:19:20 +00:00
|
|
|
global $CFG, $USER, $OUTPUT;
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
$status = true;
|
|
|
|
$result = "";
|
|
|
|
|
2009-11-01 09:21:41 +00:00
|
|
|
$site = get_site();
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
if ($status) {
|
|
|
|
|
|
|
|
//Calculate title, link and description
|
|
|
|
if (empty($title)) {
|
2007-03-19 04:26:45 +00:00
|
|
|
$title = format_string($site->fullname);
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
if (empty($link)) {
|
|
|
|
$link = $CFG->wwwroot;
|
|
|
|
}
|
|
|
|
if (empty($description)) {
|
|
|
|
$description = $site->summary;
|
|
|
|
}
|
|
|
|
|
|
|
|
//xml headers
|
|
|
|
$result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
|
$result .= "<rss version=\"2.0\">\n";
|
|
|
|
|
|
|
|
//open the channel
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= rss_start_tag('channel', 1, true);
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
//write channel info
|
2006-09-05 03:15:09 +00:00
|
|
|
$result .= rss_full_tag('title', 2, false, strip_tags($title));
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= rss_full_tag('link', 2, false, $link);
|
|
|
|
$result .= rss_full_tag('description', 2, false, $description);
|
2005-12-02 07:07:44 +00:00
|
|
|
$result .= rss_full_tag('generator', 2, false, 'Moodle');
|
2004-12-01 19:51:45 +00:00
|
|
|
if (!empty($USER->lang)) {
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= rss_full_tag('language', 2, false, substr($USER->lang,0,2));
|
2004-12-01 19:51:45 +00:00
|
|
|
}
|
2004-05-02 23:08:19 +00:00
|
|
|
$today = getdate();
|
2007-03-19 04:26:45 +00:00
|
|
|
$result .= rss_full_tag('copyright', 2, false, '© '. $today['year'] .' '. format_string($site->fullname));
|
2006-03-17 07:44:22 +00:00
|
|
|
/*
|
2006-03-21 20:54:50 +00:00
|
|
|
if (!empty($USER->email)) {
|
2006-02-27 05:11:02 +00:00
|
|
|
$result .= rss_full_tag('managingEditor', 2, false, fullname($USER));
|
|
|
|
$result .= rss_full_tag('webMaster', 2, false, fullname($USER));
|
2004-12-01 19:51:45 +00:00
|
|
|
}
|
2006-03-21 20:54:50 +00:00
|
|
|
*/
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
//write image info
|
2009-12-16 21:50:45 +00:00
|
|
|
$rsspix = $OUTPUT->pix_url('i/rsssitelogo');
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
//write the info
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= rss_start_tag('image', 2, true);
|
|
|
|
$result .= rss_full_tag('url', 3, false, $rsspix);
|
|
|
|
$result .= rss_full_tag('title', 3, false, 'moodle');
|
|
|
|
$result .= rss_full_tag('link', 3, false, $CFG->wwwroot);
|
|
|
|
$result .= rss_full_tag('width', 3, false, '140');
|
|
|
|
$result .= rss_full_tag('height', 3, false, '35');
|
|
|
|
$result .= rss_end_tag('image', 2, true);
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$status) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//This function returns the rss XML code for every item passed in the array
|
|
|
|
//item->title: The title of the item
|
2004-05-09 18:34:33 +00:00
|
|
|
//item->author: The author of the item. Optional !!
|
2004-05-02 23:08:19 +00:00
|
|
|
//item->pubdate: The pubdate of the item
|
|
|
|
//item->link: The link url of the item
|
|
|
|
//item->description: The content of the item
|
|
|
|
function rss_add_items($items) {
|
|
|
|
global $CFG;
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2005-05-02 15:54:24 +00:00
|
|
|
$result = '';
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
if (!empty($items)) {
|
|
|
|
foreach ($items as $item) {
|
2005-05-02 15:54:24 +00:00
|
|
|
$result .= rss_start_tag('item',2,true);
|
2005-07-24 23:04:58 +00:00
|
|
|
//Include the category if exists (some rss readers will use it to group items)
|
|
|
|
if (isset($item->category)) {
|
|
|
|
$result .= rss_full_tag('category',3,false,$item->category);
|
|
|
|
}
|
2009-05-22 15:16:58 +00:00
|
|
|
if (isset($item->tags)) {
|
|
|
|
$attributes = array();
|
|
|
|
if (isset($item->tagscheme)) {
|
2010-01-15 14:36:11 +00:00
|
|
|
$attributes['domain'] = s($item->tagscheme);
|
2009-05-22 15:16:58 +00:00
|
|
|
}
|
|
|
|
foreach ($item->tags as $tag) {
|
|
|
|
$result .= rss_full_tag('category', 3, false, $tag, $attributes);
|
|
|
|
}
|
|
|
|
}
|
2006-09-05 03:15:09 +00:00
|
|
|
$result .= rss_full_tag('title',3,false,strip_tags($item->title));
|
2005-05-02 15:54:24 +00:00
|
|
|
$result .= rss_full_tag('link',3,false,$item->link);
|
2006-03-21 22:35:23 +00:00
|
|
|
$result .= rss_add_enclosures($item);
|
2008-09-16 08:57:34 +00:00
|
|
|
$result .= rss_full_tag('pubDate',3,false,gmdate('D, d M Y H:i:s',$item->pubdate).' GMT'); # MDL-12563
|
2009-11-01 16:48:45 +00:00
|
|
|
//Include the author if exists
|
2004-05-09 18:34:33 +00:00
|
|
|
if (isset($item->author)) {
|
2005-05-02 16:28:14 +00:00
|
|
|
//$result .= rss_full_tag('author',3,false,$item->author);
|
2008-05-30 20:29:51 +00:00
|
|
|
//We put it in the description instead because it's more important
|
2005-05-02 16:28:14 +00:00
|
|
|
//for moodle than most other feeds, and most rss software seems to ignore
|
|
|
|
//the author field ...
|
2006-03-28 18:46:07 +00:00
|
|
|
$item->description = get_string('byname','',$item->author).'. <p>'.$item->description.'</p>';
|
2004-05-09 18:34:33 +00:00
|
|
|
}
|
2005-05-02 15:54:24 +00:00
|
|
|
$result .= rss_full_tag('description',3,false,$item->description);
|
2006-03-28 18:46:07 +00:00
|
|
|
$result .= rss_full_tag('guid',3,false,$item->link,array('isPermaLink' => 'true'));
|
2005-05-02 15:54:24 +00:00
|
|
|
$result .= rss_end_tag('item',2,true);
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function return all the common footers for every rss feed in the site
|
|
|
|
*/
|
2004-05-02 23:08:19 +00:00
|
|
|
function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
|
|
|
|
$status = true;
|
2005-05-02 15:54:24 +00:00
|
|
|
$result = '';
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
//Close the chanel
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= rss_end_tag('channel', 1, true);
|
2004-05-02 23:08:19 +00:00
|
|
|
////Close the rss tag
|
2005-01-27 22:45:50 +00:00
|
|
|
$result .= '</rss>';
|
2004-05-02 23:08:19 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* This function return an error xml file (string)
|
|
|
|
* to be sent when a rss is required (file.php)
|
|
|
|
* and something goes wrong
|
|
|
|
*/
|
2004-12-01 19:51:45 +00:00
|
|
|
function rss_geterrorxmlfile() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$return = '';
|
|
|
|
|
|
|
|
//XML Header
|
|
|
|
$return = rss_standard_header();
|
|
|
|
|
2007-07-30 19:13:19 +00:00
|
|
|
//XML item
|
2004-12-01 19:51:45 +00:00
|
|
|
if ($return) {
|
2007-12-31 15:37:12 +00:00
|
|
|
$item = new object();
|
2008-05-30 20:29:51 +00:00
|
|
|
$item->title = "RSS Error";
|
|
|
|
$item->link = $CFG->wwwroot;
|
|
|
|
$item->pubdate = time();
|
2004-12-01 19:51:45 +00:00
|
|
|
$item->description = get_string("rsserror");
|
|
|
|
$return .= rss_add_items(array($item));
|
|
|
|
}
|
|
|
|
|
|
|
|
//XML Footer
|
|
|
|
if ($return) {
|
|
|
|
$return .= rss_standard_footer();
|
|
|
|
}
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2004-12-01 19:51:45 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2004-05-02 23:08:19 +00:00
|
|
|
// ===== This function are used to write XML tags =========
|
|
|
|
// [stronk7]: They are similar to the glossary export and backup generation
|
|
|
|
// but I've replicated them here because they have some minor
|
|
|
|
// diferences. Someday all they should go to a common place.
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* Return the xml start tag
|
|
|
|
*/
|
2006-03-28 18:46:07 +00:00
|
|
|
function rss_start_tag($tag,$level=0,$endline=false,$attributes=null) {
|
2004-05-02 23:08:19 +00:00
|
|
|
if ($endline) {
|
|
|
|
$endchar = "\n";
|
|
|
|
} else {
|
|
|
|
$endchar = "";
|
|
|
|
}
|
2006-03-28 18:46:07 +00:00
|
|
|
$attrstring = '';
|
|
|
|
if (!empty($attributes) && is_array($attributes)) {
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
$attrstring .= " ".$key."=\"".$value."\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str_repeat(" ",$level*2)."<".$tag.$attrstring.">".$endchar;
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* Return the xml end tag
|
|
|
|
*/
|
2004-05-02 23:08:19 +00:00
|
|
|
function rss_end_tag($tag,$level=0,$endline=true) {
|
|
|
|
if ($endline) {
|
|
|
|
$endchar = "\n";
|
|
|
|
} else {
|
|
|
|
$endchar = "";
|
|
|
|
}
|
|
|
|
return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:29:51 +00:00
|
|
|
/**
|
|
|
|
* Return the start tag, the contents and the end tag
|
|
|
|
*/
|
2006-03-28 18:46:07 +00:00
|
|
|
function rss_full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
|
|
|
|
$st = rss_start_tag($tag,$level,$endline,$attributes);
|
2004-05-02 23:08:19 +00:00
|
|
|
$co="";
|
2006-11-11 17:23:20 +00:00
|
|
|
$co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
|
2004-05-02 23:08:19 +00:00
|
|
|
$et = rss_end_tag($tag,0,true);
|
2006-03-28 18:46:07 +00:00
|
|
|
|
2004-05-02 23:08:19 +00:00
|
|
|
return $st.$co.$et;
|
|
|
|
}
|
|
|
|
|
2005-12-02 07:07:44 +00:00
|
|
|
/**
|
2009-05-26 03:23:32 +00:00
|
|
|
* Adds RSS Media Enclosures for "podcasting" by examining links to media files,
|
|
|
|
* and attachments which are media files. Please note that the RSS that is
|
|
|
|
* produced cannot be strictly valid for the linked files, since we do not know
|
|
|
|
* the files' sizes and cannot include them in the "length" attribute. At
|
|
|
|
* present, the validity (and therefore the podcast working in most software)
|
|
|
|
* can only be ensured for attachments, and not for links.
|
|
|
|
* Note also that iTunes does some things very badly - one thing it does is
|
|
|
|
* refuse to download ANY of your files if you're using "file.php?file=blah"
|
|
|
|
* and can't use the more elegant "file.php/blah" slasharguments setting. It
|
|
|
|
* stops after ".php" and assumes the files are not media files, despite what
|
|
|
|
* is specified in the "type" attribute. Dodgy coding all round!
|
|
|
|
*
|
|
|
|
* Authors
|
|
|
|
* - Hannes Gassert <hannes@mediagonal.ch>
|
|
|
|
* - Dan Stowell
|
|
|
|
*
|
|
|
|
* @global object
|
|
|
|
* @param $item object representing an RSS item
|
|
|
|
* @return string RSS enclosure tags
|
|
|
|
*/
|
2005-12-02 07:07:44 +00:00
|
|
|
function rss_add_enclosures($item){
|
2007-03-05 16:43:40 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2005-12-02 07:07:44 +00:00
|
|
|
$returnstring = '';
|
|
|
|
$rss_text = $item->description;
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2005-12-02 07:07:44 +00:00
|
|
|
// list of media file extensions and their respective mime types
|
2007-03-05 16:43:40 +00:00
|
|
|
include_once($CFG->libdir.'/filelib.php');
|
|
|
|
$mediafiletypes = get_mimetypes_array();
|
2005-12-02 07:07:44 +00:00
|
|
|
|
|
|
|
// regular expression (hopefully) matching all links to media files
|
|
|
|
$medialinkpattern = '@href\s*=\s*(\'|")(\S+(' . implode('|', array_keys($mediafiletypes)) . '))\1@Usie';
|
|
|
|
|
2006-03-17 07:44:22 +00:00
|
|
|
// take into account attachments (e.g. from forum) - with these, we are able to know the file size
|
|
|
|
if (isset($item->attachments) && is_array($item->attachments)) {
|
|
|
|
foreach ($item->attachments as $attachment){
|
2007-03-08 08:13:11 +00:00
|
|
|
$extension = strtolower(substr($attachment->url, strrpos($attachment->url, '.')+1));
|
|
|
|
if (isset($mediafiletypes[$extension]['type'])) {
|
|
|
|
$type = $mediafiletypes[$extension]['type'];
|
|
|
|
} else {
|
|
|
|
$type = 'document/unknown';
|
|
|
|
}
|
2006-03-21 22:35:23 +00:00
|
|
|
$returnstring .= "\n<enclosure url=\"$attachment->url\" length=\"$attachment->length\" type=\"$type\" />\n";
|
2006-03-17 07:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2005-12-02 07:07:44 +00:00
|
|
|
if (!preg_match_all($medialinkpattern, $rss_text, $matches)){
|
|
|
|
return $returnstring;
|
|
|
|
}
|
|
|
|
|
2007-07-30 19:13:19 +00:00
|
|
|
// loop over matches of regular expression
|
2005-12-02 07:07:44 +00:00
|
|
|
for ($i = 0; $i < count($matches[2]); $i++){
|
|
|
|
$url = htmlspecialchars($matches[2][$i]);
|
2007-03-08 08:13:11 +00:00
|
|
|
$extension = strtolower($matches[3][$i]);
|
|
|
|
if (isset($mediafiletypes[$extension]['type'])) {
|
|
|
|
$type = $mediafiletypes[$extension]['type'];
|
|
|
|
} else {
|
|
|
|
$type = 'document/unknown';
|
|
|
|
}
|
2005-12-02 07:07:44 +00:00
|
|
|
|
|
|
|
// the rss_*_tag functions can't deal with methods, unfortunately
|
|
|
|
$returnstring .= "\n<enclosure url='$url' type='$type' />\n";
|
|
|
|
}
|
2007-07-30 19:13:19 +00:00
|
|
|
|
2005-12-02 07:07:44 +00:00
|
|
|
return $returnstring;
|
|
|
|
}
|