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