File:
[LON-CAPA] /
loncom /
interface /
lonrss.pm
Revision
1.29:
download - view:
text,
annotated -
select for diffs
Fri Dec 1 20:17:47 2006 UTC (17 years, 7 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
lonrss.pm --
bug 5055 (part). A logged in user may not edit any of his/her own course or other blogs if he/she has any roles in courses with active blocks with the 'blogs' option set, and does not have the evb privilege.
A logged-in user subject to active blocks can not view personal or course blogs of other users via the web.
A public user may not view the personal or course blogs of any user who is in a course with active blocks (blogs option set) who does not have the evb privilege.
Course blogs (i.e., the ones with links on the syllabus page) which are owned by the course instead of an actual user are always visible.
Replace http://$ENV{'HTTP_HOST'} with call to lonnet::absolute_url()
reg exp used to check is username is a course or an actual user changed. This will be switched in the near future to call a function in LONCAPA.pm
loncommon.pm --
$uname and $udom can be passed in to &finallcourses() to allow active course information to be retrieved for users other than logged in user.
reg exp for custom role was not saving section information from role in &findallcourses()
hash value set in %courses in &findallcourses() is now a string containing role, domain, num, and section instead of 1.
$uname and $udom can be passed in to &blockcheck() to allow blocking checks to be made for users other than the logged in user.
1: # The LearningOnline Network
2: # RSS Feeder
3: #
4: # $Id: lonrss.pm,v 1.29 2006/12/01 20:17:47 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonrss;
30:
31: use strict;
32: use LONCAPA;
33: use Apache::Constants qw(:common);
34: use Apache::loncommon;
35: use Apache::lonnet;
36: use Apache::lontexconvert;
37: use Apache::lonlocal;
38: use Apache::lonhtmlcommon;
39:
40:
41: sub filterfeedname {
42: my $filename=shift;
43: $filename=~s/(\_rss\.html|\.rss)$//;
44: $filename=~s/\W//g;
45: $filename=~s/\_rssfeed$//;
46: $filename=~s/^nohist\_//;
47: return $filename;
48: }
49:
50: sub feedname {
51: return 'nohist_'.&filterfeedname(shift).'_rssfeed';
52: }
53:
54: sub displayfeedname {
55: my ($rawname,$uname,$udom)=@_;
56: my $filterfilename=&filterfeedname($rawname);
57: # do we have a stored name?
58: my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename,'feed_display_option_'.$filterfilename],$udom,$uname);
59: if ($stored{$filterfilename}) { return ($stored{$filterfilename},$stored{'feed_display_option_'.$filterfilename}); }
60: # no, construct a name
61: my $name=$filterfilename;
62: if ($name=~/^CourseBlog/) {
63: $name=&mt('Course Blog');
64: if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
65: $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
66: }
67: } else {
68: $name=~s/\_/ /g;
69: }
70: return ($name,$stored{'feed_display_option_'.$filterfilename});
71: }
72:
73: sub namefeed {
74: my ($rawname,$uname,$udom,$newname)=@_;
75: return &Apache::lonnet::put('nohist_all_rss_feeds',
76: { &filterfeedname($rawname) => $newname },
77: $udom,$uname);
78: }
79:
80: sub changefeeddisplay {
81: my ($rawname,$uname,$udom,$newstatus)=@_;
82: return &Apache::lonnet::put('nohist_all_rss_feeds',
83: { 'feed_display_option_'.&filterfeedname($rawname) => $newstatus },
84: $udom,$uname);
85: }
86:
87: sub advertisefeeds {
88: my ($uname,$udom,$edit)=@_;
89: my $feeds='';
90: my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
91: my $mode='public';
92: if ($edit) {
93: $mode='adm';
94: }
95: my $server = &Apache::lonnet::absolute_url();
96: foreach my $feed (sort(keys(%feednames))) {
97: if (($feed!~/^error\:/) && ($feed!~/^feed\_display\_option\_/)) {
98: my $feedurl= $server.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
99: my $htmlurl= $server.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html';
100: if ($feednames{'feed_display_option_'.$feed} eq 'hidden') {
101: if ($edit) {
102: $feeds.='<li><i>'.$feednames{$feed}.'</i><br />'.&mt('Hidden').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a></li>';
103: }
104: } else {
105: $feeds.='<li><b>'.$feednames{$feed}.
106: '</b><br />'.($edit?&mt('Edit'):'HTML').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a>'.
107: '<br />RSS: <a href="'.$feedurl.'"><tt>'.$feedurl.'</tt></a></li>';
108: }
109: }
110: }
111: if ($feeds) {
112: return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
113: } else {
114: return '';
115: }
116: }
117:
118: sub rss_link {
119: my ($url) = @_;
120: return qq|<link rel="alternate" type="application/rss+xml" title="Course Announcements" href="$url" />|;
121: }
122:
123: {
124: my $feedcounter;
125: sub get_new_feed_id {
126: $feedcounter++;
127: return time().'00000'.$$.'00000'.$feedcounter;
128: }
129: }
130:
131: sub addentry {
132: my $id=&get_new_feed_id();
133: return &editentry($id,@_);
134: }
135:
136: sub editentry {
137: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_;
138: if ($status eq 'deleted') {
139: return &changestatus($id,$uname,$udom,$filename,$status);
140: }
141: my $feedname=&feedname($filename);
142: &Apache::lonnet::put('nohist_all_rss_feeds',
143: { &filterfeedname($filename) =>
144: (&displayfeedname($filename,$uname,$udom))[0] },
145: $udom,$uname);
146: return &Apache::lonnet::put($feedname,{
147: $id.'_title' => $title,
148: $id.'_description' => $description,
149: $id.'_link' => $url,
150: $id.'_enclosureurl' => $encurl,
151: $id.'_enclosuretype' => $enctype,
152: $id.'_status' => $status},$udom,$uname);
153: }
154:
155: sub changestatus {
156: my ($id,$uname,$udom,$filename,$status)=@_;
157: my $feedname=&feedname($filename);
158: if ($status eq 'deleted') {
159: return &Apache::lonnet::del($feedname,[$id.'_title',
160: $id.'_description',
161: $id.'_link',
162: $id.'_enclosureurl',
163: $id.'_enclosuretype',
164: $id.'_status'],$udom,$uname);
165: } else {
166: return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
167: }
168: }
169:
170: sub changed_js {
171: return <<ENDSCRIPT;
172: <script type="text/javascript">
173: function changed(tform,id) {
174: tform.elements[id+"_modified"].checked=true;
175: }
176: </script>
177: ENDSCRIPT
178: }
179:
180: sub determine_enclosure_types {
181: my ($url)=@_;
182: my ($ending)=($url=~/\.(\w+)$/);
183: return &Apache::loncommon::filemimetype($ending);
184: }
185:
186: sub course_blog_link {
187: my ($id,$title,$description,$url,$encurl,$enctype)=@_;
188: if ($env{'request.course.id'}) {
189: return &add_blog_entry_link($id,
190: $env{'course.'.$env{'request.course.id'}.'.num'},
191: $env{'course.'.$env{'request.course.id'}.'.domain'},
192: 'Course_Announcements',
193: $title,$description,$url,'public',$encurl,$enctype,
194: &mt('Add to Course Announcements'));
195: } else {
196: return '';
197: }
198: }
199:
200: sub add_blog_entry_link {
201: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype,$linktext)=@_;
202: return "<a href='/adm/$udom/$uname/".&filterfeedname($filename).'_rss.html?queryid='.
203: &escape($id).
204: '&title='.&escape($title).
205: '&description='.&escape($description).
206: '&url='.&escape($url).
207: '&status='.&escape($status).
208: '&encurl='.&escape($encurl).
209: '&enctype='.&escape($enctype).
210: "'>".$linktext.'</a>';
211:
212: }
213:
214: sub blocking_blogdisplay {
215: my ($uname,$udom,$html,$filterfeedname) = @_;
216: my $user = &Apache::loncommon::plainname($uname,$udom);
217: if ($html) {
218: $user = &Apache::loncommon::aboutmewrapper($user,$uname,$udom);
219: } else {
220: $user = $user.' ('.$uname.':'.$udom.')';
221: }
222: my %setters;
223: my ($blocked,$output,$blockcause);
224: my ($startblock,$endblock) =
225: &Apache::loncommon::blockcheck(\%setters,'blogs');
226: if ($startblock && $endblock) {
227: $blockcause = 'user';
228: } else {
229: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
230: ($startblock,$endblock) =
231: &Apache::loncommon::blockcheck(\%setters,'blogs',
232: $uname,$udom);
233: $blockcause = 'blogowner';
234: }
235: }
236: if ($startblock && $endblock) {
237: $blocked = 1;
238: my $showstart = &Apache::lonlocal::locallocaltime($startblock);
239: my $showend = &Apache::lonlocal::locallocaltime($endblock);
240: $output = &mt('Blogs belonging to [_1] are unavailable from [_2] to [_3].',$user,$showstart,$showend);
241: if ($html) {$output.='<br />';}
242: if ($blockcause eq 'user') {
243: $output .= &mt('This is because you are a student in one or more courses in which communication is being blocked.');
244: if ($html) {
245: $output .= '<br />'.
246: &Apache::loncommon::build_block_table($startblock,
247: $endblock,\%setters);
248: }
249: } else {
250: $output .= &mt('This is because the blog owner is a student in one or more courses in which communication is being blocked.');
251: }
252: if (!$html) {
253: my $id = &get_new_feed_id();
254: $output = '<title/><item><title/><description>'.$output."</description><link/><guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname.'</guid></item>';
255: }
256: }
257: return ($blocked,$output);
258: }
259:
260: sub handler {
261: my ($r) = @_;
262:
263: my $edit=0;
264: my $html=0;
265: my (undef,$mode,$udom,$uname,$filename)=split(/\//,$r->uri);
266: if (($mode eq 'adm') && ($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
267: $edit=1;
268: $html=1;
269: }
270: if (($mode eq 'adm') && (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
271: $edit=1;
272: $html=1;
273: }
274: if ($filename=~/\.html$/) {
275: $html=1;
276: }
277: if ($html) {
278: &Apache::loncommon::content_type($r,'text/html');
279: } else {
280: # Workaround Mozilla/Firefox
281: # &Apache::loncommon::content_type($r,'application/rss+xml');
282: &Apache::loncommon::content_type($r,'text/xml');
283: }
284: $r->send_http_header;
285: return OK if $r->header_only;
286:
287: my $filterfeedname=&filterfeedname($filename);
288: my $feedname=&feedname($filename);
289: my ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
290: my ($blocked,$blocktext);
291: if ($uname !~ /^\d\w\d[\w\-.]+$/) {
292: ($blocked,$blocktext) = &blocking_blogdisplay($uname,$udom,$html,$filterfeedname);
293: }
294: if ($html) {
295: my $title = $displayfeedname?$displayfeedname
296: :"Available RSS Feeds and Blogs";
297: $r->print(&Apache::loncommon::start_page($title,undef,
298: {'domain' => $udom,
299: 'force_register' =>
300: $env{'form.register'}}).
301: &changed_js());
302: } else { # render RSS
303: my $server = &Apache::lonnet::absolute_url();
304: $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
305: "\n".'<link>'.$server.'/public/'.$udom.'/'.$uname.'/'.
306: $filterfeedname.'_rss.html</link>'.
307: "\n<description>".
308: &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').
309: '</description>');
310: }
311: # This will be the entry id for new additions to the blog
312: my $newid = &get_new_feed_id();
313: # Is this user for real?
314: my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
315: if ($html && !$blocked) {
316: # Any new feeds or renaming of feeds?
317: if ($edit) {
318: # Hide a feed?
319: if ($env{'form.hidethisblog'}) {
320: &changefeeddisplay($feedname,$uname,$udom,'hidden');
321: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
322: }
323: # Advertise a feed?
324: if ($env{'form.advertisethisblog'}) {
325: &changefeeddisplay($feedname,$uname,$udom,'public');
326: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
327: }
328: # New feed?
329: if ($env{'form.namenewblog'}=~/\w/) {
330: &namefeed($env{'form.namenewblog'},$uname,$udom,$env{'form.namenewblog'});
331: }
332: # Old feed that is being renamed?
333: if (($displayfeedname) && ($env{'form.newblogname'}=~/\w/)) {
334: if ($env{'form.newblogname'} ne $displayfeedname) {
335: &namefeed($feedname,$uname,$udom,$env{'form.newblogname'});
336: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
337: }
338: }
339: }
340: $r->print(&advertisefeeds($uname,$udom,$edit));
341: }
342: if ($homeserver eq 'no_host') {
343: $r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
344: } elsif ($blocked) {
345: $r->print($blocktext);
346: $r->print(($html?&Apache::loncommon::end_page():'</channel></rss>'."\n"));
347: } else { # is indeed a user
348: # Course or user?
349: my $name='';
350: if ($uname =~ /^\d\w\d[\w\-.]+$/) {
351: my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
352: $name=$cenv{'description'};
353: } else {
354: $name=&Apache::loncommon::nickname($uname,$udom);
355: }
356: # Add a new feed
357: if (($html) && ($edit)) {
358: $r->print('<form method="post">');
359: $r->print(&mt('Name for New Feed').": <input type='text' size='40' name='namenewblog' />");
360: $r->print('<input type="submit" value="'.&mt('Start a New Feed').'" />');
361: $r->print('</form>');
362: }
363: if ($displayfeedname) { # this is an existing feed
364: # Anything to store?
365: if ($edit) {
366: # check if this was called with a query string
367: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['queryid']);
368: if ($env{'form.queryid'}) {
369: # yes, collect the remainder
370: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
371: ['title',
372: 'description',
373: 'url',
374: 'status',
375: 'enclosureurl',
376: 'enclosuretype']);
377: # my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_;
378:
379: &editentry($env{'form.queryid'},
380: $uname,$udom,$filename,
381: $env{'form.title'},
382: $env{'form.description'},
383: $env{'form.url'},
384: $env{'form.status'},
385: $env{'form.encurl'},
386: $env{'form.enctype'}
387: );
388: }
389: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
390: foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
391: if ($entry=~/^(\d+)\_status$/) {
392: my $id=$1;
393: if ($env{'form.'.$id.'_modified'}) {
394: &editentry($id,$uname,$udom,$feedname,
395: $env{'form.'.$id.'_title'},
396: $env{'form.'.$id.'_description'},
397: $env{'form.'.$id.'_link'},
398: $env{'form.'.$id.'_status'},
399: $env{'form.'.$id.'_enclosureurl'},
400: $env{'form.'.$id.'_enclosuretype'},
401: );
402: }
403: }
404: }
405: } #done storing
406:
407: # Render private items?
408: my $viewpubliconly=1;
409: $r->print("\n".
410: ($html?'<hr /><h3>':'<title>').
411: &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
412: ($displayoption eq 'hidden'?' ('.&mt('Hidden').')':'').
413: ($html?'</h3>'.($edit?'<form method="post"><br />'.
414: &mt('Name of this Feed').
415: ': <input type="text" size="50" name="newblogname" value="'.
416: $displayfeedname.'" />':'').'<ul>':'</title>'));
417: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
418: $viewpubliconly=0;
419: }
420: # Get feed items
421: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
422: foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
423: if ($entry=~/^(\d+)\_status$/) { # is an entry
424: my $id=$1;
425: if ($edit) {
426: my %lt=&Apache::lonlocal::texthash('public' => 'public',
427: 'private' => 'private',
428: 'hidden' => 'hidden',
429: 'delete' => 'delete',
430: 'store' => 'Store changes',
431: 'title' => 'Title',
432: 'link' => 'Link',
433: 'description' => 'Description');
434: my %status=();
435: unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
436: $status{$newsfeed{$id.'_status'}}='checked="checked"';
437: $r->print(<<ENDEDIT);
438: <li>
439: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
440:
441: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
442:
443: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
444:
445: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
446:
447: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
448: <br />
449: $lt{'title'}:
450: <input name='$id\_title' type='text' size='60' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
451: $lt{'description'}:<br />
452: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
453: $lt{'link'}:
454: <input name='$id\_link' type='text' size='60' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
455: <hr /></li>
456: ENDEDIT
457: } else { # not in edit mode, just displaying
458: if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
459: if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
460: my $link = $newsfeed{$id.'_link'};
461: if ($link =~ m|^/| ) {
462: $link = "http://".$ENV{'HTTP_HOST'}.$link;
463: }
464: $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
465: ($html?"</b><br />\n":"</title>\n<description>").
466: $newsfeed{$id.'_description'}.
467: ($html?"<br />\n<a href='":"</description>\n<link>").
468:
469: $link.
470: ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
471: # Enclosure? Get stats
472: if ($newsfeed{$id.'_enclosureurl'}) {
473: my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'});
474: if ($stat[7]) {
475: # Has non-zero length (and exists)
476: my $enclosuretype=$newsfeed{$id.'_enclosetype'};
477: $r->print(($html?"<a href='":"\n<enclosure url='").
478: $newsfeed{$id.'_enclosureurl'}."' length='".$stat[7].
479: "' type='".$enclosuretype.($html?"'>".&mt('Enclosure')."</a>":"' />"));
480: }
481: }
482: if ($html) { # is HTML
483: $r->print("\n<hr /></li>\n");
484: } else { # is RSS
485: $r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
486: }
487: } # end of "in edit mode"
488: } # end of rendering a real entry
489: } # end of loop through all keys
490: if ($html) {
491: $r->print('</ul>');
492: if ($edit) {
493: $r->print('<input type="hidden" name="newid" value="'.$newid.'"/><input type="submit" value="'.&mt('Store Marked Changes').'" />'.
494: ($displayoption eq 'hidden'?'<input type="submit" name="advertisethisblog" value="'.&mt('Advertise this Feed').'" />':
495: '<input type="submit" name="hidethisblog" value="'.&mt('Hide this Feed').'" />'));
496: }
497: }
498: } # was a real display feedname
499: $r->print(($html?'</form>'.&Apache::loncommon::end_page():'</channel></rss>'."\n"));
500: } # a real user
501: return OK;
502: } # end handler
503: 1;
504: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>