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