Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.301
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.301 ! www 4: # $Id: lonnet.pm,v 1.300 2002/11/12 22:23:37 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: # 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
29: # 7/1,7/2,7/9,7/10,7/12,7/14,7/15,7/19,
30: # 11/8,11/16,11/18,11/22,11/23,12/22,
31: # 01/06,01/13,02/24,02/28,02/29,
32: # 03/01,03/02,03/06,03/07,03/13,
33: # 04/05,05/29,05/31,06/01,
34: # 06/05,06/26 Gerd Kortemeyer
35: # 06/26 Ben Tyszka
36: # 06/30,07/15,07/17,07/18,07/20,07/21,07/22,07/25 Gerd Kortemeyer
37: # 08/14 Ben Tyszka
38: # 08/22,08/28,08/31,09/01,09/02,09/04,09/05,09/25,09/28,09/30 Gerd Kortemeyer
39: # 10/04 Gerd Kortemeyer
40: # 10/04 Guy Albertelli
41: # 10/06,10/09,10/10,10/11,10/14,10/20,10/23,10/25,10/26,10/27,10/28,10/29,
42: # 10/30,10/31,
43: # 11/2,11/14,11/15,11/16,11/20,11/21,11/22,11/25,11/27,
44: # 12/02,12/12,12/13,12/14,12/28,12/29 Gerd Kortemeyer
45: # 05/01/01 Guy Albertelli
46: # 05/01,06/01,09/01 Gerd Kortemeyer
47: # 09/01 Guy Albertelli
48: # 09/01,10/01,11/01 Gerd Kortemeyer
49: # YEAR=2001
50: # 02/27/01 Scott Harrison
51: # 3/2 Gerd Kortemeyer
52: # 3/15,3/19 Scott Harrison
53: # 3/19,3/20 Gerd Kortemeyer
54: # 3/22,3/27,4/2,4/16,4/17 Scott Harrison
55: # 5/26,5/28 Gerd Kortemeyer
56: # 5/30 H. K. Ng
57: # 6/1 Gerd Kortemeyer
58: # July Guy Albertelli
59: # 8/4,8/7,8/8,8/9,8/11,8/16,8/17,8/18,8/20,8/23,9/20,9/21,9/26,
60: # 10/2 Gerd Kortemeyer
61: # 10/5,10/10,11/13,11/15 Scott Harrison
1.179 www 62: # 11/17,11/20,11/22,11/29 Gerd Kortemeyer
1.182 matthew 63: # 12/5 Matthew Hall
1.184 www 64: # 12/5 Guy Albertelli
1.190 www 65: # 12/6,12/7,12/12 Gerd Kortemeyer
1.191 harris41 66: # 12/18 Scott Harrison
1.195 www 67: # 12/21,12/22,12/27,12/28 Gerd Kortemeyer
1.196 www 68: # YEAR=2002
1.200 www 69: # 1/4,2/4,2/7 Gerd Kortemeyer
1.171 www 70: #
1.169 harris41 71: ###
72:
1.1 albertel 73: package Apache::lonnet;
74:
75: use strict;
76: use Apache::File;
1.8 www 77: use LWP::UserAgent();
1.15 www 78: use HTTP::Headers;
1.11 www 79: use vars
1.300 albertel 80: qw(%perlvar %hostname %homecache %badServerCache %hostip %iphost %spareid %hostdom
1.301 ! www 81: %libserv %pr %prp %metacache %packagetab %titlecache
1.188 www 82: %courselogs %accesshash $processmarker $dumpcount
1.245 www 83: %coursedombuf %coursehombuf %courseresdatacache %domaindescription);
1.1 albertel 84: use IO::Socket;
1.31 www 85: use GDBM_File;
1.8 www 86: use Apache::Constants qw(:common :http);
1.208 albertel 87: use HTML::LCParser;
1.88 www 88: use Fcntl qw(:flock);
1.294 matthew 89: use Apache::loncoursedata;
90:
1.195 www 91: my $readit;
1.1 albertel 92:
93: # --------------------------------------------------------------------- Logging
94:
1.163 harris41 95: sub logtouch {
96: my $execdir=$perlvar{'lonDaemons'};
97: unless (-e "$execdir/logs/lonnet.log") {
98: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
99: close $fh;
100: }
101: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
102: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
103: }
104:
1.1 albertel 105: sub logthis {
106: my $message=shift;
107: my $execdir=$perlvar{'lonDaemons'};
108: my $now=time;
109: my $local=localtime($now);
1.162 harris41 110: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
111: print $fh "$local ($$): $message\n";
1.1 albertel 112: return 1;
113: }
114:
115: sub logperm {
116: my $message=shift;
117: my $execdir=$perlvar{'lonDaemons'};
118: my $now=time;
119: my $local=localtime($now);
1.162 harris41 120: my $fh=Apache::File->new(">>$execdir/logs/lonnet.perm.log");
121: print $fh "$now:$message:$local\n";
1.1 albertel 122: return 1;
123: }
124:
125: # -------------------------------------------------- Non-critical communication
126: sub subreply {
127: my ($cmd,$server)=@_;
128: my $peerfile="$perlvar{'lonSockDir'}/$server";
129: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
130: Type => SOCK_STREAM,
131: Timeout => 10)
132: or return "con_lost";
133: print $client "$cmd\n";
134: my $answer=<$client>;
1.9 www 135: if (!$answer) { $answer="con_lost"; }
1.1 albertel 136: chomp($answer);
137: return $answer;
138: }
139:
140: sub reply {
141: my ($cmd,$server)=@_;
1.205 www 142: unless (defined($hostname{$server})) { return 'no_such_host'; }
1.1 albertel 143: my $answer=subreply($cmd,$server);
1.203 www 144: if ($answer eq 'con_lost') {
1.233 albertel 145: #sleep 5;
146: #$answer=subreply($cmd,$server);
147: #if ($answer eq 'con_lost') {
148: # &logthis("Second attempt con_lost on $server");
149: # my $peerfile="$perlvar{'lonSockDir'}/$server";
150: # my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
151: # Type => SOCK_STREAM,
152: # Timeout => 10)
153: # or return "con_lost";
154: # &logthis("Killing socket");
155: # print $client "close_connection_exit\n";
156: #sleep 5;
157: # $answer=subreply($cmd,$server);
158: #}
1.203 www 159: }
1.65 www 160: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.12 www 161: &logthis("<font color=blue>WARNING:".
162: " $cmd to $server returned $answer</font>");
163: }
1.1 albertel 164: return $answer;
165: }
166:
167: # ----------------------------------------------------------- Send USR1 to lonc
168:
169: sub reconlonc {
170: my $peerfile=shift;
171: &logthis("Trying to reconnect for $peerfile");
172: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
173: if (my $fh=Apache::File->new("$loncfile")) {
174: my $loncpid=<$fh>;
175: chomp($loncpid);
176: if (kill 0 => $loncpid) {
177: &logthis("lonc at pid $loncpid responding, sending USR1");
178: kill USR1 => $loncpid;
179: sleep 1;
180: if (-e "$peerfile") { return; }
181: &logthis("$peerfile still not there, give it another try");
182: sleep 5;
183: if (-e "$peerfile") { return; }
1.12 www 184: &logthis(
185: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 186: } else {
1.12 www 187: &logthis(
188: "<font color=blue>WARNING:".
189: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 190: }
191: } else {
1.12 www 192: &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
1.1 albertel 193: }
194: }
195:
196: # ------------------------------------------------------ Critical communication
1.12 www 197:
1.1 albertel 198: sub critical {
199: my ($cmd,$server)=@_;
1.89 www 200: unless ($hostname{$server}) {
201: &logthis("<font color=blue>WARNING:".
202: " Critical message to unknown server ($server)</font>");
203: return 'no_such_host';
204: }
1.1 albertel 205: my $answer=reply($cmd,$server);
206: if ($answer eq 'con_lost') {
207: my $pingreply=reply('ping',$server);
208: &reconlonc("$perlvar{'lonSockDir'}/$server");
209: my $pongreply=reply('pong',$server);
210: &logthis("Ping/Pong for $server: $pingreply/$pongreply");
211: $answer=reply($cmd,$server);
212: if ($answer eq 'con_lost') {
213: my $now=time;
214: my $middlename=$cmd;
1.5 www 215: $middlename=substr($middlename,0,16);
1.1 albertel 216: $middlename=~s/\W//g;
217: my $dfilename=
218: "$perlvar{'lonSockDir'}/delayed/$now.$middlename.$server";
219: {
220: my $dfh;
221: if ($dfh=Apache::File->new(">$dfilename")) {
1.7 www 222: print $dfh "$cmd\n";
1.1 albertel 223: }
224: }
225: sleep 2;
226: my $wcmd='';
227: {
228: my $dfh;
229: if ($dfh=Apache::File->new("$dfilename")) {
230: $wcmd=<$dfh>;
231: }
232: }
233: chomp($wcmd);
1.7 www 234: if ($wcmd eq $cmd) {
1.12 www 235: &logthis("<font color=blue>WARNING: ".
236: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 237: &logperm("D:$server:$cmd");
238: return 'con_delayed';
239: } else {
1.12 www 240: &logthis("<font color=red>CRITICAL:"
241: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 242: &logperm("F:$server:$cmd");
243: return 'con_failed';
244: }
245: }
246: }
247: return $answer;
248: }
249:
1.5 www 250: # ---------------------------------------------------------- Append Environment
251:
252: sub appenv {
1.6 www 253: my %newenv=@_;
1.191 harris41 254: foreach (keys %newenv) {
1.35 www 255: if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
256: &logthis("<font color=blue>WARNING: ".
1.151 www 257: "Attempt to modify environment ".$_." to ".$newenv{$_}
258: .'</font>');
1.35 www 259: delete($newenv{$_});
260: } else {
261: $ENV{$_}=$newenv{$_};
262: }
1.191 harris41 263: }
1.95 www 264:
265: my $lockfh;
266: unless ($lockfh=Apache::File->new("$ENV{'user.environment'}")) {
1.97 www 267: return 'error: '.$!;
1.95 www 268: }
269: unless (flock($lockfh,LOCK_EX)) {
270: &logthis("<font color=blue>WARNING: ".
271: 'Could not obtain exclusive lock in appenv: '.$!);
272: $lockfh->close();
273: return 'error: '.$!;
274: }
275:
1.6 www 276: my @oldenv;
277: {
278: my $fh;
279: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
1.97 www 280: return 'error: '.$!;
1.6 www 281: }
282: @oldenv=<$fh>;
1.89 www 283: $fh->close();
1.6 www 284: }
285: for (my $i=0; $i<=$#oldenv; $i++) {
286: chomp($oldenv[$i]);
1.9 www 287: if ($oldenv[$i] ne '') {
288: my ($name,$value)=split(/=/,$oldenv[$i]);
1.24 www 289: unless (defined($newenv{$name})) {
290: $newenv{$name}=$value;
291: }
1.9 www 292: }
1.6 www 293: }
294: {
295: my $fh;
296: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
297: return 'error';
298: }
299: my $newname;
1.93 www 300: foreach $newname (keys %newenv) {
1.6 www 301: print $fh "$newname=$newenv{$newname}\n";
302: }
1.86 albertel 303: $fh->close();
1.56 www 304: }
1.95 www 305:
306: $lockfh->close();
1.56 www 307: return 'ok';
308: }
309: # ----------------------------------------------------- Delete from Environment
310:
311: sub delenv {
312: my $delthis=shift;
313: my %newenv=();
314: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
315: &logthis("<font color=blue>WARNING: ".
316: "Attempt to delete from environment ".$delthis);
317: return 'error';
318: }
319: my @oldenv;
320: {
321: my $fh;
322: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
323: return 'error';
324: }
1.89 www 325: unless (flock($fh,LOCK_SH)) {
326: &logthis("<font color=blue>WARNING: ".
327: 'Could not obtain shared lock in delenv: '.$!);
328: $fh->close();
329: return 'error: '.$!;
330: }
1.56 www 331: @oldenv=<$fh>;
1.89 www 332: $fh->close();
1.56 www 333: }
334: {
335: my $fh;
336: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
337: return 'error';
338: }
1.89 www 339: unless (flock($fh,LOCK_EX)) {
340: &logthis("<font color=blue>WARNING: ".
341: 'Could not obtain exclusive lock in delenv: '.$!);
342: $fh->close();
343: return 'error: '.$!;
344: }
1.191 harris41 345: foreach (@oldenv) {
1.56 www 346: unless ($_=~/^$delthis/) { print $fh $_; }
1.191 harris41 347: }
1.87 www 348: $fh->close();
1.5 www 349: }
350: return 'ok';
1.283 www 351: }
352:
353: # ------------------------------------------ Fight off request when overloaded
354:
355: sub overloaderror {
356: my ($r,$checkserver)=@_;
357: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
358: my $loadavg;
359: if ($checkserver eq $perlvar{'lonHostID'}) {
360: my $loadfile=Apache::File->new('/proc/loadavg');
361: $loadavg=<$loadfile>;
362: $loadavg =~ s/\s.*//g;
1.285 matthew 363: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.283 www 364: } else {
365: $loadavg=&reply('load',$checkserver);
366: }
1.285 matthew 367: my $overload=$loadavg-100;
1.283 www 368: if ($overload>0) {
1.285 matthew 369: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 370: $r->log_error('Overload of '.$overload.' on '.$checkserver);
371: return 413;
372: }
373: return '';
1.5 www 374: }
1.1 albertel 375:
376: # ------------------------------ Find server with least workload from spare.tab
1.11 www 377:
1.1 albertel 378: sub spareserver {
1.284 matthew 379: my $loadpercent = shift;
1.1 albertel 380: my $tryserver;
381: my $spareserver='';
1.284 matthew 382: my $lowestserver=$loadpercent;
1.1 albertel 383: foreach $tryserver (keys %spareid) {
384: my $answer=reply('load',$tryserver);
385: if (($answer =~ /\d/) && ($answer<$lowestserver)) {
386: $spareserver="http://$hostname{$tryserver}";
387: $lowestserver=$answer;
388: }
389: }
390: return $spareserver;
1.202 matthew 391: }
392:
393: # --------------------------------------------- Try to change a user's password
394:
395: sub changepass {
396: my ($uname,$udom,$currentpass,$newpass,$server)=@_;
397: $currentpass = &escape($currentpass);
398: $newpass = &escape($newpass);
399: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
400: $server);
401: if (! $answer) {
402: &logthis("No reply on password change request to $server ".
403: "by $uname in domain $udom.");
404: } elsif ($answer =~ "^ok") {
405: &logthis("$uname in $udom successfully changed their password ".
406: "on $server.");
407: } elsif ($answer =~ "^pwchange_failure") {
408: &logthis("$uname in $udom was unable to change their password ".
409: "on $server. The action was blocked by either lcpasswd ".
410: "or pwchange");
411: } elsif ($answer =~ "^non_authorized") {
412: &logthis("$uname in $udom did not get their password correct when ".
413: "attempting to change it on $server.");
414: } elsif ($answer =~ "^auth_mode_error") {
415: &logthis("$uname in $udom attempted to change their password despite ".
416: "not being locally or internally authenticated on $server.");
417: } elsif ($answer =~ "^unknown_user") {
418: &logthis("$uname in $udom attempted to change their password ".
419: "on $server but were unable to because $server is not ".
420: "their home server.");
421: } elsif ($answer =~ "^refused") {
422: &logthis("$server refused to change $uname in $udom password because ".
423: "it was sent an unencrypted request to change the password.");
424: }
425: return $answer;
1.1 albertel 426: }
427:
1.169 harris41 428: # ----------------------- Try to determine user's current authentication scheme
429:
430: sub queryauthenticate {
431: my ($uname,$udom)=@_;
432: if (($perlvar{'lonRole'} eq 'library') &&
433: ($udom eq $perlvar{'lonDefDomain'})) {
434: my $answer=reply("encrypt:currentauth:$udom:$uname",
435: $perlvar{'lonHostID'});
436: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
437: if (length($answer)) {
438: return $answer;
439: }
440: else {
441: &logthis("User $uname at $udom lacks an authentication mechanism");
442: return 'no_host';
443: }
444: }
445: }
446:
447: my $tryserver;
448: foreach $tryserver (keys %libserv) {
449: if ($hostdom{$tryserver} eq $udom) {
450: my $answer=reply("encrypt:currentauth:$udom:$uname",$tryserver);
451: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
452: if (length($answer)) {
453: return $answer;
454: }
455: else {
456: &logthis("User $uname at $udom lacks an authentication mechanism");
457: return 'no_host';
458: }
459: }
460: }
461: }
462: &logthis("User $uname at $udom lacks an authentication mechanism");
463: return 'no_host';
464: }
465:
1.1 albertel 466: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 467:
1.1 albertel 468: sub authenticate {
469: my ($uname,$upass,$udom)=@_;
1.12 www 470: $upass=escape($upass);
1.199 www 471: $uname=~s/\W//g;
1.1 albertel 472: if (($perlvar{'lonRole'} eq 'library') &&
473: ($udom eq $perlvar{'lonDefDomain'})) {
1.3 www 474: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$perlvar{'lonHostID'});
1.2 www 475: if ($answer =~ /authorized/) {
1.9 www 476: if ($answer eq 'authorized') {
477: &logthis("User $uname at $udom authorized by local server");
478: return $perlvar{'lonHostID'};
479: }
480: if ($answer eq 'non_authorized') {
481: &logthis("User $uname at $udom rejected by local server");
482: return 'no_host';
483: }
1.2 www 484: }
1.1 albertel 485: }
486:
487: my $tryserver;
488: foreach $tryserver (keys %libserv) {
489: if ($hostdom{$tryserver} eq $udom) {
1.10 www 490: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$tryserver);
1.1 albertel 491: if ($answer =~ /authorized/) {
1.9 www 492: if ($answer eq 'authorized') {
493: &logthis("User $uname at $udom authorized by $tryserver");
494: return $tryserver;
495: }
496: if ($answer eq 'non_authorized') {
497: &logthis("User $uname at $udom rejected by $tryserver");
498: return 'no_host';
499: }
1.1 albertel 500: }
501: }
1.9 www 502: }
503: &logthis("User $uname at $udom could not be authenticated");
1.1 albertel 504: return 'no_host';
505: }
506:
507: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 508:
1.1 albertel 509: sub homeserver {
1.230 stredwic 510: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 511: my $index="$uname:$udom";
1.221 matthew 512: if ($homecache{$index}) {
513: return "$homecache{$index}";
514: }
1.1 albertel 515: my $tryserver;
516: foreach $tryserver (keys %libserv) {
1.230 stredwic 517: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 518: exists($badServerCache{$tryserver}));
1.1 albertel 519: if ($hostdom{$tryserver} eq $udom) {
520: my $answer=reply("home:$udom:$uname",$tryserver);
521: if ($answer eq 'found') {
1.221 matthew 522: $homecache{$index}=$tryserver;
1.1 albertel 523: return $tryserver;
1.231 stredwic 524: } elsif ($answer eq 'no_host') {
525: $badServerCache{$tryserver}=1;
1.221 matthew 526: }
1.1 albertel 527: }
528: }
529: return 'no_host';
1.70 www 530: }
531:
532: # ------------------------------------- Find the usernames behind a list of IDs
533:
534: sub idget {
535: my ($udom,@ids)=@_;
536: my %returnhash=();
537:
538: my $tryserver;
539: foreach $tryserver (keys %libserv) {
540: if ($hostdom{$tryserver} eq $udom) {
541: my $idlist=join('&',@ids);
542: $idlist=~tr/A-Z/a-z/;
543: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
544: my @answer=();
1.76 www 545: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1.70 www 546: @answer=split(/\&/,$reply);
547: } ;
548: my $i;
549: for ($i=0;$i<=$#ids;$i++) {
550: if ($answer[$i]) {
551: $returnhash{$ids[$i]}=$answer[$i];
552: }
553: }
554: }
555: }
556: return %returnhash;
557: }
558:
559: # ------------------------------------- Find the IDs behind a list of usernames
560:
561: sub idrget {
562: my ($udom,@unames)=@_;
563: my %returnhash=();
1.191 harris41 564: foreach (@unames) {
1.70 www 565: $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
1.191 harris41 566: }
1.70 www 567: return %returnhash;
568: }
569:
570: # ------------------------------- Store away a list of names and associated IDs
571:
572: sub idput {
573: my ($udom,%ids)=@_;
574: my %servers=();
1.191 harris41 575: foreach (keys %ids) {
1.70 www 576: my $uhom=&homeserver($_,$udom);
577: if ($uhom ne 'no_host') {
578: my $id=&escape($ids{$_});
579: $id=~tr/A-Z/a-z/;
580: my $unam=&escape($_);
581: if ($servers{$uhom}) {
582: $servers{$uhom}.='&'.$id.'='.$unam;
583: } else {
584: $servers{$uhom}=$id.'='.$unam;
585: }
586: &critical('put:'.$udom.':'.$unam.':environment:id='.$id,$uhom);
587: }
1.191 harris41 588: }
589: foreach (keys %servers) {
1.70 www 590: &critical('idput:'.$udom.':'.$servers{$_},$_);
1.191 harris41 591: }
1.70 www 592: }
593:
594: # ------------------------------------- Find the section of student in a course
1.298 matthew 595:
596: sub getsection {
597: my ($udom,$unam,$courseid)=@_;
598: $courseid=~s/\_/\//g;
599: $courseid=~s/^(\w)/\/$1/;
600: my %Pending;
601: my %Expired;
602: #
603: # Each role can either have not started yet (pending), be active,
604: # or have expired.
605: #
606: # If there is an active role, we are done.
607: #
608: # If there is more than one role which has not started yet,
609: # choose the one which will start sooner
610: # If there is one role which has not started yet, return it.
611: #
612: # If there is more than one expired role, choose the one which ended last.
613: # If there is a role which has expired, return it.
614: #
615: foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
616: &homeserver($unam,$udom)))) {
617: my ($key,$value)=split(/\=/,$_);
618: $key=&unescape($key);
619: next if ($key !~/^$courseid(?:\/)*(\w+)*\_st$/);
620: my $section=$1;
621: if ($key eq $courseid.'_st') { $section=''; }
622: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
623: my $now=time;
624: if (defined($end) && ($now > $end)) {
625: $Expired{$end}=$section;
626: next;
627: }
628: if (defined($start) && ($now < $start)) {
629: $Pending{$start}=$section;
630: next;
631: }
632: return $section;
633: }
634: #
635: # Presumedly there will be few matching roles from the above
636: # loop and the sorting time will be negligible.
637: if (scalar(keys(%Pending))) {
638: my ($time) = sort {$a <=> $b} keys(%Pending);
639: return $Pending{$time};
640: }
641: if (scalar(keys(%Expired))) {
642: my @sorted = sort {$a <=> $b} keys(%Expired);
643: my $time = pop(@sorted);
644: return $Expired{$time};
645: }
646: return '-1';
647: }
1.70 www 648:
649: sub usection {
650: my ($udom,$unam,$courseid)=@_;
651: $courseid=~s/\_/\//g;
652: $courseid=~s/^(\w)/\/$1/;
1.191 harris41 653: foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
654: &homeserver($unam,$udom)))) {
1.70 www 655: my ($key,$value)=split(/\=/,$_);
656: $key=&unescape($key);
657: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
658: my $section=$1;
659: if ($key eq $courseid.'_st') { $section=''; }
660: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
661: my $now=time;
662: my $notactive=0;
663: if ($start) {
664: if ($now<$start) { $notactive=1; }
665: }
666: if ($end) {
667: if ($now>$end) { $notactive=1; }
668: }
669: unless ($notactive) { return $section; }
670: }
1.191 harris41 671: }
1.70 www 672: return '-1';
673: }
674:
675: # ------------------------------------- Read an entry from a user's environment
676:
677: sub userenvironment {
678: my ($udom,$unam,@what)=@_;
679: my %returnhash=();
680: my @answer=split(/\&/,
681: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
682: &homeserver($unam,$udom)));
683: my $i;
684: for ($i=0;$i<=$#what;$i++) {
685: $returnhash{$what[$i]}=&unescape($answer[$i]);
686: }
687: return %returnhash;
1.1 albertel 688: }
689:
1.263 www 690: # -------------------------------------------------------------------- New chat
691:
692: sub chatsend {
693: my ($newentry,$anon)=@_;
694: my $cnum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
695: my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
696: my $chome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
697: &reply('chatsend:'.$cdom.':'.$cnum.':'.
698: &escape($ENV{'user.domain'}.':'.$ENV{'user.name'}.':'.$anon.':'.
699: &escape($newentry)),$chome);
1.292 www 700: }
701:
702: # ------------------------------------------ Find current version of a resource
703:
704: sub getversion {
705: my $fname=&clutter(shift);
706: unless ($fname=~/^\/res\//) { return -1; }
707: return ¤tversion(&filelocation('',$fname));
708: }
709:
710: sub currentversion {
711: my $fname=shift;
712: my $author=$fname;
713: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
714: my ($udom,$uname)=split(/\//,$author);
715: my $home=homeserver($uname,$udom);
716: if ($home eq 'no_host') {
717: return -1;
718: }
719: my $answer=reply("currentversion:$fname",$home);
720: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
721: return -1;
722: }
723: return $answer;
1.263 www 724: }
725:
1.1 albertel 726: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 727:
1.1 albertel 728: sub subscribe {
729: my $fname=shift;
730: my $author=$fname;
731: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
732: my ($udom,$uname)=split(/\//,$author);
733: my $home=homeserver($uname,$udom);
1.290 www 734: if ($home eq 'no_host') {
1.1 albertel 735: return 'not_found';
736: }
737: my $answer=reply("sub:$fname",$home);
1.64 www 738: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
739: $answer.=' by '.$home;
740: }
1.1 albertel 741: return $answer;
742: }
743:
1.8 www 744: # -------------------------------------------------------------- Replicate file
745:
746: sub repcopy {
747: my $filename=shift;
1.23 www 748: $filename=~s/\/+/\//g;
1.214 www 749: if ($filename=~/^\/home\/httpd\/html\/adm\//) { return OK; }
1.8 www 750: my $transname="$filename.in.transfer";
1.17 www 751: if ((-e $filename) || (-e $transname)) { return OK; }
1.8 www 752: my $remoteurl=subscribe($filename);
1.64 www 753: if ($remoteurl =~ /^con_lost by/) {
754: &logthis("Subscribe returned $remoteurl: $filename");
1.8 www 755: return HTTP_SERVICE_UNAVAILABLE;
756: } elsif ($remoteurl eq 'not_found') {
757: &logthis("Subscribe returned not_found: $filename");
758: return HTTP_NOT_FOUND;
1.64 www 759: } elsif ($remoteurl =~ /^rejected by/) {
760: &logthis("Subscribe returned $remoteurl: $filename");
1.8 www 761: return FORBIDDEN;
1.20 www 762: } elsif ($remoteurl eq 'directory') {
763: return OK;
1.8 www 764: } else {
1.290 www 765: my $author=$filename;
766: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
767: my ($udom,$uname)=split(/\//,$author);
768: my $home=homeserver($uname,$udom);
769: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 770: my @parts=split(/\//,$filename);
771: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
772: if ($path ne "$perlvar{'lonDocRoot'}/res") {
773: &logthis("Malconfiguration for replication: $filename");
774: return HTTP_BAD_REQUEST;
775: }
776: my $count;
777: for ($count=5;$count<$#parts;$count++) {
778: $path.="/$parts[$count]";
779: if ((-e $path)!=1) {
780: mkdir($path,0777);
781: }
782: }
783: my $ua=new LWP::UserAgent;
784: my $request=new HTTP::Request('GET',"$remoteurl");
785: my $response=$ua->request($request,$transname);
786: if ($response->is_error()) {
787: unlink($transname);
788: my $message=$response->status_line;
1.12 www 789: &logthis("<font color=blue>WARNING:"
790: ." LWP get: $message: $filename</font>");
1.8 www 791: return HTTP_SERVICE_UNAVAILABLE;
792: } else {
1.16 www 793: if ($remoteurl!~/\.meta$/) {
794: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
795: my $mresponse=$ua->request($mrequest,$filename.'.meta');
796: if ($mresponse->is_error()) {
797: unlink($filename.'.meta');
798: &logthis(
799: "<font color=yellow>INFO: No metadata: $filename</font>");
800: }
801: }
1.8 www 802: rename($transname,$filename);
803: return OK;
804: }
1.290 www 805: }
1.8 www 806: }
807: }
808:
1.15 www 809: # --------------------------------------------------------- Server Side Include
810:
811: sub ssi {
812:
1.23 www 813: my ($fn,%form)=@_;
1.15 www 814:
815: my $ua=new LWP::UserAgent;
1.23 www 816:
817: my $request;
818:
819: if (%form) {
820: $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
1.201 albertel 821: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23 www 822: } else {
823: $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
824: }
825:
1.15 www 826: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
827: my $response=$ua->request($request);
828:
829: return $response->content;
830: }
1.254 www 831:
832: # ------- Add a token to a remote URI's query string to vouch for access rights
833:
834: sub tokenwrapper {
835: my $uri=shift;
1.259 www 836: $uri=~s/^http\:\/\/([^\/]+)//;
837: $uri=~s/^\///;
838: $ENV{'user.environment'}=~/\/([^\/]+)\.id/;
839: my $token=$1;
840: if ($uri=~/^uploaded\/([^\/]+)\/([^\/]+)\/([^\/]+)(\?\.*)*$/) {
841: &appenv('userfile.'.$1.'/'.$2.'/'.$3 => $ENV{'request.course.id'});
842: return 'http://'.$hostname{ &homeserver($2,$1)}.'/'.$uri.
843: (($uri=~/\?/)?'&':'?').'token='.$token;
844: } else {
845: return '/adm/notfound.html';
846: }
1.254 www 847: }
848:
1.257 www 849: # --------------- Take an uploaded file and put it into the userfiles directory
1.259 www 850: # input: name of form element, coursedoc=1 means this is for the course
1.257 www 851: # output: url of file in userspace
852:
853: sub userfileupload {
1.259 www 854: my ($formname,$coursedoc)=@_;
1.257 www 855: my $fname=$ENV{'form.'.$formname.'.filename'};
856: $fname=~s/\\/\//g;
857: $fname=~s/^.*\/([^\/]+)$/$1/;
858: unless ($fname) { return 'error: no uploaded file'; }
859: chop($ENV{'form.'.$formname});
1.258 www 860: # Create the directory if not present
1.259 www 861: my $docuname='';
862: my $docudom='';
863: my $docuhome='';
864: if ($coursedoc) {
865: $docuname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
866: $docudom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
867: $docuhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
868: } else {
869: $docuname=$ENV{'user.name'};
870: $docudom=$ENV{'user.domain'};
871: $docuhome=$ENV{'user.home'};
872: }
1.271 www 873: return
874: &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
875: }
876:
877: sub finishuserfileupload {
878: my ($docuname,$docudom,$docuhome,$formname,$fname)=@_;
1.259 www 879: my $path=$docudom.'/'.$docuname.'/';
1.258 www 880: my $filepath=$perlvar{'lonDocRoot'};
1.259 www 881: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 882: my $count;
883: for ($count=4;$count<=$#parts;$count++) {
884: $filepath.="/$parts[$count]";
885: if ((-e $filepath)!=1) {
886: mkdir($filepath,0777);
887: }
888: }
889: # Save the file
890: {
891: my $fh=Apache::File->new('>'.$filepath.'/'.$fname);
892: print $fh $ENV{'form.'.$formname};
893: }
1.259 www 894: # Notify homeserver to grep it
895: #
1.295 www 896:
897: my $fetchresult=
898: &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$fname,$docuhome);
899: if ($fetchresult eq 'ok') {
1.259 www 900: #
1.258 www 901: # Return the URL to it
1.263 www 902: return '/uploaded/'.$path.$fname;
903: } else {
1.295 www 904: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$fname.
905: ' to host '.$docuhome.': '.$fetchresult);
1.263 www 906: return '/adm/notfound.html';
907: }
1.257 www 908: }
1.15 www 909:
1.14 www 910: # ------------------------------------------------------------------------- Log
911:
912: sub log {
913: my ($dom,$nam,$hom,$what)=@_;
1.47 www 914: return critical("log:$dom:$nam:$what",$hom);
1.157 www 915: }
916:
917: # ------------------------------------------------------------------ Course Log
918:
919: sub flushcourselogs {
920: &logthis('Flushing course log buffers');
1.191 harris41 921: foreach (keys %courselogs) {
1.157 www 922: my $crsid=$_;
1.188 www 923: if (&reply('log:'.$coursedombuf{$crsid}.':'.
924: &escape($courselogs{$crsid}),
925: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 926: delete $courselogs{$crsid};
927: } else {
928: &logthis('Failed to flush log buffer for '.$crsid);
929: if (length($courselogs{$crsid})>40000) {
930: &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
931: " exceeded maximum size, deleting.</font>");
932: delete $courselogs{$crsid};
933: }
934: }
1.191 harris41 935: }
1.185 www 936: &logthis('Flushing access logs');
1.191 harris41 937: foreach (keys %accesshash) {
1.185 www 938: my $entry=$_;
939: $entry=~/\_\_\_(\w+)\/(\w+)\/(.*)\_\_\_(\w+)$/;
940: my %temphash=($entry => $accesshash{$entry});
1.266 albertel 941: if (&Apache::lonnet::put('nohist_resevaldata',\%temphash,$1,$2) eq 'ok') {
1.185 www 942: delete $accesshash{$entry};
943: }
1.191 harris41 944: }
1.186 www 945: $dumpcount++;
1.157 www 946: }
947:
948: sub courselog {
949: my $what=shift;
1.158 www 950: $what=time.':'.$what;
1.157 www 951: unless ($ENV{'request.course.id'}) { return ''; }
1.188 www 952: $coursedombuf{$ENV{'request.course.id'}}=
953: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
954: $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
955: $coursehombuf{$ENV{'request.course.id'}}=
956: $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.157 www 957: if (defined $courselogs{$ENV{'request.course.id'}}) {
958: $courselogs{$ENV{'request.course.id'}}.='&'.$what;
959: } else {
960: $courselogs{$ENV{'request.course.id'}}.=$what;
961: }
962: if (length($courselogs{$ENV{'request.course.id'}})>4048) {
963: &flushcourselogs();
964: }
1.158 www 965: }
966:
967: sub courseacclog {
968: my $fnsymb=shift;
969: unless ($ENV{'request.course.id'}) { return ''; }
970: my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.192 www 971: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form)$/) {
1.187 www 972: $what.=':POST';
1.191 harris41 973: foreach (keys %ENV) {
1.158 www 974: if ($_=~/^form\.(.*)/) {
975: $what.=':'.$1.'='.$ENV{$_};
976: }
1.191 harris41 977: }
1.158 www 978: }
979: &courselog($what);
1.149 www 980: }
981:
1.185 www 982: sub countacc {
983: my $url=&declutter(shift);
984: unless ($ENV{'request.course.id'}) { return ''; }
985: $accesshash{$ENV{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 986: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.185 www 987: if (defined($accesshash{$key})) {
988: $accesshash{$key}++;
989: } else {
990: $accesshash{$key}=1;
991: }
992: }
993:
1.149 www 994: # ----------------------------------------------------------- Check out an item
995:
996: sub checkout {
997: my ($symb,$tuname,$tudom,$tcrsid)=@_;
998: my $now=time;
999: my $lonhost=$perlvar{'lonHostID'};
1000: my $infostr=&escape(
1.234 www 1001: 'CHECKOUTTOKEN&'.
1.149 www 1002: $tuname.'&'.
1003: $tudom.'&'.
1004: $tcrsid.'&'.
1005: $symb.'&'.
1006: $now.'&'.$ENV{'REMOTE_ADDR'});
1007: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 1008: if ($token=~/^error\:/) {
1009: &logthis("<font color=blue>WARNING: ".
1010: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
1011: "</font>");
1012: return '';
1013: }
1014:
1.149 www 1015: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
1016: $token=~tr/a-z/A-Z/;
1017:
1.153 www 1018: my %infohash=('resource.0.outtoken' => $token,
1019: 'resource.0.checkouttime' => $now,
1020: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 1021:
1022: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
1023: return '';
1.151 www 1024: } else {
1025: &logthis("<font color=blue>WARNING: ".
1026: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
1027: "</font>");
1.149 www 1028: }
1029:
1030: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
1031: &escape('Checkout '.$infostr.' - '.
1032: $token)) ne 'ok') {
1033: return '';
1.151 www 1034: } else {
1035: &logthis("<font color=blue>WARNING: ".
1036: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
1037: "</font>");
1.149 www 1038: }
1.151 www 1039: return $token;
1.149 www 1040: }
1041:
1042: # ------------------------------------------------------------ Check in an item
1043:
1044: sub checkin {
1045: my $token=shift;
1.150 www 1046: my $now=time;
1047: my ($ta,$tb,$lonhost)=split(/\*/,$token);
1048: $lonhost=~tr/A-Z/a-z/;
1049: my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
1050: $dtoken=~s/\W/\_/g;
1.234 www 1051: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 1052: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
1053:
1.154 www 1054: unless (($tuname) && ($tudom)) {
1055: &logthis('Check in '.$token.' ('.$dtoken.') failed');
1056: return '';
1057: }
1058:
1059: unless (&allowed('mgr',$tcrsid)) {
1060: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1061: $ENV{'user.name'}.' - '.$ENV{'user.domain'});
1062: return '';
1063: }
1064:
1.153 www 1065: my %infohash=('resource.0.intoken' => $token,
1066: 'resource.0.checkintime' => $now,
1067: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 1068:
1069: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
1070: return '';
1071: }
1072:
1073: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
1074: &escape('Checkin - '.$token)) ne 'ok') {
1075: return '';
1076: }
1077:
1078: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 1079: }
1080:
1081: # --------------------------------------------- Set Expire Date for Spreadsheet
1082:
1083: sub expirespread {
1084: my ($uname,$udom,$stype,$usymb)=@_;
1085: my $cid=$ENV{'request.course.id'};
1086: if ($cid) {
1087: my $now=time;
1088: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1089: return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1090: $ENV{'course.'.$cid.'.num'}.
1091: ':nohist_expirationdates:'.
1092: &escape($key).'='.$now,
1093: $ENV{'course.'.$cid.'.home'})
1094: }
1095: return 'ok';
1.14 www 1096: }
1097:
1.109 www 1098: # ----------------------------------------------------- Devalidate Spreadsheets
1099:
1100: sub devalidate {
1101: my $symb=shift;
1102: my $cid=$ENV{'request.course.id'};
1103: if ($cid) {
1104: my $key=$ENV{'user.name'}.':'.$ENV{'user.domain'}.':';
1105: my $status=
1.299 matthew 1106: &del('nohist_calculatedsheets',
1.133 albertel 1107: [$key.'studentcalc'],
1108: $ENV{'course.'.$cid.'.domain'},
1109: $ENV{'course.'.$cid.'.num'})
1110: .' '.
1111: &del('nohist_calculatedsheets_'.$cid,
1112: [$key.'assesscalc:'.$symb]);
1.109 www 1113: unless ($status eq 'ok ok') {
1114: &logthis('Could not devalidate spreadsheet '.
1115: $ENV{'user.name'}.' at '.$ENV{'user.domain'}.' for '.
1116: $symb.': '.$status);
1.133 albertel 1117: }
1.109 www 1118: }
1119: }
1120:
1.265 albertel 1121: sub get_scalar {
1122: my ($string,$end) = @_;
1123: my $value;
1124: if ($$string =~ s/^([^&]*?)($end)/$2/) {
1125: $value = $1;
1126: } elsif ($$string =~ s/^([^&]*?)&//) {
1127: $value = $1;
1128: }
1129: return &unescape($value);
1130: }
1131:
1132: sub array2str {
1133: my (@array) = @_;
1134: my $result=&arrayref2str(\@array);
1135: $result=~s/^__ARRAY_REF__//;
1136: $result=~s/__END_ARRAY_REF__$//;
1137: return $result;
1138: }
1139:
1.204 albertel 1140: sub arrayref2str {
1141: my ($arrayref) = @_;
1.265 albertel 1142: my $result='__ARRAY_REF__';
1.204 albertel 1143: foreach my $elem (@$arrayref) {
1.265 albertel 1144: if(ref($elem) eq 'ARRAY') {
1145: $result.=&arrayref2str($elem).'&';
1146: } elsif(ref($elem) eq 'HASH') {
1147: $result.=&hashref2str($elem).'&';
1148: } elsif(ref($elem)) {
1149: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 1150: } else {
1151: $result.=&escape($elem).'&';
1152: }
1153: }
1154: $result=~s/\&$//;
1.265 albertel 1155: $result .= '__END_ARRAY_REF__';
1.204 albertel 1156: return $result;
1157: }
1158:
1.168 albertel 1159: sub hash2str {
1.204 albertel 1160: my (%hash) = @_;
1161: my $result=&hashref2str(\%hash);
1.265 albertel 1162: $result=~s/^__HASH_REF__//;
1163: $result=~s/__END_HASH_REF__$//;
1.204 albertel 1164: return $result;
1165: }
1166:
1167: sub hashref2str {
1168: my ($hashref)=@_;
1.265 albertel 1169: my $result='__HASH_REF__';
1.204 albertel 1170: foreach (keys(%$hashref)) {
1171: if (ref($_) eq 'ARRAY') {
1.265 albertel 1172: $result.=&arrayref2str($_).'=';
1.204 albertel 1173: } elsif (ref($_) eq 'HASH') {
1.265 albertel 1174: $result.=&hashref2str($_).'=';
1.204 albertel 1175: } elsif (ref($_)) {
1.265 albertel 1176: $result.='=';
1177: #print("Got a ref of ".(ref($_))." skipping.");
1.204 albertel 1178: } else {
1.265 albertel 1179: if ($_) {$result.=&escape($_).'=';} else { last; }
1.204 albertel 1180: }
1181:
1.265 albertel 1182: if(ref($hashref->{$_}) eq 'ARRAY') {
1183: $result.=&arrayref2str($hashref->{$_}).'&';
1184: } elsif(ref($hashref->{$_}) eq 'HASH') {
1185: $result.=&hashref2str($hashref->{$_}).'&';
1186: } elsif(ref($hashref->{$_})) {
1187: $result.='&';
1188: #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
1.204 albertel 1189: } else {
1.265 albertel 1190: $result.=&escape($hashref->{$_}).'&';
1.204 albertel 1191: }
1192: }
1.168 albertel 1193: $result=~s/\&$//;
1.265 albertel 1194: $result .= '__END_HASH_REF__';
1.168 albertel 1195: return $result;
1196: }
1197:
1198: sub str2hash {
1.265 albertel 1199: my ($string)=@_;
1200: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
1201: return %$hash;
1202: }
1203:
1204: sub str2hashref {
1.168 albertel 1205: my ($string) = @_;
1.265 albertel 1206:
1207: my %hash;
1208:
1209: if($string !~ /^__HASH_REF__/) {
1210: if (! ($string eq '' || !defined($string))) {
1211: $hash{'error'}='Not hash reference';
1212: }
1213: return (\%hash, $string);
1214: }
1215:
1216: $string =~ s/^__HASH_REF__//;
1217:
1218: while($string !~ /^__END_HASH_REF__/) {
1219: #key
1220: my $key='';
1221: if($string =~ /^__HASH_REF__/) {
1222: ($key, $string)=&str2hashref($string);
1223: if(defined($key->{'error'})) {
1224: $hash{'error'}='Bad data';
1225: return (\%hash, $string);
1226: }
1227: } elsif($string =~ /^__ARRAY_REF__/) {
1228: ($key, $string)=&str2arrayref($string);
1229: if($key->[0] eq 'Array reference error') {
1230: $hash{'error'}='Bad data';
1231: return (\%hash, $string);
1232: }
1233: } else {
1234: $string =~ s/^(.*?)=//;
1.267 albertel 1235: $key=&unescape($1);
1.265 albertel 1236: }
1237: $string =~ s/^=//;
1238:
1239: #value
1240: my $value='';
1241: if($string =~ /^__HASH_REF__/) {
1242: ($value, $string)=&str2hashref($string);
1243: if(defined($value->{'error'})) {
1244: $hash{'error'}='Bad data';
1245: return (\%hash, $string);
1246: }
1247: } elsif($string =~ /^__ARRAY_REF__/) {
1248: ($value, $string)=&str2arrayref($string);
1249: if($value->[0] eq 'Array reference error') {
1250: $hash{'error'}='Bad data';
1251: return (\%hash, $string);
1252: }
1253: } else {
1254: $value=&get_scalar(\$string,'__END_HASH_REF__');
1255: }
1256: $string =~ s/^&//;
1257:
1258: $hash{$key}=$value;
1.204 albertel 1259: }
1.265 albertel 1260:
1261: $string =~ s/^__END_HASH_REF__//;
1262:
1263: return (\%hash, $string);
1.204 albertel 1264: }
1265:
1266: sub str2array {
1.265 albertel 1267: my ($string)=@_;
1268: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
1269: return @$array;
1270: }
1271:
1272: sub str2arrayref {
1.204 albertel 1273: my ($string) = @_;
1.265 albertel 1274: my @array;
1275:
1276: if($string !~ /^__ARRAY_REF__/) {
1277: if (! ($string eq '' || !defined($string))) {
1278: $array[0]='Array reference error';
1279: }
1280: return (\@array, $string);
1281: }
1282:
1283: $string =~ s/^__ARRAY_REF__//;
1284:
1285: while($string !~ /^__END_ARRAY_REF__/) {
1286: my $value='';
1287: if($string =~ /^__HASH_REF__/) {
1288: ($value, $string)=&str2hashref($string);
1289: if(defined($value->{'error'})) {
1290: $array[0] ='Array reference error';
1291: return (\@array, $string);
1292: }
1293: } elsif($string =~ /^__ARRAY_REF__/) {
1294: ($value, $string)=&str2arrayref($string);
1295: if($value->[0] eq 'Array reference error') {
1296: $array[0] ='Array reference error';
1297: return (\@array, $string);
1298: }
1299: } else {
1300: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
1301: }
1302: $string =~ s/^&//;
1303:
1304: push(@array, $value);
1.191 harris41 1305: }
1.265 albertel 1306:
1307: $string =~ s/^__END_ARRAY_REF__//;
1308:
1309: return (\@array, $string);
1.168 albertel 1310: }
1311:
1.167 albertel 1312: # -------------------------------------------------------------------Temp Store
1313:
1.168 albertel 1314: sub tmpreset {
1315: my ($symb,$namespace,$domain,$stuname) = @_;
1316: if (!$symb) {
1317: $symb=&symbread();
1318: if (!$symb) { $symb= $ENV{'REQUEST_URI'}; }
1319: }
1320: $symb=escape($symb);
1321:
1322: if (!$namespace) { $namespace=$ENV{'request.state'}; }
1323: $namespace=~s/\//\_/g;
1324: $namespace=~s/\W//g;
1325:
1326: #FIXME needs to do something for /pub resources
1327: if (!$domain) { $domain=$ENV{'user.domain'}; }
1328: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1329: my $path=$perlvar{'lonDaemons'}.'/tmp';
1330: my %hash;
1331: if (tie(%hash,'GDBM_File',
1332: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 1333: &GDBM_WRCREAT(),0640)) {
1.168 albertel 1334: foreach my $key (keys %hash) {
1.180 albertel 1335: if ($key=~ /:$symb/) {
1.168 albertel 1336: delete($hash{$key});
1337: }
1338: }
1339: }
1340: }
1341:
1.167 albertel 1342: sub tmpstore {
1.168 albertel 1343: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1344:
1345: if (!$symb) {
1346: $symb=&symbread();
1347: if (!$symb) { $symb= $ENV{'request.url'}; }
1348: }
1349: $symb=escape($symb);
1350:
1351: if (!$namespace) {
1352: # I don't think we would ever want to store this for a course.
1353: # it seems this will only be used if we don't have a course.
1354: #$namespace=$ENV{'request.course.id'};
1355: #if (!$namespace) {
1356: $namespace=$ENV{'request.state'};
1357: #}
1358: }
1359: $namespace=~s/\//\_/g;
1360: $namespace=~s/\W//g;
1361: #FIXME needs to do something for /pub resources
1362: if (!$domain) { $domain=$ENV{'user.domain'}; }
1363: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1364: my $now=time;
1365: my %hash;
1366: my $path=$perlvar{'lonDaemons'}.'/tmp';
1367: if (tie(%hash,'GDBM_File',
1368: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 1369: &GDBM_WRCREAT(),0640)) {
1.168 albertel 1370: $hash{"version:$symb"}++;
1371: my $version=$hash{"version:$symb"};
1372: my $allkeys='';
1373: foreach my $key (keys(%$storehash)) {
1374: $allkeys.=$key.':';
1375: $hash{"$version:$symb:$key"}=$$storehash{$key};
1376: }
1377: $hash{"$version:$symb:timestamp"}=$now;
1378: $allkeys.='timestamp';
1379: $hash{"$version:keys:$symb"}=$allkeys;
1380: if (untie(%hash)) {
1381: return 'ok';
1382: } else {
1383: return "error:$!";
1384: }
1385: } else {
1386: return "error:$!";
1387: }
1388: }
1.167 albertel 1389:
1.168 albertel 1390: # -----------------------------------------------------------------Temp Restore
1.167 albertel 1391:
1.168 albertel 1392: sub tmprestore {
1393: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 1394:
1.168 albertel 1395: if (!$symb) {
1396: $symb=&symbread();
1397: if (!$symb) { $symb= $ENV{'request.url'}; }
1398: }
1399: $symb=escape($symb);
1400:
1401: if (!$namespace) { $namespace=$ENV{'request.state'}; }
1402: #FIXME needs to do something for /pub resources
1403: if (!$domain) { $domain=$ENV{'user.domain'}; }
1404: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1405:
1406: my %returnhash;
1407: $namespace=~s/\//\_/g;
1408: $namespace=~s/\W//g;
1409: my %hash;
1410: my $path=$perlvar{'lonDaemons'}.'/tmp';
1411: if (tie(%hash,'GDBM_File',
1412: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 1413: &GDBM_READER(),0640)) {
1.168 albertel 1414: my $version=$hash{"version:$symb"};
1415: $returnhash{'version'}=$version;
1416: my $scope;
1417: for ($scope=1;$scope<=$version;$scope++) {
1418: my $vkeys=$hash{"$scope:keys:$symb"};
1419: my @keys=split(/:/,$vkeys);
1420: my $key;
1421: $returnhash{"$scope:keys"}=$vkeys;
1422: foreach $key (@keys) {
1423: $returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
1424: $returnhash{"$key"}=$hash{"$scope:$symb:$key"};
1.167 albertel 1425: }
1426: }
1.168 albertel 1427: if (!(untie(%hash))) {
1428: return "error:$!";
1429: }
1430: } else {
1431: return "error:$!";
1432: }
1433: return %returnhash;
1.167 albertel 1434: }
1435:
1.9 www 1436: # ----------------------------------------------------------------------- Store
1437:
1438: sub store {
1.124 www 1439: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1440: my $home='';
1441:
1.168 albertel 1442: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1443:
1.213 www 1444: $symb=&symbclean($symb);
1.122 albertel 1445: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 1446:
1447: &devalidate($symb);
1448:
1449: $symb=escape($symb);
1.187 www 1450: if (!$namespace) {
1451: unless ($namespace=$ENV{'request.course.id'}) {
1452: return '';
1453: }
1454: }
1.122 albertel 1455: if (!$domain) { $domain=$ENV{'user.domain'}; }
1456: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1457: if (!$home) { $home=$ENV{'user.home'}; }
1.12 www 1458: my $namevalue='';
1.191 harris41 1459: foreach (keys %$storehash) {
1.122 albertel 1460: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191 harris41 1461: }
1.12 www 1462: $namevalue=~s/\&$//;
1.187 www 1463: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 1464: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 1465: }
1466:
1.47 www 1467: # -------------------------------------------------------------- Critical Store
1468:
1469: sub cstore {
1.124 www 1470: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1471: my $home='';
1472:
1.168 albertel 1473: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1474:
1.213 www 1475: $symb=&symbclean($symb);
1.122 albertel 1476: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 1477:
1478: &devalidate($symb);
1479:
1480: $symb=escape($symb);
1.187 www 1481: if (!$namespace) {
1482: unless ($namespace=$ENV{'request.course.id'}) {
1483: return '';
1484: }
1485: }
1.122 albertel 1486: if (!$domain) { $domain=$ENV{'user.domain'}; }
1487: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1488: if (!$home) { $home=$ENV{'user.home'}; }
1489:
1.47 www 1490: my $namevalue='';
1.191 harris41 1491: foreach (keys %$storehash) {
1.122 albertel 1492: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191 harris41 1493: }
1.47 www 1494: $namevalue=~s/\&$//;
1.187 www 1495: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 1496: return critical
1497: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 1498: }
1499:
1.9 www 1500: # --------------------------------------------------------------------- Restore
1501:
1502: sub restore {
1.124 www 1503: my ($symb,$namespace,$domain,$stuname) = @_;
1504: my $home='';
1505:
1.168 albertel 1506: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1507:
1.122 albertel 1508: if (!$symb) {
1509: unless ($symb=escape(&symbread())) { return ''; }
1510: } else {
1.213 www 1511: $symb=&escape(&symbclean($symb));
1.122 albertel 1512: }
1.188 www 1513: if (!$namespace) {
1514: unless ($namespace=$ENV{'request.course.id'}) {
1515: return '';
1516: }
1517: }
1.122 albertel 1518: if (!$domain) { $domain=$ENV{'user.domain'}; }
1519: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1520: if (!$home) { $home=$ENV{'user.home'}; }
1521: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
1522:
1.12 www 1523: my %returnhash=();
1.191 harris41 1524: foreach (split(/\&/,$answer)) {
1.12 www 1525: my ($name,$value)=split(/\=/,$_);
1526: $returnhash{&unescape($name)}=&unescape($value);
1.191 harris41 1527: }
1.75 www 1528: my $version;
1529: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.191 harris41 1530: foreach (split(/\:/,$returnhash{$version.':keys'})) {
1.75 www 1531: $returnhash{$_}=$returnhash{$version.':'.$_};
1.191 harris41 1532: }
1.75 www 1533: }
1.13 www 1534: return %returnhash;
1.34 www 1535: }
1536:
1537: # ---------------------------------------------------------- Course Description
1538:
1539: sub coursedescription {
1540: my $courseid=shift;
1541: $courseid=~s/^\///;
1.49 www 1542: $courseid=~s/\_/\//g;
1.34 www 1543: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 1544: my $chome=&homeserver($cnum,$cdomain);
1.34 www 1545: if ($chome ne 'no_host') {
1.129 albertel 1546: my %returnhash=&dump('environment',$cdomain,$cnum);
1547: if (!exists($returnhash{'con_lost'})) {
1.96 www 1548: my $normalid=$cdomain.'_'.$cnum;
1.53 www 1549: my %envhash=();
1.129 albertel 1550: $returnhash{'home'}= $chome;
1551: $returnhash{'domain'} = $cdomain;
1552: $returnhash{'num'} = $cnum;
1.130 albertel 1553: while (my ($name,$value) = each %returnhash) {
1.53 www 1554: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 1555: }
1.270 www 1556: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 1557: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.38 www 1558: $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.54 www 1559: $envhash{'course.'.$normalid.'.last_cache'}=time;
1.60 www 1560: $envhash{'course.'.$normalid.'.home'}=$chome;
1561: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
1562: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.53 www 1563: &appenv(%envhash);
1.34 www 1564: return %returnhash;
1565: }
1566: }
1567: return ();
1.9 www 1568: }
1.1 albertel 1569:
1.103 harris41 1570: # -------------------------------------------------------- Get user privileges
1.11 www 1571:
1572: sub rolesinit {
1573: my ($domain,$username,$authhost)=@_;
1574: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12 www 1575: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11 www 1576: my %allroles=();
1577: my %thesepriv=();
1578: my $now=time;
1.21 www 1579: my $userroles="user.login.time=$now\n";
1.11 www 1580: my $thesestr;
1581:
1582: if ($rolesdump ne '') {
1.191 harris41 1583: foreach (split(/&/,$rolesdump)) {
1.21 www 1584: if ($_!~/^rolesdef\&/) {
1.11 www 1585: my ($area,$role)=split(/=/,$_);
1.21 www 1586: $area=~s/\_\w\w$//;
1.11 www 1587: my ($trole,$tend,$tstart)=split(/_/,$role);
1.21 www 1588: $userroles.='user.role.'.$trole.'.'.$area.'='.
1589: $tstart.'.'.$tend."\n";
1.11 www 1590: if ($tend!=0) {
1591: if ($tend<$now) {
1592: $trole='';
1593: }
1594: }
1595: if ($tstart!=0) {
1596: if ($tstart>$now) {
1597: $trole='';
1598: }
1599: }
1600: if (($area ne '') && ($trole ne '')) {
1.50 www 1601: my $spec=$trole.'.'.$area;
1.12 www 1602: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
1603: if ($trole =~ /^cr\//) {
1604: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1605: my $homsvr=homeserver($rauthor,$rdomain);
1606: if ($hostname{$homsvr} ne '') {
1607: my $roledef=
1.21 www 1608: reply("get:$rdomain:$rauthor:roles:rolesdef_$rrole",
1.12 www 1609: $homsvr);
1610: if (($roledef ne 'con_lost') && ($roledef ne '')) {
1611: my ($syspriv,$dompriv,$coursepriv)=
1.21 www 1612: split(/\_/,unescape($roledef));
1.50 www 1613: $allroles{'cm./'}.=':'.$syspriv;
1614: $allroles{$spec.'./'}.=':'.$syspriv;
1.12 www 1615: if ($tdomain ne '') {
1.50 www 1616: $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
1617: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
1.12 www 1618: if ($trest ne '') {
1.50 www 1619: $allroles{'cm.'.$area}.=':'.$coursepriv;
1620: $allroles{$spec.'.'.$area}.=':'.$coursepriv;
1.12 www 1621: }
1622: }
1623: }
1.11 www 1624: }
1.12 www 1625: } else {
1.50 www 1626: $allroles{'cm./'}.=':'.$pr{$trole.':s'};
1627: $allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
1.12 www 1628: if ($tdomain ne '') {
1.50 www 1629: $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1630: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1.12 www 1631: if ($trest ne '') {
1.50 www 1632: $allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
1633: $allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
1.12 www 1634: }
1635: }
1.11 www 1636: }
1.12 www 1637: }
1638: }
1.191 harris41 1639: }
1.125 www 1640: my $adv=0;
1.128 www 1641: my $author=0;
1.191 harris41 1642: foreach (keys %allroles) {
1.11 www 1643: %thesepriv=();
1.146 www 1644: if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
1.128 www 1645: if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
1.191 harris41 1646: foreach (split(/:/,$allroles{$_})) {
1.11 www 1647: if ($_ ne '') {
1.103 harris41 1648: my ($privilege,$restrictions)=split(/&/,$_);
1.11 www 1649: if ($restrictions eq '') {
1.103 harris41 1650: $thesepriv{$privilege}='F';
1.11 www 1651: } else {
1.103 harris41 1652: if ($thesepriv{$privilege} ne 'F') {
1653: $thesepriv{$privilege}.=$restrictions;
1.11 www 1654: }
1655: }
1656: }
1.191 harris41 1657: }
1.11 www 1658: $thesestr='';
1.191 harris41 1659: foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
1.11 www 1660: $userroles.='user.priv.'.$_.'='.$thesestr."\n";
1.191 harris41 1661: }
1.128 www 1662: $userroles.='user.adv='.$adv."\n".
1663: 'user.author='.$author."\n";
1.126 www 1664: $ENV{'user.adv'}=$adv;
1.11 www 1665: }
1666: return $userroles;
1667: }
1668:
1.12 www 1669: # --------------------------------------------------------------- get interface
1670:
1671: sub get {
1.131 albertel 1672: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 1673: my $items='';
1.191 harris41 1674: foreach (@$storearr) {
1.12 www 1675: $items.=escape($_).'&';
1.191 harris41 1676: }
1.12 www 1677: $items=~s/\&$//;
1.131 albertel 1678: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1679: if (!$uname) { $uname=$ENV{'user.name'}; }
1680: my $uhome=&homeserver($uname,$udomain);
1681:
1.133 albertel 1682: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 1683: my @pairs=split(/\&/,$rep);
1.273 albertel 1684: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1685: return @pairs;
1686: }
1.15 www 1687: my %returnhash=();
1.42 www 1688: my $i=0;
1.191 harris41 1689: foreach (@$storearr) {
1.42 www 1690: $returnhash{$_}=unescape($pairs[$i]);
1691: $i++;
1.191 harris41 1692: }
1.15 www 1693: return %returnhash;
1.27 www 1694: }
1695:
1696: # --------------------------------------------------------------- del interface
1697:
1698: sub del {
1.133 albertel 1699: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 1700: my $items='';
1.191 harris41 1701: foreach (@$storearr) {
1.27 www 1702: $items.=escape($_).'&';
1.191 harris41 1703: }
1.27 www 1704: $items=~s/\&$//;
1.133 albertel 1705: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1706: if (!$uname) { $uname=$ENV{'user.name'}; }
1707: my $uhome=&homeserver($uname,$udomain);
1708:
1709: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 1710: }
1711:
1712: # -------------------------------------------------------------- dump interface
1713:
1714: sub dump {
1.193 www 1715: my ($namespace,$udomain,$uname,$regexp)=@_;
1.129 albertel 1716: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1717: if (!$uname) { $uname=$ENV{'user.name'}; }
1718: my $uhome=&homeserver($uname,$udomain);
1.193 www 1719: if ($regexp) {
1720: $regexp=&escape($regexp);
1721: } else {
1722: $regexp='.';
1723: }
1724: my $rep=reply("dump:$udomain:$uname:$namespace:$regexp",$uhome);
1.12 www 1725: my @pairs=split(/\&/,$rep);
1726: my %returnhash=();
1.191 harris41 1727: foreach (@pairs) {
1.12 www 1728: my ($key,$value)=split(/=/,$_);
1.29 www 1729: $returnhash{unescape($key)}=unescape($value);
1.191 harris41 1730: }
1.12 www 1731: return %returnhash;
1732: }
1733:
1734: # --------------------------------------------------------------- put interface
1735:
1736: sub put {
1.134 albertel 1737: my ($namespace,$storehash,$udomain,$uname)=@_;
1738: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1739: if (!$uname) { $uname=$ENV{'user.name'}; }
1740: my $uhome=&homeserver($uname,$udomain);
1.12 www 1741: my $items='';
1.191 harris41 1742: foreach (keys %$storehash) {
1.134 albertel 1743: $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
1.191 harris41 1744: }
1.12 www 1745: $items=~s/\&$//;
1.134 albertel 1746: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 1747: }
1748:
1749: # ------------------------------------------------------ critical put interface
1750:
1751: sub cput {
1.134 albertel 1752: my ($namespace,$storehash,$udomain,$uname)=@_;
1753: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1754: if (!$uname) { $uname=$ENV{'user.name'}; }
1755: my $uhome=&homeserver($uname,$udomain);
1.47 www 1756: my $items='';
1.191 harris41 1757: foreach (keys %$storehash) {
1.134 albertel 1758: $items.=escape($_).'='.escape($$storehash{$_}).'&';
1.191 harris41 1759: }
1.47 www 1760: $items=~s/\&$//;
1.134 albertel 1761: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 1762: }
1763:
1764: # -------------------------------------------------------------- eget interface
1765:
1766: sub eget {
1.133 albertel 1767: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 1768: my $items='';
1.191 harris41 1769: foreach (@$storearr) {
1.12 www 1770: $items.=escape($_).'&';
1.191 harris41 1771: }
1.12 www 1772: $items=~s/\&$//;
1.133 albertel 1773: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1774: if (!$uname) { $uname=$ENV{'user.name'}; }
1775: my $uhome=&homeserver($uname,$udomain);
1776: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 1777: my @pairs=split(/\&/,$rep);
1778: my %returnhash=();
1.42 www 1779: my $i=0;
1.191 harris41 1780: foreach (@$storearr) {
1.42 www 1781: $returnhash{$_}=unescape($pairs[$i]);
1782: $i++;
1.191 harris41 1783: }
1.12 www 1784: return %returnhash;
1785: }
1786:
1.103 harris41 1787: # ------------------------------------------------- Check for a user privilege
1.12 www 1788:
1789: sub allowed {
1790: my ($priv,$uri)=@_;
1.152 www 1791:
1792: my $orguri=$uri;
1.52 www 1793: $uri=&declutter($uri);
1.29 www 1794:
1.54 www 1795: # Free bre access to adm and meta resources
1.29 www 1796:
1.54 www 1797: if ((($uri=~/^adm\//) || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14 www 1798: return 'F';
1.159 www 1799: }
1800:
1801: # Free bre to public access
1802:
1803: if ($priv eq 'bre') {
1.238 www 1804: my $copyright=&metadata($uri,'copyright');
1.301 ! www 1805: if (($copyright eq 'public') && (!$ENV{'request.course.id'})) {
! 1806: return 'F';
! 1807: }
1.238 www 1808: if ($copyright eq 'priv') {
1809: $uri=~/([^\/]+)\/([^\/]+)\//;
1810: unless (($ENV{'user.name'} eq $2) && ($ENV{'user.domain'} eq $1)) {
1811: return '';
1812: }
1813: }
1814: if ($copyright eq 'domain') {
1815: $uri=~/([^\/]+)\/([^\/]+)\//;
1816: unless (($ENV{'user.domain'} eq $1) ||
1817: ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $1)) {
1818: return '';
1819: }
1.262 matthew 1820: }
1821: if ($ENV{'request.role'}=~ /li\.\//) {
1822: # Library role, so allow browsing of resources in this domain.
1823: return 'F';
1.238 www 1824: }
1.14 www 1825: }
1.264 matthew 1826: # Domain coordinator is trying to create a course
1827: if (($priv eq 'ccc') && ($ENV{'request.role'} =~ /^dc\./)) {
1828: # uri is the requested domain in this case.
1829: # comparison to 'request.role.domain' shows if the user has selected
1830: # a role of dc for the domain in question.
1831: return 'F' if ($uri eq $ENV{'request.role.domain'});
1832: }
1.29 www 1833:
1.52 www 1834: my $thisallowed='';
1835: my $statecond=0;
1836: my $courseprivid='';
1837:
1838: # Course
1839:
1840: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/$priv\&([^\:]*)/) {
1841: $thisallowed.=$1;
1842: }
1.29 www 1843:
1.52 www 1844: # Domain
1845:
1846: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1847: =~/$priv\&([^\:]*)/) {
1.12 www 1848: $thisallowed.=$1;
1849: }
1.52 www 1850:
1851: # Course: uri itself is a course
1.66 www 1852: my $courseuri=$uri;
1853: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 1854: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 1855:
1.83 www 1856: if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1.52 www 1857: =~/$priv\&([^\:]*)/) {
1.12 www 1858: $thisallowed.=$1;
1859: }
1.29 www 1860:
1.52 www 1861: # Full access at system, domain or course-wide level? Exit.
1.29 www 1862:
1863: if ($thisallowed=~/F/) {
1864: return 'F';
1865: }
1866:
1.52 www 1867: # If this is generating or modifying users, exit with special codes
1.29 www 1868:
1.166 www 1869: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:$priv\:/) {
1.52 www 1870: return $thisallowed;
1871: }
1872: #
1.103 harris41 1873: # Gathered so far: system, domain and course wide privileges
1.52 www 1874: #
1875: # Course: See if uri or referer is an individual resource that is part of
1876: # the course
1877:
1878: if ($ENV{'request.course.id'}) {
1.232 www 1879:
1.52 www 1880: $courseprivid=$ENV{'request.course.id'};
1881: if ($ENV{'request.course.sec'}) {
1882: $courseprivid.='/'.$ENV{'request.course.sec'};
1883: }
1884: $courseprivid=~s/\_/\//;
1885: my $checkreferer=1;
1.232 www 1886: my ($match,$cond)=&is_on_map($uri);
1887: if ($match) {
1888: $statecond=$cond;
1.52 www 1889: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1890: =~/$priv\&([^\:]*)/) {
1891: $thisallowed.=$1;
1892: $checkreferer=0;
1893: }
1.29 www 1894: }
1.83 www 1895:
1.148 www 1896: if ($checkreferer) {
1.152 www 1897: my $refuri=$ENV{'httpref.'.$orguri};
1.148 www 1898: unless ($refuri) {
1.191 harris41 1899: foreach (keys %ENV) {
1.148 www 1900: if ($_=~/^httpref\..*\*/) {
1901: my $pattern=$_;
1.156 www 1902: $pattern=~s/^httpref\.\/res\///;
1.148 www 1903: $pattern=~s/\*/\[\^\/\]\+/g;
1904: $pattern=~s/\//\\\//g;
1.152 www 1905: if ($orguri=~/$pattern/) {
1.148 www 1906: $refuri=$ENV{$_};
1907: }
1908: }
1.191 harris41 1909: }
1.148 www 1910: }
1.232 www 1911:
1.148 www 1912: if ($refuri) {
1.152 www 1913: $refuri=&declutter($refuri);
1.232 www 1914: my ($match,$cond)=&is_on_map($refuri);
1915: if ($match) {
1916: my $refstatecond=$cond;
1.52 www 1917: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1918: =~/$priv\&([^\:]*)/) {
1919: $thisallowed.=$1;
1.53 www 1920: $uri=$refuri;
1921: $statecond=$refstatecond;
1.52 www 1922: }
1923: }
1.148 www 1924: }
1.29 www 1925: }
1.52 www 1926: }
1.29 www 1927:
1.52 www 1928: #
1.103 harris41 1929: # Gathered now: all privileges that could apply, and condition number
1.52 www 1930: #
1931: #
1932: # Full or no access?
1933: #
1.29 www 1934:
1.52 www 1935: if ($thisallowed=~/F/) {
1936: return 'F';
1937: }
1.29 www 1938:
1.52 www 1939: unless ($thisallowed) {
1940: return '';
1941: }
1.29 www 1942:
1.52 www 1943: # Restrictions exist, deal with them
1944: #
1945: # C:according to course preferences
1946: # R:according to resource settings
1947: # L:unless locked
1948: # X:according to user session state
1949: #
1950:
1951: # Possibly locked functionality, check all courses
1.54 www 1952: # Locks might take effect only after 10 minutes cache expiration for other
1953: # courses, and 2 minutes for current course
1.52 www 1954:
1955: my $envkey;
1956: if ($thisallowed=~/L/) {
1957: foreach $envkey (keys %ENV) {
1.54 www 1958: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
1959: my $courseid=$2;
1960: my $roleid=$1.'.'.$2;
1.92 www 1961: $courseid=~s/^\///;
1.54 www 1962: my $expiretime=600;
1963: if ($ENV{'request.role'} eq $roleid) {
1964: $expiretime=120;
1965: }
1966: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
1967: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1968: if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
1969: &coursedescription($courseid);
1970: }
1971: if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,$csec\,/)
1972: || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
1973: if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1.57 www 1974: &log($ENV{'user.domain'},$ENV{'user.name'},
1.239 www 1975: $ENV{'user.home'},
1.57 www 1976: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 1977: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1978: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1979: return '';
1980: }
1981: }
1.54 www 1982: if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,$csec\,/)
1983: || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1984: if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1.57 www 1985: &log($ENV{'user.domain'},$ENV{'user.name'},
1.239 www 1986: $ENV{'user.home'},
1.57 www 1987: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 1988: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1989: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1990: return '';
1991: }
1992: }
1993: }
1.29 www 1994: }
1.52 www 1995: }
1996:
1997: #
1998: # Rest of the restrictions depend on selected course
1999: #
2000:
2001: unless ($ENV{'request.course.id'}) {
2002: return '1';
2003: }
1.29 www 2004:
1.52 www 2005: #
2006: # Now user is definitely in a course
2007: #
1.53 www 2008:
2009:
2010: # Course preferences
2011:
2012: if ($thisallowed=~/C/) {
1.54 www 2013: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1.237 www 2014: my $unamedom=$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.54 www 2015: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.194 www 2016: =~/$rolecode/) {
1.57 www 2017: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
2018: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1.237 www 2019: $ENV{'request.course.id'});
2020: return '';
2021: }
2022:
2023: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.users.denied'}
2024: =~/$unamedom/) {
2025: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
2026: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
1.54 www 2027: $ENV{'request.course.id'});
2028: return '';
2029: }
1.53 www 2030: }
2031:
2032: # Resource preferences
2033:
2034: if ($thisallowed=~/R/) {
1.54 www 2035: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
2036: my $filename=$perlvar{'lonDocRoot'}.'/res/'.$uri.'.meta';
2037: if (-e $filename) {
2038: my @content;
2039: {
2040: my $fh=Apache::File->new($filename);
2041: @content=<$fh>;
2042: }
2043: if (join('',@content)=~
2044: /\<roledeny[^\>]*\>[^\<]*$rolecode[^\<]*\<\/roledeny\>/) {
1.57 www 2045: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
2046: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1.54 www 2047: return '';
2048:
2049: }
2050: }
1.53 www 2051: }
1.30 www 2052:
1.246 www 2053: # Restricted by state or randomout?
1.30 www 2054:
1.52 www 2055: if ($thisallowed=~/X/) {
1.247 www 2056: if ($ENV{'acc.randomout'}) {
1.249 www 2057: my $symb=&symbread($uri,1);
1.248 www 2058: if (($symb) && ($ENV{'acc.randomout'}=~/\&$symb\&/)) {
2059: return '';
2060: }
1.247 www 2061: }
2062: if (&condval($statecond)) {
1.52 www 2063: return '2';
2064: } else {
2065: return '';
2066: }
2067: }
1.30 www 2068:
1.52 www 2069: return 'F';
1.232 www 2070: }
2071:
2072: # --------------------------------------------------- Is a resource on the map?
2073:
2074: sub is_on_map {
2075: my $uri=&declutter(shift);
2076: my @uriparts=split(/\//,$uri);
2077: my $filename=$uriparts[$#uriparts];
2078: my $pathname=$uri;
1.289 bowersj2 2079: $pathname=~s|/\Q$filename\E$||;
2080: #Trying to find the conditional for the file
1.232 www 2081: my $match=($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 2082: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 2083: if ($match) {
1.289 bowersj2 2084: return (1,$1);
2085: } else {
2086: return (0,0);
2087: }
1.12 www 2088: }
2089:
2090: # ----------------------------------------------------------------- Define Role
2091:
2092: sub definerole {
2093: if (allowed('mcr','/')) {
2094: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.191 harris41 2095: foreach (split('/',$sysrole)) {
1.21 www 2096: my ($crole,$cqual)=split(/\&/,$_);
2097: if ($pr{'cr:s'}!~/$crole/) { return "refused:s:$crole"; }
2098: if ($pr{'cr:s'}=~/$crole\&/) {
2099: if ($pr{'cr:s'}!~/$crole\&\w*$cqual/) {
2100: return "refused:s:$crole&$cqual";
2101: }
2102: }
1.191 harris41 2103: }
2104: foreach (split('/',$domrole)) {
1.21 www 2105: my ($crole,$cqual)=split(/\&/,$_);
2106: if ($pr{'cr:d'}!~/$crole/) { return "refused:d:$crole"; }
2107: if ($pr{'cr:d'}=~/$crole\&/) {
2108: if ($pr{'cr:d'}!~/$crole\&\w*$cqual/) {
2109: return "refused:d:$crole&$cqual";
2110: }
2111: }
1.191 harris41 2112: }
2113: foreach (split('/',$courole)) {
1.21 www 2114: my ($crole,$cqual)=split(/\&/,$_);
2115: if ($pr{'cr:c'}!~/$crole/) { return "refused:c:$crole"; }
2116: if ($pr{'cr:c'}=~/$crole\&/) {
2117: if ($pr{'cr:c'}!~/$crole\&\w*$cqual/) {
2118: return "refused:c:$crole&$cqual";
2119: }
2120: }
1.191 harris41 2121: }
1.12 www 2122: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
2123: "$ENV{'user.domain'}:$ENV{'user.name'}:".
1.21 www 2124: "rolesdef_$rolename=".
2125: escape($sysrole.'_'.$domrole.'_'.$courole);
1.12 www 2126: return reply($command,$ENV{'user.home'});
2127: } else {
2128: return 'refused';
2129: }
1.105 harris41 2130: }
2131:
2132: # ---------------- Make a metadata query against the network of library servers
2133:
2134: sub metadata_query {
1.244 matthew 2135: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 2136: my %rhash;
1.244 matthew 2137: my @server_list = (defined($server_array) ? @$server_array
2138: : keys(%libserv) );
2139: for my $server (@server_list) {
1.118 harris41 2140: unless ($custom or $customshow) {
2141: my $reply=&reply("querysend:".&escape($query),$server);
2142: $rhash{$server}=$reply;
2143: }
2144: else {
2145: my $reply=&reply("querysend:".&escape($query).':'.
2146: &escape($custom).':'.&escape($customshow),
2147: $server);
2148: $rhash{$server}=$reply;
2149: }
1.112 harris41 2150: }
1.118 harris41 2151: return \%rhash;
1.240 www 2152: }
2153:
2154: # ----------------------------------------- Send log queries and wait for reply
2155:
2156: sub log_query {
2157: my ($uname,$udom,$query,%filters)=@_;
2158: my $uhome=&homeserver($uname,$udom);
2159: if ($uhome eq 'no_host') { return 'error: no_host'; }
2160: my $uhost=$hostname{$uhome};
1.241 www 2161: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
1.240 www 2162: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
2163: $uhome);
2164: unless ($queryid=~/^$uhost\_/) { return 'error: '.$queryid; }
1.242 www 2165: return get_query_reply($queryid);
2166: }
2167:
2168: sub get_query_reply {
2169: my $queryid=shift;
1.240 www 2170: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
2171: my $reply='';
2172: for (1..100) {
2173: sleep 2;
2174: if (-e $replyfile.'.end') {
2175: if (my $fh=Apache::File->new($replyfile)) {
2176: $reply.=<$fh>;
2177: $fh->close;
2178: } else { return 'error: reply_file_error'; }
1.242 www 2179: return &unescape($reply);
2180: }
1.240 www 2181: }
1.242 www 2182: return 'timeout:'.$queryid;
1.240 www 2183: }
2184:
2185: sub courselog_query {
1.241 www 2186: #
2187: # possible filters:
2188: # url: url or symb
2189: # username
2190: # domain
2191: # action: view, submit, grade
2192: # start: timestamp
2193: # end: timestamp
2194: #
1.240 www 2195: my (%filters)=@_;
2196: unless ($ENV{'request.course.id'}) { return 'no_course'; }
1.241 www 2197: if ($filters{'url'}) {
2198: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
2199: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
2200: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
2201: }
1.240 www 2202: my $cname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
2203: my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
2204: return &log_query($cname,$cdom,'courselog',%filters);
2205: }
2206:
2207: sub userlog_query {
2208: my ($uname,$udom,%filters)=@_;
2209: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 2210: }
2211:
2212: # ------------------------------------------------------------------ Plain Text
2213:
2214: sub plaintext {
1.22 www 2215: my $short=shift;
2216: return $prp{$short};
1.12 www 2217: }
2218:
2219: # ----------------------------------------------------------------- Assign Role
2220:
2221: sub assignrole {
1.21 www 2222: my ($udom,$uname,$url,$role,$end,$start)=@_;
2223: my $mrole;
2224: if ($role =~ /^cr\//) {
1.104 www 2225: unless (&allowed('ccr',$url)) {
2226: &logthis('Refused custom assignrole: '.
2227: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
2228: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
2229: return 'refused';
2230: }
1.21 www 2231: $mrole='cr';
2232: } else {
1.82 www 2233: my $cwosec=$url;
1.83 www 2234: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.104 www 2235: unless (&allowed('c'.$role,$cwosec)) {
2236: &logthis('Refused assignrole: '.
2237: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
2238: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
2239: return 'refused';
2240: }
1.21 www 2241: $mrole=$role;
2242: }
2243: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
2244: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 2245: if ($end) { $command.='_'.$end; }
1.21 www 2246: if ($start) {
2247: if ($end) {
1.81 www 2248: $command.='_'.$start;
1.21 www 2249: } else {
1.81 www 2250: $command.='_0_'.$start;
1.21 www 2251: }
2252: }
2253: return &reply($command,&homeserver($uname,$udom));
1.169 harris41 2254: }
2255:
2256: # -------------------------------------------------- Modify user authentication
1.197 www 2257: # Overrides without validation
2258:
1.169 harris41 2259: sub modifyuserauth {
2260: my ($udom,$uname,$umode,$upass)=@_;
2261: my $uhome=&homeserver($uname,$udom);
1.197 www 2262: unless (&allowed('mau',$udom)) { return 'refused'; }
2263: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.272 matthew 2264: $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
2265: ' in domain '.$ENV{'request.role.domain'});
1.169 harris41 2266: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
2267: &escape($upass),$uhome);
1.197 www 2268: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
2269: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
2270: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
2271: &log($udom,,$uname,$uhome,
2272: 'Authentication changed by '.$ENV{'user.domain'}.', '.
2273: $ENV{'user.name'}.', '.$umode.
2274: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 2275: unless ($reply eq 'ok') {
1.197 www 2276: &logthis('Authentication mode error: '.$reply);
1.169 harris41 2277: return 'error: '.$reply;
2278: }
1.170 harris41 2279: return 'ok';
1.80 www 2280: }
2281:
1.81 www 2282: # --------------------------------------------------------------- Modify a user
1.80 www 2283:
1.81 www 2284: sub modifyuser {
1.206 matthew 2285: my ($udom, $uname, $uid,
2286: $umode, $upass, $first,
2287: $middle, $last, $gene,
2288: $forceid, $desiredhome)=@_;
1.198 www 2289: $udom=~s/\W//g;
2290: $uname=~s/\W//g;
1.81 www 2291: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 2292: $umode.', '.$first.', '.$middle.', '.
1.206 matthew 2293: $last.', '.$gene.'(forceid: '.$forceid.')'.
2294: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
2295: ' desiredhome not specified').
1.272 matthew 2296: ' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
2297: ' in domain '.$ENV{'request.role.domain'});
1.230 stredwic 2298: my $uhome=&homeserver($uname,$udom,'true');
1.80 www 2299: # ----------------------------------------------------------------- Create User
1.81 www 2300: if (($uhome eq 'no_host') && ($umode) && ($upass)) {
1.80 www 2301: my $unhome='';
1.209 matthew 2302: if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) {
2303: $unhome = $desiredhome;
2304: } elsif($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1.80 www 2305: $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.209 matthew 2306: } else { # load balancing routine for determining $unhome
1.80 www 2307: my $tryserver;
1.81 www 2308: my $loadm=10000000;
1.80 www 2309: foreach $tryserver (keys %libserv) {
2310: if ($hostdom{$tryserver} eq $udom) {
2311: my $answer=reply('load',$tryserver);
2312: if (($answer=~/\d+/) && ($answer<$loadm)) {
2313: $loadm=$answer;
2314: $unhome=$tryserver;
2315: }
2316: }
2317: }
2318: }
2319: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 2320: return 'error: unable to find a home server for '.$uname.
2321: ' in domain '.$udom;
1.80 www 2322: }
2323: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
2324: &escape($upass),$unhome);
2325: unless ($reply eq 'ok') {
2326: return 'error: '.$reply;
2327: }
1.230 stredwic 2328: $uhome=&homeserver($uname,$udom,'true');
1.80 www 2329: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
2330: return 'error: verify home';
2331: }
1.209 matthew 2332: } # End of creation of new user
1.80 www 2333: # ---------------------------------------------------------------------- Add ID
2334: if ($uid) {
2335: $uid=~tr/A-Z/a-z/;
2336: my %uidhash=&idrget($udom,$uname);
1.196 www 2337: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
2338: && (!$forceid)) {
1.80 www 2339: unless ($uid eq $uidhash{$uname}) {
2340: return 'error: mismatch '.$uidhash{$uname}.' versus '.$uid;
2341: }
2342: } else {
2343: &idput($udom,($uname => $uid));
2344: }
2345: }
2346: # -------------------------------------------------------------- Add names, etc
1.134 albertel 2347: my %names=&get('environment',
2348: ['firstname','middlename','lastname','generation'],
2349: $udom,$uname);
1.207 albertel 2350: if ($names{'firstname'} =~ m/^error:.*/) { %names=(); }
1.134 albertel 2351: if ($first) { $names{'firstname'} = $first; }
2352: if ($middle) { $names{'middlename'} = $middle; }
2353: if ($last) { $names{'lastname'} = $last; }
2354: if ($gene) { $names{'generation'} = $gene; }
2355: my $reply = &put('environment', \%names, $udom,$uname);
2356: if ($reply ne 'ok') { return 'error: '.$reply; }
1.81 www 2357: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 2358: $umode.', '.$first.', '.$middle.', '.
2359: $last.', '.$gene.' by '.
2360: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1.134 albertel 2361: return 'ok';
1.80 www 2362: }
2363:
1.81 www 2364: # -------------------------------------------------------------- Modify student
1.80 www 2365:
1.81 www 2366: sub modifystudent {
2367: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.209 matthew 2368: $end,$start,$forceid,$desiredhome)=@_;
1.81 www 2369: my $cid='';
2370: unless ($cid=$ENV{'request.course.id'}) {
1.80 www 2371: return 'not_in_class';
2372: }
2373: # --------------------------------------------------------------- Make the user
1.81 www 2374: my $reply=&modifyuser
1.209 matthew 2375: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
2376: $desiredhome);
1.80 www 2377: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 2378: # This will cause &modify_student_enrollment to get the uid from the
2379: # students environment
2380: $uid = undef if (!$forceid);
2381: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,
2382: $last,$gene,$usec,$end,$start);
2383: return $reply;
2384: }
2385:
2386: sub modify_student_enrollment {
2387: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start) = @_;
2388: # Get the course id from the environment
2389: my $cid='';
2390: unless ($cid=$ENV{'request.course.id'}) {
2391: return 'not_in_class';
2392: }
2393: # Make sure the user exists
1.81 www 2394: my $uhome=&homeserver($uname,$udom);
2395: if (($uhome eq '') || ($uhome eq 'no_host')) {
2396: return 'error: no such user';
2397: }
1.297 matthew 2398: #
2399: # Get student data if we were not given enough information
2400: if (!defined($first) || $first eq '' ||
2401: !defined($last) || $last eq '' ||
2402: !defined($uid) || $uid eq '' ||
2403: !defined($middle) || $middle eq '' ||
2404: !defined($gene) || $gene eq '') {
1.294 matthew 2405: # They did not supply us with enough data to enroll the student, so
2406: # we need to pick up more information.
1.297 matthew 2407: my %tmp = &get('environment',
1.294 matthew 2408: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 2409: ,$udom,$uname);
2410:
2411: foreach (keys(%tmp)) {
2412: &logthis("key $_ = ".$tmp{$_});
2413: }
1.294 matthew 2414: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
2415: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
2416: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 2417: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 2418: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
2419: }
2420: my $fullname = &Apache::loncoursedata::ProcessFullName($last,$gene,
2421: $first,$middle);
1.297 matthew 2422: my $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1.81 www 2423: $ENV{'course.'.$cid.'.num'}.':classlist:'.
2424: &escape($uname.':'.$udom).'='.
1.294 matthew 2425: &escape(join(':',$end,$start,$uid,$usec,$fullname)),
1.81 www 2426: $ENV{'course.'.$cid.'.home'});
2427: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
2428: return 'error: '.$reply;
2429: }
1.297 matthew 2430: # Add student role to user
1.83 www 2431: my $uurl='/'.$cid;
1.81 www 2432: $uurl=~s/\_/\//g;
2433: if ($usec) {
2434: $uurl.='/'.$usec;
2435: }
2436: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21 www 2437: }
2438:
1.84 www 2439: # ------------------------------------------------- Write to course preferences
2440:
2441: sub writecoursepref {
2442: my ($courseid,%prefs)=@_;
2443: $courseid=~s/^\///;
2444: $courseid=~s/\_/\//g;
2445: my ($cdomain,$cnum)=split(/\//,$courseid);
2446: my $chome=homeserver($cnum,$cdomain);
2447: if (($chome eq '') || ($chome eq 'no_host')) {
2448: return 'error: no such course';
2449: }
2450: my $cstring='';
1.191 harris41 2451: foreach (keys %prefs) {
1.84 www 2452: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
1.191 harris41 2453: }
1.84 www 2454: $cstring=~s/\&$//;
2455: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
2456: }
2457:
2458: # ---------------------------------------------------------- Make/modify course
2459:
2460: sub createcourse {
1.271 www 2461: my ($udom,$description,$url,$course_server,$nonstandard)=@_;
1.84 www 2462: $url=&declutter($url);
2463: my $cid='';
1.264 matthew 2464: unless (&allowed('ccc',$udom)) {
1.84 www 2465: return 'refused';
2466: }
2467: # ------------------------------------------------------------------- Create ID
2468: my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
2469: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
2470: # ----------------------------------------------- Make sure that does not exist
1.230 stredwic 2471: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 2472: unless (($uhome eq '') || ($uhome eq 'no_host')) {
2473: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
2474: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230 stredwic 2475: $uhome=&homeserver($uname,$udom,'true');
1.84 www 2476: unless (($uhome eq '') || ($uhome eq 'no_host')) {
2477: return 'error: unable to generate unique course-ID';
2478: }
2479: }
1.264 matthew 2480: # ------------------------------------------------ Check supplied server name
2481: $course_server = $ENV{'user.homeserver'} if (! defined($course_server));
2482: if (! exists($libserv{$course_server})) {
2483: return 'error:bad server name '.$course_server;
2484: }
1.84 www 2485: # ------------------------------------------------------------- Make the course
2486: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 2487: $course_server);
1.84 www 2488: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230 stredwic 2489: $uhome=&homeserver($uname,$udom,'true');
1.84 www 2490: if (($uhome eq '') || ($uhome eq 'no_host')) {
2491: return 'error: no such course';
2492: }
1.271 www 2493: # ----------------------------------------------------------------- Course made
2494: my $topurl=$url;
2495: unless ($nonstandard) {
2496: # ------------------------------------------ For standard courses, make top url
2497: my $mapurl=&clutter($url);
1.278 www 2498: if ($mapurl eq '/res/') { $mapurl=''; }
1.271 www 2499: $ENV{'form.initmap'}=(<<ENDINITMAP);
2500: <map>
2501: <resource id="1" type="start"></resource>
2502: <resource id="2" src="$mapurl"></resource>
2503: <resource id="3" type="finish"></resource>
2504: <link index="1" from="1" to="2"></link>
2505: <link index="2" from="2" to="3"></link>
2506: </map>
2507: ENDINITMAP
2508: $topurl=&declutter(
2509: &finishuserfileupload($uname,$udom,$uhome,'initmap','default.sequence')
2510: );
2511: }
2512: # ----------------------------------------------------------- Write preferences
1.84 www 2513: &writecoursepref($udom.'_'.$uname,
2514: ('description' => $description,
1.271 www 2515: 'url' => $topurl));
1.84 www 2516: return '/'.$udom.'/'.$uname;
2517: }
2518:
1.21 www 2519: # ---------------------------------------------------------- Assign Custom Role
2520:
2521: sub assigncustomrole {
2522: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start)=@_;
2523: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
2524: $end,$start);
2525: }
2526:
2527: # ----------------------------------------------------------------- Revoke Role
2528:
2529: sub revokerole {
2530: my ($udom,$uname,$url,$role)=@_;
2531: my $now=time;
2532: return &assignrole($udom,$uname,$url,$role,$now);
2533: }
2534:
2535: # ---------------------------------------------------------- Revoke Custom Role
2536:
2537: sub revokecustomrole {
2538: my ($udom,$uname,$url,$rdom,$rnam,$rolename)=@_;
2539: my $now=time;
2540: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now);
1.17 www 2541: }
2542:
2543: # ------------------------------------------------------------ Directory lister
2544:
2545: sub dirlist {
1.253 stredwic 2546: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
2547:
1.18 www 2548: $uri=~s/^\///;
2549: $uri=~s/\/$//;
1.253 stredwic 2550: my ($udom, $uname);
2551: (undef,$udom,$uname)=split(/\//,$uri);
2552: if(defined($userdomain)) {
2553: $udom = $userdomain;
2554: }
2555: if(defined($username)) {
2556: $uname = $username;
2557: }
2558:
2559: my $dirRoot = $perlvar{'lonDocRoot'};
2560: if(defined($alternateDirectoryRoot)) {
2561: $dirRoot = $alternateDirectoryRoot;
2562: $dirRoot =~ s/\/$//;
2563: }
2564:
2565: if($udom) {
2566: if($uname) {
2567: my $listing=reply('ls:'.$dirRoot.'/'.$uri,
2568: homeserver($uname,$udom));
2569: return split(/:/,$listing);
2570: } elsif(!defined($alternateDirectoryRoot)) {
2571: my $tryserver;
2572: my %allusers=();
2573: foreach $tryserver (keys %libserv) {
2574: if($hostdom{$tryserver} eq $udom) {
2575: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
2576: $udom, $tryserver);
2577: if (($listing ne 'no_such_dir') && ($listing ne 'empty')
2578: && ($listing ne 'con_lost')) {
2579: foreach (split(/:/,$listing)) {
2580: my ($entry,@stat)=split(/&/,$_);
2581: $allusers{$entry}=1;
2582: }
2583: }
1.191 harris41 2584: }
1.253 stredwic 2585: }
2586: my $alluserstr='';
2587: foreach (sort keys %allusers) {
2588: $alluserstr.=$_.'&user:';
2589: }
2590: $alluserstr=~s/:$//;
2591: return split(/:/,$alluserstr);
2592: } else {
2593: my @emptyResults = ();
2594: push(@emptyResults, 'missing user name');
2595: return split(':',@emptyResults);
2596: }
2597: } elsif(!defined($alternateDirectoryRoot)) {
2598: my $tryserver;
2599: my %alldom=();
2600: foreach $tryserver (keys %libserv) {
2601: $alldom{$hostdom{$tryserver}}=1;
2602: }
2603: my $alldomstr='';
2604: foreach (sort keys %alldom) {
2605: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'&domain:';
2606: }
2607: $alldomstr=~s/:$//;
2608: return split(/:/,$alldomstr);
2609: } else {
2610: my @emptyResults = ();
2611: push(@emptyResults, 'missing domain');
2612: return split(':',@emptyResults);
1.275 stredwic 2613: }
2614: }
2615:
2616: # --------------------------------------------- GetFileTimestamp
2617: # This function utilizes dirlist and returns the date stamp for
2618: # when it was last modified. It will also return an error of -1
2619: # if an error occurs
2620:
2621: sub GetFileTimestamp {
2622: my ($studentDomain,$studentName,$filename,$root)=@_;
2623: $studentDomain=~s/\W//g;
2624: $studentName=~s/\W//g;
2625: my $subdir=$studentName.'__';
2626: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
2627: my $proname="$studentDomain/$subdir/$studentName";
2628: $proname .= '/'.$filename;
2629: my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
2630: $root);
2631: my $fileStat = $dir[0];
2632: my @stats = split('&', $fileStat);
2633: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
2634: return $stats[9];
2635: } else {
2636: return -1;
1.253 stredwic 2637: }
1.26 www 2638: }
2639:
2640: # -------------------------------------------------------- Value of a Condition
2641:
1.40 www 2642: sub directcondval {
2643: my $number=shift;
2644: if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
2645: return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
2646: } else {
2647: return 2;
2648: }
2649: }
2650:
1.26 www 2651: sub condval {
2652: my $condidx=shift;
2653: my $result=0;
1.54 www 2654: my $allpathcond='';
1.191 harris41 2655: foreach (split(/\|/,$condidx)) {
1.54 www 2656: if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
2657: $allpathcond.=
2658: '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
2659: }
1.191 harris41 2660: }
1.54 www 2661: $allpathcond=~s/\|$//;
1.33 www 2662: if ($ENV{'request.course.id'}) {
1.54 www 2663: if ($allpathcond) {
1.26 www 2664: my $operand='|';
2665: my @stack;
1.191 harris41 2666: foreach ($allpathcond=~/(\d+|\(|\)|\&|\|)/g) {
1.26 www 2667: if ($_ eq '(') {
2668: push @stack,($operand,$result)
2669: } elsif ($_ eq ')') {
2670: my $before=pop @stack;
2671: if (pop @stack eq '&') {
2672: $result=$result>$before?$before:$result;
2673: } else {
2674: $result=$result>$before?$result:$before;
2675: }
2676: } elsif (($_ eq '&') || ($_ eq '|')) {
2677: $operand=$_;
2678: } else {
1.40 www 2679: my $new=directcondval($_);
1.26 www 2680: if ($operand eq '&') {
2681: $result=$result>$new?$new:$result;
2682: } else {
2683: $result=$result>$new?$result:$new;
1.191 harris41 2684: }
1.26 www 2685: }
1.191 harris41 2686: }
1.26 www 2687: }
2688: }
2689: return $result;
1.279 www 2690: }
2691:
2692: # ---------------------------------------------------- Devalidate courseresdata
2693:
2694: sub devalidatecourseresdata {
2695: my ($coursenum,$coursedomain)=@_;
2696: my $hashid=$coursenum.':'.$coursedomain;
2697: delete $courseresdatacache{$hashid.'.time'};
1.28 www 2698: }
2699:
1.200 www 2700: # --------------------------------------------------- Course Resourcedata Query
2701:
2702: sub courseresdata {
2703: my ($coursenum,$coursedomain,@which)=@_;
2704: my $coursehom=&homeserver($coursenum,$coursedomain);
2705: my $hashid=$coursenum.':'.$coursedomain;
1.250 albertel 2706: my $dodump=0;
2707: if (!defined($courseresdatacache{$hashid.'.time'})) {
2708: $dodump=1;
2709: } else {
2710: if (time-$courseresdatacache{$hashid.'.time'}>300) { $dodump=1; }
2711: }
2712: if ($dodump) {
1.251 albertel 2713: my %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
2714: my ($tmp) = keys(%dumpreply);
2715: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
2716: $courseresdatacache{$hashid.'.time'}=time;
2717: $courseresdatacache{$hashid}=\%dumpreply;
1.250 albertel 2718: }
2719: }
1.251 albertel 2720: foreach my $item (@which) {
1.287 albertel 2721: if (defined($courseresdatacache{$hashid}->{$item})) {
1.251 albertel 2722: return $courseresdatacache{$hashid}->{$item};
2723: }
1.250 albertel 2724: }
1.291 albertel 2725: return undef;
1.200 www 2726: }
2727:
1.28 www 2728: # --------------------------------------------------------- Value of a Variable
2729:
1.58 www 2730: sub EXT {
1.282 albertel 2731: my ($varname,$symbparm,$udom,$uname,)=@_;
1.218 albertel 2732:
1.68 www 2733: unless ($varname) { return ''; }
1.218 albertel 2734:
2735: #get real user name/domain, courseid and symb
2736: my $courseid;
2737: if (!($uname && $udom)) {
2738: (my $cursymb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
2739: if (!$symbparm) { $symbparm=$cursymb; }
2740: } else {
2741: $courseid=$ENV{'request.course.id'};
2742: }
2743:
1.48 www 2744: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
2745: my $rest;
2746: if ($therest[0]) {
2747: $rest=join('.',@therest);
2748: } else {
2749: $rest='';
2750: }
1.57 www 2751: my $qualifierrest=$qualifier;
2752: if ($rest) { $qualifierrest.='.'.$rest; }
2753: my $spacequalifierrest=$space;
2754: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 2755: if ($realm eq 'user') {
1.48 www 2756: # --------------------------------------------------------------- user.resource
2757: if ($space eq 'resource') {
1.218 albertel 2758: my %restored=&restore(undef,undef,$udom,$uname);
1.57 www 2759: return $restored{$qualifierrest};
1.48 www 2760: # ----------------------------------------------------------------- user.access
2761: } elsif ($space eq 'access') {
1.218 albertel 2762: # FIXME - not supporting calls for a specific user
1.48 www 2763: return &allowed($qualifier,$rest);
2764: # ------------------------------------------ user.preferences, user.environment
2765: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.218 albertel 2766: if (($uname eq $ENV{'user.name'}) &&
2767: ($udom eq $ENV{'user.domain'})) {
2768: return $ENV{join('.',('environment',$qualifierrest))};
2769: } else {
2770: my %returnhash=&userenvironment($udom,$uname,$qualifierrest);
2771: return $returnhash{$qualifierrest};
2772: }
1.48 www 2773: # ----------------------------------------------------------------- user.course
2774: } elsif ($space eq 'course') {
1.218 albertel 2775: # FIXME - not supporting calls for a specific user
1.48 www 2776: return $ENV{join('.',('request.course',$qualifier))};
2777: # ------------------------------------------------------------------- user.role
2778: } elsif ($space eq 'role') {
1.218 albertel 2779: # FIXME - not supporting calls for a specific user
1.48 www 2780: my ($role,$where)=split(/\./,$ENV{'request.role'});
2781: if ($qualifier eq 'value') {
2782: return $role;
2783: } elsif ($qualifier eq 'extent') {
2784: return $where;
2785: }
2786: # ----------------------------------------------------------------- user.domain
2787: } elsif ($space eq 'domain') {
1.218 albertel 2788: return $udom;
1.48 www 2789: # ------------------------------------------------------------------- user.name
2790: } elsif ($space eq 'name') {
1.218 albertel 2791: return $uname;
1.48 www 2792: # ---------------------------------------------------- Any other user namespace
1.29 www 2793: } else {
1.48 www 2794: my $item=($rest)?$qualifier.'.'.$rest:$qualifier;
1.131 albertel 2795: my %reply=&get($space,[$item]);
1.48 www 2796: return $reply{$item};
2797: }
1.236 www 2798: } elsif ($realm eq 'query') {
2799: # ---------------------------------------------- pull stuff out of query string
2800: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},[$space]);
2801: return $ENV{'form.'.$space};
2802: } elsif ($realm eq 'request') {
1.48 www 2803: # ------------------------------------------------------------- request.browser
2804: if ($space eq 'browser') {
2805: return $ENV{'browser.'.$qualifier};
1.57 www 2806: # ------------------------------------------------------------ request.filename
2807: } else {
2808: return $ENV{'request.'.$spacequalifierrest};
1.29 www 2809: }
1.28 www 2810: } elsif ($realm eq 'course') {
1.48 www 2811: # ---------------------------------------------------------- course.description
1.218 albertel 2812: return $ENV{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 2813: } elsif ($realm eq 'resource') {
1.165 www 2814:
1.218 albertel 2815: if ($courseid eq $ENV{'request.course.id'}) {
1.165 www 2816:
1.218 albertel 2817: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 2818:
1.60 www 2819: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 2820: if (!$symbparm) { $symbparm=&symbread(); }
2821: my $symbp=$symbparm;
2822: my $mapp=(split(/\_\_\_/,$symbp))[0];
2823:
2824: my $symbparm=$symbp.'.'.$spacequalifierrest;
2825: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
2826:
2827: my $section;
2828: if (($ENV{'user.name'} eq $uname) &&
2829: ($ENV{'user.domain'} eq $udom)) {
1.255 albertel 2830: $section=$ENV{'request.course.sec'};
1.218 albertel 2831: } else {
2832: $section=&usection($udom,$uname,$courseid);
2833: }
2834:
2835: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
2836: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
2837: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
2838:
2839: my $courselevel=$courseid.'.'.$spacequalifierrest;
2840: my $courselevelr=$courseid.'.'.$symbparm;
2841: my $courselevelm=$courseid.'.'.$mapparm;
1.69 www 2842:
1.60 www 2843: # ----------------------------------------------------------- first, check user
1.218 albertel 2844: my %resourcedata=&get('resourcedata',
2845: [$courselevelr,$courselevelm,$courselevel],
2846: $udom,$uname);
2847: if (($resourcedata{$courselevelr}!~/^error\:/) &&
2848: ($resourcedata{$courselevelr}!~/^con_lost/)) {
2849:
2850: if ($resourcedata{$courselevelr}) {
2851: return $resourcedata{$courselevelr}; }
2852: if ($resourcedata{$courselevelm}) {
2853: return $resourcedata{$courselevelm}; }
2854: if ($resourcedata{$courselevel}) {
2855: return $resourcedata{$courselevel}; }
2856: } else {
2857: if ($resourcedata{$courselevelr}!~/No such file/) {
2858: &logthis("<font color=blue>WARNING:".
2859: " Trying to get resource data for ".
2860: $uname." at ".$udom.": ".
2861: $resourcedata{$courselevelr}."</font>");
2862: }
2863: }
1.95 www 2864:
1.60 www 2865: # -------------------------------------------------------- second, check course
1.96 www 2866:
1.218 albertel 2867: my $coursereply=&courseresdata($ENV{'course.'.$courseid.'.num'},
2868: $ENV{'course.'.$courseid.'.domain'},
2869: ($seclevelr,$seclevelm,$seclevel,
2870: $courselevelr,$courselevelm,
2871: $courselevel));
1.287 albertel 2872: if (defined($coursereply)) { return $coursereply; }
1.200 www 2873:
1.60 www 2874: # ------------------------------------------------------ third, check map parms
1.218 albertel 2875: my %parmhash=();
2876: my $thisparm='';
2877: if (tie(%parmhash,'GDBM_File',
2878: $ENV{'request.course.fn'}.'_parms.db',
1.256 albertel 2879: &GDBM_READER(),0640)) {
1.218 albertel 2880: $thisparm=$parmhash{$symbparm};
2881: untie(%parmhash);
2882: }
2883: if ($thisparm) { return $thisparm; }
2884: }
1.60 www 2885: # --------------------------------------------- last, look in resource metadata
1.71 www 2886:
1.218 albertel 2887: $spacequalifierrest=~s/\./\_/;
1.282 albertel 2888: my $filename;
2889: if (!$symbparm) { $symbparm=&symbread(); }
2890: if ($symbparm) {
2891: $filename=(split(/\_\_\_/,$symbparm))[2];
2892: } else {
2893: $filename=$ENV{'request.filename'};
2894: }
2895: my $metadata=&metadata($filename,$spacequalifierrest);
1.288 albertel 2896: if (defined($metadata)) { return $metadata; }
1.282 albertel 2897: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288 albertel 2898: if (defined($metadata)) { return $metadata; }
1.142 www 2899:
1.145 www 2900: # ------------------------------------------------------------------ Cascade up
1.218 albertel 2901: unless ($space eq '0') {
2902: my ($part,$id)=split(/\_/,$space);
2903: if ($id) {
2904: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
2905: $symbparm,$udom,$uname);
1.288 albertel 2906: if (defined($partgeneral)) { return $partgeneral; }
1.218 albertel 2907: } else {
2908: my $resourcegeneral=&EXT('resource.0.'.$qualifierrest,
2909: $symbparm,$udom,$uname);
1.288 albertel 2910: if (defined($resourcegeneral)) { return $resourcegeneral; }
1.218 albertel 2911: }
2912: }
1.71 www 2913:
1.48 www 2914: # ---------------------------------------------------- Any other user namespace
2915: } elsif ($realm eq 'environment') {
2916: # ----------------------------------------------------------------- environment
1.219 albertel 2917: if (($uname eq $ENV{'user.name'})&&($udom eq $ENV{'user.domain'})) {
2918: return $ENV{'environment.'.$spacequalifierrest};
2919: } else {
2920: my %returnhash=&userenvironment($udom,$uname,
2921: $spacequalifierrest);
2922: return $returnhash{$spacequalifierrest};
2923: }
1.28 www 2924: } elsif ($realm eq 'system') {
1.48 www 2925: # ----------------------------------------------------------------- system.time
2926: if ($space eq 'time') {
2927: return time;
2928: }
1.28 www 2929: }
1.48 www 2930: return '';
1.61 www 2931: }
2932:
1.71 www 2933: # ---------------------------------------------------------------- Get metadata
2934:
2935: sub metadata {
1.176 www 2936: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.78 www 2937:
1.71 www 2938: $uri=&declutter($uri);
1.288 albertel 2939: # if it is a non metadata possible uri return quickly
1.293 matthew 2940: if (($uri eq '') || (($uri =~ m|^/*adm/|) && ($uri !~ m|^adm/includes|)) ||
2941: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|)) {
1.288 albertel 2942: return '';
2943: }
1.73 www 2944: my $filename=$uri;
2945: $uri=~s/\.meta$//;
1.172 www 2946: #
2947: # Is the metadata already cached?
1.177 www 2948: # Look at timestamp of caching
1.172 www 2949: # Everything is cached by the main uri, libraries are never directly cached
2950: #
1.277 albertel 2951: unless (abs($metacache{$uri.':cachedtimestamp'}-time)<600 && !defined($liburi)) {
1.172 www 2952: #
2953: # Is this a recursive call for a library?
2954: #
1.171 www 2955: if ($liburi) {
2956: $liburi=&declutter($liburi);
2957: $filename=$liburi;
2958: }
1.140 www 2959: my %metathesekeys=();
1.73 www 2960: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
2961: my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
1.208 albertel 2962: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 2963: my $token;
1.140 www 2964: undef %metathesekeys;
1.71 www 2965: while ($token=$parser->get_token) {
2966: if ($token->[0] eq 'S') {
1.135 www 2967: if (defined($token->[2]->{'package'})) {
1.172 www 2968: #
2969: # This is a package - get package info
2970: #
1.136 www 2971: my $package=$token->[2]->{'package'};
2972: my $keyroot='';
1.172 www 2973: if ($prefix) {
1.276 albertel 2974: $keyroot.=$prefix;
1.172 www 2975: } else {
2976: if (defined($token->[2]->{'part'})) {
2977: $keyroot.='_'.$token->[2]->{'part'};
2978: }
1.136 www 2979: }
2980: if (defined($token->[2]->{'id'})) {
1.165 www 2981: $keyroot.='_'.$token->[2]->{'id'};
1.136 www 2982: }
2983: if ($metacache{$uri.':packages'}) {
2984: $metacache{$uri.':packages'}.=','.$package.$keyroot;
2985: } else {
2986: $metacache{$uri.':packages'}=$package.$keyroot;
2987: }
1.191 harris41 2988: foreach (keys %packagetab) {
1.137 www 2989: if ($_=~/^$package\&/) {
2990: my ($pack,$name,$subp)=split(/\&/,$_);
1.139 www 2991: my $value=$packagetab{$_};
1.144 www 2992: my $part=$keyroot;
2993: $part=~s/^\_//;
1.139 www 2994: if ($subp eq 'display') {
2995: $value.=' [Part: '.$part.']';
2996: }
2997: my $unikey='parameter'.$keyroot.'_'.$name;
1.140 www 2998: $metathesekeys{$unikey}=1;
1.144 www 2999: $metacache{$uri.':'.$unikey.'.part'}=$part;
1.141 www 3000: unless
3001: (defined($metacache{$uri.':'.$unikey.'.'.$subp})) {
3002: $metacache{$uri.':'.$unikey.'.'.$subp}=$value;
3003: }
1.137 www 3004: }
1.191 harris41 3005: }
1.135 www 3006: } else {
1.172 www 3007: #
3008: # This is not a package - some other kind of start tag
3009: #
1.175 www 3010: my $entry=$token->[1];
1.176 www 3011: my $unikey;
3012: if ($entry eq 'import') {
3013: $unikey='';
3014: } else {
3015: $unikey=$entry;
3016: }
1.172 www 3017: if ($prefix) {
1.176 www 3018: $unikey.=$prefix;
1.172 www 3019: } else {
3020: if (defined($token->[2]->{'part'})) {
3021: $unikey.='_'.$token->[2]->{'part'};
3022: }
1.136 www 3023: }
3024: if (defined($token->[2]->{'id'})) {
1.165 www 3025: $unikey.='_'.$token->[2]->{'id'};
1.71 www 3026: }
1.175 www 3027:
3028: if ($entry eq 'import') {
3029: #
3030: # Importing a library here
1.176 www 3031: #
3032: if ($depthcount<20) {
1.276 albertel 3033: my $location=$parser->get_text('/import');
3034: my $dir=$filename;
3035: $dir=~s|[^/]*$||;
3036: $location=&filelocation($dir,$location);
3037: foreach (sort(split(/\,/,&metadata($uri,'keys',
3038: $location,$unikey,
3039: $depthcount+1)))) {
1.177 www 3040: $metathesekeys{$_}=1;
1.191 harris41 3041: }
1.176 www 3042: }
1.175 www 3043: } else {
3044:
1.72 www 3045: if (defined($token->[2]->{'name'})) {
1.71 www 3046: $unikey.='_'.$token->[2]->{'name'};
3047: }
1.140 www 3048: $metathesekeys{$unikey}=1;
1.191 harris41 3049: foreach (@{$token->[3]}) {
1.71 www 3050: $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.191 harris41 3051: }
1.78 www 3052: unless (
1.208 albertel 3053: $metacache{$uri.':'.$unikey}=&HTML::Entities::decode($parser->get_text('/'.$entry))
1.78 www 3054: ) { $metacache{$uri.':'.$unikey}=
3055: $metacache{$uri.':'.$unikey.'.default'};
3056: }
1.172 www 3057: # end of not-a-package not-a-library import
1.175 www 3058: }
1.172 www 3059: # end of not-a-package start tag
3060: }
3061: # the next is the end of "start tag"
1.140 www 3062: }
1.71 www 3063: }
1.274 albertel 3064: $metacache{$uri.':keys'}=join(',',keys %metathesekeys);
1.261 albertel 3065: &metadata_generate_part0(\%metathesekeys,\%metacache,$uri);
1.274 albertel 3066: $metacache{$uri.':allpossiblekeys'}=join(',',keys %metathesekeys);
1.174 www 3067: $metacache{$uri.':cachedtimestamp'}=time;
1.177 www 3068: # this is the end of "was not already recently cached
1.71 www 3069: }
3070: return $metacache{$uri.':'.$what};
1.261 albertel 3071: }
3072:
3073: sub metadata_generate_part0 {
3074: my ($metadata,$metacache,$uri) = @_;
3075: my %allnames;
3076: foreach my $metakey (sort keys %$metadata) {
3077: if ($metakey=~/^parameter\_(.*)/) {
3078: my $part=$$metacache{$uri.':'.$metakey.'.part'};
3079: my $name=$$metacache{$uri.':'.$metakey.'.name'};
3080: if (! exists($$metadata{'parameter_0_'.$name})) {
3081: $allnames{$name}=$part;
3082: }
3083: }
3084: }
3085: foreach my $name (keys(%allnames)) {
3086: $$metadata{"parameter_0_$name"}=1;
3087: my $key="$uri:parameter_0_$name";
3088: $$metacache{"$key.part"}='0';
3089: $$metacache{"$key.name"}=$name;
3090: $$metacache{"$key.type"}=$$metacache{$uri.':parameter_'.
3091: $allnames{$name}.'_'.$name.
3092: '.type'};
3093: my $olddis=$$metacache{$uri.':parameter_'.$allnames{$name}.'_'.$name.
3094: '.display'};
3095: my $expr='\\[Part: '.$allnames{$name}.'\\]';
3096: $olddis=~s/$expr/\[Part: 0\]/;
3097: $$metacache{"$key.display"}=$olddis;
3098: }
1.71 www 3099: }
3100:
1.301 ! www 3101: # ------------------------------------------------- Get the title of a resource
! 3102:
! 3103: sub gettitle {
! 3104: my $urlsymb=shift;
! 3105: my $symb=&symbread($urlsymb);
! 3106: unless ($symb) {
! 3107: unless ($urlsymb) { $urlsymb=$ENV{'request.filename'}; }
! 3108: return &metadata($urlsymb,'title');
! 3109: }
! 3110: if ($titlecache{$symb}) { return $titlecache{$symb}; }
! 3111: my ($map,$resid,$url)=split(/\_\_\_/,$symb);
! 3112: my $title='';
! 3113: my %bighash;
! 3114: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
! 3115: &GDBM_READER(),0640)) {
! 3116: my $mapid=$bighash{'map_pc_'.&clutter($map)};
! 3117: $title=$bighash{'title_'.$mapid.'.'.$resid};
! 3118: untie %bighash;
! 3119: }
! 3120: if ($title) {
! 3121: $titlecache{$symb}=$title;
! 3122: return $title;
! 3123: } else {
! 3124: return &metadata($urlsymb,'title');
! 3125: }
! 3126: }
! 3127:
1.31 www 3128: # ------------------------------------------------- Update symbolic store links
3129:
3130: sub symblist {
3131: my ($mapname,%newhash)=@_;
3132: $mapname=declutter($mapname);
3133: my %hash;
3134: if (($ENV{'request.course.fn'}) && (%newhash)) {
3135: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256 albertel 3136: &GDBM_WRCREAT(),0640)) {
1.191 harris41 3137: foreach (keys %newhash) {
1.211 www 3138: $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
1.191 harris41 3139: }
1.31 www 3140: if (untie(%hash)) {
3141: return 'ok';
3142: }
3143: }
3144: }
3145: return 'error';
1.212 www 3146: }
3147:
3148: # --------------------------------------------------------------- Verify a symb
3149:
3150: sub symbverify {
3151: my ($symb,$thisfn)=@_;
1.213 www 3152: $thisfn=&declutter($thisfn);
1.215 www 3153: # direct jump to resource in page or to a sequence - will construct own symbs
3154: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
3155: # check URL part
1.213 www 3156: my ($map,$resid,$url)=split(/\_\_\_/,$symb);
3157: unless (&symbclean($url) eq &symbclean($thisfn)) { return 0; }
3158:
1.216 www 3159: $symb=&symbclean($symb);
1.213 www 3160:
3161: my %bighash;
3162: my $okay=0;
3163: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256 albertel 3164: &GDBM_READER(),0640)) {
1.280 www 3165: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.216 www 3166: unless ($ids) {
3167: $ids=$bighash{'ids_/'.$thisfn};
3168: }
3169: if ($ids) {
3170: # ------------------------------------------------------------------- Has ID(s)
3171: foreach (split(/\,/,$ids)) {
3172: my ($mapid,$resid)=split(/\./,$_);
3173: if (
3174: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
3175: eq $symb) {
3176: $okay=1;
3177: }
3178: }
3179: }
1.213 www 3180: untie(%bighash);
3181: }
3182: return $okay;
1.31 www 3183: }
3184:
1.210 www 3185: # --------------------------------------------------------------- Clean-up symb
3186:
3187: sub symbclean {
3188: my $symb=shift;
1.213 www 3189:
1.210 www 3190: # remove version from map
3191: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 3192:
1.210 www 3193: # remove version from URL
3194: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 3195:
1.210 www 3196: return $symb;
3197: }
3198:
1.31 www 3199: # ------------------------------------------------------ Return symb list entry
3200:
3201: sub symbread {
1.249 www 3202: my ($thisfn,$donotrecurse)=@_;
1.242 www 3203: # no filename provided? try from environment
1.44 www 3204: unless ($thisfn) {
1.210 www 3205: if ($ENV{'request.symb'}) { return &symbclean($ENV{'request.symb'}); }
1.44 www 3206: $thisfn=$ENV{'request.filename'};
3207: }
1.242 www 3208: # is that filename actually a symb? Verify, clean, and return
3209: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
3210: if (&symbverify($thisfn,$1)) { return &symbclean($thisfn); }
3211: }
1.44 www 3212: $thisfn=declutter($thisfn);
1.31 www 3213: my %hash;
1.37 www 3214: my %bighash;
3215: my $syval='';
1.45 www 3216: if (($ENV{'request.course.fn'}) && ($thisfn)) {
1.31 www 3217: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256 albertel 3218: &GDBM_READER(),0640)) {
1.31 www 3219: $syval=$hash{$thisfn};
1.37 www 3220: untie(%hash);
3221: }
3222: # ---------------------------------------------------------- There was an entry
3223: if ($syval) {
3224: unless ($syval=~/\_\d+$/) {
3225: unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.44 www 3226: &appenv('request.ambiguous' => $thisfn);
1.37 www 3227: return '';
3228: }
3229: $syval.=$1;
3230: }
3231: } else {
3232: # ------------------------------------------------------- Was not in symb table
3233: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256 albertel 3234: &GDBM_READER(),0640)) {
1.37 www 3235: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 3236: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 3237: unless ($ids) {
3238: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 3239: }
3240: unless ($ids) {
3241: # alias?
3242: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 3243: }
1.37 www 3244: if ($ids) {
3245: # ------------------------------------------------------------------- Has ID(s)
3246: my @possibilities=split(/\,/,$ids);
1.39 www 3247: if ($#possibilities==0) {
3248: # ----------------------------------------------- There is only one possibility
1.37 www 3249: my ($mapid,$resid)=split(/\./,$ids);
3250: $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
1.249 www 3251: } elsif (!$donotrecurse) {
1.39 www 3252: # ------------------------------------------ There is more than one possibility
3253: my $realpossible=0;
1.191 harris41 3254: foreach (@possibilities) {
1.39 www 3255: my $file=$bighash{'src_'.$_};
3256: if (&allowed('bre',$file)) {
3257: my ($mapid,$resid)=split(/\./,$_);
3258: if ($bighash{'map_type_'.$mapid} ne 'page') {
3259: $realpossible++;
3260: $syval=declutter($bighash{'map_id_'.$mapid}).
3261: '___'.$resid;
3262: }
3263: }
1.191 harris41 3264: }
1.39 www 3265: if ($realpossible!=1) { $syval=''; }
1.249 www 3266: } else {
3267: $syval='';
1.37 www 3268: }
3269: }
3270: untie(%bighash)
3271: }
1.31 www 3272: }
1.62 www 3273: if ($syval) {
1.210 www 3274: return &symbclean($syval.'___'.$thisfn);
1.62 www 3275: }
1.31 www 3276: }
1.44 www 3277: &appenv('request.ambiguous' => $thisfn);
1.31 www 3278: return '';
3279: }
3280:
3281: # ---------------------------------------------------------- Return random seed
3282:
1.32 www 3283: sub numval {
3284: my $txt=shift;
3285: $txt=~tr/A-J/0-9/;
3286: $txt=~tr/a-j/0-9/;
3287: $txt=~tr/K-T/0-9/;
3288: $txt=~tr/k-t/0-9/;
3289: $txt=~tr/U-Z/0-5/;
3290: $txt=~tr/u-z/0-5/;
3291: $txt=~s/\D//g;
3292: return int($txt);
3293: }
3294:
1.31 www 3295: sub rndseed {
1.155 albertel 3296: my ($symb,$courseid,$domain,$username)=@_;
3297: if (!$symb) {
3298: unless ($symb=&symbread()) { return time; }
3299: }
3300: if (!$courseid) { $courseid=$ENV{'request.course.id'};}
3301: if (!$domain) {$domain=$ENV{'user.domain'};}
3302: if (!$username) {$username=$ENV{'user.name'};}
3303: {
1.98 albertel 3304: use integer;
3305: my $symbchck=unpack("%32C*",$symb) << 27;
1.100 albertel 3306: my $symbseed=numval($symb) << 22;
1.155 albertel 3307: my $namechck=unpack("%32C*",$username) << 17;
3308: my $nameseed=numval($username) << 12;
3309: my $domainseed=unpack("%32C*",$domain) << 7;
3310: my $courseseed=unpack("%32C*",$courseid);
1.98 albertel 3311: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.99 albertel 3312: #uncommenting these lines can break things!
3313: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
3314: #&Apache::lonxml::debug("rndseed :$num:$symb");
1.98 albertel 3315: return $num;
3316: }
1.36 albertel 3317: }
3318:
1.76 www 3319: sub ireceipt {
3320: my ($funame,$fudom,$fucourseid,$fusymb)=@_;
3321: my $cuname=unpack("%32C*",$funame);
3322: my $cudom=unpack("%32C*",$fudom);
3323: my $cucourseid=unpack("%32C*",$fucourseid);
3324: my $cusymb=unpack("%32C*",$fusymb);
1.77 www 3325: my $cunique=unpack("%32C*",$perlvar{'lonReceipt'});
1.76 www 3326: return unpack("%32C*",$perlvar{'lonHostID'}).'-'.
3327: ($cunique%$cuname+
3328: $cunique%$cudom+
3329: $cusymb%$cuname+
3330: $cusymb%$cudom+
3331: $cucourseid%$cuname+
3332: $cucourseid%$cudom);
3333: }
3334:
3335: sub receipt {
1.260 ng 3336: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
3337: return &ireceipt($name,$domain,$courseid,$symb);
1.76 www 3338: }
1.260 ng 3339:
1.36 albertel 3340: # ------------------------------------------------------------ Serves up a file
3341: # returns either the contents of the file or a -1
3342: sub getfile {
1.269 www 3343: my $file=shift;
3344: if ($file=~/^\/*uploaded\//) { # user file
3345: my $ua=new LWP::UserAgent;
3346: my $request=new HTTP::Request('GET',&tokenwrapper($file));
3347: my $response=$ua->request($request);
3348: if ($response->is_success()) {
3349: return $response->content;
3350: } else {
3351: return -1;
3352: }
3353: } else { # normal file from res space
1.37 www 3354: &repcopy($file);
1.36 albertel 3355: if (! -e $file ) { return -1; };
3356: my $fh=Apache::File->new($file);
3357: my $a='';
3358: while (<$fh>) { $a .=$_; }
1.269 www 3359: return $a;
3360: }
1.36 albertel 3361: }
3362:
3363: sub filelocation {
3364: my ($dir,$file) = @_;
3365: my $location;
3366: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.59 albertel 3367: if ($file=~m:^/~:) { # is a contruction space reference
3368: $location = $file;
3369: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.270 www 3370: } elsif ($file=~/^\/*uploaded/) { # is an uploaded file
3371: $location=$file;
1.36 albertel 3372: } else {
1.59 albertel 3373: $file=~s/^$perlvar{'lonDocRoot'}//;
3374: $file=~s:^/*res::;
3375: if ( !( $file =~ m:^/:) ) {
3376: $location = $dir. '/'.$file;
3377: } else {
3378: $location = '/home/httpd/html/res'.$file;
3379: }
1.36 albertel 3380: }
3381: $location=~s://+:/:g; # remove duplicate /
1.46 www 3382: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
3383: return $location;
3384: }
1.36 albertel 3385:
1.46 www 3386: sub hreflocation {
3387: my ($dir,$file)=@_;
1.191 harris41 3388: unless (($file=~/^http:\/\//i) || ($file=~/^\//)) {
1.46 www 3389: my $finalpath=filelocation($dir,$file);
3390: $finalpath=~s/^\/home\/httpd\/html//;
1.225 albertel 3391: $finalpath=~s-/home/(\w+)/public_html/-/~$1/-;
1.46 www 3392: return $finalpath;
3393: } else {
3394: return $file;
3395: }
1.31 www 3396: }
3397:
3398: # ------------------------------------------------------------- Declutters URLs
3399:
3400: sub declutter {
3401: my $thisfn=shift;
3402: $thisfn=~s/^$perlvar{'lonDocRoot'}//;
3403: $thisfn=~s/^\///;
3404: $thisfn=~s/^res\///;
1.235 www 3405: $thisfn=~s/\?.+$//;
1.268 www 3406: return $thisfn;
3407: }
3408:
3409: # ------------------------------------------------------------- Clutter up URLs
3410:
3411: sub clutter {
3412: my $thisfn='/'.&declutter(shift);
1.270 www 3413: unless ($thisfn=~/^\/(uploaded|adm|userfiles|ext|raw|priv)\//) {
3414: $thisfn='/res'.$thisfn;
3415: }
1.31 www 3416: return $thisfn;
1.12 www 3417: }
3418:
3419: # -------------------------------------------------------- Escape Special Chars
3420:
3421: sub escape {
3422: my $str=shift;
3423: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
3424: return $str;
3425: }
3426:
3427: # ----------------------------------------------------- Un-Escape Special Chars
3428:
3429: sub unescape {
3430: my $str=shift;
3431: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
3432: return $str;
3433: }
1.11 www 3434:
1.1 albertel 3435: # ================================================================ Main Program
3436:
1.184 www 3437: sub goodbye {
1.204 albertel 3438: &logthis("Starting Shut down");
1.184 www 3439: &flushcourselogs();
3440: &logthis("Shutting down");
3441: }
3442:
1.179 www 3443: BEGIN {
1.228 harris41 3444: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
1.195 www 3445: unless ($readit) {
1.217 harris41 3446: {
3447: my $config=Apache::File->new("/etc/httpd/conf/loncapa.conf");
3448:
3449: while (my $configline=<$config>) {
3450: if ($configline =~ /^[^\#]*PerlSetVar/) {
1.1 albertel 3451: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8 www 3452: chomp($varvalue);
1.1 albertel 3453: $perlvar{$varname}=$varvalue;
3454: }
3455: }
3456: }
1.227 harris41 3457: {
3458: my $config=Apache::File->new("/etc/httpd/conf/loncapa_apache.conf");
3459:
3460: while (my $configline=<$config>) {
3461: if ($configline =~ /^[^\#]*PerlSetVar/) {
3462: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
3463: chomp($varvalue);
3464: $perlvar{$varname}=$varvalue;
3465: }
3466: }
3467: }
1.1 albertel 3468:
3469: # ------------------------------------------------------------- Read hosts file
3470: {
3471: my $config=Apache::File->new("$perlvar{'lonTabDir'}/hosts.tab");
3472:
3473: while (my $configline=<$config>) {
1.154 www 3474: chomp($configline);
1.245 www 3475: my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
1.252 albertel 3476: if ($id && $domain && $role && $name && $ip) {
3477: $hostname{$id}=$name;
3478: $hostdom{$id}=$domain;
3479: $hostip{$id}=$ip;
1.300 albertel 3480: $iphost{$ip}=$id;
1.252 albertel 3481: if ($domdescr) { $domaindescription{$domain}=$domdescr; }
3482: if ($role eq 'library') { $libserv{$id}=$name; }
3483: } else {
3484: if ($configline) {
3485: &logthis("Skipping hosts.tab line -$configline-");
3486: }
1.245 www 3487: }
1.1 albertel 3488: }
3489: }
3490:
3491: # ------------------------------------------------------ Read spare server file
3492: {
3493: my $config=Apache::File->new("$perlvar{'lonTabDir'}/spare.tab");
3494:
3495: while (my $configline=<$config>) {
3496: chomp($configline);
1.284 matthew 3497: if ($configline) {
1.1 albertel 3498: $spareid{$configline}=1;
3499: }
3500: }
3501: }
1.11 www 3502: # ------------------------------------------------------------ Read permissions
3503: {
3504: my $config=Apache::File->new("$perlvar{'lonTabDir'}/roles.tab");
3505:
3506: while (my $configline=<$config>) {
3507: chomp($configline);
1.160 www 3508: if ($configline) {
1.11 www 3509: my ($role,$perm)=split(/ /,$configline);
3510: if ($perm ne '') { $pr{$role}=$perm; }
1.160 www 3511: }
1.11 www 3512: }
3513: }
3514:
3515: # -------------------------------------------- Read plain texts for permissions
3516: {
3517: my $config=Apache::File->new("$perlvar{'lonTabDir'}/rolesplain.tab");
3518:
3519: while (my $configline=<$config>) {
3520: chomp($configline);
1.160 www 3521: if ($configline) {
1.11 www 3522: my ($short,$plain)=split(/:/,$configline);
3523: if ($plain ne '') { $prp{$short}=$plain; }
1.160 www 3524: }
1.135 www 3525: }
3526: }
3527:
3528: # ---------------------------------------------------------- Read package table
3529: {
3530: my $config=Apache::File->new("$perlvar{'lonTabDir'}/packages.tab");
3531:
3532: while (my $configline=<$config>) {
3533: chomp($configline);
3534: my ($short,$plain)=split(/:/,$configline);
1.143 www 3535: my ($pack,$name)=split(/\&/,$short);
3536: if ($plain ne '') {
3537: $packagetab{$pack.'&'.$name.'&name'}=$name;
3538: $packagetab{$short}=$plain;
1.25 www 3539: }
1.11 www 3540: }
3541: }
3542:
1.71 www 3543: %metacache=();
1.185 www 3544:
1.281 www 3545: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 3546: $dumpcount=0;
1.22 www 3547:
1.163 harris41 3548: &logtouch();
1.12 www 3549: &logthis('<font color=yellow>INFO: Read configuration</font>');
1.195 www 3550: $readit=1;
3551: }
1.1 albertel 3552: }
1.179 www 3553:
1.1 albertel 3554: 1;
1.191 harris41 3555: __END__
3556:
1.243 albertel 3557: =pod
3558:
1.191 harris41 3559: =head1 NAME
3560:
1.243 albertel 3561: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 3562:
3563: =head1 SYNOPSIS
3564:
1.243 albertel 3565: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 3566:
3567: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
3568:
1.243 albertel 3569: Common parameters:
3570:
3571: =over 4
3572:
3573: =item *
3574:
3575: $uname : an internal username (if $cname expecting a course Id specifically)
3576:
3577: =item *
3578:
3579: $udom : a domain (if $cdom expecting a course's domain specifically)
3580:
3581: =item *
3582:
3583: $symb : a resource instance identifier
3584:
3585: =item *
3586:
3587: $namespace : the name of a .db file that contains the data needed or
3588: being set.
3589:
3590: =back
3591:
1.191 harris41 3592: =head1 INTRODUCTION
3593:
3594: This module provides subroutines which interact with the
1.243 albertel 3595: lonc/lond (TCP) network layer of LON-CAPA. And Can be used to ask about
3596: - classes
3597: - users
3598: - resources
3599:
3600: For many of these objects you can also use this to store data about
3601: them or modify them in various ways.
1.191 harris41 3602:
3603: This is part of the LearningOnline Network with CAPA project
3604: described at http://www.lon-capa.org.
3605:
1.243 albertel 3606: =head1 RETURN MESSAGES
1.191 harris41 3607:
3608: =over 4
3609:
3610: =item *
3611:
1.243 albertel 3612: con_lost : unable to contact remote host
1.191 harris41 3613:
3614: =item *
3615:
1.243 albertel 3616: con_delayed : unable to contact remote host, message will be delivered
3617: when the connection is brought back up
1.191 harris41 3618:
3619: =item *
3620:
1.243 albertel 3621: con_failed : unable to contact remote host and unable to save message
3622: for later delivery
1.191 harris41 3623:
3624: =item *
3625:
1.243 albertel 3626: error: : an error a occured, a description of the error follows the :
1.191 harris41 3627:
3628: =item *
3629:
1.243 albertel 3630: no_such_host : unable to fund a host associated with the user/domain
3631: that was requested
1.191 harris41 3632:
1.243 albertel 3633: =back
1.191 harris41 3634:
1.243 albertel 3635: =head1 PUBLIC SUBROUTINES
1.191 harris41 3636:
1.243 albertel 3637: =head2 Session Environment Functions
1.191 harris41 3638:
1.243 albertel 3639: =over 4
1.191 harris41 3640:
3641: =item *
3642:
1.243 albertel 3643: appenv(%hash) : the value of %hash is written to the user envirnoment
3644: file, and will be restored for each access this user makes during this
3645: session, also modifies the %ENV for the current process
1.191 harris41 3646:
3647: =item *
3648:
1.243 albertel 3649: delenv($regexp) : removes all items from the session environment file that matches the regular expression in $regexp. The values are also delted from the current processes %ENV.
1.191 harris41 3650:
1.243 albertel 3651: =back
3652:
3653: =head2 User Information
1.191 harris41 3654:
1.243 albertel 3655: =over 4
1.191 harris41 3656:
3657: =item *
3658:
3659: queryauthenticate($uname,$udom) : try to determine user's current
3660: authentication scheme
3661:
3662: =item *
3663:
3664: authenticate($uname,$upass,$udom) : try to authenticate user from domain's lib
1.243 albertel 3665: servers (first use the current one), $upass should be the users password
1.191 harris41 3666:
3667: =item *
3668:
1.243 albertel 3669: homeserver($uname,$udom) : find the server which has the user's
3670: directory and files (there must be only one), this caches the answer,
3671: and also caches if there is a borken connection.
1.191 harris41 3672:
3673: =item *
3674:
1.243 albertel 3675: idget($udom,@ids) : find the usernames behind a list of IDs (IDs are a
3676: unique resource in a domain, there must be only 1 ID per username, and
3677: only 1 username per ID in a specific domain) (returns hash:
1.191 harris41 3678: id=>name,id=>name)
3679:
3680: =item *
3681:
3682: idrget($udom,@unames) : find the IDs behind a list of usernames (returns hash:
3683: name=>id,name=>id)
3684:
3685: =item *
3686:
3687: idput($udom,%ids) : store away a list of names and associated IDs
3688:
3689: =item *
3690:
1.243 albertel 3691: rolesinit($udom,$username,$authhost) : get user privileges
3692:
3693: =item *
3694:
3695: usection($udom,$uname,$cname) : finds the section of student in the
3696: course $cname, return section name/number or '' for "not in course"
3697: and '-1' for "no section"
3698:
3699: =item *
3700:
3701: userenvironment($udom,$uname,@what) : gets the values of the keys
3702: passed in @what from the requested user's environment, returns a hash
3703:
3704: =back
3705:
3706: =head2 User Roles
3707:
3708: =over 4
3709:
3710: =item *
3711:
3712: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
3713: actions
3714: F: full access
3715: U,I,K: authentication modes (cxx only)
3716: '': forbidden
3717: 1: user needs to choose course
3718: 2: browse allowed
3719:
3720: =item *
3721:
3722: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
3723: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
3724: and course level
3725:
3726: =item *
3727:
3728: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
3729: explanation of a user role term
3730:
3731: =back
3732:
3733: =head2 User Modification
3734:
3735: =over 4
3736:
3737: =item *
3738:
3739: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
3740: user for the level given by URL. Optional start and end dates (leave empty
3741: string or zero for "no date")
1.191 harris41 3742:
3743: =item *
3744:
1.243 albertel 3745: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
3746: change a users, password, possible return values are: ok,
3747: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
3748: refused
1.191 harris41 3749:
3750: =item *
3751:
1.243 albertel 3752: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 3753:
3754: =item *
3755:
1.243 albertel 3756: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
3757: modify user
1.191 harris41 3758:
3759: =item *
3760:
1.286 matthew 3761: modifystudent
3762:
3763: modify a students enrollment and identification information.
3764: The course id is resolved based on the current users environment.
3765: This means the envoking user must be a course coordinator or otherwise
3766: associated with a course.
3767:
1.297 matthew 3768: This call is essentially a wrapper for lonnet::modifyuser and
3769: lonnet::modify_student_enrollment
1.286 matthew 3770:
3771: Inputs:
3772:
3773: =over 4
3774:
3775: =item B<$udom> Students loncapa domain
3776:
3777: =item B<$uname> Students loncapa login name
3778:
3779: =item B<$uid> Students id/student number
3780:
3781: =item B<$umode> Students authentication mode
3782:
3783: =item B<$upass> Students password
3784:
3785: =item B<$first> Students first name
3786:
3787: =item B<$middle> Students middle name
3788:
3789: =item B<$last> Students last name
3790:
3791: =item B<$gene> Students generation
3792:
3793: =item B<$usec> Students section in course
3794:
3795: =item B<$end> Unix time of the roles expiration
3796:
3797: =item B<$start> Unix time of the roles start date
3798:
3799: =item B<$forceid> If defined, allow $uid to be changed
3800:
3801: =item B<$desiredhome> server to use as home server for student
3802:
3803: =back
1.297 matthew 3804:
3805: =item *
3806:
3807: modify_student_enrollment
3808:
3809: Change a students enrollment status in a class. The environment variable
3810: 'role.request.course' must be defined for this function to proceed.
3811:
3812: Inputs:
3813:
3814: =over 4
3815:
3816: =item $udom, students domain
3817:
3818: =item $uname, students name
3819:
3820: =item $uid, students user id
3821:
3822: =item $first, students first name
3823:
3824: =item $middle
3825:
3826: =item $last
3827:
3828: =item $gene
3829:
3830: =item $usec
3831:
3832: =item $end
3833:
3834: =item $start
3835:
3836: =back
3837:
1.191 harris41 3838:
3839: =item *
3840:
1.243 albertel 3841: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
3842: custom role; give a custom role to a user for the level given by URL. Specify
3843: name and domain of role author, and role name
1.191 harris41 3844:
3845: =item *
3846:
1.243 albertel 3847: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 3848:
3849: =item *
3850:
1.243 albertel 3851: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
3852:
3853: =back
3854:
3855: =head2 Course Infomation
3856:
3857: =over 4
1.191 harris41 3858:
3859: =item *
3860:
1.243 albertel 3861: coursedescription($courseid) : course description
1.191 harris41 3862:
3863: =item *
3864:
1.243 albertel 3865: courseresdata($coursenum,$coursedomain,@which) : request for current
3866: parameter setting for a specific course, @what should be a list of
3867: parameters to ask about. This routine caches answers for 5 minutes.
3868:
3869: =back
3870:
3871: =head2 Course Modification
3872:
3873: =over 4
1.191 harris41 3874:
3875: =item *
3876:
1.243 albertel 3877: writecoursepref($courseid,%prefs) : write preferences (environment
3878: database) for a course
1.191 harris41 3879:
3880: =item *
3881:
1.243 albertel 3882: createcourse($udom,$description,$url) : make/modify course
3883:
3884: =back
3885:
3886: =head2 Resource Subroutines
3887:
3888: =over 4
1.191 harris41 3889:
3890: =item *
3891:
1.243 albertel 3892: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 3893:
3894: =item *
3895:
1.243 albertel 3896: repcopy($filename) : subscribes to the requested file, and attempts to
3897: replicate from the owning library server, Might return
3898: HTTP_SERVICE_UNAVAILABLE, HTTP_NOT_FOUND, FORBIDDEN, OK, or
3899: HTTP_BAD_REQUEST, also attempts to grab the metadata for the
3900: resource. Expects the local filesystem pathname
3901: (/home/httpd/html/res/....)
3902:
3903: =back
3904:
3905: =head2 Resource Information
3906:
3907: =over 4
1.191 harris41 3908:
3909: =item *
3910:
1.243 albertel 3911: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
3912: a vairety of different possible values, $varname should be a request
3913: string, and the other parameters can be used to specify who and what
3914: one is asking about.
3915:
3916: Possible values for $varname are environment.lastname (or other item
3917: from the envirnment hash), user.name (or someother aspect about the
3918: user), resource.0.maxtries (or some other part and parameter of a
3919: resource)
1.204 albertel 3920:
3921: =item *
3922:
1.243 albertel 3923: directcondval($number) : get current value of a condition; reads from a state
3924: string
1.204 albertel 3925:
3926: =item *
3927:
1.243 albertel 3928: condval($condidx) : value of condition index based on state
1.204 albertel 3929:
3930: =item *
3931:
1.243 albertel 3932: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
3933: resource's metadata, $what should be either a specific key, or either
3934: 'keys' (to get a list of possible keys) or 'packages' to get a list of
3935: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
3936:
3937: this function automatically caches all requests
1.191 harris41 3938:
3939: =item *
3940:
1.243 albertel 3941: metadata_query($query,$custom,$customshow) : make a metadata query against the
3942: network of library servers; returns file handle of where SQL and regex results
3943: will be stored for query
1.191 harris41 3944:
3945: =item *
3946:
1.243 albertel 3947: symbread($filename) : return symbolic list entry (filename argument optional);
3948: returns the data handle
1.191 harris41 3949:
3950: =item *
3951:
1.243 albertel 3952: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
3953: a possible symb for the URL in $thisfn, returns a 1 on success, 0 on
3954: failure, user must be in a course, as it assumes the existance of the
3955: course initi hash, and uses $ENV('request.course.id'}
3956:
1.191 harris41 3957:
3958: =item *
3959:
1.243 albertel 3960: symbclean($symb) : removes versions numbers from a symb, returns the
3961: cleaned symb
1.191 harris41 3962:
3963: =item *
3964:
1.243 albertel 3965: is_on_map($uri) : checks if the $uri is somewhere on the current
3966: course map, user must be in a course for it to work.
1.191 harris41 3967:
3968: =item *
3969:
1.243 albertel 3970: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 3971:
3972: =item *
3973:
1.243 albertel 3974: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
3975: a random seed, all arguments are optional, if they aren't sent it uses the
3976: environment to derive them. Note: if symb isn't sent and it can't get one
3977: from &symbread it will use the current time as its return value
1.191 harris41 3978:
3979: =item *
3980:
1.243 albertel 3981: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
3982: unfakeable, receipt
1.191 harris41 3983:
3984: =item *
3985:
1.243 albertel 3986: receipt() : API to ireceipt working off of ENV values; given out to users
1.191 harris41 3987:
3988: =item *
3989:
1.243 albertel 3990: countacc($url) : count the number of accesses to a given URL
1.191 harris41 3991:
3992: =item *
3993:
1.243 albertel 3994: 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 3995:
3996: =item *
3997:
1.243 albertel 3998: 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 3999:
4000: =item *
4001:
1.243 albertel 4002: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 4003:
4004: =item *
4005:
1.243 albertel 4006: devalidate($symb) : devalidate temporary spreadsheet calculations,
4007: forcing spreadsheet to reevaluate the resource scores next time.
4008:
4009: =back
4010:
4011: =head2 Storing/Retreiving Data
4012:
4013: =over 4
1.191 harris41 4014:
4015: =item *
4016:
1.243 albertel 4017: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
4018: for this url; hashref needs to be given and should be a \%hashname; the
4019: remaining args aren't required and if they aren't passed or are '' they will
4020: be derived from the ENV
1.191 harris41 4021:
4022: =item *
4023:
1.243 albertel 4024: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
4025: uses critical subroutine
1.191 harris41 4026:
4027: =item *
4028:
1.243 albertel 4029: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
4030: all args are optional
1.191 harris41 4031:
4032: =item *
4033:
1.243 albertel 4034: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
4035: works very similar to store/cstore, but all data is stored in a
4036: temporary location and can be reset using tmpreset, $storehash should
4037: be a hash reference, returns nothing on success
1.191 harris41 4038:
4039: =item *
4040:
1.243 albertel 4041: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
4042: similar to restore, but all data is stored in a temporary location and
4043: can be reset using tmpreset. Returns a hash of values on success,
4044: error string otherwise.
1.191 harris41 4045:
4046: =item *
4047:
1.243 albertel 4048: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
4049: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 4050:
4051: =item *
4052:
1.243 albertel 4053: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
4054: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 4055:
4056: =item *
4057:
1.243 albertel 4058: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
4059: namesp ($udom and $uname are optional)
1.191 harris41 4060:
4061: =item *
4062:
1.243 albertel 4063: dump($namespace,$udom,$uname,$regexp) :
4064: dumps the complete (or key matching regexp) namespace into a hash
4065: ($udom, $uname and $regexp are optional)
1.191 harris41 4066:
4067: =item *
4068:
1.243 albertel 4069: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
4070: ($udom and $uname are optional)
1.191 harris41 4071:
4072: =item *
4073:
1.243 albertel 4074: cput($namespace,$storehash,$udom,$uname) : critical put
4075: ($udom and $uname are optional)
1.191 harris41 4076:
4077: =item *
4078:
1.243 albertel 4079: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
4080: reference filled in from namesp (encrypts the return communication)
4081: ($udom and $uname are optional)
1.191 harris41 4082:
4083: =item *
4084:
1.243 albertel 4085: log($udom,$name,$home,$message) : write to permanent log for user; use
4086: critical subroutine
4087:
4088: =back
4089:
4090: =head2 Network Status Functions
4091:
4092: =over 4
1.191 harris41 4093:
4094: =item *
4095:
4096: dirlist($uri) : return directory list based on URI
4097:
4098: =item *
4099:
1.243 albertel 4100: spareserver() : find server with least workload from spare.tab
4101:
4102: =back
4103:
4104: =head2 Apache Request
4105:
4106: =over 4
1.191 harris41 4107:
4108: =item *
4109:
1.243 albertel 4110: ssi($url,%hash) : server side include, does a complete request cycle on url to
4111: localhost, posts hash
4112:
4113: =back
4114:
4115: =head2 Data to String to Data
4116:
4117: =over 4
1.191 harris41 4118:
4119: =item *
4120:
1.243 albertel 4121: hash2str(%hash) : convert a hash into a string complete with escaping and '='
4122: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 4123:
4124: =item *
4125:
1.243 albertel 4126: hashref2str($hashref) : convert a hashref into a string complete with
4127: escaping and '=' and '&' separators, supports elements that are
4128: arrayrefs and hashrefs
1.191 harris41 4129:
4130: =item *
4131:
1.243 albertel 4132: arrayref2str($arrayref) : convert an arrayref into a string complete
4133: with escaping and '&' separators, supports elements that are arrayrefs
4134: and hashrefs
1.191 harris41 4135:
4136: =item *
4137:
1.243 albertel 4138: str2hash($string) : convert string to hash using unescaping and
4139: splitting on '=' and '&', supports elements that are arrayrefs and
4140: hashrefs
1.191 harris41 4141:
4142: =item *
4143:
1.243 albertel 4144: str2array($string) : convert string to hash using unescaping and
4145: splitting on '&', supports elements that are arrayrefs and hashrefs
4146:
4147: =back
4148:
4149: =head2 Logging Routines
4150:
4151: =over 4
4152:
4153: These routines allow one to make log messages in the lonnet.log and
4154: lonnet.perm logfiles.
1.191 harris41 4155:
4156: =item *
4157:
1.243 albertel 4158: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 4159:
4160: =item *
4161:
1.243 albertel 4162: logthis() : append message to the normal lonnet.log file, it gets
4163: preiodically rolled over and deleted.
1.191 harris41 4164:
4165: =item *
4166:
1.243 albertel 4167: logperm() : append a permanent message to lonnet.perm.log, this log
4168: file never gets deleted by any automated portion of the system, only
4169: messages of critical importance should go in here.
4170:
4171: =back
4172:
4173: =head2 General File Helper Routines
4174:
4175: =over 4
1.191 harris41 4176:
4177: =item *
4178:
1.243 albertel 4179: getfile($file) : returns the entire contents of a file or -1; it
4180: properly subscribes to and replicates the file if neccessary.
1.191 harris41 4181:
4182: =item *
4183:
1.243 albertel 4184: filelocation($dir,$file) : returns file system location of a file
4185: based on URI; meant to be "fairly clean" absolute reference, $dir is a
4186: directory that relative $file lookups are to looked in ($dir of /a/dir
4187: and a file of ../bob will become /a/bob)
1.191 harris41 4188:
4189: =item *
4190:
4191: hreflocation($dir,$file) : returns file system location or a URL; same as
4192: filelocation except for hrefs
4193:
4194: =item *
4195:
4196: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
4197:
1.243 albertel 4198: =back
4199:
4200: =head2 HTTP Helper Routines
4201:
4202: =over 4
4203:
1.191 harris41 4204: =item *
4205:
4206: escape() : unpack non-word characters into CGI-compatible hex codes
4207:
4208: =item *
4209:
4210: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
4211:
1.243 albertel 4212: =back
4213:
4214: =head1 PRIVATE SUBROUTINES
4215:
4216: =head2 Underlying communication routines (Shouldn't call)
4217:
4218: =over 4
4219:
4220: =item *
4221:
4222: subreply() : tries to pass a message to lonc, returns con_lost if incapable
4223:
4224: =item *
4225:
4226: reply() : uses subreply to send a message to remote machine, logs all failures
4227:
4228: =item *
4229:
4230: critical() : passes a critical message to another server; if cannot
4231: get through then place message in connection buffer directory and
4232: returns con_delayed, if incapable of saving message, returns
4233: con_failed
4234:
4235: =item *
4236:
4237: reconlonc() : tries to reconnect lonc client processes.
4238:
4239: =back
4240:
4241: =head2 Resource Access Logging
4242:
4243: =over 4
4244:
4245: =item *
4246:
4247: flushcourselogs() : flush (save) buffer logs and access logs
4248:
4249: =item *
4250:
4251: courselog($what) : save message for course in hash
4252:
4253: =item *
4254:
4255: courseacclog($what) : save message for course using &courselog(). Perform
4256: special processing for specific resource types (problems, exams, quizzes, etc).
4257:
1.191 harris41 4258: =item *
4259:
4260: goodbye() : flush course logs and log shutting down; it is called in srm.conf
4261: as a PerlChildExitHandler
1.243 albertel 4262:
4263: =back
4264:
4265: =head2 Other
4266:
4267: =over 4
4268:
4269: =item *
4270:
4271: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 4272:
4273: =back
4274:
4275: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>