2004-12-14 18:09:20 +00:00
< ? php //$Id$
2005-06-28 21:21:59 +00:00
/*******************************************************************
2005-08-11 15:27:47 +00:00
* This file contains no classes . It will display a list of existing feeds
* defined for the site and allow add / edit / delete of site feeds .
2005-06-28 21:21:59 +00:00
*
* @ author Daryl Hawes
* @ version $Id $
* @ license http :// www . gnu . org / copyleft / gpl . html GNU Public License
* @ package base
******************************************************************/
2005-08-11 15:27:47 +00:00
require_once ( '../../config.php' );
require_once ( $CFG -> libdir . '/rsslib.php' );
require_once ( MAGPIE_DIR . 'rss_fetch.inc' );
require_login ();
global $USER ;
//ensure that the logged in user is not using the guest account
if ( isset ( $_SERVER [ 'HTTP_REFERER' ])) {
$referrer = $_SERVER [ 'HTTP_REFERER' ];
} else {
$referrer = $CFG -> wwwroot ;
}
if ( isguest ()) {
error ( get_string ( 'noguestpost' , 'forum' ), $referrer );
}
2006-05-08 07:09:28 +00:00
$url = optional_param ( 'url' , '' , PARAM_URL );
2005-12-14 00:51:34 +00:00
if ( ! empty ( $url )) {
// attempting to replace feed and rss url types with http
// it appears that the rss feed validator will validate these url types but magpie will not load them $url = str_replace ("feed://", "http://", "$url");
// Shifting this forward since PARAM_URL rejects these feed types as invalid entries!
$url = str_replace ( " feed:// " , " http:// " , " $url " );
$url = str_replace ( " FEED:// " , " http:// " , " $url " );
$url = str_replace ( " rss:// " , " http:// " , " $url " );
$url = str_replace ( " RSS:// " , " http:// " , " $url " );
}
2005-10-02 14:30:05 +00:00
$act = optional_param ( 'act' , NULL , PARAM_ALPHA );
$rssid = optional_param ( 'rssid' , NULL , PARAM_INT );
2005-08-11 15:27:47 +00:00
$id = optional_param ( 'id' , SITEID , PARAM_INT );
2006-05-08 07:09:28 +00:00
//$url = clean_param($url, PARAM_URL);
2005-10-02 14:30:05 +00:00
$preferredtitle = optional_param ( 'preferredtitle' , '' , PARAM_ALPHA );
2005-08-11 15:27:47 +00:00
if ( ! defined ( 'MAGPIE_OUTPUT_ENCODING' )) {
2006-01-05 13:13:06 +00:00
define ( 'MAGPIE_OUTPUT_ENCODING' , current_charset ()); // see bug 3107
2005-08-11 15:27:47 +00:00
}
if ( ! empty ( $id )) {
// we get the complete $course object here because print_header assumes this is
// a complete object (needed for proper course theme settings)
$course = get_record ( 'course' , 'id' , $id );
}
$straddedit = get_string ( 'feedsaddedit' , 'block_rss_client' );
if ( isadmin () ) {
2005-10-02 15:46:06 +00:00
$navigation = '<a href="' . $CFG -> wwwroot . '/' . $CFG -> admin . '/index.php">' . get_string ( 'administration' ) . '</a> -> ' .
'<a href="' . $CFG -> wwwroot . '/' . $CFG -> admin . '/configure.php">' . get_string ( 'configuration' ) . '</a> -> ' . $straddedit ;
2005-08-11 15:27:47 +00:00
} else if ( ! empty ( $course )) {
2005-10-02 15:46:06 +00:00
$navigation = '<a href="' . $CFG -> wwwroot . '/course/view.php?id=' . $id . '">' . $course -> shortname . '</a> -> ' . $straddedit ;
2005-08-11 15:27:47 +00:00
} else {
$navigation = $straddedit ;
}
2005-10-02 15:46:06 +00:00
print_header ( $straddedit , $straddedit , $navigation );
2005-08-11 15:27:47 +00:00
//check to make sure that the user is allowed to post new feeds
$submitters = $CFG -> block_rss_client_submitters ;
2005-10-02 15:46:06 +00:00
$isteacher = empty ( $course ) ? false : isteacher ( $id );
2005-08-11 15:27:47 +00:00
2005-10-02 17:45:20 +00:00
if ( ! isset ( $act ) ) {
2005-10-02 14:30:05 +00:00
rss_display_feeds ( $id );
rss_print_form ( $act , $url , $rssid , $preferredtitle , $id );
print_footer ();
die ();
}
2005-10-02 17:45:20 +00:00
if ( isset ( $rssid ) ) {
2005-10-02 14:30:05 +00:00
$rss_record = get_record ( 'block_rss_client' , 'id' , $rssid );
}
2005-08-11 15:27:47 +00:00
//if the user is an admin or course teacher then allow the user to
//assign categories to other uses than personal
2005-10-02 15:46:06 +00:00
if ( isset ( $rss_record ) && ! ( isadmin () || $submitters == SUBMITTERS_ALL_ACCOUNT_HOLDERS ||
2005-08-11 15:27:47 +00:00
( $submitters == SUBMITTERS_ADMIN_AND_TEACHER && $isteacher ) ||
2005-11-10 23:34:28 +00:00
( ( $act == 'rssedit' || $act == 'delfeed' || $act == 'updfeed' ) && $USER -> id == $rss_record -> userid ) ) ) {
2005-08-11 15:27:47 +00:00
error ( get_string ( 'noguestpost' , 'forum' ) . ' You are not allowed to make modifications to this RSS feed at this time.' , $referrer );
}
2005-08-01 14:02:46 +00:00
2005-10-02 14:30:05 +00:00
if ( $act == 'updfeed' ) {
2005-08-11 15:27:47 +00:00
if ( empty ( $url )) {
error ( 'url not defined for rss feed' );
2005-05-04 14:28:46 +00:00
}
2005-06-13 02:38:48 +00:00
2005-08-11 15:27:47 +00:00
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
$message = '' ;
ob_start ();
$rss = fetch_rss ( $url );
if ( $CFG -> debug ) {
$message .= ob_get_contents ();
2005-06-13 02:38:48 +00:00
}
2005-08-11 15:27:47 +00:00
ob_end_clean ();
2005-06-13 02:38:48 +00:00
2005-08-11 15:27:47 +00:00
$dataobject -> id = $rssid ;
if ( $rss === false ) {
$dataobject -> description = '' ;
$dataobject -> title = '' ;
$dataobject -> preferredtitle = '' ;
2005-01-24 02:58:19 +00:00
} else {
2006-01-05 13:13:06 +00:00
$dataobject -> description = addslashes ( $rss -> channel [ 'description' ]);
$dataobject -> title = addslashes ( $rss -> channel [ 'title' ]);
2005-08-11 15:27:47 +00:00
$dataobject -> preferredtitle = addslashes ( $preferredtitle );
2005-01-24 02:58:19 +00:00
}
2005-08-11 15:27:47 +00:00
$dataobject -> url = addslashes ( $url );
2005-08-11 12:45:38 +00:00
2005-08-11 15:27:47 +00:00
if ( ! update_record ( 'block_rss_client' , $dataobject )) {
error ( 'There was an error trying to update rss feed with id:' . $rssid );
2004-12-14 18:09:20 +00:00
}
2005-06-13 02:38:48 +00:00
2005-08-11 15:27:47 +00:00
$message .= '<br />' . get_string ( 'feedupdated' , 'block_rss_client' );
redirect ( $referrer , $message );
2005-08-01 14:02:46 +00:00
2005-08-11 15:27:47 +00:00
} else if ( $act == 'addfeed' ) {
2004-12-14 18:09:20 +00:00
2005-08-11 15:27:47 +00:00
if ( empty ( $url )) {
error ( 'url not defined for rss feed' );
}
$dataobject -> userid = $USER -> id ;
$dataobject -> description = '' ;
$dataobject -> title = '' ;
$dataobject -> url = addslashes ( $url );
$dataobject -> preferredtitle = addslashes ( $preferredtitle );
$rssid = insert_record ( 'block_rss_client' , $dataobject );
if ( ! $rssid ) {
error ( 'There was an error trying to add a new rss feed:' . $url );
}
2004-12-14 18:09:20 +00:00
2005-08-11 15:27:47 +00:00
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
$message = '' ;
ob_start ();
$rss = fetch_rss ( $url );
if ( $CFG -> debug ) {
$message .= ob_get_contents ();
}
ob_end_clean ();
2005-08-11 12:45:38 +00:00
2005-08-11 15:27:47 +00:00
if ( $rss === false ) {
$message .= '<br /><br />There was an error loading this rss feed. You may want to verify the url you have specified before using it.' ; //Daryl Hawes note: localize this line
} else {
2005-08-11 12:45:38 +00:00
2004-12-14 18:09:20 +00:00
$dataobject -> id = $rssid ;
2005-08-11 15:27:47 +00:00
if ( ! empty ( $rss -> channel [ 'description' ])) {
2006-01-05 13:13:06 +00:00
$dataobject -> description = addslashes ( $rss -> channel [ 'description' ]);
2005-01-23 19:40:33 +00:00
}
2005-08-11 15:27:47 +00:00
if ( ! empty ( $rss -> channel [ 'title' ])) {
2006-01-05 13:13:06 +00:00
$dataobject -> title = addslashes ( $rss -> channel [ 'title' ]);
2005-08-11 15:27:47 +00:00
}
2004-12-14 18:09:20 +00:00
if ( ! update_record ( 'block_rss_client' , $dataobject )) {
error ( 'There was an error trying to update rss feed with id:' . $rssid );
}
2005-08-11 15:27:47 +00:00
$message .= '<br />' . get_string ( 'feedadded' , 'block_rss_client' );
}
redirect ( $referrer , $message );
/*
rss_display_feeds ( $id );
rss_print_form ( $act , $dataobject -> url , $dataobject -> id , $dataobject -> preferredtitle , $id );
*/
2005-11-10 23:34:28 +00:00
} else if ( isset ( $rss_record ) && $act == 'rssedit' ) {
2005-01-27 01:43:29 +00:00
2005-08-11 15:27:47 +00:00
$preferredtitle = stripslashes_safe ( $rss_record -> preferredtitle );
if ( empty ( $preferredtitle )) {
$preferredtitle = stripslashes_safe ( $rss_record -> title );
}
$url = stripslashes_safe ( $rss_record -> url );
rss_display_feeds ( $id , '' , $rssid );
rss_print_form ( $act , $url , $rssid , $preferredtitle , $id );
2005-06-28 21:21:59 +00:00
2005-08-11 15:27:47 +00:00
} else if ( $act == 'delfeed' ) {
2005-01-27 01:43:29 +00:00
2005-08-11 15:27:47 +00:00
$file = $CFG -> dataroot . '/cache/rsscache/' . $rssid . '.xml' ;
if ( file_exists ( $file )) {
unlink ( $file );
}
2005-01-27 01:43:29 +00:00
2005-08-11 15:27:47 +00:00
// echo "DEBUG: act = delfeed"; //debug
2005-10-02 15:46:06 +00:00
delete_records ( 'block_rss_client' , 'id' , $rssid );
2005-08-11 15:27:47 +00:00
redirect ( $referrer , get_string ( 'feeddeleted' , 'block_rss_client' ) );
2005-01-27 01:43:29 +00:00
2005-10-02 17:45:20 +00:00
} else if ( isset ( $rss_record ) && $act == 'view' ) {
2005-08-11 15:27:47 +00:00
// echo $sql; //debug
// print_object($res); //debug
if ( ! $rss_record -> id ) {
print '<strong>' . get_string ( 'couldnotfindfeed' , 'block_rss_client' ) . ': ' . $rssid . '</strong>' ;
} else {
2005-01-23 19:40:33 +00:00
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
ob_start ();
2005-08-11 15:27:47 +00:00
$rss = fetch_rss ( $rss_record -> url );
2005-01-23 19:40:33 +00:00
ob_end_clean ();
2005-08-11 12:45:38 +00:00
2005-08-11 15:27:47 +00:00
if ( empty ( $rss_record -> preferredtitle )) {
2006-01-05 13:13:06 +00:00
$feedtitle = $rss_record -> preferredtitle ;
2005-01-23 19:40:33 +00:00
} else {
2006-01-05 13:13:06 +00:00
$feedtitle = $rss -> channel [ 'title' ];
2004-12-14 18:09:20 +00:00
}
2005-08-11 15:27:47 +00:00
print '<table align="center" width="50%" cellspacing="1">' . " \n " ;
print '<tr><td colspan="2"><strong>' . $feedtitle . '</strong></td></tr>' . " \n " ;
for ( $y = 0 ; $y < count ( $rss -> items ); $y ++ ) {
if ( $rss -> items [ $y ][ 'link' ] == '' ) {
$rss -> items [ $y ][ 'link' ] = $rss -> items [ $y ][ 'guid' ];
}
2004-12-14 18:09:20 +00:00
2005-08-11 15:27:47 +00:00
if ( $rss -> items [ $y ][ 'title' ] == '' ) {
$rss -> items [ $y ][ 'title' ] = '>>' ;
}
2004-12-14 18:09:20 +00:00
2005-08-11 15:27:47 +00:00
print '<tr><td valign="middle">' . " \n " ;
print '<a href="' . $rss -> items [ $y ][ 'link' ] . '" target="_blank"><strong>' . $rss -> items [ $y ][ 'title' ];
print '</strong></a>' . " \n " ;
print '</td>' . " \n " ;
if ( file_exists ( $CFG -> dirroot . '/blog/lib.php' )) {
//Blog module is installed - provide "blog this" link
print '<td align="right">' . " \n " ;
print '<img src="' . $CFG -> pixpath . '/blog/blog.gif" alt="' . get_string ( 'blogthis' , 'blog' ) . '" title="' . get_string ( 'blogthis' , 'blog' ) . '" border="0" align="middle" />' . " \n " ;
print '<a href="' . $CFG -> wwwroot . '/blog/blogthis.php?userid=' . $userid . '&act=use&item=' . $y . '&rssid=' . $rssid . '"><small><strong>' . get_string ( 'blogthis' , 'blog' ) . '</strong></small></a>' . " \n " ;
2005-01-27 22:47:13 +00:00
} else {
2005-08-11 15:27:47 +00:00
print '<td> ' ;
2005-01-27 22:47:13 +00:00
}
2005-08-11 15:27:47 +00:00
print '</td></tr>' . " \n " ;
print '<tr><td colspan=2><small>' ;
print $rss -> items [ $y ][ 'description' ] . '</small></td></tr>' . " \n " ;
2004-12-14 18:09:20 +00:00
}
2005-08-11 15:27:47 +00:00
print '</table>' . " \n " ;
2004-12-14 18:09:20 +00:00
}
2005-08-11 15:27:47 +00:00
} else {
rss_display_feeds ( $id );
rss_print_form ( $act , $url , $rssid , $preferredtitle , $id );
}
print_footer ();
2006-01-05 13:13:06 +00:00
?>