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