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