File:
[LON-CAPA] /
loncom /
lonnet /
perl /
lonnet.pm
Revision
1.543:
download - view:
text,
annotated -
select for diffs
Mon Sep 20 19:54:47 2004 UTC (19 years, 10 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- metadata cache only invalidates if
- .time is past (so the entry has been around 300 seconds)
- all of the files listed in .file have mtime < the cache .time
- (breaks disk based caching for now .file isn't being saved loaded)
1: # The LearningOnline Network
2: # TCP networking package
3: #
4: # $Id: lonnet.pm,v 1.543 2004/09/20 19:54:47 albertel 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:
30: package Apache::lonnet;
31:
32: use strict;
33: use LWP::UserAgent();
34: use HTTP::Headers;
35: use HTTP::Date;
36: # use Date::Parse;
37: use vars
38: qw(%perlvar %hostname %homecache %badServerCache %hostip %iphost %spareid %hostdom
39: %libserv %pr %prp %metacache %packagetab %titlecache %courseresversioncache %resversioncache
40: %courselogs %accesshash %userrolehash $processmarker $dumpcount
41: %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseresdatacache
42: %userresdatacache %usectioncache %domaindescription %domain_auth_def %domain_auth_arg_def
43: %domain_lang_def %domain_city %domain_longi %domain_lati $tmpdir);
44:
45: use IO::Socket;
46: use GDBM_File;
47: use Apache::Constants qw(:common :http);
48: use HTML::LCParser;
49: use Fcntl qw(:flock);
50: use Apache::loncoursedata;
51: use Apache::lonlocal;
52: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw);
53: use Time::HiRes qw( gettimeofday tv_interval );
54: my $readit;
55:
56: =pod
57:
58: =head1 Package Variables
59:
60: These are largely undocumented, so if you decipher one please note it here.
61:
62: =over 4
63:
64: =item $processmarker
65:
66: Contains the time this process was started and this servers host id.
67:
68: =item $dumpcount
69:
70: Counts the number of times a message log flush has been attempted (regardless
71: of success) by this process. Used as part of the filename when messages are
72: delayed.
73:
74: =back
75:
76: =cut
77:
78:
79: # --------------------------------------------------------------------- Logging
80:
81: sub logtouch {
82: my $execdir=$perlvar{'lonDaemons'};
83: unless (-e "$execdir/logs/lonnet.log") {
84: open(my $fh,">>$execdir/logs/lonnet.log");
85: close $fh;
86: }
87: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
88: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
89: }
90:
91: sub logthis {
92: my $message=shift;
93: my $execdir=$perlvar{'lonDaemons'};
94: my $now=time;
95: my $local=localtime($now);
96: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
97: print $fh "$local ($$): $message\n";
98: close($fh);
99: }
100: return 1;
101: }
102:
103: sub logperm {
104: my $message=shift;
105: my $execdir=$perlvar{'lonDaemons'};
106: my $now=time;
107: my $local=localtime($now);
108: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
109: print $fh "$now:$message:$local\n";
110: close($fh);
111: }
112: return 1;
113: }
114:
115: # -------------------------------------------------- Non-critical communication
116: sub subreply {
117: my ($cmd,$server)=@_;
118: my $peerfile="$perlvar{'lonSockDir'}/$server";
119: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
120: Type => SOCK_STREAM,
121: Timeout => 10)
122: or return "con_lost";
123: print $client "$cmd\n";
124: my $answer=<$client>;
125: if (!$answer) { $answer="con_lost"; }
126: chomp($answer);
127: return $answer;
128: }
129:
130: sub reply {
131: my ($cmd,$server)=@_;
132: unless (defined($hostname{$server})) { return 'no_such_host'; }
133: my $answer=subreply($cmd,$server);
134: if ($answer eq 'con_lost') {
135: #sleep 5;
136: #$answer=subreply($cmd,$server);
137: #if ($answer eq 'con_lost') {
138: # &logthis("Second attempt con_lost on $server");
139: # my $peerfile="$perlvar{'lonSockDir'}/$server";
140: # my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
141: # Type => SOCK_STREAM,
142: # Timeout => 10)
143: # or return "con_lost";
144: # &logthis("Killing socket");
145: # print $client "close_connection_exit\n";
146: #sleep 5;
147: # $answer=subreply($cmd,$server);
148: #}
149: }
150: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
151: &logthis("<font color=blue>WARNING:".
152: " $cmd to $server returned $answer</font>");
153: }
154: return $answer;
155: }
156:
157: # ----------------------------------------------------------- Send USR1 to lonc
158:
159: sub reconlonc {
160: my $peerfile=shift;
161: &logthis("Trying to reconnect for $peerfile");
162: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
163: if (open(my $fh,"<$loncfile")) {
164: my $loncpid=<$fh>;
165: chomp($loncpid);
166: if (kill 0 => $loncpid) {
167: &logthis("lonc at pid $loncpid responding, sending USR1");
168: kill USR1 => $loncpid;
169: sleep 1;
170: if (-e "$peerfile") { return; }
171: &logthis("$peerfile still not there, give it another try");
172: sleep 5;
173: if (-e "$peerfile") { return; }
174: &logthis(
175: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
176: } else {
177: &logthis(
178: "<font color=blue>WARNING:".
179: " lonc at pid $loncpid not responding, giving up</font>");
180: }
181: } else {
182: &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
183: }
184: }
185:
186: # ------------------------------------------------------ Critical communication
187:
188: sub critical {
189: my ($cmd,$server)=@_;
190: unless ($hostname{$server}) {
191: &logthis("<font color=blue>WARNING:".
192: " Critical message to unknown server ($server)</font>");
193: return 'no_such_host';
194: }
195: my $answer=reply($cmd,$server);
196: if ($answer eq 'con_lost') {
197: my $pingreply=reply('ping',$server);
198: &reconlonc("$perlvar{'lonSockDir'}/$server");
199: my $pongreply=reply('pong',$server);
200: &logthis("Ping/Pong for $server: $pingreply/$pongreply");
201: $answer=reply($cmd,$server);
202: if ($answer eq 'con_lost') {
203: my $now=time;
204: my $middlename=$cmd;
205: $middlename=substr($middlename,0,16);
206: $middlename=~s/\W//g;
207: my $dfilename=
208: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
209: $dumpcount++;
210: {
211: my $dfh;
212: if (open($dfh,">$dfilename")) {
213: print $dfh "$cmd\n";
214: close($dfh);
215: }
216: }
217: sleep 2;
218: my $wcmd='';
219: {
220: my $dfh;
221: if (open($dfh,"<$dfilename")) {
222: $wcmd=<$dfh>;
223: close($dfh);
224: }
225: }
226: chomp($wcmd);
227: if ($wcmd eq $cmd) {
228: &logthis("<font color=blue>WARNING: ".
229: "Connection buffer $dfilename: $cmd</font>");
230: &logperm("D:$server:$cmd");
231: return 'con_delayed';
232: } else {
233: &logthis("<font color=red>CRITICAL:"
234: ." Critical connection failed: $server $cmd</font>");
235: &logperm("F:$server:$cmd");
236: return 'con_failed';
237: }
238: }
239: }
240: return $answer;
241: }
242:
243: #
244: # -------------- Remove all key from the env that start witha lowercase letter
245: # (Which is always a lon-capa value)
246:
247: sub cleanenv {
248: # unless (defined(&Apache::exists_config_define("MODPERL2"))) { return; }
249: # unless (&Apache::exists_config_define("MODPERL2")) { return; }
250: foreach my $key (keys(%ENV)) {
251: if ($key =~ /^[a-z]/) {
252: delete($ENV{$key});
253: }
254: }
255: }
256:
257: # ------------------------------------------- Transfer profile into environment
258:
259: sub transfer_profile_to_env {
260: my ($lonidsdir,$handle)=@_;
261: my @profile;
262: {
263: open(my $idf,"$lonidsdir/$handle.id");
264: flock($idf,LOCK_SH);
265: @profile=<$idf>;
266: close($idf);
267: }
268: my $envi;
269: my %Remove;
270: for ($envi=0;$envi<=$#profile;$envi++) {
271: chomp($profile[$envi]);
272: my ($envname,$envvalue)=split(/=/,$profile[$envi]);
273: $ENV{$envname} = $envvalue;
274: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
275: if ($time < time-300) {
276: $Remove{$key}++;
277: }
278: }
279: }
280: $ENV{'user.environment'} = "$lonidsdir/$handle.id";
281: foreach my $expired_key (keys(%Remove)) {
282: &delenv($expired_key);
283: }
284: }
285:
286: # ---------------------------------------------------------- Append Environment
287:
288: sub appenv {
289: my %newenv=@_;
290: foreach (keys %newenv) {
291: if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
292: &logthis("<font color=blue>WARNING: ".
293: "Attempt to modify environment ".$_." to ".$newenv{$_}
294: .'</font>');
295: delete($newenv{$_});
296: } else {
297: $ENV{$_}=$newenv{$_};
298: }
299: }
300:
301: my $lockfh;
302: unless (open($lockfh,"$ENV{'user.environment'}")) {
303: return 'error: '.$!;
304: }
305: unless (flock($lockfh,LOCK_EX)) {
306: &logthis("<font color=blue>WARNING: ".
307: 'Could not obtain exclusive lock in appenv: '.$!);
308: close($lockfh);
309: return 'error: '.$!;
310: }
311:
312: my @oldenv;
313: {
314: my $fh;
315: unless (open($fh,"$ENV{'user.environment'}")) {
316: return 'error: '.$!;
317: }
318: @oldenv=<$fh>;
319: close($fh);
320: }
321: for (my $i=0; $i<=$#oldenv; $i++) {
322: chomp($oldenv[$i]);
323: if ($oldenv[$i] ne '') {
324: my ($name,$value)=split(/=/,$oldenv[$i]);
325: unless (defined($newenv{$name})) {
326: $newenv{$name}=$value;
327: }
328: }
329: }
330: {
331: my $fh;
332: unless (open($fh,">$ENV{'user.environment'}")) {
333: return 'error';
334: }
335: my $newname;
336: foreach $newname (keys %newenv) {
337: print $fh "$newname=$newenv{$newname}\n";
338: }
339: close($fh);
340: }
341:
342: close($lockfh);
343: return 'ok';
344: }
345: # ----------------------------------------------------- Delete from Environment
346:
347: sub delenv {
348: my $delthis=shift;
349: my %newenv=();
350: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
351: &logthis("<font color=blue>WARNING: ".
352: "Attempt to delete from environment ".$delthis);
353: return 'error';
354: }
355: my @oldenv;
356: {
357: my $fh;
358: unless (open($fh,"$ENV{'user.environment'}")) {
359: return 'error';
360: }
361: unless (flock($fh,LOCK_SH)) {
362: &logthis("<font color=blue>WARNING: ".
363: 'Could not obtain shared lock in delenv: '.$!);
364: close($fh);
365: return 'error: '.$!;
366: }
367: @oldenv=<$fh>;
368: close($fh);
369: }
370: {
371: my $fh;
372: unless (open($fh,">$ENV{'user.environment'}")) {
373: return 'error';
374: }
375: unless (flock($fh,LOCK_EX)) {
376: &logthis("<font color=blue>WARNING: ".
377: 'Could not obtain exclusive lock in delenv: '.$!);
378: close($fh);
379: return 'error: '.$!;
380: }
381: foreach (@oldenv) {
382: if ($_=~/^$delthis/) {
383: my ($key,undef) = split('=',$_);
384: delete($ENV{$key});
385: } else {
386: print $fh $_;
387: }
388: }
389: close($fh);
390: }
391: return 'ok';
392: }
393:
394: # ------------------------------------------ Find out current server userload
395: # there is a copy in lond
396: sub userload {
397: my $numusers=0;
398: {
399: opendir(LONIDS,$perlvar{'lonIDsDir'});
400: my $filename;
401: my $curtime=time;
402: while ($filename=readdir(LONIDS)) {
403: if ($filename eq '.' || $filename eq '..') {next;}
404: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
405: if ($curtime-$mtime < 1800) { $numusers++; }
406: }
407: closedir(LONIDS);
408: }
409: my $userloadpercent=0;
410: my $maxuserload=$perlvar{'lonUserLoadLim'};
411: if ($maxuserload) {
412: $userloadpercent=100*$numusers/$maxuserload;
413: }
414: $userloadpercent=sprintf("%.2f",$userloadpercent);
415: return $userloadpercent;
416: }
417:
418: # ------------------------------------------ Fight off request when overloaded
419:
420: sub overloaderror {
421: my ($r,$checkserver)=@_;
422: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
423: my $loadavg;
424: if ($checkserver eq $perlvar{'lonHostID'}) {
425: open(my $loadfile,'/proc/loadavg');
426: $loadavg=<$loadfile>;
427: $loadavg =~ s/\s.*//g;
428: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
429: close($loadfile);
430: } else {
431: $loadavg=&reply('load',$checkserver);
432: }
433: my $overload=$loadavg-100;
434: if ($overload>0) {
435: $r->err_headers_out->{'Retry-After'}=$overload;
436: $r->log_error('Overload of '.$overload.' on '.$checkserver);
437: return 409;
438: }
439: return '';
440: }
441:
442: # ------------------------------ Find server with least workload from spare.tab
443:
444: sub spareserver {
445: my ($loadpercent,$userloadpercent) = @_;
446: my $tryserver;
447: my $spareserver='';
448: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
449: my $lowestserver=$loadpercent > $userloadpercent?
450: $loadpercent : $userloadpercent;
451: foreach $tryserver (keys %spareid) {
452: my $loadans=reply('load',$tryserver);
453: my $userloadans=reply('userload',$tryserver);
454: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
455: next; #didn't get a number from the server
456: }
457: my $answer;
458: if ($loadans =~ /\d/) {
459: if ($userloadans =~ /\d/) {
460: #both are numbers, pick the bigger one
461: $answer=$loadans > $userloadans?
462: $loadans : $userloadans;
463: } else {
464: $answer = $loadans;
465: }
466: } else {
467: $answer = $userloadans;
468: }
469: if (($answer =~ /\d/) && ($answer<$lowestserver)) {
470: $spareserver="http://$hostname{$tryserver}";
471: $lowestserver=$answer;
472: }
473: }
474: return $spareserver;
475: }
476:
477: # --------------------------------------------- Try to change a user's password
478:
479: sub changepass {
480: my ($uname,$udom,$currentpass,$newpass,$server)=@_;
481: $currentpass = &escape($currentpass);
482: $newpass = &escape($newpass);
483: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
484: $server);
485: if (! $answer) {
486: &logthis("No reply on password change request to $server ".
487: "by $uname in domain $udom.");
488: } elsif ($answer =~ "^ok") {
489: &logthis("$uname in $udom successfully changed their password ".
490: "on $server.");
491: } elsif ($answer =~ "^pwchange_failure") {
492: &logthis("$uname in $udom was unable to change their password ".
493: "on $server. The action was blocked by either lcpasswd ".
494: "or pwchange");
495: } elsif ($answer =~ "^non_authorized") {
496: &logthis("$uname in $udom did not get their password correct when ".
497: "attempting to change it on $server.");
498: } elsif ($answer =~ "^auth_mode_error") {
499: &logthis("$uname in $udom attempted to change their password despite ".
500: "not being locally or internally authenticated on $server.");
501: } elsif ($answer =~ "^unknown_user") {
502: &logthis("$uname in $udom attempted to change their password ".
503: "on $server but were unable to because $server is not ".
504: "their home server.");
505: } elsif ($answer =~ "^refused") {
506: &logthis("$server refused to change $uname in $udom password because ".
507: "it was sent an unencrypted request to change the password.");
508: }
509: return $answer;
510: }
511:
512: # ----------------------- Try to determine user's current authentication scheme
513:
514: sub queryauthenticate {
515: my ($uname,$udom)=@_;
516: my $uhome=&homeserver($uname,$udom);
517: if (!$uhome) {
518: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
519: return 'no_host';
520: }
521: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
522: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
523: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
524: }
525: return $answer;
526: }
527:
528: # --------- Try to authenticate user from domain's lib servers (first this one)
529:
530: sub authenticate {
531: my ($uname,$upass,$udom)=@_;
532: $upass=escape($upass);
533: $uname=~s/\W//g;
534: my $uhome=&homeserver($uname,$udom);
535: if (!$uhome) {
536: &logthis("User $uname at $udom is unknown in authenticate");
537: return 'no_host';
538: }
539: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
540: if ($answer eq 'authorized') {
541: &logthis("User $uname at $udom authorized by $uhome");
542: return $uhome;
543: }
544: if ($answer eq 'non_authorized') {
545: &logthis("User $uname at $udom rejected by $uhome");
546: return 'no_host';
547: }
548: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
549: return 'no_host';
550: }
551:
552: # ---------------------- Find the homebase for a user from domain's lib servers
553:
554: sub homeserver {
555: my ($uname,$udom,$ignoreBadCache)=@_;
556: my $index="$uname:$udom";
557:
558: my ($result,$cached)=&is_cached(\%homecache,$index,'home',86400);
559: if (defined($cached)) { return $result; }
560: my $tryserver;
561: foreach $tryserver (keys %libserv) {
562: next if ($ignoreBadCache ne 'true' &&
563: exists($badServerCache{$tryserver}));
564: if ($hostdom{$tryserver} eq $udom) {
565: my $answer=reply("home:$udom:$uname",$tryserver);
566: if ($answer eq 'found') {
567: return &do_cache(\%homecache,$index,$tryserver,'home');
568: } elsif ($answer eq 'no_host') {
569: $badServerCache{$tryserver}=1;
570: }
571: }
572: }
573: return 'no_host';
574: }
575:
576: # ------------------------------------- Find the usernames behind a list of IDs
577:
578: sub idget {
579: my ($udom,@ids)=@_;
580: my %returnhash=();
581:
582: my $tryserver;
583: foreach $tryserver (keys %libserv) {
584: if ($hostdom{$tryserver} eq $udom) {
585: my $idlist=join('&',@ids);
586: $idlist=~tr/A-Z/a-z/;
587: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
588: my @answer=();
589: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
590: @answer=split(/\&/,$reply);
591: } ;
592: my $i;
593: for ($i=0;$i<=$#ids;$i++) {
594: if ($answer[$i]) {
595: $returnhash{$ids[$i]}=$answer[$i];
596: }
597: }
598: }
599: }
600: return %returnhash;
601: }
602:
603: # ------------------------------------- Find the IDs behind a list of usernames
604:
605: sub idrget {
606: my ($udom,@unames)=@_;
607: my %returnhash=();
608: foreach (@unames) {
609: $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
610: }
611: return %returnhash;
612: }
613:
614: # ------------------------------- Store away a list of names and associated IDs
615:
616: sub idput {
617: my ($udom,%ids)=@_;
618: my %servers=();
619: foreach (keys %ids) {
620: &cput('environment',{'id'=>$ids{$_}},$udom,$_);
621: my $uhom=&homeserver($_,$udom);
622: if ($uhom ne 'no_host') {
623: my $id=&escape($ids{$_});
624: $id=~tr/A-Z/a-z/;
625: my $unam=&escape($_);
626: if ($servers{$uhom}) {
627: $servers{$uhom}.='&'.$id.'='.$unam;
628: } else {
629: $servers{$uhom}=$id.'='.$unam;
630: }
631: }
632: }
633: foreach (keys %servers) {
634: &critical('idput:'.$udom.':'.$servers{$_},$_);
635: }
636: }
637:
638: # --------------------------------------------------- Assign a key to a student
639:
640: sub assign_access_key {
641: #
642: # a valid key looks like uname:udom#comments
643: # comments are being appended
644: #
645: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
646: $kdom=
647: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($kdom));
648: $knum=
649: $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($knum));
650: $cdom=
651: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
652: $cnum=
653: $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
654: $udom=$ENV{'user.name'} unless (defined($udom));
655: $uname=$ENV{'user.domain'} unless (defined($uname));
656: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
657: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
658: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
659: # assigned to this person
660: # - this should not happen,
661: # unless something went wrong
662: # the first time around
663: # ready to assign
664: $logentry=$1.'; '.$logentry;
665: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
666: $kdom,$knum) eq 'ok') {
667: # key now belongs to user
668: my $envkey='key.'.$cdom.'_'.$cnum;
669: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
670: &appenv('environment.'.$envkey => $ckey);
671: return 'ok';
672: } else {
673: return
674: 'error: Count not permanently assign key, will need to be re-entered later.';
675: }
676: } else {
677: return 'error: Could not assign key, try again later.';
678: }
679: } elsif (!$existing{$ckey}) {
680: # the key does not exist
681: return 'error: The key does not exist';
682: } else {
683: # the key is somebody else's
684: return 'error: The key is already in use';
685: }
686: }
687:
688: # ------------------------------------------ put an additional comment on a key
689:
690: sub comment_access_key {
691: #
692: # a valid key looks like uname:udom#comments
693: # comments are being appended
694: #
695: my ($ckey,$cdom,$cnum,$logentry)=@_;
696: $cdom=
697: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
698: $cnum=
699: $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
700: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
701: if ($existing{$ckey}) {
702: $existing{$ckey}.='; '.$logentry;
703: # ready to assign
704: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
705: $cdom,$cnum) eq 'ok') {
706: return 'ok';
707: } else {
708: return 'error: Count not store comment.';
709: }
710: } else {
711: # the key does not exist
712: return 'error: The key does not exist';
713: }
714: }
715:
716: # ------------------------------------------------------ Generate a set of keys
717:
718: sub generate_access_keys {
719: my ($number,$cdom,$cnum,$logentry)=@_;
720: $cdom=
721: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
722: $cnum=
723: $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
724: unless (&allowed('mky',$cdom)) { return 0; }
725: unless (($cdom) && ($cnum)) { return 0; }
726: if ($number>10000) { return 0; }
727: sleep(2); # make sure don't get same seed twice
728: srand(time()^($$+($$<<15))); # from "Programming Perl"
729: my $total=0;
730: for (my $i=1;$i<=$number;$i++) {
731: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
732: sprintf("%lx",int(100000*rand)).'-'.
733: sprintf("%lx",int(100000*rand));
734: $newkey=~s/1/g/g; # folks mix up 1 and l
735: $newkey=~s/0/h/g; # and also 0 and O
736: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
737: if ($existing{$newkey}) {
738: $i--;
739: } else {
740: if (&put('accesskeys',
741: { $newkey => '# generated '.localtime().
742: ' by '.$ENV{'user.name'}.'@'.$ENV{'user.domain'}.
743: '; '.$logentry },
744: $cdom,$cnum) eq 'ok') {
745: $total++;
746: }
747: }
748: }
749: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
750: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
751: return $total;
752: }
753:
754: # ------------------------------------------------------- Validate an accesskey
755:
756: sub validate_access_key {
757: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
758: $cdom=
759: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
760: $cnum=
761: $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
762: $udom=$ENV{'user.domain'} unless (defined($udom));
763: $uname=$ENV{'user.name'} unless (defined($uname));
764: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
765: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
766: }
767:
768: # ------------------------------------- Find the section of student in a course
769:
770: sub getsection {
771: my ($udom,$unam,$courseid)=@_;
772: $courseid=~s/\_/\//g;
773: $courseid=~s/^(\w)/\/$1/;
774: my %Pending;
775: my %Expired;
776: #
777: # Each role can either have not started yet (pending), be active,
778: # or have expired.
779: #
780: # If there is an active role, we are done.
781: #
782: # If there is more than one role which has not started yet,
783: # choose the one which will start sooner
784: # If there is one role which has not started yet, return it.
785: #
786: # If there is more than one expired role, choose the one which ended last.
787: # If there is a role which has expired, return it.
788: #
789: foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
790: &homeserver($unam,$udom)))) {
791: my ($key,$value)=split(/\=/,$_);
792: $key=&unescape($key);
793: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
794: my $section=$1;
795: if ($key eq $courseid.'_st') { $section=''; }
796: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
797: my $now=time;
798: if (defined($end) && ($now > $end)) {
799: $Expired{$end}=$section;
800: next;
801: }
802: if (defined($start) && ($now < $start)) {
803: $Pending{$start}=$section;
804: next;
805: }
806: return $section;
807: }
808: #
809: # Presumedly there will be few matching roles from the above
810: # loop and the sorting time will be negligible.
811: if (scalar(keys(%Pending))) {
812: my ($time) = sort {$a <=> $b} keys(%Pending);
813: return $Pending{$time};
814: }
815: if (scalar(keys(%Expired))) {
816: my @sorted = sort {$a <=> $b} keys(%Expired);
817: my $time = pop(@sorted);
818: return $Expired{$time};
819: }
820: return '-1';
821: }
822:
823:
824: my $disk_caching_disabled=1;
825:
826: sub devalidate_cache {
827: my ($cache,$id,$name) = @_;
828: delete $$cache{$id.'.time'};
829: delete $$cache{$id.'.file'};
830: delete $$cache{$id};
831: if (1 || $disk_caching_disabled) { return; }
832: my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
833: if (!-e $filename) { return; }
834: open(DB,">$filename.lock");
835: flock(DB,LOCK_EX);
836: my %hash;
837: if (tie(%hash,'GDBM_File',$filename,&GDBM_WRCREAT(),0640)) {
838: eval <<'EVALBLOCK';
839: delete($hash{$id});
840: delete($hash{$id.'.time'});
841: EVALBLOCK
842: if ($@) {
843: &logthis("<font color='red'>devalidate_cache blew up :$@:$name</font>");
844: unlink($filename);
845: }
846: } else {
847: if (-e $filename) {
848: &logthis("Unable to tie hash (devalidate cache): $name");
849: unlink($filename);
850: }
851: }
852: untie(%hash);
853: flock(DB,LOCK_UN);
854: close(DB);
855: }
856:
857: sub is_cached {
858: my ($cache,$id,$name,$time) = @_;
859: if (!$time) { $time=300; }
860: if (!exists($$cache{$id.'.time'})) {
861: &load_cache_item($cache,$name,$id,$time);
862: }
863: if (!exists($$cache{$id.'.time'})) {
864: # &logthis("Didn't find $id");
865: return (undef,undef);
866: } else {
867: if (time-($$cache{$id.'.time'})>$time) {
868: if (exists($$cache{$id.'.file'})) {
869: foreach my $filename (@{ $$cache{$id.'.file'} }) {
870: my $mtime=(stat($filename))[9];
871: #+1 is to take care of edge effects
872: if ($mtime && (($mtime+1) < ($$cache{$id.'.time'}))) {
873: # &logthis("Upping $mtime - ".$$cache{$id.'.time'}.
874: # "$id because of $filename");
875: } else {
876: &logthis("Devalidating $filename $id - ".(time-($$cache{$id.'.time'})));
877: &devalidate_cache($cache,$id,$name);
878: return (undef,undef);
879: }
880: }
881: $$cache{$id.'.time'}=time;
882: } else {
883: # &logthis("Devalidating $id - ".time-($$cache{$id.'.time'}));
884: &devalidate_cache($cache,$id,$name);
885: return (undef,undef);
886: }
887: }
888: }
889: return ($$cache{$id},1);
890: }
891:
892: sub do_cache {
893: my ($cache,$id,$value,$name) = @_;
894: $$cache{$id.'.time'}=time;
895: $$cache{$id}=$value;
896: # &logthis("Caching $id as :$value:");
897: &save_cache_item($cache,$name,$id);
898: # do_cache implictly return the set value
899: $$cache{$id};
900: }
901:
902: my %do_save_item;
903: my %do_save;
904: sub save_cache_item {
905: my ($cache,$name,$id)=@_;
906: if ($disk_caching_disabled) { return; }
907: $do_save{$name}=$cache;
908: if (!exists($do_save_item{$name})) { $do_save_item{$name}={} }
909: $do_save_item{$name}->{$id}=1;
910: return;
911: }
912:
913: sub save_cache {
914: if ($disk_caching_disabled) { return; }
915: my ($cache,$name,$id);
916: foreach $name (keys(%do_save)) {
917: $cache=$do_save{$name};
918:
919: my $starttime=&Time::HiRes::time();
920: &logthis("Saving :$name:");
921: my %hash;
922: my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
923: open(DB,">$filename.lock");
924: flock(DB,LOCK_EX);
925: if (tie(%hash,'GDBM_File',$filename,&GDBM_WRCREAT(),0640)) {
926: foreach $id (keys(%{ $do_save_item{$name} })) {
927: eval <<'EVALBLOCK';
928: $hash{$id.'.time'}=$$cache{$id.'.time'};
929: $hash{$id}=freeze({'item'=>$$cache{$id}});
930: EVALBLOCK
931: if ($@) {
932: &logthis("<font color='red'>save_cache blew up :$@:$name</font>");
933: unlink($filename);
934: last;
935: }
936: }
937: } else {
938: if (-e $filename) {
939: &logthis("Unable to tie hash (save cache): $name ($!)");
940: unlink($filename);
941: }
942: }
943: untie(%hash);
944: flock(DB,LOCK_UN);
945: close(DB);
946: &logthis("save_cache $name took ".(&Time::HiRes::time()-$starttime));
947: }
948: undef(%do_save);
949: undef(%do_save_item);
950:
951: }
952:
953: sub load_cache_item {
954: my ($cache,$name,$id,$time)=@_;
955: if ($disk_caching_disabled) { return; }
956: my $starttime=&Time::HiRes::time();
957: # &logthis("Before Loading $name for $id size is ".scalar(%$cache));
958: my %hash;
959: my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
960: if (!-e $filename) { return; }
961: open(DB,">$filename.lock");
962: flock(DB,LOCK_SH);
963: if (tie(%hash,'GDBM_File',$filename,&GDBM_READER(),0640)) {
964: eval <<'EVALBLOCK';
965: if (!%$cache) {
966: my $count;
967: while (my ($key,$value)=each(%hash)) {
968: $count++;
969: if ($key =~ /\.time$/) {
970: $$cache{$key}=$value;
971: } else {
972: my $hashref=thaw($value);
973: $$cache{$key}=$hashref->{'item'};
974: }
975: }
976: # &logthis("Initial load: $count");
977: } else {
978: if (($$cache{$id.'.time'}+$time) < time) {
979: $$cache{$id.'.time'}=$hash{$id.'.time'};
980: my $hashref=thaw($hash{$id});
981: $$cache{$id}=$hashref->{'item'};
982: }
983: }
984: EVALBLOCK
985: if ($@) {
986: &logthis("<font color='red'>load_cache blew up :$@:$name</font>");
987: unlink($filename);
988: }
989: } else {
990: if (-e $filename) {
991: &logthis("Unable to tie hash (load cache item): $name ($!)");
992: unlink($filename);
993: }
994: }
995: untie(%hash);
996: flock(DB,LOCK_UN);
997: close(DB);
998: # &logthis("After Loading $name size is ".scalar(%$cache));
999: # &logthis("load_cache_item $name took ".(&Time::HiRes::time()-$starttime));
1000: }
1001:
1002: sub usection {
1003: my ($udom,$unam,$courseid)=@_;
1004: my $hashid="$udom:$unam:$courseid";
1005:
1006: my ($result,$cached)=&is_cached(\%usectioncache,$hashid,'usection');
1007: if (defined($cached)) { return $result; }
1008: $courseid=~s/\_/\//g;
1009: $courseid=~s/^(\w)/\/$1/;
1010: foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
1011: &homeserver($unam,$udom)))) {
1012: my ($key,$value)=split(/\=/,$_);
1013: $key=&unescape($key);
1014: if ($key=~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/) {
1015: my $section=$1;
1016: if ($key eq $courseid.'_st') { $section=''; }
1017: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
1018: my $now=time;
1019: my $notactive=0;
1020: if ($start) {
1021: if ($now<$start) { $notactive=1; }
1022: }
1023: if ($end) {
1024: if ($now>$end) { $notactive=1; }
1025: }
1026: unless ($notactive) {
1027: return &do_cache(\%usectioncache,$hashid,$section,'usection');
1028: }
1029: }
1030: }
1031: return &do_cache(\%usectioncache,$hashid,'-1','usection');
1032: }
1033:
1034: # ------------------------------------- Read an entry from a user's environment
1035:
1036: sub userenvironment {
1037: my ($udom,$unam,@what)=@_;
1038: my %returnhash=();
1039: my @answer=split(/\&/,
1040: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
1041: &homeserver($unam,$udom)));
1042: my $i;
1043: for ($i=0;$i<=$#what;$i++) {
1044: $returnhash{$what[$i]}=&unescape($answer[$i]);
1045: }
1046: return %returnhash;
1047: }
1048:
1049: # -------------------------------------------------------------------- New chat
1050:
1051: sub chatsend {
1052: my ($newentry,$anon)=@_;
1053: my $cnum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1054: my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1055: my $chome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1056: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1057: &escape($ENV{'user.domain'}.':'.$ENV{'user.name'}.':'.$anon.':'.
1058: &escape($newentry)),$chome);
1059: }
1060:
1061: # ------------------------------------------ Find current version of a resource
1062:
1063: sub getversion {
1064: my $fname=&clutter(shift);
1065: unless ($fname=~/^\/res\//) { return -1; }
1066: return ¤tversion(&filelocation('',$fname));
1067: }
1068:
1069: sub currentversion {
1070: my $fname=shift;
1071: my ($result,$cached)=&is_cached(\%resversioncache,$fname,'resversion',600);
1072: if (defined($cached)) { return $result; }
1073: my $author=$fname;
1074: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1075: my ($udom,$uname)=split(/\//,$author);
1076: my $home=homeserver($uname,$udom);
1077: if ($home eq 'no_host') {
1078: return -1;
1079: }
1080: my $answer=reply("currentversion:$fname",$home);
1081: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1082: return -1;
1083: }
1084: return &do_cache(\%resversioncache,$fname,$answer,'resversion');
1085: }
1086:
1087: # ----------------------------- Subscribe to a resource, return URL if possible
1088:
1089: sub subscribe {
1090: my $fname=shift;
1091: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1092: $fname=~s/[\n\r]//g;
1093: my $author=$fname;
1094: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1095: my ($udom,$uname)=split(/\//,$author);
1096: my $home=homeserver($uname,$udom);
1097: if ($home eq 'no_host') {
1098: return 'not_found';
1099: }
1100: my $answer=reply("sub:$fname",$home);
1101: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1102: $answer.=' by '.$home;
1103: }
1104: return $answer;
1105: }
1106:
1107: # -------------------------------------------------------------- Replicate file
1108:
1109: sub repcopy {
1110: my $filename=shift;
1111: $filename=~s/\/+/\//g;
1112: if ($filename=~m|^/home/httpd/html/adm/|) { return OK; }
1113: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return OK; }
1114: if ($filename=~m|^/home/httpd/html/userfiles/| or
1115: $filename=~m|^/*uploaded/|) {
1116: return &repcopy_userfile($filename);
1117: }
1118: $filename=~s/[\n\r]//g;
1119: my $transname="$filename.in.transfer";
1120: if ((-e $filename) || (-e $transname)) { return OK; }
1121: my $remoteurl=subscribe($filename);
1122: if ($remoteurl =~ /^con_lost by/) {
1123: &logthis("Subscribe returned $remoteurl: $filename");
1124: return HTTP_SERVICE_UNAVAILABLE;
1125: } elsif ($remoteurl eq 'not_found') {
1126: #&logthis("Subscribe returned not_found: $filename");
1127: return HTTP_NOT_FOUND;
1128: } elsif ($remoteurl =~ /^rejected by/) {
1129: &logthis("Subscribe returned $remoteurl: $filename");
1130: return FORBIDDEN;
1131: } elsif ($remoteurl eq 'directory') {
1132: return OK;
1133: } else {
1134: my $author=$filename;
1135: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1136: my ($udom,$uname)=split(/\//,$author);
1137: my $home=homeserver($uname,$udom);
1138: unless ($home eq $perlvar{'lonHostID'}) {
1139: my @parts=split(/\//,$filename);
1140: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1141: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1142: &logthis("Malconfiguration for replication: $filename");
1143: return HTTP_BAD_REQUEST;
1144: }
1145: my $count;
1146: for ($count=5;$count<$#parts;$count++) {
1147: $path.="/$parts[$count]";
1148: if ((-e $path)!=1) {
1149: mkdir($path,0777);
1150: }
1151: }
1152: my $ua=new LWP::UserAgent;
1153: my $request=new HTTP::Request('GET',"$remoteurl");
1154: my $response=$ua->request($request,$transname);
1155: if ($response->is_error()) {
1156: unlink($transname);
1157: my $message=$response->status_line;
1158: &logthis("<font color=blue>WARNING:"
1159: ." LWP get: $message: $filename</font>");
1160: return HTTP_SERVICE_UNAVAILABLE;
1161: } else {
1162: if ($remoteurl!~/\.meta$/) {
1163: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1164: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1165: if ($mresponse->is_error()) {
1166: unlink($filename.'.meta');
1167: &logthis(
1168: "<font color=yellow>INFO: No metadata: $filename</font>");
1169: }
1170: }
1171: rename($transname,$filename);
1172: return OK;
1173: }
1174: }
1175: }
1176: }
1177:
1178: # ------------------------------------------------ Get server side include body
1179: sub ssi_body {
1180: my ($filelink,%form)=@_;
1181: my $output=($filelink=~/^http\:/?&externalssi($filelink):
1182: &ssi($filelink,%form));
1183: $output=~
1184: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1185: $output=~s/^.*?\<body[^\>]*\>//si;
1186: $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1187: return $output;
1188: }
1189:
1190: # --------------------------------------------------------- Server Side Include
1191:
1192: sub ssi {
1193:
1194: my ($fn,%form)=@_;
1195:
1196: my $ua=new LWP::UserAgent;
1197:
1198: my $request;
1199:
1200: if (%form) {
1201: $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
1202: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1203: } else {
1204: $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
1205: }
1206:
1207: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1208: my $response=$ua->request($request);
1209:
1210: return $response->content;
1211: }
1212:
1213: sub externalssi {
1214: my ($url)=@_;
1215: my $ua=new LWP::UserAgent;
1216: my $request=new HTTP::Request('GET',$url);
1217: my $response=$ua->request($request);
1218: return $response->content;
1219: }
1220:
1221: # -------------------------------- Allow a /uploaded/ URI to be vouched for
1222:
1223: sub allowuploaded {
1224: my ($srcurl,$url)=@_;
1225: $url=&clutter(&declutter($url));
1226: my $dir=$url;
1227: $dir=~s/\/[^\/]+$//;
1228: my %httpref=();
1229: my $httpurl=&hreflocation('',$url);
1230: $httpref{'httpref.'.$httpurl}=$srcurl;
1231: &Apache::lonnet::appenv(%httpref);
1232: }
1233:
1234: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1235: # input: action, courseID, current domain, home server for course, intended
1236: # path to file, source of file.
1237: # output: url to file (if action was uploaddoc),
1238: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1239: #
1240: # Allows directory structure to be used within lonUsers/../userfiles/ for a
1241: # course.
1242: #
1243: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1244: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
1245: # course's home server.
1246: #
1247: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
1248: # be copied from $source (current location) to
1249: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1250: # and will then be copied to
1251: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
1252: # course's home server.
1253: #
1254: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1255: # will be retrived from $ENV{form.uploaddoc} (from DOCS interface) to
1256: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1257: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
1258: # in course's home server.
1259:
1260:
1261: sub process_coursefile {
1262: my ($action,$docuname,$docudom,$docuhome,$file,$source)=@_;
1263: my $fetchresult;
1264: if ($action eq 'propagate') {
1265: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file
1266: ,$docuhome);
1267: } else {
1268: my $fetchresult = '';
1269: my $fpath = '';
1270: my $fname = $file;
1271: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1272: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1273: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
1274: unless ($fpath eq '') {
1275: my @parts=split('/',$fpath);
1276: foreach my $part (@parts) {
1277: $filepath.= '/'.$part;
1278: if ((-e $filepath)!=1) {
1279: mkdir($filepath,0777);
1280: }
1281: }
1282: }
1283: if ($action eq 'copy') {
1284: if ($source eq '') {
1285: $fetchresult = 'no source file';
1286: return $fetchresult;
1287: } else {
1288: my $destination = $filepath.'/'.$fname;
1289: rename($source,$destination);
1290: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1291: $docuhome);
1292: }
1293: } elsif ($action eq 'uploaddoc') {
1294: open(my $fh,'>'.$filepath.'/'.$fname);
1295: print $fh $ENV{'form.'.$source};
1296: close($fh);
1297: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1298: $docuhome);
1299: if ($fetchresult eq 'ok') {
1300: return '/uploaded/'.$fpath.'/'.$fname;
1301: } else {
1302: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1303: ' to host '.$docuhome.': '.$fetchresult);
1304: return '/adm/notfound.html';
1305: }
1306: }
1307: }
1308: unless ( $fetchresult eq 'ok') {
1309: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1310: ' to host '.$docuhome.': '.$fetchresult);
1311: }
1312: return $fetchresult;
1313: }
1314:
1315: # --------------- Take an uploaded file and put it into the userfiles directory
1316: # input: name of form element, coursedoc=1 means this is for the course
1317: # output: url of file in userspace
1318:
1319: sub clean_filename {
1320: my ($fname)=@_;
1321: # Replace Windows backslashes by forward slashes
1322: $fname=~s/\\/\//g;
1323: # Get rid of everything but the actual filename
1324: $fname=~s/^.*\/([^\/]+)$/$1/;
1325: # Replace spaces by underscores
1326: $fname=~s/\s+/\_/g;
1327: # Replace all other weird characters by nothing
1328: $fname=~s/[^\w\.\-]//g;
1329: # Replace all .\d. sequences with _\d. so they no longer look like version
1330: # numbers
1331: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1332: return $fname;
1333: }
1334:
1335: sub userfileupload {
1336: my ($formname,$coursedoc,$subdir)=@_;
1337: if (!defined($subdir)) { $subdir='unknown'; }
1338: my $fname=$ENV{'form.'.$formname.'.filename'};
1339: $fname=&clean_filename($fname);
1340: # See if there is anything left
1341: unless ($fname) { return 'error: no uploaded file'; }
1342: chop($ENV{'form.'.$formname});
1343: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
1344: my $now = time;
1345: my $filepath = 'tmp/helprequests/'.$now;
1346: my @parts=split(/\//,$filepath);
1347: my $fullpath = $perlvar{'lonDaemons'};
1348: for (my $i=0;$i<@parts;$i++) {
1349: $fullpath .= '/'.$parts[$i];
1350: if ((-e $fullpath)!=1) {
1351: mkdir($fullpath,0777);
1352: }
1353: }
1354: open(my $fh,'>'.$fullpath.'/'.$fname);
1355: print $fh $ENV{'form.'.$formname};
1356: close($fh);
1357: return $fullpath.'/'.$fname;
1358: }
1359: # Create the directory if not present
1360: my $docuname='';
1361: my $docudom='';
1362: my $docuhome='';
1363: $fname="$subdir/$fname";
1364: if ($coursedoc) {
1365: $docuname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1366: $docudom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1367: $docuhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1368: if ($ENV{'form.folder'} =~ m/^default/) {
1369: return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
1370: } else {
1371: $fname=$ENV{'form.folder'}.'/'.$fname;
1372: return &process_coursefile('uploaddoc',$docuname,$docudom,$docuhome,$fname,$formname);
1373: }
1374: } else {
1375: $docuname=$ENV{'user.name'};
1376: $docudom=$ENV{'user.domain'};
1377: $docuhome=$ENV{'user.home'};
1378: return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
1379: }
1380: }
1381:
1382: sub finishuserfileupload {
1383: my ($docuname,$docudom,$docuhome,$formname,$fname)=@_;
1384: my $path=$docudom.'/'.$docuname.'/';
1385: my $filepath=$perlvar{'lonDocRoot'};
1386: my ($fnamepath,$file);
1387: $file=$fname;
1388: if ($fname=~m|/|) {
1389: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
1390: $path.=$fnamepath.'/';
1391: }
1392: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1393: my $count;
1394: for ($count=4;$count<=$#parts;$count++) {
1395: $filepath.="/$parts[$count]";
1396: if ((-e $filepath)!=1) {
1397: mkdir($filepath,0777);
1398: }
1399: }
1400: # Save the file
1401: {
1402: #&Apache::lonnet::logthis("Saving to $filepath $file");
1403: open(my $fh,'>'.$filepath.'/'.$file);
1404: print $fh $ENV{'form.'.$formname};
1405: close($fh);
1406: }
1407: # Notify homeserver to grep it
1408: #
1409: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1410: if ($fetchresult eq 'ok') {
1411: #
1412: # Return the URL to it
1413: return '/uploaded/'.$path.$file;
1414: } else {
1415: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
1416: ': '.$fetchresult);
1417: return '/adm/notfound.html';
1418: }
1419: }
1420:
1421: sub removeuploadedurl {
1422: my ($url)=@_;
1423: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1424: return &Apache::lonnet::removeuserfile($uname,$udom,$fname);
1425: }
1426:
1427: sub removeuserfile {
1428: my ($docuname,$docudom,$fname)=@_;
1429: my $home=&homeserver($docuname,$docudom);
1430: return &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1431: }
1432:
1433: sub mkdiruserfile {
1434: my ($docuname,$docudom,$dir)=@_;
1435: my $home=&homeserver($docuname,$docudom);
1436: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
1437: }
1438:
1439: sub renameuserfile {
1440: my ($docuname,$docudom,$old,$new)=@_;
1441: my $home=&homeserver($docuname,$docudom);
1442: return &reply("renameuserfile:$docudom:$docuname:".&escape("$old").':'.
1443: &escape("$new"),$home);
1444: }
1445:
1446: # ------------------------------------------------------------------------- Log
1447:
1448: sub log {
1449: my ($dom,$nam,$hom,$what)=@_;
1450: return critical("log:$dom:$nam:$what",$hom);
1451: }
1452:
1453: # ------------------------------------------------------------------ Course Log
1454: #
1455: # This routine flushes several buffers of non-mission-critical nature
1456: #
1457:
1458: sub flushcourselogs {
1459: &logthis('Flushing log buffers');
1460: #
1461: # course logs
1462: # This is a log of all transactions in a course, which can be used
1463: # for data mining purposes
1464: #
1465: # It also collects the courseid database, which lists last transaction
1466: # times and course titles for all courseids
1467: #
1468: my %courseidbuffer=();
1469: foreach (keys %courselogs) {
1470: my $crsid=$_;
1471: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1472: &escape($courselogs{$crsid}),
1473: $coursehombuf{$crsid}) eq 'ok') {
1474: delete $courselogs{$crsid};
1475: } else {
1476: &logthis('Failed to flush log buffer for '.$crsid);
1477: if (length($courselogs{$crsid})>40000) {
1478: &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
1479: " exceeded maximum size, deleting.</font>");
1480: delete $courselogs{$crsid};
1481: }
1482: }
1483: if ($courseidbuffer{$coursehombuf{$crsid}}) {
1484: $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1485: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1486: '='.&escape($courseinstcodebuf{$crsid});
1487: } else {
1488: $courseidbuffer{$coursehombuf{$crsid}}=
1489: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1490: '='.&escape($courseinstcodebuf{$crsid});
1491: }
1492: }
1493: #
1494: # Write course id database (reverse lookup) to homeserver of courses
1495: # Is used in pickcourse
1496: #
1497: foreach (keys %courseidbuffer) {
1498: &courseidput($hostdom{$_},$courseidbuffer{$_},$_);
1499: }
1500: #
1501: # File accesses
1502: # Writes to the dynamic metadata of resources to get hit counts, etc.
1503: #
1504: foreach my $entry (keys(%accesshash)) {
1505: if ($entry =~ /___count$/) {
1506: my ($dom,$name);
1507: ($dom,$name,undef)=($entry=~m:___(\w+)/(\w+)/(.*)___count$:);
1508: if (! defined($dom) || $dom eq '' ||
1509: ! defined($name) || $name eq '') {
1510: my $cid = $ENV{'request.course.id'};
1511: $dom = $ENV{'request.'.$cid.'.domain'};
1512: $name = $ENV{'request.'.$cid.'.num'};
1513: }
1514: my $value = $accesshash{$entry};
1515: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
1516: my %temphash=($url => $value);
1517: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
1518: if ($result eq 'ok') {
1519: delete $accesshash{$entry};
1520: } elsif ($result eq 'unknown_cmd') {
1521: # Target server has old code running on it.
1522: my %temphash=($entry => $value);
1523: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1524: delete $accesshash{$entry};
1525: }
1526: }
1527: } else {
1528: my ($dom,$name) = ($entry=~m:___(\w+)/(\w+)/(.*)___(\w+)$:);
1529: my %temphash=($entry => $accesshash{$entry});
1530: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1531: delete $accesshash{$entry};
1532: }
1533: }
1534: }
1535: #
1536: # Roles
1537: # Reverse lookup of user roles for course faculty/staff and co-authorship
1538: #
1539: foreach (keys %userrolehash) {
1540: my $entry=$_;
1541: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1542: split(/\:/,$entry);
1543: if (&Apache::lonnet::put('nohist_userroles',
1544: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1545: $rudom,$runame) eq 'ok') {
1546: delete $userrolehash{$entry};
1547: }
1548: }
1549: $dumpcount++;
1550: }
1551:
1552: sub courselog {
1553: my $what=shift;
1554: $what=time.':'.$what;
1555: unless ($ENV{'request.course.id'}) { return ''; }
1556: $coursedombuf{$ENV{'request.course.id'}}=
1557: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1558: $coursenumbuf{$ENV{'request.course.id'}}=
1559: $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1560: $coursehombuf{$ENV{'request.course.id'}}=
1561: $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1562: $coursedescrbuf{$ENV{'request.course.id'}}=
1563: $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1564: $courseinstcodebuf{$ENV{'request.course.id'}}=
1565: $ENV{'course.'.$ENV{'request.course.id'}.'.internal.coursecode'};
1566: if (defined $courselogs{$ENV{'request.course.id'}}) {
1567: $courselogs{$ENV{'request.course.id'}}.='&'.$what;
1568: } else {
1569: $courselogs{$ENV{'request.course.id'}}.=$what;
1570: }
1571: if (length($courselogs{$ENV{'request.course.id'}})>4048) {
1572: &flushcourselogs();
1573: }
1574: }
1575:
1576: sub courseacclog {
1577: my $fnsymb=shift;
1578: unless ($ENV{'request.course.id'}) { return ''; }
1579: my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
1580: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|page)$/) {
1581: $what.=':POST';
1582: foreach (keys %ENV) {
1583: if ($_=~/^form\.(.*)/) {
1584: $what.=':'.$1.'='.$ENV{$_};
1585: }
1586: }
1587: }
1588: &courselog($what);
1589: }
1590:
1591: sub countacc {
1592: my $url=&declutter(shift);
1593: return if (! defined($url) || $url eq '');
1594: unless ($ENV{'request.course.id'}) { return ''; }
1595: $accesshash{$ENV{'request.course.id'}.'___'.$url.'___course'}=1;
1596: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1597: $accesshash{$key}++;
1598: }
1599:
1600: sub linklog {
1601: my ($from,$to)=@_;
1602: $from=&declutter($from);
1603: $to=&declutter($to);
1604: $accesshash{$from.'___'.$to.'___comefrom'}=1;
1605: $accesshash{$to.'___'.$from.'___goto'}=1;
1606: }
1607:
1608: sub userrolelog {
1609: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1610: if (($trole=~/^ca/) || ($trole=~/^in/) ||
1611: ($trole=~/^cc/) || ($trole=~/^ep/) ||
1612: ($trole=~/^cr/) || ($trole=~/^ta/)) {
1613: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
1614: $userrolehash
1615: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1616: =$tend.':'.$tstart;
1617: }
1618: }
1619:
1620: sub get_course_adv_roles {
1621: my $cid=shift;
1622: $cid=$ENV{'request.course.id'} unless (defined($cid));
1623: my %coursehash=&coursedescription($cid);
1624: my %nothide=();
1625: foreach (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1626: $nothide{join(':',split(/[\@\:]/,$_))}=1;
1627: }
1628: my %returnhash=();
1629: my %dumphash=
1630: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
1631: my $now=time;
1632: foreach (keys %dumphash) {
1633: my ($tend,$tstart)=split(/\:/,$dumphash{$_});
1634: if (($tstart) && ($tstart<0)) { next; }
1635: if (($tend) && ($tend<$now)) { next; }
1636: if (($tstart) && ($now<$tstart)) { next; }
1637: my ($role,$username,$domain,$section)=split(/\:/,$_);
1638: if ((&privileged($username,$domain)) &&
1639: (!$nothide{$username.':'.$domain})) { next; }
1640: my $key=&plaintext($role);
1641: if ($section) { $key.=' (Sec/Grp '.$section.')'; }
1642: if ($returnhash{$key}) {
1643: $returnhash{$key}.=','.$username.':'.$domain;
1644: } else {
1645: $returnhash{$key}=$username.':'.$domain;
1646: }
1647: }
1648: return %returnhash;
1649: }
1650:
1651: sub get_my_roles {
1652: my ($uname,$udom)=@_;
1653: unless (defined($uname)) { $uname=$ENV{'user.name'}; }
1654: unless (defined($udom)) { $udom=$ENV{'user.domain'}; }
1655: my %dumphash=
1656: &dump('nohist_userroles',$udom,$uname);
1657: my %returnhash=();
1658: my $now=time;
1659: foreach (keys %dumphash) {
1660: my ($tend,$tstart)=split(/\:/,$dumphash{$_});
1661: if (($tstart) && ($tstart<0)) { next; }
1662: if (($tend) && ($tend<$now)) { next; }
1663: if (($tstart) && ($now<$tstart)) { next; }
1664: my ($role,$username,$domain,$section)=split(/\:/,$_);
1665: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1666: }
1667: return %returnhash;
1668: }
1669:
1670: # ----------------------------------------------------- Frontpage Announcements
1671: #
1672: #
1673:
1674: sub postannounce {
1675: my ($server,$text)=@_;
1676: unless (&allowed('psa',$hostdom{$server})) { return 'refused'; }
1677: unless ($text=~/\w/) { $text=''; }
1678: return &reply('setannounce:'.&escape($text),$server);
1679: }
1680:
1681: sub getannounce {
1682:
1683: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1684: my $announcement='';
1685: while (<$fh>) { $announcement .=$_; }
1686: close($fh);
1687: if ($announcement=~/\w/) {
1688: return
1689: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1690: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1691: } else {
1692: return '';
1693: }
1694: } else {
1695: return '';
1696: }
1697: }
1698:
1699: # ---------------------------------------------------------- Course ID routines
1700: # Deal with domain's nohist_courseid.db files
1701: #
1702:
1703: sub courseidput {
1704: my ($domain,$what,$coursehome)=@_;
1705: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
1706: }
1707:
1708: sub courseiddump {
1709: my ($domfilter,$descfilter,$sincefilter,$hostidflag,$hostidref)=@_;
1710: my %returnhash=();
1711: unless ($domfilter) { $domfilter=''; }
1712: foreach my $tryserver (keys %libserv) {
1713: if ( ($hostidflag == 1 && grep/^$tryserver$/,@{$hostidref}) || (!defined($hostidflag)) ) {
1714: if ((!$domfilter) || ($hostdom{$tryserver} eq $domfilter)) {
1715: foreach (
1716: split(/\&/,&reply('courseiddump:'.$hostdom{$tryserver}.':'.
1717: $sincefilter.':'.&escape($descfilter),
1718: $tryserver))) {
1719: my ($key,$value)=split(/\=/,$_);
1720: if (($key) && ($value)) {
1721: $returnhash{&unescape($key)}=$value;
1722: }
1723: }
1724: }
1725: }
1726: }
1727: return %returnhash;
1728: }
1729:
1730: #
1731: # ----------------------------------------------------------- Check out an item
1732:
1733: sub get_first_access {
1734: my ($type,$argsymb)=@_;
1735: my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1736: if ($argsymb) { $symb=$argsymb; }
1737: my ($map,$id,$res)=&decode_symb($symb);
1738: if ($type eq 'map') { $res=$map; }
1739: my %times=&get('firstaccesstimes',[$res],$udom,$uname);
1740: return $times{$res};
1741: }
1742:
1743: sub set_first_access {
1744: my ($type)=@_;
1745: my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1746: my ($map,$id,$res)=&decode_symb($symb);
1747: if ($type eq 'map') { $res=$map; }
1748: my $firstaccess=&get_first_access($type);
1749: if (!$firstaccess) {
1750: return &put('firstaccesstimes',{$res=>time},$udom,$uname);
1751: }
1752: return 'already_set';
1753: }
1754:
1755: sub checkout {
1756: my ($symb,$tuname,$tudom,$tcrsid)=@_;
1757: my $now=time;
1758: my $lonhost=$perlvar{'lonHostID'};
1759: my $infostr=&escape(
1760: 'CHECKOUTTOKEN&'.
1761: $tuname.'&'.
1762: $tudom.'&'.
1763: $tcrsid.'&'.
1764: $symb.'&'.
1765: $now.'&'.$ENV{'REMOTE_ADDR'});
1766: my $token=&reply('tmpput:'.$infostr,$lonhost);
1767: if ($token=~/^error\:/) {
1768: &logthis("<font color=blue>WARNING: ".
1769: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
1770: "</font>");
1771: return '';
1772: }
1773:
1774: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
1775: $token=~tr/a-z/A-Z/;
1776:
1777: my %infohash=('resource.0.outtoken' => $token,
1778: 'resource.0.checkouttime' => $now,
1779: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1780:
1781: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
1782: return '';
1783: } else {
1784: &logthis("<font color=blue>WARNING: ".
1785: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
1786: "</font>");
1787: }
1788:
1789: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
1790: &escape('Checkout '.$infostr.' - '.
1791: $token)) ne 'ok') {
1792: return '';
1793: } else {
1794: &logthis("<font color=blue>WARNING: ".
1795: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
1796: "</font>");
1797: }
1798: return $token;
1799: }
1800:
1801: # ------------------------------------------------------------ Check in an item
1802:
1803: sub checkin {
1804: my $token=shift;
1805: my $now=time;
1806: my ($ta,$tb,$lonhost)=split(/\*/,$token);
1807: $lonhost=~tr/A-Z/a-z/;
1808: my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
1809: $dtoken=~s/\W/\_/g;
1810: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1811: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
1812:
1813: unless (($tuname) && ($tudom)) {
1814: &logthis('Check in '.$token.' ('.$dtoken.') failed');
1815: return '';
1816: }
1817:
1818: unless (&allowed('mgr',$tcrsid)) {
1819: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1820: $ENV{'user.name'}.' - '.$ENV{'user.domain'});
1821: return '';
1822: }
1823:
1824: my %infohash=('resource.0.intoken' => $token,
1825: 'resource.0.checkintime' => $now,
1826: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1827:
1828: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
1829: return '';
1830: }
1831:
1832: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
1833: &escape('Checkin - '.$token)) ne 'ok') {
1834: return '';
1835: }
1836:
1837: return ($symb,$tuname,$tudom,$tcrsid);
1838: }
1839:
1840: # --------------------------------------------- Set Expire Date for Spreadsheet
1841:
1842: sub expirespread {
1843: my ($uname,$udom,$stype,$usymb)=@_;
1844: my $cid=$ENV{'request.course.id'};
1845: if ($cid) {
1846: my $now=time;
1847: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1848: return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1849: $ENV{'course.'.$cid.'.num'}.
1850: ':nohist_expirationdates:'.
1851: &escape($key).'='.$now,
1852: $ENV{'course.'.$cid.'.home'})
1853: }
1854: return 'ok';
1855: }
1856:
1857: # ----------------------------------------------------- Devalidate Spreadsheets
1858:
1859: sub devalidate {
1860: my ($symb,$uname,$udom)=@_;
1861: my $cid=$ENV{'request.course.id'};
1862: if ($cid) {
1863: # delete the stored spreadsheets for
1864: # - the student level sheet of this user in course's homespace
1865: # - the assessment level sheet for this resource
1866: # for this user in user's homespace
1867: my $key=$uname.':'.$udom.':';
1868: my $status=
1869: &del('nohist_calculatedsheets',
1870: [$key.'studentcalc:'],
1871: $ENV{'course.'.$cid.'.domain'},
1872: $ENV{'course.'.$cid.'.num'})
1873: .' '.
1874: &del('nohist_calculatedsheets_'.$cid,
1875: [$key.'assesscalc:'.$symb],$udom,$uname);
1876: unless ($status eq 'ok ok') {
1877: &logthis('Could not devalidate spreadsheet '.
1878: $uname.' at '.$udom.' for '.
1879: $symb.': '.$status);
1880: }
1881: }
1882: }
1883:
1884: sub get_scalar {
1885: my ($string,$end) = @_;
1886: my $value;
1887: if ($$string =~ s/^([^&]*?)($end)/$2/) {
1888: $value = $1;
1889: } elsif ($$string =~ s/^([^&]*?)&//) {
1890: $value = $1;
1891: }
1892: return &unescape($value);
1893: }
1894:
1895: sub array2str {
1896: my (@array) = @_;
1897: my $result=&arrayref2str(\@array);
1898: $result=~s/^__ARRAY_REF__//;
1899: $result=~s/__END_ARRAY_REF__$//;
1900: return $result;
1901: }
1902:
1903: sub arrayref2str {
1904: my ($arrayref) = @_;
1905: my $result='__ARRAY_REF__';
1906: foreach my $elem (@$arrayref) {
1907: if(ref($elem) eq 'ARRAY') {
1908: $result.=&arrayref2str($elem).'&';
1909: } elsif(ref($elem) eq 'HASH') {
1910: $result.=&hashref2str($elem).'&';
1911: } elsif(ref($elem)) {
1912: #print("Got a ref of ".(ref($elem))." skipping.");
1913: } else {
1914: $result.=&escape($elem).'&';
1915: }
1916: }
1917: $result=~s/\&$//;
1918: $result .= '__END_ARRAY_REF__';
1919: return $result;
1920: }
1921:
1922: sub hash2str {
1923: my (%hash) = @_;
1924: my $result=&hashref2str(\%hash);
1925: $result=~s/^__HASH_REF__//;
1926: $result=~s/__END_HASH_REF__$//;
1927: return $result;
1928: }
1929:
1930: sub hashref2str {
1931: my ($hashref)=@_;
1932: my $result='__HASH_REF__';
1933: foreach (sort(keys(%$hashref))) {
1934: if (ref($_) eq 'ARRAY') {
1935: $result.=&arrayref2str($_).'=';
1936: } elsif (ref($_) eq 'HASH') {
1937: $result.=&hashref2str($_).'=';
1938: } elsif (ref($_)) {
1939: $result.='=';
1940: #print("Got a ref of ".(ref($_))." skipping.");
1941: } else {
1942: if ($_) {$result.=&escape($_).'=';} else { last; }
1943: }
1944:
1945: if(ref($hashref->{$_}) eq 'ARRAY') {
1946: $result.=&arrayref2str($hashref->{$_}).'&';
1947: } elsif(ref($hashref->{$_}) eq 'HASH') {
1948: $result.=&hashref2str($hashref->{$_}).'&';
1949: } elsif(ref($hashref->{$_})) {
1950: $result.='&';
1951: #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
1952: } else {
1953: $result.=&escape($hashref->{$_}).'&';
1954: }
1955: }
1956: $result=~s/\&$//;
1957: $result .= '__END_HASH_REF__';
1958: return $result;
1959: }
1960:
1961: sub str2hash {
1962: my ($string)=@_;
1963: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
1964: return %$hash;
1965: }
1966:
1967: sub str2hashref {
1968: my ($string) = @_;
1969:
1970: my %hash;
1971:
1972: if($string !~ /^__HASH_REF__/) {
1973: if (! ($string eq '' || !defined($string))) {
1974: $hash{'error'}='Not hash reference';
1975: }
1976: return (\%hash, $string);
1977: }
1978:
1979: $string =~ s/^__HASH_REF__//;
1980:
1981: while($string !~ /^__END_HASH_REF__/) {
1982: #key
1983: my $key='';
1984: if($string =~ /^__HASH_REF__/) {
1985: ($key, $string)=&str2hashref($string);
1986: if(defined($key->{'error'})) {
1987: $hash{'error'}='Bad data';
1988: return (\%hash, $string);
1989: }
1990: } elsif($string =~ /^__ARRAY_REF__/) {
1991: ($key, $string)=&str2arrayref($string);
1992: if($key->[0] eq 'Array reference error') {
1993: $hash{'error'}='Bad data';
1994: return (\%hash, $string);
1995: }
1996: } else {
1997: $string =~ s/^(.*?)=//;
1998: $key=&unescape($1);
1999: }
2000: $string =~ s/^=//;
2001:
2002: #value
2003: my $value='';
2004: if($string =~ /^__HASH_REF__/) {
2005: ($value, $string)=&str2hashref($string);
2006: if(defined($value->{'error'})) {
2007: $hash{'error'}='Bad data';
2008: return (\%hash, $string);
2009: }
2010: } elsif($string =~ /^__ARRAY_REF__/) {
2011: ($value, $string)=&str2arrayref($string);
2012: if($value->[0] eq 'Array reference error') {
2013: $hash{'error'}='Bad data';
2014: return (\%hash, $string);
2015: }
2016: } else {
2017: $value=&get_scalar(\$string,'__END_HASH_REF__');
2018: }
2019: $string =~ s/^&//;
2020:
2021: $hash{$key}=$value;
2022: }
2023:
2024: $string =~ s/^__END_HASH_REF__//;
2025:
2026: return (\%hash, $string);
2027: }
2028:
2029: sub str2array {
2030: my ($string)=@_;
2031: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
2032: return @$array;
2033: }
2034:
2035: sub str2arrayref {
2036: my ($string) = @_;
2037: my @array;
2038:
2039: if($string !~ /^__ARRAY_REF__/) {
2040: if (! ($string eq '' || !defined($string))) {
2041: $array[0]='Array reference error';
2042: }
2043: return (\@array, $string);
2044: }
2045:
2046: $string =~ s/^__ARRAY_REF__//;
2047:
2048: while($string !~ /^__END_ARRAY_REF__/) {
2049: my $value='';
2050: if($string =~ /^__HASH_REF__/) {
2051: ($value, $string)=&str2hashref($string);
2052: if(defined($value->{'error'})) {
2053: $array[0] ='Array reference error';
2054: return (\@array, $string);
2055: }
2056: } elsif($string =~ /^__ARRAY_REF__/) {
2057: ($value, $string)=&str2arrayref($string);
2058: if($value->[0] eq 'Array reference error') {
2059: $array[0] ='Array reference error';
2060: return (\@array, $string);
2061: }
2062: } else {
2063: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
2064: }
2065: $string =~ s/^&//;
2066:
2067: push(@array, $value);
2068: }
2069:
2070: $string =~ s/^__END_ARRAY_REF__//;
2071:
2072: return (\@array, $string);
2073: }
2074:
2075: # -------------------------------------------------------------------Temp Store
2076:
2077: sub tmpreset {
2078: my ($symb,$namespace,$domain,$stuname) = @_;
2079: if (!$symb) {
2080: $symb=&symbread();
2081: if (!$symb) { $symb= $ENV{'request.url'}; }
2082: }
2083: $symb=escape($symb);
2084:
2085: if (!$namespace) { $namespace=$ENV{'request.state'}; }
2086: $namespace=~s/\//\_/g;
2087: $namespace=~s/\W//g;
2088:
2089: #FIXME needs to do something for /pub resources
2090: if (!$domain) { $domain=$ENV{'user.domain'}; }
2091: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2092: my $path=$perlvar{'lonDaemons'}.'/tmp';
2093: my %hash;
2094: if (tie(%hash,'GDBM_File',
2095: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2096: &GDBM_WRCREAT(),0640)) {
2097: foreach my $key (keys %hash) {
2098: if ($key=~ /:$symb/) {
2099: delete($hash{$key});
2100: }
2101: }
2102: }
2103: }
2104:
2105: sub tmpstore {
2106: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2107:
2108: if (!$symb) {
2109: $symb=&symbread();
2110: if (!$symb) { $symb= $ENV{'request.url'}; }
2111: }
2112: $symb=escape($symb);
2113:
2114: if (!$namespace) {
2115: # I don't think we would ever want to store this for a course.
2116: # it seems this will only be used if we don't have a course.
2117: #$namespace=$ENV{'request.course.id'};
2118: #if (!$namespace) {
2119: $namespace=$ENV{'request.state'};
2120: #}
2121: }
2122: $namespace=~s/\//\_/g;
2123: $namespace=~s/\W//g;
2124: #FIXME needs to do something for /pub resources
2125: if (!$domain) { $domain=$ENV{'user.domain'}; }
2126: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2127: my $now=time;
2128: my %hash;
2129: my $path=$perlvar{'lonDaemons'}.'/tmp';
2130: if (tie(%hash,'GDBM_File',
2131: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2132: &GDBM_WRCREAT(),0640)) {
2133: $hash{"version:$symb"}++;
2134: my $version=$hash{"version:$symb"};
2135: my $allkeys='';
2136: foreach my $key (keys(%$storehash)) {
2137: $allkeys.=$key.':';
2138: $hash{"$version:$symb:$key"}=$$storehash{$key};
2139: }
2140: $hash{"$version:$symb:timestamp"}=$now;
2141: $allkeys.='timestamp';
2142: $hash{"$version:keys:$symb"}=$allkeys;
2143: if (untie(%hash)) {
2144: return 'ok';
2145: } else {
2146: return "error:$!";
2147: }
2148: } else {
2149: return "error:$!";
2150: }
2151: }
2152:
2153: # -----------------------------------------------------------------Temp Restore
2154:
2155: sub tmprestore {
2156: my ($symb,$namespace,$domain,$stuname) = @_;
2157:
2158: if (!$symb) {
2159: $symb=&symbread();
2160: if (!$symb) { $symb= $ENV{'request.url'}; }
2161: }
2162: $symb=escape($symb);
2163:
2164: if (!$namespace) { $namespace=$ENV{'request.state'}; }
2165: #FIXME needs to do something for /pub resources
2166: if (!$domain) { $domain=$ENV{'user.domain'}; }
2167: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2168:
2169: my %returnhash;
2170: $namespace=~s/\//\_/g;
2171: $namespace=~s/\W//g;
2172: my %hash;
2173: my $path=$perlvar{'lonDaemons'}.'/tmp';
2174: if (tie(%hash,'GDBM_File',
2175: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2176: &GDBM_READER(),0640)) {
2177: my $version=$hash{"version:$symb"};
2178: $returnhash{'version'}=$version;
2179: my $scope;
2180: for ($scope=1;$scope<=$version;$scope++) {
2181: my $vkeys=$hash{"$scope:keys:$symb"};
2182: my @keys=split(/:/,$vkeys);
2183: my $key;
2184: $returnhash{"$scope:keys"}=$vkeys;
2185: foreach $key (@keys) {
2186: $returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
2187: $returnhash{"$key"}=$hash{"$scope:$symb:$key"};
2188: }
2189: }
2190: if (!(untie(%hash))) {
2191: return "error:$!";
2192: }
2193: } else {
2194: return "error:$!";
2195: }
2196: return %returnhash;
2197: }
2198:
2199: # ----------------------------------------------------------------------- Store
2200:
2201: sub store {
2202: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2203: my $home='';
2204:
2205: if ($stuname) { $home=&homeserver($stuname,$domain); }
2206:
2207: $symb=&symbclean($symb);
2208: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
2209:
2210: if (!$domain) { $domain=$ENV{'user.domain'}; }
2211: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2212:
2213: &devalidate($symb,$stuname,$domain);
2214:
2215: $symb=escape($symb);
2216: if (!$namespace) {
2217: unless ($namespace=$ENV{'request.course.id'}) {
2218: return '';
2219: }
2220: }
2221: if (!$home) { $home=$ENV{'user.home'}; }
2222:
2223: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2224: $$storehash{'host'}=$perlvar{'lonHostID'};
2225:
2226: my $namevalue='';
2227: foreach (keys %$storehash) {
2228: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
2229: }
2230: $namevalue=~s/\&$//;
2231: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
2232: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
2233: }
2234:
2235: # -------------------------------------------------------------- Critical Store
2236:
2237: sub cstore {
2238: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2239: my $home='';
2240:
2241: if ($stuname) { $home=&homeserver($stuname,$domain); }
2242:
2243: $symb=&symbclean($symb);
2244: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
2245:
2246: if (!$domain) { $domain=$ENV{'user.domain'}; }
2247: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2248:
2249: &devalidate($symb,$stuname,$domain);
2250:
2251: $symb=escape($symb);
2252: if (!$namespace) {
2253: unless ($namespace=$ENV{'request.course.id'}) {
2254: return '';
2255: }
2256: }
2257: if (!$home) { $home=$ENV{'user.home'}; }
2258:
2259: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2260: $$storehash{'host'}=$perlvar{'lonHostID'};
2261:
2262: my $namevalue='';
2263: foreach (keys %$storehash) {
2264: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
2265: }
2266: $namevalue=~s/\&$//;
2267: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
2268: return critical
2269: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
2270: }
2271:
2272: # --------------------------------------------------------------------- Restore
2273:
2274: sub restore {
2275: my ($symb,$namespace,$domain,$stuname) = @_;
2276: my $home='';
2277:
2278: if ($stuname) { $home=&homeserver($stuname,$domain); }
2279:
2280: if (!$symb) {
2281: unless ($symb=escape(&symbread())) { return ''; }
2282: } else {
2283: $symb=&escape(&symbclean($symb));
2284: }
2285: if (!$namespace) {
2286: unless ($namespace=$ENV{'request.course.id'}) {
2287: return '';
2288: }
2289: }
2290: if (!$domain) { $domain=$ENV{'user.domain'}; }
2291: if (!$stuname) { $stuname=$ENV{'user.name'}; }
2292: if (!$home) { $home=$ENV{'user.home'}; }
2293: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
2294:
2295: my %returnhash=();
2296: foreach (split(/\&/,$answer)) {
2297: my ($name,$value)=split(/\=/,$_);
2298: $returnhash{&unescape($name)}=&unescape($value);
2299: }
2300: my $version;
2301: for ($version=1;$version<=$returnhash{'version'};$version++) {
2302: foreach (split(/\:/,$returnhash{$version.':keys'})) {
2303: $returnhash{$_}=$returnhash{$version.':'.$_};
2304: }
2305: }
2306: return %returnhash;
2307: }
2308:
2309: # ---------------------------------------------------------- Course Description
2310:
2311: sub coursedescription {
2312: my $courseid=shift;
2313: $courseid=~s/^\///;
2314: $courseid=~s/\_/\//g;
2315: my ($cdomain,$cnum)=split(/\//,$courseid);
2316: my $chome=&homeserver($cnum,$cdomain);
2317: my $normalid=$cdomain.'_'.$cnum;
2318: # need to always cache even if we get errors otherwise we keep
2319: # trying and trying and trying to get the course description.
2320: my %envhash=();
2321: my %returnhash=();
2322: $envhash{'course.'.$normalid.'.last_cache'}=time;
2323: if ($chome ne 'no_host') {
2324: %returnhash=&dump('environment',$cdomain,$cnum);
2325: if (!exists($returnhash{'con_lost'})) {
2326: $returnhash{'home'}= $chome;
2327: $returnhash{'domain'} = $cdomain;
2328: $returnhash{'num'} = $cnum;
2329: while (my ($name,$value) = each %returnhash) {
2330: $envhash{'course.'.$normalid.'.'.$name}=$value;
2331: }
2332: $returnhash{'url'}=&clutter($returnhash{'url'});
2333: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
2334: $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
2335: $envhash{'course.'.$normalid.'.home'}=$chome;
2336: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
2337: $envhash{'course.'.$normalid.'.num'}=$cnum;
2338: }
2339: }
2340: &appenv(%envhash);
2341: return %returnhash;
2342: }
2343:
2344: # -------------------------------------------------See if a user is privileged
2345:
2346: sub privileged {
2347: my ($username,$domain)=@_;
2348: my $rolesdump=&reply("dump:$domain:$username:roles",
2349: &homeserver($username,$domain));
2350: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
2351: my $now=time;
2352: if ($rolesdump ne '') {
2353: foreach (split(/&/,$rolesdump)) {
2354: if ($_!~/^rolesdef\&/) {
2355: my ($area,$role)=split(/=/,$_);
2356: $area=~s/\_\w\w$//;
2357: my ($trole,$tend,$tstart)=split(/_/,$role);
2358: if (($trole eq 'dc') || ($trole eq 'su')) {
2359: my $active=1;
2360: if ($tend) {
2361: if ($tend<$now) { $active=0; }
2362: }
2363: if ($tstart) {
2364: if ($tstart>$now) { $active=0; }
2365: }
2366: if ($active) { return 1; }
2367: }
2368: }
2369: }
2370: }
2371: return 0;
2372: }
2373:
2374: # -------------------------------------------------------- Get user privileges
2375:
2376: sub rolesinit {
2377: my ($domain,$username,$authhost)=@_;
2378: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
2379: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
2380: my %allroles=();
2381: my %thesepriv=();
2382: my $now=time;
2383: my $userroles="user.login.time=$now\n";
2384: my $thesestr;
2385:
2386: if ($rolesdump ne '') {
2387: foreach (split(/&/,$rolesdump)) {
2388: if ($_!~/^rolesdef\&/) {
2389: my ($area,$role)=split(/=/,$_);
2390: $area=~s/\_\w\w$//;
2391: my ($trole,$tend,$tstart)=split(/_/,$role);
2392: $userroles.='user.role.'.$trole.'.'.$area.'='.
2393: $tstart.'.'.$tend."\n";
2394: # log the associated role with the area
2395: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
2396: if ($tend!=0) {
2397: if ($tend<$now) {
2398: $trole='';
2399: }
2400: }
2401: if ($tstart!=0) {
2402: if ($tstart>$now) {
2403: $trole='';
2404: }
2405: }
2406: if (($area ne '') && ($trole ne '')) {
2407: my $spec=$trole.'.'.$area;
2408: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
2409: if ($trole =~ /^cr\//) {
2410: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
2411: my $homsvr=homeserver($rauthor,$rdomain);
2412: if ($hostname{$homsvr} ne '') {
2413: my ($rdummy,$roledef)=
2414: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
2415:
2416: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
2417: my ($syspriv,$dompriv,$coursepriv)=
2418: split(/\_/,$roledef);
2419: if (defined($syspriv)) {
2420: $allroles{'cm./'}.=':'.$syspriv;
2421: $allroles{$spec.'./'}.=':'.$syspriv;
2422: }
2423: if ($tdomain ne '') {
2424: if (defined($dompriv)) {
2425: $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
2426: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
2427: }
2428: if ($trest ne '') {
2429: if (defined($coursepriv)) {
2430: $allroles{'cm.'.$area}.=':'.$coursepriv;
2431: $allroles{$spec.'.'.$area}.=':'.$coursepriv;
2432: }
2433: }
2434: }
2435: }
2436: }
2437: } else {
2438: if (defined($pr{$trole.':s'})) {
2439: $allroles{'cm./'}.=':'.$pr{$trole.':s'};
2440: $allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
2441: }
2442: if ($tdomain ne '') {
2443: if (defined($pr{$trole.':d'})) {
2444: $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
2445: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
2446: }
2447: if ($trest ne '') {
2448: if (defined($pr{$trole.':c'})) {
2449: $allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
2450: $allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
2451: }
2452: }
2453: }
2454: }
2455: }
2456: }
2457: }
2458: my $adv=0;
2459: my $author=0;
2460: foreach (keys %allroles) {
2461: %thesepriv=();
2462: if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
2463: if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
2464: foreach (split(/:/,$allroles{$_})) {
2465: if ($_ ne '') {
2466: my ($privilege,$restrictions)=split(/&/,$_);
2467: if ($restrictions eq '') {
2468: $thesepriv{$privilege}='F';
2469: } else {
2470: if ($thesepriv{$privilege} ne 'F') {
2471: $thesepriv{$privilege}.=$restrictions;
2472: }
2473: }
2474: }
2475: }
2476: $thesestr='';
2477: foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
2478: $userroles.='user.priv.'.$_.'='.$thesestr."\n";
2479: }
2480: $userroles.='user.adv='.$adv."\n".
2481: 'user.author='.$author."\n";
2482: $ENV{'user.adv'}=$adv;
2483: }
2484: return $userroles;
2485: }
2486:
2487: # --------------------------------------------------------------- get interface
2488:
2489: sub get {
2490: my ($namespace,$storearr,$udomain,$uname)=@_;
2491: my $items='';
2492: foreach (@$storearr) {
2493: $items.=escape($_).'&';
2494: }
2495: $items=~s/\&$//;
2496: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2497: if (!$uname) { $uname=$ENV{'user.name'}; }
2498: my $uhome=&homeserver($uname,$udomain);
2499:
2500: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
2501: my @pairs=split(/\&/,$rep);
2502: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
2503: return @pairs;
2504: }
2505: my %returnhash=();
2506: my $i=0;
2507: foreach (@$storearr) {
2508: $returnhash{$_}=unescape($pairs[$i]);
2509: $i++;
2510: }
2511: return %returnhash;
2512: }
2513:
2514: # --------------------------------------------------------------- del interface
2515:
2516: sub del {
2517: my ($namespace,$storearr,$udomain,$uname)=@_;
2518: my $items='';
2519: foreach (@$storearr) {
2520: $items.=escape($_).'&';
2521: }
2522: $items=~s/\&$//;
2523: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2524: if (!$uname) { $uname=$ENV{'user.name'}; }
2525: my $uhome=&homeserver($uname,$udomain);
2526:
2527: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
2528: }
2529:
2530: # -------------------------------------------------------------- dump interface
2531:
2532: sub dump {
2533: my ($namespace,$udomain,$uname,$regexp)=@_;
2534: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2535: if (!$uname) { $uname=$ENV{'user.name'}; }
2536: my $uhome=&homeserver($uname,$udomain);
2537: if ($regexp) {
2538: $regexp=&escape($regexp);
2539: } else {
2540: $regexp='.';
2541: }
2542: my $rep=reply("dump:$udomain:$uname:$namespace:$regexp",$uhome);
2543: my @pairs=split(/\&/,$rep);
2544: my %returnhash=();
2545: foreach (@pairs) {
2546: my ($key,$value)=split(/=/,$_);
2547: $returnhash{unescape($key)}=unescape($value);
2548: }
2549: return %returnhash;
2550: }
2551:
2552: # -------------------------------------------------------------- keys interface
2553:
2554: sub getkeys {
2555: my ($namespace,$udomain,$uname)=@_;
2556: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2557: if (!$uname) { $uname=$ENV{'user.name'}; }
2558: my $uhome=&homeserver($uname,$udomain);
2559: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
2560: my @keyarray=();
2561: foreach (split(/\&/,$rep)) {
2562: push (@keyarray,&unescape($_));
2563: }
2564: return @keyarray;
2565: }
2566:
2567: # --------------------------------------------------------------- currentdump
2568: sub currentdump {
2569: my ($courseid,$sdom,$sname)=@_;
2570: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2571: $sdom = $ENV{'user.domain'} if (! defined($sdom));
2572: $sname = $ENV{'user.name'} if (! defined($sname));
2573: my $uhome = &homeserver($sname,$sdom);
2574: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
2575: return if ($rep =~ /^(error:|no_such_host)/);
2576: #
2577: my %returnhash=();
2578: #
2579: if ($rep eq "unknown_cmd") {
2580: # an old lond will not know currentdump
2581: # Do a dump and make it look like a currentdump
2582: my @tmp = &dump($courseid,$sdom,$sname,'.');
2583: return if ($tmp[0] =~ /^(error:|no_such_host)/);
2584: my %hash = @tmp;
2585: @tmp=();
2586: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
2587: } else {
2588: my @pairs=split(/\&/,$rep);
2589: foreach (@pairs) {
2590: my ($key,$value)=split(/=/,$_);
2591: my ($symb,$param) = split(/:/,$key);
2592: $returnhash{&unescape($symb)}->{&unescape($param)} =
2593: &unescape($value);
2594: }
2595: }
2596: return %returnhash;
2597: }
2598:
2599: sub convert_dump_to_currentdump{
2600: my %hash = %{shift()};
2601: my %returnhash;
2602: # Code ripped from lond, essentially. The only difference
2603: # here is the unescaping done by lonnet::dump(). Conceivably
2604: # we might run in to problems with parameter names =~ /^v\./
2605: while (my ($key,$value) = each(%hash)) {
2606: my ($v,$symb,$param) = split(/:/,$key);
2607: next if ($v eq 'version' || $symb eq 'keys');
2608: next if (exists($returnhash{$symb}) &&
2609: exists($returnhash{$symb}->{$param}) &&
2610: $returnhash{$symb}->{'v.'.$param} > $v);
2611: $returnhash{$symb}->{$param}=$value;
2612: $returnhash{$symb}->{'v.'.$param}=$v;
2613: }
2614: #
2615: # Remove all of the keys in the hashes which keep track of
2616: # the version of the parameter.
2617: while (my ($symb,$param_hash) = each(%returnhash)) {
2618: # use a foreach because we are going to delete from the hash.
2619: foreach my $key (keys(%$param_hash)) {
2620: delete($param_hash->{$key}) if ($key =~ /^v\./);
2621: }
2622: }
2623: return \%returnhash;
2624: }
2625:
2626: # --------------------------------------------------------------- inc interface
2627:
2628: sub inc {
2629: my ($namespace,$store,$udomain,$uname) = @_;
2630: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2631: if (!$uname) { $uname=$ENV{'user.name'}; }
2632: my $uhome=&homeserver($uname,$udomain);
2633: my $items='';
2634: if (! ref($store)) {
2635: # got a single value, so use that instead
2636: $items = &escape($store).'=&';
2637: } elsif (ref($store) eq 'SCALAR') {
2638: $items = &escape($$store).'=&';
2639: } elsif (ref($store) eq 'ARRAY') {
2640: $items = join('=&',map {&escape($_);} @{$store});
2641: } elsif (ref($store) eq 'HASH') {
2642: while (my($key,$value) = each(%{$store})) {
2643: $items.= &escape($key).'='.&escape($value).'&';
2644: }
2645: }
2646: $items=~s/\&$//;
2647: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
2648: }
2649:
2650: # --------------------------------------------------------------- put interface
2651:
2652: sub put {
2653: my ($namespace,$storehash,$udomain,$uname)=@_;
2654: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2655: if (!$uname) { $uname=$ENV{'user.name'}; }
2656: my $uhome=&homeserver($uname,$udomain);
2657: my $items='';
2658: foreach (keys %$storehash) {
2659: $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
2660: }
2661: $items=~s/\&$//;
2662: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
2663: }
2664:
2665: # ---------------------------------------------------------- putstore interface
2666:
2667: sub putstore {
2668: my ($namespace,$storehash,$udomain,$uname)=@_;
2669: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2670: if (!$uname) { $uname=$ENV{'user.name'}; }
2671: my $uhome=&homeserver($uname,$udomain);
2672: my $items='';
2673: my %allitems = ();
2674: foreach (keys %$storehash) {
2675: if ($_ =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
2676: my $key = $1.':keys:'.$2;
2677: $allitems{$key} .= $3.':';
2678: }
2679: $items.=$_.'='.&escape($$storehash{$_}).'&';
2680: }
2681: foreach (keys %allitems) {
2682: $allitems{$_} =~ s/\:$//;
2683: $items.= $_.'='.$allitems{$_}.'&';
2684: }
2685: $items=~s/\&$//;
2686: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
2687: }
2688:
2689: # ------------------------------------------------------ critical put interface
2690:
2691: sub cput {
2692: my ($namespace,$storehash,$udomain,$uname)=@_;
2693: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2694: if (!$uname) { $uname=$ENV{'user.name'}; }
2695: my $uhome=&homeserver($uname,$udomain);
2696: my $items='';
2697: foreach (keys %$storehash) {
2698: $items.=escape($_).'='.escape($$storehash{$_}).'&';
2699: }
2700: $items=~s/\&$//;
2701: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
2702: }
2703:
2704: # -------------------------------------------------------------- eget interface
2705:
2706: sub eget {
2707: my ($namespace,$storearr,$udomain,$uname)=@_;
2708: my $items='';
2709: foreach (@$storearr) {
2710: $items.=escape($_).'&';
2711: }
2712: $items=~s/\&$//;
2713: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
2714: if (!$uname) { $uname=$ENV{'user.name'}; }
2715: my $uhome=&homeserver($uname,$udomain);
2716: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
2717: my @pairs=split(/\&/,$rep);
2718: my %returnhash=();
2719: my $i=0;
2720: foreach (@$storearr) {
2721: $returnhash{$_}=unescape($pairs[$i]);
2722: $i++;
2723: }
2724: return %returnhash;
2725: }
2726:
2727: # ---------------------------------------------- Custom access rule evaluation
2728:
2729: sub customaccess {
2730: my ($priv,$uri)=@_;
2731: my ($urole,$urealm)=split(/\./,$ENV{'request.role'});
2732: $urealm=~s/^\W//;
2733: my ($udom,$ucrs,$usec)=split(/\//,$urealm);
2734: my $access=0;
2735: foreach (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
2736: my ($effect,$realm,$role)=split(/\:/,$_);
2737: if ($role) {
2738: if ($role ne $urole) { next; }
2739: }
2740: foreach (split(/\s*\,\s*/,$realm)) {
2741: my ($tdom,$tcrs,$tsec)=split(/\_/,$_);
2742: if ($tdom) {
2743: if ($tdom ne $udom) { next; }
2744: }
2745: if ($tcrs) {
2746: if ($tcrs ne $ucrs) { next; }
2747: }
2748: if ($tsec) {
2749: if ($tsec ne $usec) { next; }
2750: }
2751: $access=($effect eq 'allow');
2752: last;
2753: }
2754: if ($realm eq '' && $role eq '') {
2755: $access=($effect eq 'allow');
2756: }
2757: }
2758: return $access;
2759: }
2760:
2761: # ------------------------------------------------- Check for a user privilege
2762:
2763: sub allowed {
2764: my ($priv,$uri)=@_;
2765: $uri=&deversion($uri);
2766: my $orguri=$uri;
2767: $uri=&declutter($uri);
2768:
2769: if (defined($ENV{'allowed.'.$priv})) { return $ENV{'allowed.'.$priv}; }
2770: # Free bre access to adm and meta resources
2771: if (((($uri=~/^adm\//) && ($uri !~ m|/bulletinboard$|))
2772: || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
2773: return 'F';
2774: }
2775:
2776: # Free bre to public access
2777:
2778: if ($priv eq 'bre') {
2779: my $copyright=&metadata($uri,'copyright');
2780: if (($copyright eq 'public') && (!$ENV{'request.course.id'})) {
2781: return 'F';
2782: }
2783: if ($copyright eq 'priv') {
2784: $uri=~/([^\/]+)\/([^\/]+)\//;
2785: unless (($ENV{'user.name'} eq $2) && ($ENV{'user.domain'} eq $1)) {
2786: return '';
2787: }
2788: }
2789: if ($copyright eq 'domain') {
2790: $uri=~/([^\/]+)\/([^\/]+)\//;
2791: unless (($ENV{'user.domain'} eq $1) ||
2792: ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $1)) {
2793: return '';
2794: }
2795: }
2796: if ($ENV{'request.role'}=~ /li\.\//) {
2797: # Library role, so allow browsing of resources in this domain.
2798: return 'F';
2799: }
2800: if ($copyright eq 'custom') {
2801: unless (&customaccess($priv,$uri)) { return ''; }
2802: }
2803: }
2804: # Domain coordinator is trying to create a course
2805: if (($priv eq 'ccc') && ($ENV{'request.role'} =~ /^dc\./)) {
2806: # uri is the requested domain in this case.
2807: # comparison to 'request.role.domain' shows if the user has selected
2808: # a role of dc for the domain in question.
2809: return 'F' if ($uri eq $ENV{'request.role.domain'});
2810: }
2811:
2812: my $thisallowed='';
2813: my $statecond=0;
2814: my $courseprivid='';
2815:
2816: # Course
2817:
2818: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
2819: $thisallowed.=$1;
2820: }
2821:
2822: # Domain
2823:
2824: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
2825: =~/\Q$priv\E\&([^\:]*)/) {
2826: $thisallowed.=$1;
2827: }
2828:
2829: # Course: uri itself is a course
2830: my $courseuri=$uri;
2831: $courseuri=~s/\_(\d)/\/$1/;
2832: $courseuri=~s/^([^\/])/\/$1/;
2833:
2834: if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
2835: =~/\Q$priv\E\&([^\:]*)/) {
2836: $thisallowed.=$1;
2837: }
2838:
2839: # URI is an uploaded document for this course
2840:
2841: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
2842: my $refuri=$ENV{'httpref.'.$orguri};
2843: if ($refuri) {
2844: if ($refuri =~ m|^/adm/|) {
2845: $thisallowed='F';
2846: }
2847: }
2848: }
2849:
2850: # Full access at system, domain or course-wide level? Exit.
2851:
2852: if ($thisallowed=~/F/) {
2853: return 'F';
2854: }
2855:
2856: # If this is generating or modifying users, exit with special codes
2857:
2858: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:\Q$priv\E\:/) {
2859: return $thisallowed;
2860: }
2861: #
2862: # Gathered so far: system, domain and course wide privileges
2863: #
2864: # Course: See if uri or referer is an individual resource that is part of
2865: # the course
2866:
2867: if ($ENV{'request.course.id'}) {
2868:
2869: $courseprivid=$ENV{'request.course.id'};
2870: if ($ENV{'request.course.sec'}) {
2871: $courseprivid.='/'.$ENV{'request.course.sec'};
2872: }
2873: $courseprivid=~s/\_/\//;
2874: my $checkreferer=1;
2875: my ($match,$cond)=&is_on_map($uri);
2876: if ($match) {
2877: $statecond=$cond;
2878: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
2879: =~/\Q$priv\E\&([^\:]*)/) {
2880: $thisallowed.=$1;
2881: $checkreferer=0;
2882: }
2883: }
2884:
2885: if ($checkreferer) {
2886: my $refuri=$ENV{'httpref.'.$orguri};
2887: unless ($refuri) {
2888: foreach (keys %ENV) {
2889: if ($_=~/^httpref\..*\*/) {
2890: my $pattern=$_;
2891: $pattern=~s/^httpref\.\/res\///;
2892: $pattern=~s/\*/\[\^\/\]\+/g;
2893: $pattern=~s/\//\\\//g;
2894: if ($orguri=~/$pattern/) {
2895: $refuri=$ENV{$_};
2896: }
2897: }
2898: }
2899: }
2900:
2901: if ($refuri) {
2902: $refuri=&declutter($refuri);
2903: my ($match,$cond)=&is_on_map($refuri);
2904: if ($match) {
2905: my $refstatecond=$cond;
2906: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
2907: =~/\Q$priv\E\&([^\:]*)/) {
2908: $thisallowed.=$1;
2909: $uri=$refuri;
2910: $statecond=$refstatecond;
2911: }
2912: }
2913: }
2914: }
2915: }
2916:
2917: #
2918: # Gathered now: all privileges that could apply, and condition number
2919: #
2920: #
2921: # Full or no access?
2922: #
2923:
2924: if ($thisallowed=~/F/) {
2925: return 'F';
2926: }
2927:
2928: unless ($thisallowed) {
2929: return '';
2930: }
2931:
2932: # Restrictions exist, deal with them
2933: #
2934: # C:according to course preferences
2935: # R:according to resource settings
2936: # L:unless locked
2937: # X:according to user session state
2938: #
2939:
2940: # Possibly locked functionality, check all courses
2941: # Locks might take effect only after 10 minutes cache expiration for other
2942: # courses, and 2 minutes for current course
2943:
2944: my $envkey;
2945: if ($thisallowed=~/L/) {
2946: foreach $envkey (keys %ENV) {
2947: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
2948: my $courseid=$2;
2949: my $roleid=$1.'.'.$2;
2950: $courseid=~s/^\///;
2951: my $expiretime=600;
2952: if ($ENV{'request.role'} eq $roleid) {
2953: $expiretime=120;
2954: }
2955: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
2956: my $prefix='course.'.$cdom.'_'.$cnum.'.';
2957: if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
2958: &coursedescription($courseid);
2959: }
2960: if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
2961: || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
2962: if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
2963: &log($ENV{'user.domain'},$ENV{'user.name'},
2964: $ENV{'user.home'},
2965: 'Locked by res: '.$priv.' for '.$uri.' due to '.
2966: $cdom.'/'.$cnum.'/'.$csec.' expire '.
2967: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
2968: return '';
2969: }
2970: }
2971: if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
2972: || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
2973: if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
2974: &log($ENV{'user.domain'},$ENV{'user.name'},
2975: $ENV{'user.home'},
2976: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
2977: $cdom.'/'.$cnum.'/'.$csec.' expire '.
2978: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
2979: return '';
2980: }
2981: }
2982: }
2983: }
2984: }
2985:
2986: #
2987: # Rest of the restrictions depend on selected course
2988: #
2989:
2990: unless ($ENV{'request.course.id'}) {
2991: return '1';
2992: }
2993:
2994: #
2995: # Now user is definitely in a course
2996: #
2997:
2998:
2999: # Course preferences
3000:
3001: if ($thisallowed=~/C/) {
3002: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
3003: my $unamedom=$ENV{'user.name'}.':'.$ENV{'user.domain'};
3004: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
3005: =~/\Q$rolecode\E/) {
3006: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
3007: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
3008: $ENV{'request.course.id'});
3009: return '';
3010: }
3011:
3012: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.users.denied'}
3013: =~/\Q$unamedom\E/) {
3014: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
3015: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
3016: $ENV{'request.course.id'});
3017: return '';
3018: }
3019: }
3020:
3021: # Resource preferences
3022:
3023: if ($thisallowed=~/R/) {
3024: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
3025: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
3026: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
3027: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
3028: return '';
3029: }
3030: }
3031:
3032: # Restricted by state or randomout?
3033:
3034: if ($thisallowed=~/X/) {
3035: if ($ENV{'acc.randomout'}) {
3036: my $symb=&symbread($uri,1);
3037: if (($symb) && ($ENV{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
3038: return '';
3039: }
3040: }
3041: if (&condval($statecond)) {
3042: return '2';
3043: } else {
3044: return '';
3045: }
3046: }
3047:
3048: return 'F';
3049: }
3050:
3051: # --------------------------------------------------- Is a resource on the map?
3052:
3053: sub is_on_map {
3054: my $uri=&declutter(shift);
3055: $uri=~s/\.\d+\.(\w+)$/\.$1/;
3056: my @uriparts=split(/\//,$uri);
3057: my $filename=$uriparts[$#uriparts];
3058: my $pathname=$uri;
3059: $pathname=~s|/\Q$filename\E$||;
3060: $pathname=~s/^adm\/wrapper\///;
3061: #Trying to find the conditional for the file
3062: my $match=($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
3063: /\&\Q$filename\E\:([\d\|]+)\&/);
3064: if ($match) {
3065: return (1,$1);
3066: } else {
3067: return (0,0);
3068: }
3069: }
3070:
3071: # --------------------------------------------------------- Get symb from alias
3072:
3073: sub get_symb_from_alias {
3074: my $symb=shift;
3075: my ($map,$resid,$url)=&decode_symb($symb);
3076: # Already is a symb
3077: if ($url) { return $symb; }
3078: # Must be an alias
3079: my $aliassymb='';
3080: my %bighash;
3081: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
3082: &GDBM_READER(),0640)) {
3083: my $rid=$bighash{'mapalias_'.$symb};
3084: if ($rid) {
3085: my ($mapid,$resid)=split(/\./,$rid);
3086: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
3087: $resid,$bighash{'src_'.$rid});
3088: }
3089: untie %bighash;
3090: }
3091: return $aliassymb;
3092: }
3093:
3094: # ----------------------------------------------------------------- Define Role
3095:
3096: sub definerole {
3097: if (allowed('mcr','/')) {
3098: my ($rolename,$sysrole,$domrole,$courole)=@_;
3099: foreach (split(':',$sysrole)) {
3100: my ($crole,$cqual)=split(/\&/,$_);
3101: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
3102: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
3103: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
3104: return "refused:s:$crole&$cqual";
3105: }
3106: }
3107: }
3108: foreach (split(':',$domrole)) {
3109: my ($crole,$cqual)=split(/\&/,$_);
3110: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
3111: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
3112: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
3113: return "refused:d:$crole&$cqual";
3114: }
3115: }
3116: }
3117: foreach (split(':',$courole)) {
3118: my ($crole,$cqual)=split(/\&/,$_);
3119: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
3120: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
3121: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
3122: return "refused:c:$crole&$cqual";
3123: }
3124: }
3125: }
3126: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
3127: "$ENV{'user.domain'}:$ENV{'user.name'}:".
3128: "rolesdef_$rolename=".
3129: escape($sysrole.'_'.$domrole.'_'.$courole);
3130: return reply($command,$ENV{'user.home'});
3131: } else {
3132: return 'refused';
3133: }
3134: }
3135:
3136: # ---------------- Make a metadata query against the network of library servers
3137:
3138: sub metadata_query {
3139: my ($query,$custom,$customshow,$server_array)=@_;
3140: my %rhash;
3141: my @server_list = (defined($server_array) ? @$server_array
3142: : keys(%libserv) );
3143: for my $server (@server_list) {
3144: unless ($custom or $customshow) {
3145: my $reply=&reply("querysend:".&escape($query),$server);
3146: $rhash{$server}=$reply;
3147: }
3148: else {
3149: my $reply=&reply("querysend:".&escape($query).':'.
3150: &escape($custom).':'.&escape($customshow),
3151: $server);
3152: $rhash{$server}=$reply;
3153: }
3154: }
3155: return \%rhash;
3156: }
3157:
3158: # ----------------------------------------- Send log queries and wait for reply
3159:
3160: sub log_query {
3161: my ($uname,$udom,$query,%filters)=@_;
3162: my $uhome=&homeserver($uname,$udom);
3163: if ($uhome eq 'no_host') { return 'error: no_host'; }
3164: my $uhost=$hostname{$uhome};
3165: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
3166: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
3167: $uhome);
3168: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
3169: return get_query_reply($queryid);
3170: }
3171:
3172: # ------- Request retrieval of institutional classlists for course(s)
3173:
3174: sub fetch_enrollment_query {
3175: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
3176: my $homeserver;
3177: if ($context eq 'automated') {
3178: $homeserver = $perlvar{'lonHostID'};
3179: } else {
3180: $homeserver = &homeserver($cnum,$dom);
3181: }
3182: my $host=$hostname{$homeserver};
3183: my $cmd = '';
3184: foreach (keys %{$affiliatesref}) {
3185: $cmd .= $_.'='.join(",",@{$$affiliatesref{$_}}).'%%';
3186: }
3187: $cmd =~ s/%%$//;
3188: $cmd = &escape($cmd);
3189: my $query = 'fetchenrollment';
3190: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$ENV{'user.name'}.':'.$cmd,$homeserver);
3191: unless ($queryid=~/^\Q$host\E\_/) {
3192: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
3193: return 'error: '.$queryid;
3194: }
3195: my $reply = &get_query_reply($queryid);
3196: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
3197: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$ENV{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum);
3198: } else {
3199: my @responses = split/:/,$reply;
3200: if ($homeserver eq $perlvar{'lonHostID'}) {
3201: foreach (@responses) {
3202: my ($key,$value) = split/=/,$_;
3203: $$replyref{$key} = $value;
3204: }
3205: } else {
3206: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
3207: foreach (@responses) {
3208: my ($key,$value) = split/=/,$_;
3209: $$replyref{$key} = $value;
3210: if ($value > 0) {
3211: foreach (@{$$affiliatesref{$key}}) {
3212: my $filename = $dom.'_'.$key.'_'.$_.'_classlist.xml';
3213: my $destname = $pathname.'/'.$filename;
3214: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
3215: if ($xml_classlist =~ /^error/) {
3216: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
3217: } else {
3218: if ( open(FILE,">$destname") ) {
3219: print FILE &unescape($xml_classlist);
3220: close(FILE);
3221: } else {
3222: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
3223: }
3224: }
3225: }
3226: }
3227: }
3228: }
3229: return 'ok';
3230: }
3231: return 'error';
3232: }
3233:
3234: sub get_query_reply {
3235: my $queryid=shift;
3236: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
3237: my $reply='';
3238: for (1..100) {
3239: sleep 2;
3240: if (-e $replyfile.'.end') {
3241: if (open(my $fh,$replyfile)) {
3242: $reply.=<$fh>;
3243: close($fh);
3244: } else { return 'error: reply_file_error'; }
3245: return &unescape($reply);
3246: }
3247: }
3248: return 'timeout:'.$queryid;
3249: }
3250:
3251: sub courselog_query {
3252: #
3253: # possible filters:
3254: # url: url or symb
3255: # username
3256: # domain
3257: # action: view, submit, grade
3258: # start: timestamp
3259: # end: timestamp
3260: #
3261: my (%filters)=@_;
3262: unless ($ENV{'request.course.id'}) { return 'no_course'; }
3263: if ($filters{'url'}) {
3264: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
3265: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
3266: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
3267: }
3268: my $cname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
3269: my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
3270: return &log_query($cname,$cdom,'courselog',%filters);
3271: }
3272:
3273: sub userlog_query {
3274: my ($uname,$udom,%filters)=@_;
3275: return &log_query($uname,$udom,'userlog',%filters);
3276: }
3277:
3278: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
3279:
3280: sub auto_run {
3281: my ($cnum,$cdom) = @_;
3282: my $homeserver = &homeserver($cnum,$cdom);
3283: my $response = &reply('autorun:'.$cdom,$homeserver);
3284: return $response;
3285: }
3286:
3287: sub auto_get_sections {
3288: my ($cnum,$cdom,$inst_coursecode) = @_;
3289: my $homeserver = &homeserver($cnum,$cdom);
3290: my @secs = ();
3291: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
3292: unless ($response eq 'refused') {
3293: @secs = split/:/,$response;
3294: }
3295: return @secs;
3296: }
3297:
3298: sub auto_new_course {
3299: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
3300: my $homeserver = &homeserver($cnum,$cdom);
3301: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
3302: return $response;
3303: }
3304:
3305: sub auto_validate_courseID {
3306: my ($cnum,$cdom,$inst_course_id) = @_;
3307: my $homeserver = &homeserver($cnum,$cdom);
3308: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
3309: return $response;
3310: }
3311:
3312: sub auto_create_password {
3313: my ($cnum,$cdom,$authparam) = @_;
3314: my $homeserver = &homeserver($cnum,$cdom);
3315: my $create_passwd = 0;
3316: my $authchk = '';
3317: my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
3318: if ($response eq 'refused') {
3319: $authchk = 'refused';
3320: } else {
3321: ($authparam,$create_passwd,$authchk) = split/:/,$response;
3322: }
3323: return ($authparam,$create_passwd,$authchk);
3324: }
3325:
3326: sub auto_instcode_format {
3327: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,$cat_order) = @_;
3328: my $courses = '';
3329: my $homeserver;
3330: if ($caller eq 'global') {
3331: $homeserver = $perlvar{'lonHostID'};
3332: } else {
3333: $homeserver = &homeserver($caller,$codedom);
3334: }
3335: my $host=$hostname{$homeserver};
3336: foreach (keys %{$instcodes}) {
3337: $courses .= &escape($_).'='.&escape($$instcodes{$_}).'&';
3338: }
3339: chop($courses);
3340: my $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$homeserver);
3341: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
3342: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = split/:/,$response;
3343: %{$codes} = &str2hash($codes_str);
3344: @{$codetitles} = &str2array($codetitles_str);
3345: %{$cat_titles} = &str2hash($cat_titles_str);
3346: %{$cat_order} = &str2hash($cat_order_str);
3347: return 'ok';
3348: }
3349: return $response;
3350: }
3351:
3352: # ------------------------------------------------------------------ Plain Text
3353:
3354: sub plaintext {
3355: my $short=shift;
3356: return &mt($prp{$short});
3357: }
3358:
3359: # ----------------------------------------------------------------- Assign Role
3360:
3361: sub assignrole {
3362: my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
3363: my $mrole;
3364: if ($role =~ /^cr\//) {
3365: my $cwosec=$url;
3366: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
3367: unless (&allowed('ccr',$cwosec)) {
3368: &logthis('Refused custom assignrole: '.
3369: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
3370: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
3371: return 'refused';
3372: }
3373: $mrole='cr';
3374: } else {
3375: my $cwosec=$url;
3376: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
3377: unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) {
3378: &logthis('Refused assignrole: '.
3379: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
3380: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
3381: return 'refused';
3382: }
3383: $mrole=$role;
3384: }
3385: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
3386: "$udom:$uname:$url".'_'."$mrole=$role";
3387: if ($end) { $command.='_'.$end; }
3388: if ($start) {
3389: if ($end) {
3390: $command.='_'.$start;
3391: } else {
3392: $command.='_0_'.$start;
3393: }
3394: }
3395: # actually delete
3396: if ($deleteflag) {
3397: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
3398: # modify command to delete the role
3399: $command="encrypt:rolesdel:$ENV{'user.domain'}:$ENV{'user.name'}:".
3400: "$udom:$uname:$url".'_'."$mrole";
3401: &logthis("$ENV{'user.name'} at $ENV{'user.domain'} deletes $mrole in $url for $uname at $udom");
3402: # set start and finish to negative values for userrolelog
3403: $start=-1;
3404: $end=-1;
3405: }
3406: }
3407: # send command
3408: my $answer=&reply($command,&homeserver($uname,$udom));
3409: # log new user role if status is ok
3410: if ($answer eq 'ok') {
3411: &userrolelog($mrole,$uname,$udom,$url,$start,$end);
3412: }
3413: return $answer;
3414: }
3415:
3416: # -------------------------------------------------- Modify user authentication
3417: # Overrides without validation
3418:
3419: sub modifyuserauth {
3420: my ($udom,$uname,$umode,$upass)=@_;
3421: my $uhome=&homeserver($uname,$udom);
3422: unless (&allowed('mau',$udom)) { return 'refused'; }
3423: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
3424: $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
3425: ' in domain '.$ENV{'request.role.domain'});
3426: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
3427: &escape($upass),$uhome);
3428: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
3429: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
3430: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
3431: &log($udom,,$uname,$uhome,
3432: 'Authentication changed by '.$ENV{'user.domain'}.', '.
3433: $ENV{'user.name'}.', '.$umode.
3434: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
3435: unless ($reply eq 'ok') {
3436: &logthis('Authentication mode error: '.$reply);
3437: return 'error: '.$reply;
3438: }
3439: return 'ok';
3440: }
3441:
3442: # --------------------------------------------------------------- Modify a user
3443:
3444: sub modifyuser {
3445: my ($udom, $uname, $uid,
3446: $umode, $upass, $first,
3447: $middle, $last, $gene,
3448: $forceid, $desiredhome, $email)=@_;
3449: $udom=~s/\W//g;
3450: $uname=~s/\W//g;
3451: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
3452: $umode.', '.$first.', '.$middle.', '.
3453: $last.', '.$gene.'(forceid: '.$forceid.')'.
3454: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
3455: ' desiredhome not specified').
3456: ' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
3457: ' in domain '.$ENV{'request.role.domain'});
3458: my $uhome=&homeserver($uname,$udom,'true');
3459: # ----------------------------------------------------------------- Create User
3460: if (($uhome eq 'no_host') &&
3461: (($umode && $upass) || ($umode eq 'localauth'))) {
3462: my $unhome='';
3463: if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) {
3464: $unhome = $desiredhome;
3465: } elsif($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
3466: $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
3467: } else { # load balancing routine for determining $unhome
3468: my $tryserver;
3469: my $loadm=10000000;
3470: foreach $tryserver (keys %libserv) {
3471: if ($hostdom{$tryserver} eq $udom) {
3472: my $answer=reply('load',$tryserver);
3473: if (($answer=~/\d+/) && ($answer<$loadm)) {
3474: $loadm=$answer;
3475: $unhome=$tryserver;
3476: }
3477: }
3478: }
3479: }
3480: if (($unhome eq '') || ($unhome eq 'no_host')) {
3481: return 'error: unable to find a home server for '.$uname.
3482: ' in domain '.$udom;
3483: }
3484: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
3485: &escape($upass),$unhome);
3486: unless ($reply eq 'ok') {
3487: return 'error: '.$reply;
3488: }
3489: $uhome=&homeserver($uname,$udom,'true');
3490: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
3491: return 'error: unable verify users home machine.';
3492: }
3493: } # End of creation of new user
3494: # ---------------------------------------------------------------------- Add ID
3495: if ($uid) {
3496: $uid=~tr/A-Z/a-z/;
3497: my %uidhash=&idrget($udom,$uname);
3498: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
3499: && (!$forceid)) {
3500: unless ($uid eq $uidhash{$uname}) {
3501: return 'error: user id "'.$uid.'" does not match '.
3502: 'current user id "'.$uidhash{$uname}.'".';
3503: }
3504: } else {
3505: &idput($udom,($uname => $uid));
3506: }
3507: }
3508: # -------------------------------------------------------------- Add names, etc
3509: my @tmp=&get('environment',
3510: ['firstname','middlename','lastname','generation'],
3511: $udom,$uname);
3512: my %names;
3513: if ($tmp[0] =~ m/^error:.*/) {
3514: %names=();
3515: } else {
3516: %names = @tmp;
3517: }
3518: #
3519: # Make sure to not trash student environment if instructor does not bother
3520: # to supply name and email information
3521: #
3522: if ($first) { $names{'firstname'} = $first; }
3523: if (defined($middle)) { $names{'middlename'} = $middle; }
3524: if ($last) { $names{'lastname'} = $last; }
3525: if (defined($gene)) { $names{'generation'} = $gene; }
3526: if ($email) { $names{'notification'} = $email;
3527: $names{'critnotification'} = $email; }
3528:
3529: my $reply = &put('environment', \%names, $udom,$uname);
3530: if ($reply ne 'ok') { return 'error: '.$reply; }
3531: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
3532: $umode.', '.$first.', '.$middle.', '.
3533: $last.', '.$gene.' by '.
3534: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
3535: return 'ok';
3536: }
3537:
3538: # -------------------------------------------------------------- Modify student
3539:
3540: sub modifystudent {
3541: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
3542: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
3543: if (!$cid) {
3544: unless ($cid=$ENV{'request.course.id'}) {
3545: return 'not_in_class';
3546: }
3547: }
3548: # --------------------------------------------------------------- Make the user
3549: my $reply=&modifyuser
3550: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
3551: $desiredhome,$email);
3552: unless ($reply eq 'ok') { return $reply; }
3553: # This will cause &modify_student_enrollment to get the uid from the
3554: # students environment
3555: $uid = undef if (!$forceid);
3556: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
3557: $gene,$usec,$end,$start,$type,$locktype,$cid);
3558: return $reply;
3559: }
3560:
3561: sub modify_student_enrollment {
3562: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
3563: my ($cdom,$cnum,$chome);
3564: if (!$cid) {
3565: unless ($cid=$ENV{'request.course.id'}) {
3566: return 'not_in_class';
3567: }
3568: $cdom=$ENV{'course.'.$cid.'.domain'};
3569: $cnum=$ENV{'course.'.$cid.'.num'};
3570: } else {
3571: ($cdom,$cnum)=split(/_/,$cid);
3572: }
3573: $chome=$ENV{'course.'.$cid.'.home'};
3574: if (!$chome) {
3575: $chome=&homeserver($cnum,$cdom);
3576: }
3577: if (!$chome) { return 'unknown_course'; }
3578: # Make sure the user exists
3579: my $uhome=&homeserver($uname,$udom);
3580: if (($uhome eq '') || ($uhome eq 'no_host')) {
3581: return 'error: no such user';
3582: }
3583: # Get student data if we were not given enough information
3584: if (!defined($first) || $first eq '' ||
3585: !defined($last) || $last eq '' ||
3586: !defined($uid) || $uid eq '' ||
3587: !defined($middle) || $middle eq '' ||
3588: !defined($gene) || $gene eq '') {
3589: # They did not supply us with enough data to enroll the student, so
3590: # we need to pick up more information.
3591: my %tmp = &get('environment',
3592: ['firstname','middlename','lastname', 'generation','id']
3593: ,$udom,$uname);
3594:
3595: #foreach (keys(%tmp)) {
3596: # &logthis("key $_ = ".$tmp{$_});
3597: #}
3598: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
3599: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
3600: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
3601: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
3602: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
3603: }
3604: my $fullname = &Apache::loncoursedata::ProcessFullName($last,$gene,
3605: $first,$middle);
3606: my $reply=cput('classlist',
3607: {"$uname:$udom" =>
3608: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
3609: $cdom,$cnum);
3610: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
3611: return 'error: '.$reply;
3612: }
3613: # Add student role to user
3614: my $uurl='/'.$cid;
3615: $uurl=~s/\_/\//g;
3616: if ($usec) {
3617: $uurl.='/'.$usec;
3618: }
3619: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
3620: }
3621:
3622: # ------------------------------------------------- Write to course preferences
3623:
3624: sub writecoursepref {
3625: my ($courseid,%prefs)=@_;
3626: $courseid=~s/^\///;
3627: $courseid=~s/\_/\//g;
3628: my ($cdomain,$cnum)=split(/\//,$courseid);
3629: my $chome=homeserver($cnum,$cdomain);
3630: if (($chome eq '') || ($chome eq 'no_host')) {
3631: return 'error: no such course';
3632: }
3633: my $cstring='';
3634: foreach (keys %prefs) {
3635: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
3636: }
3637: $cstring=~s/\&$//;
3638: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
3639: }
3640:
3641: # ---------------------------------------------------------- Make/modify course
3642:
3643: sub createcourse {
3644: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code)=@_;
3645: $url=&declutter($url);
3646: my $cid='';
3647: unless (&allowed('ccc',$udom)) {
3648: return 'refused';
3649: }
3650: # ------------------------------------------------------------------- Create ID
3651: my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
3652: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
3653: # ----------------------------------------------- Make sure that does not exist
3654: my $uhome=&homeserver($uname,$udom,'true');
3655: unless (($uhome eq '') || ($uhome eq 'no_host')) {
3656: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
3657: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
3658: $uhome=&homeserver($uname,$udom,'true');
3659: unless (($uhome eq '') || ($uhome eq 'no_host')) {
3660: return 'error: unable to generate unique course-ID';
3661: }
3662: }
3663: # ------------------------------------------------ Check supplied server name
3664: $course_server = $ENV{'user.homeserver'} if (! defined($course_server));
3665: if (! exists($libserv{$course_server})) {
3666: return 'error:bad server name '.$course_server;
3667: }
3668: # ------------------------------------------------------------- Make the course
3669: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
3670: $course_server);
3671: unless ($reply eq 'ok') { return 'error: '.$reply; }
3672: $uhome=&homeserver($uname,$udom,'true');
3673: if (($uhome eq '') || ($uhome eq 'no_host')) {
3674: return 'error: no such course';
3675: }
3676: # ----------------------------------------------------------------- Course made
3677: # log existence
3678: &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
3679: '='.&escape($inst_code),$uhome);
3680: &flushcourselogs();
3681: # set toplevel url
3682: my $topurl=$url;
3683: unless ($nonstandard) {
3684: # ------------------------------------------ For standard courses, make top url
3685: my $mapurl=&clutter($url);
3686: if ($mapurl eq '/res/') { $mapurl=''; }
3687: $ENV{'form.initmap'}=(<<ENDINITMAP);
3688: <map>
3689: <resource id="1" type="start"></resource>
3690: <resource id="2" src="$mapurl"></resource>
3691: <resource id="3" type="finish"></resource>
3692: <link index="1" from="1" to="2"></link>
3693: <link index="2" from="2" to="3"></link>
3694: </map>
3695: ENDINITMAP
3696: $topurl=&declutter(
3697: &finishuserfileupload($uname,$udom,$uhome,'initmap','default.sequence')
3698: );
3699: }
3700: # ----------------------------------------------------------- Write preferences
3701: &writecoursepref($udom.'_'.$uname,
3702: ('description' => $description,
3703: 'url' => $topurl));
3704: return '/'.$udom.'/'.$uname;
3705: }
3706:
3707: # ---------------------------------------------------------- Assign Custom Role
3708:
3709: sub assigncustomrole {
3710: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
3711: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
3712: $end,$start,$deleteflag);
3713: }
3714:
3715: # ----------------------------------------------------------------- Revoke Role
3716:
3717: sub revokerole {
3718: my ($udom,$uname,$url,$role,$deleteflag)=@_;
3719: my $now=time;
3720: return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
3721: }
3722:
3723: # ---------------------------------------------------------- Revoke Custom Role
3724:
3725: sub revokecustomrole {
3726: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
3727: my $now=time;
3728: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
3729: $deleteflag);
3730: }
3731:
3732: # ------------------------------------------------------------ Disk usage
3733: sub diskusage {
3734: my ($udom,$uname,$directoryRoot)=@_;
3735: $directoryRoot =~ s/\/$//;
3736: my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
3737: return $listing;
3738: }
3739:
3740:
3741: # ------------------------------------------------------------ Directory lister
3742:
3743: sub dirlist {
3744: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
3745:
3746: $uri=~s/^\///;
3747: $uri=~s/\/$//;
3748: my ($udom, $uname);
3749: (undef,$udom,$uname)=split(/\//,$uri);
3750: if(defined($userdomain)) {
3751: $udom = $userdomain;
3752: }
3753: if(defined($username)) {
3754: $uname = $username;
3755: }
3756:
3757: my $dirRoot = $perlvar{'lonDocRoot'};
3758: if(defined($alternateDirectoryRoot)) {
3759: $dirRoot = $alternateDirectoryRoot;
3760: $dirRoot =~ s/\/$//;
3761: }
3762:
3763: if($udom) {
3764: if($uname) {
3765: my $listing=reply('ls:'.$dirRoot.'/'.$uri,
3766: homeserver($uname,$udom));
3767: return split(/:/,$listing);
3768: } elsif(!defined($alternateDirectoryRoot)) {
3769: my $tryserver;
3770: my %allusers=();
3771: foreach $tryserver (keys %libserv) {
3772: if($hostdom{$tryserver} eq $udom) {
3773: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
3774: $udom, $tryserver);
3775: if (($listing ne 'no_such_dir') && ($listing ne 'empty')
3776: && ($listing ne 'con_lost')) {
3777: foreach (split(/:/,$listing)) {
3778: my ($entry,@stat)=split(/&/,$_);
3779: $allusers{$entry}=1;
3780: }
3781: }
3782: }
3783: }
3784: my $alluserstr='';
3785: foreach (sort keys %allusers) {
3786: $alluserstr.=$_.'&user:';
3787: }
3788: $alluserstr=~s/:$//;
3789: return split(/:/,$alluserstr);
3790: } else {
3791: my @emptyResults = ();
3792: push(@emptyResults, 'missing user name');
3793: return split(':',@emptyResults);
3794: }
3795: } elsif(!defined($alternateDirectoryRoot)) {
3796: my $tryserver;
3797: my %alldom=();
3798: foreach $tryserver (keys %libserv) {
3799: $alldom{$hostdom{$tryserver}}=1;
3800: }
3801: my $alldomstr='';
3802: foreach (sort keys %alldom) {
3803: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'/&domain:';
3804: }
3805: $alldomstr=~s/:$//;
3806: return split(/:/,$alldomstr);
3807: } else {
3808: my @emptyResults = ();
3809: push(@emptyResults, 'missing domain');
3810: return split(':',@emptyResults);
3811: }
3812: }
3813:
3814: # --------------------------------------------- GetFileTimestamp
3815: # This function utilizes dirlist and returns the date stamp for
3816: # when it was last modified. It will also return an error of -1
3817: # if an error occurs
3818:
3819: ##
3820: ## FIXME: This subroutine assumes its caller knows something about the
3821: ## directory structure of the home server for the student ($root).
3822: ## Not a good assumption to make. Since this is for looking up files
3823: ## in user directories, the full path should be constructed by lond, not
3824: ## whatever machine we request data from.
3825: ##
3826: sub GetFileTimestamp {
3827: my ($studentDomain,$studentName,$filename,$root)=@_;
3828: $studentDomain=~s/\W//g;
3829: $studentName=~s/\W//g;
3830: my $subdir=$studentName.'__';
3831: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
3832: my $proname="$studentDomain/$subdir/$studentName";
3833: $proname .= '/'.$filename;
3834: my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain,
3835: $studentName, $root);
3836: my @stats = split('&', $fileStat);
3837: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
3838: # @stats contains first the filename, then the stat output
3839: return $stats[10]; # so this is 10 instead of 9.
3840: } else {
3841: return -1;
3842: }
3843: }
3844:
3845: # -------------------------------------------------------- Value of a Condition
3846:
3847: sub directcondval {
3848: my $number=shift;
3849: if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
3850: return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
3851: } else {
3852: return 2;
3853: }
3854: }
3855:
3856: sub condval {
3857: my $condidx=shift;
3858: my $result=0;
3859: my $allpathcond='';
3860: foreach (split(/\|/,$condidx)) {
3861: if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
3862: $allpathcond.=
3863: '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
3864: }
3865: }
3866: $allpathcond=~s/\|$//;
3867: if ($ENV{'request.course.id'}) {
3868: if ($allpathcond) {
3869: my $operand='|';
3870: my @stack;
3871: foreach ($allpathcond=~/(\d+|\(|\)|\&|\|)/g) {
3872: if ($_ eq '(') {
3873: push @stack,($operand,$result)
3874: } elsif ($_ eq ')') {
3875: my $before=pop @stack;
3876: if (pop @stack eq '&') {
3877: $result=$result>$before?$before:$result;
3878: } else {
3879: $result=$result>$before?$result:$before;
3880: }
3881: } elsif (($_ eq '&') || ($_ eq '|')) {
3882: $operand=$_;
3883: } else {
3884: my $new=directcondval($_);
3885: if ($operand eq '&') {
3886: $result=$result>$new?$new:$result;
3887: } else {
3888: $result=$result>$new?$result:$new;
3889: }
3890: }
3891: }
3892: }
3893: }
3894: return $result;
3895: }
3896:
3897: # ---------------------------------------------------- Devalidate courseresdata
3898:
3899: sub devalidatecourseresdata {
3900: my ($coursenum,$coursedomain)=@_;
3901: my $hashid=$coursenum.':'.$coursedomain;
3902: &devalidate_cache(\%courseresdatacache,$hashid,'courseres');
3903: }
3904:
3905: # --------------------------------------------------- Course Resourcedata Query
3906:
3907: sub courseresdata {
3908: my ($coursenum,$coursedomain,@which)=@_;
3909: my $coursehom=&homeserver($coursenum,$coursedomain);
3910: my $hashid=$coursenum.':'.$coursedomain;
3911: my ($result,$cached)=&is_cached(\%courseresdatacache,$hashid,'courseres');
3912: unless (defined($cached)) {
3913: my %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
3914: $result=\%dumpreply;
3915: my ($tmp) = keys(%dumpreply);
3916: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
3917: &do_cache(\%courseresdatacache,$hashid,$result,'courseres');
3918: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
3919: return $tmp;
3920: } elsif ($tmp =~ /^(error)/) {
3921: $result=undef;
3922: &do_cache(\%courseresdatacache,$hashid,$result,'courseres');
3923: }
3924: }
3925: foreach my $item (@which) {
3926: if (defined($result->{$item})) {
3927: return $result->{$item};
3928: }
3929: }
3930: return undef;
3931: }
3932:
3933: #
3934: # EXT resource caching routines
3935: #
3936:
3937: sub clear_EXT_cache_status {
3938: &delenv('cache.EXT.');
3939: }
3940:
3941: sub EXT_cache_status {
3942: my ($target_domain,$target_user) = @_;
3943: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
3944: if (exists($ENV{$cachename}) && ($ENV{$cachename}+600) > time) {
3945: # We know already the user has no data
3946: return 1;
3947: } else {
3948: return 0;
3949: }
3950: }
3951:
3952: sub EXT_cache_set {
3953: my ($target_domain,$target_user) = @_;
3954: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
3955: &appenv($cachename => time);
3956: }
3957:
3958: # --------------------------------------------------------- Value of a Variable
3959: sub EXT {
3960: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
3961:
3962: unless ($varname) { return ''; }
3963: #get real user name/domain, courseid and symb
3964: my $courseid;
3965: my $publicuser;
3966: if ($symbparm) {
3967: $symbparm=&get_symb_from_alias($symbparm);
3968: }
3969: if (!($uname && $udom)) {
3970: (my $cursymb,$courseid,$udom,$uname,$publicuser)=
3971: &Apache::lonxml::whichuser($symbparm);
3972: if (!$symbparm) { $symbparm=$cursymb; }
3973: } else {
3974: $courseid=$ENV{'request.course.id'};
3975: }
3976: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
3977: my $rest;
3978: if (defined($therest[0])) {
3979: $rest=join('.',@therest);
3980: } else {
3981: $rest='';
3982: }
3983:
3984: my $qualifierrest=$qualifier;
3985: if ($rest) { $qualifierrest.='.'.$rest; }
3986: my $spacequalifierrest=$space;
3987: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
3988: if ($realm eq 'user') {
3989: # --------------------------------------------------------------- user.resource
3990: if ($space eq 'resource') {
3991: if (defined($Apache::lonhomework::parsing_a_problem)) {
3992: return $Apache::lonhomework::history{$qualifierrest};
3993: } else {
3994: my %restored;
3995: if ($publicuser || $ENV{'request.state'} eq 'construct') {
3996: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
3997: } else {
3998: %restored=&restore($symbparm,$courseid,$udom,$uname);
3999: }
4000: return $restored{$qualifierrest};
4001: }
4002: # ----------------------------------------------------------------- user.access
4003: } elsif ($space eq 'access') {
4004: # FIXME - not supporting calls for a specific user
4005: return &allowed($qualifier,$rest);
4006: # ------------------------------------------ user.preferences, user.environment
4007: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
4008: if (($uname eq $ENV{'user.name'}) &&
4009: ($udom eq $ENV{'user.domain'})) {
4010: return $ENV{join('.',('environment',$qualifierrest))};
4011: } else {
4012: my %returnhash;
4013: if (!$publicuser) {
4014: %returnhash=&userenvironment($udom,$uname,
4015: $qualifierrest);
4016: }
4017: return $returnhash{$qualifierrest};
4018: }
4019: # ----------------------------------------------------------------- user.course
4020: } elsif ($space eq 'course') {
4021: # FIXME - not supporting calls for a specific user
4022: return $ENV{join('.',('request.course',$qualifier))};
4023: # ------------------------------------------------------------------- user.role
4024: } elsif ($space eq 'role') {
4025: # FIXME - not supporting calls for a specific user
4026: my ($role,$where)=split(/\./,$ENV{'request.role'});
4027: if ($qualifier eq 'value') {
4028: return $role;
4029: } elsif ($qualifier eq 'extent') {
4030: return $where;
4031: }
4032: # ----------------------------------------------------------------- user.domain
4033: } elsif ($space eq 'domain') {
4034: return $udom;
4035: # ------------------------------------------------------------------- user.name
4036: } elsif ($space eq 'name') {
4037: return $uname;
4038: # ---------------------------------------------------- Any other user namespace
4039: } else {
4040: my %reply;
4041: if (!$publicuser) {
4042: %reply=&get($space,[$qualifierrest],$udom,$uname);
4043: }
4044: return $reply{$qualifierrest};
4045: }
4046: } elsif ($realm eq 'query') {
4047: # ---------------------------------------------- pull stuff out of query string
4048: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
4049: [$spacequalifierrest]);
4050: return $ENV{'form.'.$spacequalifierrest};
4051: } elsif ($realm eq 'request') {
4052: # ------------------------------------------------------------- request.browser
4053: if ($space eq 'browser') {
4054: if ($qualifier eq 'textremote') {
4055: if (&mt('textual_remote_display') eq 'on') {
4056: return 1;
4057: } else {
4058: return 0;
4059: }
4060: } else {
4061: return $ENV{'browser.'.$qualifier};
4062: }
4063: # ------------------------------------------------------------ request.filename
4064: } else {
4065: return $ENV{'request.'.$spacequalifierrest};
4066: }
4067: } elsif ($realm eq 'course') {
4068: # ---------------------------------------------------------- course.description
4069: return $ENV{'course.'.$courseid.'.'.$spacequalifierrest};
4070: } elsif ($realm eq 'resource') {
4071:
4072: my $section;
4073: if (defined($courseid) && $courseid eq $ENV{'request.course.id'}) {
4074: if (!$symbparm) { $symbparm=&symbread(); }
4075: }
4076: if ($symbparm && defined($courseid) &&
4077: $courseid eq $ENV{'request.course.id'}) {
4078:
4079: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
4080:
4081: # ----------------------------------------------------- Cascading lookup scheme
4082: my $symbp=$symbparm;
4083: my $mapp=(&decode_symb($symbp))[0];
4084:
4085: my $symbparm=$symbp.'.'.$spacequalifierrest;
4086: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
4087:
4088: if (($ENV{'user.name'} eq $uname) &&
4089: ($ENV{'user.domain'} eq $udom)) {
4090: $section=$ENV{'request.course.sec'};
4091: } else {
4092: if (! defined($usection)) {
4093: $section=&usection($udom,$uname,$courseid);
4094: } else {
4095: $section = $usection;
4096: }
4097: }
4098:
4099: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
4100: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
4101: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
4102:
4103: my $courselevel=$courseid.'.'.$spacequalifierrest;
4104: my $courselevelr=$courseid.'.'.$symbparm;
4105: my $courselevelm=$courseid.'.'.$mapparm;
4106:
4107: # ----------------------------------------------------------- first, check user
4108: #most student don\'t have any data set, check if there is some data
4109: if (! &EXT_cache_status($udom,$uname)) {
4110: my $hashid="$udom:$uname";
4111: my ($result,$cached)=&is_cached(\%userresdatacache,$hashid,
4112: 'userres');
4113: if (!defined($cached)) {
4114: my %resourcedata=&dump('resourcedata',$udom,$uname);
4115: $result=\%resourcedata;
4116: &do_cache(\%userresdatacache,$hashid,$result,'userres');
4117: }
4118: my ($tmp)=keys(%$result);
4119: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
4120: if ($$result{$courselevelr}) {
4121: return $$result{$courselevelr}; }
4122: if ($$result{$courselevelm}) {
4123: return $$result{$courselevelm}; }
4124: if ($$result{$courselevel}) {
4125: return $$result{$courselevel}; }
4126: } else {
4127: #error 2 occurs when the .db doesn't exist
4128: if ($tmp!~/error: 2 /) {
4129: &logthis("<font color=blue>WARNING:".
4130: " Trying to get resource data for ".
4131: $uname." at ".$udom.": ".
4132: $tmp."</font>");
4133: } elsif ($tmp=~/error: 2 /) {
4134: &EXT_cache_set($udom,$uname);
4135: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
4136: return $tmp;
4137: }
4138: }
4139: }
4140:
4141: # -------------------------------------------------------- second, check course
4142:
4143: my $coursereply=&courseresdata($ENV{'course.'.$courseid.'.num'},
4144: $ENV{'course.'.$courseid.'.domain'},
4145: ($seclevelr,$seclevelm,$seclevel,
4146: $courselevelr,$courselevelm,
4147: $courselevel));
4148: if (defined($coursereply)) { return $coursereply; }
4149:
4150: # ------------------------------------------------------ third, check map parms
4151: my %parmhash=();
4152: my $thisparm='';
4153: if (tie(%parmhash,'GDBM_File',
4154: $ENV{'request.course.fn'}.'_parms.db',
4155: &GDBM_READER(),0640)) {
4156: $thisparm=$parmhash{$symbparm};
4157: untie(%parmhash);
4158: }
4159: if ($thisparm) { return $thisparm; }
4160: }
4161: # --------------------------------------------- last, look in resource metadata
4162:
4163: $spacequalifierrest=~s/\./\_/;
4164: my $filename;
4165: if (!$symbparm) { $symbparm=&symbread(); }
4166: if ($symbparm) {
4167: $filename=(&decode_symb($symbparm))[2];
4168: } else {
4169: $filename=$ENV{'request.filename'};
4170: }
4171: my $metadata=&metadata($filename,$spacequalifierrest);
4172: if (defined($metadata)) { return $metadata; }
4173: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
4174: if (defined($metadata)) { return $metadata; }
4175:
4176: # ------------------------------------------------------------------ Cascade up
4177: unless ($space eq '0') {
4178: my @parts=split(/_/,$space);
4179: my $id=pop(@parts);
4180: my $part=join('_',@parts);
4181: if ($part eq '') { $part='0'; }
4182: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
4183: $symbparm,$udom,$uname,$section,1);
4184: if (defined($partgeneral)) { return $partgeneral; }
4185: }
4186: if ($recurse) { return undef; }
4187: my $pack_def=&packages_tab_default($filename,$varname);
4188: if (defined($pack_def)) { return $pack_def; }
4189:
4190: # ---------------------------------------------------- Any other user namespace
4191: } elsif ($realm eq 'environment') {
4192: # ----------------------------------------------------------------- environment
4193: if (($uname eq $ENV{'user.name'})&&($udom eq $ENV{'user.domain'})) {
4194: return $ENV{'environment.'.$spacequalifierrest};
4195: } else {
4196: my %returnhash=&userenvironment($udom,$uname,
4197: $spacequalifierrest);
4198: return $returnhash{$spacequalifierrest};
4199: }
4200: } elsif ($realm eq 'system') {
4201: # ----------------------------------------------------------------- system.time
4202: if ($space eq 'time') {
4203: return time;
4204: }
4205: }
4206: return '';
4207: }
4208:
4209: sub packages_tab_default {
4210: my ($uri,$varname)=@_;
4211: my (undef,$part,$name)=split(/\./,$varname);
4212: my $packages=&metadata($uri,'packages');
4213: foreach my $package (split(/,/,$packages)) {
4214: my ($pack_type,$pack_part)=split(/_/,$package,2);
4215: if (defined($packagetab{"$pack_type&$name&default"})) {
4216: return $packagetab{"$pack_type&$name&default"};
4217: }
4218: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
4219: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
4220: }
4221: }
4222: return undef;
4223: }
4224:
4225: sub add_prefix_and_part {
4226: my ($prefix,$part)=@_;
4227: my $keyroot;
4228: if (defined($prefix) && $prefix !~ /^__/) {
4229: # prefix that has a part already
4230: $keyroot=$prefix;
4231: } elsif (defined($prefix)) {
4232: # prefix that is missing a part
4233: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
4234: } else {
4235: # no prefix at all
4236: if (defined($part)) { $keyroot='_'.$part; }
4237: }
4238: return $keyroot;
4239: }
4240:
4241: # ---------------------------------------------------------------- Get metadata
4242:
4243: sub metadata {
4244: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
4245: $uri=&declutter($uri);
4246: # if it is a non metadata possible uri return quickly
4247: if (($uri eq '') ||
4248: (($uri =~ m|^/*adm/|) &&
4249: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
4250: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
4251: ($uri =~ m|home/[^/]+/public_html/|)) {
4252: return undef;
4253: }
4254: my $filename=$uri;
4255: $uri=~s/\.meta$//;
4256: #
4257: # Is the metadata already cached?
4258: # Look at timestamp of caching
4259: # Everything is cached by the main uri, libraries are never directly cached
4260: #
4261: if (!defined($liburi)) {
4262: my ($result,$cached)=&is_cached(\%metacache,$uri,'meta');
4263: if (defined($cached)) { return $result->{':'.$what}; }
4264: }
4265: {
4266: #
4267: # Is this a recursive call for a library?
4268: #
4269: if (! exists($metacache{$uri})) {
4270: $metacache{$uri}={};
4271: }
4272: if ($liburi) {
4273: $liburi=&declutter($liburi);
4274: $filename=$liburi;
4275: } else {
4276: &devalidate_cache(\%metacache,$uri,'meta');
4277: }
4278: my %metathesekeys=();
4279: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
4280: my $metastring;
4281: if ($uri !~ m|^uploaded/|) {
4282: my $file=&filelocation('',&clutter($filename));
4283: push(@{$metacache{$uri.'.file'}},$file);
4284: $metastring=&getfile($file);
4285: }
4286: my $parser=HTML::LCParser->new(\$metastring);
4287: my $token;
4288: undef %metathesekeys;
4289: while ($token=$parser->get_token) {
4290: if ($token->[0] eq 'S') {
4291: if (defined($token->[2]->{'package'})) {
4292: #
4293: # This is a package - get package info
4294: #
4295: my $package=$token->[2]->{'package'};
4296: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
4297: if (defined($token->[2]->{'id'})) {
4298: $keyroot.='_'.$token->[2]->{'id'};
4299: }
4300: if ($metacache{$uri}->{':packages'}) {
4301: $metacache{$uri}->{':packages'}.=','.$package.$keyroot;
4302: } else {
4303: $metacache{$uri}->{':packages'}=$package.$keyroot;
4304: }
4305: foreach (keys %packagetab) {
4306: my $part=$keyroot;
4307: $part=~s/^\_//;
4308: if ($_=~/^\Q$package\E\&/ ||
4309: $_=~/^\Q$package\E_0\&/) {
4310: my ($pack,$name,$subp)=split(/\&/,$_);
4311: # ignore package.tab specified default values
4312: # here &package_tab_default() will fetch those
4313: if ($subp eq 'default') { next; }
4314: my $value=$packagetab{$_};
4315: my $unikey;
4316: if ($pack =~ /_0$/) {
4317: $unikey='parameter_0_'.$name;
4318: $part=0;
4319: } else {
4320: $unikey='parameter'.$keyroot.'_'.$name;
4321: }
4322: if ($subp eq 'display') {
4323: $value.=' [Part: '.$part.']';
4324: }
4325: $metacache{$uri}->{':'.$unikey.'.part'}=$part;
4326: $metathesekeys{$unikey}=1;
4327: unless (defined($metacache{$uri}->{':'.$unikey.'.'.$subp})) {
4328: $metacache{$uri}->{':'.$unikey.'.'.$subp}=$value;
4329: }
4330: if (defined($metacache{$uri}->{':'.$unikey.'.default'})) {
4331: $metacache{$uri}->{':'.$unikey}=
4332: $metacache{$uri}->{':'.$unikey.'.default'};
4333: }
4334: }
4335: }
4336: } else {
4337: #
4338: # This is not a package - some other kind of start tag
4339: #
4340: my $entry=$token->[1];
4341: my $unikey;
4342: if ($entry eq 'import') {
4343: $unikey='';
4344: } else {
4345: $unikey=$entry;
4346: }
4347: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
4348:
4349: if (defined($token->[2]->{'id'})) {
4350: $unikey.='_'.$token->[2]->{'id'};
4351: }
4352:
4353: if ($entry eq 'import') {
4354: #
4355: # Importing a library here
4356: #
4357: if ($depthcount<20) {
4358: my $location=$parser->get_text('/import');
4359: my $dir=$filename;
4360: $dir=~s|[^/]*$||;
4361: $location=&filelocation($dir,$location);
4362: foreach (sort(split(/\,/,&metadata($uri,'keys',
4363: $location,$unikey,
4364: $depthcount+1)))) {
4365: $metacache{$uri}->{':'.$_}=$metacache{$uri}->{':'.$_};
4366: $metathesekeys{$_}=1;
4367: }
4368: }
4369: } else {
4370:
4371: if (defined($token->[2]->{'name'})) {
4372: $unikey.='_'.$token->[2]->{'name'};
4373: }
4374: $metathesekeys{$unikey}=1;
4375: foreach (@{$token->[3]}) {
4376: $metacache{$uri}->{':'.$unikey.'.'.$_}=$token->[2]->{$_};
4377: }
4378: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
4379: my $default=$metacache{$uri}->{':'.$unikey.'.default'};
4380: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
4381: # only ws inside the tag, and not in default, so use default
4382: # as value
4383: $metacache{$uri}->{':'.$unikey}=$default;
4384: } else {
4385: # either something interesting inside the tag or default
4386: # uninteresting
4387: $metacache{$uri}->{':'.$unikey}=$internaltext;
4388: }
4389: # end of not-a-package not-a-library import
4390: }
4391: # end of not-a-package start tag
4392: }
4393: # the next is the end of "start tag"
4394: }
4395: }
4396: my ($extension) = ($uri =~ /\.(\w+)$/);
4397: foreach my $key (sort(keys(%packagetab))) {
4398: #&logthis("extsion1 $extension $key !!");
4399: #no specific packages #how's our extension
4400: if ($key!~/^extension_\Q$extension\E&/) { next; }
4401: &metadata_create_package_def($uri,$key,'extension_'.$extension,
4402: \%metathesekeys);
4403: }
4404: if (!exists($metacache{$uri}->{':packages'})) {
4405: foreach my $key (sort(keys(%packagetab))) {
4406: #no specific packages well let's get default then
4407: if ($key!~/^default&/) { next; }
4408: &metadata_create_package_def($uri,$key,'default',
4409: \%metathesekeys);
4410: }
4411: }
4412: # are there custom rights to evaluate
4413: if ($metacache{$uri}->{':copyright'} eq 'custom') {
4414:
4415: #
4416: # Importing a rights file here
4417: #
4418: unless ($depthcount) {
4419: my $location=$metacache{$uri}->{':customdistributionfile'};
4420: my $dir=$filename;
4421: $dir=~s|[^/]*$||;
4422: $location=&filelocation($dir,$location);
4423: foreach (sort(split(/\,/,&metadata($uri,'keys',
4424: $location,'_rights',
4425: $depthcount+1)))) {
4426: $metacache{$uri}->{':'.$_}=$metacache{$uri}->{':'.$_};
4427: $metathesekeys{$_}=1;
4428: }
4429: }
4430: }
4431: $metacache{$uri}->{':keys'}=join(',',keys %metathesekeys);
4432: &metadata_generate_part0(\%metathesekeys,$metacache{$uri},$uri);
4433: $metacache{$uri}->{':allpossiblekeys'}=join(',',keys %metathesekeys);
4434: &do_cache(\%metacache,$uri,$metacache{$uri},'meta');
4435: # this is the end of "was not already recently cached
4436: }
4437: return $metacache{$uri}->{':'.$what};
4438: }
4439:
4440: sub metadata_create_package_def {
4441: my ($uri,$key,$package,$metathesekeys)=@_;
4442: my ($pack,$name,$subp)=split(/\&/,$key);
4443: if ($subp eq 'default') { next; }
4444:
4445: if (defined($metacache{$uri}->{':packages'})) {
4446: $metacache{$uri}->{':packages'}.=','.$package;
4447: } else {
4448: $metacache{$uri}->{':packages'}=$package;
4449: }
4450: my $value=$packagetab{$key};
4451: my $unikey;
4452: $unikey='parameter_0_'.$name;
4453: $metacache{$uri}->{':'.$unikey.'.part'}=0;
4454: $$metathesekeys{$unikey}=1;
4455: unless (defined($metacache{$uri}->{':'.$unikey.'.'.$subp})) {
4456: $metacache{$uri}->{':'.$unikey.'.'.$subp}=$value;
4457: }
4458: if (defined($metacache{$uri}->{':'.$unikey.'.default'})) {
4459: $metacache{$uri}->{':'.$unikey}=
4460: $metacache{$uri}->{':'.$unikey.'.default'};
4461: }
4462: }
4463:
4464: sub metadata_generate_part0 {
4465: my ($metadata,$metacache,$uri) = @_;
4466: my %allnames;
4467: foreach my $metakey (sort keys %$metadata) {
4468: if ($metakey=~/^parameter\_(.*)/) {
4469: my $part=$$metacache{':'.$metakey.'.part'};
4470: my $name=$$metacache{':'.$metakey.'.name'};
4471: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
4472: $allnames{$name}=$part;
4473: }
4474: }
4475: }
4476: foreach my $name (keys(%allnames)) {
4477: $$metadata{"parameter_0_$name"}=1;
4478: my $key=":parameter_0_$name";
4479: $$metacache{"$key.part"}='0';
4480: $$metacache{"$key.name"}=$name;
4481: $$metacache{"$key.type"}=$$metacache{':parameter_'.
4482: $allnames{$name}.'_'.$name.
4483: '.type'};
4484: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
4485: '.display'};
4486: my $expr='\\[Part: '.$allnames{$name}.'\\]';
4487: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
4488: $$metacache{"$key.display"}=$olddis;
4489: }
4490: }
4491:
4492: # ------------------------------------------------- Get the title of a resource
4493:
4494: sub gettitle {
4495: my $urlsymb=shift;
4496: my $symb=&symbread($urlsymb);
4497: if ($symb) {
4498: my ($result,$cached)=&is_cached(\%titlecache,$symb,'title',600);
4499: if (defined($cached)) { return $result; }
4500: my ($map,$resid,$url)=&decode_symb($symb);
4501: my $title='';
4502: my %bighash;
4503: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
4504: &GDBM_READER(),0640)) {
4505: my $mapid=$bighash{'map_pc_'.&clutter($map)};
4506: $title=$bighash{'title_'.$mapid.'.'.$resid};
4507: untie %bighash;
4508: }
4509: $title=~s/\&colon\;/\:/gs;
4510: if ($title) {
4511: return &do_cache(\%titlecache,$symb,$title,'title');
4512: }
4513: $urlsymb=$url;
4514: }
4515: my $title=&metadata($urlsymb,'title');
4516: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
4517: return $title;
4518: }
4519:
4520: # ------------------------------------------------- Update symbolic store links
4521:
4522: sub symblist {
4523: my ($mapname,%newhash)=@_;
4524: $mapname=&deversion(&declutter($mapname));
4525: my %hash;
4526: if (($ENV{'request.course.fn'}) && (%newhash)) {
4527: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
4528: &GDBM_WRCREAT(),0640)) {
4529: foreach (keys %newhash) {
4530: $hash{declutter($_)}=$mapname.'___'.&deversion($newhash{$_});
4531: }
4532: if (untie(%hash)) {
4533: return 'ok';
4534: }
4535: }
4536: }
4537: return 'error';
4538: }
4539:
4540: # --------------------------------------------------------------- Verify a symb
4541:
4542: sub symbverify {
4543: my ($symb,$thisurl)=@_;
4544: my $thisfn=$thisurl;
4545: # wrapper not part of symbs
4546: $thisfn=~s/^\/adm\/wrapper//;
4547: $thisfn=&declutter($thisfn);
4548: # direct jump to resource in page or to a sequence - will construct own symbs
4549: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
4550: # check URL part
4551: my ($map,$resid,$url)=&decode_symb($symb);
4552:
4553: unless ($url eq $thisfn) { return 0; }
4554:
4555: $symb=&symbclean($symb);
4556: $thisurl=&deversion($thisurl);
4557: $thisfn=&deversion($thisfn);
4558:
4559: my %bighash;
4560: my $okay=0;
4561:
4562: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
4563: &GDBM_READER(),0640)) {
4564: my $ids=$bighash{'ids_'.&clutter($thisurl)};
4565: unless ($ids) {
4566: $ids=$bighash{'ids_/'.$thisurl};
4567: }
4568: if ($ids) {
4569: # ------------------------------------------------------------------- Has ID(s)
4570: foreach (split(/\,/,$ids)) {
4571: my ($mapid,$resid)=split(/\./,$_);
4572: if (
4573: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
4574: eq $symb) {
4575: $okay=1;
4576: }
4577: }
4578: }
4579: untie(%bighash);
4580: }
4581: return $okay;
4582: }
4583:
4584: # --------------------------------------------------------------- Clean-up symb
4585:
4586: sub symbclean {
4587: my $symb=shift;
4588:
4589: # remove version from map
4590: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
4591:
4592: # remove version from URL
4593: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
4594:
4595: # remove wrapper
4596:
4597: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
4598: return $symb;
4599: }
4600:
4601: # ---------------------------------------------- Split symb to find map and url
4602:
4603: sub encode_symb {
4604: my ($map,$resid,$url)=@_;
4605: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
4606: }
4607:
4608: sub decode_symb {
4609: my ($map,$resid,$url)=split(/\_\_\_/,shift);
4610: return (&fixversion($map),$resid,&fixversion($url));
4611: }
4612:
4613: sub fixversion {
4614: my $fn=shift;
4615: if ($fn=~/^(adm|uploaded|public)/) { return $fn; }
4616: my %bighash;
4617: my $uri=&clutter($fn);
4618: my $key=$ENV{'request.course.id'}.'_'.$uri;
4619: # is this cached?
4620: my ($result,$cached)=&is_cached(\%courseresversioncache,$key,
4621: 'courseresversion',600);
4622: if (defined($cached)) { return $result; }
4623: # unfortunately not cached, or expired
4624: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
4625: &GDBM_READER(),0640)) {
4626: if ($bighash{'version_'.$uri}) {
4627: my $version=$bighash{'version_'.$uri};
4628: unless (($version eq 'mostrecent') ||
4629: ($version==&getversion($uri))) {
4630: $uri=~s/\.(\w+)$/\.$version\.$1/;
4631: }
4632: }
4633: untie %bighash;
4634: }
4635: return &do_cache
4636: (\%courseresversioncache,$key,&declutter($uri),'courseresversion');
4637: }
4638:
4639: sub deversion {
4640: my $url=shift;
4641: $url=~s/\.\d+\.(\w+)$/\.$1/;
4642: return $url;
4643: }
4644:
4645: # ------------------------------------------------------ Return symb list entry
4646:
4647: sub symbread {
4648: my ($thisfn,$donotrecurse)=@_;
4649: my $cache_str='request.symbread.cached.'.$thisfn;
4650: if (defined($ENV{$cache_str})) { return $ENV{$cache_str}; }
4651: # no filename provided? try from environment
4652: unless ($thisfn) {
4653: if ($ENV{'request.symb'}) {
4654: return $ENV{$cache_str}=&symbclean($ENV{'request.symb'});
4655: }
4656: $thisfn=$ENV{'request.filename'};
4657: }
4658: # is that filename actually a symb? Verify, clean, and return
4659: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
4660: if (&symbverify($thisfn,$1)) {
4661: return $ENV{$cache_str}=&symbclean($thisfn);
4662: }
4663: }
4664: $thisfn=declutter($thisfn);
4665: my %hash;
4666: my %bighash;
4667: my $syval='';
4668: if (($ENV{'request.course.fn'}) && ($thisfn)) {
4669: my $targetfn = $thisfn;
4670: if ( ($thisfn =~ m/^uploaded\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
4671: $targetfn = 'adm/wrapper/'.$thisfn;
4672: }
4673: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
4674: &GDBM_READER(),0640)) {
4675: $syval=$hash{$targetfn};
4676: untie(%hash);
4677: }
4678: # ---------------------------------------------------------- There was an entry
4679: if ($syval) {
4680: unless ($syval=~/\_\d+$/) {
4681: unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
4682: &appenv('request.ambiguous' => $thisfn);
4683: return $ENV{$cache_str}='';
4684: }
4685: $syval.=$1;
4686: }
4687: } else {
4688: # ------------------------------------------------------- Was not in symb table
4689: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
4690: &GDBM_READER(),0640)) {
4691: # ---------------------------------------------- Get ID(s) for current resource
4692: my $ids=$bighash{'ids_'.&clutter($thisfn)};
4693: unless ($ids) {
4694: $ids=$bighash{'ids_/'.$thisfn};
4695: }
4696: unless ($ids) {
4697: # alias?
4698: $ids=$bighash{'mapalias_'.$thisfn};
4699: }
4700: if ($ids) {
4701: # ------------------------------------------------------------------- Has ID(s)
4702: my @possibilities=split(/\,/,$ids);
4703: if ($#possibilities==0) {
4704: # ----------------------------------------------- There is only one possibility
4705: my ($mapid,$resid)=split(/\./,$ids);
4706: $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
4707: } elsif (!$donotrecurse) {
4708: # ------------------------------------------ There is more than one possibility
4709: my $realpossible=0;
4710: foreach (@possibilities) {
4711: my $file=$bighash{'src_'.$_};
4712: if (&allowed('bre',$file)) {
4713: my ($mapid,$resid)=split(/\./,$_);
4714: if ($bighash{'map_type_'.$mapid} ne 'page') {
4715: $realpossible++;
4716: $syval=declutter($bighash{'map_id_'.$mapid}).
4717: '___'.$resid;
4718: }
4719: }
4720: }
4721: if ($realpossible!=1) { $syval=''; }
4722: } else {
4723: $syval='';
4724: }
4725: }
4726: untie(%bighash)
4727: }
4728: }
4729: if ($syval) {
4730: return $ENV{$cache_str}=&symbclean($syval.'___'.$thisfn);
4731: }
4732: }
4733: &appenv('request.ambiguous' => $thisfn);
4734: return $ENV{$cache_str}='';
4735: }
4736:
4737: # ---------------------------------------------------------- Return random seed
4738:
4739: sub numval {
4740: my $txt=shift;
4741: $txt=~tr/A-J/0-9/;
4742: $txt=~tr/a-j/0-9/;
4743: $txt=~tr/K-T/0-9/;
4744: $txt=~tr/k-t/0-9/;
4745: $txt=~tr/U-Z/0-5/;
4746: $txt=~tr/u-z/0-5/;
4747: $txt=~s/\D//g;
4748: return int($txt);
4749: }
4750:
4751: sub numval2 {
4752: my $txt=shift;
4753: $txt=~tr/A-J/0-9/;
4754: $txt=~tr/a-j/0-9/;
4755: $txt=~tr/K-T/0-9/;
4756: $txt=~tr/k-t/0-9/;
4757: $txt=~tr/U-Z/0-5/;
4758: $txt=~tr/u-z/0-5/;
4759: $txt=~s/\D//g;
4760: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
4761: my $total;
4762: foreach my $val (@txts) { $total+=$val; }
4763: return int($total);
4764: }
4765:
4766: sub latest_rnd_algorithm_id {
4767: return '64bit3';
4768: }
4769:
4770: sub get_rand_alg {
4771: my ($courseid)=@_;
4772: if (!$courseid) { $courseid=(&Apache::lonxml::whichuser())[1]; }
4773: if ($courseid) {
4774: return $ENV{"course.$courseid.rndseed"};
4775: }
4776: return &latest_rnd_algorithm_id();
4777: }
4778:
4779: sub getCODE {
4780: if (defined($ENV{'form.CODE'})) { return $ENV{'form.CODE'}; }
4781: if (defined($Apache::lonhomework::parsing_a_problem) &&
4782: defined($Apache::lonhomework::history{'resource.CODE'})) {
4783: return $Apache::lonhomework::history{'resource.CODE'};
4784: }
4785: return undef;
4786: }
4787:
4788: sub rndseed {
4789: my ($symb,$courseid,$domain,$username)=@_;
4790:
4791: my ($wsymb,$wcourseid,$wdomain,$wusername)=&Apache::lonxml::whichuser();
4792: if (!$symb) {
4793: unless ($symb=$wsymb) { return time; }
4794: }
4795: if (!$courseid) { $courseid=$wcourseid; }
4796: if (!$domain) { $domain=$wdomain; }
4797: if (!$username) { $username=$wusername }
4798: my $which=&get_rand_alg();
4799: if (defined(&getCODE())) {
4800: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
4801: } elsif ($which eq '64bit3') {
4802: return &rndseed_64bit3($symb,$courseid,$domain,$username);
4803: } elsif ($which eq '64bit2') {
4804: return &rndseed_64bit2($symb,$courseid,$domain,$username);
4805: } elsif ($which eq '64bit') {
4806: return &rndseed_64bit($symb,$courseid,$domain,$username);
4807: }
4808: return &rndseed_32bit($symb,$courseid,$domain,$username);
4809: }
4810:
4811: sub rndseed_32bit {
4812: my ($symb,$courseid,$domain,$username)=@_;
4813: {
4814: use integer;
4815: my $symbchck=unpack("%32C*",$symb) << 27;
4816: my $symbseed=numval($symb) << 22;
4817: my $namechck=unpack("%32C*",$username) << 17;
4818: my $nameseed=numval($username) << 12;
4819: my $domainseed=unpack("%32C*",$domain) << 7;
4820: my $courseseed=unpack("%32C*",$courseid);
4821: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
4822: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
4823: #&Apache::lonxml::debug("rndseed :$num:$symb");
4824: return $num;
4825: }
4826: }
4827:
4828: sub rndseed_64bit {
4829: my ($symb,$courseid,$domain,$username)=@_;
4830: {
4831: use integer;
4832: my $symbchck=unpack("%32S*",$symb) << 21;
4833: my $symbseed=numval($symb) << 10;
4834: my $namechck=unpack("%32S*",$username);
4835:
4836: my $nameseed=numval($username) << 21;
4837: my $domainseed=unpack("%32S*",$domain) << 10;
4838: my $courseseed=unpack("%32S*",$courseid);
4839:
4840: my $num1=$symbchck+$symbseed+$namechck;
4841: my $num2=$nameseed+$domainseed+$courseseed;
4842: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
4843: #&Apache::lonxml::debug("rndseed :$num:$symb");
4844: return "$num1,$num2";
4845: }
4846: }
4847:
4848: sub rndseed_64bit2 {
4849: my ($symb,$courseid,$domain,$username)=@_;
4850: {
4851: use integer;
4852: # strings need to be an even # of cahracters long, it it is odd the
4853: # last characters gets thrown away
4854: my $symbchck=unpack("%32S*",$symb.' ') << 21;
4855: my $symbseed=numval($symb) << 10;
4856: my $namechck=unpack("%32S*",$username.' ');
4857:
4858: my $nameseed=numval($username) << 21;
4859: my $domainseed=unpack("%32S*",$domain.' ') << 10;
4860: my $courseseed=unpack("%32S*",$courseid.' ');
4861:
4862: my $num1=$symbchck+$symbseed+$namechck;
4863: my $num2=$nameseed+$domainseed+$courseseed;
4864: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
4865: #&Apache::lonxml::debug("rndseed :$num:$symb");
4866: return "$num1,$num2";
4867: }
4868: }
4869:
4870: sub rndseed_64bit3 {
4871: my ($symb,$courseid,$domain,$username)=@_;
4872: {
4873: use integer;
4874: # strings need to be an even # of cahracters long, it it is odd the
4875: # last characters gets thrown away
4876: my $symbchck=unpack("%32S*",$symb.' ') << 21;
4877: my $symbseed=numval2($symb) << 10;
4878: my $namechck=unpack("%32S*",$username.' ');
4879:
4880: my $nameseed=numval2($username) << 21;
4881: my $domainseed=unpack("%32S*",$domain.' ') << 10;
4882: my $courseseed=unpack("%32S*",$courseid.' ');
4883:
4884: my $num1=$symbchck+$symbseed+$namechck;
4885: my $num2=$nameseed+$domainseed+$courseseed;
4886: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
4887: #&Apache::lonxml::debug("rndseed :$num:$symb");
4888: return "$num1:$num2";
4889: }
4890: }
4891:
4892: sub rndseed_CODE_64bit {
4893: my ($symb,$courseid,$domain,$username)=@_;
4894: {
4895: use integer;
4896: my $symbchck=unpack("%32S*",$symb.' ') << 16;
4897: my $symbseed=numval2($symb);
4898: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
4899: my $CODEseed=numval(&getCODE());
4900: my $courseseed=unpack("%32S*",$courseid.' ');
4901: my $num1=$symbseed+$CODEchck;
4902: my $num2=$CODEseed+$courseseed+$symbchck;
4903: #&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
4904: #&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
4905: return "$num1:$num2";
4906: }
4907: }
4908:
4909: sub setup_random_from_rndseed {
4910: my ($rndseed)=@_;
4911: if ($rndseed =~/([,:])/) {
4912: my ($num1,$num2)=split(/[,:]/,$rndseed);
4913: &Math::Random::random_set_seed(abs($num1),abs($num2));
4914: } else {
4915: &Math::Random::random_set_seed_from_phrase($rndseed);
4916: }
4917: }
4918:
4919: sub latest_receipt_algorithm_id {
4920: return 'receipt2';
4921: }
4922:
4923: sub recunique {
4924: my $fucourseid=shift;
4925: my $unique;
4926: if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2') {
4927: $unique=$ENV{"course.$fucourseid.internal.encseed"};
4928: } else {
4929: $unique=$perlvar{'lonReceipt'};
4930: }
4931: return unpack("%32C*",$unique);
4932: }
4933:
4934: sub recprefix {
4935: my $fucourseid=shift;
4936: my $prefix;
4937: if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2') {
4938: $prefix=$ENV{"course.$fucourseid.internal.encpref"};
4939: } else {
4940: $prefix=$perlvar{'lonHostID'};
4941: }
4942: return unpack("%32C*",$prefix);
4943: }
4944:
4945: sub ireceipt {
4946: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
4947: my $cuname=unpack("%32C*",$funame);
4948: my $cudom=unpack("%32C*",$fudom);
4949: my $cucourseid=unpack("%32C*",$fucourseid);
4950: my $cusymb=unpack("%32C*",$fusymb);
4951: my $cunique=&recunique($fucourseid);
4952: my $cpart=unpack("%32S*",$part);
4953: my $return =&recprefix($fucourseid).'-';
4954: if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
4955: $ENV{'request.state'} eq 'construct') {
4956: &Apache::lonxml::debug("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname).
4957: " and ".($cpart%$cudom));
4958:
4959: $return.= ($cunique%$cuname+
4960: $cunique%$cudom+
4961: $cusymb%$cuname+
4962: $cusymb%$cudom+
4963: $cucourseid%$cuname+
4964: $cucourseid%$cudom+
4965: $cpart%$cuname+
4966: $cpart%$cudom);
4967: } else {
4968: $return.= ($cunique%$cuname+
4969: $cunique%$cudom+
4970: $cusymb%$cuname+
4971: $cusymb%$cudom+
4972: $cucourseid%$cuname+
4973: $cucourseid%$cudom);
4974: }
4975: return $return;
4976: }
4977:
4978: sub receipt {
4979: my ($part)=@_;
4980: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
4981: return &ireceipt($name,$domain,$courseid,$symb,$part);
4982: }
4983:
4984: # ------------------------------------------------------------ Serves up a file
4985: # returns either the contents of the file or
4986: # -1 if the file doesn't exist
4987: #
4988: # if the target is a file that was uploaded via DOCS,
4989: # a check will be made to see if a current copy exists on the local server,
4990: # if it does this will be served, otherwise a copy will be retrieved from
4991: # the home server for the course and stored in /home/httpd/html/userfiles on
4992: # the local server.
4993:
4994: sub getfile {
4995: my ($file) = @_;
4996:
4997: if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
4998: &repcopy($file);
4999: return &readfile($file);
5000: }
5001:
5002: sub repcopy_userfile {
5003: my ($file)=@_;
5004:
5005: if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
5006: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return OK; }
5007:
5008: my ($cdom,$cnum,$filename) =
5009: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+([^/]+)/+([^/]+)/+(.*)|);
5010: my ($info,$rtncode);
5011: my $uri="/uploaded/$cdom/$cnum/$filename";
5012: if (-e "$file") {
5013: my @fileinfo = stat($file);
5014: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
5015: if ($lwpresp ne 'ok') {
5016: if ($rtncode eq '404') {
5017: unlink($file);
5018: }
5019: #my $ua=new LWP::UserAgent;
5020: #my $request=new HTTP::Request('GET',&tokenwrapper($uri));
5021: #my $response=$ua->request($request);
5022: #if ($response->is_success()) {
5023: # return $response->content;
5024: # } else {
5025: # return -1;
5026: # }
5027: return -1;
5028: }
5029: if ($info < $fileinfo[9]) {
5030: return OK;
5031: }
5032: $info = '';
5033: $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
5034: if ($lwpresp ne 'ok') {
5035: return -1;
5036: }
5037: } else {
5038: my $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
5039: if ($lwpresp ne 'ok') {
5040: my $ua=new LWP::UserAgent;
5041: my $request=new HTTP::Request('GET',&tokenwrapper($uri));
5042: my $response=$ua->request($request);
5043: if ($response->is_success()) {
5044: $info=$response->content;
5045: } else {
5046: return -1;
5047: }
5048: }
5049: my @parts = ($cdom,$cnum);
5050: if ($filename =~ m|^(.+)/[^/]+$|) {
5051: push @parts, split(/\//,$1);
5052: }
5053: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
5054: foreach my $part (@parts) {
5055: $path .= '/'.$part;
5056: if (!-e $path) {
5057: mkdir($path,0770);
5058: }
5059: }
5060: }
5061: open(FILE,">$file");
5062: print FILE $info;
5063: close(FILE);
5064: return OK;
5065: }
5066:
5067: sub tokenwrapper {
5068: my $uri=shift;
5069: $uri=~s/^http\:\/\/([^\/]+)//;
5070: $uri=~s/^\///;
5071: $ENV{'user.environment'}=~/\/([^\/]+)\.id/;
5072: my $token=$1;
5073: if ($uri=~/^uploaded\/([^\/]+)\/([^\/]+)\/([^\/]+)(\?\.*)*$/) {
5074: &appenv('userfile.'.$1.'/'.$2.'/'.$3 => $ENV{'request.course.id'});
5075: return 'http://'.$hostname{ &homeserver($2,$1)}.'/'.$uri.
5076: (($uri=~/\?/)?'&':'?').'token='.$token.
5077: '&tokenissued='.$perlvar{'lonHostID'};
5078: } else {
5079: return '/adm/notfound.html';
5080: }
5081: }
5082:
5083: sub getuploaded {
5084: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
5085: $uri=~s/^\///;
5086: $uri = 'http://'.$hostname{ &homeserver($cnum,$cdom)}.'/raw/'.$uri;
5087: my $ua=new LWP::UserAgent;
5088: my $request=new HTTP::Request($reqtype,$uri);
5089: my $response=$ua->request($request);
5090: $$rtncode = $response->code;
5091: if (! $response->is_success()) {
5092: return 'failed';
5093: }
5094: if ($reqtype eq 'HEAD') {
5095: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
5096: } elsif ($reqtype eq 'GET') {
5097: $$info = $response->content;
5098: }
5099: return 'ok';
5100: }
5101:
5102: sub readfile {
5103: my $file = shift;
5104: if ( (! -e $file ) || ($file eq '') ) { return -1; };
5105: my $fh;
5106: open($fh,"<$file");
5107: my $a='';
5108: while (<$fh>) { $a .=$_; }
5109: return $a;
5110: }
5111:
5112: sub filelocation {
5113: my ($dir,$file) = @_;
5114: my $location;
5115: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
5116: if ($file=~m:^/~:) { # is a contruction space reference
5117: $location = $file;
5118: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
5119: } elsif ($file=~/^\/*uploaded/) { # is an uploaded file
5120: my ($udom,$uname,$filename)=
5121: ($file=~m|^/+uploaded/+([^/]+)/+([^/]+)/+(.*)$|);
5122: my $home=&homeserver($uname,$udom);
5123: my $is_me=0;
5124: my @ids=¤t_machine_ids();
5125: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
5126: if ($is_me) {
5127: $location=&Apache::loncommon::propath($udom,$uname).
5128: '/userfiles/'.$filename;
5129: } else {
5130: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
5131: $udom.'/'.$uname.'/'.$filename;
5132: }
5133: } else {
5134: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
5135: $file=~s:^/res/:/:;
5136: if ( !( $file =~ m:^/:) ) {
5137: $location = $dir. '/'.$file;
5138: } else {
5139: $location = '/home/httpd/html/res'.$file;
5140: }
5141: }
5142: $location=~s://+:/:g; # remove duplicate /
5143: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
5144: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
5145: return $location;
5146: }
5147:
5148: sub hreflocation {
5149: my ($dir,$file)=@_;
5150: unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
5151: my $finalpath=filelocation($dir,$file);
5152: $finalpath=~s-^/home/httpd/html--;
5153: $finalpath=~s-^/home/(\w+)/public_html/-/~$1/-;
5154: return $finalpath;
5155: } elsif ($file=~m-^/home-) {
5156: $file=~s-^/home/httpd/html--;
5157: $file=~s-^/home/(\w+)/public_html/-/~$1/-;
5158: return $file;
5159: }
5160: return $file;
5161: }
5162:
5163: sub current_machine_domains {
5164: my $hostname=$hostname{$perlvar{'lonHostID'}};
5165: my @domains;
5166: while( my($id, $name) = each(%hostname)) {
5167: # &logthis("-$id-$name-$hostname-");
5168: if ($hostname eq $name) {
5169: push(@domains,$hostdom{$id});
5170: }
5171: }
5172: return @domains;
5173: }
5174:
5175: sub current_machine_ids {
5176: my $hostname=$hostname{$perlvar{'lonHostID'}};
5177: my @ids;
5178: while( my($id, $name) = each(%hostname)) {
5179: # &logthis("-$id-$name-$hostname-");
5180: if ($hostname eq $name) {
5181: push(@ids,$id);
5182: }
5183: }
5184: return @ids;
5185: }
5186:
5187: # ------------------------------------------------------------- Declutters URLs
5188:
5189: sub declutter {
5190: my $thisfn=shift;
5191: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
5192: $thisfn=~s/^\///;
5193: $thisfn=~s/^res\///;
5194: $thisfn=~s/\?.+$//;
5195: return $thisfn;
5196: }
5197:
5198: # ------------------------------------------------------------- Clutter up URLs
5199:
5200: sub clutter {
5201: my $thisfn='/'.&declutter(shift);
5202: unless ($thisfn=~/^\/(uploaded|adm|userfiles|ext|raw|priv|public)\//) {
5203: $thisfn='/res'.$thisfn;
5204: }
5205: return $thisfn;
5206: }
5207:
5208: # -------------------------------------------------------- Escape Special Chars
5209:
5210: sub escape {
5211: my $str=shift;
5212: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
5213: return $str;
5214: }
5215:
5216: # ----------------------------------------------------- Un-Escape Special Chars
5217:
5218: sub unescape {
5219: my $str=shift;
5220: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
5221: return $str;
5222: }
5223:
5224: sub mod_perl_version {
5225: if (defined($perlvar{'MODPERL2'})) {
5226: return 2;
5227: }
5228: return 1;
5229: }
5230:
5231: sub correct_line_ends {
5232: my ($result)=@_;
5233: $$result =~s/\r\n/\n/mg;
5234: $$result =~s/\r/\n/mg;
5235: }
5236: # ================================================================ Main Program
5237:
5238: sub goodbye {
5239: &logthis("Starting Shut down");
5240: #not converted to using infrastruture and probably shouldn't be
5241: &logthis(sprintf("%-20s is %s",'%badServerCache',scalar(%badServerCache)));
5242: #converted
5243: &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
5244: &logthis(sprintf("%-20s is %s",'%homecache',scalar(%homecache)));
5245: &logthis(sprintf("%-20s is %s",'%titlecache',scalar(%titlecache)));
5246: &logthis(sprintf("%-20s is %s",'%courseresdatacache',scalar(%courseresdatacache)));
5247: #1.1 only
5248: &logthis(sprintf("%-20s is %s",'%userresdatacache',scalar(%userresdatacache)));
5249: &logthis(sprintf("%-20s is %s",'%usectioncache',scalar(%usectioncache)));
5250: &logthis(sprintf("%-20s is %s",'%courseresversioncache',scalar(%courseresversioncache)));
5251: &logthis(sprintf("%-20s is %s",'%resversioncache',scalar(%resversioncache)));
5252: &flushcourselogs();
5253: &logthis("Shutting down");
5254: return DONE;
5255: }
5256:
5257: BEGIN {
5258: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
5259: unless ($readit) {
5260: {
5261: open(my $config,"</etc/httpd/conf/loncapa.conf");
5262:
5263: while (my $configline=<$config>) {
5264: if ($configline=~/\S/ && $configline =~ /^[^\#]*PerlSetVar/) {
5265: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
5266: chomp($varvalue);
5267: $perlvar{$varname}=$varvalue;
5268: }
5269: }
5270: close($config);
5271: }
5272: {
5273: open(my $config,"</etc/httpd/conf/loncapa_apache.conf");
5274:
5275: while (my $configline=<$config>) {
5276: if ($configline =~ /^[^\#]*PerlSetVar/) {
5277: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
5278: chomp($varvalue);
5279: $perlvar{$varname}=$varvalue;
5280: }
5281: }
5282: close($config);
5283: }
5284:
5285: # ------------------------------------------------------------ Read domain file
5286: {
5287: %domaindescription = ();
5288: %domain_auth_def = ();
5289: %domain_auth_arg_def = ();
5290: my $fh;
5291: if (open($fh,"<".$Apache::lonnet::perlvar{'lonTabDir'}.'/domain.tab')) {
5292: while (<$fh>) {
5293: next if (/^(\#|\s*$)/);
5294: # next if /^\#/;
5295: chomp;
5296: my ($domain, $domain_description, $def_auth, $def_auth_arg,
5297: $def_lang, $city, $longi, $lati) = split(/:/,$_);
5298: $domain_auth_def{$domain}=$def_auth;
5299: $domain_auth_arg_def{$domain}=$def_auth_arg;
5300: $domaindescription{$domain}=$domain_description;
5301: $domain_lang_def{$domain}=$def_lang;
5302: $domain_city{$domain}=$city;
5303: $domain_longi{$domain}=$longi;
5304: $domain_lati{$domain}=$lati;
5305:
5306: # &logthis("Domain.tab: $domain, $domain_auth_def{$domain}, $domain_auth_arg_def{$domain},$domaindescription{$domain}");
5307: # &logthis("Domain.tab: $domain ".$domaindescription{$domain} );
5308: }
5309: }
5310: close ($fh);
5311: }
5312:
5313:
5314: # ------------------------------------------------------------- Read hosts file
5315: {
5316: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
5317:
5318: while (my $configline=<$config>) {
5319: next if ($configline =~ /^(\#|\s*$)/);
5320: chomp($configline);
5321: my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
5322: if ($id && $domain && $role && $name && $ip) {
5323: $hostname{$id}=$name;
5324: $hostdom{$id}=$domain;
5325: $hostip{$id}=$ip;
5326: $iphost{$ip}=$id;
5327: if ($role eq 'library') { $libserv{$id}=$name; }
5328: }
5329: }
5330: close($config);
5331: }
5332:
5333: # ------------------------------------------------------ Read spare server file
5334: {
5335: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
5336:
5337: while (my $configline=<$config>) {
5338: chomp($configline);
5339: if ($configline) {
5340: $spareid{$configline}=1;
5341: }
5342: }
5343: close($config);
5344: }
5345: # ------------------------------------------------------------ Read permissions
5346: {
5347: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
5348:
5349: while (my $configline=<$config>) {
5350: chomp($configline);
5351: if ($configline) {
5352: my ($role,$perm)=split(/ /,$configline);
5353: if ($perm ne '') { $pr{$role}=$perm; }
5354: }
5355: }
5356: close($config);
5357: }
5358:
5359: # -------------------------------------------- Read plain texts for permissions
5360: {
5361: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
5362:
5363: while (my $configline=<$config>) {
5364: chomp($configline);
5365: if ($configline) {
5366: my ($short,$plain)=split(/:/,$configline);
5367: if ($plain ne '') { $prp{$short}=$plain; }
5368: }
5369: }
5370: close($config);
5371: }
5372:
5373: # ---------------------------------------------------------- Read package table
5374: {
5375: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
5376:
5377: while (my $configline=<$config>) {
5378: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
5379: chomp($configline);
5380: my ($short,$plain)=split(/:/,$configline);
5381: my ($pack,$name)=split(/\&/,$short);
5382: if ($plain ne '') {
5383: $packagetab{$pack.'&'.$name.'&name'}=$name;
5384: $packagetab{$short}=$plain;
5385: }
5386: }
5387: close($config);
5388: }
5389:
5390: # ------------- set up temporary directory
5391: {
5392: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
5393:
5394: }
5395:
5396: %metacache=();
5397:
5398: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
5399: $dumpcount=0;
5400:
5401: &logtouch();
5402: &logthis('<font color=yellow>INFO: Read configuration</font>');
5403: $readit=1;
5404: }
5405: }
5406:
5407: 1;
5408: __END__
5409:
5410: =pod
5411:
5412: =head1 NAME
5413:
5414: Apache::lonnet - Subroutines to ask questions about things in the network.
5415:
5416: =head1 SYNOPSIS
5417:
5418: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
5419:
5420: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
5421:
5422: Common parameters:
5423:
5424: =over 4
5425:
5426: =item *
5427:
5428: $uname : an internal username (if $cname expecting a course Id specifically)
5429:
5430: =item *
5431:
5432: $udom : a domain (if $cdom expecting a course's domain specifically)
5433:
5434: =item *
5435:
5436: $symb : a resource instance identifier
5437:
5438: =item *
5439:
5440: $namespace : the name of a .db file that contains the data needed or
5441: being set.
5442:
5443: =back
5444:
5445: =head1 OVERVIEW
5446:
5447: lonnet provides subroutines which interact with the
5448: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
5449: about classes, users, and resources.
5450:
5451: For many of these objects you can also use this to store data about
5452: them or modify them in various ways.
5453:
5454: =head2 Symbs
5455:
5456: To identify a specific instance of a resource, LON-CAPA uses symbols
5457: or "symbs"X<symb>. These identifiers are built from the URL of the
5458: map, the resource number of the resource in the map, and the URL of
5459: the resource itself. The latter is somewhat redundant, but might help
5460: if maps change.
5461:
5462: An example is
5463:
5464: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
5465:
5466: The respective map entry is
5467:
5468: <resource id="19" src="/res/msu/korte/tests/part12.problem"
5469: title="Problem 2">
5470: </resource>
5471:
5472: Symbs are used by the random number generator, as well as to store and
5473: restore data specific to a certain instance of for example a problem.
5474:
5475: =head2 Storing And Retrieving Data
5476:
5477: X<store()>X<cstore()>X<restore()>Three of the most important functions
5478: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
5479: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
5480: is is the non-critical message twin of cstore. These functions are for
5481: handlers to store a perl hash to a user's permanent data space in an
5482: easy manner, and to retrieve it again on another call. It is expected
5483: that a handler would use this once at the beginning to retrieve data,
5484: and then again once at the end to send only the new data back.
5485:
5486: The data is stored in the user's data directory on the user's
5487: homeserver under the ID of the course.
5488:
5489: The hash that is returned by restore will have all of the previous
5490: value for all of the elements of the hash.
5491:
5492: Example:
5493:
5494: #creating a hash
5495: my %hash;
5496: $hash{'foo'}='bar';
5497:
5498: #storing it
5499: &Apache::lonnet::cstore(\%hash);
5500:
5501: #changing a value
5502: $hash{'foo'}='notbar';
5503:
5504: #adding a new value
5505: $hash{'bar'}='foo';
5506: &Apache::lonnet::cstore(\%hash);
5507:
5508: #retrieving the hash
5509: my %history=&Apache::lonnet::restore();
5510:
5511: #print the hash
5512: foreach my $key (sort(keys(%history))) {
5513: print("\%history{$key} = $history{$key}");
5514: }
5515:
5516: Will print out:
5517:
5518: %history{1:foo} = bar
5519: %history{1:keys} = foo:timestamp
5520: %history{1:timestamp} = 990455579
5521: %history{2:bar} = foo
5522: %history{2:foo} = notbar
5523: %history{2:keys} = foo:bar:timestamp
5524: %history{2:timestamp} = 990455580
5525: %history{bar} = foo
5526: %history{foo} = notbar
5527: %history{timestamp} = 990455580
5528: %history{version} = 2
5529:
5530: Note that the special hash entries C<keys>, C<version> and
5531: C<timestamp> were added to the hash. C<version> will be equal to the
5532: total number of versions of the data that have been stored. The
5533: C<timestamp> attribute will be the UNIX time the hash was
5534: stored. C<keys> is available in every historical section to list which
5535: keys were added or changed at a specific historical revision of a
5536: hash.
5537:
5538: B<Warning>: do not store the hash that restore returns directly. This
5539: will cause a mess since it will restore the historical keys as if the
5540: were new keys. I.E. 1:foo will become 1:1:foo etc.
5541:
5542: Calling convention:
5543:
5544: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
5545: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
5546:
5547: For more detailed information, see lonnet specific documentation.
5548:
5549: =head1 RETURN MESSAGES
5550:
5551: =over 4
5552:
5553: =item * B<con_lost>: unable to contact remote host
5554:
5555: =item * B<con_delayed>: unable to contact remote host, message will be delivered
5556: when the connection is brought back up
5557:
5558: =item * B<con_failed>: unable to contact remote host and unable to save message
5559: for later delivery
5560:
5561: =item * B<error:>: an error a occured, a description of the error follows the :
5562:
5563: =item * B<no_such_host>: unable to fund a host associated with the user/domain
5564: that was requested
5565:
5566: =back
5567:
5568: =head1 PUBLIC SUBROUTINES
5569:
5570: =head2 Session Environment Functions
5571:
5572: =over 4
5573:
5574: =item *
5575: X<appenv()>
5576: B<appenv(%hash)>: the value of %hash is written to
5577: the user envirnoment file, and will be restored for each access this
5578: user makes during this session, also modifies the %ENV for the current
5579: process
5580:
5581: =item *
5582: X<delenv()>
5583: B<delenv($regexp)>: removes all items from the session
5584: environment file that matches the regular expression in $regexp. The
5585: values are also delted from the current processes %ENV.
5586:
5587: =back
5588:
5589: =head2 User Information
5590:
5591: =over 4
5592:
5593: =item *
5594: X<queryauthenticate()>
5595: B<queryauthenticate($uname,$udom)>: try to determine user's current
5596: authentication scheme
5597:
5598: =item *
5599: X<authenticate()>
5600: B<authenticate($uname,$upass,$udom)>: try to
5601: authenticate user from domain's lib servers (first use the current
5602: one). C<$upass> should be the users password.
5603:
5604: =item *
5605: X<homeserver()>
5606: B<homeserver($uname,$udom)>: find the server which has
5607: the user's directory and files (there must be only one), this caches
5608: the answer, and also caches if there is a borken connection.
5609:
5610: =item *
5611: X<idget()>
5612: B<idget($udom,@ids)>: find the usernames behind a list of IDs
5613: (IDs are a unique resource in a domain, there must be only 1 ID per
5614: username, and only 1 username per ID in a specific domain) (returns
5615: hash: id=>name,id=>name)
5616:
5617: =item *
5618: X<idrget()>
5619: B<idrget($udom,@unames)>: find the IDs behind a list of
5620: usernames (returns hash: name=>id,name=>id)
5621:
5622: =item *
5623: X<idput()>
5624: B<idput($udom,%ids)>: store away a list of names and associated IDs
5625:
5626: =item *
5627: X<rolesinit()>
5628: B<rolesinit($udom,$username,$authhost)>: get user privileges
5629:
5630: =item *
5631: X<usection()>
5632: B<usection($udom,$uname,$cname)>: finds the section of student in the
5633: course $cname, return section name/number or '' for "not in course"
5634: and '-1' for "no section"
5635:
5636: =item *
5637: X<userenvironment()>
5638: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
5639: passed in @what from the requested user's environment, returns a hash
5640:
5641: =back
5642:
5643: =head2 User Roles
5644:
5645: =over 4
5646:
5647: =item *
5648:
5649: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
5650: actions
5651: F: full access
5652: U,I,K: authentication modes (cxx only)
5653: '': forbidden
5654: 1: user needs to choose course
5655: 2: browse allowed
5656:
5657: =item *
5658:
5659: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
5660: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
5661: and course level
5662:
5663: =item *
5664:
5665: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
5666: explanation of a user role term
5667:
5668: =back
5669:
5670: =head2 User Modification
5671:
5672: =over 4
5673:
5674: =item *
5675:
5676: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
5677: user for the level given by URL. Optional start and end dates (leave empty
5678: string or zero for "no date")
5679:
5680: =item *
5681:
5682: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
5683: change a users, password, possible return values are: ok,
5684: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
5685: refused
5686:
5687: =item *
5688:
5689: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
5690:
5691: =item *
5692:
5693: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
5694: modify user
5695:
5696: =item *
5697:
5698: modifystudent
5699:
5700: modify a students enrollment and identification information.
5701: The course id is resolved based on the current users environment.
5702: This means the envoking user must be a course coordinator or otherwise
5703: associated with a course.
5704:
5705: This call is essentially a wrapper for lonnet::modifyuser and
5706: lonnet::modify_student_enrollment
5707:
5708: Inputs:
5709:
5710: =over 4
5711:
5712: =item B<$udom> Students loncapa domain
5713:
5714: =item B<$uname> Students loncapa login name
5715:
5716: =item B<$uid> Students id/student number
5717:
5718: =item B<$umode> Students authentication mode
5719:
5720: =item B<$upass> Students password
5721:
5722: =item B<$first> Students first name
5723:
5724: =item B<$middle> Students middle name
5725:
5726: =item B<$last> Students last name
5727:
5728: =item B<$gene> Students generation
5729:
5730: =item B<$usec> Students section in course
5731:
5732: =item B<$end> Unix time of the roles expiration
5733:
5734: =item B<$start> Unix time of the roles start date
5735:
5736: =item B<$forceid> If defined, allow $uid to be changed
5737:
5738: =item B<$desiredhome> server to use as home server for student
5739:
5740: =back
5741:
5742: =item *
5743:
5744: modify_student_enrollment
5745:
5746: Change a students enrollment status in a class. The environment variable
5747: 'role.request.course' must be defined for this function to proceed.
5748:
5749: Inputs:
5750:
5751: =over 4
5752:
5753: =item $udom, students domain
5754:
5755: =item $uname, students name
5756:
5757: =item $uid, students user id
5758:
5759: =item $first, students first name
5760:
5761: =item $middle
5762:
5763: =item $last
5764:
5765: =item $gene
5766:
5767: =item $usec
5768:
5769: =item $end
5770:
5771: =item $start
5772:
5773: =back
5774:
5775:
5776: =item *
5777:
5778: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
5779: custom role; give a custom role to a user for the level given by URL. Specify
5780: name and domain of role author, and role name
5781:
5782: =item *
5783:
5784: revokerole($udom,$uname,$url,$role) : revoke a role for url
5785:
5786: =item *
5787:
5788: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
5789:
5790: =back
5791:
5792: =head2 Course Infomation
5793:
5794: =over 4
5795:
5796: =item *
5797:
5798: coursedescription($courseid) : course description
5799:
5800: =item *
5801:
5802: courseresdata($coursenum,$coursedomain,@which) : request for current
5803: parameter setting for a specific course, @what should be a list of
5804: parameters to ask about. This routine caches answers for 5 minutes.
5805:
5806: =back
5807:
5808: =head2 Course Modification
5809:
5810: =over 4
5811:
5812: =item *
5813:
5814: writecoursepref($courseid,%prefs) : write preferences (environment
5815: database) for a course
5816:
5817: =item *
5818:
5819: createcourse($udom,$description,$url) : make/modify course
5820:
5821: =back
5822:
5823: =head2 Resource Subroutines
5824:
5825: =over 4
5826:
5827: =item *
5828:
5829: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
5830:
5831: =item *
5832:
5833: repcopy($filename) : subscribes to the requested file, and attempts to
5834: replicate from the owning library server, Might return
5835: HTTP_SERVICE_UNAVAILABLE, HTTP_NOT_FOUND, FORBIDDEN, OK, or
5836: HTTP_BAD_REQUEST, also attempts to grab the metadata for the
5837: resource. Expects the local filesystem pathname
5838: (/home/httpd/html/res/....)
5839:
5840: =back
5841:
5842: =head2 Resource Information
5843:
5844: =over 4
5845:
5846: =item *
5847:
5848: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
5849: a vairety of different possible values, $varname should be a request
5850: string, and the other parameters can be used to specify who and what
5851: one is asking about.
5852:
5853: Possible values for $varname are environment.lastname (or other item
5854: from the envirnment hash), user.name (or someother aspect about the
5855: user), resource.0.maxtries (or some other part and parameter of a
5856: resource)
5857:
5858: =item *
5859:
5860: directcondval($number) : get current value of a condition; reads from a state
5861: string
5862:
5863: =item *
5864:
5865: condval($condidx) : value of condition index based on state
5866:
5867: =item *
5868:
5869: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
5870: resource's metadata, $what should be either a specific key, or either
5871: 'keys' (to get a list of possible keys) or 'packages' to get a list of
5872: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
5873:
5874: this function automatically caches all requests
5875:
5876: =item *
5877:
5878: metadata_query($query,$custom,$customshow) : make a metadata query against the
5879: network of library servers; returns file handle of where SQL and regex results
5880: will be stored for query
5881:
5882: =item *
5883:
5884: symbread($filename) : return symbolic list entry (filename argument optional);
5885: returns the data handle
5886:
5887: =item *
5888:
5889: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
5890: a possible symb for the URL in $thisfn, returns a 1 on success, 0 on
5891: failure, user must be in a course, as it assumes the existance of the
5892: course initi hash, and uses $ENV('request.course.id'}
5893:
5894:
5895: =item *
5896:
5897: symbclean($symb) : removes versions numbers from a symb, returns the
5898: cleaned symb
5899:
5900: =item *
5901:
5902: is_on_map($uri) : checks if the $uri is somewhere on the current
5903: course map, user must be in a course for it to work.
5904:
5905: =item *
5906:
5907: numval($salt) : return random seed value (addend for rndseed)
5908:
5909: =item *
5910:
5911: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
5912: a random seed, all arguments are optional, if they aren't sent it uses the
5913: environment to derive them. Note: if symb isn't sent and it can't get one
5914: from &symbread it will use the current time as its return value
5915:
5916: =item *
5917:
5918: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
5919: unfakeable, receipt
5920:
5921: =item *
5922:
5923: receipt() : API to ireceipt working off of ENV values; given out to users
5924:
5925: =item *
5926:
5927: countacc($url) : count the number of accesses to a given URL
5928:
5929: =item *
5930:
5931: checkout($symb,$tuname,$tudom,$tcrsid) : creates a record of a user having looked at an item, most likely printed out or otherwise using a resource
5932:
5933: =item *
5934:
5935: checkin($token) : updates that a resource has beeen returned (a hard copy version for instance) and returns the data that $token was Checkout with ($symb, $tuname, $tudom, and $tcrsid)
5936:
5937: =item *
5938:
5939: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
5940:
5941: =item *
5942:
5943: devalidate($symb) : devalidate temporary spreadsheet calculations,
5944: forcing spreadsheet to reevaluate the resource scores next time.
5945:
5946: =back
5947:
5948: =head2 Storing/Retreiving Data
5949:
5950: =over 4
5951:
5952: =item *
5953:
5954: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
5955: for this url; hashref needs to be given and should be a \%hashname; the
5956: remaining args aren't required and if they aren't passed or are '' they will
5957: be derived from the ENV
5958:
5959: =item *
5960:
5961: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
5962: uses critical subroutine
5963:
5964: =item *
5965:
5966: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
5967: all args are optional
5968:
5969: =item *
5970:
5971: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
5972: works very similar to store/cstore, but all data is stored in a
5973: temporary location and can be reset using tmpreset, $storehash should
5974: be a hash reference, returns nothing on success
5975:
5976: =item *
5977:
5978: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
5979: similar to restore, but all data is stored in a temporary location and
5980: can be reset using tmpreset. Returns a hash of values on success,
5981: error string otherwise.
5982:
5983: =item *
5984:
5985: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
5986: deltes all keys for $symb form the temporary storage hash.
5987:
5988: =item *
5989:
5990: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
5991: reference filled in from namesp ($udom and $uname are optional)
5992:
5993: =item *
5994:
5995: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
5996: namesp ($udom and $uname are optional)
5997:
5998: =item *
5999:
6000: dump($namespace,$udom,$uname,$regexp) :
6001: dumps the complete (or key matching regexp) namespace into a hash
6002: ($udom, $uname and $regexp are optional)
6003:
6004: =item *
6005:
6006: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
6007: $store can be a scalar, an array reference, or if the amount to be
6008: incremented is > 1, a hash reference.
6009:
6010: ($udom and $uname are optional)
6011:
6012: =item *
6013:
6014: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
6015: ($udom and $uname are optional)
6016:
6017: =item *
6018:
6019: putstore($namespace,$storehash,$udomain,$uname) : stores hash in namesp
6020: keys used in storehash include version information (e.g., 1:$symb:message etc.) as
6021: used in records written by &store and retrieved by &restore. This function
6022: was created for use in editing discussion posts, without incrementing the
6023: version number included in the key for a particular post. The colon
6024: separated list of attribute names (e.g., the value associated with the key
6025: 1:keys:$symb) is also generated and passed in the ampersand separated
6026: items sent to lonnet::reply().
6027:
6028: =item *
6029:
6030: cput($namespace,$storehash,$udom,$uname) : critical put
6031: ($udom and $uname are optional)
6032:
6033: =item *
6034:
6035: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
6036: reference filled in from namesp (encrypts the return communication)
6037: ($udom and $uname are optional)
6038:
6039: =item *
6040:
6041: log($udom,$name,$home,$message) : write to permanent log for user; use
6042: critical subroutine
6043:
6044: =back
6045:
6046: =head2 Network Status Functions
6047:
6048: =over 4
6049:
6050: =item *
6051:
6052: dirlist($uri) : return directory list based on URI
6053:
6054: =item *
6055:
6056: spareserver() : find server with least workload from spare.tab
6057:
6058: =back
6059:
6060: =head2 Apache Request
6061:
6062: =over 4
6063:
6064: =item *
6065:
6066: ssi($url,%hash) : server side include, does a complete request cycle on url to
6067: localhost, posts hash
6068:
6069: =back
6070:
6071: =head2 Data to String to Data
6072:
6073: =over 4
6074:
6075: =item *
6076:
6077: hash2str(%hash) : convert a hash into a string complete with escaping and '='
6078: and '&' separators, supports elements that are arrayrefs and hashrefs
6079:
6080: =item *
6081:
6082: hashref2str($hashref) : convert a hashref into a string complete with
6083: escaping and '=' and '&' separators, supports elements that are
6084: arrayrefs and hashrefs
6085:
6086: =item *
6087:
6088: arrayref2str($arrayref) : convert an arrayref into a string complete
6089: with escaping and '&' separators, supports elements that are arrayrefs
6090: and hashrefs
6091:
6092: =item *
6093:
6094: str2hash($string) : convert string to hash using unescaping and
6095: splitting on '=' and '&', supports elements that are arrayrefs and
6096: hashrefs
6097:
6098: =item *
6099:
6100: str2array($string) : convert string to hash using unescaping and
6101: splitting on '&', supports elements that are arrayrefs and hashrefs
6102:
6103: =back
6104:
6105: =head2 Logging Routines
6106:
6107: =over 4
6108:
6109: These routines allow one to make log messages in the lonnet.log and
6110: lonnet.perm logfiles.
6111:
6112: =item *
6113:
6114: logtouch() : make sure the logfile, lonnet.log, exists
6115:
6116: =item *
6117:
6118: logthis() : append message to the normal lonnet.log file, it gets
6119: preiodically rolled over and deleted.
6120:
6121: =item *
6122:
6123: logperm() : append a permanent message to lonnet.perm.log, this log
6124: file never gets deleted by any automated portion of the system, only
6125: messages of critical importance should go in here.
6126:
6127: =back
6128:
6129: =head2 General File Helper Routines
6130:
6131: =over 4
6132:
6133: =item *
6134:
6135: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
6136: (a) files in /uploaded
6137: (i) If a local copy of the file exists -
6138: compares modification date of local copy with last-modified date for
6139: definitive version stored on home server for course. If local copy is
6140: stale, requests a new version from the home server and stores it.
6141: If the original has been removed from the home server, then local copy
6142: is unlinked.
6143: (ii) If local copy does not exist -
6144: requests the file from the home server and stores it.
6145:
6146: If $caller is 'uploadrep':
6147: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
6148: for request for files originally uploaded via DOCS.
6149: - returns 'ok' if fresh local copy now available, -1 otherwise.
6150:
6151: Otherwise:
6152: This indicates a call from the content generation phase of the request.
6153: - returns the entire contents of the file or -1.
6154:
6155: (b) files in /res
6156: - returns the entire contents of a file or -1;
6157: it properly subscribes to and replicates the file if neccessary.
6158:
6159: =item *
6160:
6161: filelocation($dir,$file) : returns file system location of a file
6162: based on URI; meant to be "fairly clean" absolute reference, $dir is a
6163: directory that relative $file lookups are to looked in ($dir of /a/dir
6164: and a file of ../bob will become /a/bob)
6165:
6166: =item *
6167:
6168: hreflocation($dir,$file) : returns file system location or a URL; same as
6169: filelocation except for hrefs
6170:
6171: =item *
6172:
6173: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
6174:
6175: =back
6176:
6177: =head2 HTTP Helper Routines
6178:
6179: =over 4
6180:
6181: =item *
6182:
6183: escape() : unpack non-word characters into CGI-compatible hex codes
6184:
6185: =item *
6186:
6187: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
6188:
6189: =back
6190:
6191: =head1 PRIVATE SUBROUTINES
6192:
6193: =head2 Underlying communication routines (Shouldn't call)
6194:
6195: =over 4
6196:
6197: =item *
6198:
6199: subreply() : tries to pass a message to lonc, returns con_lost if incapable
6200:
6201: =item *
6202:
6203: reply() : uses subreply to send a message to remote machine, logs all failures
6204:
6205: =item *
6206:
6207: critical() : passes a critical message to another server; if cannot
6208: get through then place message in connection buffer directory and
6209: returns con_delayed, if incapable of saving message, returns
6210: con_failed
6211:
6212: =item *
6213:
6214: reconlonc() : tries to reconnect lonc client processes.
6215:
6216: =back
6217:
6218: =head2 Resource Access Logging
6219:
6220: =over 4
6221:
6222: =item *
6223:
6224: flushcourselogs() : flush (save) buffer logs and access logs
6225:
6226: =item *
6227:
6228: courselog($what) : save message for course in hash
6229:
6230: =item *
6231:
6232: courseacclog($what) : save message for course using &courselog(). Perform
6233: special processing for specific resource types (problems, exams, quizzes, etc).
6234:
6235: =item *
6236:
6237: goodbye() : flush course logs and log shutting down; it is called in srm.conf
6238: as a PerlChildExitHandler
6239:
6240: =back
6241:
6242: =head2 Other
6243:
6244: =over 4
6245:
6246: =item *
6247:
6248: symblist($mapname,%newhash) : update symbolic storage links
6249:
6250: =back
6251:
6252: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>