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