1: # The LearningOnline Network
2: # TCP networking package
3: #
4: # $Id: lonnet.pm,v 1.866 2007/04/05 22:04:49 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
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 %badServerCache %spareid
39: %pr %prp $memcache %packagetab
40: %courselogs %accesshash %userrolehash %domainrolehash $processmarker $dumpcount
41: %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseownerbuf %coursetypebuf
42: $tmpdir $_64bit %env);
43:
44: use IO::Socket;
45: use GDBM_File;
46: use HTML::LCParser;
47: use HTML::Parser;
48: use Fcntl qw(:flock);
49: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
50: use Time::HiRes qw( gettimeofday tv_interval );
51: use Cache::Memcached;
52: use Digest::MD5;
53: use Math::Random;
54: use LONCAPA qw(:DEFAULT :match);
55: use LONCAPA::Configuration;
56:
57: my $readit;
58: my $max_connection_retries = 10; # Or some such value.
59:
60: require Exporter;
61:
62: our @ISA = qw (Exporter);
63: our @EXPORT = qw(%env);
64:
65: =pod
66:
67: =head1 Package Variables
68:
69: These are largely undocumented, so if you decipher one please note it here.
70:
71: =over 4
72:
73: =item $processmarker
74:
75: Contains the time this process was started and this servers host id.
76:
77: =item $dumpcount
78:
79: Counts the number of times a message log flush has been attempted (regardless
80: of success) by this process. Used as part of the filename when messages are
81: delayed.
82:
83: =back
84:
85: =cut
86:
87:
88: # --------------------------------------------------------------------- Logging
89: {
90: my $logid;
91: sub instructor_log {
92: my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
93: $logid++;
94: my $id=time().'00000'.$$.'00000'.$logid;
95: return &Apache::lonnet::put('nohist_'.$hash_name,
96: { $id => {
97: 'exe_uname' => $env{'user.name'},
98: 'exe_udom' => $env{'user.domain'},
99: 'exe_time' => time(),
100: 'exe_ip' => $ENV{'REMOTE_ADDR'},
101: 'delflag' => $delflag,
102: 'logentry' => $storehash,
103: 'uname' => $uname,
104: 'udom' => $udom,
105: }
106: },
107: $env{'course.'.$env{'request.course.id'}.'.domain'},
108: $env{'course.'.$env{'request.course.id'}.'.num'}
109: );
110: }
111: }
112:
113: sub logtouch {
114: my $execdir=$perlvar{'lonDaemons'};
115: unless (-e "$execdir/logs/lonnet.log") {
116: open(my $fh,">>$execdir/logs/lonnet.log");
117: close $fh;
118: }
119: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
120: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
121: }
122:
123: sub logthis {
124: my $message=shift;
125: my $execdir=$perlvar{'lonDaemons'};
126: my $now=time;
127: my $local=localtime($now);
128: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
129: print $fh "$local ($$): $message\n";
130: close($fh);
131: }
132: return 1;
133: }
134:
135: sub logperm {
136: my $message=shift;
137: my $execdir=$perlvar{'lonDaemons'};
138: my $now=time;
139: my $local=localtime($now);
140: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
141: print $fh "$now:$message:$local\n";
142: close($fh);
143: }
144: return 1;
145: }
146:
147: sub create_connection {
148: my ($hostname,$lonid) = @_;
149: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
150: Type => SOCK_STREAM,
151: Timeout => 10);
152: return 0 if (!$client);
153: print $client (join(':',$hostname,$lonid,&machine_ids($lonid))."\n");
154: my $result = <$client>;
155: chomp($result);
156: return 1 if ($result eq 'done');
157: return 0;
158: }
159:
160:
161: # -------------------------------------------------- Non-critical communication
162: sub subreply {
163: my ($cmd,$server)=@_;
164: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
165: #
166: # With loncnew process trimming, there's a timing hole between lonc server
167: # process exit and the master server picking up the listen on the AF_UNIX
168: # socket. In that time interval, a lock file will exist:
169:
170: my $lockfile=$peerfile.".lock";
171: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
172: sleep(1);
173: }
174: # At this point, either a loncnew parent is listening or an old lonc
175: # or loncnew child is listening so we can connect or everything's dead.
176: #
177: # We'll give the connection a few tries before abandoning it. If
178: # connection is not possible, we'll con_lost back to the client.
179: #
180: my $client;
181: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
182: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
183: Type => SOCK_STREAM,
184: Timeout => 10);
185: if($client) {
186: last; # Connected!
187: } else {
188: &create_connection(&hostname($server),$server);
189: }
190: sleep(1); # Try again later if failed connection.
191: }
192: my $answer;
193: if ($client) {
194: print $client "sethost:$server:$cmd\n";
195: $answer=<$client>;
196: if (!$answer) { $answer="con_lost"; }
197: chomp($answer);
198: } else {
199: $answer = 'con_lost'; # Failed connection.
200: }
201: return $answer;
202: }
203:
204: sub reply {
205: my ($cmd,$server)=@_;
206: unless (defined(&hostname($server))) { return 'no_such_host'; }
207: my $answer=subreply($cmd,$server);
208: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
209: &logthis("<font color=\"blue\">WARNING:".
210: " $cmd to $server returned $answer</font>");
211: }
212: return $answer;
213: }
214:
215: # ----------------------------------------------------------- Send USR1 to lonc
216:
217: sub reconlonc {
218: &logthis("Trying to reconnect lonc");
219: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
220: if (open(my $fh,"<$loncfile")) {
221: my $loncpid=<$fh>;
222: chomp($loncpid);
223: if (kill 0 => $loncpid) {
224: &logthis("lonc at pid $loncpid responding, sending USR1");
225: kill USR1 => $loncpid;
226: sleep 1;
227: } else {
228: &logthis(
229: "<font color=\"blue\">WARNING:".
230: " lonc at pid $loncpid not responding, giving up</font>");
231: }
232: } else {
233: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
234: }
235: }
236:
237: # ------------------------------------------------------ Critical communication
238:
239: sub critical {
240: my ($cmd,$server)=@_;
241: unless (&hostname($server)) {
242: &logthis("<font color=\"blue\">WARNING:".
243: " Critical message to unknown server ($server)</font>");
244: return 'no_such_host';
245: }
246: my $answer=reply($cmd,$server);
247: if ($answer eq 'con_lost') {
248: &reconlonc("$perlvar{'lonSockDir'}/$server");
249: my $answer=reply($cmd,$server);
250: if ($answer eq 'con_lost') {
251: my $now=time;
252: my $middlename=$cmd;
253: $middlename=substr($middlename,0,16);
254: $middlename=~s/\W//g;
255: my $dfilename=
256: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
257: $dumpcount++;
258: {
259: my $dfh;
260: if (open($dfh,">$dfilename")) {
261: print $dfh "$cmd\n";
262: close($dfh);
263: }
264: }
265: sleep 2;
266: my $wcmd='';
267: {
268: my $dfh;
269: if (open($dfh,"<$dfilename")) {
270: $wcmd=<$dfh>;
271: close($dfh);
272: }
273: }
274: chomp($wcmd);
275: if ($wcmd eq $cmd) {
276: &logthis("<font color=\"blue\">WARNING: ".
277: "Connection buffer $dfilename: $cmd</font>");
278: &logperm("D:$server:$cmd");
279: return 'con_delayed';
280: } else {
281: &logthis("<font color=\"red\">CRITICAL:"
282: ." Critical connection failed: $server $cmd</font>");
283: &logperm("F:$server:$cmd");
284: return 'con_failed';
285: }
286: }
287: }
288: return $answer;
289: }
290:
291: # ------------------------------------------- check if return value is an error
292:
293: sub error {
294: my ($result) = @_;
295: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
296: if ($2 == 2) { return undef; }
297: return $1;
298: }
299: return undef;
300: }
301:
302: sub convert_and_load_session_env {
303: my ($lonidsdir,$handle)=@_;
304: my @profile;
305: {
306: open(my $idf,"$lonidsdir/$handle.id");
307: flock($idf,LOCK_SH);
308: @profile=<$idf>;
309: close($idf);
310: }
311: my %temp_env;
312: foreach my $line (@profile) {
313: if ($line !~ m/=/) {
314: return 0;
315: }
316: chomp($line);
317: my ($envname,$envvalue)=split(/=/,$line,2);
318: $temp_env{&unescape($envname)} = &unescape($envvalue);
319: }
320: unlink("$lonidsdir/$handle.id");
321: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
322: 0640)) {
323: %disk_env = %temp_env;
324: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
325: untie(%disk_env);
326: }
327: return 1;
328: }
329:
330: # ------------------------------------------- Transfer profile into environment
331: my $env_loaded;
332: sub transfer_profile_to_env {
333: my ($lonidsdir,$handle,$force_transfer) = @_;
334: if (!$force_transfer && $env_loaded) { return; }
335:
336: if (!defined($lonidsdir)) {
337: $lonidsdir = $perlvar{'lonIDsDir'};
338: }
339: if (!defined($handle)) {
340: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
341: }
342:
343: my $convert;
344: {
345: open(my $idf,"$lonidsdir/$handle.id");
346: flock($idf,LOCK_SH);
347: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
348: &GDBM_READER(),0640)) {
349: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
350: untie(%disk_env);
351: } else {
352: $convert = 1;
353: }
354: }
355: if ($convert) {
356: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
357: &logthis("Failed to load session, or convert session.");
358: }
359: }
360:
361: my %remove;
362: while ( my $envname = each(%env) ) {
363: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
364: if ($time < time-300) {
365: $remove{$key}++;
366: }
367: }
368: }
369:
370: $env{'user.environment'} = "$lonidsdir/$handle.id";
371: $env_loaded=1;
372: foreach my $expired_key (keys(%remove)) {
373: &delenv($expired_key);
374: }
375: }
376:
377: sub timed_flock {
378: my ($file,$lock_type) = @_;
379: my $failed=0;
380: eval {
381: local $SIG{__DIE__}='DEFAULT';
382: local $SIG{ALRM}=sub {
383: $failed=1;
384: die("failed lock");
385: };
386: alarm(13);
387: flock($file,$lock_type);
388: alarm(0);
389: };
390: if ($failed) {
391: return undef;
392: } else {
393: return 1;
394: }
395: }
396:
397: # ---------------------------------------------------------- Append Environment
398:
399: sub appenv {
400: my %newenv=@_;
401: foreach my $key (keys(%newenv)) {
402: if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
403: &logthis("<font color=\"blue\">WARNING: ".
404: "Attempt to modify environment ".$key." to ".$newenv{$key}
405: .'</font>');
406: delete($newenv{$key});
407: } else {
408: $env{$key}=$newenv{$key};
409: }
410: }
411: open(my $env_file,$env{'user.environment'});
412: if (&timed_flock($env_file,LOCK_EX)
413: &&
414: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
415: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
416: while (my ($key,$value) = each(%newenv)) {
417: $disk_env{$key} = $value;
418: }
419: untie(%disk_env);
420: }
421: return 'ok';
422: }
423: # ----------------------------------------------------- Delete from Environment
424:
425: sub delenv {
426: my $delthis=shift;
427: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
428: &logthis("<font color=\"blue\">WARNING: ".
429: "Attempt to delete from environment ".$delthis);
430: return 'error';
431: }
432: open(my $env_file,$env{'user.environment'});
433: if (&timed_flock($env_file,LOCK_EX)
434: &&
435: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
436: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
437: foreach my $key (keys(%disk_env)) {
438: if ($key=~/^$delthis/) {
439: delete($env{$key});
440: delete($disk_env{$key});
441: }
442: }
443: untie(%disk_env);
444: }
445: return 'ok';
446: }
447:
448: sub get_env_multiple {
449: my ($name) = @_;
450: my @values;
451: if (defined($env{$name})) {
452: # exists is it an array
453: if (ref($env{$name})) {
454: @values=@{ $env{$name} };
455: } else {
456: $values[0]=$env{$name};
457: }
458: }
459: return(@values);
460: }
461:
462: # ------------------------------------------ Find out current server userload
463: # there is a copy in lond
464: sub userload {
465: my $numusers=0;
466: {
467: opendir(LONIDS,$perlvar{'lonIDsDir'});
468: my $filename;
469: my $curtime=time;
470: while ($filename=readdir(LONIDS)) {
471: if ($filename eq '.' || $filename eq '..') {next;}
472: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
473: if ($curtime-$mtime < 1800) { $numusers++; }
474: }
475: closedir(LONIDS);
476: }
477: my $userloadpercent=0;
478: my $maxuserload=$perlvar{'lonUserLoadLim'};
479: if ($maxuserload) {
480: $userloadpercent=100*$numusers/$maxuserload;
481: }
482: $userloadpercent=sprintf("%.2f",$userloadpercent);
483: return $userloadpercent;
484: }
485:
486: # ------------------------------------------ Fight off request when overloaded
487:
488: sub overloaderror {
489: my ($r,$checkserver)=@_;
490: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
491: my $loadavg;
492: if ($checkserver eq $perlvar{'lonHostID'}) {
493: open(my $loadfile,'/proc/loadavg');
494: $loadavg=<$loadfile>;
495: $loadavg =~ s/\s.*//g;
496: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
497: close($loadfile);
498: } else {
499: $loadavg=&reply('load',$checkserver);
500: }
501: my $overload=$loadavg-100;
502: if ($overload>0) {
503: $r->err_headers_out->{'Retry-After'}=$overload;
504: $r->log_error('Overload of '.$overload.' on '.$checkserver);
505: return 413;
506: }
507: return '';
508: }
509:
510: # ------------------------------ Find server with least workload from spare.tab
511:
512: sub spareserver {
513: my ($loadpercent,$userloadpercent,$want_server_name) = @_;
514: my $spare_server;
515: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
516: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
517: : $userloadpercent;
518:
519: foreach my $try_server (@{ $spareid{'primary'} }) {
520: ($spare_server, $lowest_load) =
521: &compare_server_load($try_server, $spare_server, $lowest_load);
522: }
523:
524: my $found_server = ($spare_server ne '' && $lowest_load < 100);
525:
526: if (!$found_server) {
527: foreach my $try_server (@{ $spareid{'default'} }) {
528: ($spare_server, $lowest_load) =
529: &compare_server_load($try_server, $spare_server, $lowest_load);
530: }
531: }
532:
533: if (!$want_server_name) {
534: $spare_server="http://".&hostname($spare_server);
535: }
536: return $spare_server;
537: }
538:
539: sub compare_server_load {
540: my ($try_server, $spare_server, $lowest_load) = @_;
541:
542: my $loadans = &reply('load', $try_server);
543: my $userloadans = &reply('userload',$try_server);
544:
545: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
546: next; #didn't get a number from the server
547: }
548:
549: my $load;
550: if ($loadans =~ /\d/) {
551: if ($userloadans =~ /\d/) {
552: #both are numbers, pick the bigger one
553: $load = ($loadans > $userloadans) ? $loadans
554: : $userloadans;
555: } else {
556: $load = $loadans;
557: }
558: } else {
559: $load = $userloadans;
560: }
561:
562: if (($load =~ /\d/) && ($load < $lowest_load)) {
563: $spare_server = $try_server;
564: $lowest_load = $load;
565: }
566: return ($spare_server,$lowest_load);
567: }
568: # --------------------------------------------- Try to change a user's password
569:
570: sub changepass {
571: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
572: $currentpass = &escape($currentpass);
573: $newpass = &escape($newpass);
574: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
575: $server);
576: if (! $answer) {
577: &logthis("No reply on password change request to $server ".
578: "by $uname in domain $udom.");
579: } elsif ($answer =~ "^ok") {
580: &logthis("$uname in $udom successfully changed their password ".
581: "on $server.");
582: } elsif ($answer =~ "^pwchange_failure") {
583: &logthis("$uname in $udom was unable to change their password ".
584: "on $server. The action was blocked by either lcpasswd ".
585: "or pwchange");
586: } elsif ($answer =~ "^non_authorized") {
587: &logthis("$uname in $udom did not get their password correct when ".
588: "attempting to change it on $server.");
589: } elsif ($answer =~ "^auth_mode_error") {
590: &logthis("$uname in $udom attempted to change their password despite ".
591: "not being locally or internally authenticated on $server.");
592: } elsif ($answer =~ "^unknown_user") {
593: &logthis("$uname in $udom attempted to change their password ".
594: "on $server but were unable to because $server is not ".
595: "their home server.");
596: } elsif ($answer =~ "^refused") {
597: &logthis("$server refused to change $uname in $udom password because ".
598: "it was sent an unencrypted request to change the password.");
599: }
600: return $answer;
601: }
602:
603: # ----------------------- Try to determine user's current authentication scheme
604:
605: sub queryauthenticate {
606: my ($uname,$udom)=@_;
607: my $uhome=&homeserver($uname,$udom);
608: if (!$uhome) {
609: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
610: return 'no_host';
611: }
612: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
613: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
614: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
615: }
616: return $answer;
617: }
618:
619: # --------- Try to authenticate user from domain's lib servers (first this one)
620:
621: sub authenticate {
622: my ($uname,$upass,$udom)=@_;
623: $upass=&escape($upass);
624: $uname= &LONCAPA::clean_username($uname);
625: my $uhome=&homeserver($uname,$udom,1);
626: if ((!$uhome) || ($uhome eq 'no_host')) {
627: # Maybe the machine was offline and only re-appeared again recently?
628: &reconlonc();
629: # One more
630: my $uhome=&homeserver($uname,$udom,1);
631: if ((!$uhome) || ($uhome eq 'no_host')) {
632: &logthis("User $uname at $udom is unknown in authenticate");
633: }
634: return 'no_host';
635: }
636: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
637: if ($answer eq 'authorized') {
638: &logthis("User $uname at $udom authorized by $uhome");
639: return $uhome;
640: }
641: if ($answer eq 'non_authorized') {
642: &logthis("User $uname at $udom rejected by $uhome");
643: return 'no_host';
644: }
645: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
646: return 'no_host';
647: }
648:
649: # ---------------------- Find the homebase for a user from domain's lib servers
650:
651: my %homecache;
652: sub homeserver {
653: my ($uname,$udom,$ignoreBadCache)=@_;
654: my $index="$uname:$udom";
655:
656: if (exists($homecache{$index})) { return $homecache{$index}; }
657:
658: my %servers = &get_servers($udom,'library');
659: foreach my $tryserver (keys(%servers)) {
660: next if ($ignoreBadCache ne 'true' &&
661: exists($badServerCache{$tryserver}));
662:
663: my $answer=reply("home:$udom:$uname",$tryserver);
664: if ($answer eq 'found') {
665: delete($badServerCache{$tryserver});
666: return $homecache{$index}=$tryserver;
667: } elsif ($answer eq 'no_host') {
668: $badServerCache{$tryserver}=1;
669: }
670: }
671: return 'no_host';
672: }
673:
674: # ------------------------------------- Find the usernames behind a list of IDs
675:
676: sub idget {
677: my ($udom,@ids)=@_;
678: my %returnhash=();
679:
680: my %servers = &get_servers($udom,'library');
681: foreach my $tryserver (keys(%servers)) {
682: my $idlist=join('&',@ids);
683: $idlist=~tr/A-Z/a-z/;
684: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
685: my @answer=();
686: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
687: @answer=split(/\&/,$reply);
688: } ;
689: my $i;
690: for ($i=0;$i<=$#ids;$i++) {
691: if ($answer[$i]) {
692: $returnhash{$ids[$i]}=$answer[$i];
693: }
694: }
695: }
696: return %returnhash;
697: }
698:
699: # ------------------------------------- Find the IDs behind a list of usernames
700:
701: sub idrget {
702: my ($udom,@unames)=@_;
703: my %returnhash=();
704: foreach my $uname (@unames) {
705: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
706: }
707: return %returnhash;
708: }
709:
710: # ------------------------------- Store away a list of names and associated IDs
711:
712: sub idput {
713: my ($udom,%ids)=@_;
714: my %servers=();
715: foreach my $uname (keys(%ids)) {
716: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
717: my $uhom=&homeserver($uname,$udom);
718: if ($uhom ne 'no_host') {
719: my $id=&escape($ids{$uname});
720: $id=~tr/A-Z/a-z/;
721: my $esc_unam=&escape($uname);
722: if ($servers{$uhom}) {
723: $servers{$uhom}.='&'.$id.'='.$esc_unam;
724: } else {
725: $servers{$uhom}=$id.'='.$esc_unam;
726: }
727: }
728: }
729: foreach my $server (keys(%servers)) {
730: &critical('idput:'.$udom.':'.$servers{$server},$server);
731: }
732: }
733:
734: # ------------------------------------------- get items from domain db files
735:
736: sub get_dom {
737: my ($namespace,$storearr,$udom,$uhome)=@_;
738: my $items='';
739: foreach my $item (@$storearr) {
740: $items.=&escape($item).'&';
741: }
742: $items=~s/\&$//;
743: if (!$udom) {
744: $udom=$env{'user.domain'};
745: if (defined(&domain($udom,'primary'))) {
746: $uhome=&domain($udom,'primary');
747: } else {
748: $uhome eq '';
749: }
750: } else {
751: if (!$uhome) {
752: if (defined(&domain($udom,'primary'))) {
753: $uhome=&domain($udom,'primary');
754: }
755: }
756: }
757: if ($udom && $uhome && ($uhome ne 'no_host')) {
758: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
759: my %returnhash;
760: if ($rep =~ /^error: 2 /) {
761: return %returnhash;
762: }
763: my @pairs=split(/\&/,$rep);
764: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
765: return @pairs;
766: }
767: my %returnhash=();
768: my $i=0;
769: foreach my $item (@$storearr) {
770: $returnhash{$item}=&thaw_unescape($pairs[$i]);
771: $i++;
772: }
773: return %returnhash;
774: } else {
775: &logthis("get_dom failed - no homeserver and/or domain");
776: }
777: }
778:
779: # -------------------------------------------- put items in domain db files
780:
781: sub put_dom {
782: my ($namespace,$storehash,$udom,$uhome)=@_;
783: if (!$udom) {
784: $udom=$env{'user.domain'};
785: if (defined(&domain($udom,'primary'))) {
786: $uhome=&domain($udom,'primary');
787: } else {
788: $uhome eq '';
789: }
790: } else {
791: if (!$uhome) {
792: if (defined(&domain($udom,'primary'))) {
793: $uhome=&domain($udom,'primary');
794: }
795: }
796: }
797: if ($udom && $uhome && ($uhome ne 'no_host')) {
798: my $items='';
799: foreach my $item (keys(%$storehash)) {
800: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
801: }
802: $items=~s/\&$//;
803: return &reply("putdom:$udom:$namespace:$items",$uhome);
804: } else {
805: &logthis("put_dom failed - no homeserver and/or domain");
806: }
807: }
808:
809: sub retrieve_inst_usertypes {
810: my ($udom) = @_;
811: my (%returnhash,@order);
812: if (defined(&domain($udom,'primary'))) {
813: my $uhome=&domain($udom,'primary');
814: my $rep=&reply("inst_usertypes:$udom",$uhome);
815: my ($hashitems,$orderitems) = split(/:/,$rep);
816: my @pairs=split(/\&/,$hashitems);
817: foreach my $item (@pairs) {
818: my ($key,$value)=split(/=/,$item,2);
819: $key = &unescape($key);
820: next if ($key =~ /^error: 2 /);
821: $returnhash{$key}=&thaw_unescape($value);
822: }
823: my @esc_order = split(/\&/,$orderitems);
824: foreach my $item (@esc_order) {
825: push(@order,&unescape($item));
826: }
827: } else {
828: &logthis("get_dom failed - no primary domain server for $udom");
829: }
830: return (\%returnhash,\@order);
831: }
832:
833: # --------------------------------------------------- Assign a key to a student
834:
835: sub assign_access_key {
836: #
837: # a valid key looks like uname:udom#comments
838: # comments are being appended
839: #
840: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
841: $kdom=
842: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
843: $knum=
844: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
845: $cdom=
846: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
847: $cnum=
848: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
849: $udom=$env{'user.name'} unless (defined($udom));
850: $uname=$env{'user.domain'} unless (defined($uname));
851: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
852: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
853: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
854: # assigned to this person
855: # - this should not happen,
856: # unless something went wrong
857: # the first time around
858: # ready to assign
859: $logentry=$1.'; '.$logentry;
860: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
861: $kdom,$knum) eq 'ok') {
862: # key now belongs to user
863: my $envkey='key.'.$cdom.'_'.$cnum;
864: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
865: &appenv('environment.'.$envkey => $ckey);
866: return 'ok';
867: } else {
868: return
869: 'error: Count not permanently assign key, will need to be re-entered later.';
870: }
871: } else {
872: return 'error: Could not assign key, try again later.';
873: }
874: } elsif (!$existing{$ckey}) {
875: # the key does not exist
876: return 'error: The key does not exist';
877: } else {
878: # the key is somebody else's
879: return 'error: The key is already in use';
880: }
881: }
882:
883: # ------------------------------------------ put an additional comment on a key
884:
885: sub comment_access_key {
886: #
887: # a valid key looks like uname:udom#comments
888: # comments are being appended
889: #
890: my ($ckey,$cdom,$cnum,$logentry)=@_;
891: $cdom=
892: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
893: $cnum=
894: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
895: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
896: if ($existing{$ckey}) {
897: $existing{$ckey}.='; '.$logentry;
898: # ready to assign
899: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
900: $cdom,$cnum) eq 'ok') {
901: return 'ok';
902: } else {
903: return 'error: Count not store comment.';
904: }
905: } else {
906: # the key does not exist
907: return 'error: The key does not exist';
908: }
909: }
910:
911: # ------------------------------------------------------ Generate a set of keys
912:
913: sub generate_access_keys {
914: my ($number,$cdom,$cnum,$logentry)=@_;
915: $cdom=
916: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
917: $cnum=
918: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
919: unless (&allowed('mky',$cdom)) { return 0; }
920: unless (($cdom) && ($cnum)) { return 0; }
921: if ($number>10000) { return 0; }
922: sleep(2); # make sure don't get same seed twice
923: srand(time()^($$+($$<<15))); # from "Programming Perl"
924: my $total=0;
925: for (my $i=1;$i<=$number;$i++) {
926: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
927: sprintf("%lx",int(100000*rand)).'-'.
928: sprintf("%lx",int(100000*rand));
929: $newkey=~s/1/g/g; # folks mix up 1 and l
930: $newkey=~s/0/h/g; # and also 0 and O
931: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
932: if ($existing{$newkey}) {
933: $i--;
934: } else {
935: if (&put('accesskeys',
936: { $newkey => '# generated '.localtime().
937: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
938: '; '.$logentry },
939: $cdom,$cnum) eq 'ok') {
940: $total++;
941: }
942: }
943: }
944: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
945: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
946: return $total;
947: }
948:
949: # ------------------------------------------------------- Validate an accesskey
950:
951: sub validate_access_key {
952: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
953: $cdom=
954: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
955: $cnum=
956: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
957: $udom=$env{'user.domain'} unless (defined($udom));
958: $uname=$env{'user.name'} unless (defined($uname));
959: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
960: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
961: }
962:
963: # ------------------------------------- Find the section of student in a course
964: sub devalidate_getsection_cache {
965: my ($udom,$unam,$courseid)=@_;
966: my $hashid="$udom:$unam:$courseid";
967: &devalidate_cache_new('getsection',$hashid);
968: }
969:
970: sub courseid_to_courseurl {
971: my ($courseid) = @_;
972: #already url style courseid
973: return $courseid if ($courseid =~ m{^/});
974:
975: if (exists($env{'course.'.$courseid.'.num'})) {
976: my $cnum = $env{'course.'.$courseid.'.num'};
977: my $cdom = $env{'course.'.$courseid.'.domain'};
978: return "/$cdom/$cnum";
979: }
980:
981: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
982: if (exists($courseinfo{'num'})) {
983: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
984: }
985:
986: return undef;
987: }
988:
989: sub getsection {
990: my ($udom,$unam,$courseid)=@_;
991: my $cachetime=1800;
992:
993: my $hashid="$udom:$unam:$courseid";
994: my ($result,$cached)=&is_cached_new('getsection',$hashid);
995: if (defined($cached)) { return $result; }
996:
997: my %Pending;
998: my %Expired;
999: #
1000: # Each role can either have not started yet (pending), be active,
1001: # or have expired.
1002: #
1003: # If there is an active role, we are done.
1004: #
1005: # If there is more than one role which has not started yet,
1006: # choose the one which will start sooner
1007: # If there is one role which has not started yet, return it.
1008: #
1009: # If there is more than one expired role, choose the one which ended last.
1010: # If there is a role which has expired, return it.
1011: #
1012: $courseid = &courseid_to_courseurl($courseid);
1013: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1014: foreach my $key (keys(%roleshash)) {
1015: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1016: my $section=$1;
1017: if ($key eq $courseid.'_st') { $section=''; }
1018: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1019: my $now=time;
1020: if (defined($end) && $end && ($now > $end)) {
1021: $Expired{$end}=$section;
1022: next;
1023: }
1024: if (defined($start) && $start && ($now < $start)) {
1025: $Pending{$start}=$section;
1026: next;
1027: }
1028: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1029: }
1030: #
1031: # Presumedly there will be few matching roles from the above
1032: # loop and the sorting time will be negligible.
1033: if (scalar(keys(%Pending))) {
1034: my ($time) = sort {$a <=> $b} keys(%Pending);
1035: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1036: }
1037: if (scalar(keys(%Expired))) {
1038: my @sorted = sort {$a <=> $b} keys(%Expired);
1039: my $time = pop(@sorted);
1040: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1041: }
1042: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1043: }
1044:
1045: sub save_cache {
1046: &purge_remembered();
1047: #&Apache::loncommon::validate_page();
1048: undef(%env);
1049: undef($env_loaded);
1050: }
1051:
1052: my $to_remember=-1;
1053: my %remembered;
1054: my %accessed;
1055: my $kicks=0;
1056: my $hits=0;
1057: sub make_key {
1058: my ($name,$id) = @_;
1059: if (length($id) > 200) { $id=length($id).':'.&Digest::MD5::md5_hex($id); }
1060: return &escape($name.':'.$id);
1061: }
1062:
1063: sub devalidate_cache_new {
1064: my ($name,$id,$debug) = @_;
1065: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1066: $id=&make_key($name,$id);
1067: $memcache->delete($id);
1068: delete($remembered{$id});
1069: delete($accessed{$id});
1070: }
1071:
1072: sub is_cached_new {
1073: my ($name,$id,$debug) = @_;
1074: $id=&make_key($name,$id);
1075: if (exists($remembered{$id})) {
1076: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1077: $accessed{$id}=[&gettimeofday()];
1078: $hits++;
1079: return ($remembered{$id},1);
1080: }
1081: my $value = $memcache->get($id);
1082: if (!(defined($value))) {
1083: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1084: return (undef,undef);
1085: }
1086: if ($value eq '__undef__') {
1087: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1088: $value=undef;
1089: }
1090: &make_room($id,$value,$debug);
1091: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1092: return ($value,1);
1093: }
1094:
1095: sub do_cache_new {
1096: my ($name,$id,$value,$time,$debug) = @_;
1097: $id=&make_key($name,$id);
1098: my $setvalue=$value;
1099: if (!defined($setvalue)) {
1100: $setvalue='__undef__';
1101: }
1102: if (!defined($time) ) {
1103: $time=600;
1104: }
1105: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1106: $memcache->set($id,$setvalue,$time);
1107: # need to make a copy of $value
1108: #&make_room($id,$value,$debug);
1109: return $value;
1110: }
1111:
1112: sub make_room {
1113: my ($id,$value,$debug)=@_;
1114: $remembered{$id}=$value;
1115: if ($to_remember<0) { return; }
1116: $accessed{$id}=[&gettimeofday()];
1117: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1118: my $to_kick;
1119: my $max_time=0;
1120: foreach my $other (keys(%accessed)) {
1121: if (&tv_interval($accessed{$other}) > $max_time) {
1122: $to_kick=$other;
1123: $max_time=&tv_interval($accessed{$other});
1124: }
1125: }
1126: delete($remembered{$to_kick});
1127: delete($accessed{$to_kick});
1128: $kicks++;
1129: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1130: return;
1131: }
1132:
1133: sub purge_remembered {
1134: #&logthis("Tossing ".scalar(keys(%remembered)));
1135: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1136: undef(%remembered);
1137: undef(%accessed);
1138: }
1139: # ------------------------------------- Read an entry from a user's environment
1140:
1141: sub userenvironment {
1142: my ($udom,$unam,@what)=@_;
1143: my %returnhash=();
1144: my @answer=split(/\&/,
1145: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
1146: &homeserver($unam,$udom)));
1147: my $i;
1148: for ($i=0;$i<=$#what;$i++) {
1149: $returnhash{$what[$i]}=&unescape($answer[$i]);
1150: }
1151: return %returnhash;
1152: }
1153:
1154: # ---------------------------------------------------------- Get a studentphoto
1155: sub studentphoto {
1156: my ($udom,$unam,$ext) = @_;
1157: my $home=&Apache::lonnet::homeserver($unam,$udom);
1158: if (defined($env{'request.course.id'})) {
1159: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1160: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1161: return(&retrievestudentphoto($udom,$unam,$ext));
1162: } else {
1163: my ($result,$perm_reqd)=
1164: &Apache::lonnet::auto_photo_permission($unam,$udom);
1165: if ($result eq 'ok') {
1166: if (!($perm_reqd eq 'yes')) {
1167: return(&retrievestudentphoto($udom,$unam,$ext));
1168: }
1169: }
1170: }
1171: }
1172: } else {
1173: my ($result,$perm_reqd) =
1174: &Apache::lonnet::auto_photo_permission($unam,$udom);
1175: if ($result eq 'ok') {
1176: if (!($perm_reqd eq 'yes')) {
1177: return(&retrievestudentphoto($udom,$unam,$ext));
1178: }
1179: }
1180: }
1181: return '/adm/lonKaputt/lonlogo_broken.gif';
1182: }
1183:
1184: sub retrievestudentphoto {
1185: my ($udom,$unam,$ext,$type) = @_;
1186: my $home=&Apache::lonnet::homeserver($unam,$udom);
1187: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
1188: if ($ret eq 'ok') {
1189: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
1190: if ($type eq 'thumbnail') {
1191: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
1192: }
1193: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
1194: return $tokenurl;
1195: } else {
1196: if ($type eq 'thumbnail') {
1197: return '/adm/lonKaputt/genericstudent_tn.gif';
1198: } else {
1199: return '/adm/lonKaputt/lonlogo_broken.gif';
1200: }
1201: }
1202: }
1203:
1204: # -------------------------------------------------------------------- New chat
1205:
1206: sub chatsend {
1207: my ($newentry,$anon,$group)=@_;
1208: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1209: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1210: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1211: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1212: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1213: &escape($newentry)).':'.$group,$chome);
1214: }
1215:
1216: # ------------------------------------------ Find current version of a resource
1217:
1218: sub getversion {
1219: my $fname=&clutter(shift);
1220: unless ($fname=~/^\/res\//) { return -1; }
1221: return ¤tversion(&filelocation('',$fname));
1222: }
1223:
1224: sub currentversion {
1225: my $fname=shift;
1226: my ($result,$cached)=&is_cached_new('resversion',$fname);
1227: if (defined($cached)) { return $result; }
1228: my $author=$fname;
1229: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1230: my ($udom,$uname)=split(/\//,$author);
1231: my $home=homeserver($uname,$udom);
1232: if ($home eq 'no_host') {
1233: return -1;
1234: }
1235: my $answer=reply("currentversion:$fname",$home);
1236: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1237: return -1;
1238: }
1239: return &do_cache_new('resversion',$fname,$answer,600);
1240: }
1241:
1242: # ----------------------------- Subscribe to a resource, return URL if possible
1243:
1244: sub subscribe {
1245: my $fname=shift;
1246: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1247: $fname=~s/[\n\r]//g;
1248: my $author=$fname;
1249: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1250: my ($udom,$uname)=split(/\//,$author);
1251: my $home=homeserver($uname,$udom);
1252: if ($home eq 'no_host') {
1253: return 'not_found';
1254: }
1255: my $answer=reply("sub:$fname",$home);
1256: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1257: $answer.=' by '.$home;
1258: }
1259: return $answer;
1260: }
1261:
1262: # -------------------------------------------------------------- Replicate file
1263:
1264: sub repcopy {
1265: my $filename=shift;
1266: $filename=~s/\/+/\//g;
1267: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
1268: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1269: if ($filename=~m|^/home/httpd/html/userfiles/| or
1270: $filename=~m -^/*(uploaded|editupload)/-) {
1271: return &repcopy_userfile($filename);
1272: }
1273: $filename=~s/[\n\r]//g;
1274: my $transname="$filename.in.transfer";
1275: # FIXME: this should flock
1276: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1277: my $remoteurl=subscribe($filename);
1278: if ($remoteurl =~ /^con_lost by/) {
1279: &logthis("Subscribe returned $remoteurl: $filename");
1280: return 'unavailable';
1281: } elsif ($remoteurl eq 'not_found') {
1282: #&logthis("Subscribe returned not_found: $filename");
1283: return 'not_found';
1284: } elsif ($remoteurl =~ /^rejected by/) {
1285: &logthis("Subscribe returned $remoteurl: $filename");
1286: return 'forbidden';
1287: } elsif ($remoteurl eq 'directory') {
1288: return 'ok';
1289: } else {
1290: my $author=$filename;
1291: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1292: my ($udom,$uname)=split(/\//,$author);
1293: my $home=homeserver($uname,$udom);
1294: unless ($home eq $perlvar{'lonHostID'}) {
1295: my @parts=split(/\//,$filename);
1296: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1297: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1298: &logthis("Malconfiguration for replication: $filename");
1299: return 'bad_request';
1300: }
1301: my $count;
1302: for ($count=5;$count<$#parts;$count++) {
1303: $path.="/$parts[$count]";
1304: if ((-e $path)!=1) {
1305: mkdir($path,0777);
1306: }
1307: }
1308: my $ua=new LWP::UserAgent;
1309: my $request=new HTTP::Request('GET',"$remoteurl");
1310: my $response=$ua->request($request,$transname);
1311: if ($response->is_error()) {
1312: unlink($transname);
1313: my $message=$response->status_line;
1314: &logthis("<font color=\"blue\">WARNING:"
1315: ." LWP get: $message: $filename</font>");
1316: return 'unavailable';
1317: } else {
1318: if ($remoteurl!~/\.meta$/) {
1319: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1320: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1321: if ($mresponse->is_error()) {
1322: unlink($filename.'.meta');
1323: &logthis(
1324: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1325: }
1326: }
1327: rename($transname,$filename);
1328: return 'ok';
1329: }
1330: }
1331: }
1332: }
1333:
1334: # ------------------------------------------------ Get server side include body
1335: sub ssi_body {
1336: my ($filelink,%form)=@_;
1337: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
1338: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
1339: }
1340: my $output=($filelink=~/^http\:/?&externalssi($filelink):
1341: &ssi($filelink,%form));
1342: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1343: $output=~s/^.*?\<body[^\>]*\>//si;
1344: $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1345: return $output;
1346: }
1347:
1348: # --------------------------------------------------------- Server Side Include
1349:
1350: sub absolute_url {
1351: my ($host_name) = @_;
1352: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
1353: if ($host_name eq '') {
1354: $host_name = $ENV{'SERVER_NAME'};
1355: }
1356: return $protocol.$host_name;
1357: }
1358:
1359: sub ssi {
1360:
1361: my ($fn,%form)=@_;
1362:
1363: my $ua=new LWP::UserAgent;
1364:
1365: my $request;
1366:
1367: $form{'no_update_last_known'}=1;
1368:
1369: if (%form) {
1370: $request=new HTTP::Request('POST',&absolute_url().$fn);
1371: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1372: } else {
1373: $request=new HTTP::Request('GET',&absolute_url().$fn);
1374: }
1375:
1376: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1377: my $response=$ua->request($request);
1378:
1379: return $response->content;
1380: }
1381:
1382: sub externalssi {
1383: my ($url)=@_;
1384: my $ua=new LWP::UserAgent;
1385: my $request=new HTTP::Request('GET',$url);
1386: my $response=$ua->request($request);
1387: return $response->content;
1388: }
1389:
1390: # -------------------------------- Allow a /uploaded/ URI to be vouched for
1391:
1392: sub allowuploaded {
1393: my ($srcurl,$url)=@_;
1394: $url=&clutter(&declutter($url));
1395: my $dir=$url;
1396: $dir=~s/\/[^\/]+$//;
1397: my %httpref=();
1398: my $httpurl=&hreflocation('',$url);
1399: $httpref{'httpref.'.$httpurl}=$srcurl;
1400: &Apache::lonnet::appenv(%httpref);
1401: }
1402:
1403: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1404: # input: action, courseID, current domain, intended
1405: # path to file, source of file, instruction to parse file for objects,
1406: # ref to hash for embedded objects,
1407: # ref to hash for codebase of java objects.
1408: #
1409: # output: url to file (if action was uploaddoc),
1410: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1411: #
1412: # Allows directory structure to be used within lonUsers/../userfiles/ for a
1413: # course.
1414: #
1415: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1416: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
1417: # course's home server.
1418: #
1419: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
1420: # be copied from $source (current location) to
1421: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1422: # and will then be copied to
1423: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
1424: # course's home server.
1425: #
1426: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1427: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1428: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1429: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
1430: # in course's home server.
1431: #
1432:
1433: sub process_coursefile {
1434: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1435: my $fetchresult;
1436: my $home=&homeserver($docuname,$docudom);
1437: if ($action eq 'propagate') {
1438: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1439: $home);
1440: } else {
1441: my $fpath = '';
1442: my $fname = $file;
1443: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1444: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1445: my $filepath = &build_filepath($fpath);
1446: if ($action eq 'copy') {
1447: if ($source eq '') {
1448: $fetchresult = 'no source file';
1449: return $fetchresult;
1450: } else {
1451: my $destination = $filepath.'/'.$fname;
1452: rename($source,$destination);
1453: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1454: $home);
1455: }
1456: } elsif ($action eq 'uploaddoc') {
1457: open(my $fh,'>'.$filepath.'/'.$fname);
1458: print $fh $env{'form.'.$source};
1459: close($fh);
1460: if ($parser eq 'parse') {
1461: my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
1462: unless ($parse_result eq 'ok') {
1463: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
1464: }
1465: }
1466: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1467: $home);
1468: if ($fetchresult eq 'ok') {
1469: return '/uploaded/'.$fpath.'/'.$fname;
1470: } else {
1471: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1472: ' to host '.$home.': '.$fetchresult);
1473: return '/adm/notfound.html';
1474: }
1475: }
1476: }
1477: unless ( $fetchresult eq 'ok') {
1478: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1479: ' to host '.$home.': '.$fetchresult);
1480: }
1481: return $fetchresult;
1482: }
1483:
1484: sub build_filepath {
1485: my ($fpath) = @_;
1486: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
1487: unless ($fpath eq '') {
1488: my @parts=split('/',$fpath);
1489: foreach my $part (@parts) {
1490: $filepath.= '/'.$part;
1491: if ((-e $filepath)!=1) {
1492: mkdir($filepath,0777);
1493: }
1494: }
1495: }
1496: return $filepath;
1497: }
1498:
1499: sub store_edited_file {
1500: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1501: my $file = $primary_url;
1502: $file =~ s#^/uploaded/$docudom/$docuname/##;
1503: my $fpath = '';
1504: my $fname = $file;
1505: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1506: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1507: my $filepath = &build_filepath($fpath);
1508: open(my $fh,'>'.$filepath.'/'.$fname);
1509: print $fh $content;
1510: close($fh);
1511: my $home=&homeserver($docuname,$docudom);
1512: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1513: $home);
1514: if ($$fetchresult eq 'ok') {
1515: return '/uploaded/'.$fpath.'/'.$fname;
1516: } else {
1517: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1518: ' to host '.$home.': '.$$fetchresult);
1519: return '/adm/notfound.html';
1520: }
1521: }
1522:
1523: sub clean_filename {
1524: my ($fname,$args)=@_;
1525: # Replace Windows backslashes by forward slashes
1526: $fname=~s/\\/\//g;
1527: if (!$args->{'keep_path'}) {
1528: # Get rid of everything but the actual filename
1529: $fname=~s/^.*\/([^\/]+)$/$1/;
1530: }
1531: # Replace spaces by underscores
1532: $fname=~s/\s+/\_/g;
1533: # Replace all other weird characters by nothing
1534: $fname=~s{[^/\w\.\-]}{}g;
1535: # Replace all .\d. sequences with _\d. so they no longer look like version
1536: # numbers
1537: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1538: return $fname;
1539: }
1540:
1541: # --------------- Take an uploaded file and put it into the userfiles directory
1542: # input: $formname - the contents of the file are in $env{"form.$formname"}
1543: # the desired filenam is in $env{"form.$formname.filename"}
1544: # $coursedoc - if true up to the current course
1545: # if false
1546: # $subdir - directory in userfile to store the file into
1547: # $parser - instruction to parse file for objects ($parser = parse)
1548: # $allfiles - reference to hash for embedded objects
1549: # $codebase - reference to hash for codebase of java objects
1550: # $desuname - username for permanent storage of uploaded file
1551: # $dsetudom - domain for permanaent storage of uploaded file
1552: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
1553: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1554: #
1555: # output: url of file in userspace, or error: <message>
1556: # or /adm/notfound.html if failure to upload occurse
1557:
1558:
1559: sub userfileupload {
1560: my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
1561: $destudom,$thumbwidth,$thumbheight)=@_;
1562: if (!defined($subdir)) { $subdir='unknown'; }
1563: my $fname=$env{'form.'.$formname.'.filename'};
1564: $fname=&clean_filename($fname);
1565: # See if there is anything left
1566: unless ($fname) { return 'error: no uploaded file'; }
1567: chop($env{'form.'.$formname});
1568: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
1569: my $now = time;
1570: my $filepath = 'tmp/helprequests/'.$now;
1571: my @parts=split(/\//,$filepath);
1572: my $fullpath = $perlvar{'lonDaemons'};
1573: for (my $i=0;$i<@parts;$i++) {
1574: $fullpath .= '/'.$parts[$i];
1575: if ((-e $fullpath)!=1) {
1576: mkdir($fullpath,0777);
1577: }
1578: }
1579: open(my $fh,'>'.$fullpath.'/'.$fname);
1580: print $fh $env{'form.'.$formname};
1581: close($fh);
1582: return $fullpath.'/'.$fname;
1583: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
1584: my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
1585: '_'.$env{'user.domain'}.'/pending';
1586: my @parts=split(/\//,$filepath);
1587: my $fullpath = $perlvar{'lonDaemons'};
1588: for (my $i=0;$i<@parts;$i++) {
1589: $fullpath .= '/'.$parts[$i];
1590: if ((-e $fullpath)!=1) {
1591: mkdir($fullpath,0777);
1592: }
1593: }
1594: open(my $fh,'>'.$fullpath.'/'.$fname);
1595: print $fh $env{'form.'.$formname};
1596: close($fh);
1597: return $fullpath.'/'.$fname;
1598: }
1599:
1600: # Create the directory if not present
1601: $fname="$subdir/$fname";
1602: if ($coursedoc) {
1603: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1604: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1605: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1606: return &finishuserfileupload($docuname,$docudom,
1607: $formname,$fname,$parser,$allfiles,
1608: $codebase,$thumbwidth,$thumbheight);
1609: } else {
1610: $fname=$env{'form.folder'}.'/'.$fname;
1611: return &process_coursefile('uploaddoc',$docuname,$docudom,
1612: $fname,$formname,$parser,
1613: $allfiles,$codebase);
1614: }
1615: } elsif (defined($destuname)) {
1616: my $docuname=$destuname;
1617: my $docudom=$destudom;
1618: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1619: $parser,$allfiles,$codebase,
1620: $thumbwidth,$thumbheight);
1621:
1622: } else {
1623: my $docuname=$env{'user.name'};
1624: my $docudom=$env{'user.domain'};
1625: if (exists($env{'form.group'})) {
1626: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1627: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1628: }
1629: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1630: $parser,$allfiles,$codebase,
1631: $thumbwidth,$thumbheight);
1632: }
1633: }
1634:
1635: sub finishuserfileupload {
1636: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1637: $thumbwidth,$thumbheight) = @_;
1638: my $path=$docudom.'/'.$docuname.'/';
1639: my $filepath=$perlvar{'lonDocRoot'};
1640: my ($fnamepath,$file,$fetchthumb);
1641: $file=$fname;
1642: if ($fname=~m|/|) {
1643: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
1644: $path.=$fnamepath.'/';
1645: }
1646: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1647: my $count;
1648: for ($count=4;$count<=$#parts;$count++) {
1649: $filepath.="/$parts[$count]";
1650: if ((-e $filepath)!=1) {
1651: mkdir($filepath,0777);
1652: }
1653: }
1654: # Save the file
1655: {
1656: if (!open(FH,'>'.$filepath.'/'.$file)) {
1657: &logthis('Failed to create '.$filepath.'/'.$file);
1658: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
1659: return '/adm/notfound.html';
1660: }
1661: if (!print FH ($env{'form.'.$formname})) {
1662: &logthis('Failed to write to '.$filepath.'/'.$file);
1663: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
1664: return '/adm/notfound.html';
1665: }
1666: close(FH);
1667: }
1668: if ($parser eq 'parse') {
1669: my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
1670: $codebase);
1671: unless ($parse_result eq 'ok') {
1672: &logthis('Failed to parse '.$filepath.$file.
1673: ' for embedded media: '.$parse_result);
1674: }
1675: }
1676: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
1677: my $input = $filepath.'/'.$file;
1678: my $output = $filepath.'/'.'tn-'.$file;
1679: my $thumbsize = $thumbwidth.'x'.$thumbheight;
1680: system("convert -sample $thumbsize $input $output");
1681: if (-e $filepath.'/'.'tn-'.$file) {
1682: $fetchthumb = 1;
1683: }
1684: }
1685:
1686: # Notify homeserver to grep it
1687: #
1688: my $docuhome=&homeserver($docuname,$docudom);
1689: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1690: if ($fetchresult eq 'ok') {
1691: if ($fetchthumb) {
1692: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
1693: if ($thumbresult ne 'ok') {
1694: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
1695: $docuhome.': '.$thumbresult);
1696: }
1697: }
1698: #
1699: # Return the URL to it
1700: return '/uploaded/'.$path.$file;
1701: } else {
1702: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
1703: ': '.$fetchresult);
1704: return '/adm/notfound.html';
1705: }
1706: }
1707:
1708: sub extract_embedded_items {
1709: my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1710: my @state = ();
1711: my %javafiles = (
1712: codebase => '',
1713: code => '',
1714: archive => ''
1715: );
1716: my %mediafiles = (
1717: src => '',
1718: movie => '',
1719: );
1720: my $p;
1721: if ($content) {
1722: $p = HTML::LCParser->new($content);
1723: } else {
1724: $p = HTML::LCParser->new($filepath.'/'.$file);
1725: }
1726: while (my $t=$p->get_token()) {
1727: if ($t->[0] eq 'S') {
1728: my ($tagname, $attr) = ($t->[1],$t->[2]);
1729: push (@state, $tagname);
1730: if (lc($tagname) eq 'allow') {
1731: &add_filetype($allfiles,$attr->{'src'},'src');
1732: }
1733: if (lc($tagname) eq 'img') {
1734: &add_filetype($allfiles,$attr->{'src'},'src');
1735: }
1736: if (lc($tagname) eq 'script') {
1737: if ($attr->{'archive'} =~ /\.jar$/i) {
1738: &add_filetype($allfiles,$attr->{'archive'},'archive');
1739: } else {
1740: &add_filetype($allfiles,$attr->{'src'},'src');
1741: }
1742: }
1743: if (lc($tagname) eq 'link') {
1744: if (lc($attr->{'rel'}) eq 'stylesheet') {
1745: &add_filetype($allfiles,$attr->{'href'},'href');
1746: }
1747: }
1748: if (lc($tagname) eq 'object' ||
1749: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
1750: foreach my $item (keys(%javafiles)) {
1751: $javafiles{$item} = '';
1752: }
1753: }
1754: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
1755: my $name = lc($attr->{'name'});
1756: foreach my $item (keys(%javafiles)) {
1757: if ($name eq $item) {
1758: $javafiles{$item} = $attr->{'value'};
1759: last;
1760: }
1761: }
1762: foreach my $item (keys(%mediafiles)) {
1763: if ($name eq $item) {
1764: &add_filetype($allfiles, $attr->{'value'}, 'value');
1765: last;
1766: }
1767: }
1768: }
1769: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
1770: foreach my $item (keys(%javafiles)) {
1771: if ($attr->{$item}) {
1772: $javafiles{$item} = $attr->{$item};
1773: last;
1774: }
1775: }
1776: foreach my $item (keys(%mediafiles)) {
1777: if ($attr->{$item}) {
1778: &add_filetype($allfiles,$attr->{$item},$item);
1779: last;
1780: }
1781: }
1782: }
1783: } elsif ($t->[0] eq 'E') {
1784: my ($tagname) = ($t->[1]);
1785: if ($javafiles{'codebase'} ne '') {
1786: $javafiles{'codebase'} .= '/';
1787: }
1788: if (lc($tagname) eq 'applet' ||
1789: lc($tagname) eq 'object' ||
1790: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
1791: ) {
1792: foreach my $item (keys(%javafiles)) {
1793: if ($item ne 'codebase' && $javafiles{$item} ne '') {
1794: my $file=$javafiles{'codebase'}.$javafiles{$item};
1795: &add_filetype($allfiles,$file,$item);
1796: }
1797: }
1798: }
1799: pop @state;
1800: }
1801: }
1802: return 'ok';
1803: }
1804:
1805: sub add_filetype {
1806: my ($allfiles,$file,$type)=@_;
1807: if (exists($allfiles->{$file})) {
1808: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
1809: push(@{$allfiles->{$file}}, &escape($type));
1810: }
1811: } else {
1812: @{$allfiles->{$file}} = (&escape($type));
1813: }
1814: }
1815:
1816: sub removeuploadedurl {
1817: my ($url)=@_;
1818: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1819: return &removeuserfile($uname,$udom,$fname);
1820: }
1821:
1822: sub removeuserfile {
1823: my ($docuname,$docudom,$fname)=@_;
1824: my $home=&homeserver($docuname,$docudom);
1825: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1826: if ($result eq 'ok') {
1827: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
1828: my $metafile = $fname.'.meta';
1829: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1830: my $url = "/uploaded/$docudom/$docuname/$fname";
1831: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1832: my $sqlresult =
1833: &update_portfolio_table($docuname,$docudom,$file,
1834: 'portfolio_metadata',$group,
1835: 'delete');
1836: }
1837: }
1838: return $result;
1839: }
1840:
1841: sub mkdiruserfile {
1842: my ($docuname,$docudom,$dir)=@_;
1843: my $home=&homeserver($docuname,$docudom);
1844: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
1845: }
1846:
1847: sub renameuserfile {
1848: my ($docuname,$docudom,$old,$new)=@_;
1849: my $home=&homeserver($docuname,$docudom);
1850: my $result = &reply("renameuserfile:$docudom:$docuname:".
1851: &escape("$old").':'.&escape("$new"),$home);
1852: if ($result eq 'ok') {
1853: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
1854: my $oldmeta = $old.'.meta';
1855: my $newmeta = $new.'.meta';
1856: my $metaresult =
1857: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1858: my $url = "/uploaded/$docudom/$docuname/$old";
1859: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1860: my $sqlresult =
1861: &update_portfolio_table($docuname,$docudom,$file,
1862: 'portfolio_metadata',$group,
1863: 'delete');
1864: }
1865: }
1866: return $result;
1867: }
1868:
1869: # ------------------------------------------------------------------------- Log
1870:
1871: sub log {
1872: my ($dom,$nam,$hom,$what)=@_;
1873: return critical("log:$dom:$nam:$what",$hom);
1874: }
1875:
1876: # ------------------------------------------------------------------ Course Log
1877: #
1878: # This routine flushes several buffers of non-mission-critical nature
1879: #
1880:
1881: sub flushcourselogs {
1882: &logthis('Flushing log buffers');
1883: #
1884: # course logs
1885: # This is a log of all transactions in a course, which can be used
1886: # for data mining purposes
1887: #
1888: # It also collects the courseid database, which lists last transaction
1889: # times and course titles for all courseids
1890: #
1891: my %courseidbuffer=();
1892: foreach my $crsid (keys %courselogs) {
1893: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1894: &escape($courselogs{$crsid}),
1895: $coursehombuf{$crsid}) eq 'ok') {
1896: delete $courselogs{$crsid};
1897: } else {
1898: &logthis('Failed to flush log buffer for '.$crsid);
1899: if (length($courselogs{$crsid})>40000) {
1900: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1901: " exceeded maximum size, deleting.</font>");
1902: delete $courselogs{$crsid};
1903: }
1904: }
1905: if ($courseidbuffer{$coursehombuf{$crsid}}) {
1906: $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1907: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1908: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1909: } else {
1910: $courseidbuffer{$coursehombuf{$crsid}}=
1911: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1912: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1913: }
1914: }
1915: #
1916: # Write course id database (reverse lookup) to homeserver of courses
1917: # Is used in pickcourse
1918: #
1919: foreach my $crs_home (keys(%courseidbuffer)) {
1920: &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1921: $crs_home);
1922: }
1923: #
1924: # File accesses
1925: # Writes to the dynamic metadata of resources to get hit counts, etc.
1926: #
1927: foreach my $entry (keys(%accesshash)) {
1928: if ($entry =~ /___count$/) {
1929: my ($dom,$name);
1930: ($dom,$name,undef)=
1931: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1932: if (! defined($dom) || $dom eq '' ||
1933: ! defined($name) || $name eq '') {
1934: my $cid = $env{'request.course.id'};
1935: $dom = $env{'request.'.$cid.'.domain'};
1936: $name = $env{'request.'.$cid.'.num'};
1937: }
1938: my $value = $accesshash{$entry};
1939: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
1940: my %temphash=($url => $value);
1941: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
1942: if ($result eq 'ok') {
1943: delete $accesshash{$entry};
1944: } elsif ($result eq 'unknown_cmd') {
1945: # Target server has old code running on it.
1946: my %temphash=($entry => $value);
1947: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1948: delete $accesshash{$entry};
1949: }
1950: }
1951: } else {
1952: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1953: my %temphash=($entry => $accesshash{$entry});
1954: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1955: delete $accesshash{$entry};
1956: }
1957: }
1958: }
1959: #
1960: # Roles
1961: # Reverse lookup of user roles for course faculty/staff and co-authorship
1962: #
1963: foreach my $entry (keys(%userrolehash)) {
1964: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1965: split(/\:/,$entry);
1966: if (&Apache::lonnet::put('nohist_userroles',
1967: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1968: $rudom,$runame) eq 'ok') {
1969: delete $userrolehash{$entry};
1970: }
1971: }
1972: #
1973: # Reverse lookup of domain roles (dc, ad, li, sc, au)
1974: #
1975: my %domrolebuffer = ();
1976: foreach my $entry (keys %domainrolehash) {
1977: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
1978: if ($domrolebuffer{$rudom}) {
1979: $domrolebuffer{$rudom}.='&'.&escape($entry).
1980: '='.&escape($domainrolehash{$entry});
1981: } else {
1982: $domrolebuffer{$rudom}.=&escape($entry).
1983: '='.&escape($domainrolehash{$entry});
1984: }
1985: delete $domainrolehash{$entry};
1986: }
1987: foreach my $dom (keys(%domrolebuffer)) {
1988: my %servers = &get_servers($dom,'library');
1989: foreach my $tryserver (keys(%servers)) {
1990: unless (&reply('domroleput:'.$dom.':'.
1991: $domrolebuffer{$dom},$tryserver) eq 'ok') {
1992: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
1993: }
1994: }
1995: }
1996: $dumpcount++;
1997: }
1998:
1999: sub courselog {
2000: my $what=shift;
2001: $what=time.':'.$what;
2002: unless ($env{'request.course.id'}) { return ''; }
2003: $coursedombuf{$env{'request.course.id'}}=
2004: $env{'course.'.$env{'request.course.id'}.'.domain'};
2005: $coursenumbuf{$env{'request.course.id'}}=
2006: $env{'course.'.$env{'request.course.id'}.'.num'};
2007: $coursehombuf{$env{'request.course.id'}}=
2008: $env{'course.'.$env{'request.course.id'}.'.home'};
2009: $coursedescrbuf{$env{'request.course.id'}}=
2010: $env{'course.'.$env{'request.course.id'}.'.description'};
2011: $courseinstcodebuf{$env{'request.course.id'}}=
2012: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2013: $courseownerbuf{$env{'request.course.id'}}=
2014: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
2015: $coursetypebuf{$env{'request.course.id'}}=
2016: $env{'course.'.$env{'request.course.id'}.'.type'};
2017: if (defined $courselogs{$env{'request.course.id'}}) {
2018: $courselogs{$env{'request.course.id'}}.='&'.$what;
2019: } else {
2020: $courselogs{$env{'request.course.id'}}.=$what;
2021: }
2022: if (length($courselogs{$env{'request.course.id'}})>4048) {
2023: &flushcourselogs();
2024: }
2025: }
2026:
2027: sub courseacclog {
2028: my $fnsymb=shift;
2029: unless ($env{'request.course.id'}) { return ''; }
2030: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
2031: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
2032: $what.=':POST';
2033: # FIXME: Probably ought to escape things....
2034: foreach my $key (keys(%env)) {
2035: if ($key=~/^form\.(.*)/) {
2036: $what.=':'.$1.'='.$env{$key};
2037: }
2038: }
2039: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
2040: # FIXME: We should not be depending on a form parameter that someone
2041: # editing lonsearchcat.pm might change in the future.
2042: if ($env{'form.phase'} eq 'course_search') {
2043: $what.= ':POST';
2044: # FIXME: Probably ought to escape things....
2045: foreach my $element ('courseexp','crsfulltext','crsrelated',
2046: 'crsdiscuss') {
2047: $what.=':'.$element.'='.$env{'form.'.$element};
2048: }
2049: }
2050: }
2051: &courselog($what);
2052: }
2053:
2054: sub countacc {
2055: my $url=&declutter(shift);
2056: return if (! defined($url) || $url eq '');
2057: unless ($env{'request.course.id'}) { return ''; }
2058: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
2059: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
2060: $accesshash{$key}++;
2061: }
2062:
2063: sub linklog {
2064: my ($from,$to)=@_;
2065: $from=&declutter($from);
2066: $to=&declutter($to);
2067: $accesshash{$from.'___'.$to.'___comefrom'}=1;
2068: $accesshash{$to.'___'.$from.'___goto'}=1;
2069: }
2070:
2071: sub userrolelog {
2072: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
2073: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
2074: ($trole=~/^in/) || ($trole=~/^cc/) ||
2075: ($trole=~/^ep/) || ($trole=~/^cr/) ||
2076: ($trole=~/^ta/)) {
2077: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2078: $userrolehash
2079: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
2080: =$tend.':'.$tstart;
2081: }
2082: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
2083: ($trole=~/^li/) || ($trole=~/^li/) ||
2084: ($trole=~/^au/) || ($trole=~/^dg/) ||
2085: ($trole=~/^sc/)) {
2086: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2087: $domainrolehash
2088: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
2089: = $tend.':'.$tstart;
2090: }
2091: }
2092:
2093: sub get_course_adv_roles {
2094: my $cid=shift;
2095: $cid=$env{'request.course.id'} unless (defined($cid));
2096: my %coursehash=&coursedescription($cid);
2097: my %nothide=();
2098: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
2099: $nothide{join(':',split(/[\@\:]/,$user))}=1;
2100: }
2101: my %returnhash=();
2102: my %dumphash=
2103: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
2104: my $now=time;
2105: foreach my $entry (keys %dumphash) {
2106: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
2107: if (($tstart) && ($tstart<0)) { next; }
2108: if (($tend) && ($tend<$now)) { next; }
2109: if (($tstart) && ($now<$tstart)) { next; }
2110: my ($role,$username,$domain,$section)=split(/\:/,$entry);
2111: if ($username eq '' || $domain eq '') { next; }
2112: if ((&privileged($username,$domain)) &&
2113: (!$nothide{$username.':'.$domain})) { next; }
2114: if ($role eq 'cr') { next; }
2115: my $key=&plaintext($role);
2116: if ($section) { $key.=' (Sec/Grp '.$section.')'; }
2117: if ($returnhash{$key}) {
2118: $returnhash{$key}.=','.$username.':'.$domain;
2119: } else {
2120: $returnhash{$key}=$username.':'.$domain;
2121: }
2122: }
2123: return %returnhash;
2124: }
2125:
2126: sub get_my_roles {
2127: my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
2128: unless (defined($uname)) { $uname=$env{'user.name'}; }
2129: unless (defined($udom)) { $udom=$env{'user.domain'}; }
2130: my %dumphash;
2131: if ($context eq 'userroles') {
2132: %dumphash = &dump('roles',$udom,$uname);
2133: } else {
2134: %dumphash=
2135: &dump('nohist_userroles',$udom,$uname);
2136: }
2137: my %returnhash=();
2138: my $now=time;
2139: foreach my $entry (keys(%dumphash)) {
2140: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
2141: if (($tstart) && ($tstart<0)) { next; }
2142: my $status = 'active';
2143: if (($tend) && ($tend<$now)) {
2144: $status = 'previous';
2145: }
2146: if (($tstart) && ($now<$tstart)) {
2147: $status = 'future';
2148: }
2149: if (ref($types) eq 'ARRAY') {
2150: if (!grep(/^\Q$status\E$/,@{$types})) {
2151: next;
2152: }
2153: } else {
2154: if ($status ne 'active') {
2155: next;
2156: }
2157: }
2158: my ($role,$username,$domain,$section)=split(/\:/,$entry);
2159: if (ref($roledoms) eq 'ARRAY') {
2160: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
2161: next;
2162: }
2163: }
2164: if (ref($roles) eq 'ARRAY') {
2165: if (!grep(/^\Q$role\E$/,@{$roles})) {
2166: next;
2167: }
2168: }
2169: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
2170: }
2171: return %returnhash;
2172: }
2173:
2174: # ----------------------------------------------------- Frontpage Announcements
2175: #
2176: #
2177:
2178: sub postannounce {
2179: my ($server,$text)=@_;
2180: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
2181: unless ($text=~/\w/) { $text=''; }
2182: return &reply('setannounce:'.&escape($text),$server);
2183: }
2184:
2185: sub getannounce {
2186:
2187: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
2188: my $announcement='';
2189: while (my $line = <$fh>) { $announcement .= $line; }
2190: close($fh);
2191: if ($announcement=~/\w/) {
2192: return
2193: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
2194: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
2195: } else {
2196: return '';
2197: }
2198: } else {
2199: return '';
2200: }
2201: }
2202:
2203: # ---------------------------------------------------------- Course ID routines
2204: # Deal with domain's nohist_courseid.db files
2205: #
2206:
2207: sub courseidput {
2208: my ($domain,$what,$coursehome)=@_;
2209: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
2210: }
2211:
2212: sub courseiddump {
2213: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
2214: my %returnhash=();
2215: unless ($domfilter) { $domfilter=''; }
2216: my %libserv = &all_library();
2217: foreach my $tryserver (keys(%libserv)) {
2218: if ( ( $hostidflag == 1
2219: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
2220: || (!defined($hostidflag)) ) {
2221:
2222: if ($domfilter eq ''
2223: || (&host_domain($tryserver) eq $domfilter)) {
2224: foreach my $line (
2225: split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
2226: $sincefilter.':'.&escape($descfilter).':'.
2227: &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
2228: $tryserver))) {
2229: my ($key,$value)=split(/\=/,$line,2);
2230: if (($key) && ($value)) {
2231: $returnhash{&unescape($key)}=$value;
2232: }
2233: }
2234: }
2235: }
2236: }
2237: return %returnhash;
2238: }
2239:
2240: # ---------------------------------------------------------- DC e-mail
2241:
2242: sub dcmailput {
2243: my ($domain,$msgid,$message,$server)=@_;
2244: my $status = &Apache::lonnet::critical(
2245: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
2246: &escape($message),$server);
2247: return $status;
2248: }
2249:
2250: sub dcmaildump {
2251: my ($dom,$startdate,$enddate,$senders) = @_;
2252: my %returnhash=();
2253:
2254: if (defined(&domain($dom,'primary'))) {
2255: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
2256: &escape($enddate).':';
2257: my @esc_senders=map { &escape($_)} @$senders;
2258: $cmd.=&escape(join('&',@esc_senders));
2259: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
2260: my ($key,$value) = split(/\=/,$line,2);
2261: if (($key) && ($value)) {
2262: $returnhash{&unescape($key)} = &unescape($value);
2263: }
2264: }
2265: }
2266: return %returnhash;
2267: }
2268: # ---------------------------------------------------------- Domain roles
2269:
2270: sub get_domain_roles {
2271: my ($dom,$roles,$startdate,$enddate)=@_;
2272: if (undef($startdate) || $startdate eq '') {
2273: $startdate = '.';
2274: }
2275: if (undef($enddate) || $enddate eq '') {
2276: $enddate = '.';
2277: }
2278: my $rolelist = join(':',@{$roles});
2279: my %personnel = ();
2280:
2281: my %servers = &get_servers($dom,'library');
2282: foreach my $tryserver (keys(%servers)) {
2283: %{$personnel{$tryserver}}=();
2284: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
2285: &escape($startdate).':'.
2286: &escape($enddate).':'.
2287: &escape($rolelist), $tryserver))) {
2288: my ($key,$value) = split(/\=/,$line,2);
2289: if (($key) && ($value)) {
2290: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
2291: }
2292: }
2293: }
2294: return %personnel;
2295: }
2296:
2297: # ----------------------------------------------------------- Check out an item
2298:
2299: sub get_first_access {
2300: my ($type,$argsymb)=@_;
2301: my ($symb,$courseid,$udom,$uname)=&whichuser();
2302: if ($argsymb) { $symb=$argsymb; }
2303: my ($map,$id,$res)=&decode_symb($symb);
2304: if ($type eq 'map') {
2305: $res=&symbread($map);
2306: } else {
2307: $res=$symb;
2308: }
2309: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
2310: return $times{"$courseid\0$res"};
2311: }
2312:
2313: sub set_first_access {
2314: my ($type)=@_;
2315: my ($symb,$courseid,$udom,$uname)=&whichuser();
2316: my ($map,$id,$res)=&decode_symb($symb);
2317: if ($type eq 'map') {
2318: $res=&symbread($map);
2319: } else {
2320: $res=$symb;
2321: }
2322: my $firstaccess=&get_first_access($type,$symb);
2323: if (!$firstaccess) {
2324: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
2325: }
2326: return 'already_set';
2327: }
2328:
2329: sub checkout {
2330: my ($symb,$tuname,$tudom,$tcrsid)=@_;
2331: my $now=time;
2332: my $lonhost=$perlvar{'lonHostID'};
2333: my $infostr=&escape(
2334: 'CHECKOUTTOKEN&'.
2335: $tuname.'&'.
2336: $tudom.'&'.
2337: $tcrsid.'&'.
2338: $symb.'&'.
2339: $now.'&'.$ENV{'REMOTE_ADDR'});
2340: my $token=&reply('tmpput:'.$infostr,$lonhost);
2341: if ($token=~/^error\:/) {
2342: &logthis("<font color=\"blue\">WARNING: ".
2343: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
2344: "</font>");
2345: return '';
2346: }
2347:
2348: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
2349: $token=~tr/a-z/A-Z/;
2350:
2351: my %infohash=('resource.0.outtoken' => $token,
2352: 'resource.0.checkouttime' => $now,
2353: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
2354:
2355: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2356: return '';
2357: } else {
2358: &logthis("<font color=\"blue\">WARNING: ".
2359: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
2360: "</font>");
2361: }
2362:
2363: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2364: &escape('Checkout '.$infostr.' - '.
2365: $token)) ne 'ok') {
2366: return '';
2367: } else {
2368: &logthis("<font color=\"blue\">WARNING: ".
2369: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
2370: "</font>");
2371: }
2372: return $token;
2373: }
2374:
2375: # ------------------------------------------------------------ Check in an item
2376:
2377: sub checkin {
2378: my $token=shift;
2379: my $now=time;
2380: my ($ta,$tb,$lonhost)=split(/\*/,$token);
2381: $lonhost=~tr/A-Z/a-z/;
2382: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
2383: $dtoken=~s/\W/\_/g;
2384: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
2385: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
2386:
2387: unless (($tuname) && ($tudom)) {
2388: &logthis('Check in '.$token.' ('.$dtoken.') failed');
2389: return '';
2390: }
2391:
2392: unless (&allowed('mgr',$tcrsid)) {
2393: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
2394: $env{'user.name'}.' - '.$env{'user.domain'});
2395: return '';
2396: }
2397:
2398: my %infohash=('resource.0.intoken' => $token,
2399: 'resource.0.checkintime' => $now,
2400: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
2401:
2402: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2403: return '';
2404: }
2405:
2406: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2407: &escape('Checkin - '.$token)) ne 'ok') {
2408: return '';
2409: }
2410:
2411: return ($symb,$tuname,$tudom,$tcrsid);
2412: }
2413:
2414: # --------------------------------------------- Set Expire Date for Spreadsheet
2415:
2416: sub expirespread {
2417: my ($uname,$udom,$stype,$usymb)=@_;
2418: my $cid=$env{'request.course.id'};
2419: if ($cid) {
2420: my $now=time;
2421: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2422: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
2423: $env{'course.'.$cid.'.num'}.
2424: ':nohist_expirationdates:'.
2425: &escape($key).'='.$now,
2426: $env{'course.'.$cid.'.home'})
2427: }
2428: return 'ok';
2429: }
2430:
2431: # ----------------------------------------------------- Devalidate Spreadsheets
2432:
2433: sub devalidate {
2434: my ($symb,$uname,$udom)=@_;
2435: my $cid=$env{'request.course.id'};
2436: if ($cid) {
2437: # delete the stored spreadsheets for
2438: # - the student level sheet of this user in course's homespace
2439: # - the assessment level sheet for this resource
2440: # for this user in user's homespace
2441: # - current conditional state info
2442: my $key=$uname.':'.$udom.':';
2443: my $status=
2444: &del('nohist_calculatedsheets',
2445: [$key.'studentcalc:'],
2446: $env{'course.'.$cid.'.domain'},
2447: $env{'course.'.$cid.'.num'})
2448: .' '.
2449: &del('nohist_calculatedsheets_'.$cid,
2450: [$key.'assesscalc:'.$symb],$udom,$uname);
2451: unless ($status eq 'ok ok') {
2452: &logthis('Could not devalidate spreadsheet '.
2453: $uname.' at '.$udom.' for '.
2454: $symb.': '.$status);
2455: }
2456: &delenv('user.state.'.$cid);
2457: }
2458: }
2459:
2460: sub get_scalar {
2461: my ($string,$end) = @_;
2462: my $value;
2463: if ($$string =~ s/^([^&]*?)($end)/$2/) {
2464: $value = $1;
2465: } elsif ($$string =~ s/^([^&]*?)&//) {
2466: $value = $1;
2467: }
2468: return &unescape($value);
2469: }
2470:
2471: sub array2str {
2472: my (@array) = @_;
2473: my $result=&arrayref2str(\@array);
2474: $result=~s/^__ARRAY_REF__//;
2475: $result=~s/__END_ARRAY_REF__$//;
2476: return $result;
2477: }
2478:
2479: sub arrayref2str {
2480: my ($arrayref) = @_;
2481: my $result='__ARRAY_REF__';
2482: foreach my $elem (@$arrayref) {
2483: if(ref($elem) eq 'ARRAY') {
2484: $result.=&arrayref2str($elem).'&';
2485: } elsif(ref($elem) eq 'HASH') {
2486: $result.=&hashref2str($elem).'&';
2487: } elsif(ref($elem)) {
2488: #print("Got a ref of ".(ref($elem))." skipping.");
2489: } else {
2490: $result.=&escape($elem).'&';
2491: }
2492: }
2493: $result=~s/\&$//;
2494: $result .= '__END_ARRAY_REF__';
2495: return $result;
2496: }
2497:
2498: sub hash2str {
2499: my (%hash) = @_;
2500: my $result=&hashref2str(\%hash);
2501: $result=~s/^__HASH_REF__//;
2502: $result=~s/__END_HASH_REF__$//;
2503: return $result;
2504: }
2505:
2506: sub hashref2str {
2507: my ($hashref)=@_;
2508: my $result='__HASH_REF__';
2509: foreach my $key (sort(keys(%$hashref))) {
2510: if (ref($key) eq 'ARRAY') {
2511: $result.=&arrayref2str($key).'=';
2512: } elsif (ref($key) eq 'HASH') {
2513: $result.=&hashref2str($key).'=';
2514: } elsif (ref($key)) {
2515: $result.='=';
2516: #print("Got a ref of ".(ref($key))." skipping.");
2517: } else {
2518: if ($key) {$result.=&escape($key).'=';} else { last; }
2519: }
2520:
2521: if(ref($hashref->{$key}) eq 'ARRAY') {
2522: $result.=&arrayref2str($hashref->{$key}).'&';
2523: } elsif(ref($hashref->{$key}) eq 'HASH') {
2524: $result.=&hashref2str($hashref->{$key}).'&';
2525: } elsif(ref($hashref->{$key})) {
2526: $result.='&';
2527: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
2528: } else {
2529: $result.=&escape($hashref->{$key}).'&';
2530: }
2531: }
2532: $result=~s/\&$//;
2533: $result .= '__END_HASH_REF__';
2534: return $result;
2535: }
2536:
2537: sub str2hash {
2538: my ($string)=@_;
2539: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
2540: return %$hash;
2541: }
2542:
2543: sub str2hashref {
2544: my ($string) = @_;
2545:
2546: my %hash;
2547:
2548: if($string !~ /^__HASH_REF__/) {
2549: if (! ($string eq '' || !defined($string))) {
2550: $hash{'error'}='Not hash reference';
2551: }
2552: return (\%hash, $string);
2553: }
2554:
2555: $string =~ s/^__HASH_REF__//;
2556:
2557: while($string !~ /^__END_HASH_REF__/) {
2558: #key
2559: my $key='';
2560: if($string =~ /^__HASH_REF__/) {
2561: ($key, $string)=&str2hashref($string);
2562: if(defined($key->{'error'})) {
2563: $hash{'error'}='Bad data';
2564: return (\%hash, $string);
2565: }
2566: } elsif($string =~ /^__ARRAY_REF__/) {
2567: ($key, $string)=&str2arrayref($string);
2568: if($key->[0] eq 'Array reference error') {
2569: $hash{'error'}='Bad data';
2570: return (\%hash, $string);
2571: }
2572: } else {
2573: $string =~ s/^(.*?)=//;
2574: $key=&unescape($1);
2575: }
2576: $string =~ s/^=//;
2577:
2578: #value
2579: my $value='';
2580: if($string =~ /^__HASH_REF__/) {
2581: ($value, $string)=&str2hashref($string);
2582: if(defined($value->{'error'})) {
2583: $hash{'error'}='Bad data';
2584: return (\%hash, $string);
2585: }
2586: } elsif($string =~ /^__ARRAY_REF__/) {
2587: ($value, $string)=&str2arrayref($string);
2588: if($value->[0] eq 'Array reference error') {
2589: $hash{'error'}='Bad data';
2590: return (\%hash, $string);
2591: }
2592: } else {
2593: $value=&get_scalar(\$string,'__END_HASH_REF__');
2594: }
2595: $string =~ s/^&//;
2596:
2597: $hash{$key}=$value;
2598: }
2599:
2600: $string =~ s/^__END_HASH_REF__//;
2601:
2602: return (\%hash, $string);
2603: }
2604:
2605: sub str2array {
2606: my ($string)=@_;
2607: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
2608: return @$array;
2609: }
2610:
2611: sub str2arrayref {
2612: my ($string) = @_;
2613: my @array;
2614:
2615: if($string !~ /^__ARRAY_REF__/) {
2616: if (! ($string eq '' || !defined($string))) {
2617: $array[0]='Array reference error';
2618: }
2619: return (\@array, $string);
2620: }
2621:
2622: $string =~ s/^__ARRAY_REF__//;
2623:
2624: while($string !~ /^__END_ARRAY_REF__/) {
2625: my $value='';
2626: if($string =~ /^__HASH_REF__/) {
2627: ($value, $string)=&str2hashref($string);
2628: if(defined($value->{'error'})) {
2629: $array[0] ='Array reference error';
2630: return (\@array, $string);
2631: }
2632: } elsif($string =~ /^__ARRAY_REF__/) {
2633: ($value, $string)=&str2arrayref($string);
2634: if($value->[0] eq 'Array reference error') {
2635: $array[0] ='Array reference error';
2636: return (\@array, $string);
2637: }
2638: } else {
2639: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
2640: }
2641: $string =~ s/^&//;
2642:
2643: push(@array, $value);
2644: }
2645:
2646: $string =~ s/^__END_ARRAY_REF__//;
2647:
2648: return (\@array, $string);
2649: }
2650:
2651: # -------------------------------------------------------------------Temp Store
2652:
2653: sub tmpreset {
2654: my ($symb,$namespace,$domain,$stuname) = @_;
2655: if (!$symb) {
2656: $symb=&symbread();
2657: if (!$symb) { $symb= $env{'request.url'}; }
2658: }
2659: $symb=escape($symb);
2660:
2661: if (!$namespace) { $namespace=$env{'request.state'}; }
2662: $namespace=~s/\//\_/g;
2663: $namespace=~s/\W//g;
2664:
2665: if (!$domain) { $domain=$env{'user.domain'}; }
2666: if (!$stuname) { $stuname=$env{'user.name'}; }
2667: if ($domain eq 'public' && $stuname eq 'public') {
2668: $stuname=$ENV{'REMOTE_ADDR'};
2669: }
2670: my $path=$perlvar{'lonDaemons'}.'/tmp';
2671: my %hash;
2672: if (tie(%hash,'GDBM_File',
2673: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2674: &GDBM_WRCREAT(),0640)) {
2675: foreach my $key (keys %hash) {
2676: if ($key=~ /:$symb/) {
2677: delete($hash{$key});
2678: }
2679: }
2680: }
2681: }
2682:
2683: sub tmpstore {
2684: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2685:
2686: if (!$symb) {
2687: $symb=&symbread();
2688: if (!$symb) { $symb= $env{'request.url'}; }
2689: }
2690: $symb=escape($symb);
2691:
2692: if (!$namespace) {
2693: # I don't think we would ever want to store this for a course.
2694: # it seems this will only be used if we don't have a course.
2695: #$namespace=$env{'request.course.id'};
2696: #if (!$namespace) {
2697: $namespace=$env{'request.state'};
2698: #}
2699: }
2700: $namespace=~s/\//\_/g;
2701: $namespace=~s/\W//g;
2702: if (!$domain) { $domain=$env{'user.domain'}; }
2703: if (!$stuname) { $stuname=$env{'user.name'}; }
2704: if ($domain eq 'public' && $stuname eq 'public') {
2705: $stuname=$ENV{'REMOTE_ADDR'};
2706: }
2707: my $now=time;
2708: my %hash;
2709: my $path=$perlvar{'lonDaemons'}.'/tmp';
2710: if (tie(%hash,'GDBM_File',
2711: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2712: &GDBM_WRCREAT(),0640)) {
2713: $hash{"version:$symb"}++;
2714: my $version=$hash{"version:$symb"};
2715: my $allkeys='';
2716: foreach my $key (keys(%$storehash)) {
2717: $allkeys.=$key.':';
2718: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
2719: }
2720: $hash{"$version:$symb:timestamp"}=$now;
2721: $allkeys.='timestamp';
2722: $hash{"$version:keys:$symb"}=$allkeys;
2723: if (untie(%hash)) {
2724: return 'ok';
2725: } else {
2726: return "error:$!";
2727: }
2728: } else {
2729: return "error:$!";
2730: }
2731: }
2732:
2733: # -----------------------------------------------------------------Temp Restore
2734:
2735: sub tmprestore {
2736: my ($symb,$namespace,$domain,$stuname) = @_;
2737:
2738: if (!$symb) {
2739: $symb=&symbread();
2740: if (!$symb) { $symb= $env{'request.url'}; }
2741: }
2742: $symb=escape($symb);
2743:
2744: if (!$namespace) { $namespace=$env{'request.state'}; }
2745:
2746: if (!$domain) { $domain=$env{'user.domain'}; }
2747: if (!$stuname) { $stuname=$env{'user.name'}; }
2748: if ($domain eq 'public' && $stuname eq 'public') {
2749: $stuname=$ENV{'REMOTE_ADDR'};
2750: }
2751: my %returnhash;
2752: $namespace=~s/\//\_/g;
2753: $namespace=~s/\W//g;
2754: my %hash;
2755: my $path=$perlvar{'lonDaemons'}.'/tmp';
2756: if (tie(%hash,'GDBM_File',
2757: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
2758: &GDBM_READER(),0640)) {
2759: my $version=$hash{"version:$symb"};
2760: $returnhash{'version'}=$version;
2761: my $scope;
2762: for ($scope=1;$scope<=$version;$scope++) {
2763: my $vkeys=$hash{"$scope:keys:$symb"};
2764: my @keys=split(/:/,$vkeys);
2765: my $key;
2766: $returnhash{"$scope:keys"}=$vkeys;
2767: foreach $key (@keys) {
2768: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
2769: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
2770: }
2771: }
2772: if (!(untie(%hash))) {
2773: return "error:$!";
2774: }
2775: } else {
2776: return "error:$!";
2777: }
2778: return %returnhash;
2779: }
2780:
2781: # ----------------------------------------------------------------------- Store
2782:
2783: sub store {
2784: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2785: my $home='';
2786:
2787: if ($stuname) { $home=&homeserver($stuname,$domain); }
2788:
2789: $symb=&symbclean($symb);
2790: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
2791:
2792: if (!$domain) { $domain=$env{'user.domain'}; }
2793: if (!$stuname) { $stuname=$env{'user.name'}; }
2794:
2795: &devalidate($symb,$stuname,$domain);
2796:
2797: $symb=escape($symb);
2798: if (!$namespace) {
2799: unless ($namespace=$env{'request.course.id'}) {
2800: return '';
2801: }
2802: }
2803: if (!$home) { $home=$env{'user.home'}; }
2804:
2805: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2806: $$storehash{'host'}=$perlvar{'lonHostID'};
2807:
2808: my $namevalue='';
2809: foreach my $key (keys(%$storehash)) {
2810: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
2811: }
2812: $namevalue=~s/\&$//;
2813: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
2814: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
2815: }
2816:
2817: # -------------------------------------------------------------- Critical Store
2818:
2819: sub cstore {
2820: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2821: my $home='';
2822:
2823: if ($stuname) { $home=&homeserver($stuname,$domain); }
2824:
2825: $symb=&symbclean($symb);
2826: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
2827:
2828: if (!$domain) { $domain=$env{'user.domain'}; }
2829: if (!$stuname) { $stuname=$env{'user.name'}; }
2830:
2831: &devalidate($symb,$stuname,$domain);
2832:
2833: $symb=escape($symb);
2834: if (!$namespace) {
2835: unless ($namespace=$env{'request.course.id'}) {
2836: return '';
2837: }
2838: }
2839: if (!$home) { $home=$env{'user.home'}; }
2840:
2841: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2842: $$storehash{'host'}=$perlvar{'lonHostID'};
2843:
2844: my $namevalue='';
2845: foreach my $key (keys(%$storehash)) {
2846: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
2847: }
2848: $namevalue=~s/\&$//;
2849: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
2850: return critical
2851: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
2852: }
2853:
2854: # --------------------------------------------------------------------- Restore
2855:
2856: sub restore {
2857: my ($symb,$namespace,$domain,$stuname) = @_;
2858: my $home='';
2859:
2860: if ($stuname) { $home=&homeserver($stuname,$domain); }
2861:
2862: if (!$symb) {
2863: unless ($symb=escape(&symbread())) { return ''; }
2864: } else {
2865: $symb=&escape(&symbclean($symb));
2866: }
2867: if (!$namespace) {
2868: unless ($namespace=$env{'request.course.id'}) {
2869: return '';
2870: }
2871: }
2872: if (!$domain) { $domain=$env{'user.domain'}; }
2873: if (!$stuname) { $stuname=$env{'user.name'}; }
2874: if (!$home) { $home=$env{'user.home'}; }
2875: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
2876:
2877: my %returnhash=();
2878: foreach my $line (split(/\&/,$answer)) {
2879: my ($name,$value)=split(/\=/,$line);
2880: $returnhash{&unescape($name)}=&thaw_unescape($value);
2881: }
2882: my $version;
2883: for ($version=1;$version<=$returnhash{'version'};$version++) {
2884: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
2885: $returnhash{$item}=$returnhash{$version.':'.$item};
2886: }
2887: }
2888: return %returnhash;
2889: }
2890:
2891: # ---------------------------------------------------------- Course Description
2892:
2893: sub coursedescription {
2894: my ($courseid,$args)=@_;
2895: $courseid=~s/^\///;
2896: $courseid=~s/\_/\//g;
2897: my ($cdomain,$cnum)=split(/\//,$courseid);
2898: my $chome=&homeserver($cnum,$cdomain);
2899: my $normalid=$cdomain.'_'.$cnum;
2900: # need to always cache even if we get errors otherwise we keep
2901: # trying and trying and trying to get the course description.
2902: my %envhash=();
2903: my %returnhash=();
2904:
2905: my $expiretime=600;
2906: if ($env{'request.course.id'} eq $normalid) {
2907: $expiretime=120;
2908: }
2909:
2910: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
2911: if (!$args->{'freshen_cache'}
2912: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
2913: foreach my $key (keys(%env)) {
2914: next if ($key !~ /^\Q$prefix\E(.*)/);
2915: my ($setting) = $1;
2916: $returnhash{$setting} = $env{$key};
2917: }
2918: return %returnhash;
2919: }
2920:
2921: # get the data agin
2922: if (!$args->{'one_time'}) {
2923: $envhash{'course.'.$normalid.'.last_cache'}=time;
2924: }
2925:
2926: if ($chome ne 'no_host') {
2927: %returnhash=&dump('environment',$cdomain,$cnum);
2928: if (!exists($returnhash{'con_lost'})) {
2929: $returnhash{'home'}= $chome;
2930: $returnhash{'domain'} = $cdomain;
2931: $returnhash{'num'} = $cnum;
2932: if (!defined($returnhash{'type'})) {
2933: $returnhash{'type'} = 'Course';
2934: }
2935: while (my ($name,$value) = each %returnhash) {
2936: $envhash{'course.'.$normalid.'.'.$name}=$value;
2937: }
2938: $returnhash{'url'}=&clutter($returnhash{'url'});
2939: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
2940: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
2941: $envhash{'course.'.$normalid.'.home'}=$chome;
2942: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
2943: $envhash{'course.'.$normalid.'.num'}=$cnum;
2944: }
2945: }
2946: if (!$args->{'one_time'}) {
2947: &appenv(%envhash);
2948: }
2949: return %returnhash;
2950: }
2951:
2952: # -------------------------------------------------See if a user is privileged
2953:
2954: sub privileged {
2955: my ($username,$domain)=@_;
2956: my $rolesdump=&reply("dump:$domain:$username:roles",
2957: &homeserver($username,$domain));
2958: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
2959: my $now=time;
2960: if ($rolesdump ne '') {
2961: foreach my $entry (split(/&/,$rolesdump)) {
2962: if ($entry!~/^rolesdef_/) {
2963: my ($area,$role)=split(/=/,$entry);
2964: $area=~s/\_\w\w$//;
2965: my ($trole,$tend,$tstart)=split(/_/,$role);
2966: if (($trole eq 'dc') || ($trole eq 'su')) {
2967: my $active=1;
2968: if ($tend) {
2969: if ($tend<$now) { $active=0; }
2970: }
2971: if ($tstart) {
2972: if ($tstart>$now) { $active=0; }
2973: }
2974: if ($active) { return 1; }
2975: }
2976: }
2977: }
2978: }
2979: return 0;
2980: }
2981:
2982: # -------------------------------------------------------- Get user privileges
2983:
2984: sub rolesinit {
2985: my ($domain,$username,$authhost)=@_;
2986: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
2987: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
2988: my %allroles=();
2989: my %allgroups=();
2990: my $now=time;
2991: my %userroles = ('user.login.time' => $now);
2992: my $group_privs;
2993:
2994: if ($rolesdump ne '') {
2995: foreach my $entry (split(/&/,$rolesdump)) {
2996: if ($entry!~/^rolesdef_/) {
2997: my ($area,$role)=split(/=/,$entry);
2998: $area=~s/\_\w\w$//;
2999: my ($trole,$tend,$tstart,$group_privs);
3000: if ($role=~/^cr/) {
3001: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
3002: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
3003: ($tend,$tstart)=split('_',$trest);
3004: } else {
3005: $trole=$role;
3006: }
3007: } elsif ($role =~ m|^gr/|) {
3008: ($trole,$tend,$tstart) = split(/_/,$role);
3009: ($trole,$group_privs) = split(/\//,$trole);
3010: $group_privs = &unescape($group_privs);
3011: } else {
3012: ($trole,$tend,$tstart)=split(/_/,$role);
3013: }
3014: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
3015: $username);
3016: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
3017: if (($tend!=0) && ($tend<$now)) { $trole=''; }
3018: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
3019: if (($area ne '') && ($trole ne '')) {
3020: my $spec=$trole.'.'.$area;
3021: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
3022: if ($trole =~ /^cr\//) {
3023: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
3024: } elsif ($trole eq 'gr') {
3025: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
3026: } else {
3027: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
3028: }
3029: }
3030: }
3031: }
3032: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
3033: $userroles{'user.adv'} = $adv;
3034: $userroles{'user.author'} = $author;
3035: $env{'user.adv'}=$adv;
3036: }
3037: return \%userroles;
3038: }
3039:
3040: sub set_arearole {
3041: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
3042: # log the associated role with the area
3043: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
3044: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
3045: }
3046:
3047: sub custom_roleprivs {
3048: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
3049: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
3050: my $homsvr=homeserver($rauthor,$rdomain);
3051: if (&hostname($homsvr) ne '') {
3052: my ($rdummy,$roledef)=
3053: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
3054: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
3055: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
3056: if (defined($syspriv)) {
3057: $$allroles{'cm./'}.=':'.$syspriv;
3058: $$allroles{$spec.'./'}.=':'.$syspriv;
3059: }
3060: if ($tdomain ne '') {
3061: if (defined($dompriv)) {
3062: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
3063: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
3064: }
3065: if (($trest ne '') && (defined($coursepriv))) {
3066: $$allroles{'cm.'.$area}.=':'.$coursepriv;
3067: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
3068: }
3069: }
3070: }
3071: }
3072: }
3073:
3074: sub group_roleprivs {
3075: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
3076: my $access = 1;
3077: my $now = time;
3078: if (($tend!=0) && ($tend<$now)) { $access = 0; }
3079: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
3080: if ($access) {
3081: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
3082: $$allgroups{$course}{$group} .=':'.$group_privs;
3083: }
3084: }
3085:
3086: sub standard_roleprivs {
3087: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
3088: if (defined($pr{$trole.':s'})) {
3089: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
3090: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
3091: }
3092: if ($tdomain ne '') {
3093: if (defined($pr{$trole.':d'})) {
3094: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3095: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3096: }
3097: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
3098: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
3099: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
3100: }
3101: }
3102: }
3103:
3104: sub set_userprivs {
3105: my ($userroles,$allroles,$allgroups) = @_;
3106: my $author=0;
3107: my $adv=0;
3108: my %grouproles = ();
3109: if (keys(%{$allgroups}) > 0) {
3110: foreach my $role (keys %{$allroles}) {
3111: my ($trole,$area,$sec,$extendedarea);
3112: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)-) {
3113: $trole = $1;
3114: $area = $2;
3115: $sec = $3;
3116: $extendedarea = $area.$sec;
3117: if (exists($$allgroups{$area})) {
3118: foreach my $group (keys(%{$$allgroups{$area}})) {
3119: my $spec = $trole.'.'.$extendedarea;
3120: $grouproles{$spec.'.'.$area.'/'.$group} =
3121: $$allgroups{$area}{$group};
3122: }
3123: }
3124: }
3125: }
3126: }
3127: foreach my $group (keys(%grouproles)) {
3128: $$allroles{$group} = $grouproles{$group};
3129: }
3130: foreach my $role (keys(%{$allroles})) {
3131: my %thesepriv;
3132: if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
3133: foreach my $item (split(/:/,$$allroles{$role})) {
3134: if ($item ne '') {
3135: my ($privilege,$restrictions)=split(/&/,$item);
3136: if ($restrictions eq '') {
3137: $thesepriv{$privilege}='F';
3138: } elsif ($thesepriv{$privilege} ne 'F') {
3139: $thesepriv{$privilege}.=$restrictions;
3140: }
3141: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
3142: }
3143: }
3144: my $thesestr='';
3145: foreach my $priv (keys(%thesepriv)) {
3146: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
3147: }
3148: $userroles->{'user.priv.'.$role} = $thesestr;
3149: }
3150: return ($author,$adv);
3151: }
3152:
3153: # --------------------------------------------------------------- get interface
3154:
3155: sub get {
3156: my ($namespace,$storearr,$udomain,$uname)=@_;
3157: my $items='';
3158: foreach my $item (@$storearr) {
3159: $items.=&escape($item).'&';
3160: }
3161: $items=~s/\&$//;
3162: if (!$udomain) { $udomain=$env{'user.domain'}; }
3163: if (!$uname) { $uname=$env{'user.name'}; }
3164: my $uhome=&homeserver($uname,$udomain);
3165:
3166: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
3167: my @pairs=split(/\&/,$rep);
3168: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
3169: return @pairs;
3170: }
3171: my %returnhash=();
3172: my $i=0;
3173: foreach my $item (@$storearr) {
3174: $returnhash{$item}=&thaw_unescape($pairs[$i]);
3175: $i++;
3176: }
3177: return %returnhash;
3178: }
3179:
3180: # --------------------------------------------------------------- del interface
3181:
3182: sub del {
3183: my ($namespace,$storearr,$udomain,$uname)=@_;
3184: my $items='';
3185: foreach my $item (@$storearr) {
3186: $items.=&escape($item).'&';
3187: }
3188: $items=~s/\&$//;
3189: if (!$udomain) { $udomain=$env{'user.domain'}; }
3190: if (!$uname) { $uname=$env{'user.name'}; }
3191: my $uhome=&homeserver($uname,$udomain);
3192:
3193: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
3194: }
3195:
3196: # -------------------------------------------------------------- dump interface
3197:
3198: sub dump {
3199: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
3200: if (!$udomain) { $udomain=$env{'user.domain'}; }
3201: if (!$uname) { $uname=$env{'user.name'}; }
3202: my $uhome=&homeserver($uname,$udomain);
3203: if ($regexp) {
3204: $regexp=&escape($regexp);
3205: } else {
3206: $regexp='.';
3207: }
3208: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3209: my @pairs=split(/\&/,$rep);
3210: my %returnhash=();
3211: foreach my $item (@pairs) {
3212: my ($key,$value)=split(/=/,$item,2);
3213: $key = &unescape($key);
3214: next if ($key =~ /^error: 2 /);
3215: $returnhash{$key}=&thaw_unescape($value);
3216: }
3217: return %returnhash;
3218: }
3219:
3220: # --------------------------------------------------------- dumpstore interface
3221:
3222: sub dumpstore {
3223: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
3224: if (!$udomain) { $udomain=$env{'user.domain'}; }
3225: if (!$uname) { $uname=$env{'user.name'}; }
3226: my $uhome=&homeserver($uname,$udomain);
3227: if ($regexp) {
3228: $regexp=&escape($regexp);
3229: } else {
3230: $regexp='.';
3231: }
3232: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3233: my @pairs=split(/\&/,$rep);
3234: my %returnhash=();
3235: foreach my $item (@pairs) {
3236: my ($key,$value)=split(/=/,$item,2);
3237: next if ($key =~ /^error: 2 /);
3238: $returnhash{$key}=&thaw_unescape($value);
3239: }
3240: return %returnhash;
3241: }
3242:
3243: # -------------------------------------------------------------- keys interface
3244:
3245: sub getkeys {
3246: my ($namespace,$udomain,$uname)=@_;
3247: if (!$udomain) { $udomain=$env{'user.domain'}; }
3248: if (!$uname) { $uname=$env{'user.name'}; }
3249: my $uhome=&homeserver($uname,$udomain);
3250: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
3251: my @keyarray=();
3252: foreach my $key (split(/\&/,$rep)) {
3253: next if ($key =~ /^error: 2 /);
3254: push(@keyarray,&unescape($key));
3255: }
3256: return @keyarray;
3257: }
3258:
3259: # --------------------------------------------------------------- currentdump
3260: sub currentdump {
3261: my ($courseid,$sdom,$sname)=@_;
3262: $courseid = $env{'request.course.id'} if (! defined($courseid));
3263: $sdom = $env{'user.domain'} if (! defined($sdom));
3264: $sname = $env{'user.name'} if (! defined($sname));
3265: my $uhome = &homeserver($sname,$sdom);
3266: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
3267: return if ($rep =~ /^(error:|no_such_host)/);
3268: #
3269: my %returnhash=();
3270: #
3271: if ($rep eq "unknown_cmd") {
3272: # an old lond will not know currentdump
3273: # Do a dump and make it look like a currentdump
3274: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
3275: return if ($tmp[0] =~ /^(error:|no_such_host)/);
3276: my %hash = @tmp;
3277: @tmp=();
3278: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
3279: } else {
3280: my @pairs=split(/\&/,$rep);
3281: foreach my $pair (@pairs) {
3282: my ($key,$value)=split(/=/,$pair,2);
3283: my ($symb,$param) = split(/:/,$key);
3284: $returnhash{&unescape($symb)}->{&unescape($param)} =
3285: &thaw_unescape($value);
3286: }
3287: }
3288: return %returnhash;
3289: }
3290:
3291: sub convert_dump_to_currentdump{
3292: my %hash = %{shift()};
3293: my %returnhash;
3294: # Code ripped from lond, essentially. The only difference
3295: # here is the unescaping done by lonnet::dump(). Conceivably
3296: # we might run in to problems with parameter names =~ /^v\./
3297: while (my ($key,$value) = each(%hash)) {
3298: my ($v,$symb,$param) = split(/:/,$key);
3299: $symb = &unescape($symb);
3300: $param = &unescape($param);
3301: next if ($v eq 'version' || $symb eq 'keys');
3302: next if (exists($returnhash{$symb}) &&
3303: exists($returnhash{$symb}->{$param}) &&
3304: $returnhash{$symb}->{'v.'.$param} > $v);
3305: $returnhash{$symb}->{$param}=$value;
3306: $returnhash{$symb}->{'v.'.$param}=$v;
3307: }
3308: #
3309: # Remove all of the keys in the hashes which keep track of
3310: # the version of the parameter.
3311: while (my ($symb,$param_hash) = each(%returnhash)) {
3312: # use a foreach because we are going to delete from the hash.
3313: foreach my $key (keys(%$param_hash)) {
3314: delete($param_hash->{$key}) if ($key =~ /^v\./);
3315: }
3316: }
3317: return \%returnhash;
3318: }
3319:
3320: # ------------------------------------------------------ critical inc interface
3321:
3322: sub cinc {
3323: return &inc(@_,'critical');
3324: }
3325:
3326: # --------------------------------------------------------------- inc interface
3327:
3328: sub inc {
3329: my ($namespace,$store,$udomain,$uname,$critical) = @_;
3330: if (!$udomain) { $udomain=$env{'user.domain'}; }
3331: if (!$uname) { $uname=$env{'user.name'}; }
3332: my $uhome=&homeserver($uname,$udomain);
3333: my $items='';
3334: if (! ref($store)) {
3335: # got a single value, so use that instead
3336: $items = &escape($store).'=&';
3337: } elsif (ref($store) eq 'SCALAR') {
3338: $items = &escape($$store).'=&';
3339: } elsif (ref($store) eq 'ARRAY') {
3340: $items = join('=&',map {&escape($_);} @{$store});
3341: } elsif (ref($store) eq 'HASH') {
3342: while (my($key,$value) = each(%{$store})) {
3343: $items.= &escape($key).'='.&escape($value).'&';
3344: }
3345: }
3346: $items=~s/\&$//;
3347: if ($critical) {
3348: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
3349: } else {
3350: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
3351: }
3352: }
3353:
3354: # --------------------------------------------------------------- put interface
3355:
3356: sub put {
3357: my ($namespace,$storehash,$udomain,$uname)=@_;
3358: if (!$udomain) { $udomain=$env{'user.domain'}; }
3359: if (!$uname) { $uname=$env{'user.name'}; }
3360: my $uhome=&homeserver($uname,$udomain);
3361: my $items='';
3362: foreach my $item (keys(%$storehash)) {
3363: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
3364: }
3365: $items=~s/\&$//;
3366: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
3367: }
3368:
3369: # ------------------------------------------------------------ newput interface
3370:
3371: sub newput {
3372: my ($namespace,$storehash,$udomain,$uname)=@_;
3373: if (!$udomain) { $udomain=$env{'user.domain'}; }
3374: if (!$uname) { $uname=$env{'user.name'}; }
3375: my $uhome=&homeserver($uname,$udomain);
3376: my $items='';
3377: foreach my $key (keys(%$storehash)) {
3378: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
3379: }
3380: $items=~s/\&$//;
3381: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
3382: }
3383:
3384: # --------------------------------------------------------- putstore interface
3385:
3386: sub putstore {
3387: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
3388: if (!$udomain) { $udomain=$env{'user.domain'}; }
3389: if (!$uname) { $uname=$env{'user.name'}; }
3390: my $uhome=&homeserver($uname,$udomain);
3391: my $items='';
3392: foreach my $key (keys(%$storehash)) {
3393: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
3394: }
3395: $items=~s/\&$//;
3396: my $esc_symb=&escape($symb);
3397: my $esc_v=&escape($version);
3398: my $reply =
3399: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
3400: $uhome);
3401: if ($reply eq 'unknown_cmd') {
3402: # gfall back to way things use to be done
3403: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
3404: $uname);
3405: }
3406: return $reply;
3407: }
3408:
3409: sub old_putstore {
3410: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
3411: if (!$udomain) { $udomain=$env{'user.domain'}; }
3412: if (!$uname) { $uname=$env{'user.name'}; }
3413: my $uhome=&homeserver($uname,$udomain);
3414: my %newstorehash;
3415: foreach my $item (keys(%$storehash)) {
3416: my $key = $version.':'.&escape($symb).':'.$item;
3417: $newstorehash{$key} = $storehash->{$item};
3418: }
3419: my $items='';
3420: my %allitems = ();
3421: foreach my $item (keys(%newstorehash)) {
3422: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
3423: my $key = $1.':keys:'.$2;
3424: $allitems{$key} .= $3.':';
3425: }
3426: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
3427: }
3428: foreach my $item (keys(%allitems)) {
3429: $allitems{$item} =~ s/\:$//;
3430: $items.= $item.'='.$allitems{$item}.'&';
3431: }
3432: $items=~s/\&$//;
3433: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
3434: }
3435:
3436: # ------------------------------------------------------ critical put interface
3437:
3438: sub cput {
3439: my ($namespace,$storehash,$udomain,$uname)=@_;
3440: if (!$udomain) { $udomain=$env{'user.domain'}; }
3441: if (!$uname) { $uname=$env{'user.name'}; }
3442: my $uhome=&homeserver($uname,$udomain);
3443: my $items='';
3444: foreach my $item (keys(%$storehash)) {
3445: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
3446: }
3447: $items=~s/\&$//;
3448: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
3449: }
3450:
3451: # -------------------------------------------------------------- eget interface
3452:
3453: sub eget {
3454: my ($namespace,$storearr,$udomain,$uname)=@_;
3455: my $items='';
3456: foreach my $item (@$storearr) {
3457: $items.=&escape($item).'&';
3458: }
3459: $items=~s/\&$//;
3460: if (!$udomain) { $udomain=$env{'user.domain'}; }
3461: if (!$uname) { $uname=$env{'user.name'}; }
3462: my $uhome=&homeserver($uname,$udomain);
3463: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
3464: my @pairs=split(/\&/,$rep);
3465: my %returnhash=();
3466: my $i=0;
3467: foreach my $item (@$storearr) {
3468: $returnhash{$item}=&thaw_unescape($pairs[$i]);
3469: $i++;
3470: }
3471: return %returnhash;
3472: }
3473:
3474: # ------------------------------------------------------------ tmpput interface
3475: sub tmpput {
3476: my ($storehash,$server,$context)=@_;
3477: my $items='';
3478: foreach my $item (keys(%$storehash)) {
3479: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
3480: }
3481: $items=~s/\&$//;
3482: if (defined($context)) {
3483: $items .= ':'.&escape($context);
3484: }
3485: return &reply("tmpput:$items",$server);
3486: }
3487:
3488: # ------------------------------------------------------------ tmpget interface
3489: sub tmpget {
3490: my ($token,$server)=@_;
3491: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3492: my $rep=&reply("tmpget:$token",$server);
3493: my %returnhash;
3494: foreach my $item (split(/\&/,$rep)) {
3495: my ($key,$value)=split(/=/,$item);
3496: $returnhash{&unescape($key)}=&thaw_unescape($value);
3497: }
3498: return %returnhash;
3499: }
3500:
3501: # ------------------------------------------------------------ tmpget interface
3502: sub tmpdel {
3503: my ($token,$server)=@_;
3504: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3505: return &reply("tmpdel:$token",$server);
3506: }
3507:
3508: # -------------------------------------------------- portfolio access checking
3509:
3510: sub portfolio_access {
3511: my ($requrl) = @_;
3512: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
3513: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
3514: if ($result) {
3515: my %setters;
3516: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
3517: my ($startblock,$endblock) =
3518: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
3519: if ($startblock && $endblock) {
3520: return 'B';
3521: }
3522: } else {
3523: my ($startblock,$endblock) =
3524: &Apache::loncommon::blockcheck(\%setters,'port');
3525: if ($startblock && $endblock) {
3526: return 'B';
3527: }
3528: }
3529: }
3530: if ($result eq 'ok') {
3531: return 'F';
3532: } elsif ($result =~ /^[^:]+:guest_/) {
3533: return 'A';
3534: }
3535: return '';
3536: }
3537:
3538: sub get_portfolio_access {
3539: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
3540:
3541: if (!ref($access_hash)) {
3542: my $current_perms = &get_portfile_permissions($udom,$unum);
3543: my %access_controls = &get_access_controls($current_perms,$group,
3544: $file_name);
3545: $access_hash = $access_controls{$file_name};
3546: }
3547:
3548: my ($public,$guest,@domains,@users,@courses,@groups);
3549: my $now = time;
3550: if (ref($access_hash) eq 'HASH') {
3551: foreach my $key (keys(%{$access_hash})) {
3552: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
3553: if ($start > $now) {
3554: next;
3555: }
3556: if ($end && $end<$now) {
3557: next;
3558: }
3559: if ($scope eq 'public') {
3560: $public = $key;
3561: last;
3562: } elsif ($scope eq 'guest') {
3563: $guest = $key;
3564: } elsif ($scope eq 'domains') {
3565: push(@domains,$key);
3566: } elsif ($scope eq 'users') {
3567: push(@users,$key);
3568: } elsif ($scope eq 'course') {
3569: push(@courses,$key);
3570: } elsif ($scope eq 'group') {
3571: push(@groups,$key);
3572: }
3573: }
3574: if ($public) {
3575: return 'ok';
3576: }
3577: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
3578: if ($guest) {
3579: return $guest;
3580: }
3581: } else {
3582: if (@domains > 0) {
3583: foreach my $domkey (@domains) {
3584: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
3585: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
3586: return 'ok';
3587: }
3588: }
3589: }
3590: }
3591: if (@users > 0) {
3592: foreach my $userkey (@users) {
3593: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
3594: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
3595: if (ref($item) eq 'HASH') {
3596: if (($item->{'uname'} eq $env{'user.name'}) &&
3597: ($item->{'udom'} eq $env{'user.domain'})) {
3598: return 'ok';
3599: }
3600: }
3601: }
3602: }
3603: }
3604: }
3605: my %roleshash;
3606: my @courses_and_groups = @courses;
3607: push(@courses_and_groups,@groups);
3608: if (@courses_and_groups > 0) {
3609: my (%allgroups,%allroles);
3610: my ($start,$end,$role,$sec,$group);
3611: foreach my $envkey (%env) {
3612: if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
3613: my $cid = $2.'_'.$3;
3614: if ($1 eq 'gr') {
3615: $group = $4;
3616: $allgroups{$cid}{$group} = $env{$envkey};
3617: } else {
3618: if ($4 eq '') {
3619: $sec = 'none';
3620: } else {
3621: $sec = $4;
3622: }
3623: $allroles{$cid}{$1}{$sec} = $env{$envkey};
3624: }
3625: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
3626: my $cid = $2.'_'.$3;
3627: if ($4 eq '') {
3628: $sec = 'none';
3629: } else {
3630: $sec = $4;
3631: }
3632: $allroles{$cid}{$1}{$sec} = $env{$envkey};
3633: }
3634: }
3635: if (keys(%allroles) == 0) {
3636: return;
3637: }
3638: foreach my $key (@courses_and_groups) {
3639: my %content = %{$$access_hash{$key}};
3640: my $cnum = $content{'number'};
3641: my $cdom = $content{'domain'};
3642: my $cid = $cdom.'_'.$cnum;
3643: if (!exists($allroles{$cid})) {
3644: next;
3645: }
3646: foreach my $role_id (keys(%{$content{'roles'}})) {
3647: my @sections = @{$content{'roles'}{$role_id}{'section'}};
3648: my @groups = @{$content{'roles'}{$role_id}{'group'}};
3649: my @status = @{$content{'roles'}{$role_id}{'access'}};
3650: my @roles = @{$content{'roles'}{$role_id}{'role'}};
3651: foreach my $role (keys(%{$allroles{$cid}})) {
3652: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
3653: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
3654: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
3655: if (grep/^all$/,@sections) {
3656: return 'ok';
3657: } else {
3658: if (grep/^$sec$/,@sections) {
3659: return 'ok';
3660: }
3661: }
3662: }
3663: }
3664: if (keys(%{$allgroups{$cid}}) == 0) {
3665: if (grep/^none$/,@groups) {
3666: return 'ok';
3667: }
3668: } else {
3669: if (grep/^all$/,@groups) {
3670: return 'ok';
3671: }
3672: foreach my $group (keys(%{$allgroups{$cid}})) {
3673: if (grep/^$group$/,@groups) {
3674: return 'ok';
3675: }
3676: }
3677: }
3678: }
3679: }
3680: }
3681: }
3682: }
3683: if ($guest) {
3684: return $guest;
3685: }
3686: }
3687: }
3688: return;
3689: }
3690:
3691: sub course_group_datechecker {
3692: my ($dates,$now,$status) = @_;
3693: my ($start,$end) = split(/\./,$dates);
3694: if (!$start && !$end) {
3695: return 'ok';
3696: }
3697: if (grep/^active$/,@{$status}) {
3698: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
3699: return 'ok';
3700: }
3701: }
3702: if (grep/^previous$/,@{$status}) {
3703: if ($end > $now ) {
3704: return 'ok';
3705: }
3706: }
3707: if (grep/^future$/,@{$status}) {
3708: if ($start > $now) {
3709: return 'ok';
3710: }
3711: }
3712: return;
3713: }
3714:
3715: sub parse_portfolio_url {
3716: my ($url) = @_;
3717:
3718: my ($type,$udom,$unum,$group,$file_name);
3719:
3720: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
3721: $type = 1;
3722: $udom = $1;
3723: $unum = $2;
3724: $file_name = $3;
3725: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
3726: $type = 2;
3727: $udom = $1;
3728: $unum = $2;
3729: $group = $3;
3730: $file_name = $3.'/'.$4;
3731: }
3732: if (wantarray) {
3733: return ($type,$udom,$unum,$file_name,$group);
3734: }
3735: return $type;
3736: }
3737:
3738: sub is_portfolio_url {
3739: my ($url) = @_;
3740: return scalar(&parse_portfolio_url($url));
3741: }
3742:
3743: sub is_portfolio_file {
3744: my ($file) = @_;
3745: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
3746: return 1;
3747: }
3748: return;
3749: }
3750:
3751:
3752: # ---------------------------------------------- Custom access rule evaluation
3753:
3754: sub customaccess {
3755: my ($priv,$uri)=@_;
3756: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
3757: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
3758: $udom = &LONCAPA::clean_domain($udom);
3759: $ucrs = &LONCAPA::clean_username($ucrs);
3760: my $access=0;
3761: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
3762: my ($effect,$realm,$role)=split(/\:/,$right);
3763: if ($role) {
3764: if ($role ne $urole) { next; }
3765: }
3766: foreach my $scope (split(/\s*\,\s*/,$realm)) {
3767: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
3768: if ($tdom) {
3769: if ($tdom ne $udom) { next; }
3770: }
3771: if ($tcrs) {
3772: if ($tcrs ne $ucrs) { next; }
3773: }
3774: if ($tsec) {
3775: if ($tsec ne $usec) { next; }
3776: }
3777: $access=($effect eq 'allow');
3778: last;
3779: }
3780: if ($realm eq '' && $role eq '') {
3781: $access=($effect eq 'allow');
3782: }
3783: }
3784: return $access;
3785: }
3786:
3787: # ------------------------------------------------- Check for a user privilege
3788:
3789: sub allowed {
3790: my ($priv,$uri,$symb,$role)=@_;
3791: my $ver_orguri=$uri;
3792: $uri=&deversion($uri);
3793: my $orguri=$uri;
3794: $uri=&declutter($uri);
3795:
3796: if ($priv eq 'evb') {
3797: # Evade communication block restrictions for specified role in a course
3798: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
3799: return $1;
3800: } else {
3801: return;
3802: }
3803: }
3804:
3805: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
3806: # Free bre access to adm and meta resources
3807: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
3808: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
3809: && ($priv eq 'bre')) {
3810: return 'F';
3811: }
3812:
3813: # Free bre access to user's own portfolio contents
3814: my ($space,$domain,$name,@dir)=split('/',$uri);
3815: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
3816: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
3817: my %setters;
3818: my ($startblock,$endblock) =
3819: &Apache::loncommon::blockcheck(\%setters,'port');
3820: if ($startblock && $endblock) {
3821: return 'B';
3822: } else {
3823: return 'F';
3824: }
3825: }
3826:
3827: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
3828: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
3829: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
3830: if (exists($env{'request.course.id'})) {
3831: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
3832: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
3833: if (($domain eq $cdom) && ($name eq $cnum)) {
3834: my $courseprivid=$env{'request.course.id'};
3835: $courseprivid=~s/\_/\//;
3836: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
3837: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
3838: return $1;
3839: } else {
3840: if ($env{'request.course.sec'}) {
3841: $courseprivid.='/'.$env{'request.course.sec'};
3842: }
3843: if ($env{'user.priv.'.$env{'request.role'}.'./'.
3844: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
3845: return $2;
3846: }
3847: }
3848: }
3849: }
3850: }
3851:
3852: # Free bre to public access
3853:
3854: if ($priv eq 'bre') {
3855: my $copyright=&metadata($uri,'copyright');
3856: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
3857: return 'F';
3858: }
3859: if ($copyright eq 'priv') {
3860: $uri=~/([^\/]+)\/([^\/]+)\//;
3861: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
3862: return '';
3863: }
3864: }
3865: if ($copyright eq 'domain') {
3866: $uri=~/([^\/]+)\/([^\/]+)\//;
3867: unless (($env{'user.domain'} eq $1) ||
3868: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
3869: return '';
3870: }
3871: }
3872: if ($env{'request.role'}=~ /li\.\//) {
3873: # Library role, so allow browsing of resources in this domain.
3874: return 'F';
3875: }
3876: if ($copyright eq 'custom') {
3877: unless (&customaccess($priv,$uri)) { return ''; }
3878: }
3879: }
3880: # Domain coordinator is trying to create a course
3881: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
3882: # uri is the requested domain in this case.
3883: # comparison to 'request.role.domain' shows if the user has selected
3884: # a role of dc for the domain in question.
3885: return 'F' if ($uri eq $env{'request.role.domain'});
3886: }
3887:
3888: my $thisallowed='';
3889: my $statecond=0;
3890: my $courseprivid='';
3891:
3892: # Course
3893:
3894: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
3895: $thisallowed.=$1;
3896: }
3897:
3898: # Domain
3899:
3900: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
3901: =~/\Q$priv\E\&([^\:]*)/) {
3902: $thisallowed.=$1;
3903: }
3904:
3905: # Course: uri itself is a course
3906: my $courseuri=$uri;
3907: $courseuri=~s/\_(\d)/\/$1/;
3908: $courseuri=~s/^([^\/])/\/$1/;
3909:
3910: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
3911: =~/\Q$priv\E\&([^\:]*)/) {
3912: $thisallowed.=$1;
3913: }
3914:
3915: # URI is an uploaded document for this course, default permissions don't matter
3916: # not allowing 'edit' access (editupload) to uploaded course docs
3917: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
3918: $thisallowed='';
3919: my ($match)=&is_on_map($uri);
3920: if ($match) {
3921: if ($env{'user.priv.'.$env{'request.role'}.'./'}
3922: =~/\Q$priv\E\&([^\:]*)/) {
3923: $thisallowed.=$1;
3924: }
3925: } else {
3926: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
3927: if ($refuri) {
3928: if ($refuri =~ m|^/adm/|) {
3929: $thisallowed='F';
3930: } else {
3931: $refuri=&declutter($refuri);
3932: my ($match) = &is_on_map($refuri);
3933: if ($match) {
3934: $thisallowed='F';
3935: }
3936: }
3937: }
3938: }
3939: }
3940:
3941: if ($priv eq 'bre'
3942: && $thisallowed ne 'F'
3943: && $thisallowed ne '2'
3944: && &is_portfolio_url($uri)) {
3945: $thisallowed = &portfolio_access($uri);
3946: }
3947:
3948: # Full access at system, domain or course-wide level? Exit.
3949:
3950: if ($thisallowed=~/F/) {
3951: return 'F';
3952: }
3953:
3954: # If this is generating or modifying users, exit with special codes
3955:
3956: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
3957: if (($priv eq 'cca') || ($priv eq 'caa')) {
3958: my ($audom,$auname)=split('/',$uri);
3959: # no author name given, so this just checks on the general right to make a co-author in this domain
3960: unless ($auname) { return $thisallowed; }
3961: # an author name is given, so we are about to actually make a co-author for a certain account
3962: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
3963: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
3964: ($audom ne $env{'request.role.domain'}))) { return ''; }
3965: }
3966: return $thisallowed;
3967: }
3968: #
3969: # Gathered so far: system, domain and course wide privileges
3970: #
3971: # Course: See if uri or referer is an individual resource that is part of
3972: # the course
3973:
3974: if ($env{'request.course.id'}) {
3975:
3976: $courseprivid=$env{'request.course.id'};
3977: if ($env{'request.course.sec'}) {
3978: $courseprivid.='/'.$env{'request.course.sec'};
3979: }
3980: $courseprivid=~s/\_/\//;
3981: my $checkreferer=1;
3982: my ($match,$cond)=&is_on_map($uri);
3983: if ($match) {
3984: $statecond=$cond;
3985: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
3986: =~/\Q$priv\E\&([^\:]*)/) {
3987: $thisallowed.=$1;
3988: $checkreferer=0;
3989: }
3990: }
3991:
3992: if ($checkreferer) {
3993: my $refuri=$env{'httpref.'.$orguri};
3994: unless ($refuri) {
3995: foreach my $key (keys(%env)) {
3996: if ($key=~/^httpref\..*\*/) {
3997: my $pattern=$key;
3998: $pattern=~s/^httpref\.\/res\///;
3999: $pattern=~s/\*/\[\^\/\]\+/g;
4000: $pattern=~s/\//\\\//g;
4001: if ($orguri=~/$pattern/) {
4002: $refuri=$env{$key};
4003: }
4004: }
4005: }
4006: }
4007:
4008: if ($refuri) {
4009: $refuri=&declutter($refuri);
4010: my ($match,$cond)=&is_on_map($refuri);
4011: if ($match) {
4012: my $refstatecond=$cond;
4013: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
4014: =~/\Q$priv\E\&([^\:]*)/) {
4015: $thisallowed.=$1;
4016: $uri=$refuri;
4017: $statecond=$refstatecond;
4018: }
4019: }
4020: }
4021: }
4022: }
4023:
4024: #
4025: # Gathered now: all privileges that could apply, and condition number
4026: #
4027: #
4028: # Full or no access?
4029: #
4030:
4031: if ($thisallowed=~/F/) {
4032: return 'F';
4033: }
4034:
4035: unless ($thisallowed) {
4036: return '';
4037: }
4038:
4039: # Restrictions exist, deal with them
4040: #
4041: # C:according to course preferences
4042: # R:according to resource settings
4043: # L:unless locked
4044: # X:according to user session state
4045: #
4046:
4047: # Possibly locked functionality, check all courses
4048: # Locks might take effect only after 10 minutes cache expiration for other
4049: # courses, and 2 minutes for current course
4050:
4051: my $envkey;
4052: if ($thisallowed=~/L/) {
4053: foreach $envkey (keys %env) {
4054: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
4055: my $courseid=$2;
4056: my $roleid=$1.'.'.$2;
4057: $courseid=~s/^\///;
4058: my $expiretime=600;
4059: if ($env{'request.role'} eq $roleid) {
4060: $expiretime=120;
4061: }
4062: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
4063: my $prefix='course.'.$cdom.'_'.$cnum.'.';
4064: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
4065: &coursedescription($courseid,{'freshen_cache' => 1});
4066: }
4067: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
4068: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
4069: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
4070: &log($env{'user.domain'},$env{'user.name'},
4071: $env{'user.home'},
4072: 'Locked by res: '.$priv.' for '.$uri.' due to '.
4073: $cdom.'/'.$cnum.'/'.$csec.' expire '.
4074: $env{$prefix.'priv.'.$priv.'.lock.expire'});
4075: return '';
4076: }
4077: }
4078: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
4079: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
4080: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
4081: &log($env{'user.domain'},$env{'user.name'},
4082: $env{'user.home'},
4083: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
4084: $cdom.'/'.$cnum.'/'.$csec.' expire '.
4085: $env{$prefix.'priv.'.$priv.'.lock.expire'});
4086: return '';
4087: }
4088: }
4089: }
4090: }
4091: }
4092:
4093: #
4094: # Rest of the restrictions depend on selected course
4095: #
4096:
4097: unless ($env{'request.course.id'}) {
4098: if ($thisallowed eq 'A') {
4099: return 'A';
4100: } elsif ($thisallowed eq 'B') {
4101: return 'B';
4102: } else {
4103: return '1';
4104: }
4105: }
4106:
4107: #
4108: # Now user is definitely in a course
4109: #
4110:
4111:
4112: # Course preferences
4113:
4114: if ($thisallowed=~/C/) {
4115: my $rolecode=(split(/\./,$env{'request.role'}))[0];
4116: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
4117: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
4118: =~/\Q$rolecode\E/) {
4119: if ($priv ne 'pch') {
4120: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4121: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
4122: $env{'request.course.id'});
4123: }
4124: return '';
4125: }
4126:
4127: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
4128: =~/\Q$unamedom\E/) {
4129: if ($priv ne 'pch') {
4130: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
4131: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
4132: $env{'request.course.id'});
4133: }
4134: return '';
4135: }
4136: }
4137:
4138: # Resource preferences
4139:
4140: if ($thisallowed=~/R/) {
4141: my $rolecode=(split(/\./,$env{'request.role'}))[0];
4142: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
4143: if ($priv ne 'pch') {
4144: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4145: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
4146: }
4147: return '';
4148: }
4149: }
4150:
4151: # Restricted by state or randomout?
4152:
4153: if ($thisallowed=~/X/) {
4154: if ($env{'acc.randomout'}) {
4155: if (!$symb) { $symb=&symbread($uri,1); }
4156: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
4157: return '';
4158: }
4159: }
4160: if (&condval($statecond)) {
4161: return '2';
4162: } else {
4163: return '';
4164: }
4165: }
4166:
4167: if ($thisallowed eq 'A') {
4168: return 'A';
4169: } elsif ($thisallowed eq 'B') {
4170: return 'B';
4171: }
4172: return 'F';
4173: }
4174:
4175: sub split_uri_for_cond {
4176: my $uri=&deversion(&declutter(shift));
4177: my @uriparts=split(/\//,$uri);
4178: my $filename=pop(@uriparts);
4179: my $pathname=join('/',@uriparts);
4180: return ($pathname,$filename);
4181: }
4182: # --------------------------------------------------- Is a resource on the map?
4183:
4184: sub is_on_map {
4185: my ($pathname,$filename) = &split_uri_for_cond(shift);
4186: #Trying to find the conditional for the file
4187: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
4188: /\&\Q$filename\E\:([\d\|]+)\&/);
4189: if ($match) {
4190: return (1,$1);
4191: } else {
4192: return (0,0);
4193: }
4194: }
4195:
4196: # --------------------------------------------------------- Get symb from alias
4197:
4198: sub get_symb_from_alias {
4199: my $symb=shift;
4200: my ($map,$resid,$url)=&decode_symb($symb);
4201: # Already is a symb
4202: if ($url) { return $symb; }
4203: # Must be an alias
4204: my $aliassymb='';
4205: my %bighash;
4206: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
4207: &GDBM_READER(),0640)) {
4208: my $rid=$bighash{'mapalias_'.$symb};
4209: if ($rid) {
4210: my ($mapid,$resid)=split(/\./,$rid);
4211: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
4212: $resid,$bighash{'src_'.$rid});
4213: }
4214: untie %bighash;
4215: }
4216: return $aliassymb;
4217: }
4218:
4219: # ----------------------------------------------------------------- Define Role
4220:
4221: sub definerole {
4222: if (allowed('mcr','/')) {
4223: my ($rolename,$sysrole,$domrole,$courole)=@_;
4224: foreach my $role (split(':',$sysrole)) {
4225: my ($crole,$cqual)=split(/\&/,$role);
4226: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
4227: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
4228: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
4229: return "refused:s:$crole&$cqual";
4230: }
4231: }
4232: }
4233: foreach my $role (split(':',$domrole)) {
4234: my ($crole,$cqual)=split(/\&/,$role);
4235: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
4236: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
4237: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
4238: return "refused:d:$crole&$cqual";
4239: }
4240: }
4241: }
4242: foreach my $role (split(':',$courole)) {
4243: my ($crole,$cqual)=split(/\&/,$role);
4244: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
4245: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
4246: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
4247: return "refused:c:$crole&$cqual";
4248: }
4249: }
4250: }
4251: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
4252: "$env{'user.domain'}:$env{'user.name'}:".
4253: "rolesdef_$rolename=".
4254: escape($sysrole.'_'.$domrole.'_'.$courole);
4255: return reply($command,$env{'user.home'});
4256: } else {
4257: return 'refused';
4258: }
4259: }
4260:
4261: # ---------------- Make a metadata query against the network of library servers
4262:
4263: sub metadata_query {
4264: my ($query,$custom,$customshow,$server_array)=@_;
4265: my %rhash;
4266: my %libserv = &all_library();
4267: my @server_list = (defined($server_array) ? @$server_array
4268: : keys(%libserv) );
4269: for my $server (@server_list) {
4270: unless ($custom or $customshow) {
4271: my $reply=&reply("querysend:".&escape($query),$server);
4272: $rhash{$server}=$reply;
4273: }
4274: else {
4275: my $reply=&reply("querysend:".&escape($query).':'.
4276: &escape($custom).':'.&escape($customshow),
4277: $server);
4278: $rhash{$server}=$reply;
4279: }
4280: }
4281: return \%rhash;
4282: }
4283:
4284: # ----------------------------------------- Send log queries and wait for reply
4285:
4286: sub log_query {
4287: my ($uname,$udom,$query,%filters)=@_;
4288: my $uhome=&homeserver($uname,$udom);
4289: if ($uhome eq 'no_host') { return 'error: no_host'; }
4290: my $uhost=&hostname($uhome);
4291: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
4292: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
4293: $uhome);
4294: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
4295: return get_query_reply($queryid);
4296: }
4297:
4298: # -------------------------- Update MySQL table for portfolio file
4299:
4300: sub update_portfolio_table {
4301: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
4302: my $homeserver = &homeserver($uname,$udom);
4303: my $queryid=
4304: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
4305: ':'.&escape($file_name).':'.$action,$homeserver);
4306: my $reply = &get_query_reply($queryid);
4307: return $reply;
4308: }
4309:
4310: # ------- Request retrieval of institutional classlists for course(s)
4311:
4312: sub fetch_enrollment_query {
4313: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
4314: my $homeserver;
4315: my $maxtries = 1;
4316: if ($context eq 'automated') {
4317: $homeserver = $perlvar{'lonHostID'};
4318: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
4319: } else {
4320: $homeserver = &homeserver($cnum,$dom);
4321: }
4322: my $host=&hostname($homeserver);
4323: my $cmd = '';
4324: foreach my $affiliate (keys %{$affiliatesref}) {
4325: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
4326: }
4327: $cmd =~ s/%%$//;
4328: $cmd = &escape($cmd);
4329: my $query = 'fetchenrollment';
4330: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
4331: unless ($queryid=~/^\Q$host\E\_/) {
4332: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
4333: return 'error: '.$queryid;
4334: }
4335: my $reply = &get_query_reply($queryid);
4336: my $tries = 1;
4337: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
4338: $reply = &get_query_reply($queryid);
4339: $tries ++;
4340: }
4341: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
4342: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
4343: } else {
4344: my @responses = split/:/,$reply;
4345: if ($homeserver eq $perlvar{'lonHostID'}) {
4346: foreach my $line (@responses) {
4347: my ($key,$value) = split(/=/,$line,2);
4348: $$replyref{$key} = $value;
4349: }
4350: } else {
4351: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
4352: foreach my $line (@responses) {
4353: my ($key,$value) = split(/=/,$line);
4354: $$replyref{$key} = $value;
4355: if ($value > 0) {
4356: foreach my $item (@{$$affiliatesref{$key}}) {
4357: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
4358: my $destname = $pathname.'/'.$filename;
4359: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
4360: if ($xml_classlist =~ /^error/) {
4361: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
4362: } else {
4363: if ( open(FILE,">$destname") ) {
4364: print FILE &unescape($xml_classlist);
4365: close(FILE);
4366: } else {
4367: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
4368: }
4369: }
4370: }
4371: }
4372: }
4373: }
4374: return 'ok';
4375: }
4376: return 'error';
4377: }
4378:
4379: sub get_query_reply {
4380: my $queryid=shift;
4381: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
4382: my $reply='';
4383: for (1..100) {
4384: sleep 2;
4385: if (-e $replyfile.'.end') {
4386: if (open(my $fh,$replyfile)) {
4387: $reply.=<$fh>;
4388: close($fh);
4389: } else { return 'error: reply_file_error'; }
4390: return &unescape($reply);
4391: }
4392: }
4393: return 'timeout:'.$queryid;
4394: }
4395:
4396: sub courselog_query {
4397: #
4398: # possible filters:
4399: # url: url or symb
4400: # username
4401: # domain
4402: # action: view, submit, grade
4403: # start: timestamp
4404: # end: timestamp
4405: #
4406: my (%filters)=@_;
4407: unless ($env{'request.course.id'}) { return 'no_course'; }
4408: if ($filters{'url'}) {
4409: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
4410: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
4411: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
4412: }
4413: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
4414: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4415: return &log_query($cname,$cdom,'courselog',%filters);
4416: }
4417:
4418: sub userlog_query {
4419: #
4420: # possible filters:
4421: # action: log check role
4422: # start: timestamp
4423: # end: timestamp
4424: #
4425: my ($uname,$udom,%filters)=@_;
4426: return &log_query($uname,$udom,'userlog',%filters);
4427: }
4428:
4429: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
4430:
4431: sub auto_run {
4432: my ($cnum,$cdom) = @_;
4433: my $homeserver = &homeserver($cnum,$cdom);
4434: my $response = &reply('autorun:'.$cdom,$homeserver);
4435: return $response;
4436: }
4437:
4438: sub auto_get_sections {
4439: my ($cnum,$cdom,$inst_coursecode) = @_;
4440: my $homeserver = &homeserver($cnum,$cdom);
4441: my @secs = ();
4442: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
4443: unless ($response eq 'refused') {
4444: @secs = split/:/,$response;
4445: }
4446: return @secs;
4447: }
4448:
4449: sub auto_new_course {
4450: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
4451: my $homeserver = &homeserver($cnum,$cdom);
4452: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
4453: return $response;
4454: }
4455:
4456: sub auto_validate_courseID {
4457: my ($cnum,$cdom,$inst_course_id) = @_;
4458: my $homeserver = &homeserver($cnum,$cdom);
4459: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
4460: return $response;
4461: }
4462:
4463: sub auto_create_password {
4464: my ($cnum,$cdom,$authparam) = @_;
4465: my $homeserver = &homeserver($cnum,$cdom);
4466: my $create_passwd = 0;
4467: my $authchk = '';
4468: my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
4469: if ($response eq 'refused') {
4470: $authchk = 'refused';
4471: } else {
4472: ($authparam,$create_passwd,$authchk) = split/:/,$response;
4473: }
4474: return ($authparam,$create_passwd,$authchk);
4475: }
4476:
4477: sub auto_photo_permission {
4478: my ($cnum,$cdom,$students) = @_;
4479: my $homeserver = &homeserver($cnum,$cdom);
4480: my ($outcome,$perm_reqd,$conditions) =
4481: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
4482: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4483: return (undef,undef);
4484: }
4485: return ($outcome,$perm_reqd,$conditions);
4486: }
4487:
4488: sub auto_checkphotos {
4489: my ($uname,$udom,$pid) = @_;
4490: my $homeserver = &homeserver($uname,$udom);
4491: my ($result,$resulttype);
4492: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
4493: &escape($uname).':'.&escape($pid),
4494: $homeserver));
4495: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4496: return (undef,undef);
4497: }
4498: if ($outcome) {
4499: ($result,$resulttype) = split(/:/,$outcome);
4500: }
4501: return ($result,$resulttype);
4502: }
4503:
4504: sub auto_photochoice {
4505: my ($cnum,$cdom) = @_;
4506: my $homeserver = &homeserver($cnum,$cdom);
4507: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
4508: &escape($cdom),
4509: $homeserver)));
4510: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4511: return (undef,undef);
4512: }
4513: return ($update,$comment);
4514: }
4515:
4516: sub auto_photoupdate {
4517: my ($affiliatesref,$dom,$cnum,$photo) = @_;
4518: my $homeserver = &homeserver($cnum,$dom);
4519: my $host=&hostname($homeserver);
4520: my $cmd = '';
4521: my $maxtries = 1;
4522: foreach my $affiliate (keys(%{$affiliatesref})) {
4523: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
4524: }
4525: $cmd =~ s/%%$//;
4526: $cmd = &escape($cmd);
4527: my $query = 'institutionalphotos';
4528: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
4529: unless ($queryid=~/^\Q$host\E\_/) {
4530: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
4531: return 'error: '.$queryid;
4532: }
4533: my $reply = &get_query_reply($queryid);
4534: my $tries = 1;
4535: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
4536: $reply = &get_query_reply($queryid);
4537: $tries ++;
4538: }
4539: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
4540: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
4541: } else {
4542: my @responses = split(/:/,$reply);
4543: my $outcome = shift(@responses);
4544: foreach my $item (@responses) {
4545: my ($key,$value) = split(/=/,$item);
4546: $$photo{$key} = $value;
4547: }
4548: return $outcome;
4549: }
4550: return 'error';
4551: }
4552:
4553: sub auto_instcode_format {
4554: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
4555: $cat_order) = @_;
4556: my $courses = '';
4557: my @homeservers;
4558: if ($caller eq 'global') {
4559: my %servers = &get_servers($codedom,'library');
4560: foreach my $tryserver (keys(%servers)) {
4561: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
4562: push(@homeservers,$tryserver);
4563: }
4564: }
4565: } else {
4566: push(@homeservers,&homeserver($caller,$codedom));
4567: }
4568: foreach my $code (keys(%{$instcodes})) {
4569: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
4570: }
4571: chop($courses);
4572: my $ok_response = 0;
4573: my $response;
4574: while (@homeservers > 0 && $ok_response == 0) {
4575: my $server = shift(@homeservers);
4576: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
4577: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
4578: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
4579: split/:/,$response;
4580: %{$codes} = (%{$codes},&str2hash($codes_str));
4581: push(@{$codetitles},&str2array($codetitles_str));
4582: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
4583: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
4584: $ok_response = 1;
4585: }
4586: }
4587: if ($ok_response) {
4588: return 'ok';
4589: } else {
4590: return $response;
4591: }
4592: }
4593:
4594: sub auto_instcode_defaults {
4595: my ($domain,$returnhash,$code_order) = @_;
4596: my @homeservers;
4597:
4598: my %servers = &get_servers($domain,'library');
4599: foreach my $tryserver (keys(%servers)) {
4600: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
4601: push(@homeservers,$tryserver);
4602: }
4603: }
4604:
4605: my $response;
4606: foreach my $server (@homeservers) {
4607: $response=&reply('autoinstcodedefaults:'.$domain,$server);
4608: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
4609:
4610: foreach my $pair (split(/\&/,$response)) {
4611: my ($name,$value)=split(/\=/,$pair);
4612: if ($name eq 'code_order') {
4613: @{$code_order} = split(/\&/,&unescape($value));
4614: } else {
4615: $returnhash->{&unescape($name)}=&unescape($value);
4616: }
4617: }
4618: return 'ok';
4619: }
4620:
4621: return $response;
4622: }
4623:
4624: sub auto_validate_class_sec {
4625: my ($cdom,$cnum,$owner,$inst_class) = @_;
4626: my $homeserver = &homeserver($cnum,$cdom);
4627: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
4628: &escape($owner).':'.$cdom,$homeserver);
4629: return $response;
4630: }
4631:
4632: # ------------------------------------------------------- Course Group routines
4633:
4634: sub get_coursegroups {
4635: my ($cdom,$cnum,$group,$namespace) = @_;
4636: return(&dump($namespace,$cdom,$cnum,$group));
4637: }
4638:
4639: sub modify_coursegroup {
4640: my ($cdom,$cnum,$groupsettings) = @_;
4641: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
4642: }
4643:
4644: sub toggle_coursegroup_status {
4645: my ($cdom,$cnum,$group,$action) = @_;
4646: my ($from_namespace,$to_namespace);
4647: if ($action eq 'delete') {
4648: $from_namespace = 'coursegroups';
4649: $to_namespace = 'deleted_groups';
4650: } else {
4651: $from_namespace = 'deleted_groups';
4652: $to_namespace = 'coursegroups';
4653: }
4654: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
4655: if (my $tmp = &error(%curr_group)) {
4656: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
4657: return ('read error',$tmp);
4658: } else {
4659: my %savedsettings = %curr_group;
4660: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
4661: my $deloutcome;
4662: if ($result eq 'ok') {
4663: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
4664: } else {
4665: return ('write error',$result);
4666: }
4667: if ($deloutcome eq 'ok') {
4668: return 'ok';
4669: } else {
4670: return ('delete error',$deloutcome);
4671: }
4672: }
4673: }
4674:
4675: sub modify_group_roles {
4676: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
4677: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
4678: my $role = 'gr/'.&escape($userprivs);
4679: my ($uname,$udom) = split(/:/,$user);
4680: my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
4681: if ($result eq 'ok') {
4682: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
4683: }
4684: return $result;
4685: }
4686:
4687: sub modify_coursegroup_membership {
4688: my ($cdom,$cnum,$membership) = @_;
4689: my $result = &put('groupmembership',$membership,$cdom,$cnum);
4690: return $result;
4691: }
4692:
4693: sub get_active_groups {
4694: my ($udom,$uname,$cdom,$cnum) = @_;
4695: my $now = time;
4696: my %groups = ();
4697: foreach my $key (keys(%env)) {
4698: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
4699: my ($start,$end) = split(/\./,$env{$key});
4700: if (($end!=0) && ($end<$now)) { next; }
4701: if (($start!=0) && ($start>$now)) { next; }
4702: if ($1 eq $cdom && $2 eq $cnum) {
4703: $groups{$3} = $env{$key} ;
4704: }
4705: }
4706: }
4707: return %groups;
4708: }
4709:
4710: sub get_group_membership {
4711: my ($cdom,$cnum,$group) = @_;
4712: return(&dump('groupmembership',$cdom,$cnum,$group));
4713: }
4714:
4715: sub get_users_groups {
4716: my ($udom,$uname,$courseid) = @_;
4717: my @usersgroups;
4718: my $cachetime=1800;
4719:
4720: my $hashid="$udom:$uname:$courseid";
4721: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
4722: if (defined($cached)) {
4723: @usersgroups = split(/:/,$grouplist);
4724: } else {
4725: $grouplist = '';
4726: my $courseurl = &courseid_to_courseurl($courseid);
4727: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
4728: my $access_end = $env{'course.'.$courseid.
4729: '.default_enrollment_end_date'};
4730: my $now = time;
4731: foreach my $key (keys(%roleshash)) {
4732: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
4733: my $group = $1;
4734: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
4735: my $start = $2;
4736: my $end = $1;
4737: if ($start == -1) { next; } # deleted from group
4738: if (($start!=0) && ($start>$now)) { next; }
4739: if (($end!=0) && ($end<$now)) {
4740: if ($access_end && $access_end < $now) {
4741: if ($access_end - $end < 86400) {
4742: push(@usersgroups,$group);
4743: }
4744: }
4745: next;
4746: }
4747: push(@usersgroups,$group);
4748: }
4749: }
4750: }
4751: @usersgroups = &sort_course_groups($courseid,@usersgroups);
4752: $grouplist = join(':',@usersgroups);
4753: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
4754: }
4755: return @usersgroups;
4756: }
4757:
4758: sub devalidate_getgroups_cache {
4759: my ($udom,$uname,$cdom,$cnum)=@_;
4760: my $courseid = $cdom.'_'.$cnum;
4761:
4762: my $hashid="$udom:$uname:$courseid";
4763: &devalidate_cache_new('getgroups',$hashid);
4764: }
4765:
4766: # ------------------------------------------------------------------ Plain Text
4767:
4768: sub plaintext {
4769: my ($short,$type,$cid) = @_;
4770: if ($short =~ /^cr/) {
4771: return (split('/',$short))[-1];
4772: }
4773: if (!defined($cid)) {
4774: $cid = $env{'request.course.id'};
4775: }
4776: if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
4777: return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
4778: '.plaintext'});
4779: }
4780: my %rolenames = (
4781: Course => 'std',
4782: Group => 'alt1',
4783: );
4784: if (defined($type) &&
4785: defined($rolenames{$type}) &&
4786: defined($prp{$short}{$rolenames{$type}})) {
4787: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
4788: } else {
4789: return &Apache::lonlocal::mt($prp{$short}{'std'});
4790: }
4791: }
4792:
4793: # ----------------------------------------------------------------- Assign Role
4794:
4795: sub assignrole {
4796: my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
4797: my $mrole;
4798: if ($role =~ /^cr\//) {
4799: my $cwosec=$url;
4800: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
4801: unless (&allowed('ccr',$cwosec)) {
4802: &logthis('Refused custom assignrole: '.
4803: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
4804: $env{'user.name'}.' at '.$env{'user.domain'});
4805: return 'refused';
4806: }
4807: $mrole='cr';
4808: } elsif ($role =~ /^gr\//) {
4809: my $cwogrp=$url;
4810: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
4811: unless (&allowed('mdg',$cwogrp)) {
4812: &logthis('Refused group assignrole: '.
4813: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
4814: $env{'user.name'}.' at '.$env{'user.domain'});
4815: return 'refused';
4816: }
4817: $mrole='gr';
4818: } else {
4819: my $cwosec=$url;
4820: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
4821: unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) {
4822: &logthis('Refused assignrole: '.
4823: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
4824: $env{'user.name'}.' at '.$env{'user.domain'});
4825: return 'refused';
4826: }
4827: $mrole=$role;
4828: }
4829: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
4830: "$udom:$uname:$url".'_'."$mrole=$role";
4831: if ($end) { $command.='_'.$end; }
4832: if ($start) {
4833: if ($end) {
4834: $command.='_'.$start;
4835: } else {
4836: $command.='_0_'.$start;
4837: }
4838: }
4839: my $origstart = $start;
4840: my $origend = $end;
4841: # actually delete
4842: if ($deleteflag) {
4843: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
4844: # modify command to delete the role
4845: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
4846: "$udom:$uname:$url".'_'."$mrole";
4847: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
4848: # set start and finish to negative values for userrolelog
4849: $start=-1;
4850: $end=-1;
4851: }
4852: }
4853: # send command
4854: my $answer=&reply($command,&homeserver($uname,$udom));
4855: # log new user role if status is ok
4856: if ($answer eq 'ok') {
4857: &userrolelog($role,$uname,$udom,$url,$start,$end);
4858: # for course roles, perform group memberships changes triggered by role change.
4859: unless ($role =~ /^gr/) {
4860: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
4861: $origstart);
4862: }
4863: }
4864: return $answer;
4865: }
4866:
4867: # -------------------------------------------------- Modify user authentication
4868: # Overrides without validation
4869:
4870: sub modifyuserauth {
4871: my ($udom,$uname,$umode,$upass)=@_;
4872: my $uhome=&homeserver($uname,$udom);
4873: unless (&allowed('mau',$udom)) { return 'refused'; }
4874: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
4875: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4876: ' in domain '.$env{'request.role.domain'});
4877: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
4878: &escape($upass),$uhome);
4879: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
4880: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
4881: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
4882: &log($udom,,$uname,$uhome,
4883: 'Authentication changed by '.$env{'user.domain'}.', '.
4884: $env{'user.name'}.', '.$umode.
4885: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
4886: unless ($reply eq 'ok') {
4887: &logthis('Authentication mode error: '.$reply);
4888: return 'error: '.$reply;
4889: }
4890: return 'ok';
4891: }
4892:
4893: # --------------------------------------------------------------- Modify a user
4894:
4895: sub modifyuser {
4896: my ($udom, $uname, $uid,
4897: $umode, $upass, $first,
4898: $middle, $last, $gene,
4899: $forceid, $desiredhome, $email)=@_;
4900: $udom= &LONCAPA::clean_domain($udom);
4901: $uname=&LONCAPA::clean_username($uname);
4902: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
4903: $umode.', '.$first.', '.$middle.', '.
4904: $last.', '.$gene.'(forceid: '.$forceid.')'.
4905: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
4906: ' desiredhome not specified').
4907: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4908: ' in domain '.$env{'request.role.domain'});
4909: my $uhome=&homeserver($uname,$udom,'true');
4910: # ----------------------------------------------------------------- Create User
4911: if (($uhome eq 'no_host') &&
4912: (($umode && $upass) || ($umode eq 'localauth'))) {
4913: my $unhome='';
4914: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
4915: $unhome = $desiredhome;
4916: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
4917: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
4918: } else { # load balancing routine for determining $unhome
4919: my $loadm=10000000;
4920: my %servers = &get_servers($udom,'library');
4921: foreach my $tryserver (keys(%servers)) {
4922: my $answer=reply('load',$tryserver);
4923: if (($answer=~/\d+/) && ($answer<$loadm)) {
4924: $loadm=$answer;
4925: $unhome=$tryserver;
4926: }
4927: }
4928: }
4929: if (($unhome eq '') || ($unhome eq 'no_host')) {
4930: return 'error: unable to find a home server for '.$uname.
4931: ' in domain '.$udom;
4932: }
4933: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
4934: &escape($upass),$unhome);
4935: unless ($reply eq 'ok') {
4936: return 'error: '.$reply;
4937: }
4938: $uhome=&homeserver($uname,$udom,'true');
4939: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
4940: return 'error: unable verify users home machine.';
4941: }
4942: } # End of creation of new user
4943: # ---------------------------------------------------------------------- Add ID
4944: if ($uid) {
4945: $uid=~tr/A-Z/a-z/;
4946: my %uidhash=&idrget($udom,$uname);
4947: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
4948: && (!$forceid)) {
4949: unless ($uid eq $uidhash{$uname}) {
4950: return 'error: user id "'.$uid.'" does not match '.
4951: 'current user id "'.$uidhash{$uname}.'".';
4952: }
4953: } else {
4954: &idput($udom,($uname => $uid));
4955: }
4956: }
4957: # -------------------------------------------------------------- Add names, etc
4958: my @tmp=&get('environment',
4959: ['firstname','middlename','lastname','generation'],
4960: $udom,$uname);
4961: my %names;
4962: if ($tmp[0] =~ m/^error:.*/) {
4963: %names=();
4964: } else {
4965: %names = @tmp;
4966: }
4967: #
4968: # Make sure to not trash student environment if instructor does not bother
4969: # to supply name and email information
4970: #
4971: if ($first) { $names{'firstname'} = $first; }
4972: if (defined($middle)) { $names{'middlename'} = $middle; }
4973: if ($last) { $names{'lastname'} = $last; }
4974: if (defined($gene)) { $names{'generation'} = $gene; }
4975: if ($email) {
4976: $email=~s/[^\w\@\.\-\,]//gs;
4977: if ($email=~/\@/) { $names{'notification'} = $email;
4978: $names{'critnotification'} = $email;
4979: $names{'permanentemail'} = $email; }
4980: }
4981: my $reply = &put('environment', \%names, $udom,$uname);
4982: if ($reply ne 'ok') { return 'error: '.$reply; }
4983: &devalidate_cache_new('namescache',$uname.':'.$udom);
4984: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
4985: $umode.', '.$first.', '.$middle.', '.
4986: $last.', '.$gene.' by '.
4987: $env{'user.name'}.' at '.$env{'user.domain'});
4988: return 'ok';
4989: }
4990:
4991: # -------------------------------------------------------------- Modify student
4992:
4993: sub modifystudent {
4994: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
4995: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
4996: if (!$cid) {
4997: unless ($cid=$env{'request.course.id'}) {
4998: return 'not_in_class';
4999: }
5000: }
5001: # --------------------------------------------------------------- Make the user
5002: my $reply=&modifyuser
5003: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
5004: $desiredhome,$email);
5005: unless ($reply eq 'ok') { return $reply; }
5006: # This will cause &modify_student_enrollment to get the uid from the
5007: # students environment
5008: $uid = undef if (!$forceid);
5009: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
5010: $gene,$usec,$end,$start,$type,$locktype,$cid);
5011: return $reply;
5012: }
5013:
5014: sub modify_student_enrollment {
5015: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
5016: my ($cdom,$cnum,$chome);
5017: if (!$cid) {
5018: unless ($cid=$env{'request.course.id'}) {
5019: return 'not_in_class';
5020: }
5021: $cdom=$env{'course.'.$cid.'.domain'};
5022: $cnum=$env{'course.'.$cid.'.num'};
5023: } else {
5024: ($cdom,$cnum)=split(/_/,$cid);
5025: }
5026: $chome=$env{'course.'.$cid.'.home'};
5027: if (!$chome) {
5028: $chome=&homeserver($cnum,$cdom);
5029: }
5030: if (!$chome) { return 'unknown_course'; }
5031: # Make sure the user exists
5032: my $uhome=&homeserver($uname,$udom);
5033: if (($uhome eq '') || ($uhome eq 'no_host')) {
5034: return 'error: no such user';
5035: }
5036: # Get student data if we were not given enough information
5037: if (!defined($first) || $first eq '' ||
5038: !defined($last) || $last eq '' ||
5039: !defined($uid) || $uid eq '' ||
5040: !defined($middle) || $middle eq '' ||
5041: !defined($gene) || $gene eq '') {
5042: # They did not supply us with enough data to enroll the student, so
5043: # we need to pick up more information.
5044: my %tmp = &get('environment',
5045: ['firstname','middlename','lastname', 'generation','id']
5046: ,$udom,$uname);
5047:
5048: #foreach my $key (keys(%tmp)) {
5049: # &logthis("key $key = ".$tmp{$key});
5050: #}
5051: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
5052: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
5053: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
5054: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
5055: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
5056: }
5057: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
5058: my $reply=cput('classlist',
5059: {"$uname:$udom" =>
5060: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
5061: $cdom,$cnum);
5062: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
5063: return 'error: '.$reply;
5064: } else {
5065: &devalidate_getsection_cache($udom,$uname,$cid);
5066: }
5067: # Add student role to user
5068: my $uurl='/'.$cid;
5069: $uurl=~s/\_/\//g;
5070: if ($usec) {
5071: $uurl.='/'.$usec;
5072: }
5073: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
5074: }
5075:
5076: sub format_name {
5077: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
5078: my $name;
5079: if ($first ne 'lastname') {
5080: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
5081: } else {
5082: if ($lastname=~/\S/) {
5083: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
5084: $name=~s/\s+,/,/;
5085: } else {
5086: $name.= $firstname.' '.$middlename.' '.$generation;
5087: }
5088: }
5089: $name=~s/^\s+//;
5090: $name=~s/\s+$//;
5091: $name=~s/\s+/ /g;
5092: return $name;
5093: }
5094:
5095: # ------------------------------------------------- Write to course preferences
5096:
5097: sub writecoursepref {
5098: my ($courseid,%prefs)=@_;
5099: $courseid=~s/^\///;
5100: $courseid=~s/\_/\//g;
5101: my ($cdomain,$cnum)=split(/\//,$courseid);
5102: my $chome=homeserver($cnum,$cdomain);
5103: if (($chome eq '') || ($chome eq 'no_host')) {
5104: return 'error: no such course';
5105: }
5106: my $cstring='';
5107: foreach my $pref (keys(%prefs)) {
5108: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
5109: }
5110: $cstring=~s/\&$//;
5111: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
5112: }
5113:
5114: # ---------------------------------------------------------- Make/modify course
5115:
5116: sub createcourse {
5117: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
5118: $course_owner,$crstype)=@_;
5119: $url=&declutter($url);
5120: my $cid='';
5121: unless (&allowed('ccc',$udom)) {
5122: return 'refused';
5123: }
5124: # ------------------------------------------------------------------- Create ID
5125: my $uname=int(1+rand(9)).
5126: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
5127: substr($$.time,0,5).unpack("H8",pack("I32",time)).
5128: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
5129: # ----------------------------------------------- Make sure that does not exist
5130: my $uhome=&homeserver($uname,$udom,'true');
5131: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5132: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
5133: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
5134: $uhome=&homeserver($uname,$udom,'true');
5135: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5136: return 'error: unable to generate unique course-ID';
5137: }
5138: }
5139: # ------------------------------------------------ Check supplied server name
5140: $course_server = $env{'user.homeserver'} if (! defined($course_server));
5141: if (! &is_library($course_server)) {
5142: return 'error:bad server name '.$course_server;
5143: }
5144: # ------------------------------------------------------------- Make the course
5145: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
5146: $course_server);
5147: unless ($reply eq 'ok') { return 'error: '.$reply; }
5148: $uhome=&homeserver($uname,$udom,'true');
5149: if (($uhome eq '') || ($uhome eq 'no_host')) {
5150: return 'error: no such course';
5151: }
5152: # ----------------------------------------------------------------- Course made
5153: # log existence
5154: &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
5155: ':'.&escape($inst_code).':'.&escape($course_owner).':'.
5156: &escape($crstype),$uhome);
5157: &flushcourselogs();
5158: # set toplevel url
5159: my $topurl=$url;
5160: unless ($nonstandard) {
5161: # ------------------------------------------ For standard courses, make top url
5162: my $mapurl=&clutter($url);
5163: if ($mapurl eq '/res/') { $mapurl=''; }
5164: $env{'form.initmap'}=(<<ENDINITMAP);
5165: <map>
5166: <resource id="1" type="start"></resource>
5167: <resource id="2" src="$mapurl"></resource>
5168: <resource id="3" type="finish"></resource>
5169: <link index="1" from="1" to="2"></link>
5170: <link index="2" from="2" to="3"></link>
5171: </map>
5172: ENDINITMAP
5173: $topurl=&declutter(
5174: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
5175: );
5176: }
5177: # ----------------------------------------------------------- Write preferences
5178: &writecoursepref($udom.'_'.$uname,
5179: ('description' => $description,
5180: 'url' => $topurl));
5181: return '/'.$udom.'/'.$uname;
5182: }
5183:
5184: sub is_course {
5185: my ($cdom,$cnum) = @_;
5186: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
5187: undef,'.');
5188: if (exists($courses{$cdom.'_'.$cnum})) {
5189: return 1;
5190: }
5191: return 0;
5192: }
5193:
5194: # ---------------------------------------------------------- Assign Custom Role
5195:
5196: sub assigncustomrole {
5197: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
5198: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
5199: $end,$start,$deleteflag);
5200: }
5201:
5202: # ----------------------------------------------------------------- Revoke Role
5203:
5204: sub revokerole {
5205: my ($udom,$uname,$url,$role,$deleteflag)=@_;
5206: my $now=time;
5207: return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
5208: }
5209:
5210: # ---------------------------------------------------------- Revoke Custom Role
5211:
5212: sub revokecustomrole {
5213: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
5214: my $now=time;
5215: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
5216: $deleteflag);
5217: }
5218:
5219: # ------------------------------------------------------------ Disk usage
5220: sub diskusage {
5221: my ($udom,$uname,$directoryRoot)=@_;
5222: $directoryRoot =~ s/\/$//;
5223: my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
5224: return $listing;
5225: }
5226:
5227: sub is_locked {
5228: my ($file_name, $domain, $user) = @_;
5229: my @check;
5230: my $is_locked;
5231: push @check, $file_name;
5232: my %locked = &get('file_permissions',\@check,
5233: $env{'user.domain'},$env{'user.name'});
5234: my ($tmp)=keys(%locked);
5235: if ($tmp=~/^error:/) { undef(%locked); }
5236:
5237: if (ref($locked{$file_name}) eq 'ARRAY') {
5238: $is_locked = 'false';
5239: foreach my $entry (@{$locked{$file_name}}) {
5240: if (ref($entry) eq 'ARRAY') {
5241: $is_locked = 'true';
5242: last;
5243: }
5244: }
5245: } else {
5246: $is_locked = 'false';
5247: }
5248: }
5249:
5250: sub declutter_portfile {
5251: my ($file) = @_;
5252: $file =~ s{^(/portfolio/|portfolio/)}{/};
5253: return $file;
5254: }
5255:
5256: # ------------------------------------------------------------- Mark as Read Only
5257:
5258: sub mark_as_readonly {
5259: my ($domain,$user,$files,$what) = @_;
5260: my %current_permissions = &dump('file_permissions',$domain,$user);
5261: my ($tmp)=keys(%current_permissions);
5262: if ($tmp=~/^error:/) { undef(%current_permissions); }
5263: foreach my $file (@{$files}) {
5264: $file = &declutter_portfile($file);
5265: push(@{$current_permissions{$file}},$what);
5266: }
5267: &put('file_permissions',\%current_permissions,$domain,$user);
5268: return;
5269: }
5270:
5271: # ------------------------------------------------------------Save Selected Files
5272:
5273: sub save_selected_files {
5274: my ($user, $path, @files) = @_;
5275: my $filename = $user."savedfiles";
5276: my @other_files = &files_not_in_path($user, $path);
5277: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5278: foreach my $file (@files) {
5279: print (OUT $env{'form.currentpath'}.$file."\n");
5280: }
5281: foreach my $file (@other_files) {
5282: print (OUT $file."\n");
5283: }
5284: close (OUT);
5285: return 'ok';
5286: }
5287:
5288: sub clear_selected_files {
5289: my ($user) = @_;
5290: my $filename = $user."savedfiles";
5291: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5292: print (OUT undef);
5293: close (OUT);
5294: return ("ok");
5295: }
5296:
5297: sub files_in_path {
5298: my ($user, $path) = @_;
5299: my $filename = $user."savedfiles";
5300: my %return_files;
5301: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5302: while (my $line_in = <IN>) {
5303: chomp ($line_in);
5304: my @paths_and_file = split (m!/!, $line_in);
5305: my $file_part = pop (@paths_and_file);
5306: my $path_part = join ('/', @paths_and_file);
5307: $path_part.='/';
5308: my $path_and_file = $path_part.$file_part;
5309: if ($path_part eq $path) {
5310: $return_files{$file_part}= 'selected';
5311: }
5312: }
5313: close (IN);
5314: return (\%return_files);
5315: }
5316:
5317: # called in portfolio select mode, to show files selected NOT in current directory
5318: sub files_not_in_path {
5319: my ($user, $path) = @_;
5320: my $filename = $user."savedfiles";
5321: my @return_files;
5322: my $path_part;
5323: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5324: while (my $line = <IN>) {
5325: #ok, I know it's clunky, but I want it to work
5326: my @paths_and_file = split(m|/|, $line);
5327: my $file_part = pop(@paths_and_file);
5328: chomp($file_part);
5329: my $path_part = join('/', @paths_and_file);
5330: $path_part .= '/';
5331: my $path_and_file = $path_part.$file_part;
5332: if ($path_part ne $path) {
5333: push(@return_files, ($path_and_file));
5334: }
5335: }
5336: close(OUT);
5337: return (@return_files);
5338: }
5339:
5340: #----------------------------------------------Get portfolio file permissions
5341:
5342: sub get_portfile_permissions {
5343: my ($domain,$user) = @_;
5344: my %current_permissions = &dump('file_permissions',$domain,$user);
5345: my ($tmp)=keys(%current_permissions);
5346: if ($tmp=~/^error:/) { undef(%current_permissions); }
5347: return \%current_permissions;
5348: }
5349:
5350: #---------------------------------------------Get portfolio file access controls
5351:
5352: sub get_access_controls {
5353: my ($current_permissions,$group,$file) = @_;
5354: my %access;
5355: my $real_file = $file;
5356: $file =~ s/\.meta$//;
5357: if (defined($file)) {
5358: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
5359: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
5360: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
5361: }
5362: }
5363: } else {
5364: foreach my $key (keys(%{$current_permissions})) {
5365: if ($key =~ /\0accesscontrol$/) {
5366: if (defined($group)) {
5367: if ($key !~ m-^\Q$group\E/-) {
5368: next;
5369: }
5370: }
5371: my ($fullpath) = split(/\0/,$key);
5372: if (ref($$current_permissions{$key}) eq 'HASH') {
5373: foreach my $control (keys(%{$$current_permissions{$key}})) {
5374: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
5375: }
5376: }
5377: }
5378: }
5379: }
5380: return %access;
5381: }
5382:
5383: sub modify_access_controls {
5384: my ($file_name,$changes,$domain,$user)=@_;
5385: my ($outcome,$deloutcome);
5386: my %store_permissions;
5387: my %new_values;
5388: my %new_control;
5389: my %translation;
5390: my @deletions = ();
5391: my $now = time;
5392: if (exists($$changes{'activate'})) {
5393: if (ref($$changes{'activate'}) eq 'HASH') {
5394: my @newitems = sort(keys(%{$$changes{'activate'}}));
5395: my $numnew = scalar(@newitems);
5396: for (my $i=0; $i<$numnew; $i++) {
5397: my $newkey = $newitems[$i];
5398: my $newid = &Apache::loncommon::get_cgi_id();
5399: if ($newkey =~ /^\d+:/) {
5400: $newkey =~ s/^(\d+)/$newid/;
5401: $translation{$1} = $newid;
5402: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
5403: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
5404: $translation{$1} = $newid;
5405: }
5406: $new_values{$file_name."\0".$newkey} =
5407: $$changes{'activate'}{$newitems[$i]};
5408: $new_control{$newkey} = $now;
5409: }
5410: }
5411: }
5412: my %todelete;
5413: my %changed_items;
5414: foreach my $action ('delete','update') {
5415: if (exists($$changes{$action})) {
5416: if (ref($$changes{$action}) eq 'HASH') {
5417: foreach my $key (keys(%{$$changes{$action}})) {
5418: my ($itemnum) = ($key =~ /^([^:]+):/);
5419: if ($action eq 'delete') {
5420: $todelete{$itemnum} = 1;
5421: } else {
5422: $changed_items{$itemnum} = $key;
5423: }
5424: }
5425: }
5426: }
5427: }
5428: # get lock on access controls for file.
5429: my $lockhash = {
5430: $file_name."\0".'locked_access_records' => $env{'user.name'}.
5431: ':'.$env{'user.domain'},
5432: };
5433: my $tries = 0;
5434: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5435:
5436: while (($gotlock ne 'ok') && $tries <3) {
5437: $tries ++;
5438: sleep 1;
5439: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5440: }
5441: if ($gotlock eq 'ok') {
5442: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
5443: my ($tmp)=keys(%curr_permissions);
5444: if ($tmp=~/^error:/) { undef(%curr_permissions); }
5445: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
5446: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
5447: if (ref($curr_controls) eq 'HASH') {
5448: foreach my $control_item (keys(%{$curr_controls})) {
5449: my ($itemnum) = ($control_item =~ /^([^:]+):/);
5450: if (defined($todelete{$itemnum})) {
5451: push(@deletions,$file_name."\0".$control_item);
5452: } else {
5453: if (defined($changed_items{$itemnum})) {
5454: $new_control{$changed_items{$itemnum}} = $now;
5455: push(@deletions,$file_name."\0".$control_item);
5456: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
5457: } else {
5458: $new_control{$control_item} = $$curr_controls{$control_item};
5459: }
5460: }
5461: }
5462: }
5463: }
5464: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
5465: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
5466: $outcome = &put('file_permissions',\%new_values,$domain,$user);
5467: # remove lock
5468: my @del_lock = ($file_name."\0".'locked_access_records');
5469: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
5470: my ($file,$group);
5471: if (&is_course($domain,$user)) {
5472: ($group,$file) = split(/\//,$file_name,2);
5473: } else {
5474: $file = $file_name;
5475: }
5476: my $sqlresult =
5477: &update_portfolio_table($user,$domain,$file,'portfolio_access',
5478: $group);
5479: } else {
5480: $outcome = "error: could not obtain lockfile\n";
5481: }
5482: return ($outcome,$deloutcome,\%new_values,\%translation);
5483: }
5484:
5485: sub make_public_indefinitely {
5486: my ($requrl) = @_;
5487: my $now = time;
5488: my $action = 'activate';
5489: my $aclnum = 0;
5490: if (&is_portfolio_url($requrl)) {
5491: my (undef,$udom,$unum,$file_name,$group) =
5492: &parse_portfolio_url($requrl);
5493: my $current_perms = &get_portfile_permissions($udom,$unum);
5494: my %access_controls = &get_access_controls($current_perms,
5495: $group,$file_name);
5496: foreach my $key (keys(%{$access_controls{$file_name}})) {
5497: my ($num,$scope,$end,$start) =
5498: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
5499: if ($scope eq 'public') {
5500: if ($start <= $now && $end == 0) {
5501: $action = 'none';
5502: } else {
5503: $action = 'update';
5504: $aclnum = $num;
5505: }
5506: last;
5507: }
5508: }
5509: if ($action eq 'none') {
5510: return 'ok';
5511: } else {
5512: my %changes;
5513: my $newend = 0;
5514: my $newstart = $now;
5515: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
5516: $changes{$action}{$newkey} = {
5517: type => 'public',
5518: time => {
5519: start => $newstart,
5520: end => $newend,
5521: },
5522: };
5523: my ($outcome,$deloutcome,$new_values,$translation) =
5524: &modify_access_controls($file_name,\%changes,$udom,$unum);
5525: return $outcome;
5526: }
5527: } else {
5528: return 'invalid';
5529: }
5530: }
5531:
5532: #------------------------------------------------------Get Marked as Read Only
5533:
5534: sub get_marked_as_readonly {
5535: my ($domain,$user,$what,$group) = @_;
5536: my $current_permissions = &get_portfile_permissions($domain,$user);
5537: my @readonly_files;
5538: my $cmp1=$what;
5539: if (ref($what)) { $cmp1=join('',@{$what}) };
5540: while (my ($file_name,$value) = each(%{$current_permissions})) {
5541: if (defined($group)) {
5542: if ($file_name !~ m-^\Q$group\E/-) {
5543: next;
5544: }
5545: }
5546: if (ref($value) eq "ARRAY"){
5547: foreach my $stored_what (@{$value}) {
5548: my $cmp2=$stored_what;
5549: if (ref($stored_what) eq 'ARRAY') {
5550: $cmp2=join('',@{$stored_what});
5551: }
5552: if ($cmp1 eq $cmp2) {
5553: push(@readonly_files, $file_name);
5554: last;
5555: } elsif (!defined($what)) {
5556: push(@readonly_files, $file_name);
5557: last;
5558: }
5559: }
5560: }
5561: }
5562: return @readonly_files;
5563: }
5564: #-----------------------------------------------------------Get Marked as Read Only Hash
5565:
5566: sub get_marked_as_readonly_hash {
5567: my ($current_permissions,$group,$what) = @_;
5568: my %readonly_files;
5569: while (my ($file_name,$value) = each(%{$current_permissions})) {
5570: if (defined($group)) {
5571: if ($file_name !~ m-^\Q$group\E/-) {
5572: next;
5573: }
5574: }
5575: if (ref($value) eq "ARRAY"){
5576: foreach my $stored_what (@{$value}) {
5577: if (ref($stored_what) eq 'ARRAY') {
5578: foreach my $lock_descriptor(@{$stored_what}) {
5579: if ($lock_descriptor eq 'graded') {
5580: $readonly_files{$file_name} = 'graded';
5581: } elsif ($lock_descriptor eq 'handback') {
5582: $readonly_files{$file_name} = 'handback';
5583: } else {
5584: if (!exists($readonly_files{$file_name})) {
5585: $readonly_files{$file_name} = 'locked';
5586: }
5587: }
5588: }
5589: }
5590: }
5591: }
5592: }
5593: return %readonly_files;
5594: }
5595: # ------------------------------------------------------------ Unmark as Read Only
5596:
5597: sub unmark_as_readonly {
5598: # unmarks $file_name (if $file_name is defined), or all files locked by $what
5599: # for portfolio submissions, $what contains [$symb,$crsid]
5600: my ($domain,$user,$what,$file_name,$group) = @_;
5601: $file_name = &declutter_portfile($file_name);
5602: my $symb_crs = $what;
5603: if (ref($what)) { $symb_crs=join('',@$what); }
5604: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
5605: my ($tmp)=keys(%current_permissions);
5606: if ($tmp=~/^error:/) { undef(%current_permissions); }
5607: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
5608: foreach my $file (@readonly_files) {
5609: my $clean_file = &declutter_portfile($file);
5610: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
5611: my $current_locks = $current_permissions{$file};
5612: my @new_locks;
5613: my @del_keys;
5614: if (ref($current_locks) eq "ARRAY"){
5615: foreach my $locker (@{$current_locks}) {
5616: my $compare=$locker;
5617: if (ref($locker) eq 'ARRAY') {
5618: $compare=join('',@{$locker});
5619: if ($compare ne $symb_crs) {
5620: push(@new_locks, $locker);
5621: }
5622: }
5623: }
5624: if (scalar(@new_locks) > 0) {
5625: $current_permissions{$file} = \@new_locks;
5626: } else {
5627: push(@del_keys, $file);
5628: &del('file_permissions',\@del_keys, $domain, $user);
5629: delete($current_permissions{$file});
5630: }
5631: }
5632: }
5633: &put('file_permissions',\%current_permissions,$domain,$user);
5634: return;
5635: }
5636:
5637: # ------------------------------------------------------------ Directory lister
5638:
5639: sub dirlist {
5640: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
5641:
5642: $uri=~s/^\///;
5643: $uri=~s/\/$//;
5644: my ($udom, $uname);
5645: (undef,$udom,$uname)=split(/\//,$uri);
5646: if(defined($userdomain)) {
5647: $udom = $userdomain;
5648: }
5649: if(defined($username)) {
5650: $uname = $username;
5651: }
5652:
5653: my $dirRoot = $perlvar{'lonDocRoot'};
5654: if(defined($alternateDirectoryRoot)) {
5655: $dirRoot = $alternateDirectoryRoot;
5656: $dirRoot =~ s/\/$//;
5657: }
5658:
5659: if($udom) {
5660: if($uname) {
5661: my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
5662: &homeserver($uname,$udom));
5663: my @listing_results;
5664: if ($listing eq 'unknown_cmd') {
5665: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
5666: &homeserver($uname,$udom));
5667: @listing_results = split(/:/,$listing);
5668: } else {
5669: @listing_results = map { &unescape($_); } split(/:/,$listing);
5670: }
5671: return @listing_results;
5672: } elsif(!defined($alternateDirectoryRoot)) {
5673: my %allusers;
5674: my %servers = &get_servers($udom,'library');
5675: foreach my $tryserver (keys(%servers)) {
5676: my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
5677: $udom, $tryserver);
5678: my @listing_results;
5679: if ($listing eq 'unknown_cmd') {
5680: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
5681: $udom, $tryserver);
5682: @listing_results = split(/:/,$listing);
5683: } else {
5684: @listing_results =
5685: map { &unescape($_); } split(/:/,$listing);
5686: }
5687: if ($listing_results[0] ne 'no_such_dir' &&
5688: $listing_results[0] ne 'empty' &&
5689: $listing_results[0] ne 'con_lost') {
5690: foreach my $line (@listing_results) {
5691: my ($entry) = split(/&/,$line,2);
5692: $allusers{$entry} = 1;
5693: }
5694: }
5695: }
5696: my $alluserstr='';
5697: foreach my $user (sort(keys(%allusers))) {
5698: $alluserstr.=$user.'&user:';
5699: }
5700: $alluserstr=~s/:$//;
5701: return split(/:/,$alluserstr);
5702: } else {
5703: return ('missing user name');
5704: }
5705: } elsif(!defined($alternateDirectoryRoot)) {
5706: my @all_domains = sort(&all_domains());
5707: foreach my $domain (@all_domains) {
5708: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
5709: }
5710: return @all_domains;
5711: } else {
5712: return ('missing domain');
5713: }
5714: }
5715:
5716: # --------------------------------------------- GetFileTimestamp
5717: # This function utilizes dirlist and returns the date stamp for
5718: # when it was last modified. It will also return an error of -1
5719: # if an error occurs
5720:
5721: ##
5722: ## FIXME: This subroutine assumes its caller knows something about the
5723: ## directory structure of the home server for the student ($root).
5724: ## Not a good assumption to make. Since this is for looking up files
5725: ## in user directories, the full path should be constructed by lond, not
5726: ## whatever machine we request data from.
5727: ##
5728: sub GetFileTimestamp {
5729: my ($studentDomain,$studentName,$filename,$root)=@_;
5730: $studentDomain = &LONCAPA::clean_domain($studentDomain);
5731: $studentName = &LONCAPA::clean_username($studentName);
5732: my $subdir=$studentName.'__';
5733: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
5734: my $proname="$studentDomain/$subdir/$studentName";
5735: $proname .= '/'.$filename;
5736: my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain,
5737: $studentName, $root);
5738: my @stats = split('&', $fileStat);
5739: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
5740: # @stats contains first the filename, then the stat output
5741: return $stats[10]; # so this is 10 instead of 9.
5742: } else {
5743: return -1;
5744: }
5745: }
5746:
5747: sub stat_file {
5748: my ($uri) = @_;
5749: $uri = &clutter_with_no_wrapper($uri);
5750:
5751: my ($udom,$uname,$file,$dir);
5752: if ($uri =~ m-^/(uploaded|editupload)/-) {
5753: ($udom,$uname,$file) =
5754: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
5755: $file = 'userfiles/'.$file;
5756: $dir = &propath($udom,$uname);
5757: }
5758: if ($uri =~ m-^/res/-) {
5759: ($udom,$uname) =
5760: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
5761: $file = $uri;
5762: }
5763:
5764: if (!$udom || !$uname || !$file) {
5765: # unable to handle the uri
5766: return ();
5767: }
5768:
5769: my ($result) = &dirlist($file,$udom,$uname,$dir);
5770: my @stats = split('&', $result);
5771:
5772: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
5773: shift(@stats); #filename is first
5774: return @stats;
5775: }
5776: return ();
5777: }
5778:
5779: # -------------------------------------------------------- Value of a Condition
5780:
5781: # gets the value of a specific preevaluated condition
5782: # stored in the string $env{user.state.<cid>}
5783: # or looks up a condition reference in the bighash and if if hasn't
5784: # already been evaluated recurses into docondval to get the value of
5785: # the condition, then memoizing it to
5786: # $env{user.state.<cid>.<condition>}
5787: sub directcondval {
5788: my $number=shift;
5789: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
5790: &Apache::lonuserstate::evalstate();
5791: }
5792: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
5793: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
5794: } elsif ($number =~ /^_/) {
5795: my $sub_condition;
5796: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
5797: &GDBM_READER(),0640)) {
5798: $sub_condition=$bighash{'conditions'.$number};
5799: untie(%bighash);
5800: }
5801: my $value = &docondval($sub_condition);
5802: &appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
5803: return $value;
5804: }
5805: if ($env{'user.state.'.$env{'request.course.id'}}) {
5806: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
5807: } else {
5808: return 2;
5809: }
5810: }
5811:
5812: # get the collection of conditions for this resource
5813: sub condval {
5814: my $condidx=shift;
5815: my $allpathcond='';
5816: foreach my $cond (split(/\|/,$condidx)) {
5817: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
5818: $allpathcond.=
5819: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
5820: }
5821: }
5822: $allpathcond=~s/\|$//;
5823: return &docondval($allpathcond);
5824: }
5825:
5826: #evaluates an expression of conditions
5827: sub docondval {
5828: my ($allpathcond) = @_;
5829: my $result=0;
5830: if ($env{'request.course.id'}
5831: && defined($allpathcond)) {
5832: my $operand='|';
5833: my @stack;
5834: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
5835: if ($chunk eq '(') {
5836: push @stack,($operand,$result);
5837: } elsif ($chunk eq ')') {
5838: my $before=pop @stack;
5839: if (pop @stack eq '&') {
5840: $result=$result>$before?$before:$result;
5841: } else {
5842: $result=$result>$before?$result:$before;
5843: }
5844: } elsif (($chunk eq '&') || ($chunk eq '|')) {
5845: $operand=$chunk;
5846: } else {
5847: my $new=directcondval($chunk);
5848: if ($operand eq '&') {
5849: $result=$result>$new?$new:$result;
5850: } else {
5851: $result=$result>$new?$result:$new;
5852: }
5853: }
5854: }
5855: }
5856: return $result;
5857: }
5858:
5859: # ---------------------------------------------------- Devalidate courseresdata
5860:
5861: sub devalidatecourseresdata {
5862: my ($coursenum,$coursedomain)=@_;
5863: my $hashid=$coursenum.':'.$coursedomain;
5864: &devalidate_cache_new('courseres',$hashid);
5865: }
5866:
5867:
5868: # --------------------------------------------------- Course Resourcedata Query
5869:
5870: sub get_courseresdata {
5871: my ($coursenum,$coursedomain)=@_;
5872: my $coursehom=&homeserver($coursenum,$coursedomain);
5873: my $hashid=$coursenum.':'.$coursedomain;
5874: my ($result,$cached)=&is_cached_new('courseres',$hashid);
5875: my %dumpreply;
5876: unless (defined($cached)) {
5877: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
5878: $result=\%dumpreply;
5879: my ($tmp) = keys(%dumpreply);
5880: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
5881: &do_cache_new('courseres',$hashid,$result,600);
5882: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
5883: return $tmp;
5884: } elsif ($tmp =~ /^(error)/) {
5885: $result=undef;
5886: &do_cache_new('courseres',$hashid,$result,600);
5887: }
5888: }
5889: return $result;
5890: }
5891:
5892: sub devalidateuserresdata {
5893: my ($uname,$udom)=@_;
5894: my $hashid="$udom:$uname";
5895: &devalidate_cache_new('userres',$hashid);
5896: }
5897:
5898: sub get_userresdata {
5899: my ($uname,$udom)=@_;
5900: #most student don\'t have any data set, check if there is some data
5901: if (&EXT_cache_status($udom,$uname)) { return undef; }
5902:
5903: my $hashid="$udom:$uname";
5904: my ($result,$cached)=&is_cached_new('userres',$hashid);
5905: if (!defined($cached)) {
5906: my %resourcedata=&dump('resourcedata',$udom,$uname);
5907: $result=\%resourcedata;
5908: &do_cache_new('userres',$hashid,$result,600);
5909: }
5910: my ($tmp)=keys(%$result);
5911: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
5912: return $result;
5913: }
5914: #error 2 occurs when the .db doesn't exist
5915: if ($tmp!~/error: 2 /) {
5916: &logthis("<font color=\"blue\">WARNING:".
5917: " Trying to get resource data for ".
5918: $uname." at ".$udom.": ".
5919: $tmp."</font>");
5920: } elsif ($tmp=~/error: 2 /) {
5921: #&EXT_cache_set($udom,$uname);
5922: &do_cache_new('userres',$hashid,undef,600);
5923: undef($tmp); # not really an error so don't send it back
5924: }
5925: return $tmp;
5926: }
5927:
5928: sub resdata {
5929: my ($name,$domain,$type,@which)=@_;
5930: my $result;
5931: if ($type eq 'course') {
5932: $result=&get_courseresdata($name,$domain);
5933: } elsif ($type eq 'user') {
5934: $result=&get_userresdata($name,$domain);
5935: }
5936: if (!ref($result)) { return $result; }
5937: foreach my $item (@which) {
5938: if (defined($result->{$item})) {
5939: return $result->{$item};
5940: }
5941: }
5942: return undef;
5943: }
5944:
5945: #
5946: # EXT resource caching routines
5947: #
5948:
5949: sub clear_EXT_cache_status {
5950: &delenv('cache.EXT.');
5951: }
5952:
5953: sub EXT_cache_status {
5954: my ($target_domain,$target_user) = @_;
5955: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
5956: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
5957: # We know already the user has no data
5958: return 1;
5959: } else {
5960: return 0;
5961: }
5962: }
5963:
5964: sub EXT_cache_set {
5965: my ($target_domain,$target_user) = @_;
5966: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
5967: #&appenv($cachename => time);
5968: }
5969:
5970: # --------------------------------------------------------- Value of a Variable
5971: sub EXT {
5972:
5973: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
5974: unless ($varname) { return ''; }
5975: #get real user name/domain, courseid and symb
5976: my $courseid;
5977: my $publicuser;
5978: if ($symbparm) {
5979: $symbparm=&get_symb_from_alias($symbparm);
5980: }
5981: if (!($uname && $udom)) {
5982: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
5983: if (!$symbparm) { $symbparm=$cursymb; }
5984: } else {
5985: $courseid=$env{'request.course.id'};
5986: }
5987: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
5988: my $rest;
5989: if (defined($therest[0])) {
5990: $rest=join('.',@therest);
5991: } else {
5992: $rest='';
5993: }
5994:
5995: my $qualifierrest=$qualifier;
5996: if ($rest) { $qualifierrest.='.'.$rest; }
5997: my $spacequalifierrest=$space;
5998: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
5999: if ($realm eq 'user') {
6000: # --------------------------------------------------------------- user.resource
6001: if ($space eq 'resource') {
6002: if ( (defined($Apache::lonhomework::parsing_a_problem)
6003: || defined($Apache::lonhomework::parsing_a_task))
6004: &&
6005: ($symbparm eq &symbread()) ) {
6006: # if we are in the middle of processing the resource the
6007: # get the value we are planning on committing
6008: if (defined($Apache::lonhomework::results{$qualifierrest})) {
6009: return $Apache::lonhomework::results{$qualifierrest};
6010: } else {
6011: return $Apache::lonhomework::history{$qualifierrest};
6012: }
6013: } else {
6014: my %restored;
6015: if ($publicuser || $env{'request.state'} eq 'construct') {
6016: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
6017: } else {
6018: %restored=&restore($symbparm,$courseid,$udom,$uname);
6019: }
6020: return $restored{$qualifierrest};
6021: }
6022: # ----------------------------------------------------------------- user.access
6023: } elsif ($space eq 'access') {
6024: # FIXME - not supporting calls for a specific user
6025: return &allowed($qualifier,$rest);
6026: # ------------------------------------------ user.preferences, user.environment
6027: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
6028: if (($uname eq $env{'user.name'}) &&
6029: ($udom eq $env{'user.domain'})) {
6030: return $env{join('.',('environment',$qualifierrest))};
6031: } else {
6032: my %returnhash;
6033: if (!$publicuser) {
6034: %returnhash=&userenvironment($udom,$uname,
6035: $qualifierrest);
6036: }
6037: return $returnhash{$qualifierrest};
6038: }
6039: # ----------------------------------------------------------------- user.course
6040: } elsif ($space eq 'course') {
6041: # FIXME - not supporting calls for a specific user
6042: return $env{join('.',('request.course',$qualifier))};
6043: # ------------------------------------------------------------------- user.role
6044: } elsif ($space eq 'role') {
6045: # FIXME - not supporting calls for a specific user
6046: my ($role,$where)=split(/\./,$env{'request.role'});
6047: if ($qualifier eq 'value') {
6048: return $role;
6049: } elsif ($qualifier eq 'extent') {
6050: return $where;
6051: }
6052: # ----------------------------------------------------------------- user.domain
6053: } elsif ($space eq 'domain') {
6054: return $udom;
6055: # ------------------------------------------------------------------- user.name
6056: } elsif ($space eq 'name') {
6057: return $uname;
6058: # ---------------------------------------------------- Any other user namespace
6059: } else {
6060: my %reply;
6061: if (!$publicuser) {
6062: %reply=&get($space,[$qualifierrest],$udom,$uname);
6063: }
6064: return $reply{$qualifierrest};
6065: }
6066: } elsif ($realm eq 'query') {
6067: # ---------------------------------------------- pull stuff out of query string
6068: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
6069: [$spacequalifierrest]);
6070: return $env{'form.'.$spacequalifierrest};
6071: } elsif ($realm eq 'request') {
6072: # ------------------------------------------------------------- request.browser
6073: if ($space eq 'browser') {
6074: if ($qualifier eq 'textremote') {
6075: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
6076: return 1;
6077: } else {
6078: return 0;
6079: }
6080: } else {
6081: return $env{'browser.'.$qualifier};
6082: }
6083: # ------------------------------------------------------------ request.filename
6084: } else {
6085: return $env{'request.'.$spacequalifierrest};
6086: }
6087: } elsif ($realm eq 'course') {
6088: # ---------------------------------------------------------- course.description
6089: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
6090: } elsif ($realm eq 'resource') {
6091:
6092: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
6093: if (!$symbparm) { $symbparm=&symbread(); }
6094: }
6095:
6096: if ($space eq 'title') {
6097: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
6098: return &gettitle($symbparm);
6099: }
6100:
6101: if ($space eq 'map') {
6102: my ($map) = &decode_symb($symbparm);
6103: return &symbread($map);
6104: }
6105:
6106: my ($section, $group, @groups);
6107: my ($courselevelm,$courselevel);
6108: if ($symbparm && defined($courseid) &&
6109: $courseid eq $env{'request.course.id'}) {
6110:
6111: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
6112:
6113: # ----------------------------------------------------- Cascading lookup scheme
6114: my $symbp=$symbparm;
6115: my $mapp=&deversion((&decode_symb($symbp))[0]);
6116:
6117: my $symbparm=$symbp.'.'.$spacequalifierrest;
6118: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
6119:
6120: if (($env{'user.name'} eq $uname) &&
6121: ($env{'user.domain'} eq $udom)) {
6122: $section=$env{'request.course.sec'};
6123: @groups = split(/:/,$env{'request.course.groups'});
6124: @groups=&sort_course_groups($courseid,@groups);
6125: } else {
6126: if (! defined($usection)) {
6127: $section=&getsection($udom,$uname,$courseid);
6128: } else {
6129: $section = $usection;
6130: }
6131: @groups = &get_users_groups($udom,$uname,$courseid);
6132: }
6133:
6134: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
6135: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
6136: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
6137:
6138: $courselevel=$courseid.'.'.$spacequalifierrest;
6139: my $courselevelr=$courseid.'.'.$symbparm;
6140: $courselevelm=$courseid.'.'.$mapparm;
6141:
6142: # ----------------------------------------------------------- first, check user
6143:
6144: my $userreply=&resdata($uname,$udom,'user',
6145: ($courselevelr,$courselevelm,
6146: $courselevel));
6147: if (defined($userreply)) { return $userreply; }
6148:
6149: # ------------------------------------------------ second, check some of course
6150: my $coursereply;
6151: if (@groups > 0) {
6152: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
6153: $mapparm,$spacequalifierrest);
6154: if (defined($coursereply)) { return $coursereply; }
6155: }
6156:
6157: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
6158: $env{'course.'.$courseid.'.domain'},
6159: 'course',
6160: ($seclevelr,$seclevelm,$seclevel,
6161: $courselevelr));
6162: if (defined($coursereply)) { return $coursereply; }
6163:
6164: # ------------------------------------------------------ third, check map parms
6165: my %parmhash=();
6166: my $thisparm='';
6167: if (tie(%parmhash,'GDBM_File',
6168: $env{'request.course.fn'}.'_parms.db',
6169: &GDBM_READER(),0640)) {
6170: $thisparm=$parmhash{$symbparm};
6171: untie(%parmhash);
6172: }
6173: if ($thisparm) { return $thisparm; }
6174: }
6175: # ------------------------------------------ fourth, look in resource metadata
6176:
6177: $spacequalifierrest=~s/\./\_/;
6178: my $filename;
6179: if (!$symbparm) { $symbparm=&symbread(); }
6180: if ($symbparm) {
6181: $filename=(&decode_symb($symbparm))[2];
6182: } else {
6183: $filename=$env{'request.filename'};
6184: }
6185: my $metadata=&metadata($filename,$spacequalifierrest);
6186: if (defined($metadata)) { return $metadata; }
6187: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
6188: if (defined($metadata)) { return $metadata; }
6189:
6190: # ---------------------------------------------- fourth, look in rest pf course
6191: if ($symbparm && defined($courseid) &&
6192: $courseid eq $env{'request.course.id'}) {
6193: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
6194: $env{'course.'.$courseid.'.domain'},
6195: 'course',
6196: ($courselevelm,$courselevel));
6197: if (defined($coursereply)) { return $coursereply; }
6198: }
6199: # ------------------------------------------------------------------ Cascade up
6200: unless ($space eq '0') {
6201: my @parts=split(/_/,$space);
6202: my $id=pop(@parts);
6203: my $part=join('_',@parts);
6204: if ($part eq '') { $part='0'; }
6205: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
6206: $symbparm,$udom,$uname,$section,1);
6207: if (defined($partgeneral)) { return $partgeneral; }
6208: }
6209: if ($recurse) { return undef; }
6210: my $pack_def=&packages_tab_default($filename,$varname);
6211: if (defined($pack_def)) { return $pack_def; }
6212:
6213: # ---------------------------------------------------- Any other user namespace
6214: } elsif ($realm eq 'environment') {
6215: # ----------------------------------------------------------------- environment
6216: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
6217: return $env{'environment.'.$spacequalifierrest};
6218: } else {
6219: if ($uname eq 'anonymous' && $udom eq '') {
6220: return '';
6221: }
6222: my %returnhash=&userenvironment($udom,$uname,
6223: $spacequalifierrest);
6224: return $returnhash{$spacequalifierrest};
6225: }
6226: } elsif ($realm eq 'system') {
6227: # ----------------------------------------------------------------- system.time
6228: if ($space eq 'time') {
6229: return time;
6230: }
6231: } elsif ($realm eq 'server') {
6232: # ----------------------------------------------------------------- system.time
6233: if ($space eq 'name') {
6234: return $ENV{'SERVER_NAME'};
6235: }
6236: }
6237: return '';
6238: }
6239:
6240: sub check_group_parms {
6241: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
6242: my @groupitems = ();
6243: my $resultitem;
6244: my @levels = ($symbparm,$mapparm,$what);
6245: foreach my $group (@{$groups}) {
6246: foreach my $level (@levels) {
6247: my $item = $courseid.'.['.$group.'].'.$level;
6248: push(@groupitems,$item);
6249: }
6250: }
6251: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
6252: $env{'course.'.$courseid.'.domain'},
6253: 'course',@groupitems);
6254: return $coursereply;
6255: }
6256:
6257: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
6258: my ($courseid,@groups) = @_;
6259: @groups = sort(@groups);
6260: return @groups;
6261: }
6262:
6263: sub packages_tab_default {
6264: my ($uri,$varname)=@_;
6265: my (undef,$part,$name)=split(/\./,$varname);
6266:
6267: my (@extension,@specifics,$do_default);
6268: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
6269: my ($pack_type,$pack_part)=split(/_/,$package,2);
6270: if ($pack_type eq 'default') {
6271: $do_default=1;
6272: } elsif ($pack_type eq 'extension') {
6273: push(@extension,[$package,$pack_type,$pack_part]);
6274: } elsif ($pack_part eq $part) {
6275: # only look at packages defaults for packages that this id is
6276: push(@specifics,[$package,$pack_type,$pack_part]);
6277: }
6278: }
6279: # first look for a package that matches the requested part id
6280: foreach my $package (@specifics) {
6281: my (undef,$pack_type,$pack_part)=@{$package};
6282: next if ($pack_part ne $part);
6283: if (defined($packagetab{"$pack_type&$name&default"})) {
6284: return $packagetab{"$pack_type&$name&default"};
6285: }
6286: }
6287: # look for any possible matching non extension_ package
6288: foreach my $package (@specifics) {
6289: my (undef,$pack_type,$pack_part)=@{$package};
6290: if (defined($packagetab{"$pack_type&$name&default"})) {
6291: return $packagetab{"$pack_type&$name&default"};
6292: }
6293: if ($pack_type eq 'part') { $pack_part='0'; }
6294: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
6295: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
6296: }
6297: }
6298: # look for any posible extension_ match
6299: foreach my $package (@extension) {
6300: my ($package,$pack_type)=@{$package};
6301: if (defined($packagetab{"$pack_type&$name&default"})) {
6302: return $packagetab{"$pack_type&$name&default"};
6303: }
6304: if (defined($packagetab{$package."&$name&default"})) {
6305: return $packagetab{$package."&$name&default"};
6306: }
6307: }
6308: # look for a global default setting
6309: if ($do_default && defined($packagetab{"default&$name&default"})) {
6310: return $packagetab{"default&$name&default"};
6311: }
6312: return undef;
6313: }
6314:
6315: sub add_prefix_and_part {
6316: my ($prefix,$part)=@_;
6317: my $keyroot;
6318: if (defined($prefix) && $prefix !~ /^__/) {
6319: # prefix that has a part already
6320: $keyroot=$prefix;
6321: } elsif (defined($prefix)) {
6322: # prefix that is missing a part
6323: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
6324: } else {
6325: # no prefix at all
6326: if (defined($part)) { $keyroot='_'.$part; }
6327: }
6328: return $keyroot;
6329: }
6330:
6331: # ---------------------------------------------------------------- Get metadata
6332:
6333: my %metaentry;
6334: sub metadata {
6335: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
6336: $uri=&declutter($uri);
6337: # if it is a non metadata possible uri return quickly
6338: if (($uri eq '') ||
6339: (($uri =~ m|^/*adm/|) &&
6340: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
6341: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
6342: ($uri =~ m|home/$match_username/public_html/|)) {
6343: return undef;
6344: }
6345: my $filename=$uri;
6346: $uri=~s/\.meta$//;
6347: #
6348: # Is the metadata already cached?
6349: # Look at timestamp of caching
6350: # Everything is cached by the main uri, libraries are never directly cached
6351: #
6352: if (!defined($liburi)) {
6353: my ($result,$cached)=&is_cached_new('meta',$uri);
6354: if (defined($cached)) { return $result->{':'.$what}; }
6355: }
6356: {
6357: #
6358: # Is this a recursive call for a library?
6359: #
6360: # if (! exists($metacache{$uri})) {
6361: # $metacache{$uri}={};
6362: # }
6363: if ($liburi) {
6364: $liburi=&declutter($liburi);
6365: $filename=$liburi;
6366: } else {
6367: &devalidate_cache_new('meta',$uri);
6368: undef(%metaentry);
6369: }
6370: my %metathesekeys=();
6371: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
6372: my $metastring;
6373: if ($uri !~ m -^(editupload)/-) {
6374: my $file=&filelocation('',&clutter($filename));
6375: #push(@{$metaentry{$uri.'.file'}},$file);
6376: $metastring=&getfile($file);
6377: }
6378: my $parser=HTML::LCParser->new(\$metastring);
6379: my $token;
6380: undef %metathesekeys;
6381: while ($token=$parser->get_token) {
6382: if ($token->[0] eq 'S') {
6383: if (defined($token->[2]->{'package'})) {
6384: #
6385: # This is a package - get package info
6386: #
6387: my $package=$token->[2]->{'package'};
6388: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
6389: if (defined($token->[2]->{'id'})) {
6390: $keyroot.='_'.$token->[2]->{'id'};
6391: }
6392: if ($metaentry{':packages'}) {
6393: $metaentry{':packages'}.=','.$package.$keyroot;
6394: } else {
6395: $metaentry{':packages'}=$package.$keyroot;
6396: }
6397: foreach my $pack_entry (keys(%packagetab)) {
6398: my $part=$keyroot;
6399: $part=~s/^\_//;
6400: if ($pack_entry=~/^\Q$package\E\&/ ||
6401: $pack_entry=~/^\Q$package\E_0\&/) {
6402: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
6403: # ignore package.tab specified default values
6404: # here &package_tab_default() will fetch those
6405: if ($subp eq 'default') { next; }
6406: my $value=$packagetab{$pack_entry};
6407: my $unikey;
6408: if ($pack =~ /_0$/) {
6409: $unikey='parameter_0_'.$name;
6410: $part=0;
6411: } else {
6412: $unikey='parameter'.$keyroot.'_'.$name;
6413: }
6414: if ($subp eq 'display') {
6415: $value.=' [Part: '.$part.']';
6416: }
6417: $metaentry{':'.$unikey.'.part'}=$part;
6418: $metathesekeys{$unikey}=1;
6419: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
6420: $metaentry{':'.$unikey.'.'.$subp}=$value;
6421: }
6422: if (defined($metaentry{':'.$unikey.'.default'})) {
6423: $metaentry{':'.$unikey}=
6424: $metaentry{':'.$unikey.'.default'};
6425: }
6426: }
6427: }
6428: } else {
6429: #
6430: # This is not a package - some other kind of start tag
6431: #
6432: my $entry=$token->[1];
6433: my $unikey;
6434: if ($entry eq 'import') {
6435: $unikey='';
6436: } else {
6437: $unikey=$entry;
6438: }
6439: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
6440:
6441: if (defined($token->[2]->{'id'})) {
6442: $unikey.='_'.$token->[2]->{'id'};
6443: }
6444:
6445: if ($entry eq 'import') {
6446: #
6447: # Importing a library here
6448: #
6449: if ($depthcount<20) {
6450: my $location=$parser->get_text('/import');
6451: my $dir=$filename;
6452: $dir=~s|[^/]*$||;
6453: $location=&filelocation($dir,$location);
6454: my $metadata =
6455: &metadata($uri,'keys', $location,$unikey,
6456: $depthcount+1);
6457: foreach my $meta (split(',',$metadata)) {
6458: $metaentry{':'.$meta}=$metaentry{':'.$meta};
6459: $metathesekeys{$meta}=1;
6460: }
6461: }
6462: } else {
6463:
6464: if (defined($token->[2]->{'name'})) {
6465: $unikey.='_'.$token->[2]->{'name'};
6466: }
6467: $metathesekeys{$unikey}=1;
6468: foreach my $param (@{$token->[3]}) {
6469: $metaentry{':'.$unikey.'.'.$param} =
6470: $token->[2]->{$param};
6471: }
6472: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
6473: my $default=$metaentry{':'.$unikey.'.default'};
6474: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
6475: # only ws inside the tag, and not in default, so use default
6476: # as value
6477: $metaentry{':'.$unikey}=$default;
6478: } else {
6479: # either something interesting inside the tag or default
6480: # uninteresting
6481: $metaentry{':'.$unikey}=$internaltext;
6482: }
6483: # end of not-a-package not-a-library import
6484: }
6485: # end of not-a-package start tag
6486: }
6487: # the next is the end of "start tag"
6488: }
6489: }
6490: my ($extension) = ($uri =~ /\.(\w+)$/);
6491: foreach my $key (keys(%packagetab)) {
6492: #no specific packages #how's our extension
6493: if ($key!~/^extension_\Q$extension\E&/) { next; }
6494: &metadata_create_package_def($uri,$key,'extension_'.$extension,
6495: \%metathesekeys);
6496: }
6497: if (!exists($metaentry{':packages'})) {
6498: foreach my $key (keys(%packagetab)) {
6499: #no specific packages well let's get default then
6500: if ($key!~/^default&/) { next; }
6501: &metadata_create_package_def($uri,$key,'default',
6502: \%metathesekeys);
6503: }
6504: }
6505: # are there custom rights to evaluate
6506: if ($metaentry{':copyright'} eq 'custom') {
6507:
6508: #
6509: # Importing a rights file here
6510: #
6511: unless ($depthcount) {
6512: my $location=$metaentry{':customdistributionfile'};
6513: my $dir=$filename;
6514: $dir=~s|[^/]*$||;
6515: $location=&filelocation($dir,$location);
6516: my $rights_metadata =
6517: &metadata($uri,'keys',$location,'_rights',
6518: $depthcount+1);
6519: foreach my $rights (split(',',$rights_metadata)) {
6520: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
6521: $metathesekeys{$rights}=1;
6522: }
6523: }
6524: }
6525: # uniqifiy package listing
6526: my %seen;
6527: my @uniq_packages =
6528: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
6529: $metaentry{':packages'} = join(',',@uniq_packages);
6530:
6531: $metaentry{':keys'} = join(',',keys(%metathesekeys));
6532: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
6533: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
6534: &do_cache_new('meta',$uri,\%metaentry,60*60);
6535: # this is the end of "was not already recently cached
6536: }
6537: return $metaentry{':'.$what};
6538: }
6539:
6540: sub metadata_create_package_def {
6541: my ($uri,$key,$package,$metathesekeys)=@_;
6542: my ($pack,$name,$subp)=split(/\&/,$key);
6543: if ($subp eq 'default') { next; }
6544:
6545: if (defined($metaentry{':packages'})) {
6546: $metaentry{':packages'}.=','.$package;
6547: } else {
6548: $metaentry{':packages'}=$package;
6549: }
6550: my $value=$packagetab{$key};
6551: my $unikey;
6552: $unikey='parameter_0_'.$name;
6553: $metaentry{':'.$unikey.'.part'}=0;
6554: $$metathesekeys{$unikey}=1;
6555: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
6556: $metaentry{':'.$unikey.'.'.$subp}=$value;
6557: }
6558: if (defined($metaentry{':'.$unikey.'.default'})) {
6559: $metaentry{':'.$unikey}=
6560: $metaentry{':'.$unikey.'.default'};
6561: }
6562: }
6563:
6564: sub metadata_generate_part0 {
6565: my ($metadata,$metacache,$uri) = @_;
6566: my %allnames;
6567: foreach my $metakey (keys(%$metadata)) {
6568: if ($metakey=~/^parameter\_(.*)/) {
6569: my $part=$$metacache{':'.$metakey.'.part'};
6570: my $name=$$metacache{':'.$metakey.'.name'};
6571: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
6572: $allnames{$name}=$part;
6573: }
6574: }
6575: }
6576: foreach my $name (keys(%allnames)) {
6577: $$metadata{"parameter_0_$name"}=1;
6578: my $key=":parameter_0_$name";
6579: $$metacache{"$key.part"}='0';
6580: $$metacache{"$key.name"}=$name;
6581: $$metacache{"$key.type"}=$$metacache{':parameter_'.
6582: $allnames{$name}.'_'.$name.
6583: '.type'};
6584: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
6585: '.display'};
6586: my $expr='[Part: '.$allnames{$name}.']';
6587: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
6588: $$metacache{"$key.display"}=$olddis;
6589: }
6590: }
6591:
6592: # ------------------------------------------------------ Devalidate title cache
6593:
6594: sub devalidate_title_cache {
6595: my ($url)=@_;
6596: if (!$env{'request.course.id'}) { return; }
6597: my $symb=&symbread($url);
6598: if (!$symb) { return; }
6599: my $key=$env{'request.course.id'}."\0".$symb;
6600: &devalidate_cache_new('title',$key);
6601: }
6602:
6603: # ------------------------------------------------- Get the title of a resource
6604:
6605: sub gettitle {
6606: my $urlsymb=shift;
6607: my $symb=&symbread($urlsymb);
6608: if ($symb) {
6609: my $key=$env{'request.course.id'}."\0".$symb;
6610: my ($result,$cached)=&is_cached_new('title',$key);
6611: if (defined($cached)) {
6612: return $result;
6613: }
6614: my ($map,$resid,$url)=&decode_symb($symb);
6615: my $title='';
6616: my %bighash;
6617: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
6618: &GDBM_READER(),0640)) {
6619: my $mapid=$bighash{'map_pc_'.&clutter($map)};
6620: $title=$bighash{'title_'.$mapid.'.'.$resid};
6621: untie %bighash;
6622: }
6623: $title=~s/\&colon\;/\:/gs;
6624: if ($title) {
6625: return &do_cache_new('title',$key,$title,600);
6626: }
6627: $urlsymb=$url;
6628: }
6629: my $title=&metadata($urlsymb,'title');
6630: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
6631: return $title;
6632: }
6633:
6634: sub get_slot {
6635: my ($which,$cnum,$cdom)=@_;
6636: if (!$cnum || !$cdom) {
6637: (undef,my $courseid)=&whichuser();
6638: $cdom=$env{'course.'.$courseid.'.domain'};
6639: $cnum=$env{'course.'.$courseid.'.num'};
6640: }
6641: my $key=join("\0",'slots',$cdom,$cnum,$which);
6642: my %slotinfo;
6643: if (exists($remembered{$key})) {
6644: $slotinfo{$which} = $remembered{$key};
6645: } else {
6646: %slotinfo=&get('slots',[$which],$cdom,$cnum);
6647: &Apache::lonhomework::showhash(%slotinfo);
6648: my ($tmp)=keys(%slotinfo);
6649: if ($tmp=~/^error:/) { return (); }
6650: $remembered{$key} = $slotinfo{$which};
6651: }
6652: if (ref($slotinfo{$which}) eq 'HASH') {
6653: return %{$slotinfo{$which}};
6654: }
6655: return $slotinfo{$which};
6656: }
6657: # ------------------------------------------------- Update symbolic store links
6658:
6659: sub symblist {
6660: my ($mapname,%newhash)=@_;
6661: $mapname=&deversion(&declutter($mapname));
6662: my %hash;
6663: if (($env{'request.course.fn'}) && (%newhash)) {
6664: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
6665: &GDBM_WRCREAT(),0640)) {
6666: foreach my $url (keys %newhash) {
6667: next if ($url eq 'last_known'
6668: && $env{'form.no_update_last_known'});
6669: $hash{declutter($url)}=&encode_symb($mapname,
6670: $newhash{$url}->[1],
6671: $newhash{$url}->[0]);
6672: }
6673: if (untie(%hash)) {
6674: return 'ok';
6675: }
6676: }
6677: }
6678: return 'error';
6679: }
6680:
6681: # --------------------------------------------------------------- Verify a symb
6682:
6683: sub symbverify {
6684: my ($symb,$thisurl)=@_;
6685: my $thisfn=$thisurl;
6686: $thisfn=&declutter($thisfn);
6687: # direct jump to resource in page or to a sequence - will construct own symbs
6688: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
6689: # check URL part
6690: my ($map,$resid,$url)=&decode_symb($symb);
6691:
6692: unless ($url eq $thisfn) { return 0; }
6693:
6694: $symb=&symbclean($symb);
6695: $thisurl=&deversion($thisurl);
6696: $thisfn=&deversion($thisfn);
6697:
6698: my %bighash;
6699: my $okay=0;
6700:
6701: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
6702: &GDBM_READER(),0640)) {
6703: my $ids=$bighash{'ids_'.&clutter($thisurl)};
6704: unless ($ids) {
6705: $ids=$bighash{'ids_/'.$thisurl};
6706: }
6707: if ($ids) {
6708: # ------------------------------------------------------------------- Has ID(s)
6709: foreach my $id (split(/\,/,$ids)) {
6710: my ($mapid,$resid)=split(/\./,$id);
6711: if (
6712: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
6713: eq $symb) {
6714: if (($env{'request.role.adv'}) ||
6715: $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
6716: $okay=1;
6717: }
6718: }
6719: }
6720: }
6721: untie(%bighash);
6722: }
6723: return $okay;
6724: }
6725:
6726: # --------------------------------------------------------------- Clean-up symb
6727:
6728: sub symbclean {
6729: my $symb=shift;
6730: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
6731: # remove version from map
6732: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
6733:
6734: # remove version from URL
6735: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
6736:
6737: # remove wrapper
6738:
6739: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
6740: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
6741: return $symb;
6742: }
6743:
6744: # ---------------------------------------------- Split symb to find map and url
6745:
6746: sub encode_symb {
6747: my ($map,$resid,$url)=@_;
6748: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
6749: }
6750:
6751: sub decode_symb {
6752: my $symb=shift;
6753: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
6754: my ($map,$resid,$url)=split(/___/,$symb);
6755: return (&fixversion($map),$resid,&fixversion($url));
6756: }
6757:
6758: sub fixversion {
6759: my $fn=shift;
6760: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
6761: my %bighash;
6762: my $uri=&clutter($fn);
6763: my $key=$env{'request.course.id'}.'_'.$uri;
6764: # is this cached?
6765: my ($result,$cached)=&is_cached_new('courseresversion',$key);
6766: if (defined($cached)) { return $result; }
6767: # unfortunately not cached, or expired
6768: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
6769: &GDBM_READER(),0640)) {
6770: if ($bighash{'version_'.$uri}) {
6771: my $version=$bighash{'version_'.$uri};
6772: unless (($version eq 'mostrecent') ||
6773: ($version==&getversion($uri))) {
6774: $uri=~s/\.(\w+)$/\.$version\.$1/;
6775: }
6776: }
6777: untie %bighash;
6778: }
6779: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
6780: }
6781:
6782: sub deversion {
6783: my $url=shift;
6784: $url=~s/\.\d+\.(\w+)$/\.$1/;
6785: return $url;
6786: }
6787:
6788: # ------------------------------------------------------ Return symb list entry
6789:
6790: sub symbread {
6791: my ($thisfn,$donotrecurse)=@_;
6792: my $cache_str='request.symbread.cached.'.$thisfn;
6793: if (defined($env{$cache_str})) { return $env{$cache_str}; }
6794: # no filename provided? try from environment
6795: unless ($thisfn) {
6796: if ($env{'request.symb'}) {
6797: return $env{$cache_str}=&symbclean($env{'request.symb'});
6798: }
6799: $thisfn=$env{'request.filename'};
6800: }
6801: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
6802: # is that filename actually a symb? Verify, clean, and return
6803: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
6804: if (&symbverify($thisfn,$1)) {
6805: return $env{$cache_str}=&symbclean($thisfn);
6806: }
6807: }
6808: $thisfn=declutter($thisfn);
6809: my %hash;
6810: my %bighash;
6811: my $syval='';
6812: if (($env{'request.course.fn'}) && ($thisfn)) {
6813: my $targetfn = $thisfn;
6814: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
6815: $targetfn = 'adm/wrapper/'.$thisfn;
6816: }
6817: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
6818: $targetfn=$1;
6819: }
6820: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
6821: &GDBM_READER(),0640)) {
6822: $syval=$hash{$targetfn};
6823: untie(%hash);
6824: }
6825: # ---------------------------------------------------------- There was an entry
6826: if ($syval) {
6827: #unless ($syval=~/\_\d+$/) {
6828: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
6829: #&appenv('request.ambiguous' => $thisfn);
6830: #return $env{$cache_str}='';
6831: #}
6832: #$syval.=$1;
6833: #}
6834: } else {
6835: # ------------------------------------------------------- Was not in symb table
6836: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
6837: &GDBM_READER(),0640)) {
6838: # ---------------------------------------------- Get ID(s) for current resource
6839: my $ids=$bighash{'ids_'.&clutter($thisfn)};
6840: unless ($ids) {
6841: $ids=$bighash{'ids_/'.$thisfn};
6842: }
6843: unless ($ids) {
6844: # alias?
6845: $ids=$bighash{'mapalias_'.$thisfn};
6846: }
6847: if ($ids) {
6848: # ------------------------------------------------------------------- Has ID(s)
6849: my @possibilities=split(/\,/,$ids);
6850: if ($#possibilities==0) {
6851: # ----------------------------------------------- There is only one possibility
6852: my ($mapid,$resid)=split(/\./,$ids);
6853: $syval=&encode_symb($bighash{'map_id_'.$mapid},
6854: $resid,$thisfn);
6855: } elsif (!$donotrecurse) {
6856: # ------------------------------------------ There is more than one possibility
6857: my $realpossible=0;
6858: foreach my $id (@possibilities) {
6859: my $file=$bighash{'src_'.$id};
6860: if (&allowed('bre',$file)) {
6861: my ($mapid,$resid)=split(/\./,$id);
6862: if ($bighash{'map_type_'.$mapid} ne 'page') {
6863: $realpossible++;
6864: $syval=&encode_symb($bighash{'map_id_'.$mapid},
6865: $resid,$thisfn);
6866: }
6867: }
6868: }
6869: if ($realpossible!=1) { $syval=''; }
6870: } else {
6871: $syval='';
6872: }
6873: }
6874: untie(%bighash)
6875: }
6876: }
6877: if ($syval) {
6878: return $env{$cache_str}=$syval;
6879: }
6880: }
6881: &appenv('request.ambiguous' => $thisfn);
6882: return $env{$cache_str}='';
6883: }
6884:
6885: # ---------------------------------------------------------- Return random seed
6886:
6887: sub numval {
6888: my $txt=shift;
6889: $txt=~tr/A-J/0-9/;
6890: $txt=~tr/a-j/0-9/;
6891: $txt=~tr/K-T/0-9/;
6892: $txt=~tr/k-t/0-9/;
6893: $txt=~tr/U-Z/0-5/;
6894: $txt=~tr/u-z/0-5/;
6895: $txt=~s/\D//g;
6896: if ($_64bit) { if ($txt > 2**32) { return -1; } }
6897: return int($txt);
6898: }
6899:
6900: sub numval2 {
6901: my $txt=shift;
6902: $txt=~tr/A-J/0-9/;
6903: $txt=~tr/a-j/0-9/;
6904: $txt=~tr/K-T/0-9/;
6905: $txt=~tr/k-t/0-9/;
6906: $txt=~tr/U-Z/0-5/;
6907: $txt=~tr/u-z/0-5/;
6908: $txt=~s/\D//g;
6909: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
6910: my $total;
6911: foreach my $val (@txts) { $total+=$val; }
6912: if ($_64bit) { if ($total > 2**32) { return -1; } }
6913: return int($total);
6914: }
6915:
6916: sub numval3 {
6917: use integer;
6918: my $txt=shift;
6919: $txt=~tr/A-J/0-9/;
6920: $txt=~tr/a-j/0-9/;
6921: $txt=~tr/K-T/0-9/;
6922: $txt=~tr/k-t/0-9/;
6923: $txt=~tr/U-Z/0-5/;
6924: $txt=~tr/u-z/0-5/;
6925: $txt=~s/\D//g;
6926: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
6927: my $total;
6928: foreach my $val (@txts) { $total+=$val; }
6929: if ($_64bit) { $total=(($total<<32)>>32); }
6930: return $total;
6931: }
6932:
6933: sub digest {
6934: my ($data)=@_;
6935: my $digest=&Digest::MD5::md5($data);
6936: my ($a,$b,$c,$d)=unpack("iiii",$digest);
6937: my ($e,$f);
6938: {
6939: use integer;
6940: $e=($a+$b);
6941: $f=($c+$d);
6942: if ($_64bit) {
6943: $e=(($e<<32)>>32);
6944: $f=(($f<<32)>>32);
6945: }
6946: }
6947: if (wantarray) {
6948: return ($e,$f);
6949: } else {
6950: my $g;
6951: {
6952: use integer;
6953: $g=($e+$f);
6954: if ($_64bit) {
6955: $g=(($g<<32)>>32);
6956: }
6957: }
6958: return $g;
6959: }
6960: }
6961:
6962: sub latest_rnd_algorithm_id {
6963: return '64bit5';
6964: }
6965:
6966: sub get_rand_alg {
6967: my ($courseid)=@_;
6968: if (!$courseid) { $courseid=(&whichuser())[1]; }
6969: if ($courseid) {
6970: return $env{"course.$courseid.rndseed"};
6971: }
6972: return &latest_rnd_algorithm_id();
6973: }
6974:
6975: sub validCODE {
6976: my ($CODE)=@_;
6977: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
6978: return 0;
6979: }
6980:
6981: sub getCODE {
6982: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
6983: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
6984: defined($Apache::lonhomework::parsing_a_task) ) &&
6985: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
6986: return $Apache::lonhomework::history{'resource.CODE'};
6987: }
6988: return undef;
6989: }
6990:
6991: sub rndseed {
6992: my ($symb,$courseid,$domain,$username)=@_;
6993: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
6994: if (!$symb) {
6995: unless ($symb=$wsymb) { return time; }
6996: }
6997: if (!$courseid) { $courseid=$wcourseid; }
6998: if (!$domain) { $domain=$wdomain; }
6999: if (!$username) { $username=$wusername }
7000: my $which=&get_rand_alg();
7001:
7002: if (defined(&getCODE())) {
7003: if ($which eq '64bit5') {
7004: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
7005: } elsif ($which eq '64bit4') {
7006: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
7007: } else {
7008: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
7009: }
7010: } elsif ($which eq '64bit5') {
7011: return &rndseed_64bit5($symb,$courseid,$domain,$username);
7012: } elsif ($which eq '64bit4') {
7013: return &rndseed_64bit4($symb,$courseid,$domain,$username);
7014: } elsif ($which eq '64bit3') {
7015: return &rndseed_64bit3($symb,$courseid,$domain,$username);
7016: } elsif ($which eq '64bit2') {
7017: return &rndseed_64bit2($symb,$courseid,$domain,$username);
7018: } elsif ($which eq '64bit') {
7019: return &rndseed_64bit($symb,$courseid,$domain,$username);
7020: }
7021: return &rndseed_32bit($symb,$courseid,$domain,$username);
7022: }
7023:
7024: sub rndseed_32bit {
7025: my ($symb,$courseid,$domain,$username)=@_;
7026: {
7027: use integer;
7028: my $symbchck=unpack("%32C*",$symb) << 27;
7029: my $symbseed=numval($symb) << 22;
7030: my $namechck=unpack("%32C*",$username) << 17;
7031: my $nameseed=numval($username) << 12;
7032: my $domainseed=unpack("%32C*",$domain) << 7;
7033: my $courseseed=unpack("%32C*",$courseid);
7034: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
7035: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7036: #&logthis("rndseed :$num:$symb");
7037: if ($_64bit) { $num=(($num<<32)>>32); }
7038: return $num;
7039: }
7040: }
7041:
7042: sub rndseed_64bit {
7043: my ($symb,$courseid,$domain,$username)=@_;
7044: {
7045: use integer;
7046: my $symbchck=unpack("%32S*",$symb) << 21;
7047: my $symbseed=numval($symb) << 10;
7048: my $namechck=unpack("%32S*",$username);
7049:
7050: my $nameseed=numval($username) << 21;
7051: my $domainseed=unpack("%32S*",$domain) << 10;
7052: my $courseseed=unpack("%32S*",$courseid);
7053:
7054: my $num1=$symbchck+$symbseed+$namechck;
7055: my $num2=$nameseed+$domainseed+$courseseed;
7056: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7057: #&logthis("rndseed :$num:$symb");
7058: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7059: return "$num1,$num2";
7060: }
7061: }
7062:
7063: sub rndseed_64bit2 {
7064: my ($symb,$courseid,$domain,$username)=@_;
7065: {
7066: use integer;
7067: # strings need to be an even # of cahracters long, it it is odd the
7068: # last characters gets thrown away
7069: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7070: my $symbseed=numval($symb) << 10;
7071: my $namechck=unpack("%32S*",$username.' ');
7072:
7073: my $nameseed=numval($username) << 21;
7074: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7075: my $courseseed=unpack("%32S*",$courseid.' ');
7076:
7077: my $num1=$symbchck+$symbseed+$namechck;
7078: my $num2=$nameseed+$domainseed+$courseseed;
7079: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7080: #&logthis("rndseed :$num:$symb");
7081: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7082: return "$num1,$num2";
7083: }
7084: }
7085:
7086: sub rndseed_64bit3 {
7087: my ($symb,$courseid,$domain,$username)=@_;
7088: {
7089: use integer;
7090: # strings need to be an even # of cahracters long, it it is odd the
7091: # last characters gets thrown away
7092: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7093: my $symbseed=numval2($symb) << 10;
7094: my $namechck=unpack("%32S*",$username.' ');
7095:
7096: my $nameseed=numval2($username) << 21;
7097: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7098: my $courseseed=unpack("%32S*",$courseid.' ');
7099:
7100: my $num1=$symbchck+$symbseed+$namechck;
7101: my $num2=$nameseed+$domainseed+$courseseed;
7102: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7103: #&logthis("rndseed :$num1:$num2:$_64bit");
7104: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7105:
7106: return "$num1:$num2";
7107: }
7108: }
7109:
7110: sub rndseed_64bit4 {
7111: my ($symb,$courseid,$domain,$username)=@_;
7112: {
7113: use integer;
7114: # strings need to be an even # of cahracters long, it it is odd the
7115: # last characters gets thrown away
7116: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7117: my $symbseed=numval3($symb) << 10;
7118: my $namechck=unpack("%32S*",$username.' ');
7119:
7120: my $nameseed=numval3($username) << 21;
7121: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7122: my $courseseed=unpack("%32S*",$courseid.' ');
7123:
7124: my $num1=$symbchck+$symbseed+$namechck;
7125: my $num2=$nameseed+$domainseed+$courseseed;
7126: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7127: #&logthis("rndseed :$num1:$num2:$_64bit");
7128: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7129:
7130: return "$num1:$num2";
7131: }
7132: }
7133:
7134: sub rndseed_64bit5 {
7135: my ($symb,$courseid,$domain,$username)=@_;
7136: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
7137: return "$num1:$num2";
7138: }
7139:
7140: sub rndseed_CODE_64bit {
7141: my ($symb,$courseid,$domain,$username)=@_;
7142: {
7143: use integer;
7144: my $symbchck=unpack("%32S*",$symb.' ') << 16;
7145: my $symbseed=numval2($symb);
7146: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7147: my $CODEseed=numval(&getCODE());
7148: my $courseseed=unpack("%32S*",$courseid.' ');
7149: my $num1=$symbseed+$CODEchck;
7150: my $num2=$CODEseed+$courseseed+$symbchck;
7151: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7152: #&logthis("rndseed :$num1:$num2:$symb");
7153: if ($_64bit) { $num1=(($num1<<32)>>32); }
7154: if ($_64bit) { $num2=(($num2<<32)>>32); }
7155: return "$num1:$num2";
7156: }
7157: }
7158:
7159: sub rndseed_CODE_64bit4 {
7160: my ($symb,$courseid,$domain,$username)=@_;
7161: {
7162: use integer;
7163: my $symbchck=unpack("%32S*",$symb.' ') << 16;
7164: my $symbseed=numval3($symb);
7165: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7166: my $CODEseed=numval3(&getCODE());
7167: my $courseseed=unpack("%32S*",$courseid.' ');
7168: my $num1=$symbseed+$CODEchck;
7169: my $num2=$CODEseed+$courseseed+$symbchck;
7170: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7171: #&logthis("rndseed :$num1:$num2:$symb");
7172: if ($_64bit) { $num1=(($num1<<32)>>32); }
7173: if ($_64bit) { $num2=(($num2<<32)>>32); }
7174: return "$num1:$num2";
7175: }
7176: }
7177:
7178: sub rndseed_CODE_64bit5 {
7179: my ($symb,$courseid,$domain,$username)=@_;
7180: my $code = &getCODE();
7181: my ($num1,$num2)=&digest("$symb,$courseid,$code");
7182: return "$num1:$num2";
7183: }
7184:
7185: sub setup_random_from_rndseed {
7186: my ($rndseed)=@_;
7187: if ($rndseed =~/([,:])/) {
7188: my ($num1,$num2)=split(/[,:]/,$rndseed);
7189: &Math::Random::random_set_seed(abs($num1),abs($num2));
7190: } else {
7191: &Math::Random::random_set_seed_from_phrase($rndseed);
7192: }
7193: }
7194:
7195: sub latest_receipt_algorithm_id {
7196: return 'receipt3';
7197: }
7198:
7199: sub recunique {
7200: my $fucourseid=shift;
7201: my $unique;
7202: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
7203: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
7204: $unique=$env{"course.$fucourseid.internal.encseed"};
7205: } else {
7206: $unique=$perlvar{'lonReceipt'};
7207: }
7208: return unpack("%32C*",$unique);
7209: }
7210:
7211: sub recprefix {
7212: my $fucourseid=shift;
7213: my $prefix;
7214: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
7215: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
7216: $prefix=$env{"course.$fucourseid.internal.encpref"};
7217: } else {
7218: $prefix=$perlvar{'lonHostID'};
7219: }
7220: return unpack("%32C*",$prefix);
7221: }
7222:
7223: sub ireceipt {
7224: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
7225:
7226: my $return =&recprefix($fucourseid).'-';
7227:
7228: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
7229: $env{'request.state'} eq 'construct') {
7230: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
7231: return $return;
7232: }
7233:
7234: my $cuname=unpack("%32C*",$funame);
7235: my $cudom=unpack("%32C*",$fudom);
7236: my $cucourseid=unpack("%32C*",$fucourseid);
7237: my $cusymb=unpack("%32C*",$fusymb);
7238: my $cunique=&recunique($fucourseid);
7239: my $cpart=unpack("%32S*",$part);
7240: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
7241:
7242: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
7243:
7244: $return.= ($cunique%$cuname+
7245: $cunique%$cudom+
7246: $cusymb%$cuname+
7247: $cusymb%$cudom+
7248: $cucourseid%$cuname+
7249: $cucourseid%$cudom+
7250: $cpart%$cuname+
7251: $cpart%$cudom);
7252: } else {
7253: $return.= ($cunique%$cuname+
7254: $cunique%$cudom+
7255: $cusymb%$cuname+
7256: $cusymb%$cudom+
7257: $cucourseid%$cuname+
7258: $cucourseid%$cudom);
7259: }
7260: return $return;
7261: }
7262:
7263: sub receipt {
7264: my ($part)=@_;
7265: my ($symb,$courseid,$domain,$name) = &whichuser();
7266: return &ireceipt($name,$domain,$courseid,$symb,$part);
7267: }
7268:
7269: sub whichuser {
7270: my ($passedsymb)=@_;
7271: my ($symb,$courseid,$domain,$name,$publicuser);
7272: if (defined($env{'form.grade_symb'})) {
7273: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
7274: my $allowed=&allowed('vgr',$tmp_courseid);
7275: if (!$allowed &&
7276: exists($env{'request.course.sec'}) &&
7277: $env{'request.course.sec'} !~ /^\s*$/) {
7278: $allowed=&allowed('vgr',$tmp_courseid.
7279: '/'.$env{'request.course.sec'});
7280: }
7281: if ($allowed) {
7282: ($symb)=&get_env_multiple('form.grade_symb');
7283: $courseid=$tmp_courseid;
7284: ($domain)=&get_env_multiple('form.grade_domain');
7285: ($name)=&get_env_multiple('form.grade_username');
7286: return ($symb,$courseid,$domain,$name,$publicuser);
7287: }
7288: }
7289: if (!$passedsymb) {
7290: $symb=&symbread();
7291: } else {
7292: $symb=$passedsymb;
7293: }
7294: $courseid=$env{'request.course.id'};
7295: $domain=$env{'user.domain'};
7296: $name=$env{'user.name'};
7297: if ($name eq 'public' && $domain eq 'public') {
7298: if (!defined($env{'form.username'})) {
7299: $env{'form.username'}.=time.rand(10000000);
7300: }
7301: $name.=$env{'form.username'};
7302: }
7303: return ($symb,$courseid,$domain,$name,$publicuser);
7304:
7305: }
7306:
7307: # ------------------------------------------------------------ Serves up a file
7308: # returns either the contents of the file or
7309: # -1 if the file doesn't exist
7310: #
7311: # if the target is a file that was uploaded via DOCS,
7312: # a check will be made to see if a current copy exists on the local server,
7313: # if it does this will be served, otherwise a copy will be retrieved from
7314: # the home server for the course and stored in /home/httpd/html/userfiles on
7315: # the local server.
7316:
7317: sub getfile {
7318: my ($file) = @_;
7319: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
7320: &repcopy($file);
7321: return &readfile($file);
7322: }
7323:
7324: sub repcopy_userfile {
7325: my ($file)=@_;
7326: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
7327: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
7328: my ($cdom,$cnum,$filename) =
7329: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
7330: my $uri="/uploaded/$cdom/$cnum/$filename";
7331: if (-e "$file") {
7332: # we already have a local copy, check it out
7333: my @fileinfo = stat($file);
7334: my $rtncode;
7335: my $info;
7336: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
7337: if ($lwpresp ne 'ok') {
7338: # there is no such file anymore, even though we had a local copy
7339: if ($rtncode eq '404') {
7340: unlink($file);
7341: }
7342: return -1;
7343: }
7344: if ($info < $fileinfo[9]) {
7345: # nice, the file we have is up-to-date, just say okay
7346: return 'ok';
7347: } else {
7348: # the file is outdated, get rid of it
7349: unlink($file);
7350: }
7351: }
7352: # one way or the other, at this point, we don't have the file
7353: # construct the correct path for the file
7354: my @parts = ($cdom,$cnum);
7355: if ($filename =~ m|^(.+)/[^/]+$|) {
7356: push @parts, split(/\//,$1);
7357: }
7358: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
7359: foreach my $part (@parts) {
7360: $path .= '/'.$part;
7361: if (!-e $path) {
7362: mkdir($path,0770);
7363: }
7364: }
7365: # now the path exists for sure
7366: # get a user agent
7367: my $ua=new LWP::UserAgent;
7368: my $transferfile=$file.'.in.transfer';
7369: # FIXME: this should flock
7370: if (-e $transferfile) { return 'ok'; }
7371: my $request;
7372: $uri=~s/^\///;
7373: $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
7374: my $response=$ua->request($request,$transferfile);
7375: # did it work?
7376: if ($response->is_error()) {
7377: unlink($transferfile);
7378: &logthis("Userfile repcopy failed for $uri");
7379: return -1;
7380: }
7381: # worked, rename the transfer file
7382: rename($transferfile,$file);
7383: return 'ok';
7384: }
7385:
7386: sub tokenwrapper {
7387: my $uri=shift;
7388: $uri=~s|^http\://([^/]+)||;
7389: $uri=~s|^/||;
7390: $env{'user.environment'}=~/\/([^\/]+)\.id/;
7391: my $token=$1;
7392: my (undef,$udom,$uname,$file)=split('/',$uri,4);
7393: if ($udom && $uname && $file) {
7394: $file=~s|(\?\.*)*$||;
7395: &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
7396: return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
7397: (($uri=~/\?/)?'&':'?').'token='.$token.
7398: '&tokenissued='.$perlvar{'lonHostID'};
7399: } else {
7400: return '/adm/notfound.html';
7401: }
7402: }
7403:
7404: # call with reqtype HEAD: get last modification time
7405: # call with reqtype GET: get the file contents
7406: # Do not call this with reqtype GET for large files! It loads everything into memory
7407: #
7408: sub getuploaded {
7409: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
7410: $uri=~s/^\///;
7411: $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
7412: my $ua=new LWP::UserAgent;
7413: my $request=new HTTP::Request($reqtype,$uri);
7414: my $response=$ua->request($request);
7415: $$rtncode = $response->code;
7416: if (! $response->is_success()) {
7417: return 'failed';
7418: }
7419: if ($reqtype eq 'HEAD') {
7420: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
7421: } elsif ($reqtype eq 'GET') {
7422: $$info = $response->content;
7423: }
7424: return 'ok';
7425: }
7426:
7427: sub readfile {
7428: my $file = shift;
7429: if ( (! -e $file ) || ($file eq '') ) { return -1; };
7430: my $fh;
7431: open($fh,"<$file");
7432: my $a='';
7433: while (my $line = <$fh>) { $a .= $line; }
7434: return $a;
7435: }
7436:
7437: sub filelocation {
7438: my ($dir,$file) = @_;
7439: my $location;
7440: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
7441:
7442: if ($file =~ m-^/adm/-) {
7443: $file=~s-^/adm/wrapper/-/-;
7444: $file=~s-^/adm/coursedocs/showdoc/-/-;
7445: }
7446: if ($file=~m:^/~:) { # is a contruction space reference
7447: $location = $file;
7448: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
7449: } elsif ($file=~m{^/home/$match_username/public_html/}) {
7450: # is a correct contruction space reference
7451: $location = $file;
7452: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
7453: my ($udom,$uname,$filename)=
7454: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
7455: my $home=&homeserver($uname,$udom);
7456: my $is_me=0;
7457: my @ids=¤t_machine_ids();
7458: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
7459: if ($is_me) {
7460: $location=&propath($udom,$uname).
7461: '/userfiles/'.$filename;
7462: } else {
7463: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
7464: $udom.'/'.$uname.'/'.$filename;
7465: }
7466: } else {
7467: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
7468: $file=~s:^/res/:/:;
7469: if ( !( $file =~ m:^/:) ) {
7470: $location = $dir. '/'.$file;
7471: } else {
7472: $location = '/home/httpd/html/res'.$file;
7473: }
7474: }
7475: $location=~s://+:/:g; # remove duplicate /
7476: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
7477: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
7478: return $location;
7479: }
7480:
7481: sub hreflocation {
7482: my ($dir,$file)=@_;
7483: unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
7484: $file=filelocation($dir,$file);
7485: } elsif ($file=~m-^/adm/-) {
7486: $file=~s-^/adm/wrapper/-/-;
7487: $file=~s-^/adm/coursedocs/showdoc/-/-;
7488: }
7489: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
7490: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
7491: } elsif ($file=~m-/home/($match_username)/public_html/-) {
7492: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
7493: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
7494: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
7495: -/uploaded/$1/$2/-x;
7496: }
7497: return $file;
7498: }
7499:
7500: sub current_machine_domains {
7501: return &machine_domains(&hostname($perlvar{'lonHostID'}));
7502: }
7503:
7504: sub machine_domains {
7505: my ($hostname) = @_;
7506: my @domains;
7507: my %hostname = &all_hostnames();
7508: while( my($id, $name) = each(%hostname)) {
7509: # &logthis("-$id-$name-$hostname-");
7510: if ($hostname eq $name) {
7511: push(@domains,&host_domain($id));
7512: }
7513: }
7514: return @domains;
7515: }
7516:
7517: sub current_machine_ids {
7518: return &machine_ids(&hostname($perlvar{'lonHostID'}));
7519: }
7520:
7521: sub machine_ids {
7522: my ($hostname) = @_;
7523: $hostname ||= &hostname($perlvar{'lonHostID'});
7524: my @ids;
7525: my %hostname = &all_hostnames();
7526: while( my($id, $name) = each(%hostname)) {
7527: # &logthis("-$id-$name-$hostname-");
7528: if ($hostname eq $name) {
7529: push(@ids,$id);
7530: }
7531: }
7532: return @ids;
7533: }
7534:
7535: sub additional_machine_domains {
7536: my @domains;
7537: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
7538: while( my $line = <$fh>) {
7539: $line =~ s/\s//g;
7540: push(@domains,$line);
7541: }
7542: return @domains;
7543: }
7544:
7545: sub default_login_domain {
7546: my $domain = $perlvar{'lonDefDomain'};
7547: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
7548: foreach my $posdom (¤t_machine_domains(),
7549: &additional_machine_domains()) {
7550: if (lc($posdom) eq lc($testdomain)) {
7551: $domain=$posdom;
7552: last;
7553: }
7554: }
7555: return $domain;
7556: }
7557:
7558: # ------------------------------------------------------------- Declutters URLs
7559:
7560: sub declutter {
7561: my $thisfn=shift;
7562: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
7563: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
7564: $thisfn=~s/^\///;
7565: $thisfn=~s|^adm/wrapper/||;
7566: $thisfn=~s|^adm/coursedocs/showdoc/||;
7567: $thisfn=~s/^res\///;
7568: $thisfn=~s/\?.+$//;
7569: return $thisfn;
7570: }
7571:
7572: # ------------------------------------------------------------- Clutter up URLs
7573:
7574: sub clutter {
7575: my $thisfn='/'.&declutter(shift);
7576: unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) {
7577: $thisfn='/res'.$thisfn;
7578: }
7579: if ($thisfn !~m|/adm|) {
7580: if ($thisfn =~ m|/ext/|) {
7581: $thisfn='/adm/wrapper'.$thisfn;
7582: } else {
7583: my ($ext) = ($thisfn =~ /\.(\w+)$/);
7584: my $embstyle=&Apache::loncommon::fileembstyle($ext);
7585: if ($embstyle eq 'ssi'
7586: || ($embstyle eq 'hdn')
7587: || ($embstyle eq 'rat')
7588: || ($embstyle eq 'prv')
7589: || ($embstyle eq 'ign')) {
7590: #do nothing with these
7591: } elsif (($embstyle eq 'img')
7592: || ($embstyle eq 'emb')
7593: || ($embstyle eq 'wrp')) {
7594: $thisfn='/adm/wrapper'.$thisfn;
7595: } elsif ($embstyle eq 'unk'
7596: && $thisfn!~/\.(sequence|page)$/) {
7597: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
7598: } else {
7599: # &logthis("Got a blank emb style");
7600: }
7601: }
7602: }
7603: return $thisfn;
7604: }
7605:
7606: sub clutter_with_no_wrapper {
7607: my $uri = &clutter(shift);
7608: if ($uri =~ m-^/adm/-) {
7609: $uri =~ s-^/adm/wrapper/-/-;
7610: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
7611: }
7612: return $uri;
7613: }
7614:
7615: sub freeze_escape {
7616: my ($value)=@_;
7617: if (ref($value)) {
7618: $value=&nfreeze($value);
7619: return '__FROZEN__'.&escape($value);
7620: }
7621: return &escape($value);
7622: }
7623:
7624:
7625: sub thaw_unescape {
7626: my ($value)=@_;
7627: if ($value =~ /^__FROZEN__/) {
7628: substr($value,0,10,undef);
7629: $value=&unescape($value);
7630: return &thaw($value);
7631: }
7632: return &unescape($value);
7633: }
7634:
7635: sub correct_line_ends {
7636: my ($result)=@_;
7637: $$result =~s/\r\n/\n/mg;
7638: $$result =~s/\r/\n/mg;
7639: }
7640: # ================================================================ Main Program
7641:
7642: sub goodbye {
7643: &logthis("Starting Shut down");
7644: #not converted to using infrastruture and probably shouldn't be
7645: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
7646: #converted
7647: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
7648: &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
7649: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
7650: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
7651: #1.1 only
7652: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
7653: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
7654: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
7655: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
7656: &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
7657: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
7658: &logthis(sprintf("%-20s is %s",'hits',$hits));
7659: &flushcourselogs();
7660: &logthis("Shutting down");
7661: }
7662:
7663: sub get_dns {
7664: my ($url,$func) = @_;
7665: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
7666: foreach my $dns (<$config>) {
7667: next if ($dns !~ /^\^(\S*)/x);
7668: $dns = $1;
7669: my $ua=new LWP::UserAgent;
7670: my $request=new HTTP::Request('GET',"http://$dns$url");
7671: my $response=$ua->request($request);
7672: next if ($response->is_error());
7673: my @content = split("\n",$response->content);
7674: &$func(\@content);
7675: }
7676: close($config);
7677: }
7678: # ------------------------------------------------------------ Read domain file
7679: {
7680: my $loaded;
7681: my %domain;
7682:
7683: sub parse_domain_tab {
7684: my ($lines) = @_;
7685: foreach my $line (@$lines) {
7686: next if ($line =~ /^(\#|\s*$ )/x);
7687:
7688: chomp($line);
7689: my ($name,@elements) = split(/:/,$line,9);
7690: my %this_domain;
7691: foreach my $field ('description', 'auth_def', 'auth_arg_def',
7692: 'lang_def', 'city', 'longi', 'lati',
7693: 'primary') {
7694: $this_domain{$field} = shift(@elements);
7695: }
7696: $domain{$name} = \%this_domain;
7697: }
7698: }
7699:
7700: sub reset_domain_info {
7701: undef($loaded);
7702: undef(%domain);
7703: }
7704:
7705: sub load_domain_tab {
7706: &get_dns('/adm/dns/domain',\&parse_domain_tab);
7707: my $fh;
7708: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
7709: my @lines = <$fh>;
7710: &parse_domain_tab(\@lines);
7711: }
7712: close($fh);
7713: $loaded = 1;
7714: }
7715:
7716: sub domain {
7717: &load_domain_tab() if (!$loaded);
7718:
7719: my ($name,$what) = @_;
7720: return if ( !exists($domain{$name}) );
7721:
7722: if (!$what) {
7723: return $domain{$name}{'description'};
7724: }
7725: return $domain{$name}{$what};
7726: }
7727: }
7728:
7729:
7730: # ------------------------------------------------------------- Read hosts file
7731: {
7732: my %hostname;
7733: my %hostdom;
7734: my %libserv;
7735: my $loaded;
7736:
7737: sub parse_hosts_tab {
7738: my ($file) = @_;
7739: foreach my $configline (@$file) {
7740: next if ($configline =~ /^(\#|\s*$ )/x);
7741: next if ($configline =~ /^\^/);
7742: chomp($configline);
7743: my ($id,$domain,$role,$name)=split(/:/,$configline);
7744: $name=~s/\s//g;
7745: if ($id && $domain && $role && $name) {
7746: $hostname{$id}=$name;
7747: $hostdom{$id}=$domain;
7748: if ($role eq 'library') { $libserv{$id}=$name; }
7749: }
7750: }
7751: }
7752:
7753: sub reset_hosts_info {
7754: &reset_domain_info();
7755: &reset_hosts_ip_info();
7756: undef(%hostname);
7757: undef(%hostdom);
7758: undef(%libserv);
7759: undef($loaded);
7760: }
7761:
7762: sub load_hosts_tab {
7763: &get_dns('/adm/dns/hosts',\&parse_hosts_tab);
7764: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
7765: my @config = <$config>;
7766: &parse_hosts_tab(\@config);
7767: close($config);
7768: $loaded=1;
7769: }
7770:
7771: sub hostname {
7772: &load_hosts_tab() if (!$loaded);
7773:
7774: my ($lonid) = @_;
7775: return $hostname{$lonid};
7776: }
7777:
7778: sub all_hostnames {
7779: &load_hosts_tab() if (!$loaded);
7780:
7781: return %hostname;
7782: }
7783:
7784: sub is_library {
7785: &load_hosts_tab() if (!$loaded);
7786:
7787: return exists($libserv{$_[0]});
7788: }
7789:
7790: sub all_library {
7791: &load_hosts_tab() if (!$loaded);
7792:
7793: return %libserv;
7794: }
7795:
7796: sub get_servers {
7797: &load_hosts_tab() if (!$loaded);
7798:
7799: my ($domain,$type) = @_;
7800: my %possible_hosts = ($type eq 'library') ? %libserv
7801: : %hostname;
7802: my %result;
7803: if (ref($domain) eq 'ARRAY') {
7804: while ( my ($host,$hostname) = each(%possible_hosts)) {
7805: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
7806: $result{$host} = $hostname;
7807: }
7808: }
7809: } else {
7810: while ( my ($host,$hostname) = each(%possible_hosts)) {
7811: if ($hostdom{$host} eq $domain) {
7812: $result{$host} = $hostname;
7813: }
7814: }
7815: }
7816: return %result;
7817: }
7818:
7819: sub host_domain {
7820: &load_hosts_tab() if (!$loaded);
7821:
7822: my ($lonid) = @_;
7823: return $hostdom{$lonid};
7824: }
7825:
7826: sub all_domains {
7827: &load_hosts_tab() if (!$loaded);
7828:
7829: my %seen;
7830: my @uniq = grep(!$seen{$_}++, values(%hostdom));
7831: return @uniq;
7832: }
7833: }
7834:
7835: {
7836: my %iphost;
7837: my %name_to_ip;
7838: my %lonid_to_ip;
7839: sub get_hosts_from_ip {
7840: my ($ip) = @_;
7841: my %iphosts = &get_iphost();
7842: if (ref($iphosts{$ip})) {
7843: return @{$iphosts{$ip}};
7844: }
7845: return;
7846: }
7847:
7848: sub reset_hosts_ip_info {
7849: undef(%iphost);
7850: undef(%name_to_ip);
7851: undef(%lonid_to_ip);
7852: }
7853:
7854: sub get_host_ip {
7855: my ($lonid) = @_;
7856: if (exists($lonid_to_ip{$lonid})) {
7857: return $lonid_to_ip{$lonid};
7858: }
7859: my $name=&hostname($lonid);
7860: my $ip = gethostbyname($name);
7861: return if (!$ip || length($ip) ne 4);
7862: $ip=inet_ntoa($ip);
7863: $name_to_ip{$name} = $ip;
7864: $lonid_to_ip{$lonid} = $ip;
7865: return $ip;
7866: }
7867:
7868: sub get_iphost {
7869: if (%iphost) { return %iphost; }
7870: my %hostname = &all_hostnames();
7871: foreach my $id (keys(%hostname)) {
7872: my $name=&hostname($id);
7873: my $ip;
7874: if (!exists($name_to_ip{$name})) {
7875: $ip = gethostbyname($name);
7876: if (!$ip || length($ip) ne 4) {
7877: &logthis("Skipping host $id name $name no IP found");
7878: next;
7879: }
7880: $ip=inet_ntoa($ip);
7881: $name_to_ip{$name} = $ip;
7882: } else {
7883: $ip = $name_to_ip{$name};
7884: }
7885: $lonid_to_ip{$id} = $ip;
7886: push(@{$iphost{$ip}},$id);
7887: }
7888: return %iphost;
7889: }
7890: }
7891:
7892: BEGIN {
7893:
7894: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
7895: unless ($readit) {
7896: {
7897: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
7898: %perlvar = (%perlvar,%{$configvars});
7899: }
7900:
7901:
7902: # ------------------------------------------------------ Read spare server file
7903: {
7904: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
7905:
7906: while (my $configline=<$config>) {
7907: chomp($configline);
7908: if ($configline) {
7909: my ($host,$type) = split(':',$configline,2);
7910: if (!defined($type) || $type eq '') { $type = 'default' };
7911: push(@{ $spareid{$type} }, $host);
7912: }
7913: }
7914: close($config);
7915: }
7916: # ------------------------------------------------------------ Read permissions
7917: {
7918: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
7919:
7920: while (my $configline=<$config>) {
7921: chomp($configline);
7922: if ($configline) {
7923: my ($role,$perm)=split(/ /,$configline);
7924: if ($perm ne '') { $pr{$role}=$perm; }
7925: }
7926: }
7927: close($config);
7928: }
7929:
7930: # -------------------------------------------- Read plain texts for permissions
7931: {
7932: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
7933:
7934: while (my $configline=<$config>) {
7935: chomp($configline);
7936: if ($configline) {
7937: my ($short,@plain)=split(/:/,$configline);
7938: %{$prp{$short}} = ();
7939: if (@plain > 0) {
7940: $prp{$short}{'std'} = $plain[0];
7941: for (my $i=1; $i<@plain; $i++) {
7942: $prp{$short}{'alt'.$i} = $plain[$i];
7943: }
7944: }
7945: }
7946: }
7947: close($config);
7948: }
7949:
7950: # ---------------------------------------------------------- Read package table
7951: {
7952: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
7953:
7954: while (my $configline=<$config>) {
7955: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
7956: chomp($configline);
7957: my ($short,$plain)=split(/:/,$configline);
7958: my ($pack,$name)=split(/\&/,$short);
7959: if ($plain ne '') {
7960: $packagetab{$pack.'&'.$name.'&name'}=$name;
7961: $packagetab{$short}=$plain;
7962: }
7963: }
7964: close($config);
7965: }
7966:
7967: # ------------- set up temporary directory
7968: {
7969: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
7970:
7971: }
7972:
7973: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
7974: 'compress_threshold'=> 20_000,
7975: });
7976:
7977: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
7978: $dumpcount=0;
7979:
7980: &logtouch();
7981: &logthis('<font color="yellow">INFO: Read configuration</font>');
7982: $readit=1;
7983: {
7984: use integer;
7985: my $test=(2**32)+1;
7986: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
7987: &logthis(" Detected 64bit platform ($_64bit)");
7988: }
7989: }
7990: }
7991:
7992: 1;
7993: __END__
7994:
7995: =pod
7996:
7997: =head1 NAME
7998:
7999: Apache::lonnet - Subroutines to ask questions about things in the network.
8000:
8001: =head1 SYNOPSIS
8002:
8003: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
8004:
8005: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
8006:
8007: Common parameters:
8008:
8009: =over 4
8010:
8011: =item *
8012:
8013: $uname : an internal username (if $cname expecting a course Id specifically)
8014:
8015: =item *
8016:
8017: $udom : a domain (if $cdom expecting a course's domain specifically)
8018:
8019: =item *
8020:
8021: $symb : a resource instance identifier
8022:
8023: =item *
8024:
8025: $namespace : the name of a .db file that contains the data needed or
8026: being set.
8027:
8028: =back
8029:
8030: =head1 OVERVIEW
8031:
8032: lonnet provides subroutines which interact with the
8033: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
8034: about classes, users, and resources.
8035:
8036: For many of these objects you can also use this to store data about
8037: them or modify them in various ways.
8038:
8039: =head2 Symbs
8040:
8041: To identify a specific instance of a resource, LON-CAPA uses symbols
8042: or "symbs"X<symb>. These identifiers are built from the URL of the
8043: map, the resource number of the resource in the map, and the URL of
8044: the resource itself. The latter is somewhat redundant, but might help
8045: if maps change.
8046:
8047: An example is
8048:
8049: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
8050:
8051: The respective map entry is
8052:
8053: <resource id="19" src="/res/msu/korte/tests/part12.problem"
8054: title="Problem 2">
8055: </resource>
8056:
8057: Symbs are used by the random number generator, as well as to store and
8058: restore data specific to a certain instance of for example a problem.
8059:
8060: =head2 Storing And Retrieving Data
8061:
8062: X<store()>X<cstore()>X<restore()>Three of the most important functions
8063: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
8064: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
8065: is is the non-critical message twin of cstore. These functions are for
8066: handlers to store a perl hash to a user's permanent data space in an
8067: easy manner, and to retrieve it again on another call. It is expected
8068: that a handler would use this once at the beginning to retrieve data,
8069: and then again once at the end to send only the new data back.
8070:
8071: The data is stored in the user's data directory on the user's
8072: homeserver under the ID of the course.
8073:
8074: The hash that is returned by restore will have all of the previous
8075: value for all of the elements of the hash.
8076:
8077: Example:
8078:
8079: #creating a hash
8080: my %hash;
8081: $hash{'foo'}='bar';
8082:
8083: #storing it
8084: &Apache::lonnet::cstore(\%hash);
8085:
8086: #changing a value
8087: $hash{'foo'}='notbar';
8088:
8089: #adding a new value
8090: $hash{'bar'}='foo';
8091: &Apache::lonnet::cstore(\%hash);
8092:
8093: #retrieving the hash
8094: my %history=&Apache::lonnet::restore();
8095:
8096: #print the hash
8097: foreach my $key (sort(keys(%history))) {
8098: print("\%history{$key} = $history{$key}");
8099: }
8100:
8101: Will print out:
8102:
8103: %history{1:foo} = bar
8104: %history{1:keys} = foo:timestamp
8105: %history{1:timestamp} = 990455579
8106: %history{2:bar} = foo
8107: %history{2:foo} = notbar
8108: %history{2:keys} = foo:bar:timestamp
8109: %history{2:timestamp} = 990455580
8110: %history{bar} = foo
8111: %history{foo} = notbar
8112: %history{timestamp} = 990455580
8113: %history{version} = 2
8114:
8115: Note that the special hash entries C<keys>, C<version> and
8116: C<timestamp> were added to the hash. C<version> will be equal to the
8117: total number of versions of the data that have been stored. The
8118: C<timestamp> attribute will be the UNIX time the hash was
8119: stored. C<keys> is available in every historical section to list which
8120: keys were added or changed at a specific historical revision of a
8121: hash.
8122:
8123: B<Warning>: do not store the hash that restore returns directly. This
8124: will cause a mess since it will restore the historical keys as if the
8125: were new keys. I.E. 1:foo will become 1:1:foo etc.
8126:
8127: Calling convention:
8128:
8129: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
8130: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
8131:
8132: For more detailed information, see lonnet specific documentation.
8133:
8134: =head1 RETURN MESSAGES
8135:
8136: =over 4
8137:
8138: =item * B<con_lost>: unable to contact remote host
8139:
8140: =item * B<con_delayed>: unable to contact remote host, message will be delivered
8141: when the connection is brought back up
8142:
8143: =item * B<con_failed>: unable to contact remote host and unable to save message
8144: for later delivery
8145:
8146: =item * B<error:>: an error a occured, a description of the error follows the :
8147:
8148: =item * B<no_such_host>: unable to fund a host associated with the user/domain
8149: that was requested
8150:
8151: =back
8152:
8153: =head1 PUBLIC SUBROUTINES
8154:
8155: =head2 Session Environment Functions
8156:
8157: =over 4
8158:
8159: =item *
8160: X<appenv()>
8161: B<appenv(%hash)>: the value of %hash is written to
8162: the user envirnoment file, and will be restored for each access this
8163: user makes during this session, also modifies the %env for the current
8164: process
8165:
8166: =item *
8167: X<delenv()>
8168: B<delenv($regexp)>: removes all items from the session
8169: environment file that matches the regular expression in $regexp. The
8170: values are also delted from the current processes %env.
8171:
8172: =item * get_env_multiple($name)
8173:
8174: gets $name from the %env hash, it seemlessly handles the cases where multiple
8175: values may be defined and end up as an array ref.
8176:
8177: returns an array of values
8178:
8179: =back
8180:
8181: =head2 User Information
8182:
8183: =over 4
8184:
8185: =item *
8186: X<queryauthenticate()>
8187: B<queryauthenticate($uname,$udom)>: try to determine user's current
8188: authentication scheme
8189:
8190: =item *
8191: X<authenticate()>
8192: B<authenticate($uname,$upass,$udom)>: try to
8193: authenticate user from domain's lib servers (first use the current
8194: one). C<$upass> should be the users password.
8195:
8196: =item *
8197: X<homeserver()>
8198: B<homeserver($uname,$udom)>: find the server which has
8199: the user's directory and files (there must be only one), this caches
8200: the answer, and also caches if there is a borken connection.
8201:
8202: =item *
8203: X<idget()>
8204: B<idget($udom,@ids)>: find the usernames behind a list of IDs
8205: (IDs are a unique resource in a domain, there must be only 1 ID per
8206: username, and only 1 username per ID in a specific domain) (returns
8207: hash: id=>name,id=>name)
8208:
8209: =item *
8210: X<idrget()>
8211: B<idrget($udom,@unames)>: find the IDs behind a list of
8212: usernames (returns hash: name=>id,name=>id)
8213:
8214: =item *
8215: X<idput()>
8216: B<idput($udom,%ids)>: store away a list of names and associated IDs
8217:
8218: =item *
8219: X<rolesinit()>
8220: B<rolesinit($udom,$username,$authhost)>: get user privileges
8221:
8222: =item *
8223: X<getsection()>
8224: B<getsection($udom,$uname,$cname)>: finds the section of student in the
8225: course $cname, return section name/number or '' for "not in course"
8226: and '-1' for "no section"
8227:
8228: =item *
8229: X<userenvironment()>
8230: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
8231: passed in @what from the requested user's environment, returns a hash
8232:
8233: =item *
8234: X<userlog_query()>
8235: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
8236: activity.log file. %filters defines filters applied when parsing the
8237: log file. These can be start or end timestamps, or the type of action
8238: - log to look for Login or Logout events, check for Checkin or
8239: Checkout, role for role selection. The response is in the form
8240: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
8241: escaped strings of the action recorded in the activity.log file.
8242:
8243: =back
8244:
8245: =head2 User Roles
8246:
8247: =over 4
8248:
8249: =item *
8250:
8251: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
8252: F: full access
8253: U,I,K: authentication modes (cxx only)
8254: '': forbidden
8255: 1: user needs to choose course
8256: 2: browse allowed
8257: A: passphrase authentication needed
8258:
8259: =item *
8260:
8261: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
8262: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
8263: and course level
8264:
8265: =item *
8266:
8267: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
8268: explanation of a user role term
8269:
8270: =item *
8271:
8272: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
8273: All arguments are optional. Returns a hash of a roles, either for
8274: co-author/assistant author roles for a user's Construction Space
8275: (default), or if $context is 'user', roles for the user himself,
8276: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
8277: and value is set to colon-separated start and end times for the role.
8278: If no username and domain are specified, will default to current
8279: user/domain. Types, roles, and roledoms are references to arrays,
8280: of role statuses (active, future or previous), roles
8281: (e.g., cc,in, st etc.) and domains of the roles which can be used
8282: to restrict the list of roles reported. If no array ref is
8283: provided for types, will default to return only active roles.
8284:
8285: =back
8286:
8287: =head2 User Modification
8288:
8289: =over 4
8290:
8291: =item *
8292:
8293: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
8294: user for the level given by URL. Optional start and end dates (leave empty
8295: string or zero for "no date")
8296:
8297: =item *
8298:
8299: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
8300: change a users, password, possible return values are: ok,
8301: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
8302: refused
8303:
8304: =item *
8305:
8306: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
8307:
8308: =item *
8309:
8310: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
8311: modify user
8312:
8313: =item *
8314:
8315: modifystudent
8316:
8317: modify a students enrollment and identification information.
8318: The course id is resolved based on the current users environment.
8319: This means the envoking user must be a course coordinator or otherwise
8320: associated with a course.
8321:
8322: This call is essentially a wrapper for lonnet::modifyuser and
8323: lonnet::modify_student_enrollment
8324:
8325: Inputs:
8326:
8327: =over 4
8328:
8329: =item B<$udom> Students loncapa domain
8330:
8331: =item B<$uname> Students loncapa login name
8332:
8333: =item B<$uid> Students id/student number
8334:
8335: =item B<$umode> Students authentication mode
8336:
8337: =item B<$upass> Students password
8338:
8339: =item B<$first> Students first name
8340:
8341: =item B<$middle> Students middle name
8342:
8343: =item B<$last> Students last name
8344:
8345: =item B<$gene> Students generation
8346:
8347: =item B<$usec> Students section in course
8348:
8349: =item B<$end> Unix time of the roles expiration
8350:
8351: =item B<$start> Unix time of the roles start date
8352:
8353: =item B<$forceid> If defined, allow $uid to be changed
8354:
8355: =item B<$desiredhome> server to use as home server for student
8356:
8357: =back
8358:
8359: =item *
8360:
8361: modify_student_enrollment
8362:
8363: Change a students enrollment status in a class. The environment variable
8364: 'role.request.course' must be defined for this function to proceed.
8365:
8366: Inputs:
8367:
8368: =over 4
8369:
8370: =item $udom, students domain
8371:
8372: =item $uname, students name
8373:
8374: =item $uid, students user id
8375:
8376: =item $first, students first name
8377:
8378: =item $middle
8379:
8380: =item $last
8381:
8382: =item $gene
8383:
8384: =item $usec
8385:
8386: =item $end
8387:
8388: =item $start
8389:
8390: =back
8391:
8392:
8393: =item *
8394:
8395: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
8396: custom role; give a custom role to a user for the level given by URL. Specify
8397: name and domain of role author, and role name
8398:
8399: =item *
8400:
8401: revokerole($udom,$uname,$url,$role) : revoke a role for url
8402:
8403: =item *
8404:
8405: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
8406:
8407: =back
8408:
8409: =head2 Course Infomation
8410:
8411: =over 4
8412:
8413: =item *
8414:
8415: coursedescription($courseid) : returns a hash of information about the
8416: specified course id, including all environment settings for the
8417: course, the description of the course will be in the hash under the
8418: key 'description'
8419:
8420: =item *
8421:
8422: resdata($name,$domain,$type,@which) : request for current parameter
8423: setting for a specific $type, where $type is either 'course' or 'user',
8424: @what should be a list of parameters to ask about. This routine caches
8425: answers for 5 minutes.
8426:
8427: =back
8428:
8429: =head2 Course Modification
8430:
8431: =over 4
8432:
8433: =item *
8434:
8435: writecoursepref($courseid,%prefs) : write preferences (environment
8436: database) for a course
8437:
8438: =item *
8439:
8440: createcourse($udom,$description,$url) : make/modify course
8441:
8442: =back
8443:
8444: =head2 Resource Subroutines
8445:
8446: =over 4
8447:
8448: =item *
8449:
8450: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
8451:
8452: =item *
8453:
8454: repcopy($filename) : subscribes to the requested file, and attempts to
8455: replicate from the owning library server, Might return
8456: 'unavailable', 'not_found', 'forbidden', 'ok', or
8457: 'bad_request', also attempts to grab the metadata for the
8458: resource. Expects the local filesystem pathname
8459: (/home/httpd/html/res/....)
8460:
8461: =back
8462:
8463: =head2 Resource Information
8464:
8465: =over 4
8466:
8467: =item *
8468:
8469: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
8470: a vairety of different possible values, $varname should be a request
8471: string, and the other parameters can be used to specify who and what
8472: one is asking about.
8473:
8474: Possible values for $varname are environment.lastname (or other item
8475: from the envirnment hash), user.name (or someother aspect about the
8476: user), resource.0.maxtries (or some other part and parameter of a
8477: resource)
8478:
8479: =item *
8480:
8481: directcondval($number) : get current value of a condition; reads from a state
8482: string
8483:
8484: =item *
8485:
8486: condval($condidx) : value of condition index based on state
8487:
8488: =item *
8489:
8490: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
8491: resource's metadata, $what should be either a specific key, or either
8492: 'keys' (to get a list of possible keys) or 'packages' to get a list of
8493: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
8494:
8495: this function automatically caches all requests
8496:
8497: =item *
8498:
8499: metadata_query($query,$custom,$customshow) : make a metadata query against the
8500: network of library servers; returns file handle of where SQL and regex results
8501: will be stored for query
8502:
8503: =item *
8504:
8505: symbread($filename) : return symbolic list entry (filename argument optional);
8506: returns the data handle
8507:
8508: =item *
8509:
8510: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
8511: a possible symb for the URL in $thisfn, and if is an encryypted
8512: resource that the user accessed using /enc/ returns a 1 on success, 0
8513: on failure, user must be in a course, as it assumes the existance of
8514: the course initial hash, and uses $env('request.course.id'}
8515:
8516:
8517: =item *
8518:
8519: symbclean($symb) : removes versions numbers from a symb, returns the
8520: cleaned symb
8521:
8522: =item *
8523:
8524: is_on_map($uri) : checks if the $uri is somewhere on the current
8525: course map, user must be in a course for it to work.
8526:
8527: =item *
8528:
8529: numval($salt) : return random seed value (addend for rndseed)
8530:
8531: =item *
8532:
8533: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
8534: a random seed, all arguments are optional, if they aren't sent it uses the
8535: environment to derive them. Note: if symb isn't sent and it can't get one
8536: from &symbread it will use the current time as its return value
8537:
8538: =item *
8539:
8540: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
8541: unfakeable, receipt
8542:
8543: =item *
8544:
8545: receipt() : API to ireceipt working off of env values; given out to users
8546:
8547: =item *
8548:
8549: countacc($url) : count the number of accesses to a given URL
8550:
8551: =item *
8552:
8553: 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
8554:
8555: =item *
8556:
8557: 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)
8558:
8559: =item *
8560:
8561: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
8562:
8563: =item *
8564:
8565: devalidate($symb) : devalidate temporary spreadsheet calculations,
8566: forcing spreadsheet to reevaluate the resource scores next time.
8567:
8568: =back
8569:
8570: =head2 Storing/Retreiving Data
8571:
8572: =over 4
8573:
8574: =item *
8575:
8576: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
8577: for this url; hashref needs to be given and should be a \%hashname; the
8578: remaining args aren't required and if they aren't passed or are '' they will
8579: be derived from the env
8580:
8581: =item *
8582:
8583: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
8584: uses critical subroutine
8585:
8586: =item *
8587:
8588: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
8589: all args are optional
8590:
8591: =item *
8592:
8593: dumpstore($namespace,$udom,$uname,$regexp,$range) :
8594: dumps the complete (or key matching regexp) namespace into a hash
8595: ($udom, $uname, $regexp, $range are optional) for a namespace that is
8596: normally &store()ed into
8597:
8598: $range should be either an integer '100' (give me the first 100
8599: matching records)
8600: or be two integers sperated by a - with no spaces
8601: '30-50' (give me the 30th through the 50th matching
8602: records)
8603:
8604:
8605: =item *
8606:
8607: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
8608: replaces a &store() version of data with a replacement set of data
8609: for a particular resource in a namespace passed in the $storehash hash
8610: reference
8611:
8612: =item *
8613:
8614: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
8615: works very similar to store/cstore, but all data is stored in a
8616: temporary location and can be reset using tmpreset, $storehash should
8617: be a hash reference, returns nothing on success
8618:
8619: =item *
8620:
8621: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
8622: similar to restore, but all data is stored in a temporary location and
8623: can be reset using tmpreset. Returns a hash of values on success,
8624: error string otherwise.
8625:
8626: =item *
8627:
8628: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
8629: deltes all keys for $symb form the temporary storage hash.
8630:
8631: =item *
8632:
8633: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
8634: reference filled in from namesp ($udom and $uname are optional)
8635:
8636: =item *
8637:
8638: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
8639: namesp ($udom and $uname are optional)
8640:
8641: =item *
8642:
8643: dump($namespace,$udom,$uname,$regexp,$range) :
8644: dumps the complete (or key matching regexp) namespace into a hash
8645: ($udom, $uname, $regexp, $range are optional)
8646:
8647: $range should be either an integer '100' (give me the first 100
8648: matching records)
8649: or be two integers sperated by a - with no spaces
8650: '30-50' (give me the 30th through the 50th matching
8651: records)
8652: =item *
8653:
8654: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
8655: $store can be a scalar, an array reference, or if the amount to be
8656: incremented is > 1, a hash reference.
8657:
8658: ($udom and $uname are optional)
8659:
8660: =item *
8661:
8662: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
8663: ($udom and $uname are optional)
8664:
8665: =item *
8666:
8667: cput($namespace,$storehash,$udom,$uname) : critical put
8668: ($udom and $uname are optional)
8669:
8670: =item *
8671:
8672: newput($namespace,$storehash,$udom,$uname) :
8673:
8674: Attempts to store the items in the $storehash, but only if they don't
8675: currently exist, if this succeeds you can be certain that you have
8676: successfully created a new key value pair in the $namespace db.
8677:
8678:
8679: Args:
8680: $namespace: name of database to store values to
8681: $storehash: hashref to store to the db
8682: $udom: (optional) domain of user containing the db
8683: $uname: (optional) name of user caontaining the db
8684:
8685: Returns:
8686: 'ok' -> succeeded in storing all keys of $storehash
8687: 'key_exists: <key>' -> failed to anything out of $storehash, as at
8688: least <key> already existed in the db (other
8689: requested keys may also already exist)
8690: 'error: <msg>' -> unable to tie the DB or other erorr occured
8691: 'con_lost' -> unable to contact request server
8692: 'refused' -> action was not allowed by remote machine
8693:
8694:
8695: =item *
8696:
8697: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
8698: reference filled in from namesp (encrypts the return communication)
8699: ($udom and $uname are optional)
8700:
8701: =item *
8702:
8703: log($udom,$name,$home,$message) : write to permanent log for user; use
8704: critical subroutine
8705:
8706: =item *
8707:
8708: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
8709: array reference filled in from namespace found in domain level on either
8710: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
8711:
8712: =item *
8713:
8714: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
8715: domain level either on specified domain server ($uhome) or primary domain
8716: server ($udom and $uhome are optional)
8717:
8718: =back
8719:
8720: =head2 Network Status Functions
8721:
8722: =over 4
8723:
8724: =item *
8725:
8726: dirlist($uri) : return directory list based on URI
8727:
8728: =item *
8729:
8730: spareserver() : find server with least workload from spare.tab
8731:
8732: =back
8733:
8734: =head2 Apache Request
8735:
8736: =over 4
8737:
8738: =item *
8739:
8740: ssi($url,%hash) : server side include, does a complete request cycle on url to
8741: localhost, posts hash
8742:
8743: =back
8744:
8745: =head2 Data to String to Data
8746:
8747: =over 4
8748:
8749: =item *
8750:
8751: hash2str(%hash) : convert a hash into a string complete with escaping and '='
8752: and '&' separators, supports elements that are arrayrefs and hashrefs
8753:
8754: =item *
8755:
8756: hashref2str($hashref) : convert a hashref into a string complete with
8757: escaping and '=' and '&' separators, supports elements that are
8758: arrayrefs and hashrefs
8759:
8760: =item *
8761:
8762: arrayref2str($arrayref) : convert an arrayref into a string complete
8763: with escaping and '&' separators, supports elements that are arrayrefs
8764: and hashrefs
8765:
8766: =item *
8767:
8768: str2hash($string) : convert string to hash using unescaping and
8769: splitting on '=' and '&', supports elements that are arrayrefs and
8770: hashrefs
8771:
8772: =item *
8773:
8774: str2array($string) : convert string to hash using unescaping and
8775: splitting on '&', supports elements that are arrayrefs and hashrefs
8776:
8777: =back
8778:
8779: =head2 Logging Routines
8780:
8781: =over 4
8782:
8783: These routines allow one to make log messages in the lonnet.log and
8784: lonnet.perm logfiles.
8785:
8786: =item *
8787:
8788: logtouch() : make sure the logfile, lonnet.log, exists
8789:
8790: =item *
8791:
8792: logthis() : append message to the normal lonnet.log file, it gets
8793: preiodically rolled over and deleted.
8794:
8795: =item *
8796:
8797: logperm() : append a permanent message to lonnet.perm.log, this log
8798: file never gets deleted by any automated portion of the system, only
8799: messages of critical importance should go in here.
8800:
8801: =back
8802:
8803: =head2 General File Helper Routines
8804:
8805: =over 4
8806:
8807: =item *
8808:
8809: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
8810: (a) files in /uploaded
8811: (i) If a local copy of the file exists -
8812: compares modification date of local copy with last-modified date for
8813: definitive version stored on home server for course. If local copy is
8814: stale, requests a new version from the home server and stores it.
8815: If the original has been removed from the home server, then local copy
8816: is unlinked.
8817: (ii) If local copy does not exist -
8818: requests the file from the home server and stores it.
8819:
8820: If $caller is 'uploadrep':
8821: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
8822: for request for files originally uploaded via DOCS.
8823: - returns 'ok' if fresh local copy now available, -1 otherwise.
8824:
8825: Otherwise:
8826: This indicates a call from the content generation phase of the request.
8827: - returns the entire contents of the file or -1.
8828:
8829: (b) files in /res
8830: - returns the entire contents of a file or -1;
8831: it properly subscribes to and replicates the file if neccessary.
8832:
8833:
8834: =item *
8835:
8836: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
8837: reference
8838:
8839: returns either a stat() list of data about the file or an empty list
8840: if the file doesn't exist or couldn't find out about it (connection
8841: problems or user unknown)
8842:
8843: =item *
8844:
8845: filelocation($dir,$file) : returns file system location of a file
8846: based on URI; meant to be "fairly clean" absolute reference, $dir is a
8847: directory that relative $file lookups are to looked in ($dir of /a/dir
8848: and a file of ../bob will become /a/bob)
8849:
8850: =item *
8851:
8852: hreflocation($dir,$file) : returns file system location or a URL; same as
8853: filelocation except for hrefs
8854:
8855: =item *
8856:
8857: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
8858:
8859: =back
8860:
8861: =head2 Usererfile file routines (/uploaded*)
8862:
8863: =over 4
8864:
8865: =item *
8866:
8867: userfileupload(): main rotine for putting a file in a user or course's
8868: filespace, arguments are,
8869:
8870: formname - required - this is the name of the element in $env where the
8871: filename, and the contents of the file to create/modifed exist
8872: the filename is in $env{'form.'.$formname.'.filename'} and the
8873: contents of the file is located in $env{'form.'.$formname}
8874: coursedoc - if true, store the file in the course of the active role
8875: of the current user
8876: subdir - required - subdirectory to put the file in under ../userfiles/
8877: if undefined, it will be placed in "unknown"
8878:
8879: (This routine calls clean_filename() to remove any dangerous
8880: characters from the filename, and then calls finuserfileupload() to
8881: complete the transaction)
8882:
8883: returns either the url of the uploaded file (/uploaded/....) if successful
8884: and /adm/notfound.html if unsuccessful
8885:
8886: =item *
8887:
8888: clean_filename(): routine for cleaing a filename up for storage in
8889: userfile space, argument is:
8890:
8891: filename - proposed filename
8892:
8893: returns: the new clean filename
8894:
8895: =item *
8896:
8897: finishuserfileupload(): routine that creaes and sends the file to
8898: userspace, probably shouldn't be called directly
8899:
8900: docuname: username or courseid of destination for the file
8901: docudom: domain of user/course of destination for the file
8902: formname: same as for userfileupload()
8903: fname: filename (inculding subdirectories) for the file
8904:
8905: returns either the url of the uploaded file (/uploaded/....) if successful
8906: and /adm/notfound.html if unsuccessful
8907:
8908: =item *
8909:
8910: renameuserfile(): renames an existing userfile to a new name
8911:
8912: Args:
8913: docuname: username or courseid of destination for the file
8914: docudom: domain of user/course of destination for the file
8915: old: current file name (including any subdirs under userfiles)
8916: new: desired file name (including any subdirs under userfiles)
8917:
8918: =item *
8919:
8920: mkdiruserfile(): creates a directory is a userfiles dir
8921:
8922: Args:
8923: docuname: username or courseid of destination for the file
8924: docudom: domain of user/course of destination for the file
8925: dir: dir to create (including any subdirs under userfiles)
8926:
8927: =item *
8928:
8929: removeuserfile(): removes a file that exists in userfiles
8930:
8931: Args:
8932: docuname: username or courseid of destination for the file
8933: docudom: domain of user/course of destination for the file
8934: fname: filname to delete (including any subdirs under userfiles)
8935:
8936: =item *
8937:
8938: removeuploadedurl(): convience function for removeuserfile()
8939:
8940: Args:
8941: url: a full /uploaded/... url to delete
8942:
8943: =item *
8944:
8945: get_portfile_permissions():
8946: Args:
8947: domain: domain of user or course contain the portfolio files
8948: user: name of user or num of course contain the portfolio files
8949: Returns:
8950: hashref of a dump of the proper file_permissions.db
8951:
8952:
8953: =item *
8954:
8955: get_access_controls():
8956:
8957: Args:
8958: current_permissions: the hash ref returned from get_portfile_permissions()
8959: group: (optional) the group you want the files associated with
8960: file: (optional) the file you want access info on
8961:
8962: Returns:
8963: a hash (keys are file names) of hashes containing
8964: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
8965: values are XML containing access control settings (see below)
8966:
8967: Internal notes:
8968:
8969: access controls are stored in file_permissions.db as key=value pairs.
8970: key -> path to file/file_name\0uniqueID:scope_end_start
8971: where scope -> public,guest,course,group,domains or users.
8972: end -> UNIX time for end of access (0 -> no end date)
8973: start -> UNIX time for start of access
8974:
8975: value -> XML description of access control
8976: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
8977: <start></start>
8978: <end></end>
8979:
8980: <password></password> for scope type = guest
8981:
8982: <domain></domain> for scope type = course or group
8983: <number></number>
8984: <roles id="">
8985: <role></role>
8986: <access></access>
8987: <section></section>
8988: <group></group>
8989: </roles>
8990:
8991: <dom></dom> for scope type = domains
8992:
8993: <users> for scope type = users
8994: <user>
8995: <uname></uname>
8996: <udom></udom>
8997: </user>
8998: </users>
8999: </scope>
9000:
9001: Access data is also aggregated for each file in an additional key=value pair:
9002: key -> path to file/file_name\0accesscontrol
9003: value -> reference to hash
9004: hash contains key = value pairs
9005: where key = uniqueID:scope_end_start
9006: value = UNIX time record was last updated
9007:
9008: Used to improve speed of look-ups of access controls for each file.
9009:
9010: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
9011:
9012: modify_access_controls():
9013:
9014: Modifies access controls for a portfolio file
9015: Args
9016: 1. file name
9017: 2. reference to hash of required changes,
9018: 3. domain
9019: 4. username
9020: where domain,username are the domain of the portfolio owner
9021: (either a user or a course)
9022:
9023: Returns:
9024: 1. result of additions or updates ('ok' or 'error', with error message).
9025: 2. result of deletions ('ok' or 'error', with error message).
9026: 3. reference to hash of any new or updated access controls.
9027: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
9028: key = integer (inbound ID)
9029: value = uniqueID
9030:
9031: =back
9032:
9033: =head2 HTTP Helper Routines
9034:
9035: =over 4
9036:
9037: =item *
9038:
9039: escape() : unpack non-word characters into CGI-compatible hex codes
9040:
9041: =item *
9042:
9043: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
9044:
9045: =back
9046:
9047: =head1 PRIVATE SUBROUTINES
9048:
9049: =head2 Underlying communication routines (Shouldn't call)
9050:
9051: =over 4
9052:
9053: =item *
9054:
9055: subreply() : tries to pass a message to lonc, returns con_lost if incapable
9056:
9057: =item *
9058:
9059: reply() : uses subreply to send a message to remote machine, logs all failures
9060:
9061: =item *
9062:
9063: critical() : passes a critical message to another server; if cannot
9064: get through then place message in connection buffer directory and
9065: returns con_delayed, if incapable of saving message, returns
9066: con_failed
9067:
9068: =item *
9069:
9070: reconlonc() : tries to reconnect lonc client processes.
9071:
9072: =back
9073:
9074: =head2 Resource Access Logging
9075:
9076: =over 4
9077:
9078: =item *
9079:
9080: flushcourselogs() : flush (save) buffer logs and access logs
9081:
9082: =item *
9083:
9084: courselog($what) : save message for course in hash
9085:
9086: =item *
9087:
9088: courseacclog($what) : save message for course using &courselog(). Perform
9089: special processing for specific resource types (problems, exams, quizzes, etc).
9090:
9091: =item *
9092:
9093: goodbye() : flush course logs and log shutting down; it is called in srm.conf
9094: as a PerlChildExitHandler
9095:
9096: =back
9097:
9098: =head2 Other
9099:
9100: =over 4
9101:
9102: =item *
9103:
9104: symblist($mapname,%newhash) : update symbolic storage links
9105:
9106: =back
9107:
9108: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>