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