Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.719
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.719 ! banghart 4: # $Id: lonnet.pm,v 1.718 2006/03/05 01:54:50 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.685 raeburn 43: %domain_lang_def %domain_city %domain_longi %domain_lati %domain_primary
44: $tmpdir $_64bit %env);
1.403 www 45:
1.1 albertel 46: use IO::Socket;
1.31 www 47: use GDBM_File;
1.8 www 48: use Apache::Constants qw(:common :http);
1.208 albertel 49: use HTML::LCParser;
1.637 raeburn 50: use HTML::Parser;
1.88 www 51: use Fcntl qw(:flock);
1.557 albertel 52: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
1.539 albertel 53: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 54: use Cache::Memcached;
1.676 albertel 55: use Digest::MD5;
56:
1.195 www 57: my $readit;
1.550 foxr 58: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 59:
1.619 albertel 60: require Exporter;
61:
62: our @ISA = qw (Exporter);
63: our @EXPORT = qw(%env);
64:
1.449 matthew 65: =pod
66:
67: =head1 Package Variables
68:
69: These are largely undocumented, so if you decipher one please note it here.
70:
71: =over 4
72:
73: =item $processmarker
74:
75: Contains the time this process was started and this servers host id.
76:
77: =item $dumpcount
78:
79: Counts the number of times a message log flush has been attempted (regardless
80: of success) by this process. Used as part of the filename when messages are
81: delayed.
82:
83: =back
84:
85: =cut
86:
87:
1.1 albertel 88: # --------------------------------------------------------------------- Logging
89:
1.163 harris41 90: sub logtouch {
91: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 92: unless (-e "$execdir/logs/lonnet.log") {
93: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 94: close $fh;
95: }
96: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
97: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
98: }
99:
1.1 albertel 100: sub logthis {
101: my $message=shift;
102: my $execdir=$perlvar{'lonDaemons'};
103: my $now=time;
104: my $local=localtime($now);
1.448 albertel 105: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
106: print $fh "$local ($$): $message\n";
107: close($fh);
108: }
1.1 albertel 109: return 1;
110: }
111:
112: sub logperm {
113: my $message=shift;
114: my $execdir=$perlvar{'lonDaemons'};
115: my $now=time;
116: my $local=localtime($now);
1.448 albertel 117: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
118: print $fh "$now:$message:$local\n";
119: close($fh);
120: }
1.1 albertel 121: return 1;
122: }
123:
124: # -------------------------------------------------- Non-critical communication
125: sub subreply {
126: my ($cmd,$server)=@_;
1.704 albertel 127: my $peerfile="$perlvar{'lonSockDir'}/".$hostname{$server};
1.549 foxr 128: #
129: # With loncnew process trimming, there's a timing hole between lonc server
130: # process exit and the master server picking up the listen on the AF_UNIX
131: # socket. In that time interval, a lock file will exist:
132:
133: my $lockfile=$peerfile.".lock";
134: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
135: sleep(1);
136: }
137: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 138: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 139: #
1.550 foxr 140: # We'll give the connection a few tries before abandoning it. If
141: # connection is not possible, we'll con_lost back to the client.
142: #
143: my $client;
144: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
145: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
146: Type => SOCK_STREAM,
147: Timeout => 10);
148: if($client) {
149: last; # Connected!
150: }
151: sleep(1); # Try again later if failed connection.
152: }
153: my $answer;
154: if ($client) {
1.704 albertel 155: print $client "sethost:$server:$cmd\n";
1.550 foxr 156: $answer=<$client>;
157: if (!$answer) { $answer="con_lost"; }
158: chomp($answer);
159: } else {
160: $answer = 'con_lost'; # Failed connection.
161: }
1.1 albertel 162: return $answer;
163: }
164:
165: sub reply {
166: my ($cmd,$server)=@_;
1.205 www 167: unless (defined($hostname{$server})) { return 'no_such_host'; }
1.1 albertel 168: my $answer=subreply($cmd,$server);
1.65 www 169: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 170: &logthis("<font color=\"blue\">WARNING:".
1.12 www 171: " $cmd to $server returned $answer</font>");
172: }
1.1 albertel 173: return $answer;
174: }
175:
176: # ----------------------------------------------------------- Send USR1 to lonc
177:
178: sub reconlonc {
179: my $peerfile=shift;
180: &logthis("Trying to reconnect for $peerfile");
181: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 182: if (open(my $fh,"<$loncfile")) {
1.1 albertel 183: my $loncpid=<$fh>;
184: chomp($loncpid);
185: if (kill 0 => $loncpid) {
186: &logthis("lonc at pid $loncpid responding, sending USR1");
187: kill USR1 => $loncpid;
188: sleep 1;
189: if (-e "$peerfile") { return; }
190: &logthis("$peerfile still not there, give it another try");
191: sleep 5;
192: if (-e "$peerfile") { return; }
1.12 www 193: &logthis(
1.672 albertel 194: "<font color=\"blue\">WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 195: } else {
1.12 www 196: &logthis(
1.672 albertel 197: "<font color=\"blue\">WARNING:".
1.12 www 198: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 199: }
200: } else {
1.672 albertel 201: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 202: }
203: }
204:
205: # ------------------------------------------------------ Critical communication
1.12 www 206:
1.1 albertel 207: sub critical {
208: my ($cmd,$server)=@_;
1.89 www 209: unless ($hostname{$server}) {
1.672 albertel 210: &logthis("<font color=\"blue\">WARNING:".
1.89 www 211: " Critical message to unknown server ($server)</font>");
212: return 'no_such_host';
213: }
1.1 albertel 214: my $answer=reply($cmd,$server);
215: if ($answer eq 'con_lost') {
216: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 217: my $answer=reply($cmd,$server);
1.1 albertel 218: if ($answer eq 'con_lost') {
219: my $now=time;
220: my $middlename=$cmd;
1.5 www 221: $middlename=substr($middlename,0,16);
1.1 albertel 222: $middlename=~s/\W//g;
223: my $dfilename=
1.305 www 224: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
225: $dumpcount++;
1.1 albertel 226: {
1.448 albertel 227: my $dfh;
228: if (open($dfh,">$dfilename")) {
229: print $dfh "$cmd\n";
230: close($dfh);
231: }
1.1 albertel 232: }
233: sleep 2;
234: my $wcmd='';
235: {
1.448 albertel 236: my $dfh;
237: if (open($dfh,"<$dfilename")) {
238: $wcmd=<$dfh>;
239: close($dfh);
240: }
1.1 albertel 241: }
242: chomp($wcmd);
1.7 www 243: if ($wcmd eq $cmd) {
1.672 albertel 244: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 245: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 246: &logperm("D:$server:$cmd");
247: return 'con_delayed';
248: } else {
1.672 albertel 249: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 250: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 251: &logperm("F:$server:$cmd");
252: return 'con_failed';
253: }
254: }
255: }
256: return $answer;
1.405 albertel 257: }
258:
1.374 www 259: # ------------------------------------------- Transfer profile into environment
260:
261: sub transfer_profile_to_env {
262: my ($lonidsdir,$handle)=@_;
263: my @profile;
264: {
1.448 albertel 265: open(my $idf,"$lonidsdir/$handle.id");
1.374 www 266: flock($idf,LOCK_SH);
267: @profile=<$idf>;
1.448 albertel 268: close($idf);
1.374 www 269: }
270: my $envi;
1.433 matthew 271: my %Remove;
1.374 www 272: for ($envi=0;$envi<=$#profile;$envi++) {
273: chomp($profile[$envi]);
1.690 albertel 274: my ($envname,$envvalue)=split(/=/,$profile[$envi],2);
1.619 albertel 275: $env{$envname} = $envvalue;
1.433 matthew 276: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
277: if ($time < time-300) {
278: $Remove{$key}++;
279: }
280: }
281: }
1.619 albertel 282: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.433 matthew 283: foreach my $expired_key (keys(%Remove)) {
284: &delenv($expired_key);
1.374 www 285: }
1.1 albertel 286: }
287:
1.5 www 288: # ---------------------------------------------------------- Append Environment
289:
290: sub appenv {
1.6 www 291: my %newenv=@_;
1.692 albertel 292: foreach my $key (keys(%newenv)) {
293: if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
1.672 albertel 294: &logthis("<font color=\"blue\">WARNING: ".
1.692 albertel 295: "Attempt to modify environment ".$key." to ".$newenv{$key}
1.151 www 296: .'</font>');
1.692 albertel 297: delete($newenv{$key});
1.35 www 298: } else {
1.692 albertel 299: $env{$key}=$newenv{$key};
1.35 www 300: }
1.191 harris41 301: }
1.95 www 302:
303: my $lockfh;
1.620 albertel 304: unless (open($lockfh,"$env{'user.environment'}")) {
1.448 albertel 305: return 'error: '.$!;
1.95 www 306: }
307: unless (flock($lockfh,LOCK_EX)) {
1.672 albertel 308: &logthis("<font color=\"blue\">WARNING: ".
1.95 www 309: 'Could not obtain exclusive lock in appenv: '.$!);
1.448 albertel 310: close($lockfh);
1.95 www 311: return 'error: '.$!;
312: }
313:
1.6 www 314: my @oldenv;
315: {
1.448 albertel 316: my $fh;
1.620 albertel 317: unless (open($fh,"$env{'user.environment'}")) {
1.448 albertel 318: return 'error: '.$!;
319: }
320: @oldenv=<$fh>;
321: close($fh);
1.6 www 322: }
323: for (my $i=0; $i<=$#oldenv; $i++) {
324: chomp($oldenv[$i]);
1.9 www 325: if ($oldenv[$i] ne '') {
1.690 albertel 326: my ($name,$value)=split(/=/,$oldenv[$i],2);
1.448 albertel 327: unless (defined($newenv{$name})) {
328: $newenv{$name}=$value;
329: }
1.9 www 330: }
1.6 www 331: }
332: {
1.448 albertel 333: my $fh;
1.620 albertel 334: unless (open($fh,">$env{'user.environment'}")) {
1.448 albertel 335: return 'error';
336: }
337: my $newname;
338: foreach $newname (keys %newenv) {
339: print $fh "$newname=$newenv{$newname}\n";
340: }
341: close($fh);
1.56 www 342: }
1.448 albertel 343:
344: close($lockfh);
1.56 www 345: return 'ok';
346: }
347: # ----------------------------------------------------- Delete from Environment
348:
349: sub delenv {
350: my $delthis=shift;
351: my %newenv=();
352: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 353: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 354: "Attempt to delete from environment ".$delthis);
355: return 'error';
356: }
357: my @oldenv;
358: {
1.448 albertel 359: my $fh;
1.620 albertel 360: unless (open($fh,"$env{'user.environment'}")) {
1.448 albertel 361: return 'error';
362: }
363: unless (flock($fh,LOCK_SH)) {
1.672 albertel 364: &logthis("<font color=\"blue\">WARNING: ".
1.448 albertel 365: 'Could not obtain shared lock in delenv: '.$!);
366: close($fh);
367: return 'error: '.$!;
368: }
369: @oldenv=<$fh>;
370: close($fh);
1.56 www 371: }
372: {
1.448 albertel 373: my $fh;
1.620 albertel 374: unless (open($fh,">$env{'user.environment'}")) {
1.448 albertel 375: return 'error';
376: }
377: unless (flock($fh,LOCK_EX)) {
1.672 albertel 378: &logthis("<font color=\"blue\">WARNING: ".
1.448 albertel 379: 'Could not obtain exclusive lock in delenv: '.$!);
380: close($fh);
381: return 'error: '.$!;
382: }
1.692 albertel 383: foreach my $cur_key (@oldenv) {
384: if ($cur_key=~/^$delthis/) {
385: my ($key,undef) = split('=',$cur_key,2);
1.619 albertel 386: delete($env{$key});
1.473 matthew 387: } else {
1.692 albertel 388: print $fh $cur_key;
1.473 matthew 389: }
1.448 albertel 390: }
391: close($fh);
1.5 www 392: }
393: return 'ok';
1.369 albertel 394: }
395:
396: # ------------------------------------------ Find out current server userload
397: # there is a copy in lond
398: sub userload {
399: my $numusers=0;
400: {
401: opendir(LONIDS,$perlvar{'lonIDsDir'});
402: my $filename;
403: my $curtime=time;
404: while ($filename=readdir(LONIDS)) {
405: if ($filename eq '.' || $filename eq '..') {next;}
1.404 albertel 406: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 407: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 408: }
409: closedir(LONIDS);
410: }
411: my $userloadpercent=0;
412: my $maxuserload=$perlvar{'lonUserLoadLim'};
413: if ($maxuserload) {
1.371 albertel 414: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 415: }
1.372 albertel 416: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 417: return $userloadpercent;
1.283 www 418: }
419:
420: # ------------------------------------------ Fight off request when overloaded
421:
422: sub overloaderror {
423: my ($r,$checkserver)=@_;
424: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
425: my $loadavg;
426: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 427: open(my $loadfile,'/proc/loadavg');
1.283 www 428: $loadavg=<$loadfile>;
429: $loadavg =~ s/\s.*//g;
1.285 matthew 430: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 431: close($loadfile);
1.283 www 432: } else {
433: $loadavg=&reply('load',$checkserver);
434: }
1.285 matthew 435: my $overload=$loadavg-100;
1.283 www 436: if ($overload>0) {
1.285 matthew 437: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 438: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 439: return 413;
1.283 www 440: }
441: return '';
1.5 www 442: }
1.1 albertel 443:
444: # ------------------------------ Find server with least workload from spare.tab
1.11 www 445:
1.1 albertel 446: sub spareserver {
1.670 albertel 447: my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.1 albertel 448: my $tryserver;
449: my $spareserver='';
1.370 albertel 450: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
451: my $lowestserver=$loadpercent > $userloadpercent?
452: $loadpercent : $userloadpercent;
1.670 albertel 453: foreach $tryserver (keys(%spareid)) {
454: my $loadans=&reply('load',$tryserver);
455: my $userloadans=&reply('userload',$tryserver);
1.411 albertel 456: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
457: next; #didn't get a number from the server
458: }
459: my $answer;
460: if ($loadans =~ /\d/) {
461: if ($userloadans =~ /\d/) {
462: #both are numbers, pick the bigger one
463: $answer=$loadans > $userloadans?
464: $loadans : $userloadans;
465: } else {
466: $answer = $loadans;
467: }
468: } else {
469: $answer = $userloadans;
470: }
471: if (($answer =~ /\d/) && ($answer<$lowestserver)) {
1.670 albertel 472: if ($want_server_name) {
473: $spareserver=$tryserver;
474: } else {
475: $spareserver="http://$hostname{$tryserver}";
476: }
1.411 albertel 477: $lowestserver=$answer;
478: }
1.370 albertel 479: }
1.1 albertel 480: return $spareserver;
1.202 matthew 481: }
482:
483: # --------------------------------------------- Try to change a user's password
484:
485: sub changepass {
486: my ($uname,$udom,$currentpass,$newpass,$server)=@_;
487: $currentpass = &escape($currentpass);
488: $newpass = &escape($newpass);
489: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
490: $server);
491: if (! $answer) {
492: &logthis("No reply on password change request to $server ".
493: "by $uname in domain $udom.");
494: } elsif ($answer =~ "^ok") {
495: &logthis("$uname in $udom successfully changed their password ".
496: "on $server.");
497: } elsif ($answer =~ "^pwchange_failure") {
498: &logthis("$uname in $udom was unable to change their password ".
499: "on $server. The action was blocked by either lcpasswd ".
500: "or pwchange");
501: } elsif ($answer =~ "^non_authorized") {
502: &logthis("$uname in $udom did not get their password correct when ".
503: "attempting to change it on $server.");
504: } elsif ($answer =~ "^auth_mode_error") {
505: &logthis("$uname in $udom attempted to change their password despite ".
506: "not being locally or internally authenticated on $server.");
507: } elsif ($answer =~ "^unknown_user") {
508: &logthis("$uname in $udom attempted to change their password ".
509: "on $server but were unable to because $server is not ".
510: "their home server.");
511: } elsif ($answer =~ "^refused") {
512: &logthis("$server refused to change $uname in $udom password because ".
513: "it was sent an unencrypted request to change the password.");
514: }
515: return $answer;
1.1 albertel 516: }
517:
1.169 harris41 518: # ----------------------- Try to determine user's current authentication scheme
519:
520: sub queryauthenticate {
521: my ($uname,$udom)=@_;
1.456 albertel 522: my $uhome=&homeserver($uname,$udom);
523: if (!$uhome) {
524: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
525: return 'no_host';
526: }
527: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
528: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
529: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 530: }
1.456 albertel 531: return $answer;
1.169 harris41 532: }
533:
1.1 albertel 534: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 535:
1.1 albertel 536: sub authenticate {
537: my ($uname,$upass,$udom)=@_;
1.12 www 538: $upass=escape($upass);
1.199 www 539: $uname=~s/\W//g;
1.471 albertel 540: my $uhome=&homeserver($uname,$udom);
541: if (!$uhome) {
542: &logthis("User $uname at $udom is unknown in authenticate");
543: return 'no_host';
1.1 albertel 544: }
1.471 albertel 545: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
546: if ($answer eq 'authorized') {
547: &logthis("User $uname at $udom authorized by $uhome");
548: return $uhome;
549: }
550: if ($answer eq 'non_authorized') {
551: &logthis("User $uname at $udom rejected by $uhome");
552: return 'no_host';
1.9 www 553: }
1.471 albertel 554: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 555: return 'no_host';
556: }
557:
558: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 559:
1.599 albertel 560: my %homecache;
1.1 albertel 561: sub homeserver {
1.230 stredwic 562: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 563: my $index="$uname:$udom";
1.426 albertel 564:
1.599 albertel 565: if (exists($homecache{$index})) { return $homecache{$index}; }
1.1 albertel 566: my $tryserver;
567: foreach $tryserver (keys %libserv) {
1.230 stredwic 568: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 569: exists($badServerCache{$tryserver}));
1.1 albertel 570: if ($hostdom{$tryserver} eq $udom) {
571: my $answer=reply("home:$udom:$uname",$tryserver);
572: if ($answer eq 'found') {
1.599 albertel 573: return $homecache{$index}=$tryserver;
1.231 stredwic 574: } elsif ($answer eq 'no_host') {
575: $badServerCache{$tryserver}=1;
1.221 matthew 576: }
1.1 albertel 577: }
578: }
579: return 'no_host';
1.70 www 580: }
581:
582: # ------------------------------------- Find the usernames behind a list of IDs
583:
584: sub idget {
585: my ($udom,@ids)=@_;
586: my %returnhash=();
587:
588: my $tryserver;
589: foreach $tryserver (keys %libserv) {
590: if ($hostdom{$tryserver} eq $udom) {
591: my $idlist=join('&',@ids);
592: $idlist=~tr/A-Z/a-z/;
593: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
594: my @answer=();
1.76 www 595: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1.70 www 596: @answer=split(/\&/,$reply);
597: } ;
598: my $i;
599: for ($i=0;$i<=$#ids;$i++) {
600: if ($answer[$i]) {
601: $returnhash{$ids[$i]}=$answer[$i];
602: }
603: }
604: }
605: }
606: return %returnhash;
607: }
608:
609: # ------------------------------------- Find the IDs behind a list of usernames
610:
611: sub idrget {
612: my ($udom,@unames)=@_;
613: my %returnhash=();
1.191 harris41 614: foreach (@unames) {
1.70 www 615: $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
1.191 harris41 616: }
1.70 www 617: return %returnhash;
618: }
619:
620: # ------------------------------- Store away a list of names and associated IDs
621:
622: sub idput {
623: my ($udom,%ids)=@_;
624: my %servers=();
1.191 harris41 625: foreach (keys %ids) {
1.487 albertel 626: &cput('environment',{'id'=>$ids{$_}},$udom,$_);
1.70 www 627: my $uhom=&homeserver($_,$udom);
628: if ($uhom ne 'no_host') {
629: my $id=&escape($ids{$_});
630: $id=~tr/A-Z/a-z/;
631: my $unam=&escape($_);
632: if ($servers{$uhom}) {
633: $servers{$uhom}.='&'.$id.'='.$unam;
634: } else {
635: $servers{$uhom}=$id.'='.$unam;
636: }
637: }
1.191 harris41 638: }
639: foreach (keys %servers) {
1.70 www 640: &critical('idput:'.$udom.':'.$servers{$_},$_);
1.191 harris41 641: }
1.344 www 642: }
643:
644: # --------------------------------------------------- Assign a key to a student
645:
646: sub assign_access_key {
1.364 www 647: #
648: # a valid key looks like uname:udom#comments
649: # comments are being appended
650: #
1.498 www 651: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
652: $kdom=
1.620 albertel 653: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 654: $knum=
1.620 albertel 655: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 656: $cdom=
1.620 albertel 657: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 658: $cnum=
1.620 albertel 659: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
660: $udom=$env{'user.name'} unless (defined($udom));
661: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 662: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 663: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 664: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 665: # assigned to this person
666: # - this should not happen,
1.345 www 667: # unless something went wrong
668: # the first time around
669: # ready to assign
1.364 www 670: $logentry=$1.'; '.$logentry;
1.496 www 671: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 672: $kdom,$knum) eq 'ok') {
1.345 www 673: # key now belongs to user
1.346 www 674: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 675: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
676: &appenv('environment.'.$envkey => $ckey);
677: return 'ok';
678: } else {
679: return
680: 'error: Count not permanently assign key, will need to be re-entered later.';
681: }
682: } else {
683: return 'error: Could not assign key, try again later.';
684: }
1.364 www 685: } elsif (!$existing{$ckey}) {
1.345 www 686: # the key does not exist
687: return 'error: The key does not exist';
688: } else {
689: # the key is somebody else's
690: return 'error: The key is already in use';
691: }
1.344 www 692: }
693:
1.364 www 694: # ------------------------------------------ put an additional comment on a key
695:
696: sub comment_access_key {
697: #
698: # a valid key looks like uname:udom#comments
699: # comments are being appended
700: #
701: my ($ckey,$cdom,$cnum,$logentry)=@_;
702: $cdom=
1.620 albertel 703: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 704: $cnum=
1.620 albertel 705: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 706: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
707: if ($existing{$ckey}) {
708: $existing{$ckey}.='; '.$logentry;
709: # ready to assign
1.367 www 710: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 711: $cdom,$cnum) eq 'ok') {
712: return 'ok';
713: } else {
714: return 'error: Count not store comment.';
715: }
716: } else {
717: # the key does not exist
718: return 'error: The key does not exist';
719: }
720: }
721:
1.344 www 722: # ------------------------------------------------------ Generate a set of keys
723:
724: sub generate_access_keys {
1.364 www 725: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 726: $cdom=
1.620 albertel 727: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 728: $cnum=
1.620 albertel 729: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 730: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 731: unless (($cdom) && ($cnum)) { return 0; }
732: if ($number>10000) { return 0; }
733: sleep(2); # make sure don't get same seed twice
734: srand(time()^($$+($$<<15))); # from "Programming Perl"
735: my $total=0;
736: for (my $i=1;$i<=$number;$i++) {
737: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
738: sprintf("%lx",int(100000*rand)).'-'.
739: sprintf("%lx",int(100000*rand));
740: $newkey=~s/1/g/g; # folks mix up 1 and l
741: $newkey=~s/0/h/g; # and also 0 and O
742: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
743: if ($existing{$newkey}) {
744: $i--;
745: } else {
1.364 www 746: if (&put('accesskeys',
747: { $newkey => '# generated '.localtime().
1.620 albertel 748: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 749: '; '.$logentry },
750: $cdom,$cnum) eq 'ok') {
1.344 www 751: $total++;
752: }
753: }
754: }
1.620 albertel 755: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 756: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
757: return $total;
758: }
759:
760: # ------------------------------------------------------- Validate an accesskey
761:
762: sub validate_access_key {
763: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
764: $cdom=
1.620 albertel 765: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 766: $cnum=
1.620 albertel 767: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
768: $udom=$env{'user.domain'} unless (defined($udom));
769: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 770: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 771: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 772: }
773:
774: # ------------------------------------- Find the section of student in a course
1.652 albertel 775: sub devalidate_getsection_cache {
776: my ($udom,$unam,$courseid)=@_;
777: $courseid=~s/\_/\//g;
778: $courseid=~s/^(\w)/\/$1/;
779: my $hashid="$udom:$unam:$courseid";
780: &devalidate_cache_new('getsection',$hashid);
781: }
1.298 matthew 782:
783: sub getsection {
784: my ($udom,$unam,$courseid)=@_;
1.599 albertel 785: my $cachetime=1800;
1.298 matthew 786: $courseid=~s/\_/\//g;
787: $courseid=~s/^(\w)/\/$1/;
1.551 albertel 788:
789: my $hashid="$udom:$unam:$courseid";
1.599 albertel 790: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 791: if (defined($cached)) { return $result; }
792:
1.298 matthew 793: my %Pending;
794: my %Expired;
795: #
796: # Each role can either have not started yet (pending), be active,
797: # or have expired.
798: #
799: # If there is an active role, we are done.
800: #
801: # If there is more than one role which has not started yet,
802: # choose the one which will start sooner
803: # If there is one role which has not started yet, return it.
804: #
805: # If there is more than one expired role, choose the one which ended last.
806: # If there is a role which has expired, return it.
807: #
808: foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
809: &homeserver($unam,$udom)))) {
810: my ($key,$value)=split(/\=/,$_);
811: $key=&unescape($key);
1.479 albertel 812: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 813: my $section=$1;
814: if ($key eq $courseid.'_st') { $section=''; }
815: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
816: my $now=time;
1.548 albertel 817: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 818: $Expired{$end}=$section;
819: next;
820: }
1.548 albertel 821: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 822: $Pending{$start}=$section;
823: next;
824: }
1.599 albertel 825: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 826: }
827: #
828: # Presumedly there will be few matching roles from the above
829: # loop and the sorting time will be negligible.
830: if (scalar(keys(%Pending))) {
831: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 832: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 833: }
834: if (scalar(keys(%Expired))) {
835: my @sorted = sort {$a <=> $b} keys(%Expired);
836: my $time = pop(@sorted);
1.599 albertel 837: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 838: }
1.599 albertel 839: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 840: }
1.70 www 841:
1.599 albertel 842: sub save_cache {
1.628 albertel 843: my ($r)=@_;
844: if (! $r->is_initial_req()) { return DECLINED; }
1.599 albertel 845: &purge_remembered();
1.620 albertel 846: undef(%env);
1.628 albertel 847: return OK;
1.599 albertel 848: }
1.452 albertel 849:
1.599 albertel 850: my $to_remember=-1;
851: my %remembered;
852: my %accessed;
853: my $kicks=0;
854: my $hits=0;
855: sub devalidate_cache_new {
856: my ($name,$id,$debug) = @_;
857: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
858: $id=&escape($name.':'.$id);
859: $memcache->delete($id);
860: delete($remembered{$id});
861: delete($accessed{$id});
862: }
863:
864: sub is_cached_new {
865: my ($name,$id,$debug) = @_;
866: $id=&escape($name.':'.$id);
867: if (exists($remembered{$id})) {
868: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
869: $accessed{$id}=[&gettimeofday()];
870: $hits++;
871: return ($remembered{$id},1);
872: }
873: my $value = $memcache->get($id);
874: if (!(defined($value))) {
875: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 876: return (undef,undef);
1.416 albertel 877: }
1.599 albertel 878: if ($value eq '__undef__') {
879: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
880: $value=undef;
881: }
882: &make_room($id,$value,$debug);
883: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
884: return ($value,1);
885: }
886:
887: sub do_cache_new {
888: my ($name,$id,$value,$time,$debug) = @_;
889: $id=&escape($name.':'.$id);
890: my $setvalue=$value;
891: if (!defined($setvalue)) {
892: $setvalue='__undef__';
893: }
1.623 albertel 894: if (!defined($time) ) {
895: $time=600;
896: }
1.599 albertel 897: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.600 albertel 898: $memcache->set($id,$setvalue,$time);
899: # need to make a copy of $value
900: #&make_room($id,$value,$debug);
1.599 albertel 901: return $value;
902: }
903:
904: sub make_room {
905: my ($id,$value,$debug)=@_;
906: $remembered{$id}=$value;
907: if ($to_remember<0) { return; }
908: $accessed{$id}=[&gettimeofday()];
909: if (scalar(keys(%remembered)) <= $to_remember) { return; }
910: my $to_kick;
911: my $max_time=0;
912: foreach my $other (keys(%accessed)) {
913: if (&tv_interval($accessed{$other}) > $max_time) {
914: $to_kick=$other;
915: $max_time=&tv_interval($accessed{$other});
916: }
917: }
918: delete($remembered{$to_kick});
919: delete($accessed{$to_kick});
920: $kicks++;
921: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 922: return;
923: }
924:
1.599 albertel 925: sub purge_remembered {
1.604 albertel 926: #&logthis("Tossing ".scalar(keys(%remembered)));
927: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 928: undef(%remembered);
929: undef(%accessed);
1.428 albertel 930: }
1.70 www 931: # ------------------------------------- Read an entry from a user's environment
932:
933: sub userenvironment {
934: my ($udom,$unam,@what)=@_;
935: my %returnhash=();
936: my @answer=split(/\&/,
937: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
938: &homeserver($unam,$udom)));
939: my $i;
940: for ($i=0;$i<=$#what;$i++) {
941: $returnhash{$what[$i]}=&unescape($answer[$i]);
942: }
943: return %returnhash;
1.1 albertel 944: }
945:
1.617 albertel 946: # ---------------------------------------------------------- Get a studentphoto
947: sub studentphoto {
948: my ($udom,$unam,$ext) = @_;
949: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 950: if (defined($env{'request.course.id'})) {
1.708 raeburn 951: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 952: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
953: return(&retrievestudentphoto($udom,$unam,$ext));
954: } else {
955: my ($result,$perm_reqd)=
1.707 albertel 956: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 957: if ($result eq 'ok') {
958: if (!($perm_reqd eq 'yes')) {
959: return(&retrievestudentphoto($udom,$unam,$ext));
960: }
961: }
962: }
963: }
964: } else {
965: my ($result,$perm_reqd) =
1.707 albertel 966: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 967: if ($result eq 'ok') {
968: if (!($perm_reqd eq 'yes')) {
969: return(&retrievestudentphoto($udom,$unam,$ext));
970: }
971: }
972: }
973: return '/adm/lonKaputt/lonlogo_broken.gif';
974: }
975:
976: sub retrievestudentphoto {
977: my ($udom,$unam,$ext,$type) = @_;
978: my $home=&Apache::lonnet::homeserver($unam,$udom);
979: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
980: if ($ret eq 'ok') {
981: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
982: if ($type eq 'thumbnail') {
983: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
984: }
985: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
986: return $tokenurl;
987: } else {
988: if ($type eq 'thumbnail') {
989: return '/adm/lonKaputt/genericstudent_tn.gif';
990: } else {
991: return '/adm/lonKaputt/lonlogo_broken.gif';
992: }
1.617 albertel 993: }
994: }
995:
1.263 www 996: # -------------------------------------------------------------------- New chat
997:
998: sub chatsend {
999: my ($newentry,$anon)=@_;
1.620 albertel 1000: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1001: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1002: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 1003: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 1004: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.263 www 1005: &escape($newentry)),$chome);
1.292 www 1006: }
1007:
1008: # ------------------------------------------ Find current version of a resource
1009:
1010: sub getversion {
1011: my $fname=&clutter(shift);
1012: unless ($fname=~/^\/res\//) { return -1; }
1013: return ¤tversion(&filelocation('',$fname));
1014: }
1015:
1016: sub currentversion {
1017: my $fname=shift;
1.599 albertel 1018: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 1019: if (defined($cached)) { return $result; }
1.292 www 1020: my $author=$fname;
1021: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1022: my ($udom,$uname)=split(/\//,$author);
1023: my $home=homeserver($uname,$udom);
1024: if ($home eq 'no_host') {
1025: return -1;
1026: }
1027: my $answer=reply("currentversion:$fname",$home);
1028: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1029: return -1;
1030: }
1.599 albertel 1031: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 1032: }
1033:
1.1 albertel 1034: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 1035:
1.1 albertel 1036: sub subscribe {
1037: my $fname=shift;
1.312 www 1038: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 1039: $fname=~s/[\n\r]//g;
1.1 albertel 1040: my $author=$fname;
1041: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1042: my ($udom,$uname)=split(/\//,$author);
1043: my $home=homeserver($uname,$udom);
1.335 albertel 1044: if ($home eq 'no_host') {
1045: return 'not_found';
1.1 albertel 1046: }
1047: my $answer=reply("sub:$fname",$home);
1.64 www 1048: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1049: $answer.=' by '.$home;
1050: }
1.1 albertel 1051: return $answer;
1052: }
1053:
1.8 www 1054: # -------------------------------------------------------------- Replicate file
1055:
1056: sub repcopy {
1057: my $filename=shift;
1.23 www 1058: $filename=~s/\/+/\//g;
1.607 raeburn 1059: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
1060: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 1061: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 1062: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 1063: return &repcopy_userfile($filename);
1064: }
1.532 albertel 1065: $filename=~s/[\n\r]//g;
1.8 www 1066: my $transname="$filename.in.transfer";
1.607 raeburn 1067: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 1068: my $remoteurl=subscribe($filename);
1.64 www 1069: if ($remoteurl =~ /^con_lost by/) {
1070: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1071: return 'unavailable';
1.8 www 1072: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 1073: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 1074: return 'not_found';
1.64 www 1075: } elsif ($remoteurl =~ /^rejected by/) {
1076: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1077: return 'forbidden';
1.20 www 1078: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 1079: return 'ok';
1.8 www 1080: } else {
1.290 www 1081: my $author=$filename;
1082: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1083: my ($udom,$uname)=split(/\//,$author);
1084: my $home=homeserver($uname,$udom);
1085: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 1086: my @parts=split(/\//,$filename);
1087: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1088: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1089: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 1090: return 'bad_request';
1.8 www 1091: }
1092: my $count;
1093: for ($count=5;$count<$#parts;$count++) {
1094: $path.="/$parts[$count]";
1095: if ((-e $path)!=1) {
1096: mkdir($path,0777);
1097: }
1098: }
1099: my $ua=new LWP::UserAgent;
1100: my $request=new HTTP::Request('GET',"$remoteurl");
1101: my $response=$ua->request($request,$transname);
1102: if ($response->is_error()) {
1103: unlink($transname);
1104: my $message=$response->status_line;
1.672 albertel 1105: &logthis("<font color=\"blue\">WARNING:"
1.12 www 1106: ." LWP get: $message: $filename</font>");
1.607 raeburn 1107: return 'unavailable';
1.8 www 1108: } else {
1.16 www 1109: if ($remoteurl!~/\.meta$/) {
1110: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1111: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1112: if ($mresponse->is_error()) {
1113: unlink($filename.'.meta');
1114: &logthis(
1.672 albertel 1115: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 1116: }
1117: }
1.8 www 1118: rename($transname,$filename);
1.607 raeburn 1119: return 'ok';
1.8 www 1120: }
1.290 www 1121: }
1.8 www 1122: }
1.330 www 1123: }
1124:
1125: # ------------------------------------------------ Get server side include body
1126: sub ssi_body {
1.381 albertel 1127: my ($filelink,%form)=@_;
1.606 matthew 1128: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
1129: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
1130: }
1.330 www 1131: my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381 albertel 1132: &ssi($filelink,%form));
1.565 albertel 1133: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 1134: $output=~s/^.*?\<body[^\>]*\>//si;
1135: $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330 www 1136: return $output;
1.8 www 1137: }
1138:
1.15 www 1139: # --------------------------------------------------------- Server Side Include
1140:
1141: sub ssi {
1142:
1.23 www 1143: my ($fn,%form)=@_;
1.15 www 1144:
1145: my $ua=new LWP::UserAgent;
1.23 www 1146:
1147: my $request;
1.711 albertel 1148:
1149: $form{'no_update_last_known'}=1;
1150:
1.23 www 1151: if (%form) {
1152: $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
1.201 albertel 1153: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23 www 1154: } else {
1155: $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
1156: }
1157:
1.15 www 1158: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1159: my $response=$ua->request($request);
1160:
1.324 www 1161: return $response->content;
1162: }
1163:
1164: sub externalssi {
1165: my ($url)=@_;
1166: my $ua=new LWP::UserAgent;
1167: my $request=new HTTP::Request('GET',$url);
1168: my $response=$ua->request($request);
1.15 www 1169: return $response->content;
1170: }
1.254 www 1171:
1.492 albertel 1172: # -------------------------------- Allow a /uploaded/ URI to be vouched for
1173:
1174: sub allowuploaded {
1175: my ($srcurl,$url)=@_;
1176: $url=&clutter(&declutter($url));
1177: my $dir=$url;
1178: $dir=~s/\/[^\/]+$//;
1179: my %httpref=();
1180: my $httpurl=&hreflocation('',$url);
1181: $httpref{'httpref.'.$httpurl}=$srcurl;
1182: &Apache::lonnet::appenv(%httpref);
1.254 www 1183: }
1.477 raeburn 1184:
1.478 albertel 1185: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 1186: # input: action, courseID, current domain, intended
1.637 raeburn 1187: # path to file, source of file, instruction to parse file for objects,
1188: # ref to hash for embedded objects,
1189: # ref to hash for codebase of java objects.
1190: #
1.485 raeburn 1191: # output: url to file (if action was uploaddoc),
1192: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 1193: #
1.478 albertel 1194: # Allows directory structure to be used within lonUsers/../userfiles/ for a
1195: # course.
1.477 raeburn 1196: #
1.478 albertel 1197: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1198: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
1199: # course's home server.
1.477 raeburn 1200: #
1.478 albertel 1201: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
1202: # be copied from $source (current location) to
1203: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1204: # and will then be copied to
1205: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
1206: # course's home server.
1.485 raeburn 1207: #
1.481 raeburn 1208: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 1209: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 1210: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1211: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
1212: # in course's home server.
1.637 raeburn 1213: #
1.477 raeburn 1214:
1215: sub process_coursefile {
1.638 albertel 1216: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477 raeburn 1217: my $fetchresult;
1.638 albertel 1218: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 1219: if ($action eq 'propagate') {
1.638 albertel 1220: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1221: $home);
1.481 raeburn 1222: } else {
1.477 raeburn 1223: my $fpath = '';
1224: my $fname = $file;
1.478 albertel 1225: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 1226: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 1227: my $filepath = &build_filepath($fpath);
1.481 raeburn 1228: if ($action eq 'copy') {
1229: if ($source eq '') {
1230: $fetchresult = 'no source file';
1231: return $fetchresult;
1232: } else {
1233: my $destination = $filepath.'/'.$fname;
1234: rename($source,$destination);
1235: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1236: $home);
1.481 raeburn 1237: }
1238: } elsif ($action eq 'uploaddoc') {
1239: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 1240: print $fh $env{'form.'.$source};
1.481 raeburn 1241: close($fh);
1.637 raeburn 1242: if ($parser eq 'parse') {
1243: my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
1244: unless ($parse_result eq 'ok') {
1245: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
1246: }
1247: }
1.477 raeburn 1248: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1249: $home);
1.481 raeburn 1250: if ($fetchresult eq 'ok') {
1251: return '/uploaded/'.$fpath.'/'.$fname;
1252: } else {
1253: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1254: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 1255: return '/adm/notfound.html';
1256: }
1.477 raeburn 1257: }
1258: }
1.485 raeburn 1259: unless ( $fetchresult eq 'ok') {
1.477 raeburn 1260: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1261: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 1262: }
1263: return $fetchresult;
1264: }
1265:
1.637 raeburn 1266: sub build_filepath {
1267: my ($fpath) = @_;
1268: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
1269: unless ($fpath eq '') {
1270: my @parts=split('/',$fpath);
1271: foreach my $part (@parts) {
1272: $filepath.= '/'.$part;
1273: if ((-e $filepath)!=1) {
1274: mkdir($filepath,0777);
1275: }
1276: }
1277: }
1278: return $filepath;
1279: }
1280:
1281: sub store_edited_file {
1.638 albertel 1282: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 1283: my $file = $primary_url;
1284: $file =~ s#^/uploaded/$docudom/$docuname/##;
1285: my $fpath = '';
1286: my $fname = $file;
1287: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1288: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1289: my $filepath = &build_filepath($fpath);
1290: open(my $fh,'>'.$filepath.'/'.$fname);
1291: print $fh $content;
1292: close($fh);
1.638 albertel 1293: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 1294: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1295: $home);
1.637 raeburn 1296: if ($$fetchresult eq 'ok') {
1297: return '/uploaded/'.$fpath.'/'.$fname;
1298: } else {
1.638 albertel 1299: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1300: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 1301: return '/adm/notfound.html';
1302: }
1303: }
1304:
1.531 albertel 1305: sub clean_filename {
1306: my ($fname)=@_;
1.315 www 1307: # Replace Windows backslashes by forward slashes
1.257 www 1308: $fname=~s/\\/\//g;
1.315 www 1309: # Get rid of everything but the actual filename
1.257 www 1310: $fname=~s/^.*\/([^\/]+)$/$1/;
1.315 www 1311: # Replace spaces by underscores
1312: $fname=~s/\s+/\_/g;
1313: # Replace all other weird characters by nothing
1.317 www 1314: $fname=~s/[^\w\.\-]//g;
1.540 albertel 1315: # Replace all .\d. sequences with _\d. so they no longer look like version
1316: # numbers
1317: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 1318: return $fname;
1319: }
1320:
1.608 albertel 1321: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 1322: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719 ! banghart 1323: # the desired filenam is in $env{"form.$formname.filename"}
1.686 albertel 1324: # $coursedoc - if true up to the current course
1325: # if false
1326: # $subdir - directory in userfile to store the file into
1327: # $parser, $allfiles, $codebase - unknown
1328: #
1329: # output: url of file in userspace, or error: <message>
1330: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 1331:
1332:
1.531 albertel 1333: sub userfileupload {
1.719 ! banghart 1334: my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,$destudom)=@_;
1.531 albertel 1335: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 1336: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 1337: $fname=&clean_filename($fname);
1.315 www 1338: # See if there is anything left
1.257 www 1339: unless ($fname) { return 'error: no uploaded file'; }
1.620 albertel 1340: chop($env{'form.'.$formname});
1.523 raeburn 1341: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
1342: my $now = time;
1343: my $filepath = 'tmp/helprequests/'.$now;
1344: my @parts=split(/\//,$filepath);
1345: my $fullpath = $perlvar{'lonDaemons'};
1346: for (my $i=0;$i<@parts;$i++) {
1347: $fullpath .= '/'.$parts[$i];
1348: if ((-e $fullpath)!=1) {
1349: mkdir($fullpath,0777);
1350: }
1351: }
1352: open(my $fh,'>'.$fullpath.'/'.$fname);
1.620 albertel 1353: print $fh $env{'form.'.$formname};
1.523 raeburn 1354: close($fh);
1355: return $fullpath.'/'.$fname;
1356: }
1.719 ! banghart 1357:
1.258 www 1358: # Create the directory if not present
1.493 albertel 1359: $fname="$subdir/$fname";
1.259 www 1360: if ($coursedoc) {
1.638 albertel 1361: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1362: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 1363: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 1364: return &finishuserfileupload($docuname,$docudom,
1365: $formname,$fname,$parser,$allfiles,
1366: $codebase);
1.481 raeburn 1367: } else {
1.620 albertel 1368: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 1369: return &process_coursefile('uploaddoc',$docuname,$docudom,
1370: $fname,$formname,$parser,
1371: $allfiles,$codebase);
1.481 raeburn 1372: }
1.719 ! banghart 1373: } elsif (defined($destuname)) {
! 1374: my $docuname=$destuname;
! 1375: my $docudom=$destudom;
! 1376: return &finishuserfileupload($docuname,$docudom,$formname,
! 1377: $fname,$parser,$allfiles,$codebase);
! 1378:
1.259 www 1379: } else {
1.638 albertel 1380: my $docuname=$env{'user.name'};
1381: my $docudom=$env{'user.domain'};
1.714 raeburn 1382: if (exists($env{'form.group'})) {
1383: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1384: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1385: }
1.638 albertel 1386: return &finishuserfileupload($docuname,$docudom,$formname,
1387: $fname,$parser,$allfiles,$codebase);
1.259 www 1388: }
1.271 www 1389: }
1390:
1391: sub finishuserfileupload {
1.638 albertel 1392: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase) = @_;
1.477 raeburn 1393: my $path=$docudom.'/'.$docuname.'/';
1.258 www 1394: my $filepath=$perlvar{'lonDocRoot'};
1.494 albertel 1395: my ($fnamepath,$file);
1396: $file=$fname;
1397: if ($fname=~m|/|) {
1398: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
1399: $path.=$fnamepath.'/';
1400: }
1.259 www 1401: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 1402: my $count;
1403: for ($count=4;$count<=$#parts;$count++) {
1404: $filepath.="/$parts[$count]";
1405: if ((-e $filepath)!=1) {
1406: mkdir($filepath,0777);
1407: }
1408: }
1409: # Save the file
1410: {
1.701 albertel 1411: if (!open(FH,'>'.$filepath.'/'.$file)) {
1412: &logthis('Failed to create '.$filepath.'/'.$file);
1413: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
1414: return '/adm/notfound.html';
1415: }
1416: if (!print FH ($env{'form.'.$formname})) {
1417: &logthis('Failed to write to '.$filepath.'/'.$file);
1418: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
1419: return '/adm/notfound.html';
1420: }
1.570 albertel 1421: close(FH);
1.258 www 1422: }
1.637 raeburn 1423: if ($parser eq 'parse') {
1.638 albertel 1424: my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
1425: $codebase);
1.637 raeburn 1426: unless ($parse_result eq 'ok') {
1.638 albertel 1427: &logthis('Failed to parse '.$filepath.$file.
1428: ' for embedded media: '.$parse_result);
1.637 raeburn 1429: }
1430: }
1.259 www 1431: # Notify homeserver to grep it
1432: #
1.638 albertel 1433: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 1434: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 1435: if ($fetchresult eq 'ok') {
1.259 www 1436: #
1.258 www 1437: # Return the URL to it
1.494 albertel 1438: return '/uploaded/'.$path.$file;
1.263 www 1439: } else {
1.494 albertel 1440: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
1441: ': '.$fetchresult);
1.263 www 1442: return '/adm/notfound.html';
1443: }
1.493 albertel 1444: }
1445:
1.637 raeburn 1446: sub extract_embedded_items {
1.648 raeburn 1447: my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637 raeburn 1448: my @state = ();
1449: my %javafiles = (
1450: codebase => '',
1451: code => '',
1452: archive => ''
1453: );
1454: my %mediafiles = (
1455: src => '',
1456: movie => '',
1457: );
1.648 raeburn 1458: my $p;
1459: if ($content) {
1460: $p = HTML::LCParser->new($content);
1461: } else {
1462: $p = HTML::LCParser->new($filepath.'/'.$file);
1463: }
1.641 albertel 1464: while (my $t=$p->get_token()) {
1.640 albertel 1465: if ($t->[0] eq 'S') {
1466: my ($tagname, $attr) = ($t->[1],$t->[2]);
1467: push (@state, $tagname);
1.648 raeburn 1468: if (lc($tagname) eq 'allow') {
1469: &add_filetype($allfiles,$attr->{'src'},'src');
1470: }
1.640 albertel 1471: if (lc($tagname) eq 'img') {
1472: &add_filetype($allfiles,$attr->{'src'},'src');
1473: }
1.645 raeburn 1474: if (lc($tagname) eq 'script') {
1475: if ($attr->{'archive'} =~ /\.jar$/i) {
1476: &add_filetype($allfiles,$attr->{'archive'},'archive');
1477: } else {
1478: &add_filetype($allfiles,$attr->{'src'},'src');
1479: }
1480: }
1481: if (lc($tagname) eq 'link') {
1482: if (lc($attr->{'rel'}) eq 'stylesheet') {
1483: &add_filetype($allfiles,$attr->{'href'},'href');
1484: }
1485: }
1.640 albertel 1486: if (lc($tagname) eq 'object' ||
1487: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
1488: foreach my $item (keys(%javafiles)) {
1489: $javafiles{$item} = '';
1490: }
1491: }
1492: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
1493: my $name = lc($attr->{'name'});
1494: foreach my $item (keys(%javafiles)) {
1495: if ($name eq $item) {
1496: $javafiles{$item} = $attr->{'value'};
1497: last;
1498: }
1499: }
1500: foreach my $item (keys(%mediafiles)) {
1501: if ($name eq $item) {
1502: &add_filetype($allfiles, $attr->{'value'}, 'value');
1503: last;
1504: }
1505: }
1506: }
1507: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
1508: foreach my $item (keys(%javafiles)) {
1509: if ($attr->{$item}) {
1510: $javafiles{$item} = $attr->{$item};
1511: last;
1512: }
1513: }
1514: foreach my $item (keys(%mediafiles)) {
1515: if ($attr->{$item}) {
1516: &add_filetype($allfiles,$attr->{$item},$item);
1517: last;
1518: }
1519: }
1520: }
1521: } elsif ($t->[0] eq 'E') {
1522: my ($tagname) = ($t->[1]);
1523: if ($javafiles{'codebase'} ne '') {
1524: $javafiles{'codebase'} .= '/';
1525: }
1526: if (lc($tagname) eq 'applet' ||
1527: lc($tagname) eq 'object' ||
1528: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
1529: ) {
1530: foreach my $item (keys(%javafiles)) {
1531: if ($item ne 'codebase' && $javafiles{$item} ne '') {
1532: my $file=$javafiles{'codebase'}.$javafiles{$item};
1533: &add_filetype($allfiles,$file,$item);
1534: }
1535: }
1536: }
1537: pop @state;
1538: }
1539: }
1.637 raeburn 1540: return 'ok';
1541: }
1542:
1.639 albertel 1543: sub add_filetype {
1544: my ($allfiles,$file,$type)=@_;
1545: if (exists($allfiles->{$file})) {
1546: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
1547: push(@{$allfiles->{$file}}, &escape($type));
1548: }
1549: } else {
1550: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 1551: }
1552: }
1553:
1.493 albertel 1554: sub removeuploadedurl {
1555: my ($url)=@_;
1556: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 1557: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 1558: }
1559:
1560: sub removeuserfile {
1561: my ($docuname,$docudom,$fname)=@_;
1562: my $home=&homeserver($docuname,$docudom);
1563: return &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.257 www 1564: }
1.15 www 1565:
1.530 albertel 1566: sub mkdiruserfile {
1567: my ($docuname,$docudom,$dir)=@_;
1568: my $home=&homeserver($docuname,$docudom);
1569: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
1570: }
1571:
1.531 albertel 1572: sub renameuserfile {
1573: my ($docuname,$docudom,$old,$new)=@_;
1574: my $home=&homeserver($docuname,$docudom);
1575: return &reply("renameuserfile:$docudom:$docuname:".&escape("$old").':'.
1576: &escape("$new"),$home);
1577: }
1578:
1.14 www 1579: # ------------------------------------------------------------------------- Log
1580:
1581: sub log {
1582: my ($dom,$nam,$hom,$what)=@_;
1.47 www 1583: return critical("log:$dom:$nam:$what",$hom);
1.157 www 1584: }
1585:
1586: # ------------------------------------------------------------------ Course Log
1.352 www 1587: #
1588: # This routine flushes several buffers of non-mission-critical nature
1589: #
1.157 www 1590:
1591: sub flushcourselogs {
1.352 www 1592: &logthis('Flushing log buffers');
1593: #
1594: # course logs
1595: # This is a log of all transactions in a course, which can be used
1596: # for data mining purposes
1597: #
1598: # It also collects the courseid database, which lists last transaction
1599: # times and course titles for all courseids
1600: #
1601: my %courseidbuffer=();
1.191 harris41 1602: foreach (keys %courselogs) {
1.157 www 1603: my $crsid=$_;
1.352 www 1604: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 1605: &escape($courselogs{$crsid}),
1606: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 1607: delete $courselogs{$crsid};
1608: } else {
1609: &logthis('Failed to flush log buffer for '.$crsid);
1610: if (length($courselogs{$crsid})>40000) {
1.672 albertel 1611: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 1612: " exceeded maximum size, deleting.</font>");
1613: delete $courselogs{$crsid};
1614: }
1.352 www 1615: }
1616: if ($courseidbuffer{$coursehombuf{$crsid}}) {
1617: $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516 raeburn 1618: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.571 raeburn 1619: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid});
1.352 www 1620: } else {
1621: $courseidbuffer{$coursehombuf{$crsid}}=
1.516 raeburn 1622: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.571 raeburn 1623: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid});
1624: }
1.191 harris41 1625: }
1.352 www 1626: #
1627: # Write course id database (reverse lookup) to homeserver of courses
1628: # Is used in pickcourse
1629: #
1630: foreach (keys %courseidbuffer) {
1.353 www 1631: &courseidput($hostdom{$_},$courseidbuffer{$_},$_);
1.352 www 1632: }
1633: #
1634: # File accesses
1635: # Writes to the dynamic metadata of resources to get hit counts, etc.
1636: #
1.449 matthew 1637: foreach my $entry (keys(%accesshash)) {
1.458 matthew 1638: if ($entry =~ /___count$/) {
1639: my ($dom,$name);
1640: ($dom,$name,undef)=($entry=~m:___(\w+)/(\w+)/(.*)___count$:);
1641: if (! defined($dom) || $dom eq '' ||
1642: ! defined($name) || $name eq '') {
1.620 albertel 1643: my $cid = $env{'request.course.id'};
1644: $dom = $env{'request.'.$cid.'.domain'};
1645: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 1646: }
1.450 matthew 1647: my $value = $accesshash{$entry};
1648: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
1649: my %temphash=($url => $value);
1.449 matthew 1650: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
1651: if ($result eq 'ok') {
1652: delete $accesshash{$entry};
1653: } elsif ($result eq 'unknown_cmd') {
1654: # Target server has old code running on it.
1.450 matthew 1655: my %temphash=($entry => $value);
1.449 matthew 1656: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1657: delete $accesshash{$entry};
1658: }
1659: }
1660: } else {
1.458 matthew 1661: my ($dom,$name) = ($entry=~m:___(\w+)/(\w+)/(.*)___(\w+)$:);
1.450 matthew 1662: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 1663: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1664: delete $accesshash{$entry};
1665: }
1.185 www 1666: }
1.191 harris41 1667: }
1.352 www 1668: #
1669: # Roles
1670: # Reverse lookup of user roles for course faculty/staff and co-authorship
1671: #
1.349 www 1672: foreach (keys %userrolehash) {
1673: my $entry=$_;
1.351 www 1674: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 1675: split(/\:/,$entry);
1676: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 1677: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 1678: $rudom,$runame) eq 'ok') {
1679: delete $userrolehash{$entry};
1680: }
1681: }
1.662 raeburn 1682: #
1683: # Reverse lookup of domain roles (dc, ad, li, sc, au)
1684: #
1685: my %domrolebuffer = ();
1686: foreach my $entry (keys %domainrolehash) {
1687: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
1688: if ($domrolebuffer{$rudom}) {
1689: $domrolebuffer{$rudom}.='&'.&escape($entry).
1690: '='.&escape($domainrolehash{$entry});
1691: } else {
1692: $domrolebuffer{$rudom}.=&escape($entry).
1693: '='.&escape($domainrolehash{$entry});
1694: }
1695: delete $domainrolehash{$entry};
1696: }
1697: foreach my $dom (keys(%domrolebuffer)) {
1698: foreach my $tryserver (keys %libserv) {
1699: if ($hostdom{$tryserver} eq $dom) {
1700: unless (&reply('domroleput:'.$dom.':'.
1701: $domrolebuffer{$dom},$tryserver) eq 'ok') {
1702: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
1703: }
1704: }
1705: }
1706: }
1.186 www 1707: $dumpcount++;
1.157 www 1708: }
1709:
1710: sub courselog {
1711: my $what=shift;
1.158 www 1712: $what=time.':'.$what;
1.620 albertel 1713: unless ($env{'request.course.id'}) { return ''; }
1714: $coursedombuf{$env{'request.course.id'}}=
1715: $env{'course.'.$env{'request.course.id'}.'.domain'};
1716: $coursenumbuf{$env{'request.course.id'}}=
1717: $env{'course.'.$env{'request.course.id'}.'.num'};
1718: $coursehombuf{$env{'request.course.id'}}=
1719: $env{'course.'.$env{'request.course.id'}.'.home'};
1720: $coursedescrbuf{$env{'request.course.id'}}=
1721: $env{'course.'.$env{'request.course.id'}.'.description'};
1722: $courseinstcodebuf{$env{'request.course.id'}}=
1723: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
1724: $courseownerbuf{$env{'request.course.id'}}=
1725: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1726: if (defined $courselogs{$env{'request.course.id'}}) {
1727: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 1728: } else {
1.620 albertel 1729: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 1730: }
1.620 albertel 1731: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 1732: &flushcourselogs();
1733: }
1.158 www 1734: }
1735:
1736: sub courseacclog {
1737: my $fnsymb=shift;
1.620 albertel 1738: unless ($env{'request.course.id'}) { return ''; }
1739: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 1740: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 1741: $what.=':POST';
1.583 matthew 1742: # FIXME: Probably ought to escape things....
1.620 albertel 1743: foreach (keys %env) {
1.158 www 1744: if ($_=~/^form\.(.*)/) {
1.620 albertel 1745: $what.=':'.$1.'='.$env{$_};
1.158 www 1746: }
1.191 harris41 1747: }
1.583 matthew 1748: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
1749: # FIXME: We should not be depending on a form parameter that someone
1750: # editing lonsearchcat.pm might change in the future.
1.620 albertel 1751: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 1752: $what.= ':POST';
1753: # FIXME: Probably ought to escape things....
1754: foreach my $element ('courseexp','crsfulltext','crsrelated',
1755: 'crsdiscuss') {
1.620 albertel 1756: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 1757: }
1758: }
1.158 www 1759: }
1760: &courselog($what);
1.149 www 1761: }
1762:
1.185 www 1763: sub countacc {
1764: my $url=&declutter(shift);
1.458 matthew 1765: return if (! defined($url) || $url eq '');
1.620 albertel 1766: unless ($env{'request.course.id'}) { return ''; }
1767: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 1768: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 1769: $accesshash{$key}++;
1.185 www 1770: }
1.349 www 1771:
1.361 www 1772: sub linklog {
1773: my ($from,$to)=@_;
1774: $from=&declutter($from);
1775: $to=&declutter($to);
1776: $accesshash{$from.'___'.$to.'___comefrom'}=1;
1777: $accesshash{$to.'___'.$from.'___goto'}=1;
1778: }
1779:
1.349 www 1780: sub userrolelog {
1781: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 1782: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 1783: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 1784: ($trole=~/^ep/) || ($trole=~/^cr/) ||
1785: ($trole=~/^ta/)) {
1.350 www 1786: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
1787: $userrolehash
1788: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 1789: =$tend.':'.$tstart;
1.662 raeburn 1790: }
1791: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
1792: ($trole=~/^li/) || ($trole=~/^li/) ||
1793: ($trole=~/^au/) || ($trole=~/^dg/) ||
1794: ($trole=~/^sc/)) {
1795: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
1796: $domainrolehash
1797: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1798: = $tend.':'.$tstart;
1799: }
1.351 www 1800: }
1801:
1802: sub get_course_adv_roles {
1803: my $cid=shift;
1.620 albertel 1804: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 1805: my %coursehash=&coursedescription($cid);
1.470 www 1806: my %nothide=();
1807: foreach (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1808: $nothide{join(':',split(/[\@\:]/,$_))}=1;
1809: }
1.351 www 1810: my %returnhash=();
1811: my %dumphash=
1812: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
1813: my $now=time;
1814: foreach (keys %dumphash) {
1815: my ($tend,$tstart)=split(/\:/,$dumphash{$_});
1816: if (($tstart) && ($tstart<0)) { next; }
1817: if (($tend) && ($tend<$now)) { next; }
1818: if (($tstart) && ($now<$tstart)) { next; }
1819: my ($role,$username,$domain,$section)=split(/\:/,$_);
1.576 albertel 1820: if ($username eq '' || $domain eq '') { next; }
1.470 www 1821: if ((&privileged($username,$domain)) &&
1822: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 1823: if ($role eq 'cr') { next; }
1.351 www 1824: my $key=&plaintext($role);
1.656 albertel 1825: if ($role =~ /^cr/) {
1826: $key=(split('/',$role))[3];
1827: }
1.351 www 1828: if ($section) { $key.=' (Sec/Grp '.$section.')'; }
1829: if ($returnhash{$key}) {
1830: $returnhash{$key}.=','.$username.':'.$domain;
1831: } else {
1832: $returnhash{$key}=$username.':'.$domain;
1833: }
1.400 www 1834: }
1835: return %returnhash;
1836: }
1837:
1838: sub get_my_roles {
1839: my ($uname,$udom)=@_;
1.620 albertel 1840: unless (defined($uname)) { $uname=$env{'user.name'}; }
1841: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.400 www 1842: my %dumphash=
1843: &dump('nohist_userroles',$udom,$uname);
1844: my %returnhash=();
1845: my $now=time;
1846: foreach (keys %dumphash) {
1847: my ($tend,$tstart)=split(/\:/,$dumphash{$_});
1848: if (($tstart) && ($tstart<0)) { next; }
1849: if (($tend) && ($tend<$now)) { next; }
1850: if (($tstart) && ($now<$tstart)) { next; }
1851: my ($role,$username,$domain,$section)=split(/\:/,$_);
1852: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.373 www 1853: }
1854: return %returnhash;
1.399 www 1855: }
1856:
1857: # ----------------------------------------------------- Frontpage Announcements
1858: #
1859: #
1860:
1861: sub postannounce {
1862: my ($server,$text)=@_;
1863: unless (&allowed('psa',$hostdom{$server})) { return 'refused'; }
1864: unless ($text=~/\w/) { $text=''; }
1865: return &reply('setannounce:'.&escape($text),$server);
1866: }
1867:
1868: sub getannounce {
1.448 albertel 1869:
1870: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 1871: my $announcement='';
1872: while (<$fh>) { $announcement .=$_; }
1.448 albertel 1873: close($fh);
1.399 www 1874: if ($announcement=~/\w/) {
1875: return
1876: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 1877: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 1878: } else {
1879: return '';
1880: }
1881: } else {
1882: return '';
1883: }
1.351 www 1884: }
1.353 www 1885:
1886: # ---------------------------------------------------------- Course ID routines
1887: # Deal with domain's nohist_courseid.db files
1888: #
1889:
1890: sub courseidput {
1891: my ($domain,$what,$coursehome)=@_;
1892: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
1893: }
1894:
1895: sub courseiddump {
1.622 raeburn 1896: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref)=@_;
1.353 www 1897: my %returnhash=();
1.355 www 1898: unless ($domfilter) { $domfilter=''; }
1.353 www 1899: foreach my $tryserver (keys %libserv) {
1.511 raeburn 1900: if ( ($hostidflag == 1 && grep/^$tryserver$/,@{$hostidref}) || (!defined($hostidflag)) ) {
1.506 raeburn 1901: if ((!$domfilter) || ($hostdom{$tryserver} eq $domfilter)) {
1902: foreach (
1903: split(/\&/,&reply('courseiddump:'.$hostdom{$tryserver}.':'.
1.571 raeburn 1904: $sincefilter.':'.&escape($descfilter).':'.
1.622 raeburn 1905: &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter),
1.354 www 1906: $tryserver))) {
1.506 raeburn 1907: my ($key,$value)=split(/\=/,$_);
1908: if (($key) && ($value)) {
1.516 raeburn 1909: $returnhash{&unescape($key)}=$value;
1.506 raeburn 1910: }
1.353 www 1911: }
1912: }
1913: }
1914: }
1915: return %returnhash;
1916: }
1917:
1.658 raeburn 1918: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 1919:
1920: sub dcmailput {
1.685 raeburn 1921: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 1922: my $status = &Apache::lonnet::critical(
1923: 'dcmailput:'.$domain.':'.&Apache::lonnet::escape($msgid).'='.
1.685 raeburn 1924: &Apache::lonnet::escape($message),$server);
1.662 raeburn 1925: return $status;
1926: }
1927:
1.658 raeburn 1928: sub dcmaildump {
1929: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 1930: my %returnhash=();
1931: if (exists($domain_primary{$dom})) {
1932: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
1933: &escape($enddate).':';
1934: my @esc_senders=map { &escape($_)} @$senders;
1935: $cmd.=&escape(join('&',@esc_senders));
1936: foreach (split(/\&/,&reply($cmd,$domain_primary{$dom}))) {
1937: my ($key,$value) = split(/\=/,$_);
1938: if (($key) && ($value)) {
1939: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 1940: }
1941: }
1942: }
1943: return %returnhash;
1944: }
1.662 raeburn 1945: # ---------------------------------------------------------- Domain roles
1946:
1947: sub get_domain_roles {
1948: my ($dom,$roles,$startdate,$enddate)=@_;
1949: if (undef($startdate) || $startdate eq '') {
1950: $startdate = '.';
1951: }
1952: if (undef($enddate) || $enddate eq '') {
1953: $enddate = '.';
1954: }
1955: my $rolelist = join(':',@{$roles});
1956: my %personnel = ();
1957: foreach my $tryserver (keys(%libserv)) {
1958: if ($hostdom{$tryserver} eq $dom) {
1959: %{$personnel{$tryserver}}=();
1960: foreach (
1961: split(/\&/,&reply('domrolesdump:'.$dom.':'.
1962: &escape($startdate).':'.&escape($enddate).':'.
1963: &escape($rolelist), $tryserver))) {
1964: my($key,$value) = split(/\=/,$_);
1965: if (($key) && ($value)) {
1966: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
1967: }
1968: }
1969: }
1970: }
1971: return %personnel;
1972: }
1.658 raeburn 1973:
1.149 www 1974: # ----------------------------------------------------------- Check out an item
1975:
1.504 albertel 1976: sub get_first_access {
1977: my ($type,$argsymb)=@_;
1978: my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1979: if ($argsymb) { $symb=$argsymb; }
1980: my ($map,$id,$res)=&decode_symb($symb);
1.588 albertel 1981: if ($type eq 'map') {
1982: $res=&symbread($map);
1983: } else {
1984: $res=$symb;
1985: }
1986: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
1987: return $times{"$courseid\0$res"};
1.504 albertel 1988: }
1989:
1990: sub set_first_access {
1991: my ($type)=@_;
1992: my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1993: my ($map,$id,$res)=&decode_symb($symb);
1.588 albertel 1994: if ($type eq 'map') {
1995: $res=&symbread($map);
1996: } else {
1997: $res=$symb;
1998: }
1999: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 2000: if (!$firstaccess) {
1.588 albertel 2001: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 2002: }
2003: return 'already_set';
1.504 albertel 2004: }
2005:
1.149 www 2006: sub checkout {
2007: my ($symb,$tuname,$tudom,$tcrsid)=@_;
2008: my $now=time;
2009: my $lonhost=$perlvar{'lonHostID'};
2010: my $infostr=&escape(
1.234 www 2011: 'CHECKOUTTOKEN&'.
1.149 www 2012: $tuname.'&'.
2013: $tudom.'&'.
2014: $tcrsid.'&'.
2015: $symb.'&'.
2016: $now.'&'.$ENV{'REMOTE_ADDR'});
2017: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 2018: if ($token=~/^error\:/) {
1.672 albertel 2019: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2020: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
2021: "</font>");
2022: return '';
2023: }
2024:
1.149 www 2025: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
2026: $token=~tr/a-z/A-Z/;
2027:
1.153 www 2028: my %infohash=('resource.0.outtoken' => $token,
2029: 'resource.0.checkouttime' => $now,
2030: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 2031:
2032: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2033: return '';
1.151 www 2034: } else {
1.672 albertel 2035: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2036: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
2037: "</font>");
1.149 www 2038: }
2039:
2040: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2041: &escape('Checkout '.$infostr.' - '.
2042: $token)) ne 'ok') {
2043: return '';
1.151 www 2044: } else {
1.672 albertel 2045: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2046: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
2047: "</font>");
1.149 www 2048: }
1.151 www 2049: return $token;
1.149 www 2050: }
2051:
2052: # ------------------------------------------------------------ Check in an item
2053:
2054: sub checkin {
2055: my $token=shift;
1.150 www 2056: my $now=time;
2057: my ($ta,$tb,$lonhost)=split(/\*/,$token);
2058: $lonhost=~tr/A-Z/a-z/;
1.595 albertel 2059: my $dtoken=$ta.'_'.$hostname{$lonhost}.'_'.$tb;
1.150 www 2060: $dtoken=~s/\W/\_/g;
1.234 www 2061: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 2062: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
2063:
1.154 www 2064: unless (($tuname) && ($tudom)) {
2065: &logthis('Check in '.$token.' ('.$dtoken.') failed');
2066: return '';
2067: }
2068:
2069: unless (&allowed('mgr',$tcrsid)) {
2070: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 2071: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 2072: return '';
2073: }
2074:
1.153 www 2075: my %infohash=('resource.0.intoken' => $token,
2076: 'resource.0.checkintime' => $now,
2077: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 2078:
2079: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2080: return '';
2081: }
2082:
2083: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2084: &escape('Checkin - '.$token)) ne 'ok') {
2085: return '';
2086: }
2087:
2088: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 2089: }
2090:
2091: # --------------------------------------------- Set Expire Date for Spreadsheet
2092:
2093: sub expirespread {
2094: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 2095: my $cid=$env{'request.course.id'};
1.110 www 2096: if ($cid) {
2097: my $now=time;
2098: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 2099: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
2100: $env{'course.'.$cid.'.num'}.
1.110 www 2101: ':nohist_expirationdates:'.
2102: &escape($key).'='.$now,
1.620 albertel 2103: $env{'course.'.$cid.'.home'})
1.110 www 2104: }
2105: return 'ok';
1.14 www 2106: }
2107:
1.109 www 2108: # ----------------------------------------------------- Devalidate Spreadsheets
2109:
2110: sub devalidate {
1.325 www 2111: my ($symb,$uname,$udom)=@_;
1.620 albertel 2112: my $cid=$env{'request.course.id'};
1.109 www 2113: if ($cid) {
1.391 matthew 2114: # delete the stored spreadsheets for
2115: # - the student level sheet of this user in course's homespace
2116: # - the assessment level sheet for this resource
2117: # for this user in user's homespace
1.553 albertel 2118: # - current conditional state info
1.325 www 2119: my $key=$uname.':'.$udom.':';
1.109 www 2120: my $status=
1.299 matthew 2121: &del('nohist_calculatedsheets',
1.391 matthew 2122: [$key.'studentcalc:'],
1.620 albertel 2123: $env{'course.'.$cid.'.domain'},
2124: $env{'course.'.$cid.'.num'})
1.133 albertel 2125: .' '.
2126: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 2127: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 2128: unless ($status eq 'ok ok') {
2129: &logthis('Could not devalidate spreadsheet '.
1.325 www 2130: $uname.' at '.$udom.' for '.
1.109 www 2131: $symb.': '.$status);
1.133 albertel 2132: }
1.553 albertel 2133: &delenv('user.state.'.$cid);
1.109 www 2134: }
2135: }
2136:
1.265 albertel 2137: sub get_scalar {
2138: my ($string,$end) = @_;
2139: my $value;
2140: if ($$string =~ s/^([^&]*?)($end)/$2/) {
2141: $value = $1;
2142: } elsif ($$string =~ s/^([^&]*?)&//) {
2143: $value = $1;
2144: }
2145: return &unescape($value);
2146: }
2147:
2148: sub array2str {
2149: my (@array) = @_;
2150: my $result=&arrayref2str(\@array);
2151: $result=~s/^__ARRAY_REF__//;
2152: $result=~s/__END_ARRAY_REF__$//;
2153: return $result;
2154: }
2155:
1.204 albertel 2156: sub arrayref2str {
2157: my ($arrayref) = @_;
1.265 albertel 2158: my $result='__ARRAY_REF__';
1.204 albertel 2159: foreach my $elem (@$arrayref) {
1.265 albertel 2160: if(ref($elem) eq 'ARRAY') {
2161: $result.=&arrayref2str($elem).'&';
2162: } elsif(ref($elem) eq 'HASH') {
2163: $result.=&hashref2str($elem).'&';
2164: } elsif(ref($elem)) {
2165: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 2166: } else {
2167: $result.=&escape($elem).'&';
2168: }
2169: }
2170: $result=~s/\&$//;
1.265 albertel 2171: $result .= '__END_ARRAY_REF__';
1.204 albertel 2172: return $result;
2173: }
2174:
1.168 albertel 2175: sub hash2str {
1.204 albertel 2176: my (%hash) = @_;
2177: my $result=&hashref2str(\%hash);
1.265 albertel 2178: $result=~s/^__HASH_REF__//;
2179: $result=~s/__END_HASH_REF__$//;
1.204 albertel 2180: return $result;
2181: }
2182:
2183: sub hashref2str {
2184: my ($hashref)=@_;
1.265 albertel 2185: my $result='__HASH_REF__';
1.495 albertel 2186: foreach (sort(keys(%$hashref))) {
1.204 albertel 2187: if (ref($_) eq 'ARRAY') {
1.265 albertel 2188: $result.=&arrayref2str($_).'=';
1.204 albertel 2189: } elsif (ref($_) eq 'HASH') {
1.265 albertel 2190: $result.=&hashref2str($_).'=';
1.204 albertel 2191: } elsif (ref($_)) {
1.265 albertel 2192: $result.='=';
2193: #print("Got a ref of ".(ref($_))." skipping.");
1.204 albertel 2194: } else {
1.265 albertel 2195: if ($_) {$result.=&escape($_).'=';} else { last; }
1.204 albertel 2196: }
2197:
1.265 albertel 2198: if(ref($hashref->{$_}) eq 'ARRAY') {
2199: $result.=&arrayref2str($hashref->{$_}).'&';
2200: } elsif(ref($hashref->{$_}) eq 'HASH') {
2201: $result.=&hashref2str($hashref->{$_}).'&';
2202: } elsif(ref($hashref->{$_})) {
2203: $result.='&';
2204: #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
1.204 albertel 2205: } else {
1.265 albertel 2206: $result.=&escape($hashref->{$_}).'&';
1.204 albertel 2207: }
2208: }
1.168 albertel 2209: $result=~s/\&$//;
1.265 albertel 2210: $result .= '__END_HASH_REF__';
1.168 albertel 2211: return $result;
2212: }
2213:
2214: sub str2hash {
1.265 albertel 2215: my ($string)=@_;
2216: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
2217: return %$hash;
2218: }
2219:
2220: sub str2hashref {
1.168 albertel 2221: my ($string) = @_;
1.265 albertel 2222:
2223: my %hash;
2224:
2225: if($string !~ /^__HASH_REF__/) {
2226: if (! ($string eq '' || !defined($string))) {
2227: $hash{'error'}='Not hash reference';
2228: }
2229: return (\%hash, $string);
2230: }
2231:
2232: $string =~ s/^__HASH_REF__//;
2233:
2234: while($string !~ /^__END_HASH_REF__/) {
2235: #key
2236: my $key='';
2237: if($string =~ /^__HASH_REF__/) {
2238: ($key, $string)=&str2hashref($string);
2239: if(defined($key->{'error'})) {
2240: $hash{'error'}='Bad data';
2241: return (\%hash, $string);
2242: }
2243: } elsif($string =~ /^__ARRAY_REF__/) {
2244: ($key, $string)=&str2arrayref($string);
2245: if($key->[0] eq 'Array reference error') {
2246: $hash{'error'}='Bad data';
2247: return (\%hash, $string);
2248: }
2249: } else {
2250: $string =~ s/^(.*?)=//;
1.267 albertel 2251: $key=&unescape($1);
1.265 albertel 2252: }
2253: $string =~ s/^=//;
2254:
2255: #value
2256: my $value='';
2257: if($string =~ /^__HASH_REF__/) {
2258: ($value, $string)=&str2hashref($string);
2259: if(defined($value->{'error'})) {
2260: $hash{'error'}='Bad data';
2261: return (\%hash, $string);
2262: }
2263: } elsif($string =~ /^__ARRAY_REF__/) {
2264: ($value, $string)=&str2arrayref($string);
2265: if($value->[0] eq 'Array reference error') {
2266: $hash{'error'}='Bad data';
2267: return (\%hash, $string);
2268: }
2269: } else {
2270: $value=&get_scalar(\$string,'__END_HASH_REF__');
2271: }
2272: $string =~ s/^&//;
2273:
2274: $hash{$key}=$value;
1.204 albertel 2275: }
1.265 albertel 2276:
2277: $string =~ s/^__END_HASH_REF__//;
2278:
2279: return (\%hash, $string);
1.204 albertel 2280: }
2281:
2282: sub str2array {
1.265 albertel 2283: my ($string)=@_;
2284: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
2285: return @$array;
2286: }
2287:
2288: sub str2arrayref {
1.204 albertel 2289: my ($string) = @_;
1.265 albertel 2290: my @array;
2291:
2292: if($string !~ /^__ARRAY_REF__/) {
2293: if (! ($string eq '' || !defined($string))) {
2294: $array[0]='Array reference error';
2295: }
2296: return (\@array, $string);
2297: }
2298:
2299: $string =~ s/^__ARRAY_REF__//;
2300:
2301: while($string !~ /^__END_ARRAY_REF__/) {
2302: my $value='';
2303: if($string =~ /^__HASH_REF__/) {
2304: ($value, $string)=&str2hashref($string);
2305: if(defined($value->{'error'})) {
2306: $array[0] ='Array reference error';
2307: return (\@array, $string);
2308: }
2309: } elsif($string =~ /^__ARRAY_REF__/) {
2310: ($value, $string)=&str2arrayref($string);
2311: if($value->[0] eq 'Array reference error') {
2312: $array[0] ='Array reference error';
2313: return (\@array, $string);
2314: }
2315: } else {
2316: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
2317: }
2318: $string =~ s/^&//;
2319:
2320: push(@array, $value);
1.191 harris41 2321: }
1.265 albertel 2322:
2323: $string =~ s/^__END_ARRAY_REF__//;
2324:
2325: return (\@array, $string);
1.168 albertel 2326: }
2327:
1.167 albertel 2328: # -------------------------------------------------------------------Temp Store
2329:
1.168 albertel 2330: sub tmpreset {
2331: my ($symb,$namespace,$domain,$stuname) = @_;
2332: if (!$symb) {
2333: $symb=&symbread();
1.620 albertel 2334: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2335: }
2336: $symb=escape($symb);
2337:
1.620 albertel 2338: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 2339: $namespace=~s/\//\_/g;
2340: $namespace=~s/\W//g;
2341:
1.620 albertel 2342: if (!$domain) { $domain=$env{'user.domain'}; }
2343: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2344: if ($domain eq 'public' && $stuname eq 'public') {
2345: $stuname=$ENV{'REMOTE_ADDR'};
2346: }
1.168 albertel 2347: my $path=$perlvar{'lonDaemons'}.'/tmp';
2348: my %hash;
2349: if (tie(%hash,'GDBM_File',
2350: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2351: &GDBM_WRCREAT(),0640)) {
1.168 albertel 2352: foreach my $key (keys %hash) {
1.180 albertel 2353: if ($key=~ /:$symb/) {
1.168 albertel 2354: delete($hash{$key});
2355: }
2356: }
2357: }
2358: }
2359:
1.167 albertel 2360: sub tmpstore {
1.168 albertel 2361: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2362:
2363: if (!$symb) {
2364: $symb=&symbread();
1.620 albertel 2365: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2366: }
2367: $symb=escape($symb);
2368:
2369: if (!$namespace) {
2370: # I don't think we would ever want to store this for a course.
2371: # it seems this will only be used if we don't have a course.
1.620 albertel 2372: #$namespace=$env{'request.course.id'};
1.168 albertel 2373: #if (!$namespace) {
1.620 albertel 2374: $namespace=$env{'request.state'};
1.168 albertel 2375: #}
2376: }
2377: $namespace=~s/\//\_/g;
2378: $namespace=~s/\W//g;
1.620 albertel 2379: if (!$domain) { $domain=$env{'user.domain'}; }
2380: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2381: if ($domain eq 'public' && $stuname eq 'public') {
2382: $stuname=$ENV{'REMOTE_ADDR'};
2383: }
1.168 albertel 2384: my $now=time;
2385: my %hash;
2386: my $path=$perlvar{'lonDaemons'}.'/tmp';
2387: if (tie(%hash,'GDBM_File',
2388: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2389: &GDBM_WRCREAT(),0640)) {
1.168 albertel 2390: $hash{"version:$symb"}++;
2391: my $version=$hash{"version:$symb"};
2392: my $allkeys='';
2393: foreach my $key (keys(%$storehash)) {
2394: $allkeys.=$key.':';
1.591 albertel 2395: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 2396: }
2397: $hash{"$version:$symb:timestamp"}=$now;
2398: $allkeys.='timestamp';
2399: $hash{"$version:keys:$symb"}=$allkeys;
2400: if (untie(%hash)) {
2401: return 'ok';
2402: } else {
2403: return "error:$!";
2404: }
2405: } else {
2406: return "error:$!";
2407: }
2408: }
1.167 albertel 2409:
1.168 albertel 2410: # -----------------------------------------------------------------Temp Restore
1.167 albertel 2411:
1.168 albertel 2412: sub tmprestore {
2413: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 2414:
1.168 albertel 2415: if (!$symb) {
2416: $symb=&symbread();
1.620 albertel 2417: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2418: }
2419: $symb=escape($symb);
2420:
1.620 albertel 2421: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 2422:
1.620 albertel 2423: if (!$domain) { $domain=$env{'user.domain'}; }
2424: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2425: if ($domain eq 'public' && $stuname eq 'public') {
2426: $stuname=$ENV{'REMOTE_ADDR'};
2427: }
1.168 albertel 2428: my %returnhash;
2429: $namespace=~s/\//\_/g;
2430: $namespace=~s/\W//g;
2431: my %hash;
2432: my $path=$perlvar{'lonDaemons'}.'/tmp';
2433: if (tie(%hash,'GDBM_File',
2434: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2435: &GDBM_READER(),0640)) {
1.168 albertel 2436: my $version=$hash{"version:$symb"};
2437: $returnhash{'version'}=$version;
2438: my $scope;
2439: for ($scope=1;$scope<=$version;$scope++) {
2440: my $vkeys=$hash{"$scope:keys:$symb"};
2441: my @keys=split(/:/,$vkeys);
2442: my $key;
2443: $returnhash{"$scope:keys"}=$vkeys;
2444: foreach $key (@keys) {
1.591 albertel 2445: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
2446: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 2447: }
2448: }
1.168 albertel 2449: if (!(untie(%hash))) {
2450: return "error:$!";
2451: }
2452: } else {
2453: return "error:$!";
2454: }
2455: return %returnhash;
1.167 albertel 2456: }
2457:
1.9 www 2458: # ----------------------------------------------------------------------- Store
2459:
2460: sub store {
1.124 www 2461: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2462: my $home='';
2463:
1.168 albertel 2464: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2465:
1.213 www 2466: $symb=&symbclean($symb);
1.122 albertel 2467: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 2468:
1.620 albertel 2469: if (!$domain) { $domain=$env{'user.domain'}; }
2470: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 2471:
2472: &devalidate($symb,$stuname,$domain);
1.109 www 2473:
2474: $symb=escape($symb);
1.187 www 2475: if (!$namespace) {
1.620 albertel 2476: unless ($namespace=$env{'request.course.id'}) {
1.187 www 2477: return '';
2478: }
2479: }
1.620 albertel 2480: if (!$home) { $home=$env{'user.home'}; }
1.447 www 2481:
2482: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2483: $$storehash{'host'}=$perlvar{'lonHostID'};
2484:
1.12 www 2485: my $namevalue='';
1.191 harris41 2486: foreach (keys %$storehash) {
1.591 albertel 2487: $namevalue.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
1.191 harris41 2488: }
1.12 www 2489: $namevalue=~s/\&$//;
1.187 www 2490: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 2491: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 2492: }
2493:
1.47 www 2494: # -------------------------------------------------------------- Critical Store
2495:
2496: sub cstore {
1.124 www 2497: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2498: my $home='';
2499:
1.168 albertel 2500: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2501:
1.213 www 2502: $symb=&symbclean($symb);
1.122 albertel 2503: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 2504:
1.620 albertel 2505: if (!$domain) { $domain=$env{'user.domain'}; }
2506: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 2507:
2508: &devalidate($symb,$stuname,$domain);
1.109 www 2509:
2510: $symb=escape($symb);
1.187 www 2511: if (!$namespace) {
1.620 albertel 2512: unless ($namespace=$env{'request.course.id'}) {
1.187 www 2513: return '';
2514: }
2515: }
1.620 albertel 2516: if (!$home) { $home=$env{'user.home'}; }
1.447 www 2517:
2518: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2519: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 2520:
1.47 www 2521: my $namevalue='';
1.191 harris41 2522: foreach (keys %$storehash) {
1.591 albertel 2523: $namevalue.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
1.191 harris41 2524: }
1.47 www 2525: $namevalue=~s/\&$//;
1.187 www 2526: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 2527: return critical
2528: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 2529: }
2530:
1.9 www 2531: # --------------------------------------------------------------------- Restore
2532:
2533: sub restore {
1.124 www 2534: my ($symb,$namespace,$domain,$stuname) = @_;
2535: my $home='';
2536:
1.168 albertel 2537: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2538:
1.122 albertel 2539: if (!$symb) {
2540: unless ($symb=escape(&symbread())) { return ''; }
2541: } else {
1.213 www 2542: $symb=&escape(&symbclean($symb));
1.122 albertel 2543: }
1.188 www 2544: if (!$namespace) {
1.620 albertel 2545: unless ($namespace=$env{'request.course.id'}) {
1.188 www 2546: return '';
2547: }
2548: }
1.620 albertel 2549: if (!$domain) { $domain=$env{'user.domain'}; }
2550: if (!$stuname) { $stuname=$env{'user.name'}; }
2551: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 2552: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
2553:
1.12 www 2554: my %returnhash=();
1.191 harris41 2555: foreach (split(/\&/,$answer)) {
1.12 www 2556: my ($name,$value)=split(/\=/,$_);
1.591 albertel 2557: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 2558: }
1.75 www 2559: my $version;
2560: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.191 harris41 2561: foreach (split(/\:/,$returnhash{$version.':keys'})) {
1.75 www 2562: $returnhash{$_}=$returnhash{$version.':'.$_};
1.191 harris41 2563: }
1.75 www 2564: }
1.13 www 2565: return %returnhash;
1.34 www 2566: }
2567:
2568: # ---------------------------------------------------------- Course Description
2569:
2570: sub coursedescription {
2571: my $courseid=shift;
2572: $courseid=~s/^\///;
1.49 www 2573: $courseid=~s/\_/\//g;
1.34 www 2574: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 2575: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 2576: my $normalid=$cdomain.'_'.$cnum;
2577: # need to always cache even if we get errors otherwise we keep
2578: # trying and trying and trying to get the course description.
2579: my %envhash=();
2580: my %returnhash=();
2581: $envhash{'course.'.$normalid.'.last_cache'}=time;
1.34 www 2582: if ($chome ne 'no_host') {
1.302 albertel 2583: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 2584: if (!exists($returnhash{'con_lost'})) {
2585: $returnhash{'home'}= $chome;
2586: $returnhash{'domain'} = $cdomain;
2587: $returnhash{'num'} = $cnum;
1.130 albertel 2588: while (my ($name,$value) = each %returnhash) {
1.53 www 2589: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 2590: }
1.270 www 2591: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 2592: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 2593: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 2594: $envhash{'course.'.$normalid.'.home'}=$chome;
2595: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
2596: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 2597: }
2598: }
1.302 albertel 2599: &appenv(%envhash);
2600: return %returnhash;
1.461 www 2601: }
2602:
2603: # -------------------------------------------------See if a user is privileged
2604:
2605: sub privileged {
2606: my ($username,$domain)=@_;
2607: my $rolesdump=&reply("dump:$domain:$username:roles",
2608: &homeserver($username,$domain));
2609: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
2610: my $now=time;
2611: if ($rolesdump ne '') {
2612: foreach (split(/&/,$rolesdump)) {
1.586 albertel 2613: if ($_!~/^rolesdef_/) {
1.461 www 2614: my ($area,$role)=split(/=/,$_);
2615: $area=~s/\_\w\w$//;
2616: my ($trole,$tend,$tstart)=split(/_/,$role);
2617: if (($trole eq 'dc') || ($trole eq 'su')) {
2618: my $active=1;
2619: if ($tend) {
2620: if ($tend<$now) { $active=0; }
2621: }
2622: if ($tstart) {
2623: if ($tstart>$now) { $active=0; }
2624: }
2625: if ($active) { return 1; }
2626: }
2627: }
2628: }
2629: }
2630: return 0;
1.9 www 2631: }
1.1 albertel 2632:
1.103 harris41 2633: # -------------------------------------------------------- Get user privileges
1.11 www 2634:
2635: sub rolesinit {
2636: my ($domain,$username,$authhost)=@_;
2637: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12 www 2638: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11 www 2639: my %allroles=();
1.678 raeburn 2640: my %allgroups=();
1.11 www 2641: my $now=time;
1.21 www 2642: my $userroles="user.login.time=$now\n";
1.678 raeburn 2643: my $group_privs;
1.11 www 2644:
2645: if ($rolesdump ne '') {
1.191 harris41 2646: foreach (split(/&/,$rolesdump)) {
1.586 albertel 2647: if ($_!~/^rolesdef_/) {
1.11 www 2648: my ($area,$role)=split(/=/,$_);
1.587 albertel 2649: $area=~s/\_\w\w$//;
1.678 raeburn 2650: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 2651: if ($role=~/^cr/) {
1.655 albertel 2652: if ($role=~m|^(cr/\w+/\w+/[a-zA-Z0-9]+)_(.*)$|) {
2653: ($trole,my $trest)=($role=~m|^(cr/\w+/\w+/[a-zA-Z0-9]+)_(.*)$|);
2654: ($tend,$tstart)=split('_',$trest);
2655: } else {
2656: $trole=$role;
2657: }
1.678 raeburn 2658: } elsif ($role =~ m|^gr/|) {
2659: ($trole,$tend,$tstart) = split(/_/,$role);
2660: ($trole,$group_privs) = split(/\//,$trole);
2661: $group_privs = &unescape($group_privs);
1.587 albertel 2662: } else {
2663: ($trole,$tend,$tstart)=split(/_/,$role);
2664: }
1.576 albertel 2665: $userroles.=&set_arearole($trole,$area,$tstart,$tend,$domain,$username);
1.567 raeburn 2666: if (($tend!=0) && ($tend<$now)) { $trole=''; }
2667: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 2668: if (($area ne '') && ($trole ne '')) {
1.347 albertel 2669: my $spec=$trole.'.'.$area;
2670: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
2671: if ($trole =~ /^cr\//) {
1.567 raeburn 2672: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 2673: } elsif ($trole eq 'gr') {
2674: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 2675: } else {
1.567 raeburn 2676: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 2677: }
1.12 www 2678: }
1.662 raeburn 2679: }
1.191 harris41 2680: }
1.678 raeburn 2681: my ($author,$adv) = &set_userprivs(\$userroles,\%allroles,\%allgroups);
1.128 www 2682: $userroles.='user.adv='.$adv."\n".
2683: 'user.author='.$author."\n";
1.620 albertel 2684: $env{'user.adv'}=$adv;
1.11 www 2685: }
2686: return $userroles;
2687: }
2688:
1.567 raeburn 2689: sub set_arearole {
2690: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
2691: # log the associated role with the area
2692: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
2693: return 'user.role.'.$trole.'.'.$area.'='.$tstart.'.'.$tend."\n";
2694: }
2695:
2696: sub custom_roleprivs {
2697: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
2698: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
2699: my $homsvr=homeserver($rauthor,$rdomain);
2700: if ($hostname{$homsvr} ne '') {
2701: my ($rdummy,$roledef)=
2702: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
2703: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
2704: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
2705: if (defined($syspriv)) {
2706: $$allroles{'cm./'}.=':'.$syspriv;
2707: $$allroles{$spec.'./'}.=':'.$syspriv;
2708: }
2709: if ($tdomain ne '') {
2710: if (defined($dompriv)) {
2711: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
2712: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
2713: }
2714: if (($trest ne '') && (defined($coursepriv))) {
2715: $$allroles{'cm.'.$area}.=':'.$coursepriv;
2716: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
2717: }
2718: }
2719: }
2720: }
2721: }
2722:
1.678 raeburn 2723: sub group_roleprivs {
2724: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
2725: my $access = 1;
2726: my $now = time;
2727: if (($tend!=0) && ($tend<$now)) { $access = 0; }
2728: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
2729: if ($access) {
2730: my ($course,$group) = ($area =~ m|(/\w+/\w+)/([^/]+)$|);
2731: $$allgroups{$course}{$group} .=':'.$group_privs;
2732: }
2733: }
1.567 raeburn 2734:
2735: sub standard_roleprivs {
2736: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
2737: if (defined($pr{$trole.':s'})) {
2738: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
2739: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
2740: }
2741: if ($tdomain ne '') {
2742: if (defined($pr{$trole.':d'})) {
2743: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
2744: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
2745: }
2746: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
2747: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
2748: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
2749: }
2750: }
2751: }
2752:
2753: sub set_userprivs {
1.678 raeburn 2754: my ($userroles,$allroles,$allgroups) = @_;
1.567 raeburn 2755: my $author=0;
2756: my $adv=0;
1.678 raeburn 2757: my %grouproles = ();
2758: if (keys(%{$allgroups}) > 0) {
2759: foreach my $role (keys %{$allroles}) {
1.681 raeburn 2760: my ($trole,$area,$sec,$extendedarea);
2761: if ($role =~ m|^(\w+)\.(/\w+/\w+)(/?\w*)|) {
1.678 raeburn 2762: $trole = $1;
2763: $area = $2;
1.681 raeburn 2764: $sec = $3;
2765: $extendedarea = $area.$sec;
2766: if (exists($$allgroups{$area})) {
2767: foreach my $group (keys(%{$$allgroups{$area}})) {
2768: my $spec = $trole.'.'.$extendedarea;
2769: $grouproles{$spec.'.'.$area.'/'.$group} =
2770: $$allgroups{$area}{$group};
1.678 raeburn 2771: }
2772: }
2773: }
2774: }
2775: }
2776: foreach (keys(%grouproles)) {
2777: $$allroles{$_} = $grouproles{$_};
2778: }
1.567 raeburn 2779: foreach (keys %{$allroles}) {
2780: my %thesepriv=();
2781: if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
2782: foreach (split(/:/,$$allroles{$_})) {
2783: if ($_ ne '') {
2784: my ($privilege,$restrictions)=split(/&/,$_);
2785: if ($restrictions eq '') {
2786: $thesepriv{$privilege}='F';
2787: } elsif ($thesepriv{$privilege} ne 'F') {
2788: $thesepriv{$privilege}.=$restrictions;
2789: }
2790: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
2791: }
2792: }
2793: my $thesestr='';
2794: foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
2795: $$userroles.='user.priv.'.$_.'='.$thesestr."\n";
2796: }
2797: return ($author,$adv);
2798: }
2799:
1.12 www 2800: # --------------------------------------------------------------- get interface
2801:
2802: sub get {
1.131 albertel 2803: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 2804: my $items='';
1.191 harris41 2805: foreach (@$storearr) {
1.12 www 2806: $items.=escape($_).'&';
1.191 harris41 2807: }
1.12 www 2808: $items=~s/\&$//;
1.620 albertel 2809: if (!$udomain) { $udomain=$env{'user.domain'}; }
2810: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 2811: my $uhome=&homeserver($uname,$udomain);
2812:
1.133 albertel 2813: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 2814: my @pairs=split(/\&/,$rep);
1.273 albertel 2815: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
2816: return @pairs;
2817: }
1.15 www 2818: my %returnhash=();
1.42 www 2819: my $i=0;
1.191 harris41 2820: foreach (@$storearr) {
1.557 albertel 2821: $returnhash{$_}=&thaw_unescape($pairs[$i]);
1.42 www 2822: $i++;
1.191 harris41 2823: }
1.15 www 2824: return %returnhash;
1.27 www 2825: }
2826:
2827: # --------------------------------------------------------------- del interface
2828:
2829: sub del {
1.133 albertel 2830: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 2831: my $items='';
1.191 harris41 2832: foreach (@$storearr) {
1.27 www 2833: $items.=escape($_).'&';
1.191 harris41 2834: }
1.27 www 2835: $items=~s/\&$//;
1.620 albertel 2836: if (!$udomain) { $udomain=$env{'user.domain'}; }
2837: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 2838: my $uhome=&homeserver($uname,$udomain);
2839:
2840: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 2841: }
2842:
2843: # -------------------------------------------------------------- dump interface
2844:
2845: sub dump {
1.702 albertel 2846: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.620 albertel 2847: if (!$udomain) { $udomain=$env{'user.domain'}; }
2848: if (!$uname) { $uname=$env{'user.name'}; }
1.129 albertel 2849: my $uhome=&homeserver($uname,$udomain);
1.193 www 2850: if ($regexp) {
2851: $regexp=&escape($regexp);
2852: } else {
2853: $regexp='.';
2854: }
1.702 albertel 2855: my $rep=reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.12 www 2856: my @pairs=split(/\&/,$rep);
2857: my %returnhash=();
1.191 harris41 2858: foreach (@pairs) {
1.702 albertel 2859: my ($key,$value)=split(/=/,$_,2);
1.557 albertel 2860: $returnhash{unescape($key)}=&thaw_unescape($value);
1.318 matthew 2861: }
2862: return %returnhash;
1.407 www 2863: }
2864:
1.717 albertel 2865: # --------------------------------------------------------- dumpstore interface
2866:
2867: sub dumpstore {
2868: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
2869: return &dump($namespace,$udomain,$uname,$regexp,$range);
2870: }
2871:
1.407 www 2872: # -------------------------------------------------------------- keys interface
2873:
2874: sub getkeys {
2875: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 2876: if (!$udomain) { $udomain=$env{'user.domain'}; }
2877: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 2878: my $uhome=&homeserver($uname,$udomain);
2879: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
2880: my @keyarray=();
2881: foreach (split(/\&/,$rep)) {
2882: push (@keyarray,&unescape($_));
2883: }
2884: return @keyarray;
1.318 matthew 2885: }
2886:
1.319 matthew 2887: # --------------------------------------------------------------- currentdump
2888: sub currentdump {
1.328 matthew 2889: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 2890: $courseid = $env{'request.course.id'} if (! defined($courseid));
2891: $sdom = $env{'user.domain'} if (! defined($sdom));
2892: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 2893: my $uhome = &homeserver($sname,$sdom);
2894: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 2895: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 2896: #
1.318 matthew 2897: my %returnhash=();
1.319 matthew 2898: #
2899: if ($rep eq "unknown_cmd") {
2900: # an old lond will not know currentdump
2901: # Do a dump and make it look like a currentdump
1.326 matthew 2902: my @tmp = &dump($courseid,$sdom,$sname,'.');
1.319 matthew 2903: return if ($tmp[0] =~ /^(error:|no_such_host)/);
2904: my %hash = @tmp;
2905: @tmp=();
1.424 matthew 2906: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 2907: } else {
2908: my @pairs=split(/\&/,$rep);
2909: foreach (@pairs) {
2910: my ($key,$value)=split(/=/,$_);
2911: my ($symb,$param) = split(/:/,$key);
2912: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 2913: &thaw_unescape($value);
1.319 matthew 2914: }
1.191 harris41 2915: }
1.12 www 2916: return %returnhash;
1.424 matthew 2917: }
2918:
2919: sub convert_dump_to_currentdump{
2920: my %hash = %{shift()};
2921: my %returnhash;
2922: # Code ripped from lond, essentially. The only difference
2923: # here is the unescaping done by lonnet::dump(). Conceivably
2924: # we might run in to problems with parameter names =~ /^v\./
2925: while (my ($key,$value) = each(%hash)) {
2926: my ($v,$symb,$param) = split(/:/,$key);
2927: next if ($v eq 'version' || $symb eq 'keys');
2928: next if (exists($returnhash{$symb}) &&
2929: exists($returnhash{$symb}->{$param}) &&
2930: $returnhash{$symb}->{'v.'.$param} > $v);
2931: $returnhash{$symb}->{$param}=$value;
2932: $returnhash{$symb}->{'v.'.$param}=$v;
2933: }
2934: #
2935: # Remove all of the keys in the hashes which keep track of
2936: # the version of the parameter.
2937: while (my ($symb,$param_hash) = each(%returnhash)) {
2938: # use a foreach because we are going to delete from the hash.
2939: foreach my $key (keys(%$param_hash)) {
2940: delete($param_hash->{$key}) if ($key =~ /^v\./);
2941: }
2942: }
2943: return \%returnhash;
1.12 www 2944: }
2945:
1.627 albertel 2946: # ------------------------------------------------------ critical inc interface
2947:
2948: sub cinc {
2949: return &inc(@_,'critical');
2950: }
2951:
1.449 matthew 2952: # --------------------------------------------------------------- inc interface
2953:
2954: sub inc {
1.627 albertel 2955: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 2956: if (!$udomain) { $udomain=$env{'user.domain'}; }
2957: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 2958: my $uhome=&homeserver($uname,$udomain);
2959: my $items='';
2960: if (! ref($store)) {
2961: # got a single value, so use that instead
2962: $items = &escape($store).'=&';
2963: } elsif (ref($store) eq 'SCALAR') {
2964: $items = &escape($$store).'=&';
2965: } elsif (ref($store) eq 'ARRAY') {
2966: $items = join('=&',map {&escape($_);} @{$store});
2967: } elsif (ref($store) eq 'HASH') {
2968: while (my($key,$value) = each(%{$store})) {
2969: $items.= &escape($key).'='.&escape($value).'&';
2970: }
2971: }
2972: $items=~s/\&$//;
1.627 albertel 2973: if ($critical) {
2974: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
2975: } else {
2976: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
2977: }
1.449 matthew 2978: }
2979:
1.12 www 2980: # --------------------------------------------------------------- put interface
2981:
2982: sub put {
1.134 albertel 2983: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 2984: if (!$udomain) { $udomain=$env{'user.domain'}; }
2985: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 2986: my $uhome=&homeserver($uname,$udomain);
1.12 www 2987: my $items='';
1.191 harris41 2988: foreach (keys %$storehash) {
1.557 albertel 2989: $items.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
1.191 harris41 2990: }
1.12 www 2991: $items=~s/\&$//;
1.134 albertel 2992: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 2993: }
2994:
1.631 albertel 2995: # ------------------------------------------------------------ newput interface
2996:
2997: sub newput {
2998: my ($namespace,$storehash,$udomain,$uname)=@_;
2999: if (!$udomain) { $udomain=$env{'user.domain'}; }
3000: if (!$uname) { $uname=$env{'user.name'}; }
3001: my $uhome=&homeserver($uname,$udomain);
3002: my $items='';
3003: foreach my $key (keys(%$storehash)) {
3004: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
3005: }
3006: $items=~s/\&$//;
3007: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
3008: }
3009:
3010: # --------------------------------------------------------- putstore interface
3011:
1.524 raeburn 3012: sub putstore {
1.715 albertel 3013: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 3014: if (!$udomain) { $udomain=$env{'user.domain'}; }
3015: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 3016: my $uhome=&homeserver($uname,$udomain);
3017: my $items='';
1.715 albertel 3018: foreach my $key (keys(%$storehash)) {
3019: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 3020: }
1.715 albertel 3021: $items=~s/\&$//;
1.716 albertel 3022: my $esc_symb=&escape($symb);
3023: my $esc_v=&escape($version);
1.715 albertel 3024: my $reply =
1.716 albertel 3025: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 3026: $uhome);
3027: if ($reply eq 'unknown_cmd') {
1.716 albertel 3028: # gfall back to way things use to be done
1.715 albertel 3029: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
3030: $uname);
1.524 raeburn 3031: }
1.715 albertel 3032: return $reply;
3033: }
3034:
3035: sub old_putstore {
1.716 albertel 3036: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
3037: if (!$udomain) { $udomain=$env{'user.domain'}; }
3038: if (!$uname) { $uname=$env{'user.name'}; }
3039: my $uhome=&homeserver($uname,$udomain);
3040: my %newstorehash;
3041: foreach (keys %$storehash) {
3042: my $key = $version.':'.&escape($symb).':'.$_;
3043: $newstorehash{$key} = $storehash->{$_};
3044: }
3045: my $items='';
3046: my %allitems = ();
3047: foreach (keys %newstorehash) {
3048: if ($_ =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
3049: my $key = $1.':keys:'.$2;
3050: $allitems{$key} .= $3.':';
3051: }
3052: $items.=$_.'='.&freeze_escape($newstorehash{$_}).'&';
3053: }
3054: foreach (keys %allitems) {
3055: $allitems{$_} =~ s/\:$//;
3056: $items.= $_.'='.$allitems{$_}.'&';
3057: }
3058: $items=~s/\&$//;
3059: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 3060: }
3061:
1.47 www 3062: # ------------------------------------------------------ critical put interface
3063:
3064: sub cput {
1.134 albertel 3065: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 3066: if (!$udomain) { $udomain=$env{'user.domain'}; }
3067: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 3068: my $uhome=&homeserver($uname,$udomain);
1.47 www 3069: my $items='';
1.191 harris41 3070: foreach (keys %$storehash) {
1.715 albertel 3071: $items.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
1.191 harris41 3072: }
1.47 www 3073: $items=~s/\&$//;
1.134 albertel 3074: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3075: }
3076:
3077: # -------------------------------------------------------------- eget interface
3078:
3079: sub eget {
1.133 albertel 3080: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 3081: my $items='';
1.191 harris41 3082: foreach (@$storearr) {
1.12 www 3083: $items.=escape($_).'&';
1.191 harris41 3084: }
1.12 www 3085: $items=~s/\&$//;
1.620 albertel 3086: if (!$udomain) { $udomain=$env{'user.domain'}; }
3087: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 3088: my $uhome=&homeserver($uname,$udomain);
3089: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3090: my @pairs=split(/\&/,$rep);
3091: my %returnhash=();
1.42 www 3092: my $i=0;
1.191 harris41 3093: foreach (@$storearr) {
1.557 albertel 3094: $returnhash{$_}=&thaw_unescape($pairs[$i]);
1.42 www 3095: $i++;
1.191 harris41 3096: }
1.12 www 3097: return %returnhash;
3098: }
3099:
1.667 albertel 3100: # ------------------------------------------------------------ tmpput interface
3101: sub tmpput {
3102: my ($storehash,$server)=@_;
3103: my $items='';
3104: foreach (keys(%$storehash)) {
3105: $items.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
3106: }
3107: $items=~s/\&$//;
3108: return &reply("tmpput:$items",$server);
3109: }
3110:
3111: # ------------------------------------------------------------ tmpget interface
3112: sub tmpget {
1.688 albertel 3113: my ($token,$server)=@_;
3114: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3115: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 3116: my %returnhash;
3117: foreach my $item (split(/\&/,$rep)) {
3118: my ($key,$value)=split(/=/,$item);
3119: $returnhash{&unescape($key)}=&thaw_unescape($value);
3120: }
3121: return %returnhash;
3122: }
3123:
1.688 albertel 3124: # ------------------------------------------------------------ tmpget interface
3125: sub tmpdel {
3126: my ($token,$server)=@_;
3127: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3128: return &reply("tmpdel:$token",$server);
3129: }
3130:
1.341 www 3131: # ---------------------------------------------- Custom access rule evaluation
3132:
3133: sub customaccess {
3134: my ($priv,$uri)=@_;
1.620 albertel 3135: my ($urole,$urealm)=split(/\./,$env{'request.role'});
1.343 www 3136: $urealm=~s/^\W//;
3137: my ($udom,$ucrs,$usec)=split(/\//,$urealm);
1.341 www 3138: my $access=0;
3139: foreach (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.342 www 3140: my ($effect,$realm,$role)=split(/\:/,$_);
1.343 www 3141: if ($role) {
3142: if ($role ne $urole) { next; }
3143: }
3144: foreach (split(/\s*\,\s*/,$realm)) {
3145: my ($tdom,$tcrs,$tsec)=split(/\_/,$_);
3146: if ($tdom) {
3147: if ($tdom ne $udom) { next; }
3148: }
3149: if ($tcrs) {
3150: if ($tcrs ne $ucrs) { next; }
3151: }
3152: if ($tsec) {
3153: if ($tsec ne $usec) { next; }
3154: }
3155: $access=($effect eq 'allow');
3156: last;
1.342 www 3157: }
1.402 bowersj2 3158: if ($realm eq '' && $role eq '') {
3159: $access=($effect eq 'allow');
3160: }
1.341 www 3161: }
3162: return $access;
3163: }
3164:
1.103 harris41 3165: # ------------------------------------------------- Check for a user privilege
1.12 www 3166:
3167: sub allowed {
1.579 albertel 3168: my ($priv,$uri,$symb)=@_;
1.705 albertel 3169: my $ver_orguri=$uri;
1.439 www 3170: $uri=&deversion($uri);
1.152 www 3171: my $orguri=$uri;
1.52 www 3172: $uri=&declutter($uri);
1.545 banghart 3173:
1.620 albertel 3174: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 3175: # Free bre access to adm and meta resources
1.529 albertel 3176: if (((($uri=~/^adm\//) && ($uri !~ m|/bulletinboard$|))
3177: || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14 www 3178: return 'F';
1.159 www 3179: }
3180:
1.545 banghart 3181: # Free bre access to user's own portfolio contents
1.714 raeburn 3182: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 3183: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 3184: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.545 banghart 3185: return 'F';
3186: }
3187:
1.714 raeburn 3188: # bre access to group if user has rgf priv for this group and course.
3189: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
3190: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
3191: if (exists($env{'request.course.id'})) {
3192: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
3193: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
3194: if (($domain eq $cdom) && ($name eq $cnum)) {
3195: my $courseprivid=$env{'request.course.id'};
3196: $courseprivid=~s/\_/\//;
3197: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
3198: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
3199: return $1;
3200: }
3201: }
3202: }
3203: }
3204:
1.159 www 3205: # Free bre to public access
3206:
3207: if ($priv eq 'bre') {
1.238 www 3208: my $copyright=&metadata($uri,'copyright');
1.620 albertel 3209: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 3210: return 'F';
3211: }
1.238 www 3212: if ($copyright eq 'priv') {
3213: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 3214: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 3215: return '';
3216: }
3217: }
3218: if ($copyright eq 'domain') {
3219: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 3220: unless (($env{'user.domain'} eq $1) ||
3221: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 3222: return '';
3223: }
1.262 matthew 3224: }
1.620 albertel 3225: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 3226: # Library role, so allow browsing of resources in this domain.
3227: return 'F';
1.238 www 3228: }
1.341 www 3229: if ($copyright eq 'custom') {
3230: unless (&customaccess($priv,$uri)) { return ''; }
3231: }
1.14 www 3232: }
1.264 matthew 3233: # Domain coordinator is trying to create a course
1.620 albertel 3234: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 3235: # uri is the requested domain in this case.
3236: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 3237: # a role of dc for the domain in question.
1.620 albertel 3238: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 3239: }
1.29 www 3240:
1.52 www 3241: my $thisallowed='';
3242: my $statecond=0;
3243: my $courseprivid='';
3244:
3245: # Course
3246:
1.620 albertel 3247: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52 www 3248: $thisallowed.=$1;
3249: }
1.29 www 3250:
1.52 www 3251: # Domain
3252:
1.620 albertel 3253: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 3254: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 3255: $thisallowed.=$1;
3256: }
1.52 www 3257:
3258: # Course: uri itself is a course
1.66 www 3259: my $courseuri=$uri;
3260: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 3261: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 3262:
1.620 albertel 3263: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 3264: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 3265: $thisallowed.=$1;
3266: }
1.29 www 3267:
1.678 raeburn 3268: # Group: uri itself is a group
3269: my $groupuri=$uri;
3270: $groupuri=~s/^([^\/])/\/$1/;
3271: if ($env{'user.priv.'.$env{'request.role'}.'.'.$groupuri}
3272: =~/\Q$priv\E\&([^\:]*)/) {
3273: $thisallowed.=$1;
3274: }
3275:
1.665 albertel 3276: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 3277: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 3278: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 3279: $thisallowed='';
1.671 raeburn 3280: my ($match)=&is_on_map($uri);
3281: if ($match) {
3282: if ($env{'user.priv.'.$env{'request.role'}.'./'}
3283: =~/\Q$priv\E\&([^\:]*)/) {
3284: $thisallowed.=$1;
3285: }
3286: } else {
1.705 albertel 3287: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 3288: if ($refuri) {
3289: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 3290: $thisallowed='F';
1.671 raeburn 3291: } else {
3292: $refuri=&declutter($refuri);
3293: my ($match) = &is_on_map($refuri);
3294: if ($match) {
3295: $thisallowed='F';
3296: }
1.669 raeburn 3297: }
1.671 raeburn 3298: }
3299: }
1.314 www 3300: }
1.492 albertel 3301:
1.52 www 3302: # Full access at system, domain or course-wide level? Exit.
1.29 www 3303:
3304: if ($thisallowed=~/F/) {
3305: return 'F';
3306: }
3307:
1.52 www 3308: # If this is generating or modifying users, exit with special codes
1.29 www 3309:
1.643 www 3310: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
3311: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 3312: my ($audom,$auname)=split('/',$uri);
1.643 www 3313: # no author name given, so this just checks on the general right to make a co-author in this domain
3314: unless ($auname) { return $thisallowed; }
3315: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 3316: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
3317: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
3318: ($audom ne $env{'request.role.domain'}))) { return ''; }
3319: }
1.52 www 3320: return $thisallowed;
3321: }
3322: #
1.103 harris41 3323: # Gathered so far: system, domain and course wide privileges
1.52 www 3324: #
3325: # Course: See if uri or referer is an individual resource that is part of
3326: # the course
3327:
1.620 albertel 3328: if ($env{'request.course.id'}) {
1.232 www 3329:
1.620 albertel 3330: $courseprivid=$env{'request.course.id'};
3331: if ($env{'request.course.sec'}) {
3332: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 3333: }
3334: $courseprivid=~s/\_/\//;
3335: my $checkreferer=1;
1.232 www 3336: my ($match,$cond)=&is_on_map($uri);
3337: if ($match) {
3338: $statecond=$cond;
1.620 albertel 3339: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 3340: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 3341: $thisallowed.=$1;
3342: $checkreferer=0;
3343: }
1.29 www 3344: }
1.83 www 3345:
1.148 www 3346: if ($checkreferer) {
1.620 albertel 3347: my $refuri=$env{'httpref.'.$orguri};
1.148 www 3348: unless ($refuri) {
1.620 albertel 3349: foreach (keys %env) {
1.148 www 3350: if ($_=~/^httpref\..*\*/) {
3351: my $pattern=$_;
1.156 www 3352: $pattern=~s/^httpref\.\/res\///;
1.148 www 3353: $pattern=~s/\*/\[\^\/\]\+/g;
3354: $pattern=~s/\//\\\//g;
1.152 www 3355: if ($orguri=~/$pattern/) {
1.620 albertel 3356: $refuri=$env{$_};
1.148 www 3357: }
3358: }
1.191 harris41 3359: }
1.148 www 3360: }
1.232 www 3361:
1.148 www 3362: if ($refuri) {
1.152 www 3363: $refuri=&declutter($refuri);
1.232 www 3364: my ($match,$cond)=&is_on_map($refuri);
3365: if ($match) {
3366: my $refstatecond=$cond;
1.620 albertel 3367: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 3368: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 3369: $thisallowed.=$1;
1.53 www 3370: $uri=$refuri;
3371: $statecond=$refstatecond;
1.52 www 3372: }
3373: }
1.148 www 3374: }
1.29 www 3375: }
1.52 www 3376: }
1.29 www 3377:
1.52 www 3378: #
1.103 harris41 3379: # Gathered now: all privileges that could apply, and condition number
1.52 www 3380: #
3381: #
3382: # Full or no access?
3383: #
1.29 www 3384:
1.52 www 3385: if ($thisallowed=~/F/) {
3386: return 'F';
3387: }
1.29 www 3388:
1.52 www 3389: unless ($thisallowed) {
3390: return '';
3391: }
1.29 www 3392:
1.52 www 3393: # Restrictions exist, deal with them
3394: #
3395: # C:according to course preferences
3396: # R:according to resource settings
3397: # L:unless locked
3398: # X:according to user session state
3399: #
3400:
3401: # Possibly locked functionality, check all courses
1.54 www 3402: # Locks might take effect only after 10 minutes cache expiration for other
3403: # courses, and 2 minutes for current course
1.52 www 3404:
3405: my $envkey;
3406: if ($thisallowed=~/L/) {
1.620 albertel 3407: foreach $envkey (keys %env) {
1.54 www 3408: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
3409: my $courseid=$2;
3410: my $roleid=$1.'.'.$2;
1.92 www 3411: $courseid=~s/^\///;
1.54 www 3412: my $expiretime=600;
1.620 albertel 3413: if ($env{'request.role'} eq $roleid) {
1.54 www 3414: $expiretime=120;
3415: }
3416: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
3417: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 3418: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.54 www 3419: &coursedescription($courseid);
3420: }
1.620 albertel 3421: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
3422: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
3423: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
3424: &log($env{'user.domain'},$env{'user.name'},
3425: $env{'user.home'},
1.57 www 3426: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 3427: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 3428: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 3429: return '';
3430: }
3431: }
1.620 albertel 3432: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
3433: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
3434: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
3435: &log($env{'user.domain'},$env{'user.name'},
3436: $env{'user.home'},
1.57 www 3437: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 3438: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 3439: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 3440: return '';
3441: }
3442: }
3443: }
1.29 www 3444: }
1.52 www 3445: }
3446:
3447: #
3448: # Rest of the restrictions depend on selected course
3449: #
3450:
1.620 albertel 3451: unless ($env{'request.course.id'}) {
1.52 www 3452: return '1';
3453: }
1.29 www 3454:
1.52 www 3455: #
3456: # Now user is definitely in a course
3457: #
1.53 www 3458:
3459:
3460: # Course preferences
3461:
3462: if ($thisallowed=~/C/) {
1.620 albertel 3463: my $rolecode=(split(/\./,$env{'request.role'}))[0];
3464: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
3465: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 3466: =~/\Q$rolecode\E/) {
1.689 albertel 3467: if ($priv ne 'pch') {
3468: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
3469: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
3470: $env{'request.course.id'});
3471: }
1.237 www 3472: return '';
3473: }
3474:
1.620 albertel 3475: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 3476: =~/\Q$unamedom\E/) {
1.689 albertel 3477: if ($priv ne 'pch') {
3478: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
3479: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
3480: $env{'request.course.id'});
3481: }
1.54 www 3482: return '';
3483: }
1.53 www 3484: }
3485:
3486: # Resource preferences
3487:
3488: if ($thisallowed=~/R/) {
1.620 albertel 3489: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 3490: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689 albertel 3491: if ($priv ne 'pch') {
3492: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
3493: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
3494: }
3495: return '';
1.54 www 3496: }
1.53 www 3497: }
1.30 www 3498:
1.246 www 3499: # Restricted by state or randomout?
1.30 www 3500:
1.52 www 3501: if ($thisallowed=~/X/) {
1.620 albertel 3502: if ($env{'acc.randomout'}) {
1.579 albertel 3503: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 3504: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 3505: return '';
3506: }
1.247 www 3507: }
3508: if (&condval($statecond)) {
1.52 www 3509: return '2';
3510: } else {
3511: return '';
3512: }
3513: }
1.30 www 3514:
1.52 www 3515: return 'F';
1.232 www 3516: }
3517:
1.710 albertel 3518: sub split_uri_for_cond {
3519: my $uri=&deversion(&declutter(shift));
3520: my @uriparts=split(/\//,$uri);
3521: my $filename=pop(@uriparts);
3522: my $pathname=join('/',@uriparts);
3523: return ($pathname,$filename);
3524: }
1.232 www 3525: # --------------------------------------------------- Is a resource on the map?
3526:
3527: sub is_on_map {
1.710 albertel 3528: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 3529: #Trying to find the conditional for the file
1.620 albertel 3530: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 3531: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 3532: if ($match) {
1.289 bowersj2 3533: return (1,$1);
3534: } else {
1.434 www 3535: return (0,0);
1.289 bowersj2 3536: }
1.12 www 3537: }
3538:
1.427 www 3539: # --------------------------------------------------------- Get symb from alias
3540:
3541: sub get_symb_from_alias {
3542: my $symb=shift;
3543: my ($map,$resid,$url)=&decode_symb($symb);
3544: # Already is a symb
3545: if ($url) { return $symb; }
3546: # Must be an alias
3547: my $aliassymb='';
3548: my %bighash;
1.620 albertel 3549: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 3550: &GDBM_READER(),0640)) {
3551: my $rid=$bighash{'mapalias_'.$symb};
3552: if ($rid) {
3553: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 3554: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
3555: $resid,$bighash{'src_'.$rid});
1.427 www 3556: }
3557: untie %bighash;
3558: }
3559: return $aliassymb;
3560: }
3561:
1.12 www 3562: # ----------------------------------------------------------------- Define Role
3563:
3564: sub definerole {
3565: if (allowed('mcr','/')) {
3566: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.392 www 3567: foreach (split(':',$sysrole)) {
1.21 www 3568: my ($crole,$cqual)=split(/\&/,$_);
1.479 albertel 3569: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
3570: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
3571: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 3572: return "refused:s:$crole&$cqual";
3573: }
3574: }
1.191 harris41 3575: }
1.392 www 3576: foreach (split(':',$domrole)) {
1.21 www 3577: my ($crole,$cqual)=split(/\&/,$_);
1.479 albertel 3578: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
3579: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
3580: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 3581: return "refused:d:$crole&$cqual";
3582: }
3583: }
1.191 harris41 3584: }
1.392 www 3585: foreach (split(':',$courole)) {
1.21 www 3586: my ($crole,$cqual)=split(/\&/,$_);
1.479 albertel 3587: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
3588: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
3589: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 3590: return "refused:c:$crole&$cqual";
3591: }
3592: }
1.191 harris41 3593: }
1.620 albertel 3594: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
3595: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 3596: "rolesdef_$rolename=".
3597: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 3598: return reply($command,$env{'user.home'});
1.12 www 3599: } else {
3600: return 'refused';
3601: }
1.105 harris41 3602: }
3603:
3604: # ---------------- Make a metadata query against the network of library servers
3605:
3606: sub metadata_query {
1.244 matthew 3607: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 3608: my %rhash;
1.244 matthew 3609: my @server_list = (defined($server_array) ? @$server_array
3610: : keys(%libserv) );
3611: for my $server (@server_list) {
1.118 harris41 3612: unless ($custom or $customshow) {
3613: my $reply=&reply("querysend:".&escape($query),$server);
3614: $rhash{$server}=$reply;
3615: }
3616: else {
3617: my $reply=&reply("querysend:".&escape($query).':'.
3618: &escape($custom).':'.&escape($customshow),
3619: $server);
3620: $rhash{$server}=$reply;
3621: }
1.112 harris41 3622: }
1.118 harris41 3623: return \%rhash;
1.240 www 3624: }
3625:
3626: # ----------------------------------------- Send log queries and wait for reply
3627:
3628: sub log_query {
3629: my ($uname,$udom,$query,%filters)=@_;
3630: my $uhome=&homeserver($uname,$udom);
3631: if ($uhome eq 'no_host') { return 'error: no_host'; }
3632: my $uhost=$hostname{$uhome};
1.241 www 3633: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
1.240 www 3634: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
3635: $uhome);
1.479 albertel 3636: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 3637: return get_query_reply($queryid);
3638: }
3639:
1.508 raeburn 3640: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 3641:
3642: sub fetch_enrollment_query {
1.511 raeburn 3643: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 3644: my $homeserver;
1.547 raeburn 3645: my $maxtries = 1;
1.508 raeburn 3646: if ($context eq 'automated') {
3647: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 3648: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 3649: } else {
3650: $homeserver = &homeserver($cnum,$dom);
3651: }
1.506 raeburn 3652: my $host=$hostname{$homeserver};
3653: my $cmd = '';
3654: foreach (keys %{$affiliatesref}) {
1.508 raeburn 3655: $cmd .= $_.'='.join(",",@{$$affiliatesref{$_}}).'%%';
1.506 raeburn 3656: }
3657: $cmd =~ s/%%$//;
3658: $cmd = &escape($cmd);
3659: my $query = 'fetchenrollment';
1.620 albertel 3660: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 3661: unless ($queryid=~/^\Q$host\E\_/) {
3662: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
3663: return 'error: '.$queryid;
3664: }
1.506 raeburn 3665: my $reply = &get_query_reply($queryid);
1.547 raeburn 3666: my $tries = 1;
3667: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
3668: $reply = &get_query_reply($queryid);
3669: $tries ++;
3670: }
1.526 raeburn 3671: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 3672: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 3673: } else {
1.515 raeburn 3674: my @responses = split/:/,$reply;
3675: if ($homeserver eq $perlvar{'lonHostID'}) {
3676: foreach (@responses) {
3677: my ($key,$value) = split/=/,$_;
3678: $$replyref{$key} = $value;
3679: }
3680: } else {
1.506 raeburn 3681: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
3682: foreach (@responses) {
3683: my ($key,$value) = split/=/,$_;
3684: $$replyref{$key} = $value;
3685: if ($value > 0) {
3686: foreach (@{$$affiliatesref{$key}}) {
3687: my $filename = $dom.'_'.$key.'_'.$_.'_classlist.xml';
3688: my $destname = $pathname.'/'.$filename;
3689: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 3690: if ($xml_classlist =~ /^error/) {
3691: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
3692: } else {
1.506 raeburn 3693: if ( open(FILE,">$destname") ) {
3694: print FILE &unescape($xml_classlist);
3695: close(FILE);
1.526 raeburn 3696: } else {
3697: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 3698: }
3699: }
3700: }
3701: }
3702: }
3703: }
3704: return 'ok';
3705: }
3706: return 'error';
3707: }
3708:
1.242 www 3709: sub get_query_reply {
3710: my $queryid=shift;
1.240 www 3711: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
3712: my $reply='';
3713: for (1..100) {
3714: sleep 2;
3715: if (-e $replyfile.'.end') {
1.448 albertel 3716: if (open(my $fh,$replyfile)) {
1.240 www 3717: $reply.=<$fh>;
1.448 albertel 3718: close($fh);
1.240 www 3719: } else { return 'error: reply_file_error'; }
1.242 www 3720: return &unescape($reply);
3721: }
1.240 www 3722: }
1.242 www 3723: return 'timeout:'.$queryid;
1.240 www 3724: }
3725:
3726: sub courselog_query {
1.241 www 3727: #
3728: # possible filters:
3729: # url: url or symb
3730: # username
3731: # domain
3732: # action: view, submit, grade
3733: # start: timestamp
3734: # end: timestamp
3735: #
1.240 www 3736: my (%filters)=@_;
1.620 albertel 3737: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 3738: if ($filters{'url'}) {
3739: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
3740: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
3741: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
3742: }
1.620 albertel 3743: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
3744: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 3745: return &log_query($cname,$cdom,'courselog',%filters);
3746: }
3747:
3748: sub userlog_query {
3749: my ($uname,$udom,%filters)=@_;
3750: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 3751: }
3752:
1.506 raeburn 3753: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
3754:
3755: sub auto_run {
1.508 raeburn 3756: my ($cnum,$cdom) = @_;
3757: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 3758: my $response = &reply('autorun:'.$cdom,$homeserver);
1.506 raeburn 3759: return $response;
3760: }
3761:
3762: sub auto_get_sections {
1.508 raeburn 3763: my ($cnum,$cdom,$inst_coursecode) = @_;
3764: my $homeserver = &homeserver($cnum,$cdom);
1.506 raeburn 3765: my @secs = ();
1.511 raeburn 3766: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506 raeburn 3767: unless ($response eq 'refused') {
3768: @secs = split/:/,$response;
3769: }
3770: return @secs;
3771: }
3772:
3773: sub auto_new_course {
1.508 raeburn 3774: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
3775: my $homeserver = &homeserver($cnum,$cdom);
1.515 raeburn 3776: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506 raeburn 3777: return $response;
3778: }
3779:
3780: sub auto_validate_courseID {
1.508 raeburn 3781: my ($cnum,$cdom,$inst_course_id) = @_;
3782: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 3783: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 3784: return $response;
3785: }
3786:
3787: sub auto_create_password {
1.508 raeburn 3788: my ($cnum,$cdom,$authparam) = @_;
3789: my $homeserver = &homeserver($cnum,$cdom);
1.506 raeburn 3790: my $create_passwd = 0;
3791: my $authchk = '';
1.511 raeburn 3792: my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
1.506 raeburn 3793: if ($response eq 'refused') {
3794: $authchk = 'refused';
3795: } else {
3796: ($authparam,$create_passwd,$authchk) = split/:/,$response;
3797: }
3798: return ($authparam,$create_passwd,$authchk);
3799: }
3800:
1.706 raeburn 3801: sub auto_photo_permission {
3802: my ($cnum,$cdom,$students) = @_;
3803: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 3804: my ($outcome,$perm_reqd,$conditions) =
3805: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 3806: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
3807: return (undef,undef);
3808: }
1.706 raeburn 3809: return ($outcome,$perm_reqd,$conditions);
3810: }
3811:
3812: sub auto_checkphotos {
3813: my ($uname,$udom,$pid) = @_;
3814: my $homeserver = &homeserver($uname,$udom);
3815: my ($result,$resulttype);
3816: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 3817: &escape($uname).':'.&escape($pid),
3818: $homeserver));
1.709 albertel 3819: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
3820: return (undef,undef);
3821: }
1.706 raeburn 3822: if ($outcome) {
3823: ($result,$resulttype) = split(/:/,$outcome);
3824: }
3825: return ($result,$resulttype);
3826: }
3827:
3828: sub auto_photochoice {
3829: my ($cnum,$cdom) = @_;
3830: my $homeserver = &homeserver($cnum,$cdom);
3831: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 3832: &escape($cdom),
3833: $homeserver)));
1.709 albertel 3834: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
3835: return (undef,undef);
3836: }
1.706 raeburn 3837: return ($update,$comment);
3838: }
3839:
3840: sub auto_photoupdate {
3841: my ($affiliatesref,$dom,$cnum,$photo) = @_;
3842: my $homeserver = &homeserver($cnum,$dom);
3843: my $host=$hostname{$homeserver};
3844: my $cmd = '';
3845: my $maxtries = 1;
3846: foreach (keys %{$affiliatesref}) {
3847: $cmd .= $_.'='.join(",",@{$$affiliatesref{$_}}).'%%';
3848: }
3849: $cmd =~ s/%%$//;
3850: $cmd = &escape($cmd);
3851: my $query = 'institutionalphotos';
3852: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
3853: unless ($queryid=~/^\Q$host\E\_/) {
3854: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
3855: return 'error: '.$queryid;
3856: }
3857: my $reply = &get_query_reply($queryid);
3858: my $tries = 1;
3859: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
3860: $reply = &get_query_reply($queryid);
3861: $tries ++;
3862: }
3863: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
3864: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
3865: } else {
3866: my @responses = split(/:/,$reply);
3867: my $outcome = shift(@responses);
3868: foreach my $item (@responses) {
3869: my ($key,$value) = split(/=/,$item);
3870: $$photo{$key} = $value;
3871: }
3872: return $outcome;
3873: }
3874: return 'error';
3875: }
3876:
1.521 raeburn 3877: sub auto_instcode_format {
3878: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,$cat_order) = @_;
3879: my $courses = '';
3880: my $homeserver;
3881: if ($caller eq 'global') {
1.584 raeburn 3882: foreach my $tryserver (keys %libserv) {
3883: if ($hostdom{$tryserver} eq $codedom) {
3884: $homeserver = $tryserver;
3885: last;
3886: }
3887: }
1.620 albertel 3888: if (($env{'user.name'}) && ($env{'user.domain'} eq $codedom)) {
3889: $homeserver = &homeserver($env{'user.name'},$codedom);
1.584 raeburn 3890: }
1.521 raeburn 3891: } else {
3892: $homeserver = &homeserver($caller,$codedom);
3893: }
3894: foreach (keys %{$instcodes}) {
3895: $courses .= &escape($_).'='.&escape($$instcodes{$_}).'&';
3896: }
3897: chop($courses);
3898: my $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$homeserver);
3899: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
3900: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = split/:/,$response;
3901: %{$codes} = &str2hash($codes_str);
3902: @{$codetitles} = &str2array($codetitles_str);
3903: %{$cat_titles} = &str2hash($cat_titles_str);
3904: %{$cat_order} = &str2hash($cat_order_str);
3905: return 'ok';
3906: }
3907: return $response;
3908: }
3909:
1.679 raeburn 3910: # ------------------------------------------------------- Course Group routines
3911:
3912: sub get_coursegroups {
1.683 raeburn 3913: my ($cdom,$cnum,$group) = @_;
3914: return(&dump('coursegroups',$cdom,$cnum,$group));
1.679 raeburn 3915: }
3916:
3917: sub modify_coursegroup {
3918: my ($cdom,$cnum,$groupsettings) = @_;
3919: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
3920: }
3921:
3922: sub modify_group_roles {
3923: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
3924: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
3925: my $role = 'gr/'.&escape($userprivs);
3926: my ($uname,$udom) = split(/:/,$user);
3927: my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684 raeburn 3928: if ($result eq 'ok') {
3929: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
3930: }
3931:
1.679 raeburn 3932: return $result;
3933: }
3934:
3935: sub modify_coursegroup_membership {
3936: my ($cdom,$cnum,$membership) = @_;
3937: my $result = &put('groupmembership',$membership,$cdom,$cnum);
3938: return $result;
3939: }
3940:
1.682 raeburn 3941: sub get_active_groups {
3942: my ($udom,$uname,$cdom,$cnum) = @_;
3943: my $now = time;
3944: my %groups = ();
3945: foreach my $key (keys(%env)) {
3946: if ($key =~ m-user\.role\.gr\./([^/]+)/([^/]+)/(\w+)$-) {
3947: my ($start,$end) = split(/\./,$env{$key});
3948: if (($end!=0) && ($end<$now)) { next; }
3949: if (($start!=0) && ($start>$now)) { next; }
3950: if ($1 eq $cdom && $2 eq $cnum) {
3951: $groups{$3} = $env{$key} ;
3952: }
3953: }
3954: }
3955: return %groups;
3956: }
3957:
1.683 raeburn 3958: sub get_group_membership {
3959: my ($cdom,$cnum,$group) = @_;
3960: return(&dump('groupmembership',$cdom,$cnum,$group));
3961: }
3962:
3963: sub get_users_groups {
3964: my ($udom,$uname,$courseid) = @_;
3965: my $cachetime=1800;
3966: $courseid=~s/\_/\//g;
3967: $courseid=~s/^(\w)/\/$1/;
3968:
3969: my $hashid="$udom:$uname:$courseid";
3970: my ($result,$cached)=&is_cached_new('getgroups',$hashid);
3971: if (defined($cached)) { return $result; }
3972:
3973: my %roleshash = &dump('roles',$udom,$uname,$courseid);
3974: my ($tmp) = keys(%roleshash);
3975: if ($tmp=~/^error:/) {
3976: &logthis('Error retrieving roles: '.$tmp.' for '.$uname.':'.$udom);
3977: return '';
3978: } else {
3979: my $grouplist;
3980: foreach my $key (keys %roleshash) {
3981: if ($key =~ /^\Q$courseid\E\/(\w+)\_gr$/) {
3982: unless ($roleshash{$key} =~ /_1_1$/) { # deleted membership
3983: $grouplist .= $1.':';
3984: }
3985: }
3986: }
3987: $grouplist =~ s/:$//;
3988: return &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
3989: }
3990: }
3991:
3992: sub devalidate_getgroups_cache {
3993: my ($udom,$uname,$cdom,$cnum)=@_;
3994: my $courseid = $cdom.'_'.$cnum;
3995: $courseid=~s/\_/\//g;
3996: $courseid=~s/^(\w)/\/$1/;
3997: my $hashid="$udom:$uname:$courseid";
3998: &devalidate_cache_new('getgroups',$hashid);
3999: }
4000:
1.12 www 4001: # ------------------------------------------------------------------ Plain Text
4002:
4003: sub plaintext {
1.22 www 4004: my $short=shift;
1.676 albertel 4005: return &Apache::lonlocal::mt($prp{$short});
1.12 www 4006: }
4007:
4008: # ----------------------------------------------------------------- Assign Role
4009:
4010: sub assignrole {
1.357 www 4011: my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21 www 4012: my $mrole;
4013: if ($role =~ /^cr\//) {
1.393 www 4014: my $cwosec=$url;
4015: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
4016: unless (&allowed('ccr',$cwosec)) {
1.104 www 4017: &logthis('Refused custom assignrole: '.
4018: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620 albertel 4019: $env{'user.name'}.' at '.$env{'user.domain'});
1.104 www 4020: return 'refused';
4021: }
1.21 www 4022: $mrole='cr';
1.678 raeburn 4023: } elsif ($role =~ /^gr\//) {
4024: my $cwogrp=$url;
4025: $cwogrp=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
4026: unless (&allowed('mdg',$cwogrp)) {
4027: &logthis('Refused group assignrole: '.
4028: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
4029: $env{'user.name'}.' at '.$env{'user.domain'});
4030: return 'refused';
4031: }
4032: $mrole='gr';
1.21 www 4033: } else {
1.82 www 4034: my $cwosec=$url;
1.83 www 4035: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.373 www 4036: unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) {
1.104 www 4037: &logthis('Refused assignrole: '.
4038: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620 albertel 4039: $env{'user.name'}.' at '.$env{'user.domain'});
1.104 www 4040: return 'refused';
4041: }
1.21 www 4042: $mrole=$role;
4043: }
1.620 albertel 4044: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 4045: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 4046: if ($end) { $command.='_'.$end; }
1.21 www 4047: if ($start) {
4048: if ($end) {
1.81 www 4049: $command.='_'.$start;
1.21 www 4050: } else {
1.81 www 4051: $command.='_0_'.$start;
1.21 www 4052: }
4053: }
1.357 www 4054: # actually delete
4055: if ($deleteflag) {
1.373 www 4056: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 4057: # modify command to delete the role
1.620 albertel 4058: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 4059: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 4060: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 4061: # set start and finish to negative values for userrolelog
4062: $start=-1;
4063: $end=-1;
4064: }
4065: }
4066: # send command
1.349 www 4067: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 4068: # log new user role if status is ok
1.349 www 4069: if ($answer eq 'ok') {
1.663 raeburn 4070: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.349 www 4071: }
4072: return $answer;
1.169 harris41 4073: }
4074:
4075: # -------------------------------------------------- Modify user authentication
1.197 www 4076: # Overrides without validation
4077:
1.169 harris41 4078: sub modifyuserauth {
4079: my ($udom,$uname,$umode,$upass)=@_;
4080: my $uhome=&homeserver($uname,$udom);
1.197 www 4081: unless (&allowed('mau',$udom)) { return 'refused'; }
4082: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 4083: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4084: ' in domain '.$env{'request.role.domain'});
1.169 harris41 4085: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
4086: &escape($upass),$uhome);
1.620 albertel 4087: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 4088: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
4089: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
4090: &log($udom,,$uname,$uhome,
1.620 albertel 4091: 'Authentication changed by '.$env{'user.domain'}.', '.
4092: $env{'user.name'}.', '.$umode.
1.197 www 4093: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 4094: unless ($reply eq 'ok') {
1.197 www 4095: &logthis('Authentication mode error: '.$reply);
1.169 harris41 4096: return 'error: '.$reply;
4097: }
1.170 harris41 4098: return 'ok';
1.80 www 4099: }
4100:
1.81 www 4101: # --------------------------------------------------------------- Modify a user
1.80 www 4102:
1.81 www 4103: sub modifyuser {
1.206 matthew 4104: my ($udom, $uname, $uid,
4105: $umode, $upass, $first,
4106: $middle, $last, $gene,
1.387 www 4107: $forceid, $desiredhome, $email)=@_;
1.198 www 4108: $udom=~s/\W//g;
4109: $uname=~s/\W//g;
1.81 www 4110: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 4111: $umode.', '.$first.', '.$middle.', '.
1.206 matthew 4112: $last.', '.$gene.'(forceid: '.$forceid.')'.
4113: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
4114: ' desiredhome not specified').
1.620 albertel 4115: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4116: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 4117: my $uhome=&homeserver($uname,$udom,'true');
1.80 www 4118: # ----------------------------------------------------------------- Create User
1.406 albertel 4119: if (($uhome eq 'no_host') &&
4120: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 4121: my $unhome='';
1.209 matthew 4122: if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) {
4123: $unhome = $desiredhome;
1.620 albertel 4124: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
4125: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 4126: } else { # load balancing routine for determining $unhome
1.80 www 4127: my $tryserver;
1.81 www 4128: my $loadm=10000000;
1.80 www 4129: foreach $tryserver (keys %libserv) {
4130: if ($hostdom{$tryserver} eq $udom) {
4131: my $answer=reply('load',$tryserver);
4132: if (($answer=~/\d+/) && ($answer<$loadm)) {
4133: $loadm=$answer;
4134: $unhome=$tryserver;
4135: }
4136: }
4137: }
4138: }
4139: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 4140: return 'error: unable to find a home server for '.$uname.
4141: ' in domain '.$udom;
1.80 www 4142: }
4143: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
4144: &escape($upass),$unhome);
4145: unless ($reply eq 'ok') {
4146: return 'error: '.$reply;
4147: }
1.230 stredwic 4148: $uhome=&homeserver($uname,$udom,'true');
1.80 www 4149: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 4150: return 'error: unable verify users home machine.';
1.80 www 4151: }
1.209 matthew 4152: } # End of creation of new user
1.80 www 4153: # ---------------------------------------------------------------------- Add ID
4154: if ($uid) {
4155: $uid=~tr/A-Z/a-z/;
4156: my %uidhash=&idrget($udom,$uname);
1.196 www 4157: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
4158: && (!$forceid)) {
1.80 www 4159: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 4160: return 'error: user id "'.$uid.'" does not match '.
4161: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 4162: }
4163: } else {
4164: &idput($udom,($uname => $uid));
4165: }
4166: }
4167: # -------------------------------------------------------------- Add names, etc
1.313 matthew 4168: my @tmp=&get('environment',
1.134 albertel 4169: ['firstname','middlename','lastname','generation'],
4170: $udom,$uname);
1.313 matthew 4171: my %names;
4172: if ($tmp[0] =~ m/^error:.*/) {
4173: %names=();
4174: } else {
4175: %names = @tmp;
4176: }
1.388 www 4177: #
4178: # Make sure to not trash student environment if instructor does not bother
4179: # to supply name and email information
4180: #
4181: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 4182: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 4183: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 4184: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 4185: if ($email) {
4186: $email=~s/[^\w\@\.\-\,]//gs;
4187: if ($email=~/\@/) { $names{'notification'} = $email;
4188: $names{'critnotification'} = $email;
4189: $names{'permanentemail'} = $email; }
4190: }
1.134 albertel 4191: my $reply = &put('environment', \%names, $udom,$uname);
4192: if ($reply ne 'ok') { return 'error: '.$reply; }
1.680 www 4193: &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81 www 4194: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 4195: $umode.', '.$first.', '.$middle.', '.
4196: $last.', '.$gene.' by '.
1.620 albertel 4197: $env{'user.name'}.' at '.$env{'user.domain'});
1.134 albertel 4198: return 'ok';
1.80 www 4199: }
4200:
1.81 www 4201: # -------------------------------------------------------------- Modify student
1.80 www 4202:
1.81 www 4203: sub modifystudent {
4204: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515 raeburn 4205: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455 albertel 4206: if (!$cid) {
1.620 albertel 4207: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 4208: return 'not_in_class';
4209: }
1.80 www 4210: }
4211: # --------------------------------------------------------------- Make the user
1.81 www 4212: my $reply=&modifyuser
1.209 matthew 4213: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387 www 4214: $desiredhome,$email);
1.80 www 4215: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 4216: # This will cause &modify_student_enrollment to get the uid from the
4217: # students environment
4218: $uid = undef if (!$forceid);
1.455 albertel 4219: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515 raeburn 4220: $gene,$usec,$end,$start,$type,$locktype,$cid);
1.297 matthew 4221: return $reply;
4222: }
4223:
4224: sub modify_student_enrollment {
1.515 raeburn 4225: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455 albertel 4226: my ($cdom,$cnum,$chome);
4227: if (!$cid) {
1.620 albertel 4228: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 4229: return 'not_in_class';
4230: }
1.620 albertel 4231: $cdom=$env{'course.'.$cid.'.domain'};
4232: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 4233: } else {
4234: ($cdom,$cnum)=split(/_/,$cid);
4235: }
1.620 albertel 4236: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 4237: if (!$chome) {
1.457 raeburn 4238: $chome=&homeserver($cnum,$cdom);
1.297 matthew 4239: }
1.455 albertel 4240: if (!$chome) { return 'unknown_course'; }
1.297 matthew 4241: # Make sure the user exists
1.81 www 4242: my $uhome=&homeserver($uname,$udom);
4243: if (($uhome eq '') || ($uhome eq 'no_host')) {
4244: return 'error: no such user';
4245: }
1.297 matthew 4246: # Get student data if we were not given enough information
4247: if (!defined($first) || $first eq '' ||
4248: !defined($last) || $last eq '' ||
4249: !defined($uid) || $uid eq '' ||
4250: !defined($middle) || $middle eq '' ||
4251: !defined($gene) || $gene eq '') {
1.294 matthew 4252: # They did not supply us with enough data to enroll the student, so
4253: # we need to pick up more information.
1.297 matthew 4254: my %tmp = &get('environment',
1.294 matthew 4255: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 4256: ,$udom,$uname);
4257:
1.455 albertel 4258: #foreach (keys(%tmp)) {
4259: # &logthis("key $_ = ".$tmp{$_});
4260: #}
1.294 matthew 4261: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
4262: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
4263: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 4264: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 4265: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
4266: }
1.556 albertel 4267: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 4268: my $reply=cput('classlist',
4269: {"$uname:$udom" =>
1.515 raeburn 4270: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 4271: $cdom,$cnum);
1.81 www 4272: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
4273: return 'error: '.$reply;
1.652 albertel 4274: } else {
4275: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 4276: }
1.297 matthew 4277: # Add student role to user
1.83 www 4278: my $uurl='/'.$cid;
1.81 www 4279: $uurl=~s/\_/\//g;
4280: if ($usec) {
4281: $uurl.='/'.$usec;
4282: }
4283: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21 www 4284: }
4285:
1.556 albertel 4286: sub format_name {
4287: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
4288: my $name;
4289: if ($first ne 'lastname') {
4290: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
4291: } else {
4292: if ($lastname=~/\S/) {
4293: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
4294: $name=~s/\s+,/,/;
4295: } else {
4296: $name.= $firstname.' '.$middlename.' '.$generation;
4297: }
4298: }
4299: $name=~s/^\s+//;
4300: $name=~s/\s+$//;
4301: $name=~s/\s+/ /g;
4302: return $name;
4303: }
4304:
1.84 www 4305: # ------------------------------------------------- Write to course preferences
4306:
4307: sub writecoursepref {
4308: my ($courseid,%prefs)=@_;
4309: $courseid=~s/^\///;
4310: $courseid=~s/\_/\//g;
4311: my ($cdomain,$cnum)=split(/\//,$courseid);
4312: my $chome=homeserver($cnum,$cdomain);
4313: if (($chome eq '') || ($chome eq 'no_host')) {
4314: return 'error: no such course';
4315: }
4316: my $cstring='';
1.191 harris41 4317: foreach (keys %prefs) {
1.84 www 4318: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
1.191 harris41 4319: }
1.84 www 4320: $cstring=~s/\&$//;
4321: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
4322: }
4323:
4324: # ---------------------------------------------------------- Make/modify course
4325:
4326: sub createcourse {
1.571 raeburn 4327: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner)=@_;
1.84 www 4328: $url=&declutter($url);
4329: my $cid='';
1.264 matthew 4330: unless (&allowed('ccc',$udom)) {
1.84 www 4331: return 'refused';
4332: }
4333: # ------------------------------------------------------------------- Create ID
1.674 www 4334: my $uname=int(1+rand(9)).
4335: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
4336: substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84 www 4337: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
4338: # ----------------------------------------------- Make sure that does not exist
1.230 stredwic 4339: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 4340: unless (($uhome eq '') || ($uhome eq 'no_host')) {
4341: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
4342: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230 stredwic 4343: $uhome=&homeserver($uname,$udom,'true');
1.84 www 4344: unless (($uhome eq '') || ($uhome eq 'no_host')) {
4345: return 'error: unable to generate unique course-ID';
4346: }
4347: }
1.264 matthew 4348: # ------------------------------------------------ Check supplied server name
1.620 albertel 4349: $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.264 matthew 4350: if (! exists($libserv{$course_server})) {
4351: return 'error:bad server name '.$course_server;
4352: }
1.84 www 4353: # ------------------------------------------------------------- Make the course
4354: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 4355: $course_server);
1.84 www 4356: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230 stredwic 4357: $uhome=&homeserver($uname,$udom,'true');
1.84 www 4358: if (($uhome eq '') || ($uhome eq 'no_host')) {
4359: return 'error: no such course';
4360: }
1.271 www 4361: # ----------------------------------------------------------------- Course made
1.516 raeburn 4362: # log existence
4363: &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.571 raeburn 4364: ':'.&escape($inst_code).':'.&escape($course_owner),$uhome);
1.358 www 4365: &flushcourselogs();
4366: # set toplevel url
1.271 www 4367: my $topurl=$url;
4368: unless ($nonstandard) {
4369: # ------------------------------------------ For standard courses, make top url
4370: my $mapurl=&clutter($url);
1.278 www 4371: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 4372: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 4373: <map>
4374: <resource id="1" type="start"></resource>
4375: <resource id="2" src="$mapurl"></resource>
4376: <resource id="3" type="finish"></resource>
4377: <link index="1" from="1" to="2"></link>
4378: <link index="2" from="2" to="3"></link>
4379: </map>
4380: ENDINITMAP
4381: $topurl=&declutter(
1.638 albertel 4382: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 4383: );
4384: }
4385: # ----------------------------------------------------------- Write preferences
1.84 www 4386: &writecoursepref($udom.'_'.$uname,
4387: ('description' => $description,
1.271 www 4388: 'url' => $topurl));
1.84 www 4389: return '/'.$udom.'/'.$uname;
4390: }
4391:
1.21 www 4392: # ---------------------------------------------------------- Assign Custom Role
4393:
4394: sub assigncustomrole {
1.357 www 4395: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21 www 4396: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357 www 4397: $end,$start,$deleteflag);
1.21 www 4398: }
4399:
4400: # ----------------------------------------------------------------- Revoke Role
4401:
4402: sub revokerole {
1.357 www 4403: my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21 www 4404: my $now=time;
1.357 www 4405: return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21 www 4406: }
4407:
4408: # ---------------------------------------------------------- Revoke Custom Role
4409:
4410: sub revokecustomrole {
1.357 www 4411: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21 www 4412: my $now=time;
1.357 www 4413: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
4414: $deleteflag);
1.17 www 4415: }
4416:
1.533 banghart 4417: # ------------------------------------------------------------ Disk usage
1.535 albertel 4418: sub diskusage {
1.533 banghart 4419: my ($udom,$uname,$directoryRoot)=@_;
4420: $directoryRoot =~ s/\/$//;
1.535 albertel 4421: my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514 albertel 4422: return $listing;
1.512 banghart 4423: }
4424:
1.566 banghart 4425: sub is_locked {
4426: my ($file_name, $domain, $user) = @_;
4427: my @check;
4428: my $is_locked;
4429: push @check, $file_name;
1.613 albertel 4430: my %locked = &get('file_permissions',\@check,
1.620 albertel 4431: $env{'user.domain'},$env{'user.name'});
1.615 albertel 4432: my ($tmp)=keys(%locked);
4433: if ($tmp=~/^error:/) { undef(%locked); }
1.613 albertel 4434:
1.566 banghart 4435: if (ref($locked{$file_name}) eq 'ARRAY') {
4436: $is_locked = 'true';
4437: } else {
4438: $is_locked = 'false';
4439: }
4440: }
4441:
1.559 banghart 4442: # ------------------------------------------------------------- Mark as Read Only
4443:
4444: sub mark_as_readonly {
4445: my ($domain,$user,$files,$what) = @_;
1.613 albertel 4446: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 4447: my ($tmp)=keys(%current_permissions);
4448: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 4449: foreach my $file (@{$files}) {
1.561 banghart 4450: push(@{$current_permissions{$file}},$what);
1.559 banghart 4451: }
1.613 albertel 4452: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 4453: return;
4454: }
4455:
1.572 banghart 4456: # ------------------------------------------------------------Save Selected Files
4457:
4458: sub save_selected_files {
4459: my ($user, $path, @files) = @_;
4460: my $filename = $user."savedfiles";
1.573 banghart 4461: my @other_files = &files_not_in_path($user, $path);
1.574 banghart 4462: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 4463: foreach my $file (@files) {
1.620 albertel 4464: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 4465: }
4466: foreach my $file (@other_files) {
1.574 banghart 4467: print (OUT $file."\n");
1.572 banghart 4468: }
1.574 banghart 4469: close (OUT);
1.572 banghart 4470: return 'ok';
4471: }
4472:
1.574 banghart 4473: sub clear_selected_files {
4474: my ($user) = @_;
4475: my $filename = $user."savedfiles";
4476: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
4477: print (OUT undef);
4478: close (OUT);
4479: return ("ok");
4480: }
4481:
1.572 banghart 4482: sub files_in_path {
4483: my ($user, $path) = @_;
4484: my $filename = $user."savedfiles";
4485: my %return_files;
1.574 banghart 4486: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 4487: while (my $line_in = <IN>) {
1.574 banghart 4488: chomp ($line_in);
4489: my @paths_and_file = split (m!/!, $line_in);
4490: my $file_part = pop (@paths_and_file);
4491: my $path_part = join ('/', @paths_and_file);
1.573 banghart 4492: $path_part.='/';
4493: my $path_and_file = $path_part.$file_part;
4494: if ($path_part eq $path) {
4495: $return_files{$file_part}= 'selected';
4496: }
4497: }
1.574 banghart 4498: close (IN);
4499: return (\%return_files);
1.572 banghart 4500: }
4501:
4502: # called in portfolio select mode, to show files selected NOT in current directory
4503: sub files_not_in_path {
4504: my ($user, $path) = @_;
4505: my $filename = $user."savedfiles";
4506: my @return_files;
4507: my $path_part;
1.574 banghart 4508: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.572 banghart 4509: while (<IN>) {
4510: #ok, I know it's clunky, but I want it to work
4511: my @paths_and_file = split m!/!, $_;
1.574 banghart 4512: my $file_part = pop (@paths_and_file);
4513: chomp ($file_part);
4514: my $path_part = join ('/', @paths_and_file);
1.572 banghart 4515: $path_part .= '/';
4516: my $path_and_file = $path_part.$file_part;
4517: if ($path_part ne $path) {
1.574 banghart 4518: push (@return_files, ($path_and_file));
1.572 banghart 4519: }
4520: }
1.574 banghart 4521: close (OUT);
4522: return (@return_files);
1.572 banghart 4523: }
4524:
1.561 banghart 4525: #--------------------------------------------------------------Get Marked as Read Only
4526:
1.629 banghart 4527:
1.561 banghart 4528: sub get_marked_as_readonly {
4529: my ($domain,$user,$what) = @_;
1.613 albertel 4530: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 4531: my ($tmp)=keys(%current_permissions);
4532: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.563 banghart 4533: my @readonly_files;
1.629 banghart 4534: my $cmp1=$what;
4535: if (ref($what)) { $cmp1=join('',@{$what}) };
1.563 banghart 4536: while (my ($file_name,$value) = each(%current_permissions)) {
1.561 banghart 4537: if (ref($value) eq "ARRAY"){
4538: foreach my $stored_what (@{$value}) {
1.629 banghart 4539: my $cmp2=$stored_what;
4540: if (ref($stored_what)) { $cmp2=join('',@{$stored_what}) };
4541: if ($cmp1 eq $cmp2) {
1.561 banghart 4542: push(@readonly_files, $file_name);
1.563 banghart 4543: } elsif (!defined($what)) {
4544: push(@readonly_files, $file_name);
1.561 banghart 4545: }
4546: }
4547: }
4548: }
4549: return @readonly_files;
4550: }
1.577 banghart 4551: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 4552:
1.577 banghart 4553: sub get_marked_as_readonly_hash {
4554: my ($domain,$user,$what) = @_;
1.613 albertel 4555: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 4556: my ($tmp)=keys(%current_permissions);
4557: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.613 albertel 4558:
1.577 banghart 4559: my %readonly_files;
4560: while (my ($file_name,$value) = each(%current_permissions)) {
4561: if (ref($value) eq "ARRAY"){
4562: foreach my $stored_what (@{$value}) {
4563: if ($stored_what eq $what) {
4564: $readonly_files{$file_name} = 'locked';
4565: } elsif (!defined($what)) {
4566: $readonly_files{$file_name} = 'locked';
4567: }
4568: }
4569: }
4570: }
4571: return %readonly_files;
4572: }
1.559 banghart 4573: # ------------------------------------------------------------ Unmark as Read Only
4574:
4575: sub unmark_as_readonly {
1.629 banghart 4576: # unmarks $file_name (if $file_name is defined), or all files locked by $what
4577: # for portfolio submissions, $what contains [$symb,$crsid]
4578: my ($domain,$user,$what,$file_name) = @_;
1.634 albertel 4579: my $symb_crs = $what;
4580: if (ref($what)) { $symb_crs=join('',@$what); }
1.613 albertel 4581: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 4582: my ($tmp)=keys(%current_permissions);
4583: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.613 albertel 4584: my @readonly_files = &get_marked_as_readonly($domain,$user,$what);
1.650 albertel 4585: foreach my $file (@readonly_files) {
4586: if (defined($file_name) && ($file_name ne $file)) { next; }
4587: my $current_locks = $current_permissions{$file};
1.563 banghart 4588: my @new_locks;
4589: my @del_keys;
4590: if (ref($current_locks) eq "ARRAY"){
4591: foreach my $locker (@{$current_locks}) {
1.632 albertel 4592: my $compare=$locker;
4593: if (ref($locker)) { $compare=join('',@{$locker}) };
1.650 albertel 4594: if ($compare ne $symb_crs) {
4595: push(@new_locks, $locker);
1.563 banghart 4596: }
4597: }
1.650 albertel 4598: if (scalar(@new_locks) > 0) {
1.563 banghart 4599: $current_permissions{$file} = \@new_locks;
4600: } else {
4601: push(@del_keys, $file);
1.613 albertel 4602: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 4603: delete($current_permissions{$file});
1.563 banghart 4604: }
4605: }
1.561 banghart 4606: }
1.613 albertel 4607: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 4608: return;
4609: }
1.512 banghart 4610:
1.17 www 4611: # ------------------------------------------------------------ Directory lister
4612:
4613: sub dirlist {
1.253 stredwic 4614: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
4615:
1.18 www 4616: $uri=~s/^\///;
4617: $uri=~s/\/$//;
1.253 stredwic 4618: my ($udom, $uname);
4619: (undef,$udom,$uname)=split(/\//,$uri);
4620: if(defined($userdomain)) {
4621: $udom = $userdomain;
4622: }
4623: if(defined($username)) {
4624: $uname = $username;
4625: }
4626:
4627: my $dirRoot = $perlvar{'lonDocRoot'};
4628: if(defined($alternateDirectoryRoot)) {
4629: $dirRoot = $alternateDirectoryRoot;
4630: $dirRoot =~ s/\/$//;
4631: }
4632:
4633: if($udom) {
4634: if($uname) {
1.605 matthew 4635: my $listing=reply('ls2:'.$dirRoot.'/'.$uri,
1.253 stredwic 4636: homeserver($uname,$udom));
1.605 matthew 4637: my @listing_results;
4638: if ($listing eq 'unknown_cmd') {
4639: $listing=reply('ls:'.$dirRoot.'/'.$uri,
4640: homeserver($uname,$udom));
4641: @listing_results = split(/:/,$listing);
4642: } else {
4643: @listing_results = map { &unescape($_); } split(/:/,$listing);
4644: }
4645: return @listing_results;
1.253 stredwic 4646: } elsif(!defined($alternateDirectoryRoot)) {
4647: my $tryserver;
4648: my %allusers=();
4649: foreach $tryserver (keys %libserv) {
4650: if($hostdom{$tryserver} eq $udom) {
1.605 matthew 4651: my $listing=reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
1.253 stredwic 4652: $udom, $tryserver);
1.605 matthew 4653: my @listing_results;
4654: if ($listing eq 'unknown_cmd') {
4655: $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
4656: $udom, $tryserver);
4657: @listing_results = split(/:/,$listing);
4658: } else {
4659: @listing_results =
4660: map { &unescape($_); } split(/:/,$listing);
4661: }
4662: if ($listing_results[0] ne 'no_such_dir' &&
4663: $listing_results[0] ne 'empty' &&
4664: $listing_results[0] ne 'con_lost') {
4665: foreach (@listing_results) {
1.253 stredwic 4666: my ($entry,@stat)=split(/&/,$_);
4667: $allusers{$entry}=1;
4668: }
4669: }
1.191 harris41 4670: }
1.253 stredwic 4671: }
4672: my $alluserstr='';
4673: foreach (sort keys %allusers) {
4674: $alluserstr.=$_.'&user:';
4675: }
4676: $alluserstr=~s/:$//;
4677: return split(/:/,$alluserstr);
4678: } else {
4679: my @emptyResults = ();
4680: push(@emptyResults, 'missing user name');
4681: return split(':',@emptyResults);
4682: }
4683: } elsif(!defined($alternateDirectoryRoot)) {
4684: my $tryserver;
4685: my %alldom=();
4686: foreach $tryserver (keys %libserv) {
4687: $alldom{$hostdom{$tryserver}}=1;
4688: }
4689: my $alldomstr='';
4690: foreach (sort keys %alldom) {
1.397 albertel 4691: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'/&domain:';
1.253 stredwic 4692: }
4693: $alldomstr=~s/:$//;
4694: return split(/:/,$alldomstr);
4695: } else {
4696: my @emptyResults = ();
4697: push(@emptyResults, 'missing domain');
4698: return split(':',@emptyResults);
1.275 stredwic 4699: }
4700: }
4701:
4702: # --------------------------------------------- GetFileTimestamp
4703: # This function utilizes dirlist and returns the date stamp for
4704: # when it was last modified. It will also return an error of -1
4705: # if an error occurs
4706:
1.410 matthew 4707: ##
4708: ## FIXME: This subroutine assumes its caller knows something about the
4709: ## directory structure of the home server for the student ($root).
4710: ## Not a good assumption to make. Since this is for looking up files
4711: ## in user directories, the full path should be constructed by lond, not
4712: ## whatever machine we request data from.
4713: ##
1.275 stredwic 4714: sub GetFileTimestamp {
4715: my ($studentDomain,$studentName,$filename,$root)=@_;
4716: $studentDomain=~s/\W//g;
4717: $studentName=~s/\W//g;
4718: my $subdir=$studentName.'__';
4719: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
4720: my $proname="$studentDomain/$subdir/$studentName";
4721: $proname .= '/'.$filename;
1.375 matthew 4722: my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain,
4723: $studentName, $root);
1.275 stredwic 4724: my @stats = split('&', $fileStat);
4725: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 4726: # @stats contains first the filename, then the stat output
4727: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 4728: } else {
4729: return -1;
1.253 stredwic 4730: }
1.26 www 4731: }
4732:
1.712 albertel 4733: sub stat_file {
4734: my ($uri) = @_;
4735: $uri = &clutter($uri);
4736: my ($udom,$uname,$file,$dir);
4737: if ($uri =~ m-^/(uploaded|editupload)/-) {
4738: ($udom,$uname,$file) =
4739: ($uri =~ m-/(?:uploaded|editupload)/?([^/]*)/?([^/]*)/?(.*)-);
4740: $file = 'userfiles/'.$file;
4741: $dir = &Apache::loncommon::propath($udom,$uname);
4742: }
4743: if ($uri =~ m-^/res/-) {
4744: ($udom,$uname) =
4745: ($uri =~ m-/(?:res)/?([^/]*)/?([^/]*)/-);
4746: $file = $uri;
4747: }
4748:
4749: if (!$udom || !$uname || !$file) {
4750: # unable to handle the uri
4751: return ();
4752: }
4753:
4754: my ($result) = &dirlist($file,$udom,$uname,$dir);
4755: my @stats = split('&', $result);
4756: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
4757: shift(@stats); #filename is first
4758: return @stats;
4759: }
4760: return ();
4761: }
4762:
1.26 www 4763: # -------------------------------------------------------- Value of a Condition
4764:
1.713 albertel 4765: # gets the value of a specific preevaluated condition
4766: # stored in the string $env{user.state.<cid>}
4767: # or looks up a condition reference in the bighash and if if hasn't
4768: # already been evaluated recurses into docondval to get the value of
4769: # the condition, then memoizing it to
4770: # $env{user.state.<cid>.<condition>}
1.40 www 4771: sub directcondval {
4772: my $number=shift;
1.620 albertel 4773: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 4774: &Apache::lonuserstate::evalstate();
4775: }
1.713 albertel 4776: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
4777: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
4778: } elsif ($number =~ /^_/) {
4779: my $sub_condition;
4780: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
4781: &GDBM_READER(),0640)) {
4782: $sub_condition=$bighash{'conditions'.$number};
4783: untie(%bighash);
4784: }
4785: my $value = &docondval($sub_condition);
4786: &appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
4787: return $value;
4788: }
1.620 albertel 4789: if ($env{'user.state.'.$env{'request.course.id'}}) {
4790: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 4791: } else {
4792: return 2;
4793: }
4794: }
4795:
1.713 albertel 4796: # get the collection of conditions for this resource
1.26 www 4797: sub condval {
4798: my $condidx=shift;
1.54 www 4799: my $allpathcond='';
1.713 albertel 4800: foreach my $cond (split(/\|/,$condidx)) {
4801: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
4802: $allpathcond.=
4803: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
4804: }
1.191 harris41 4805: }
1.54 www 4806: $allpathcond=~s/\|$//;
1.713 albertel 4807: return &docondval($allpathcond);
4808: }
4809:
4810: #evaluates an expression of conditions
4811: sub docondval {
4812: my ($allpathcond) = @_;
4813: my $result=0;
4814: if ($env{'request.course.id'}
4815: && defined($allpathcond)) {
4816: my $operand='|';
4817: my @stack;
4818: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
4819: if ($chunk eq '(') {
4820: push @stack,($operand,$result);
4821: } elsif ($chunk eq ')') {
4822: my $before=pop @stack;
4823: if (pop @stack eq '&') {
4824: $result=$result>$before?$before:$result;
4825: } else {
4826: $result=$result>$before?$result:$before;
4827: }
4828: } elsif (($chunk eq '&') || ($chunk eq '|')) {
4829: $operand=$chunk;
4830: } else {
4831: my $new=directcondval($chunk);
4832: if ($operand eq '&') {
4833: $result=$result>$new?$new:$result;
4834: } else {
4835: $result=$result>$new?$result:$new;
4836: }
4837: }
4838: }
1.26 www 4839: }
4840: return $result;
1.421 albertel 4841: }
4842:
4843: # ---------------------------------------------------- Devalidate courseresdata
4844:
4845: sub devalidatecourseresdata {
4846: my ($coursenum,$coursedomain)=@_;
4847: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 4848: &devalidate_cache_new('courseres',$hashid);
1.28 www 4849: }
4850:
1.200 www 4851: # --------------------------------------------------- Course Resourcedata Query
4852:
1.624 albertel 4853: sub get_courseresdata {
4854: my ($coursenum,$coursedomain)=@_;
1.200 www 4855: my $coursehom=&homeserver($coursenum,$coursedomain);
4856: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 4857: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 4858: my %dumpreply;
1.417 albertel 4859: unless (defined($cached)) {
1.624 albertel 4860: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 4861: $result=\%dumpreply;
1.251 albertel 4862: my ($tmp) = keys(%dumpreply);
4863: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 4864: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 4865: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
4866: return $tmp;
1.416 albertel 4867: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 4868: $result=undef;
1.599 albertel 4869: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 4870: }
4871: }
1.624 albertel 4872: return $result;
4873: }
4874:
1.633 albertel 4875: sub devalidateuserresdata {
4876: my ($uname,$udom)=@_;
4877: my $hashid="$udom:$uname";
4878: &devalidate_cache_new('userres',$hashid);
4879: }
4880:
1.624 albertel 4881: sub get_userresdata {
4882: my ($uname,$udom)=@_;
4883: #most student don\'t have any data set, check if there is some data
4884: if (&EXT_cache_status($udom,$uname)) { return undef; }
4885:
4886: my $hashid="$udom:$uname";
4887: my ($result,$cached)=&is_cached_new('userres',$hashid);
4888: if (!defined($cached)) {
4889: my %resourcedata=&dump('resourcedata',$udom,$uname);
4890: $result=\%resourcedata;
4891: &do_cache_new('userres',$hashid,$result,600);
4892: }
4893: my ($tmp)=keys(%$result);
4894: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
4895: return $result;
4896: }
4897: #error 2 occurs when the .db doesn't exist
4898: if ($tmp!~/error: 2 /) {
1.672 albertel 4899: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 4900: " Trying to get resource data for ".
4901: $uname." at ".$udom.": ".
4902: $tmp."</font>");
4903: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 4904: #&EXT_cache_set($udom,$uname);
4905: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 4906: undef($tmp); # not really an error so don't send it back
1.624 albertel 4907: }
4908: return $tmp;
4909: }
4910:
4911: sub resdata {
4912: my ($name,$domain,$type,@which)=@_;
4913: my $result;
4914: if ($type eq 'course') {
4915: $result=&get_courseresdata($name,$domain);
4916: } elsif ($type eq 'user') {
4917: $result=&get_userresdata($name,$domain);
4918: }
4919: if (!ref($result)) { return $result; }
1.251 albertel 4920: foreach my $item (@which) {
1.417 albertel 4921: if (defined($result->{$item})) {
4922: return $result->{$item};
1.251 albertel 4923: }
1.250 albertel 4924: }
1.291 albertel 4925: return undef;
1.200 www 4926: }
4927:
1.379 matthew 4928: #
4929: # EXT resource caching routines
4930: #
4931:
4932: sub clear_EXT_cache_status {
1.383 albertel 4933: &delenv('cache.EXT.');
1.379 matthew 4934: }
4935:
4936: sub EXT_cache_status {
4937: my ($target_domain,$target_user) = @_;
1.383 albertel 4938: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 4939: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 4940: # We know already the user has no data
4941: return 1;
4942: } else {
4943: return 0;
4944: }
4945: }
4946:
4947: sub EXT_cache_set {
4948: my ($target_domain,$target_user) = @_;
1.383 albertel 4949: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633 albertel 4950: #&appenv($cachename => time);
1.379 matthew 4951: }
4952:
1.28 www 4953: # --------------------------------------------------------- Value of a Variable
1.58 www 4954: sub EXT {
1.715 albertel 4955:
1.395 albertel 4956: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 4957: unless ($varname) { return ''; }
1.218 albertel 4958: #get real user name/domain, courseid and symb
4959: my $courseid;
1.359 albertel 4960: my $publicuser;
1.427 www 4961: if ($symbparm) {
4962: $symbparm=&get_symb_from_alias($symbparm);
4963: }
1.218 albertel 4964: if (!($uname && $udom)) {
1.360 albertel 4965: (my $cursymb,$courseid,$udom,$uname,$publicuser)=
1.378 matthew 4966: &Apache::lonxml::whichuser($symbparm);
1.218 albertel 4967: if (!$symbparm) { $symbparm=$cursymb; }
4968: } else {
1.620 albertel 4969: $courseid=$env{'request.course.id'};
1.218 albertel 4970: }
1.48 www 4971: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
4972: my $rest;
1.320 albertel 4973: if (defined($therest[0])) {
1.48 www 4974: $rest=join('.',@therest);
4975: } else {
4976: $rest='';
4977: }
1.320 albertel 4978:
1.57 www 4979: my $qualifierrest=$qualifier;
4980: if ($rest) { $qualifierrest.='.'.$rest; }
4981: my $spacequalifierrest=$space;
4982: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 4983: if ($realm eq 'user') {
1.48 www 4984: # --------------------------------------------------------------- user.resource
4985: if ($space eq 'resource') {
1.651 albertel 4986: if ( (defined($Apache::lonhomework::parsing_a_problem)
4987: || defined($Apache::lonhomework::parsing_a_task))
4988: &&
4989: ($symbparm eq &symbread()) ) {
1.335 albertel 4990: return $Apache::lonhomework::history{$qualifierrest};
4991: } else {
1.359 albertel 4992: my %restored;
1.620 albertel 4993: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 4994: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
4995: } else {
4996: %restored=&restore($symbparm,$courseid,$udom,$uname);
4997: }
1.335 albertel 4998: return $restored{$qualifierrest};
4999: }
1.48 www 5000: # ----------------------------------------------------------------- user.access
5001: } elsif ($space eq 'access') {
1.218 albertel 5002: # FIXME - not supporting calls for a specific user
1.48 www 5003: return &allowed($qualifier,$rest);
5004: # ------------------------------------------ user.preferences, user.environment
5005: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 5006: if (($uname eq $env{'user.name'}) &&
5007: ($udom eq $env{'user.domain'})) {
5008: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 5009: } else {
1.359 albertel 5010: my %returnhash;
5011: if (!$publicuser) {
5012: %returnhash=&userenvironment($udom,$uname,
5013: $qualifierrest);
5014: }
1.218 albertel 5015: return $returnhash{$qualifierrest};
5016: }
1.48 www 5017: # ----------------------------------------------------------------- user.course
5018: } elsif ($space eq 'course') {
1.218 albertel 5019: # FIXME - not supporting calls for a specific user
1.620 albertel 5020: return $env{join('.',('request.course',$qualifier))};
1.48 www 5021: # ------------------------------------------------------------------- user.role
5022: } elsif ($space eq 'role') {
1.218 albertel 5023: # FIXME - not supporting calls for a specific user
1.620 albertel 5024: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 5025: if ($qualifier eq 'value') {
5026: return $role;
5027: } elsif ($qualifier eq 'extent') {
5028: return $where;
5029: }
5030: # ----------------------------------------------------------------- user.domain
5031: } elsif ($space eq 'domain') {
1.218 albertel 5032: return $udom;
1.48 www 5033: # ------------------------------------------------------------------- user.name
5034: } elsif ($space eq 'name') {
1.218 albertel 5035: return $uname;
1.48 www 5036: # ---------------------------------------------------- Any other user namespace
1.29 www 5037: } else {
1.359 albertel 5038: my %reply;
5039: if (!$publicuser) {
5040: %reply=&get($space,[$qualifierrest],$udom,$uname);
5041: }
5042: return $reply{$qualifierrest};
1.48 www 5043: }
1.236 www 5044: } elsif ($realm eq 'query') {
5045: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 5046: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
5047: [$spacequalifierrest]);
1.620 albertel 5048: return $env{'form.'.$spacequalifierrest};
1.236 www 5049: } elsif ($realm eq 'request') {
1.48 www 5050: # ------------------------------------------------------------- request.browser
5051: if ($space eq 'browser') {
1.430 www 5052: if ($qualifier eq 'textremote') {
1.676 albertel 5053: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 5054: return 1;
5055: } else {
5056: return 0;
5057: }
5058: } else {
1.620 albertel 5059: return $env{'browser.'.$qualifier};
1.430 www 5060: }
1.57 www 5061: # ------------------------------------------------------------ request.filename
5062: } else {
1.620 albertel 5063: return $env{'request.'.$spacequalifierrest};
1.29 www 5064: }
1.28 www 5065: } elsif ($realm eq 'course') {
1.48 www 5066: # ---------------------------------------------------------- course.description
1.620 albertel 5067: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 5068: } elsif ($realm eq 'resource') {
1.165 www 5069:
1.620 albertel 5070: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 5071: if (!$symbparm) { $symbparm=&symbread(); }
5072: }
1.693 albertel 5073:
5074: if ($space eq 'title') {
5075: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
5076: return &gettitle($symbparm);
5077: }
5078:
5079: if ($space eq 'map') {
5080: my ($map) = &decode_symb($symbparm);
5081: return &symbread($map);
5082: }
5083:
5084: my ($section, $group, @groups);
1.593 albertel 5085: my ($courselevelm,$courselevel);
1.539 albertel 5086: if ($symbparm && defined($courseid) &&
1.620 albertel 5087: $courseid eq $env{'request.course.id'}) {
1.165 www 5088:
1.218 albertel 5089: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 5090:
1.60 www 5091: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 5092: my $symbp=$symbparm;
1.409 www 5093: my $mapp=(&decode_symb($symbp))[0];
1.218 albertel 5094:
5095: my $symbparm=$symbp.'.'.$spacequalifierrest;
5096: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
5097:
1.620 albertel 5098: if (($env{'user.name'} eq $uname) &&
5099: ($env{'user.domain'} eq $udom)) {
5100: $section=$env{'request.course.sec'};
1.691 raeburn 5101: @groups=&sort_course_groups($env{'request.course.groups'},$courseid);
1.684 raeburn 5102: if (@groups > 0) {
5103: @groups = sort(@groups);
5104: }
1.218 albertel 5105: } else {
1.539 albertel 5106: if (! defined($usection)) {
1.551 albertel 5107: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 5108: } else {
5109: $section = $usection;
5110: }
1.684 raeburn 5111: my $grouplist = &get_users_groups($udom,$uname,$courseid);
5112: if ($grouplist) {
1.691 raeburn 5113: @groups=&sort_course_groups($grouplist,$courseid);
1.684 raeburn 5114: }
1.218 albertel 5115: }
5116:
5117: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
5118: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
5119: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
5120:
1.593 albertel 5121: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 5122: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 5123: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 5124:
1.60 www 5125: # ----------------------------------------------------------- first, check user
1.624 albertel 5126:
5127: my $userreply=&resdata($uname,$udom,'user',
5128: ($courselevelr,$courselevelm,
5129: $courselevel));
5130: if (defined($userreply)) { return $userreply; }
1.95 www 5131:
1.594 albertel 5132: # ------------------------------------------------ second, check some of course
1.684 raeburn 5133: my $coursereply;
1.691 raeburn 5134: if (@groups > 0) {
5135: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
5136: $mapparm,$spacequalifierrest);
1.684 raeburn 5137: if (defined($coursereply)) { return $coursereply; }
5138: }
1.96 www 5139:
1.684 raeburn 5140: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624 albertel 5141: $env{'course.'.$courseid.'.domain'},
5142: 'course',
5143: ($seclevelr,$seclevelm,$seclevel,
5144: $courselevelr));
1.287 albertel 5145: if (defined($coursereply)) { return $coursereply; }
1.200 www 5146:
1.60 www 5147: # ------------------------------------------------------ third, check map parms
1.218 albertel 5148: my %parmhash=();
5149: my $thisparm='';
5150: if (tie(%parmhash,'GDBM_File',
1.620 albertel 5151: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 5152: &GDBM_READER(),0640)) {
1.218 albertel 5153: $thisparm=$parmhash{$symbparm};
5154: untie(%parmhash);
5155: }
5156: if ($thisparm) { return $thisparm; }
5157: }
1.594 albertel 5158: # ------------------------------------------ fourth, look in resource metadata
1.71 www 5159:
1.218 albertel 5160: $spacequalifierrest=~s/\./\_/;
1.282 albertel 5161: my $filename;
5162: if (!$symbparm) { $symbparm=&symbread(); }
5163: if ($symbparm) {
1.409 www 5164: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 5165: } else {
1.620 albertel 5166: $filename=$env{'request.filename'};
1.282 albertel 5167: }
5168: my $metadata=&metadata($filename,$spacequalifierrest);
1.288 albertel 5169: if (defined($metadata)) { return $metadata; }
1.282 albertel 5170: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288 albertel 5171: if (defined($metadata)) { return $metadata; }
1.142 www 5172:
1.594 albertel 5173: # ---------------------------------------------- fourth, look in rest pf course
1.593 albertel 5174: if ($symbparm && defined($courseid) &&
1.620 albertel 5175: $courseid eq $env{'request.course.id'}) {
1.624 albertel 5176: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
5177: $env{'course.'.$courseid.'.domain'},
5178: 'course',
5179: ($courselevelm,$courselevel));
1.593 albertel 5180: if (defined($coursereply)) { return $coursereply; }
5181: }
1.145 www 5182: # ------------------------------------------------------------------ Cascade up
1.218 albertel 5183: unless ($space eq '0') {
1.336 albertel 5184: my @parts=split(/_/,$space);
5185: my $id=pop(@parts);
5186: my $part=join('_',@parts);
5187: if ($part eq '') { $part='0'; }
5188: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 5189: $symbparm,$udom,$uname,$section,1);
1.337 albertel 5190: if (defined($partgeneral)) { return $partgeneral; }
1.218 albertel 5191: }
1.395 albertel 5192: if ($recurse) { return undef; }
5193: my $pack_def=&packages_tab_default($filename,$varname);
5194: if (defined($pack_def)) { return $pack_def; }
1.71 www 5195:
1.48 www 5196: # ---------------------------------------------------- Any other user namespace
5197: } elsif ($realm eq 'environment') {
5198: # ----------------------------------------------------------------- environment
1.620 albertel 5199: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
5200: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 5201: } else {
5202: my %returnhash=&userenvironment($udom,$uname,
5203: $spacequalifierrest);
5204: return $returnhash{$spacequalifierrest};
5205: }
1.28 www 5206: } elsif ($realm eq 'system') {
1.48 www 5207: # ----------------------------------------------------------------- system.time
5208: if ($space eq 'time') {
5209: return time;
5210: }
1.696 albertel 5211: } elsif ($realm eq 'server') {
5212: # ----------------------------------------------------------------- system.time
5213: if ($space eq 'name') {
5214: return $ENV{'SERVER_NAME'};
5215: }
1.28 www 5216: }
1.48 www 5217: return '';
1.61 www 5218: }
5219:
1.691 raeburn 5220: sub check_group_parms {
5221: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
5222: my @groupitems = ();
5223: my $resultitem;
5224: my @levels = ($symbparm,$mapparm,$what);
5225: foreach my $group (@{$groups}) {
5226: foreach my $level (@levels) {
5227: my $item = $courseid.'.['.$group.'].'.$level;
5228: push(@groupitems,$item);
5229: }
5230: }
5231: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
5232: $env{'course.'.$courseid.'.domain'},
5233: 'course',@groupitems);
5234: return $coursereply;
5235: }
5236:
5237: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
5238: my ($grouplist,$courseid) = @_;
5239: my @groups = split/:/,$grouplist;
5240: if (@groups > 1) {
5241: @groups = sort(@groups);
5242: }
5243: return @groups;
5244: }
5245:
1.395 albertel 5246: sub packages_tab_default {
5247: my ($uri,$varname)=@_;
5248: my (undef,$part,$name)=split(/\./,$varname);
5249: my $packages=&metadata($uri,'packages');
5250: foreach my $package (split(/,/,$packages)) {
5251: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.468 albertel 5252: if (defined($packagetab{"$pack_type&$name&default"})) {
5253: return $packagetab{"$pack_type&$name&default"};
5254: }
1.585 albertel 5255: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 5256: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
5257: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 5258: }
5259: }
5260: return undef;
5261: }
5262:
1.334 albertel 5263: sub add_prefix_and_part {
5264: my ($prefix,$part)=@_;
5265: my $keyroot;
5266: if (defined($prefix) && $prefix !~ /^__/) {
5267: # prefix that has a part already
5268: $keyroot=$prefix;
5269: } elsif (defined($prefix)) {
5270: # prefix that is missing a part
5271: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
5272: } else {
5273: # no prefix at all
5274: if (defined($part)) { $keyroot='_'.$part; }
5275: }
5276: return $keyroot;
5277: }
5278:
1.71 www 5279: # ---------------------------------------------------------------- Get metadata
5280:
1.599 albertel 5281: my %metaentry;
1.71 www 5282: sub metadata {
1.176 www 5283: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 5284: $uri=&declutter($uri);
1.288 albertel 5285: # if it is a non metadata possible uri return quickly
1.529 albertel 5286: if (($uri eq '') ||
5287: (($uri =~ m|^/*adm/|) &&
1.698 albertel 5288: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423 albertel 5289: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.489 albertel 5290: ($uri =~ m|home/[^/]+/public_html/|)) {
1.468 albertel 5291: return undef;
1.288 albertel 5292: }
1.73 www 5293: my $filename=$uri;
5294: $uri=~s/\.meta$//;
1.172 www 5295: #
5296: # Is the metadata already cached?
1.177 www 5297: # Look at timestamp of caching
1.172 www 5298: # Everything is cached by the main uri, libraries are never directly cached
5299: #
1.428 albertel 5300: if (!defined($liburi)) {
1.599 albertel 5301: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 5302: if (defined($cached)) { return $result->{':'.$what}; }
5303: }
5304: {
1.172 www 5305: #
5306: # Is this a recursive call for a library?
5307: #
1.599 albertel 5308: # if (! exists($metacache{$uri})) {
5309: # $metacache{$uri}={};
5310: # }
1.171 www 5311: if ($liburi) {
5312: $liburi=&declutter($liburi);
5313: $filename=$liburi;
1.401 bowersj2 5314: } else {
1.599 albertel 5315: &devalidate_cache_new('meta',$uri);
5316: undef(%metaentry);
1.401 bowersj2 5317: }
1.140 www 5318: my %metathesekeys=();
1.73 www 5319: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 5320: my $metastring;
1.609 banghart 5321: if ($uri !~ m -^(uploaded|editupload)/-) {
1.543 albertel 5322: my $file=&filelocation('',&clutter($filename));
1.599 albertel 5323: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 5324: $metastring=&getfile($file);
1.489 albertel 5325: }
1.208 albertel 5326: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 5327: my $token;
1.140 www 5328: undef %metathesekeys;
1.71 www 5329: while ($token=$parser->get_token) {
1.339 albertel 5330: if ($token->[0] eq 'S') {
5331: if (defined($token->[2]->{'package'})) {
1.172 www 5332: #
5333: # This is a package - get package info
5334: #
1.339 albertel 5335: my $package=$token->[2]->{'package'};
5336: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
5337: if (defined($token->[2]->{'id'})) {
5338: $keyroot.='_'.$token->[2]->{'id'};
5339: }
1.599 albertel 5340: if ($metaentry{':packages'}) {
5341: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 5342: } else {
1.599 albertel 5343: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 5344: }
1.613 albertel 5345: foreach (sort keys %packagetab) {
1.432 albertel 5346: my $part=$keyroot;
5347: $part=~s/^\_//;
5348: if ($_=~/^\Q$package\E\&/ ||
5349: $_=~/^\Q$package\E_0\&/) {
1.339 albertel 5350: my ($pack,$name,$subp)=split(/\&/,$_);
1.395 albertel 5351: # ignore package.tab specified default values
5352: # here &package_tab_default() will fetch those
5353: if ($subp eq 'default') { next; }
1.339 albertel 5354: my $value=$packagetab{$_};
1.432 albertel 5355: my $unikey;
5356: if ($pack =~ /_0$/) {
5357: $unikey='parameter_0_'.$name;
5358: $part=0;
5359: } else {
5360: $unikey='parameter'.$keyroot.'_'.$name;
5361: }
1.339 albertel 5362: if ($subp eq 'display') {
5363: $value.=' [Part: '.$part.']';
5364: }
1.599 albertel 5365: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 5366: $metathesekeys{$unikey}=1;
1.599 albertel 5367: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
5368: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 5369: }
1.599 albertel 5370: if (defined($metaentry{':'.$unikey.'.default'})) {
5371: $metaentry{':'.$unikey}=
5372: $metaentry{':'.$unikey.'.default'};
1.356 albertel 5373: }
1.339 albertel 5374: }
5375: }
5376: } else {
1.172 www 5377: #
5378: # This is not a package - some other kind of start tag
1.339 albertel 5379: #
5380: my $entry=$token->[1];
5381: my $unikey;
5382: if ($entry eq 'import') {
5383: $unikey='';
5384: } else {
5385: $unikey=$entry;
5386: }
5387: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
5388:
5389: if (defined($token->[2]->{'id'})) {
5390: $unikey.='_'.$token->[2]->{'id'};
5391: }
1.175 www 5392:
1.339 albertel 5393: if ($entry eq 'import') {
1.175 www 5394: #
5395: # Importing a library here
1.339 albertel 5396: #
5397: if ($depthcount<20) {
5398: my $location=$parser->get_text('/import');
5399: my $dir=$filename;
5400: $dir=~s|[^/]*$||;
5401: $location=&filelocation($dir,$location);
5402: foreach (sort(split(/\,/,&metadata($uri,'keys',
5403: $location,$unikey,
5404: $depthcount+1)))) {
1.599 albertel 5405: $metaentry{':'.$_}=$metaentry{':'.$_};
1.339 albertel 5406: $metathesekeys{$_}=1;
5407: }
5408: }
5409: } else {
5410:
5411: if (defined($token->[2]->{'name'})) {
5412: $unikey.='_'.$token->[2]->{'name'};
5413: }
5414: $metathesekeys{$unikey}=1;
5415: foreach (@{$token->[3]}) {
1.599 albertel 5416: $metaentry{':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.339 albertel 5417: }
5418: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 5419: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 5420: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
5421: # only ws inside the tag, and not in default, so use default
5422: # as value
1.599 albertel 5423: $metaentry{':'.$unikey}=$default;
1.339 albertel 5424: } else {
1.321 albertel 5425: # either something interesting inside the tag or default
5426: # uninteresting
1.599 albertel 5427: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 5428: }
1.172 www 5429: # end of not-a-package not-a-library import
1.339 albertel 5430: }
1.172 www 5431: # end of not-a-package start tag
1.339 albertel 5432: }
1.172 www 5433: # the next is the end of "start tag"
1.339 albertel 5434: }
5435: }
1.483 albertel 5436: my ($extension) = ($uri =~ /\.(\w+)$/);
5437: foreach my $key (sort(keys(%packagetab))) {
5438: #no specific packages #how's our extension
5439: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 5440: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 5441: \%metathesekeys);
5442: }
1.599 albertel 5443: if (!exists($metaentry{':packages'})) {
1.483 albertel 5444: foreach my $key (sort(keys(%packagetab))) {
5445: #no specific packages well let's get default then
5446: if ($key!~/^default&/) { next; }
1.488 albertel 5447: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 5448: \%metathesekeys);
5449: }
5450: }
1.338 www 5451: # are there custom rights to evaluate
1.599 albertel 5452: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 5453:
1.338 www 5454: #
5455: # Importing a rights file here
1.339 albertel 5456: #
5457: unless ($depthcount) {
1.599 albertel 5458: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 5459: my $dir=$filename;
5460: $dir=~s|[^/]*$||;
5461: $location=&filelocation($dir,$location);
5462: foreach (sort(split(/\,/,&metadata($uri,'keys',
5463: $location,'_rights',
5464: $depthcount+1)))) {
1.599 albertel 5465: #$metaentry{':'.$_}=$metacache{$uri}->{':'.$_};
1.339 albertel 5466: $metathesekeys{$_}=1;
5467: }
5468: }
5469: }
1.599 albertel 5470: $metaentry{':keys'}=join(',',keys %metathesekeys);
5471: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
5472: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699 albertel 5473: &do_cache_new('meta',$uri,\%metaentry,60*60);
1.177 www 5474: # this is the end of "was not already recently cached
1.71 www 5475: }
1.599 albertel 5476: return $metaentry{':'.$what};
1.261 albertel 5477: }
5478:
1.488 albertel 5479: sub metadata_create_package_def {
1.483 albertel 5480: my ($uri,$key,$package,$metathesekeys)=@_;
5481: my ($pack,$name,$subp)=split(/\&/,$key);
5482: if ($subp eq 'default') { next; }
5483:
1.599 albertel 5484: if (defined($metaentry{':packages'})) {
5485: $metaentry{':packages'}.=','.$package;
1.483 albertel 5486: } else {
1.599 albertel 5487: $metaentry{':packages'}=$package;
1.483 albertel 5488: }
5489: my $value=$packagetab{$key};
5490: my $unikey;
5491: $unikey='parameter_0_'.$name;
1.599 albertel 5492: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 5493: $$metathesekeys{$unikey}=1;
1.599 albertel 5494: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
5495: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 5496: }
1.599 albertel 5497: if (defined($metaentry{':'.$unikey.'.default'})) {
5498: $metaentry{':'.$unikey}=
5499: $metaentry{':'.$unikey.'.default'};
1.483 albertel 5500: }
5501: }
5502:
1.261 albertel 5503: sub metadata_generate_part0 {
5504: my ($metadata,$metacache,$uri) = @_;
5505: my %allnames;
5506: foreach my $metakey (sort keys %$metadata) {
5507: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 5508: my $part=$$metacache{':'.$metakey.'.part'};
5509: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 5510: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 5511: $allnames{$name}=$part;
5512: }
5513: }
5514: }
5515: foreach my $name (keys(%allnames)) {
5516: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 5517: my $key=":parameter_0_$name";
1.261 albertel 5518: $$metacache{"$key.part"}='0';
5519: $$metacache{"$key.name"}=$name;
1.428 albertel 5520: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 5521: $allnames{$name}.'_'.$name.
5522: '.type'};
1.428 albertel 5523: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 5524: '.display'};
1.644 www 5525: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 5526: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 5527: $$metacache{"$key.display"}=$olddis;
5528: }
1.71 www 5529: }
5530:
1.301 www 5531: # ------------------------------------------------- Get the title of a resource
5532:
5533: sub gettitle {
5534: my $urlsymb=shift;
5535: my $symb=&symbread($urlsymb);
1.534 albertel 5536: if ($symb) {
1.620 albertel 5537: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 5538: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 5539: if (defined($cached)) {
5540: return $result;
5541: }
1.534 albertel 5542: my ($map,$resid,$url)=&decode_symb($symb);
5543: my $title='';
5544: my %bighash;
1.620 albertel 5545: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.534 albertel 5546: &GDBM_READER(),0640)) {
5547: my $mapid=$bighash{'map_pc_'.&clutter($map)};
5548: $title=$bighash{'title_'.$mapid.'.'.$resid};
5549: untie %bighash;
5550: }
5551: $title=~s/\&colon\;/\:/gs;
5552: if ($title) {
1.599 albertel 5553: return &do_cache_new('title',$key,$title,600);
1.534 albertel 5554: }
5555: $urlsymb=$url;
5556: }
5557: my $title=&metadata($urlsymb,'title');
5558: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
5559: return $title;
1.301 www 5560: }
1.613 albertel 5561:
1.614 albertel 5562: sub get_slot {
5563: my ($which,$cnum,$cdom)=@_;
5564: if (!$cnum || !$cdom) {
5565: (undef,my $courseid)=&Apache::lonxml::whichuser();
1.620 albertel 5566: $cdom=$env{'course.'.$courseid.'.domain'};
5567: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 5568: }
1.703 albertel 5569: my $key=join("\0",'slots',$cdom,$cnum,$which);
5570: my %slotinfo;
5571: if (exists($remembered{$key})) {
5572: $slotinfo{$which} = $remembered{$key};
5573: } else {
5574: %slotinfo=&get('slots',[$which],$cdom,$cnum);
5575: &Apache::lonhomework::showhash(%slotinfo);
5576: my ($tmp)=keys(%slotinfo);
5577: if ($tmp=~/^error:/) { return (); }
5578: $remembered{$key} = $slotinfo{$which};
5579: }
1.616 albertel 5580: if (ref($slotinfo{$which}) eq 'HASH') {
5581: return %{$slotinfo{$which}};
5582: }
5583: return $slotinfo{$which};
1.614 albertel 5584: }
1.31 www 5585: # ------------------------------------------------- Update symbolic store links
5586:
5587: sub symblist {
5588: my ($mapname,%newhash)=@_;
1.438 www 5589: $mapname=&deversion(&declutter($mapname));
1.31 www 5590: my %hash;
1.620 albertel 5591: if (($env{'request.course.fn'}) && (%newhash)) {
5592: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 5593: &GDBM_WRCREAT(),0640)) {
1.711 albertel 5594: foreach my $url (keys %newhash) {
5595: next if ($url eq 'last_known'
5596: && $env{'form.no_update_last_known'});
5597: $hash{declutter($url)}=&encode_symb($mapname,
5598: $newhash{$url}->[1],
5599: $newhash{$url}->[0]);
1.191 harris41 5600: }
1.31 www 5601: if (untie(%hash)) {
5602: return 'ok';
5603: }
5604: }
5605: }
5606: return 'error';
1.212 www 5607: }
5608:
5609: # --------------------------------------------------------------- Verify a symb
5610:
5611: sub symbverify {
1.510 www 5612: my ($symb,$thisurl)=@_;
5613: my $thisfn=$thisurl;
5614: # wrapper not part of symbs
5615: $thisfn=~s/^\/adm\/wrapper//;
1.694 albertel 5616: $thisfn=~s/^\/adm\/coursedocs\/showdoc\///;
1.439 www 5617: $thisfn=&declutter($thisfn);
1.215 www 5618: # direct jump to resource in page or to a sequence - will construct own symbs
5619: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
5620: # check URL part
1.409 www 5621: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 5622:
1.431 www 5623: unless ($url eq $thisfn) { return 0; }
1.213 www 5624:
1.216 www 5625: $symb=&symbclean($symb);
1.510 www 5626: $thisurl=&deversion($thisurl);
1.439 www 5627: $thisfn=&deversion($thisfn);
1.213 www 5628:
5629: my %bighash;
5630: my $okay=0;
1.431 www 5631:
1.620 albertel 5632: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 5633: &GDBM_READER(),0640)) {
1.510 www 5634: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 5635: unless ($ids) {
1.510 www 5636: $ids=$bighash{'ids_/'.$thisurl};
1.216 www 5637: }
5638: if ($ids) {
5639: # ------------------------------------------------------------------- Has ID(s)
5640: foreach (split(/\,/,$ids)) {
1.644 www 5641: my ($mapid,$resid)=split(/\./,$_);
1.216 www 5642: if (
5643: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
5644: eq $symb) {
1.620 albertel 5645: if (($env{'request.role.adv'}) ||
5646: $bighash{'encrypted_'.$_} eq $env{'request.enc'}) {
1.582 albertel 5647: $okay=1;
5648: }
5649: }
1.216 www 5650: }
5651: }
1.213 www 5652: untie(%bighash);
5653: }
5654: return $okay;
1.31 www 5655: }
5656:
1.210 www 5657: # --------------------------------------------------------------- Clean-up symb
5658:
5659: sub symbclean {
5660: my $symb=shift;
1.568 albertel 5661: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 5662: # remove version from map
5663: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 5664:
1.210 www 5665: # remove version from URL
5666: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 5667:
1.507 www 5668: # remove wrapper
5669:
1.510 www 5670: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 5671: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 5672: return $symb;
1.409 www 5673: }
5674:
5675: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 5676:
5677: sub encode_symb {
5678: my ($map,$resid,$url)=@_;
5679: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
5680: }
1.409 www 5681:
5682: sub decode_symb {
1.568 albertel 5683: my $symb=shift;
5684: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
5685: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 5686: return (&fixversion($map),$resid,&fixversion($url));
5687: }
5688:
5689: sub fixversion {
5690: my $fn=shift;
1.609 banghart 5691: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 5692: my %bighash;
5693: my $uri=&clutter($fn);
1.620 albertel 5694: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 5695: # is this cached?
1.599 albertel 5696: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 5697: if (defined($cached)) { return $result; }
5698: # unfortunately not cached, or expired
1.620 albertel 5699: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 5700: &GDBM_READER(),0640)) {
5701: if ($bighash{'version_'.$uri}) {
5702: my $version=$bighash{'version_'.$uri};
1.444 www 5703: unless (($version eq 'mostrecent') ||
5704: ($version==&getversion($uri))) {
1.440 www 5705: $uri=~s/\.(\w+)$/\.$version\.$1/;
5706: }
5707: }
5708: untie %bighash;
1.413 www 5709: }
1.599 albertel 5710: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 5711: }
5712:
5713: sub deversion {
5714: my $url=shift;
5715: $url=~s/\.\d+\.(\w+)$/\.$1/;
5716: return $url;
1.210 www 5717: }
5718:
1.31 www 5719: # ------------------------------------------------------ Return symb list entry
5720:
5721: sub symbread {
1.249 www 5722: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 5723: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 5724: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 5725: # no filename provided? try from environment
1.44 www 5726: unless ($thisfn) {
1.620 albertel 5727: if ($env{'request.symb'}) {
5728: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 5729: }
1.620 albertel 5730: $thisfn=$env{'request.filename'};
1.44 www 5731: }
1.569 albertel 5732: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 5733: # is that filename actually a symb? Verify, clean, and return
5734: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 5735: if (&symbverify($thisfn,$1)) {
1.620 albertel 5736: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 5737: }
1.242 www 5738: }
1.44 www 5739: $thisfn=declutter($thisfn);
1.31 www 5740: my %hash;
1.37 www 5741: my %bighash;
5742: my $syval='';
1.620 albertel 5743: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 5744: my $targetfn = $thisfn;
1.609 banghart 5745: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 5746: $targetfn = 'adm/wrapper/'.$thisfn;
5747: }
1.687 albertel 5748: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
5749: $targetfn=$1;
5750: }
1.620 albertel 5751: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 5752: &GDBM_READER(),0640)) {
1.481 raeburn 5753: $syval=$hash{$targetfn};
1.37 www 5754: untie(%hash);
5755: }
5756: # ---------------------------------------------------------- There was an entry
5757: if ($syval) {
1.601 albertel 5758: #unless ($syval=~/\_\d+$/) {
1.620 albertel 5759: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601 albertel 5760: #&appenv('request.ambiguous' => $thisfn);
1.620 albertel 5761: #return $env{$cache_str}='';
1.601 albertel 5762: #}
5763: #$syval.=$1;
5764: #}
1.37 www 5765: } else {
5766: # ------------------------------------------------------- Was not in symb table
1.620 albertel 5767: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 5768: &GDBM_READER(),0640)) {
1.37 www 5769: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 5770: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 5771: unless ($ids) {
5772: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 5773: }
5774: unless ($ids) {
5775: # alias?
5776: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 5777: }
1.37 www 5778: if ($ids) {
5779: # ------------------------------------------------------------------- Has ID(s)
5780: my @possibilities=split(/\,/,$ids);
1.39 www 5781: if ($#possibilities==0) {
5782: # ----------------------------------------------- There is only one possibility
1.37 www 5783: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 5784: $syval=&encode_symb($bighash{'map_id_'.$mapid},
5785: $resid,$thisfn);
1.249 www 5786: } elsif (!$donotrecurse) {
1.39 www 5787: # ------------------------------------------ There is more than one possibility
5788: my $realpossible=0;
1.191 harris41 5789: foreach (@possibilities) {
1.39 www 5790: my $file=$bighash{'src_'.$_};
5791: if (&allowed('bre',$file)) {
5792: my ($mapid,$resid)=split(/\./,$_);
5793: if ($bighash{'map_type_'.$mapid} ne 'page') {
5794: $realpossible++;
1.626 albertel 5795: $syval=&encode_symb($bighash{'map_id_'.$mapid},
5796: $resid,$thisfn);
1.39 www 5797: }
5798: }
1.191 harris41 5799: }
1.39 www 5800: if ($realpossible!=1) { $syval=''; }
1.249 www 5801: } else {
5802: $syval='';
1.37 www 5803: }
5804: }
5805: untie(%bighash)
1.481 raeburn 5806: }
1.31 www 5807: }
1.62 www 5808: if ($syval) {
1.620 albertel 5809: return $env{$cache_str}=$syval;
1.62 www 5810: }
1.31 www 5811: }
1.44 www 5812: &appenv('request.ambiguous' => $thisfn);
1.620 albertel 5813: return $env{$cache_str}='';
1.31 www 5814: }
5815:
5816: # ---------------------------------------------------------- Return random seed
5817:
1.32 www 5818: sub numval {
5819: my $txt=shift;
5820: $txt=~tr/A-J/0-9/;
5821: $txt=~tr/a-j/0-9/;
5822: $txt=~tr/K-T/0-9/;
5823: $txt=~tr/k-t/0-9/;
5824: $txt=~tr/U-Z/0-5/;
5825: $txt=~tr/u-z/0-5/;
5826: $txt=~s/\D//g;
1.564 albertel 5827: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 5828: return int($txt);
1.368 albertel 5829: }
5830:
1.484 albertel 5831: sub numval2 {
5832: my $txt=shift;
5833: $txt=~tr/A-J/0-9/;
5834: $txt=~tr/a-j/0-9/;
5835: $txt=~tr/K-T/0-9/;
5836: $txt=~tr/k-t/0-9/;
5837: $txt=~tr/U-Z/0-5/;
5838: $txt=~tr/u-z/0-5/;
5839: $txt=~s/\D//g;
5840: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
5841: my $total;
5842: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 5843: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 5844: return int($total);
5845: }
5846:
1.575 albertel 5847: sub numval3 {
5848: use integer;
5849: my $txt=shift;
5850: $txt=~tr/A-J/0-9/;
5851: $txt=~tr/a-j/0-9/;
5852: $txt=~tr/K-T/0-9/;
5853: $txt=~tr/k-t/0-9/;
5854: $txt=~tr/U-Z/0-5/;
5855: $txt=~tr/u-z/0-5/;
5856: $txt=~s/\D//g;
5857: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
5858: my $total;
5859: foreach my $val (@txts) { $total+=$val; }
5860: if ($_64bit) { $total=(($total<<32)>>32); }
5861: return $total;
5862: }
5863:
1.675 albertel 5864: sub digest {
5865: my ($data)=@_;
5866: my $digest=&Digest::MD5::md5($data);
5867: my ($a,$b,$c,$d)=unpack("iiii",$digest);
5868: my ($e,$f);
5869: {
5870: use integer;
5871: $e=($a+$b);
5872: $f=($c+$d);
5873: if ($_64bit) {
5874: $e=(($e<<32)>>32);
5875: $f=(($f<<32)>>32);
5876: }
5877: }
5878: if (wantarray) {
5879: return ($e,$f);
5880: } else {
5881: my $g;
5882: {
5883: use integer;
5884: $g=($e+$f);
5885: if ($_64bit) {
5886: $g=(($g<<32)>>32);
5887: }
5888: }
5889: return $g;
5890: }
5891: }
5892:
1.368 albertel 5893: sub latest_rnd_algorithm_id {
1.675 albertel 5894: return '64bit5';
1.366 albertel 5895: }
1.32 www 5896:
1.503 albertel 5897: sub get_rand_alg {
5898: my ($courseid)=@_;
5899: if (!$courseid) { $courseid=(&Apache::lonxml::whichuser())[1]; }
5900: if ($courseid) {
1.620 albertel 5901: return $env{"course.$courseid.rndseed"};
1.503 albertel 5902: }
5903: return &latest_rnd_algorithm_id();
5904: }
5905:
1.562 albertel 5906: sub validCODE {
5907: my ($CODE)=@_;
5908: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
5909: return 0;
5910: }
5911:
1.491 albertel 5912: sub getCODE {
1.620 albertel 5913: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 5914: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
5915: defined($Apache::lonhomework::parsing_a_task) ) &&
5916: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 5917: return $Apache::lonhomework::history{'resource.CODE'};
5918: }
5919: return undef;
5920: }
5921:
1.31 www 5922: sub rndseed {
1.155 albertel 5923: my ($symb,$courseid,$domain,$username)=@_;
1.366 albertel 5924:
5925: my ($wsymb,$wcourseid,$wdomain,$wusername)=&Apache::lonxml::whichuser();
1.155 albertel 5926: if (!$symb) {
1.366 albertel 5927: unless ($symb=$wsymb) { return time; }
5928: }
5929: if (!$courseid) { $courseid=$wcourseid; }
5930: if (!$domain) { $domain=$wdomain; }
5931: if (!$username) { $username=$wusername }
1.503 albertel 5932: my $which=&get_rand_alg();
1.491 albertel 5933: if (defined(&getCODE())) {
1.675 albertel 5934: if ($which eq '64bit5') {
5935: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
5936: } elsif ($which eq '64bit4') {
1.575 albertel 5937: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
5938: } else {
5939: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
5940: }
1.675 albertel 5941: } elsif ($which eq '64bit5') {
5942: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 5943: } elsif ($which eq '64bit4') {
5944: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 5945: } elsif ($which eq '64bit3') {
5946: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 5947: } elsif ($which eq '64bit2') {
5948: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 5949: } elsif ($which eq '64bit') {
5950: return &rndseed_64bit($symb,$courseid,$domain,$username);
5951: }
5952: return &rndseed_32bit($symb,$courseid,$domain,$username);
5953: }
5954:
5955: sub rndseed_32bit {
5956: my ($symb,$courseid,$domain,$username)=@_;
5957: {
5958: use integer;
5959: my $symbchck=unpack("%32C*",$symb) << 27;
5960: my $symbseed=numval($symb) << 22;
5961: my $namechck=unpack("%32C*",$username) << 17;
5962: my $nameseed=numval($username) << 12;
5963: my $domainseed=unpack("%32C*",$domain) << 7;
5964: my $courseseed=unpack("%32C*",$courseid);
5965: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
5966: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
5967: #&Apache::lonxml::debug("rndseed :$num:$symb");
1.564 albertel 5968: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 5969: return $num;
5970: }
5971: }
5972:
5973: sub rndseed_64bit {
5974: my ($symb,$courseid,$domain,$username)=@_;
5975: {
5976: use integer;
5977: my $symbchck=unpack("%32S*",$symb) << 21;
5978: my $symbseed=numval($symb) << 10;
5979: my $namechck=unpack("%32S*",$username);
5980:
5981: my $nameseed=numval($username) << 21;
5982: my $domainseed=unpack("%32S*",$domain) << 10;
5983: my $courseseed=unpack("%32S*",$courseid);
5984:
5985: my $num1=$symbchck+$symbseed+$namechck;
5986: my $num2=$nameseed+$domainseed+$courseseed;
5987: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
5988: #&Apache::lonxml::debug("rndseed :$num:$symb");
1.564 albertel 5989: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
5990: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 5991: return "$num1,$num2";
1.155 albertel 5992: }
1.366 albertel 5993: }
5994:
1.443 albertel 5995: sub rndseed_64bit2 {
5996: my ($symb,$courseid,$domain,$username)=@_;
5997: {
5998: use integer;
5999: # strings need to be an even # of cahracters long, it it is odd the
6000: # last characters gets thrown away
6001: my $symbchck=unpack("%32S*",$symb.' ') << 21;
6002: my $symbseed=numval($symb) << 10;
6003: my $namechck=unpack("%32S*",$username.' ');
6004:
6005: my $nameseed=numval($username) << 21;
1.501 albertel 6006: my $domainseed=unpack("%32S*",$domain.' ') << 10;
6007: my $courseseed=unpack("%32S*",$courseid.' ');
6008:
6009: my $num1=$symbchck+$symbseed+$namechck;
6010: my $num2=$nameseed+$domainseed+$courseseed;
6011: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
6012: #&Apache::lonxml::debug("rndseed :$num:$symb");
6013: return "$num1,$num2";
6014: }
6015: }
6016:
6017: sub rndseed_64bit3 {
6018: my ($symb,$courseid,$domain,$username)=@_;
6019: {
6020: use integer;
6021: # strings need to be an even # of cahracters long, it it is odd the
6022: # last characters gets thrown away
6023: my $symbchck=unpack("%32S*",$symb.' ') << 21;
6024: my $symbseed=numval2($symb) << 10;
6025: my $namechck=unpack("%32S*",$username.' ');
6026:
6027: my $nameseed=numval2($username) << 21;
1.443 albertel 6028: my $domainseed=unpack("%32S*",$domain.' ') << 10;
6029: my $courseseed=unpack("%32S*",$courseid.' ');
6030:
6031: my $num1=$symbchck+$symbseed+$namechck;
6032: my $num2=$nameseed+$domainseed+$courseseed;
6033: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
1.564 albertel 6034: #&Apache::lonxml::debug("rndseed :$num1:$num2:$_64bit");
6035: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
6036:
1.503 albertel 6037: return "$num1:$num2";
1.443 albertel 6038: }
6039: }
6040:
1.575 albertel 6041: sub rndseed_64bit4 {
6042: my ($symb,$courseid,$domain,$username)=@_;
6043: {
6044: use integer;
6045: # strings need to be an even # of cahracters long, it it is odd the
6046: # last characters gets thrown away
6047: my $symbchck=unpack("%32S*",$symb.' ') << 21;
6048: my $symbseed=numval3($symb) << 10;
6049: my $namechck=unpack("%32S*",$username.' ');
6050:
6051: my $nameseed=numval3($username) << 21;
6052: my $domainseed=unpack("%32S*",$domain.' ') << 10;
6053: my $courseseed=unpack("%32S*",$courseid.' ');
6054:
6055: my $num1=$symbchck+$symbseed+$namechck;
6056: my $num2=$nameseed+$domainseed+$courseseed;
6057: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
6058: #&Apache::lonxml::debug("rndseed :$num1:$num2:$_64bit");
6059: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
6060:
6061: return "$num1:$num2";
6062: }
6063: }
6064:
1.675 albertel 6065: sub rndseed_64bit5 {
6066: my ($symb,$courseid,$domain,$username)=@_;
6067: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
6068: return "$num1:$num2";
6069: }
6070:
1.366 albertel 6071: sub rndseed_CODE_64bit {
6072: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 6073: {
1.366 albertel 6074: use integer;
1.443 albertel 6075: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 6076: my $symbseed=numval2($symb);
1.491 albertel 6077: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
6078: my $CODEseed=numval(&getCODE());
1.443 albertel 6079: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 6080: my $num1=$symbseed+$CODEchck;
6081: my $num2=$CODEseed+$courseseed+$symbchck;
6082: #&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
1.366 albertel 6083: #&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
1.564 albertel 6084: if ($_64bit) { $num1=(($num1<<32)>>32); }
6085: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 6086: return "$num1:$num2";
1.366 albertel 6087: }
6088: }
6089:
1.575 albertel 6090: sub rndseed_CODE_64bit4 {
6091: my ($symb,$courseid,$domain,$username)=@_;
6092: {
6093: use integer;
6094: my $symbchck=unpack("%32S*",$symb.' ') << 16;
6095: my $symbseed=numval3($symb);
6096: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
6097: my $CODEseed=numval3(&getCODE());
6098: my $courseseed=unpack("%32S*",$courseid.' ');
6099: my $num1=$symbseed+$CODEchck;
6100: my $num2=$CODEseed+$courseseed+$symbchck;
6101: #&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
6102: #&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
6103: if ($_64bit) { $num1=(($num1<<32)>>32); }
6104: if ($_64bit) { $num2=(($num2<<32)>>32); }
6105: return "$num1:$num2";
6106: }
6107: }
6108:
1.675 albertel 6109: sub rndseed_CODE_64bit5 {
6110: my ($symb,$courseid,$domain,$username)=@_;
6111: my $code = &getCODE();
6112: my ($num1,$num2)=&digest("$symb,$courseid,$code");
6113: return "$num1:$num2";
6114: }
6115:
1.366 albertel 6116: sub setup_random_from_rndseed {
6117: my ($rndseed)=@_;
1.503 albertel 6118: if ($rndseed =~/([,:])/) {
6119: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 6120: &Math::Random::random_set_seed(abs($num1),abs($num2));
6121: } else {
6122: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 6123: }
1.36 albertel 6124: }
6125:
1.474 albertel 6126: sub latest_receipt_algorithm_id {
6127: return 'receipt2';
6128: }
6129:
1.480 www 6130: sub recunique {
6131: my $fucourseid=shift;
6132: my $unique;
1.620 albertel 6133: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
6134: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 6135: } else {
6136: $unique=$perlvar{'lonReceipt'};
6137: }
6138: return unpack("%32C*",$unique);
6139: }
6140:
6141: sub recprefix {
6142: my $fucourseid=shift;
6143: my $prefix;
1.620 albertel 6144: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
6145: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 6146: } else {
6147: $prefix=$perlvar{'lonHostID'};
6148: }
6149: return unpack("%32C*",$prefix);
6150: }
6151:
1.76 www 6152: sub ireceipt {
1.474 albertel 6153: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.76 www 6154: my $cuname=unpack("%32C*",$funame);
6155: my $cudom=unpack("%32C*",$fudom);
6156: my $cucourseid=unpack("%32C*",$fucourseid);
6157: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 6158: my $cunique=&recunique($fucourseid);
1.474 albertel 6159: my $cpart=unpack("%32S*",$part);
1.480 www 6160: my $return =&recprefix($fucourseid).'-';
1.620 albertel 6161: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
6162: $env{'request.state'} eq 'construct') {
1.474 albertel 6163: &Apache::lonxml::debug("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname).
6164: " and ".($cpart%$cudom));
6165:
6166: $return.= ($cunique%$cuname+
6167: $cunique%$cudom+
6168: $cusymb%$cuname+
6169: $cusymb%$cudom+
6170: $cucourseid%$cuname+
6171: $cucourseid%$cudom+
6172: $cpart%$cuname+
6173: $cpart%$cudom);
6174: } else {
6175: $return.= ($cunique%$cuname+
6176: $cunique%$cudom+
6177: $cusymb%$cuname+
6178: $cusymb%$cudom+
6179: $cucourseid%$cuname+
6180: $cucourseid%$cudom);
6181: }
6182: return $return;
1.76 www 6183: }
6184:
6185: sub receipt {
1.474 albertel 6186: my ($part)=@_;
6187: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
6188: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 6189: }
1.260 ng 6190:
1.36 albertel 6191: # ------------------------------------------------------------ Serves up a file
1.472 albertel 6192: # returns either the contents of the file or
6193: # -1 if the file doesn't exist
1.481 raeburn 6194: #
6195: # if the target is a file that was uploaded via DOCS,
6196: # a check will be made to see if a current copy exists on the local server,
6197: # if it does this will be served, otherwise a copy will be retrieved from
6198: # the home server for the course and stored in /home/httpd/html/userfiles on
6199: # the local server.
1.472 albertel 6200:
1.36 albertel 6201: sub getfile {
1.538 albertel 6202: my ($file) = @_;
1.609 banghart 6203: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 6204: &repcopy($file);
6205: return &readfile($file);
6206: }
6207:
6208: sub repcopy_userfile {
6209: my ($file)=@_;
1.609 banghart 6210: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 6211: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 6212: my ($cdom,$cnum,$filename) =
6213: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+([^/]+)/+([^/]+)/+(.*)|);
6214: my ($info,$rtncode);
6215: my $uri="/uploaded/$cdom/$cnum/$filename";
6216: if (-e "$file") {
6217: my @fileinfo = stat($file);
6218: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 6219: if ($lwpresp ne 'ok') {
6220: if ($rtncode eq '404') {
1.538 albertel 6221: unlink($file);
1.482 albertel 6222: }
1.517 albertel 6223: #my $ua=new LWP::UserAgent;
1.538 albertel 6224: #my $request=new HTTP::Request('GET',&tokenwrapper($uri));
1.517 albertel 6225: #my $response=$ua->request($request);
6226: #if ($response->is_success()) {
6227: # return $response->content;
6228: # } else {
6229: # return -1;
6230: # }
1.482 albertel 6231: return -1;
6232: }
6233: if ($info < $fileinfo[9]) {
1.607 raeburn 6234: return 'ok';
1.482 albertel 6235: }
6236: $info = '';
1.538 albertel 6237: $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 6238: if ($lwpresp ne 'ok') {
6239: return -1;
6240: }
6241: } else {
1.538 albertel 6242: my $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 6243: if ($lwpresp ne 'ok') {
1.517 albertel 6244: my $ua=new LWP::UserAgent;
1.538 albertel 6245: my $request=new HTTP::Request('GET',&tokenwrapper($uri));
1.517 albertel 6246: my $response=$ua->request($request);
6247: if ($response->is_success()) {
1.538 albertel 6248: $info=$response->content;
1.517 albertel 6249: } else {
6250: return -1;
6251: }
1.482 albertel 6252: }
6253: my @parts = ($cdom,$cnum);
6254: if ($filename =~ m|^(.+)/[^/]+$|) {
6255: push @parts, split(/\//,$1);
1.518 albertel 6256: }
1.538 albertel 6257: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
1.482 albertel 6258: foreach my $part (@parts) {
6259: $path .= '/'.$part;
6260: if (!-e $path) {
6261: mkdir($path,0770);
6262: }
6263: }
6264: }
1.538 albertel 6265: open(FILE,">$file");
1.482 albertel 6266: print FILE $info;
6267: close(FILE);
1.607 raeburn 6268: return 'ok';
1.481 raeburn 6269: }
6270:
1.517 albertel 6271: sub tokenwrapper {
6272: my $uri=shift;
1.552 albertel 6273: $uri=~s|^http\://([^/]+)||;
6274: $uri=~s|^/||;
1.620 albertel 6275: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 6276: my $token=$1;
1.552 albertel 6277: my (undef,$udom,$uname,$file)=split('/',$uri,4);
6278: if ($udom && $uname && $file) {
6279: $file=~s|(\?\.*)*$||;
1.620 albertel 6280: &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.552 albertel 6281: return 'http://'.$hostname{ &homeserver($uname,$udom)}.'/'.$uri.
1.517 albertel 6282: (($uri=~/\?/)?'&':'?').'token='.$token.
6283: '&tokenissued='.$perlvar{'lonHostID'};
6284: } else {
6285: return '/adm/notfound.html';
6286: }
6287: }
6288:
1.481 raeburn 6289: sub getuploaded {
6290: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
6291: $uri=~s/^\///;
6292: $uri = 'http://'.$hostname{ &homeserver($cnum,$cdom)}.'/raw/'.$uri;
6293: my $ua=new LWP::UserAgent;
6294: my $request=new HTTP::Request($reqtype,$uri);
6295: my $response=$ua->request($request);
6296: $$rtncode = $response->code;
1.482 albertel 6297: if (! $response->is_success()) {
6298: return 'failed';
6299: }
6300: if ($reqtype eq 'HEAD') {
1.486 www 6301: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 6302: } elsif ($reqtype eq 'GET') {
6303: $$info = $response->content;
1.472 albertel 6304: }
1.482 albertel 6305: return 'ok';
1.36 albertel 6306: }
6307:
1.481 raeburn 6308: sub readfile {
6309: my $file = shift;
6310: if ( (! -e $file ) || ($file eq '') ) { return -1; };
6311: my $fh;
6312: open($fh,"<$file");
6313: my $a='';
6314: while (<$fh>) { $a .=$_; }
6315: return $a;
6316: }
6317:
1.36 albertel 6318: sub filelocation {
1.590 banghart 6319: my ($dir,$file) = @_;
6320: my $location;
6321: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 6322:
6323: if ($file =~ m-^/adm/-) {
6324: $file=~s-^/adm/wrapper/-/-;
6325: $file=~s-^/adm/coursedocs/showdoc/-/-;
6326: }
1.590 banghart 6327: if ($file=~m:^/~:) { # is a contruction space reference
6328: $location = $file;
6329: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.649 albertel 6330: } elsif ($file=~m:^/home/[^/]*/public_html/:) {
6331: # is a correct contruction space reference
6332: $location = $file;
1.609 banghart 6333: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 6334: my ($udom,$uname,$filename)=
1.609 banghart 6335: ($file=~m -^/+(?:uploaded|editupload)/+([^/]+)/+([^/]+)/+(.*)$-);
1.590 banghart 6336: my $home=&homeserver($uname,$udom);
6337: my $is_me=0;
6338: my @ids=¤t_machine_ids();
6339: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
6340: if ($is_me) {
6341: $location=&Apache::loncommon::propath($udom,$uname).
6342: '/userfiles/'.$filename;
6343: } else {
6344: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
6345: $udom.'/'.$uname.'/'.$filename;
6346: }
6347: } else {
6348: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
6349: $file=~s:^/res/:/:;
6350: if ( !( $file =~ m:^/:) ) {
6351: $location = $dir. '/'.$file;
6352: } else {
6353: $location = '/home/httpd/html/res'.$file;
6354: }
1.59 albertel 6355: }
1.590 banghart 6356: $location=~s://+:/:g; # remove duplicate /
6357: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
6358: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
6359: return $location;
1.46 www 6360: }
1.36 albertel 6361:
1.46 www 6362: sub hreflocation {
6363: my ($dir,$file)=@_;
1.460 albertel 6364: unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666 albertel 6365: $file=filelocation($dir,$file);
1.700 albertel 6366: } elsif ($file=~m-^/adm/-) {
6367: $file=~s-^/adm/wrapper/-/-;
6368: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 6369: }
6370: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
6371: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
6372: } elsif ($file=~m-/home/(\w+)/public_html/-) {
1.462 albertel 6373: $file=~s-^/home/(\w+)/public_html/-/~$1/-;
1.666 albertel 6374: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
6375: $file=~s-^/home/httpd/lonUsers/([^/]*)/./././([^/]*)/userfiles/
6376: -/uploaded/$1/$2/-x;
1.46 www 6377: }
1.462 albertel 6378: return $file;
1.465 albertel 6379: }
6380:
6381: sub current_machine_domains {
6382: my $hostname=$hostname{$perlvar{'lonHostID'}};
6383: my @domains;
6384: while( my($id, $name) = each(%hostname)) {
1.467 matthew 6385: # &logthis("-$id-$name-$hostname-");
1.465 albertel 6386: if ($hostname eq $name) {
6387: push(@domains,$hostdom{$id});
6388: }
6389: }
6390: return @domains;
6391: }
6392:
6393: sub current_machine_ids {
6394: my $hostname=$hostname{$perlvar{'lonHostID'}};
6395: my @ids;
6396: while( my($id, $name) = each(%hostname)) {
1.467 matthew 6397: # &logthis("-$id-$name-$hostname-");
1.465 albertel 6398: if ($hostname eq $name) {
6399: push(@ids,$id);
6400: }
6401: }
6402: return @ids;
1.31 www 6403: }
6404:
6405: # ------------------------------------------------------------- Declutters URLs
6406:
6407: sub declutter {
6408: my $thisfn=shift;
1.569 albertel 6409: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 6410: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 6411: $thisfn=~s/^\///;
1.697 albertel 6412: $thisfn=~s|^adm/wrapper/||;
6413: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 6414: $thisfn=~s/^res\///;
1.235 www 6415: $thisfn=~s/\?.+$//;
1.268 www 6416: return $thisfn;
6417: }
6418:
6419: # ------------------------------------------------------------- Clutter up URLs
6420:
6421: sub clutter {
6422: my $thisfn='/'.&declutter(shift);
1.609 banghart 6423: unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) {
1.270 www 6424: $thisfn='/res'.$thisfn;
6425: }
1.694 albertel 6426: if ($thisfn !~m|/adm|) {
1.695 albertel 6427: if ($thisfn =~ m|/ext/|) {
1.694 albertel 6428: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 6429: } else {
6430: my ($ext) = ($thisfn =~ /\.(\w+)$/);
6431: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 6432: if ($embstyle eq 'ssi'
6433: || ($embstyle eq 'hdn')
6434: || ($embstyle eq 'rat')
6435: || ($embstyle eq 'prv')
6436: || ($embstyle eq 'ign')) {
6437: #do nothing with these
6438: } elsif (($embstyle eq 'img')
1.695 albertel 6439: || ($embstyle eq 'emb')
6440: || ($embstyle eq 'wrp')) {
6441: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 6442: } elsif ($embstyle eq 'unk'
6443: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 6444: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 6445: } else {
1.718 www 6446: # &logthis("Got a blank emb style");
1.695 albertel 6447: }
1.694 albertel 6448: }
6449: }
1.31 www 6450: return $thisfn;
1.12 www 6451: }
6452:
1.557 albertel 6453: sub freeze_escape {
6454: my ($value)=@_;
6455: if (ref($value)) {
6456: $value=&nfreeze($value);
6457: return '__FROZEN__'.&escape($value);
6458: }
6459: return &escape($value);
6460: }
6461:
1.12 www 6462: # -------------------------------------------------------- Escape Special Chars
6463:
6464: sub escape {
6465: my $str=shift;
6466: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
6467: return $str;
6468: }
6469:
6470: # ----------------------------------------------------- Un-Escape Special Chars
6471:
6472: sub unescape {
6473: my $str=shift;
6474: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
6475: return $str;
6476: }
1.11 www 6477:
1.557 albertel 6478: sub thaw_unescape {
6479: my ($value)=@_;
6480: if ($value =~ /^__FROZEN__/) {
6481: substr($value,0,10,undef);
6482: $value=&unescape($value);
6483: return &thaw($value);
6484: }
6485: return &unescape($value);
6486: }
6487:
1.436 albertel 6488: sub correct_line_ends {
6489: my ($result)=@_;
6490: $$result =~s/\r\n/\n/mg;
6491: $$result =~s/\r/\n/mg;
1.415 albertel 6492: }
1.1 albertel 6493: # ================================================================ Main Program
6494:
1.184 www 6495: sub goodbye {
1.204 albertel 6496: &logthis("Starting Shut down");
1.443 albertel 6497: #not converted to using infrastruture and probably shouldn't be
1.599 albertel 6498: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
1.443 albertel 6499: #converted
1.599 albertel 6500: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
6501: &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
6502: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
6503: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
1.425 albertel 6504: #1.1 only
1.599 albertel 6505: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
6506: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
6507: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
6508: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
6509: &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
6510: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
6511: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 6512: &flushcourselogs();
6513: &logthis("Shutting down");
1.362 albertel 6514: return DONE;
1.184 www 6515: }
6516:
1.179 www 6517: BEGIN {
1.228 harris41 6518: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
1.195 www 6519: unless ($readit) {
1.217 harris41 6520: {
1.581 matthew 6521: # FIXME: Use LONCAPA::Configuration::read_conf here and omit next block
1.448 albertel 6522: open(my $config,"</etc/httpd/conf/loncapa.conf");
1.217 harris41 6523:
6524: while (my $configline=<$config>) {
1.484 albertel 6525: if ($configline=~/\S/ && $configline =~ /^[^\#]*PerlSetVar/) {
1.1 albertel 6526: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8 www 6527: chomp($varvalue);
1.1 albertel 6528: $perlvar{$varname}=$varvalue;
6529: }
6530: }
1.448 albertel 6531: close($config);
1.1 albertel 6532: }
1.227 harris41 6533: {
1.448 albertel 6534: open(my $config,"</etc/httpd/conf/loncapa_apache.conf");
1.227 harris41 6535:
6536: while (my $configline=<$config>) {
6537: if ($configline =~ /^[^\#]*PerlSetVar/) {
6538: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
6539: chomp($varvalue);
6540: $perlvar{$varname}=$varvalue;
6541: }
6542: }
1.448 albertel 6543: close($config);
1.227 harris41 6544: }
1.1 albertel 6545:
1.327 albertel 6546: # ------------------------------------------------------------ Read domain file
6547: {
6548: %domaindescription = ();
6549: %domain_auth_def = ();
6550: %domain_auth_arg_def = ();
1.448 albertel 6551: my $fh;
6552: if (open($fh,"<".$Apache::lonnet::perlvar{'lonTabDir'}.'/domain.tab')) {
1.327 albertel 6553: while (<$fh>) {
1.390 matthew 6554: next if (/^(\#|\s*$)/);
6555: # next if /^\#/;
1.327 albertel 6556: chomp;
1.403 www 6557: my ($domain, $domain_description, $def_auth, $def_auth_arg,
1.685 raeburn 6558: $def_lang, $city, $longi, $lati, $primary) = split(/:/,$_);
1.403 www 6559: $domain_auth_def{$domain}=$def_auth;
1.327 albertel 6560: $domain_auth_arg_def{$domain}=$def_auth_arg;
1.403 www 6561: $domaindescription{$domain}=$domain_description;
6562: $domain_lang_def{$domain}=$def_lang;
6563: $domain_city{$domain}=$city;
6564: $domain_longi{$domain}=$longi;
6565: $domain_lati{$domain}=$lati;
1.685 raeburn 6566: $domain_primary{$domain}=$primary;
1.403 www 6567:
1.448 albertel 6568: # &logthis("Domain.tab: $domain, $domain_auth_def{$domain}, $domain_auth_arg_def{$domain},$domaindescription{$domain}");
1.327 albertel 6569: # &logthis("Domain.tab: $domain ".$domaindescription{$domain} );
1.448 albertel 6570: }
1.327 albertel 6571: }
1.448 albertel 6572: close ($fh);
1.327 albertel 6573: }
6574:
6575:
1.1 albertel 6576: # ------------------------------------------------------------- Read hosts file
6577: {
1.448 albertel 6578: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
1.1 albertel 6579:
6580: while (my $configline=<$config>) {
1.303 matthew 6581: next if ($configline =~ /^(\#|\s*$)/);
1.154 www 6582: chomp($configline);
1.595 albertel 6583: my ($id,$domain,$role,$name)=split(/:/,$configline);
1.597 albertel 6584: $name=~s/\s//g;
1.595 albertel 6585: if ($id && $domain && $role && $name) {
1.252 albertel 6586: $hostname{$id}=$name;
6587: $hostdom{$id}=$domain;
6588: if ($role eq 'library') { $libserv{$id}=$name; }
1.245 www 6589: }
1.1 albertel 6590: }
1.448 albertel 6591: close($config);
1.619 albertel 6592: # FIXME: dev server don't want this, production servers _do_ want this
1.654 albertel 6593: #&get_iphost();
1.1 albertel 6594: }
6595:
1.598 albertel 6596: sub get_iphost {
6597: if (%iphost) { return %iphost; }
1.653 albertel 6598: my %name_to_ip;
1.598 albertel 6599: foreach my $id (keys(%hostname)) {
6600: my $name=$hostname{$id};
1.653 albertel 6601: my $ip;
6602: if (!exists($name_to_ip{$name})) {
6603: $ip = gethostbyname($name);
6604: if (!$ip || length($ip) ne 4) {
6605: &logthis("Skipping host $id name $name no IP found\n");
6606: next;
6607: }
6608: $ip=inet_ntoa($ip);
6609: $name_to_ip{$name} = $ip;
6610: } else {
6611: $ip = $name_to_ip{$name};
1.598 albertel 6612: }
6613: push(@{$iphost{$ip}},$id);
6614: }
6615: return %iphost;
6616: }
6617:
1.1 albertel 6618: # ------------------------------------------------------ Read spare server file
6619: {
1.448 albertel 6620: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 6621:
6622: while (my $configline=<$config>) {
6623: chomp($configline);
1.284 matthew 6624: if ($configline) {
1.1 albertel 6625: $spareid{$configline}=1;
6626: }
6627: }
1.448 albertel 6628: close($config);
1.1 albertel 6629: }
1.11 www 6630: # ------------------------------------------------------------ Read permissions
6631: {
1.448 albertel 6632: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 6633:
6634: while (my $configline=<$config>) {
1.448 albertel 6635: chomp($configline);
6636: if ($configline) {
6637: my ($role,$perm)=split(/ /,$configline);
6638: if ($perm ne '') { $pr{$role}=$perm; }
6639: }
1.11 www 6640: }
1.448 albertel 6641: close($config);
1.11 www 6642: }
6643:
6644: # -------------------------------------------- Read plain texts for permissions
6645: {
1.448 albertel 6646: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 6647:
6648: while (my $configline=<$config>) {
1.448 albertel 6649: chomp($configline);
6650: if ($configline) {
6651: my ($short,$plain)=split(/:/,$configline);
6652: if ($plain ne '') { $prp{$short}=$plain; }
6653: }
1.135 www 6654: }
1.448 albertel 6655: close($config);
1.135 www 6656: }
6657:
6658: # ---------------------------------------------------------- Read package table
6659: {
1.448 albertel 6660: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 6661:
6662: while (my $configline=<$config>) {
1.483 albertel 6663: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 6664: chomp($configline);
6665: my ($short,$plain)=split(/:/,$configline);
6666: my ($pack,$name)=split(/\&/,$short);
6667: if ($plain ne '') {
6668: $packagetab{$pack.'&'.$name.'&name'}=$name;
6669: $packagetab{$short}=$plain;
6670: }
1.11 www 6671: }
1.448 albertel 6672: close($config);
1.329 matthew 6673: }
6674:
6675: # ------------- set up temporary directory
6676: {
6677: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
6678:
1.11 www 6679: }
6680:
1.599 albertel 6681: $memcache=new Cache::Memcached({'servers'=>['127.0.0.1:11211']});
1.185 www 6682:
1.281 www 6683: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 6684: $dumpcount=0;
1.22 www 6685:
1.163 harris41 6686: &logtouch();
1.672 albertel 6687: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 6688: $readit=1;
1.564 albertel 6689: {
6690: use integer;
6691: my $test=(2**32)+1;
1.568 albertel 6692: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 6693: &logthis(" Detected 64bit platform ($_64bit)");
6694: }
1.195 www 6695: }
1.1 albertel 6696: }
1.179 www 6697:
1.1 albertel 6698: 1;
1.191 harris41 6699: __END__
6700:
1.243 albertel 6701: =pod
6702:
1.191 harris41 6703: =head1 NAME
6704:
1.243 albertel 6705: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 6706:
6707: =head1 SYNOPSIS
6708:
1.243 albertel 6709: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 6710:
6711: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
6712:
1.243 albertel 6713: Common parameters:
6714:
6715: =over 4
6716:
6717: =item *
6718:
6719: $uname : an internal username (if $cname expecting a course Id specifically)
6720:
6721: =item *
6722:
6723: $udom : a domain (if $cdom expecting a course's domain specifically)
6724:
6725: =item *
6726:
6727: $symb : a resource instance identifier
6728:
6729: =item *
6730:
6731: $namespace : the name of a .db file that contains the data needed or
6732: being set.
6733:
6734: =back
6735:
1.394 bowersj2 6736: =head1 OVERVIEW
1.191 harris41 6737:
1.394 bowersj2 6738: lonnet provides subroutines which interact with the
6739: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
6740: about classes, users, and resources.
1.243 albertel 6741:
6742: For many of these objects you can also use this to store data about
6743: them or modify them in various ways.
1.191 harris41 6744:
1.394 bowersj2 6745: =head2 Symbs
1.191 harris41 6746:
1.394 bowersj2 6747: To identify a specific instance of a resource, LON-CAPA uses symbols
6748: or "symbs"X<symb>. These identifiers are built from the URL of the
6749: map, the resource number of the resource in the map, and the URL of
6750: the resource itself. The latter is somewhat redundant, but might help
6751: if maps change.
6752:
6753: An example is
6754:
6755: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
6756:
6757: The respective map entry is
6758:
6759: <resource id="19" src="/res/msu/korte/tests/part12.problem"
6760: title="Problem 2">
6761: </resource>
6762:
6763: Symbs are used by the random number generator, as well as to store and
6764: restore data specific to a certain instance of for example a problem.
6765:
6766: =head2 Storing And Retrieving Data
6767:
6768: X<store()>X<cstore()>X<restore()>Three of the most important functions
6769: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
6770: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
6771: is is the non-critical message twin of cstore. These functions are for
6772: handlers to store a perl hash to a user's permanent data space in an
6773: easy manner, and to retrieve it again on another call. It is expected
6774: that a handler would use this once at the beginning to retrieve data,
6775: and then again once at the end to send only the new data back.
6776:
6777: The data is stored in the user's data directory on the user's
6778: homeserver under the ID of the course.
6779:
6780: The hash that is returned by restore will have all of the previous
6781: value for all of the elements of the hash.
6782:
6783: Example:
6784:
6785: #creating a hash
6786: my %hash;
6787: $hash{'foo'}='bar';
6788:
6789: #storing it
6790: &Apache::lonnet::cstore(\%hash);
6791:
6792: #changing a value
6793: $hash{'foo'}='notbar';
6794:
6795: #adding a new value
6796: $hash{'bar'}='foo';
6797: &Apache::lonnet::cstore(\%hash);
6798:
6799: #retrieving the hash
6800: my %history=&Apache::lonnet::restore();
6801:
6802: #print the hash
6803: foreach my $key (sort(keys(%history))) {
6804: print("\%history{$key} = $history{$key}");
6805: }
6806:
6807: Will print out:
1.191 harris41 6808:
1.394 bowersj2 6809: %history{1:foo} = bar
6810: %history{1:keys} = foo:timestamp
6811: %history{1:timestamp} = 990455579
6812: %history{2:bar} = foo
6813: %history{2:foo} = notbar
6814: %history{2:keys} = foo:bar:timestamp
6815: %history{2:timestamp} = 990455580
6816: %history{bar} = foo
6817: %history{foo} = notbar
6818: %history{timestamp} = 990455580
6819: %history{version} = 2
6820:
6821: Note that the special hash entries C<keys>, C<version> and
6822: C<timestamp> were added to the hash. C<version> will be equal to the
6823: total number of versions of the data that have been stored. The
6824: C<timestamp> attribute will be the UNIX time the hash was
6825: stored. C<keys> is available in every historical section to list which
6826: keys were added or changed at a specific historical revision of a
6827: hash.
6828:
6829: B<Warning>: do not store the hash that restore returns directly. This
6830: will cause a mess since it will restore the historical keys as if the
6831: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 6832:
1.394 bowersj2 6833: Calling convention:
1.191 harris41 6834:
1.394 bowersj2 6835: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
6836: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 6837:
1.394 bowersj2 6838: For more detailed information, see lonnet specific documentation.
1.191 harris41 6839:
1.394 bowersj2 6840: =head1 RETURN MESSAGES
1.191 harris41 6841:
1.394 bowersj2 6842: =over 4
1.191 harris41 6843:
1.394 bowersj2 6844: =item * B<con_lost>: unable to contact remote host
1.191 harris41 6845:
1.394 bowersj2 6846: =item * B<con_delayed>: unable to contact remote host, message will be delivered
6847: when the connection is brought back up
1.191 harris41 6848:
1.394 bowersj2 6849: =item * B<con_failed>: unable to contact remote host and unable to save message
6850: for later delivery
1.191 harris41 6851:
1.394 bowersj2 6852: =item * B<error:>: an error a occured, a description of the error follows the :
1.191 harris41 6853:
1.394 bowersj2 6854: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 6855: that was requested
1.191 harris41 6856:
1.243 albertel 6857: =back
1.191 harris41 6858:
1.243 albertel 6859: =head1 PUBLIC SUBROUTINES
1.191 harris41 6860:
1.243 albertel 6861: =head2 Session Environment Functions
1.191 harris41 6862:
1.243 albertel 6863: =over 4
1.191 harris41 6864:
1.394 bowersj2 6865: =item *
6866: X<appenv()>
6867: B<appenv(%hash)>: the value of %hash is written to
6868: the user envirnoment file, and will be restored for each access this
1.620 albertel 6869: user makes during this session, also modifies the %env for the current
1.394 bowersj2 6870: process
1.191 harris41 6871:
6872: =item *
1.394 bowersj2 6873: X<delenv()>
6874: B<delenv($regexp)>: removes all items from the session
6875: environment file that matches the regular expression in $regexp. The
1.620 albertel 6876: values are also delted from the current processes %env.
1.191 harris41 6877:
1.243 albertel 6878: =back
6879:
6880: =head2 User Information
1.191 harris41 6881:
1.243 albertel 6882: =over 4
1.191 harris41 6883:
6884: =item *
1.394 bowersj2 6885: X<queryauthenticate()>
6886: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 6887: authentication scheme
6888:
6889: =item *
1.394 bowersj2 6890: X<authenticate()>
6891: B<authenticate($uname,$upass,$udom)>: try to
6892: authenticate user from domain's lib servers (first use the current
6893: one). C<$upass> should be the users password.
1.191 harris41 6894:
6895: =item *
1.394 bowersj2 6896: X<homeserver()>
6897: B<homeserver($uname,$udom)>: find the server which has
6898: the user's directory and files (there must be only one), this caches
6899: the answer, and also caches if there is a borken connection.
1.191 harris41 6900:
6901: =item *
1.394 bowersj2 6902: X<idget()>
6903: B<idget($udom,@ids)>: find the usernames behind a list of IDs
6904: (IDs are a unique resource in a domain, there must be only 1 ID per
6905: username, and only 1 username per ID in a specific domain) (returns
6906: hash: id=>name,id=>name)
1.191 harris41 6907:
6908: =item *
1.394 bowersj2 6909: X<idrget()>
6910: B<idrget($udom,@unames)>: find the IDs behind a list of
6911: usernames (returns hash: name=>id,name=>id)
1.191 harris41 6912:
6913: =item *
1.394 bowersj2 6914: X<idput()>
6915: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 6916:
6917: =item *
1.394 bowersj2 6918: X<rolesinit()>
6919: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 6920:
6921: =item *
1.551 albertel 6922: X<getsection()>
6923: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 6924: course $cname, return section name/number or '' for "not in course"
6925: and '-1' for "no section"
6926:
6927: =item *
1.394 bowersj2 6928: X<userenvironment()>
6929: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 6930: passed in @what from the requested user's environment, returns a hash
6931:
6932: =back
6933:
6934: =head2 User Roles
6935:
6936: =over 4
6937:
6938: =item *
6939:
6940: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
6941: actions
6942: F: full access
6943: U,I,K: authentication modes (cxx only)
6944: '': forbidden
6945: 1: user needs to choose course
6946: 2: browse allowed
6947:
6948: =item *
6949:
6950: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
6951: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
6952: and course level
6953:
6954: =item *
6955:
6956: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
6957: explanation of a user role term
6958:
6959: =back
6960:
6961: =head2 User Modification
6962:
6963: =over 4
6964:
6965: =item *
6966:
6967: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
6968: user for the level given by URL. Optional start and end dates (leave empty
6969: string or zero for "no date")
1.191 harris41 6970:
6971: =item *
6972:
1.243 albertel 6973: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
6974: change a users, password, possible return values are: ok,
6975: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
6976: refused
1.191 harris41 6977:
6978: =item *
6979:
1.243 albertel 6980: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 6981:
6982: =item *
6983:
1.243 albertel 6984: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
6985: modify user
1.191 harris41 6986:
6987: =item *
6988:
1.286 matthew 6989: modifystudent
6990:
6991: modify a students enrollment and identification information.
6992: The course id is resolved based on the current users environment.
6993: This means the envoking user must be a course coordinator or otherwise
6994: associated with a course.
6995:
1.297 matthew 6996: This call is essentially a wrapper for lonnet::modifyuser and
6997: lonnet::modify_student_enrollment
1.286 matthew 6998:
6999: Inputs:
7000:
7001: =over 4
7002:
7003: =item B<$udom> Students loncapa domain
7004:
7005: =item B<$uname> Students loncapa login name
7006:
7007: =item B<$uid> Students id/student number
7008:
7009: =item B<$umode> Students authentication mode
7010:
7011: =item B<$upass> Students password
7012:
7013: =item B<$first> Students first name
7014:
7015: =item B<$middle> Students middle name
7016:
7017: =item B<$last> Students last name
7018:
7019: =item B<$gene> Students generation
7020:
7021: =item B<$usec> Students section in course
7022:
7023: =item B<$end> Unix time of the roles expiration
7024:
7025: =item B<$start> Unix time of the roles start date
7026:
7027: =item B<$forceid> If defined, allow $uid to be changed
7028:
7029: =item B<$desiredhome> server to use as home server for student
7030:
7031: =back
1.297 matthew 7032:
7033: =item *
7034:
7035: modify_student_enrollment
7036:
7037: Change a students enrollment status in a class. The environment variable
7038: 'role.request.course' must be defined for this function to proceed.
7039:
7040: Inputs:
7041:
7042: =over 4
7043:
7044: =item $udom, students domain
7045:
7046: =item $uname, students name
7047:
7048: =item $uid, students user id
7049:
7050: =item $first, students first name
7051:
7052: =item $middle
7053:
7054: =item $last
7055:
7056: =item $gene
7057:
7058: =item $usec
7059:
7060: =item $end
7061:
7062: =item $start
7063:
7064: =back
7065:
1.191 harris41 7066:
7067: =item *
7068:
1.243 albertel 7069: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
7070: custom role; give a custom role to a user for the level given by URL. Specify
7071: name and domain of role author, and role name
1.191 harris41 7072:
7073: =item *
7074:
1.243 albertel 7075: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 7076:
7077: =item *
7078:
1.243 albertel 7079: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
7080:
7081: =back
7082:
7083: =head2 Course Infomation
7084:
7085: =over 4
1.191 harris41 7086:
7087: =item *
7088:
1.631 albertel 7089: coursedescription($courseid) : returns a hash of information about the
7090: specified course id, including all environment settings for the
7091: course, the description of the course will be in the hash under the
7092: key 'description'
1.191 harris41 7093:
7094: =item *
7095:
1.624 albertel 7096: resdata($name,$domain,$type,@which) : request for current parameter
7097: setting for a specific $type, where $type is either 'course' or 'user',
7098: @what should be a list of parameters to ask about. This routine caches
7099: answers for 5 minutes.
1.243 albertel 7100:
7101: =back
7102:
7103: =head2 Course Modification
7104:
7105: =over 4
1.191 harris41 7106:
7107: =item *
7108:
1.243 albertel 7109: writecoursepref($courseid,%prefs) : write preferences (environment
7110: database) for a course
1.191 harris41 7111:
7112: =item *
7113:
1.243 albertel 7114: createcourse($udom,$description,$url) : make/modify course
7115:
7116: =back
7117:
7118: =head2 Resource Subroutines
7119:
7120: =over 4
1.191 harris41 7121:
7122: =item *
7123:
1.243 albertel 7124: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 7125:
7126: =item *
7127:
1.243 albertel 7128: repcopy($filename) : subscribes to the requested file, and attempts to
7129: replicate from the owning library server, Might return
1.607 raeburn 7130: 'unavailable', 'not_found', 'forbidden', 'ok', or
7131: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 7132: resource. Expects the local filesystem pathname
7133: (/home/httpd/html/res/....)
7134:
7135: =back
7136:
7137: =head2 Resource Information
7138:
7139: =over 4
1.191 harris41 7140:
7141: =item *
7142:
1.243 albertel 7143: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
7144: a vairety of different possible values, $varname should be a request
7145: string, and the other parameters can be used to specify who and what
7146: one is asking about.
7147:
7148: Possible values for $varname are environment.lastname (or other item
7149: from the envirnment hash), user.name (or someother aspect about the
7150: user), resource.0.maxtries (or some other part and parameter of a
7151: resource)
1.204 albertel 7152:
7153: =item *
7154:
1.243 albertel 7155: directcondval($number) : get current value of a condition; reads from a state
7156: string
1.204 albertel 7157:
7158: =item *
7159:
1.243 albertel 7160: condval($condidx) : value of condition index based on state
1.204 albertel 7161:
7162: =item *
7163:
1.243 albertel 7164: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
7165: resource's metadata, $what should be either a specific key, or either
7166: 'keys' (to get a list of possible keys) or 'packages' to get a list of
7167: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
7168:
7169: this function automatically caches all requests
1.191 harris41 7170:
7171: =item *
7172:
1.243 albertel 7173: metadata_query($query,$custom,$customshow) : make a metadata query against the
7174: network of library servers; returns file handle of where SQL and regex results
7175: will be stored for query
1.191 harris41 7176:
7177: =item *
7178:
1.243 albertel 7179: symbread($filename) : return symbolic list entry (filename argument optional);
7180: returns the data handle
1.191 harris41 7181:
7182: =item *
7183:
1.243 albertel 7184: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 7185: a possible symb for the URL in $thisfn, and if is an encryypted
7186: resource that the user accessed using /enc/ returns a 1 on success, 0
7187: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 7188: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 7189:
1.191 harris41 7190:
7191: =item *
7192:
1.243 albertel 7193: symbclean($symb) : removes versions numbers from a symb, returns the
7194: cleaned symb
1.191 harris41 7195:
7196: =item *
7197:
1.243 albertel 7198: is_on_map($uri) : checks if the $uri is somewhere on the current
7199: course map, user must be in a course for it to work.
1.191 harris41 7200:
7201: =item *
7202:
1.243 albertel 7203: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 7204:
7205: =item *
7206:
1.243 albertel 7207: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
7208: a random seed, all arguments are optional, if they aren't sent it uses the
7209: environment to derive them. Note: if symb isn't sent and it can't get one
7210: from &symbread it will use the current time as its return value
1.191 harris41 7211:
7212: =item *
7213:
1.243 albertel 7214: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
7215: unfakeable, receipt
1.191 harris41 7216:
7217: =item *
7218:
1.620 albertel 7219: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 7220:
7221: =item *
7222:
1.243 albertel 7223: countacc($url) : count the number of accesses to a given URL
1.191 harris41 7224:
7225: =item *
7226:
1.243 albertel 7227: 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 7228:
7229: =item *
7230:
1.243 albertel 7231: 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 7232:
7233: =item *
7234:
1.243 albertel 7235: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 7236:
7237: =item *
7238:
1.243 albertel 7239: devalidate($symb) : devalidate temporary spreadsheet calculations,
7240: forcing spreadsheet to reevaluate the resource scores next time.
7241:
7242: =back
7243:
7244: =head2 Storing/Retreiving Data
7245:
7246: =over 4
1.191 harris41 7247:
7248: =item *
7249:
1.243 albertel 7250: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
7251: for this url; hashref needs to be given and should be a \%hashname; the
7252: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 7253: be derived from the env
1.191 harris41 7254:
7255: =item *
7256:
1.243 albertel 7257: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
7258: uses critical subroutine
1.191 harris41 7259:
7260: =item *
7261:
1.243 albertel 7262: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
7263: all args are optional
1.191 harris41 7264:
7265: =item *
7266:
1.717 albertel 7267: dumpstore($namespace,$udom,$uname,$regexp,$range) :
7268: dumps the complete (or key matching regexp) namespace into a hash
7269: ($udom, $uname, $regexp, $range are optional) for a namespace that is
7270: normally &store()ed into
7271:
7272: $range should be either an integer '100' (give me the first 100
7273: matching records)
7274: or be two integers sperated by a - with no spaces
7275: '30-50' (give me the 30th through the 50th matching
7276: records)
7277:
7278:
7279: =item *
7280:
7281: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
7282: replaces a &store() version of data with a replacement set of data
7283: for a particular resource in a namespace passed in the $storehash hash
7284: reference
7285:
7286: =item *
7287:
1.243 albertel 7288: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
7289: works very similar to store/cstore, but all data is stored in a
7290: temporary location and can be reset using tmpreset, $storehash should
7291: be a hash reference, returns nothing on success
1.191 harris41 7292:
7293: =item *
7294:
1.243 albertel 7295: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
7296: similar to restore, but all data is stored in a temporary location and
7297: can be reset using tmpreset. Returns a hash of values on success,
7298: error string otherwise.
1.191 harris41 7299:
7300: =item *
7301:
1.243 albertel 7302: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
7303: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 7304:
7305: =item *
7306:
1.243 albertel 7307: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
7308: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 7309:
7310: =item *
7311:
1.243 albertel 7312: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
7313: namesp ($udom and $uname are optional)
1.191 harris41 7314:
7315: =item *
7316:
1.702 albertel 7317: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 7318: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 7319: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 7320:
1.702 albertel 7321: $range should be either an integer '100' (give me the first 100
7322: matching records)
7323: or be two integers sperated by a - with no spaces
7324: '30-50' (give me the 30th through the 50th matching
7325: records)
1.449 matthew 7326: =item *
7327:
7328: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
7329: $store can be a scalar, an array reference, or if the amount to be
7330: incremented is > 1, a hash reference.
7331:
7332: ($udom and $uname are optional)
1.191 harris41 7333:
7334: =item *
7335:
1.243 albertel 7336: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
7337: ($udom and $uname are optional)
1.191 harris41 7338:
7339: =item *
7340:
1.243 albertel 7341: cput($namespace,$storehash,$udom,$uname) : critical put
7342: ($udom and $uname are optional)
1.191 harris41 7343:
7344: =item *
7345:
1.243 albertel 7346: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
7347: reference filled in from namesp (encrypts the return communication)
7348: ($udom and $uname are optional)
1.191 harris41 7349:
7350: =item *
7351:
1.243 albertel 7352: log($udom,$name,$home,$message) : write to permanent log for user; use
7353: critical subroutine
7354:
7355: =back
7356:
7357: =head2 Network Status Functions
7358:
7359: =over 4
1.191 harris41 7360:
7361: =item *
7362:
7363: dirlist($uri) : return directory list based on URI
7364:
7365: =item *
7366:
1.243 albertel 7367: spareserver() : find server with least workload from spare.tab
7368:
7369: =back
7370:
7371: =head2 Apache Request
7372:
7373: =over 4
1.191 harris41 7374:
7375: =item *
7376:
1.243 albertel 7377: ssi($url,%hash) : server side include, does a complete request cycle on url to
7378: localhost, posts hash
7379:
7380: =back
7381:
7382: =head2 Data to String to Data
7383:
7384: =over 4
1.191 harris41 7385:
7386: =item *
7387:
1.243 albertel 7388: hash2str(%hash) : convert a hash into a string complete with escaping and '='
7389: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 7390:
7391: =item *
7392:
1.243 albertel 7393: hashref2str($hashref) : convert a hashref into a string complete with
7394: escaping and '=' and '&' separators, supports elements that are
7395: arrayrefs and hashrefs
1.191 harris41 7396:
7397: =item *
7398:
1.243 albertel 7399: arrayref2str($arrayref) : convert an arrayref into a string complete
7400: with escaping and '&' separators, supports elements that are arrayrefs
7401: and hashrefs
1.191 harris41 7402:
7403: =item *
7404:
1.243 albertel 7405: str2hash($string) : convert string to hash using unescaping and
7406: splitting on '=' and '&', supports elements that are arrayrefs and
7407: hashrefs
1.191 harris41 7408:
7409: =item *
7410:
1.243 albertel 7411: str2array($string) : convert string to hash using unescaping and
7412: splitting on '&', supports elements that are arrayrefs and hashrefs
7413:
7414: =back
7415:
7416: =head2 Logging Routines
7417:
7418: =over 4
7419:
7420: These routines allow one to make log messages in the lonnet.log and
7421: lonnet.perm logfiles.
1.191 harris41 7422:
7423: =item *
7424:
1.243 albertel 7425: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 7426:
7427: =item *
7428:
1.243 albertel 7429: logthis() : append message to the normal lonnet.log file, it gets
7430: preiodically rolled over and deleted.
1.191 harris41 7431:
7432: =item *
7433:
1.243 albertel 7434: logperm() : append a permanent message to lonnet.perm.log, this log
7435: file never gets deleted by any automated portion of the system, only
7436: messages of critical importance should go in here.
7437:
7438: =back
7439:
7440: =head2 General File Helper Routines
7441:
7442: =over 4
1.191 harris41 7443:
7444: =item *
7445:
1.481 raeburn 7446: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
7447: (a) files in /uploaded
7448: (i) If a local copy of the file exists -
7449: compares modification date of local copy with last-modified date for
7450: definitive version stored on home server for course. If local copy is
7451: stale, requests a new version from the home server and stores it.
7452: If the original has been removed from the home server, then local copy
7453: is unlinked.
7454: (ii) If local copy does not exist -
7455: requests the file from the home server and stores it.
7456:
7457: If $caller is 'uploadrep':
7458: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
7459: for request for files originally uploaded via DOCS.
7460: - returns 'ok' if fresh local copy now available, -1 otherwise.
7461:
7462: Otherwise:
7463: This indicates a call from the content generation phase of the request.
7464: - returns the entire contents of the file or -1.
7465:
7466: (b) files in /res
7467: - returns the entire contents of a file or -1;
7468: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 7469:
1.712 albertel 7470:
7471: =item *
7472:
7473: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
7474: reference
7475:
7476: returns either a stat() list of data about the file or an empty list
7477: if the file doesn't exist or couldn't find out about it (connection
7478: problems or user unknown)
7479:
1.191 harris41 7480: =item *
7481:
1.243 albertel 7482: filelocation($dir,$file) : returns file system location of a file
7483: based on URI; meant to be "fairly clean" absolute reference, $dir is a
7484: directory that relative $file lookups are to looked in ($dir of /a/dir
7485: and a file of ../bob will become /a/bob)
1.191 harris41 7486:
7487: =item *
7488:
7489: hreflocation($dir,$file) : returns file system location or a URL; same as
7490: filelocation except for hrefs
7491:
7492: =item *
7493:
7494: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
7495:
1.243 albertel 7496: =back
7497:
1.608 albertel 7498: =head2 Usererfile file routines (/uploaded*)
7499:
7500: =over 4
7501:
7502: =item *
7503:
7504: userfileupload(): main rotine for putting a file in a user or course's
7505: filespace, arguments are,
7506:
1.620 albertel 7507: formname - required - this is the name of the element in $env where the
1.608 albertel 7508: filename, and the contents of the file to create/modifed exist
1.620 albertel 7509: the filename is in $env{'form.'.$formname.'.filename'} and the
7510: contents of the file is located in $env{'form.'.$formname}
1.608 albertel 7511: coursedoc - if true, store the file in the course of the active role
7512: of the current user
7513: subdir - required - subdirectory to put the file in under ../userfiles/
7514: if undefined, it will be placed in "unknown"
7515:
7516: (This routine calls clean_filename() to remove any dangerous
7517: characters from the filename, and then calls finuserfileupload() to
7518: complete the transaction)
7519:
7520: returns either the url of the uploaded file (/uploaded/....) if successful
7521: and /adm/notfound.html if unsuccessful
7522:
7523: =item *
7524:
7525: clean_filename(): routine for cleaing a filename up for storage in
7526: userfile space, argument is:
7527:
7528: filename - proposed filename
7529:
7530: returns: the new clean filename
7531:
7532: =item *
7533:
7534: finishuserfileupload(): routine that creaes and sends the file to
7535: userspace, probably shouldn't be called directly
7536:
7537: docuname: username or courseid of destination for the file
7538: docudom: domain of user/course of destination for the file
7539: formname: same as for userfileupload()
7540: fname: filename (inculding subdirectories) for the file
7541:
7542: returns either the url of the uploaded file (/uploaded/....) if successful
7543: and /adm/notfound.html if unsuccessful
7544:
7545: =item *
7546:
7547: renameuserfile(): renames an existing userfile to a new name
7548:
7549: Args:
7550: docuname: username or courseid of destination for the file
7551: docudom: domain of user/course of destination for the file
7552: old: current file name (including any subdirs under userfiles)
7553: new: desired file name (including any subdirs under userfiles)
7554:
7555: =item *
7556:
7557: mkdiruserfile(): creates a directory is a userfiles dir
7558:
7559: Args:
7560: docuname: username or courseid of destination for the file
7561: docudom: domain of user/course of destination for the file
7562: dir: dir to create (including any subdirs under userfiles)
7563:
7564: =item *
7565:
7566: removeuserfile(): removes a file that exists in userfiles
7567:
7568: Args:
7569: docuname: username or courseid of destination for the file
7570: docudom: domain of user/course of destination for the file
7571: fname: filname to delete (including any subdirs under userfiles)
7572:
7573: =item *
7574:
7575: removeuploadedurl(): convience function for removeuserfile()
7576:
7577: Args:
7578: url: a full /uploaded/... url to delete
7579:
7580: =back
7581:
1.243 albertel 7582: =head2 HTTP Helper Routines
7583:
7584: =over 4
7585:
1.191 harris41 7586: =item *
7587:
7588: escape() : unpack non-word characters into CGI-compatible hex codes
7589:
7590: =item *
7591:
7592: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
7593:
1.243 albertel 7594: =back
7595:
7596: =head1 PRIVATE SUBROUTINES
7597:
7598: =head2 Underlying communication routines (Shouldn't call)
7599:
7600: =over 4
7601:
7602: =item *
7603:
7604: subreply() : tries to pass a message to lonc, returns con_lost if incapable
7605:
7606: =item *
7607:
7608: reply() : uses subreply to send a message to remote machine, logs all failures
7609:
7610: =item *
7611:
7612: critical() : passes a critical message to another server; if cannot
7613: get through then place message in connection buffer directory and
7614: returns con_delayed, if incapable of saving message, returns
7615: con_failed
7616:
7617: =item *
7618:
7619: reconlonc() : tries to reconnect lonc client processes.
7620:
7621: =back
7622:
7623: =head2 Resource Access Logging
7624:
7625: =over 4
7626:
7627: =item *
7628:
7629: flushcourselogs() : flush (save) buffer logs and access logs
7630:
7631: =item *
7632:
7633: courselog($what) : save message for course in hash
7634:
7635: =item *
7636:
7637: courseacclog($what) : save message for course using &courselog(). Perform
7638: special processing for specific resource types (problems, exams, quizzes, etc).
7639:
1.191 harris41 7640: =item *
7641:
7642: goodbye() : flush course logs and log shutting down; it is called in srm.conf
7643: as a PerlChildExitHandler
1.243 albertel 7644:
7645: =back
7646:
7647: =head2 Other
7648:
7649: =over 4
7650:
7651: =item *
7652:
7653: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 7654:
7655: =back
7656:
7657: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>