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