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