PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/rss_client/block_rss_client_action.php

https://github.com/nadavkav/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 303 lines | 211 code | 53 blank | 39 comment | 72 complexity | 7bb9f54a0cd2f30bf44d1ce6d27d0b3e MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
  1. <?php //$Id: block_rss_client_action.php,v 1.54.2.6 2008/06/08 18:29:51 dongsheng Exp $
  2. /*******************************************************************
  3. * This file contains no classes. It will display a list of existing feeds
  4. * defined for the site and allow add/edit/delete of site feeds.
  5. *
  6. * @author Daryl Hawes
  7. * @version $Id: block_rss_client_action.php,v 1.54.2.6 2008/06/08 18:29:51 dongsheng Exp $
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  9. * @package base
  10. ******************************************************************/
  11. require_once('../../config.php');
  12. require_once($CFG->libdir .'/rsslib.php');
  13. require_once(MAGPIE_DIR .'rss_fetch.inc');
  14. require_login();
  15. global $USER;
  16. if (isset($_SERVER['HTTP_REFERER'])) {
  17. $referrer = $_SERVER['HTTP_REFERER'];
  18. } else {
  19. $referrer = $CFG->wwwroot.'/';
  20. }
  21. // Ensure that the logged in user is not using the guest account
  22. if (isguest()) {
  23. print_error('noguestpost', 'forum', $referrer);
  24. }
  25. $url = optional_param('url','',PARAM_URL);
  26. if (!empty($url)) {
  27. // attempting to replace feed and rss url types with http
  28. // it appears that the rss feed validator will validate these url types but magpie will not load them $url = str_replace ("feed://", "http://", "$url");
  29. // Shifting this forward since PARAM_URL rejects these feed types as invalid entries!
  30. $url = str_replace ("feed://", "http://", "$url");
  31. $url = str_replace ("FEED://", "http://", "$url");
  32. $url = str_replace ("rss://", "http://", "$url");
  33. $url = str_replace ("RSS://", "http://", "$url");
  34. }
  35. $act = optional_param('act', NULL, PARAM_ALPHA);
  36. $rssid = optional_param('rssid', NULL, PARAM_INT);
  37. $id = optional_param('id', SITEID, PARAM_INT);
  38. //$url = clean_param($url, PARAM_URL);
  39. $preferredtitle = optional_param('preferredtitle', '', PARAM_TEXT);
  40. $shared = optional_param('shared', 0, PARAM_INT);
  41. if (!defined('MAGPIE_OUTPUT_ENCODING')) {
  42. define('MAGPIE_OUTPUT_ENCODING', 'utf-8'); // see bug 3107
  43. }
  44. if (!empty($id)) {
  45. // we get the complete $course object here because print_header assumes this is
  46. // a complete object (needed for proper course theme settings)
  47. if ($course = get_record('course', 'id', $id)) {
  48. $context = get_context_instance(CONTEXT_COURSE, $id);
  49. }
  50. } else {
  51. $context = get_context_instance(CONTEXT_SYSTEM);
  52. }
  53. $straddedit = get_string('feedsaddedit', 'block_rss_client');
  54. $link = $CFG->wwwroot.'/course/view.php?id='.$id;
  55. if (empty($course)) {
  56. $link = '';
  57. }
  58. $navlinks = array();
  59. $navlinks = array(array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'));
  60. $navlinks[] = array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc');
  61. $navlinks[] = array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc');
  62. $navlinks[] = array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc');
  63. $navlinks[] = array('name' => get_string('addnew', 'block_rss_client'), 'link' => null, 'type' => 'misc');
  64. $navigation = build_navigation($navlinks);
  65. print_header($straddedit, $straddedit, $navigation);
  66. if ( !isset($act) ) {
  67. rss_display_feeds($id, $USER->id, '', $context);
  68. rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
  69. print_footer();
  70. die();
  71. }
  72. if ( isset($rssid) ) {
  73. $rss_record = get_record('block_rss_client', 'id', $rssid);
  74. }
  75. if (isset($rss_record)) {
  76. $managefeeds = ($rss_record->userid == $USER->id && has_capability('block/rss_client:manageownfeeds', $context))
  77. || ($rss_record->userid != $USER->id && has_capability('block/rss_client:manageanyfeeds', $context));
  78. }
  79. if ($act == 'updfeed') {
  80. if (!$managefeeds) {
  81. error(get_string('noguestpost', 'forum').
  82. ' You are not allowed to make modifications to this RSS feed at this time.',
  83. $referrer);
  84. //print_error('noguestpost', 'forum', $referrer, 'You are not allowed to make modifications to this RSS feed at this time.');
  85. }
  86. if (empty($url)) {
  87. error( 'URL not defined for rss feed' );
  88. }
  89. // By capturing the output from fetch_rss this way
  90. // error messages do not display and clutter up the moodle interface
  91. // however, we do lose out on seeing helpful messages like "cache hit", etc.
  92. $message = '';
  93. ob_start();
  94. $rss = fetch_rss($url);
  95. if (debugging()) {
  96. $message .= ob_get_contents();
  97. }
  98. ob_end_clean();
  99. $canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
  100. $dataobject->id = $rssid;
  101. if ($rss === false) {
  102. $dataobject->description = '';
  103. $dataobject->title = '';
  104. $dataobject->preferredtitle = '';
  105. $dataobject->shared = 0;
  106. } else {
  107. $dataobject->description = addslashes($rss->channel['description']);
  108. $dataobject->title = addslashes($rss->channel['title']);
  109. $dataobject->preferredtitle = addslashes($preferredtitle);
  110. if ($shared == 1 && $canaddsharedfeeds) {
  111. $dataobject->shared = 1;
  112. } else {
  113. $dataobject->shared = 0;
  114. }
  115. }
  116. $dataobject->url = addslashes($url);
  117. if (!update_record('block_rss_client', $dataobject)) {
  118. error('There was an error trying to update rss feed with id:'. $rssid);
  119. }
  120. $message .= '<br />'. get_string('feedupdated', 'block_rss_client');
  121. redirect($referrer, $message);
  122. } else if ($act == 'addfeed' ) {
  123. $canaddprivfeeds = has_capability('block/rss_client:createprivatefeeds', $context);
  124. $canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
  125. if (!$canaddprivfeeds && !$canaddsharedfeeds) {
  126. error('You do not have the permission to add RSS feeds');
  127. }
  128. if (empty($url)) {
  129. error('URL not defined for rss feed');
  130. }
  131. $dataobject->userid = $USER->id;
  132. $dataobject->description = '';
  133. $dataobject->title = '';
  134. $dataobject->url = addslashes($url);
  135. $dataobject->preferredtitle = addslashes($preferredtitle);
  136. if ($shared == 1 && $canaddsharedfeeds) {
  137. $dataobject->shared = 1;
  138. } else {
  139. $dataobject->shared = 0;
  140. }
  141. $rssid = insert_record('block_rss_client', $dataobject);
  142. if (!$rssid) {
  143. error('There was an error trying to add a new rss feed:'. $url);
  144. }
  145. // By capturing the output from fetch_rss this way
  146. // error messages do not display and clutter up the moodle interface
  147. // however, we do lose out on seeing helpful messages like "cache hit", etc.
  148. $message = '';
  149. ob_start();
  150. $rss = fetch_rss($url);
  151. if (debugging()) {
  152. $message .= ob_get_contents();
  153. }
  154. ob_end_clean();
  155. if ($rss === false) {
  156. $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
  157. } else {
  158. $dataobject->id = $rssid;
  159. if (!empty($rss->channel['description'])) {
  160. $dataobject->description = addslashes($rss->channel['description']);
  161. }
  162. if (!empty($rss->channel['title'])) {
  163. $dataobject->title = addslashes($rss->channel['title']);
  164. }
  165. if (!update_record('block_rss_client', $dataobject)) {
  166. error('There was an error trying to update rss feed with id:'. $rssid);
  167. }
  168. $message .= '<br />'. get_string('feedadded', 'block_rss_client');
  169. }
  170. redirect($referrer, $message);
  171. /*
  172. rss_display_feeds($id, $USER->id, '', $context);
  173. rss_print_form($act, $dataobject->url, $dataobject->id, $dataobject->preferredtitle, $shared, $id, $context);
  174. */
  175. } else if ( isset($rss_record) && $act == 'rssedit' ) {
  176. $preferredtitle = stripslashes_safe($rss_record->preferredtitle);
  177. if (empty($preferredtitle)) {
  178. $preferredtitle = stripslashes_safe($rss_record->title);
  179. }
  180. $url = stripslashes_safe($rss_record->url);
  181. $shared = stripslashes_safe($rss_record->shared);
  182. rss_display_feeds($id, $USER->id, $rssid, $context);
  183. rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
  184. } else if ($act == 'delfeed') {
  185. if (!$managefeeds) {
  186. error(get_string('noguestpost', 'forum').
  187. ' You are not allowed to make modifications to this RSS feed at this time.',
  188. $referrer);
  189. //print_error('noguestpost', 'forum', $referrer, 'You are not allowed to make modifications to this RSS feed at this time.');
  190. }
  191. $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml';
  192. if (file_exists($file)) {
  193. unlink($file);
  194. }
  195. // echo "DEBUG: act = delfeed"; //debug
  196. delete_records('block_rss_client', 'id', $rssid);
  197. redirect($referrer, get_string('feeddeleted', 'block_rss_client') );
  198. } else if ( isset($rss_record) && $act == 'view' ) {
  199. // echo $sql; //debug
  200. // print_object($res); //debug
  201. if (!$rss_record->id) {
  202. print '<strong>'. get_string('couldnotfindfeed', 'block_rss_client') .': '. $rssid .'</strong>';
  203. } else {
  204. // By capturing the output from fetch_rss this way
  205. // error messages do not display and clutter up the moodle interface
  206. // however, we do lose out on seeing helpful messages like "cache hit", etc.
  207. ob_start();
  208. $rss = fetch_rss($rss_record->url);
  209. ob_end_clean();
  210. if (empty($rss_record->preferredtitle)) {
  211. $feedtitle = $rss_record->preferredtitle;
  212. } else {
  213. $feedtitle = $rss->channel['title'];
  214. }
  215. print '<table align="center" width="50%" cellspacing="1">'."\n";
  216. print '<tr><td colspan="2"><strong>'. $feedtitle .'</strong></td></tr>'."\n";
  217. for($y=0; $y < count($rss->items); $y++) {
  218. if ($rss->items[$y]['link'] == '') {
  219. $rss->items[$y]['link'] = $rss->items[$y]['guid'];
  220. }
  221. if ($rss->items[$y]['title'] == '') {
  222. $rss->items[$y]['title'] = '&gt;&gt;';
  223. }
  224. print '<tr><td valign="middle">'."\n";
  225. print '<a href="'. $rss->items[$y]['link'] .'" target="_blank"><strong>'. $rss->items[$y]['title'];
  226. print '</strong></a>'."\n";
  227. print '</td>'."\n";
  228. if (file_exists($CFG->dirroot .'/blog/lib.php')) {
  229. //Blog module is installed - provide "blog this" link
  230. print '<td align="right">'."\n";
  231. /// MDL-9291, blog this feature needs further discussion/implementation
  232. /// temporarily disabling for now.
  233. // print '<img src="'. $CFG->pixpath .'/blog/blog.gif" alt="'. get_string('blogthis', 'blog').'" title="'. get_string('blogthis', 'blog') .'" border="0" align="middle" />'."\n";
  234. //print '<a href="'. $CFG->wwwroot .'/blog/blogthis.php?userid='. $USER->id .'&act=use&item='. $y .'&rssid='. $rssid .'"><small><strong>'. get_string('blogthis', 'blog') .'</strong></small></a>'."\n";
  235. } else {
  236. print '<td>&nbsp;';
  237. }
  238. print '</td></tr>'."\n";
  239. print '<tr><td colspan=2><small>';
  240. print $rss->items[$y]['description'] .'</small></td></tr>'."\n";
  241. }
  242. print '</table>'."\n";
  243. }
  244. } else {
  245. rss_display_feeds($id, $USER->id, '', $context);
  246. rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
  247. }
  248. echo '<div id=israelifeeds><a href=http://www.rssfeeds.co.il/>www.rssfeeds.co.il</a></div>';
  249. print_footer();
  250. ?>