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