Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.951
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.951 ! raeburn 4: # $Id: lonnet.pm,v 1.950 2008/03/22 15:47:41 raeburn 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.486 www 34: use HTTP::Date;
35: # use Date::Parse;
1.871 albertel 36: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
37: $_64bit %env);
38:
39: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
40: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
41: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
42: %courseownerbuf, %coursetypebuf);
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.88 www 47: use Fcntl qw(:flock);
1.870 albertel 48: use Storable qw(thaw nfreeze);
1.539 albertel 49: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 50: use Cache::Memcached;
1.676 albertel 51: use Digest::MD5;
1.790 albertel 52: use Math::Random;
1.807 albertel 53: use LONCAPA qw(:DEFAULT :match);
1.740 www 54: use LONCAPA::Configuration;
1.676 albertel 55:
1.195 www 56: my $readit;
1.550 foxr 57: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 58:
1.619 albertel 59: require Exporter;
60:
61: our @ISA = qw (Exporter);
62: our @EXPORT = qw(%env);
63:
1.449 matthew 64: =pod
65:
66: =head1 Package Variables
67:
68: These are largely undocumented, so if you decipher one please note it here.
69:
70: =over 4
71:
72: =item $processmarker
73:
74: Contains the time this process was started and this servers host id.
75:
76: =item $dumpcount
77:
78: Counts the number of times a message log flush has been attempted (regardless
79: of success) by this process. Used as part of the filename when messages are
80: delayed.
81:
82: =back
83:
84: =cut
85:
86:
1.1 albertel 87: # --------------------------------------------------------------------- Logging
1.729 www 88: {
89: my $logid;
90: sub instructor_log {
91: my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
92: $logid++;
93: my $id=time().'00000'.$$.'00000'.$logid;
94: return &Apache::lonnet::put('nohist_'.$hash_name,
1.730 www 95: { $id => {
96: 'exe_uname' => $env{'user.name'},
97: 'exe_udom' => $env{'user.domain'},
98: 'exe_time' => time(),
99: 'exe_ip' => $ENV{'REMOTE_ADDR'},
100: 'delflag' => $delflag,
101: 'logentry' => $storehash,
102: 'uname' => $uname,
103: 'udom' => $udom,
104: }
105: },
1.729 www 106: $env{'course.'.$env{'request.course.id'}.'.domain'},
107: $env{'course.'.$env{'request.course.id'}.'.num'}
108: );
109: }
110: }
1.1 albertel 111:
1.163 harris41 112: sub logtouch {
113: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 114: unless (-e "$execdir/logs/lonnet.log") {
115: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 116: close $fh;
117: }
118: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
119: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
120: }
121:
1.1 albertel 122: sub logthis {
123: my $message=shift;
124: my $execdir=$perlvar{'lonDaemons'};
125: my $now=time;
126: my $local=localtime($now);
1.448 albertel 127: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
128: print $fh "$local ($$): $message\n";
129: close($fh);
130: }
1.1 albertel 131: return 1;
132: }
133:
134: sub logperm {
135: my $message=shift;
136: my $execdir=$perlvar{'lonDaemons'};
137: my $now=time;
138: my $local=localtime($now);
1.448 albertel 139: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
140: print $fh "$now:$message:$local\n";
141: close($fh);
142: }
1.1 albertel 143: return 1;
144: }
145:
1.850 albertel 146: sub create_connection {
1.853 albertel 147: my ($hostname,$lonid) = @_;
1.851 albertel 148: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 149: Type => SOCK_STREAM,
150: Timeout => 10);
151: return 0 if (!$client);
1.890 albertel 152: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 153: my $result = <$client>;
154: chomp($result);
155: return 1 if ($result eq 'done');
156: return 0;
157: }
158:
159:
1.1 albertel 160: # -------------------------------------------------- Non-critical communication
161: sub subreply {
162: my ($cmd,$server)=@_;
1.838 albertel 163: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 164: #
165: # With loncnew process trimming, there's a timing hole between lonc server
166: # process exit and the master server picking up the listen on the AF_UNIX
167: # socket. In that time interval, a lock file will exist:
168:
169: my $lockfile=$peerfile.".lock";
170: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
171: sleep(1);
172: }
173: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 174: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 175: #
1.550 foxr 176: # We'll give the connection a few tries before abandoning it. If
177: # connection is not possible, we'll con_lost back to the client.
178: #
179: my $client;
180: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
181: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
182: Type => SOCK_STREAM,
183: Timeout => 10);
1.869 albertel 184: if ($client) {
1.550 foxr 185: last; # Connected!
1.850 albertel 186: } else {
1.853 albertel 187: &create_connection(&hostname($server),$server);
1.550 foxr 188: }
1.850 albertel 189: sleep(1); # Try again later if failed connection.
1.550 foxr 190: }
191: my $answer;
192: if ($client) {
1.704 albertel 193: print $client "sethost:$server:$cmd\n";
1.550 foxr 194: $answer=<$client>;
195: if (!$answer) { $answer="con_lost"; }
196: chomp($answer);
197: } else {
198: $answer = 'con_lost'; # Failed connection.
199: }
1.1 albertel 200: return $answer;
201: }
202:
203: sub reply {
204: my ($cmd,$server)=@_;
1.838 albertel 205: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 206: my $answer=subreply($cmd,$server);
1.65 www 207: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 208: &logthis("<font color=\"blue\">WARNING:".
1.12 www 209: " $cmd to $server returned $answer</font>");
210: }
1.1 albertel 211: return $answer;
212: }
213:
214: # ----------------------------------------------------------- Send USR1 to lonc
215:
216: sub reconlonc {
1.891 albertel 217: my ($lonid) = @_;
218: my $hostname = &hostname($lonid);
219: if ($lonid) {
220: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
221: if ($hostname && -e $peerfile) {
222: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
223: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
224: Type => SOCK_STREAM,
225: Timeout => 10);
226: if ($client) {
227: print $client ("reset_retries\n");
228: my $answer=<$client>;
229: #reset just this one.
230: }
231: }
232: return;
233: }
234:
1.836 www 235: &logthis("Trying to reconnect lonc");
1.1 albertel 236: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 237: if (open(my $fh,"<$loncfile")) {
1.1 albertel 238: my $loncpid=<$fh>;
239: chomp($loncpid);
240: if (kill 0 => $loncpid) {
241: &logthis("lonc at pid $loncpid responding, sending USR1");
242: kill USR1 => $loncpid;
243: sleep 1;
1.836 www 244: } else {
1.12 www 245: &logthis(
1.672 albertel 246: "<font color=\"blue\">WARNING:".
1.12 www 247: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 248: }
249: } else {
1.836 www 250: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 251: }
252: }
253:
254: # ------------------------------------------------------ Critical communication
1.12 www 255:
1.1 albertel 256: sub critical {
257: my ($cmd,$server)=@_;
1.838 albertel 258: unless (&hostname($server)) {
1.672 albertel 259: &logthis("<font color=\"blue\">WARNING:".
1.89 www 260: " Critical message to unknown server ($server)</font>");
261: return 'no_such_host';
262: }
1.1 albertel 263: my $answer=reply($cmd,$server);
264: if ($answer eq 'con_lost') {
265: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 266: my $answer=reply($cmd,$server);
1.1 albertel 267: if ($answer eq 'con_lost') {
268: my $now=time;
269: my $middlename=$cmd;
1.5 www 270: $middlename=substr($middlename,0,16);
1.1 albertel 271: $middlename=~s/\W//g;
272: my $dfilename=
1.305 www 273: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
274: $dumpcount++;
1.1 albertel 275: {
1.448 albertel 276: my $dfh;
277: if (open($dfh,">$dfilename")) {
278: print $dfh "$cmd\n";
279: close($dfh);
280: }
1.1 albertel 281: }
282: sleep 2;
283: my $wcmd='';
284: {
1.448 albertel 285: my $dfh;
286: if (open($dfh,"<$dfilename")) {
287: $wcmd=<$dfh>;
288: close($dfh);
289: }
1.1 albertel 290: }
291: chomp($wcmd);
1.7 www 292: if ($wcmd eq $cmd) {
1.672 albertel 293: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 294: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 295: &logperm("D:$server:$cmd");
296: return 'con_delayed';
297: } else {
1.672 albertel 298: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 299: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 300: &logperm("F:$server:$cmd");
301: return 'con_failed';
302: }
303: }
304: }
305: return $answer;
1.405 albertel 306: }
307:
1.755 albertel 308: # ------------------------------------------- check if return value is an error
309:
310: sub error {
311: my ($result) = @_;
1.756 albertel 312: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 313: if ($2 == 2) { return undef; }
314: return $1;
315: }
316: return undef;
317: }
318:
1.783 albertel 319: sub convert_and_load_session_env {
320: my ($lonidsdir,$handle)=@_;
321: my @profile;
322: {
1.917 albertel 323: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
324: if (!$opened) {
1.915 albertel 325: return 0;
326: }
1.783 albertel 327: flock($idf,LOCK_SH);
328: @profile=<$idf>;
329: close($idf);
330: }
331: my %temp_env;
332: foreach my $line (@profile) {
1.786 albertel 333: if ($line !~ m/=/) {
334: return 0;
335: }
1.783 albertel 336: chomp($line);
337: my ($envname,$envvalue)=split(/=/,$line,2);
338: $temp_env{&unescape($envname)} = &unescape($envvalue);
339: }
340: unlink("$lonidsdir/$handle.id");
341: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
342: 0640)) {
343: %disk_env = %temp_env;
344: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
345: untie(%disk_env);
346: }
1.786 albertel 347: return 1;
1.783 albertel 348: }
349:
1.374 www 350: # ------------------------------------------- Transfer profile into environment
1.780 albertel 351: my $env_loaded;
352: sub transfer_profile_to_env {
1.788 albertel 353: my ($lonidsdir,$handle,$force_transfer) = @_;
354: if (!$force_transfer && $env_loaded) { return; }
1.374 www 355:
1.720 albertel 356: if (!defined($lonidsdir)) {
357: $lonidsdir = $perlvar{'lonIDsDir'};
358: }
359: if (!defined($handle)) {
360: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
361: }
362:
1.786 albertel 363: my $convert;
364: {
1.917 albertel 365: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
366: if (!$opened) {
1.915 albertel 367: return;
368: }
1.786 albertel 369: flock($idf,LOCK_SH);
370: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
371: &GDBM_READER(),0640)) {
372: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
373: untie(%disk_env);
374: } else {
375: $convert = 1;
376: }
377: }
378: if ($convert) {
379: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
380: &logthis("Failed to load session, or convert session.");
381: }
1.374 www 382: }
1.783 albertel 383:
1.786 albertel 384: my %remove;
1.783 albertel 385: while ( my $envname = each(%env) ) {
1.433 matthew 386: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
387: if ($time < time-300) {
1.783 albertel 388: $remove{$key}++;
1.433 matthew 389: }
390: }
391: }
1.783 albertel 392:
1.619 albertel 393: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 394: $env_loaded=1;
1.783 albertel 395: foreach my $expired_key (keys(%remove)) {
1.433 matthew 396: &delenv($expired_key);
1.374 www 397: }
1.1 albertel 398: }
399:
1.916 albertel 400: # ---------------------------------------------------- Check for valid session
401: sub check_for_valid_session {
402: my ($r) = @_;
403: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
404: my $lonid=$cookies{'lonID'};
405: return undef if (!$lonid);
406:
407: my $handle=&LONCAPA::clean_handle($lonid->value);
408: my $lonidsdir=$r->dir_config('lonIDsDir');
409: return undef if (!-e "$lonidsdir/$handle.id");
410:
1.917 albertel 411: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
412: return undef if (!$opened);
1.916 albertel 413:
414: flock($idf,LOCK_SH);
415: my %disk_env;
416: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
417: &GDBM_READER(),0640)) {
418: return undef;
419: }
420:
421: if (!defined($disk_env{'user.name'})
422: || !defined($disk_env{'user.domain'})) {
423: return undef;
424: }
425: return $handle;
426: }
427:
1.830 albertel 428: sub timed_flock {
429: my ($file,$lock_type) = @_;
430: my $failed=0;
431: eval {
432: local $SIG{__DIE__}='DEFAULT';
433: local $SIG{ALRM}=sub {
434: $failed=1;
435: die("failed lock");
436: };
437: alarm(13);
438: flock($file,$lock_type);
439: alarm(0);
440: };
441: if ($failed) {
442: return undef;
443: } else {
444: return 1;
445: }
446: }
447:
1.5 www 448: # ---------------------------------------------------------- Append Environment
449:
450: sub appenv {
1.949 raeburn 451: my ($newenv,$roles) = @_;
452: if (ref($newenv) eq 'HASH') {
453: foreach my $key (keys(%{$newenv})) {
454: my $refused = 0;
455: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
456: $refused = 1;
457: if (ref($roles) eq 'ARRAY') {
458: my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
459: if (grep(/^\Q$role\E$/,@{$roles})) {
460: $refused = 0;
461: }
462: }
463: }
464: if ($refused) {
465: &logthis("<font color=\"blue\">WARNING: ".
466: "Attempt to modify environment ".$key." to ".$newenv->{$key}
467: .'</font>');
468: delete($newenv->{$key});
469: } else {
470: $env{$key}=$newenv->{$key};
471: }
472: }
473: my $opened = open(my $env_file,'+<',$env{'user.environment'});
474: if ($opened
475: && &timed_flock($env_file,LOCK_EX)
476: &&
477: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
478: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
479: while (my ($key,$value) = each(%{$newenv})) {
480: $disk_env{$key} = $value;
481: }
482: untie(%disk_env);
1.35 www 483: }
1.191 harris41 484: }
1.56 www 485: return 'ok';
486: }
487: # ----------------------------------------------------- Delete from Environment
488:
489: sub delenv {
490: my $delthis=shift;
491: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 492: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 493: "Attempt to delete from environment ".$delthis);
494: return 'error';
495: }
1.917 albertel 496: my $opened = open(my $env_file,'+<',$env{'user.environment'});
497: if ($opened
1.915 albertel 498: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 499: &&
500: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
501: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 502: foreach my $key (keys(%disk_env)) {
503: if ($key=~/^$delthis/) {
1.915 albertel 504: delete($env{$key});
505: delete($disk_env{$key});
506: }
1.448 albertel 507: }
1.783 albertel 508: untie(%disk_env);
1.5 www 509: }
510: return 'ok';
1.369 albertel 511: }
512:
1.790 albertel 513: sub get_env_multiple {
514: my ($name) = @_;
515: my @values;
516: if (defined($env{$name})) {
517: # exists is it an array
518: if (ref($env{$name})) {
519: @values=@{ $env{$name} };
520: } else {
521: $values[0]=$env{$name};
522: }
523: }
524: return(@values);
525: }
526:
1.369 albertel 527: # ------------------------------------------ Find out current server userload
528: sub userload {
529: my $numusers=0;
530: {
531: opendir(LONIDS,$perlvar{'lonIDsDir'});
532: my $filename;
533: my $curtime=time;
534: while ($filename=readdir(LONIDS)) {
1.925 albertel 535: next if ($filename eq '.' || $filename eq '..');
536: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 537: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 538: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 539: }
540: closedir(LONIDS);
541: }
542: my $userloadpercent=0;
543: my $maxuserload=$perlvar{'lonUserLoadLim'};
544: if ($maxuserload) {
1.371 albertel 545: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 546: }
1.372 albertel 547: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 548: return $userloadpercent;
1.283 www 549: }
550:
551: # ------------------------------------------ Fight off request when overloaded
552:
553: sub overloaderror {
554: my ($r,$checkserver)=@_;
555: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
556: my $loadavg;
557: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 558: open(my $loadfile,'/proc/loadavg');
1.283 www 559: $loadavg=<$loadfile>;
560: $loadavg =~ s/\s.*//g;
1.285 matthew 561: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 562: close($loadfile);
1.283 www 563: } else {
564: $loadavg=&reply('load',$checkserver);
565: }
1.285 matthew 566: my $overload=$loadavg-100;
1.283 www 567: if ($overload>0) {
1.285 matthew 568: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 569: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 570: return 413;
1.283 www 571: }
572: return '';
1.5 www 573: }
1.1 albertel 574:
575: # ------------------------------ Find server with least workload from spare.tab
1.11 www 576:
1.1 albertel 577: sub spareserver {
1.670 albertel 578: my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784 albertel 579: my $spare_server;
1.370 albertel 580: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 581: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
582: : $userloadpercent;
583:
584: foreach my $try_server (@{ $spareid{'primary'} }) {
585: ($spare_server, $lowest_load) =
586: &compare_server_load($try_server, $spare_server, $lowest_load);
587: }
588:
589: my $found_server = ($spare_server ne '' && $lowest_load < 100);
590:
591: if (!$found_server) {
592: foreach my $try_server (@{ $spareid{'default'} }) {
593: ($spare_server, $lowest_load) =
594: &compare_server_load($try_server, $spare_server, $lowest_load);
595: }
596: }
597:
598: if (!$want_server_name) {
1.838 albertel 599: $spare_server="http://".&hostname($spare_server);
1.784 albertel 600: }
601: return $spare_server;
602: }
603:
604: sub compare_server_load {
605: my ($try_server, $spare_server, $lowest_load) = @_;
606:
607: my $loadans = &reply('load', $try_server);
608: my $userloadans = &reply('userload',$try_server);
609:
610: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
611: next; #didn't get a number from the server
612: }
613:
614: my $load;
615: if ($loadans =~ /\d/) {
616: if ($userloadans =~ /\d/) {
617: #both are numbers, pick the bigger one
618: $load = ($loadans > $userloadans) ? $loadans
619: : $userloadans;
1.411 albertel 620: } else {
1.784 albertel 621: $load = $loadans;
1.411 albertel 622: }
1.784 albertel 623: } else {
624: $load = $userloadans;
625: }
626:
627: if (($load =~ /\d/) && ($load < $lowest_load)) {
628: $spare_server = $try_server;
629: $lowest_load = $load;
1.370 albertel 630: }
1.784 albertel 631: return ($spare_server,$lowest_load);
1.202 matthew 632: }
1.914 albertel 633:
634: # --------------------------- ask offload servers if user already has a session
635: sub find_existing_session {
636: my ($udom,$uname) = @_;
637: foreach my $try_server (@{ $spareid{'primary'} },
638: @{ $spareid{'default'} }) {
639: return $try_server if (&has_user_session($try_server, $udom, $uname));
640: }
641: return;
642: }
643:
644: # -------------------------------- ask if server already has a session for user
645: sub has_user_session {
646: my ($lonid,$udom,$uname) = @_;
647: my $result = &reply(join(':','userhassession',
648: map {&escape($_)} ($udom,$uname)),$lonid);
649: return 1 if ($result eq 'ok');
650:
651: return 0;
652: }
653:
1.202 matthew 654: # --------------------------------------------- Try to change a user's password
655:
656: sub changepass {
1.799 raeburn 657: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 658: $currentpass = &escape($currentpass);
659: $newpass = &escape($newpass);
1.799 raeburn 660: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202 matthew 661: $server);
662: if (! $answer) {
663: &logthis("No reply on password change request to $server ".
664: "by $uname in domain $udom.");
665: } elsif ($answer =~ "^ok") {
666: &logthis("$uname in $udom successfully changed their password ".
667: "on $server.");
668: } elsif ($answer =~ "^pwchange_failure") {
669: &logthis("$uname in $udom was unable to change their password ".
670: "on $server. The action was blocked by either lcpasswd ".
671: "or pwchange");
672: } elsif ($answer =~ "^non_authorized") {
673: &logthis("$uname in $udom did not get their password correct when ".
674: "attempting to change it on $server.");
675: } elsif ($answer =~ "^auth_mode_error") {
676: &logthis("$uname in $udom attempted to change their password despite ".
677: "not being locally or internally authenticated on $server.");
678: } elsif ($answer =~ "^unknown_user") {
679: &logthis("$uname in $udom attempted to change their password ".
680: "on $server but were unable to because $server is not ".
681: "their home server.");
682: } elsif ($answer =~ "^refused") {
683: &logthis("$server refused to change $uname in $udom password because ".
684: "it was sent an unencrypted request to change the password.");
685: }
686: return $answer;
1.1 albertel 687: }
688:
1.169 harris41 689: # ----------------------- Try to determine user's current authentication scheme
690:
691: sub queryauthenticate {
692: my ($uname,$udom)=@_;
1.456 albertel 693: my $uhome=&homeserver($uname,$udom);
694: if (!$uhome) {
695: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
696: return 'no_host';
697: }
698: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
699: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
700: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 701: }
1.456 albertel 702: return $answer;
1.169 harris41 703: }
704:
1.1 albertel 705: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 706:
1.1 albertel 707: sub authenticate {
708: my ($uname,$upass,$udom)=@_;
1.807 albertel 709: $upass=&escape($upass);
710: $uname= &LONCAPA::clean_username($uname);
1.836 www 711: my $uhome=&homeserver($uname,$udom,1);
712: if ((!$uhome) || ($uhome eq 'no_host')) {
713: # Maybe the machine was offline and only re-appeared again recently?
714: &reconlonc();
715: # One more
716: my $uhome=&homeserver($uname,$udom,1);
717: if ((!$uhome) || ($uhome eq 'no_host')) {
718: &logthis("User $uname at $udom is unknown in authenticate");
719: }
1.471 albertel 720: return 'no_host';
1.1 albertel 721: }
1.471 albertel 722: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
723: if ($answer eq 'authorized') {
724: &logthis("User $uname at $udom authorized by $uhome");
725: return $uhome;
726: }
727: if ($answer eq 'non_authorized') {
728: &logthis("User $uname at $udom rejected by $uhome");
729: return 'no_host';
1.9 www 730: }
1.471 albertel 731: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 732: return 'no_host';
733: }
734:
735: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 736:
1.599 albertel 737: my %homecache;
1.1 albertel 738: sub homeserver {
1.230 stredwic 739: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 740: my $index="$uname:$udom";
1.426 albertel 741:
1.599 albertel 742: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 743:
744: my %servers = &get_servers($udom,'library');
745: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 746: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 747: exists($badServerCache{$tryserver}));
1.841 albertel 748:
749: my $answer=reply("home:$udom:$uname",$tryserver);
750: if ($answer eq 'found') {
751: delete($badServerCache{$tryserver});
752: return $homecache{$index}=$tryserver;
753: } elsif ($answer eq 'no_host') {
754: $badServerCache{$tryserver}=1;
755: }
1.1 albertel 756: }
757: return 'no_host';
1.70 www 758: }
759:
760: # ------------------------------------- Find the usernames behind a list of IDs
761:
762: sub idget {
763: my ($udom,@ids)=@_;
764: my %returnhash=();
765:
1.841 albertel 766: my %servers = &get_servers($udom,'library');
767: foreach my $tryserver (keys(%servers)) {
768: my $idlist=join('&',@ids);
769: $idlist=~tr/A-Z/a-z/;
770: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
771: my @answer=();
772: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
773: @answer=split(/\&/,$reply);
774: } ;
775: my $i;
776: for ($i=0;$i<=$#ids;$i++) {
777: if ($answer[$i]) {
778: $returnhash{$ids[$i]}=$answer[$i];
779: }
780: }
781: }
1.70 www 782: return %returnhash;
783: }
784:
785: # ------------------------------------- Find the IDs behind a list of usernames
786:
787: sub idrget {
788: my ($udom,@unames)=@_;
789: my %returnhash=();
1.800 albertel 790: foreach my $uname (@unames) {
791: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 792: }
1.70 www 793: return %returnhash;
794: }
795:
796: # ------------------------------- Store away a list of names and associated IDs
797:
798: sub idput {
799: my ($udom,%ids)=@_;
800: my %servers=();
1.800 albertel 801: foreach my $uname (keys(%ids)) {
802: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
803: my $uhom=&homeserver($uname,$udom);
1.70 www 804: if ($uhom ne 'no_host') {
1.800 albertel 805: my $id=&escape($ids{$uname});
1.70 www 806: $id=~tr/A-Z/a-z/;
1.800 albertel 807: my $esc_unam=&escape($uname);
1.70 www 808: if ($servers{$uhom}) {
1.800 albertel 809: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 810: } else {
1.800 albertel 811: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 812: }
813: }
1.191 harris41 814: }
1.800 albertel 815: foreach my $server (keys(%servers)) {
816: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 817: }
1.344 www 818: }
819:
1.806 raeburn 820: # ------------------------------------------- get items from domain db files
821:
822: sub get_dom {
1.860 raeburn 823: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 824: my $items='';
825: foreach my $item (@$storearr) {
826: $items.=&escape($item).'&';
827: }
828: $items=~s/\&$//;
1.860 raeburn 829: if (!$udom) {
830: $udom=$env{'user.domain'};
831: if (defined(&domain($udom,'primary'))) {
832: $uhome=&domain($udom,'primary');
833: } else {
1.874 albertel 834: undef($uhome);
1.860 raeburn 835: }
836: } else {
837: if (!$uhome) {
838: if (defined(&domain($udom,'primary'))) {
839: $uhome=&domain($udom,'primary');
840: }
841: }
842: }
843: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 844: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 845: my %returnhash;
1.875 albertel 846: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 847: return %returnhash;
848: }
1.806 raeburn 849: my @pairs=split(/\&/,$rep);
850: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
851: return @pairs;
852: }
853: my $i=0;
854: foreach my $item (@$storearr) {
855: $returnhash{$item}=&thaw_unescape($pairs[$i]);
856: $i++;
857: }
858: return %returnhash;
859: } else {
1.880 banghart 860: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 861: }
862: }
863:
864: # -------------------------------------------- put items in domain db files
865:
866: sub put_dom {
1.860 raeburn 867: my ($namespace,$storehash,$udom,$uhome)=@_;
868: if (!$udom) {
869: $udom=$env{'user.domain'};
870: if (defined(&domain($udom,'primary'))) {
871: $uhome=&domain($udom,'primary');
872: } else {
1.874 albertel 873: undef($uhome);
1.860 raeburn 874: }
875: } else {
876: if (!$uhome) {
877: if (defined(&domain($udom,'primary'))) {
878: $uhome=&domain($udom,'primary');
879: }
880: }
881: }
882: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 883: my $items='';
884: foreach my $item (keys(%$storehash)) {
885: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
886: }
887: $items=~s/\&$//;
888: return &reply("putdom:$udom:$namespace:$items",$uhome);
889: } else {
1.860 raeburn 890: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 891: }
892: }
893:
1.837 raeburn 894: sub retrieve_inst_usertypes {
895: my ($udom) = @_;
896: my (%returnhash,@order);
1.846 albertel 897: if (defined(&domain($udom,'primary'))) {
898: my $uhome=&domain($udom,'primary');
1.837 raeburn 899: my $rep=&reply("inst_usertypes:$udom",$uhome);
900: my ($hashitems,$orderitems) = split(/:/,$rep);
901: my @pairs=split(/\&/,$hashitems);
902: foreach my $item (@pairs) {
903: my ($key,$value)=split(/=/,$item,2);
904: $key = &unescape($key);
905: next if ($key =~ /^error: 2 /);
906: $returnhash{$key}=&thaw_unescape($value);
907: }
908: my @esc_order = split(/\&/,$orderitems);
909: foreach my $item (@esc_order) {
910: push(@order,&unescape($item));
911: }
912: } else {
913: &logthis("get_dom failed - no primary domain server for $udom");
914: }
915: return (\%returnhash,\@order);
916: }
917:
1.868 raeburn 918: sub is_domainimage {
919: my ($url) = @_;
920: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
921: if (&domain($1) ne '') {
922: return '1';
923: }
924: }
925: return;
926: }
927:
1.899 raeburn 928: sub inst_directory_query {
929: my ($srch) = @_;
930: my $udom = $srch->{'srchdomain'};
931: my %results;
932: my $homeserver = &domain($udom,'primary');
1.909 raeburn 933: my $outcome;
1.899 raeburn 934: if ($homeserver ne '') {
1.904 albertel 935: my $queryid=&reply("querysend:instdirsearch:".
936: &escape($srch->{'srchby'}).':'.
937: &escape($srch->{'srchterm'}).':'.
938: &escape($srch->{'srchtype'}),$homeserver);
939: my $host=&hostname($homeserver);
940: if ($queryid !~/^\Q$host\E\_/) {
941: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
942: return;
943: }
944: my $response = &get_query_reply($queryid);
945: my $maxtries = 5;
946: my $tries = 1;
947: while (($response=~/^timeout/) && ($tries < $maxtries)) {
948: $response = &get_query_reply($queryid);
949: $tries ++;
950: }
951:
952: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 953: if ($response eq 'unavailable') {
954: $outcome = $response;
955: } else {
956: $outcome = 'ok';
957: my @matches = split(/\n/,$response);
958: foreach my $match (@matches) {
959: my ($key,$value) = split(/=/,$match);
960: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
961: }
1.899 raeburn 962: }
963: }
964: }
1.909 raeburn 965: return ($outcome,%results);
1.899 raeburn 966: }
967:
968: sub usersearch {
969: my ($srch) = @_;
970: my $dom = $srch->{'srchdomain'};
971: my %results;
972: my %libserv = &all_library();
973: my $query = 'usersearch';
974: foreach my $tryserver (keys(%libserv)) {
975: if (&host_domain($tryserver) eq $dom) {
976: my $host=&hostname($tryserver);
977: my $queryid=
1.911 raeburn 978: &reply("querysend:".&escape($query).':'.
979: &escape($srch->{'srchby'}).':'.
1.899 raeburn 980: &escape($srch->{'srchtype'}).':'.
981: &escape($srch->{'srchterm'}),$tryserver);
982: if ($queryid !~/^\Q$host\E\_/) {
983: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 984: next;
1.899 raeburn 985: }
986: my $reply = &get_query_reply($queryid);
987: my $maxtries = 1;
988: my $tries = 1;
989: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
990: $reply = &get_query_reply($queryid);
991: $tries ++;
992: }
993: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
994: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
995: } else {
1.911 raeburn 996: my @matches;
997: if ($reply =~ /\n/) {
998: @matches = split(/\n/,$reply);
999: } else {
1000: @matches = split(/\&/,$reply);
1001: }
1.899 raeburn 1002: foreach my $match (@matches) {
1003: my ($uname,$udom,%userhash);
1.911 raeburn 1004: foreach my $entry (split(/:/,$match)) {
1005: my ($key,$value) =
1006: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 1007: $userhash{$key} = $value;
1008: if ($key eq 'username') {
1009: $uname = $value;
1010: } elsif ($key eq 'domain') {
1011: $udom = $value;
1.911 raeburn 1012: }
1.899 raeburn 1013: }
1014: $results{$uname.':'.$udom} = \%userhash;
1015: }
1016: }
1017: }
1018: }
1019: return %results;
1020: }
1021:
1.912 raeburn 1022: sub get_instuser {
1023: my ($udom,$uname,$id) = @_;
1024: my $homeserver = &domain($udom,'primary');
1025: my ($outcome,%results);
1026: if ($homeserver ne '') {
1027: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
1028: &escape($id).':'.&escape($udom),$homeserver);
1029: my $host=&hostname($homeserver);
1030: if ($queryid !~/^\Q$host\E\_/) {
1031: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1032: return;
1033: }
1034: my $response = &get_query_reply($queryid);
1035: my $maxtries = 5;
1036: my $tries = 1;
1037: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1038: $response = &get_query_reply($queryid);
1039: $tries ++;
1040: }
1041: if (!&error($response) && $response ne 'refused') {
1042: if ($response eq 'unavailable') {
1043: $outcome = $response;
1044: } else {
1045: $outcome = 'ok';
1046: my @matches = split(/\n/,$response);
1047: foreach my $match (@matches) {
1048: my ($key,$value) = split(/=/,$match);
1049: $results{&unescape($key)} = &thaw_unescape($value);
1050: }
1051: }
1052: }
1053: }
1054: my %userinfo;
1055: if (ref($results{$uname}) eq 'HASH') {
1056: %userinfo = %{$results{$uname}};
1057: }
1058: return ($outcome,%userinfo);
1059: }
1060:
1061: sub inst_rulecheck {
1.923 raeburn 1062: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 1063: my %returnhash;
1064: if ($udom ne '') {
1065: if (ref($rules) eq 'ARRAY') {
1066: @{$rules} = map {&escape($_);} (@{$rules});
1067: my $rulestr = join(':',@{$rules});
1068: my $homeserver=&domain($udom,'primary');
1069: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1070: my $response;
1071: if ($item eq 'username') {
1072: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
1073: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 1074: $homeserver));
1.923 raeburn 1075: } elsif ($item eq 'id') {
1076: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
1077: ':'.&escape($id).':'.$rulestr,
1078: $homeserver));
1.945 raeburn 1079: } elsif ($item eq 'selfcreate') {
1080: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 1081: &escape($udom).':'.&escape($uname).
1082: ':'.$rulestr,$homeserver));
1.923 raeburn 1083: }
1.912 raeburn 1084: if ($response ne 'refused') {
1085: my @pairs=split(/\&/,$response);
1086: foreach my $item (@pairs) {
1087: my ($key,$value)=split(/=/,$item,2);
1088: $key = &unescape($key);
1089: next if ($key =~ /^error: 2 /);
1090: $returnhash{$key}=&thaw_unescape($value);
1091: }
1092: }
1093: }
1094: }
1095: }
1096: return %returnhash;
1097: }
1098:
1099: sub inst_userrules {
1.923 raeburn 1100: my ($udom,$check) = @_;
1.912 raeburn 1101: my (%ruleshash,@ruleorder);
1102: if ($udom ne '') {
1103: my $homeserver=&domain($udom,'primary');
1104: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1105: my $response;
1106: if ($check eq 'id') {
1107: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 1108: $homeserver);
1.943 raeburn 1109: } elsif ($check eq 'email') {
1110: $response=&reply('instemailrules:'.&escape($udom),
1111: $homeserver);
1.923 raeburn 1112: } else {
1113: $response=&reply('instuserrules:'.&escape($udom),
1114: $homeserver);
1115: }
1.912 raeburn 1116: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 1117: ($response ne 'unknown_cmd') &&
1.912 raeburn 1118: ($response ne 'no_such_host')) {
1119: my ($hashitems,$orderitems) = split(/:/,$response);
1120: my @pairs=split(/\&/,$hashitems);
1121: foreach my $item (@pairs) {
1122: my ($key,$value)=split(/=/,$item,2);
1123: $key = &unescape($key);
1124: next if ($key =~ /^error: 2 /);
1125: $ruleshash{$key}=&thaw_unescape($value);
1126: }
1127: my @esc_order = split(/\&/,$orderitems);
1128: foreach my $item (@esc_order) {
1129: push(@ruleorder,&unescape($item));
1130: }
1131: }
1132: }
1133: }
1134: return (\%ruleshash,\@ruleorder);
1135: }
1136:
1.943 raeburn 1137: # ------------------------- Get Authentication and Language Defaults for Domain
1138:
1139: sub get_domain_defaults {
1140: my ($domain) = @_;
1141: my $cachetime = 60*60*24;
1142: my ($defauthtype,$defautharg,$deflang);
1143: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
1144: if (defined($cached)) {
1145: if (ref($result) eq 'HASH') {
1146: return %{$result};
1147: }
1148: }
1149: my %domdefaults;
1150: my %domconfig =
1151: &Apache::lonnet::get_dom('configuration',['defaults'],$domain);
1152: if (ref($domconfig{'defaults'}) eq 'HASH') {
1153: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
1154: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
1155: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1156: } else {
1157: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
1158: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
1159: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
1160: }
1161: &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
1162: $cachetime);
1163: return %domdefaults;
1164: }
1165:
1.344 www 1166: # --------------------------------------------------- Assign a key to a student
1167:
1168: sub assign_access_key {
1.364 www 1169: #
1170: # a valid key looks like uname:udom#comments
1171: # comments are being appended
1172: #
1.498 www 1173: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
1174: $kdom=
1.620 albertel 1175: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 1176: $knum=
1.620 albertel 1177: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 1178: $cdom=
1.620 albertel 1179: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1180: $cnum=
1.620 albertel 1181: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1182: $udom=$env{'user.name'} unless (defined($udom));
1183: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 1184: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 1185: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 1186: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 1187: # assigned to this person
1188: # - this should not happen,
1.345 www 1189: # unless something went wrong
1190: # the first time around
1191: # ready to assign
1.364 www 1192: $logentry=$1.'; '.$logentry;
1.496 www 1193: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 1194: $kdom,$knum) eq 'ok') {
1.345 www 1195: # key now belongs to user
1.346 www 1196: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 1197: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 1198: &appenv({'environment.'.$envkey => $ckey});
1.345 www 1199: return 'ok';
1200: } else {
1201: return
1202: 'error: Count not permanently assign key, will need to be re-entered later.';
1203: }
1204: } else {
1205: return 'error: Could not assign key, try again later.';
1206: }
1.364 www 1207: } elsif (!$existing{$ckey}) {
1.345 www 1208: # the key does not exist
1209: return 'error: The key does not exist';
1210: } else {
1211: # the key is somebody else's
1212: return 'error: The key is already in use';
1213: }
1.344 www 1214: }
1215:
1.364 www 1216: # ------------------------------------------ put an additional comment on a key
1217:
1218: sub comment_access_key {
1219: #
1220: # a valid key looks like uname:udom#comments
1221: # comments are being appended
1222: #
1223: my ($ckey,$cdom,$cnum,$logentry)=@_;
1224: $cdom=
1.620 albertel 1225: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 1226: $cnum=
1.620 albertel 1227: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 1228: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1229: if ($existing{$ckey}) {
1230: $existing{$ckey}.='; '.$logentry;
1231: # ready to assign
1.367 www 1232: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 1233: $cdom,$cnum) eq 'ok') {
1234: return 'ok';
1235: } else {
1236: return 'error: Count not store comment.';
1237: }
1238: } else {
1239: # the key does not exist
1240: return 'error: The key does not exist';
1241: }
1242: }
1243:
1.344 www 1244: # ------------------------------------------------------ Generate a set of keys
1245:
1246: sub generate_access_keys {
1.364 www 1247: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 1248: $cdom=
1.620 albertel 1249: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1250: $cnum=
1.620 albertel 1251: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 1252: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 1253: unless (($cdom) && ($cnum)) { return 0; }
1254: if ($number>10000) { return 0; }
1255: sleep(2); # make sure don't get same seed twice
1256: srand(time()^($$+($$<<15))); # from "Programming Perl"
1257: my $total=0;
1258: for (my $i=1;$i<=$number;$i++) {
1259: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
1260: sprintf("%lx",int(100000*rand)).'-'.
1261: sprintf("%lx",int(100000*rand));
1262: $newkey=~s/1/g/g; # folks mix up 1 and l
1263: $newkey=~s/0/h/g; # and also 0 and O
1264: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
1265: if ($existing{$newkey}) {
1266: $i--;
1267: } else {
1.364 www 1268: if (&put('accesskeys',
1269: { $newkey => '# generated '.localtime().
1.620 albertel 1270: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 1271: '; '.$logentry },
1272: $cdom,$cnum) eq 'ok') {
1.344 www 1273: $total++;
1274: }
1275: }
1276: }
1.620 albertel 1277: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 1278: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
1279: return $total;
1280: }
1281:
1282: # ------------------------------------------------------- Validate an accesskey
1283:
1284: sub validate_access_key {
1285: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
1286: $cdom=
1.620 albertel 1287: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1288: $cnum=
1.620 albertel 1289: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1290: $udom=$env{'user.domain'} unless (defined($udom));
1291: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 1292: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 1293: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 1294: }
1295:
1296: # ------------------------------------- Find the section of student in a course
1.652 albertel 1297: sub devalidate_getsection_cache {
1298: my ($udom,$unam,$courseid)=@_;
1299: my $hashid="$udom:$unam:$courseid";
1300: &devalidate_cache_new('getsection',$hashid);
1301: }
1.298 matthew 1302:
1.815 albertel 1303: sub courseid_to_courseurl {
1304: my ($courseid) = @_;
1305: #already url style courseid
1306: return $courseid if ($courseid =~ m{^/});
1307:
1308: if (exists($env{'course.'.$courseid.'.num'})) {
1309: my $cnum = $env{'course.'.$courseid.'.num'};
1310: my $cdom = $env{'course.'.$courseid.'.domain'};
1311: return "/$cdom/$cnum";
1312: }
1313:
1314: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
1315: if (exists($courseinfo{'num'})) {
1316: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
1317: }
1318:
1319: return undef;
1320: }
1321:
1.298 matthew 1322: sub getsection {
1323: my ($udom,$unam,$courseid)=@_;
1.599 albertel 1324: my $cachetime=1800;
1.551 albertel 1325:
1326: my $hashid="$udom:$unam:$courseid";
1.599 albertel 1327: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 1328: if (defined($cached)) { return $result; }
1329:
1.298 matthew 1330: my %Pending;
1331: my %Expired;
1332: #
1333: # Each role can either have not started yet (pending), be active,
1334: # or have expired.
1335: #
1336: # If there is an active role, we are done.
1337: #
1338: # If there is more than one role which has not started yet,
1339: # choose the one which will start sooner
1340: # If there is one role which has not started yet, return it.
1341: #
1342: # If there is more than one expired role, choose the one which ended last.
1343: # If there is a role which has expired, return it.
1344: #
1.815 albertel 1345: $courseid = &courseid_to_courseurl($courseid);
1.817 raeburn 1346: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1347: foreach my $key (keys(%roleshash)) {
1.479 albertel 1348: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1349: my $section=$1;
1350: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1351: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1352: my $now=time;
1.548 albertel 1353: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1354: $Expired{$end}=$section;
1355: next;
1356: }
1.548 albertel 1357: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1358: $Pending{$start}=$section;
1359: next;
1360: }
1.599 albertel 1361: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1362: }
1363: #
1364: # Presumedly there will be few matching roles from the above
1365: # loop and the sorting time will be negligible.
1366: if (scalar(keys(%Pending))) {
1367: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1368: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1369: }
1370: if (scalar(keys(%Expired))) {
1371: my @sorted = sort {$a <=> $b} keys(%Expired);
1372: my $time = pop(@sorted);
1.599 albertel 1373: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1374: }
1.599 albertel 1375: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1376: }
1.70 www 1377:
1.599 albertel 1378: sub save_cache {
1379: &purge_remembered();
1.722 albertel 1380: #&Apache::loncommon::validate_page();
1.620 albertel 1381: undef(%env);
1.780 albertel 1382: undef($env_loaded);
1.599 albertel 1383: }
1.452 albertel 1384:
1.599 albertel 1385: my $to_remember=-1;
1386: my %remembered;
1387: my %accessed;
1388: my $kicks=0;
1389: my $hits=0;
1.849 albertel 1390: sub make_key {
1391: my ($name,$id) = @_;
1.872 albertel 1392: if (length($id) > 65
1393: && length(&escape($id)) > 200) {
1394: $id=length($id).':'.&Digest::MD5::md5_hex($id);
1395: }
1.849 albertel 1396: return &escape($name.':'.$id);
1397: }
1398:
1.599 albertel 1399: sub devalidate_cache_new {
1400: my ($name,$id,$debug) = @_;
1401: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1402: $id=&make_key($name,$id);
1.599 albertel 1403: $memcache->delete($id);
1404: delete($remembered{$id});
1405: delete($accessed{$id});
1406: }
1407:
1408: sub is_cached_new {
1409: my ($name,$id,$debug) = @_;
1.849 albertel 1410: $id=&make_key($name,$id);
1.599 albertel 1411: if (exists($remembered{$id})) {
1412: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1413: $accessed{$id}=[&gettimeofday()];
1414: $hits++;
1415: return ($remembered{$id},1);
1416: }
1417: my $value = $memcache->get($id);
1418: if (!(defined($value))) {
1419: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1420: return (undef,undef);
1.416 albertel 1421: }
1.599 albertel 1422: if ($value eq '__undef__') {
1423: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1424: $value=undef;
1425: }
1426: &make_room($id,$value,$debug);
1427: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1428: return ($value,1);
1429: }
1430:
1431: sub do_cache_new {
1432: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1433: $id=&make_key($name,$id);
1.599 albertel 1434: my $setvalue=$value;
1435: if (!defined($setvalue)) {
1436: $setvalue='__undef__';
1437: }
1.623 albertel 1438: if (!defined($time) ) {
1439: $time=600;
1440: }
1.599 albertel 1441: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 1442: my $result = $memcache->set($id,$setvalue,$time);
1443: if (! $result) {
1.872 albertel 1444: &logthis("caching of id -> $id failed");
1.910 albertel 1445: $memcache->disconnect_all();
1.872 albertel 1446: }
1.600 albertel 1447: # need to make a copy of $value
1.919 albertel 1448: &make_room($id,$value,$debug);
1.599 albertel 1449: return $value;
1450: }
1451:
1452: sub make_room {
1453: my ($id,$value,$debug)=@_;
1.919 albertel 1454:
1455: $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
1456: : $value;
1.599 albertel 1457: if ($to_remember<0) { return; }
1458: $accessed{$id}=[&gettimeofday()];
1459: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1460: my $to_kick;
1461: my $max_time=0;
1462: foreach my $other (keys(%accessed)) {
1463: if (&tv_interval($accessed{$other}) > $max_time) {
1464: $to_kick=$other;
1465: $max_time=&tv_interval($accessed{$other});
1466: }
1467: }
1468: delete($remembered{$to_kick});
1469: delete($accessed{$to_kick});
1470: $kicks++;
1471: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1472: return;
1473: }
1474:
1.599 albertel 1475: sub purge_remembered {
1.604 albertel 1476: #&logthis("Tossing ".scalar(keys(%remembered)));
1477: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1478: undef(%remembered);
1479: undef(%accessed);
1.428 albertel 1480: }
1.70 www 1481: # ------------------------------------- Read an entry from a user's environment
1482:
1483: sub userenvironment {
1484: my ($udom,$unam,@what)=@_;
1485: my %returnhash=();
1486: my @answer=split(/\&/,
1487: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
1488: &homeserver($unam,$udom)));
1489: my $i;
1490: for ($i=0;$i<=$#what;$i++) {
1491: $returnhash{$what[$i]}=&unescape($answer[$i]);
1492: }
1493: return %returnhash;
1.1 albertel 1494: }
1495:
1.617 albertel 1496: # ---------------------------------------------------------- Get a studentphoto
1497: sub studentphoto {
1498: my ($udom,$unam,$ext) = @_;
1499: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 1500: if (defined($env{'request.course.id'})) {
1.708 raeburn 1501: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 1502: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1503: return(&retrievestudentphoto($udom,$unam,$ext));
1504: } else {
1505: my ($result,$perm_reqd)=
1.707 albertel 1506: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1507: if ($result eq 'ok') {
1508: if (!($perm_reqd eq 'yes')) {
1509: return(&retrievestudentphoto($udom,$unam,$ext));
1510: }
1511: }
1512: }
1513: }
1514: } else {
1515: my ($result,$perm_reqd) =
1.707 albertel 1516: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1517: if ($result eq 'ok') {
1518: if (!($perm_reqd eq 'yes')) {
1519: return(&retrievestudentphoto($udom,$unam,$ext));
1520: }
1521: }
1522: }
1523: return '/adm/lonKaputt/lonlogo_broken.gif';
1524: }
1525:
1526: sub retrievestudentphoto {
1527: my ($udom,$unam,$ext,$type) = @_;
1528: my $home=&Apache::lonnet::homeserver($unam,$udom);
1529: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
1530: if ($ret eq 'ok') {
1531: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
1532: if ($type eq 'thumbnail') {
1533: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
1534: }
1535: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
1536: return $tokenurl;
1537: } else {
1538: if ($type eq 'thumbnail') {
1539: return '/adm/lonKaputt/genericstudent_tn.gif';
1540: } else {
1541: return '/adm/lonKaputt/lonlogo_broken.gif';
1542: }
1.617 albertel 1543: }
1544: }
1545:
1.263 www 1546: # -------------------------------------------------------------------- New chat
1547:
1548: sub chatsend {
1.724 raeburn 1549: my ($newentry,$anon,$group)=@_;
1.620 albertel 1550: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1551: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1552: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 1553: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 1554: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 1555: &escape($newentry)).':'.$group,$chome);
1.292 www 1556: }
1557:
1558: # ------------------------------------------ Find current version of a resource
1559:
1560: sub getversion {
1561: my $fname=&clutter(shift);
1562: unless ($fname=~/^\/res\//) { return -1; }
1563: return ¤tversion(&filelocation('',$fname));
1564: }
1565:
1566: sub currentversion {
1567: my $fname=shift;
1.599 albertel 1568: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 1569: if (defined($cached)) { return $result; }
1.292 www 1570: my $author=$fname;
1571: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1572: my ($udom,$uname)=split(/\//,$author);
1573: my $home=homeserver($uname,$udom);
1574: if ($home eq 'no_host') {
1575: return -1;
1576: }
1577: my $answer=reply("currentversion:$fname",$home);
1578: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1579: return -1;
1580: }
1.599 albertel 1581: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 1582: }
1583:
1.1 albertel 1584: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 1585:
1.1 albertel 1586: sub subscribe {
1587: my $fname=shift;
1.761 raeburn 1588: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 1589: $fname=~s/[\n\r]//g;
1.1 albertel 1590: my $author=$fname;
1591: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1592: my ($udom,$uname)=split(/\//,$author);
1593: my $home=homeserver($uname,$udom);
1.335 albertel 1594: if ($home eq 'no_host') {
1595: return 'not_found';
1.1 albertel 1596: }
1597: my $answer=reply("sub:$fname",$home);
1.64 www 1598: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1599: $answer.=' by '.$home;
1600: }
1.1 albertel 1601: return $answer;
1602: }
1603:
1.8 www 1604: # -------------------------------------------------------------- Replicate file
1605:
1606: sub repcopy {
1607: my $filename=shift;
1.23 www 1608: $filename=~s/\/+/\//g;
1.607 raeburn 1609: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
1610: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 1611: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 1612: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 1613: return &repcopy_userfile($filename);
1614: }
1.532 albertel 1615: $filename=~s/[\n\r]//g;
1.8 www 1616: my $transname="$filename.in.transfer";
1.828 www 1617: # FIXME: this should flock
1.607 raeburn 1618: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 1619: my $remoteurl=subscribe($filename);
1.64 www 1620: if ($remoteurl =~ /^con_lost by/) {
1621: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1622: return 'unavailable';
1.8 www 1623: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 1624: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 1625: return 'not_found';
1.64 www 1626: } elsif ($remoteurl =~ /^rejected by/) {
1627: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1628: return 'forbidden';
1.20 www 1629: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 1630: return 'ok';
1.8 www 1631: } else {
1.290 www 1632: my $author=$filename;
1633: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1634: my ($udom,$uname)=split(/\//,$author);
1635: my $home=homeserver($uname,$udom);
1636: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 1637: my @parts=split(/\//,$filename);
1638: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1639: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1640: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 1641: return 'bad_request';
1.8 www 1642: }
1643: my $count;
1644: for ($count=5;$count<$#parts;$count++) {
1645: $path.="/$parts[$count]";
1646: if ((-e $path)!=1) {
1647: mkdir($path,0777);
1648: }
1649: }
1650: my $ua=new LWP::UserAgent;
1651: my $request=new HTTP::Request('GET',"$remoteurl");
1652: my $response=$ua->request($request,$transname);
1653: if ($response->is_error()) {
1654: unlink($transname);
1655: my $message=$response->status_line;
1.672 albertel 1656: &logthis("<font color=\"blue\">WARNING:"
1.12 www 1657: ." LWP get: $message: $filename</font>");
1.607 raeburn 1658: return 'unavailable';
1.8 www 1659: } else {
1.16 www 1660: if ($remoteurl!~/\.meta$/) {
1661: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1662: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1663: if ($mresponse->is_error()) {
1664: unlink($filename.'.meta');
1665: &logthis(
1.672 albertel 1666: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 1667: }
1668: }
1.8 www 1669: rename($transname,$filename);
1.607 raeburn 1670: return 'ok';
1.8 www 1671: }
1.290 www 1672: }
1.8 www 1673: }
1.330 www 1674: }
1675:
1676: # ------------------------------------------------ Get server side include body
1677: sub ssi_body {
1.381 albertel 1678: my ($filelink,%form)=@_;
1.606 matthew 1679: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
1680: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
1681: }
1.330 www 1682: my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381 albertel 1683: &ssi($filelink,%form));
1.778 albertel 1684: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 1685: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 1686: $output=~s/\<\/body\s*\>.*?$//si;
1.330 www 1687: return $output;
1.8 www 1688: }
1689:
1.15 www 1690: # --------------------------------------------------------- Server Side Include
1691:
1.782 albertel 1692: sub absolute_url {
1693: my ($host_name) = @_;
1694: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
1695: if ($host_name eq '') {
1696: $host_name = $ENV{'SERVER_NAME'};
1697: }
1698: return $protocol.$host_name;
1699: }
1700:
1.942 foxr 1701: #
1702: # Server side include.
1703: # Parameters:
1704: # fn Possibly encrypted resource name/id.
1705: # form Hash that describes how the rendering should be done
1706: # and other things.
1.944 foxr 1707: # Returns:
1.950 raeburn 1708: # Scalar context: The content of the response.
1709: # Array context: 2 element list of the content and the full response object.
1.942 foxr 1710: #
1.15 www 1711: sub ssi {
1712:
1.944 foxr 1713: my ($fn,%form)=@_;
1.15 www 1714: my $ua=new LWP::UserAgent;
1.23 www 1715: my $request;
1.711 albertel 1716:
1717: $form{'no_update_last_known'}=1;
1.895 albertel 1718: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 1719: if (%form) {
1.782 albertel 1720: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201 albertel 1721: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23 www 1722: } else {
1.782 albertel 1723: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 1724: }
1725:
1.15 www 1726: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1727: my $response=$ua->request($request);
1728:
1.944 foxr 1729: if (wantarray) {
1730: return ($response->content, $response);
1731: } else {
1732: return $response->content;
1.942 foxr 1733: }
1.324 www 1734: }
1735:
1736: sub externalssi {
1737: my ($url)=@_;
1738: my $ua=new LWP::UserAgent;
1739: my $request=new HTTP::Request('GET',$url);
1740: my $response=$ua->request($request);
1.15 www 1741: return $response->content;
1742: }
1.254 www 1743:
1.492 albertel 1744: # -------------------------------- Allow a /uploaded/ URI to be vouched for
1745:
1746: sub allowuploaded {
1747: my ($srcurl,$url)=@_;
1748: $url=&clutter(&declutter($url));
1749: my $dir=$url;
1750: $dir=~s/\/[^\/]+$//;
1751: my %httpref=();
1752: my $httpurl=&hreflocation('',$url);
1753: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 1754: &Apache::lonnet::appenv(\%httpref);
1.254 www 1755: }
1.477 raeburn 1756:
1.478 albertel 1757: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 1758: # input: action, courseID, current domain, intended
1.637 raeburn 1759: # path to file, source of file, instruction to parse file for objects,
1760: # ref to hash for embedded objects,
1761: # ref to hash for codebase of java objects.
1762: #
1.485 raeburn 1763: # output: url to file (if action was uploaddoc),
1764: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 1765: #
1.478 albertel 1766: # Allows directory structure to be used within lonUsers/../userfiles/ for a
1767: # course.
1.477 raeburn 1768: #
1.478 albertel 1769: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1770: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
1771: # course's home server.
1.477 raeburn 1772: #
1.478 albertel 1773: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
1774: # be copied from $source (current location) to
1775: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1776: # and will then be copied to
1777: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
1778: # course's home server.
1.485 raeburn 1779: #
1.481 raeburn 1780: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 1781: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 1782: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1783: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
1784: # in course's home server.
1.637 raeburn 1785: #
1.477 raeburn 1786:
1787: sub process_coursefile {
1.638 albertel 1788: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477 raeburn 1789: my $fetchresult;
1.638 albertel 1790: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 1791: if ($action eq 'propagate') {
1.638 albertel 1792: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1793: $home);
1.481 raeburn 1794: } else {
1.477 raeburn 1795: my $fpath = '';
1796: my $fname = $file;
1.478 albertel 1797: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 1798: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 1799: my $filepath = &build_filepath($fpath);
1.481 raeburn 1800: if ($action eq 'copy') {
1801: if ($source eq '') {
1802: $fetchresult = 'no source file';
1803: return $fetchresult;
1804: } else {
1805: my $destination = $filepath.'/'.$fname;
1806: rename($source,$destination);
1807: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1808: $home);
1.481 raeburn 1809: }
1810: } elsif ($action eq 'uploaddoc') {
1811: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 1812: print $fh $env{'form.'.$source};
1.481 raeburn 1813: close($fh);
1.637 raeburn 1814: if ($parser eq 'parse') {
1815: my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
1816: unless ($parse_result eq 'ok') {
1817: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
1818: }
1819: }
1.477 raeburn 1820: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1821: $home);
1.481 raeburn 1822: if ($fetchresult eq 'ok') {
1823: return '/uploaded/'.$fpath.'/'.$fname;
1824: } else {
1825: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1826: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 1827: return '/adm/notfound.html';
1828: }
1.477 raeburn 1829: }
1830: }
1.485 raeburn 1831: unless ( $fetchresult eq 'ok') {
1.477 raeburn 1832: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 1833: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 1834: }
1835: return $fetchresult;
1836: }
1837:
1.637 raeburn 1838: sub build_filepath {
1839: my ($fpath) = @_;
1840: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
1841: unless ($fpath eq '') {
1842: my @parts=split('/',$fpath);
1843: foreach my $part (@parts) {
1844: $filepath.= '/'.$part;
1845: if ((-e $filepath)!=1) {
1846: mkdir($filepath,0777);
1847: }
1848: }
1849: }
1850: return $filepath;
1851: }
1852:
1853: sub store_edited_file {
1.638 albertel 1854: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 1855: my $file = $primary_url;
1856: $file =~ s#^/uploaded/$docudom/$docuname/##;
1857: my $fpath = '';
1858: my $fname = $file;
1859: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1860: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1861: my $filepath = &build_filepath($fpath);
1862: open(my $fh,'>'.$filepath.'/'.$fname);
1863: print $fh $content;
1864: close($fh);
1.638 albertel 1865: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 1866: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 1867: $home);
1.637 raeburn 1868: if ($$fetchresult eq 'ok') {
1869: return '/uploaded/'.$fpath.'/'.$fname;
1870: } else {
1.638 albertel 1871: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1872: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 1873: return '/adm/notfound.html';
1874: }
1875: }
1876:
1.531 albertel 1877: sub clean_filename {
1.831 albertel 1878: my ($fname,$args)=@_;
1.315 www 1879: # Replace Windows backslashes by forward slashes
1.257 www 1880: $fname=~s/\\/\//g;
1.831 albertel 1881: if (!$args->{'keep_path'}) {
1882: # Get rid of everything but the actual filename
1883: $fname=~s/^.*\/([^\/]+)$/$1/;
1884: }
1.315 www 1885: # Replace spaces by underscores
1886: $fname=~s/\s+/\_/g;
1887: # Replace all other weird characters by nothing
1.831 albertel 1888: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 1889: # Replace all .\d. sequences with _\d. so they no longer look like version
1890: # numbers
1891: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 1892: return $fname;
1893: }
1894:
1.608 albertel 1895: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 1896: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719 banghart 1897: # the desired filenam is in $env{"form.$formname.filename"}
1.686 albertel 1898: # $coursedoc - if true up to the current course
1899: # if false
1900: # $subdir - directory in userfile to store the file into
1.858 raeburn 1901: # $parser - instruction to parse file for objects ($parser = parse)
1902: # $allfiles - reference to hash for embedded objects
1903: # $codebase - reference to hash for codebase of java objects
1904: # $desuname - username for permanent storage of uploaded file
1905: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 1906: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
1907: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858 raeburn 1908: #
1.686 albertel 1909: # output: url of file in userspace, or error: <message>
1910: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 1911:
1912:
1.531 albertel 1913: sub userfileupload {
1.860 raeburn 1914: my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
1915: $destudom,$thumbwidth,$thumbheight)=@_;
1.531 albertel 1916: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 1917: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 1918: $fname=&clean_filename($fname);
1.315 www 1919: # See if there is anything left
1.257 www 1920: unless ($fname) { return 'error: no uploaded file'; }
1.620 albertel 1921: chop($env{'form.'.$formname});
1.523 raeburn 1922: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
1923: my $now = time;
1924: my $filepath = 'tmp/helprequests/'.$now;
1925: my @parts=split(/\//,$filepath);
1926: my $fullpath = $perlvar{'lonDaemons'};
1927: for (my $i=0;$i<@parts;$i++) {
1928: $fullpath .= '/'.$parts[$i];
1929: if ((-e $fullpath)!=1) {
1930: mkdir($fullpath,0777);
1931: }
1932: }
1933: open(my $fh,'>'.$fullpath.'/'.$fname);
1.620 albertel 1934: print $fh $env{'form.'.$formname};
1.523 raeburn 1935: close($fh);
1.741 raeburn 1936: return $fullpath.'/'.$fname;
1937: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
1938: my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
1939: '_'.$env{'user.domain'}.'/pending';
1940: my @parts=split(/\//,$filepath);
1941: my $fullpath = $perlvar{'lonDaemons'};
1942: for (my $i=0;$i<@parts;$i++) {
1943: $fullpath .= '/'.$parts[$i];
1944: if ((-e $fullpath)!=1) {
1945: mkdir($fullpath,0777);
1946: }
1947: }
1948: open(my $fh,'>'.$fullpath.'/'.$fname);
1949: print $fh $env{'form.'.$formname};
1950: close($fh);
1951: return $fullpath.'/'.$fname;
1.523 raeburn 1952: }
1.719 banghart 1953:
1.258 www 1954: # Create the directory if not present
1.493 albertel 1955: $fname="$subdir/$fname";
1.259 www 1956: if ($coursedoc) {
1.638 albertel 1957: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1958: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 1959: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 1960: return &finishuserfileupload($docuname,$docudom,
1961: $formname,$fname,$parser,$allfiles,
1.860 raeburn 1962: $codebase,$thumbwidth,$thumbheight);
1.481 raeburn 1963: } else {
1.620 albertel 1964: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 1965: return &process_coursefile('uploaddoc',$docuname,$docudom,
1966: $fname,$formname,$parser,
1967: $allfiles,$codebase);
1.481 raeburn 1968: }
1.719 banghart 1969: } elsif (defined($destuname)) {
1970: my $docuname=$destuname;
1971: my $docudom=$destudom;
1.860 raeburn 1972: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1973: $parser,$allfiles,$codebase,
1974: $thumbwidth,$thumbheight);
1.719 banghart 1975:
1.259 www 1976: } else {
1.638 albertel 1977: my $docuname=$env{'user.name'};
1978: my $docudom=$env{'user.domain'};
1.714 raeburn 1979: if (exists($env{'form.group'})) {
1980: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
1981: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1982: }
1.860 raeburn 1983: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
1984: $parser,$allfiles,$codebase,
1985: $thumbwidth,$thumbheight);
1.259 www 1986: }
1.271 www 1987: }
1988:
1989: sub finishuserfileupload {
1.860 raeburn 1990: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1991: $thumbwidth,$thumbheight) = @_;
1.477 raeburn 1992: my $path=$docudom.'/'.$docuname.'/';
1.258 www 1993: my $filepath=$perlvar{'lonDocRoot'};
1.860 raeburn 1994: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 1995: $file=$fname;
1996: if ($fname=~m|/|) {
1997: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
1998: $path.=$fnamepath.'/';
1999: }
1.259 www 2000: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 2001: my $count;
2002: for ($count=4;$count<=$#parts;$count++) {
2003: $filepath.="/$parts[$count]";
2004: if ((-e $filepath)!=1) {
2005: mkdir($filepath,0777);
2006: }
2007: }
2008: # Save the file
2009: {
1.701 albertel 2010: if (!open(FH,'>'.$filepath.'/'.$file)) {
2011: &logthis('Failed to create '.$filepath.'/'.$file);
2012: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
2013: return '/adm/notfound.html';
2014: }
2015: if (!print FH ($env{'form.'.$formname})) {
2016: &logthis('Failed to write to '.$filepath.'/'.$file);
2017: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
2018: return '/adm/notfound.html';
2019: }
1.570 albertel 2020: close(FH);
1.258 www 2021: }
1.637 raeburn 2022: if ($parser eq 'parse') {
1.638 albertel 2023: my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
2024: $codebase);
1.637 raeburn 2025: unless ($parse_result eq 'ok') {
1.638 albertel 2026: &logthis('Failed to parse '.$filepath.$file.
2027: ' for embedded media: '.$parse_result);
1.637 raeburn 2028: }
2029: }
1.860 raeburn 2030: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
2031: my $input = $filepath.'/'.$file;
2032: my $output = $filepath.'/'.'tn-'.$file;
2033: my $thumbsize = $thumbwidth.'x'.$thumbheight;
2034: system("convert -sample $thumbsize $input $output");
2035: if (-e $filepath.'/'.'tn-'.$file) {
2036: $fetchthumb = 1;
2037: }
2038: }
1.858 raeburn 2039:
1.259 www 2040: # Notify homeserver to grep it
2041: #
1.638 albertel 2042: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 2043: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 2044: if ($fetchresult eq 'ok') {
1.860 raeburn 2045: if ($fetchthumb) {
2046: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
2047: if ($thumbresult ne 'ok') {
2048: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
2049: $docuhome.': '.$thumbresult);
2050: }
2051: }
1.259 www 2052: #
1.258 www 2053: # Return the URL to it
1.494 albertel 2054: return '/uploaded/'.$path.$file;
1.263 www 2055: } else {
1.494 albertel 2056: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
2057: ': '.$fetchresult);
1.263 www 2058: return '/adm/notfound.html';
1.858 raeburn 2059: }
1.493 albertel 2060: }
2061:
1.637 raeburn 2062: sub extract_embedded_items {
1.648 raeburn 2063: my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637 raeburn 2064: my @state = ();
2065: my %javafiles = (
2066: codebase => '',
2067: code => '',
2068: archive => ''
2069: );
2070: my %mediafiles = (
2071: src => '',
2072: movie => '',
2073: );
1.648 raeburn 2074: my $p;
2075: if ($content) {
2076: $p = HTML::LCParser->new($content);
2077: } else {
2078: $p = HTML::LCParser->new($filepath.'/'.$file);
2079: }
1.641 albertel 2080: while (my $t=$p->get_token()) {
1.640 albertel 2081: if ($t->[0] eq 'S') {
2082: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 2083: push(@state, $tagname);
1.648 raeburn 2084: if (lc($tagname) eq 'allow') {
2085: &add_filetype($allfiles,$attr->{'src'},'src');
2086: }
1.640 albertel 2087: if (lc($tagname) eq 'img') {
2088: &add_filetype($allfiles,$attr->{'src'},'src');
2089: }
1.886 albertel 2090: if (lc($tagname) eq 'a') {
2091: &add_filetype($allfiles,$attr->{'href'},'href');
2092: }
1.645 raeburn 2093: if (lc($tagname) eq 'script') {
2094: if ($attr->{'archive'} =~ /\.jar$/i) {
2095: &add_filetype($allfiles,$attr->{'archive'},'archive');
2096: } else {
2097: &add_filetype($allfiles,$attr->{'src'},'src');
2098: }
2099: }
2100: if (lc($tagname) eq 'link') {
2101: if (lc($attr->{'rel'}) eq 'stylesheet') {
2102: &add_filetype($allfiles,$attr->{'href'},'href');
2103: }
2104: }
1.640 albertel 2105: if (lc($tagname) eq 'object' ||
2106: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
2107: foreach my $item (keys(%javafiles)) {
2108: $javafiles{$item} = '';
2109: }
2110: }
2111: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
2112: my $name = lc($attr->{'name'});
2113: foreach my $item (keys(%javafiles)) {
2114: if ($name eq $item) {
2115: $javafiles{$item} = $attr->{'value'};
2116: last;
2117: }
2118: }
2119: foreach my $item (keys(%mediafiles)) {
2120: if ($name eq $item) {
2121: &add_filetype($allfiles, $attr->{'value'}, 'value');
2122: last;
2123: }
2124: }
2125: }
2126: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
2127: foreach my $item (keys(%javafiles)) {
2128: if ($attr->{$item}) {
2129: $javafiles{$item} = $attr->{$item};
2130: last;
2131: }
2132: }
2133: foreach my $item (keys(%mediafiles)) {
2134: if ($attr->{$item}) {
2135: &add_filetype($allfiles,$attr->{$item},$item);
2136: last;
2137: }
2138: }
2139: }
2140: } elsif ($t->[0] eq 'E') {
2141: my ($tagname) = ($t->[1]);
2142: if ($javafiles{'codebase'} ne '') {
2143: $javafiles{'codebase'} .= '/';
2144: }
2145: if (lc($tagname) eq 'applet' ||
2146: lc($tagname) eq 'object' ||
2147: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
2148: ) {
2149: foreach my $item (keys(%javafiles)) {
2150: if ($item ne 'codebase' && $javafiles{$item} ne '') {
2151: my $file=$javafiles{'codebase'}.$javafiles{$item};
2152: &add_filetype($allfiles,$file,$item);
2153: }
2154: }
2155: }
2156: pop @state;
2157: }
2158: }
1.637 raeburn 2159: return 'ok';
2160: }
2161:
1.639 albertel 2162: sub add_filetype {
2163: my ($allfiles,$file,$type)=@_;
2164: if (exists($allfiles->{$file})) {
2165: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
2166: push(@{$allfiles->{$file}}, &escape($type));
2167: }
2168: } else {
2169: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 2170: }
2171: }
2172:
1.493 albertel 2173: sub removeuploadedurl {
2174: my ($url)=@_;
2175: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 2176: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 2177: }
2178:
2179: sub removeuserfile {
2180: my ($docuname,$docudom,$fname)=@_;
2181: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2182: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
2183: if ($result eq 'ok') {
2184: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
2185: my $metafile = $fname.'.meta';
2186: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 2187: my $url = "/uploaded/$docudom/$docuname/$fname";
2188: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2189: my $sqlresult =
1.823 albertel 2190: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2191: 'portfolio_metadata',$group,
2192: 'delete');
1.798 raeburn 2193: }
2194: }
2195: return $result;
1.257 www 2196: }
1.15 www 2197:
1.530 albertel 2198: sub mkdiruserfile {
2199: my ($docuname,$docudom,$dir)=@_;
2200: my $home=&homeserver($docuname,$docudom);
2201: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
2202: }
2203:
1.531 albertel 2204: sub renameuserfile {
2205: my ($docuname,$docudom,$old,$new)=@_;
2206: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2207: my $result = &reply("renameuserfile:$docudom:$docuname:".
2208: &escape("$old").':'.&escape("$new"),$home);
2209: if ($result eq 'ok') {
2210: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
2211: my $oldmeta = $old.'.meta';
2212: my $newmeta = $new.'.meta';
2213: my $metaresult =
2214: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 2215: my $url = "/uploaded/$docudom/$docuname/$old";
2216: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2217: my $sqlresult =
1.823 albertel 2218: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2219: 'portfolio_metadata',$group,
2220: 'delete');
1.798 raeburn 2221: }
2222: }
2223: return $result;
1.531 albertel 2224: }
2225:
1.14 www 2226: # ------------------------------------------------------------------------- Log
2227:
2228: sub log {
2229: my ($dom,$nam,$hom,$what)=@_;
1.47 www 2230: return critical("log:$dom:$nam:$what",$hom);
1.157 www 2231: }
2232:
2233: # ------------------------------------------------------------------ Course Log
1.352 www 2234: #
2235: # This routine flushes several buffers of non-mission-critical nature
2236: #
1.157 www 2237:
2238: sub flushcourselogs {
1.352 www 2239: &logthis('Flushing log buffers');
2240: #
2241: # course logs
2242: # This is a log of all transactions in a course, which can be used
2243: # for data mining purposes
2244: #
2245: # It also collects the courseid database, which lists last transaction
2246: # times and course titles for all courseids
2247: #
2248: my %courseidbuffer=();
1.921 raeburn 2249: foreach my $crsid (keys(%courselogs)) {
1.352 www 2250: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 2251: &escape($courselogs{$crsid}),
2252: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 2253: delete $courselogs{$crsid};
2254: } else {
2255: &logthis('Failed to flush log buffer for '.$crsid);
2256: if (length($courselogs{$crsid})>40000) {
1.672 albertel 2257: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 2258: " exceeded maximum size, deleting.</font>");
2259: delete $courselogs{$crsid};
2260: }
1.352 www 2261: }
1.920 raeburn 2262: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 2263: 'description' => $coursedescrbuf{$crsid},
2264: 'inst_code' => $courseinstcodebuf{$crsid},
2265: 'type' => $coursetypebuf{$crsid},
2266: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 2267: };
1.191 harris41 2268: }
1.352 www 2269: #
2270: # Write course id database (reverse lookup) to homeserver of courses
2271: # Is used in pickcourse
2272: #
1.840 albertel 2273: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 2274: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 2275: $courseidbuffer{$crs_home},
2276: $crs_home,'timeonly');
1.352 www 2277: }
2278: #
2279: # File accesses
2280: # Writes to the dynamic metadata of resources to get hit counts, etc.
2281: #
1.449 matthew 2282: foreach my $entry (keys(%accesshash)) {
1.458 matthew 2283: if ($entry =~ /___count$/) {
2284: my ($dom,$name);
1.807 albertel 2285: ($dom,$name,undef)=
1.811 albertel 2286: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 2287: if (! defined($dom) || $dom eq '' ||
2288: ! defined($name) || $name eq '') {
1.620 albertel 2289: my $cid = $env{'request.course.id'};
2290: $dom = $env{'request.'.$cid.'.domain'};
2291: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 2292: }
1.450 matthew 2293: my $value = $accesshash{$entry};
2294: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
2295: my %temphash=($url => $value);
1.449 matthew 2296: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
2297: if ($result eq 'ok') {
2298: delete $accesshash{$entry};
2299: } elsif ($result eq 'unknown_cmd') {
2300: # Target server has old code running on it.
1.450 matthew 2301: my %temphash=($entry => $value);
1.449 matthew 2302: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2303: delete $accesshash{$entry};
2304: }
2305: }
2306: } else {
1.811 albertel 2307: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 2308: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 2309: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2310: delete $accesshash{$entry};
2311: }
1.185 www 2312: }
1.191 harris41 2313: }
1.352 www 2314: #
2315: # Roles
2316: # Reverse lookup of user roles for course faculty/staff and co-authorship
2317: #
1.800 albertel 2318: foreach my $entry (keys(%userrolehash)) {
1.351 www 2319: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 2320: split(/\:/,$entry);
2321: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 2322: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 2323: $rudom,$runame) eq 'ok') {
2324: delete $userrolehash{$entry};
2325: }
2326: }
1.662 raeburn 2327: #
2328: # Reverse lookup of domain roles (dc, ad, li, sc, au)
2329: #
2330: my %domrolebuffer = ();
2331: foreach my $entry (keys %domainrolehash) {
1.901 albertel 2332: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 2333: if ($domrolebuffer{$rudom}) {
2334: $domrolebuffer{$rudom}.='&'.&escape($entry).
2335: '='.&escape($domainrolehash{$entry});
2336: } else {
2337: $domrolebuffer{$rudom}.=&escape($entry).
2338: '='.&escape($domainrolehash{$entry});
2339: }
2340: delete $domainrolehash{$entry};
2341: }
2342: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 2343: my %servers = &get_servers($dom,'library');
2344: foreach my $tryserver (keys(%servers)) {
2345: unless (&reply('domroleput:'.$dom.':'.
2346: $domrolebuffer{$dom},$tryserver) eq 'ok') {
2347: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
2348: }
1.662 raeburn 2349: }
2350: }
1.186 www 2351: $dumpcount++;
1.157 www 2352: }
2353:
2354: sub courselog {
2355: my $what=shift;
1.158 www 2356: $what=time.':'.$what;
1.620 albertel 2357: unless ($env{'request.course.id'}) { return ''; }
2358: $coursedombuf{$env{'request.course.id'}}=
2359: $env{'course.'.$env{'request.course.id'}.'.domain'};
2360: $coursenumbuf{$env{'request.course.id'}}=
2361: $env{'course.'.$env{'request.course.id'}.'.num'};
2362: $coursehombuf{$env{'request.course.id'}}=
2363: $env{'course.'.$env{'request.course.id'}.'.home'};
2364: $coursedescrbuf{$env{'request.course.id'}}=
2365: $env{'course.'.$env{'request.course.id'}.'.description'};
2366: $courseinstcodebuf{$env{'request.course.id'}}=
2367: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2368: $courseownerbuf{$env{'request.course.id'}}=
2369: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 2370: $coursetypebuf{$env{'request.course.id'}}=
2371: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 2372: if (defined $courselogs{$env{'request.course.id'}}) {
2373: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 2374: } else {
1.620 albertel 2375: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 2376: }
1.620 albertel 2377: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 2378: &flushcourselogs();
2379: }
1.158 www 2380: }
2381:
2382: sub courseacclog {
2383: my $fnsymb=shift;
1.620 albertel 2384: unless ($env{'request.course.id'}) { return ''; }
2385: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 2386: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 2387: $what.=':POST';
1.583 matthew 2388: # FIXME: Probably ought to escape things....
1.800 albertel 2389: foreach my $key (keys(%env)) {
2390: if ($key=~/^form\.(.*)/) {
2391: $what.=':'.$1.'='.$env{$key};
1.158 www 2392: }
1.191 harris41 2393: }
1.583 matthew 2394: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
2395: # FIXME: We should not be depending on a form parameter that someone
2396: # editing lonsearchcat.pm might change in the future.
1.620 albertel 2397: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 2398: $what.= ':POST';
2399: # FIXME: Probably ought to escape things....
2400: foreach my $element ('courseexp','crsfulltext','crsrelated',
2401: 'crsdiscuss') {
1.620 albertel 2402: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 2403: }
2404: }
1.158 www 2405: }
2406: &courselog($what);
1.149 www 2407: }
2408:
1.185 www 2409: sub countacc {
2410: my $url=&declutter(shift);
1.458 matthew 2411: return if (! defined($url) || $url eq '');
1.620 albertel 2412: unless ($env{'request.course.id'}) { return ''; }
2413: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 2414: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 2415: $accesshash{$key}++;
1.185 www 2416: }
1.349 www 2417:
1.361 www 2418: sub linklog {
2419: my ($from,$to)=@_;
2420: $from=&declutter($from);
2421: $to=&declutter($to);
2422: $accesshash{$from.'___'.$to.'___comefrom'}=1;
2423: $accesshash{$to.'___'.$from.'___goto'}=1;
2424: }
2425:
1.349 www 2426: sub userrolelog {
2427: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 2428: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 2429: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 2430: ($trole=~/^ep/) || ($trole=~/^cr/) ||
2431: ($trole=~/^ta/)) {
1.350 www 2432: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2433: $userrolehash
2434: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 2435: =$tend.':'.$tstart;
1.662 raeburn 2436: }
1.898 albertel 2437: if (($env{'request.role'} =~ /dc\./) &&
2438: (($trole=~/^au/) || ($trole=~/^in/) ||
2439: ($trole=~/^cc/) || ($trole=~/^ep/) ||
2440: ($trole=~/^cr/) || ($trole=~/^ta/))) {
2441: $userrolehash
2442: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
2443: =$tend.':'.$tstart;
2444: }
1.662 raeburn 2445: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
2446: ($trole=~/^li/) || ($trole=~/^li/) ||
2447: ($trole=~/^au/) || ($trole=~/^dg/) ||
2448: ($trole=~/^sc/)) {
2449: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2450: $domainrolehash
2451: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
2452: = $tend.':'.$tstart;
2453: }
1.351 www 2454: }
2455:
2456: sub get_course_adv_roles {
1.948 raeburn 2457: my ($cid,$codes) = @_;
1.620 albertel 2458: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 2459: my %coursehash=&coursedescription($cid);
1.470 www 2460: my %nothide=();
1.800 albertel 2461: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 2462: if ($user !~ /:/) {
2463: $nothide{join(':',split(/[\@]/,$user))}=1;
2464: } else {
2465: $nothide{$user}=1;
2466: }
1.470 www 2467: }
1.351 www 2468: my %returnhash=();
2469: my %dumphash=
2470: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
2471: my $now=time;
1.800 albertel 2472: foreach my $entry (keys %dumphash) {
2473: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 2474: if (($tstart) && ($tstart<0)) { next; }
2475: if (($tend) && ($tend<$now)) { next; }
2476: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 2477: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 2478: if ($username eq '' || $domain eq '') { next; }
1.470 www 2479: if ((&privileged($username,$domain)) &&
2480: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 2481: if ($role eq 'cr') { next; }
1.948 raeburn 2482: if ($codes) {
2483: if ($section) { $role .= ':'.$section; }
2484: if ($returnhash{$role}) {
2485: $returnhash{$role}.=','.$username.':'.$domain;
2486: } else {
2487: $returnhash{$role}=$username.':'.$domain;
2488: }
1.351 www 2489: } else {
1.948 raeburn 2490: my $key=&plaintext($role);
2491: if ($section) { $key.=' (Section '.$section.')'; }
2492: if ($returnhash{$key}) {
2493: $returnhash{$key}.=','.$username.':'.$domain;
2494: } else {
2495: $returnhash{$key}=$username.':'.$domain;
2496: }
1.351 www 2497: }
1.948 raeburn 2498: }
1.400 www 2499: return %returnhash;
2500: }
2501:
2502: sub get_my_roles {
1.937 raeburn 2503: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 2504: unless (defined($uname)) { $uname=$env{'user.name'}; }
2505: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 2506: my (%dumphash,%nothide);
1.858 raeburn 2507: if ($context eq 'userroles') {
2508: %dumphash = &dump('roles',$udom,$uname);
2509: } else {
2510: %dumphash=
1.400 www 2511: &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 2512: if ($hidepriv) {
2513: my %coursehash=&coursedescription($udom.'_'.$uname);
2514: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
2515: if ($user !~ /:/) {
2516: $nothide{join(':',split(/[\@]/,$user))} = 1;
2517: } else {
2518: $nothide{$user} = 1;
2519: }
2520: }
2521: }
1.858 raeburn 2522: }
1.400 www 2523: my %returnhash=();
2524: my $now=time;
1.800 albertel 2525: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 2526: my ($role,$tend,$tstart);
2527: if ($context eq 'userroles') {
2528: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
2529: } else {
2530: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
2531: }
1.400 www 2532: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 2533: my $status = 'active';
1.939 raeburn 2534: if (($tend) && ($tend<=$now)) {
1.832 raeburn 2535: $status = 'previous';
2536: }
2537: if (($tstart) && ($now<$tstart)) {
2538: $status = 'future';
2539: }
2540: if (ref($types) eq 'ARRAY') {
2541: if (!grep(/^\Q$status\E$/,@{$types})) {
2542: next;
2543: }
2544: } else {
2545: if ($status ne 'active') {
2546: next;
2547: }
2548: }
1.867 raeburn 2549: my ($rolecode,$username,$domain,$section,$area);
2550: if ($context eq 'userroles') {
2551: ($area,$rolecode) = split(/_/,$entry);
2552: (undef,$domain,$username,$section) = split(/\//,$area);
2553: } else {
2554: ($role,$username,$domain,$section) = split(/\:/,$entry);
2555: }
1.832 raeburn 2556: if (ref($roledoms) eq 'ARRAY') {
2557: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
2558: next;
2559: }
2560: }
2561: if (ref($roles) eq 'ARRAY') {
2562: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 2563: if ($role =~ /^cr\//) {
2564: if (!grep(/^cr$/,@{$roles})) {
2565: next;
2566: }
2567: } else {
2568: next;
2569: }
1.832 raeburn 2570: }
1.867 raeburn 2571: }
1.937 raeburn 2572: if ($hidepriv) {
2573: if ((&privileged($username,$domain)) &&
2574: (!$nothide{$username.':'.$domain})) {
2575: next;
2576: }
2577: }
1.933 raeburn 2578: if ($withsec) {
2579: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
2580: $tstart.':'.$tend;
2581: } else {
2582: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
2583: }
1.832 raeburn 2584: }
1.373 www 2585: return %returnhash;
1.399 www 2586: }
2587:
2588: # ----------------------------------------------------- Frontpage Announcements
2589: #
2590: #
2591:
2592: sub postannounce {
2593: my ($server,$text)=@_;
1.844 albertel 2594: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 2595: unless ($text=~/\w/) { $text=''; }
2596: return &reply('setannounce:'.&escape($text),$server);
2597: }
2598:
2599: sub getannounce {
1.448 albertel 2600:
2601: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 2602: my $announcement='';
1.800 albertel 2603: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 2604: close($fh);
1.399 www 2605: if ($announcement=~/\w/) {
2606: return
2607: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 2608: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 2609: } else {
2610: return '';
2611: }
2612: } else {
2613: return '';
2614: }
1.351 www 2615: }
1.353 www 2616:
2617: # ---------------------------------------------------------- Course ID routines
2618: # Deal with domain's nohist_courseid.db files
2619: #
2620:
2621: sub courseidput {
1.921 raeburn 2622: my ($domain,$storehash,$coursehome,$caller) = @_;
2623: my $outcome;
2624: if ($caller eq 'timeonly') {
2625: my $cids = '';
2626: foreach my $item (keys(%$storehash)) {
2627: $cids.=&escape($item).'&';
2628: }
2629: $cids=~s/\&$//;
2630: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
2631: $coursehome);
2632: } else {
2633: my $items = '';
2634: foreach my $item (keys(%$storehash)) {
2635: $items.= &escape($item).'='.
2636: &freeze_escape($$storehash{$item}).'&';
2637: }
2638: $items=~s/\&$//;
2639: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
2640: $coursehome);
1.918 raeburn 2641: }
2642: if ($outcome eq 'unknown_cmd') {
2643: my $what;
2644: foreach my $cid (keys(%$storehash)) {
2645: $what .= &escape($cid).'=';
1.921 raeburn 2646: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 2647: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 2648: }
2649: $what =~ s/\:$/&/;
2650: }
2651: $what =~ s/\&$//;
2652: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
2653: } else {
2654: return $outcome;
2655: }
1.353 www 2656: }
2657:
2658: sub courseiddump {
1.921 raeburn 2659: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 2660: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
2661: $selfenrollonly)=@_;
1.918 raeburn 2662: my $as_hash = 1;
2663: my %returnhash;
2664: if (!$domfilter) { $domfilter=''; }
1.845 albertel 2665: my %libserv = &all_library();
2666: foreach my $tryserver (keys(%libserv)) {
2667: if ( ( $hostidflag == 1
2668: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
2669: || (!defined($hostidflag)) ) {
2670:
1.918 raeburn 2671: if (($domfilter eq '') ||
2672: (&host_domain($tryserver) eq $domfilter)) {
2673: my $rep =
2674: &reply('courseiddump:'.&host_domain($tryserver).':'.
2675: $sincefilter.':'.&escape($descfilter).':'.
2676: &escape($instcodefilter).':'.&escape($ownerfilter).
2677: ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947 raeburn 2678: ':'.&escape($regexp_ok).':'.$as_hash.':'.
2679: &escape($selfenrollonly),$tryserver);
1.918 raeburn 2680: my @pairs=split(/\&/,$rep);
2681: foreach my $item (@pairs) {
2682: my ($key,$value)=split(/\=/,$item,2);
2683: $key = &unescape($key);
2684: next if ($key =~ /^error: 2 /);
2685: my $result = &thaw_unescape($value);
2686: if (ref($result) eq 'HASH') {
2687: $returnhash{$key}=$result;
2688: } else {
1.921 raeburn 2689: my @responses = split(/:/,$value);
2690: my @items = ('description','inst_code','owner','type');
1.918 raeburn 2691: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 2692: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 2693: }
2694: }
1.353 www 2695: }
2696: }
2697: }
2698: }
2699: return %returnhash;
2700: }
2701:
1.658 raeburn 2702: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 2703:
2704: sub dcmailput {
1.685 raeburn 2705: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 2706: my $status = &Apache::lonnet::critical(
1.740 www 2707: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
2708: &escape($message),$server);
1.662 raeburn 2709: return $status;
2710: }
2711:
1.658 raeburn 2712: sub dcmaildump {
2713: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 2714: my %returnhash=();
1.846 albertel 2715:
2716: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 2717: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
2718: &escape($enddate).':';
2719: my @esc_senders=map { &escape($_)} @$senders;
2720: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 2721: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 2722: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 2723: if (($key) && ($value)) {
2724: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 2725: }
2726: }
2727: }
2728: return %returnhash;
2729: }
1.662 raeburn 2730: # ---------------------------------------------------------- Domain roles
2731:
2732: sub get_domain_roles {
2733: my ($dom,$roles,$startdate,$enddate)=@_;
2734: if (undef($startdate) || $startdate eq '') {
2735: $startdate = '.';
2736: }
2737: if (undef($enddate) || $enddate eq '') {
2738: $enddate = '.';
2739: }
1.922 raeburn 2740: my $rolelist;
2741: if (ref($roles) eq 'ARRAY') {
2742: $rolelist = join(':',@{$roles});
2743: }
1.662 raeburn 2744: my %personnel = ();
1.841 albertel 2745:
2746: my %servers = &get_servers($dom,'library');
2747: foreach my $tryserver (keys(%servers)) {
2748: %{$personnel{$tryserver}}=();
2749: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
2750: &escape($startdate).':'.
2751: &escape($enddate).':'.
2752: &escape($rolelist), $tryserver))) {
2753: my ($key,$value) = split(/\=/,$line,2);
2754: if (($key) && ($value)) {
2755: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
2756: }
2757: }
1.662 raeburn 2758: }
2759: return %personnel;
2760: }
1.658 raeburn 2761:
1.149 www 2762: # ----------------------------------------------------------- Check out an item
2763:
1.504 albertel 2764: sub get_first_access {
2765: my ($type,$argsymb)=@_;
1.790 albertel 2766: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 2767: if ($argsymb) { $symb=$argsymb; }
2768: my ($map,$id,$res)=&decode_symb($symb);
1.926 albertel 2769: if ($type eq 'course') {
2770: $res='course';
2771: } elsif ($type eq 'map') {
1.588 albertel 2772: $res=&symbread($map);
2773: } else {
2774: $res=$symb;
2775: }
2776: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
2777: return $times{"$courseid\0$res"};
1.504 albertel 2778: }
2779:
2780: sub set_first_access {
2781: my ($type)=@_;
1.790 albertel 2782: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 2783: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 2784: if ($type eq 'course') {
2785: $res='course';
2786: } elsif ($type eq 'map') {
1.588 albertel 2787: $res=&symbread($map);
2788: } else {
2789: $res=$symb;
2790: }
2791: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 2792: if (!$firstaccess) {
1.588 albertel 2793: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 2794: }
2795: return 'already_set';
1.504 albertel 2796: }
2797:
1.149 www 2798: sub checkout {
2799: my ($symb,$tuname,$tudom,$tcrsid)=@_;
2800: my $now=time;
2801: my $lonhost=$perlvar{'lonHostID'};
2802: my $infostr=&escape(
1.234 www 2803: 'CHECKOUTTOKEN&'.
1.149 www 2804: $tuname.'&'.
2805: $tudom.'&'.
2806: $tcrsid.'&'.
2807: $symb.'&'.
2808: $now.'&'.$ENV{'REMOTE_ADDR'});
2809: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 2810: if ($token=~/^error\:/) {
1.672 albertel 2811: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2812: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
2813: "</font>");
2814: return '';
2815: }
2816:
1.149 www 2817: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
2818: $token=~tr/a-z/A-Z/;
2819:
1.153 www 2820: my %infohash=('resource.0.outtoken' => $token,
2821: 'resource.0.checkouttime' => $now,
2822: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 2823:
2824: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2825: return '';
1.151 www 2826: } else {
1.672 albertel 2827: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2828: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
2829: "</font>");
1.149 www 2830: }
2831:
2832: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2833: &escape('Checkout '.$infostr.' - '.
2834: $token)) ne 'ok') {
2835: return '';
1.151 www 2836: } else {
1.672 albertel 2837: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 2838: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
2839: "</font>");
1.149 www 2840: }
1.151 www 2841: return $token;
1.149 www 2842: }
2843:
2844: # ------------------------------------------------------------ Check in an item
2845:
2846: sub checkin {
2847: my $token=shift;
1.150 www 2848: my $now=time;
2849: my ($ta,$tb,$lonhost)=split(/\*/,$token);
2850: $lonhost=~tr/A-Z/a-z/;
1.838 albertel 2851: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150 www 2852: $dtoken=~s/\W/\_/g;
1.234 www 2853: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 2854: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
2855:
1.154 www 2856: unless (($tuname) && ($tudom)) {
2857: &logthis('Check in '.$token.' ('.$dtoken.') failed');
2858: return '';
2859: }
2860:
2861: unless (&allowed('mgr',$tcrsid)) {
2862: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 2863: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 2864: return '';
2865: }
2866:
1.153 www 2867: my %infohash=('resource.0.intoken' => $token,
2868: 'resource.0.checkintime' => $now,
2869: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 2870:
2871: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
2872: return '';
2873: }
2874:
2875: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
2876: &escape('Checkin - '.$token)) ne 'ok') {
2877: return '';
2878: }
2879:
2880: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 2881: }
2882:
2883: # --------------------------------------------- Set Expire Date for Spreadsheet
2884:
2885: sub expirespread {
2886: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 2887: my $cid=$env{'request.course.id'};
1.110 www 2888: if ($cid) {
2889: my $now=time;
2890: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 2891: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
2892: $env{'course.'.$cid.'.num'}.
1.110 www 2893: ':nohist_expirationdates:'.
2894: &escape($key).'='.$now,
1.620 albertel 2895: $env{'course.'.$cid.'.home'})
1.110 www 2896: }
2897: return 'ok';
1.14 www 2898: }
2899:
1.109 www 2900: # ----------------------------------------------------- Devalidate Spreadsheets
2901:
2902: sub devalidate {
1.325 www 2903: my ($symb,$uname,$udom)=@_;
1.620 albertel 2904: my $cid=$env{'request.course.id'};
1.109 www 2905: if ($cid) {
1.391 matthew 2906: # delete the stored spreadsheets for
2907: # - the student level sheet of this user in course's homespace
2908: # - the assessment level sheet for this resource
2909: # for this user in user's homespace
1.553 albertel 2910: # - current conditional state info
1.325 www 2911: my $key=$uname.':'.$udom.':';
1.109 www 2912: my $status=
1.299 matthew 2913: &del('nohist_calculatedsheets',
1.391 matthew 2914: [$key.'studentcalc:'],
1.620 albertel 2915: $env{'course.'.$cid.'.domain'},
2916: $env{'course.'.$cid.'.num'})
1.133 albertel 2917: .' '.
2918: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 2919: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 2920: unless ($status eq 'ok ok') {
2921: &logthis('Could not devalidate spreadsheet '.
1.325 www 2922: $uname.' at '.$udom.' for '.
1.109 www 2923: $symb.': '.$status);
1.133 albertel 2924: }
1.553 albertel 2925: &delenv('user.state.'.$cid);
1.109 www 2926: }
2927: }
2928:
1.265 albertel 2929: sub get_scalar {
2930: my ($string,$end) = @_;
2931: my $value;
2932: if ($$string =~ s/^([^&]*?)($end)/$2/) {
2933: $value = $1;
2934: } elsif ($$string =~ s/^([^&]*?)&//) {
2935: $value = $1;
2936: }
2937: return &unescape($value);
2938: }
2939:
2940: sub array2str {
2941: my (@array) = @_;
2942: my $result=&arrayref2str(\@array);
2943: $result=~s/^__ARRAY_REF__//;
2944: $result=~s/__END_ARRAY_REF__$//;
2945: return $result;
2946: }
2947:
1.204 albertel 2948: sub arrayref2str {
2949: my ($arrayref) = @_;
1.265 albertel 2950: my $result='__ARRAY_REF__';
1.204 albertel 2951: foreach my $elem (@$arrayref) {
1.265 albertel 2952: if(ref($elem) eq 'ARRAY') {
2953: $result.=&arrayref2str($elem).'&';
2954: } elsif(ref($elem) eq 'HASH') {
2955: $result.=&hashref2str($elem).'&';
2956: } elsif(ref($elem)) {
2957: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 2958: } else {
2959: $result.=&escape($elem).'&';
2960: }
2961: }
2962: $result=~s/\&$//;
1.265 albertel 2963: $result .= '__END_ARRAY_REF__';
1.204 albertel 2964: return $result;
2965: }
2966:
1.168 albertel 2967: sub hash2str {
1.204 albertel 2968: my (%hash) = @_;
2969: my $result=&hashref2str(\%hash);
1.265 albertel 2970: $result=~s/^__HASH_REF__//;
2971: $result=~s/__END_HASH_REF__$//;
1.204 albertel 2972: return $result;
2973: }
2974:
2975: sub hashref2str {
2976: my ($hashref)=@_;
1.265 albertel 2977: my $result='__HASH_REF__';
1.800 albertel 2978: foreach my $key (sort(keys(%$hashref))) {
2979: if (ref($key) eq 'ARRAY') {
2980: $result.=&arrayref2str($key).'=';
2981: } elsif (ref($key) eq 'HASH') {
2982: $result.=&hashref2str($key).'=';
2983: } elsif (ref($key)) {
1.265 albertel 2984: $result.='=';
1.800 albertel 2985: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 2986: } else {
1.800 albertel 2987: if ($key) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 2988: }
2989:
1.800 albertel 2990: if(ref($hashref->{$key}) eq 'ARRAY') {
2991: $result.=&arrayref2str($hashref->{$key}).'&';
2992: } elsif(ref($hashref->{$key}) eq 'HASH') {
2993: $result.=&hashref2str($hashref->{$key}).'&';
2994: } elsif(ref($hashref->{$key})) {
1.265 albertel 2995: $result.='&';
1.800 albertel 2996: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 2997: } else {
1.800 albertel 2998: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 2999: }
3000: }
1.168 albertel 3001: $result=~s/\&$//;
1.265 albertel 3002: $result .= '__END_HASH_REF__';
1.168 albertel 3003: return $result;
3004: }
3005:
3006: sub str2hash {
1.265 albertel 3007: my ($string)=@_;
3008: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
3009: return %$hash;
3010: }
3011:
3012: sub str2hashref {
1.168 albertel 3013: my ($string) = @_;
1.265 albertel 3014:
3015: my %hash;
3016:
3017: if($string !~ /^__HASH_REF__/) {
3018: if (! ($string eq '' || !defined($string))) {
3019: $hash{'error'}='Not hash reference';
3020: }
3021: return (\%hash, $string);
3022: }
3023:
3024: $string =~ s/^__HASH_REF__//;
3025:
3026: while($string !~ /^__END_HASH_REF__/) {
3027: #key
3028: my $key='';
3029: if($string =~ /^__HASH_REF__/) {
3030: ($key, $string)=&str2hashref($string);
3031: if(defined($key->{'error'})) {
3032: $hash{'error'}='Bad data';
3033: return (\%hash, $string);
3034: }
3035: } elsif($string =~ /^__ARRAY_REF__/) {
3036: ($key, $string)=&str2arrayref($string);
3037: if($key->[0] eq 'Array reference error') {
3038: $hash{'error'}='Bad data';
3039: return (\%hash, $string);
3040: }
3041: } else {
3042: $string =~ s/^(.*?)=//;
1.267 albertel 3043: $key=&unescape($1);
1.265 albertel 3044: }
3045: $string =~ s/^=//;
3046:
3047: #value
3048: my $value='';
3049: if($string =~ /^__HASH_REF__/) {
3050: ($value, $string)=&str2hashref($string);
3051: if(defined($value->{'error'})) {
3052: $hash{'error'}='Bad data';
3053: return (\%hash, $string);
3054: }
3055: } elsif($string =~ /^__ARRAY_REF__/) {
3056: ($value, $string)=&str2arrayref($string);
3057: if($value->[0] eq 'Array reference error') {
3058: $hash{'error'}='Bad data';
3059: return (\%hash, $string);
3060: }
3061: } else {
3062: $value=&get_scalar(\$string,'__END_HASH_REF__');
3063: }
3064: $string =~ s/^&//;
3065:
3066: $hash{$key}=$value;
1.204 albertel 3067: }
1.265 albertel 3068:
3069: $string =~ s/^__END_HASH_REF__//;
3070:
3071: return (\%hash, $string);
1.204 albertel 3072: }
3073:
3074: sub str2array {
1.265 albertel 3075: my ($string)=@_;
3076: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
3077: return @$array;
3078: }
3079:
3080: sub str2arrayref {
1.204 albertel 3081: my ($string) = @_;
1.265 albertel 3082: my @array;
3083:
3084: if($string !~ /^__ARRAY_REF__/) {
3085: if (! ($string eq '' || !defined($string))) {
3086: $array[0]='Array reference error';
3087: }
3088: return (\@array, $string);
3089: }
3090:
3091: $string =~ s/^__ARRAY_REF__//;
3092:
3093: while($string !~ /^__END_ARRAY_REF__/) {
3094: my $value='';
3095: if($string =~ /^__HASH_REF__/) {
3096: ($value, $string)=&str2hashref($string);
3097: if(defined($value->{'error'})) {
3098: $array[0] ='Array reference error';
3099: return (\@array, $string);
3100: }
3101: } elsif($string =~ /^__ARRAY_REF__/) {
3102: ($value, $string)=&str2arrayref($string);
3103: if($value->[0] eq 'Array reference error') {
3104: $array[0] ='Array reference error';
3105: return (\@array, $string);
3106: }
3107: } else {
3108: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
3109: }
3110: $string =~ s/^&//;
3111:
3112: push(@array, $value);
1.191 harris41 3113: }
1.265 albertel 3114:
3115: $string =~ s/^__END_ARRAY_REF__//;
3116:
3117: return (\@array, $string);
1.168 albertel 3118: }
3119:
1.167 albertel 3120: # -------------------------------------------------------------------Temp Store
3121:
1.168 albertel 3122: sub tmpreset {
3123: my ($symb,$namespace,$domain,$stuname) = @_;
3124: if (!$symb) {
3125: $symb=&symbread();
1.620 albertel 3126: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3127: }
3128: $symb=escape($symb);
3129:
1.620 albertel 3130: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 3131: $namespace=~s/\//\_/g;
3132: $namespace=~s/\W//g;
3133:
1.620 albertel 3134: if (!$domain) { $domain=$env{'user.domain'}; }
3135: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3136: if ($domain eq 'public' && $stuname eq 'public') {
3137: $stuname=$ENV{'REMOTE_ADDR'};
3138: }
1.168 albertel 3139: my $path=$perlvar{'lonDaemons'}.'/tmp';
3140: my %hash;
3141: if (tie(%hash,'GDBM_File',
3142: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3143: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3144: foreach my $key (keys %hash) {
1.180 albertel 3145: if ($key=~ /:$symb/) {
1.168 albertel 3146: delete($hash{$key});
3147: }
3148: }
3149: }
3150: }
3151:
1.167 albertel 3152: sub tmpstore {
1.168 albertel 3153: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3154:
3155: if (!$symb) {
3156: $symb=&symbread();
1.620 albertel 3157: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3158: }
3159: $symb=escape($symb);
3160:
3161: if (!$namespace) {
3162: # I don't think we would ever want to store this for a course.
3163: # it seems this will only be used if we don't have a course.
1.620 albertel 3164: #$namespace=$env{'request.course.id'};
1.168 albertel 3165: #if (!$namespace) {
1.620 albertel 3166: $namespace=$env{'request.state'};
1.168 albertel 3167: #}
3168: }
3169: $namespace=~s/\//\_/g;
3170: $namespace=~s/\W//g;
1.620 albertel 3171: if (!$domain) { $domain=$env{'user.domain'}; }
3172: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3173: if ($domain eq 'public' && $stuname eq 'public') {
3174: $stuname=$ENV{'REMOTE_ADDR'};
3175: }
1.168 albertel 3176: my $now=time;
3177: my %hash;
3178: my $path=$perlvar{'lonDaemons'}.'/tmp';
3179: if (tie(%hash,'GDBM_File',
3180: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3181: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3182: $hash{"version:$symb"}++;
3183: my $version=$hash{"version:$symb"};
3184: my $allkeys='';
3185: foreach my $key (keys(%$storehash)) {
3186: $allkeys.=$key.':';
1.591 albertel 3187: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 3188: }
3189: $hash{"$version:$symb:timestamp"}=$now;
3190: $allkeys.='timestamp';
3191: $hash{"$version:keys:$symb"}=$allkeys;
3192: if (untie(%hash)) {
3193: return 'ok';
3194: } else {
3195: return "error:$!";
3196: }
3197: } else {
3198: return "error:$!";
3199: }
3200: }
1.167 albertel 3201:
1.168 albertel 3202: # -----------------------------------------------------------------Temp Restore
1.167 albertel 3203:
1.168 albertel 3204: sub tmprestore {
3205: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 3206:
1.168 albertel 3207: if (!$symb) {
3208: $symb=&symbread();
1.620 albertel 3209: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3210: }
3211: $symb=escape($symb);
3212:
1.620 albertel 3213: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 3214:
1.620 albertel 3215: if (!$domain) { $domain=$env{'user.domain'}; }
3216: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3217: if ($domain eq 'public' && $stuname eq 'public') {
3218: $stuname=$ENV{'REMOTE_ADDR'};
3219: }
1.168 albertel 3220: my %returnhash;
3221: $namespace=~s/\//\_/g;
3222: $namespace=~s/\W//g;
3223: my %hash;
3224: my $path=$perlvar{'lonDaemons'}.'/tmp';
3225: if (tie(%hash,'GDBM_File',
3226: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3227: &GDBM_READER(),0640)) {
1.168 albertel 3228: my $version=$hash{"version:$symb"};
3229: $returnhash{'version'}=$version;
3230: my $scope;
3231: for ($scope=1;$scope<=$version;$scope++) {
3232: my $vkeys=$hash{"$scope:keys:$symb"};
3233: my @keys=split(/:/,$vkeys);
3234: my $key;
3235: $returnhash{"$scope:keys"}=$vkeys;
3236: foreach $key (@keys) {
1.591 albertel 3237: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
3238: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 3239: }
3240: }
1.168 albertel 3241: if (!(untie(%hash))) {
3242: return "error:$!";
3243: }
3244: } else {
3245: return "error:$!";
3246: }
3247: return %returnhash;
1.167 albertel 3248: }
3249:
1.9 www 3250: # ----------------------------------------------------------------------- Store
3251:
3252: sub store {
1.124 www 3253: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3254: my $home='';
3255:
1.168 albertel 3256: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3257:
1.213 www 3258: $symb=&symbclean($symb);
1.122 albertel 3259: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 3260:
1.620 albertel 3261: if (!$domain) { $domain=$env{'user.domain'}; }
3262: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 3263:
3264: &devalidate($symb,$stuname,$domain);
1.109 www 3265:
3266: $symb=escape($symb);
1.187 www 3267: if (!$namespace) {
1.620 albertel 3268: unless ($namespace=$env{'request.course.id'}) {
1.187 www 3269: return '';
3270: }
3271: }
1.620 albertel 3272: if (!$home) { $home=$env{'user.home'}; }
1.447 www 3273:
3274: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
3275: $$storehash{'host'}=$perlvar{'lonHostID'};
3276:
1.12 www 3277: my $namevalue='';
1.800 albertel 3278: foreach my $key (keys(%$storehash)) {
3279: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 3280: }
1.12 www 3281: $namevalue=~s/\&$//;
1.187 www 3282: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 3283: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 3284: }
3285:
1.47 www 3286: # -------------------------------------------------------------- Critical Store
3287:
3288: sub cstore {
1.124 www 3289: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3290: my $home='';
3291:
1.168 albertel 3292: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3293:
1.213 www 3294: $symb=&symbclean($symb);
1.122 albertel 3295: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 3296:
1.620 albertel 3297: if (!$domain) { $domain=$env{'user.domain'}; }
3298: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 3299:
3300: &devalidate($symb,$stuname,$domain);
1.109 www 3301:
3302: $symb=escape($symb);
1.187 www 3303: if (!$namespace) {
1.620 albertel 3304: unless ($namespace=$env{'request.course.id'}) {
1.187 www 3305: return '';
3306: }
3307: }
1.620 albertel 3308: if (!$home) { $home=$env{'user.home'}; }
1.447 www 3309:
3310: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
3311: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 3312:
1.47 www 3313: my $namevalue='';
1.800 albertel 3314: foreach my $key (keys(%$storehash)) {
3315: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 3316: }
1.47 www 3317: $namevalue=~s/\&$//;
1.187 www 3318: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 3319: return critical
3320: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 3321: }
3322:
1.9 www 3323: # --------------------------------------------------------------------- Restore
3324:
3325: sub restore {
1.124 www 3326: my ($symb,$namespace,$domain,$stuname) = @_;
3327: my $home='';
3328:
1.168 albertel 3329: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3330:
1.122 albertel 3331: if (!$symb) {
3332: unless ($symb=escape(&symbread())) { return ''; }
3333: } else {
1.213 www 3334: $symb=&escape(&symbclean($symb));
1.122 albertel 3335: }
1.188 www 3336: if (!$namespace) {
1.620 albertel 3337: unless ($namespace=$env{'request.course.id'}) {
1.188 www 3338: return '';
3339: }
3340: }
1.620 albertel 3341: if (!$domain) { $domain=$env{'user.domain'}; }
3342: if (!$stuname) { $stuname=$env{'user.name'}; }
3343: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 3344: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
3345:
1.12 www 3346: my %returnhash=();
1.800 albertel 3347: foreach my $line (split(/\&/,$answer)) {
3348: my ($name,$value)=split(/\=/,$line);
1.591 albertel 3349: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 3350: }
1.75 www 3351: my $version;
3352: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 3353: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
3354: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 3355: }
1.75 www 3356: }
1.13 www 3357: return %returnhash;
1.34 www 3358: }
3359:
3360: # ---------------------------------------------------------- Course Description
3361:
3362: sub coursedescription {
1.731 albertel 3363: my ($courseid,$args)=@_;
1.34 www 3364: $courseid=~s/^\///;
1.49 www 3365: $courseid=~s/\_/\//g;
1.34 www 3366: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 3367: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 3368: my $normalid=$cdomain.'_'.$cnum;
3369: # need to always cache even if we get errors otherwise we keep
3370: # trying and trying and trying to get the course description.
3371: my %envhash=();
3372: my %returnhash=();
1.731 albertel 3373:
3374: my $expiretime=600;
3375: if ($env{'request.course.id'} eq $normalid) {
3376: $expiretime=120;
3377: }
3378:
3379: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
3380: if (!$args->{'freshen_cache'}
3381: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
3382: foreach my $key (keys(%env)) {
3383: next if ($key !~ /^\Q$prefix\E(.*)/);
3384: my ($setting) = $1;
3385: $returnhash{$setting} = $env{$key};
3386: }
3387: return %returnhash;
3388: }
3389:
3390: # get the data agin
3391: if (!$args->{'one_time'}) {
3392: $envhash{'course.'.$normalid.'.last_cache'}=time;
3393: }
1.811 albertel 3394:
1.34 www 3395: if ($chome ne 'no_host') {
1.302 albertel 3396: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 3397: if (!exists($returnhash{'con_lost'})) {
3398: $returnhash{'home'}= $chome;
3399: $returnhash{'domain'} = $cdomain;
3400: $returnhash{'num'} = $cnum;
1.741 raeburn 3401: if (!defined($returnhash{'type'})) {
3402: $returnhash{'type'} = 'Course';
3403: }
1.130 albertel 3404: while (my ($name,$value) = each %returnhash) {
1.53 www 3405: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 3406: }
1.270 www 3407: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 3408: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 3409: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 3410: $envhash{'course.'.$normalid.'.home'}=$chome;
3411: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
3412: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 3413: }
3414: }
1.731 albertel 3415: if (!$args->{'one_time'}) {
1.949 raeburn 3416: &appenv(\%envhash);
1.731 albertel 3417: }
1.302 albertel 3418: return %returnhash;
1.461 www 3419: }
3420:
3421: # -------------------------------------------------See if a user is privileged
3422:
3423: sub privileged {
3424: my ($username,$domain)=@_;
3425: my $rolesdump=&reply("dump:$domain:$username:roles",
3426: &homeserver($username,$domain));
3427: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
3428: my $now=time;
3429: if ($rolesdump ne '') {
1.800 albertel 3430: foreach my $entry (split(/&/,$rolesdump)) {
3431: if ($entry!~/^rolesdef_/) {
3432: my ($area,$role)=split(/=/,$entry);
1.461 www 3433: $area=~s/\_\w\w$//;
3434: my ($trole,$tend,$tstart)=split(/_/,$role);
3435: if (($trole eq 'dc') || ($trole eq 'su')) {
3436: my $active=1;
3437: if ($tend) {
3438: if ($tend<$now) { $active=0; }
3439: }
3440: if ($tstart) {
3441: if ($tstart>$now) { $active=0; }
3442: }
3443: if ($active) { return 1; }
3444: }
3445: }
3446: }
3447: }
3448: return 0;
1.9 www 3449: }
1.1 albertel 3450:
1.103 harris41 3451: # -------------------------------------------------------- Get user privileges
1.11 www 3452:
3453: sub rolesinit {
3454: my ($domain,$username,$authhost)=@_;
3455: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12 www 3456: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11 www 3457: my %allroles=();
1.678 raeburn 3458: my %allgroups=();
1.11 www 3459: my $now=time;
1.743 albertel 3460: my %userroles = ('user.login.time' => $now);
1.678 raeburn 3461: my $group_privs;
1.11 www 3462:
3463: if ($rolesdump ne '') {
1.800 albertel 3464: foreach my $entry (split(/&/,$rolesdump)) {
3465: if ($entry!~/^rolesdef_/) {
3466: my ($area,$role)=split(/=/,$entry);
1.587 albertel 3467: $area=~s/\_\w\w$//;
1.678 raeburn 3468: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 3469: if ($role=~/^cr/) {
1.807 albertel 3470: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
3471: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 3472: ($tend,$tstart)=split('_',$trest);
3473: } else {
3474: $trole=$role;
3475: }
1.678 raeburn 3476: } elsif ($role =~ m|^gr/|) {
3477: ($trole,$tend,$tstart) = split(/_/,$role);
3478: ($trole,$group_privs) = split(/\//,$trole);
3479: $group_privs = &unescape($group_privs);
1.587 albertel 3480: } else {
3481: ($trole,$tend,$tstart)=split(/_/,$role);
3482: }
1.743 albertel 3483: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
3484: $username);
3485: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 3486: if (($tend!=0) && ($tend<$now)) { $trole=''; }
3487: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 3488: if (($area ne '') && ($trole ne '')) {
1.347 albertel 3489: my $spec=$trole.'.'.$area;
3490: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
3491: if ($trole =~ /^cr\//) {
1.567 raeburn 3492: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 3493: } elsif ($trole eq 'gr') {
3494: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 3495: } else {
1.567 raeburn 3496: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 3497: }
1.12 www 3498: }
1.662 raeburn 3499: }
1.191 harris41 3500: }
1.743 albertel 3501: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
3502: $userroles{'user.adv'} = $adv;
3503: $userroles{'user.author'} = $author;
1.620 albertel 3504: $env{'user.adv'}=$adv;
1.11 www 3505: }
1.743 albertel 3506: return \%userroles;
1.11 www 3507: }
3508:
1.567 raeburn 3509: sub set_arearole {
3510: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
3511: # log the associated role with the area
3512: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 3513: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 3514: }
3515:
3516: sub custom_roleprivs {
3517: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
3518: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
3519: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 3520: if (&hostname($homsvr) ne '') {
1.567 raeburn 3521: my ($rdummy,$roledef)=
3522: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
3523: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
3524: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
3525: if (defined($syspriv)) {
3526: $$allroles{'cm./'}.=':'.$syspriv;
3527: $$allroles{$spec.'./'}.=':'.$syspriv;
3528: }
3529: if ($tdomain ne '') {
3530: if (defined($dompriv)) {
3531: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
3532: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
3533: }
3534: if (($trest ne '') && (defined($coursepriv))) {
3535: $$allroles{'cm.'.$area}.=':'.$coursepriv;
3536: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
3537: }
3538: }
3539: }
3540: }
3541: }
3542:
1.678 raeburn 3543: sub group_roleprivs {
3544: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
3545: my $access = 1;
3546: my $now = time;
3547: if (($tend!=0) && ($tend<$now)) { $access = 0; }
3548: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
3549: if ($access) {
1.811 albertel 3550: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 3551: $$allgroups{$course}{$group} .=':'.$group_privs;
3552: }
3553: }
1.567 raeburn 3554:
3555: sub standard_roleprivs {
3556: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
3557: if (defined($pr{$trole.':s'})) {
3558: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
3559: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
3560: }
3561: if ($tdomain ne '') {
3562: if (defined($pr{$trole.':d'})) {
3563: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3564: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
3565: }
3566: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
3567: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
3568: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
3569: }
3570: }
3571: }
3572:
3573: sub set_userprivs {
1.678 raeburn 3574: my ($userroles,$allroles,$allgroups) = @_;
1.567 raeburn 3575: my $author=0;
3576: my $adv=0;
1.678 raeburn 3577: my %grouproles = ();
3578: if (keys(%{$allgroups}) > 0) {
3579: foreach my $role (keys %{$allroles}) {
1.681 raeburn 3580: my ($trole,$area,$sec,$extendedarea);
1.881 raeburn 3581: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
1.678 raeburn 3582: $trole = $1;
3583: $area = $2;
1.681 raeburn 3584: $sec = $3;
3585: $extendedarea = $area.$sec;
3586: if (exists($$allgroups{$area})) {
3587: foreach my $group (keys(%{$$allgroups{$area}})) {
3588: my $spec = $trole.'.'.$extendedarea;
3589: $grouproles{$spec.'.'.$area.'/'.$group} =
3590: $$allgroups{$area}{$group};
1.678 raeburn 3591: }
3592: }
3593: }
3594: }
3595: }
1.800 albertel 3596: foreach my $group (keys(%grouproles)) {
3597: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 3598: }
1.800 albertel 3599: foreach my $role (keys(%{$allroles})) {
3600: my %thesepriv;
1.941 raeburn 3601: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 3602: foreach my $item (split(/:/,$$allroles{$role})) {
3603: if ($item ne '') {
3604: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 3605: if ($restrictions eq '') {
3606: $thesepriv{$privilege}='F';
3607: } elsif ($thesepriv{$privilege} ne 'F') {
3608: $thesepriv{$privilege}.=$restrictions;
3609: }
3610: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
3611: }
3612: }
3613: my $thesestr='';
1.800 albertel 3614: foreach my $priv (keys(%thesepriv)) {
3615: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
3616: }
3617: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 3618: }
3619: return ($author,$adv);
3620: }
3621:
1.12 www 3622: # --------------------------------------------------------------- get interface
3623:
3624: sub get {
1.131 albertel 3625: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 3626: my $items='';
1.800 albertel 3627: foreach my $item (@$storearr) {
3628: $items.=&escape($item).'&';
1.191 harris41 3629: }
1.12 www 3630: $items=~s/\&$//;
1.620 albertel 3631: if (!$udomain) { $udomain=$env{'user.domain'}; }
3632: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 3633: my $uhome=&homeserver($uname,$udomain);
3634:
1.133 albertel 3635: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 3636: my @pairs=split(/\&/,$rep);
1.273 albertel 3637: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
3638: return @pairs;
3639: }
1.15 www 3640: my %returnhash=();
1.42 www 3641: my $i=0;
1.800 albertel 3642: foreach my $item (@$storearr) {
3643: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 3644: $i++;
1.191 harris41 3645: }
1.15 www 3646: return %returnhash;
1.27 www 3647: }
3648:
3649: # --------------------------------------------------------------- del interface
3650:
3651: sub del {
1.133 albertel 3652: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 3653: my $items='';
1.800 albertel 3654: foreach my $item (@$storearr) {
3655: $items.=&escape($item).'&';
1.191 harris41 3656: }
1.27 www 3657: $items=~s/\&$//;
1.620 albertel 3658: if (!$udomain) { $udomain=$env{'user.domain'}; }
3659: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 3660: my $uhome=&homeserver($uname,$udomain);
3661:
3662: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 3663: }
3664:
3665: # -------------------------------------------------------------- dump interface
3666:
3667: sub dump {
1.755 albertel 3668: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
3669: if (!$udomain) { $udomain=$env{'user.domain'}; }
3670: if (!$uname) { $uname=$env{'user.name'}; }
3671: my $uhome=&homeserver($uname,$udomain);
3672: if ($regexp) {
3673: $regexp=&escape($regexp);
3674: } else {
3675: $regexp='.';
3676: }
3677: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3678: my @pairs=split(/\&/,$rep);
3679: my %returnhash=();
3680: foreach my $item (@pairs) {
3681: my ($key,$value)=split(/=/,$item,2);
3682: $key = &unescape($key);
3683: next if ($key =~ /^error: 2 /);
3684: $returnhash{$key}=&thaw_unescape($value);
3685: }
3686: return %returnhash;
1.407 www 3687: }
3688:
1.717 albertel 3689: # --------------------------------------------------------- dumpstore interface
3690:
3691: sub dumpstore {
3692: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 3693: if (!$udomain) { $udomain=$env{'user.domain'}; }
3694: if (!$uname) { $uname=$env{'user.name'}; }
3695: my $uhome=&homeserver($uname,$udomain);
3696: if ($regexp) {
3697: $regexp=&escape($regexp);
3698: } else {
3699: $regexp='.';
3700: }
3701: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
3702: my @pairs=split(/\&/,$rep);
3703: my %returnhash=();
3704: foreach my $item (@pairs) {
3705: my ($key,$value)=split(/=/,$item,2);
3706: next if ($key =~ /^error: 2 /);
3707: $returnhash{$key}=&thaw_unescape($value);
3708: }
3709: return %returnhash;
1.717 albertel 3710: }
3711:
1.407 www 3712: # -------------------------------------------------------------- keys interface
3713:
3714: sub getkeys {
3715: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 3716: if (!$udomain) { $udomain=$env{'user.domain'}; }
3717: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 3718: my $uhome=&homeserver($uname,$udomain);
3719: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
3720: my @keyarray=();
1.800 albertel 3721: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 3722: next if ($key =~ /^error: 2 /);
1.800 albertel 3723: push(@keyarray,&unescape($key));
1.407 www 3724: }
3725: return @keyarray;
1.318 matthew 3726: }
3727:
1.319 matthew 3728: # --------------------------------------------------------------- currentdump
3729: sub currentdump {
1.328 matthew 3730: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 3731: $courseid = $env{'request.course.id'} if (! defined($courseid));
3732: $sdom = $env{'user.domain'} if (! defined($sdom));
3733: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 3734: my $uhome = &homeserver($sname,$sdom);
3735: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 3736: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 3737: #
1.318 matthew 3738: my %returnhash=();
1.319 matthew 3739: #
3740: if ($rep eq "unknown_cmd") {
3741: # an old lond will not know currentdump
3742: # Do a dump and make it look like a currentdump
1.822 albertel 3743: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 3744: return if ($tmp[0] =~ /^(error:|no_such_host)/);
3745: my %hash = @tmp;
3746: @tmp=();
1.424 matthew 3747: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 3748: } else {
3749: my @pairs=split(/\&/,$rep);
1.800 albertel 3750: foreach my $pair (@pairs) {
3751: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 3752: my ($symb,$param) = split(/:/,$key);
3753: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 3754: &thaw_unescape($value);
1.319 matthew 3755: }
1.191 harris41 3756: }
1.12 www 3757: return %returnhash;
1.424 matthew 3758: }
3759:
3760: sub convert_dump_to_currentdump{
3761: my %hash = %{shift()};
3762: my %returnhash;
3763: # Code ripped from lond, essentially. The only difference
3764: # here is the unescaping done by lonnet::dump(). Conceivably
3765: # we might run in to problems with parameter names =~ /^v\./
3766: while (my ($key,$value) = each(%hash)) {
3767: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 3768: $symb = &unescape($symb);
3769: $param = &unescape($param);
1.424 matthew 3770: next if ($v eq 'version' || $symb eq 'keys');
3771: next if (exists($returnhash{$symb}) &&
3772: exists($returnhash{$symb}->{$param}) &&
3773: $returnhash{$symb}->{'v.'.$param} > $v);
3774: $returnhash{$symb}->{$param}=$value;
3775: $returnhash{$symb}->{'v.'.$param}=$v;
3776: }
3777: #
3778: # Remove all of the keys in the hashes which keep track of
3779: # the version of the parameter.
3780: while (my ($symb,$param_hash) = each(%returnhash)) {
3781: # use a foreach because we are going to delete from the hash.
3782: foreach my $key (keys(%$param_hash)) {
3783: delete($param_hash->{$key}) if ($key =~ /^v\./);
3784: }
3785: }
3786: return \%returnhash;
1.12 www 3787: }
3788:
1.627 albertel 3789: # ------------------------------------------------------ critical inc interface
3790:
3791: sub cinc {
3792: return &inc(@_,'critical');
3793: }
3794:
1.449 matthew 3795: # --------------------------------------------------------------- inc interface
3796:
3797: sub inc {
1.627 albertel 3798: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 3799: if (!$udomain) { $udomain=$env{'user.domain'}; }
3800: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 3801: my $uhome=&homeserver($uname,$udomain);
3802: my $items='';
3803: if (! ref($store)) {
3804: # got a single value, so use that instead
3805: $items = &escape($store).'=&';
3806: } elsif (ref($store) eq 'SCALAR') {
3807: $items = &escape($$store).'=&';
3808: } elsif (ref($store) eq 'ARRAY') {
3809: $items = join('=&',map {&escape($_);} @{$store});
3810: } elsif (ref($store) eq 'HASH') {
3811: while (my($key,$value) = each(%{$store})) {
3812: $items.= &escape($key).'='.&escape($value).'&';
3813: }
3814: }
3815: $items=~s/\&$//;
1.627 albertel 3816: if ($critical) {
3817: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
3818: } else {
3819: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
3820: }
1.449 matthew 3821: }
3822:
1.12 www 3823: # --------------------------------------------------------------- put interface
3824:
3825: sub put {
1.134 albertel 3826: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 3827: if (!$udomain) { $udomain=$env{'user.domain'}; }
3828: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 3829: my $uhome=&homeserver($uname,$udomain);
1.12 www 3830: my $items='';
1.800 albertel 3831: foreach my $item (keys(%$storehash)) {
3832: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 3833: }
1.12 www 3834: $items=~s/\&$//;
1.134 albertel 3835: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 3836: }
3837:
1.631 albertel 3838: # ------------------------------------------------------------ newput interface
3839:
3840: sub newput {
3841: my ($namespace,$storehash,$udomain,$uname)=@_;
3842: if (!$udomain) { $udomain=$env{'user.domain'}; }
3843: if (!$uname) { $uname=$env{'user.name'}; }
3844: my $uhome=&homeserver($uname,$udomain);
3845: my $items='';
3846: foreach my $key (keys(%$storehash)) {
3847: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
3848: }
3849: $items=~s/\&$//;
3850: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
3851: }
3852:
3853: # --------------------------------------------------------- putstore interface
3854:
1.524 raeburn 3855: sub putstore {
1.715 albertel 3856: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 3857: if (!$udomain) { $udomain=$env{'user.domain'}; }
3858: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 3859: my $uhome=&homeserver($uname,$udomain);
3860: my $items='';
1.715 albertel 3861: foreach my $key (keys(%$storehash)) {
3862: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 3863: }
1.715 albertel 3864: $items=~s/\&$//;
1.716 albertel 3865: my $esc_symb=&escape($symb);
3866: my $esc_v=&escape($version);
1.715 albertel 3867: my $reply =
1.716 albertel 3868: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 3869: $uhome);
3870: if ($reply eq 'unknown_cmd') {
1.716 albertel 3871: # gfall back to way things use to be done
1.715 albertel 3872: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
3873: $uname);
1.524 raeburn 3874: }
1.715 albertel 3875: return $reply;
3876: }
3877:
3878: sub old_putstore {
1.716 albertel 3879: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
3880: if (!$udomain) { $udomain=$env{'user.domain'}; }
3881: if (!$uname) { $uname=$env{'user.name'}; }
3882: my $uhome=&homeserver($uname,$udomain);
3883: my %newstorehash;
1.800 albertel 3884: foreach my $item (keys(%$storehash)) {
3885: my $key = $version.':'.&escape($symb).':'.$item;
3886: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 3887: }
3888: my $items='';
3889: my %allitems = ();
1.800 albertel 3890: foreach my $item (keys(%newstorehash)) {
3891: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 3892: my $key = $1.':keys:'.$2;
3893: $allitems{$key} .= $3.':';
3894: }
1.800 albertel 3895: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 3896: }
1.800 albertel 3897: foreach my $item (keys(%allitems)) {
3898: $allitems{$item} =~ s/\:$//;
3899: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 3900: }
3901: $items=~s/\&$//;
3902: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 3903: }
3904:
1.47 www 3905: # ------------------------------------------------------ critical put interface
3906:
3907: sub cput {
1.134 albertel 3908: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 3909: if (!$udomain) { $udomain=$env{'user.domain'}; }
3910: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 3911: my $uhome=&homeserver($uname,$udomain);
1.47 www 3912: my $items='';
1.800 albertel 3913: foreach my $item (keys(%$storehash)) {
3914: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 3915: }
1.47 www 3916: $items=~s/\&$//;
1.134 albertel 3917: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3918: }
3919:
3920: # -------------------------------------------------------------- eget interface
3921:
3922: sub eget {
1.133 albertel 3923: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 3924: my $items='';
1.800 albertel 3925: foreach my $item (@$storearr) {
3926: $items.=&escape($item).'&';
1.191 harris41 3927: }
1.12 www 3928: $items=~s/\&$//;
1.620 albertel 3929: if (!$udomain) { $udomain=$env{'user.domain'}; }
3930: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 3931: my $uhome=&homeserver($uname,$udomain);
3932: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 3933: my @pairs=split(/\&/,$rep);
3934: my %returnhash=();
1.42 www 3935: my $i=0;
1.800 albertel 3936: foreach my $item (@$storearr) {
3937: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 3938: $i++;
1.191 harris41 3939: }
1.12 www 3940: return %returnhash;
3941: }
3942:
1.667 albertel 3943: # ------------------------------------------------------------ tmpput interface
3944: sub tmpput {
1.802 raeburn 3945: my ($storehash,$server,$context)=@_;
1.667 albertel 3946: my $items='';
1.800 albertel 3947: foreach my $item (keys(%$storehash)) {
3948: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 3949: }
3950: $items=~s/\&$//;
1.802 raeburn 3951: if (defined($context)) {
3952: $items .= ':'.&escape($context);
3953: }
1.667 albertel 3954: return &reply("tmpput:$items",$server);
3955: }
3956:
3957: # ------------------------------------------------------------ tmpget interface
3958: sub tmpget {
1.688 albertel 3959: my ($token,$server)=@_;
3960: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3961: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 3962: my %returnhash;
3963: foreach my $item (split(/\&/,$rep)) {
3964: my ($key,$value)=split(/=/,$item);
1.951 ! raeburn 3965: next if ($key =~ /^error: 2 /);
1.667 albertel 3966: $returnhash{&unescape($key)}=&thaw_unescape($value);
3967: }
3968: return %returnhash;
3969: }
3970:
1.688 albertel 3971: # ------------------------------------------------------------ tmpget interface
3972: sub tmpdel {
3973: my ($token,$server)=@_;
3974: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
3975: return &reply("tmpdel:$token",$server);
3976: }
3977:
1.765 albertel 3978: # -------------------------------------------------- portfolio access checking
3979:
3980: sub portfolio_access {
1.766 albertel 3981: my ($requrl) = @_;
1.765 albertel 3982: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
3983: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 3984: if ($result) {
3985: my %setters;
3986: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
3987: my ($startblock,$endblock) =
3988: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
3989: if ($startblock && $endblock) {
3990: return 'B';
3991: }
3992: } else {
3993: my ($startblock,$endblock) =
3994: &Apache::loncommon::blockcheck(\%setters,'port');
3995: if ($startblock && $endblock) {
3996: return 'B';
3997: }
3998: }
3999: }
1.765 albertel 4000: if ($result eq 'ok') {
1.766 albertel 4001: return 'F';
1.765 albertel 4002: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 4003: return 'A';
1.765 albertel 4004: }
1.766 albertel 4005: return '';
1.765 albertel 4006: }
4007:
4008: sub get_portfolio_access {
1.767 albertel 4009: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
4010:
4011: if (!ref($access_hash)) {
4012: my $current_perms = &get_portfile_permissions($udom,$unum);
4013: my %access_controls = &get_access_controls($current_perms,$group,
4014: $file_name);
4015: $access_hash = $access_controls{$file_name};
4016: }
4017:
1.765 albertel 4018: my ($public,$guest,@domains,@users,@courses,@groups);
4019: my $now = time;
4020: if (ref($access_hash) eq 'HASH') {
4021: foreach my $key (keys(%{$access_hash})) {
4022: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
4023: if ($start > $now) {
4024: next;
4025: }
4026: if ($end && $end<$now) {
4027: next;
4028: }
4029: if ($scope eq 'public') {
4030: $public = $key;
4031: last;
4032: } elsif ($scope eq 'guest') {
4033: $guest = $key;
4034: } elsif ($scope eq 'domains') {
4035: push(@domains,$key);
4036: } elsif ($scope eq 'users') {
4037: push(@users,$key);
4038: } elsif ($scope eq 'course') {
4039: push(@courses,$key);
4040: } elsif ($scope eq 'group') {
4041: push(@groups,$key);
4042: }
4043: }
4044: if ($public) {
4045: return 'ok';
4046: }
4047: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4048: if ($guest) {
4049: return $guest;
4050: }
4051: } else {
4052: if (@domains > 0) {
4053: foreach my $domkey (@domains) {
4054: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
4055: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
4056: return 'ok';
4057: }
4058: }
4059: }
4060: }
4061: if (@users > 0) {
4062: foreach my $userkey (@users) {
1.865 raeburn 4063: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
4064: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
4065: if (ref($item) eq 'HASH') {
4066: if (($item->{'uname'} eq $env{'user.name'}) &&
4067: ($item->{'udom'} eq $env{'user.domain'})) {
4068: return 'ok';
4069: }
4070: }
4071: }
4072: }
1.765 albertel 4073: }
4074: }
4075: my %roleshash;
4076: my @courses_and_groups = @courses;
4077: push(@courses_and_groups,@groups);
4078: if (@courses_and_groups > 0) {
4079: my (%allgroups,%allroles);
4080: my ($start,$end,$role,$sec,$group);
4081: foreach my $envkey (%env) {
1.811 albertel 4082: if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 4083: my $cid = $2.'_'.$3;
4084: if ($1 eq 'gr') {
4085: $group = $4;
4086: $allgroups{$cid}{$group} = $env{$envkey};
4087: } else {
4088: if ($4 eq '') {
4089: $sec = 'none';
4090: } else {
4091: $sec = $4;
4092: }
4093: $allroles{$cid}{$1}{$sec} = $env{$envkey};
4094: }
1.811 albertel 4095: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 4096: my $cid = $2.'_'.$3;
4097: if ($4 eq '') {
4098: $sec = 'none';
4099: } else {
4100: $sec = $4;
4101: }
4102: $allroles{$cid}{$1}{$sec} = $env{$envkey};
4103: }
4104: }
4105: if (keys(%allroles) == 0) {
4106: return;
4107: }
4108: foreach my $key (@courses_and_groups) {
4109: my %content = %{$$access_hash{$key}};
4110: my $cnum = $content{'number'};
4111: my $cdom = $content{'domain'};
4112: my $cid = $cdom.'_'.$cnum;
4113: if (!exists($allroles{$cid})) {
4114: next;
4115: }
4116: foreach my $role_id (keys(%{$content{'roles'}})) {
4117: my @sections = @{$content{'roles'}{$role_id}{'section'}};
4118: my @groups = @{$content{'roles'}{$role_id}{'group'}};
4119: my @status = @{$content{'roles'}{$role_id}{'access'}};
4120: my @roles = @{$content{'roles'}{$role_id}{'role'}};
4121: foreach my $role (keys(%{$allroles{$cid}})) {
4122: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
4123: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
4124: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
4125: if (grep/^all$/,@sections) {
4126: return 'ok';
4127: } else {
4128: if (grep/^$sec$/,@sections) {
4129: return 'ok';
4130: }
4131: }
4132: }
4133: }
4134: if (keys(%{$allgroups{$cid}}) == 0) {
4135: if (grep/^none$/,@groups) {
4136: return 'ok';
4137: }
4138: } else {
4139: if (grep/^all$/,@groups) {
4140: return 'ok';
4141: }
4142: foreach my $group (keys(%{$allgroups{$cid}})) {
4143: if (grep/^$group$/,@groups) {
4144: return 'ok';
4145: }
4146: }
4147: }
4148: }
4149: }
4150: }
4151: }
4152: }
4153: if ($guest) {
4154: return $guest;
4155: }
4156: }
4157: }
4158: return;
4159: }
4160:
4161: sub course_group_datechecker {
4162: my ($dates,$now,$status) = @_;
4163: my ($start,$end) = split(/\./,$dates);
4164: if (!$start && !$end) {
4165: return 'ok';
4166: }
4167: if (grep/^active$/,@{$status}) {
4168: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
4169: return 'ok';
4170: }
4171: }
4172: if (grep/^previous$/,@{$status}) {
4173: if ($end > $now ) {
4174: return 'ok';
4175: }
4176: }
4177: if (grep/^future$/,@{$status}) {
4178: if ($start > $now) {
4179: return 'ok';
4180: }
4181: }
4182: return;
4183: }
4184:
4185: sub parse_portfolio_url {
4186: my ($url) = @_;
4187:
4188: my ($type,$udom,$unum,$group,$file_name);
4189:
1.823 albertel 4190: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 4191: $type = 1;
4192: $udom = $1;
4193: $unum = $2;
4194: $file_name = $3;
1.823 albertel 4195: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 4196: $type = 2;
4197: $udom = $1;
4198: $unum = $2;
4199: $group = $3;
4200: $file_name = $3.'/'.$4;
4201: }
4202: if (wantarray) {
4203: return ($type,$udom,$unum,$file_name,$group);
4204: }
4205: return $type;
4206: }
4207:
4208: sub is_portfolio_url {
4209: my ($url) = @_;
4210: return scalar(&parse_portfolio_url($url));
4211: }
4212:
1.798 raeburn 4213: sub is_portfolio_file {
4214: my ($file) = @_;
1.820 raeburn 4215: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 4216: return 1;
4217: }
4218: return;
4219: }
4220:
4221:
1.341 www 4222: # ---------------------------------------------- Custom access rule evaluation
4223:
4224: sub customaccess {
4225: my ($priv,$uri)=@_;
1.807 albertel 4226: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 4227: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 4228: $udom = &LONCAPA::clean_domain($udom);
4229: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 4230: my $access=0;
1.800 albertel 4231: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 4232: my ($effect,$realm,$role,$type)=split(/\:/,$right);
4233: if ($type eq 'user') {
4234: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 4235: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 4236: if ($tdom) {
4237: if ($tdom ne $env{'user.domain'}) { next; }
4238: }
1.896 albertel 4239: if ($tuname) {
4240: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 4241: }
4242: $access=($effect eq 'allow');
4243: last;
4244: }
4245: } else {
4246: if ($role) {
4247: if ($role ne $urole) { next; }
4248: }
4249: foreach my $scope (split(/\s*\,\s*/,$realm)) {
4250: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
4251: if ($tdom) {
4252: if ($tdom ne $udom) { next; }
4253: }
4254: if ($tcrs) {
4255: if ($tcrs ne $ucrs) { next; }
4256: }
4257: if ($tsec) {
4258: if ($tsec ne $usec) { next; }
4259: }
4260: $access=($effect eq 'allow');
4261: last;
4262: }
4263: if ($realm eq '' && $role eq '') {
4264: $access=($effect eq 'allow');
4265: }
1.402 bowersj2 4266: }
1.341 www 4267: }
4268: return $access;
4269: }
4270:
1.103 harris41 4271: # ------------------------------------------------- Check for a user privilege
1.12 www 4272:
4273: sub allowed {
1.810 raeburn 4274: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 4275: my $ver_orguri=$uri;
1.439 www 4276: $uri=&deversion($uri);
1.152 www 4277: my $orguri=$uri;
1.52 www 4278: $uri=&declutter($uri);
1.809 raeburn 4279:
1.810 raeburn 4280: if ($priv eq 'evb') {
4281: # Evade communication block restrictions for specified role in a course
4282: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
4283: return $1;
4284: } else {
4285: return;
4286: }
4287: }
4288:
1.620 albertel 4289: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 4290: # Free bre access to adm and meta resources
1.775 albertel 4291: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 4292: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
4293: && ($priv eq 'bre')) {
1.14 www 4294: return 'F';
1.159 www 4295: }
4296:
1.545 banghart 4297: # Free bre access to user's own portfolio contents
1.714 raeburn 4298: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 4299: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 4300: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 4301: my %setters;
4302: my ($startblock,$endblock) =
4303: &Apache::loncommon::blockcheck(\%setters,'port');
4304: if ($startblock && $endblock) {
4305: return 'B';
4306: } else {
4307: return 'F';
4308: }
1.545 banghart 4309: }
4310:
1.762 raeburn 4311: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 4312: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
4313: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
4314: if (exists($env{'request.course.id'})) {
4315: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
4316: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
4317: if (($domain eq $cdom) && ($name eq $cnum)) {
4318: my $courseprivid=$env{'request.course.id'};
4319: $courseprivid=~s/\_/\//;
4320: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
4321: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
4322: return $1;
1.762 raeburn 4323: } else {
4324: if ($env{'request.course.sec'}) {
4325: $courseprivid.='/'.$env{'request.course.sec'};
4326: }
4327: if ($env{'user.priv.'.$env{'request.role'}.'./'.
4328: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
4329: return $2;
4330: }
1.714 raeburn 4331: }
4332: }
4333: }
4334: }
4335:
1.159 www 4336: # Free bre to public access
4337:
4338: if ($priv eq 'bre') {
1.238 www 4339: my $copyright=&metadata($uri,'copyright');
1.620 albertel 4340: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 4341: return 'F';
4342: }
1.238 www 4343: if ($copyright eq 'priv') {
4344: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 4345: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 4346: return '';
4347: }
4348: }
4349: if ($copyright eq 'domain') {
4350: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 4351: unless (($env{'user.domain'} eq $1) ||
4352: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 4353: return '';
4354: }
1.262 matthew 4355: }
1.620 albertel 4356: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 4357: # Library role, so allow browsing of resources in this domain.
4358: return 'F';
1.238 www 4359: }
1.341 www 4360: if ($copyright eq 'custom') {
4361: unless (&customaccess($priv,$uri)) { return ''; }
4362: }
1.14 www 4363: }
1.264 matthew 4364: # Domain coordinator is trying to create a course
1.620 albertel 4365: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 4366: # uri is the requested domain in this case.
4367: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 4368: # a role of dc for the domain in question.
1.620 albertel 4369: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 4370: }
1.29 www 4371:
1.52 www 4372: my $thisallowed='';
4373: my $statecond=0;
4374: my $courseprivid='';
4375:
4376: # Course
4377:
1.620 albertel 4378: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52 www 4379: $thisallowed.=$1;
4380: }
1.29 www 4381:
1.52 www 4382: # Domain
4383:
1.620 albertel 4384: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 4385: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 4386: $thisallowed.=$1;
4387: }
1.52 www 4388:
4389: # Course: uri itself is a course
1.66 www 4390: my $courseuri=$uri;
4391: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 4392: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 4393:
1.620 albertel 4394: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 4395: =~/\Q$priv\E\&([^\:]*)/) {
1.12 www 4396: $thisallowed.=$1;
4397: }
1.29 www 4398:
1.665 albertel 4399: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 4400: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 4401: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 4402: $thisallowed='';
1.671 raeburn 4403: my ($match)=&is_on_map($uri);
4404: if ($match) {
4405: if ($env{'user.priv.'.$env{'request.role'}.'./'}
4406: =~/\Q$priv\E\&([^\:]*)/) {
4407: $thisallowed.=$1;
4408: }
4409: } else {
1.705 albertel 4410: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 4411: if ($refuri) {
4412: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 4413: $thisallowed='F';
1.671 raeburn 4414: } else {
4415: $refuri=&declutter($refuri);
4416: my ($match) = &is_on_map($refuri);
4417: if ($match) {
4418: $thisallowed='F';
4419: }
1.669 raeburn 4420: }
1.671 raeburn 4421: }
4422: }
1.314 www 4423: }
1.492 albertel 4424:
1.766 albertel 4425: if ($priv eq 'bre'
4426: && $thisallowed ne 'F'
4427: && $thisallowed ne '2'
4428: && &is_portfolio_url($uri)) {
4429: $thisallowed = &portfolio_access($uri);
4430: }
4431:
1.52 www 4432: # Full access at system, domain or course-wide level? Exit.
1.29 www 4433:
4434: if ($thisallowed=~/F/) {
4435: return 'F';
4436: }
4437:
1.52 www 4438: # If this is generating or modifying users, exit with special codes
1.29 www 4439:
1.643 www 4440: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
4441: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 4442: my ($audom,$auname)=split('/',$uri);
1.643 www 4443: # no author name given, so this just checks on the general right to make a co-author in this domain
4444: unless ($auname) { return $thisallowed; }
4445: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 4446: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
4447: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
4448: ($audom ne $env{'request.role.domain'}))) { return ''; }
4449: }
1.52 www 4450: return $thisallowed;
4451: }
4452: #
1.103 harris41 4453: # Gathered so far: system, domain and course wide privileges
1.52 www 4454: #
4455: # Course: See if uri or referer is an individual resource that is part of
4456: # the course
4457:
1.620 albertel 4458: if ($env{'request.course.id'}) {
1.232 www 4459:
1.620 albertel 4460: $courseprivid=$env{'request.course.id'};
4461: if ($env{'request.course.sec'}) {
4462: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 4463: }
4464: $courseprivid=~s/\_/\//;
4465: my $checkreferer=1;
1.232 www 4466: my ($match,$cond)=&is_on_map($uri);
4467: if ($match) {
4468: $statecond=$cond;
1.620 albertel 4469: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 4470: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 4471: $thisallowed.=$1;
4472: $checkreferer=0;
4473: }
1.29 www 4474: }
1.83 www 4475:
1.148 www 4476: if ($checkreferer) {
1.620 albertel 4477: my $refuri=$env{'httpref.'.$orguri};
1.148 www 4478: unless ($refuri) {
1.800 albertel 4479: foreach my $key (keys(%env)) {
4480: if ($key=~/^httpref\..*\*/) {
4481: my $pattern=$key;
1.156 www 4482: $pattern=~s/^httpref\.\/res\///;
1.148 www 4483: $pattern=~s/\*/\[\^\/\]\+/g;
4484: $pattern=~s/\//\\\//g;
1.152 www 4485: if ($orguri=~/$pattern/) {
1.800 albertel 4486: $refuri=$env{$key};
1.148 www 4487: }
4488: }
1.191 harris41 4489: }
1.148 www 4490: }
1.232 www 4491:
1.148 www 4492: if ($refuri) {
1.152 www 4493: $refuri=&declutter($refuri);
1.232 www 4494: my ($match,$cond)=&is_on_map($refuri);
4495: if ($match) {
4496: my $refstatecond=$cond;
1.620 albertel 4497: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 4498: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 4499: $thisallowed.=$1;
1.53 www 4500: $uri=$refuri;
4501: $statecond=$refstatecond;
1.52 www 4502: }
4503: }
1.148 www 4504: }
1.29 www 4505: }
1.52 www 4506: }
1.29 www 4507:
1.52 www 4508: #
1.103 harris41 4509: # Gathered now: all privileges that could apply, and condition number
1.52 www 4510: #
4511: #
4512: # Full or no access?
4513: #
1.29 www 4514:
1.52 www 4515: if ($thisallowed=~/F/) {
4516: return 'F';
4517: }
1.29 www 4518:
1.52 www 4519: unless ($thisallowed) {
4520: return '';
4521: }
1.29 www 4522:
1.52 www 4523: # Restrictions exist, deal with them
4524: #
4525: # C:according to course preferences
4526: # R:according to resource settings
4527: # L:unless locked
4528: # X:according to user session state
4529: #
4530:
4531: # Possibly locked functionality, check all courses
1.54 www 4532: # Locks might take effect only after 10 minutes cache expiration for other
4533: # courses, and 2 minutes for current course
1.52 www 4534:
4535: my $envkey;
4536: if ($thisallowed=~/L/) {
1.620 albertel 4537: foreach $envkey (keys %env) {
1.54 www 4538: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
4539: my $courseid=$2;
4540: my $roleid=$1.'.'.$2;
1.92 www 4541: $courseid=~s/^\///;
1.54 www 4542: my $expiretime=600;
1.620 albertel 4543: if ($env{'request.role'} eq $roleid) {
1.54 www 4544: $expiretime=120;
4545: }
4546: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
4547: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 4548: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 4549: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 4550: }
1.620 albertel 4551: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
4552: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
4553: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
4554: &log($env{'user.domain'},$env{'user.name'},
4555: $env{'user.home'},
1.57 www 4556: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 4557: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 4558: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 4559: return '';
4560: }
4561: }
1.620 albertel 4562: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
4563: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
4564: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
4565: &log($env{'user.domain'},$env{'user.name'},
4566: $env{'user.home'},
1.57 www 4567: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 4568: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 4569: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 4570: return '';
4571: }
4572: }
4573: }
1.29 www 4574: }
1.52 www 4575: }
4576:
4577: #
4578: # Rest of the restrictions depend on selected course
4579: #
4580:
1.620 albertel 4581: unless ($env{'request.course.id'}) {
1.766 albertel 4582: if ($thisallowed eq 'A') {
4583: return 'A';
1.814 raeburn 4584: } elsif ($thisallowed eq 'B') {
4585: return 'B';
1.766 albertel 4586: } else {
4587: return '1';
4588: }
1.52 www 4589: }
1.29 www 4590:
1.52 www 4591: #
4592: # Now user is definitely in a course
4593: #
1.53 www 4594:
4595:
4596: # Course preferences
4597:
4598: if ($thisallowed=~/C/) {
1.620 albertel 4599: my $rolecode=(split(/\./,$env{'request.role'}))[0];
4600: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
4601: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 4602: =~/\Q$rolecode\E/) {
1.689 albertel 4603: if ($priv ne 'pch') {
4604: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4605: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
4606: $env{'request.course.id'});
4607: }
1.237 www 4608: return '';
4609: }
4610:
1.620 albertel 4611: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 4612: =~/\Q$unamedom\E/) {
1.689 albertel 4613: if ($priv ne 'pch') {
4614: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
4615: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
4616: $env{'request.course.id'});
4617: }
1.54 www 4618: return '';
4619: }
1.53 www 4620: }
4621:
4622: # Resource preferences
4623:
4624: if ($thisallowed=~/R/) {
1.620 albertel 4625: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 4626: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689 albertel 4627: if ($priv ne 'pch') {
4628: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
4629: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
4630: }
4631: return '';
1.54 www 4632: }
1.53 www 4633: }
1.30 www 4634:
1.246 www 4635: # Restricted by state or randomout?
1.30 www 4636:
1.52 www 4637: if ($thisallowed=~/X/) {
1.620 albertel 4638: if ($env{'acc.randomout'}) {
1.579 albertel 4639: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 4640: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 4641: return '';
4642: }
1.247 www 4643: }
4644: if (&condval($statecond)) {
1.52 www 4645: return '2';
4646: } else {
4647: return '';
4648: }
4649: }
1.30 www 4650:
1.766 albertel 4651: if ($thisallowed eq 'A') {
4652: return 'A';
1.814 raeburn 4653: } elsif ($thisallowed eq 'B') {
4654: return 'B';
1.766 albertel 4655: }
1.52 www 4656: return 'F';
1.232 www 4657: }
4658:
1.710 albertel 4659: sub split_uri_for_cond {
4660: my $uri=&deversion(&declutter(shift));
4661: my @uriparts=split(/\//,$uri);
4662: my $filename=pop(@uriparts);
4663: my $pathname=join('/',@uriparts);
4664: return ($pathname,$filename);
4665: }
1.232 www 4666: # --------------------------------------------------- Is a resource on the map?
4667:
4668: sub is_on_map {
1.710 albertel 4669: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 4670: #Trying to find the conditional for the file
1.620 albertel 4671: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 4672: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 4673: if ($match) {
1.289 bowersj2 4674: return (1,$1);
4675: } else {
1.434 www 4676: return (0,0);
1.289 bowersj2 4677: }
1.12 www 4678: }
4679:
1.427 www 4680: # --------------------------------------------------------- Get symb from alias
4681:
4682: sub get_symb_from_alias {
4683: my $symb=shift;
4684: my ($map,$resid,$url)=&decode_symb($symb);
4685: # Already is a symb
4686: if ($url) { return $symb; }
4687: # Must be an alias
4688: my $aliassymb='';
4689: my %bighash;
1.620 albertel 4690: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 4691: &GDBM_READER(),0640)) {
4692: my $rid=$bighash{'mapalias_'.$symb};
4693: if ($rid) {
4694: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 4695: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
4696: $resid,$bighash{'src_'.$rid});
1.427 www 4697: }
4698: untie %bighash;
4699: }
4700: return $aliassymb;
4701: }
4702:
1.12 www 4703: # ----------------------------------------------------------------- Define Role
4704:
4705: sub definerole {
4706: if (allowed('mcr','/')) {
4707: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 4708: foreach my $role (split(':',$sysrole)) {
4709: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4710: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
4711: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
4712: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 4713: return "refused:s:$crole&$cqual";
4714: }
4715: }
1.191 harris41 4716: }
1.800 albertel 4717: foreach my $role (split(':',$domrole)) {
4718: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4719: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
4720: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
4721: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 4722: return "refused:d:$crole&$cqual";
4723: }
4724: }
1.191 harris41 4725: }
1.800 albertel 4726: foreach my $role (split(':',$courole)) {
4727: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 4728: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
4729: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
4730: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 4731: return "refused:c:$crole&$cqual";
4732: }
4733: }
1.191 harris41 4734: }
1.620 albertel 4735: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
4736: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 4737: "rolesdef_$rolename=".
4738: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 4739: return reply($command,$env{'user.home'});
1.12 www 4740: } else {
4741: return 'refused';
4742: }
1.105 harris41 4743: }
4744:
4745: # ---------------- Make a metadata query against the network of library servers
4746:
4747: sub metadata_query {
1.244 matthew 4748: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 4749: my %rhash;
1.845 albertel 4750: my %libserv = &all_library();
1.244 matthew 4751: my @server_list = (defined($server_array) ? @$server_array
4752: : keys(%libserv) );
4753: for my $server (@server_list) {
1.118 harris41 4754: unless ($custom or $customshow) {
4755: my $reply=&reply("querysend:".&escape($query),$server);
4756: $rhash{$server}=$reply;
4757: }
4758: else {
4759: my $reply=&reply("querysend:".&escape($query).':'.
4760: &escape($custom).':'.&escape($customshow),
4761: $server);
4762: $rhash{$server}=$reply;
4763: }
1.112 harris41 4764: }
1.118 harris41 4765: return \%rhash;
1.240 www 4766: }
4767:
4768: # ----------------------------------------- Send log queries and wait for reply
4769:
4770: sub log_query {
4771: my ($uname,$udom,$query,%filters)=@_;
4772: my $uhome=&homeserver($uname,$udom);
4773: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 4774: my $uhost=&hostname($uhome);
1.800 albertel 4775: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 4776: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
4777: $uhome);
1.479 albertel 4778: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 4779: return get_query_reply($queryid);
4780: }
4781:
1.818 raeburn 4782: # -------------------------- Update MySQL table for portfolio file
4783:
4784: sub update_portfolio_table {
1.821 raeburn 4785: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818 raeburn 4786: my $homeserver = &homeserver($uname,$udom);
4787: my $queryid=
1.821 raeburn 4788: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
4789: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 4790: my $reply = &get_query_reply($queryid);
4791: return $reply;
4792: }
4793:
1.899 raeburn 4794: # -------------------------- Update MySQL allusers table
4795:
4796: sub update_allusers_table {
4797: my ($uname,$udom,$names) = @_;
4798: my $homeserver = &homeserver($uname,$udom);
4799: my $queryid=
4800: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
4801: 'lastname='.&escape($names->{'lastname'}).'%%'.
4802: 'firstname='.&escape($names->{'firstname'}).'%%'.
4803: 'middlename='.&escape($names->{'middlename'}).'%%'.
4804: 'generation='.&escape($names->{'generation'}).'%%'.
4805: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
4806: 'id='.&escape($names->{'id'}),$homeserver);
4807: my $reply = &get_query_reply($queryid);
4808: return $reply;
4809: }
4810:
1.508 raeburn 4811: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 4812:
4813: sub fetch_enrollment_query {
1.511 raeburn 4814: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 4815: my $homeserver;
1.547 raeburn 4816: my $maxtries = 1;
1.508 raeburn 4817: if ($context eq 'automated') {
4818: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 4819: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 4820: } else {
4821: $homeserver = &homeserver($cnum,$dom);
4822: }
1.838 albertel 4823: my $host=&hostname($homeserver);
1.506 raeburn 4824: my $cmd = '';
1.800 albertel 4825: foreach my $affiliate (keys %{$affiliatesref}) {
4826: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 4827: }
4828: $cmd =~ s/%%$//;
4829: $cmd = &escape($cmd);
4830: my $query = 'fetchenrollment';
1.620 albertel 4831: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 4832: unless ($queryid=~/^\Q$host\E\_/) {
4833: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
4834: return 'error: '.$queryid;
4835: }
1.506 raeburn 4836: my $reply = &get_query_reply($queryid);
1.547 raeburn 4837: my $tries = 1;
4838: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
4839: $reply = &get_query_reply($queryid);
4840: $tries ++;
4841: }
1.526 raeburn 4842: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 4843: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 4844: } else {
1.901 albertel 4845: my @responses = split(/:/,$reply);
1.515 raeburn 4846: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 4847: foreach my $line (@responses) {
4848: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 4849: $$replyref{$key} = $value;
4850: }
4851: } else {
1.506 raeburn 4852: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 4853: foreach my $line (@responses) {
4854: my ($key,$value) = split(/=/,$line);
1.506 raeburn 4855: $$replyref{$key} = $value;
4856: if ($value > 0) {
1.800 albertel 4857: foreach my $item (@{$$affiliatesref{$key}}) {
4858: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 4859: my $destname = $pathname.'/'.$filename;
4860: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 4861: if ($xml_classlist =~ /^error/) {
4862: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
4863: } else {
1.506 raeburn 4864: if ( open(FILE,">$destname") ) {
4865: print FILE &unescape($xml_classlist);
4866: close(FILE);
1.526 raeburn 4867: } else {
4868: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 4869: }
4870: }
4871: }
4872: }
4873: }
4874: }
4875: return 'ok';
4876: }
4877: return 'error';
4878: }
4879:
1.242 www 4880: sub get_query_reply {
4881: my $queryid=shift;
1.240 www 4882: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
4883: my $reply='';
4884: for (1..100) {
4885: sleep 2;
4886: if (-e $replyfile.'.end') {
1.448 albertel 4887: if (open(my $fh,$replyfile)) {
1.904 albertel 4888: $reply = join('',<$fh>);
4889: close($fh);
1.240 www 4890: } else { return 'error: reply_file_error'; }
1.242 www 4891: return &unescape($reply);
4892: }
1.240 www 4893: }
1.242 www 4894: return 'timeout:'.$queryid;
1.240 www 4895: }
4896:
4897: sub courselog_query {
1.241 www 4898: #
4899: # possible filters:
4900: # url: url or symb
4901: # username
4902: # domain
4903: # action: view, submit, grade
4904: # start: timestamp
4905: # end: timestamp
4906: #
1.240 www 4907: my (%filters)=@_;
1.620 albertel 4908: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 4909: if ($filters{'url'}) {
4910: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
4911: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
4912: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
4913: }
1.620 albertel 4914: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
4915: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 4916: return &log_query($cname,$cdom,'courselog',%filters);
4917: }
4918:
4919: sub userlog_query {
1.858 raeburn 4920: #
4921: # possible filters:
4922: # action: log check role
4923: # start: timestamp
4924: # end: timestamp
4925: #
1.240 www 4926: my ($uname,$udom,%filters)=@_;
4927: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 4928: }
4929:
1.506 raeburn 4930: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
4931:
4932: sub auto_run {
1.508 raeburn 4933: my ($cnum,$cdom) = @_;
1.876 raeburn 4934: my $response = 0;
4935: my $settings;
4936: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
4937: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
4938: $settings = $domconfig{'autoenroll'};
4939: if ($settings->{'run'} eq '1') {
4940: $response = 1;
4941: }
4942: } else {
1.934 raeburn 4943: my $homeserver;
4944: if (&is_course($cdom,$cnum)) {
4945: $homeserver = &homeserver($cnum,$cdom);
4946: } else {
4947: $homeserver = &domain($cdom,'primary');
4948: }
4949: if ($homeserver ne 'no_host') {
4950: $response = &reply('autorun:'.$cdom,$homeserver);
4951: }
1.876 raeburn 4952: }
1.506 raeburn 4953: return $response;
4954: }
1.776 albertel 4955:
1.506 raeburn 4956: sub auto_get_sections {
1.508 raeburn 4957: my ($cnum,$cdom,$inst_coursecode) = @_;
4958: my $homeserver = &homeserver($cnum,$cdom);
1.506 raeburn 4959: my @secs = ();
1.511 raeburn 4960: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506 raeburn 4961: unless ($response eq 'refused') {
1.901 albertel 4962: @secs = split(/:/,$response);
1.506 raeburn 4963: }
4964: return @secs;
4965: }
1.776 albertel 4966:
1.506 raeburn 4967: sub auto_new_course {
1.508 raeburn 4968: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
4969: my $homeserver = &homeserver($cnum,$cdom);
1.515 raeburn 4970: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506 raeburn 4971: return $response;
4972: }
1.776 albertel 4973:
1.506 raeburn 4974: sub auto_validate_courseID {
1.508 raeburn 4975: my ($cnum,$cdom,$inst_course_id) = @_;
4976: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 4977: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 4978: return $response;
4979: }
1.776 albertel 4980:
1.506 raeburn 4981: sub auto_create_password {
1.873 raeburn 4982: my ($cnum,$cdom,$authparam,$udom) = @_;
4983: my ($homeserver,$response);
1.506 raeburn 4984: my $create_passwd = 0;
4985: my $authchk = '';
1.873 raeburn 4986: if ($udom =~ /^$match_domain$/) {
4987: $homeserver = &domain($udom,'primary');
4988: }
4989: if ($homeserver eq '') {
4990: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
4991: $homeserver = &homeserver($cnum,$cdom);
4992: }
4993: }
4994: if ($homeserver eq '') {
4995: $authchk = 'nodomain';
1.506 raeburn 4996: } else {
1.873 raeburn 4997: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
4998: if ($response eq 'refused') {
4999: $authchk = 'refused';
5000: } else {
1.901 albertel 5001: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 5002: }
1.506 raeburn 5003: }
5004: return ($authparam,$create_passwd,$authchk);
5005: }
5006:
1.706 raeburn 5007: sub auto_photo_permission {
5008: my ($cnum,$cdom,$students) = @_;
5009: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 5010: my ($outcome,$perm_reqd,$conditions) =
5011: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 5012: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5013: return (undef,undef);
5014: }
1.706 raeburn 5015: return ($outcome,$perm_reqd,$conditions);
5016: }
5017:
5018: sub auto_checkphotos {
5019: my ($uname,$udom,$pid) = @_;
5020: my $homeserver = &homeserver($uname,$udom);
5021: my ($result,$resulttype);
5022: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 5023: &escape($uname).':'.&escape($pid),
5024: $homeserver));
1.709 albertel 5025: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5026: return (undef,undef);
5027: }
1.706 raeburn 5028: if ($outcome) {
5029: ($result,$resulttype) = split(/:/,$outcome);
5030: }
5031: return ($result,$resulttype);
5032: }
5033:
5034: sub auto_photochoice {
5035: my ($cnum,$cdom) = @_;
5036: my $homeserver = &homeserver($cnum,$cdom);
5037: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 5038: &escape($cdom),
5039: $homeserver)));
1.709 albertel 5040: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5041: return (undef,undef);
5042: }
1.706 raeburn 5043: return ($update,$comment);
5044: }
5045:
5046: sub auto_photoupdate {
5047: my ($affiliatesref,$dom,$cnum,$photo) = @_;
5048: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 5049: my $host=&hostname($homeserver);
1.706 raeburn 5050: my $cmd = '';
5051: my $maxtries = 1;
1.800 albertel 5052: foreach my $affiliate (keys(%{$affiliatesref})) {
5053: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 5054: }
5055: $cmd =~ s/%%$//;
5056: $cmd = &escape($cmd);
5057: my $query = 'institutionalphotos';
5058: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
5059: unless ($queryid=~/^\Q$host\E\_/) {
5060: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
5061: return 'error: '.$queryid;
5062: }
5063: my $reply = &get_query_reply($queryid);
5064: my $tries = 1;
5065: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
5066: $reply = &get_query_reply($queryid);
5067: $tries ++;
5068: }
5069: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
5070: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
5071: } else {
5072: my @responses = split(/:/,$reply);
5073: my $outcome = shift(@responses);
5074: foreach my $item (@responses) {
5075: my ($key,$value) = split(/=/,$item);
5076: $$photo{$key} = $value;
5077: }
5078: return $outcome;
5079: }
5080: return 'error';
5081: }
5082:
1.521 raeburn 5083: sub auto_instcode_format {
1.793 albertel 5084: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
5085: $cat_order) = @_;
1.521 raeburn 5086: my $courses = '';
1.772 raeburn 5087: my @homeservers;
1.521 raeburn 5088: if ($caller eq 'global') {
1.841 albertel 5089: my %servers = &get_servers($codedom,'library');
5090: foreach my $tryserver (keys(%servers)) {
5091: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
5092: push(@homeservers,$tryserver);
5093: }
1.584 raeburn 5094: }
1.521 raeburn 5095: } else {
1.772 raeburn 5096: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 5097: }
1.793 albertel 5098: foreach my $code (keys(%{$instcodes})) {
5099: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 5100: }
5101: chop($courses);
1.772 raeburn 5102: my $ok_response = 0;
5103: my $response;
5104: while (@homeservers > 0 && $ok_response == 0) {
5105: my $server = shift(@homeservers);
5106: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
5107: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
5108: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 5109: split(/:/,$response);
1.772 raeburn 5110: %{$codes} = (%{$codes},&str2hash($codes_str));
5111: push(@{$codetitles},&str2array($codetitles_str));
5112: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
5113: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
5114: $ok_response = 1;
5115: }
5116: }
5117: if ($ok_response) {
1.521 raeburn 5118: return 'ok';
1.772 raeburn 5119: } else {
5120: return $response;
1.521 raeburn 5121: }
5122: }
5123:
1.792 raeburn 5124: sub auto_instcode_defaults {
5125: my ($domain,$returnhash,$code_order) = @_;
5126: my @homeservers;
1.841 albertel 5127:
5128: my %servers = &get_servers($domain,'library');
5129: foreach my $tryserver (keys(%servers)) {
5130: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
5131: push(@homeservers,$tryserver);
5132: }
1.792 raeburn 5133: }
1.841 albertel 5134:
1.792 raeburn 5135: my $response;
1.841 albertel 5136: foreach my $server (@homeservers) {
1.792 raeburn 5137: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 5138: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
5139:
5140: foreach my $pair (split(/\&/,$response)) {
5141: my ($name,$value)=split(/\=/,$pair);
5142: if ($name eq 'code_order') {
5143: @{$code_order} = split(/\&/,&unescape($value));
5144: } else {
5145: $returnhash->{&unescape($name)}=&unescape($value);
5146: }
5147: }
5148: return 'ok';
1.792 raeburn 5149: }
1.841 albertel 5150:
5151: return $response;
1.792 raeburn 5152: }
5153:
1.777 albertel 5154: sub auto_validate_class_sec {
1.918 raeburn 5155: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 5156: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 5157: my $ownerlist;
5158: if (ref($owners) eq 'ARRAY') {
5159: $ownerlist = join(',',@{$owners});
5160: } else {
5161: $ownerlist = $owners;
5162: }
1.773 raeburn 5163: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 5164: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 5165: return $response;
5166: }
5167:
1.679 raeburn 5168: # ------------------------------------------------------- Course Group routines
5169:
5170: sub get_coursegroups {
1.809 raeburn 5171: my ($cdom,$cnum,$group,$namespace) = @_;
5172: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 5173: }
5174:
1.679 raeburn 5175: sub modify_coursegroup {
5176: my ($cdom,$cnum,$groupsettings) = @_;
5177: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
5178: }
5179:
1.809 raeburn 5180: sub toggle_coursegroup_status {
5181: my ($cdom,$cnum,$group,$action) = @_;
5182: my ($from_namespace,$to_namespace);
5183: if ($action eq 'delete') {
5184: $from_namespace = 'coursegroups';
5185: $to_namespace = 'deleted_groups';
5186: } else {
5187: $from_namespace = 'deleted_groups';
5188: $to_namespace = 'coursegroups';
5189: }
5190: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 5191: if (my $tmp = &error(%curr_group)) {
5192: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
5193: return ('read error',$tmp);
5194: } else {
5195: my %savedsettings = %curr_group;
1.809 raeburn 5196: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 5197: my $deloutcome;
5198: if ($result eq 'ok') {
1.809 raeburn 5199: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 5200: } else {
5201: return ('write error',$result);
5202: }
5203: if ($deloutcome eq 'ok') {
5204: return 'ok';
5205: } else {
5206: return ('delete error',$deloutcome);
5207: }
5208: }
5209: }
5210:
1.679 raeburn 5211: sub modify_group_roles {
5212: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
5213: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
5214: my $role = 'gr/'.&escape($userprivs);
5215: my ($uname,$udom) = split(/:/,$user);
5216: my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684 raeburn 5217: if ($result eq 'ok') {
5218: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
5219: }
1.679 raeburn 5220: return $result;
5221: }
5222:
5223: sub modify_coursegroup_membership {
5224: my ($cdom,$cnum,$membership) = @_;
5225: my $result = &put('groupmembership',$membership,$cdom,$cnum);
5226: return $result;
5227: }
5228:
1.682 raeburn 5229: sub get_active_groups {
5230: my ($udom,$uname,$cdom,$cnum) = @_;
5231: my $now = time;
5232: my %groups = ();
5233: foreach my $key (keys(%env)) {
1.811 albertel 5234: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 5235: my ($start,$end) = split(/\./,$env{$key});
5236: if (($end!=0) && ($end<$now)) { next; }
5237: if (($start!=0) && ($start>$now)) { next; }
5238: if ($1 eq $cdom && $2 eq $cnum) {
5239: $groups{$3} = $env{$key} ;
5240: }
5241: }
5242: }
5243: return %groups;
5244: }
5245:
1.683 raeburn 5246: sub get_group_membership {
5247: my ($cdom,$cnum,$group) = @_;
5248: return(&dump('groupmembership',$cdom,$cnum,$group));
5249: }
5250:
5251: sub get_users_groups {
5252: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 5253: my @usersgroups;
1.683 raeburn 5254: my $cachetime=1800;
5255:
5256: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 5257: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
5258: if (defined($cached)) {
1.734 albertel 5259: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 5260: } else {
5261: $grouplist = '';
1.816 raeburn 5262: my $courseurl = &courseid_to_courseurl($courseid);
5263: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 5264: my $access_end = $env{'course.'.$courseid.
5265: '.default_enrollment_end_date'};
5266: my $now = time;
5267: foreach my $key (keys(%roleshash)) {
5268: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
5269: my $group = $1;
5270: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
5271: my $start = $2;
5272: my $end = $1;
5273: if ($start == -1) { next; } # deleted from group
5274: if (($start!=0) && ($start>$now)) { next; }
5275: if (($end!=0) && ($end<$now)) {
5276: if ($access_end && $access_end < $now) {
5277: if ($access_end - $end < 86400) {
5278: push(@usersgroups,$group);
1.733 raeburn 5279: }
5280: }
1.817 raeburn 5281: next;
1.733 raeburn 5282: }
1.817 raeburn 5283: push(@usersgroups,$group);
1.683 raeburn 5284: }
5285: }
5286: }
1.817 raeburn 5287: @usersgroups = &sort_course_groups($courseid,@usersgroups);
5288: $grouplist = join(':',@usersgroups);
5289: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 5290: }
1.733 raeburn 5291: return @usersgroups;
1.683 raeburn 5292: }
5293:
5294: sub devalidate_getgroups_cache {
5295: my ($udom,$uname,$cdom,$cnum)=@_;
5296: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 5297:
1.683 raeburn 5298: my $hashid="$udom:$uname:$courseid";
5299: &devalidate_cache_new('getgroups',$hashid);
5300: }
5301:
1.12 www 5302: # ------------------------------------------------------------------ Plain Text
5303:
5304: sub plaintext {
1.742 raeburn 5305: my ($short,$type,$cid) = @_;
1.758 albertel 5306: if ($short =~ /^cr/) {
5307: return (split('/',$short))[-1];
5308: }
1.742 raeburn 5309: if (!defined($cid)) {
5310: $cid = $env{'request.course.id'};
5311: }
5312: if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
5313: return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
5314: '.plaintext'});
5315: }
5316: my %rolenames = (
5317: Course => 'std',
5318: Group => 'alt1',
5319: );
5320: if (defined($type) &&
5321: defined($rolenames{$type}) &&
5322: defined($prp{$short}{$rolenames{$type}})) {
5323: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
5324: } else {
5325: return &Apache::lonlocal::mt($prp{$short}{'std'});
5326: }
1.12 www 5327: }
5328:
5329: # ----------------------------------------------------------------- Assign Role
5330:
5331: sub assignrole {
1.947 raeburn 5332: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll)=@_;
1.21 www 5333: my $mrole;
5334: if ($role =~ /^cr\//) {
1.393 www 5335: my $cwosec=$url;
1.811 albertel 5336: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 5337: unless (&allowed('ccr',$cwosec)) {
1.104 www 5338: &logthis('Refused custom assignrole: '.
5339: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620 albertel 5340: $env{'user.name'}.' at '.$env{'user.domain'});
1.104 www 5341: return 'refused';
5342: }
1.21 www 5343: $mrole='cr';
1.678 raeburn 5344: } elsif ($role =~ /^gr\//) {
5345: my $cwogrp=$url;
1.811 albertel 5346: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 5347: unless (&allowed('mdg',$cwogrp)) {
5348: &logthis('Refused group assignrole: '.
5349: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
5350: $env{'user.name'}.' at '.$env{'user.domain'});
5351: return 'refused';
5352: }
5353: $mrole='gr';
1.21 www 5354: } else {
1.82 www 5355: my $cwosec=$url;
1.811 albertel 5356: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 5357: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
5358: my $refused;
5359: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
5360: if (!(&allowed('c'.$role,$url))) {
5361: $refused = 1;
5362: }
5363: } else {
5364: $refused = 1;
5365: }
1.947 raeburn 5366: if ($refused) {
5367: if (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5368: $refused = '';
5369: } else {
5370: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
5371: ' '.$role.' '.$end.' '.$start.' by '.
5372: $env{'user.name'}.' at '.$env{'user.domain'});
5373: return 'refused';
5374: }
1.932 raeburn 5375: }
1.104 www 5376: }
1.21 www 5377: $mrole=$role;
5378: }
1.620 albertel 5379: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 5380: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 5381: if ($end) { $command.='_'.$end; }
1.21 www 5382: if ($start) {
5383: if ($end) {
1.81 www 5384: $command.='_'.$start;
1.21 www 5385: } else {
1.81 www 5386: $command.='_0_'.$start;
1.21 www 5387: }
5388: }
1.739 raeburn 5389: my $origstart = $start;
5390: my $origend = $end;
1.357 www 5391: # actually delete
5392: if ($deleteflag) {
1.373 www 5393: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 5394: # modify command to delete the role
1.620 albertel 5395: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 5396: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 5397: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 5398: # set start and finish to negative values for userrolelog
5399: $start=-1;
5400: $end=-1;
5401: }
5402: }
5403: # send command
1.349 www 5404: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 5405: # log new user role if status is ok
1.349 www 5406: if ($answer eq 'ok') {
1.663 raeburn 5407: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 5408: # for course roles, perform group memberships changes triggered by role change.
5409: unless ($role =~ /^gr/) {
5410: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
5411: $origstart);
5412: }
1.349 www 5413: }
5414: return $answer;
1.169 harris41 5415: }
5416:
5417: # -------------------------------------------------- Modify user authentication
1.197 www 5418: # Overrides without validation
5419:
1.169 harris41 5420: sub modifyuserauth {
5421: my ($udom,$uname,$umode,$upass)=@_;
5422: my $uhome=&homeserver($uname,$udom);
1.197 www 5423: unless (&allowed('mau',$udom)) { return 'refused'; }
5424: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 5425: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
5426: ' in domain '.$env{'request.role.domain'});
1.169 harris41 5427: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
5428: &escape($upass),$uhome);
1.620 albertel 5429: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 5430: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
5431: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
5432: &log($udom,,$uname,$uhome,
1.620 albertel 5433: 'Authentication changed by '.$env{'user.domain'}.', '.
5434: $env{'user.name'}.', '.$umode.
1.197 www 5435: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 5436: unless ($reply eq 'ok') {
1.197 www 5437: &logthis('Authentication mode error: '.$reply);
1.169 harris41 5438: return 'error: '.$reply;
5439: }
1.170 harris41 5440: return 'ok';
1.80 www 5441: }
5442:
1.81 www 5443: # --------------------------------------------------------------- Modify a user
1.80 www 5444:
1.81 www 5445: sub modifyuser {
1.206 matthew 5446: my ($udom, $uname, $uid,
5447: $umode, $upass, $first,
5448: $middle, $last, $gene,
1.387 www 5449: $forceid, $desiredhome, $email)=@_;
1.807 albertel 5450: $udom= &LONCAPA::clean_domain($udom);
5451: $uname=&LONCAPA::clean_username($uname);
1.81 www 5452: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 5453: $umode.', '.$first.', '.$middle.', '.
1.206 matthew 5454: $last.', '.$gene.'(forceid: '.$forceid.')'.
5455: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
5456: ' desiredhome not specified').
1.620 albertel 5457: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
5458: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 5459: my $uhome=&homeserver($uname,$udom,'true');
1.80 www 5460: # ----------------------------------------------------------------- Create User
1.406 albertel 5461: if (($uhome eq 'no_host') &&
5462: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 5463: my $unhome='';
1.844 albertel 5464: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 5465: $unhome = $desiredhome;
1.620 albertel 5466: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
5467: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 5468: } else { # load balancing routine for determining $unhome
1.81 www 5469: my $loadm=10000000;
1.841 albertel 5470: my %servers = &get_servers($udom,'library');
5471: foreach my $tryserver (keys(%servers)) {
5472: my $answer=reply('load',$tryserver);
5473: if (($answer=~/\d+/) && ($answer<$loadm)) {
5474: $loadm=$answer;
5475: $unhome=$tryserver;
5476: }
1.80 www 5477: }
5478: }
5479: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 5480: return 'error: unable to find a home server for '.$uname.
5481: ' in domain '.$udom;
1.80 www 5482: }
5483: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
5484: &escape($upass),$unhome);
5485: unless ($reply eq 'ok') {
5486: return 'error: '.$reply;
5487: }
1.230 stredwic 5488: $uhome=&homeserver($uname,$udom,'true');
1.80 www 5489: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 5490: return 'error: unable verify users home machine.';
1.80 www 5491: }
1.209 matthew 5492: } # End of creation of new user
1.80 www 5493: # ---------------------------------------------------------------------- Add ID
5494: if ($uid) {
5495: $uid=~tr/A-Z/a-z/;
5496: my %uidhash=&idrget($udom,$uname);
1.196 www 5497: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
5498: && (!$forceid)) {
1.80 www 5499: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 5500: return 'error: user id "'.$uid.'" does not match '.
5501: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 5502: }
5503: } else {
5504: &idput($udom,($uname => $uid));
5505: }
5506: }
5507: # -------------------------------------------------------------- Add names, etc
1.313 matthew 5508: my @tmp=&get('environment',
1.899 raeburn 5509: ['firstname','middlename','lastname','generation','id',
5510: 'permanentemail'],
1.134 albertel 5511: $udom,$uname);
1.313 matthew 5512: my %names;
5513: if ($tmp[0] =~ m/^error:.*/) {
5514: %names=();
5515: } else {
5516: %names = @tmp;
5517: }
1.388 www 5518: #
5519: # Make sure to not trash student environment if instructor does not bother
5520: # to supply name and email information
5521: #
5522: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 5523: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 5524: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 5525: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 5526: if ($email) {
5527: $email=~s/[^\w\@\.\-\,]//gs;
5528: if ($email=~/\@/) { $names{'notification'} = $email;
5529: $names{'critnotification'} = $email;
5530: $names{'permanentemail'} = $email; }
5531: }
1.899 raeburn 5532: if ($uid) { $names{'id'} = $uid; }
1.134 albertel 5533: my $reply = &put('environment', \%names, $udom,$uname);
5534: if ($reply ne 'ok') { return 'error: '.$reply; }
1.899 raeburn 5535: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
1.680 www 5536: &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81 www 5537: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 5538: $umode.', '.$first.', '.$middle.', '.
5539: $last.', '.$gene.' by '.
1.620 albertel 5540: $env{'user.name'}.' at '.$env{'user.domain'});
1.134 albertel 5541: return 'ok';
1.80 www 5542: }
5543:
1.81 www 5544: # -------------------------------------------------------------- Modify student
1.80 www 5545:
1.81 www 5546: sub modifystudent {
5547: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515 raeburn 5548: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455 albertel 5549: if (!$cid) {
1.620 albertel 5550: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 5551: return 'not_in_class';
5552: }
1.80 www 5553: }
5554: # --------------------------------------------------------------- Make the user
1.81 www 5555: my $reply=&modifyuser
1.209 matthew 5556: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387 www 5557: $desiredhome,$email);
1.80 www 5558: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 5559: # This will cause &modify_student_enrollment to get the uid from the
5560: # students environment
5561: $uid = undef if (!$forceid);
1.455 albertel 5562: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515 raeburn 5563: $gene,$usec,$end,$start,$type,$locktype,$cid);
1.297 matthew 5564: return $reply;
5565: }
5566:
5567: sub modify_student_enrollment {
1.947 raeburn 5568: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll) = @_;
1.455 albertel 5569: my ($cdom,$cnum,$chome);
5570: if (!$cid) {
1.620 albertel 5571: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 5572: return 'not_in_class';
5573: }
1.620 albertel 5574: $cdom=$env{'course.'.$cid.'.domain'};
5575: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 5576: } else {
5577: ($cdom,$cnum)=split(/_/,$cid);
5578: }
1.620 albertel 5579: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 5580: if (!$chome) {
1.457 raeburn 5581: $chome=&homeserver($cnum,$cdom);
1.297 matthew 5582: }
1.455 albertel 5583: if (!$chome) { return 'unknown_course'; }
1.297 matthew 5584: # Make sure the user exists
1.81 www 5585: my $uhome=&homeserver($uname,$udom);
5586: if (($uhome eq '') || ($uhome eq 'no_host')) {
5587: return 'error: no such user';
5588: }
1.297 matthew 5589: # Get student data if we were not given enough information
5590: if (!defined($first) || $first eq '' ||
5591: !defined($last) || $last eq '' ||
5592: !defined($uid) || $uid eq '' ||
5593: !defined($middle) || $middle eq '' ||
5594: !defined($gene) || $gene eq '') {
1.294 matthew 5595: # They did not supply us with enough data to enroll the student, so
5596: # we need to pick up more information.
1.297 matthew 5597: my %tmp = &get('environment',
1.294 matthew 5598: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 5599: ,$udom,$uname);
5600:
1.800 albertel 5601: #foreach my $key (keys(%tmp)) {
5602: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 5603: #}
1.294 matthew 5604: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
5605: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
5606: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 5607: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 5608: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
5609: }
1.556 albertel 5610: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 5611: my $reply=cput('classlist',
5612: {"$uname:$udom" =>
1.515 raeburn 5613: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 5614: $cdom,$cnum);
1.81 www 5615: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
5616: return 'error: '.$reply;
1.652 albertel 5617: } else {
5618: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 5619: }
1.297 matthew 5620: # Add student role to user
1.83 www 5621: my $uurl='/'.$cid;
1.81 www 5622: $uurl=~s/\_/\//g;
5623: if ($usec) {
5624: $uurl.='/'.$usec;
5625: }
1.947 raeburn 5626: return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll);
1.21 www 5627: }
5628:
1.556 albertel 5629: sub format_name {
5630: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
5631: my $name;
5632: if ($first ne 'lastname') {
5633: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
5634: } else {
5635: if ($lastname=~/\S/) {
5636: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
5637: $name=~s/\s+,/,/;
5638: } else {
5639: $name.= $firstname.' '.$middlename.' '.$generation;
5640: }
5641: }
5642: $name=~s/^\s+//;
5643: $name=~s/\s+$//;
5644: $name=~s/\s+/ /g;
5645: return $name;
5646: }
5647:
1.84 www 5648: # ------------------------------------------------- Write to course preferences
5649:
5650: sub writecoursepref {
5651: my ($courseid,%prefs)=@_;
5652: $courseid=~s/^\///;
5653: $courseid=~s/\_/\//g;
5654: my ($cdomain,$cnum)=split(/\//,$courseid);
5655: my $chome=homeserver($cnum,$cdomain);
5656: if (($chome eq '') || ($chome eq 'no_host')) {
5657: return 'error: no such course';
5658: }
5659: my $cstring='';
1.800 albertel 5660: foreach my $pref (keys(%prefs)) {
5661: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 5662: }
1.84 www 5663: $cstring=~s/\&$//;
5664: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
5665: }
5666:
5667: # ---------------------------------------------------------- Make/modify course
5668:
5669: sub createcourse {
1.741 raeburn 5670: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
5671: $course_owner,$crstype)=@_;
1.84 www 5672: $url=&declutter($url);
5673: my $cid='';
1.264 matthew 5674: unless (&allowed('ccc',$udom)) {
1.84 www 5675: return 'refused';
5676: }
5677: # ------------------------------------------------------------------- Create ID
1.674 www 5678: my $uname=int(1+rand(9)).
5679: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
5680: substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84 www 5681: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
5682: # ----------------------------------------------- Make sure that does not exist
1.230 stredwic 5683: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 5684: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5685: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
5686: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230 stredwic 5687: $uhome=&homeserver($uname,$udom,'true');
1.84 www 5688: unless (($uhome eq '') || ($uhome eq 'no_host')) {
5689: return 'error: unable to generate unique course-ID';
5690: }
5691: }
1.264 matthew 5692: # ------------------------------------------------ Check supplied server name
1.620 albertel 5693: $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845 albertel 5694: if (! &is_library($course_server)) {
1.264 matthew 5695: return 'error:bad server name '.$course_server;
5696: }
1.84 www 5697: # ------------------------------------------------------------- Make the course
5698: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 5699: $course_server);
1.84 www 5700: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230 stredwic 5701: $uhome=&homeserver($uname,$udom,'true');
1.84 www 5702: if (($uhome eq '') || ($uhome eq 'no_host')) {
5703: return 'error: no such course';
5704: }
1.271 www 5705: # ----------------------------------------------------------------- Course made
1.516 raeburn 5706: # log existence
1.918 raeburn 5707: my $newcourse = {
5708: $udom.'_'.$uname => {
1.921 raeburn 5709: description => $description,
5710: inst_code => $inst_code,
5711: owner => $course_owner,
5712: type => $crstype,
1.918 raeburn 5713: },
5714: };
1.921 raeburn 5715: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 5716: # set toplevel url
1.271 www 5717: my $topurl=$url;
5718: unless ($nonstandard) {
5719: # ------------------------------------------ For standard courses, make top url
5720: my $mapurl=&clutter($url);
1.278 www 5721: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 5722: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 5723: <map>
5724: <resource id="1" type="start"></resource>
5725: <resource id="2" src="$mapurl"></resource>
5726: <resource id="3" type="finish"></resource>
5727: <link index="1" from="1" to="2"></link>
5728: <link index="2" from="2" to="3"></link>
5729: </map>
5730: ENDINITMAP
5731: $topurl=&declutter(
1.638 albertel 5732: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 5733: );
5734: }
5735: # ----------------------------------------------------------- Write preferences
1.84 www 5736: &writecoursepref($udom.'_'.$uname,
5737: ('description' => $description,
1.271 www 5738: 'url' => $topurl));
1.84 www 5739: return '/'.$udom.'/'.$uname;
5740: }
5741:
1.813 albertel 5742: sub is_course {
5743: my ($cdom,$cnum) = @_;
5744: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946 raeburn 5745: undef,'.');
1.813 albertel 5746: if (exists($courses{$cdom.'_'.$cnum})) {
5747: return 1;
5748: }
5749: return 0;
5750: }
5751:
1.21 www 5752: # ---------------------------------------------------------- Assign Custom Role
5753:
5754: sub assigncustomrole {
1.357 www 5755: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21 www 5756: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357 www 5757: $end,$start,$deleteflag);
1.21 www 5758: }
5759:
5760: # ----------------------------------------------------------------- Revoke Role
5761:
5762: sub revokerole {
1.357 www 5763: my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21 www 5764: my $now=time;
1.357 www 5765: return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21 www 5766: }
5767:
5768: # ---------------------------------------------------------- Revoke Custom Role
5769:
5770: sub revokecustomrole {
1.357 www 5771: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21 www 5772: my $now=time;
1.357 www 5773: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
5774: $deleteflag);
1.17 www 5775: }
5776:
1.533 banghart 5777: # ------------------------------------------------------------ Disk usage
1.535 albertel 5778: sub diskusage {
1.533 banghart 5779: my ($udom,$uname,$directoryRoot)=@_;
5780: $directoryRoot =~ s/\/$//;
1.535 albertel 5781: my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514 albertel 5782: return $listing;
1.512 banghart 5783: }
5784:
1.566 banghart 5785: sub is_locked {
5786: my ($file_name, $domain, $user) = @_;
5787: my @check;
5788: my $is_locked;
5789: push @check, $file_name;
1.613 albertel 5790: my %locked = &get('file_permissions',\@check,
1.620 albertel 5791: $env{'user.domain'},$env{'user.name'});
1.615 albertel 5792: my ($tmp)=keys(%locked);
5793: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 5794:
1.566 banghart 5795: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 5796: $is_locked = 'false';
5797: foreach my $entry (@{$locked{$file_name}}) {
5798: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 5799: $is_locked = 'true';
5800: last;
1.745 raeburn 5801: }
5802: }
1.566 banghart 5803: } else {
5804: $is_locked = 'false';
5805: }
5806: }
5807:
1.759 albertel 5808: sub declutter_portfile {
5809: my ($file) = @_;
1.833 albertel 5810: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 5811: return $file;
5812: }
5813:
1.559 banghart 5814: # ------------------------------------------------------------- Mark as Read Only
5815:
5816: sub mark_as_readonly {
5817: my ($domain,$user,$files,$what) = @_;
1.613 albertel 5818: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 5819: my ($tmp)=keys(%current_permissions);
5820: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 5821: foreach my $file (@{$files}) {
1.759 albertel 5822: $file = &declutter_portfile($file);
1.561 banghart 5823: push(@{$current_permissions{$file}},$what);
1.559 banghart 5824: }
1.613 albertel 5825: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 5826: return;
5827: }
5828:
1.572 banghart 5829: # ------------------------------------------------------------Save Selected Files
5830:
5831: sub save_selected_files {
5832: my ($user, $path, @files) = @_;
5833: my $filename = $user."savedfiles";
1.573 banghart 5834: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 5835: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 5836: foreach my $file (@files) {
1.620 albertel 5837: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 5838: }
5839: foreach my $file (@other_files) {
1.574 banghart 5840: print (OUT $file."\n");
1.572 banghart 5841: }
1.574 banghart 5842: close (OUT);
1.572 banghart 5843: return 'ok';
5844: }
5845:
1.574 banghart 5846: sub clear_selected_files {
5847: my ($user) = @_;
5848: my $filename = $user."savedfiles";
5849: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5850: print (OUT undef);
5851: close (OUT);
5852: return ("ok");
5853: }
5854:
1.572 banghart 5855: sub files_in_path {
5856: my ($user, $path) = @_;
5857: my $filename = $user."savedfiles";
5858: my %return_files;
1.574 banghart 5859: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 5860: while (my $line_in = <IN>) {
1.574 banghart 5861: chomp ($line_in);
5862: my @paths_and_file = split (m!/!, $line_in);
5863: my $file_part = pop (@paths_and_file);
5864: my $path_part = join ('/', @paths_and_file);
1.573 banghart 5865: $path_part.='/';
5866: my $path_and_file = $path_part.$file_part;
5867: if ($path_part eq $path) {
5868: $return_files{$file_part}= 'selected';
5869: }
5870: }
1.574 banghart 5871: close (IN);
5872: return (\%return_files);
1.572 banghart 5873: }
5874:
5875: # called in portfolio select mode, to show files selected NOT in current directory
5876: sub files_not_in_path {
5877: my ($user, $path) = @_;
5878: my $filename = $user."savedfiles";
5879: my @return_files;
5880: my $path_part;
1.800 albertel 5881: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
5882: while (my $line = <IN>) {
1.572 banghart 5883: #ok, I know it's clunky, but I want it to work
1.800 albertel 5884: my @paths_and_file = split(m|/|, $line);
5885: my $file_part = pop(@paths_and_file);
5886: chomp($file_part);
5887: my $path_part = join('/', @paths_and_file);
1.572 banghart 5888: $path_part .= '/';
5889: my $path_and_file = $path_part.$file_part;
5890: if ($path_part ne $path) {
1.800 albertel 5891: push(@return_files, ($path_and_file));
1.572 banghart 5892: }
5893: }
1.800 albertel 5894: close(OUT);
1.574 banghart 5895: return (@return_files);
1.572 banghart 5896: }
5897:
1.745 raeburn 5898: #----------------------------------------------Get portfolio file permissions
1.629 banghart 5899:
1.745 raeburn 5900: sub get_portfile_permissions {
5901: my ($domain,$user) = @_;
1.613 albertel 5902: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 5903: my ($tmp)=keys(%current_permissions);
5904: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 5905: return \%current_permissions;
5906: }
5907:
5908: #---------------------------------------------Get portfolio file access controls
5909:
1.749 raeburn 5910: sub get_access_controls {
1.745 raeburn 5911: my ($current_permissions,$group,$file) = @_;
1.769 albertel 5912: my %access;
5913: my $real_file = $file;
5914: $file =~ s/\.meta$//;
1.745 raeburn 5915: if (defined($file)) {
1.749 raeburn 5916: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
5917: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 5918: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 5919: }
5920: }
1.745 raeburn 5921: } else {
1.749 raeburn 5922: foreach my $key (keys(%{$current_permissions})) {
5923: if ($key =~ /\0accesscontrol$/) {
5924: if (defined($group)) {
5925: if ($key !~ m-^\Q$group\E/-) {
5926: next;
5927: }
5928: }
5929: my ($fullpath) = split(/\0/,$key);
5930: if (ref($$current_permissions{$key}) eq 'HASH') {
5931: foreach my $control (keys(%{$$current_permissions{$key}})) {
5932: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
5933: }
5934: }
5935: }
5936: }
5937: }
5938: return %access;
5939: }
5940:
5941: sub modify_access_controls {
5942: my ($file_name,$changes,$domain,$user)=@_;
5943: my ($outcome,$deloutcome);
5944: my %store_permissions;
5945: my %new_values;
5946: my %new_control;
5947: my %translation;
5948: my @deletions = ();
5949: my $now = time;
5950: if (exists($$changes{'activate'})) {
5951: if (ref($$changes{'activate'}) eq 'HASH') {
5952: my @newitems = sort(keys(%{$$changes{'activate'}}));
5953: my $numnew = scalar(@newitems);
5954: for (my $i=0; $i<$numnew; $i++) {
5955: my $newkey = $newitems[$i];
5956: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 5957: if ($newkey =~ /^\d+:/) {
5958: $newkey =~ s/^(\d+)/$newid/;
5959: $translation{$1} = $newid;
5960: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
5961: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
5962: $translation{$1} = $newid;
5963: }
1.749 raeburn 5964: $new_values{$file_name."\0".$newkey} =
5965: $$changes{'activate'}{$newitems[$i]};
5966: $new_control{$newkey} = $now;
5967: }
5968: }
5969: }
5970: my %todelete;
5971: my %changed_items;
5972: foreach my $action ('delete','update') {
5973: if (exists($$changes{$action})) {
5974: if (ref($$changes{$action}) eq 'HASH') {
5975: foreach my $key (keys(%{$$changes{$action}})) {
5976: my ($itemnum) = ($key =~ /^([^:]+):/);
5977: if ($action eq 'delete') {
5978: $todelete{$itemnum} = 1;
5979: } else {
5980: $changed_items{$itemnum} = $key;
5981: }
5982: }
1.745 raeburn 5983: }
5984: }
1.749 raeburn 5985: }
5986: # get lock on access controls for file.
5987: my $lockhash = {
5988: $file_name."\0".'locked_access_records' => $env{'user.name'}.
5989: ':'.$env{'user.domain'},
5990: };
5991: my $tries = 0;
5992: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5993:
5994: while (($gotlock ne 'ok') && $tries <3) {
5995: $tries ++;
5996: sleep 1;
5997: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
5998: }
5999: if ($gotlock eq 'ok') {
6000: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
6001: my ($tmp)=keys(%curr_permissions);
6002: if ($tmp=~/^error:/) { undef(%curr_permissions); }
6003: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
6004: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
6005: if (ref($curr_controls) eq 'HASH') {
6006: foreach my $control_item (keys(%{$curr_controls})) {
6007: my ($itemnum) = ($control_item =~ /^([^:]+):/);
6008: if (defined($todelete{$itemnum})) {
6009: push(@deletions,$file_name."\0".$control_item);
6010: } else {
6011: if (defined($changed_items{$itemnum})) {
6012: $new_control{$changed_items{$itemnum}} = $now;
6013: push(@deletions,$file_name."\0".$control_item);
6014: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
6015: } else {
6016: $new_control{$control_item} = $$curr_controls{$control_item};
6017: }
6018: }
1.745 raeburn 6019: }
6020: }
6021: }
1.749 raeburn 6022: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
6023: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
6024: $outcome = &put('file_permissions',\%new_values,$domain,$user);
6025: # remove lock
6026: my @del_lock = ($file_name."\0".'locked_access_records');
6027: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 6028: my ($file,$group);
6029: if (&is_course($domain,$user)) {
6030: ($group,$file) = split(/\//,$file_name,2);
6031: } else {
6032: $file = $file_name;
6033: }
6034: my $sqlresult =
6035: &update_portfolio_table($user,$domain,$file,'portfolio_access',
6036: $group);
1.749 raeburn 6037: } else {
6038: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 6039: }
1.749 raeburn 6040: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 6041: }
6042:
1.827 raeburn 6043: sub make_public_indefinitely {
6044: my ($requrl) = @_;
6045: my $now = time;
6046: my $action = 'activate';
6047: my $aclnum = 0;
6048: if (&is_portfolio_url($requrl)) {
6049: my (undef,$udom,$unum,$file_name,$group) =
6050: &parse_portfolio_url($requrl);
6051: my $current_perms = &get_portfile_permissions($udom,$unum);
6052: my %access_controls = &get_access_controls($current_perms,
6053: $group,$file_name);
6054: foreach my $key (keys(%{$access_controls{$file_name}})) {
6055: my ($num,$scope,$end,$start) =
6056: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
6057: if ($scope eq 'public') {
6058: if ($start <= $now && $end == 0) {
6059: $action = 'none';
6060: } else {
6061: $action = 'update';
6062: $aclnum = $num;
6063: }
6064: last;
6065: }
6066: }
6067: if ($action eq 'none') {
6068: return 'ok';
6069: } else {
6070: my %changes;
6071: my $newend = 0;
6072: my $newstart = $now;
6073: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
6074: $changes{$action}{$newkey} = {
6075: type => 'public',
6076: time => {
6077: start => $newstart,
6078: end => $newend,
6079: },
6080: };
6081: my ($outcome,$deloutcome,$new_values,$translation) =
6082: &modify_access_controls($file_name,\%changes,$udom,$unum);
6083: return $outcome;
6084: }
6085: } else {
6086: return 'invalid';
6087: }
6088: }
6089:
1.745 raeburn 6090: #------------------------------------------------------Get Marked as Read Only
6091:
6092: sub get_marked_as_readonly {
6093: my ($domain,$user,$what,$group) = @_;
6094: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 6095: my @readonly_files;
1.629 banghart 6096: my $cmp1=$what;
6097: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 6098: while (my ($file_name,$value) = each(%{$current_permissions})) {
6099: if (defined($group)) {
6100: if ($file_name !~ m-^\Q$group\E/-) {
6101: next;
6102: }
6103: }
1.561 banghart 6104: if (ref($value) eq "ARRAY"){
6105: foreach my $stored_what (@{$value}) {
1.629 banghart 6106: my $cmp2=$stored_what;
1.759 albertel 6107: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 6108: $cmp2=join('',@{$stored_what});
1.745 raeburn 6109: }
1.629 banghart 6110: if ($cmp1 eq $cmp2) {
1.561 banghart 6111: push(@readonly_files, $file_name);
1.745 raeburn 6112: last;
1.563 banghart 6113: } elsif (!defined($what)) {
6114: push(@readonly_files, $file_name);
1.745 raeburn 6115: last;
1.561 banghart 6116: }
6117: }
1.745 raeburn 6118: }
1.561 banghart 6119: }
6120: return @readonly_files;
6121: }
1.577 banghart 6122: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 6123:
1.577 banghart 6124: sub get_marked_as_readonly_hash {
1.745 raeburn 6125: my ($current_permissions,$group,$what) = @_;
1.577 banghart 6126: my %readonly_files;
1.745 raeburn 6127: while (my ($file_name,$value) = each(%{$current_permissions})) {
6128: if (defined($group)) {
6129: if ($file_name !~ m-^\Q$group\E/-) {
6130: next;
6131: }
6132: }
1.577 banghart 6133: if (ref($value) eq "ARRAY"){
6134: foreach my $stored_what (@{$value}) {
1.745 raeburn 6135: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 6136: foreach my $lock_descriptor(@{$stored_what}) {
6137: if ($lock_descriptor eq 'graded') {
6138: $readonly_files{$file_name} = 'graded';
6139: } elsif ($lock_descriptor eq 'handback') {
6140: $readonly_files{$file_name} = 'handback';
6141: } else {
6142: if (!exists($readonly_files{$file_name})) {
6143: $readonly_files{$file_name} = 'locked';
6144: }
6145: }
1.745 raeburn 6146: }
1.750 banghart 6147: }
1.577 banghart 6148: }
6149: }
6150: }
6151: return %readonly_files;
6152: }
1.559 banghart 6153: # ------------------------------------------------------------ Unmark as Read Only
6154:
6155: sub unmark_as_readonly {
1.629 banghart 6156: # unmarks $file_name (if $file_name is defined), or all files locked by $what
6157: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 6158: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 6159: $file_name = &declutter_portfile($file_name);
1.634 albertel 6160: my $symb_crs = $what;
6161: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 6162: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 6163: my ($tmp)=keys(%current_permissions);
6164: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 6165: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 6166: foreach my $file (@readonly_files) {
1.759 albertel 6167: my $clean_file = &declutter_portfile($file);
6168: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 6169: my $current_locks = $current_permissions{$file};
1.563 banghart 6170: my @new_locks;
6171: my @del_keys;
6172: if (ref($current_locks) eq "ARRAY"){
6173: foreach my $locker (@{$current_locks}) {
1.632 albertel 6174: my $compare=$locker;
1.749 raeburn 6175: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 6176: $compare=join('',@{$locker});
1.746 raeburn 6177: if ($compare ne $symb_crs) {
6178: push(@new_locks, $locker);
6179: }
1.563 banghart 6180: }
6181: }
1.650 albertel 6182: if (scalar(@new_locks) > 0) {
1.563 banghart 6183: $current_permissions{$file} = \@new_locks;
6184: } else {
6185: push(@del_keys, $file);
1.613 albertel 6186: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 6187: delete($current_permissions{$file});
1.563 banghart 6188: }
6189: }
1.561 banghart 6190: }
1.613 albertel 6191: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 6192: return;
6193: }
1.512 banghart 6194:
1.17 www 6195: # ------------------------------------------------------------ Directory lister
6196:
6197: sub dirlist {
1.253 stredwic 6198: my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
6199:
1.18 www 6200: $uri=~s/^\///;
6201: $uri=~s/\/$//;
1.253 stredwic 6202: my ($udom, $uname);
6203: (undef,$udom,$uname)=split(/\//,$uri);
6204: if(defined($userdomain)) {
6205: $udom = $userdomain;
6206: }
6207: if(defined($username)) {
6208: $uname = $username;
6209: }
6210:
6211: my $dirRoot = $perlvar{'lonDocRoot'};
6212: if(defined($alternateDirectoryRoot)) {
6213: $dirRoot = $alternateDirectoryRoot;
6214: $dirRoot =~ s/\/$//;
1.751 banghart 6215: }
1.253 stredwic 6216:
6217: if($udom) {
6218: if($uname) {
1.800 albertel 6219: my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
6220: &homeserver($uname,$udom));
1.605 matthew 6221: my @listing_results;
6222: if ($listing eq 'unknown_cmd') {
1.800 albertel 6223: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
6224: &homeserver($uname,$udom));
1.605 matthew 6225: @listing_results = split(/:/,$listing);
6226: } else {
6227: @listing_results = map { &unescape($_); } split(/:/,$listing);
6228: }
6229: return @listing_results;
1.253 stredwic 6230: } elsif(!defined($alternateDirectoryRoot)) {
1.800 albertel 6231: my %allusers;
1.841 albertel 6232: my %servers = &get_servers($udom,'library');
6233: foreach my $tryserver (keys(%servers)) {
6234: my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
6235: $udom, $tryserver);
6236: my @listing_results;
6237: if ($listing eq 'unknown_cmd') {
6238: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
6239: $udom, $tryserver);
6240: @listing_results = split(/:/,$listing);
6241: } else {
6242: @listing_results =
6243: map { &unescape($_); } split(/:/,$listing);
6244: }
6245: if ($listing_results[0] ne 'no_such_dir' &&
6246: $listing_results[0] ne 'empty' &&
6247: $listing_results[0] ne 'con_lost') {
6248: foreach my $line (@listing_results) {
6249: my ($entry) = split(/&/,$line,2);
6250: $allusers{$entry} = 1;
6251: }
6252: }
1.253 stredwic 6253: }
6254: my $alluserstr='';
1.800 albertel 6255: foreach my $user (sort(keys(%allusers))) {
6256: $alluserstr.=$user.'&user:';
1.253 stredwic 6257: }
6258: $alluserstr=~s/:$//;
6259: return split(/:/,$alluserstr);
6260: } else {
1.800 albertel 6261: return ('missing user name');
1.253 stredwic 6262: }
6263: } elsif(!defined($alternateDirectoryRoot)) {
1.841 albertel 6264: my @all_domains = sort(&all_domains());
6265: foreach my $domain (@all_domains) {
6266: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
6267: }
6268: return @all_domains;
6269: } else {
1.800 albertel 6270: return ('missing domain');
1.275 stredwic 6271: }
6272: }
6273:
6274: # --------------------------------------------- GetFileTimestamp
6275: # This function utilizes dirlist and returns the date stamp for
6276: # when it was last modified. It will also return an error of -1
6277: # if an error occurs
6278:
1.410 matthew 6279: ##
6280: ## FIXME: This subroutine assumes its caller knows something about the
6281: ## directory structure of the home server for the student ($root).
6282: ## Not a good assumption to make. Since this is for looking up files
6283: ## in user directories, the full path should be constructed by lond, not
6284: ## whatever machine we request data from.
6285: ##
1.275 stredwic 6286: sub GetFileTimestamp {
6287: my ($studentDomain,$studentName,$filename,$root)=@_;
1.807 albertel 6288: $studentDomain = &LONCAPA::clean_domain($studentDomain);
6289: $studentName = &LONCAPA::clean_username($studentName);
1.275 stredwic 6290: my $subdir=$studentName.'__';
6291: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
6292: my $proname="$studentDomain/$subdir/$studentName";
6293: $proname .= '/'.$filename;
1.375 matthew 6294: my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain,
6295: $studentName, $root);
1.275 stredwic 6296: my @stats = split('&', $fileStat);
6297: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 6298: # @stats contains first the filename, then the stat output
6299: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 6300: } else {
6301: return -1;
1.253 stredwic 6302: }
1.26 www 6303: }
6304:
1.712 albertel 6305: sub stat_file {
6306: my ($uri) = @_;
1.787 albertel 6307: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 6308:
1.712 albertel 6309: my ($udom,$uname,$file,$dir);
6310: if ($uri =~ m-^/(uploaded|editupload)/-) {
6311: ($udom,$uname,$file) =
1.811 albertel 6312: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 6313: $file = 'userfiles/'.$file;
1.740 www 6314: $dir = &propath($udom,$uname);
1.712 albertel 6315: }
6316: if ($uri =~ m-^/res/-) {
6317: ($udom,$uname) =
1.807 albertel 6318: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 6319: $file = $uri;
6320: }
6321:
6322: if (!$udom || !$uname || !$file) {
6323: # unable to handle the uri
6324: return ();
6325: }
6326:
6327: my ($result) = &dirlist($file,$udom,$uname,$dir);
6328: my @stats = split('&', $result);
1.721 banghart 6329:
1.712 albertel 6330: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
6331: shift(@stats); #filename is first
6332: return @stats;
6333: }
6334: return ();
6335: }
6336:
1.26 www 6337: # -------------------------------------------------------- Value of a Condition
6338:
1.713 albertel 6339: # gets the value of a specific preevaluated condition
6340: # stored in the string $env{user.state.<cid>}
6341: # or looks up a condition reference in the bighash and if if hasn't
6342: # already been evaluated recurses into docondval to get the value of
6343: # the condition, then memoizing it to
6344: # $env{user.state.<cid>.<condition>}
1.40 www 6345: sub directcondval {
6346: my $number=shift;
1.620 albertel 6347: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 6348: &Apache::lonuserstate::evalstate();
6349: }
1.713 albertel 6350: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
6351: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
6352: } elsif ($number =~ /^_/) {
6353: my $sub_condition;
6354: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
6355: &GDBM_READER(),0640)) {
6356: $sub_condition=$bighash{'conditions'.$number};
6357: untie(%bighash);
6358: }
6359: my $value = &docondval($sub_condition);
1.949 raeburn 6360: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 6361: return $value;
6362: }
1.620 albertel 6363: if ($env{'user.state.'.$env{'request.course.id'}}) {
6364: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 6365: } else {
6366: return 2;
6367: }
6368: }
6369:
1.713 albertel 6370: # get the collection of conditions for this resource
1.26 www 6371: sub condval {
6372: my $condidx=shift;
1.54 www 6373: my $allpathcond='';
1.713 albertel 6374: foreach my $cond (split(/\|/,$condidx)) {
6375: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
6376: $allpathcond.=
6377: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
6378: }
1.191 harris41 6379: }
1.54 www 6380: $allpathcond=~s/\|$//;
1.713 albertel 6381: return &docondval($allpathcond);
6382: }
6383:
6384: #evaluates an expression of conditions
6385: sub docondval {
6386: my ($allpathcond) = @_;
6387: my $result=0;
6388: if ($env{'request.course.id'}
6389: && defined($allpathcond)) {
6390: my $operand='|';
6391: my @stack;
6392: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
6393: if ($chunk eq '(') {
6394: push @stack,($operand,$result);
6395: } elsif ($chunk eq ')') {
6396: my $before=pop @stack;
6397: if (pop @stack eq '&') {
6398: $result=$result>$before?$before:$result;
6399: } else {
6400: $result=$result>$before?$result:$before;
6401: }
6402: } elsif (($chunk eq '&') || ($chunk eq '|')) {
6403: $operand=$chunk;
6404: } else {
6405: my $new=directcondval($chunk);
6406: if ($operand eq '&') {
6407: $result=$result>$new?$new:$result;
6408: } else {
6409: $result=$result>$new?$result:$new;
6410: }
6411: }
6412: }
1.26 www 6413: }
6414: return $result;
1.421 albertel 6415: }
6416:
6417: # ---------------------------------------------------- Devalidate courseresdata
6418:
6419: sub devalidatecourseresdata {
6420: my ($coursenum,$coursedomain)=@_;
6421: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 6422: &devalidate_cache_new('courseres',$hashid);
1.28 www 6423: }
6424:
1.763 www 6425:
1.200 www 6426: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 6427: #
6428: # Parameters:
6429: # $coursenum - Number of the course.
6430: # $coursedomain - Domain at which the course was created.
6431: # Returns:
6432: # A hash of the course parameters along (I think) with timestamps
6433: # and version info.
1.877 foxr 6434:
1.624 albertel 6435: sub get_courseresdata {
6436: my ($coursenum,$coursedomain)=@_;
1.200 www 6437: my $coursehom=&homeserver($coursenum,$coursedomain);
6438: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 6439: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 6440: my %dumpreply;
1.417 albertel 6441: unless (defined($cached)) {
1.624 albertel 6442: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 6443: $result=\%dumpreply;
1.251 albertel 6444: my ($tmp) = keys(%dumpreply);
6445: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 6446: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 6447: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
6448: return $tmp;
1.416 albertel 6449: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 6450: $result=undef;
1.599 albertel 6451: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 6452: }
6453: }
1.624 albertel 6454: return $result;
6455: }
6456:
1.633 albertel 6457: sub devalidateuserresdata {
6458: my ($uname,$udom)=@_;
6459: my $hashid="$udom:$uname";
6460: &devalidate_cache_new('userres',$hashid);
6461: }
6462:
1.624 albertel 6463: sub get_userresdata {
6464: my ($uname,$udom)=@_;
6465: #most student don\'t have any data set, check if there is some data
6466: if (&EXT_cache_status($udom,$uname)) { return undef; }
6467:
6468: my $hashid="$udom:$uname";
6469: my ($result,$cached)=&is_cached_new('userres',$hashid);
6470: if (!defined($cached)) {
6471: my %resourcedata=&dump('resourcedata',$udom,$uname);
6472: $result=\%resourcedata;
6473: &do_cache_new('userres',$hashid,$result,600);
6474: }
6475: my ($tmp)=keys(%$result);
6476: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
6477: return $result;
6478: }
6479: #error 2 occurs when the .db doesn't exist
6480: if ($tmp!~/error: 2 /) {
1.672 albertel 6481: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 6482: " Trying to get resource data for ".
6483: $uname." at ".$udom.": ".
6484: $tmp."</font>");
6485: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 6486: #&EXT_cache_set($udom,$uname);
6487: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 6488: undef($tmp); # not really an error so don't send it back
1.624 albertel 6489: }
6490: return $tmp;
6491: }
1.879 foxr 6492: #----------------------------------------------- resdata - return resource data
6493: # Purpose:
6494: # Return resource data for either users or for a course.
6495: # Parameters:
6496: # $name - Course/user name.
6497: # $domain - Name of the domain the user/course is registered on.
6498: # $type - Type of thing $name is (must be 'course' or 'user'
6499: # @which - Array of names of resources desired.
6500: # Returns:
6501: # The value of the first reasource in @which that is found in the
6502: # resource hash.
6503: # Exceptional Conditions:
6504: # If the $type passed in is not valid (not the string 'course' or
6505: # 'user', an undefined reference is returned.
6506: # If none of the resources are found, an undef is returned
1.624 albertel 6507: sub resdata {
6508: my ($name,$domain,$type,@which)=@_;
6509: my $result;
6510: if ($type eq 'course') {
6511: $result=&get_courseresdata($name,$domain);
6512: } elsif ($type eq 'user') {
6513: $result=&get_userresdata($name,$domain);
6514: }
6515: if (!ref($result)) { return $result; }
1.251 albertel 6516: foreach my $item (@which) {
1.927 albertel 6517: if (defined($result->{$item->[0]})) {
6518: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 6519: }
1.250 albertel 6520: }
1.291 albertel 6521: return undef;
1.200 www 6522: }
6523:
1.379 matthew 6524: #
6525: # EXT resource caching routines
6526: #
6527:
6528: sub clear_EXT_cache_status {
1.383 albertel 6529: &delenv('cache.EXT.');
1.379 matthew 6530: }
6531:
6532: sub EXT_cache_status {
6533: my ($target_domain,$target_user) = @_;
1.383 albertel 6534: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 6535: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 6536: # We know already the user has no data
6537: return 1;
6538: } else {
6539: return 0;
6540: }
6541: }
6542:
6543: sub EXT_cache_set {
6544: my ($target_domain,$target_user) = @_;
1.383 albertel 6545: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 6546: #&appenv({$cachename => time});
1.379 matthew 6547: }
6548:
1.28 www 6549: # --------------------------------------------------------- Value of a Variable
1.58 www 6550: sub EXT {
1.715 albertel 6551:
1.395 albertel 6552: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 6553: unless ($varname) { return ''; }
1.218 albertel 6554: #get real user name/domain, courseid and symb
6555: my $courseid;
1.359 albertel 6556: my $publicuser;
1.427 www 6557: if ($symbparm) {
6558: $symbparm=&get_symb_from_alias($symbparm);
6559: }
1.218 albertel 6560: if (!($uname && $udom)) {
1.790 albertel 6561: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 6562: if (!$symbparm) { $symbparm=$cursymb; }
6563: } else {
1.620 albertel 6564: $courseid=$env{'request.course.id'};
1.218 albertel 6565: }
1.48 www 6566: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
6567: my $rest;
1.320 albertel 6568: if (defined($therest[0])) {
1.48 www 6569: $rest=join('.',@therest);
6570: } else {
6571: $rest='';
6572: }
1.320 albertel 6573:
1.57 www 6574: my $qualifierrest=$qualifier;
6575: if ($rest) { $qualifierrest.='.'.$rest; }
6576: my $spacequalifierrest=$space;
6577: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 6578: if ($realm eq 'user') {
1.48 www 6579: # --------------------------------------------------------------- user.resource
6580: if ($space eq 'resource') {
1.651 albertel 6581: if ( (defined($Apache::lonhomework::parsing_a_problem)
6582: || defined($Apache::lonhomework::parsing_a_task))
6583: &&
1.744 albertel 6584: ($symbparm eq &symbread()) ) {
6585: # if we are in the middle of processing the resource the
6586: # get the value we are planning on committing
6587: if (defined($Apache::lonhomework::results{$qualifierrest})) {
6588: return $Apache::lonhomework::results{$qualifierrest};
6589: } else {
6590: return $Apache::lonhomework::history{$qualifierrest};
6591: }
1.335 albertel 6592: } else {
1.359 albertel 6593: my %restored;
1.620 albertel 6594: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 6595: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
6596: } else {
6597: %restored=&restore($symbparm,$courseid,$udom,$uname);
6598: }
1.335 albertel 6599: return $restored{$qualifierrest};
6600: }
1.48 www 6601: # ----------------------------------------------------------------- user.access
6602: } elsif ($space eq 'access') {
1.218 albertel 6603: # FIXME - not supporting calls for a specific user
1.48 www 6604: return &allowed($qualifier,$rest);
6605: # ------------------------------------------ user.preferences, user.environment
6606: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 6607: if (($uname eq $env{'user.name'}) &&
6608: ($udom eq $env{'user.domain'})) {
6609: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 6610: } else {
1.359 albertel 6611: my %returnhash;
6612: if (!$publicuser) {
6613: %returnhash=&userenvironment($udom,$uname,
6614: $qualifierrest);
6615: }
1.218 albertel 6616: return $returnhash{$qualifierrest};
6617: }
1.48 www 6618: # ----------------------------------------------------------------- user.course
6619: } elsif ($space eq 'course') {
1.218 albertel 6620: # FIXME - not supporting calls for a specific user
1.620 albertel 6621: return $env{join('.',('request.course',$qualifier))};
1.48 www 6622: # ------------------------------------------------------------------- user.role
6623: } elsif ($space eq 'role') {
1.218 albertel 6624: # FIXME - not supporting calls for a specific user
1.620 albertel 6625: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 6626: if ($qualifier eq 'value') {
6627: return $role;
6628: } elsif ($qualifier eq 'extent') {
6629: return $where;
6630: }
6631: # ----------------------------------------------------------------- user.domain
6632: } elsif ($space eq 'domain') {
1.218 albertel 6633: return $udom;
1.48 www 6634: # ------------------------------------------------------------------- user.name
6635: } elsif ($space eq 'name') {
1.218 albertel 6636: return $uname;
1.48 www 6637: # ---------------------------------------------------- Any other user namespace
1.29 www 6638: } else {
1.359 albertel 6639: my %reply;
6640: if (!$publicuser) {
6641: %reply=&get($space,[$qualifierrest],$udom,$uname);
6642: }
6643: return $reply{$qualifierrest};
1.48 www 6644: }
1.236 www 6645: } elsif ($realm eq 'query') {
6646: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 6647: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
6648: [$spacequalifierrest]);
1.620 albertel 6649: return $env{'form.'.$spacequalifierrest};
1.236 www 6650: } elsif ($realm eq 'request') {
1.48 www 6651: # ------------------------------------------------------------- request.browser
6652: if ($space eq 'browser') {
1.430 www 6653: if ($qualifier eq 'textremote') {
1.676 albertel 6654: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 6655: return 1;
6656: } else {
6657: return 0;
6658: }
6659: } else {
1.620 albertel 6660: return $env{'browser.'.$qualifier};
1.430 www 6661: }
1.57 www 6662: # ------------------------------------------------------------ request.filename
6663: } else {
1.620 albertel 6664: return $env{'request.'.$spacequalifierrest};
1.29 www 6665: }
1.28 www 6666: } elsif ($realm eq 'course') {
1.48 www 6667: # ---------------------------------------------------------- course.description
1.620 albertel 6668: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 6669: } elsif ($realm eq 'resource') {
1.165 www 6670:
1.620 albertel 6671: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 6672: if (!$symbparm) { $symbparm=&symbread(); }
6673: }
1.693 albertel 6674:
6675: if ($space eq 'title') {
6676: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
6677: return &gettitle($symbparm);
6678: }
6679:
6680: if ($space eq 'map') {
6681: my ($map) = &decode_symb($symbparm);
6682: return &symbread($map);
6683: }
1.905 albertel 6684: if ($space eq 'filename') {
6685: if ($symbparm) {
6686: return &clutter((&decode_symb($symbparm))[2]);
6687: }
6688: return &hreflocation('',$env{'request.filename'});
6689: }
1.693 albertel 6690:
6691: my ($section, $group, @groups);
1.593 albertel 6692: my ($courselevelm,$courselevel);
1.539 albertel 6693: if ($symbparm && defined($courseid) &&
1.620 albertel 6694: $courseid eq $env{'request.course.id'}) {
1.165 www 6695:
1.218 albertel 6696: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 6697:
1.60 www 6698: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 6699: my $symbp=$symbparm;
1.735 albertel 6700: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 6701:
6702: my $symbparm=$symbp.'.'.$spacequalifierrest;
6703: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
6704:
1.620 albertel 6705: if (($env{'user.name'} eq $uname) &&
6706: ($env{'user.domain'} eq $udom)) {
6707: $section=$env{'request.course.sec'};
1.733 raeburn 6708: @groups = split(/:/,$env{'request.course.groups'});
6709: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 6710: } else {
1.539 albertel 6711: if (! defined($usection)) {
1.551 albertel 6712: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 6713: } else {
6714: $section = $usection;
6715: }
1.733 raeburn 6716: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 6717: }
6718:
6719: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
6720: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
6721: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
6722:
1.593 albertel 6723: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 6724: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 6725: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 6726:
1.60 www 6727: # ----------------------------------------------------------- first, check user
1.624 albertel 6728:
6729: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 6730: ([$courselevelr,'resource'],
6731: [$courselevelm,'map' ],
6732: [$courselevel, 'course' ]));
1.931 albertel 6733: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 6734:
1.594 albertel 6735: # ------------------------------------------------ second, check some of course
1.684 raeburn 6736: my $coursereply;
1.691 raeburn 6737: if (@groups > 0) {
6738: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
6739: $mapparm,$spacequalifierrest);
1.927 albertel 6740: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 6741: }
1.96 www 6742:
1.684 raeburn 6743: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 6744: $env{'course.'.$courseid.'.domain'},
6745: 'course',
6746: ([$seclevelr, 'resource'],
6747: [$seclevelm, 'map' ],
6748: [$seclevel, 'course' ],
6749: [$courselevelr,'resource']));
6750: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 6751:
1.60 www 6752: # ------------------------------------------------------ third, check map parms
1.218 albertel 6753: my %parmhash=();
6754: my $thisparm='';
6755: if (tie(%parmhash,'GDBM_File',
1.620 albertel 6756: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 6757: &GDBM_READER(),0640)) {
1.218 albertel 6758: $thisparm=$parmhash{$symbparm};
6759: untie(%parmhash);
6760: }
1.927 albertel 6761: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 6762: }
1.594 albertel 6763: # ------------------------------------------ fourth, look in resource metadata
1.71 www 6764:
1.218 albertel 6765: $spacequalifierrest=~s/\./\_/;
1.282 albertel 6766: my $filename;
6767: if (!$symbparm) { $symbparm=&symbread(); }
6768: if ($symbparm) {
1.409 www 6769: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 6770: } else {
1.620 albertel 6771: $filename=$env{'request.filename'};
1.282 albertel 6772: }
6773: my $metadata=&metadata($filename,$spacequalifierrest);
1.927 albertel 6774: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282 albertel 6775: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927 albertel 6776: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 6777:
1.927 albertel 6778: # ---------------------------------------------- fourth, look in rest of course
1.593 albertel 6779: if ($symbparm && defined($courseid) &&
1.620 albertel 6780: $courseid eq $env{'request.course.id'}) {
1.624 albertel 6781: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
6782: $env{'course.'.$courseid.'.domain'},
6783: 'course',
1.927 albertel 6784: ([$courselevelm,'map' ],
6785: [$courselevel, 'course']));
6786: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 6787: }
1.145 www 6788: # ------------------------------------------------------------------ Cascade up
1.218 albertel 6789: unless ($space eq '0') {
1.336 albertel 6790: my @parts=split(/_/,$space);
6791: my $id=pop(@parts);
6792: my $part=join('_',@parts);
6793: if ($part eq '') { $part='0'; }
1.927 albertel 6794: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 6795: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 6796: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 6797: }
1.395 albertel 6798: if ($recurse) { return undef; }
6799: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 6800: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 6801: # ---------------------------------------------------- Any other user namespace
6802: } elsif ($realm eq 'environment') {
6803: # ----------------------------------------------------------------- environment
1.620 albertel 6804: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
6805: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 6806: } else {
1.770 albertel 6807: if ($uname eq 'anonymous' && $udom eq '') {
6808: return '';
6809: }
1.219 albertel 6810: my %returnhash=&userenvironment($udom,$uname,
6811: $spacequalifierrest);
6812: return $returnhash{$spacequalifierrest};
6813: }
1.28 www 6814: } elsif ($realm eq 'system') {
1.48 www 6815: # ----------------------------------------------------------------- system.time
6816: if ($space eq 'time') {
6817: return time;
6818: }
1.696 albertel 6819: } elsif ($realm eq 'server') {
6820: # ----------------------------------------------------------------- system.time
6821: if ($space eq 'name') {
6822: return $ENV{'SERVER_NAME'};
6823: }
1.28 www 6824: }
1.48 www 6825: return '';
1.61 www 6826: }
6827:
1.927 albertel 6828: sub get_reply {
6829: my ($reply_value) = @_;
1.940 raeburn 6830: if (ref($reply_value) eq 'ARRAY') {
6831: if (wantarray) {
6832: return @$reply_value;
6833: }
6834: return $reply_value->[0];
6835: } else {
6836: return $reply_value;
1.927 albertel 6837: }
6838: }
6839:
1.691 raeburn 6840: sub check_group_parms {
6841: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
6842: my @groupitems = ();
6843: my $resultitem;
1.927 albertel 6844: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 6845: foreach my $group (@{$groups}) {
6846: foreach my $level (@levels) {
1.927 albertel 6847: my $item = $courseid.'.['.$group.'].'.$level->[0];
6848: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 6849: }
6850: }
6851: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
6852: $env{'course.'.$courseid.'.domain'},
6853: 'course',@groupitems);
6854: return $coursereply;
6855: }
6856:
6857: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 6858: my ($courseid,@groups) = @_;
6859: @groups = sort(@groups);
1.691 raeburn 6860: return @groups;
6861: }
6862:
1.395 albertel 6863: sub packages_tab_default {
6864: my ($uri,$varname)=@_;
6865: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 6866:
6867: my (@extension,@specifics,$do_default);
6868: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 6869: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 6870: if ($pack_type eq 'default') {
6871: $do_default=1;
6872: } elsif ($pack_type eq 'extension') {
6873: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 6874: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 6875: # only look at packages defaults for packages that this id is
1.738 albertel 6876: push(@specifics,[$package,$pack_type,$pack_part]);
6877: }
6878: }
6879: # first look for a package that matches the requested part id
6880: foreach my $package (@specifics) {
6881: my (undef,$pack_type,$pack_part)=@{$package};
6882: next if ($pack_part ne $part);
6883: if (defined($packagetab{"$pack_type&$name&default"})) {
6884: return $packagetab{"$pack_type&$name&default"};
6885: }
6886: }
6887: # look for any possible matching non extension_ package
6888: foreach my $package (@specifics) {
6889: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 6890: if (defined($packagetab{"$pack_type&$name&default"})) {
6891: return $packagetab{"$pack_type&$name&default"};
6892: }
1.585 albertel 6893: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 6894: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
6895: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 6896: }
6897: }
1.738 albertel 6898: # look for any posible extension_ match
6899: foreach my $package (@extension) {
6900: my ($package,$pack_type)=@{$package};
6901: if (defined($packagetab{"$pack_type&$name&default"})) {
6902: return $packagetab{"$pack_type&$name&default"};
6903: }
6904: if (defined($packagetab{$package."&$name&default"})) {
6905: return $packagetab{$package."&$name&default"};
6906: }
6907: }
6908: # look for a global default setting
6909: if ($do_default && defined($packagetab{"default&$name&default"})) {
6910: return $packagetab{"default&$name&default"};
6911: }
1.395 albertel 6912: return undef;
6913: }
6914:
1.334 albertel 6915: sub add_prefix_and_part {
6916: my ($prefix,$part)=@_;
6917: my $keyroot;
6918: if (defined($prefix) && $prefix !~ /^__/) {
6919: # prefix that has a part already
6920: $keyroot=$prefix;
6921: } elsif (defined($prefix)) {
6922: # prefix that is missing a part
6923: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
6924: } else {
6925: # no prefix at all
6926: if (defined($part)) { $keyroot='_'.$part; }
6927: }
6928: return $keyroot;
6929: }
6930:
1.71 www 6931: # ---------------------------------------------------------------- Get metadata
6932:
1.599 albertel 6933: my %metaentry;
1.71 www 6934: sub metadata {
1.176 www 6935: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 6936: $uri=&declutter($uri);
1.288 albertel 6937: # if it is a non metadata possible uri return quickly
1.529 albertel 6938: if (($uri eq '') ||
6939: (($uri =~ m|^/*adm/|) &&
1.698 albertel 6940: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.924 albertel 6941: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) ) {
6942: return undef;
6943: }
6944: if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/})
6945: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 6946: return undef;
1.288 albertel 6947: }
1.73 www 6948: my $filename=$uri;
6949: $uri=~s/\.meta$//;
1.172 www 6950: #
6951: # Is the metadata already cached?
1.177 www 6952: # Look at timestamp of caching
1.172 www 6953: # Everything is cached by the main uri, libraries are never directly cached
6954: #
1.428 albertel 6955: if (!defined($liburi)) {
1.599 albertel 6956: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 6957: if (defined($cached)) { return $result->{':'.$what}; }
6958: }
6959: {
1.172 www 6960: #
6961: # Is this a recursive call for a library?
6962: #
1.599 albertel 6963: # if (! exists($metacache{$uri})) {
6964: # $metacache{$uri}={};
6965: # }
1.924 albertel 6966: my $cachetime = 60*60;
1.171 www 6967: if ($liburi) {
6968: $liburi=&declutter($liburi);
6969: $filename=$liburi;
1.401 bowersj2 6970: } else {
1.599 albertel 6971: &devalidate_cache_new('meta',$uri);
6972: undef(%metaentry);
1.401 bowersj2 6973: }
1.140 www 6974: my %metathesekeys=();
1.73 www 6975: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 6976: my $metastring;
1.924 albertel 6977: if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929 albertel 6978: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 6979: $metastring =
1.929 albertel 6980: &Apache::lonnet::ssi_body($which,
1.924 albertel 6981: ('grade_target' => 'meta'));
6982: $cachetime = 1; # only want this cached in the child not long term
6983: } elsif ($uri !~ m -^(editupload)/-) {
1.543 albertel 6984: my $file=&filelocation('',&clutter($filename));
1.599 albertel 6985: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 6986: $metastring=&getfile($file);
1.489 albertel 6987: }
1.208 albertel 6988: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 6989: my $token;
1.140 www 6990: undef %metathesekeys;
1.71 www 6991: while ($token=$parser->get_token) {
1.339 albertel 6992: if ($token->[0] eq 'S') {
6993: if (defined($token->[2]->{'package'})) {
1.172 www 6994: #
6995: # This is a package - get package info
6996: #
1.339 albertel 6997: my $package=$token->[2]->{'package'};
6998: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
6999: if (defined($token->[2]->{'id'})) {
7000: $keyroot.='_'.$token->[2]->{'id'};
7001: }
1.599 albertel 7002: if ($metaentry{':packages'}) {
7003: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 7004: } else {
1.599 albertel 7005: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 7006: }
1.736 albertel 7007: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 7008: my $part=$keyroot;
7009: $part=~s/^\_//;
1.736 albertel 7010: if ($pack_entry=~/^\Q$package\E\&/ ||
7011: $pack_entry=~/^\Q$package\E_0\&/) {
7012: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 7013: # ignore package.tab specified default values
7014: # here &package_tab_default() will fetch those
7015: if ($subp eq 'default') { next; }
1.736 albertel 7016: my $value=$packagetab{$pack_entry};
1.432 albertel 7017: my $unikey;
7018: if ($pack =~ /_0$/) {
7019: $unikey='parameter_0_'.$name;
7020: $part=0;
7021: } else {
7022: $unikey='parameter'.$keyroot.'_'.$name;
7023: }
1.339 albertel 7024: if ($subp eq 'display') {
7025: $value.=' [Part: '.$part.']';
7026: }
1.599 albertel 7027: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 7028: $metathesekeys{$unikey}=1;
1.599 albertel 7029: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
7030: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 7031: }
1.599 albertel 7032: if (defined($metaentry{':'.$unikey.'.default'})) {
7033: $metaentry{':'.$unikey}=
7034: $metaentry{':'.$unikey.'.default'};
1.356 albertel 7035: }
1.339 albertel 7036: }
7037: }
7038: } else {
1.172 www 7039: #
7040: # This is not a package - some other kind of start tag
1.339 albertel 7041: #
7042: my $entry=$token->[1];
7043: my $unikey;
7044: if ($entry eq 'import') {
7045: $unikey='';
7046: } else {
7047: $unikey=$entry;
7048: }
7049: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
7050:
7051: if (defined($token->[2]->{'id'})) {
7052: $unikey.='_'.$token->[2]->{'id'};
7053: }
1.175 www 7054:
1.339 albertel 7055: if ($entry eq 'import') {
1.175 www 7056: #
7057: # Importing a library here
1.339 albertel 7058: #
7059: if ($depthcount<20) {
7060: my $location=$parser->get_text('/import');
7061: my $dir=$filename;
7062: $dir=~s|[^/]*$||;
7063: $location=&filelocation($dir,$location);
1.736 albertel 7064: my $metadata =
7065: &metadata($uri,'keys', $location,$unikey,
7066: $depthcount+1);
7067: foreach my $meta (split(',',$metadata)) {
7068: $metaentry{':'.$meta}=$metaentry{':'.$meta};
7069: $metathesekeys{$meta}=1;
1.339 albertel 7070: }
7071: }
7072: } else {
7073:
7074: if (defined($token->[2]->{'name'})) {
7075: $unikey.='_'.$token->[2]->{'name'};
7076: }
7077: $metathesekeys{$unikey}=1;
1.736 albertel 7078: foreach my $param (@{$token->[3]}) {
7079: $metaentry{':'.$unikey.'.'.$param} =
7080: $token->[2]->{$param};
1.339 albertel 7081: }
7082: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 7083: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 7084: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
7085: # only ws inside the tag, and not in default, so use default
7086: # as value
1.599 albertel 7087: $metaentry{':'.$unikey}=$default;
1.908 albertel 7088: } elsif ( $internaltext =~ /\S/ ) {
7089: # something interesting inside the tag
7090: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 7091: } else {
1.908 albertel 7092: # no interesting values, don't set a default
1.339 albertel 7093: }
1.172 www 7094: # end of not-a-package not-a-library import
1.339 albertel 7095: }
1.172 www 7096: # end of not-a-package start tag
1.339 albertel 7097: }
1.172 www 7098: # the next is the end of "start tag"
1.339 albertel 7099: }
7100: }
1.483 albertel 7101: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 7102: $extension = lc($extension);
7103: if ($extension eq 'htm') { $extension='html'; }
7104:
1.737 albertel 7105: foreach my $key (keys(%packagetab)) {
1.483 albertel 7106: #no specific packages #how's our extension
7107: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 7108: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 7109: \%metathesekeys);
7110: }
1.883 albertel 7111:
7112: if (!exists($metaentry{':packages'})
7113: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 7114: foreach my $key (keys(%packagetab)) {
1.483 albertel 7115: #no specific packages well let's get default then
7116: if ($key!~/^default&/) { next; }
1.488 albertel 7117: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 7118: \%metathesekeys);
7119: }
7120: }
1.338 www 7121: # are there custom rights to evaluate
1.599 albertel 7122: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 7123:
1.338 www 7124: #
7125: # Importing a rights file here
1.339 albertel 7126: #
7127: unless ($depthcount) {
1.599 albertel 7128: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 7129: my $dir=$filename;
7130: $dir=~s|[^/]*$||;
7131: $location=&filelocation($dir,$location);
1.736 albertel 7132: my $rights_metadata =
7133: &metadata($uri,'keys',$location,'_rights',
7134: $depthcount+1);
7135: foreach my $rights (split(',',$rights_metadata)) {
7136: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
7137: $metathesekeys{$rights}=1;
1.339 albertel 7138: }
7139: }
7140: }
1.737 albertel 7141: # uniqifiy package listing
7142: my %seen;
7143: my @uniq_packages =
7144: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
7145: $metaentry{':packages'} = join(',',@uniq_packages);
7146:
7147: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 7148: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
7149: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924 albertel 7150: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 7151: # this is the end of "was not already recently cached
1.71 www 7152: }
1.599 albertel 7153: return $metaentry{':'.$what};
1.261 albertel 7154: }
7155:
1.488 albertel 7156: sub metadata_create_package_def {
1.483 albertel 7157: my ($uri,$key,$package,$metathesekeys)=@_;
7158: my ($pack,$name,$subp)=split(/\&/,$key);
7159: if ($subp eq 'default') { next; }
7160:
1.599 albertel 7161: if (defined($metaentry{':packages'})) {
7162: $metaentry{':packages'}.=','.$package;
1.483 albertel 7163: } else {
1.599 albertel 7164: $metaentry{':packages'}=$package;
1.483 albertel 7165: }
7166: my $value=$packagetab{$key};
7167: my $unikey;
7168: $unikey='parameter_0_'.$name;
1.599 albertel 7169: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 7170: $$metathesekeys{$unikey}=1;
1.599 albertel 7171: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
7172: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 7173: }
1.599 albertel 7174: if (defined($metaentry{':'.$unikey.'.default'})) {
7175: $metaentry{':'.$unikey}=
7176: $metaentry{':'.$unikey.'.default'};
1.483 albertel 7177: }
7178: }
7179:
1.261 albertel 7180: sub metadata_generate_part0 {
7181: my ($metadata,$metacache,$uri) = @_;
7182: my %allnames;
1.737 albertel 7183: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 7184: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 7185: my $part=$$metacache{':'.$metakey.'.part'};
7186: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 7187: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 7188: $allnames{$name}=$part;
7189: }
7190: }
7191: }
7192: foreach my $name (keys(%allnames)) {
7193: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 7194: my $key=":parameter_0_$name";
1.261 albertel 7195: $$metacache{"$key.part"}='0';
7196: $$metacache{"$key.name"}=$name;
1.428 albertel 7197: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 7198: $allnames{$name}.'_'.$name.
7199: '.type'};
1.428 albertel 7200: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 7201: '.display'};
1.644 www 7202: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 7203: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 7204: $$metacache{"$key.display"}=$olddis;
7205: }
1.71 www 7206: }
7207:
1.764 albertel 7208: # ------------------------------------------------------ Devalidate title cache
7209:
7210: sub devalidate_title_cache {
7211: my ($url)=@_;
7212: if (!$env{'request.course.id'}) { return; }
7213: my $symb=&symbread($url);
7214: if (!$symb) { return; }
7215: my $key=$env{'request.course.id'}."\0".$symb;
7216: &devalidate_cache_new('title',$key);
7217: }
7218:
1.301 www 7219: # ------------------------------------------------- Get the title of a resource
7220:
7221: sub gettitle {
7222: my $urlsymb=shift;
7223: my $symb=&symbread($urlsymb);
1.534 albertel 7224: if ($symb) {
1.620 albertel 7225: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 7226: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 7227: if (defined($cached)) {
7228: return $result;
7229: }
1.534 albertel 7230: my ($map,$resid,$url)=&decode_symb($symb);
7231: my $title='';
1.907 albertel 7232: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
7233: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
7234: } else {
7235: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
7236: &GDBM_READER(),0640)) {
7237: my $mapid=$bighash{'map_pc_'.&clutter($map)};
7238: $title=$bighash{'title_'.$mapid.'.'.$resid};
7239: untie(%bighash);
7240: }
1.534 albertel 7241: }
7242: $title=~s/\&colon\;/\:/gs;
7243: if ($title) {
1.599 albertel 7244: return &do_cache_new('title',$key,$title,600);
1.534 albertel 7245: }
7246: $urlsymb=$url;
7247: }
7248: my $title=&metadata($urlsymb,'title');
7249: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
7250: return $title;
1.301 www 7251: }
1.613 albertel 7252:
1.614 albertel 7253: sub get_slot {
7254: my ($which,$cnum,$cdom)=@_;
7255: if (!$cnum || !$cdom) {
1.790 albertel 7256: (undef,my $courseid)=&whichuser();
1.620 albertel 7257: $cdom=$env{'course.'.$courseid.'.domain'};
7258: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 7259: }
1.703 albertel 7260: my $key=join("\0",'slots',$cdom,$cnum,$which);
7261: my %slotinfo;
7262: if (exists($remembered{$key})) {
7263: $slotinfo{$which} = $remembered{$key};
7264: } else {
7265: %slotinfo=&get('slots',[$which],$cdom,$cnum);
7266: &Apache::lonhomework::showhash(%slotinfo);
7267: my ($tmp)=keys(%slotinfo);
7268: if ($tmp=~/^error:/) { return (); }
7269: $remembered{$key} = $slotinfo{$which};
7270: }
1.616 albertel 7271: if (ref($slotinfo{$which}) eq 'HASH') {
7272: return %{$slotinfo{$which}};
7273: }
7274: return $slotinfo{$which};
1.614 albertel 7275: }
1.31 www 7276: # ------------------------------------------------- Update symbolic store links
7277:
7278: sub symblist {
7279: my ($mapname,%newhash)=@_;
1.438 www 7280: $mapname=&deversion(&declutter($mapname));
1.31 www 7281: my %hash;
1.620 albertel 7282: if (($env{'request.course.fn'}) && (%newhash)) {
7283: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 7284: &GDBM_WRCREAT(),0640)) {
1.711 albertel 7285: foreach my $url (keys %newhash) {
7286: next if ($url eq 'last_known'
7287: && $env{'form.no_update_last_known'});
7288: $hash{declutter($url)}=&encode_symb($mapname,
7289: $newhash{$url}->[1],
7290: $newhash{$url}->[0]);
1.191 harris41 7291: }
1.31 www 7292: if (untie(%hash)) {
7293: return 'ok';
7294: }
7295: }
7296: }
7297: return 'error';
1.212 www 7298: }
7299:
7300: # --------------------------------------------------------------- Verify a symb
7301:
7302: sub symbverify {
1.510 www 7303: my ($symb,$thisurl)=@_;
7304: my $thisfn=$thisurl;
1.439 www 7305: $thisfn=&declutter($thisfn);
1.215 www 7306: # direct jump to resource in page or to a sequence - will construct own symbs
7307: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
7308: # check URL part
1.409 www 7309: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 7310:
1.431 www 7311: unless ($url eq $thisfn) { return 0; }
1.213 www 7312:
1.216 www 7313: $symb=&symbclean($symb);
1.510 www 7314: $thisurl=&deversion($thisurl);
1.439 www 7315: $thisfn=&deversion($thisfn);
1.213 www 7316:
7317: my %bighash;
7318: my $okay=0;
1.431 www 7319:
1.620 albertel 7320: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 7321: &GDBM_READER(),0640)) {
1.510 www 7322: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 7323: unless ($ids) {
1.510 www 7324: $ids=$bighash{'ids_/'.$thisurl};
1.216 www 7325: }
7326: if ($ids) {
7327: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 7328: foreach my $id (split(/\,/,$ids)) {
7329: my ($mapid,$resid)=split(/\./,$id);
1.216 www 7330: if (
7331: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
7332: eq $symb) {
1.620 albertel 7333: if (($env{'request.role.adv'}) ||
1.800 albertel 7334: $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582 albertel 7335: $okay=1;
7336: }
7337: }
1.216 www 7338: }
7339: }
1.213 www 7340: untie(%bighash);
7341: }
7342: return $okay;
1.31 www 7343: }
7344:
1.210 www 7345: # --------------------------------------------------------------- Clean-up symb
7346:
7347: sub symbclean {
7348: my $symb=shift;
1.568 albertel 7349: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 7350: # remove version from map
7351: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 7352:
1.210 www 7353: # remove version from URL
7354: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 7355:
1.507 www 7356: # remove wrapper
7357:
1.510 www 7358: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 7359: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 7360: return $symb;
1.409 www 7361: }
7362:
7363: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 7364:
7365: sub encode_symb {
7366: my ($map,$resid,$url)=@_;
7367: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
7368: }
1.409 www 7369:
7370: sub decode_symb {
1.568 albertel 7371: my $symb=shift;
7372: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
7373: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 7374: return (&fixversion($map),$resid,&fixversion($url));
7375: }
7376:
7377: sub fixversion {
7378: my $fn=shift;
1.609 banghart 7379: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 7380: my %bighash;
7381: my $uri=&clutter($fn);
1.620 albertel 7382: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 7383: # is this cached?
1.599 albertel 7384: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 7385: if (defined($cached)) { return $result; }
7386: # unfortunately not cached, or expired
1.620 albertel 7387: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 7388: &GDBM_READER(),0640)) {
7389: if ($bighash{'version_'.$uri}) {
7390: my $version=$bighash{'version_'.$uri};
1.444 www 7391: unless (($version eq 'mostrecent') ||
7392: ($version==&getversion($uri))) {
1.440 www 7393: $uri=~s/\.(\w+)$/\.$version\.$1/;
7394: }
7395: }
7396: untie %bighash;
1.413 www 7397: }
1.599 albertel 7398: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 7399: }
7400:
7401: sub deversion {
7402: my $url=shift;
7403: $url=~s/\.\d+\.(\w+)$/\.$1/;
7404: return $url;
1.210 www 7405: }
7406:
1.31 www 7407: # ------------------------------------------------------ Return symb list entry
7408:
7409: sub symbread {
1.249 www 7410: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 7411: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 7412: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 7413: # no filename provided? try from environment
1.44 www 7414: unless ($thisfn) {
1.620 albertel 7415: if ($env{'request.symb'}) {
7416: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 7417: }
1.620 albertel 7418: $thisfn=$env{'request.filename'};
1.44 www 7419: }
1.569 albertel 7420: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 7421: # is that filename actually a symb? Verify, clean, and return
7422: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 7423: if (&symbverify($thisfn,$1)) {
1.620 albertel 7424: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 7425: }
1.242 www 7426: }
1.44 www 7427: $thisfn=declutter($thisfn);
1.31 www 7428: my %hash;
1.37 www 7429: my %bighash;
7430: my $syval='';
1.620 albertel 7431: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 7432: my $targetfn = $thisfn;
1.609 banghart 7433: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 7434: $targetfn = 'adm/wrapper/'.$thisfn;
7435: }
1.687 albertel 7436: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
7437: $targetfn=$1;
7438: }
1.620 albertel 7439: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 7440: &GDBM_READER(),0640)) {
1.481 raeburn 7441: $syval=$hash{$targetfn};
1.37 www 7442: untie(%hash);
7443: }
7444: # ---------------------------------------------------------- There was an entry
7445: if ($syval) {
1.601 albertel 7446: #unless ($syval=~/\_\d+$/) {
1.620 albertel 7447: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 7448: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 7449: #return $env{$cache_str}='';
1.601 albertel 7450: #}
7451: #$syval.=$1;
7452: #}
1.37 www 7453: } else {
7454: # ------------------------------------------------------- Was not in symb table
1.620 albertel 7455: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 7456: &GDBM_READER(),0640)) {
1.37 www 7457: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 7458: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 7459: unless ($ids) {
7460: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 7461: }
7462: unless ($ids) {
7463: # alias?
7464: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 7465: }
1.37 www 7466: if ($ids) {
7467: # ------------------------------------------------------------------- Has ID(s)
7468: my @possibilities=split(/\,/,$ids);
1.39 www 7469: if ($#possibilities==0) {
7470: # ----------------------------------------------- There is only one possibility
1.37 www 7471: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 7472: $syval=&encode_symb($bighash{'map_id_'.$mapid},
7473: $resid,$thisfn);
1.249 www 7474: } elsif (!$donotrecurse) {
1.39 www 7475: # ------------------------------------------ There is more than one possibility
7476: my $realpossible=0;
1.800 albertel 7477: foreach my $id (@possibilities) {
7478: my $file=$bighash{'src_'.$id};
1.39 www 7479: if (&allowed('bre',$file)) {
1.800 albertel 7480: my ($mapid,$resid)=split(/\./,$id);
1.39 www 7481: if ($bighash{'map_type_'.$mapid} ne 'page') {
7482: $realpossible++;
1.626 albertel 7483: $syval=&encode_symb($bighash{'map_id_'.$mapid},
7484: $resid,$thisfn);
1.39 www 7485: }
7486: }
1.191 harris41 7487: }
1.39 www 7488: if ($realpossible!=1) { $syval=''; }
1.249 www 7489: } else {
7490: $syval='';
1.37 www 7491: }
7492: }
7493: untie(%bighash)
1.481 raeburn 7494: }
1.31 www 7495: }
1.62 www 7496: if ($syval) {
1.620 albertel 7497: return $env{$cache_str}=$syval;
1.62 www 7498: }
1.31 www 7499: }
1.949 raeburn 7500: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 7501: return $env{$cache_str}='';
1.31 www 7502: }
7503:
7504: # ---------------------------------------------------------- Return random seed
7505:
1.32 www 7506: sub numval {
7507: my $txt=shift;
7508: $txt=~tr/A-J/0-9/;
7509: $txt=~tr/a-j/0-9/;
7510: $txt=~tr/K-T/0-9/;
7511: $txt=~tr/k-t/0-9/;
7512: $txt=~tr/U-Z/0-5/;
7513: $txt=~tr/u-z/0-5/;
7514: $txt=~s/\D//g;
1.564 albertel 7515: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 7516: return int($txt);
1.368 albertel 7517: }
7518:
1.484 albertel 7519: sub numval2 {
7520: my $txt=shift;
7521: $txt=~tr/A-J/0-9/;
7522: $txt=~tr/a-j/0-9/;
7523: $txt=~tr/K-T/0-9/;
7524: $txt=~tr/k-t/0-9/;
7525: $txt=~tr/U-Z/0-5/;
7526: $txt=~tr/u-z/0-5/;
7527: $txt=~s/\D//g;
7528: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
7529: my $total;
7530: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 7531: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 7532: return int($total);
7533: }
7534:
1.575 albertel 7535: sub numval3 {
7536: use integer;
7537: my $txt=shift;
7538: $txt=~tr/A-J/0-9/;
7539: $txt=~tr/a-j/0-9/;
7540: $txt=~tr/K-T/0-9/;
7541: $txt=~tr/k-t/0-9/;
7542: $txt=~tr/U-Z/0-5/;
7543: $txt=~tr/u-z/0-5/;
7544: $txt=~s/\D//g;
7545: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
7546: my $total;
7547: foreach my $val (@txts) { $total+=$val; }
7548: if ($_64bit) { $total=(($total<<32)>>32); }
7549: return $total;
7550: }
7551:
1.675 albertel 7552: sub digest {
7553: my ($data)=@_;
7554: my $digest=&Digest::MD5::md5($data);
7555: my ($a,$b,$c,$d)=unpack("iiii",$digest);
7556: my ($e,$f);
7557: {
7558: use integer;
7559: $e=($a+$b);
7560: $f=($c+$d);
7561: if ($_64bit) {
7562: $e=(($e<<32)>>32);
7563: $f=(($f<<32)>>32);
7564: }
7565: }
7566: if (wantarray) {
7567: return ($e,$f);
7568: } else {
7569: my $g;
7570: {
7571: use integer;
7572: $g=($e+$f);
7573: if ($_64bit) {
7574: $g=(($g<<32)>>32);
7575: }
7576: }
7577: return $g;
7578: }
7579: }
7580:
1.368 albertel 7581: sub latest_rnd_algorithm_id {
1.675 albertel 7582: return '64bit5';
1.366 albertel 7583: }
1.32 www 7584:
1.503 albertel 7585: sub get_rand_alg {
7586: my ($courseid)=@_;
1.790 albertel 7587: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 7588: if ($courseid) {
1.620 albertel 7589: return $env{"course.$courseid.rndseed"};
1.503 albertel 7590: }
7591: return &latest_rnd_algorithm_id();
7592: }
7593:
1.562 albertel 7594: sub validCODE {
7595: my ($CODE)=@_;
7596: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
7597: return 0;
7598: }
7599:
1.491 albertel 7600: sub getCODE {
1.620 albertel 7601: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 7602: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
7603: defined($Apache::lonhomework::parsing_a_task) ) &&
7604: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 7605: return $Apache::lonhomework::history{'resource.CODE'};
7606: }
7607: return undef;
7608: }
7609:
1.31 www 7610: sub rndseed {
1.155 albertel 7611: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 7612: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 7613: if (!defined($symb)) {
1.366 albertel 7614: unless ($symb=$wsymb) { return time; }
7615: }
7616: if (!$courseid) { $courseid=$wcourseid; }
7617: if (!$domain) { $domain=$wdomain; }
7618: if (!$username) { $username=$wusername }
1.503 albertel 7619: my $which=&get_rand_alg();
1.803 albertel 7620:
1.491 albertel 7621: if (defined(&getCODE())) {
1.675 albertel 7622: if ($which eq '64bit5') {
7623: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
7624: } elsif ($which eq '64bit4') {
1.575 albertel 7625: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
7626: } else {
7627: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
7628: }
1.675 albertel 7629: } elsif ($which eq '64bit5') {
7630: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 7631: } elsif ($which eq '64bit4') {
7632: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 7633: } elsif ($which eq '64bit3') {
7634: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 7635: } elsif ($which eq '64bit2') {
7636: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 7637: } elsif ($which eq '64bit') {
7638: return &rndseed_64bit($symb,$courseid,$domain,$username);
7639: }
7640: return &rndseed_32bit($symb,$courseid,$domain,$username);
7641: }
7642:
7643: sub rndseed_32bit {
7644: my ($symb,$courseid,$domain,$username)=@_;
7645: {
7646: use integer;
7647: my $symbchck=unpack("%32C*",$symb) << 27;
7648: my $symbseed=numval($symb) << 22;
7649: my $namechck=unpack("%32C*",$username) << 17;
7650: my $nameseed=numval($username) << 12;
7651: my $domainseed=unpack("%32C*",$domain) << 7;
7652: my $courseseed=unpack("%32C*",$courseid);
7653: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 7654: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7655: #&logthis("rndseed :$num:$symb");
1.564 albertel 7656: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 7657: return $num;
7658: }
7659: }
7660:
7661: sub rndseed_64bit {
7662: my ($symb,$courseid,$domain,$username)=@_;
7663: {
7664: use integer;
7665: my $symbchck=unpack("%32S*",$symb) << 21;
7666: my $symbseed=numval($symb) << 10;
7667: my $namechck=unpack("%32S*",$username);
7668:
7669: my $nameseed=numval($username) << 21;
7670: my $domainseed=unpack("%32S*",$domain) << 10;
7671: my $courseseed=unpack("%32S*",$courseid);
7672:
7673: my $num1=$symbchck+$symbseed+$namechck;
7674: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7675: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7676: #&logthis("rndseed :$num:$symb");
1.564 albertel 7677: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 7678: return "$num1,$num2";
1.155 albertel 7679: }
1.366 albertel 7680: }
7681:
1.443 albertel 7682: sub rndseed_64bit2 {
7683: my ($symb,$courseid,$domain,$username)=@_;
7684: {
7685: use integer;
7686: # strings need to be an even # of cahracters long, it it is odd the
7687: # last characters gets thrown away
7688: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7689: my $symbseed=numval($symb) << 10;
7690: my $namechck=unpack("%32S*",$username.' ');
7691:
7692: my $nameseed=numval($username) << 21;
1.501 albertel 7693: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7694: my $courseseed=unpack("%32S*",$courseid.' ');
7695:
7696: my $num1=$symbchck+$symbseed+$namechck;
7697: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7698: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7699: #&logthis("rndseed :$num:$symb");
1.803 albertel 7700: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 7701: return "$num1,$num2";
7702: }
7703: }
7704:
7705: sub rndseed_64bit3 {
7706: my ($symb,$courseid,$domain,$username)=@_;
7707: {
7708: use integer;
7709: # strings need to be an even # of cahracters long, it it is odd the
7710: # last characters gets thrown away
7711: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7712: my $symbseed=numval2($symb) << 10;
7713: my $namechck=unpack("%32S*",$username.' ');
7714:
7715: my $nameseed=numval2($username) << 21;
1.443 albertel 7716: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7717: my $courseseed=unpack("%32S*",$courseid.' ');
7718:
7719: my $num1=$symbchck+$symbseed+$namechck;
7720: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7721: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7722: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 7723: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7724:
1.503 albertel 7725: return "$num1:$num2";
1.443 albertel 7726: }
7727: }
7728:
1.575 albertel 7729: sub rndseed_64bit4 {
7730: my ($symb,$courseid,$domain,$username)=@_;
7731: {
7732: use integer;
7733: # strings need to be an even # of cahracters long, it it is odd the
7734: # last characters gets thrown away
7735: my $symbchck=unpack("%32S*",$symb.' ') << 21;
7736: my $symbseed=numval3($symb) << 10;
7737: my $namechck=unpack("%32S*",$username.' ');
7738:
7739: my $nameseed=numval3($username) << 21;
7740: my $domainseed=unpack("%32S*",$domain.' ') << 10;
7741: my $courseseed=unpack("%32S*",$courseid.' ');
7742:
7743: my $num1=$symbchck+$symbseed+$namechck;
7744: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 7745: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
7746: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 7747: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
7748:
7749: return "$num1:$num2";
7750: }
7751: }
7752:
1.675 albertel 7753: sub rndseed_64bit5 {
7754: my ($symb,$courseid,$domain,$username)=@_;
7755: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
7756: return "$num1:$num2";
7757: }
7758:
1.366 albertel 7759: sub rndseed_CODE_64bit {
7760: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 7761: {
1.366 albertel 7762: use integer;
1.443 albertel 7763: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 7764: my $symbseed=numval2($symb);
1.491 albertel 7765: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7766: my $CODEseed=numval(&getCODE());
1.443 albertel 7767: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 7768: my $num1=$symbseed+$CODEchck;
7769: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 7770: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7771: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 7772: if ($_64bit) { $num1=(($num1<<32)>>32); }
7773: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 7774: return "$num1:$num2";
1.366 albertel 7775: }
7776: }
7777:
1.575 albertel 7778: sub rndseed_CODE_64bit4 {
7779: my ($symb,$courseid,$domain,$username)=@_;
7780: {
7781: use integer;
7782: my $symbchck=unpack("%32S*",$symb.' ') << 16;
7783: my $symbseed=numval3($symb);
7784: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
7785: my $CODEseed=numval3(&getCODE());
7786: my $courseseed=unpack("%32S*",$courseid.' ');
7787: my $num1=$symbseed+$CODEchck;
7788: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 7789: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
7790: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 7791: if ($_64bit) { $num1=(($num1<<32)>>32); }
7792: if ($_64bit) { $num2=(($num2<<32)>>32); }
7793: return "$num1:$num2";
7794: }
7795: }
7796:
1.675 albertel 7797: sub rndseed_CODE_64bit5 {
7798: my ($symb,$courseid,$domain,$username)=@_;
7799: my $code = &getCODE();
7800: my ($num1,$num2)=&digest("$symb,$courseid,$code");
7801: return "$num1:$num2";
7802: }
7803:
1.366 albertel 7804: sub setup_random_from_rndseed {
7805: my ($rndseed)=@_;
1.503 albertel 7806: if ($rndseed =~/([,:])/) {
7807: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 7808: &Math::Random::random_set_seed(abs($num1),abs($num2));
7809: } else {
7810: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 7811: }
1.36 albertel 7812: }
7813:
1.474 albertel 7814: sub latest_receipt_algorithm_id {
1.835 albertel 7815: return 'receipt3';
1.474 albertel 7816: }
7817:
1.480 www 7818: sub recunique {
7819: my $fucourseid=shift;
7820: my $unique;
1.835 albertel 7821: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
7822: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 7823: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 7824: } else {
7825: $unique=$perlvar{'lonReceipt'};
7826: }
7827: return unpack("%32C*",$unique);
7828: }
7829:
7830: sub recprefix {
7831: my $fucourseid=shift;
7832: my $prefix;
1.835 albertel 7833: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
7834: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 7835: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 7836: } else {
7837: $prefix=$perlvar{'lonHostID'};
7838: }
7839: return unpack("%32C*",$prefix);
7840: }
7841:
1.76 www 7842: sub ireceipt {
1.474 albertel 7843: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 7844:
7845: my $return =&recprefix($fucourseid).'-';
7846:
7847: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
7848: $env{'request.state'} eq 'construct') {
7849: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
7850: return $return;
7851: }
7852:
1.76 www 7853: my $cuname=unpack("%32C*",$funame);
7854: my $cudom=unpack("%32C*",$fudom);
7855: my $cucourseid=unpack("%32C*",$fucourseid);
7856: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 7857: my $cunique=&recunique($fucourseid);
1.474 albertel 7858: my $cpart=unpack("%32S*",$part);
1.835 albertel 7859: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
7860:
1.790 albertel 7861: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 7862:
7863: $return.= ($cunique%$cuname+
7864: $cunique%$cudom+
7865: $cusymb%$cuname+
7866: $cusymb%$cudom+
7867: $cucourseid%$cuname+
7868: $cucourseid%$cudom+
7869: $cpart%$cuname+
7870: $cpart%$cudom);
7871: } else {
7872: $return.= ($cunique%$cuname+
7873: $cunique%$cudom+
7874: $cusymb%$cuname+
7875: $cusymb%$cudom+
7876: $cucourseid%$cuname+
7877: $cucourseid%$cudom);
7878: }
7879: return $return;
1.76 www 7880: }
7881:
7882: sub receipt {
1.474 albertel 7883: my ($part)=@_;
1.790 albertel 7884: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 7885: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 7886: }
1.260 ng 7887:
1.790 albertel 7888: sub whichuser {
7889: my ($passedsymb)=@_;
7890: my ($symb,$courseid,$domain,$name,$publicuser);
7891: if (defined($env{'form.grade_symb'})) {
7892: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
7893: my $allowed=&allowed('vgr',$tmp_courseid);
7894: if (!$allowed &&
7895: exists($env{'request.course.sec'}) &&
7896: $env{'request.course.sec'} !~ /^\s*$/) {
7897: $allowed=&allowed('vgr',$tmp_courseid.
7898: '/'.$env{'request.course.sec'});
7899: }
7900: if ($allowed) {
7901: ($symb)=&get_env_multiple('form.grade_symb');
7902: $courseid=$tmp_courseid;
7903: ($domain)=&get_env_multiple('form.grade_domain');
7904: ($name)=&get_env_multiple('form.grade_username');
7905: return ($symb,$courseid,$domain,$name,$publicuser);
7906: }
7907: }
7908: if (!$passedsymb) {
7909: $symb=&symbread();
7910: } else {
7911: $symb=$passedsymb;
7912: }
7913: $courseid=$env{'request.course.id'};
7914: $domain=$env{'user.domain'};
7915: $name=$env{'user.name'};
7916: if ($name eq 'public' && $domain eq 'public') {
7917: if (!defined($env{'form.username'})) {
7918: $env{'form.username'}.=time.rand(10000000);
7919: }
7920: $name.=$env{'form.username'};
7921: }
7922: return ($symb,$courseid,$domain,$name,$publicuser);
7923:
7924: }
7925:
1.36 albertel 7926: # ------------------------------------------------------------ Serves up a file
1.472 albertel 7927: # returns either the contents of the file or
7928: # -1 if the file doesn't exist
1.481 raeburn 7929: #
7930: # if the target is a file that was uploaded via DOCS,
7931: # a check will be made to see if a current copy exists on the local server,
7932: # if it does this will be served, otherwise a copy will be retrieved from
7933: # the home server for the course and stored in /home/httpd/html/userfiles on
7934: # the local server.
1.472 albertel 7935:
1.36 albertel 7936: sub getfile {
1.538 albertel 7937: my ($file) = @_;
1.609 banghart 7938: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 7939: &repcopy($file);
7940: return &readfile($file);
7941: }
7942:
7943: sub repcopy_userfile {
7944: my ($file)=@_;
1.609 banghart 7945: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 7946: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 7947: my ($cdom,$cnum,$filename) =
1.811 albertel 7948: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 7949: my $uri="/uploaded/$cdom/$cnum/$filename";
7950: if (-e "$file") {
1.828 www 7951: # we already have a local copy, check it out
1.538 albertel 7952: my @fileinfo = stat($file);
1.828 www 7953: my $rtncode;
7954: my $info;
1.538 albertel 7955: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 7956: if ($lwpresp ne 'ok') {
1.828 www 7957: # there is no such file anymore, even though we had a local copy
1.482 albertel 7958: if ($rtncode eq '404') {
1.538 albertel 7959: unlink($file);
1.482 albertel 7960: }
7961: return -1;
7962: }
7963: if ($info < $fileinfo[9]) {
1.828 www 7964: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 7965: return 'ok';
1.828 www 7966: } else {
7967: # the file is outdated, get rid of it
7968: unlink($file);
1.482 albertel 7969: }
1.828 www 7970: }
7971: # one way or the other, at this point, we don't have the file
7972: # construct the correct path for the file
7973: my @parts = ($cdom,$cnum);
7974: if ($filename =~ m|^(.+)/[^/]+$|) {
7975: push @parts, split(/\//,$1);
7976: }
7977: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
7978: foreach my $part (@parts) {
7979: $path .= '/'.$part;
7980: if (!-e $path) {
7981: mkdir($path,0770);
1.482 albertel 7982: }
7983: }
1.828 www 7984: # now the path exists for sure
7985: # get a user agent
7986: my $ua=new LWP::UserAgent;
7987: my $transferfile=$file.'.in.transfer';
7988: # FIXME: this should flock
7989: if (-e $transferfile) { return 'ok'; }
7990: my $request;
7991: $uri=~s/^\///;
1.838 albertel 7992: $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828 www 7993: my $response=$ua->request($request,$transferfile);
7994: # did it work?
7995: if ($response->is_error()) {
7996: unlink($transferfile);
7997: &logthis("Userfile repcopy failed for $uri");
7998: return -1;
7999: }
8000: # worked, rename the transfer file
8001: rename($transferfile,$file);
1.607 raeburn 8002: return 'ok';
1.481 raeburn 8003: }
8004:
1.517 albertel 8005: sub tokenwrapper {
8006: my $uri=shift;
1.552 albertel 8007: $uri=~s|^http\://([^/]+)||;
8008: $uri=~s|^/||;
1.620 albertel 8009: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 8010: my $token=$1;
1.552 albertel 8011: my (undef,$udom,$uname,$file)=split('/',$uri,4);
8012: if ($udom && $uname && $file) {
8013: $file=~s|(\?\.*)*$||;
1.949 raeburn 8014: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.838 albertel 8015: return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517 albertel 8016: (($uri=~/\?/)?'&':'?').'token='.$token.
8017: '&tokenissued='.$perlvar{'lonHostID'};
8018: } else {
8019: return '/adm/notfound.html';
8020: }
8021: }
8022:
1.828 www 8023: # call with reqtype HEAD: get last modification time
8024: # call with reqtype GET: get the file contents
8025: # Do not call this with reqtype GET for large files! It loads everything into memory
8026: #
1.481 raeburn 8027: sub getuploaded {
8028: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
8029: $uri=~s/^\///;
1.838 albertel 8030: $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481 raeburn 8031: my $ua=new LWP::UserAgent;
8032: my $request=new HTTP::Request($reqtype,$uri);
8033: my $response=$ua->request($request);
8034: $$rtncode = $response->code;
1.482 albertel 8035: if (! $response->is_success()) {
8036: return 'failed';
8037: }
8038: if ($reqtype eq 'HEAD') {
1.486 www 8039: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 8040: } elsif ($reqtype eq 'GET') {
8041: $$info = $response->content;
1.472 albertel 8042: }
1.482 albertel 8043: return 'ok';
1.36 albertel 8044: }
8045:
1.481 raeburn 8046: sub readfile {
8047: my $file = shift;
8048: if ( (! -e $file ) || ($file eq '') ) { return -1; };
8049: my $fh;
8050: open($fh,"<$file");
8051: my $a='';
1.800 albertel 8052: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 8053: return $a;
8054: }
8055:
1.36 albertel 8056: sub filelocation {
1.590 banghart 8057: my ($dir,$file) = @_;
8058: my $location;
8059: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 8060:
8061: if ($file =~ m-^/adm/-) {
8062: $file=~s-^/adm/wrapper/-/-;
8063: $file=~s-^/adm/coursedocs/showdoc/-/-;
8064: }
1.882 albertel 8065:
1.590 banghart 8066: if ($file=~m:^/~:) { # is a contruction space reference
8067: $location = $file;
8068: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 8069: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 8070: # is a correct contruction space reference
8071: $location = $file;
1.609 banghart 8072: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 8073: my ($udom,$uname,$filename)=
1.811 albertel 8074: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 8075: my $home=&homeserver($uname,$udom);
8076: my $is_me=0;
8077: my @ids=¤t_machine_ids();
8078: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
8079: if ($is_me) {
1.740 www 8080: $location=&propath($udom,$uname).
1.590 banghart 8081: '/userfiles/'.$filename;
8082: } else {
8083: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
8084: $udom.'/'.$uname.'/'.$filename;
8085: }
1.882 albertel 8086: } elsif ($file =~ m-^/adm/-) {
8087: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 8088: } else {
8089: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
8090: $file=~s:^/res/:/:;
8091: if ( !( $file =~ m:^/:) ) {
8092: $location = $dir. '/'.$file;
8093: } else {
8094: $location = '/home/httpd/html/res'.$file;
8095: }
1.59 albertel 8096: }
1.590 banghart 8097: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 8098: while ($location=~m{/\.\./}) {
8099: if ($location =~ m{/[^/]+/\.\./}) {
8100: $location=~ s{/[^/]+/\.\./}{/}g;
8101: } else {
8102: $location=~ s{/\.\./}{/}g;
8103: }
8104: } #remove dir/..
1.590 banghart 8105: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
8106: return $location;
1.46 www 8107: }
1.36 albertel 8108:
1.46 www 8109: sub hreflocation {
8110: my ($dir,$file)=@_;
1.460 albertel 8111: unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666 albertel 8112: $file=filelocation($dir,$file);
1.700 albertel 8113: } elsif ($file=~m-^/adm/-) {
8114: $file=~s-^/adm/wrapper/-/-;
8115: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 8116: }
8117: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
8118: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 8119: } elsif ($file=~m-/home/($match_username)/public_html/-) {
8120: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 8121: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 8122: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 8123: -/uploaded/$1/$2/-x;
1.46 www 8124: }
1.913 albertel 8125: if ($file=~ m{^/userfiles/}) {
8126: $file =~ s{^/userfiles/}{/uploaded/};
8127: }
1.462 albertel 8128: return $file;
1.465 albertel 8129: }
8130:
8131: sub current_machine_domains {
1.853 albertel 8132: return &machine_domains(&hostname($perlvar{'lonHostID'}));
8133: }
8134:
8135: sub machine_domains {
8136: my ($hostname) = @_;
1.465 albertel 8137: my @domains;
1.838 albertel 8138: my %hostname = &all_hostnames();
1.465 albertel 8139: while( my($id, $name) = each(%hostname)) {
1.467 matthew 8140: # &logthis("-$id-$name-$hostname-");
1.465 albertel 8141: if ($hostname eq $name) {
1.844 albertel 8142: push(@domains,&host_domain($id));
1.465 albertel 8143: }
8144: }
8145: return @domains;
8146: }
8147:
8148: sub current_machine_ids {
1.853 albertel 8149: return &machine_ids(&hostname($perlvar{'lonHostID'}));
8150: }
8151:
8152: sub machine_ids {
8153: my ($hostname) = @_;
8154: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 8155: my @ids;
1.888 albertel 8156: my %name_to_host = &all_names();
1.889 albertel 8157: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
8158: return @{ $name_to_host{$hostname} };
8159: }
8160: return;
1.31 www 8161: }
8162:
1.824 raeburn 8163: sub additional_machine_domains {
8164: my @domains;
8165: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
8166: while( my $line = <$fh>) {
8167: $line =~ s/\s//g;
8168: push(@domains,$line);
8169: }
8170: return @domains;
8171: }
8172:
8173: sub default_login_domain {
8174: my $domain = $perlvar{'lonDefDomain'};
8175: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
8176: foreach my $posdom (¤t_machine_domains(),
8177: &additional_machine_domains()) {
8178: if (lc($posdom) eq lc($testdomain)) {
8179: $domain=$posdom;
8180: last;
8181: }
8182: }
8183: return $domain;
8184: }
8185:
1.31 www 8186: # ------------------------------------------------------------- Declutters URLs
8187:
8188: sub declutter {
8189: my $thisfn=shift;
1.569 albertel 8190: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 8191: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 8192: $thisfn=~s/^\///;
1.697 albertel 8193: $thisfn=~s|^adm/wrapper/||;
8194: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 8195: $thisfn=~s/^res\///;
1.235 www 8196: $thisfn=~s/\?.+$//;
1.268 www 8197: return $thisfn;
8198: }
8199:
8200: # ------------------------------------------------------------- Clutter up URLs
8201:
8202: sub clutter {
8203: my $thisfn='/'.&declutter(shift);
1.887 albertel 8204: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 8205: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 8206: $thisfn='/res'.$thisfn;
8207: }
1.694 albertel 8208: if ($thisfn !~m|/adm|) {
1.695 albertel 8209: if ($thisfn =~ m|/ext/|) {
1.694 albertel 8210: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 8211: } else {
8212: my ($ext) = ($thisfn =~ /\.(\w+)$/);
8213: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 8214: if ($embstyle eq 'ssi'
8215: || ($embstyle eq 'hdn')
8216: || ($embstyle eq 'rat')
8217: || ($embstyle eq 'prv')
8218: || ($embstyle eq 'ign')) {
8219: #do nothing with these
8220: } elsif (($embstyle eq 'img')
1.695 albertel 8221: || ($embstyle eq 'emb')
8222: || ($embstyle eq 'wrp')) {
8223: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 8224: } elsif ($embstyle eq 'unk'
8225: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 8226: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 8227: } else {
1.718 www 8228: # &logthis("Got a blank emb style");
1.695 albertel 8229: }
1.694 albertel 8230: }
8231: }
1.31 www 8232: return $thisfn;
1.12 www 8233: }
8234:
1.787 albertel 8235: sub clutter_with_no_wrapper {
8236: my $uri = &clutter(shift);
8237: if ($uri =~ m-^/adm/-) {
8238: $uri =~ s-^/adm/wrapper/-/-;
8239: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
8240: }
8241: return $uri;
8242: }
8243:
1.557 albertel 8244: sub freeze_escape {
8245: my ($value)=@_;
8246: if (ref($value)) {
8247: $value=&nfreeze($value);
8248: return '__FROZEN__'.&escape($value);
8249: }
8250: return &escape($value);
8251: }
8252:
1.11 www 8253:
1.557 albertel 8254: sub thaw_unescape {
8255: my ($value)=@_;
8256: if ($value =~ /^__FROZEN__/) {
8257: substr($value,0,10,undef);
8258: $value=&unescape($value);
8259: return &thaw($value);
8260: }
8261: return &unescape($value);
8262: }
8263:
1.436 albertel 8264: sub correct_line_ends {
8265: my ($result)=@_;
8266: $$result =~s/\r\n/\n/mg;
8267: $$result =~s/\r/\n/mg;
1.415 albertel 8268: }
1.1 albertel 8269: # ================================================================ Main Program
8270:
1.184 www 8271: sub goodbye {
1.204 albertel 8272: &logthis("Starting Shut down");
1.443 albertel 8273: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 8274: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 8275: #converted
1.599 albertel 8276: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 8277: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
8278: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
8279: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 8280: #1.1 only
1.870 albertel 8281: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
8282: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
8283: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
8284: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
8285: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 8286: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
8287: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 8288: &flushcourselogs();
8289: &logthis("Shutting down");
8290: }
8291:
1.852 albertel 8292: sub get_dns {
1.869 albertel 8293: my ($url,$func,$ignore_cache) = @_;
8294: if (!$ignore_cache) {
8295: my ($content,$cached)=
8296: &Apache::lonnet::is_cached_new('dns',$url);
8297: if ($cached) {
8298: &$func($content);
8299: return;
8300: }
8301: }
8302:
8303: my %alldns;
1.852 albertel 8304: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
8305: foreach my $dns (<$config>) {
8306: next if ($dns !~ /^\^(\S*)/x);
1.869 albertel 8307: $alldns{$1} = 1;
8308: }
8309: while (%alldns) {
8310: my ($dns) = keys(%alldns);
8311: delete($alldns{$dns});
1.852 albertel 8312: my $ua=new LWP::UserAgent;
8313: my $request=new HTTP::Request('GET',"http://$dns$url");
8314: my $response=$ua->request($request);
8315: next if ($response->is_error());
8316: my @content = split("\n",$response->content);
1.869 albertel 8317: &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852 albertel 8318: &$func(\@content);
1.869 albertel 8319: return;
1.852 albertel 8320: }
8321: close($config);
1.871 albertel 8322: my $which = (split('/',$url))[3];
8323: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
8324: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 8325: my @content = <$config>;
8326: &$func(\@content);
8327: return;
1.852 albertel 8328: }
1.327 albertel 8329: # ------------------------------------------------------------ Read domain file
8330: {
1.852 albertel 8331: my $loaded;
1.846 albertel 8332: my %domain;
8333:
1.852 albertel 8334: sub parse_domain_tab {
8335: my ($lines) = @_;
8336: foreach my $line (@$lines) {
8337: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 8338:
1.846 albertel 8339: chomp($line);
1.852 albertel 8340: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 8341: my %this_domain;
8342: foreach my $field ('description', 'auth_def', 'auth_arg_def',
8343: 'lang_def', 'city', 'longi', 'lati',
8344: 'primary') {
8345: $this_domain{$field} = shift(@elements);
8346: }
8347: $domain{$name} = \%this_domain;
1.852 albertel 8348: }
8349: }
1.864 albertel 8350:
8351: sub reset_domain_info {
8352: undef($loaded);
8353: undef(%domain);
8354: }
8355:
1.852 albertel 8356: sub load_domain_tab {
1.869 albertel 8357: my ($ignore_cache) = @_;
8358: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852 albertel 8359: my $fh;
8360: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
8361: my @lines = <$fh>;
8362: &parse_domain_tab(\@lines);
1.448 albertel 8363: }
1.852 albertel 8364: close($fh);
8365: $loaded = 1;
1.327 albertel 8366: }
1.846 albertel 8367:
8368: sub domain {
1.852 albertel 8369: &load_domain_tab() if (!$loaded);
8370:
1.846 albertel 8371: my ($name,$what) = @_;
8372: return if ( !exists($domain{$name}) );
8373:
8374: if (!$what) {
8375: return $domain{$name}{'description'};
8376: }
8377: return $domain{$name}{$what};
8378: }
1.327 albertel 8379: }
8380:
8381:
1.1 albertel 8382: # ------------------------------------------------------------- Read hosts file
8383: {
1.838 albertel 8384: my %hostname;
1.844 albertel 8385: my %hostdom;
1.845 albertel 8386: my %libserv;
1.852 albertel 8387: my $loaded;
1.888 albertel 8388: my %name_to_host;
1.852 albertel 8389:
8390: sub parse_hosts_tab {
8391: my ($file) = @_;
8392: foreach my $configline (@$file) {
8393: next if ($configline =~ /^(\#|\s*$ )/x);
8394: next if ($configline =~ /^\^/);
8395: chomp($configline);
8396: my ($id,$domain,$role,$name)=split(/:/,$configline);
8397: $name=~s/\s//g;
8398: if ($id && $domain && $role && $name) {
8399: $hostname{$id}=$name;
1.888 albertel 8400: push(@{$name_to_host{$name}}, $id);
1.852 albertel 8401: $hostdom{$id}=$domain;
8402: if ($role eq 'library') { $libserv{$id}=$name; }
8403: }
8404: }
8405: }
1.864 albertel 8406:
8407: sub reset_hosts_info {
1.897 albertel 8408: &purge_remembered();
1.864 albertel 8409: &reset_domain_info();
8410: &reset_hosts_ip_info();
1.892 albertel 8411: undef(%name_to_host);
1.864 albertel 8412: undef(%hostname);
8413: undef(%hostdom);
8414: undef(%libserv);
8415: undef($loaded);
8416: }
1.1 albertel 8417:
1.852 albertel 8418: sub load_hosts_tab {
1.869 albertel 8419: my ($ignore_cache) = @_;
8420: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852 albertel 8421: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
8422: my @config = <$config>;
8423: &parse_hosts_tab(\@config);
8424: close($config);
8425: $loaded=1;
1.1 albertel 8426: }
1.852 albertel 8427:
1.838 albertel 8428: sub hostname {
1.852 albertel 8429: &load_hosts_tab() if (!$loaded);
8430:
1.838 albertel 8431: my ($lonid) = @_;
8432: return $hostname{$lonid};
8433: }
1.845 albertel 8434:
1.838 albertel 8435: sub all_hostnames {
1.852 albertel 8436: &load_hosts_tab() if (!$loaded);
8437:
1.838 albertel 8438: return %hostname;
8439: }
1.845 albertel 8440:
1.888 albertel 8441: sub all_names {
8442: &load_hosts_tab() if (!$loaded);
8443:
8444: return %name_to_host;
8445: }
8446:
1.845 albertel 8447: sub is_library {
1.852 albertel 8448: &load_hosts_tab() if (!$loaded);
8449:
1.845 albertel 8450: return exists($libserv{$_[0]});
8451: }
8452:
8453: sub all_library {
1.852 albertel 8454: &load_hosts_tab() if (!$loaded);
8455:
1.845 albertel 8456: return %libserv;
8457: }
8458:
1.841 albertel 8459: sub get_servers {
1.852 albertel 8460: &load_hosts_tab() if (!$loaded);
8461:
1.841 albertel 8462: my ($domain,$type) = @_;
8463: my %possible_hosts = ($type eq 'library') ? %libserv
8464: : %hostname;
8465: my %result;
1.842 albertel 8466: if (ref($domain) eq 'ARRAY') {
8467: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 8468: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 8469: $result{$host} = $hostname;
8470: }
8471: }
8472: } else {
8473: while ( my ($host,$hostname) = each(%possible_hosts)) {
8474: if ($hostdom{$host} eq $domain) {
8475: $result{$host} = $hostname;
8476: }
1.841 albertel 8477: }
8478: }
8479: return %result;
8480: }
1.845 albertel 8481:
1.844 albertel 8482: sub host_domain {
1.852 albertel 8483: &load_hosts_tab() if (!$loaded);
8484:
1.844 albertel 8485: my ($lonid) = @_;
8486: return $hostdom{$lonid};
8487: }
8488:
1.841 albertel 8489: sub all_domains {
1.852 albertel 8490: &load_hosts_tab() if (!$loaded);
8491:
1.841 albertel 8492: my %seen;
8493: my @uniq = grep(!$seen{$_}++, values(%hostdom));
8494: return @uniq;
8495: }
1.1 albertel 8496: }
8497:
1.847 albertel 8498: {
8499: my %iphost;
1.856 albertel 8500: my %name_to_ip;
8501: my %lonid_to_ip;
1.869 albertel 8502:
1.847 albertel 8503: sub get_hosts_from_ip {
8504: my ($ip) = @_;
8505: my %iphosts = &get_iphost();
8506: if (ref($iphosts{$ip})) {
8507: return @{$iphosts{$ip}};
8508: }
8509: return;
1.839 albertel 8510: }
1.864 albertel 8511:
8512: sub reset_hosts_ip_info {
8513: undef(%iphost);
8514: undef(%name_to_ip);
8515: undef(%lonid_to_ip);
8516: }
1.856 albertel 8517:
8518: sub get_host_ip {
8519: my ($lonid) = @_;
8520: if (exists($lonid_to_ip{$lonid})) {
8521: return $lonid_to_ip{$lonid};
8522: }
8523: my $name=&hostname($lonid);
8524: my $ip = gethostbyname($name);
8525: return if (!$ip || length($ip) ne 4);
8526: $ip=inet_ntoa($ip);
8527: $name_to_ip{$name} = $ip;
8528: $lonid_to_ip{$lonid} = $ip;
8529: return $ip;
8530: }
1.847 albertel 8531:
8532: sub get_iphost {
1.869 albertel 8533: my ($ignore_cache) = @_;
1.894 albertel 8534:
1.869 albertel 8535: if (!$ignore_cache) {
8536: if (%iphost) {
8537: return %iphost;
8538: }
8539: my ($ip_info,$cached)=
8540: &Apache::lonnet::is_cached_new('iphost','iphost');
8541: if ($cached) {
8542: %iphost = %{$ip_info->[0]};
8543: %name_to_ip = %{$ip_info->[1]};
8544: %lonid_to_ip = %{$ip_info->[2]};
8545: return %iphost;
8546: }
8547: }
1.894 albertel 8548:
8549: # get yesterday's info for fallback
8550: my %old_name_to_ip;
8551: my ($ip_info,$cached)=
8552: &Apache::lonnet::is_cached_new('iphost','iphost');
8553: if ($cached) {
8554: %old_name_to_ip = %{$ip_info->[1]};
8555: }
8556:
1.888 albertel 8557: my %name_to_host = &all_names();
8558: foreach my $name (keys(%name_to_host)) {
1.847 albertel 8559: my $ip;
8560: if (!exists($name_to_ip{$name})) {
8561: $ip = gethostbyname($name);
8562: if (!$ip || length($ip) ne 4) {
1.894 albertel 8563: if (defined($old_name_to_ip{$name})) {
8564: $ip = $old_name_to_ip{$name};
8565: &logthis("Can't find $name defaulting to old $ip");
8566: } else {
8567: &logthis("Name $name no IP found");
8568: next;
8569: }
8570: } else {
8571: $ip=inet_ntoa($ip);
1.847 albertel 8572: }
8573: $name_to_ip{$name} = $ip;
8574: } else {
8575: $ip = $name_to_ip{$name};
1.653 albertel 8576: }
1.888 albertel 8577: foreach my $id (@{ $name_to_host{$name} }) {
8578: $lonid_to_ip{$id} = $ip;
8579: }
8580: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 8581: }
1.869 albertel 8582: &Apache::lonnet::do_cache_new('iphost','iphost',
8583: [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894 albertel 8584: 48*60*60);
1.869 albertel 8585:
1.847 albertel 8586: return %iphost;
1.598 albertel 8587: }
8588: }
8589:
1.862 albertel 8590: BEGIN {
8591:
8592: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
8593: unless ($readit) {
8594: {
8595: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
8596: %perlvar = (%perlvar,%{$configvars});
8597: }
8598:
8599:
1.1 albertel 8600: # ------------------------------------------------------ Read spare server file
8601: {
1.448 albertel 8602: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 8603:
8604: while (my $configline=<$config>) {
8605: chomp($configline);
1.284 matthew 8606: if ($configline) {
1.784 albertel 8607: my ($host,$type) = split(':',$configline,2);
1.785 albertel 8608: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 8609: push(@{ $spareid{$type} }, $host);
1.1 albertel 8610: }
8611: }
1.448 albertel 8612: close($config);
1.1 albertel 8613: }
1.11 www 8614: # ------------------------------------------------------------ Read permissions
8615: {
1.448 albertel 8616: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 8617:
8618: while (my $configline=<$config>) {
1.448 albertel 8619: chomp($configline);
8620: if ($configline) {
8621: my ($role,$perm)=split(/ /,$configline);
8622: if ($perm ne '') { $pr{$role}=$perm; }
8623: }
1.11 www 8624: }
1.448 albertel 8625: close($config);
1.11 www 8626: }
8627:
8628: # -------------------------------------------- Read plain texts for permissions
8629: {
1.448 albertel 8630: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 8631:
8632: while (my $configline=<$config>) {
1.448 albertel 8633: chomp($configline);
8634: if ($configline) {
1.742 raeburn 8635: my ($short,@plain)=split(/:/,$configline);
8636: %{$prp{$short}} = ();
8637: if (@plain > 0) {
8638: $prp{$short}{'std'} = $plain[0];
8639: for (my $i=1; $i<@plain; $i++) {
8640: $prp{$short}{'alt'.$i} = $plain[$i];
8641: }
8642: }
1.448 albertel 8643: }
1.135 www 8644: }
1.448 albertel 8645: close($config);
1.135 www 8646: }
8647:
8648: # ---------------------------------------------------------- Read package table
8649: {
1.448 albertel 8650: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 8651:
8652: while (my $configline=<$config>) {
1.483 albertel 8653: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 8654: chomp($configline);
8655: my ($short,$plain)=split(/:/,$configline);
8656: my ($pack,$name)=split(/\&/,$short);
8657: if ($plain ne '') {
8658: $packagetab{$pack.'&'.$name.'&name'}=$name;
8659: $packagetab{$short}=$plain;
8660: }
1.11 www 8661: }
1.448 albertel 8662: close($config);
1.329 matthew 8663: }
8664:
8665: # ------------- set up temporary directory
8666: {
8667: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
8668:
1.11 www 8669: }
8670:
1.794 albertel 8671: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
8672: 'compress_threshold'=> 20_000,
8673: });
1.185 www 8674:
1.281 www 8675: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 8676: $dumpcount=0;
1.22 www 8677:
1.163 harris41 8678: &logtouch();
1.672 albertel 8679: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 8680: $readit=1;
1.564 albertel 8681: {
8682: use integer;
8683: my $test=(2**32)+1;
1.568 albertel 8684: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 8685: &logthis(" Detected 64bit platform ($_64bit)");
8686: }
1.195 www 8687: }
1.1 albertel 8688: }
1.179 www 8689:
1.1 albertel 8690: 1;
1.191 harris41 8691: __END__
8692:
1.243 albertel 8693: =pod
8694:
1.191 harris41 8695: =head1 NAME
8696:
1.243 albertel 8697: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 8698:
8699: =head1 SYNOPSIS
8700:
1.243 albertel 8701: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 8702:
8703: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
8704:
1.243 albertel 8705: Common parameters:
8706:
8707: =over 4
8708:
8709: =item *
8710:
8711: $uname : an internal username (if $cname expecting a course Id specifically)
8712:
8713: =item *
8714:
8715: $udom : a domain (if $cdom expecting a course's domain specifically)
8716:
8717: =item *
8718:
8719: $symb : a resource instance identifier
8720:
8721: =item *
8722:
8723: $namespace : the name of a .db file that contains the data needed or
8724: being set.
8725:
8726: =back
8727:
1.394 bowersj2 8728: =head1 OVERVIEW
1.191 harris41 8729:
1.394 bowersj2 8730: lonnet provides subroutines which interact with the
8731: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
8732: about classes, users, and resources.
1.243 albertel 8733:
8734: For many of these objects you can also use this to store data about
8735: them or modify them in various ways.
1.191 harris41 8736:
1.394 bowersj2 8737: =head2 Symbs
1.191 harris41 8738:
1.394 bowersj2 8739: To identify a specific instance of a resource, LON-CAPA uses symbols
8740: or "symbs"X<symb>. These identifiers are built from the URL of the
8741: map, the resource number of the resource in the map, and the URL of
8742: the resource itself. The latter is somewhat redundant, but might help
8743: if maps change.
8744:
8745: An example is
8746:
8747: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
8748:
8749: The respective map entry is
8750:
8751: <resource id="19" src="/res/msu/korte/tests/part12.problem"
8752: title="Problem 2">
8753: </resource>
8754:
8755: Symbs are used by the random number generator, as well as to store and
8756: restore data specific to a certain instance of for example a problem.
8757:
8758: =head2 Storing And Retrieving Data
8759:
8760: X<store()>X<cstore()>X<restore()>Three of the most important functions
8761: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
8762: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
8763: is is the non-critical message twin of cstore. These functions are for
8764: handlers to store a perl hash to a user's permanent data space in an
8765: easy manner, and to retrieve it again on another call. It is expected
8766: that a handler would use this once at the beginning to retrieve data,
8767: and then again once at the end to send only the new data back.
8768:
8769: The data is stored in the user's data directory on the user's
8770: homeserver under the ID of the course.
8771:
8772: The hash that is returned by restore will have all of the previous
8773: value for all of the elements of the hash.
8774:
8775: Example:
8776:
8777: #creating a hash
8778: my %hash;
8779: $hash{'foo'}='bar';
8780:
8781: #storing it
8782: &Apache::lonnet::cstore(\%hash);
8783:
8784: #changing a value
8785: $hash{'foo'}='notbar';
8786:
8787: #adding a new value
8788: $hash{'bar'}='foo';
8789: &Apache::lonnet::cstore(\%hash);
8790:
8791: #retrieving the hash
8792: my %history=&Apache::lonnet::restore();
8793:
8794: #print the hash
8795: foreach my $key (sort(keys(%history))) {
8796: print("\%history{$key} = $history{$key}");
8797: }
8798:
8799: Will print out:
1.191 harris41 8800:
1.394 bowersj2 8801: %history{1:foo} = bar
8802: %history{1:keys} = foo:timestamp
8803: %history{1:timestamp} = 990455579
8804: %history{2:bar} = foo
8805: %history{2:foo} = notbar
8806: %history{2:keys} = foo:bar:timestamp
8807: %history{2:timestamp} = 990455580
8808: %history{bar} = foo
8809: %history{foo} = notbar
8810: %history{timestamp} = 990455580
8811: %history{version} = 2
8812:
8813: Note that the special hash entries C<keys>, C<version> and
8814: C<timestamp> were added to the hash. C<version> will be equal to the
8815: total number of versions of the data that have been stored. The
8816: C<timestamp> attribute will be the UNIX time the hash was
8817: stored. C<keys> is available in every historical section to list which
8818: keys were added or changed at a specific historical revision of a
8819: hash.
8820:
8821: B<Warning>: do not store the hash that restore returns directly. This
8822: will cause a mess since it will restore the historical keys as if the
8823: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 8824:
1.394 bowersj2 8825: Calling convention:
1.191 harris41 8826:
1.394 bowersj2 8827: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
8828: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 8829:
1.394 bowersj2 8830: For more detailed information, see lonnet specific documentation.
1.191 harris41 8831:
1.394 bowersj2 8832: =head1 RETURN MESSAGES
1.191 harris41 8833:
1.394 bowersj2 8834: =over 4
1.191 harris41 8835:
1.394 bowersj2 8836: =item * B<con_lost>: unable to contact remote host
1.191 harris41 8837:
1.394 bowersj2 8838: =item * B<con_delayed>: unable to contact remote host, message will be delivered
8839: when the connection is brought back up
1.191 harris41 8840:
1.394 bowersj2 8841: =item * B<con_failed>: unable to contact remote host and unable to save message
8842: for later delivery
1.191 harris41 8843:
1.394 bowersj2 8844: =item * B<error:>: an error a occured, a description of the error follows the :
1.191 harris41 8845:
1.394 bowersj2 8846: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 8847: that was requested
1.191 harris41 8848:
1.243 albertel 8849: =back
1.191 harris41 8850:
1.243 albertel 8851: =head1 PUBLIC SUBROUTINES
1.191 harris41 8852:
1.243 albertel 8853: =head2 Session Environment Functions
1.191 harris41 8854:
1.243 albertel 8855: =over 4
1.191 harris41 8856:
1.394 bowersj2 8857: =item *
8858: X<appenv()>
1.949 raeburn 8859: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 8860: the user envirnoment file, and will be restored for each access this
1.620 albertel 8861: user makes during this session, also modifies the %env for the current
1.949 raeburn 8862: process. Optional rolesarrayref - if defined contains a reference to an array
8863: of roles which are exempt from the restriction on modifying user.role entries
8864: in the user's environment.db and in %env.
1.191 harris41 8865:
8866: =item *
1.394 bowersj2 8867: X<delenv()>
8868: B<delenv($regexp)>: removes all items from the session
8869: environment file that matches the regular expression in $regexp. The
1.620 albertel 8870: values are also delted from the current processes %env.
1.191 harris41 8871:
1.795 albertel 8872: =item * get_env_multiple($name)
8873:
8874: gets $name from the %env hash, it seemlessly handles the cases where multiple
8875: values may be defined and end up as an array ref.
8876:
8877: returns an array of values
8878:
1.243 albertel 8879: =back
8880:
8881: =head2 User Information
1.191 harris41 8882:
1.243 albertel 8883: =over 4
1.191 harris41 8884:
8885: =item *
1.394 bowersj2 8886: X<queryauthenticate()>
8887: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 8888: authentication scheme
8889:
8890: =item *
1.394 bowersj2 8891: X<authenticate()>
8892: B<authenticate($uname,$upass,$udom)>: try to
8893: authenticate user from domain's lib servers (first use the current
8894: one). C<$upass> should be the users password.
1.191 harris41 8895:
8896: =item *
1.394 bowersj2 8897: X<homeserver()>
8898: B<homeserver($uname,$udom)>: find the server which has
8899: the user's directory and files (there must be only one), this caches
8900: the answer, and also caches if there is a borken connection.
1.191 harris41 8901:
8902: =item *
1.394 bowersj2 8903: X<idget()>
8904: B<idget($udom,@ids)>: find the usernames behind a list of IDs
8905: (IDs are a unique resource in a domain, there must be only 1 ID per
8906: username, and only 1 username per ID in a specific domain) (returns
8907: hash: id=>name,id=>name)
1.191 harris41 8908:
8909: =item *
1.394 bowersj2 8910: X<idrget()>
8911: B<idrget($udom,@unames)>: find the IDs behind a list of
8912: usernames (returns hash: name=>id,name=>id)
1.191 harris41 8913:
8914: =item *
1.394 bowersj2 8915: X<idput()>
8916: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 8917:
8918: =item *
1.394 bowersj2 8919: X<rolesinit()>
8920: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 8921:
8922: =item *
1.551 albertel 8923: X<getsection()>
8924: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 8925: course $cname, return section name/number or '' for "not in course"
8926: and '-1' for "no section"
8927:
8928: =item *
1.394 bowersj2 8929: X<userenvironment()>
8930: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 8931: passed in @what from the requested user's environment, returns a hash
8932:
1.858 raeburn 8933: =item *
8934: X<userlog_query()>
1.859 albertel 8935: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
8936: activity.log file. %filters defines filters applied when parsing the
8937: log file. These can be start or end timestamps, or the type of action
8938: - log to look for Login or Logout events, check for Checkin or
8939: Checkout, role for role selection. The response is in the form
8940: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
8941: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 8942:
1.243 albertel 8943: =back
8944:
8945: =head2 User Roles
8946:
8947: =over 4
8948:
8949: =item *
8950:
1.810 raeburn 8951: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 8952: F: full access
8953: U,I,K: authentication modes (cxx only)
8954: '': forbidden
8955: 1: user needs to choose course
8956: 2: browse allowed
1.766 albertel 8957: A: passphrase authentication needed
1.243 albertel 8958:
8959: =item *
8960:
8961: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
8962: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
8963: and course level
8964:
8965: =item *
8966:
8967: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
8968: explanation of a user role term
8969:
1.832 raeburn 8970: =item *
8971:
1.935 raeburn 8972: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858 raeburn 8973: All arguments are optional. Returns a hash of a roles, either for
8974: co-author/assistant author roles for a user's Construction Space
1.906 albertel 8975: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 8976: In the hash, keys are set to colon-separated $uname,$udom,$role, and
8977: (optionally) if $withsec is true, a fourth colon-separated item - $section.
8978: For each key, value is set to colon-separated start and end times for
8979: the role. If no username and domain are specified, will default to
1.934 raeburn 8980: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 8981: of role statuses (active, future or previous), roles
8982: (e.g., cc,in, st etc.) and domains of the roles which can be used
8983: to restrict the list of roles reported. If no array ref is
8984: provided for types, will default to return only active roles.
1.834 albertel 8985:
1.243 albertel 8986: =back
8987:
8988: =head2 User Modification
8989:
8990: =over 4
8991:
8992: =item *
8993:
8994: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
8995: user for the level given by URL. Optional start and end dates (leave empty
8996: string or zero for "no date")
1.191 harris41 8997:
8998: =item *
8999:
1.243 albertel 9000: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
9001: change a users, password, possible return values are: ok,
9002: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
9003: refused
1.191 harris41 9004:
9005: =item *
9006:
1.243 albertel 9007: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 9008:
9009: =item *
9010:
1.243 albertel 9011: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) :
9012: modify user
1.191 harris41 9013:
9014: =item *
9015:
1.286 matthew 9016: modifystudent
9017:
9018: modify a students enrollment and identification information.
9019: The course id is resolved based on the current users environment.
9020: This means the envoking user must be a course coordinator or otherwise
9021: associated with a course.
9022:
1.297 matthew 9023: This call is essentially a wrapper for lonnet::modifyuser and
9024: lonnet::modify_student_enrollment
1.286 matthew 9025:
9026: Inputs:
9027:
9028: =over 4
9029:
9030: =item B<$udom> Students loncapa domain
9031:
9032: =item B<$uname> Students loncapa login name
9033:
9034: =item B<$uid> Students id/student number
9035:
9036: =item B<$umode> Students authentication mode
9037:
9038: =item B<$upass> Students password
9039:
9040: =item B<$first> Students first name
9041:
9042: =item B<$middle> Students middle name
9043:
9044: =item B<$last> Students last name
9045:
9046: =item B<$gene> Students generation
9047:
9048: =item B<$usec> Students section in course
9049:
9050: =item B<$end> Unix time of the roles expiration
9051:
9052: =item B<$start> Unix time of the roles start date
9053:
9054: =item B<$forceid> If defined, allow $uid to be changed
9055:
9056: =item B<$desiredhome> server to use as home server for student
9057:
9058: =back
1.297 matthew 9059:
9060: =item *
9061:
9062: modify_student_enrollment
9063:
9064: Change a students enrollment status in a class. The environment variable
9065: 'role.request.course' must be defined for this function to proceed.
9066:
9067: Inputs:
9068:
9069: =over 4
9070:
9071: =item $udom, students domain
9072:
9073: =item $uname, students name
9074:
9075: =item $uid, students user id
9076:
9077: =item $first, students first name
9078:
9079: =item $middle
9080:
9081: =item $last
9082:
9083: =item $gene
9084:
9085: =item $usec
9086:
9087: =item $end
9088:
9089: =item $start
9090:
9091: =back
9092:
1.191 harris41 9093:
9094: =item *
9095:
1.243 albertel 9096: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
9097: custom role; give a custom role to a user for the level given by URL. Specify
9098: name and domain of role author, and role name
1.191 harris41 9099:
9100: =item *
9101:
1.243 albertel 9102: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 9103:
9104: =item *
9105:
1.243 albertel 9106: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
9107:
9108: =back
9109:
9110: =head2 Course Infomation
9111:
9112: =over 4
1.191 harris41 9113:
9114: =item *
9115:
1.631 albertel 9116: coursedescription($courseid) : returns a hash of information about the
9117: specified course id, including all environment settings for the
9118: course, the description of the course will be in the hash under the
9119: key 'description'
1.191 harris41 9120:
9121: =item *
9122:
1.624 albertel 9123: resdata($name,$domain,$type,@which) : request for current parameter
9124: setting for a specific $type, where $type is either 'course' or 'user',
9125: @what should be a list of parameters to ask about. This routine caches
9126: answers for 5 minutes.
1.243 albertel 9127:
1.877 foxr 9128: =item *
9129:
9130: get_courseresdata($courseid, $domain) : dump the entire course resource
9131: data base, returning a hash that is keyed by the resource name and has
9132: values that are the resource value. I believe that the timestamps and
9133: versions are also returned.
9134:
9135:
1.243 albertel 9136: =back
9137:
9138: =head2 Course Modification
9139:
9140: =over 4
1.191 harris41 9141:
9142: =item *
9143:
1.243 albertel 9144: writecoursepref($courseid,%prefs) : write preferences (environment
9145: database) for a course
1.191 harris41 9146:
9147: =item *
9148:
1.243 albertel 9149: createcourse($udom,$description,$url) : make/modify course
9150:
9151: =back
9152:
9153: =head2 Resource Subroutines
9154:
9155: =over 4
1.191 harris41 9156:
9157: =item *
9158:
1.243 albertel 9159: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 9160:
9161: =item *
9162:
1.243 albertel 9163: repcopy($filename) : subscribes to the requested file, and attempts to
9164: replicate from the owning library server, Might return
1.607 raeburn 9165: 'unavailable', 'not_found', 'forbidden', 'ok', or
9166: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 9167: resource. Expects the local filesystem pathname
9168: (/home/httpd/html/res/....)
9169:
9170: =back
9171:
9172: =head2 Resource Information
9173:
9174: =over 4
1.191 harris41 9175:
9176: =item *
9177:
1.243 albertel 9178: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
9179: a vairety of different possible values, $varname should be a request
9180: string, and the other parameters can be used to specify who and what
9181: one is asking about.
9182:
9183: Possible values for $varname are environment.lastname (or other item
9184: from the envirnment hash), user.name (or someother aspect about the
9185: user), resource.0.maxtries (or some other part and parameter of a
9186: resource)
1.204 albertel 9187:
9188: =item *
9189:
1.243 albertel 9190: directcondval($number) : get current value of a condition; reads from a state
9191: string
1.204 albertel 9192:
9193: =item *
9194:
1.243 albertel 9195: condval($condidx) : value of condition index based on state
1.204 albertel 9196:
9197: =item *
9198:
1.243 albertel 9199: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
9200: resource's metadata, $what should be either a specific key, or either
9201: 'keys' (to get a list of possible keys) or 'packages' to get a list of
9202: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
9203:
9204: this function automatically caches all requests
1.191 harris41 9205:
9206: =item *
9207:
1.243 albertel 9208: metadata_query($query,$custom,$customshow) : make a metadata query against the
9209: network of library servers; returns file handle of where SQL and regex results
9210: will be stored for query
1.191 harris41 9211:
9212: =item *
9213:
1.243 albertel 9214: symbread($filename) : return symbolic list entry (filename argument optional);
9215: returns the data handle
1.191 harris41 9216:
9217: =item *
9218:
1.243 albertel 9219: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 9220: a possible symb for the URL in $thisfn, and if is an encryypted
9221: resource that the user accessed using /enc/ returns a 1 on success, 0
9222: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 9223: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 9224:
1.191 harris41 9225:
9226: =item *
9227:
1.243 albertel 9228: symbclean($symb) : removes versions numbers from a symb, returns the
9229: cleaned symb
1.191 harris41 9230:
9231: =item *
9232:
1.243 albertel 9233: is_on_map($uri) : checks if the $uri is somewhere on the current
9234: course map, user must be in a course for it to work.
1.191 harris41 9235:
9236: =item *
9237:
1.243 albertel 9238: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 9239:
9240: =item *
9241:
1.243 albertel 9242: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
9243: a random seed, all arguments are optional, if they aren't sent it uses the
9244: environment to derive them. Note: if symb isn't sent and it can't get one
9245: from &symbread it will use the current time as its return value
1.191 harris41 9246:
9247: =item *
9248:
1.243 albertel 9249: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
9250: unfakeable, receipt
1.191 harris41 9251:
9252: =item *
9253:
1.620 albertel 9254: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 9255:
9256: =item *
9257:
1.243 albertel 9258: countacc($url) : count the number of accesses to a given URL
1.191 harris41 9259:
9260: =item *
9261:
1.243 albertel 9262: 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 9263:
9264: =item *
9265:
1.243 albertel 9266: 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 9267:
9268: =item *
9269:
1.243 albertel 9270: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 9271:
9272: =item *
9273:
1.243 albertel 9274: devalidate($symb) : devalidate temporary spreadsheet calculations,
9275: forcing spreadsheet to reevaluate the resource scores next time.
9276:
9277: =back
9278:
9279: =head2 Storing/Retreiving Data
9280:
9281: =over 4
1.191 harris41 9282:
9283: =item *
9284:
1.243 albertel 9285: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
9286: for this url; hashref needs to be given and should be a \%hashname; the
9287: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 9288: be derived from the env
1.191 harris41 9289:
9290: =item *
9291:
1.243 albertel 9292: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
9293: uses critical subroutine
1.191 harris41 9294:
9295: =item *
9296:
1.243 albertel 9297: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
9298: all args are optional
1.191 harris41 9299:
9300: =item *
9301:
1.717 albertel 9302: dumpstore($namespace,$udom,$uname,$regexp,$range) :
9303: dumps the complete (or key matching regexp) namespace into a hash
9304: ($udom, $uname, $regexp, $range are optional) for a namespace that is
9305: normally &store()ed into
9306:
9307: $range should be either an integer '100' (give me the first 100
9308: matching records)
9309: or be two integers sperated by a - with no spaces
9310: '30-50' (give me the 30th through the 50th matching
9311: records)
9312:
9313:
9314: =item *
9315:
9316: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
9317: replaces a &store() version of data with a replacement set of data
9318: for a particular resource in a namespace passed in the $storehash hash
9319: reference
9320:
9321: =item *
9322:
1.243 albertel 9323: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
9324: works very similar to store/cstore, but all data is stored in a
9325: temporary location and can be reset using tmpreset, $storehash should
9326: be a hash reference, returns nothing on success
1.191 harris41 9327:
9328: =item *
9329:
1.243 albertel 9330: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
9331: similar to restore, but all data is stored in a temporary location and
9332: can be reset using tmpreset. Returns a hash of values on success,
9333: error string otherwise.
1.191 harris41 9334:
9335: =item *
9336:
1.243 albertel 9337: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
9338: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 9339:
9340: =item *
9341:
1.243 albertel 9342: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
9343: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 9344:
9345: =item *
9346:
1.243 albertel 9347: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
9348: namesp ($udom and $uname are optional)
1.191 harris41 9349:
9350: =item *
9351:
1.702 albertel 9352: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 9353: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 9354: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 9355:
1.702 albertel 9356: $range should be either an integer '100' (give me the first 100
9357: matching records)
9358: or be two integers sperated by a - with no spaces
9359: '30-50' (give me the 30th through the 50th matching
9360: records)
1.449 matthew 9361: =item *
9362:
9363: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
9364: $store can be a scalar, an array reference, or if the amount to be
9365: incremented is > 1, a hash reference.
9366:
9367: ($udom and $uname are optional)
1.191 harris41 9368:
9369: =item *
9370:
1.243 albertel 9371: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
9372: ($udom and $uname are optional)
1.191 harris41 9373:
9374: =item *
9375:
1.243 albertel 9376: cput($namespace,$storehash,$udom,$uname) : critical put
9377: ($udom and $uname are optional)
1.191 harris41 9378:
9379: =item *
9380:
1.748 albertel 9381: newput($namespace,$storehash,$udom,$uname) :
9382:
9383: Attempts to store the items in the $storehash, but only if they don't
9384: currently exist, if this succeeds you can be certain that you have
9385: successfully created a new key value pair in the $namespace db.
9386:
9387:
9388: Args:
9389: $namespace: name of database to store values to
9390: $storehash: hashref to store to the db
9391: $udom: (optional) domain of user containing the db
9392: $uname: (optional) name of user caontaining the db
9393:
9394: Returns:
9395: 'ok' -> succeeded in storing all keys of $storehash
9396: 'key_exists: <key>' -> failed to anything out of $storehash, as at
9397: least <key> already existed in the db (other
9398: requested keys may also already exist)
9399: 'error: <msg>' -> unable to tie the DB or other erorr occured
9400: 'con_lost' -> unable to contact request server
9401: 'refused' -> action was not allowed by remote machine
9402:
9403:
9404: =item *
9405:
1.243 albertel 9406: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
9407: reference filled in from namesp (encrypts the return communication)
9408: ($udom and $uname are optional)
1.191 harris41 9409:
9410: =item *
9411:
1.243 albertel 9412: log($udom,$name,$home,$message) : write to permanent log for user; use
9413: critical subroutine
9414:
1.806 raeburn 9415: =item *
9416:
1.860 raeburn 9417: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
9418: array reference filled in from namespace found in domain level on either
9419: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 9420:
9421: =item *
9422:
1.860 raeburn 9423: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
9424: domain level either on specified domain server ($uhome) or primary domain
9425: server ($udom and $uhome are optional)
1.806 raeburn 9426:
1.943 raeburn 9427: =item *
9428:
9429: get_domain_defaults($target_domain) : returns hash with defaults for
9430: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
9431: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
9432: or localauth), initial password or a kerberos realm, language (e.g., en-us).
9433: Values are retrieved from cache (if current), or from domain's configuration.db
9434: (if available), or lastly from values in lonTabs/dns_domain,tab,
9435: or lonTabs/domain.tab.
9436:
9437: %domdefaults = &get_auth_defaults($target_domain);
9438:
1.243 albertel 9439: =back
9440:
9441: =head2 Network Status Functions
9442:
9443: =over 4
1.191 harris41 9444:
9445: =item *
9446:
9447: dirlist($uri) : return directory list based on URI
9448:
9449: =item *
9450:
1.243 albertel 9451: spareserver() : find server with least workload from spare.tab
9452:
9453: =back
9454:
9455: =head2 Apache Request
9456:
9457: =over 4
1.191 harris41 9458:
9459: =item *
9460:
1.243 albertel 9461: ssi($url,%hash) : server side include, does a complete request cycle on url to
9462: localhost, posts hash
9463:
9464: =back
9465:
9466: =head2 Data to String to Data
9467:
9468: =over 4
1.191 harris41 9469:
9470: =item *
9471:
1.243 albertel 9472: hash2str(%hash) : convert a hash into a string complete with escaping and '='
9473: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 9474:
9475: =item *
9476:
1.243 albertel 9477: hashref2str($hashref) : convert a hashref into a string complete with
9478: escaping and '=' and '&' separators, supports elements that are
9479: arrayrefs and hashrefs
1.191 harris41 9480:
9481: =item *
9482:
1.243 albertel 9483: arrayref2str($arrayref) : convert an arrayref into a string complete
9484: with escaping and '&' separators, supports elements that are arrayrefs
9485: and hashrefs
1.191 harris41 9486:
9487: =item *
9488:
1.243 albertel 9489: str2hash($string) : convert string to hash using unescaping and
9490: splitting on '=' and '&', supports elements that are arrayrefs and
9491: hashrefs
1.191 harris41 9492:
9493: =item *
9494:
1.243 albertel 9495: str2array($string) : convert string to hash using unescaping and
9496: splitting on '&', supports elements that are arrayrefs and hashrefs
9497:
9498: =back
9499:
9500: =head2 Logging Routines
9501:
9502: =over 4
9503:
9504: These routines allow one to make log messages in the lonnet.log and
9505: lonnet.perm logfiles.
1.191 harris41 9506:
9507: =item *
9508:
1.243 albertel 9509: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 9510:
9511: =item *
9512:
1.243 albertel 9513: logthis() : append message to the normal lonnet.log file, it gets
9514: preiodically rolled over and deleted.
1.191 harris41 9515:
9516: =item *
9517:
1.243 albertel 9518: logperm() : append a permanent message to lonnet.perm.log, this log
9519: file never gets deleted by any automated portion of the system, only
9520: messages of critical importance should go in here.
9521:
9522: =back
9523:
9524: =head2 General File Helper Routines
9525:
9526: =over 4
1.191 harris41 9527:
9528: =item *
9529:
1.481 raeburn 9530: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
9531: (a) files in /uploaded
9532: (i) If a local copy of the file exists -
9533: compares modification date of local copy with last-modified date for
9534: definitive version stored on home server for course. If local copy is
9535: stale, requests a new version from the home server and stores it.
9536: If the original has been removed from the home server, then local copy
9537: is unlinked.
9538: (ii) If local copy does not exist -
9539: requests the file from the home server and stores it.
9540:
9541: If $caller is 'uploadrep':
9542: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
9543: for request for files originally uploaded via DOCS.
9544: - returns 'ok' if fresh local copy now available, -1 otherwise.
9545:
9546: Otherwise:
9547: This indicates a call from the content generation phase of the request.
9548: - returns the entire contents of the file or -1.
9549:
9550: (b) files in /res
9551: - returns the entire contents of a file or -1;
9552: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 9553:
1.712 albertel 9554:
9555: =item *
9556:
9557: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
9558: reference
9559:
9560: returns either a stat() list of data about the file or an empty list
9561: if the file doesn't exist or couldn't find out about it (connection
9562: problems or user unknown)
9563:
1.191 harris41 9564: =item *
9565:
1.243 albertel 9566: filelocation($dir,$file) : returns file system location of a file
9567: based on URI; meant to be "fairly clean" absolute reference, $dir is a
9568: directory that relative $file lookups are to looked in ($dir of /a/dir
9569: and a file of ../bob will become /a/bob)
1.191 harris41 9570:
9571: =item *
9572:
9573: hreflocation($dir,$file) : returns file system location or a URL; same as
9574: filelocation except for hrefs
9575:
9576: =item *
9577:
9578: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
9579:
1.243 albertel 9580: =back
9581:
1.608 albertel 9582: =head2 Usererfile file routines (/uploaded*)
9583:
9584: =over 4
9585:
9586: =item *
9587:
9588: userfileupload(): main rotine for putting a file in a user or course's
9589: filespace, arguments are,
9590:
1.620 albertel 9591: formname - required - this is the name of the element in $env where the
1.608 albertel 9592: filename, and the contents of the file to create/modifed exist
1.620 albertel 9593: the filename is in $env{'form.'.$formname.'.filename'} and the
9594: contents of the file is located in $env{'form.'.$formname}
1.608 albertel 9595: coursedoc - if true, store the file in the course of the active role
9596: of the current user
9597: subdir - required - subdirectory to put the file in under ../userfiles/
9598: if undefined, it will be placed in "unknown"
9599:
9600: (This routine calls clean_filename() to remove any dangerous
9601: characters from the filename, and then calls finuserfileupload() to
9602: complete the transaction)
9603:
9604: returns either the url of the uploaded file (/uploaded/....) if successful
9605: and /adm/notfound.html if unsuccessful
9606:
9607: =item *
9608:
9609: clean_filename(): routine for cleaing a filename up for storage in
9610: userfile space, argument is:
9611:
9612: filename - proposed filename
9613:
9614: returns: the new clean filename
9615:
9616: =item *
9617:
9618: finishuserfileupload(): routine that creaes and sends the file to
9619: userspace, probably shouldn't be called directly
9620:
9621: docuname: username or courseid of destination for the file
9622: docudom: domain of user/course of destination for the file
9623: formname: same as for userfileupload()
9624: fname: filename (inculding subdirectories) for the file
9625:
9626: returns either the url of the uploaded file (/uploaded/....) if successful
9627: and /adm/notfound.html if unsuccessful
9628:
9629: =item *
9630:
9631: renameuserfile(): renames an existing userfile to a new name
9632:
9633: Args:
9634: docuname: username or courseid of destination for the file
9635: docudom: domain of user/course of destination for the file
9636: old: current file name (including any subdirs under userfiles)
9637: new: desired file name (including any subdirs under userfiles)
9638:
9639: =item *
9640:
9641: mkdiruserfile(): creates a directory is a userfiles dir
9642:
9643: Args:
9644: docuname: username or courseid of destination for the file
9645: docudom: domain of user/course of destination for the file
9646: dir: dir to create (including any subdirs under userfiles)
9647:
9648: =item *
9649:
9650: removeuserfile(): removes a file that exists in userfiles
9651:
9652: Args:
9653: docuname: username or courseid of destination for the file
9654: docudom: domain of user/course of destination for the file
9655: fname: filname to delete (including any subdirs under userfiles)
9656:
9657: =item *
9658:
9659: removeuploadedurl(): convience function for removeuserfile()
9660:
9661: Args:
9662: url: a full /uploaded/... url to delete
9663:
1.747 albertel 9664: =item *
9665:
9666: get_portfile_permissions():
9667: Args:
9668: domain: domain of user or course contain the portfolio files
9669: user: name of user or num of course contain the portfolio files
9670: Returns:
9671: hashref of a dump of the proper file_permissions.db
9672:
9673:
9674: =item *
9675:
9676: get_access_controls():
9677:
9678: Args:
9679: current_permissions: the hash ref returned from get_portfile_permissions()
9680: group: (optional) the group you want the files associated with
9681: file: (optional) the file you want access info on
9682:
9683: Returns:
1.749 raeburn 9684: a hash (keys are file names) of hashes containing
9685: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
9686: values are XML containing access control settings (see below)
1.747 albertel 9687:
9688: Internal notes:
9689:
1.749 raeburn 9690: access controls are stored in file_permissions.db as key=value pairs.
9691: key -> path to file/file_name\0uniqueID:scope_end_start
9692: where scope -> public,guest,course,group,domains or users.
9693: end -> UNIX time for end of access (0 -> no end date)
9694: start -> UNIX time for start of access
9695:
9696: value -> XML description of access control
9697: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
9698: <start></start>
9699: <end></end>
9700:
9701: <password></password> for scope type = guest
9702:
9703: <domain></domain> for scope type = course or group
9704: <number></number>
9705: <roles id="">
9706: <role></role>
9707: <access></access>
9708: <section></section>
9709: <group></group>
9710: </roles>
9711:
9712: <dom></dom> for scope type = domains
9713:
9714: <users> for scope type = users
9715: <user>
9716: <uname></uname>
9717: <udom></udom>
9718: </user>
9719: </users>
9720: </scope>
9721:
9722: Access data is also aggregated for each file in an additional key=value pair:
9723: key -> path to file/file_name\0accesscontrol
9724: value -> reference to hash
9725: hash contains key = value pairs
9726: where key = uniqueID:scope_end_start
9727: value = UNIX time record was last updated
9728:
9729: Used to improve speed of look-ups of access controls for each file.
9730:
9731: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
9732:
9733: modify_access_controls():
9734:
9735: Modifies access controls for a portfolio file
9736: Args
9737: 1. file name
9738: 2. reference to hash of required changes,
9739: 3. domain
9740: 4. username
9741: where domain,username are the domain of the portfolio owner
9742: (either a user or a course)
9743:
9744: Returns:
9745: 1. result of additions or updates ('ok' or 'error', with error message).
9746: 2. result of deletions ('ok' or 'error', with error message).
9747: 3. reference to hash of any new or updated access controls.
9748: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
9749: key = integer (inbound ID)
9750: value = uniqueID
1.747 albertel 9751:
1.608 albertel 9752: =back
9753:
1.243 albertel 9754: =head2 HTTP Helper Routines
9755:
9756: =over 4
9757:
1.191 harris41 9758: =item *
9759:
9760: escape() : unpack non-word characters into CGI-compatible hex codes
9761:
9762: =item *
9763:
9764: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
9765:
1.243 albertel 9766: =back
9767:
9768: =head1 PRIVATE SUBROUTINES
9769:
9770: =head2 Underlying communication routines (Shouldn't call)
9771:
9772: =over 4
9773:
9774: =item *
9775:
9776: subreply() : tries to pass a message to lonc, returns con_lost if incapable
9777:
9778: =item *
9779:
9780: reply() : uses subreply to send a message to remote machine, logs all failures
9781:
9782: =item *
9783:
9784: critical() : passes a critical message to another server; if cannot
9785: get through then place message in connection buffer directory and
9786: returns con_delayed, if incapable of saving message, returns
9787: con_failed
9788:
9789: =item *
9790:
9791: reconlonc() : tries to reconnect lonc client processes.
9792:
9793: =back
9794:
9795: =head2 Resource Access Logging
9796:
9797: =over 4
9798:
9799: =item *
9800:
9801: flushcourselogs() : flush (save) buffer logs and access logs
9802:
9803: =item *
9804:
9805: courselog($what) : save message for course in hash
9806:
9807: =item *
9808:
9809: courseacclog($what) : save message for course using &courselog(). Perform
9810: special processing for specific resource types (problems, exams, quizzes, etc).
9811:
1.191 harris41 9812: =item *
9813:
9814: goodbye() : flush course logs and log shutting down; it is called in srm.conf
9815: as a PerlChildExitHandler
1.243 albertel 9816:
9817: =back
9818:
9819: =head2 Other
9820:
9821: =over 4
9822:
9823: =item *
9824:
9825: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 9826:
9827: =back
9828:
9829: =cut
1.877 foxr 9830:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>