Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.865
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.865 ! raeburn 4: # $Id: lonnet.pm,v 1.864 2007/04/04 00:48:23 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.847 albertel 38: qw(%perlvar %badServerCache %spareid
1.845 albertel 39: %pr %prp $memcache %packagetab
1.662 raeburn 40: %courselogs %accesshash %userrolehash %domainrolehash $processmarker $dumpcount
1.741 raeburn 41: %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseownerbuf %coursetypebuf
1.685 raeburn 42: $tmpdir $_64bit %env);
1.403 www 43:
1.1 albertel 44: use IO::Socket;
1.31 www 45: use GDBM_File;
1.208 albertel 46: use HTML::LCParser;
1.637 raeburn 47: use HTML::Parser;
1.88 www 48: use Fcntl qw(:flock);
1.557 albertel 49: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
1.539 albertel 50: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 51: use Cache::Memcached;
1.676 albertel 52: use Digest::MD5;
1.790 albertel 53: use Math::Random;
1.807 albertel 54: use LONCAPA qw(:DEFAULT :match);
1.740 www 55: use LONCAPA::Configuration;
1.676 albertel 56:
1.195 www 57: my $readit;
1.550 foxr 58: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 59:
1.619 albertel 60: require Exporter;
61:
62: our @ISA = qw (Exporter);
63: our @EXPORT = qw(%env);
64:
1.449 matthew 65: =pod
66:
67: =head1 Package Variables
68:
69: These are largely undocumented, so if you decipher one please note it here.
70:
71: =over 4
72:
73: =item $processmarker
74:
75: Contains the time this process was started and this servers host id.
76:
77: =item $dumpcount
78:
79: Counts the number of times a message log flush has been attempted (regardless
80: of success) by this process. Used as part of the filename when messages are
81: delayed.
82:
83: =back
84:
85: =cut
86:
87:
1.1 albertel 88: # --------------------------------------------------------------------- Logging
1.729 www 89: {
90: my $logid;
91: sub instructor_log {
92: my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
93: $logid++;
94: my $id=time().'00000'.$$.'00000'.$logid;
95: return &Apache::lonnet::put('nohist_'.$hash_name,
1.730 www 96: { $id => {
97: 'exe_uname' => $env{'user.name'},
98: 'exe_udom' => $env{'user.domain'},
99: 'exe_time' => time(),
100: 'exe_ip' => $ENV{'REMOTE_ADDR'},
101: 'delflag' => $delflag,
102: 'logentry' => $storehash,
103: 'uname' => $uname,
104: 'udom' => $udom,
105: }
106: },
1.729 www 107: $env{'course.'.$env{'request.course.id'}.'.domain'},
108: $env{'course.'.$env{'request.course.id'}.'.num'}
109: );
110: }
111: }
1.1 albertel 112:
1.163 harris41 113: sub logtouch {
114: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 115: unless (-e "$execdir/logs/lonnet.log") {
116: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 117: close $fh;
118: }
119: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
120: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
121: }
122:
1.1 albertel 123: sub logthis {
124: my $message=shift;
125: my $execdir=$perlvar{'lonDaemons'};
126: my $now=time;
127: my $local=localtime($now);
1.448 albertel 128: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
129: print $fh "$local ($$): $message\n";
130: close($fh);
131: }
1.1 albertel 132: return 1;
133: }
134:
135: sub logperm {
136: my $message=shift;
137: my $execdir=$perlvar{'lonDaemons'};
138: my $now=time;
139: my $local=localtime($now);
1.448 albertel 140: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
141: print $fh "$now:$message:$local\n";
142: close($fh);
143: }
1.1 albertel 144: return 1;
145: }
146:
1.850 albertel 147: sub create_connection {
1.853 albertel 148: my ($hostname,$lonid) = @_;
1.851 albertel 149: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 150: Type => SOCK_STREAM,
151: Timeout => 10);
152: return 0 if (!$client);
1.854 albertel 153: print $client (join(':',$hostname,$lonid,&machine_ids($lonid))."\n");
1.850 albertel 154: my $result = <$client>;
155: chomp($result);
156: return 1 if ($result eq 'done');
157: return 0;
158: }
159:
160:
1.1 albertel 161: # -------------------------------------------------- Non-critical communication
162: sub subreply {
163: my ($cmd,$server)=@_;
1.838 albertel 164: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 165: #
166: # With loncnew process trimming, there's a timing hole between lonc server
167: # process exit and the master server picking up the listen on the AF_UNIX
168: # socket. In that time interval, a lock file will exist:
169:
170: my $lockfile=$peerfile.".lock";
171: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
172: sleep(1);
173: }
174: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 175: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 176: #
1.550 foxr 177: # We'll give the connection a few tries before abandoning it. If
178: # connection is not possible, we'll con_lost back to the client.
179: #
180: my $client;
181: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
182: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
183: Type => SOCK_STREAM,
184: Timeout => 10);
185: if($client) {
186: last; # Connected!
1.850 albertel 187: } else {
1.853 albertel 188: &create_connection(&hostname($server),$server);
1.550 foxr 189: }
1.850 albertel 190: sleep(1); # Try again later if failed connection.
1.550 foxr 191: }
192: my $answer;
193: if ($client) {
1.704 albertel 194: print $client "sethost:$server:$cmd\n";
1.550 foxr 195: $answer=<$client>;
196: if (!$answer) { $answer="con_lost"; }
197: chomp($answer);
198: } else {
199: $answer = 'con_lost'; # Failed connection.
200: }
1.1 albertel 201: return $answer;
202: }
203:
204: sub reply {
205: my ($cmd,$server)=@_;
1.838 albertel 206: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 207: my $answer=subreply($cmd,$server);
1.65 www 208: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 209: &logthis("<font color=\"blue\">WARNING:".
1.12 www 210: " $cmd to $server returned $answer</font>");
211: }
1.1 albertel 212: return $answer;
213: }
214:
215: # ----------------------------------------------------------- Send USR1 to lonc
216:
217: sub reconlonc {
1.836 www 218: &logthis("Trying to reconnect lonc");
1.1 albertel 219: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 220: if (open(my $fh,"<$loncfile")) {
1.1 albertel 221: my $loncpid=<$fh>;
222: chomp($loncpid);
223: if (kill 0 => $loncpid) {
224: &logthis("lonc at pid $loncpid responding, sending USR1");
225: kill USR1 => $loncpid;
226: sleep 1;
1.836 www 227: } else {
1.12 www 228: &logthis(
1.672 albertel 229: "<font color=\"blue\">WARNING:".
1.12 www 230: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 231: }
232: } else {
1.836 www 233: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 234: }
235: }
236:
237: # ------------------------------------------------------ Critical communication
1.12 www 238:
1.1 albertel 239: sub critical {
240: my ($cmd,$server)=@_;
1.838 albertel 241: unless (&hostname($server)) {
1.672 albertel 242: &logthis("<font color=\"blue\">WARNING:".
1.89 www 243: " Critical message to unknown server ($server)</font>");
244: return 'no_such_host';
245: }
1.1 albertel 246: my $answer=reply($cmd,$server);
247: if ($answer eq 'con_lost') {
248: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 249: my $answer=reply($cmd,$server);
1.1 albertel 250: if ($answer eq 'con_lost') {
251: my $now=time;
252: my $middlename=$cmd;
1.5 www 253: $middlename=substr($middlename,0,16);
1.1 albertel 254: $middlename=~s/\W//g;
255: my $dfilename=
1.305 www 256: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
257: $dumpcount++;
1.1 albertel 258: {
1.448 albertel 259: my $dfh;
260: if (open($dfh,">$dfilename")) {
261: print $dfh "$cmd\n";
262: close($dfh);
263: }
1.1 albertel 264: }
265: sleep 2;
266: my $wcmd='';
267: {
1.448 albertel 268: my $dfh;
269: if (open($dfh,"<$dfilename")) {
270: $wcmd=<$dfh>;
271: close($dfh);
272: }
1.1 albertel 273: }
274: chomp($wcmd);
1.7 www 275: if ($wcmd eq $cmd) {
1.672 albertel 276: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 277: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 278: &logperm("D:$server:$cmd");
279: return 'con_delayed';
280: } else {
1.672 albertel 281: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 282: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 283: &logperm("F:$server:$cmd");
284: return 'con_failed';
285: }
286: }
287: }
288: return $answer;
1.405 albertel 289: }
290:
1.755 albertel 291: # ------------------------------------------- check if return value is an error
292:
293: sub error {
294: my ($result) = @_;
1.756 albertel 295: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 296: if ($2 == 2) { return undef; }
297: return $1;
298: }
299: return undef;
300: }
301:
1.783 albertel 302: sub convert_and_load_session_env {
303: my ($lonidsdir,$handle)=@_;
304: my @profile;
305: {
306: open(my $idf,"$lonidsdir/$handle.id");
307: flock($idf,LOCK_SH);
308: @profile=<$idf>;
309: close($idf);
310: }
311: my %temp_env;
312: foreach my $line (@profile) {
1.786 albertel 313: if ($line !~ m/=/) {
314: return 0;
315: }
1.783 albertel 316: chomp($line);
317: my ($envname,$envvalue)=split(/=/,$line,2);
318: $temp_env{&unescape($envname)} = &unescape($envvalue);
319: }
320: unlink("$lonidsdir/$handle.id");
321: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
322: 0640)) {
323: %disk_env = %temp_env;
324: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
325: untie(%disk_env);
326: }
1.786 albertel 327: return 1;
1.783 albertel 328: }
329:
1.374 www 330: # ------------------------------------------- Transfer profile into environment
1.780 albertel 331: my $env_loaded;
332: sub transfer_profile_to_env {
1.788 albertel 333: my ($lonidsdir,$handle,$force_transfer) = @_;
334: if (!$force_transfer && $env_loaded) { return; }
1.374 www 335:
1.720 albertel 336: if (!defined($lonidsdir)) {
337: $lonidsdir = $perlvar{'lonIDsDir'};
338: }
339: if (!defined($handle)) {
340: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
341: }
342:
1.786 albertel 343: my $convert;
344: {
345: open(my $idf,"$lonidsdir/$handle.id");
346: flock($idf,LOCK_SH);
347: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
348: &GDBM_READER(),0640)) {
349: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
350: untie(%disk_env);
351: } else {
352: $convert = 1;
353: }
354: }
355: if ($convert) {
356: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
357: &logthis("Failed to load session, or convert session.");
358: }
1.374 www 359: }
1.783 albertel 360:
1.786 albertel 361: my %remove;
1.783 albertel 362: while ( my $envname = each(%env) ) {
1.433 matthew 363: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
364: if ($time < time-300) {
1.783 albertel 365: $remove{$key}++;
1.433 matthew 366: }
367: }
368: }
1.783 albertel 369:
1.619 albertel 370: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 371: $env_loaded=1;
1.783 albertel 372: foreach my $expired_key (keys(%remove)) {
1.433 matthew 373: &delenv($expired_key);
1.374 www 374: }
1.1 albertel 375: }
376:
1.830 albertel 377: sub timed_flock {
378: my ($file,$lock_type) = @_;
379: my $failed=0;
380: eval {
381: local $SIG{__DIE__}='DEFAULT';
382: local $SIG{ALRM}=sub {
383: $failed=1;
384: die("failed lock");
385: };
386: alarm(13);
387: flock($file,$lock_type);
388: alarm(0);
389: };
390: if ($failed) {
391: return undef;
392: } else {
393: return 1;
394: }
395: }
396:
1.5 www 397: # ---------------------------------------------------------- Append Environment
398:
399: sub appenv {
1.6 www 400: my %newenv=@_;
1.692 albertel 401: foreach my $key (keys(%newenv)) {
402: if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
1.672 albertel 403: &logthis("<font color=\"blue\">WARNING: ".
1.692 albertel 404: "Attempt to modify environment ".$key." to ".$newenv{$key}
1.151 www 405: .'</font>');
1.692 albertel 406: delete($newenv{$key});
1.35 www 407: } else {
1.692 albertel 408: $env{$key}=$newenv{$key};
1.35 www 409: }
1.191 harris41 410: }
1.830 albertel 411: open(my $env_file,$env{'user.environment'});
412: if (&timed_flock($env_file,LOCK_EX)
413: &&
414: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
415: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 416: while (my ($key,$value) = each(%newenv)) {
417: $disk_env{$key} = $value;
1.448 albertel 418: }
1.783 albertel 419: untie(%disk_env);
1.56 www 420: }
421: return 'ok';
422: }
423: # ----------------------------------------------------- Delete from Environment
424:
425: sub delenv {
426: my $delthis=shift;
427: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 428: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 429: "Attempt to delete from environment ".$delthis);
430: return 'error';
431: }
1.830 albertel 432: open(my $env_file,$env{'user.environment'});
433: if (&timed_flock($env_file,LOCK_EX)
434: &&
435: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
436: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 437: foreach my $key (keys(%disk_env)) {
438: if ($key=~/^$delthis/) {
1.619 albertel 439: delete($env{$key});
1.783 albertel 440: delete($disk_env{$key});
1.473 matthew 441: }
1.448 albertel 442: }
1.783 albertel 443: untie(%disk_env);
1.5 www 444: }
445: return 'ok';
1.369 albertel 446: }
447:
1.790 albertel 448: sub get_env_multiple {
449: my ($name) = @_;
450: my @values;
451: if (defined($env{$name})) {
452: # exists is it an array
453: if (ref($env{$name})) {
454: @values=@{ $env{$name} };
455: } else {
456: $values[0]=$env{$name};
457: }
458: }
459: return(@values);
460: }
461:
1.369 albertel 462: # ------------------------------------------ Find out current server userload
463: # there is a copy in lond
464: sub userload {
465: my $numusers=0;
466: {
467: opendir(LONIDS,$perlvar{'lonIDsDir'});
468: my $filename;
469: my $curtime=time;
470: while ($filename=readdir(LONIDS)) {
471: if ($filename eq '.' || $filename eq '..') {next;}
1.404 albertel 472: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 473: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 474: }
475: closedir(LONIDS);
476: }
477: my $userloadpercent=0;
478: my $maxuserload=$perlvar{'lonUserLoadLim'};
479: if ($maxuserload) {
1.371 albertel 480: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 481: }
1.372 albertel 482: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 483: return $userloadpercent;
1.283 www 484: }
485:
486: # ------------------------------------------ Fight off request when overloaded
487:
488: sub overloaderror {
489: my ($r,$checkserver)=@_;
490: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
491: my $loadavg;
492: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 493: open(my $loadfile,'/proc/loadavg');
1.283 www 494: $loadavg=<$loadfile>;
495: $loadavg =~ s/\s.*//g;
1.285 matthew 496: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 497: close($loadfile);
1.283 www 498: } else {
499: $loadavg=&reply('load',$checkserver);
500: }
1.285 matthew 501: my $overload=$loadavg-100;
1.283 www 502: if ($overload>0) {
1.285 matthew 503: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 504: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 505: return 413;
1.283 www 506: }
507: return '';
1.5 www 508: }
1.1 albertel 509:
510: # ------------------------------ Find server with least workload from spare.tab
1.11 www 511:
1.1 albertel 512: sub spareserver {
1.670 albertel 513: my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784 albertel 514: my $spare_server;
1.370 albertel 515: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 516: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
517: : $userloadpercent;
518:
519: foreach my $try_server (@{ $spareid{'primary'} }) {
520: ($spare_server, $lowest_load) =
521: &compare_server_load($try_server, $spare_server, $lowest_load);
522: }
523:
524: my $found_server = ($spare_server ne '' && $lowest_load < 100);
525:
526: if (!$found_server) {
527: foreach my $try_server (@{ $spareid{'default'} }) {
528: ($spare_server, $lowest_load) =
529: &compare_server_load($try_server, $spare_server, $lowest_load);
530: }
531: }
532:
533: if (!$want_server_name) {
1.838 albertel 534: $spare_server="http://".&hostname($spare_server);
1.784 albertel 535: }
536: return $spare_server;
537: }
538:
539: sub compare_server_load {
540: my ($try_server, $spare_server, $lowest_load) = @_;
541:
542: my $loadans = &reply('load', $try_server);
543: my $userloadans = &reply('userload',$try_server);
544:
545: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
546: next; #didn't get a number from the server
547: }
548:
549: my $load;
550: if ($loadans =~ /\d/) {
551: if ($userloadans =~ /\d/) {
552: #both are numbers, pick the bigger one
553: $load = ($loadans > $userloadans) ? $loadans
554: : $userloadans;
1.411 albertel 555: } else {
1.784 albertel 556: $load = $loadans;
1.411 albertel 557: }
1.784 albertel 558: } else {
559: $load = $userloadans;
560: }
561:
562: if (($load =~ /\d/) && ($load < $lowest_load)) {
563: $spare_server = $try_server;
564: $lowest_load = $load;
1.370 albertel 565: }
1.784 albertel 566: return ($spare_server,$lowest_load);
1.202 matthew 567: }
568: # --------------------------------------------- Try to change a user's password
569:
570: sub changepass {
1.799 raeburn 571: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 572: $currentpass = &escape($currentpass);
573: $newpass = &escape($newpass);
1.799 raeburn 574: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202 matthew 575: $server);
576: if (! $answer) {
577: &logthis("No reply on password change request to $server ".
578: "by $uname in domain $udom.");
579: } elsif ($answer =~ "^ok") {
580: &logthis("$uname in $udom successfully changed their password ".
581: "on $server.");
582: } elsif ($answer =~ "^pwchange_failure") {
583: &logthis("$uname in $udom was unable to change their password ".
584: "on $server. The action was blocked by either lcpasswd ".
585: "or pwchange");
586: } elsif ($answer =~ "^non_authorized") {
587: &logthis("$uname in $udom did not get their password correct when ".
588: "attempting to change it on $server.");
589: } elsif ($answer =~ "^auth_mode_error") {
590: &logthis("$uname in $udom attempted to change their password despite ".
591: "not being locally or internally authenticated on $server.");
592: } elsif ($answer =~ "^unknown_user") {
593: &logthis("$uname in $udom attempted to change their password ".
594: "on $server but were unable to because $server is not ".
595: "their home server.");
596: } elsif ($answer =~ "^refused") {
597: &logthis("$server refused to change $uname in $udom password because ".
598: "it was sent an unencrypted request to change the password.");
599: }
600: return $answer;
1.1 albertel 601: }
602:
1.169 harris41 603: # ----------------------- Try to determine user's current authentication scheme
604:
605: sub queryauthenticate {
606: my ($uname,$udom)=@_;
1.456 albertel 607: my $uhome=&homeserver($uname,$udom);
608: if (!$uhome) {
609: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
610: return 'no_host';
611: }
612: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
613: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
614: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 615: }
1.456 albertel 616: return $answer;
1.169 harris41 617: }
618:
1.1 albertel 619: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 620:
1.1 albertel 621: sub authenticate {
622: my ($uname,$upass,$udom)=@_;
1.807 albertel 623: $upass=&escape($upass);
624: $uname= &LONCAPA::clean_username($uname);
1.836 www 625: my $uhome=&homeserver($uname,$udom,1);
626: if ((!$uhome) || ($uhome eq 'no_host')) {
627: # Maybe the machine was offline and only re-appeared again recently?
628: &reconlonc();
629: # One more
630: my $uhome=&homeserver($uname,$udom,1);
631: if ((!$uhome) || ($uhome eq 'no_host')) {
632: &logthis("User $uname at $udom is unknown in authenticate");
633: }
1.471 albertel 634: return 'no_host';
1.1 albertel 635: }
1.471 albertel 636: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
637: if ($answer eq 'authorized') {
638: &logthis("User $uname at $udom authorized by $uhome");
639: return $uhome;
640: }
641: if ($answer eq 'non_authorized') {
642: &logthis("User $uname at $udom rejected by $uhome");
643: return 'no_host';
1.9 www 644: }
1.471 albertel 645: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 646: return 'no_host';
647: }
648:
649: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 650:
1.599 albertel 651: my %homecache;
1.1 albertel 652: sub homeserver {
1.230 stredwic 653: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 654: my $index="$uname:$udom";
1.426 albertel 655:
1.599 albertel 656: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 657:
658: my %servers = &get_servers($udom,'library');
659: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 660: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 661: exists($badServerCache{$tryserver}));
1.841 albertel 662:
663: my $answer=reply("home:$udom:$uname",$tryserver);
664: if ($answer eq 'found') {
665: delete($badServerCache{$tryserver});
666: return $homecache{$index}=$tryserver;
667: } elsif ($answer eq 'no_host') {
668: $badServerCache{$tryserver}=1;
669: }
1.1 albertel 670: }
671: return 'no_host';
1.70 www 672: }
673:
674: # ------------------------------------- Find the usernames behind a list of IDs
675:
676: sub idget {
677: my ($udom,@ids)=@_;
678: my %returnhash=();
679:
1.841 albertel 680: my %servers = &get_servers($udom,'library');
681: foreach my $tryserver (keys(%servers)) {
682: my $idlist=join('&',@ids);
683: $idlist=~tr/A-Z/a-z/;
684: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
685: my @answer=();
686: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
687: @answer=split(/\&/,$reply);
688: } ;
689: my $i;
690: for ($i=0;$i<=$#ids;$i++) {
691: if ($answer[$i]) {
692: $returnhash{$ids[$i]}=$answer[$i];
693: }
694: }
695: }
1.70 www 696: return %returnhash;
697: }
698:
699: # ------------------------------------- Find the IDs behind a list of usernames
700:
701: sub idrget {
702: my ($udom,@unames)=@_;
703: my %returnhash=();
1.800 albertel 704: foreach my $uname (@unames) {
705: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 706: }
1.70 www 707: return %returnhash;
708: }
709:
710: # ------------------------------- Store away a list of names and associated IDs
711:
712: sub idput {
713: my ($udom,%ids)=@_;
714: my %servers=();
1.800 albertel 715: foreach my $uname (keys(%ids)) {
716: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
717: my $uhom=&homeserver($uname,$udom);
1.70 www 718: if ($uhom ne 'no_host') {
1.800 albertel 719: my $id=&escape($ids{$uname});
1.70 www 720: $id=~tr/A-Z/a-z/;
1.800 albertel 721: my $esc_unam=&escape($uname);
1.70 www 722: if ($servers{$uhom}) {
1.800 albertel 723: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 724: } else {
1.800 albertel 725: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 726: }
727: }
1.191 harris41 728: }
1.800 albertel 729: foreach my $server (keys(%servers)) {
730: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 731: }
1.344 www 732: }
733:
1.806 raeburn 734: # ------------------------------------------- get items from domain db files
735:
736: sub get_dom {
1.860 raeburn 737: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 738: my $items='';
739: foreach my $item (@$storearr) {
740: $items.=&escape($item).'&';
741: }
742: $items=~s/\&$//;
1.860 raeburn 743: if (!$udom) {
744: $udom=$env{'user.domain'};
745: if (defined(&domain($udom,'primary'))) {
746: $uhome=&domain($udom,'primary');
747: } else {
748: $uhome eq '';
749: }
750: } else {
751: if (!$uhome) {
752: if (defined(&domain($udom,'primary'))) {
753: $uhome=&domain($udom,'primary');
754: }
755: }
756: }
757: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 758: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
759: my @pairs=split(/\&/,$rep);
760: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
761: return @pairs;
762: }
763: my %returnhash=();
764: my $i=0;
765: foreach my $item (@$storearr) {
766: $returnhash{$item}=&thaw_unescape($pairs[$i]);
767: $i++;
768: }
769: return %returnhash;
770: } else {
1.860 raeburn 771: &logthis("get_dom failed - no homeserver and/or domain");
1.806 raeburn 772: }
773: }
774:
775: # -------------------------------------------- put items in domain db files
776:
777: sub put_dom {
1.860 raeburn 778: my ($namespace,$storehash,$udom,$uhome)=@_;
779: if (!$udom) {
780: $udom=$env{'user.domain'};
781: if (defined(&domain($udom,'primary'))) {
782: $uhome=&domain($udom,'primary');
783: } else {
784: $uhome eq '';
785: }
786: } else {
787: if (!$uhome) {
788: if (defined(&domain($udom,'primary'))) {
789: $uhome=&domain($udom,'primary');
790: }
791: }
792: }
793: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 794: my $items='';
795: foreach my $item (keys(%$storehash)) {
796: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
797: }
798: $items=~s/\&$//;
799: return &reply("putdom:$udom:$namespace:$items",$uhome);
800: } else {
1.860 raeburn 801: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 802: }
803: }
804:
1.837 raeburn 805: sub retrieve_inst_usertypes {
806: my ($udom) = @_;
807: my (%returnhash,@order);
1.846 albertel 808: if (defined(&domain($udom,'primary'))) {
809: my $uhome=&domain($udom,'primary');
1.837 raeburn 810: my $rep=&reply("inst_usertypes:$udom",$uhome);
811: my ($hashitems,$orderitems) = split(/:/,$rep);
812: my @pairs=split(/\&/,$hashitems);
813: foreach my $item (@pairs) {
814: my ($key,$value)=split(/=/,$item,2);
815: $key = &unescape($key);
816: next if ($key =~ /^error: 2 /);
817: $returnhash{$key}=&thaw_unescape($value);
818: }
819: my @esc_order = split(/\&/,$orderitems);
820: foreach my $item (@esc_order) {
821: push(@order,&unescape($item));
822: }
823: } else {
824: &logthis("get_dom failed - no primary domain server for $udom");
825: }
826: return (\%returnhash,\@order);
827: }
828:
1.344 www 829: # --------------------------------------------------- Assign a key to a student
830:
831: sub assign_access_key {
1.364 www 832: #
833: # a valid key looks like uname:udom#comments
834: # comments are being appended
835: #
1.498 www 836: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
837: $kdom=
1.620 albertel 838: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 839: $knum=
1.620 albertel 840: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 841: $cdom=
1.620 albertel 842: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 843: $cnum=
1.620 albertel 844: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
845: $udom=$env{'user.name'} unless (defined($udom));
846: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 847: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 848: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 849: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 850: # assigned to this person
851: # - this should not happen,
1.345 www 852: # unless something went wrong
853: # the first time around
854: # ready to assign
1.364 www 855: $logentry=$1.'; '.$logentry;
1.496 www 856: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 857: $kdom,$knum) eq 'ok') {
1.345 www 858: # key now belongs to user
1.346 www 859: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 860: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
861: &appenv('environment.'.$envkey => $ckey);
862: return 'ok';
863: } else {
864: return
865: 'error: Count not permanently assign key, will need to be re-entered later.';
866: }
867: } else {
868: return 'error: Could not assign key, try again later.';
869: }
1.364 www 870: } elsif (!$existing{$ckey}) {
1.345 www 871: # the key does not exist
872: return 'error: The key does not exist';
873: } else {
874: # the key is somebody else's
875: return 'error: The key is already in use';
876: }
1.344 www 877: }
878:
1.364 www 879: # ------------------------------------------ put an additional comment on a key
880:
881: sub comment_access_key {
882: #
883: # a valid key looks like uname:udom#comments
884: # comments are being appended
885: #
886: my ($ckey,$cdom,$cnum,$logentry)=@_;
887: $cdom=
1.620 albertel 888: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 889: $cnum=
1.620 albertel 890: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 891: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
892: if ($existing{$ckey}) {
893: $existing{$ckey}.='; '.$logentry;
894: # ready to assign
1.367 www 895: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 896: $cdom,$cnum) eq 'ok') {
897: return 'ok';
898: } else {
899: return 'error: Count not store comment.';
900: }
901: } else {
902: # the key does not exist
903: return 'error: The key does not exist';
904: }
905: }
906:
1.344 www 907: # ------------------------------------------------------ Generate a set of keys
908:
909: sub generate_access_keys {
1.364 www 910: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 911: $cdom=
1.620 albertel 912: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 913: $cnum=
1.620 albertel 914: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 915: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 916: unless (($cdom) && ($cnum)) { return 0; }
917: if ($number>10000) { return 0; }
918: sleep(2); # make sure don't get same seed twice
919: srand(time()^($$+($$<<15))); # from "Programming Perl"
920: my $total=0;
921: for (my $i=1;$i<=$number;$i++) {
922: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
923: sprintf("%lx",int(100000*rand)).'-'.
924: sprintf("%lx",int(100000*rand));
925: $newkey=~s/1/g/g; # folks mix up 1 and l
926: $newkey=~s/0/h/g; # and also 0 and O
927: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
928: if ($existing{$newkey}) {
929: $i--;
930: } else {
1.364 www 931: if (&put('accesskeys',
932: { $newkey => '# generated '.localtime().
1.620 albertel 933: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 934: '; '.$logentry },
935: $cdom,$cnum) eq 'ok') {
1.344 www 936: $total++;
937: }
938: }
939: }
1.620 albertel 940: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 941: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
942: return $total;
943: }
944:
945: # ------------------------------------------------------- Validate an accesskey
946:
947: sub validate_access_key {
948: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
949: $cdom=
1.620 albertel 950: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 951: $cnum=
1.620 albertel 952: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
953: $udom=$env{'user.domain'} unless (defined($udom));
954: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 955: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 956: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 957: }
958:
959: # ------------------------------------- Find the section of student in a course
1.652 albertel 960: sub devalidate_getsection_cache {
961: my ($udom,$unam,$courseid)=@_;
962: my $hashid="$udom:$unam:$courseid";
963: &devalidate_cache_new('getsection',$hashid);
964: }
1.298 matthew 965:
1.815 albertel 966: sub courseid_to_courseurl {
967: my ($courseid) = @_;
968: #already url style courseid
969: return $courseid if ($courseid =~ m{^/});
970:
971: if (exists($env{'course.'.$courseid.'.num'})) {
972: my $cnum = $env{'course.'.$courseid.'.num'};
973: my $cdom = $env{'course.'.$courseid.'.domain'};
974: return "/$cdom/$cnum";
975: }
976:
977: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
978: if (exists($courseinfo{'num'})) {
979: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
980: }
981:
982: return undef;
983: }
984:
1.298 matthew 985: sub getsection {
986: my ($udom,$unam,$courseid)=@_;
1.599 albertel 987: my $cachetime=1800;
1.551 albertel 988:
989: my $hashid="$udom:$unam:$courseid";
1.599 albertel 990: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 991: if (defined($cached)) { return $result; }
992:
1.298 matthew 993: my %Pending;
994: my %Expired;
995: #
996: # Each role can either have not started yet (pending), be active,
997: # or have expired.
998: #
999: # If there is an active role, we are done.
1000: #
1001: # If there is more than one role which has not started yet,
1002: # choose the one which will start sooner
1003: # If there is one role which has not started yet, return it.
1004: #
1005: # If there is more than one expired role, choose the one which ended last.
1006: # If there is a role which has expired, return it.
1007: #
1.815 albertel 1008: $courseid = &courseid_to_courseurl($courseid);
1.817 raeburn 1009: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1010: foreach my $key (keys(%roleshash)) {
1.479 albertel 1011: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1012: my $section=$1;
1013: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1014: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1015: my $now=time;
1.548 albertel 1016: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1017: $Expired{$end}=$section;
1018: next;
1019: }
1.548 albertel 1020: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1021: $Pending{$start}=$section;
1022: next;
1023: }
1.599 albertel 1024: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1025: }
1026: #
1027: # Presumedly there will be few matching roles from the above
1028: # loop and the sorting time will be negligible.
1029: if (scalar(keys(%Pending))) {
1030: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1031: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1032: }
1033: if (scalar(keys(%Expired))) {
1034: my @sorted = sort {$a <=> $b} keys(%Expired);
1035: my $time = pop(@sorted);
1.599 albertel 1036: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1037: }
1.599 albertel 1038: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1039: }
1.70 www 1040:
1.599 albertel 1041: sub save_cache {
1042: &purge_remembered();
1.722 albertel 1043: #&Apache::loncommon::validate_page();
1.620 albertel 1044: undef(%env);
1.780 albertel 1045: undef($env_loaded);
1.599 albertel 1046: }
1.452 albertel 1047:
1.599 albertel 1048: my $to_remember=-1;
1049: my %remembered;
1050: my %accessed;
1051: my $kicks=0;
1052: my $hits=0;
1.849 albertel 1053: sub make_key {
1054: my ($name,$id) = @_;
1055: if (length($id) > 200) { $id=length($id).':'.&Digest::MD5::md5_hex($id); }
1056: return &escape($name.':'.$id);
1057: }
1058:
1.599 albertel 1059: sub devalidate_cache_new {
1060: my ($name,$id,$debug) = @_;
1061: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1062: $id=&make_key($name,$id);
1.599 albertel 1063: $memcache->delete($id);
1064: delete($remembered{$id});
1065: delete($accessed{$id});
1066: }
1067:
1068: sub is_cached_new {
1069: my ($name,$id,$debug) = @_;
1.849 albertel 1070: $id=&make_key($name,$id);
1.599 albertel 1071: if (exists($remembered{$id})) {
1072: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1073: $accessed{$id}=[&gettimeofday()];
1074: $hits++;
1075: return ($remembered{$id},1);
1076: }
1077: my $value = $memcache->get($id);
1078: if (!(defined($value))) {
1079: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1080: return (undef,undef);
1.416 albertel 1081: }
1.599 albertel 1082: if ($value eq '__undef__') {
1083: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1084: $value=undef;
1085: }
1086: &make_room($id,$value,$debug);
1087: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1088: return ($value,1);
1089: }
1090:
1091: sub do_cache_new {
1092: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1093: $id=&make_key($name,$id);
1.599 albertel 1094: my $setvalue=$value;
1095: if (!defined($setvalue)) {
1096: $setvalue='__undef__';
1097: }
1.623 albertel 1098: if (!defined($time) ) {
1099: $time=600;
1100: }
1.599 albertel 1101: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.600 albertel 1102: $memcache->set($id,$setvalue,$time);
1103: # need to make a copy of $value
1104: #&make_room($id,$value,$debug);
1.599 albertel 1105: return $value;
1106: }
1107:
1108: sub make_room {
1109: my ($id,$value,$debug)=@_;
1110: $remembered{$id}=$value;
1111: if ($to_remember<0) { return; }
1112: $accessed{$id}=[&gettimeofday()];
1113: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1114: my $to_kick;
1115: my $max_time=0;
1116: foreach my $other (keys(%accessed)) {
1117: if (&tv_interval($accessed{$other}) > $max_time) {
1118: $to_kick=$other;
1119: $max_time=&tv_interval($accessed{$other});
1120: }
1121: }
1122: delete($remembered{$to_kick});
1123: delete($accessed{$to_kick});
1124: $kicks++;
1125: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1126: return;
1127: }
1128:
1.599 albertel 1129: sub purge_remembered {
1.604 albertel 1130: #&logthis("Tossing ".scalar(keys(%remembered)));
1131: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1132: undef(%remembered);
1133: undef(%accessed);
1.428 albertel 1134: }
1.70 www 1135: # ------------------------------------- Read an entry from a user's environment
1136:
1137: sub userenvironment {
1138: my ($udom,$unam,@what)=@_;
1139: my %returnhash=();
1140: my @answer=split(/\&/,
1141: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
1142: &homeserver($unam,$udom)));
1143: my $i;
1144: for ($i=0;$i<=$#what;$i++) {
1145: $returnhash{$what[$i]}=&unescape($answer[$i]);
1146: }
1147: return %returnhash;
1.1 albertel 1148: }
1149:
1.617 albertel 1150: # ---------------------------------------------------------- Get a studentphoto
1151: sub studentphoto {
1152: my ($udom,$unam,$ext) = @_;
1153: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 1154: if (defined($env{'request.course.id'})) {
1.708 raeburn 1155: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 1156: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1157: return(&retrievestudentphoto($udom,$unam,$ext));
1158: } else {
1159: my ($result,$perm_reqd)=
1.707 albertel 1160: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1161: if ($result eq 'ok') {
1162: if (!($perm_reqd eq 'yes')) {
1163: return(&retrievestudentphoto($udom,$unam,$ext));
1164: }
1165: }
1166: }
1167: }
1168: } else {
1169: my ($result,$perm_reqd) =
1.707 albertel 1170: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1171: if ($result eq 'ok') {
1172: if (!($perm_reqd eq 'yes')) {
1173: return(&retrievestudentphoto($udom,$unam,$ext));
1174: }
1175: }
1176: }
1177: return '/adm/lonKaputt/lonlogo_broken.gif';
1178: }
1179:
1180: sub retrievestudentphoto {
1181: my ($udom,$unam,$ext,$type) = @_;
1182: my $home=&Apache::lonnet::homeserver($unam,$udom);
1183: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
1184: if ($ret eq 'ok') {
1185: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
1186: if ($type eq 'thumbnail') {
1187: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
1188: }
1189: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
1190: return $tokenurl;
1191: } else {
1192: if ($type eq 'thumbnail') {
1193: return '/adm/lonKaputt/genericstudent_tn.gif';
1194: } else {
1195: return '/adm/lonKaputt/lonlogo_broken.gif';
1196: }
1.617 albertel 1197: }
1198: }
1199:
1.263 www 1200: # -------------------------------------------------------------------- New chat
1201:
1202: sub chatsend {
1.724 raeburn 1203: my ($newentry,$anon,$group)=@_;
1.620 albertel 1204: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1205: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1206: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 1207: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 1208: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 1209: &escape($newentry)).':'.$group,$chome);
1.292 www 1210: }
1211:
1212: # ------------------------------------------ Find current version of a resource
1213:
1214: sub getversion {
1215: my $fname=&clutter(shift);
1216: unless ($fname=~/^\/res\//) { return -1; }
1217: return ¤tversion(&filelocation('',$fname));
1218: }
1219:
1220: sub currentversion {
1221: my $fname=shift;
1.599 albertel 1222: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 1223: if (defined($cached)) { return $result; }
1.292 www 1224: my $author=$fname;
1225: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1226: my ($udom,$uname)=split(/\//,$author);
1227: my $home=homeserver($uname,$udom);
1228: if ($home eq 'no_host') {
1229: return -1;
1230: }
1231: my $answer=reply("currentversion:$fname",$home);
1232: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1233: return -1;
1234: }
1.599 albertel 1235: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 1236: }
1237:
1.1 albertel 1238: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 1239:
1.1 albertel 1240: sub subscribe {
1241: my $fname=shift;
1.761 raeburn 1242: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 1243: $fname=~s/[\n\r]//g;
1.1 albertel 1244: my $author=$fname;
1245: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1246: my ($udom,$uname)=split(/\//,$author);
1247: my $home=homeserver($uname,$udom);
1.335 albertel 1248: if ($home eq 'no_host') {
1249: return 'not_found';
1.1 albertel 1250: }
1251: my $answer=reply("sub:$fname",$home);
1.64 www 1252: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1253: $answer.=' by '.$home;
1254: }
1.1 albertel 1255: return $answer;
1256: }
1257:
1.8 www 1258: # -------------------------------------------------------------- Replicate file
1259:
1260: sub repcopy {
1261: my $filename=shift;
1.23 www 1262: $filename=~s/\/+/\//g;
1.607 raeburn 1263: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
1264: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 1265: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 1266: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 1267: return &repcopy_userfile($filename);
1268: }
1.532 albertel 1269: $filename=~s/[\n\r]//g;
1.8 www 1270: my $transname="$filename.in.transfer";
1.828 www 1271: # FIXME: this should flock
1.607 raeburn 1272: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 1273: my $remoteurl=subscribe($filename);
1.64 www 1274: if ($remoteurl =~ /^con_lost by/) {
1275: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1276: return 'unavailable';
1.8 www 1277: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 1278: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 1279: return 'not_found';
1.64 www 1280: } elsif ($remoteurl =~ /^rejected by/) {
1281: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1282: return 'forbidden';
1.20 www 1283: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 1284: return 'ok';
1.8 www 1285: } else {
1.290 www 1286: my $author=$filename;
1287: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1288: my ($udom,$uname)=split(/\//,$author);
1289: my $home=homeserver($uname,$udom);
1290: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 1291: my @parts=split(/\//,$filename);
1292: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1293: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1294: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 1295: return 'bad_request';
1.8 www 1296: }
1297: my $count;
1298: for ($count=5;$count<$#parts;$count++) {
1299: $path.="/$parts[$count]";
1300: if ((-e $path)!=1) {
1301: mkdir($path,0777);
1302: }
1303: }
1304: my $ua=new LWP::UserAgent;
1305: my $request=new HTTP::Request('GET',"$remoteurl");
1306: my $response=$ua->request($request,$transname);
1307: if ($response->is_error()) {
1308: unlink($transname);
1309: my $message=$response->status_line;
1.672 albertel 1310: &logthis("<font color=\"blue\">WARNING:"
1.12 www 1311: ." LWP get: $message: $filename</font>");
1.607 raeburn 1312: return 'unavailable';
1.8 www 1313: } else {
1.16 www 1314: if ($remoteurl!~/\.meta$/) {
1315: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1316: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1317: if ($mresponse->is_error()) {
1318: unlink($filename.'.meta');
1319: &logthis(
1.672 albertel 1320: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 1321: }
1322: }
1.8 www 1323: rename($transname,$filename);
1.607 raeburn 1324: return 'ok';
1.8 www 1325: }
1.290 www 1326: }
1.8 www 1327: }
1.330 www 1328: }
1329:
1330: # ------------------------------------------------ Get server side include body
1331: sub ssi_body {
1.381 albertel 1332: my ($filelink,%form)=@_;
1.606 matthew 1333: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
1334: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
1335: }
1.330 www 1336: my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381 albertel 1337: &ssi($filelink,%form));
1.778 albertel 1338: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 1339: $output=~s/^.*?\<body[^\>]*\>//si;
1340: $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330 www 1341: return $output;
1.8 www 1342: }
1343:
1.15 www 1344: # --------------------------------------------------------- Server Side Include
1345:
1.782 albertel 1346: sub absolute_url {
1347: my ($host_name) = @_;
1348: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
1349: if ($host_name eq '') {
1350: $host_name = $ENV{'SERVER_NAME'};
1351: }
1352: return $protocol.$host_name;
1353: }
1354:
1.15 www 1355: sub ssi {
1356:
1.23 www 1357: my ($fn,%form)=@_;
1.15 www 1358:
1359: my $ua=new LWP::UserAgent;
1.23 www 1360:
1361: my $request;
1.711 albertel 1362:
1363: $form{'no_update_last_known'}=1;
1364:
1.23 www 1365: if (%form) {
1.782 albertel 1366: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201 albertel 1367: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23 www 1368: } else {
1.782 albertel 1369: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 1370: }
1371:
1.15 www 1372: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1373: my $response=$ua->request($request);
1374:
1.324 www 1375: return $response->content;
1376: }
1377:
1378: sub externalssi {
1379: my ($url)=@_;
1380: my $ua=new LWP::UserAgent;
1381: my $request=new HTTP::Request('GET',$url);
1382: my $response=$ua->request($request);
1.15 www 1383: return $response->content;
1384: }
1.254 www 1385:
1.492 albertel 1386: # -------------------------------- Allow a /uploaded/ URI to be vouched for
1387:
1388: sub allowuploaded {
1389: my ($srcurl,$url)=@_;
1390: $url=&clutter(&declutter($url));
1391: my $dir=$url;
1392: $dir=~s/\/[^\/]+$//;
1393: my %httpref=();
1394: my $httpurl=&hreflocation('',$url);
1395: $httpref{'httpref.'.$httpurl}=$srcurl;
1396: &Apache::lonnet::appenv(%httpref);
1.254 www 1397: }
1.477 raeburn 1398:
1.478 albertel 1399: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 1400: # input: action, courseID, current domain, intended
1.637 raeburn 1401: # path to file, source of file, instruction to parse file for objects,
1402: # ref to hash for embedded objects,
1403: # ref to hash for codebase of java objects.
1404: #
1.485 raeburn 1405: # output: url to file (if action was uploaddoc),
1406: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 1407: #
1.478 albertel 1408: # Allows directory structure to be used within lonUsers/../userfiles/ for a
1409: # course.
1.477 raeburn 1410: #
1.478 albertel 1411: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1412: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
1413: # course's home server.
1.477 raeburn 1414: #
1.478 albertel 1415: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
1416: # be copied from $source (current location) to
1417: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1418: # and will then be copied to
1419: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
1420: # course's home server.
1.485 raeburn 1421: #
1.481 raeburn 1422: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 1423: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 1424: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1425: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
1426: # in course's home server.
1.637 raeburn 1427: #
1.477 raeburn 1428:
1429: sub process_coursefile {
1.638 albertel 1430: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477 raeburn 1431: my $fetchresult;
1.638 albertel 1432: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 1433: if ($action eq 'propagate') {
1.638 albertel 1434: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1435: $home);
1.481 raeburn 1436: } else {
1.477 raeburn 1437: my $fpath = '';
1438: my $fname = $file;
1.478 albertel 1439: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 1440: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 1441: my $filepath = &build_filepath($fpath);
1.481 raeburn 1442: if ($action eq 'copy') {
1443: if ($source eq '') {
1444: $fetchresult = 'no source file';
1445: return $fetchresult;
1446: } else {
1447: my $destination = $filepath.'/'.$fname;
1448: rename($source,$destination);
1449: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1450: $home);
1.481 raeburn 1451: }
1452: } elsif ($action eq 'uploaddoc') {
1453: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 1454: print $fh $env{'form.'.$source};
1.481 raeburn 1455: close($fh);
1.637 raeburn 1456: if ($parser eq 'parse') {
1457: my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
1458: unless ($parse_result eq 'ok') {
1459: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
1460: }
1461: }
1.477 raeburn 1462: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1463: $home);
1.481 raeburn 1464: if ($fetchresult eq 'ok') {
1465: return '/uploaded/'.$fpath.'/'.$fname;
1466: } else {
1467: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1468: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 1469: return '/adm/notfound.html';
1470: }
1.477 raeburn 1471: }
1472: }
1.485 raeburn 1473: unless ( $fetchresult eq 'ok') {
1.477 raeburn 1474: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1475: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 1476: }
1477: return $fetchresult;
1478: }
1479:
1.637 raeburn 1480: sub build_filepath {
1481: my ($fpath) = @_;
1482: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
1483: unless ($fpath eq '') {
1484: my @parts=split('/',$fpath);
1485: foreach my $part (@parts) {
1486: $filepath.= '/'.$part;
1487: if ((-e $filepath)!=1) {
1488: mkdir($filepath,0777);
1489: }
1490: }
1491: }
1492: return $filepath;
1493: }
1494:
1495: sub store_edited_file {
1.638 albertel 1496: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 1497: my $file = $primary_url;
1498: $file =~ s#^/uploaded/$docudom/$docuname/##;
1499: my $fpath = '';
1500: my $fname = $file;
1501: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1502: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1503: my $filepath = &build_filepath($fpath);
1504: open(my $fh,'>'.$filepath.'/'.$fname);
1505: print $fh $content;
1506: close($fh);
1.638 albertel 1507: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 1508: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1509: $home);
1.637 raeburn 1510: if ($$fetchresult eq 'ok') {
1511: return '/uploaded/'.$fpath.'/'.$fname;
1512: } else {
1.638 albertel 1513: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1514: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 1515: return '/adm/notfound.html';
1516: }
1517: }
1518:
1.531 albertel 1519: sub clean_filename {
1.831 albertel 1520: my ($fname,$args)=@_;
1.315 www 1521: # Replace Windows backslashes by forward slashes
1.257 www 1522: $fname=~s/\\/\//g;
1.831 albertel 1523: if (!$args->{'keep_path'}) {
1524: # Get rid of everything but the actual filename
1525: $fname=~s/^.*\/([^\/]+)$/$1/;
1526: }
1.315 www 1527: # Replace spaces by underscores
1528: $fname=~s/\s+/\_/g;
1529: # Replace all other weird characters by nothing
1.831 albertel 1530: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 1531: # Replace all .\d. sequences with _\d. so they no longer look like version
1532: # numbers
1533: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 1534: return $fname;
1535: }
1536:
1.608 albertel 1537: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 1538: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719 banghart 1539: # the desired filenam is in $env{"form.$formname.filename"}
1.686 albertel 1540: # $coursedoc - if true up to the current course
1541: # if false
1542: # $subdir - directory in userfile to store the file into
1.858 raeburn 1543: # $parser - instruction to parse file for objects ($parser = parse)
1544: # $allfiles - reference to hash for embedded objects
1545: # $codebase - reference to hash for codebase of java objects
1546: # $desuname - username for permanent storage of uploaded file
1547: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 1548: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
1549: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858 raeburn 1550: #
1.686 albertel 1551: # output: url of file in userspace, or error: <message>
1552: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 1553:
1554:
1.531 albertel 1555: sub userfileupload {
1.860 raeburn 1556: my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
1557: $destudom,$thumbwidth,$thumbheight)=@_;
1.531 albertel 1558: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 1559: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 1560: $fname=&clean_filename($fname);
1.315 www 1561: # See if there is anything left
1.257 www 1562: unless ($fname) { return 'error: no uploaded file'; }
1.620 albertel 1563: chop($env{'form.'.$formname});
1.523 raeburn 1564: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
1565: my $now = time;
1566: my $filepath = 'tmp/helprequests/'.$now;
1567: my @parts=split(/\//,$filepath);
1568: my $fullpath = $perlvar{'lonDaemons'};
1569: for (my $i=0;$i<@parts;$i++) {
1570: $fullpath .= '/'.$parts[$i];
1571: if ((-e $fullpath)!=1) {
1572: mkdir($fullpath,0777);
1573: }
1574: }
1575: open(my $fh,'>'.$fullpath.'/'.$fname);
1.620 albertel 1576: print $fh $env{'form.'.$formname};
1.523 raeburn 1577: close($fh);
1.741 raeburn 1578: return $fullpath.'/'.$fname;
1579: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
1580: my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
1581: '_'.$env{'user.domain'}.'/pending';
1582: my @parts=split(/\//,$filepath);
1583: my $fullpath = $perlvar{'lonDaemons'};
1584: for (my $i=0;$i<@parts;$i++) {
1585: $fullpath .= '/'.$parts[$i];
1586: if ((-e $fullpath)!=1) {
1587: mkdir($fullpath,0777);
1588: }
1589: }
1590: open(my $fh,'>'.$fullpath.'/'.$fname);
1591: print $fh $env{'form.'.$formname};
1592: close($fh);
1593: return $fullpath.'/'.$fname;
1.523 raeburn 1594: }
1.719 banghart 1595:
1.258 www 1596: # Create the directory if not present
1.493 albertel 1597: $fname="$subdir/$fname";
1.259 www 1598: if ($coursedoc) {
1.638 albertel 1599: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1600: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 1601: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 1602: return &finishuserfileupload($docuname,$docudom,
1603: $formname,$fname,$parser,$allfiles,
1.860 raeburn 1604: $codebase,$thumbwidth,$thumbheight);
1.481 raeburn 1605: } else {
1.620 albertel 1606: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 1607: return &process_coursefile('uploaddoc',$docuname,$docudom,
1608: $fname,$formname,$parser,
1609: $allfiles,$codebase);
1.481 raeburn 1610: }
1.719 banghart 1611: } elsif (defined($destuname)) {
1612: my $docuname=$destuname;
1613: my $docudom=$destudom;
1.860 raeburn 1614: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1615: $parser,$allfiles,$codebase,
1616: $thumbwidth,$thumbheight);
1.719 banghart 1617:
1.259 www 1618: } else {
1.638 albertel 1619: my $docuname=$env{'user.name'};
1620: my $docudom=$env{'user.domain'};
1.714 raeburn 1621: if (exists($env{'form.group'})) {
1622: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1623: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1624: }
1.860 raeburn 1625: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1626: $parser,$allfiles,$codebase,
1627: $thumbwidth,$thumbheight);
1.259 www 1628: }
1.271 www 1629: }
1630:
1631: sub finishuserfileupload {
1.860 raeburn 1632: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1633: $thumbwidth,$thumbheight) = @_;
1.477 raeburn 1634: my $path=$docudom.'/'.$docuname.'/';
1.258 www 1635: my $filepath=$perlvar{'lonDocRoot'};
1.860 raeburn 1636: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 1637: $file=$fname;
1638: if ($fname=~m|/|) {
1639: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
1640: $path.=$fnamepath.'/';
1641: }
1.259 www 1642: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 1643: my $count;
1644: for ($count=4;$count<=$#parts;$count++) {
1645: $filepath.="/$parts[$count]";
1646: if ((-e $filepath)!=1) {
1647: mkdir($filepath,0777);
1648: }
1649: }
1650: # Save the file
1651: {
1.701 albertel 1652: if (!open(FH,'>'.$filepath.'/'.$file)) {
1653: &logthis('Failed to create '.$filepath.'/'.$file);
1654: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
1655: return '/adm/notfound.html';
1656: }
1657: if (!print FH ($env{'form.'.$formname})) {
1658: &logthis('Failed to write to '.$filepath.'/'.$file);
1659: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
1660: return '/adm/notfound.html';
1661: }
1.570 albertel 1662: close(FH);
1.258 www 1663: }
1.637 raeburn 1664: if ($parser eq 'parse') {
1.638 albertel 1665: my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
1666: $codebase);
1.637 raeburn 1667: unless ($parse_result eq 'ok') {
1.638 albertel 1668: &logthis('Failed to parse '.$filepath.$file.
1669: ' for embedded media: '.$parse_result);
1.637 raeburn 1670: }
1671: }
1.860 raeburn 1672: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
1673: my $input = $filepath.'/'.$file;
1674: my $output = $filepath.'/'.'tn-'.$file;
1675: my $thumbsize = $thumbwidth.'x'.$thumbheight;
1676: system("convert -sample $thumbsize $input $output");
1677: if (-e $filepath.'/'.'tn-'.$file) {
1678: $fetchthumb = 1;
1679: }
1680: }
1.858 raeburn 1681:
1.259 www 1682: # Notify homeserver to grep it
1683: #
1.638 albertel 1684: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 1685: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 1686: if ($fetchresult eq 'ok') {
1.860 raeburn 1687: if ($fetchthumb) {
1688: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
1689: if ($thumbresult ne 'ok') {
1690: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
1691: $docuhome.': '.$thumbresult);
1692: }
1693: }
1.259 www 1694: #
1.258 www 1695: # Return the URL to it
1.494 albertel 1696: return '/uploaded/'.$path.$file;
1.263 www 1697: } else {
1.494 albertel 1698: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
1699: ': '.$fetchresult);
1.263 www 1700: return '/adm/notfound.html';
1.858 raeburn 1701: }
1.493 albertel 1702: }
1703:
1.637 raeburn 1704: sub extract_embedded_items {
1.648 raeburn 1705: my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637 raeburn 1706: my @state = ();
1707: my %javafiles = (
1708: codebase => '',
1709: code => '',
1710: archive => ''
1711: );
1712: my %mediafiles = (
1713: src => '',
1714: movie => '',
1715: );
1.648 raeburn 1716: my $p;
1717: if ($content) {
1718: $p = HTML::LCParser->new($content);
1719: } else {
1720: $p = HTML::LCParser->new($filepath.'/'.$file);
1721: }
1.641 albertel 1722: while (my $t=$p->get_token()) {
1.640 albertel 1723: if ($t->[0] eq 'S') {
1724: my ($tagname, $attr) = ($t->[1],$t->[2]);
1725: push (@state, $tagname);
1.648 raeburn 1726: if (lc($tagname) eq 'allow') {
1727: &add_filetype($allfiles,$attr->{'src'},'src');
1728: }
1.640 albertel 1729: if (lc($tagname) eq 'img') {
1730: &add_filetype($allfiles,$attr->{'src'},'src');
1731: }
1.645 raeburn 1732: if (lc($tagname) eq 'script') {
1733: if ($attr->{'archive'} =~ /\.jar$/i) {
1734: &add_filetype($allfiles,$attr->{'archive'},'archive');
1735: } else {
1736: &add_filetype($allfiles,$attr->{'src'},'src');
1737: }
1738: }
1739: if (lc($tagname) eq 'link') {
1740: if (lc($attr->{'rel'}) eq 'stylesheet') {
1741: &add_filetype($allfiles,$attr->{'href'},'href');
1742: }
1743: }
1.640 albertel 1744: if (lc($tagname) eq 'object' ||
1745: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
1746: foreach my $item (keys(%javafiles)) {
1747: $javafiles{$item} = '';
1748: }
1749: }
1750: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
1751: my $name = lc($attr->{'name'});
1752: foreach my $item (keys(%javafiles)) {
1753: if ($name eq $item) {
1754: $javafiles{$item} = $attr->{'value'};
1755: last;
1756: }
1757: }
1758: foreach my $item (keys(%mediafiles)) {
1759: if ($name eq $item) {
1760: &add_filetype($allfiles, $attr->{'value'}, 'value');
1761: last;
1762: }
1763: }
1764: }
1765: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
1766: foreach my $item (keys(%javafiles)) {
1767: if ($attr->{$item}) {
1768: $javafiles{$item} = $attr->{$item};
1769: last;
1770: }
1771: }
1772: foreach my $item (keys(%mediafiles)) {
1773: if ($attr->{$item}) {
1774: &add_filetype($allfiles,$attr->{$item},$item);
1775: last;
1776: }
1777: }
1778: }
1779: } elsif ($t->[0] eq 'E') {
1780: my ($tagname) = ($t->[1]);
1781: if ($javafiles{'codebase'} ne '') {
1782: $javafiles{'codebase'} .= '/';
1783: }
1784: if (lc($tagname) eq 'applet' ||
1785: lc($tagname) eq 'object' ||
1786: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
1787: ) {
1788: foreach my $item (keys(%javafiles)) {
1789: if ($item ne 'codebase' && $javafiles{$item} ne '') {
1790: my $file=$javafiles{'codebase'}.$javafiles{$item};
1791: &add_filetype($allfiles,$file,$item);
1792: }
1793: }
1794: }
1795: pop @state;
1796: }
1797: }
1.637 raeburn 1798: return 'ok';
1799: }
1800:
1.639 albertel 1801: sub add_filetype {
1802: my ($allfiles,$file,$type)=@_;
1803: if (exists($allfiles->{$file})) {
1804: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
1805: push(@{$allfiles->{$file}}, &escape($type));
1806: }
1807: } else {
1808: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 1809: }
1810: }
1811:
1.493 albertel 1812: sub removeuploadedurl {
1813: my ($url)=@_;
1814: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 1815: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 1816: }
1817:
1818: sub removeuserfile {
1819: my ($docuname,$docudom,$fname)=@_;
1820: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 1821: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1822: if ($result eq 'ok') {
1823: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
1824: my $metafile = $fname.'.meta';
1825: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 1826: my $url = "/uploaded/$docudom/$docuname/$fname";
1827: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 1828: my $sqlresult =
1.823 albertel 1829: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 1830: 'portfolio_metadata',$group,
1831: 'delete');
1.798 raeburn 1832: }
1833: }
1834: return $result;
1.257 www 1835: }
1.15 www 1836:
1.530 albertel 1837: sub mkdiruserfile {
1838: my ($docuname,$docudom,$dir)=@_;
1839: my $home=&homeserver($docuname,$docudom);
1840: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
1841: }
1842:
1.531 albertel 1843: sub renameuserfile {
1844: my ($docuname,$docudom,$old,$new)=@_;
1845: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 1846: my $result = &reply("renameuserfile:$docudom:$docuname:".
1847: &escape("$old").':'.&escape("$new"),$home);
1848: if ($result eq 'ok') {
1849: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
1850: my $oldmeta = $old.'.meta';
1851: my $newmeta = $new.'.meta';
1852: my $metaresult =
1853: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 1854: my $url = "/uploaded/$docudom/$docuname/$old";
1855: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 1856: my $sqlresult =
1.823 albertel 1857: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 1858: 'portfolio_metadata',$group,
1859: 'delete');
1.798 raeburn 1860: }
1861: }
1862: return $result;
1.531 albertel 1863: }
1864:
1.14 www 1865: # ------------------------------------------------------------------------- Log
1866:
1867: sub log {
1868: my ($dom,$nam,$hom,$what)=@_;
1.47 www 1869: return critical("log:$dom:$nam:$what",$hom);
1.157 www 1870: }
1871:
1872: # ------------------------------------------------------------------ Course Log
1.352 www 1873: #
1874: # This routine flushes several buffers of non-mission-critical nature
1875: #
1.157 www 1876:
1877: sub flushcourselogs {
1.352 www 1878: &logthis('Flushing log buffers');
1879: #
1880: # course logs
1881: # This is a log of all transactions in a course, which can be used
1882: # for data mining purposes
1883: #
1884: # It also collects the courseid database, which lists last transaction
1885: # times and course titles for all courseids
1886: #
1887: my %courseidbuffer=();
1.800 albertel 1888: foreach my $crsid (keys %courselogs) {
1.352 www 1889: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 1890: &escape($courselogs{$crsid}),
1891: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 1892: delete $courselogs{$crsid};
1893: } else {
1894: &logthis('Failed to flush log buffer for '.$crsid);
1895: if (length($courselogs{$crsid})>40000) {
1.672 albertel 1896: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 1897: " exceeded maximum size, deleting.</font>");
1898: delete $courselogs{$crsid};
1899: }
1.352 www 1900: }
1901: if ($courseidbuffer{$coursehombuf{$crsid}}) {
1902: $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516 raeburn 1903: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741 raeburn 1904: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.352 www 1905: } else {
1906: $courseidbuffer{$coursehombuf{$crsid}}=
1.516 raeburn 1907: &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741 raeburn 1908: ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.571 raeburn 1909: }
1.191 harris41 1910: }
1.352 www 1911: #
1912: # Write course id database (reverse lookup) to homeserver of courses
1913: # Is used in pickcourse
1914: #
1.840 albertel 1915: foreach my $crs_home (keys(%courseidbuffer)) {
1.844 albertel 1916: &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1.840 albertel 1917: $crs_home);
1.352 www 1918: }
1919: #
1920: # File accesses
1921: # Writes to the dynamic metadata of resources to get hit counts, etc.
1922: #
1.449 matthew 1923: foreach my $entry (keys(%accesshash)) {
1.458 matthew 1924: if ($entry =~ /___count$/) {
1925: my ($dom,$name);
1.807 albertel 1926: ($dom,$name,undef)=
1.811 albertel 1927: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 1928: if (! defined($dom) || $dom eq '' ||
1929: ! defined($name) || $name eq '') {
1.620 albertel 1930: my $cid = $env{'request.course.id'};
1931: $dom = $env{'request.'.$cid.'.domain'};
1932: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 1933: }
1.450 matthew 1934: my $value = $accesshash{$entry};
1935: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
1936: my %temphash=($url => $value);
1.449 matthew 1937: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
1938: if ($result eq 'ok') {
1939: delete $accesshash{$entry};
1940: } elsif ($result eq 'unknown_cmd') {
1941: # Target server has old code running on it.
1.450 matthew 1942: my %temphash=($entry => $value);
1.449 matthew 1943: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1944: delete $accesshash{$entry};
1945: }
1946: }
1947: } else {
1.811 albertel 1948: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 1949: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 1950: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
1951: delete $accesshash{$entry};
1952: }
1.185 www 1953: }
1.191 harris41 1954: }
1.352 www 1955: #
1956: # Roles
1957: # Reverse lookup of user roles for course faculty/staff and co-authorship
1958: #
1.800 albertel 1959: foreach my $entry (keys(%userrolehash)) {
1.351 www 1960: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 1961: split(/\:/,$entry);
1962: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 1963: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 1964: $rudom,$runame) eq 'ok') {
1965: delete $userrolehash{$entry};
1966: }
1967: }
1.662 raeburn 1968: #
1969: # Reverse lookup of domain roles (dc, ad, li, sc, au)
1970: #
1971: my %domrolebuffer = ();
1972: foreach my $entry (keys %domainrolehash) {
1973: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
1974: if ($domrolebuffer{$rudom}) {
1975: $domrolebuffer{$rudom}.='&'.&escape($entry).
1976: '='.&escape($domainrolehash{$entry});
1977: } else {
1978: $domrolebuffer{$rudom}.=&escape($entry).
1979: '='.&escape($domainrolehash{$entry});
1980: }
1981: delete $domainrolehash{$entry};
1982: }
1983: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 1984: my %servers = &get_servers($dom,'library');
1985: foreach my $tryserver (keys(%servers)) {
1986: unless (&reply('domroleput:'.$dom.':'.
1987: $domrolebuffer{$dom},$tryserver) eq 'ok') {
1988: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
1989: }
1.662 raeburn 1990: }
1991: }
1.186 www 1992: $dumpcount++;
1.157 www 1993: }
1994:
1995: sub courselog {
1996: my $what=shift;
1.158 www 1997: $what=time.':'.$what;
1.620 albertel 1998: unless ($env{'request.course.id'}) { return ''; }
1999: $coursedombuf{$env{'request.course.id'}}=
2000: $env{'course.'.$env{'request.course.id'}.'.domain'};
2001: $coursenumbuf{$env{'request.course.id'}}=
2002: $env{'course.'.$env{'request.course.id'}.'.num'};
2003: $coursehombuf{$env{'request.course.id'}}=
2004: $env{'course.'.$env{'request.course.id'}.'.home'};
2005: $coursedescrbuf{$env{'request.course.id'}}=
2006: $env{'course.'.$env{'request.course.id'}.'.description'};
2007: $courseinstcodebuf{$env{'request.course.id'}}=
2008: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2009: $courseownerbuf{$env{'request.course.id'}}=
2010: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 2011: $coursetypebuf{$env{'request.course.id'}}=
2012: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 2013: if (defined $courselogs{$env{'request.course.id'}}) {
2014: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 2015: } else {
1.620 albertel 2016: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 2017: }
1.620 albertel 2018: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 2019: &flushcourselogs();
2020: }
1.158 www 2021: }
2022:
2023: sub courseacclog {
2024: my $fnsymb=shift;
1.620 albertel 2025: unless ($env{'request.course.id'}) { return ''; }
2026: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 2027: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 2028: $what.=':POST';
1.583 matthew 2029: # FIXME: Probably ought to escape things....
1.800 albertel 2030: foreach my $key (keys(%env)) {
2031: if ($key=~/^form\.(.*)/) {
2032: $what.=':'.$1.'='.$env{$key};
1.158 www 2033: }
1.191 harris41 2034: }
1.583 matthew 2035: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
2036: # FIXME: We should not be depending on a form parameter that someone
2037: # editing lonsearchcat.pm might change in the future.
1.620 albertel 2038: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 2039: $what.= ':POST';
2040: # FIXME: Probably ought to escape things....
2041: foreach my $element ('courseexp','crsfulltext','crsrelated',
2042: 'crsdiscuss') {
1.620 albertel 2043: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 2044: }
2045: }
1.158 www 2046: }
2047: &courselog($what);
1.149 www 2048: }
2049:
1.185 www 2050: sub countacc {
2051: my $url=&declutter(shift);
1.458 matthew 2052: return if (! defined($url) || $url eq '');
1.620 albertel 2053: unless ($env{'request.course.id'}) { return ''; }
2054: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 2055: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 2056: $accesshash{$key}++;
1.185 www 2057: }
1.349 www 2058:
1.361 www 2059: sub linklog {
2060: my ($from,$to)=@_;
2061: $from=&declutter($from);
2062: $to=&declutter($to);
2063: $accesshash{$from.'___'.$to.'___comefrom'}=1;
2064: $accesshash{$to.'___'.$from.'___goto'}=1;
2065: }
2066:
1.349 www 2067: sub userrolelog {
2068: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 2069: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 2070: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 2071: ($trole=~/^ep/) || ($trole=~/^cr/) ||
2072: ($trole=~/^ta/)) {
1.350 www 2073: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2074: $userrolehash
2075: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 2076: =$tend.':'.$tstart;
1.662 raeburn 2077: }
2078: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
2079: ($trole=~/^li/) || ($trole=~/^li/) ||
2080: ($trole=~/^au/) || ($trole=~/^dg/) ||
2081: ($trole=~/^sc/)) {
2082: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2083: $domainrolehash
2084: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
2085: = $tend.':'.$tstart;
2086: }
1.351 www 2087: }
2088:
2089: sub get_course_adv_roles {
2090: my $cid=shift;
1.620 albertel 2091: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 2092: my %coursehash=&coursedescription($cid);
1.470 www 2093: my %nothide=();
1.800 albertel 2094: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
2095: $nothide{join(':',split(/[\@\:]/,$user))}=1;
1.470 www 2096: }
1.351 www 2097: my %returnhash=();
2098: my %dumphash=
2099: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
2100: my $now=time;
1.800 albertel 2101: foreach my $entry (keys %dumphash) {
2102: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 2103: if (($tstart) && ($tstart<0)) { next; }
2104: if (($tend) && ($tend<$now)) { next; }
2105: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 2106: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 2107: if ($username eq '' || $domain eq '') { next; }
1.470 www 2108: if ((&privileged($username,$domain)) &&
2109: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 2110: if ($role eq 'cr') { next; }
1.351 www 2111: my $key=&plaintext($role);
2112: if ($section) { $key.=' (Sec/Grp '.$section.')'; }
2113: if ($returnhash{$key}) {
2114: $returnhash{$key}.=','.$username.':'.$domain;
2115: } else {
2116: $returnhash{$key}=$username.':'.$domain;
2117: }
1.400 www 2118: }
2119: return %returnhash;
2120: }
2121:
2122: sub get_my_roles {
1.858 raeburn 2123: my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
1.620 albertel 2124: unless (defined($uname)) { $uname=$env{'user.name'}; }
2125: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.858 raeburn 2126: my %dumphash;
2127: if ($context eq 'userroles') {
2128: %dumphash = &dump('roles',$udom,$uname);
2129: } else {
2130: %dumphash=
1.400 www 2131: &dump('nohist_userroles',$udom,$uname);
1.858 raeburn 2132: }
1.400 www 2133: my %returnhash=();
2134: my $now=time;
1.800 albertel 2135: foreach my $entry (keys(%dumphash)) {
2136: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.400 www 2137: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 2138: my $status = 'active';
2139: if (($tend) && ($tend<$now)) {
2140: $status = 'previous';
2141: }
2142: if (($tstart) && ($now<$tstart)) {
2143: $status = 'future';
2144: }
2145: if (ref($types) eq 'ARRAY') {
2146: if (!grep(/^\Q$status\E$/,@{$types})) {
2147: next;
2148: }
2149: } else {
2150: if ($status ne 'active') {
2151: next;
2152: }
2153: }
1.800 albertel 2154: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.832 raeburn 2155: if (ref($roledoms) eq 'ARRAY') {
2156: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
2157: next;
2158: }
2159: }
2160: if (ref($roles) eq 'ARRAY') {
2161: if (!grep(/^\Q$role\E$/,@{$roles})) {
2162: next;
2163: }
2164: }
1.400 www 2165: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.832 raeburn 2166: }
1.373 www 2167: return %returnhash;
1.399 www 2168: }
2169:
2170: # ----------------------------------------------------- Frontpage Announcements
2171: #
2172: #
2173:
2174: sub postannounce {
2175: my ($server,$text)=@_;
1.844 albertel 2176: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 2177: unless ($text=~/\w/) { $text=''; }
2178: return &reply('setannounce:'.&escape($text),$server);
2179: }
2180:
2181: sub getannounce {
1.448 albertel 2182:
2183: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 2184: my $announcement='';
1.800 albertel 2185: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 2186: close($fh);
1.399 www 2187: if ($announcement=~/\w/) {
2188: return
2189: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 2190: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 2191: } else {
2192: return '';
2193: }
2194: } else {
2195: return '';
2196: }
1.351 www 2197: }
1.353 www 2198:
2199: # ---------------------------------------------------------- Course ID routines
2200: # Deal with domain's nohist_courseid.db files
2201: #
2202:
2203: sub courseidput {
2204: my ($domain,$what,$coursehome)=@_;
2205: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
2206: }
2207:
2208: sub courseiddump {
1.791 raeburn 2209: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
1.353 www 2210: my %returnhash=();
1.355 www 2211: unless ($domfilter) { $domfilter=''; }
1.845 albertel 2212: my %libserv = &all_library();
2213: foreach my $tryserver (keys(%libserv)) {
2214: if ( ( $hostidflag == 1
2215: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
2216: || (!defined($hostidflag)) ) {
2217:
2218: if ($domfilter eq ''
2219: || (&host_domain($tryserver) eq $domfilter)) {
1.800 albertel 2220: foreach my $line (
1.844 albertel 2221: split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
1.571 raeburn 2222: $sincefilter.':'.&escape($descfilter).':'.
1.791 raeburn 2223: &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
1.354 www 2224: $tryserver))) {
1.800 albertel 2225: my ($key,$value)=split(/\=/,$line,2);
1.506 raeburn 2226: if (($key) && ($value)) {
1.516 raeburn 2227: $returnhash{&unescape($key)}=$value;
1.506 raeburn 2228: }
1.353 www 2229: }
2230: }
2231: }
2232: }
2233: return %returnhash;
2234: }
2235:
1.658 raeburn 2236: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 2237:
2238: sub dcmailput {
1.685 raeburn 2239: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 2240: my $status = &Apache::lonnet::critical(
1.740 www 2241: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
2242: &escape($message),$server);
1.662 raeburn 2243: return $status;
2244: }
2245:
1.658 raeburn 2246: sub dcmaildump {
2247: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 2248: my %returnhash=();
1.846 albertel 2249:
2250: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 2251: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
2252: &escape($enddate).':';
2253: my @esc_senders=map { &escape($_)} @$senders;
2254: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 2255: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 2256: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 2257: if (($key) && ($value)) {
2258: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 2259: }
2260: }
2261: }
2262: return %returnhash;
2263: }
1.662 raeburn 2264: # ---------------------------------------------------------- Domain roles
2265:
2266: sub get_domain_roles {
2267: my ($dom,$roles,$startdate,$enddate)=@_;
2268: if (undef($startdate) || $startdate eq '') {
2269: $startdate = '.';
2270: }
2271: if (undef($enddate) || $enddate eq '') {
2272: $enddate = '.';
2273: }
2274: my $rolelist = join(':',@{$roles});
2275: my %personnel = ();
1.841 albertel 2276:
2277: my %servers = &get_servers($dom,'library');
2278: foreach my $tryserver (keys(%servers)) {
2279: %{$personnel{$tryserver}}=();
2280: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
2281: &escape($startdate).':'.
2282: &escape($enddate).':'.
2283: &escape($rolelist), $tryserver))) {
2284: my ($key,$value) = split(/\=/,$line,2);
2285: if (($key) && ($value)) {
2286: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
2287: }
2288: }
1.662 raeburn 2289: }
2290: return %personnel;
2291: }
1.658 raeburn 2292:
1.149 www 2293: # ----------------------------------------------------------- Check out an item
2294:
1.504 albertel 2295: sub get_first_access {
2296: my ($type,$argsymb)=@_;
1.790 albertel 2297: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 2298: if ($argsymb) { $symb=$argsymb; }
2299: my ($map,$id,$res)=&decode_symb($symb);
1.588 albertel 2300: if ($type eq 'map') {
2301: $res=&symbread($map);
2302: } else {
2303: $res=$symb;
2304: }
2305: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
2306: return $times{"$courseid\0$res"};
1.504 albertel 2307: }
2308:
2309: sub set_first_access {
2310: my ($type)=@_;
1.790 albertel 2311: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 2312: my ($map,$id,$res)=&decode_symb($symb);
1.588 albertel 2313: if ($type eq 'map') {
2314: $res=&symbread($map);
2315: } else {
2316: $res=$symb;
2317: }
2318: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 2319: if (!$firstaccess) {
1.588 albertel 2320: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 2321: }
2322: return 'already_set';
1.504 albertel 2323: }
2324:
1.149 www 2325: sub checkout {
2326: my ($symb,$tuname,$tudom,$tcrsid)=@_;
2327: my $now=time;
2328: my $lonhost=$perlvar{'lonHostID'};
2329: my $infostr=&escape(
1.234 www 2330: 'CHECKOUTTOKEN&'.
1.149 www 2331: $tuname.'&'.
2332: $tudom.'&'.
2333: $tcrsid.'&'.
2334: $symb.'&'.
2335: $now.'&'.$ENV{'REMOTE_ADDR'});
2336: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 2337: if ($token=~/^error\:/) {
1.672 albertel 2338: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2339: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
2340: "</font>");
2341: return '';
2342: }
2343:
1.149 www 2344: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
2345: $token=~tr/a-z/A-Z/;
2346:
1.153 www 2347: my %infohash=('resource.0.outtoken' => $token,
2348: 'resource.0.checkouttime' => $now,
2349: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 2350:
2351: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2352: return '';
1.151 www 2353: } else {
1.672 albertel 2354: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2355: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
2356: "</font>");
1.149 www 2357: }
2358:
2359: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2360: &escape('Checkout '.$infostr.' - '.
2361: $token)) ne 'ok') {
2362: return '';
1.151 www 2363: } else {
1.672 albertel 2364: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2365: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
2366: "</font>");
1.149 www 2367: }
1.151 www 2368: return $token;
1.149 www 2369: }
2370:
2371: # ------------------------------------------------------------ Check in an item
2372:
2373: sub checkin {
2374: my $token=shift;
1.150 www 2375: my $now=time;
2376: my ($ta,$tb,$lonhost)=split(/\*/,$token);
2377: $lonhost=~tr/A-Z/a-z/;
1.838 albertel 2378: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150 www 2379: $dtoken=~s/\W/\_/g;
1.234 www 2380: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 2381: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
2382:
1.154 www 2383: unless (($tuname) && ($tudom)) {
2384: &logthis('Check in '.$token.' ('.$dtoken.') failed');
2385: return '';
2386: }
2387:
2388: unless (&allowed('mgr',$tcrsid)) {
2389: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 2390: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 2391: return '';
2392: }
2393:
1.153 www 2394: my %infohash=('resource.0.intoken' => $token,
2395: 'resource.0.checkintime' => $now,
2396: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 2397:
2398: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2399: return '';
2400: }
2401:
2402: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2403: &escape('Checkin - '.$token)) ne 'ok') {
2404: return '';
2405: }
2406:
2407: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 2408: }
2409:
2410: # --------------------------------------------- Set Expire Date for Spreadsheet
2411:
2412: sub expirespread {
2413: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 2414: my $cid=$env{'request.course.id'};
1.110 www 2415: if ($cid) {
2416: my $now=time;
2417: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 2418: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
2419: $env{'course.'.$cid.'.num'}.
1.110 www 2420: ':nohist_expirationdates:'.
2421: &escape($key).'='.$now,
1.620 albertel 2422: $env{'course.'.$cid.'.home'})
1.110 www 2423: }
2424: return 'ok';
1.14 www 2425: }
2426:
1.109 www 2427: # ----------------------------------------------------- Devalidate Spreadsheets
2428:
2429: sub devalidate {
1.325 www 2430: my ($symb,$uname,$udom)=@_;
1.620 albertel 2431: my $cid=$env{'request.course.id'};
1.109 www 2432: if ($cid) {
1.391 matthew 2433: # delete the stored spreadsheets for
2434: # - the student level sheet of this user in course's homespace
2435: # - the assessment level sheet for this resource
2436: # for this user in user's homespace
1.553 albertel 2437: # - current conditional state info
1.325 www 2438: my $key=$uname.':'.$udom.':';
1.109 www 2439: my $status=
1.299 matthew 2440: &del('nohist_calculatedsheets',
1.391 matthew 2441: [$key.'studentcalc:'],
1.620 albertel 2442: $env{'course.'.$cid.'.domain'},
2443: $env{'course.'.$cid.'.num'})
1.133 albertel 2444: .' '.
2445: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 2446: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 2447: unless ($status eq 'ok ok') {
2448: &logthis('Could not devalidate spreadsheet '.
1.325 www 2449: $uname.' at '.$udom.' for '.
1.109 www 2450: $symb.': '.$status);
1.133 albertel 2451: }
1.553 albertel 2452: &delenv('user.state.'.$cid);
1.109 www 2453: }
2454: }
2455:
1.265 albertel 2456: sub get_scalar {
2457: my ($string,$end) = @_;
2458: my $value;
2459: if ($$string =~ s/^([^&]*?)($end)/$2/) {
2460: $value = $1;
2461: } elsif ($$string =~ s/^([^&]*?)&//) {
2462: $value = $1;
2463: }
2464: return &unescape($value);
2465: }
2466:
2467: sub array2str {
2468: my (@array) = @_;
2469: my $result=&arrayref2str(\@array);
2470: $result=~s/^__ARRAY_REF__//;
2471: $result=~s/__END_ARRAY_REF__$//;
2472: return $result;
2473: }
2474:
1.204 albertel 2475: sub arrayref2str {
2476: my ($arrayref) = @_;
1.265 albertel 2477: my $result='__ARRAY_REF__';
1.204 albertel 2478: foreach my $elem (@$arrayref) {
1.265 albertel 2479: if(ref($elem) eq 'ARRAY') {
2480: $result.=&arrayref2str($elem).'&';
2481: } elsif(ref($elem) eq 'HASH') {
2482: $result.=&hashref2str($elem).'&';
2483: } elsif(ref($elem)) {
2484: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 2485: } else {
2486: $result.=&escape($elem).'&';
2487: }
2488: }
2489: $result=~s/\&$//;
1.265 albertel 2490: $result .= '__END_ARRAY_REF__';
1.204 albertel 2491: return $result;
2492: }
2493:
1.168 albertel 2494: sub hash2str {
1.204 albertel 2495: my (%hash) = @_;
2496: my $result=&hashref2str(\%hash);
1.265 albertel 2497: $result=~s/^__HASH_REF__//;
2498: $result=~s/__END_HASH_REF__$//;
1.204 albertel 2499: return $result;
2500: }
2501:
2502: sub hashref2str {
2503: my ($hashref)=@_;
1.265 albertel 2504: my $result='__HASH_REF__';
1.800 albertel 2505: foreach my $key (sort(keys(%$hashref))) {
2506: if (ref($key) eq 'ARRAY') {
2507: $result.=&arrayref2str($key).'=';
2508: } elsif (ref($key) eq 'HASH') {
2509: $result.=&hashref2str($key).'=';
2510: } elsif (ref($key)) {
1.265 albertel 2511: $result.='=';
1.800 albertel 2512: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 2513: } else {
1.800 albertel 2514: if ($key) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 2515: }
2516:
1.800 albertel 2517: if(ref($hashref->{$key}) eq 'ARRAY') {
2518: $result.=&arrayref2str($hashref->{$key}).'&';
2519: } elsif(ref($hashref->{$key}) eq 'HASH') {
2520: $result.=&hashref2str($hashref->{$key}).'&';
2521: } elsif(ref($hashref->{$key})) {
1.265 albertel 2522: $result.='&';
1.800 albertel 2523: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 2524: } else {
1.800 albertel 2525: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 2526: }
2527: }
1.168 albertel 2528: $result=~s/\&$//;
1.265 albertel 2529: $result .= '__END_HASH_REF__';
1.168 albertel 2530: return $result;
2531: }
2532:
2533: sub str2hash {
1.265 albertel 2534: my ($string)=@_;
2535: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
2536: return %$hash;
2537: }
2538:
2539: sub str2hashref {
1.168 albertel 2540: my ($string) = @_;
1.265 albertel 2541:
2542: my %hash;
2543:
2544: if($string !~ /^__HASH_REF__/) {
2545: if (! ($string eq '' || !defined($string))) {
2546: $hash{'error'}='Not hash reference';
2547: }
2548: return (\%hash, $string);
2549: }
2550:
2551: $string =~ s/^__HASH_REF__//;
2552:
2553: while($string !~ /^__END_HASH_REF__/) {
2554: #key
2555: my $key='';
2556: if($string =~ /^__HASH_REF__/) {
2557: ($key, $string)=&str2hashref($string);
2558: if(defined($key->{'error'})) {
2559: $hash{'error'}='Bad data';
2560: return (\%hash, $string);
2561: }
2562: } elsif($string =~ /^__ARRAY_REF__/) {
2563: ($key, $string)=&str2arrayref($string);
2564: if($key->[0] eq 'Array reference error') {
2565: $hash{'error'}='Bad data';
2566: return (\%hash, $string);
2567: }
2568: } else {
2569: $string =~ s/^(.*?)=//;
1.267 albertel 2570: $key=&unescape($1);
1.265 albertel 2571: }
2572: $string =~ s/^=//;
2573:
2574: #value
2575: my $value='';
2576: if($string =~ /^__HASH_REF__/) {
2577: ($value, $string)=&str2hashref($string);
2578: if(defined($value->{'error'})) {
2579: $hash{'error'}='Bad data';
2580: return (\%hash, $string);
2581: }
2582: } elsif($string =~ /^__ARRAY_REF__/) {
2583: ($value, $string)=&str2arrayref($string);
2584: if($value->[0] eq 'Array reference error') {
2585: $hash{'error'}='Bad data';
2586: return (\%hash, $string);
2587: }
2588: } else {
2589: $value=&get_scalar(\$string,'__END_HASH_REF__');
2590: }
2591: $string =~ s/^&//;
2592:
2593: $hash{$key}=$value;
1.204 albertel 2594: }
1.265 albertel 2595:
2596: $string =~ s/^__END_HASH_REF__//;
2597:
2598: return (\%hash, $string);
1.204 albertel 2599: }
2600:
2601: sub str2array {
1.265 albertel 2602: my ($string)=@_;
2603: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
2604: return @$array;
2605: }
2606:
2607: sub str2arrayref {
1.204 albertel 2608: my ($string) = @_;
1.265 albertel 2609: my @array;
2610:
2611: if($string !~ /^__ARRAY_REF__/) {
2612: if (! ($string eq '' || !defined($string))) {
2613: $array[0]='Array reference error';
2614: }
2615: return (\@array, $string);
2616: }
2617:
2618: $string =~ s/^__ARRAY_REF__//;
2619:
2620: while($string !~ /^__END_ARRAY_REF__/) {
2621: my $value='';
2622: if($string =~ /^__HASH_REF__/) {
2623: ($value, $string)=&str2hashref($string);
2624: if(defined($value->{'error'})) {
2625: $array[0] ='Array reference error';
2626: return (\@array, $string);
2627: }
2628: } elsif($string =~ /^__ARRAY_REF__/) {
2629: ($value, $string)=&str2arrayref($string);
2630: if($value->[0] eq 'Array reference error') {
2631: $array[0] ='Array reference error';
2632: return (\@array, $string);
2633: }
2634: } else {
2635: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
2636: }
2637: $string =~ s/^&//;
2638:
2639: push(@array, $value);
1.191 harris41 2640: }
1.265 albertel 2641:
2642: $string =~ s/^__END_ARRAY_REF__//;
2643:
2644: return (\@array, $string);
1.168 albertel 2645: }
2646:
1.167 albertel 2647: # -------------------------------------------------------------------Temp Store
2648:
1.168 albertel 2649: sub tmpreset {
2650: my ($symb,$namespace,$domain,$stuname) = @_;
2651: if (!$symb) {
2652: $symb=&symbread();
1.620 albertel 2653: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2654: }
2655: $symb=escape($symb);
2656:
1.620 albertel 2657: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 2658: $namespace=~s/\//\_/g;
2659: $namespace=~s/\W//g;
2660:
1.620 albertel 2661: if (!$domain) { $domain=$env{'user.domain'}; }
2662: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2663: if ($domain eq 'public' && $stuname eq 'public') {
2664: $stuname=$ENV{'REMOTE_ADDR'};
2665: }
1.168 albertel 2666: my $path=$perlvar{'lonDaemons'}.'/tmp';
2667: my %hash;
2668: if (tie(%hash,'GDBM_File',
2669: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2670: &GDBM_WRCREAT(),0640)) {
1.168 albertel 2671: foreach my $key (keys %hash) {
1.180 albertel 2672: if ($key=~ /:$symb/) {
1.168 albertel 2673: delete($hash{$key});
2674: }
2675: }
2676: }
2677: }
2678:
1.167 albertel 2679: sub tmpstore {
1.168 albertel 2680: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2681:
2682: if (!$symb) {
2683: $symb=&symbread();
1.620 albertel 2684: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2685: }
2686: $symb=escape($symb);
2687:
2688: if (!$namespace) {
2689: # I don't think we would ever want to store this for a course.
2690: # it seems this will only be used if we don't have a course.
1.620 albertel 2691: #$namespace=$env{'request.course.id'};
1.168 albertel 2692: #if (!$namespace) {
1.620 albertel 2693: $namespace=$env{'request.state'};
1.168 albertel 2694: #}
2695: }
2696: $namespace=~s/\//\_/g;
2697: $namespace=~s/\W//g;
1.620 albertel 2698: if (!$domain) { $domain=$env{'user.domain'}; }
2699: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2700: if ($domain eq 'public' && $stuname eq 'public') {
2701: $stuname=$ENV{'REMOTE_ADDR'};
2702: }
1.168 albertel 2703: my $now=time;
2704: my %hash;
2705: my $path=$perlvar{'lonDaemons'}.'/tmp';
2706: if (tie(%hash,'GDBM_File',
2707: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2708: &GDBM_WRCREAT(),0640)) {
1.168 albertel 2709: $hash{"version:$symb"}++;
2710: my $version=$hash{"version:$symb"};
2711: my $allkeys='';
2712: foreach my $key (keys(%$storehash)) {
2713: $allkeys.=$key.':';
1.591 albertel 2714: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 2715: }
2716: $hash{"$version:$symb:timestamp"}=$now;
2717: $allkeys.='timestamp';
2718: $hash{"$version:keys:$symb"}=$allkeys;
2719: if (untie(%hash)) {
2720: return 'ok';
2721: } else {
2722: return "error:$!";
2723: }
2724: } else {
2725: return "error:$!";
2726: }
2727: }
1.167 albertel 2728:
1.168 albertel 2729: # -----------------------------------------------------------------Temp Restore
1.167 albertel 2730:
1.168 albertel 2731: sub tmprestore {
2732: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 2733:
1.168 albertel 2734: if (!$symb) {
2735: $symb=&symbread();
1.620 albertel 2736: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 2737: }
2738: $symb=escape($symb);
2739:
1.620 albertel 2740: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 2741:
1.620 albertel 2742: if (!$domain) { $domain=$env{'user.domain'}; }
2743: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 2744: if ($domain eq 'public' && $stuname eq 'public') {
2745: $stuname=$ENV{'REMOTE_ADDR'};
2746: }
1.168 albertel 2747: my %returnhash;
2748: $namespace=~s/\//\_/g;
2749: $namespace=~s/\W//g;
2750: my %hash;
2751: my $path=$perlvar{'lonDaemons'}.'/tmp';
2752: if (tie(%hash,'GDBM_File',
2753: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 2754: &GDBM_READER(),0640)) {
1.168 albertel 2755: my $version=$hash{"version:$symb"};
2756: $returnhash{'version'}=$version;
2757: my $scope;
2758: for ($scope=1;$scope<=$version;$scope++) {
2759: my $vkeys=$hash{"$scope:keys:$symb"};
2760: my @keys=split(/:/,$vkeys);
2761: my $key;
2762: $returnhash{"$scope:keys"}=$vkeys;
2763: foreach $key (@keys) {
1.591 albertel 2764: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
2765: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 2766: }
2767: }
1.168 albertel 2768: if (!(untie(%hash))) {
2769: return "error:$!";
2770: }
2771: } else {
2772: return "error:$!";
2773: }
2774: return %returnhash;
1.167 albertel 2775: }
2776:
1.9 www 2777: # ----------------------------------------------------------------------- Store
2778:
2779: sub store {
1.124 www 2780: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2781: my $home='';
2782:
1.168 albertel 2783: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2784:
1.213 www 2785: $symb=&symbclean($symb);
1.122 albertel 2786: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 2787:
1.620 albertel 2788: if (!$domain) { $domain=$env{'user.domain'}; }
2789: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 2790:
2791: &devalidate($symb,$stuname,$domain);
1.109 www 2792:
2793: $symb=escape($symb);
1.187 www 2794: if (!$namespace) {
1.620 albertel 2795: unless ($namespace=$env{'request.course.id'}) {
1.187 www 2796: return '';
2797: }
2798: }
1.620 albertel 2799: if (!$home) { $home=$env{'user.home'}; }
1.447 www 2800:
2801: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2802: $$storehash{'host'}=$perlvar{'lonHostID'};
2803:
1.12 www 2804: my $namevalue='';
1.800 albertel 2805: foreach my $key (keys(%$storehash)) {
2806: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 2807: }
1.12 www 2808: $namevalue=~s/\&$//;
1.187 www 2809: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 2810: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 2811: }
2812:
1.47 www 2813: # -------------------------------------------------------------- Critical Store
2814:
2815: sub cstore {
1.124 www 2816: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
2817: my $home='';
2818:
1.168 albertel 2819: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2820:
1.213 www 2821: $symb=&symbclean($symb);
1.122 albertel 2822: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 2823:
1.620 albertel 2824: if (!$domain) { $domain=$env{'user.domain'}; }
2825: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 2826:
2827: &devalidate($symb,$stuname,$domain);
1.109 www 2828:
2829: $symb=escape($symb);
1.187 www 2830: if (!$namespace) {
1.620 albertel 2831: unless ($namespace=$env{'request.course.id'}) {
1.187 www 2832: return '';
2833: }
2834: }
1.620 albertel 2835: if (!$home) { $home=$env{'user.home'}; }
1.447 www 2836:
2837: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
2838: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 2839:
1.47 www 2840: my $namevalue='';
1.800 albertel 2841: foreach my $key (keys(%$storehash)) {
2842: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 2843: }
1.47 www 2844: $namevalue=~s/\&$//;
1.187 www 2845: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 2846: return critical
2847: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 2848: }
2849:
1.9 www 2850: # --------------------------------------------------------------------- Restore
2851:
2852: sub restore {
1.124 www 2853: my ($symb,$namespace,$domain,$stuname) = @_;
2854: my $home='';
2855:
1.168 albertel 2856: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 2857:
1.122 albertel 2858: if (!$symb) {
2859: unless ($symb=escape(&symbread())) { return ''; }
2860: } else {
1.213 www 2861: $symb=&escape(&symbclean($symb));
1.122 albertel 2862: }
1.188 www 2863: if (!$namespace) {
1.620 albertel 2864: unless ($namespace=$env{'request.course.id'}) {
1.188 www 2865: return '';
2866: }
2867: }
1.620 albertel 2868: if (!$domain) { $domain=$env{'user.domain'}; }
2869: if (!$stuname) { $stuname=$env{'user.name'}; }
2870: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 2871: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
2872:
1.12 www 2873: my %returnhash=();
1.800 albertel 2874: foreach my $line (split(/\&/,$answer)) {
2875: my ($name,$value)=split(/\=/,$line);
1.591 albertel 2876: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 2877: }
1.75 www 2878: my $version;
2879: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 2880: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
2881: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 2882: }
1.75 www 2883: }
1.13 www 2884: return %returnhash;
1.34 www 2885: }
2886:
2887: # ---------------------------------------------------------- Course Description
2888:
2889: sub coursedescription {
1.731 albertel 2890: my ($courseid,$args)=@_;
1.34 www 2891: $courseid=~s/^\///;
1.49 www 2892: $courseid=~s/\_/\//g;
1.34 www 2893: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 2894: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 2895: my $normalid=$cdomain.'_'.$cnum;
2896: # need to always cache even if we get errors otherwise we keep
2897: # trying and trying and trying to get the course description.
2898: my %envhash=();
2899: my %returnhash=();
1.731 albertel 2900:
2901: my $expiretime=600;
2902: if ($env{'request.course.id'} eq $normalid) {
2903: $expiretime=120;
2904: }
2905:
2906: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
2907: if (!$args->{'freshen_cache'}
2908: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
2909: foreach my $key (keys(%env)) {
2910: next if ($key !~ /^\Q$prefix\E(.*)/);
2911: my ($setting) = $1;
2912: $returnhash{$setting} = $env{$key};
2913: }
2914: return %returnhash;
2915: }
2916:
2917: # get the data agin
2918: if (!$args->{'one_time'}) {
2919: $envhash{'course.'.$normalid.'.last_cache'}=time;
2920: }
1.811 albertel 2921:
1.34 www 2922: if ($chome ne 'no_host') {
1.302 albertel 2923: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 2924: if (!exists($returnhash{'con_lost'})) {
2925: $returnhash{'home'}= $chome;
2926: $returnhash{'domain'} = $cdomain;
2927: $returnhash{'num'} = $cnum;
1.741 raeburn 2928: if (!defined($returnhash{'type'})) {
2929: $returnhash{'type'} = 'Course';
2930: }
1.130 albertel 2931: while (my ($name,$value) = each %returnhash) {
1.53 www 2932: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 2933: }
1.270 www 2934: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 2935: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 2936: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 2937: $envhash{'course.'.$normalid.'.home'}=$chome;
2938: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
2939: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 2940: }
2941: }
1.731 albertel 2942: if (!$args->{'one_time'}) {
2943: &appenv(%envhash);
2944: }
1.302 albertel 2945: return %returnhash;
1.461 www 2946: }
2947:
2948: # -------------------------------------------------See if a user is privileged
2949:
2950: sub privileged {
2951: my ($username,$domain)=@_;
2952: my $rolesdump=&reply("dump:$domain:$username:roles",
2953: &homeserver($username,$domain));
2954: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
2955: my $now=time;
2956: if ($rolesdump ne '') {
1.800 albertel 2957: foreach my $entry (split(/&/,$rolesdump)) {
2958: if ($entry!~/^rolesdef_/) {
2959: my ($area,$role)=split(/=/,$entry);
1.461 www 2960: $area=~s/\_\w\w$//;
2961: my ($trole,$tend,$tstart)=split(/_/,$role);
2962: if (($trole eq 'dc') || ($trole eq 'su')) {
2963: my $active=1;
2964: if ($tend) {
2965: if ($tend<$now) { $active=0; }
2966: }
2967: if ($tstart) {
2968: if ($tstart>$now) { $active=0; }
2969: }
2970: if ($active) { return 1; }
2971: }
2972: }
2973: }
2974: }
2975: return 0;
1.9 www 2976: }
1.1 albertel 2977:
1.103 harris41 2978: # -------------------------------------------------------- Get user privileges
1.11 www 2979:
2980: sub rolesinit {
2981: my ($domain,$username,$authhost)=@_;
2982: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12 www 2983: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11 www 2984: my %allroles=();
1.678 raeburn 2985: my %allgroups=();
1.11 www 2986: my $now=time;
1.743 albertel 2987: my %userroles = ('user.login.time' => $now);
1.678 raeburn 2988: my $group_privs;
1.11 www 2989:
2990: if ($rolesdump ne '') {
1.800 albertel 2991: foreach my $entry (split(/&/,$rolesdump)) {
2992: if ($entry!~/^rolesdef_/) {
2993: my ($area,$role)=split(/=/,$entry);
1.587 albertel 2994: $area=~s/\_\w\w$//;
1.678 raeburn 2995: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 2996: if ($role=~/^cr/) {
1.807 albertel 2997: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
2998: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 2999: ($tend,$tstart)=split('_',$trest);
3000: } else {
3001: $trole=$role;
3002: }
1.678 raeburn 3003: } elsif ($role =~ m|^gr/|) {
3004: ($trole,$tend,$tstart) = split(/_/,$role);
3005: ($trole,$group_privs) = split(/\//,$trole);
3006: $group_privs = &unescape($group_privs);
1.587 albertel 3007: } else {
3008: ($trole,$tend,$tstart)=split(/_/,$role);
3009: }
1.743 albertel 3010: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
3011: $username);
3012: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 3013: if (($tend!=0) && ($tend<$now)) { $trole=''; }
3014: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 3015: if (($area ne '') && ($trole ne '')) {
1.347 albertel 3016: my $spec=$trole.'.'.$area;
3017: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
3018: if ($trole =~ /^cr\//) {
1.567 raeburn 3019: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 3020: } elsif ($trole eq 'gr') {
3021: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 3022: } else {
1.567 raeburn 3023: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 3024: }
1.12 www 3025: }
1.662 raeburn 3026: }
1.191 harris41 3027: }
1.743 albertel 3028: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
3029: $userroles{'user.adv'} = $adv;
3030: $userroles{'user.author'} = $author;
1.620 albertel 3031: $env{'user.adv'}=$adv;
1.11 www 3032: }
1.743 albertel 3033: return \%userroles;
1.11 www 3034: }
3035:
1.567 raeburn 3036: sub set_arearole {
3037: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
3038: # log the associated role with the area
3039: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 3040: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 3041: }
3042:
3043: sub custom_roleprivs {
3044: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
3045: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
3046: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 3047: if (&hostname($homsvr) ne '') {
1.567 raeburn 3048: my ($rdummy,$roledef)=
3049: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
3050: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
3051: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
3052: if (defined($syspriv)) {
3053: $$allroles{'cm./'}.=':'.$syspriv;
3054: $$allroles{$spec.'./'}.=':'.$syspriv;
3055: }
3056: if ($tdomain ne '') {
3057: if (defined($dompriv)) {
3058: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
3059: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
3060: }
3061: if (($trest ne '') && (defined($coursepriv))) {
3062: $$allroles{'cm.'.$area}.=':'.$coursepriv;
3063: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
3064: }
3065: }
3066: }
3067: }
3068: }
3069:
1.678 raeburn 3070: sub group_roleprivs {
3071: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
3072: my $access = 1;
3073: my $now = time;
3074: if (($tend!=0) && ($tend<$now)) { $access = 0; }
3075: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
3076: if ($access) {
1.811 albertel 3077: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 3078: $$allgroups{$course}{$group} .=':'.$group_privs;
3079: }
3080: }
1.567 raeburn 3081:
3082: sub standard_roleprivs {
3083: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
3084: if (defined($pr{$trole.':s'})) {
3085: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
3086: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
3087: }
3088: if ($tdomain ne '') {
3089: if (defined($pr{$trole.':d'})) {
3090: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3091: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3092: }
3093: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
3094: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
3095: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
3096: }
3097: }
3098: }
3099:
3100: sub set_userprivs {
1.678 raeburn 3101: my ($userroles,$allroles,$allgroups) = @_;
1.567 raeburn 3102: my $author=0;
3103: my $adv=0;
1.678 raeburn 3104: my %grouproles = ();
3105: if (keys(%{$allgroups}) > 0) {
3106: foreach my $role (keys %{$allroles}) {
1.681 raeburn 3107: my ($trole,$area,$sec,$extendedarea);
1.811 albertel 3108: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)-) {
1.678 raeburn 3109: $trole = $1;
3110: $area = $2;
1.681 raeburn 3111: $sec = $3;
3112: $extendedarea = $area.$sec;
3113: if (exists($$allgroups{$area})) {
3114: foreach my $group (keys(%{$$allgroups{$area}})) {
3115: my $spec = $trole.'.'.$extendedarea;
3116: $grouproles{$spec.'.'.$area.'/'.$group} =
3117: $$allgroups{$area}{$group};
1.678 raeburn 3118: }
3119: }
3120: }
3121: }
3122: }
1.800 albertel 3123: foreach my $group (keys(%grouproles)) {
3124: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 3125: }
1.800 albertel 3126: foreach my $role (keys(%{$allroles})) {
3127: my %thesepriv;
3128: if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
3129: foreach my $item (split(/:/,$$allroles{$role})) {
3130: if ($item ne '') {
3131: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 3132: if ($restrictions eq '') {
3133: $thesepriv{$privilege}='F';
3134: } elsif ($thesepriv{$privilege} ne 'F') {
3135: $thesepriv{$privilege}.=$restrictions;
3136: }
3137: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
3138: }
3139: }
3140: my $thesestr='';
1.800 albertel 3141: foreach my $priv (keys(%thesepriv)) {
3142: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
3143: }
3144: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 3145: }
3146: return ($author,$adv);
3147: }
3148:
1.12 www 3149: # --------------------------------------------------------------- get interface
3150:
3151: sub get {
1.131 albertel 3152: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 3153: my $items='';
1.800 albertel 3154: foreach my $item (@$storearr) {
3155: $items.=&escape($item).'&';
1.191 harris41 3156: }
1.12 www 3157: $items=~s/\&$//;
1.620 albertel 3158: if (!$udomain) { $udomain=$env{'user.domain'}; }
3159: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 3160: my $uhome=&homeserver($uname,$udomain);
3161:
1.133 albertel 3162: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 3163: my @pairs=split(/\&/,$rep);
1.273 albertel 3164: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
3165: return @pairs;
3166: }
1.15 www 3167: my %returnhash=();
1.42 www 3168: my $i=0;
1.800 albertel 3169: foreach my $item (@$storearr) {
3170: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 3171: $i++;
1.191 harris41 3172: }
1.15 www 3173: return %returnhash;
1.27 www 3174: }
3175:
3176: # --------------------------------------------------------------- del interface
3177:
3178: sub del {
1.133 albertel 3179: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 3180: my $items='';
1.800 albertel 3181: foreach my $item (@$storearr) {
3182: $items.=&escape($item).'&';
1.191 harris41 3183: }
1.27 www 3184: $items=~s/\&$//;
1.620 albertel 3185: if (!$udomain) { $udomain=$env{'user.domain'}; }
3186: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 3187: my $uhome=&homeserver($uname,$udomain);
3188:
3189: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 3190: }
3191:
3192: # -------------------------------------------------------------- dump interface
3193:
3194: sub dump {
1.755 albertel 3195: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
3196: if (!$udomain) { $udomain=$env{'user.domain'}; }
3197: if (!$uname) { $uname=$env{'user.name'}; }
3198: my $uhome=&homeserver($uname,$udomain);
3199: if ($regexp) {
3200: $regexp=&escape($regexp);
3201: } else {
3202: $regexp='.';
3203: }
3204: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3205: my @pairs=split(/\&/,$rep);
3206: my %returnhash=();
3207: foreach my $item (@pairs) {
3208: my ($key,$value)=split(/=/,$item,2);
3209: $key = &unescape($key);
3210: next if ($key =~ /^error: 2 /);
3211: $returnhash{$key}=&thaw_unescape($value);
3212: }
3213: return %returnhash;
1.407 www 3214: }
3215:
1.717 albertel 3216: # --------------------------------------------------------- dumpstore interface
3217:
3218: sub dumpstore {
3219: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 3220: if (!$udomain) { $udomain=$env{'user.domain'}; }
3221: if (!$uname) { $uname=$env{'user.name'}; }
3222: my $uhome=&homeserver($uname,$udomain);
3223: if ($regexp) {
3224: $regexp=&escape($regexp);
3225: } else {
3226: $regexp='.';
3227: }
3228: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3229: my @pairs=split(/\&/,$rep);
3230: my %returnhash=();
3231: foreach my $item (@pairs) {
3232: my ($key,$value)=split(/=/,$item,2);
3233: next if ($key =~ /^error: 2 /);
3234: $returnhash{$key}=&thaw_unescape($value);
3235: }
3236: return %returnhash;
1.717 albertel 3237: }
3238:
1.407 www 3239: # -------------------------------------------------------------- keys interface
3240:
3241: sub getkeys {
3242: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 3243: if (!$udomain) { $udomain=$env{'user.domain'}; }
3244: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 3245: my $uhome=&homeserver($uname,$udomain);
3246: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
3247: my @keyarray=();
1.800 albertel 3248: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 3249: next if ($key =~ /^error: 2 /);
1.800 albertel 3250: push(@keyarray,&unescape($key));
1.407 www 3251: }
3252: return @keyarray;
1.318 matthew 3253: }
3254:
1.319 matthew 3255: # --------------------------------------------------------------- currentdump
3256: sub currentdump {
1.328 matthew 3257: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 3258: $courseid = $env{'request.course.id'} if (! defined($courseid));
3259: $sdom = $env{'user.domain'} if (! defined($sdom));
3260: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 3261: my $uhome = &homeserver($sname,$sdom);
3262: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 3263: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 3264: #
1.318 matthew 3265: my %returnhash=();
1.319 matthew 3266: #
3267: if ($rep eq "unknown_cmd") {
3268: # an old lond will not know currentdump
3269: # Do a dump and make it look like a currentdump
1.822 albertel 3270: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 3271: return if ($tmp[0] =~ /^(error:|no_such_host)/);
3272: my %hash = @tmp;
3273: @tmp=();
1.424 matthew 3274: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 3275: } else {
3276: my @pairs=split(/\&/,$rep);
1.800 albertel 3277: foreach my $pair (@pairs) {
3278: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 3279: my ($symb,$param) = split(/:/,$key);
3280: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 3281: &thaw_unescape($value);
1.319 matthew 3282: }
1.191 harris41 3283: }
1.12 www 3284: return %returnhash;
1.424 matthew 3285: }
3286:
3287: sub convert_dump_to_currentdump{
3288: my %hash = %{shift()};
3289: my %returnhash;
3290: # Code ripped from lond, essentially. The only difference
3291: # here is the unescaping done by lonnet::dump(). Conceivably
3292: # we might run in to problems with parameter names =~ /^v\./
3293: while (my ($key,$value) = each(%hash)) {
3294: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 3295: $symb = &unescape($symb);
3296: $param = &unescape($param);
1.424 matthew 3297: next if ($v eq 'version' || $symb eq 'keys');
3298: next if (exists($returnhash{$symb}) &&
3299: exists($returnhash{$symb}->{$param}) &&
3300: $returnhash{$symb}->{'v.'.$param} > $v);
3301: $returnhash{$symb}->{$param}=$value;
3302: $returnhash{$symb}->{'v.'.$param}=$v;
3303: }
3304: #
3305: # Remove all of the keys in the hashes which keep track of
3306: # the version of the parameter.
3307: while (my ($symb,$param_hash) = each(%returnhash)) {
3308: # use a foreach because we are going to delete from the hash.
3309: foreach my $key (keys(%$param_hash)) {
3310: delete($param_hash->{$key}) if ($key =~ /^v\./);
3311: }
3312: }
3313: return \%returnhash;
1.12 www 3314: }
3315:
1.627 albertel 3316: # ------------------------------------------------------ critical inc interface
3317:
3318: sub cinc {
3319: return &inc(@_,'critical');
3320: }
3321:
1.449 matthew 3322: # --------------------------------------------------------------- inc interface
3323:
3324: sub inc {
1.627 albertel 3325: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 3326: if (!$udomain) { $udomain=$env{'user.domain'}; }
3327: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 3328: my $uhome=&homeserver($uname,$udomain);
3329: my $items='';
3330: if (! ref($store)) {
3331: # got a single value, so use that instead
3332: $items = &escape($store).'=&';
3333: } elsif (ref($store) eq 'SCALAR') {
3334: $items = &escape($$store).'=&';
3335: } elsif (ref($store) eq 'ARRAY') {
3336: $items = join('=&',map {&escape($_);} @{$store});
3337: } elsif (ref($store) eq 'HASH') {
3338: while (my($key,$value) = each(%{$store})) {
3339: $items.= &escape($key).'='.&escape($value).'&';
3340: }
3341: }
3342: $items=~s/\&$//;
1.627 albertel 3343: if ($critical) {
3344: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
3345: } else {
3346: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
3347: }
1.449 matthew 3348: }
3349:
1.12 www 3350: # --------------------------------------------------------------- put interface
3351:
3352: sub put {
1.134 albertel 3353: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 3354: if (!$udomain) { $udomain=$env{'user.domain'}; }
3355: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 3356: my $uhome=&homeserver($uname,$udomain);
1.12 www 3357: my $items='';
1.800 albertel 3358: foreach my $item (keys(%$storehash)) {
3359: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 3360: }
1.12 www 3361: $items=~s/\&$//;
1.134 albertel 3362: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 3363: }
3364:
1.631 albertel 3365: # ------------------------------------------------------------ newput interface
3366:
3367: sub newput {
3368: my ($namespace,$storehash,$udomain,$uname)=@_;
3369: if (!$udomain) { $udomain=$env{'user.domain'}; }
3370: if (!$uname) { $uname=$env{'user.name'}; }
3371: my $uhome=&homeserver($uname,$udomain);
3372: my $items='';
3373: foreach my $key (keys(%$storehash)) {
3374: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
3375: }
3376: $items=~s/\&$//;
3377: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
3378: }
3379:
3380: # --------------------------------------------------------- putstore interface
3381:
1.524 raeburn 3382: sub putstore {
1.715 albertel 3383: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 3384: if (!$udomain) { $udomain=$env{'user.domain'}; }
3385: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 3386: my $uhome=&homeserver($uname,$udomain);
3387: my $items='';
1.715 albertel 3388: foreach my $key (keys(%$storehash)) {
3389: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 3390: }
1.715 albertel 3391: $items=~s/\&$//;
1.716 albertel 3392: my $esc_symb=&escape($symb);
3393: my $esc_v=&escape($version);
1.715 albertel 3394: my $reply =
1.716 albertel 3395: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 3396: $uhome);
3397: if ($reply eq 'unknown_cmd') {
1.716 albertel 3398: # gfall back to way things use to be done
1.715 albertel 3399: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
3400: $uname);
1.524 raeburn 3401: }
1.715 albertel 3402: return $reply;
3403: }
3404:
3405: sub old_putstore {
1.716 albertel 3406: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
3407: if (!$udomain) { $udomain=$env{'user.domain'}; }
3408: if (!$uname) { $uname=$env{'user.name'}; }
3409: my $uhome=&homeserver($uname,$udomain);
3410: my %newstorehash;
1.800 albertel 3411: foreach my $item (keys(%$storehash)) {
3412: my $key = $version.':'.&escape($symb).':'.$item;
3413: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 3414: }
3415: my $items='';
3416: my %allitems = ();
1.800 albertel 3417: foreach my $item (keys(%newstorehash)) {
3418: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 3419: my $key = $1.':keys:'.$2;
3420: $allitems{$key} .= $3.':';
3421: }
1.800 albertel 3422: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 3423: }
1.800 albertel 3424: foreach my $item (keys(%allitems)) {
3425: $allitems{$item} =~ s/\:$//;
3426: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 3427: }
3428: $items=~s/\&$//;
3429: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 3430: }
3431:
1.47 www 3432: # ------------------------------------------------------ critical put interface
3433:
3434: sub cput {
1.134 albertel 3435: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 3436: if (!$udomain) { $udomain=$env{'user.domain'}; }
3437: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 3438: my $uhome=&homeserver($uname,$udomain);
1.47 www 3439: my $items='';
1.800 albertel 3440: foreach my $item (keys(%$storehash)) {
3441: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 3442: }
1.47 www 3443: $items=~s/\&$//;
1.134 albertel 3444: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3445: }
3446:
3447: # -------------------------------------------------------------- eget interface
3448:
3449: sub eget {
1.133 albertel 3450: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 3451: my $items='';
1.800 albertel 3452: foreach my $item (@$storearr) {
3453: $items.=&escape($item).'&';
1.191 harris41 3454: }
1.12 www 3455: $items=~s/\&$//;
1.620 albertel 3456: if (!$udomain) { $udomain=$env{'user.domain'}; }
3457: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 3458: my $uhome=&homeserver($uname,$udomain);
3459: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3460: my @pairs=split(/\&/,$rep);
3461: my %returnhash=();
1.42 www 3462: my $i=0;
1.800 albertel 3463: foreach my $item (@$storearr) {
3464: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 3465: $i++;
1.191 harris41 3466: }
1.12 www 3467: return %returnhash;
3468: }
3469:
1.667 albertel 3470: # ------------------------------------------------------------ tmpput interface
3471: sub tmpput {
1.802 raeburn 3472: my ($storehash,$server,$context)=@_;
1.667 albertel 3473: my $items='';
1.800 albertel 3474: foreach my $item (keys(%$storehash)) {
3475: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 3476: }
3477: $items=~s/\&$//;
1.802 raeburn 3478: if (defined($context)) {
3479: $items .= ':'.&escape($context);
3480: }
1.667 albertel 3481: return &reply("tmpput:$items",$server);
3482: }
3483:
3484: # ------------------------------------------------------------ tmpget interface
3485: sub tmpget {
1.688 albertel 3486: my ($token,$server)=@_;
3487: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3488: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 3489: my %returnhash;
3490: foreach my $item (split(/\&/,$rep)) {
3491: my ($key,$value)=split(/=/,$item);
3492: $returnhash{&unescape($key)}=&thaw_unescape($value);
3493: }
3494: return %returnhash;
3495: }
3496:
1.688 albertel 3497: # ------------------------------------------------------------ tmpget interface
3498: sub tmpdel {
3499: my ($token,$server)=@_;
3500: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3501: return &reply("tmpdel:$token",$server);
3502: }
3503:
1.765 albertel 3504: # -------------------------------------------------- portfolio access checking
3505:
3506: sub portfolio_access {
1.766 albertel 3507: my ($requrl) = @_;
1.765 albertel 3508: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
3509: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 3510: if ($result) {
3511: my %setters;
3512: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
3513: my ($startblock,$endblock) =
3514: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
3515: if ($startblock && $endblock) {
3516: return 'B';
3517: }
3518: } else {
3519: my ($startblock,$endblock) =
3520: &Apache::loncommon::blockcheck(\%setters,'port');
3521: if ($startblock && $endblock) {
3522: return 'B';
3523: }
3524: }
3525: }
1.765 albertel 3526: if ($result eq 'ok') {
1.766 albertel 3527: return 'F';
1.765 albertel 3528: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 3529: return 'A';
1.765 albertel 3530: }
1.766 albertel 3531: return '';
1.765 albertel 3532: }
3533:
3534: sub get_portfolio_access {
1.767 albertel 3535: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
3536:
3537: if (!ref($access_hash)) {
3538: my $current_perms = &get_portfile_permissions($udom,$unum);
3539: my %access_controls = &get_access_controls($current_perms,$group,
3540: $file_name);
3541: $access_hash = $access_controls{$file_name};
3542: }
3543:
1.765 albertel 3544: my ($public,$guest,@domains,@users,@courses,@groups);
3545: my $now = time;
3546: if (ref($access_hash) eq 'HASH') {
3547: foreach my $key (keys(%{$access_hash})) {
3548: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
3549: if ($start > $now) {
3550: next;
3551: }
3552: if ($end && $end<$now) {
3553: next;
3554: }
3555: if ($scope eq 'public') {
3556: $public = $key;
3557: last;
3558: } elsif ($scope eq 'guest') {
3559: $guest = $key;
3560: } elsif ($scope eq 'domains') {
3561: push(@domains,$key);
3562: } elsif ($scope eq 'users') {
3563: push(@users,$key);
3564: } elsif ($scope eq 'course') {
3565: push(@courses,$key);
3566: } elsif ($scope eq 'group') {
3567: push(@groups,$key);
3568: }
3569: }
3570: if ($public) {
3571: return 'ok';
3572: }
3573: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
3574: if ($guest) {
3575: return $guest;
3576: }
3577: } else {
3578: if (@domains > 0) {
3579: foreach my $domkey (@domains) {
3580: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
3581: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
3582: return 'ok';
3583: }
3584: }
3585: }
3586: }
3587: if (@users > 0) {
3588: foreach my $userkey (@users) {
1.865 ! raeburn 3589: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
! 3590: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
! 3591: if (ref($item) eq 'HASH') {
! 3592: if (($item->{'uname'} eq $env{'user.name'}) &&
! 3593: ($item->{'udom'} eq $env{'user.domain'})) {
! 3594: return 'ok';
! 3595: }
! 3596: }
! 3597: }
! 3598: }
1.765 albertel 3599: }
3600: }
3601: my %roleshash;
3602: my @courses_and_groups = @courses;
3603: push(@courses_and_groups,@groups);
3604: if (@courses_and_groups > 0) {
3605: my (%allgroups,%allroles);
3606: my ($start,$end,$role,$sec,$group);
3607: foreach my $envkey (%env) {
1.811 albertel 3608: if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 3609: my $cid = $2.'_'.$3;
3610: if ($1 eq 'gr') {
3611: $group = $4;
3612: $allgroups{$cid}{$group} = $env{$envkey};
3613: } else {
3614: if ($4 eq '') {
3615: $sec = 'none';
3616: } else {
3617: $sec = $4;
3618: }
3619: $allroles{$cid}{$1}{$sec} = $env{$envkey};
3620: }
1.811 albertel 3621: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 3622: my $cid = $2.'_'.$3;
3623: if ($4 eq '') {
3624: $sec = 'none';
3625: } else {
3626: $sec = $4;
3627: }
3628: $allroles{$cid}{$1}{$sec} = $env{$envkey};
3629: }
3630: }
3631: if (keys(%allroles) == 0) {
3632: return;
3633: }
3634: foreach my $key (@courses_and_groups) {
3635: my %content = %{$$access_hash{$key}};
3636: my $cnum = $content{'number'};
3637: my $cdom = $content{'domain'};
3638: my $cid = $cdom.'_'.$cnum;
3639: if (!exists($allroles{$cid})) {
3640: next;
3641: }
3642: foreach my $role_id (keys(%{$content{'roles'}})) {
3643: my @sections = @{$content{'roles'}{$role_id}{'section'}};
3644: my @groups = @{$content{'roles'}{$role_id}{'group'}};
3645: my @status = @{$content{'roles'}{$role_id}{'access'}};
3646: my @roles = @{$content{'roles'}{$role_id}{'role'}};
3647: foreach my $role (keys(%{$allroles{$cid}})) {
3648: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
3649: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
3650: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
3651: if (grep/^all$/,@sections) {
3652: return 'ok';
3653: } else {
3654: if (grep/^$sec$/,@sections) {
3655: return 'ok';
3656: }
3657: }
3658: }
3659: }
3660: if (keys(%{$allgroups{$cid}}) == 0) {
3661: if (grep/^none$/,@groups) {
3662: return 'ok';
3663: }
3664: } else {
3665: if (grep/^all$/,@groups) {
3666: return 'ok';
3667: }
3668: foreach my $group (keys(%{$allgroups{$cid}})) {
3669: if (grep/^$group$/,@groups) {
3670: return 'ok';
3671: }
3672: }
3673: }
3674: }
3675: }
3676: }
3677: }
3678: }
3679: if ($guest) {
3680: return $guest;
3681: }
3682: }
3683: }
3684: return;
3685: }
3686:
3687: sub course_group_datechecker {
3688: my ($dates,$now,$status) = @_;
3689: my ($start,$end) = split(/\./,$dates);
3690: if (!$start && !$end) {
3691: return 'ok';
3692: }
3693: if (grep/^active$/,@{$status}) {
3694: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
3695: return 'ok';
3696: }
3697: }
3698: if (grep/^previous$/,@{$status}) {
3699: if ($end > $now ) {
3700: return 'ok';
3701: }
3702: }
3703: if (grep/^future$/,@{$status}) {
3704: if ($start > $now) {
3705: return 'ok';
3706: }
3707: }
3708: return;
3709: }
3710:
3711: sub parse_portfolio_url {
3712: my ($url) = @_;
3713:
3714: my ($type,$udom,$unum,$group,$file_name);
3715:
1.823 albertel 3716: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 3717: $type = 1;
3718: $udom = $1;
3719: $unum = $2;
3720: $file_name = $3;
1.823 albertel 3721: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 3722: $type = 2;
3723: $udom = $1;
3724: $unum = $2;
3725: $group = $3;
3726: $file_name = $3.'/'.$4;
3727: }
3728: if (wantarray) {
3729: return ($type,$udom,$unum,$file_name,$group);
3730: }
3731: return $type;
3732: }
3733:
3734: sub is_portfolio_url {
3735: my ($url) = @_;
3736: return scalar(&parse_portfolio_url($url));
3737: }
3738:
1.798 raeburn 3739: sub is_portfolio_file {
3740: my ($file) = @_;
1.820 raeburn 3741: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 3742: return 1;
3743: }
3744: return;
3745: }
3746:
3747:
1.341 www 3748: # ---------------------------------------------- Custom access rule evaluation
3749:
3750: sub customaccess {
3751: my ($priv,$uri)=@_;
1.807 albertel 3752: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 3753: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 3754: $udom = &LONCAPA::clean_domain($udom);
3755: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 3756: my $access=0;
1.800 albertel 3757: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
3758: my ($effect,$realm,$role)=split(/\:/,$right);
1.343 www 3759: if ($role) {
3760: if ($role ne $urole) { next; }
3761: }
1.800 albertel 3762: foreach my $scope (split(/\s*\,\s*/,$realm)) {
3763: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
1.343 www 3764: if ($tdom) {
3765: if ($tdom ne $udom) { next; }
3766: }
3767: if ($tcrs) {
3768: if ($tcrs ne $ucrs) { next; }
3769: }
3770: if ($tsec) {
3771: if ($tsec ne $usec) { next; }
3772: }
3773: $access=($effect eq 'allow');
3774: last;
1.342 www 3775: }
1.402 bowersj2 3776: if ($realm eq '' && $role eq '') {
3777: $access=($effect eq 'allow');
3778: }
1.341 www 3779: }
3780: return $access;
3781: }
3782:
1.103 harris41 3783: # ------------------------------------------------- Check for a user privilege
1.12 www 3784:
3785: sub allowed {
1.810 raeburn 3786: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 3787: my $ver_orguri=$uri;
1.439 www 3788: $uri=&deversion($uri);
1.152 www 3789: my $orguri=$uri;
1.52 www 3790: $uri=&declutter($uri);
1.809 raeburn 3791:
1.810 raeburn 3792: if ($priv eq 'evb') {
3793: # Evade communication block restrictions for specified role in a course
3794: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
3795: return $1;
3796: } else {
3797: return;
3798: }
3799: }
3800:
1.620 albertel 3801: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 3802: # Free bre access to adm and meta resources
1.775 albertel 3803: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 3804: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
3805: && ($priv eq 'bre')) {
1.14 www 3806: return 'F';
1.159 www 3807: }
3808:
1.545 banghart 3809: # Free bre access to user's own portfolio contents
1.714 raeburn 3810: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 3811: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 3812: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 3813: my %setters;
3814: my ($startblock,$endblock) =
3815: &Apache::loncommon::blockcheck(\%setters,'port');
3816: if ($startblock && $endblock) {
3817: return 'B';
3818: } else {
3819: return 'F';
3820: }
1.545 banghart 3821: }
3822:
1.762 raeburn 3823: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 3824: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
3825: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
3826: if (exists($env{'request.course.id'})) {
3827: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
3828: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
3829: if (($domain eq $cdom) && ($name eq $cnum)) {
3830: my $courseprivid=$env{'request.course.id'};
3831: $courseprivid=~s/\_/\//;
3832: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
3833: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
3834: return $1;
1.762 raeburn 3835: } else {
3836: if ($env{'request.course.sec'}) {
3837: $courseprivid.='/'.$env{'request.course.sec'};
3838: }
3839: if ($env{'user.priv.'.$env{'request.role'}.'./'.
3840: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
3841: return $2;
3842: }
1.714 raeburn 3843: }
3844: }
3845: }
3846: }
3847:
1.159 www 3848: # Free bre to public access
3849:
3850: if ($priv eq 'bre') {
1.238 www 3851: my $copyright=&metadata($uri,'copyright');
1.620 albertel 3852: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 3853: return 'F';
3854: }
1.238 www 3855: if ($copyright eq 'priv') {
3856: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 3857: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 3858: return '';
3859: }
3860: }
3861: if ($copyright eq 'domain') {
3862: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 3863: unless (($env{'user.domain'} eq $1) ||
3864: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 3865: return '';
3866: }
1.262 matthew 3867: }
1.620 albertel 3868: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 3869: # Library role, so allow browsing of resources in this domain.
3870: return 'F';
1.238 www 3871: }
1.341 www 3872: if ($copyright eq 'custom') {
3873: unless (&customaccess($priv,$uri)) { return ''; }
3874: }
1.14 www 3875: }
1.264 matthew 3876: # Domain coordinator is trying to create a course
1.620 albertel 3877: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 3878: # uri is the requested domain in this case.
3879: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 3880: # a role of dc for the domain in question.
1.620 albertel 3881: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 3882: }
1.29 www 3883:
1.52 www 3884: my $thisallowed='';
3885: my $statecond=0;
3886: my $courseprivid='';
3887:
3888: # Course
3889:
1.620 albertel 3890: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52 www 3891: $thisallowed.=$1;
3892: }
1.29 www 3893:
1.52 www 3894: # Domain
3895:
1.620 albertel 3896: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 3897: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 3898: $thisallowed.=$1;
3899: }
1.52 www 3900:
3901: # Course: uri itself is a course
1.66 www 3902: my $courseuri=$uri;
3903: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 3904: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 3905:
1.620 albertel 3906: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 3907: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 3908: $thisallowed.=$1;
3909: }
1.29 www 3910:
1.665 albertel 3911: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 3912: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 3913: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 3914: $thisallowed='';
1.671 raeburn 3915: my ($match)=&is_on_map($uri);
3916: if ($match) {
3917: if ($env{'user.priv.'.$env{'request.role'}.'./'}
3918: =~/\Q$priv\E\&([^\:]*)/) {
3919: $thisallowed.=$1;
3920: }
3921: } else {
1.705 albertel 3922: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 3923: if ($refuri) {
3924: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 3925: $thisallowed='F';
1.671 raeburn 3926: } else {
3927: $refuri=&declutter($refuri);
3928: my ($match) = &is_on_map($refuri);
3929: if ($match) {
3930: $thisallowed='F';
3931: }
1.669 raeburn 3932: }
1.671 raeburn 3933: }
3934: }
1.314 www 3935: }
1.492 albertel 3936:
1.766 albertel 3937: if ($priv eq 'bre'
3938: && $thisallowed ne 'F'
3939: && $thisallowed ne '2'
3940: && &is_portfolio_url($uri)) {
3941: $thisallowed = &portfolio_access($uri);
3942: }
3943:
1.52 www 3944: # Full access at system, domain or course-wide level? Exit.
1.29 www 3945:
3946: if ($thisallowed=~/F/) {
3947: return 'F';
3948: }
3949:
1.52 www 3950: # If this is generating or modifying users, exit with special codes
1.29 www 3951:
1.643 www 3952: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
3953: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 3954: my ($audom,$auname)=split('/',$uri);
1.643 www 3955: # no author name given, so this just checks on the general right to make a co-author in this domain
3956: unless ($auname) { return $thisallowed; }
3957: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 3958: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
3959: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
3960: ($audom ne $env{'request.role.domain'}))) { return ''; }
3961: }
1.52 www 3962: return $thisallowed;
3963: }
3964: #
1.103 harris41 3965: # Gathered so far: system, domain and course wide privileges
1.52 www 3966: #
3967: # Course: See if uri or referer is an individual resource that is part of
3968: # the course
3969:
1.620 albertel 3970: if ($env{'request.course.id'}) {
1.232 www 3971:
1.620 albertel 3972: $courseprivid=$env{'request.course.id'};
3973: if ($env{'request.course.sec'}) {
3974: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 3975: }
3976: $courseprivid=~s/\_/\//;
3977: my $checkreferer=1;
1.232 www 3978: my ($match,$cond)=&is_on_map($uri);
3979: if ($match) {
3980: $statecond=$cond;
1.620 albertel 3981: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 3982: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 3983: $thisallowed.=$1;
3984: $checkreferer=0;
3985: }
1.29 www 3986: }
1.83 www 3987:
1.148 www 3988: if ($checkreferer) {
1.620 albertel 3989: my $refuri=$env{'httpref.'.$orguri};
1.148 www 3990: unless ($refuri) {
1.800 albertel 3991: foreach my $key (keys(%env)) {
3992: if ($key=~/^httpref\..*\*/) {
3993: my $pattern=$key;
1.156 www 3994: $pattern=~s/^httpref\.\/res\///;
1.148 www 3995: $pattern=~s/\*/\[\^\/\]\+/g;
3996: $pattern=~s/\//\\\//g;
1.152 www 3997: if ($orguri=~/$pattern/) {
1.800 albertel 3998: $refuri=$env{$key};
1.148 www 3999: }
4000: }
1.191 harris41 4001: }
1.148 www 4002: }
1.232 www 4003:
1.148 www 4004: if ($refuri) {
1.152 www 4005: $refuri=&declutter($refuri);
1.232 www 4006: my ($match,$cond)=&is_on_map($refuri);
4007: if ($match) {
4008: my $refstatecond=$cond;
1.620 albertel 4009: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 4010: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 4011: $thisallowed.=$1;
1.53 www 4012: $uri=$refuri;
4013: $statecond=$refstatecond;
1.52 www 4014: }
4015: }
1.148 www 4016: }
1.29 www 4017: }
1.52 www 4018: }
1.29 www 4019:
1.52 www 4020: #
1.103 harris41 4021: # Gathered now: all privileges that could apply, and condition number
1.52 www 4022: #
4023: #
4024: # Full or no access?
4025: #
1.29 www 4026:
1.52 www 4027: if ($thisallowed=~/F/) {
4028: return 'F';
4029: }
1.29 www 4030:
1.52 www 4031: unless ($thisallowed) {
4032: return '';
4033: }
1.29 www 4034:
1.52 www 4035: # Restrictions exist, deal with them
4036: #
4037: # C:according to course preferences
4038: # R:according to resource settings
4039: # L:unless locked
4040: # X:according to user session state
4041: #
4042:
4043: # Possibly locked functionality, check all courses
1.54 www 4044: # Locks might take effect only after 10 minutes cache expiration for other
4045: # courses, and 2 minutes for current course
1.52 www 4046:
4047: my $envkey;
4048: if ($thisallowed=~/L/) {
1.620 albertel 4049: foreach $envkey (keys %env) {
1.54 www 4050: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
4051: my $courseid=$2;
4052: my $roleid=$1.'.'.$2;
1.92 www 4053: $courseid=~s/^\///;
1.54 www 4054: my $expiretime=600;
1.620 albertel 4055: if ($env{'request.role'} eq $roleid) {
1.54 www 4056: $expiretime=120;
4057: }
4058: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
4059: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 4060: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 4061: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 4062: }
1.620 albertel 4063: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
4064: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
4065: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
4066: &log($env{'user.domain'},$env{'user.name'},
4067: $env{'user.home'},
1.57 www 4068: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 4069: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 4070: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 4071: return '';
4072: }
4073: }
1.620 albertel 4074: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
4075: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
4076: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
4077: &log($env{'user.domain'},$env{'user.name'},
4078: $env{'user.home'},
1.57 www 4079: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 4080: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 4081: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 4082: return '';
4083: }
4084: }
4085: }
1.29 www 4086: }
1.52 www 4087: }
4088:
4089: #
4090: # Rest of the restrictions depend on selected course
4091: #
4092:
1.620 albertel 4093: unless ($env{'request.course.id'}) {
1.766 albertel 4094: if ($thisallowed eq 'A') {
4095: return 'A';
1.814 raeburn 4096: } elsif ($thisallowed eq 'B') {
4097: return 'B';
1.766 albertel 4098: } else {
4099: return '1';
4100: }
1.52 www 4101: }
1.29 www 4102:
1.52 www 4103: #
4104: # Now user is definitely in a course
4105: #
1.53 www 4106:
4107:
4108: # Course preferences
4109:
4110: if ($thisallowed=~/C/) {
1.620 albertel 4111: my $rolecode=(split(/\./,$env{'request.role'}))[0];
4112: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
4113: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 4114: =~/\Q$rolecode\E/) {
1.689 albertel 4115: if ($priv ne 'pch') {
4116: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4117: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
4118: $env{'request.course.id'});
4119: }
1.237 www 4120: return '';
4121: }
4122:
1.620 albertel 4123: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 4124: =~/\Q$unamedom\E/) {
1.689 albertel 4125: if ($priv ne 'pch') {
4126: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
4127: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
4128: $env{'request.course.id'});
4129: }
1.54 www 4130: return '';
4131: }
1.53 www 4132: }
4133:
4134: # Resource preferences
4135:
4136: if ($thisallowed=~/R/) {
1.620 albertel 4137: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 4138: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689 albertel 4139: if ($priv ne 'pch') {
4140: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4141: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
4142: }
4143: return '';
1.54 www 4144: }
1.53 www 4145: }
1.30 www 4146:
1.246 www 4147: # Restricted by state or randomout?
1.30 www 4148:
1.52 www 4149: if ($thisallowed=~/X/) {
1.620 albertel 4150: if ($env{'acc.randomout'}) {
1.579 albertel 4151: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 4152: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 4153: return '';
4154: }
1.247 www 4155: }
4156: if (&condval($statecond)) {
1.52 www 4157: return '2';
4158: } else {
4159: return '';
4160: }
4161: }
1.30 www 4162:
1.766 albertel 4163: if ($thisallowed eq 'A') {
4164: return 'A';
1.814 raeburn 4165: } elsif ($thisallowed eq 'B') {
4166: return 'B';
1.766 albertel 4167: }
1.52 www 4168: return 'F';
1.232 www 4169: }
4170:
1.710 albertel 4171: sub split_uri_for_cond {
4172: my $uri=&deversion(&declutter(shift));
4173: my @uriparts=split(/\//,$uri);
4174: my $filename=pop(@uriparts);
4175: my $pathname=join('/',@uriparts);
4176: return ($pathname,$filename);
4177: }
1.232 www 4178: # --------------------------------------------------- Is a resource on the map?
4179:
4180: sub is_on_map {
1.710 albertel 4181: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 4182: #Trying to find the conditional for the file
1.620 albertel 4183: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 4184: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 4185: if ($match) {
1.289 bowersj2 4186: return (1,$1);
4187: } else {
1.434 www 4188: return (0,0);
1.289 bowersj2 4189: }
1.12 www 4190: }
4191:
1.427 www 4192: # --------------------------------------------------------- Get symb from alias
4193:
4194: sub get_symb_from_alias {
4195: my $symb=shift;
4196: my ($map,$resid,$url)=&decode_symb($symb);
4197: # Already is a symb
4198: if ($url) { return $symb; }
4199: # Must be an alias
4200: my $aliassymb='';
4201: my %bighash;
1.620 albertel 4202: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 4203: &GDBM_READER(),0640)) {
4204: my $rid=$bighash{'mapalias_'.$symb};
4205: if ($rid) {
4206: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 4207: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
4208: $resid,$bighash{'src_'.$rid});
1.427 www 4209: }
4210: untie %bighash;
4211: }
4212: return $aliassymb;
4213: }
4214:
1.12 www 4215: # ----------------------------------------------------------------- Define Role
4216:
4217: sub definerole {
4218: if (allowed('mcr','/')) {
4219: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 4220: foreach my $role (split(':',$sysrole)) {
4221: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4222: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
4223: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
4224: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 4225: return "refused:s:$crole&$cqual";
4226: }
4227: }
1.191 harris41 4228: }
1.800 albertel 4229: foreach my $role (split(':',$domrole)) {
4230: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4231: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
4232: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
4233: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 4234: return "refused:d:$crole&$cqual";
4235: }
4236: }
1.191 harris41 4237: }
1.800 albertel 4238: foreach my $role (split(':',$courole)) {
4239: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4240: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
4241: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
4242: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 4243: return "refused:c:$crole&$cqual";
4244: }
4245: }
1.191 harris41 4246: }
1.620 albertel 4247: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
4248: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 4249: "rolesdef_$rolename=".
4250: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 4251: return reply($command,$env{'user.home'});
1.12 www 4252: } else {
4253: return 'refused';
4254: }
1.105 harris41 4255: }
4256:
4257: # ---------------- Make a metadata query against the network of library servers
4258:
4259: sub metadata_query {
1.244 matthew 4260: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 4261: my %rhash;
1.845 albertel 4262: my %libserv = &all_library();
1.244 matthew 4263: my @server_list = (defined($server_array) ? @$server_array
4264: : keys(%libserv) );
4265: for my $server (@server_list) {
1.118 harris41 4266: unless ($custom or $customshow) {
4267: my $reply=&reply("querysend:".&escape($query),$server);
4268: $rhash{$server}=$reply;
4269: }
4270: else {
4271: my $reply=&reply("querysend:".&escape($query).':'.
4272: &escape($custom).':'.&escape($customshow),
4273: $server);
4274: $rhash{$server}=$reply;
4275: }
1.112 harris41 4276: }
1.118 harris41 4277: return \%rhash;
1.240 www 4278: }
4279:
4280: # ----------------------------------------- Send log queries and wait for reply
4281:
4282: sub log_query {
4283: my ($uname,$udom,$query,%filters)=@_;
4284: my $uhome=&homeserver($uname,$udom);
4285: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 4286: my $uhost=&hostname($uhome);
1.800 albertel 4287: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 4288: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
4289: $uhome);
1.479 albertel 4290: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 4291: return get_query_reply($queryid);
4292: }
4293:
1.818 raeburn 4294: # -------------------------- Update MySQL table for portfolio file
4295:
4296: sub update_portfolio_table {
1.821 raeburn 4297: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818 raeburn 4298: my $homeserver = &homeserver($uname,$udom);
4299: my $queryid=
1.821 raeburn 4300: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
4301: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 4302: my $reply = &get_query_reply($queryid);
4303: return $reply;
4304: }
4305:
1.508 raeburn 4306: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 4307:
4308: sub fetch_enrollment_query {
1.511 raeburn 4309: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 4310: my $homeserver;
1.547 raeburn 4311: my $maxtries = 1;
1.508 raeburn 4312: if ($context eq 'automated') {
4313: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 4314: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 4315: } else {
4316: $homeserver = &homeserver($cnum,$dom);
4317: }
1.838 albertel 4318: my $host=&hostname($homeserver);
1.506 raeburn 4319: my $cmd = '';
1.800 albertel 4320: foreach my $affiliate (keys %{$affiliatesref}) {
4321: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 4322: }
4323: $cmd =~ s/%%$//;
4324: $cmd = &escape($cmd);
4325: my $query = 'fetchenrollment';
1.620 albertel 4326: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 4327: unless ($queryid=~/^\Q$host\E\_/) {
4328: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
4329: return 'error: '.$queryid;
4330: }
1.506 raeburn 4331: my $reply = &get_query_reply($queryid);
1.547 raeburn 4332: my $tries = 1;
4333: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
4334: $reply = &get_query_reply($queryid);
4335: $tries ++;
4336: }
1.526 raeburn 4337: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 4338: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 4339: } else {
1.515 raeburn 4340: my @responses = split/:/,$reply;
4341: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 4342: foreach my $line (@responses) {
4343: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 4344: $$replyref{$key} = $value;
4345: }
4346: } else {
1.506 raeburn 4347: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 4348: foreach my $line (@responses) {
4349: my ($key,$value) = split(/=/,$line);
1.506 raeburn 4350: $$replyref{$key} = $value;
4351: if ($value > 0) {
1.800 albertel 4352: foreach my $item (@{$$affiliatesref{$key}}) {
4353: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 4354: my $destname = $pathname.'/'.$filename;
4355: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 4356: if ($xml_classlist =~ /^error/) {
4357: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
4358: } else {
1.506 raeburn 4359: if ( open(FILE,">$destname") ) {
4360: print FILE &unescape($xml_classlist);
4361: close(FILE);
1.526 raeburn 4362: } else {
4363: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 4364: }
4365: }
4366: }
4367: }
4368: }
4369: }
4370: return 'ok';
4371: }
4372: return 'error';
4373: }
4374:
1.242 www 4375: sub get_query_reply {
4376: my $queryid=shift;
1.240 www 4377: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
4378: my $reply='';
4379: for (1..100) {
4380: sleep 2;
4381: if (-e $replyfile.'.end') {
1.448 albertel 4382: if (open(my $fh,$replyfile)) {
1.240 www 4383: $reply.=<$fh>;
1.448 albertel 4384: close($fh);
1.240 www 4385: } else { return 'error: reply_file_error'; }
1.242 www 4386: return &unescape($reply);
4387: }
1.240 www 4388: }
1.242 www 4389: return 'timeout:'.$queryid;
1.240 www 4390: }
4391:
4392: sub courselog_query {
1.241 www 4393: #
4394: # possible filters:
4395: # url: url or symb
4396: # username
4397: # domain
4398: # action: view, submit, grade
4399: # start: timestamp
4400: # end: timestamp
4401: #
1.240 www 4402: my (%filters)=@_;
1.620 albertel 4403: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 4404: if ($filters{'url'}) {
4405: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
4406: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
4407: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
4408: }
1.620 albertel 4409: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
4410: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 4411: return &log_query($cname,$cdom,'courselog',%filters);
4412: }
4413:
4414: sub userlog_query {
1.858 raeburn 4415: #
4416: # possible filters:
4417: # action: log check role
4418: # start: timestamp
4419: # end: timestamp
4420: #
1.240 www 4421: my ($uname,$udom,%filters)=@_;
4422: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 4423: }
4424:
1.506 raeburn 4425: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
4426:
4427: sub auto_run {
1.508 raeburn 4428: my ($cnum,$cdom) = @_;
4429: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 4430: my $response = &reply('autorun:'.$cdom,$homeserver);
1.506 raeburn 4431: return $response;
4432: }
1.776 albertel 4433:
1.506 raeburn 4434: sub auto_get_sections {
1.508 raeburn 4435: my ($cnum,$cdom,$inst_coursecode) = @_;
4436: my $homeserver = &homeserver($cnum,$cdom);
1.506 raeburn 4437: my @secs = ();
1.511 raeburn 4438: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506 raeburn 4439: unless ($response eq 'refused') {
4440: @secs = split/:/,$response;
4441: }
4442: return @secs;
4443: }
1.776 albertel 4444:
1.506 raeburn 4445: sub auto_new_course {
1.508 raeburn 4446: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
4447: my $homeserver = &homeserver($cnum,$cdom);
1.515 raeburn 4448: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506 raeburn 4449: return $response;
4450: }
1.776 albertel 4451:
1.506 raeburn 4452: sub auto_validate_courseID {
1.508 raeburn 4453: my ($cnum,$cdom,$inst_course_id) = @_;
4454: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 4455: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 4456: return $response;
4457: }
1.776 albertel 4458:
1.506 raeburn 4459: sub auto_create_password {
1.508 raeburn 4460: my ($cnum,$cdom,$authparam) = @_;
4461: my $homeserver = &homeserver($cnum,$cdom);
1.506 raeburn 4462: my $create_passwd = 0;
4463: my $authchk = '';
1.511 raeburn 4464: my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
1.506 raeburn 4465: if ($response eq 'refused') {
4466: $authchk = 'refused';
4467: } else {
4468: ($authparam,$create_passwd,$authchk) = split/:/,$response;
4469: }
4470: return ($authparam,$create_passwd,$authchk);
4471: }
4472:
1.706 raeburn 4473: sub auto_photo_permission {
4474: my ($cnum,$cdom,$students) = @_;
4475: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 4476: my ($outcome,$perm_reqd,$conditions) =
4477: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 4478: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4479: return (undef,undef);
4480: }
1.706 raeburn 4481: return ($outcome,$perm_reqd,$conditions);
4482: }
4483:
4484: sub auto_checkphotos {
4485: my ($uname,$udom,$pid) = @_;
4486: my $homeserver = &homeserver($uname,$udom);
4487: my ($result,$resulttype);
4488: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 4489: &escape($uname).':'.&escape($pid),
4490: $homeserver));
1.709 albertel 4491: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4492: return (undef,undef);
4493: }
1.706 raeburn 4494: if ($outcome) {
4495: ($result,$resulttype) = split(/:/,$outcome);
4496: }
4497: return ($result,$resulttype);
4498: }
4499:
4500: sub auto_photochoice {
4501: my ($cnum,$cdom) = @_;
4502: my $homeserver = &homeserver($cnum,$cdom);
4503: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 4504: &escape($cdom),
4505: $homeserver)));
1.709 albertel 4506: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
4507: return (undef,undef);
4508: }
1.706 raeburn 4509: return ($update,$comment);
4510: }
4511:
4512: sub auto_photoupdate {
4513: my ($affiliatesref,$dom,$cnum,$photo) = @_;
4514: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 4515: my $host=&hostname($homeserver);
1.706 raeburn 4516: my $cmd = '';
4517: my $maxtries = 1;
1.800 albertel 4518: foreach my $affiliate (keys(%{$affiliatesref})) {
4519: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 4520: }
4521: $cmd =~ s/%%$//;
4522: $cmd = &escape($cmd);
4523: my $query = 'institutionalphotos';
4524: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
4525: unless ($queryid=~/^\Q$host\E\_/) {
4526: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
4527: return 'error: '.$queryid;
4528: }
4529: my $reply = &get_query_reply($queryid);
4530: my $tries = 1;
4531: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
4532: $reply = &get_query_reply($queryid);
4533: $tries ++;
4534: }
4535: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
4536: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
4537: } else {
4538: my @responses = split(/:/,$reply);
4539: my $outcome = shift(@responses);
4540: foreach my $item (@responses) {
4541: my ($key,$value) = split(/=/,$item);
4542: $$photo{$key} = $value;
4543: }
4544: return $outcome;
4545: }
4546: return 'error';
4547: }
4548:
1.521 raeburn 4549: sub auto_instcode_format {
1.793 albertel 4550: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
4551: $cat_order) = @_;
1.521 raeburn 4552: my $courses = '';
1.772 raeburn 4553: my @homeservers;
1.521 raeburn 4554: if ($caller eq 'global') {
1.841 albertel 4555: my %servers = &get_servers($codedom,'library');
4556: foreach my $tryserver (keys(%servers)) {
4557: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
4558: push(@homeservers,$tryserver);
4559: }
1.584 raeburn 4560: }
1.521 raeburn 4561: } else {
1.772 raeburn 4562: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 4563: }
1.793 albertel 4564: foreach my $code (keys(%{$instcodes})) {
4565: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 4566: }
4567: chop($courses);
1.772 raeburn 4568: my $ok_response = 0;
4569: my $response;
4570: while (@homeservers > 0 && $ok_response == 0) {
4571: my $server = shift(@homeservers);
4572: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
4573: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
4574: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.793 albertel 4575: split/:/,$response;
1.772 raeburn 4576: %{$codes} = (%{$codes},&str2hash($codes_str));
4577: push(@{$codetitles},&str2array($codetitles_str));
4578: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
4579: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
4580: $ok_response = 1;
4581: }
4582: }
4583: if ($ok_response) {
1.521 raeburn 4584: return 'ok';
1.772 raeburn 4585: } else {
4586: return $response;
1.521 raeburn 4587: }
4588: }
4589:
1.792 raeburn 4590: sub auto_instcode_defaults {
4591: my ($domain,$returnhash,$code_order) = @_;
4592: my @homeservers;
1.841 albertel 4593:
4594: my %servers = &get_servers($domain,'library');
4595: foreach my $tryserver (keys(%servers)) {
4596: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
4597: push(@homeservers,$tryserver);
4598: }
1.792 raeburn 4599: }
1.841 albertel 4600:
1.792 raeburn 4601: my $response;
1.841 albertel 4602: foreach my $server (@homeservers) {
1.792 raeburn 4603: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 4604: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
4605:
4606: foreach my $pair (split(/\&/,$response)) {
4607: my ($name,$value)=split(/\=/,$pair);
4608: if ($name eq 'code_order') {
4609: @{$code_order} = split(/\&/,&unescape($value));
4610: } else {
4611: $returnhash->{&unescape($name)}=&unescape($value);
4612: }
4613: }
4614: return 'ok';
1.792 raeburn 4615: }
1.841 albertel 4616:
4617: return $response;
1.792 raeburn 4618: }
4619:
1.777 albertel 4620: sub auto_validate_class_sec {
1.773 raeburn 4621: my ($cdom,$cnum,$owner,$inst_class) = @_;
4622: my $homeserver = &homeserver($cnum,$cdom);
4623: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.774 banghart 4624: &escape($owner).':'.$cdom,$homeserver);
1.773 raeburn 4625: return $response;
4626: }
4627:
1.679 raeburn 4628: # ------------------------------------------------------- Course Group routines
4629:
4630: sub get_coursegroups {
1.809 raeburn 4631: my ($cdom,$cnum,$group,$namespace) = @_;
4632: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 4633: }
4634:
1.679 raeburn 4635: sub modify_coursegroup {
4636: my ($cdom,$cnum,$groupsettings) = @_;
4637: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
4638: }
4639:
1.809 raeburn 4640: sub toggle_coursegroup_status {
4641: my ($cdom,$cnum,$group,$action) = @_;
4642: my ($from_namespace,$to_namespace);
4643: if ($action eq 'delete') {
4644: $from_namespace = 'coursegroups';
4645: $to_namespace = 'deleted_groups';
4646: } else {
4647: $from_namespace = 'deleted_groups';
4648: $to_namespace = 'coursegroups';
4649: }
4650: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 4651: if (my $tmp = &error(%curr_group)) {
4652: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
4653: return ('read error',$tmp);
4654: } else {
4655: my %savedsettings = %curr_group;
1.809 raeburn 4656: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 4657: my $deloutcome;
4658: if ($result eq 'ok') {
1.809 raeburn 4659: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 4660: } else {
4661: return ('write error',$result);
4662: }
4663: if ($deloutcome eq 'ok') {
4664: return 'ok';
4665: } else {
4666: return ('delete error',$deloutcome);
4667: }
4668: }
4669: }
4670:
1.679 raeburn 4671: sub modify_group_roles {
4672: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
4673: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
4674: my $role = 'gr/'.&escape($userprivs);
4675: my ($uname,$udom) = split(/:/,$user);
4676: my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684 raeburn 4677: if ($result eq 'ok') {
4678: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
4679: }
1.679 raeburn 4680: return $result;
4681: }
4682:
4683: sub modify_coursegroup_membership {
4684: my ($cdom,$cnum,$membership) = @_;
4685: my $result = &put('groupmembership',$membership,$cdom,$cnum);
4686: return $result;
4687: }
4688:
1.682 raeburn 4689: sub get_active_groups {
4690: my ($udom,$uname,$cdom,$cnum) = @_;
4691: my $now = time;
4692: my %groups = ();
4693: foreach my $key (keys(%env)) {
1.811 albertel 4694: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 4695: my ($start,$end) = split(/\./,$env{$key});
4696: if (($end!=0) && ($end<$now)) { next; }
4697: if (($start!=0) && ($start>$now)) { next; }
4698: if ($1 eq $cdom && $2 eq $cnum) {
4699: $groups{$3} = $env{$key} ;
4700: }
4701: }
4702: }
4703: return %groups;
4704: }
4705:
1.683 raeburn 4706: sub get_group_membership {
4707: my ($cdom,$cnum,$group) = @_;
4708: return(&dump('groupmembership',$cdom,$cnum,$group));
4709: }
4710:
4711: sub get_users_groups {
4712: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 4713: my @usersgroups;
1.683 raeburn 4714: my $cachetime=1800;
4715:
4716: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 4717: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
4718: if (defined($cached)) {
1.734 albertel 4719: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 4720: } else {
4721: $grouplist = '';
1.816 raeburn 4722: my $courseurl = &courseid_to_courseurl($courseid);
4723: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 4724: my $access_end = $env{'course.'.$courseid.
4725: '.default_enrollment_end_date'};
4726: my $now = time;
4727: foreach my $key (keys(%roleshash)) {
4728: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
4729: my $group = $1;
4730: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
4731: my $start = $2;
4732: my $end = $1;
4733: if ($start == -1) { next; } # deleted from group
4734: if (($start!=0) && ($start>$now)) { next; }
4735: if (($end!=0) && ($end<$now)) {
4736: if ($access_end && $access_end < $now) {
4737: if ($access_end - $end < 86400) {
4738: push(@usersgroups,$group);
1.733 raeburn 4739: }
4740: }
1.817 raeburn 4741: next;
1.733 raeburn 4742: }
1.817 raeburn 4743: push(@usersgroups,$group);
1.683 raeburn 4744: }
4745: }
4746: }
1.817 raeburn 4747: @usersgroups = &sort_course_groups($courseid,@usersgroups);
4748: $grouplist = join(':',@usersgroups);
4749: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 4750: }
1.733 raeburn 4751: return @usersgroups;
1.683 raeburn 4752: }
4753:
4754: sub devalidate_getgroups_cache {
4755: my ($udom,$uname,$cdom,$cnum)=@_;
4756: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 4757:
1.683 raeburn 4758: my $hashid="$udom:$uname:$courseid";
4759: &devalidate_cache_new('getgroups',$hashid);
4760: }
4761:
1.12 www 4762: # ------------------------------------------------------------------ Plain Text
4763:
4764: sub plaintext {
1.742 raeburn 4765: my ($short,$type,$cid) = @_;
1.758 albertel 4766: if ($short =~ /^cr/) {
4767: return (split('/',$short))[-1];
4768: }
1.742 raeburn 4769: if (!defined($cid)) {
4770: $cid = $env{'request.course.id'};
4771: }
4772: if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
4773: return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
4774: '.plaintext'});
4775: }
4776: my %rolenames = (
4777: Course => 'std',
4778: Group => 'alt1',
4779: );
4780: if (defined($type) &&
4781: defined($rolenames{$type}) &&
4782: defined($prp{$short}{$rolenames{$type}})) {
4783: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
4784: } else {
4785: return &Apache::lonlocal::mt($prp{$short}{'std'});
4786: }
1.12 www 4787: }
4788:
4789: # ----------------------------------------------------------------- Assign Role
4790:
4791: sub assignrole {
1.357 www 4792: my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21 www 4793: my $mrole;
4794: if ($role =~ /^cr\//) {
1.393 www 4795: my $cwosec=$url;
1.811 albertel 4796: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 4797: unless (&allowed('ccr',$cwosec)) {
1.104 www 4798: &logthis('Refused custom assignrole: '.
4799: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620 albertel 4800: $env{'user.name'}.' at '.$env{'user.domain'});
1.104 www 4801: return 'refused';
4802: }
1.21 www 4803: $mrole='cr';
1.678 raeburn 4804: } elsif ($role =~ /^gr\//) {
4805: my $cwogrp=$url;
1.811 albertel 4806: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 4807: unless (&allowed('mdg',$cwogrp)) {
4808: &logthis('Refused group assignrole: '.
4809: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
4810: $env{'user.name'}.' at '.$env{'user.domain'});
4811: return 'refused';
4812: }
4813: $mrole='gr';
1.21 www 4814: } else {
1.82 www 4815: my $cwosec=$url;
1.811 albertel 4816: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.373 www 4817: unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) {
1.104 www 4818: &logthis('Refused assignrole: '.
4819: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620 albertel 4820: $env{'user.name'}.' at '.$env{'user.domain'});
1.104 www 4821: return 'refused';
4822: }
1.21 www 4823: $mrole=$role;
4824: }
1.620 albertel 4825: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 4826: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 4827: if ($end) { $command.='_'.$end; }
1.21 www 4828: if ($start) {
4829: if ($end) {
1.81 www 4830: $command.='_'.$start;
1.21 www 4831: } else {
1.81 www 4832: $command.='_0_'.$start;
1.21 www 4833: }
4834: }
1.739 raeburn 4835: my $origstart = $start;
4836: my $origend = $end;
1.357 www 4837: # actually delete
4838: if ($deleteflag) {
1.373 www 4839: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 4840: # modify command to delete the role
1.620 albertel 4841: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 4842: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 4843: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 4844: # set start and finish to negative values for userrolelog
4845: $start=-1;
4846: $end=-1;
4847: }
4848: }
4849: # send command
1.349 www 4850: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 4851: # log new user role if status is ok
1.349 www 4852: if ($answer eq 'ok') {
1.663 raeburn 4853: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 4854: # for course roles, perform group memberships changes triggered by role change.
4855: unless ($role =~ /^gr/) {
4856: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
4857: $origstart);
4858: }
1.349 www 4859: }
4860: return $answer;
1.169 harris41 4861: }
4862:
4863: # -------------------------------------------------- Modify user authentication
1.197 www 4864: # Overrides without validation
4865:
1.169 harris41 4866: sub modifyuserauth {
4867: my ($udom,$uname,$umode,$upass)=@_;
4868: my $uhome=&homeserver($uname,$udom);
1.197 www 4869: unless (&allowed('mau',$udom)) { return 'refused'; }
4870: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 4871: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4872: ' in domain '.$env{'request.role.domain'});
1.169 harris41 4873: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
4874: &escape($upass),$uhome);
1.620 albertel 4875: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 4876: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
4877: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
4878: &log($udom,,$uname,$uhome,
1.620 albertel 4879: 'Authentication changed by '.$env{'user.domain'}.', '.
4880: $env{'user.name'}.', '.$umode.
1.197 www 4881: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 4882: unless ($reply eq 'ok') {
1.197 www 4883: &logthis('Authentication mode error: '.$reply);
1.169 harris41 4884: return 'error: '.$reply;
4885: }
1.170 harris41 4886: return 'ok';
1.80 www 4887: }
4888:
1.81 www 4889: # --------------------------------------------------------------- Modify a user
1.80 www 4890:
1.81 www 4891: sub modifyuser {
1.206 matthew 4892: my ($udom, $uname, $uid,
4893: $umode, $upass, $first,
4894: $middle, $last, $gene,
1.387 www 4895: $forceid, $desiredhome, $email)=@_;
1.807 albertel 4896: $udom= &LONCAPA::clean_domain($udom);
4897: $uname=&LONCAPA::clean_username($uname);
1.81 www 4898: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 4899: $umode.', '.$first.', '.$middle.', '.
1.206 matthew 4900: $last.', '.$gene.'(forceid: '.$forceid.')'.
4901: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
4902: ' desiredhome not specified').
1.620 albertel 4903: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
4904: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 4905: my $uhome=&homeserver($uname,$udom,'true');
1.80 www 4906: # ----------------------------------------------------------------- Create User
1.406 albertel 4907: if (($uhome eq 'no_host') &&
4908: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 4909: my $unhome='';
1.844 albertel 4910: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 4911: $unhome = $desiredhome;
1.620 albertel 4912: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
4913: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 4914: } else { # load balancing routine for determining $unhome
1.81 www 4915: my $loadm=10000000;
1.841 albertel 4916: my %servers = &get_servers($udom,'library');
4917: foreach my $tryserver (keys(%servers)) {
4918: my $answer=reply('load',$tryserver);
4919: if (($answer=~/\d+/) && ($answer<$loadm)) {
4920: $loadm=$answer;
4921: $unhome=$tryserver;
4922: }
1.80 www 4923: }
4924: }
4925: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 4926: return 'error: unable to find a home server for '.$uname.
4927: ' in domain '.$udom;
1.80 www 4928: }
4929: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
4930: &escape($upass),$unhome);
4931: unless ($reply eq 'ok') {
4932: return 'error: '.$reply;
4933: }
1.230 stredwic 4934: $uhome=&homeserver($uname,$udom,'true');
1.80 www 4935: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 4936: return 'error: unable verify users home machine.';
1.80 www 4937: }
1.209 matthew 4938: } # End of creation of new user
1.80 www 4939: # ---------------------------------------------------------------------- Add ID
4940: if ($uid) {
4941: $uid=~tr/A-Z/a-z/;
4942: my %uidhash=&idrget($udom,$uname);
1.196 www 4943: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
4944: && (!$forceid)) {
1.80 www 4945: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 4946: return 'error: user id "'.$uid.'" does not match '.
4947: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 4948: }
4949: } else {
4950: &idput($udom,($uname => $uid));
4951: }
4952: }
4953: # -------------------------------------------------------------- Add names, etc
1.313 matthew 4954: my @tmp=&get('environment',
1.134 albertel 4955: ['firstname','middlename','lastname','generation'],
4956: $udom,$uname);
1.313 matthew 4957: my %names;
4958: if ($tmp[0] =~ m/^error:.*/) {
4959: %names=();
4960: } else {
4961: %names = @tmp;
4962: }
1.388 www 4963: #
4964: # Make sure to not trash student environment if instructor does not bother
4965: # to supply name and email information
4966: #
4967: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 4968: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 4969: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 4970: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 4971: if ($email) {
4972: $email=~s/[^\w\@\.\-\,]//gs;
4973: if ($email=~/\@/) { $names{'notification'} = $email;
4974: $names{'critnotification'} = $email;
4975: $names{'permanentemail'} = $email; }
4976: }
1.134 albertel 4977: my $reply = &put('environment', \%names, $udom,$uname);
4978: if ($reply ne 'ok') { return 'error: '.$reply; }
1.680 www 4979: &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81 www 4980: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 4981: $umode.', '.$first.', '.$middle.', '.
4982: $last.', '.$gene.' by '.
1.620 albertel 4983: $env{'user.name'}.' at '.$env{'user.domain'});
1.134 albertel 4984: return 'ok';
1.80 www 4985: }
4986:
1.81 www 4987: # -------------------------------------------------------------- Modify student
1.80 www 4988:
1.81 www 4989: sub modifystudent {
4990: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515 raeburn 4991: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455 albertel 4992: if (!$cid) {
1.620 albertel 4993: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 4994: return 'not_in_class';
4995: }
1.80 www 4996: }
4997: # --------------------------------------------------------------- Make the user
1.81 www 4998: my $reply=&modifyuser
1.209 matthew 4999: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387 www 5000: $desiredhome,$email);
1.80 www 5001: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 5002: # This will cause &modify_student_enrollment to get the uid from the
5003: # students environment
5004: $uid = undef if (!$forceid);
1.455 albertel 5005: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515 raeburn 5006: $gene,$usec,$end,$start,$type,$locktype,$cid);
1.297 matthew 5007: return $reply;
5008: }
5009:
5010: sub modify_student_enrollment {
1.515 raeburn 5011: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455 albertel 5012: my ($cdom,$cnum,$chome);
5013: if (!$cid) {
1.620 albertel 5014: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 5015: return 'not_in_class';
5016: }
1.620 albertel 5017: $cdom=$env{'course.'.$cid.'.domain'};
5018: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 5019: } else {
5020: ($cdom,$cnum)=split(/_/,$cid);
5021: }
1.620 albertel 5022: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 5023: if (!$chome) {
1.457 raeburn 5024: $chome=&homeserver($cnum,$cdom);
1.297 matthew 5025: }
1.455 albertel 5026: if (!$chome) { return 'unknown_course'; }
1.297 matthew 5027: # Make sure the user exists
1.81 www 5028: my $uhome=&homeserver($uname,$udom);
5029: if (($uhome eq '') || ($uhome eq 'no_host')) {
5030: return 'error: no such user';
5031: }
1.297 matthew 5032: # Get student data if we were not given enough information
5033: if (!defined($first) || $first eq '' ||
5034: !defined($last) || $last eq '' ||
5035: !defined($uid) || $uid eq '' ||
5036: !defined($middle) || $middle eq '' ||
5037: !defined($gene) || $gene eq '') {
1.294 matthew 5038: # They did not supply us with enough data to enroll the student, so
5039: # we need to pick up more information.
1.297 matthew 5040: my %tmp = &get('environment',
1.294 matthew 5041: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 5042: ,$udom,$uname);
5043:
1.800 albertel 5044: #foreach my $key (keys(%tmp)) {
5045: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 5046: #}
1.294 matthew 5047: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
5048: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
5049: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 5050: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 5051: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
5052: }
1.556 albertel 5053: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 5054: my $reply=cput('classlist',
5055: {"$uname:$udom" =>
1.515 raeburn 5056: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 5057: $cdom,$cnum);
1.81 www 5058: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
5059: return 'error: '.$reply;
1.652 albertel 5060: } else {
5061: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 5062: }
1.297 matthew 5063: # Add student role to user
1.83 www 5064: my $uurl='/'.$cid;
1.81 www 5065: $uurl=~s/\_/\//g;
5066: if ($usec) {
5067: $uurl.='/'.$usec;
5068: }
5069: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21 www 5070: }
5071:
1.556 albertel 5072: sub format_name {
5073: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
5074: my $name;
5075: if ($first ne 'lastname') {
5076: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
5077: } else {
5078: if ($lastname=~/\S/) {
5079: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
5080: $name=~s/\s+,/,/;
5081: } else {
5082: $name.= $firstname.' '.$middlename.' '.$generation;
5083: }
5084: }
5085: $name=~s/^\s+//;
5086: $name=~s/\s+$//;
5087: $name=~s/\s+/ /g;
5088: return $name;
5089: }
5090:
1.84 www 5091: # ------------------------------------------------- Write to course preferences
5092:
5093: sub writecoursepref {
5094: my ($courseid,%prefs)=@_;
5095: $courseid=~s/^\///;
5096: $courseid=~s/\_/\//g;
5097: my ($cdomain,$cnum)=split(/\//,$courseid);
5098: my $chome=homeserver($cnum,$cdomain);
5099: if (($chome eq '') || ($chome eq 'no_host')) {
5100: return 'error: no such course';
5101: }
5102: my $cstring='';
1.800 albertel 5103: foreach my $pref (keys(%prefs)) {
5104: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 5105: }
1.84 www 5106: $cstring=~s/\&$//;
5107: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
5108: }
5109:
5110: # ---------------------------------------------------------- Make/modify course
5111:
5112: sub createcourse {
1.741 raeburn 5113: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
5114: $course_owner,$crstype)=@_;
1.84 www 5115: $url=&declutter($url);
5116: my $cid='';
1.264 matthew 5117: unless (&allowed('ccc',$udom)) {
1.84 www 5118: return 'refused';
5119: }
5120: # ------------------------------------------------------------------- Create ID
1.674 www 5121: my $uname=int(1+rand(9)).
5122: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
5123: substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84 www 5124: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
5125: # ----------------------------------------------- Make sure that does not exist
1.230 stredwic 5126: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 5127: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5128: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
5129: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230 stredwic 5130: $uhome=&homeserver($uname,$udom,'true');
1.84 www 5131: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5132: return 'error: unable to generate unique course-ID';
5133: }
5134: }
1.264 matthew 5135: # ------------------------------------------------ Check supplied server name
1.620 albertel 5136: $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845 albertel 5137: if (! &is_library($course_server)) {
1.264 matthew 5138: return 'error:bad server name '.$course_server;
5139: }
1.84 www 5140: # ------------------------------------------------------------- Make the course
5141: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 5142: $course_server);
1.84 www 5143: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230 stredwic 5144: $uhome=&homeserver($uname,$udom,'true');
1.84 www 5145: if (($uhome eq '') || ($uhome eq 'no_host')) {
5146: return 'error: no such course';
5147: }
1.271 www 5148: # ----------------------------------------------------------------- Course made
1.516 raeburn 5149: # log existence
5150: &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.741 raeburn 5151: ':'.&escape($inst_code).':'.&escape($course_owner).':'.
5152: &escape($crstype),$uhome);
1.358 www 5153: &flushcourselogs();
5154: # set toplevel url
1.271 www 5155: my $topurl=$url;
5156: unless ($nonstandard) {
5157: # ------------------------------------------ For standard courses, make top url
5158: my $mapurl=&clutter($url);
1.278 www 5159: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 5160: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 5161: <map>
5162: <resource id="1" type="start"></resource>
5163: <resource id="2" src="$mapurl"></resource>
5164: <resource id="3" type="finish"></resource>
5165: <link index="1" from="1" to="2"></link>
5166: <link index="2" from="2" to="3"></link>
5167: </map>
5168: ENDINITMAP
5169: $topurl=&declutter(
1.638 albertel 5170: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 5171: );
5172: }
5173: # ----------------------------------------------------------- Write preferences
1.84 www 5174: &writecoursepref($udom.'_'.$uname,
5175: ('description' => $description,
1.271 www 5176: 'url' => $topurl));
1.84 www 5177: return '/'.$udom.'/'.$uname;
5178: }
5179:
1.813 albertel 5180: sub is_course {
5181: my ($cdom,$cnum) = @_;
5182: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
5183: undef,'.');
5184: if (exists($courses{$cdom.'_'.$cnum})) {
5185: return 1;
5186: }
5187: return 0;
5188: }
5189:
1.21 www 5190: # ---------------------------------------------------------- Assign Custom Role
5191:
5192: sub assigncustomrole {
1.357 www 5193: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21 www 5194: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357 www 5195: $end,$start,$deleteflag);
1.21 www 5196: }
5197:
5198: # ----------------------------------------------------------------- Revoke Role
5199:
5200: sub revokerole {
1.357 www 5201: my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21 www 5202: my $now=time;
1.357 www 5203: return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21 www 5204: }
5205:
5206: # ---------------------------------------------------------- Revoke Custom Role
5207:
5208: sub revokecustomrole {
1.357 www 5209: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21 www 5210: my $now=time;
1.357 www 5211: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
5212: $deleteflag);
1.17 www 5213: }
5214:
1.533 banghart 5215: # ------------------------------------------------------------ Disk usage
1.535 albertel 5216: sub diskusage {
1.533 banghart 5217: my ($udom,$uname,$directoryRoot)=@_;
5218: $directoryRoot =~ s/\/$//;
1.535 albertel 5219: my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514 albertel 5220: return $listing;
1.512 banghart 5221: }
5222:
1.566 banghart 5223: sub is_locked {
5224: my ($file_name, $domain, $user) = @_;
5225: my @check;
5226: my $is_locked;
5227: push @check, $file_name;
1.613 albertel 5228: my %locked = &get('file_permissions',\@check,
1.620 albertel 5229: $env{'user.domain'},$env{'user.name'});
1.615 albertel 5230: my ($tmp)=keys(%locked);
5231: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 5232:
1.566 banghart 5233: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 5234: $is_locked = 'false';
5235: foreach my $entry (@{$locked{$file_name}}) {
5236: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 5237: $is_locked = 'true';
5238: last;
1.745 raeburn 5239: }
5240: }
1.566 banghart 5241: } else {
5242: $is_locked = 'false';
5243: }
5244: }
5245:
1.759 albertel 5246: sub declutter_portfile {
5247: my ($file) = @_;
1.833 albertel 5248: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 5249: return $file;
5250: }
5251:
1.559 banghart 5252: # ------------------------------------------------------------- Mark as Read Only
5253:
5254: sub mark_as_readonly {
5255: my ($domain,$user,$files,$what) = @_;
1.613 albertel 5256: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 5257: my ($tmp)=keys(%current_permissions);
5258: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 5259: foreach my $file (@{$files}) {
1.759 albertel 5260: $file = &declutter_portfile($file);
1.561 banghart 5261: push(@{$current_permissions{$file}},$what);
1.559 banghart 5262: }
1.613 albertel 5263: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 5264: return;
5265: }
5266:
1.572 banghart 5267: # ------------------------------------------------------------Save Selected Files
5268:
5269: sub save_selected_files {
5270: my ($user, $path, @files) = @_;
5271: my $filename = $user."savedfiles";
1.573 banghart 5272: my @other_files = &files_not_in_path($user, $path);
1.574 banghart 5273: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 5274: foreach my $file (@files) {
1.620 albertel 5275: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 5276: }
5277: foreach my $file (@other_files) {
1.574 banghart 5278: print (OUT $file."\n");
1.572 banghart 5279: }
1.574 banghart 5280: close (OUT);
1.572 banghart 5281: return 'ok';
5282: }
5283:
1.574 banghart 5284: sub clear_selected_files {
5285: my ($user) = @_;
5286: my $filename = $user."savedfiles";
5287: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5288: print (OUT undef);
5289: close (OUT);
5290: return ("ok");
5291: }
5292:
1.572 banghart 5293: sub files_in_path {
5294: my ($user, $path) = @_;
5295: my $filename = $user."savedfiles";
5296: my %return_files;
1.574 banghart 5297: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 5298: while (my $line_in = <IN>) {
1.574 banghart 5299: chomp ($line_in);
5300: my @paths_and_file = split (m!/!, $line_in);
5301: my $file_part = pop (@paths_and_file);
5302: my $path_part = join ('/', @paths_and_file);
1.573 banghart 5303: $path_part.='/';
5304: my $path_and_file = $path_part.$file_part;
5305: if ($path_part eq $path) {
5306: $return_files{$file_part}= 'selected';
5307: }
5308: }
1.574 banghart 5309: close (IN);
5310: return (\%return_files);
1.572 banghart 5311: }
5312:
5313: # called in portfolio select mode, to show files selected NOT in current directory
5314: sub files_not_in_path {
5315: my ($user, $path) = @_;
5316: my $filename = $user."savedfiles";
5317: my @return_files;
5318: my $path_part;
1.800 albertel 5319: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5320: while (my $line = <IN>) {
1.572 banghart 5321: #ok, I know it's clunky, but I want it to work
1.800 albertel 5322: my @paths_and_file = split(m|/|, $line);
5323: my $file_part = pop(@paths_and_file);
5324: chomp($file_part);
5325: my $path_part = join('/', @paths_and_file);
1.572 banghart 5326: $path_part .= '/';
5327: my $path_and_file = $path_part.$file_part;
5328: if ($path_part ne $path) {
1.800 albertel 5329: push(@return_files, ($path_and_file));
1.572 banghart 5330: }
5331: }
1.800 albertel 5332: close(OUT);
1.574 banghart 5333: return (@return_files);
1.572 banghart 5334: }
5335:
1.745 raeburn 5336: #----------------------------------------------Get portfolio file permissions
1.629 banghart 5337:
1.745 raeburn 5338: sub get_portfile_permissions {
5339: my ($domain,$user) = @_;
1.613 albertel 5340: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 5341: my ($tmp)=keys(%current_permissions);
5342: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 5343: return \%current_permissions;
5344: }
5345:
5346: #---------------------------------------------Get portfolio file access controls
5347:
1.749 raeburn 5348: sub get_access_controls {
1.745 raeburn 5349: my ($current_permissions,$group,$file) = @_;
1.769 albertel 5350: my %access;
5351: my $real_file = $file;
5352: $file =~ s/\.meta$//;
1.745 raeburn 5353: if (defined($file)) {
1.749 raeburn 5354: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
5355: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 5356: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 5357: }
5358: }
1.745 raeburn 5359: } else {
1.749 raeburn 5360: foreach my $key (keys(%{$current_permissions})) {
5361: if ($key =~ /\0accesscontrol$/) {
5362: if (defined($group)) {
5363: if ($key !~ m-^\Q$group\E/-) {
5364: next;
5365: }
5366: }
5367: my ($fullpath) = split(/\0/,$key);
5368: if (ref($$current_permissions{$key}) eq 'HASH') {
5369: foreach my $control (keys(%{$$current_permissions{$key}})) {
5370: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
5371: }
5372: }
5373: }
5374: }
5375: }
5376: return %access;
5377: }
5378:
5379: sub modify_access_controls {
5380: my ($file_name,$changes,$domain,$user)=@_;
5381: my ($outcome,$deloutcome);
5382: my %store_permissions;
5383: my %new_values;
5384: my %new_control;
5385: my %translation;
5386: my @deletions = ();
5387: my $now = time;
5388: if (exists($$changes{'activate'})) {
5389: if (ref($$changes{'activate'}) eq 'HASH') {
5390: my @newitems = sort(keys(%{$$changes{'activate'}}));
5391: my $numnew = scalar(@newitems);
5392: for (my $i=0; $i<$numnew; $i++) {
5393: my $newkey = $newitems[$i];
5394: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 5395: if ($newkey =~ /^\d+:/) {
5396: $newkey =~ s/^(\d+)/$newid/;
5397: $translation{$1} = $newid;
5398: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
5399: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
5400: $translation{$1} = $newid;
5401: }
1.749 raeburn 5402: $new_values{$file_name."\0".$newkey} =
5403: $$changes{'activate'}{$newitems[$i]};
5404: $new_control{$newkey} = $now;
5405: }
5406: }
5407: }
5408: my %todelete;
5409: my %changed_items;
5410: foreach my $action ('delete','update') {
5411: if (exists($$changes{$action})) {
5412: if (ref($$changes{$action}) eq 'HASH') {
5413: foreach my $key (keys(%{$$changes{$action}})) {
5414: my ($itemnum) = ($key =~ /^([^:]+):/);
5415: if ($action eq 'delete') {
5416: $todelete{$itemnum} = 1;
5417: } else {
5418: $changed_items{$itemnum} = $key;
5419: }
5420: }
1.745 raeburn 5421: }
5422: }
1.749 raeburn 5423: }
5424: # get lock on access controls for file.
5425: my $lockhash = {
5426: $file_name."\0".'locked_access_records' => $env{'user.name'}.
5427: ':'.$env{'user.domain'},
5428: };
5429: my $tries = 0;
5430: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5431:
5432: while (($gotlock ne 'ok') && $tries <3) {
5433: $tries ++;
5434: sleep 1;
5435: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5436: }
5437: if ($gotlock eq 'ok') {
5438: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
5439: my ($tmp)=keys(%curr_permissions);
5440: if ($tmp=~/^error:/) { undef(%curr_permissions); }
5441: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
5442: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
5443: if (ref($curr_controls) eq 'HASH') {
5444: foreach my $control_item (keys(%{$curr_controls})) {
5445: my ($itemnum) = ($control_item =~ /^([^:]+):/);
5446: if (defined($todelete{$itemnum})) {
5447: push(@deletions,$file_name."\0".$control_item);
5448: } else {
5449: if (defined($changed_items{$itemnum})) {
5450: $new_control{$changed_items{$itemnum}} = $now;
5451: push(@deletions,$file_name."\0".$control_item);
5452: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
5453: } else {
5454: $new_control{$control_item} = $$curr_controls{$control_item};
5455: }
5456: }
1.745 raeburn 5457: }
5458: }
5459: }
1.749 raeburn 5460: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
5461: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
5462: $outcome = &put('file_permissions',\%new_values,$domain,$user);
5463: # remove lock
5464: my @del_lock = ($file_name."\0".'locked_access_records');
5465: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 5466: my ($file,$group);
5467: if (&is_course($domain,$user)) {
5468: ($group,$file) = split(/\//,$file_name,2);
5469: } else {
5470: $file = $file_name;
5471: }
5472: my $sqlresult =
5473: &update_portfolio_table($user,$domain,$file,'portfolio_access',
5474: $group);
1.749 raeburn 5475: } else {
5476: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 5477: }
1.749 raeburn 5478: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 5479: }
5480:
1.827 raeburn 5481: sub make_public_indefinitely {
5482: my ($requrl) = @_;
5483: my $now = time;
5484: my $action = 'activate';
5485: my $aclnum = 0;
5486: if (&is_portfolio_url($requrl)) {
5487: my (undef,$udom,$unum,$file_name,$group) =
5488: &parse_portfolio_url($requrl);
5489: my $current_perms = &get_portfile_permissions($udom,$unum);
5490: my %access_controls = &get_access_controls($current_perms,
5491: $group,$file_name);
5492: foreach my $key (keys(%{$access_controls{$file_name}})) {
5493: my ($num,$scope,$end,$start) =
5494: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
5495: if ($scope eq 'public') {
5496: if ($start <= $now && $end == 0) {
5497: $action = 'none';
5498: } else {
5499: $action = 'update';
5500: $aclnum = $num;
5501: }
5502: last;
5503: }
5504: }
5505: if ($action eq 'none') {
5506: return 'ok';
5507: } else {
5508: my %changes;
5509: my $newend = 0;
5510: my $newstart = $now;
5511: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
5512: $changes{$action}{$newkey} = {
5513: type => 'public',
5514: time => {
5515: start => $newstart,
5516: end => $newend,
5517: },
5518: };
5519: my ($outcome,$deloutcome,$new_values,$translation) =
5520: &modify_access_controls($file_name,\%changes,$udom,$unum);
5521: return $outcome;
5522: }
5523: } else {
5524: return 'invalid';
5525: }
5526: }
5527:
1.745 raeburn 5528: #------------------------------------------------------Get Marked as Read Only
5529:
5530: sub get_marked_as_readonly {
5531: my ($domain,$user,$what,$group) = @_;
5532: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 5533: my @readonly_files;
1.629 banghart 5534: my $cmp1=$what;
5535: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 5536: while (my ($file_name,$value) = each(%{$current_permissions})) {
5537: if (defined($group)) {
5538: if ($file_name !~ m-^\Q$group\E/-) {
5539: next;
5540: }
5541: }
1.561 banghart 5542: if (ref($value) eq "ARRAY"){
5543: foreach my $stored_what (@{$value}) {
1.629 banghart 5544: my $cmp2=$stored_what;
1.759 albertel 5545: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 5546: $cmp2=join('',@{$stored_what});
1.745 raeburn 5547: }
1.629 banghart 5548: if ($cmp1 eq $cmp2) {
1.561 banghart 5549: push(@readonly_files, $file_name);
1.745 raeburn 5550: last;
1.563 banghart 5551: } elsif (!defined($what)) {
5552: push(@readonly_files, $file_name);
1.745 raeburn 5553: last;
1.561 banghart 5554: }
5555: }
1.745 raeburn 5556: }
1.561 banghart 5557: }
5558: return @readonly_files;
5559: }
1.577 banghart 5560: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 5561:
1.577 banghart 5562: sub get_marked_as_readonly_hash {
1.745 raeburn 5563: my ($current_permissions,$group,$what) = @_;
1.577 banghart 5564: my %readonly_files;
1.745 raeburn 5565: while (my ($file_name,$value) = each(%{$current_permissions})) {
5566: if (defined($group)) {
5567: if ($file_name !~ m-^\Q$group\E/-) {
5568: next;
5569: }
5570: }
1.577 banghart 5571: if (ref($value) eq "ARRAY"){
5572: foreach my $stored_what (@{$value}) {
1.745 raeburn 5573: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 5574: foreach my $lock_descriptor(@{$stored_what}) {
5575: if ($lock_descriptor eq 'graded') {
5576: $readonly_files{$file_name} = 'graded';
5577: } elsif ($lock_descriptor eq 'handback') {
5578: $readonly_files{$file_name} = 'handback';
5579: } else {
5580: if (!exists($readonly_files{$file_name})) {
5581: $readonly_files{$file_name} = 'locked';
5582: }
5583: }
1.745 raeburn 5584: }
1.750 banghart 5585: }
1.577 banghart 5586: }
5587: }
5588: }
5589: return %readonly_files;
5590: }
1.559 banghart 5591: # ------------------------------------------------------------ Unmark as Read Only
5592:
5593: sub unmark_as_readonly {
1.629 banghart 5594: # unmarks $file_name (if $file_name is defined), or all files locked by $what
5595: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 5596: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 5597: $file_name = &declutter_portfile($file_name);
1.634 albertel 5598: my $symb_crs = $what;
5599: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 5600: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 5601: my ($tmp)=keys(%current_permissions);
5602: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 5603: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 5604: foreach my $file (@readonly_files) {
1.759 albertel 5605: my $clean_file = &declutter_portfile($file);
5606: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 5607: my $current_locks = $current_permissions{$file};
1.563 banghart 5608: my @new_locks;
5609: my @del_keys;
5610: if (ref($current_locks) eq "ARRAY"){
5611: foreach my $locker (@{$current_locks}) {
1.632 albertel 5612: my $compare=$locker;
1.749 raeburn 5613: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 5614: $compare=join('',@{$locker});
1.746 raeburn 5615: if ($compare ne $symb_crs) {
5616: push(@new_locks, $locker);
5617: }
1.563 banghart 5618: }
5619: }
1.650 albertel 5620: if (scalar(@new_locks) > 0) {
1.563 banghart 5621: $current_permissions{$file} = \@new_locks;
5622: } else {
5623: push(@del_keys, $file);
1.613 albertel 5624: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 5625: delete($current_permissions{$file});
1.563 banghart 5626: }
5627: }
1.561 banghart 5628: }
1.613 albertel 5629: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 5630: return;
5631: }
1.512 banghart 5632:
1.17 www 5633: # ------------------------------------------------------------ Directory lister
5634:
5635: sub dirlist {
1.253 stredwic 5636: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
5637:
1.18 www 5638: $uri=~s/^\///;
5639: $uri=~s/\/$//;
1.253 stredwic 5640: my ($udom, $uname);
5641: (undef,$udom,$uname)=split(/\//,$uri);
5642: if(defined($userdomain)) {
5643: $udom = $userdomain;
5644: }
5645: if(defined($username)) {
5646: $uname = $username;
5647: }
5648:
5649: my $dirRoot = $perlvar{'lonDocRoot'};
5650: if(defined($alternateDirectoryRoot)) {
5651: $dirRoot = $alternateDirectoryRoot;
5652: $dirRoot =~ s/\/$//;
1.751 banghart 5653: }
1.253 stredwic 5654:
5655: if($udom) {
5656: if($uname) {
1.800 albertel 5657: my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
5658: &homeserver($uname,$udom));
1.605 matthew 5659: my @listing_results;
5660: if ($listing eq 'unknown_cmd') {
1.800 albertel 5661: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
5662: &homeserver($uname,$udom));
1.605 matthew 5663: @listing_results = split(/:/,$listing);
5664: } else {
5665: @listing_results = map { &unescape($_); } split(/:/,$listing);
5666: }
5667: return @listing_results;
1.253 stredwic 5668: } elsif(!defined($alternateDirectoryRoot)) {
1.800 albertel 5669: my %allusers;
1.841 albertel 5670: my %servers = &get_servers($udom,'library');
5671: foreach my $tryserver (keys(%servers)) {
5672: my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
5673: $udom, $tryserver);
5674: my @listing_results;
5675: if ($listing eq 'unknown_cmd') {
5676: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
5677: $udom, $tryserver);
5678: @listing_results = split(/:/,$listing);
5679: } else {
5680: @listing_results =
5681: map { &unescape($_); } split(/:/,$listing);
5682: }
5683: if ($listing_results[0] ne 'no_such_dir' &&
5684: $listing_results[0] ne 'empty' &&
5685: $listing_results[0] ne 'con_lost') {
5686: foreach my $line (@listing_results) {
5687: my ($entry) = split(/&/,$line,2);
5688: $allusers{$entry} = 1;
5689: }
5690: }
1.253 stredwic 5691: }
5692: my $alluserstr='';
1.800 albertel 5693: foreach my $user (sort(keys(%allusers))) {
5694: $alluserstr.=$user.'&user:';
1.253 stredwic 5695: }
5696: $alluserstr=~s/:$//;
5697: return split(/:/,$alluserstr);
5698: } else {
1.800 albertel 5699: return ('missing user name');
1.253 stredwic 5700: }
5701: } elsif(!defined($alternateDirectoryRoot)) {
1.841 albertel 5702: my @all_domains = sort(&all_domains());
5703: foreach my $domain (@all_domains) {
5704: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
5705: }
5706: return @all_domains;
5707: } else {
1.800 albertel 5708: return ('missing domain');
1.275 stredwic 5709: }
5710: }
5711:
5712: # --------------------------------------------- GetFileTimestamp
5713: # This function utilizes dirlist and returns the date stamp for
5714: # when it was last modified. It will also return an error of -1
5715: # if an error occurs
5716:
1.410 matthew 5717: ##
5718: ## FIXME: This subroutine assumes its caller knows something about the
5719: ## directory structure of the home server for the student ($root).
5720: ## Not a good assumption to make. Since this is for looking up files
5721: ## in user directories, the full path should be constructed by lond, not
5722: ## whatever machine we request data from.
5723: ##
1.275 stredwic 5724: sub GetFileTimestamp {
5725: my ($studentDomain,$studentName,$filename,$root)=@_;
1.807 albertel 5726: $studentDomain = &LONCAPA::clean_domain($studentDomain);
5727: $studentName = &LONCAPA::clean_username($studentName);
1.275 stredwic 5728: my $subdir=$studentName.'__';
5729: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
5730: my $proname="$studentDomain/$subdir/$studentName";
5731: $proname .= '/'.$filename;
1.375 matthew 5732: my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain,
5733: $studentName, $root);
1.275 stredwic 5734: my @stats = split('&', $fileStat);
5735: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 5736: # @stats contains first the filename, then the stat output
5737: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 5738: } else {
5739: return -1;
1.253 stredwic 5740: }
1.26 www 5741: }
5742:
1.712 albertel 5743: sub stat_file {
5744: my ($uri) = @_;
1.787 albertel 5745: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 5746:
1.712 albertel 5747: my ($udom,$uname,$file,$dir);
5748: if ($uri =~ m-^/(uploaded|editupload)/-) {
5749: ($udom,$uname,$file) =
1.811 albertel 5750: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 5751: $file = 'userfiles/'.$file;
1.740 www 5752: $dir = &propath($udom,$uname);
1.712 albertel 5753: }
5754: if ($uri =~ m-^/res/-) {
5755: ($udom,$uname) =
1.807 albertel 5756: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 5757: $file = $uri;
5758: }
5759:
5760: if (!$udom || !$uname || !$file) {
5761: # unable to handle the uri
5762: return ();
5763: }
5764:
5765: my ($result) = &dirlist($file,$udom,$uname,$dir);
5766: my @stats = split('&', $result);
1.721 banghart 5767:
1.712 albertel 5768: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
5769: shift(@stats); #filename is first
5770: return @stats;
5771: }
5772: return ();
5773: }
5774:
1.26 www 5775: # -------------------------------------------------------- Value of a Condition
5776:
1.713 albertel 5777: # gets the value of a specific preevaluated condition
5778: # stored in the string $env{user.state.<cid>}
5779: # or looks up a condition reference in the bighash and if if hasn't
5780: # already been evaluated recurses into docondval to get the value of
5781: # the condition, then memoizing it to
5782: # $env{user.state.<cid>.<condition>}
1.40 www 5783: sub directcondval {
5784: my $number=shift;
1.620 albertel 5785: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 5786: &Apache::lonuserstate::evalstate();
5787: }
1.713 albertel 5788: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
5789: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
5790: } elsif ($number =~ /^_/) {
5791: my $sub_condition;
5792: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
5793: &GDBM_READER(),0640)) {
5794: $sub_condition=$bighash{'conditions'.$number};
5795: untie(%bighash);
5796: }
5797: my $value = &docondval($sub_condition);
5798: &appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
5799: return $value;
5800: }
1.620 albertel 5801: if ($env{'user.state.'.$env{'request.course.id'}}) {
5802: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 5803: } else {
5804: return 2;
5805: }
5806: }
5807:
1.713 albertel 5808: # get the collection of conditions for this resource
1.26 www 5809: sub condval {
5810: my $condidx=shift;
1.54 www 5811: my $allpathcond='';
1.713 albertel 5812: foreach my $cond (split(/\|/,$condidx)) {
5813: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
5814: $allpathcond.=
5815: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
5816: }
1.191 harris41 5817: }
1.54 www 5818: $allpathcond=~s/\|$//;
1.713 albertel 5819: return &docondval($allpathcond);
5820: }
5821:
5822: #evaluates an expression of conditions
5823: sub docondval {
5824: my ($allpathcond) = @_;
5825: my $result=0;
5826: if ($env{'request.course.id'}
5827: && defined($allpathcond)) {
5828: my $operand='|';
5829: my @stack;
5830: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
5831: if ($chunk eq '(') {
5832: push @stack,($operand,$result);
5833: } elsif ($chunk eq ')') {
5834: my $before=pop @stack;
5835: if (pop @stack eq '&') {
5836: $result=$result>$before?$before:$result;
5837: } else {
5838: $result=$result>$before?$result:$before;
5839: }
5840: } elsif (($chunk eq '&') || ($chunk eq '|')) {
5841: $operand=$chunk;
5842: } else {
5843: my $new=directcondval($chunk);
5844: if ($operand eq '&') {
5845: $result=$result>$new?$new:$result;
5846: } else {
5847: $result=$result>$new?$result:$new;
5848: }
5849: }
5850: }
1.26 www 5851: }
5852: return $result;
1.421 albertel 5853: }
5854:
5855: # ---------------------------------------------------- Devalidate courseresdata
5856:
5857: sub devalidatecourseresdata {
5858: my ($coursenum,$coursedomain)=@_;
5859: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 5860: &devalidate_cache_new('courseres',$hashid);
1.28 www 5861: }
5862:
1.763 www 5863:
1.200 www 5864: # --------------------------------------------------- Course Resourcedata Query
5865:
1.624 albertel 5866: sub get_courseresdata {
5867: my ($coursenum,$coursedomain)=@_;
1.200 www 5868: my $coursehom=&homeserver($coursenum,$coursedomain);
5869: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 5870: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 5871: my %dumpreply;
1.417 albertel 5872: unless (defined($cached)) {
1.624 albertel 5873: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 5874: $result=\%dumpreply;
1.251 albertel 5875: my ($tmp) = keys(%dumpreply);
5876: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 5877: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 5878: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
5879: return $tmp;
1.416 albertel 5880: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 5881: $result=undef;
1.599 albertel 5882: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 5883: }
5884: }
1.624 albertel 5885: return $result;
5886: }
5887:
1.633 albertel 5888: sub devalidateuserresdata {
5889: my ($uname,$udom)=@_;
5890: my $hashid="$udom:$uname";
5891: &devalidate_cache_new('userres',$hashid);
5892: }
5893:
1.624 albertel 5894: sub get_userresdata {
5895: my ($uname,$udom)=@_;
5896: #most student don\'t have any data set, check if there is some data
5897: if (&EXT_cache_status($udom,$uname)) { return undef; }
5898:
5899: my $hashid="$udom:$uname";
5900: my ($result,$cached)=&is_cached_new('userres',$hashid);
5901: if (!defined($cached)) {
5902: my %resourcedata=&dump('resourcedata',$udom,$uname);
5903: $result=\%resourcedata;
5904: &do_cache_new('userres',$hashid,$result,600);
5905: }
5906: my ($tmp)=keys(%$result);
5907: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
5908: return $result;
5909: }
5910: #error 2 occurs when the .db doesn't exist
5911: if ($tmp!~/error: 2 /) {
1.672 albertel 5912: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 5913: " Trying to get resource data for ".
5914: $uname." at ".$udom.": ".
5915: $tmp."</font>");
5916: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 5917: #&EXT_cache_set($udom,$uname);
5918: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 5919: undef($tmp); # not really an error so don't send it back
1.624 albertel 5920: }
5921: return $tmp;
5922: }
5923:
5924: sub resdata {
5925: my ($name,$domain,$type,@which)=@_;
5926: my $result;
5927: if ($type eq 'course') {
5928: $result=&get_courseresdata($name,$domain);
5929: } elsif ($type eq 'user') {
5930: $result=&get_userresdata($name,$domain);
5931: }
5932: if (!ref($result)) { return $result; }
1.251 albertel 5933: foreach my $item (@which) {
1.417 albertel 5934: if (defined($result->{$item})) {
5935: return $result->{$item};
1.251 albertel 5936: }
1.250 albertel 5937: }
1.291 albertel 5938: return undef;
1.200 www 5939: }
5940:
1.379 matthew 5941: #
5942: # EXT resource caching routines
5943: #
5944:
5945: sub clear_EXT_cache_status {
1.383 albertel 5946: &delenv('cache.EXT.');
1.379 matthew 5947: }
5948:
5949: sub EXT_cache_status {
5950: my ($target_domain,$target_user) = @_;
1.383 albertel 5951: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 5952: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 5953: # We know already the user has no data
5954: return 1;
5955: } else {
5956: return 0;
5957: }
5958: }
5959:
5960: sub EXT_cache_set {
5961: my ($target_domain,$target_user) = @_;
1.383 albertel 5962: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633 albertel 5963: #&appenv($cachename => time);
1.379 matthew 5964: }
5965:
1.28 www 5966: # --------------------------------------------------------- Value of a Variable
1.58 www 5967: sub EXT {
1.715 albertel 5968:
1.395 albertel 5969: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 5970: unless ($varname) { return ''; }
1.218 albertel 5971: #get real user name/domain, courseid and symb
5972: my $courseid;
1.359 albertel 5973: my $publicuser;
1.427 www 5974: if ($symbparm) {
5975: $symbparm=&get_symb_from_alias($symbparm);
5976: }
1.218 albertel 5977: if (!($uname && $udom)) {
1.790 albertel 5978: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 5979: if (!$symbparm) { $symbparm=$cursymb; }
5980: } else {
1.620 albertel 5981: $courseid=$env{'request.course.id'};
1.218 albertel 5982: }
1.48 www 5983: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
5984: my $rest;
1.320 albertel 5985: if (defined($therest[0])) {
1.48 www 5986: $rest=join('.',@therest);
5987: } else {
5988: $rest='';
5989: }
1.320 albertel 5990:
1.57 www 5991: my $qualifierrest=$qualifier;
5992: if ($rest) { $qualifierrest.='.'.$rest; }
5993: my $spacequalifierrest=$space;
5994: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 5995: if ($realm eq 'user') {
1.48 www 5996: # --------------------------------------------------------------- user.resource
5997: if ($space eq 'resource') {
1.651 albertel 5998: if ( (defined($Apache::lonhomework::parsing_a_problem)
5999: || defined($Apache::lonhomework::parsing_a_task))
6000: &&
1.744 albertel 6001: ($symbparm eq &symbread()) ) {
6002: # if we are in the middle of processing the resource the
6003: # get the value we are planning on committing
6004: if (defined($Apache::lonhomework::results{$qualifierrest})) {
6005: return $Apache::lonhomework::results{$qualifierrest};
6006: } else {
6007: return $Apache::lonhomework::history{$qualifierrest};
6008: }
1.335 albertel 6009: } else {
1.359 albertel 6010: my %restored;
1.620 albertel 6011: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 6012: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
6013: } else {
6014: %restored=&restore($symbparm,$courseid,$udom,$uname);
6015: }
1.335 albertel 6016: return $restored{$qualifierrest};
6017: }
1.48 www 6018: # ----------------------------------------------------------------- user.access
6019: } elsif ($space eq 'access') {
1.218 albertel 6020: # FIXME - not supporting calls for a specific user
1.48 www 6021: return &allowed($qualifier,$rest);
6022: # ------------------------------------------ user.preferences, user.environment
6023: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 6024: if (($uname eq $env{'user.name'}) &&
6025: ($udom eq $env{'user.domain'})) {
6026: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 6027: } else {
1.359 albertel 6028: my %returnhash;
6029: if (!$publicuser) {
6030: %returnhash=&userenvironment($udom,$uname,
6031: $qualifierrest);
6032: }
1.218 albertel 6033: return $returnhash{$qualifierrest};
6034: }
1.48 www 6035: # ----------------------------------------------------------------- user.course
6036: } elsif ($space eq 'course') {
1.218 albertel 6037: # FIXME - not supporting calls for a specific user
1.620 albertel 6038: return $env{join('.',('request.course',$qualifier))};
1.48 www 6039: # ------------------------------------------------------------------- user.role
6040: } elsif ($space eq 'role') {
1.218 albertel 6041: # FIXME - not supporting calls for a specific user
1.620 albertel 6042: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 6043: if ($qualifier eq 'value') {
6044: return $role;
6045: } elsif ($qualifier eq 'extent') {
6046: return $where;
6047: }
6048: # ----------------------------------------------------------------- user.domain
6049: } elsif ($space eq 'domain') {
1.218 albertel 6050: return $udom;
1.48 www 6051: # ------------------------------------------------------------------- user.name
6052: } elsif ($space eq 'name') {
1.218 albertel 6053: return $uname;
1.48 www 6054: # ---------------------------------------------------- Any other user namespace
1.29 www 6055: } else {
1.359 albertel 6056: my %reply;
6057: if (!$publicuser) {
6058: %reply=&get($space,[$qualifierrest],$udom,$uname);
6059: }
6060: return $reply{$qualifierrest};
1.48 www 6061: }
1.236 www 6062: } elsif ($realm eq 'query') {
6063: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 6064: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
6065: [$spacequalifierrest]);
1.620 albertel 6066: return $env{'form.'.$spacequalifierrest};
1.236 www 6067: } elsif ($realm eq 'request') {
1.48 www 6068: # ------------------------------------------------------------- request.browser
6069: if ($space eq 'browser') {
1.430 www 6070: if ($qualifier eq 'textremote') {
1.676 albertel 6071: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 6072: return 1;
6073: } else {
6074: return 0;
6075: }
6076: } else {
1.620 albertel 6077: return $env{'browser.'.$qualifier};
1.430 www 6078: }
1.57 www 6079: # ------------------------------------------------------------ request.filename
6080: } else {
1.620 albertel 6081: return $env{'request.'.$spacequalifierrest};
1.29 www 6082: }
1.28 www 6083: } elsif ($realm eq 'course') {
1.48 www 6084: # ---------------------------------------------------------- course.description
1.620 albertel 6085: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 6086: } elsif ($realm eq 'resource') {
1.165 www 6087:
1.620 albertel 6088: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 6089: if (!$symbparm) { $symbparm=&symbread(); }
6090: }
1.693 albertel 6091:
6092: if ($space eq 'title') {
6093: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
6094: return &gettitle($symbparm);
6095: }
6096:
6097: if ($space eq 'map') {
6098: my ($map) = &decode_symb($symbparm);
6099: return &symbread($map);
6100: }
6101:
6102: my ($section, $group, @groups);
1.593 albertel 6103: my ($courselevelm,$courselevel);
1.539 albertel 6104: if ($symbparm && defined($courseid) &&
1.620 albertel 6105: $courseid eq $env{'request.course.id'}) {
1.165 www 6106:
1.218 albertel 6107: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 6108:
1.60 www 6109: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 6110: my $symbp=$symbparm;
1.735 albertel 6111: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 6112:
6113: my $symbparm=$symbp.'.'.$spacequalifierrest;
6114: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
6115:
1.620 albertel 6116: if (($env{'user.name'} eq $uname) &&
6117: ($env{'user.domain'} eq $udom)) {
6118: $section=$env{'request.course.sec'};
1.733 raeburn 6119: @groups = split(/:/,$env{'request.course.groups'});
6120: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 6121: } else {
1.539 albertel 6122: if (! defined($usection)) {
1.551 albertel 6123: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 6124: } else {
6125: $section = $usection;
6126: }
1.733 raeburn 6127: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 6128: }
6129:
6130: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
6131: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
6132: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
6133:
1.593 albertel 6134: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 6135: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 6136: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 6137:
1.60 www 6138: # ----------------------------------------------------------- first, check user
1.624 albertel 6139:
6140: my $userreply=&resdata($uname,$udom,'user',
6141: ($courselevelr,$courselevelm,
6142: $courselevel));
6143: if (defined($userreply)) { return $userreply; }
1.95 www 6144:
1.594 albertel 6145: # ------------------------------------------------ second, check some of course
1.684 raeburn 6146: my $coursereply;
1.691 raeburn 6147: if (@groups > 0) {
6148: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
6149: $mapparm,$spacequalifierrest);
1.684 raeburn 6150: if (defined($coursereply)) { return $coursereply; }
6151: }
1.96 www 6152:
1.684 raeburn 6153: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624 albertel 6154: $env{'course.'.$courseid.'.domain'},
6155: 'course',
6156: ($seclevelr,$seclevelm,$seclevel,
6157: $courselevelr));
1.287 albertel 6158: if (defined($coursereply)) { return $coursereply; }
1.200 www 6159:
1.60 www 6160: # ------------------------------------------------------ third, check map parms
1.218 albertel 6161: my %parmhash=();
6162: my $thisparm='';
6163: if (tie(%parmhash,'GDBM_File',
1.620 albertel 6164: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 6165: &GDBM_READER(),0640)) {
1.218 albertel 6166: $thisparm=$parmhash{$symbparm};
6167: untie(%parmhash);
6168: }
6169: if ($thisparm) { return $thisparm; }
6170: }
1.594 albertel 6171: # ------------------------------------------ fourth, look in resource metadata
1.71 www 6172:
1.218 albertel 6173: $spacequalifierrest=~s/\./\_/;
1.282 albertel 6174: my $filename;
6175: if (!$symbparm) { $symbparm=&symbread(); }
6176: if ($symbparm) {
1.409 www 6177: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 6178: } else {
1.620 albertel 6179: $filename=$env{'request.filename'};
1.282 albertel 6180: }
6181: my $metadata=&metadata($filename,$spacequalifierrest);
1.288 albertel 6182: if (defined($metadata)) { return $metadata; }
1.282 albertel 6183: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288 albertel 6184: if (defined($metadata)) { return $metadata; }
1.142 www 6185:
1.594 albertel 6186: # ---------------------------------------------- fourth, look in rest pf course
1.593 albertel 6187: if ($symbparm && defined($courseid) &&
1.620 albertel 6188: $courseid eq $env{'request.course.id'}) {
1.624 albertel 6189: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
6190: $env{'course.'.$courseid.'.domain'},
6191: 'course',
6192: ($courselevelm,$courselevel));
1.593 albertel 6193: if (defined($coursereply)) { return $coursereply; }
6194: }
1.145 www 6195: # ------------------------------------------------------------------ Cascade up
1.218 albertel 6196: unless ($space eq '0') {
1.336 albertel 6197: my @parts=split(/_/,$space);
6198: my $id=pop(@parts);
6199: my $part=join('_',@parts);
6200: if ($part eq '') { $part='0'; }
6201: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 6202: $symbparm,$udom,$uname,$section,1);
1.337 albertel 6203: if (defined($partgeneral)) { return $partgeneral; }
1.218 albertel 6204: }
1.395 albertel 6205: if ($recurse) { return undef; }
6206: my $pack_def=&packages_tab_default($filename,$varname);
6207: if (defined($pack_def)) { return $pack_def; }
1.71 www 6208:
1.48 www 6209: # ---------------------------------------------------- Any other user namespace
6210: } elsif ($realm eq 'environment') {
6211: # ----------------------------------------------------------------- environment
1.620 albertel 6212: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
6213: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 6214: } else {
1.770 albertel 6215: if ($uname eq 'anonymous' && $udom eq '') {
6216: return '';
6217: }
1.219 albertel 6218: my %returnhash=&userenvironment($udom,$uname,
6219: $spacequalifierrest);
6220: return $returnhash{$spacequalifierrest};
6221: }
1.28 www 6222: } elsif ($realm eq 'system') {
1.48 www 6223: # ----------------------------------------------------------------- system.time
6224: if ($space eq 'time') {
6225: return time;
6226: }
1.696 albertel 6227: } elsif ($realm eq 'server') {
6228: # ----------------------------------------------------------------- system.time
6229: if ($space eq 'name') {
6230: return $ENV{'SERVER_NAME'};
6231: }
1.28 www 6232: }
1.48 www 6233: return '';
1.61 www 6234: }
6235:
1.691 raeburn 6236: sub check_group_parms {
6237: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
6238: my @groupitems = ();
6239: my $resultitem;
6240: my @levels = ($symbparm,$mapparm,$what);
6241: foreach my $group (@{$groups}) {
6242: foreach my $level (@levels) {
6243: my $item = $courseid.'.['.$group.'].'.$level;
6244: push(@groupitems,$item);
6245: }
6246: }
6247: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
6248: $env{'course.'.$courseid.'.domain'},
6249: 'course',@groupitems);
6250: return $coursereply;
6251: }
6252:
6253: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 6254: my ($courseid,@groups) = @_;
6255: @groups = sort(@groups);
1.691 raeburn 6256: return @groups;
6257: }
6258:
1.395 albertel 6259: sub packages_tab_default {
6260: my ($uri,$varname)=@_;
6261: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 6262:
6263: my (@extension,@specifics,$do_default);
6264: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 6265: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 6266: if ($pack_type eq 'default') {
6267: $do_default=1;
6268: } elsif ($pack_type eq 'extension') {
6269: push(@extension,[$package,$pack_type,$pack_part]);
1.848 albertel 6270: } elsif ($pack_part eq $part) {
6271: # only look at packages defaults for packages that this id is
1.738 albertel 6272: push(@specifics,[$package,$pack_type,$pack_part]);
6273: }
6274: }
6275: # first look for a package that matches the requested part id
6276: foreach my $package (@specifics) {
6277: my (undef,$pack_type,$pack_part)=@{$package};
6278: next if ($pack_part ne $part);
6279: if (defined($packagetab{"$pack_type&$name&default"})) {
6280: return $packagetab{"$pack_type&$name&default"};
6281: }
6282: }
6283: # look for any possible matching non extension_ package
6284: foreach my $package (@specifics) {
6285: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 6286: if (defined($packagetab{"$pack_type&$name&default"})) {
6287: return $packagetab{"$pack_type&$name&default"};
6288: }
1.585 albertel 6289: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 6290: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
6291: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 6292: }
6293: }
1.738 albertel 6294: # look for any posible extension_ match
6295: foreach my $package (@extension) {
6296: my ($package,$pack_type)=@{$package};
6297: if (defined($packagetab{"$pack_type&$name&default"})) {
6298: return $packagetab{"$pack_type&$name&default"};
6299: }
6300: if (defined($packagetab{$package."&$name&default"})) {
6301: return $packagetab{$package."&$name&default"};
6302: }
6303: }
6304: # look for a global default setting
6305: if ($do_default && defined($packagetab{"default&$name&default"})) {
6306: return $packagetab{"default&$name&default"};
6307: }
1.395 albertel 6308: return undef;
6309: }
6310:
1.334 albertel 6311: sub add_prefix_and_part {
6312: my ($prefix,$part)=@_;
6313: my $keyroot;
6314: if (defined($prefix) && $prefix !~ /^__/) {
6315: # prefix that has a part already
6316: $keyroot=$prefix;
6317: } elsif (defined($prefix)) {
6318: # prefix that is missing a part
6319: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
6320: } else {
6321: # no prefix at all
6322: if (defined($part)) { $keyroot='_'.$part; }
6323: }
6324: return $keyroot;
6325: }
6326:
1.71 www 6327: # ---------------------------------------------------------------- Get metadata
6328:
1.599 albertel 6329: my %metaentry;
1.71 www 6330: sub metadata {
1.176 www 6331: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 6332: $uri=&declutter($uri);
1.288 albertel 6333: # if it is a non metadata possible uri return quickly
1.529 albertel 6334: if (($uri eq '') ||
6335: (($uri =~ m|^/*adm/|) &&
1.698 albertel 6336: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423 albertel 6337: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.807 albertel 6338: ($uri =~ m|home/$match_username/public_html/|)) {
1.468 albertel 6339: return undef;
1.288 albertel 6340: }
1.73 www 6341: my $filename=$uri;
6342: $uri=~s/\.meta$//;
1.172 www 6343: #
6344: # Is the metadata already cached?
1.177 www 6345: # Look at timestamp of caching
1.172 www 6346: # Everything is cached by the main uri, libraries are never directly cached
6347: #
1.428 albertel 6348: if (!defined($liburi)) {
1.599 albertel 6349: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 6350: if (defined($cached)) { return $result->{':'.$what}; }
6351: }
6352: {
1.172 www 6353: #
6354: # Is this a recursive call for a library?
6355: #
1.599 albertel 6356: # if (! exists($metacache{$uri})) {
6357: # $metacache{$uri}={};
6358: # }
1.171 www 6359: if ($liburi) {
6360: $liburi=&declutter($liburi);
6361: $filename=$liburi;
1.401 bowersj2 6362: } else {
1.599 albertel 6363: &devalidate_cache_new('meta',$uri);
6364: undef(%metaentry);
1.401 bowersj2 6365: }
1.140 www 6366: my %metathesekeys=();
1.73 www 6367: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 6368: my $metastring;
1.768 albertel 6369: if ($uri !~ m -^(editupload)/-) {
1.543 albertel 6370: my $file=&filelocation('',&clutter($filename));
1.599 albertel 6371: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 6372: $metastring=&getfile($file);
1.489 albertel 6373: }
1.208 albertel 6374: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 6375: my $token;
1.140 www 6376: undef %metathesekeys;
1.71 www 6377: while ($token=$parser->get_token) {
1.339 albertel 6378: if ($token->[0] eq 'S') {
6379: if (defined($token->[2]->{'package'})) {
1.172 www 6380: #
6381: # This is a package - get package info
6382: #
1.339 albertel 6383: my $package=$token->[2]->{'package'};
6384: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
6385: if (defined($token->[2]->{'id'})) {
6386: $keyroot.='_'.$token->[2]->{'id'};
6387: }
1.599 albertel 6388: if ($metaentry{':packages'}) {
6389: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 6390: } else {
1.599 albertel 6391: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 6392: }
1.736 albertel 6393: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 6394: my $part=$keyroot;
6395: $part=~s/^\_//;
1.736 albertel 6396: if ($pack_entry=~/^\Q$package\E\&/ ||
6397: $pack_entry=~/^\Q$package\E_0\&/) {
6398: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 6399: # ignore package.tab specified default values
6400: # here &package_tab_default() will fetch those
6401: if ($subp eq 'default') { next; }
1.736 albertel 6402: my $value=$packagetab{$pack_entry};
1.432 albertel 6403: my $unikey;
6404: if ($pack =~ /_0$/) {
6405: $unikey='parameter_0_'.$name;
6406: $part=0;
6407: } else {
6408: $unikey='parameter'.$keyroot.'_'.$name;
6409: }
1.339 albertel 6410: if ($subp eq 'display') {
6411: $value.=' [Part: '.$part.']';
6412: }
1.599 albertel 6413: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 6414: $metathesekeys{$unikey}=1;
1.599 albertel 6415: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
6416: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 6417: }
1.599 albertel 6418: if (defined($metaentry{':'.$unikey.'.default'})) {
6419: $metaentry{':'.$unikey}=
6420: $metaentry{':'.$unikey.'.default'};
1.356 albertel 6421: }
1.339 albertel 6422: }
6423: }
6424: } else {
1.172 www 6425: #
6426: # This is not a package - some other kind of start tag
1.339 albertel 6427: #
6428: my $entry=$token->[1];
6429: my $unikey;
6430: if ($entry eq 'import') {
6431: $unikey='';
6432: } else {
6433: $unikey=$entry;
6434: }
6435: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
6436:
6437: if (defined($token->[2]->{'id'})) {
6438: $unikey.='_'.$token->[2]->{'id'};
6439: }
1.175 www 6440:
1.339 albertel 6441: if ($entry eq 'import') {
1.175 www 6442: #
6443: # Importing a library here
1.339 albertel 6444: #
6445: if ($depthcount<20) {
6446: my $location=$parser->get_text('/import');
6447: my $dir=$filename;
6448: $dir=~s|[^/]*$||;
6449: $location=&filelocation($dir,$location);
1.736 albertel 6450: my $metadata =
6451: &metadata($uri,'keys', $location,$unikey,
6452: $depthcount+1);
6453: foreach my $meta (split(',',$metadata)) {
6454: $metaentry{':'.$meta}=$metaentry{':'.$meta};
6455: $metathesekeys{$meta}=1;
1.339 albertel 6456: }
6457: }
6458: } else {
6459:
6460: if (defined($token->[2]->{'name'})) {
6461: $unikey.='_'.$token->[2]->{'name'};
6462: }
6463: $metathesekeys{$unikey}=1;
1.736 albertel 6464: foreach my $param (@{$token->[3]}) {
6465: $metaentry{':'.$unikey.'.'.$param} =
6466: $token->[2]->{$param};
1.339 albertel 6467: }
6468: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 6469: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 6470: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
6471: # only ws inside the tag, and not in default, so use default
6472: # as value
1.599 albertel 6473: $metaentry{':'.$unikey}=$default;
1.339 albertel 6474: } else {
1.321 albertel 6475: # either something interesting inside the tag or default
6476: # uninteresting
1.599 albertel 6477: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 6478: }
1.172 www 6479: # end of not-a-package not-a-library import
1.339 albertel 6480: }
1.172 www 6481: # end of not-a-package start tag
1.339 albertel 6482: }
1.172 www 6483: # the next is the end of "start tag"
1.339 albertel 6484: }
6485: }
1.483 albertel 6486: my ($extension) = ($uri =~ /\.(\w+)$/);
1.737 albertel 6487: foreach my $key (keys(%packagetab)) {
1.483 albertel 6488: #no specific packages #how's our extension
6489: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 6490: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 6491: \%metathesekeys);
6492: }
1.599 albertel 6493: if (!exists($metaentry{':packages'})) {
1.737 albertel 6494: foreach my $key (keys(%packagetab)) {
1.483 albertel 6495: #no specific packages well let's get default then
6496: if ($key!~/^default&/) { next; }
1.488 albertel 6497: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 6498: \%metathesekeys);
6499: }
6500: }
1.338 www 6501: # are there custom rights to evaluate
1.599 albertel 6502: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 6503:
1.338 www 6504: #
6505: # Importing a rights file here
1.339 albertel 6506: #
6507: unless ($depthcount) {
1.599 albertel 6508: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 6509: my $dir=$filename;
6510: $dir=~s|[^/]*$||;
6511: $location=&filelocation($dir,$location);
1.736 albertel 6512: my $rights_metadata =
6513: &metadata($uri,'keys',$location,'_rights',
6514: $depthcount+1);
6515: foreach my $rights (split(',',$rights_metadata)) {
6516: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
6517: $metathesekeys{$rights}=1;
1.339 albertel 6518: }
6519: }
6520: }
1.737 albertel 6521: # uniqifiy package listing
6522: my %seen;
6523: my @uniq_packages =
6524: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
6525: $metaentry{':packages'} = join(',',@uniq_packages);
6526:
6527: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 6528: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
6529: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699 albertel 6530: &do_cache_new('meta',$uri,\%metaentry,60*60);
1.177 www 6531: # this is the end of "was not already recently cached
1.71 www 6532: }
1.599 albertel 6533: return $metaentry{':'.$what};
1.261 albertel 6534: }
6535:
1.488 albertel 6536: sub metadata_create_package_def {
1.483 albertel 6537: my ($uri,$key,$package,$metathesekeys)=@_;
6538: my ($pack,$name,$subp)=split(/\&/,$key);
6539: if ($subp eq 'default') { next; }
6540:
1.599 albertel 6541: if (defined($metaentry{':packages'})) {
6542: $metaentry{':packages'}.=','.$package;
1.483 albertel 6543: } else {
1.599 albertel 6544: $metaentry{':packages'}=$package;
1.483 albertel 6545: }
6546: my $value=$packagetab{$key};
6547: my $unikey;
6548: $unikey='parameter_0_'.$name;
1.599 albertel 6549: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 6550: $$metathesekeys{$unikey}=1;
1.599 albertel 6551: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
6552: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 6553: }
1.599 albertel 6554: if (defined($metaentry{':'.$unikey.'.default'})) {
6555: $metaentry{':'.$unikey}=
6556: $metaentry{':'.$unikey.'.default'};
1.483 albertel 6557: }
6558: }
6559:
1.261 albertel 6560: sub metadata_generate_part0 {
6561: my ($metadata,$metacache,$uri) = @_;
6562: my %allnames;
1.737 albertel 6563: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 6564: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 6565: my $part=$$metacache{':'.$metakey.'.part'};
6566: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 6567: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 6568: $allnames{$name}=$part;
6569: }
6570: }
6571: }
6572: foreach my $name (keys(%allnames)) {
6573: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 6574: my $key=":parameter_0_$name";
1.261 albertel 6575: $$metacache{"$key.part"}='0';
6576: $$metacache{"$key.name"}=$name;
1.428 albertel 6577: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 6578: $allnames{$name}.'_'.$name.
6579: '.type'};
1.428 albertel 6580: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 6581: '.display'};
1.644 www 6582: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 6583: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 6584: $$metacache{"$key.display"}=$olddis;
6585: }
1.71 www 6586: }
6587:
1.764 albertel 6588: # ------------------------------------------------------ Devalidate title cache
6589:
6590: sub devalidate_title_cache {
6591: my ($url)=@_;
6592: if (!$env{'request.course.id'}) { return; }
6593: my $symb=&symbread($url);
6594: if (!$symb) { return; }
6595: my $key=$env{'request.course.id'}."\0".$symb;
6596: &devalidate_cache_new('title',$key);
6597: }
6598:
1.301 www 6599: # ------------------------------------------------- Get the title of a resource
6600:
6601: sub gettitle {
6602: my $urlsymb=shift;
6603: my $symb=&symbread($urlsymb);
1.534 albertel 6604: if ($symb) {
1.620 albertel 6605: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 6606: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 6607: if (defined($cached)) {
6608: return $result;
6609: }
1.534 albertel 6610: my ($map,$resid,$url)=&decode_symb($symb);
6611: my $title='';
6612: my %bighash;
1.620 albertel 6613: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.534 albertel 6614: &GDBM_READER(),0640)) {
6615: my $mapid=$bighash{'map_pc_'.&clutter($map)};
6616: $title=$bighash{'title_'.$mapid.'.'.$resid};
6617: untie %bighash;
6618: }
6619: $title=~s/\&colon\;/\:/gs;
6620: if ($title) {
1.599 albertel 6621: return &do_cache_new('title',$key,$title,600);
1.534 albertel 6622: }
6623: $urlsymb=$url;
6624: }
6625: my $title=&metadata($urlsymb,'title');
6626: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
6627: return $title;
1.301 www 6628: }
1.613 albertel 6629:
1.614 albertel 6630: sub get_slot {
6631: my ($which,$cnum,$cdom)=@_;
6632: if (!$cnum || !$cdom) {
1.790 albertel 6633: (undef,my $courseid)=&whichuser();
1.620 albertel 6634: $cdom=$env{'course.'.$courseid.'.domain'};
6635: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 6636: }
1.703 albertel 6637: my $key=join("\0",'slots',$cdom,$cnum,$which);
6638: my %slotinfo;
6639: if (exists($remembered{$key})) {
6640: $slotinfo{$which} = $remembered{$key};
6641: } else {
6642: %slotinfo=&get('slots',[$which],$cdom,$cnum);
6643: &Apache::lonhomework::showhash(%slotinfo);
6644: my ($tmp)=keys(%slotinfo);
6645: if ($tmp=~/^error:/) { return (); }
6646: $remembered{$key} = $slotinfo{$which};
6647: }
1.616 albertel 6648: if (ref($slotinfo{$which}) eq 'HASH') {
6649: return %{$slotinfo{$which}};
6650: }
6651: return $slotinfo{$which};
1.614 albertel 6652: }
1.31 www 6653: # ------------------------------------------------- Update symbolic store links
6654:
6655: sub symblist {
6656: my ($mapname,%newhash)=@_;
1.438 www 6657: $mapname=&deversion(&declutter($mapname));
1.31 www 6658: my %hash;
1.620 albertel 6659: if (($env{'request.course.fn'}) && (%newhash)) {
6660: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 6661: &GDBM_WRCREAT(),0640)) {
1.711 albertel 6662: foreach my $url (keys %newhash) {
6663: next if ($url eq 'last_known'
6664: && $env{'form.no_update_last_known'});
6665: $hash{declutter($url)}=&encode_symb($mapname,
6666: $newhash{$url}->[1],
6667: $newhash{$url}->[0]);
1.191 harris41 6668: }
1.31 www 6669: if (untie(%hash)) {
6670: return 'ok';
6671: }
6672: }
6673: }
6674: return 'error';
1.212 www 6675: }
6676:
6677: # --------------------------------------------------------------- Verify a symb
6678:
6679: sub symbverify {
1.510 www 6680: my ($symb,$thisurl)=@_;
6681: my $thisfn=$thisurl;
1.439 www 6682: $thisfn=&declutter($thisfn);
1.215 www 6683: # direct jump to resource in page or to a sequence - will construct own symbs
6684: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
6685: # check URL part
1.409 www 6686: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 6687:
1.431 www 6688: unless ($url eq $thisfn) { return 0; }
1.213 www 6689:
1.216 www 6690: $symb=&symbclean($symb);
1.510 www 6691: $thisurl=&deversion($thisurl);
1.439 www 6692: $thisfn=&deversion($thisfn);
1.213 www 6693:
6694: my %bighash;
6695: my $okay=0;
1.431 www 6696:
1.620 albertel 6697: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 6698: &GDBM_READER(),0640)) {
1.510 www 6699: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 6700: unless ($ids) {
1.510 www 6701: $ids=$bighash{'ids_/'.$thisurl};
1.216 www 6702: }
6703: if ($ids) {
6704: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 6705: foreach my $id (split(/\,/,$ids)) {
6706: my ($mapid,$resid)=split(/\./,$id);
1.216 www 6707: if (
6708: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
6709: eq $symb) {
1.620 albertel 6710: if (($env{'request.role.adv'}) ||
1.800 albertel 6711: $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582 albertel 6712: $okay=1;
6713: }
6714: }
1.216 www 6715: }
6716: }
1.213 www 6717: untie(%bighash);
6718: }
6719: return $okay;
1.31 www 6720: }
6721:
1.210 www 6722: # --------------------------------------------------------------- Clean-up symb
6723:
6724: sub symbclean {
6725: my $symb=shift;
1.568 albertel 6726: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 6727: # remove version from map
6728: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 6729:
1.210 www 6730: # remove version from URL
6731: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 6732:
1.507 www 6733: # remove wrapper
6734:
1.510 www 6735: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 6736: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 6737: return $symb;
1.409 www 6738: }
6739:
6740: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 6741:
6742: sub encode_symb {
6743: my ($map,$resid,$url)=@_;
6744: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
6745: }
1.409 www 6746:
6747: sub decode_symb {
1.568 albertel 6748: my $symb=shift;
6749: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
6750: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 6751: return (&fixversion($map),$resid,&fixversion($url));
6752: }
6753:
6754: sub fixversion {
6755: my $fn=shift;
1.609 banghart 6756: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 6757: my %bighash;
6758: my $uri=&clutter($fn);
1.620 albertel 6759: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 6760: # is this cached?
1.599 albertel 6761: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 6762: if (defined($cached)) { return $result; }
6763: # unfortunately not cached, or expired
1.620 albertel 6764: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 6765: &GDBM_READER(),0640)) {
6766: if ($bighash{'version_'.$uri}) {
6767: my $version=$bighash{'version_'.$uri};
1.444 www 6768: unless (($version eq 'mostrecent') ||
6769: ($version==&getversion($uri))) {
1.440 www 6770: $uri=~s/\.(\w+)$/\.$version\.$1/;
6771: }
6772: }
6773: untie %bighash;
1.413 www 6774: }
1.599 albertel 6775: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 6776: }
6777:
6778: sub deversion {
6779: my $url=shift;
6780: $url=~s/\.\d+\.(\w+)$/\.$1/;
6781: return $url;
1.210 www 6782: }
6783:
1.31 www 6784: # ------------------------------------------------------ Return symb list entry
6785:
6786: sub symbread {
1.249 www 6787: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 6788: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 6789: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 6790: # no filename provided? try from environment
1.44 www 6791: unless ($thisfn) {
1.620 albertel 6792: if ($env{'request.symb'}) {
6793: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 6794: }
1.620 albertel 6795: $thisfn=$env{'request.filename'};
1.44 www 6796: }
1.569 albertel 6797: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 6798: # is that filename actually a symb? Verify, clean, and return
6799: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 6800: if (&symbverify($thisfn,$1)) {
1.620 albertel 6801: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 6802: }
1.242 www 6803: }
1.44 www 6804: $thisfn=declutter($thisfn);
1.31 www 6805: my %hash;
1.37 www 6806: my %bighash;
6807: my $syval='';
1.620 albertel 6808: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 6809: my $targetfn = $thisfn;
1.609 banghart 6810: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 6811: $targetfn = 'adm/wrapper/'.$thisfn;
6812: }
1.687 albertel 6813: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
6814: $targetfn=$1;
6815: }
1.620 albertel 6816: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 6817: &GDBM_READER(),0640)) {
1.481 raeburn 6818: $syval=$hash{$targetfn};
1.37 www 6819: untie(%hash);
6820: }
6821: # ---------------------------------------------------------- There was an entry
6822: if ($syval) {
1.601 albertel 6823: #unless ($syval=~/\_\d+$/) {
1.620 albertel 6824: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601 albertel 6825: #&appenv('request.ambiguous' => $thisfn);
1.620 albertel 6826: #return $env{$cache_str}='';
1.601 albertel 6827: #}
6828: #$syval.=$1;
6829: #}
1.37 www 6830: } else {
6831: # ------------------------------------------------------- Was not in symb table
1.620 albertel 6832: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 6833: &GDBM_READER(),0640)) {
1.37 www 6834: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 6835: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 6836: unless ($ids) {
6837: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 6838: }
6839: unless ($ids) {
6840: # alias?
6841: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 6842: }
1.37 www 6843: if ($ids) {
6844: # ------------------------------------------------------------------- Has ID(s)
6845: my @possibilities=split(/\,/,$ids);
1.39 www 6846: if ($#possibilities==0) {
6847: # ----------------------------------------------- There is only one possibility
1.37 www 6848: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 6849: $syval=&encode_symb($bighash{'map_id_'.$mapid},
6850: $resid,$thisfn);
1.249 www 6851: } elsif (!$donotrecurse) {
1.39 www 6852: # ------------------------------------------ There is more than one possibility
6853: my $realpossible=0;
1.800 albertel 6854: foreach my $id (@possibilities) {
6855: my $file=$bighash{'src_'.$id};
1.39 www 6856: if (&allowed('bre',$file)) {
1.800 albertel 6857: my ($mapid,$resid)=split(/\./,$id);
1.39 www 6858: if ($bighash{'map_type_'.$mapid} ne 'page') {
6859: $realpossible++;
1.626 albertel 6860: $syval=&encode_symb($bighash{'map_id_'.$mapid},
6861: $resid,$thisfn);
1.39 www 6862: }
6863: }
1.191 harris41 6864: }
1.39 www 6865: if ($realpossible!=1) { $syval=''; }
1.249 www 6866: } else {
6867: $syval='';
1.37 www 6868: }
6869: }
6870: untie(%bighash)
1.481 raeburn 6871: }
1.31 www 6872: }
1.62 www 6873: if ($syval) {
1.620 albertel 6874: return $env{$cache_str}=$syval;
1.62 www 6875: }
1.31 www 6876: }
1.44 www 6877: &appenv('request.ambiguous' => $thisfn);
1.620 albertel 6878: return $env{$cache_str}='';
1.31 www 6879: }
6880:
6881: # ---------------------------------------------------------- Return random seed
6882:
1.32 www 6883: sub numval {
6884: my $txt=shift;
6885: $txt=~tr/A-J/0-9/;
6886: $txt=~tr/a-j/0-9/;
6887: $txt=~tr/K-T/0-9/;
6888: $txt=~tr/k-t/0-9/;
6889: $txt=~tr/U-Z/0-5/;
6890: $txt=~tr/u-z/0-5/;
6891: $txt=~s/\D//g;
1.564 albertel 6892: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 6893: return int($txt);
1.368 albertel 6894: }
6895:
1.484 albertel 6896: sub numval2 {
6897: my $txt=shift;
6898: $txt=~tr/A-J/0-9/;
6899: $txt=~tr/a-j/0-9/;
6900: $txt=~tr/K-T/0-9/;
6901: $txt=~tr/k-t/0-9/;
6902: $txt=~tr/U-Z/0-5/;
6903: $txt=~tr/u-z/0-5/;
6904: $txt=~s/\D//g;
6905: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
6906: my $total;
6907: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 6908: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 6909: return int($total);
6910: }
6911:
1.575 albertel 6912: sub numval3 {
6913: use integer;
6914: my $txt=shift;
6915: $txt=~tr/A-J/0-9/;
6916: $txt=~tr/a-j/0-9/;
6917: $txt=~tr/K-T/0-9/;
6918: $txt=~tr/k-t/0-9/;
6919: $txt=~tr/U-Z/0-5/;
6920: $txt=~tr/u-z/0-5/;
6921: $txt=~s/\D//g;
6922: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
6923: my $total;
6924: foreach my $val (@txts) { $total+=$val; }
6925: if ($_64bit) { $total=(($total<<32)>>32); }
6926: return $total;
6927: }
6928:
1.675 albertel 6929: sub digest {
6930: my ($data)=@_;
6931: my $digest=&Digest::MD5::md5($data);
6932: my ($a,$b,$c,$d)=unpack("iiii",$digest);
6933: my ($e,$f);
6934: {
6935: use integer;
6936: $e=($a+$b);
6937: $f=($c+$d);
6938: if ($_64bit) {
6939: $e=(($e<<32)>>32);
6940: $f=(($f<<32)>>32);
6941: }
6942: }
6943: if (wantarray) {
6944: return ($e,$f);
6945: } else {
6946: my $g;
6947: {
6948: use integer;
6949: $g=($e+$f);
6950: if ($_64bit) {
6951: $g=(($g<<32)>>32);
6952: }
6953: }
6954: return $g;
6955: }
6956: }
6957:
1.368 albertel 6958: sub latest_rnd_algorithm_id {
1.675 albertel 6959: return '64bit5';
1.366 albertel 6960: }
1.32 www 6961:
1.503 albertel 6962: sub get_rand_alg {
6963: my ($courseid)=@_;
1.790 albertel 6964: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 6965: if ($courseid) {
1.620 albertel 6966: return $env{"course.$courseid.rndseed"};
1.503 albertel 6967: }
6968: return &latest_rnd_algorithm_id();
6969: }
6970:
1.562 albertel 6971: sub validCODE {
6972: my ($CODE)=@_;
6973: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
6974: return 0;
6975: }
6976:
1.491 albertel 6977: sub getCODE {
1.620 albertel 6978: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 6979: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
6980: defined($Apache::lonhomework::parsing_a_task) ) &&
6981: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 6982: return $Apache::lonhomework::history{'resource.CODE'};
6983: }
6984: return undef;
6985: }
6986:
1.31 www 6987: sub rndseed {
1.155 albertel 6988: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 6989: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.155 albertel 6990: if (!$symb) {
1.366 albertel 6991: unless ($symb=$wsymb) { return time; }
6992: }
6993: if (!$courseid) { $courseid=$wcourseid; }
6994: if (!$domain) { $domain=$wdomain; }
6995: if (!$username) { $username=$wusername }
1.503 albertel 6996: my $which=&get_rand_alg();
1.803 albertel 6997:
1.491 albertel 6998: if (defined(&getCODE())) {
1.675 albertel 6999: if ($which eq '64bit5') {
7000: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
7001: } elsif ($which eq '64bit4') {
1.575 albertel 7002: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
7003: } else {
7004: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
7005: }
1.675 albertel 7006: } elsif ($which eq '64bit5') {
7007: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 7008: } elsif ($which eq '64bit4') {
7009: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 7010: } elsif ($which eq '64bit3') {
7011: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 7012: } elsif ($which eq '64bit2') {
7013: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 7014: } elsif ($which eq '64bit') {
7015: return &rndseed_64bit($symb,$courseid,$domain,$username);
7016: }
7017: return &rndseed_32bit($symb,$courseid,$domain,$username);
7018: }
7019:
7020: sub rndseed_32bit {
7021: my ($symb,$courseid,$domain,$username)=@_;
7022: {
7023: use integer;
7024: my $symbchck=unpack("%32C*",$symb) << 27;
7025: my $symbseed=numval($symb) << 22;
7026: my $namechck=unpack("%32C*",$username) << 17;
7027: my $nameseed=numval($username) << 12;
7028: my $domainseed=unpack("%32C*",$domain) << 7;
7029: my $courseseed=unpack("%32C*",$courseid);
7030: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 7031: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7032: #&logthis("rndseed :$num:$symb");
1.564 albertel 7033: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 7034: return $num;
7035: }
7036: }
7037:
7038: sub rndseed_64bit {
7039: my ($symb,$courseid,$domain,$username)=@_;
7040: {
7041: use integer;
7042: my $symbchck=unpack("%32S*",$symb) << 21;
7043: my $symbseed=numval($symb) << 10;
7044: my $namechck=unpack("%32S*",$username);
7045:
7046: my $nameseed=numval($username) << 21;
7047: my $domainseed=unpack("%32S*",$domain) << 10;
7048: my $courseseed=unpack("%32S*",$courseid);
7049:
7050: my $num1=$symbchck+$symbseed+$namechck;
7051: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7052: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7053: #&logthis("rndseed :$num:$symb");
1.564 albertel 7054: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 7055: return "$num1,$num2";
1.155 albertel 7056: }
1.366 albertel 7057: }
7058:
1.443 albertel 7059: sub rndseed_64bit2 {
7060: my ($symb,$courseid,$domain,$username)=@_;
7061: {
7062: use integer;
7063: # strings need to be an even # of cahracters long, it it is odd the
7064: # last characters gets thrown away
7065: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7066: my $symbseed=numval($symb) << 10;
7067: my $namechck=unpack("%32S*",$username.' ');
7068:
7069: my $nameseed=numval($username) << 21;
1.501 albertel 7070: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7071: my $courseseed=unpack("%32S*",$courseid.' ');
7072:
7073: my $num1=$symbchck+$symbseed+$namechck;
7074: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7075: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7076: #&logthis("rndseed :$num:$symb");
1.803 albertel 7077: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 7078: return "$num1,$num2";
7079: }
7080: }
7081:
7082: sub rndseed_64bit3 {
7083: my ($symb,$courseid,$domain,$username)=@_;
7084: {
7085: use integer;
7086: # strings need to be an even # of cahracters long, it it is odd the
7087: # last characters gets thrown away
7088: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7089: my $symbseed=numval2($symb) << 10;
7090: my $namechck=unpack("%32S*",$username.' ');
7091:
7092: my $nameseed=numval2($username) << 21;
1.443 albertel 7093: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7094: my $courseseed=unpack("%32S*",$courseid.' ');
7095:
7096: my $num1=$symbchck+$symbseed+$namechck;
7097: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7098: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7099: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 7100: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7101:
1.503 albertel 7102: return "$num1:$num2";
1.443 albertel 7103: }
7104: }
7105:
1.575 albertel 7106: sub rndseed_64bit4 {
7107: my ($symb,$courseid,$domain,$username)=@_;
7108: {
7109: use integer;
7110: # strings need to be an even # of cahracters long, it it is odd the
7111: # last characters gets thrown away
7112: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7113: my $symbseed=numval3($symb) << 10;
7114: my $namechck=unpack("%32S*",$username.' ');
7115:
7116: my $nameseed=numval3($username) << 21;
7117: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7118: my $courseseed=unpack("%32S*",$courseid.' ');
7119:
7120: my $num1=$symbchck+$symbseed+$namechck;
7121: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7122: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7123: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 7124: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7125:
7126: return "$num1:$num2";
7127: }
7128: }
7129:
1.675 albertel 7130: sub rndseed_64bit5 {
7131: my ($symb,$courseid,$domain,$username)=@_;
7132: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
7133: return "$num1:$num2";
7134: }
7135:
1.366 albertel 7136: sub rndseed_CODE_64bit {
7137: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 7138: {
1.366 albertel 7139: use integer;
1.443 albertel 7140: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 7141: my $symbseed=numval2($symb);
1.491 albertel 7142: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7143: my $CODEseed=numval(&getCODE());
1.443 albertel 7144: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 7145: my $num1=$symbseed+$CODEchck;
7146: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 7147: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7148: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 7149: if ($_64bit) { $num1=(($num1<<32)>>32); }
7150: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 7151: return "$num1:$num2";
1.366 albertel 7152: }
7153: }
7154:
1.575 albertel 7155: sub rndseed_CODE_64bit4 {
7156: my ($symb,$courseid,$domain,$username)=@_;
7157: {
7158: use integer;
7159: my $symbchck=unpack("%32S*",$symb.' ') << 16;
7160: my $symbseed=numval3($symb);
7161: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7162: my $CODEseed=numval3(&getCODE());
7163: my $courseseed=unpack("%32S*",$courseid.' ');
7164: my $num1=$symbseed+$CODEchck;
7165: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 7166: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7167: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 7168: if ($_64bit) { $num1=(($num1<<32)>>32); }
7169: if ($_64bit) { $num2=(($num2<<32)>>32); }
7170: return "$num1:$num2";
7171: }
7172: }
7173:
1.675 albertel 7174: sub rndseed_CODE_64bit5 {
7175: my ($symb,$courseid,$domain,$username)=@_;
7176: my $code = &getCODE();
7177: my ($num1,$num2)=&digest("$symb,$courseid,$code");
7178: return "$num1:$num2";
7179: }
7180:
1.366 albertel 7181: sub setup_random_from_rndseed {
7182: my ($rndseed)=@_;
1.503 albertel 7183: if ($rndseed =~/([,:])/) {
7184: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 7185: &Math::Random::random_set_seed(abs($num1),abs($num2));
7186: } else {
7187: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 7188: }
1.36 albertel 7189: }
7190:
1.474 albertel 7191: sub latest_receipt_algorithm_id {
1.835 albertel 7192: return 'receipt3';
1.474 albertel 7193: }
7194:
1.480 www 7195: sub recunique {
7196: my $fucourseid=shift;
7197: my $unique;
1.835 albertel 7198: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
7199: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 7200: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 7201: } else {
7202: $unique=$perlvar{'lonReceipt'};
7203: }
7204: return unpack("%32C*",$unique);
7205: }
7206:
7207: sub recprefix {
7208: my $fucourseid=shift;
7209: my $prefix;
1.835 albertel 7210: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
7211: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 7212: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 7213: } else {
7214: $prefix=$perlvar{'lonHostID'};
7215: }
7216: return unpack("%32C*",$prefix);
7217: }
7218:
1.76 www 7219: sub ireceipt {
1.474 albertel 7220: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 7221:
7222: my $return =&recprefix($fucourseid).'-';
7223:
7224: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
7225: $env{'request.state'} eq 'construct') {
7226: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
7227: return $return;
7228: }
7229:
1.76 www 7230: my $cuname=unpack("%32C*",$funame);
7231: my $cudom=unpack("%32C*",$fudom);
7232: my $cucourseid=unpack("%32C*",$fucourseid);
7233: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 7234: my $cunique=&recunique($fucourseid);
1.474 albertel 7235: my $cpart=unpack("%32S*",$part);
1.835 albertel 7236: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
7237:
1.790 albertel 7238: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 7239:
7240: $return.= ($cunique%$cuname+
7241: $cunique%$cudom+
7242: $cusymb%$cuname+
7243: $cusymb%$cudom+
7244: $cucourseid%$cuname+
7245: $cucourseid%$cudom+
7246: $cpart%$cuname+
7247: $cpart%$cudom);
7248: } else {
7249: $return.= ($cunique%$cuname+
7250: $cunique%$cudom+
7251: $cusymb%$cuname+
7252: $cusymb%$cudom+
7253: $cucourseid%$cuname+
7254: $cucourseid%$cudom);
7255: }
7256: return $return;
1.76 www 7257: }
7258:
7259: sub receipt {
1.474 albertel 7260: my ($part)=@_;
1.790 albertel 7261: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 7262: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 7263: }
1.260 ng 7264:
1.790 albertel 7265: sub whichuser {
7266: my ($passedsymb)=@_;
7267: my ($symb,$courseid,$domain,$name,$publicuser);
7268: if (defined($env{'form.grade_symb'})) {
7269: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
7270: my $allowed=&allowed('vgr',$tmp_courseid);
7271: if (!$allowed &&
7272: exists($env{'request.course.sec'}) &&
7273: $env{'request.course.sec'} !~ /^\s*$/) {
7274: $allowed=&allowed('vgr',$tmp_courseid.
7275: '/'.$env{'request.course.sec'});
7276: }
7277: if ($allowed) {
7278: ($symb)=&get_env_multiple('form.grade_symb');
7279: $courseid=$tmp_courseid;
7280: ($domain)=&get_env_multiple('form.grade_domain');
7281: ($name)=&get_env_multiple('form.grade_username');
7282: return ($symb,$courseid,$domain,$name,$publicuser);
7283: }
7284: }
7285: if (!$passedsymb) {
7286: $symb=&symbread();
7287: } else {
7288: $symb=$passedsymb;
7289: }
7290: $courseid=$env{'request.course.id'};
7291: $domain=$env{'user.domain'};
7292: $name=$env{'user.name'};
7293: if ($name eq 'public' && $domain eq 'public') {
7294: if (!defined($env{'form.username'})) {
7295: $env{'form.username'}.=time.rand(10000000);
7296: }
7297: $name.=$env{'form.username'};
7298: }
7299: return ($symb,$courseid,$domain,$name,$publicuser);
7300:
7301: }
7302:
1.36 albertel 7303: # ------------------------------------------------------------ Serves up a file
1.472 albertel 7304: # returns either the contents of the file or
7305: # -1 if the file doesn't exist
1.481 raeburn 7306: #
7307: # if the target is a file that was uploaded via DOCS,
7308: # a check will be made to see if a current copy exists on the local server,
7309: # if it does this will be served, otherwise a copy will be retrieved from
7310: # the home server for the course and stored in /home/httpd/html/userfiles on
7311: # the local server.
1.472 albertel 7312:
1.36 albertel 7313: sub getfile {
1.538 albertel 7314: my ($file) = @_;
1.609 banghart 7315: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 7316: &repcopy($file);
7317: return &readfile($file);
7318: }
7319:
7320: sub repcopy_userfile {
7321: my ($file)=@_;
1.609 banghart 7322: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 7323: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 7324: my ($cdom,$cnum,$filename) =
1.811 albertel 7325: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 7326: my $uri="/uploaded/$cdom/$cnum/$filename";
7327: if (-e "$file") {
1.828 www 7328: # we already have a local copy, check it out
1.538 albertel 7329: my @fileinfo = stat($file);
1.828 www 7330: my $rtncode;
7331: my $info;
1.538 albertel 7332: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 7333: if ($lwpresp ne 'ok') {
1.828 www 7334: # there is no such file anymore, even though we had a local copy
1.482 albertel 7335: if ($rtncode eq '404') {
1.538 albertel 7336: unlink($file);
1.482 albertel 7337: }
7338: return -1;
7339: }
7340: if ($info < $fileinfo[9]) {
1.828 www 7341: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 7342: return 'ok';
1.828 www 7343: } else {
7344: # the file is outdated, get rid of it
7345: unlink($file);
1.482 albertel 7346: }
1.828 www 7347: }
7348: # one way or the other, at this point, we don't have the file
7349: # construct the correct path for the file
7350: my @parts = ($cdom,$cnum);
7351: if ($filename =~ m|^(.+)/[^/]+$|) {
7352: push @parts, split(/\//,$1);
7353: }
7354: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
7355: foreach my $part (@parts) {
7356: $path .= '/'.$part;
7357: if (!-e $path) {
7358: mkdir($path,0770);
1.482 albertel 7359: }
7360: }
1.828 www 7361: # now the path exists for sure
7362: # get a user agent
7363: my $ua=new LWP::UserAgent;
7364: my $transferfile=$file.'.in.transfer';
7365: # FIXME: this should flock
7366: if (-e $transferfile) { return 'ok'; }
7367: my $request;
7368: $uri=~s/^\///;
1.838 albertel 7369: $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828 www 7370: my $response=$ua->request($request,$transferfile);
7371: # did it work?
7372: if ($response->is_error()) {
7373: unlink($transferfile);
7374: &logthis("Userfile repcopy failed for $uri");
7375: return -1;
7376: }
7377: # worked, rename the transfer file
7378: rename($transferfile,$file);
1.607 raeburn 7379: return 'ok';
1.481 raeburn 7380: }
7381:
1.517 albertel 7382: sub tokenwrapper {
7383: my $uri=shift;
1.552 albertel 7384: $uri=~s|^http\://([^/]+)||;
7385: $uri=~s|^/||;
1.620 albertel 7386: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 7387: my $token=$1;
1.552 albertel 7388: my (undef,$udom,$uname,$file)=split('/',$uri,4);
7389: if ($udom && $uname && $file) {
7390: $file=~s|(\?\.*)*$||;
1.620 albertel 7391: &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.838 albertel 7392: return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517 albertel 7393: (($uri=~/\?/)?'&':'?').'token='.$token.
7394: '&tokenissued='.$perlvar{'lonHostID'};
7395: } else {
7396: return '/adm/notfound.html';
7397: }
7398: }
7399:
1.828 www 7400: # call with reqtype HEAD: get last modification time
7401: # call with reqtype GET: get the file contents
7402: # Do not call this with reqtype GET for large files! It loads everything into memory
7403: #
1.481 raeburn 7404: sub getuploaded {
7405: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
7406: $uri=~s/^\///;
1.838 albertel 7407: $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481 raeburn 7408: my $ua=new LWP::UserAgent;
7409: my $request=new HTTP::Request($reqtype,$uri);
7410: my $response=$ua->request($request);
7411: $$rtncode = $response->code;
1.482 albertel 7412: if (! $response->is_success()) {
7413: return 'failed';
7414: }
7415: if ($reqtype eq 'HEAD') {
1.486 www 7416: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 7417: } elsif ($reqtype eq 'GET') {
7418: $$info = $response->content;
1.472 albertel 7419: }
1.482 albertel 7420: return 'ok';
1.36 albertel 7421: }
7422:
1.481 raeburn 7423: sub readfile {
7424: my $file = shift;
7425: if ( (! -e $file ) || ($file eq '') ) { return -1; };
7426: my $fh;
7427: open($fh,"<$file");
7428: my $a='';
1.800 albertel 7429: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 7430: return $a;
7431: }
7432:
1.36 albertel 7433: sub filelocation {
1.590 banghart 7434: my ($dir,$file) = @_;
7435: my $location;
7436: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 7437:
7438: if ($file =~ m-^/adm/-) {
7439: $file=~s-^/adm/wrapper/-/-;
7440: $file=~s-^/adm/coursedocs/showdoc/-/-;
7441: }
1.590 banghart 7442: if ($file=~m:^/~:) { # is a contruction space reference
7443: $location = $file;
7444: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 7445: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 7446: # is a correct contruction space reference
7447: $location = $file;
1.609 banghart 7448: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 7449: my ($udom,$uname,$filename)=
1.811 albertel 7450: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 7451: my $home=&homeserver($uname,$udom);
7452: my $is_me=0;
7453: my @ids=¤t_machine_ids();
7454: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
7455: if ($is_me) {
1.740 www 7456: $location=&propath($udom,$uname).
1.590 banghart 7457: '/userfiles/'.$filename;
7458: } else {
7459: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
7460: $udom.'/'.$uname.'/'.$filename;
7461: }
7462: } else {
7463: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
7464: $file=~s:^/res/:/:;
7465: if ( !( $file =~ m:^/:) ) {
7466: $location = $dir. '/'.$file;
7467: } else {
7468: $location = '/home/httpd/html/res'.$file;
7469: }
1.59 albertel 7470: }
1.590 banghart 7471: $location=~s://+:/:g; # remove duplicate /
7472: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
7473: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
7474: return $location;
1.46 www 7475: }
1.36 albertel 7476:
1.46 www 7477: sub hreflocation {
7478: my ($dir,$file)=@_;
1.460 albertel 7479: unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666 albertel 7480: $file=filelocation($dir,$file);
1.700 albertel 7481: } elsif ($file=~m-^/adm/-) {
7482: $file=~s-^/adm/wrapper/-/-;
7483: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 7484: }
7485: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
7486: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 7487: } elsif ($file=~m-/home/($match_username)/public_html/-) {
7488: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 7489: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 7490: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 7491: -/uploaded/$1/$2/-x;
1.46 www 7492: }
1.462 albertel 7493: return $file;
1.465 albertel 7494: }
7495:
7496: sub current_machine_domains {
1.853 albertel 7497: return &machine_domains(&hostname($perlvar{'lonHostID'}));
7498: }
7499:
7500: sub machine_domains {
7501: my ($hostname) = @_;
1.465 albertel 7502: my @domains;
1.838 albertel 7503: my %hostname = &all_hostnames();
1.465 albertel 7504: while( my($id, $name) = each(%hostname)) {
1.467 matthew 7505: # &logthis("-$id-$name-$hostname-");
1.465 albertel 7506: if ($hostname eq $name) {
1.844 albertel 7507: push(@domains,&host_domain($id));
1.465 albertel 7508: }
7509: }
7510: return @domains;
7511: }
7512:
7513: sub current_machine_ids {
1.853 albertel 7514: return &machine_ids(&hostname($perlvar{'lonHostID'}));
7515: }
7516:
7517: sub machine_ids {
7518: my ($hostname) = @_;
7519: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 7520: my @ids;
1.838 albertel 7521: my %hostname = &all_hostnames();
1.465 albertel 7522: while( my($id, $name) = each(%hostname)) {
1.467 matthew 7523: # &logthis("-$id-$name-$hostname-");
1.465 albertel 7524: if ($hostname eq $name) {
7525: push(@ids,$id);
7526: }
7527: }
7528: return @ids;
1.31 www 7529: }
7530:
1.824 raeburn 7531: sub additional_machine_domains {
7532: my @domains;
7533: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
7534: while( my $line = <$fh>) {
7535: $line =~ s/\s//g;
7536: push(@domains,$line);
7537: }
7538: return @domains;
7539: }
7540:
7541: sub default_login_domain {
7542: my $domain = $perlvar{'lonDefDomain'};
7543: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
7544: foreach my $posdom (¤t_machine_domains(),
7545: &additional_machine_domains()) {
7546: if (lc($posdom) eq lc($testdomain)) {
7547: $domain=$posdom;
7548: last;
7549: }
7550: }
7551: return $domain;
7552: }
7553:
1.31 www 7554: # ------------------------------------------------------------- Declutters URLs
7555:
7556: sub declutter {
7557: my $thisfn=shift;
1.569 albertel 7558: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 7559: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 7560: $thisfn=~s/^\///;
1.697 albertel 7561: $thisfn=~s|^adm/wrapper/||;
7562: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 7563: $thisfn=~s/^res\///;
1.235 www 7564: $thisfn=~s/\?.+$//;
1.268 www 7565: return $thisfn;
7566: }
7567:
7568: # ------------------------------------------------------------- Clutter up URLs
7569:
7570: sub clutter {
7571: my $thisfn='/'.&declutter(shift);
1.609 banghart 7572: unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) {
1.270 www 7573: $thisfn='/res'.$thisfn;
7574: }
1.694 albertel 7575: if ($thisfn !~m|/adm|) {
1.695 albertel 7576: if ($thisfn =~ m|/ext/|) {
1.694 albertel 7577: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 7578: } else {
7579: my ($ext) = ($thisfn =~ /\.(\w+)$/);
7580: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 7581: if ($embstyle eq 'ssi'
7582: || ($embstyle eq 'hdn')
7583: || ($embstyle eq 'rat')
7584: || ($embstyle eq 'prv')
7585: || ($embstyle eq 'ign')) {
7586: #do nothing with these
7587: } elsif (($embstyle eq 'img')
1.695 albertel 7588: || ($embstyle eq 'emb')
7589: || ($embstyle eq 'wrp')) {
7590: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 7591: } elsif ($embstyle eq 'unk'
7592: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 7593: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 7594: } else {
1.718 www 7595: # &logthis("Got a blank emb style");
1.695 albertel 7596: }
1.694 albertel 7597: }
7598: }
1.31 www 7599: return $thisfn;
1.12 www 7600: }
7601:
1.787 albertel 7602: sub clutter_with_no_wrapper {
7603: my $uri = &clutter(shift);
7604: if ($uri =~ m-^/adm/-) {
7605: $uri =~ s-^/adm/wrapper/-/-;
7606: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
7607: }
7608: return $uri;
7609: }
7610:
1.557 albertel 7611: sub freeze_escape {
7612: my ($value)=@_;
7613: if (ref($value)) {
7614: $value=&nfreeze($value);
7615: return '__FROZEN__'.&escape($value);
7616: }
7617: return &escape($value);
7618: }
7619:
1.11 www 7620:
1.557 albertel 7621: sub thaw_unescape {
7622: my ($value)=@_;
7623: if ($value =~ /^__FROZEN__/) {
7624: substr($value,0,10,undef);
7625: $value=&unescape($value);
7626: return &thaw($value);
7627: }
7628: return &unescape($value);
7629: }
7630:
1.436 albertel 7631: sub correct_line_ends {
7632: my ($result)=@_;
7633: $$result =~s/\r\n/\n/mg;
7634: $$result =~s/\r/\n/mg;
1.415 albertel 7635: }
1.1 albertel 7636: # ================================================================ Main Program
7637:
1.184 www 7638: sub goodbye {
1.204 albertel 7639: &logthis("Starting Shut down");
1.443 albertel 7640: #not converted to using infrastruture and probably shouldn't be
1.599 albertel 7641: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
1.443 albertel 7642: #converted
1.599 albertel 7643: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
7644: &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
7645: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
7646: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
1.425 albertel 7647: #1.1 only
1.599 albertel 7648: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
7649: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
7650: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
7651: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
7652: &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
7653: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
7654: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 7655: &flushcourselogs();
7656: &logthis("Shutting down");
7657: }
7658:
1.852 albertel 7659: sub get_dns {
7660: my ($url,$func) = @_;
7661: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
7662: foreach my $dns (<$config>) {
7663: next if ($dns !~ /^\^(\S*)/x);
7664: $dns = $1;
7665: my $ua=new LWP::UserAgent;
7666: my $request=new HTTP::Request('GET',"http://$dns$url");
7667: my $response=$ua->request($request);
7668: next if ($response->is_error());
7669: my @content = split("\n",$response->content);
7670: &$func(\@content);
7671: }
7672: close($config);
7673: }
1.327 albertel 7674: # ------------------------------------------------------------ Read domain file
7675: {
1.852 albertel 7676: my $loaded;
1.846 albertel 7677: my %domain;
7678:
1.852 albertel 7679: sub parse_domain_tab {
7680: my ($lines) = @_;
7681: foreach my $line (@$lines) {
7682: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 7683:
1.846 albertel 7684: chomp($line);
1.852 albertel 7685: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 7686: my %this_domain;
7687: foreach my $field ('description', 'auth_def', 'auth_arg_def',
7688: 'lang_def', 'city', 'longi', 'lati',
7689: 'primary') {
7690: $this_domain{$field} = shift(@elements);
7691: }
7692: $domain{$name} = \%this_domain;
1.852 albertel 7693: }
7694: }
1.864 albertel 7695:
7696: sub reset_domain_info {
7697: undef($loaded);
7698: undef(%domain);
7699: }
7700:
1.852 albertel 7701: sub load_domain_tab {
7702: &get_dns('/adm/dns/domain',\&parse_domain_tab);
7703: my $fh;
7704: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
7705: my @lines = <$fh>;
7706: &parse_domain_tab(\@lines);
1.448 albertel 7707: }
1.852 albertel 7708: close($fh);
7709: $loaded = 1;
1.327 albertel 7710: }
1.846 albertel 7711:
7712: sub domain {
1.852 albertel 7713: &load_domain_tab() if (!$loaded);
7714:
1.846 albertel 7715: my ($name,$what) = @_;
7716: return if ( !exists($domain{$name}) );
7717:
7718: if (!$what) {
7719: return $domain{$name}{'description'};
7720: }
7721: return $domain{$name}{$what};
7722: }
1.327 albertel 7723: }
7724:
7725:
1.1 albertel 7726: # ------------------------------------------------------------- Read hosts file
7727: {
1.838 albertel 7728: my %hostname;
1.844 albertel 7729: my %hostdom;
1.845 albertel 7730: my %libserv;
1.852 albertel 7731: my $loaded;
7732:
7733: sub parse_hosts_tab {
7734: my ($file) = @_;
7735: foreach my $configline (@$file) {
7736: next if ($configline =~ /^(\#|\s*$ )/x);
7737: next if ($configline =~ /^\^/);
7738: chomp($configline);
7739: my ($id,$domain,$role,$name)=split(/:/,$configline);
7740: $name=~s/\s//g;
7741: if ($id && $domain && $role && $name) {
7742: $hostname{$id}=$name;
7743: $hostdom{$id}=$domain;
7744: if ($role eq 'library') { $libserv{$id}=$name; }
7745: }
7746: }
7747: }
1.864 albertel 7748:
7749: sub reset_hosts_info {
7750: &reset_domain_info();
7751: &reset_hosts_ip_info();
7752: undef(%hostname);
7753: undef(%hostdom);
7754: undef(%libserv);
7755: undef($loaded);
7756: }
1.1 albertel 7757:
1.852 albertel 7758: sub load_hosts_tab {
7759: &get_dns('/adm/dns/hosts',\&parse_hosts_tab);
7760: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
7761: my @config = <$config>;
7762: &parse_hosts_tab(\@config);
7763: close($config);
7764: $loaded=1;
1.1 albertel 7765: }
1.852 albertel 7766:
1.838 albertel 7767: sub hostname {
1.852 albertel 7768: &load_hosts_tab() if (!$loaded);
7769:
1.838 albertel 7770: my ($lonid) = @_;
7771: return $hostname{$lonid};
7772: }
1.845 albertel 7773:
1.838 albertel 7774: sub all_hostnames {
1.852 albertel 7775: &load_hosts_tab() if (!$loaded);
7776:
1.838 albertel 7777: return %hostname;
7778: }
1.845 albertel 7779:
7780: sub is_library {
1.852 albertel 7781: &load_hosts_tab() if (!$loaded);
7782:
1.845 albertel 7783: return exists($libserv{$_[0]});
7784: }
7785:
7786: sub all_library {
1.852 albertel 7787: &load_hosts_tab() if (!$loaded);
7788:
1.845 albertel 7789: return %libserv;
7790: }
7791:
1.841 albertel 7792: sub get_servers {
1.852 albertel 7793: &load_hosts_tab() if (!$loaded);
7794:
1.841 albertel 7795: my ($domain,$type) = @_;
7796: my %possible_hosts = ($type eq 'library') ? %libserv
7797: : %hostname;
7798: my %result;
1.842 albertel 7799: if (ref($domain) eq 'ARRAY') {
7800: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 7801: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 7802: $result{$host} = $hostname;
7803: }
7804: }
7805: } else {
7806: while ( my ($host,$hostname) = each(%possible_hosts)) {
7807: if ($hostdom{$host} eq $domain) {
7808: $result{$host} = $hostname;
7809: }
1.841 albertel 7810: }
7811: }
7812: return %result;
7813: }
1.845 albertel 7814:
1.844 albertel 7815: sub host_domain {
1.852 albertel 7816: &load_hosts_tab() if (!$loaded);
7817:
1.844 albertel 7818: my ($lonid) = @_;
7819: return $hostdom{$lonid};
7820: }
7821:
1.841 albertel 7822: sub all_domains {
1.852 albertel 7823: &load_hosts_tab() if (!$loaded);
7824:
1.841 albertel 7825: my %seen;
7826: my @uniq = grep(!$seen{$_}++, values(%hostdom));
7827: return @uniq;
7828: }
1.1 albertel 7829: }
7830:
1.847 albertel 7831: {
7832: my %iphost;
1.856 albertel 7833: my %name_to_ip;
7834: my %lonid_to_ip;
1.847 albertel 7835: sub get_hosts_from_ip {
7836: my ($ip) = @_;
7837: my %iphosts = &get_iphost();
7838: if (ref($iphosts{$ip})) {
7839: return @{$iphosts{$ip}};
7840: }
7841: return;
1.839 albertel 7842: }
1.864 albertel 7843:
7844: sub reset_hosts_ip_info {
7845: undef(%iphost);
7846: undef(%name_to_ip);
7847: undef(%lonid_to_ip);
7848: }
1.856 albertel 7849:
7850: sub get_host_ip {
7851: my ($lonid) = @_;
7852: if (exists($lonid_to_ip{$lonid})) {
7853: return $lonid_to_ip{$lonid};
7854: }
7855: my $name=&hostname($lonid);
7856: my $ip = gethostbyname($name);
7857: return if (!$ip || length($ip) ne 4);
7858: $ip=inet_ntoa($ip);
7859: $name_to_ip{$name} = $ip;
7860: $lonid_to_ip{$lonid} = $ip;
7861: return $ip;
7862: }
1.847 albertel 7863:
7864: sub get_iphost {
7865: if (%iphost) { return %iphost; }
7866: my %hostname = &all_hostnames();
7867: foreach my $id (keys(%hostname)) {
1.864 albertel 7868: my $name=&hostname($id);
1.847 albertel 7869: my $ip;
7870: if (!exists($name_to_ip{$name})) {
7871: $ip = gethostbyname($name);
7872: if (!$ip || length($ip) ne 4) {
7873: &logthis("Skipping host $id name $name no IP found");
7874: next;
7875: }
7876: $ip=inet_ntoa($ip);
7877: $name_to_ip{$name} = $ip;
7878: } else {
7879: $ip = $name_to_ip{$name};
1.653 albertel 7880: }
1.856 albertel 7881: $lonid_to_ip{$id} = $ip;
1.847 albertel 7882: push(@{$iphost{$ip}},$id);
1.598 albertel 7883: }
1.847 albertel 7884: return %iphost;
1.598 albertel 7885: }
7886: }
7887:
1.862 albertel 7888: BEGIN {
7889:
7890: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
7891: unless ($readit) {
7892: {
7893: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
7894: %perlvar = (%perlvar,%{$configvars});
7895: }
7896:
7897:
1.1 albertel 7898: # ------------------------------------------------------ Read spare server file
7899: {
1.448 albertel 7900: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 7901:
7902: while (my $configline=<$config>) {
7903: chomp($configline);
1.284 matthew 7904: if ($configline) {
1.784 albertel 7905: my ($host,$type) = split(':',$configline,2);
1.785 albertel 7906: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 7907: push(@{ $spareid{$type} }, $host);
1.1 albertel 7908: }
7909: }
1.448 albertel 7910: close($config);
1.1 albertel 7911: }
1.11 www 7912: # ------------------------------------------------------------ Read permissions
7913: {
1.448 albertel 7914: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 7915:
7916: while (my $configline=<$config>) {
1.448 albertel 7917: chomp($configline);
7918: if ($configline) {
7919: my ($role,$perm)=split(/ /,$configline);
7920: if ($perm ne '') { $pr{$role}=$perm; }
7921: }
1.11 www 7922: }
1.448 albertel 7923: close($config);
1.11 www 7924: }
7925:
7926: # -------------------------------------------- Read plain texts for permissions
7927: {
1.448 albertel 7928: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 7929:
7930: while (my $configline=<$config>) {
1.448 albertel 7931: chomp($configline);
7932: if ($configline) {
1.742 raeburn 7933: my ($short,@plain)=split(/:/,$configline);
7934: %{$prp{$short}} = ();
7935: if (@plain > 0) {
7936: $prp{$short}{'std'} = $plain[0];
7937: for (my $i=1; $i<@plain; $i++) {
7938: $prp{$short}{'alt'.$i} = $plain[$i];
7939: }
7940: }
1.448 albertel 7941: }
1.135 www 7942: }
1.448 albertel 7943: close($config);
1.135 www 7944: }
7945:
7946: # ---------------------------------------------------------- Read package table
7947: {
1.448 albertel 7948: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 7949:
7950: while (my $configline=<$config>) {
1.483 albertel 7951: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 7952: chomp($configline);
7953: my ($short,$plain)=split(/:/,$configline);
7954: my ($pack,$name)=split(/\&/,$short);
7955: if ($plain ne '') {
7956: $packagetab{$pack.'&'.$name.'&name'}=$name;
7957: $packagetab{$short}=$plain;
7958: }
1.11 www 7959: }
1.448 albertel 7960: close($config);
1.329 matthew 7961: }
7962:
7963: # ------------- set up temporary directory
7964: {
7965: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
7966:
1.11 www 7967: }
7968:
1.794 albertel 7969: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
7970: 'compress_threshold'=> 20_000,
7971: });
1.185 www 7972:
1.281 www 7973: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 7974: $dumpcount=0;
1.22 www 7975:
1.163 harris41 7976: &logtouch();
1.672 albertel 7977: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 7978: $readit=1;
1.564 albertel 7979: {
7980: use integer;
7981: my $test=(2**32)+1;
1.568 albertel 7982: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 7983: &logthis(" Detected 64bit platform ($_64bit)");
7984: }
1.195 www 7985: }
1.1 albertel 7986: }
1.179 www 7987:
1.1 albertel 7988: 1;
1.191 harris41 7989: __END__
7990:
1.243 albertel 7991: =pod
7992:
1.191 harris41 7993: =head1 NAME
7994:
1.243 albertel 7995: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 7996:
7997: =head1 SYNOPSIS
7998:
1.243 albertel 7999: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 8000:
8001: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
8002:
1.243 albertel 8003: Common parameters:
8004:
8005: =over 4
8006:
8007: =item *
8008:
8009: $uname : an internal username (if $cname expecting a course Id specifically)
8010:
8011: =item *
8012:
8013: $udom : a domain (if $cdom expecting a course's domain specifically)
8014:
8015: =item *
8016:
8017: $symb : a resource instance identifier
8018:
8019: =item *
8020:
8021: $namespace : the name of a .db file that contains the data needed or
8022: being set.
8023:
8024: =back
8025:
1.394 bowersj2 8026: =head1 OVERVIEW
1.191 harris41 8027:
1.394 bowersj2 8028: lonnet provides subroutines which interact with the
8029: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
8030: about classes, users, and resources.
1.243 albertel 8031:
8032: For many of these objects you can also use this to store data about
8033: them or modify them in various ways.
1.191 harris41 8034:
1.394 bowersj2 8035: =head2 Symbs
1.191 harris41 8036:
1.394 bowersj2 8037: To identify a specific instance of a resource, LON-CAPA uses symbols
8038: or "symbs"X<symb>. These identifiers are built from the URL of the
8039: map, the resource number of the resource in the map, and the URL of
8040: the resource itself. The latter is somewhat redundant, but might help
8041: if maps change.
8042:
8043: An example is
8044:
8045: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
8046:
8047: The respective map entry is
8048:
8049: <resource id="19" src="/res/msu/korte/tests/part12.problem"
8050: title="Problem 2">
8051: </resource>
8052:
8053: Symbs are used by the random number generator, as well as to store and
8054: restore data specific to a certain instance of for example a problem.
8055:
8056: =head2 Storing And Retrieving Data
8057:
8058: X<store()>X<cstore()>X<restore()>Three of the most important functions
8059: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
8060: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
8061: is is the non-critical message twin of cstore. These functions are for
8062: handlers to store a perl hash to a user's permanent data space in an
8063: easy manner, and to retrieve it again on another call. It is expected
8064: that a handler would use this once at the beginning to retrieve data,
8065: and then again once at the end to send only the new data back.
8066:
8067: The data is stored in the user's data directory on the user's
8068: homeserver under the ID of the course.
8069:
8070: The hash that is returned by restore will have all of the previous
8071: value for all of the elements of the hash.
8072:
8073: Example:
8074:
8075: #creating a hash
8076: my %hash;
8077: $hash{'foo'}='bar';
8078:
8079: #storing it
8080: &Apache::lonnet::cstore(\%hash);
8081:
8082: #changing a value
8083: $hash{'foo'}='notbar';
8084:
8085: #adding a new value
8086: $hash{'bar'}='foo';
8087: &Apache::lonnet::cstore(\%hash);
8088:
8089: #retrieving the hash
8090: my %history=&Apache::lonnet::restore();
8091:
8092: #print the hash
8093: foreach my $key (sort(keys(%history))) {
8094: print("\%history{$key} = $history{$key}");
8095: }
8096:
8097: Will print out:
1.191 harris41 8098:
1.394 bowersj2 8099: %history{1:foo} = bar
8100: %history{1:keys} = foo:timestamp
8101: %history{1:timestamp} = 990455579
8102: %history{2:bar} = foo
8103: %history{2:foo} = notbar
8104: %history{2:keys} = foo:bar:timestamp
8105: %history{2:timestamp} = 990455580
8106: %history{bar} = foo
8107: %history{foo} = notbar
8108: %history{timestamp} = 990455580
8109: %history{version} = 2
8110:
8111: Note that the special hash entries C<keys>, C<version> and
8112: C<timestamp> were added to the hash. C<version> will be equal to the
8113: total number of versions of the data that have been stored. The
8114: C<timestamp> attribute will be the UNIX time the hash was
8115: stored. C<keys> is available in every historical section to list which
8116: keys were added or changed at a specific historical revision of a
8117: hash.
8118:
8119: B<Warning>: do not store the hash that restore returns directly. This
8120: will cause a mess since it will restore the historical keys as if the
8121: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 8122:
1.394 bowersj2 8123: Calling convention:
1.191 harris41 8124:
1.394 bowersj2 8125: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
8126: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 8127:
1.394 bowersj2 8128: For more detailed information, see lonnet specific documentation.
1.191 harris41 8129:
1.394 bowersj2 8130: =head1 RETURN MESSAGES
1.191 harris41 8131:
1.394 bowersj2 8132: =over 4
1.191 harris41 8133:
1.394 bowersj2 8134: =item * B<con_lost>: unable to contact remote host
1.191 harris41 8135:
1.394 bowersj2 8136: =item * B<con_delayed>: unable to contact remote host, message will be delivered
8137: when the connection is brought back up
1.191 harris41 8138:
1.394 bowersj2 8139: =item * B<con_failed>: unable to contact remote host and unable to save message
8140: for later delivery
1.191 harris41 8141:
1.394 bowersj2 8142: =item * B<error:>: an error a occured, a description of the error follows the :
1.191 harris41 8143:
1.394 bowersj2 8144: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 8145: that was requested
1.191 harris41 8146:
1.243 albertel 8147: =back
1.191 harris41 8148:
1.243 albertel 8149: =head1 PUBLIC SUBROUTINES
1.191 harris41 8150:
1.243 albertel 8151: =head2 Session Environment Functions
1.191 harris41 8152:
1.243 albertel 8153: =over 4
1.191 harris41 8154:
1.394 bowersj2 8155: =item *
8156: X<appenv()>
8157: B<appenv(%hash)>: the value of %hash is written to
8158: the user envirnoment file, and will be restored for each access this
1.620 albertel 8159: user makes during this session, also modifies the %env for the current
1.394 bowersj2 8160: process
1.191 harris41 8161:
8162: =item *
1.394 bowersj2 8163: X<delenv()>
8164: B<delenv($regexp)>: removes all items from the session
8165: environment file that matches the regular expression in $regexp. The
1.620 albertel 8166: values are also delted from the current processes %env.
1.191 harris41 8167:
1.795 albertel 8168: =item * get_env_multiple($name)
8169:
8170: gets $name from the %env hash, it seemlessly handles the cases where multiple
8171: values may be defined and end up as an array ref.
8172:
8173: returns an array of values
8174:
1.243 albertel 8175: =back
8176:
8177: =head2 User Information
1.191 harris41 8178:
1.243 albertel 8179: =over 4
1.191 harris41 8180:
8181: =item *
1.394 bowersj2 8182: X<queryauthenticate()>
8183: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 8184: authentication scheme
8185:
8186: =item *
1.394 bowersj2 8187: X<authenticate()>
8188: B<authenticate($uname,$upass,$udom)>: try to
8189: authenticate user from domain's lib servers (first use the current
8190: one). C<$upass> should be the users password.
1.191 harris41 8191:
8192: =item *
1.394 bowersj2 8193: X<homeserver()>
8194: B<homeserver($uname,$udom)>: find the server which has
8195: the user's directory and files (there must be only one), this caches
8196: the answer, and also caches if there is a borken connection.
1.191 harris41 8197:
8198: =item *
1.394 bowersj2 8199: X<idget()>
8200: B<idget($udom,@ids)>: find the usernames behind a list of IDs
8201: (IDs are a unique resource in a domain, there must be only 1 ID per
8202: username, and only 1 username per ID in a specific domain) (returns
8203: hash: id=>name,id=>name)
1.191 harris41 8204:
8205: =item *
1.394 bowersj2 8206: X<idrget()>
8207: B<idrget($udom,@unames)>: find the IDs behind a list of
8208: usernames (returns hash: name=>id,name=>id)
1.191 harris41 8209:
8210: =item *
1.394 bowersj2 8211: X<idput()>
8212: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 8213:
8214: =item *
1.394 bowersj2 8215: X<rolesinit()>
8216: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 8217:
8218: =item *
1.551 albertel 8219: X<getsection()>
8220: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 8221: course $cname, return section name/number or '' for "not in course"
8222: and '-1' for "no section"
8223:
8224: =item *
1.394 bowersj2 8225: X<userenvironment()>
8226: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 8227: passed in @what from the requested user's environment, returns a hash
8228:
1.858 raeburn 8229: =item *
8230: X<userlog_query()>
1.859 albertel 8231: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
8232: activity.log file. %filters defines filters applied when parsing the
8233: log file. These can be start or end timestamps, or the type of action
8234: - log to look for Login or Logout events, check for Checkin or
8235: Checkout, role for role selection. The response is in the form
8236: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
8237: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 8238:
1.243 albertel 8239: =back
8240:
8241: =head2 User Roles
8242:
8243: =over 4
8244:
8245: =item *
8246:
1.810 raeburn 8247: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 8248: F: full access
8249: U,I,K: authentication modes (cxx only)
8250: '': forbidden
8251: 1: user needs to choose course
8252: 2: browse allowed
1.766 albertel 8253: A: passphrase authentication needed
1.243 albertel 8254:
8255: =item *
8256:
8257: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
8258: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
8259: and course level
8260:
8261: =item *
8262:
8263: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
8264: explanation of a user role term
8265:
1.832 raeburn 8266: =item *
8267:
1.858 raeburn 8268: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
8269: All arguments are optional. Returns a hash of a roles, either for
8270: co-author/assistant author roles for a user's Construction Space
8271: (default), or if $context is 'user', roles for the user himself,
8272: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
8273: and value is set to colon-separated start and end times for the role.
8274: If no username and domain are specified, will default to current
8275: user/domain. Types, roles, and roledoms are references to arrays,
8276: of role statuses (active, future or previous), roles
8277: (e.g., cc,in, st etc.) and domains of the roles which can be used
8278: to restrict the list of roles reported. If no array ref is
8279: provided for types, will default to return only active roles.
1.834 albertel 8280:
1.243 albertel 8281: =back
8282:
8283: =head2 User Modification
8284:
8285: =over 4
8286:
8287: =item *
8288:
8289: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
8290: user for the level given by URL. Optional start and end dates (leave empty
8291: string or zero for "no date")
1.191 harris41 8292:
8293: =item *
8294:
1.243 albertel 8295: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
8296: change a users, password, possible return values are: ok,
8297: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
8298: refused
1.191 harris41 8299:
8300: =item *
8301:
1.243 albertel 8302: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 8303:
8304: =item *
8305:
1.243 albertel 8306: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
8307: modify user
1.191 harris41 8308:
8309: =item *
8310:
1.286 matthew 8311: modifystudent
8312:
8313: modify a students enrollment and identification information.
8314: The course id is resolved based on the current users environment.
8315: This means the envoking user must be a course coordinator or otherwise
8316: associated with a course.
8317:
1.297 matthew 8318: This call is essentially a wrapper for lonnet::modifyuser and
8319: lonnet::modify_student_enrollment
1.286 matthew 8320:
8321: Inputs:
8322:
8323: =over 4
8324:
8325: =item B<$udom> Students loncapa domain
8326:
8327: =item B<$uname> Students loncapa login name
8328:
8329: =item B<$uid> Students id/student number
8330:
8331: =item B<$umode> Students authentication mode
8332:
8333: =item B<$upass> Students password
8334:
8335: =item B<$first> Students first name
8336:
8337: =item B<$middle> Students middle name
8338:
8339: =item B<$last> Students last name
8340:
8341: =item B<$gene> Students generation
8342:
8343: =item B<$usec> Students section in course
8344:
8345: =item B<$end> Unix time of the roles expiration
8346:
8347: =item B<$start> Unix time of the roles start date
8348:
8349: =item B<$forceid> If defined, allow $uid to be changed
8350:
8351: =item B<$desiredhome> server to use as home server for student
8352:
8353: =back
1.297 matthew 8354:
8355: =item *
8356:
8357: modify_student_enrollment
8358:
8359: Change a students enrollment status in a class. The environment variable
8360: 'role.request.course' must be defined for this function to proceed.
8361:
8362: Inputs:
8363:
8364: =over 4
8365:
8366: =item $udom, students domain
8367:
8368: =item $uname, students name
8369:
8370: =item $uid, students user id
8371:
8372: =item $first, students first name
8373:
8374: =item $middle
8375:
8376: =item $last
8377:
8378: =item $gene
8379:
8380: =item $usec
8381:
8382: =item $end
8383:
8384: =item $start
8385:
8386: =back
8387:
1.191 harris41 8388:
8389: =item *
8390:
1.243 albertel 8391: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
8392: custom role; give a custom role to a user for the level given by URL. Specify
8393: name and domain of role author, and role name
1.191 harris41 8394:
8395: =item *
8396:
1.243 albertel 8397: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 8398:
8399: =item *
8400:
1.243 albertel 8401: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
8402:
8403: =back
8404:
8405: =head2 Course Infomation
8406:
8407: =over 4
1.191 harris41 8408:
8409: =item *
8410:
1.631 albertel 8411: coursedescription($courseid) : returns a hash of information about the
8412: specified course id, including all environment settings for the
8413: course, the description of the course will be in the hash under the
8414: key 'description'
1.191 harris41 8415:
8416: =item *
8417:
1.624 albertel 8418: resdata($name,$domain,$type,@which) : request for current parameter
8419: setting for a specific $type, where $type is either 'course' or 'user',
8420: @what should be a list of parameters to ask about. This routine caches
8421: answers for 5 minutes.
1.243 albertel 8422:
8423: =back
8424:
8425: =head2 Course Modification
8426:
8427: =over 4
1.191 harris41 8428:
8429: =item *
8430:
1.243 albertel 8431: writecoursepref($courseid,%prefs) : write preferences (environment
8432: database) for a course
1.191 harris41 8433:
8434: =item *
8435:
1.243 albertel 8436: createcourse($udom,$description,$url) : make/modify course
8437:
8438: =back
8439:
8440: =head2 Resource Subroutines
8441:
8442: =over 4
1.191 harris41 8443:
8444: =item *
8445:
1.243 albertel 8446: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 8447:
8448: =item *
8449:
1.243 albertel 8450: repcopy($filename) : subscribes to the requested file, and attempts to
8451: replicate from the owning library server, Might return
1.607 raeburn 8452: 'unavailable', 'not_found', 'forbidden', 'ok', or
8453: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 8454: resource. Expects the local filesystem pathname
8455: (/home/httpd/html/res/....)
8456:
8457: =back
8458:
8459: =head2 Resource Information
8460:
8461: =over 4
1.191 harris41 8462:
8463: =item *
8464:
1.243 albertel 8465: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
8466: a vairety of different possible values, $varname should be a request
8467: string, and the other parameters can be used to specify who and what
8468: one is asking about.
8469:
8470: Possible values for $varname are environment.lastname (or other item
8471: from the envirnment hash), user.name (or someother aspect about the
8472: user), resource.0.maxtries (or some other part and parameter of a
8473: resource)
1.204 albertel 8474:
8475: =item *
8476:
1.243 albertel 8477: directcondval($number) : get current value of a condition; reads from a state
8478: string
1.204 albertel 8479:
8480: =item *
8481:
1.243 albertel 8482: condval($condidx) : value of condition index based on state
1.204 albertel 8483:
8484: =item *
8485:
1.243 albertel 8486: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
8487: resource's metadata, $what should be either a specific key, or either
8488: 'keys' (to get a list of possible keys) or 'packages' to get a list of
8489: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
8490:
8491: this function automatically caches all requests
1.191 harris41 8492:
8493: =item *
8494:
1.243 albertel 8495: metadata_query($query,$custom,$customshow) : make a metadata query against the
8496: network of library servers; returns file handle of where SQL and regex results
8497: will be stored for query
1.191 harris41 8498:
8499: =item *
8500:
1.243 albertel 8501: symbread($filename) : return symbolic list entry (filename argument optional);
8502: returns the data handle
1.191 harris41 8503:
8504: =item *
8505:
1.243 albertel 8506: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 8507: a possible symb for the URL in $thisfn, and if is an encryypted
8508: resource that the user accessed using /enc/ returns a 1 on success, 0
8509: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 8510: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 8511:
1.191 harris41 8512:
8513: =item *
8514:
1.243 albertel 8515: symbclean($symb) : removes versions numbers from a symb, returns the
8516: cleaned symb
1.191 harris41 8517:
8518: =item *
8519:
1.243 albertel 8520: is_on_map($uri) : checks if the $uri is somewhere on the current
8521: course map, user must be in a course for it to work.
1.191 harris41 8522:
8523: =item *
8524:
1.243 albertel 8525: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 8526:
8527: =item *
8528:
1.243 albertel 8529: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
8530: a random seed, all arguments are optional, if they aren't sent it uses the
8531: environment to derive them. Note: if symb isn't sent and it can't get one
8532: from &symbread it will use the current time as its return value
1.191 harris41 8533:
8534: =item *
8535:
1.243 albertel 8536: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
8537: unfakeable, receipt
1.191 harris41 8538:
8539: =item *
8540:
1.620 albertel 8541: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 8542:
8543: =item *
8544:
1.243 albertel 8545: countacc($url) : count the number of accesses to a given URL
1.191 harris41 8546:
8547: =item *
8548:
1.243 albertel 8549: 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 8550:
8551: =item *
8552:
1.243 albertel 8553: 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 8554:
8555: =item *
8556:
1.243 albertel 8557: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 8558:
8559: =item *
8560:
1.243 albertel 8561: devalidate($symb) : devalidate temporary spreadsheet calculations,
8562: forcing spreadsheet to reevaluate the resource scores next time.
8563:
8564: =back
8565:
8566: =head2 Storing/Retreiving Data
8567:
8568: =over 4
1.191 harris41 8569:
8570: =item *
8571:
1.243 albertel 8572: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
8573: for this url; hashref needs to be given and should be a \%hashname; the
8574: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 8575: be derived from the env
1.191 harris41 8576:
8577: =item *
8578:
1.243 albertel 8579: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
8580: uses critical subroutine
1.191 harris41 8581:
8582: =item *
8583:
1.243 albertel 8584: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
8585: all args are optional
1.191 harris41 8586:
8587: =item *
8588:
1.717 albertel 8589: dumpstore($namespace,$udom,$uname,$regexp,$range) :
8590: dumps the complete (or key matching regexp) namespace into a hash
8591: ($udom, $uname, $regexp, $range are optional) for a namespace that is
8592: normally &store()ed into
8593:
8594: $range should be either an integer '100' (give me the first 100
8595: matching records)
8596: or be two integers sperated by a - with no spaces
8597: '30-50' (give me the 30th through the 50th matching
8598: records)
8599:
8600:
8601: =item *
8602:
8603: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
8604: replaces a &store() version of data with a replacement set of data
8605: for a particular resource in a namespace passed in the $storehash hash
8606: reference
8607:
8608: =item *
8609:
1.243 albertel 8610: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
8611: works very similar to store/cstore, but all data is stored in a
8612: temporary location and can be reset using tmpreset, $storehash should
8613: be a hash reference, returns nothing on success
1.191 harris41 8614:
8615: =item *
8616:
1.243 albertel 8617: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
8618: similar to restore, but all data is stored in a temporary location and
8619: can be reset using tmpreset. Returns a hash of values on success,
8620: error string otherwise.
1.191 harris41 8621:
8622: =item *
8623:
1.243 albertel 8624: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
8625: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 8626:
8627: =item *
8628:
1.243 albertel 8629: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
8630: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 8631:
8632: =item *
8633:
1.243 albertel 8634: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
8635: namesp ($udom and $uname are optional)
1.191 harris41 8636:
8637: =item *
8638:
1.702 albertel 8639: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 8640: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 8641: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 8642:
1.702 albertel 8643: $range should be either an integer '100' (give me the first 100
8644: matching records)
8645: or be two integers sperated by a - with no spaces
8646: '30-50' (give me the 30th through the 50th matching
8647: records)
1.449 matthew 8648: =item *
8649:
8650: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
8651: $store can be a scalar, an array reference, or if the amount to be
8652: incremented is > 1, a hash reference.
8653:
8654: ($udom and $uname are optional)
1.191 harris41 8655:
8656: =item *
8657:
1.243 albertel 8658: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
8659: ($udom and $uname are optional)
1.191 harris41 8660:
8661: =item *
8662:
1.243 albertel 8663: cput($namespace,$storehash,$udom,$uname) : critical put
8664: ($udom and $uname are optional)
1.191 harris41 8665:
8666: =item *
8667:
1.748 albertel 8668: newput($namespace,$storehash,$udom,$uname) :
8669:
8670: Attempts to store the items in the $storehash, but only if they don't
8671: currently exist, if this succeeds you can be certain that you have
8672: successfully created a new key value pair in the $namespace db.
8673:
8674:
8675: Args:
8676: $namespace: name of database to store values to
8677: $storehash: hashref to store to the db
8678: $udom: (optional) domain of user containing the db
8679: $uname: (optional) name of user caontaining the db
8680:
8681: Returns:
8682: 'ok' -> succeeded in storing all keys of $storehash
8683: 'key_exists: <key>' -> failed to anything out of $storehash, as at
8684: least <key> already existed in the db (other
8685: requested keys may also already exist)
8686: 'error: <msg>' -> unable to tie the DB or other erorr occured
8687: 'con_lost' -> unable to contact request server
8688: 'refused' -> action was not allowed by remote machine
8689:
8690:
8691: =item *
8692:
1.243 albertel 8693: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
8694: reference filled in from namesp (encrypts the return communication)
8695: ($udom and $uname are optional)
1.191 harris41 8696:
8697: =item *
8698:
1.243 albertel 8699: log($udom,$name,$home,$message) : write to permanent log for user; use
8700: critical subroutine
8701:
1.806 raeburn 8702: =item *
8703:
1.860 raeburn 8704: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
8705: array reference filled in from namespace found in domain level on either
8706: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 8707:
8708: =item *
8709:
1.860 raeburn 8710: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
8711: domain level either on specified domain server ($uhome) or primary domain
8712: server ($udom and $uhome are optional)
1.806 raeburn 8713:
1.243 albertel 8714: =back
8715:
8716: =head2 Network Status Functions
8717:
8718: =over 4
1.191 harris41 8719:
8720: =item *
8721:
8722: dirlist($uri) : return directory list based on URI
8723:
8724: =item *
8725:
1.243 albertel 8726: spareserver() : find server with least workload from spare.tab
8727:
8728: =back
8729:
8730: =head2 Apache Request
8731:
8732: =over 4
1.191 harris41 8733:
8734: =item *
8735:
1.243 albertel 8736: ssi($url,%hash) : server side include, does a complete request cycle on url to
8737: localhost, posts hash
8738:
8739: =back
8740:
8741: =head2 Data to String to Data
8742:
8743: =over 4
1.191 harris41 8744:
8745: =item *
8746:
1.243 albertel 8747: hash2str(%hash) : convert a hash into a string complete with escaping and '='
8748: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 8749:
8750: =item *
8751:
1.243 albertel 8752: hashref2str($hashref) : convert a hashref into a string complete with
8753: escaping and '=' and '&' separators, supports elements that are
8754: arrayrefs and hashrefs
1.191 harris41 8755:
8756: =item *
8757:
1.243 albertel 8758: arrayref2str($arrayref) : convert an arrayref into a string complete
8759: with escaping and '&' separators, supports elements that are arrayrefs
8760: and hashrefs
1.191 harris41 8761:
8762: =item *
8763:
1.243 albertel 8764: str2hash($string) : convert string to hash using unescaping and
8765: splitting on '=' and '&', supports elements that are arrayrefs and
8766: hashrefs
1.191 harris41 8767:
8768: =item *
8769:
1.243 albertel 8770: str2array($string) : convert string to hash using unescaping and
8771: splitting on '&', supports elements that are arrayrefs and hashrefs
8772:
8773: =back
8774:
8775: =head2 Logging Routines
8776:
8777: =over 4
8778:
8779: These routines allow one to make log messages in the lonnet.log and
8780: lonnet.perm logfiles.
1.191 harris41 8781:
8782: =item *
8783:
1.243 albertel 8784: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 8785:
8786: =item *
8787:
1.243 albertel 8788: logthis() : append message to the normal lonnet.log file, it gets
8789: preiodically rolled over and deleted.
1.191 harris41 8790:
8791: =item *
8792:
1.243 albertel 8793: logperm() : append a permanent message to lonnet.perm.log, this log
8794: file never gets deleted by any automated portion of the system, only
8795: messages of critical importance should go in here.
8796:
8797: =back
8798:
8799: =head2 General File Helper Routines
8800:
8801: =over 4
1.191 harris41 8802:
8803: =item *
8804:
1.481 raeburn 8805: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
8806: (a) files in /uploaded
8807: (i) If a local copy of the file exists -
8808: compares modification date of local copy with last-modified date for
8809: definitive version stored on home server for course. If local copy is
8810: stale, requests a new version from the home server and stores it.
8811: If the original has been removed from the home server, then local copy
8812: is unlinked.
8813: (ii) If local copy does not exist -
8814: requests the file from the home server and stores it.
8815:
8816: If $caller is 'uploadrep':
8817: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
8818: for request for files originally uploaded via DOCS.
8819: - returns 'ok' if fresh local copy now available, -1 otherwise.
8820:
8821: Otherwise:
8822: This indicates a call from the content generation phase of the request.
8823: - returns the entire contents of the file or -1.
8824:
8825: (b) files in /res
8826: - returns the entire contents of a file or -1;
8827: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 8828:
1.712 albertel 8829:
8830: =item *
8831:
8832: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
8833: reference
8834:
8835: returns either a stat() list of data about the file or an empty list
8836: if the file doesn't exist or couldn't find out about it (connection
8837: problems or user unknown)
8838:
1.191 harris41 8839: =item *
8840:
1.243 albertel 8841: filelocation($dir,$file) : returns file system location of a file
8842: based on URI; meant to be "fairly clean" absolute reference, $dir is a
8843: directory that relative $file lookups are to looked in ($dir of /a/dir
8844: and a file of ../bob will become /a/bob)
1.191 harris41 8845:
8846: =item *
8847:
8848: hreflocation($dir,$file) : returns file system location or a URL; same as
8849: filelocation except for hrefs
8850:
8851: =item *
8852:
8853: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
8854:
1.243 albertel 8855: =back
8856:
1.608 albertel 8857: =head2 Usererfile file routines (/uploaded*)
8858:
8859: =over 4
8860:
8861: =item *
8862:
8863: userfileupload(): main rotine for putting a file in a user or course's
8864: filespace, arguments are,
8865:
1.620 albertel 8866: formname - required - this is the name of the element in $env where the
1.608 albertel 8867: filename, and the contents of the file to create/modifed exist
1.620 albertel 8868: the filename is in $env{'form.'.$formname.'.filename'} and the
8869: contents of the file is located in $env{'form.'.$formname}
1.608 albertel 8870: coursedoc - if true, store the file in the course of the active role
8871: of the current user
8872: subdir - required - subdirectory to put the file in under ../userfiles/
8873: if undefined, it will be placed in "unknown"
8874:
8875: (This routine calls clean_filename() to remove any dangerous
8876: characters from the filename, and then calls finuserfileupload() to
8877: complete the transaction)
8878:
8879: returns either the url of the uploaded file (/uploaded/....) if successful
8880: and /adm/notfound.html if unsuccessful
8881:
8882: =item *
8883:
8884: clean_filename(): routine for cleaing a filename up for storage in
8885: userfile space, argument is:
8886:
8887: filename - proposed filename
8888:
8889: returns: the new clean filename
8890:
8891: =item *
8892:
8893: finishuserfileupload(): routine that creaes and sends the file to
8894: userspace, probably shouldn't be called directly
8895:
8896: docuname: username or courseid of destination for the file
8897: docudom: domain of user/course of destination for the file
8898: formname: same as for userfileupload()
8899: fname: filename (inculding subdirectories) for the file
8900:
8901: returns either the url of the uploaded file (/uploaded/....) if successful
8902: and /adm/notfound.html if unsuccessful
8903:
8904: =item *
8905:
8906: renameuserfile(): renames an existing userfile to a new name
8907:
8908: Args:
8909: docuname: username or courseid of destination for the file
8910: docudom: domain of user/course of destination for the file
8911: old: current file name (including any subdirs under userfiles)
8912: new: desired file name (including any subdirs under userfiles)
8913:
8914: =item *
8915:
8916: mkdiruserfile(): creates a directory is a userfiles dir
8917:
8918: Args:
8919: docuname: username or courseid of destination for the file
8920: docudom: domain of user/course of destination for the file
8921: dir: dir to create (including any subdirs under userfiles)
8922:
8923: =item *
8924:
8925: removeuserfile(): removes a file that exists in userfiles
8926:
8927: Args:
8928: docuname: username or courseid of destination for the file
8929: docudom: domain of user/course of destination for the file
8930: fname: filname to delete (including any subdirs under userfiles)
8931:
8932: =item *
8933:
8934: removeuploadedurl(): convience function for removeuserfile()
8935:
8936: Args:
8937: url: a full /uploaded/... url to delete
8938:
1.747 albertel 8939: =item *
8940:
8941: get_portfile_permissions():
8942: Args:
8943: domain: domain of user or course contain the portfolio files
8944: user: name of user or num of course contain the portfolio files
8945: Returns:
8946: hashref of a dump of the proper file_permissions.db
8947:
8948:
8949: =item *
8950:
8951: get_access_controls():
8952:
8953: Args:
8954: current_permissions: the hash ref returned from get_portfile_permissions()
8955: group: (optional) the group you want the files associated with
8956: file: (optional) the file you want access info on
8957:
8958: Returns:
1.749 raeburn 8959: a hash (keys are file names) of hashes containing
8960: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
8961: values are XML containing access control settings (see below)
1.747 albertel 8962:
8963: Internal notes:
8964:
1.749 raeburn 8965: access controls are stored in file_permissions.db as key=value pairs.
8966: key -> path to file/file_name\0uniqueID:scope_end_start
8967: where scope -> public,guest,course,group,domains or users.
8968: end -> UNIX time for end of access (0 -> no end date)
8969: start -> UNIX time for start of access
8970:
8971: value -> XML description of access control
8972: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
8973: <start></start>
8974: <end></end>
8975:
8976: <password></password> for scope type = guest
8977:
8978: <domain></domain> for scope type = course or group
8979: <number></number>
8980: <roles id="">
8981: <role></role>
8982: <access></access>
8983: <section></section>
8984: <group></group>
8985: </roles>
8986:
8987: <dom></dom> for scope type = domains
8988:
8989: <users> for scope type = users
8990: <user>
8991: <uname></uname>
8992: <udom></udom>
8993: </user>
8994: </users>
8995: </scope>
8996:
8997: Access data is also aggregated for each file in an additional key=value pair:
8998: key -> path to file/file_name\0accesscontrol
8999: value -> reference to hash
9000: hash contains key = value pairs
9001: where key = uniqueID:scope_end_start
9002: value = UNIX time record was last updated
9003:
9004: Used to improve speed of look-ups of access controls for each file.
9005:
9006: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
9007:
9008: modify_access_controls():
9009:
9010: Modifies access controls for a portfolio file
9011: Args
9012: 1. file name
9013: 2. reference to hash of required changes,
9014: 3. domain
9015: 4. username
9016: where domain,username are the domain of the portfolio owner
9017: (either a user or a course)
9018:
9019: Returns:
9020: 1. result of additions or updates ('ok' or 'error', with error message).
9021: 2. result of deletions ('ok' or 'error', with error message).
9022: 3. reference to hash of any new or updated access controls.
9023: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
9024: key = integer (inbound ID)
9025: value = uniqueID
1.747 albertel 9026:
1.608 albertel 9027: =back
9028:
1.243 albertel 9029: =head2 HTTP Helper Routines
9030:
9031: =over 4
9032:
1.191 harris41 9033: =item *
9034:
9035: escape() : unpack non-word characters into CGI-compatible hex codes
9036:
9037: =item *
9038:
9039: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
9040:
1.243 albertel 9041: =back
9042:
9043: =head1 PRIVATE SUBROUTINES
9044:
9045: =head2 Underlying communication routines (Shouldn't call)
9046:
9047: =over 4
9048:
9049: =item *
9050:
9051: subreply() : tries to pass a message to lonc, returns con_lost if incapable
9052:
9053: =item *
9054:
9055: reply() : uses subreply to send a message to remote machine, logs all failures
9056:
9057: =item *
9058:
9059: critical() : passes a critical message to another server; if cannot
9060: get through then place message in connection buffer directory and
9061: returns con_delayed, if incapable of saving message, returns
9062: con_failed
9063:
9064: =item *
9065:
9066: reconlonc() : tries to reconnect lonc client processes.
9067:
9068: =back
9069:
9070: =head2 Resource Access Logging
9071:
9072: =over 4
9073:
9074: =item *
9075:
9076: flushcourselogs() : flush (save) buffer logs and access logs
9077:
9078: =item *
9079:
9080: courselog($what) : save message for course in hash
9081:
9082: =item *
9083:
9084: courseacclog($what) : save message for course using &courselog(). Perform
9085: special processing for specific resource types (problems, exams, quizzes, etc).
9086:
1.191 harris41 9087: =item *
9088:
9089: goodbye() : flush course logs and log shutting down; it is called in srm.conf
9090: as a PerlChildExitHandler
1.243 albertel 9091:
9092: =back
9093:
9094: =head2 Other
9095:
9096: =over 4
9097:
9098: =item *
9099:
9100: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 9101:
9102: =back
9103:
9104: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>