Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1056.4.38
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1056.4.38! raeburn 4: # $Id: lonnet.pm,v 1.1056.4.37 2013/05/27 14:22:55 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.971 jms 30: =pod
31:
1.972 jms 32: =head1 NAME
33:
34: Apache::lonnet.pm
35:
36: =head1 SYNOPSIS
37:
38: This file is an interface to the lonc processes of
39: the LON-CAPA network as well as set of elaborated functions for handling information
40: necessary for navigating through a given cluster of LON-CAPA machines within a
41: domain. There are over 40 specialized functions in this module which handle the
42: reading and transmission of metadata, user information (ids, names, environments, roles,
43: logs), file information (storage, reading, directories, extensions, replication, embedded
44: styles and descriptors), educational resources (course descriptions, section names and
45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
46: and from more descriptive phrases or explanations.
47:
48: This is part of the LearningOnline Network with CAPA project
49: described at http://www.lon-capa.org.
50:
1.971 jms 51: =head1 Package Variables
52:
53: These are largely undocumented, so if you decipher one please note it here.
54:
55: =over 4
56:
57: =item $processmarker
58:
59: Contains the time this process was started and this servers host id.
60:
61: =item $dumpcount
62:
63: Counts the number of times a message log flush has been attempted (regardless
64: of success) by this process. Used as part of the filename when messages are
65: delayed.
66:
67: =back
68:
69: =cut
70:
1.1 albertel 71: package Apache::lonnet;
72:
73: use strict;
1.8 www 74: use LWP::UserAgent();
1.486 www 75: use HTTP::Date;
1.977 amueller 76: use Image::Magick;
77:
1.871 albertel 78: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1056.4.33 raeburn 79: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
80: %managerstab);
1.871 albertel 81:
82: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
83: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
84: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 85: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 86:
1.1 albertel 87: use IO::Socket;
1.31 www 88: use GDBM_File;
1.208 albertel 89: use HTML::LCParser;
1.88 www 90: use Fcntl qw(:flock);
1.870 albertel 91: use Storable qw(thaw nfreeze);
1.539 albertel 92: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 93: use Cache::Memcached;
1.676 albertel 94: use Digest::MD5;
1.790 albertel 95: use Math::Random;
1.1024 raeburn 96: use File::MMagic;
1.807 albertel 97: use LONCAPA qw(:DEFAULT :match);
1.740 www 98: use LONCAPA::Configuration;
1.1056.4.13 raeburn 99: use File::Copy;
1.676 albertel 100:
1.195 www 101: my $readit;
1.550 foxr 102: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 103:
1.619 albertel 104: require Exporter;
105:
106: our @ISA = qw (Exporter);
107: our @EXPORT = qw(%env);
108:
1.449 matthew 109:
1.1 albertel 110: # --------------------------------------------------------------------- Logging
1.729 www 111: {
112: my $logid;
113: sub instructor_log {
1.957 raeburn 114: my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
115: if (($cnum eq '') || ($cdom eq '')) {
116: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
117: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
118: }
1.729 www 119: $logid++;
1.957 raeburn 120: my $now = time();
121: my $id=$now.'00000'.$$.'00000'.$logid;
1.729 www 122: return &Apache::lonnet::put('nohist_'.$hash_name,
1.730 www 123: { $id => {
124: 'exe_uname' => $env{'user.name'},
125: 'exe_udom' => $env{'user.domain'},
1.957 raeburn 126: 'exe_time' => $now,
1.730 www 127: 'exe_ip' => $ENV{'REMOTE_ADDR'},
128: 'delflag' => $delflag,
129: 'logentry' => $storehash,
130: 'uname' => $uname,
131: 'udom' => $udom,
132: }
1.957 raeburn 133: },$cdom,$cnum);
1.729 www 134: }
135: }
1.1 albertel 136:
1.163 harris41 137: sub logtouch {
138: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 139: unless (-e "$execdir/logs/lonnet.log") {
140: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 141: close $fh;
142: }
143: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
144: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
145: }
146:
1.1 albertel 147: sub logthis {
148: my $message=shift;
149: my $execdir=$perlvar{'lonDaemons'};
150: my $now=time;
151: my $local=localtime($now);
1.448 albertel 152: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986 foxr 153: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
154: print $fh $logstring;
1.448 albertel 155: close($fh);
156: }
1.1 albertel 157: return 1;
158: }
159:
160: sub logperm {
161: my $message=shift;
162: my $execdir=$perlvar{'lonDaemons'};
163: my $now=time;
164: my $local=localtime($now);
1.448 albertel 165: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
166: print $fh "$now:$message:$local\n";
167: close($fh);
168: }
1.1 albertel 169: return 1;
170: }
171:
1.850 albertel 172: sub create_connection {
1.853 albertel 173: my ($hostname,$lonid) = @_;
1.851 albertel 174: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 175: Type => SOCK_STREAM,
176: Timeout => 10);
177: return 0 if (!$client);
1.890 albertel 178: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 179: my $result = <$client>;
180: chomp($result);
181: return 1 if ($result eq 'done');
182: return 0;
183: }
184:
1.983 raeburn 185: sub get_server_timezone {
186: my ($cnum,$cdom) = @_;
187: my $home=&homeserver($cnum,$cdom);
188: if ($home ne 'no_host') {
189: my $cachetime = 24*3600;
190: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
191: if (defined($cached)) {
192: return $timezone;
193: } else {
194: my $timezone = &reply('servertimezone',$home);
195: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
196: }
197: }
198: }
1.850 albertel 199:
1.1056.4.25 raeburn 200: sub get_server_distarch {
201: my ($lonhost,$ignore_cache) = @_;
202: if (defined($lonhost)) {
203: if (!defined(&hostname($lonhost))) {
204: return;
205: }
206: my $cachetime = 12*3600;
207: if (!$ignore_cache) {
208: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
209: if (defined($cached)) {
210: return $distarch;
211: }
212: }
213: my $rep = &reply('serverdistarch',$lonhost);
214: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
215: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
216: $rep eq '') {
217: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
218: }
219: }
220: return;
221: }
222:
1.993 raeburn 223: sub get_server_loncaparev {
1.1056.4.3 raeburn 224: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 225: if (defined($lonhost)) {
226: if (!defined(&hostname($lonhost))) {
227: undef($lonhost);
228: }
229: }
230: if (!defined($lonhost)) {
231: if (defined(&domain($dom,'primary'))) {
232: $lonhost=&domain($dom,'primary');
233: if ($lonhost eq 'no_host') {
234: undef($lonhost);
235: }
236: }
237: }
238: if (defined($lonhost)) {
1.1056.4.3 raeburn 239: my $cachetime = 12*3600;
240: if (!$ignore_cache) {
241: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
242: if (defined($cached)) {
243: return $loncaparev;
244: }
245: }
246: my ($answer,$loncaparev);
247: my @ids=¤t_machine_ids();
248: if (grep(/^\Q$lonhost\E$/,@ids)) {
249: $answer = $perlvar{'lonVersion'};
1.1056.4.7 raeburn 250: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1056.4.3 raeburn 251: $loncaparev = $1;
252: }
253: } else {
254: $answer = &reply('serverloncaparev',$lonhost);
255: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
256: if ($caller eq 'loncron') {
257: my $ua=new LWP::UserAgent;
1.1056.4.8 raeburn 258: $ua->timeout(4);
1.1056.4.3 raeburn 259: my $protocol = $protocol{$lonhost};
260: $protocol = 'http' if ($protocol ne 'https');
261: my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
262: my $request=new HTTP::Request('GET',$url);
263: my $response=$ua->request($request);
264: unless ($response->is_error()) {
265: my $content = $response->content;
1.1056.4.7 raeburn 266: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1056.4.3 raeburn 267: $loncaparev = $1;
268: }
269: }
270: } else {
271: $loncaparev = $loncaparevs{$lonhost};
272: }
1.1056.4.7 raeburn 273: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1056.4.3 raeburn 274: $loncaparev = $1;
275: }
276: }
277: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
278: }
279: }
280:
281: sub get_server_homeID {
282: my ($hostname,$ignore_cache,$caller) = @_;
283: unless ($ignore_cache) {
284: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
1.993 raeburn 285: if (defined($cached)) {
1.1056.4.3 raeburn 286: return $serverhomeID;
287: }
288: }
289: my $cachetime = 12*3600;
290: my $serverhomeID;
291: if ($caller eq 'loncron') {
292: my @machine_ids = &machine_ids($hostname);
293: foreach my $id (@machine_ids) {
294: my $response = &reply('serverhomeID',$id);
295: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
296: $serverhomeID = $response;
297: last;
298: }
1.993 raeburn 299: }
1.1056.4.3 raeburn 300: if ($serverhomeID eq '') {
301: $serverhomeID = $machine_ids[-1];
302: }
303: } else {
304: $serverhomeID = $serverhomeIDs{$hostname};
1.993 raeburn 305: }
1.1056.4.3 raeburn 306: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
1.993 raeburn 307: }
308:
1.1 albertel 309: # -------------------------------------------------- Non-critical communication
310: sub subreply {
311: my ($cmd,$server)=@_;
1.838 albertel 312: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 313: #
314: # With loncnew process trimming, there's a timing hole between lonc server
315: # process exit and the master server picking up the listen on the AF_UNIX
316: # socket. In that time interval, a lock file will exist:
317:
318: my $lockfile=$peerfile.".lock";
319: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
320: sleep(1);
321: }
322: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 323: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 324: #
1.550 foxr 325: # We'll give the connection a few tries before abandoning it. If
326: # connection is not possible, we'll con_lost back to the client.
327: #
328: my $client;
329: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
330: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
331: Type => SOCK_STREAM,
332: Timeout => 10);
1.869 albertel 333: if ($client) {
1.550 foxr 334: last; # Connected!
1.850 albertel 335: } else {
1.853 albertel 336: &create_connection(&hostname($server),$server);
1.550 foxr 337: }
1.850 albertel 338: sleep(1); # Try again later if failed connection.
1.550 foxr 339: }
340: my $answer;
341: if ($client) {
1.704 albertel 342: print $client "sethost:$server:$cmd\n";
1.550 foxr 343: $answer=<$client>;
344: if (!$answer) { $answer="con_lost"; }
345: chomp($answer);
346: } else {
347: $answer = 'con_lost'; # Failed connection.
348: }
1.1 albertel 349: return $answer;
350: }
351:
352: sub reply {
353: my ($cmd,$server)=@_;
1.838 albertel 354: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 355: my $answer=subreply($cmd,$server);
1.65 www 356: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 357: &logthis("<font color=\"blue\">WARNING:".
1.12 www 358: " $cmd to $server returned $answer</font>");
359: }
1.1 albertel 360: return $answer;
361: }
362:
363: # ----------------------------------------------------------- Send USR1 to lonc
364:
365: sub reconlonc {
1.891 albertel 366: my ($lonid) = @_;
367: my $hostname = &hostname($lonid);
368: if ($lonid) {
369: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
370: if ($hostname && -e $peerfile) {
371: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
372: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
373: Type => SOCK_STREAM,
374: Timeout => 10);
375: if ($client) {
376: print $client ("reset_retries\n");
377: my $answer=<$client>;
378: #reset just this one.
379: }
380: }
381: return;
382: }
383:
1.836 www 384: &logthis("Trying to reconnect lonc");
1.1 albertel 385: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 386: if (open(my $fh,"<$loncfile")) {
1.1 albertel 387: my $loncpid=<$fh>;
388: chomp($loncpid);
389: if (kill 0 => $loncpid) {
390: &logthis("lonc at pid $loncpid responding, sending USR1");
391: kill USR1 => $loncpid;
392: sleep 1;
1.836 www 393: } else {
1.12 www 394: &logthis(
1.672 albertel 395: "<font color=\"blue\">WARNING:".
1.12 www 396: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 397: }
398: } else {
1.836 www 399: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 400: }
401: }
402:
403: # ------------------------------------------------------ Critical communication
1.12 www 404:
1.1 albertel 405: sub critical {
406: my ($cmd,$server)=@_;
1.838 albertel 407: unless (&hostname($server)) {
1.672 albertel 408: &logthis("<font color=\"blue\">WARNING:".
1.89 www 409: " Critical message to unknown server ($server)</font>");
410: return 'no_such_host';
411: }
1.1 albertel 412: my $answer=reply($cmd,$server);
413: if ($answer eq 'con_lost') {
414: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 415: my $answer=reply($cmd,$server);
1.1 albertel 416: if ($answer eq 'con_lost') {
417: my $now=time;
418: my $middlename=$cmd;
1.5 www 419: $middlename=substr($middlename,0,16);
1.1 albertel 420: $middlename=~s/\W//g;
421: my $dfilename=
1.305 www 422: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
423: $dumpcount++;
1.1 albertel 424: {
1.448 albertel 425: my $dfh;
426: if (open($dfh,">$dfilename")) {
427: print $dfh "$cmd\n";
428: close($dfh);
429: }
1.1 albertel 430: }
431: sleep 2;
432: my $wcmd='';
433: {
1.448 albertel 434: my $dfh;
435: if (open($dfh,"<$dfilename")) {
436: $wcmd=<$dfh>;
437: close($dfh);
438: }
1.1 albertel 439: }
440: chomp($wcmd);
1.7 www 441: if ($wcmd eq $cmd) {
1.672 albertel 442: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 443: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 444: &logperm("D:$server:$cmd");
445: return 'con_delayed';
446: } else {
1.672 albertel 447: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 448: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 449: &logperm("F:$server:$cmd");
450: return 'con_failed';
451: }
452: }
453: }
454: return $answer;
1.405 albertel 455: }
456:
1.755 albertel 457: # ------------------------------------------- check if return value is an error
458:
459: sub error {
460: my ($result) = @_;
1.756 albertel 461: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 462: if ($2 == 2) { return undef; }
463: return $1;
464: }
465: return undef;
466: }
467:
1.783 albertel 468: sub convert_and_load_session_env {
469: my ($lonidsdir,$handle)=@_;
470: my @profile;
471: {
1.917 albertel 472: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
473: if (!$opened) {
1.915 albertel 474: return 0;
475: }
1.783 albertel 476: flock($idf,LOCK_SH);
477: @profile=<$idf>;
478: close($idf);
479: }
480: my %temp_env;
481: foreach my $line (@profile) {
1.786 albertel 482: if ($line !~ m/=/) {
483: return 0;
484: }
1.783 albertel 485: chomp($line);
486: my ($envname,$envvalue)=split(/=/,$line,2);
487: $temp_env{&unescape($envname)} = &unescape($envvalue);
488: }
489: unlink("$lonidsdir/$handle.id");
490: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
491: 0640)) {
492: %disk_env = %temp_env;
493: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
494: untie(%disk_env);
495: }
1.786 albertel 496: return 1;
1.783 albertel 497: }
498:
1.374 www 499: # ------------------------------------------- Transfer profile into environment
1.780 albertel 500: my $env_loaded;
501: sub transfer_profile_to_env {
1.788 albertel 502: my ($lonidsdir,$handle,$force_transfer) = @_;
503: if (!$force_transfer && $env_loaded) { return; }
1.374 www 504:
1.720 albertel 505: if (!defined($lonidsdir)) {
506: $lonidsdir = $perlvar{'lonIDsDir'};
507: }
508: if (!defined($handle)) {
509: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
510: }
511:
1.786 albertel 512: my $convert;
513: {
1.917 albertel 514: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
515: if (!$opened) {
1.915 albertel 516: return;
517: }
1.786 albertel 518: flock($idf,LOCK_SH);
519: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
520: &GDBM_READER(),0640)) {
521: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
522: untie(%disk_env);
523: } else {
524: $convert = 1;
525: }
526: }
527: if ($convert) {
528: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
529: &logthis("Failed to load session, or convert session.");
530: }
1.374 www 531: }
1.783 albertel 532:
1.786 albertel 533: my %remove;
1.783 albertel 534: while ( my $envname = each(%env) ) {
1.433 matthew 535: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
536: if ($time < time-300) {
1.783 albertel 537: $remove{$key}++;
1.433 matthew 538: }
539: }
540: }
1.783 albertel 541:
1.619 albertel 542: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 543: $env_loaded=1;
1.783 albertel 544: foreach my $expired_key (keys(%remove)) {
1.433 matthew 545: &delenv($expired_key);
1.374 www 546: }
1.1 albertel 547: }
548:
1.916 albertel 549: # ---------------------------------------------------- Check for valid session
550: sub check_for_valid_session {
551: my ($r) = @_;
552: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
553: my $lonid=$cookies{'lonID'};
554: return undef if (!$lonid);
555:
556: my $handle=&LONCAPA::clean_handle($lonid->value);
557: my $lonidsdir=$r->dir_config('lonIDsDir');
558: return undef if (!-e "$lonidsdir/$handle.id");
559:
1.917 albertel 560: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
561: return undef if (!$opened);
1.916 albertel 562:
563: flock($idf,LOCK_SH);
564: my %disk_env;
565: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
566: &GDBM_READER(),0640)) {
567: return undef;
568: }
569:
570: if (!defined($disk_env{'user.name'})
571: || !defined($disk_env{'user.domain'})) {
572: return undef;
573: }
574: return $handle;
575: }
576:
1.830 albertel 577: sub timed_flock {
578: my ($file,$lock_type) = @_;
579: my $failed=0;
580: eval {
581: local $SIG{__DIE__}='DEFAULT';
582: local $SIG{ALRM}=sub {
583: $failed=1;
584: die("failed lock");
585: };
586: alarm(13);
587: flock($file,$lock_type);
588: alarm(0);
589: };
590: if ($failed) {
591: return undef;
592: } else {
593: return 1;
594: }
595: }
596:
1.5 www 597: # ---------------------------------------------------------- Append Environment
598:
599: sub appenv {
1.949 raeburn 600: my ($newenv,$roles) = @_;
601: if (ref($newenv) eq 'HASH') {
602: foreach my $key (keys(%{$newenv})) {
603: my $refused = 0;
604: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
605: $refused = 1;
606: if (ref($roles) eq 'ARRAY') {
607: my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
608: if (grep(/^\Q$role\E$/,@{$roles})) {
609: $refused = 0;
610: }
611: }
612: }
613: if ($refused) {
614: &logthis("<font color=\"blue\">WARNING: ".
615: "Attempt to modify environment ".$key." to ".$newenv->{$key}
616: .'</font>');
617: delete($newenv->{$key});
618: } else {
619: $env{$key}=$newenv->{$key};
620: }
621: }
622: my $opened = open(my $env_file,'+<',$env{'user.environment'});
623: if ($opened
624: && &timed_flock($env_file,LOCK_EX)
625: &&
626: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
627: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
628: while (my ($key,$value) = each(%{$newenv})) {
629: $disk_env{$key} = $value;
630: }
631: untie(%disk_env);
1.35 www 632: }
1.191 harris41 633: }
1.56 www 634: return 'ok';
635: }
636: # ----------------------------------------------------- Delete from Environment
637:
638: sub delenv {
1.987 raeburn 639: my ($delthis,$regexp) = @_;
1.56 www 640: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 641: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 642: "Attempt to delete from environment ".$delthis);
643: return 'error';
644: }
1.917 albertel 645: my $opened = open(my $env_file,'+<',$env{'user.environment'});
646: if ($opened
1.915 albertel 647: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 648: &&
649: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
650: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 651: foreach my $key (keys(%disk_env)) {
1.987 raeburn 652: if ($regexp) {
653: if ($key=~/^$delthis/) {
654: delete($env{$key});
655: delete($disk_env{$key});
656: }
657: } else {
658: if ($key=~/^\Q$delthis\E/) {
659: delete($env{$key});
660: delete($disk_env{$key});
661: }
662: }
1.448 albertel 663: }
1.783 albertel 664: untie(%disk_env);
1.5 www 665: }
666: return 'ok';
1.369 albertel 667: }
668:
1.790 albertel 669: sub get_env_multiple {
670: my ($name) = @_;
671: my @values;
672: if (defined($env{$name})) {
673: # exists is it an array
674: if (ref($env{$name})) {
675: @values=@{ $env{$name} };
676: } else {
677: $values[0]=$env{$name};
678: }
679: }
680: return(@values);
681: }
682:
1.958 www 683: # ------------------------------------------------------------------- Locking
684:
685: sub set_lock {
686: my ($text)=@_;
687: $locknum++;
688: my $id=$$.'-'.$locknum;
689: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
690: 'session.lock.'.$id => $text});
691: return $id;
692: }
693:
694: sub get_locks {
695: my $num=0;
696: my %texts=();
697: foreach my $lock (split(/\,/,$env{'session.locks'})) {
698: if ($lock=~/\w/) {
699: $num++;
700: $texts{$lock}=$env{'session.lock.'.$lock};
701: }
702: }
703: return ($num,%texts);
704: }
705:
706: sub remove_lock {
707: my ($id)=@_;
708: my $newlocks='';
709: foreach my $lock (split(/\,/,$env{'session.locks'})) {
710: if (($lock=~/\w/) && ($lock ne $id)) {
711: $newlocks.=','.$lock;
712: }
713: }
714: &appenv({'session.locks' => $newlocks});
715: &delenv('session.lock.'.$id);
716: }
717:
718: sub remove_all_locks {
719: my $activelocks=$env{'session.locks'};
720: foreach my $lock (split(/\,/,$env{'session.locks'})) {
721: if ($lock=~/\w/) {
722: &remove_lock($lock);
723: }
724: }
725: }
726:
727:
1.369 albertel 728: # ------------------------------------------ Find out current server userload
729: sub userload {
730: my $numusers=0;
731: {
732: opendir(LONIDS,$perlvar{'lonIDsDir'});
733: my $filename;
734: my $curtime=time;
735: while ($filename=readdir(LONIDS)) {
1.925 albertel 736: next if ($filename eq '.' || $filename eq '..');
737: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 738: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 739: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 740: }
741: closedir(LONIDS);
742: }
743: my $userloadpercent=0;
744: my $maxuserload=$perlvar{'lonUserLoadLim'};
745: if ($maxuserload) {
1.371 albertel 746: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 747: }
1.372 albertel 748: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 749: return $userloadpercent;
1.283 www 750: }
751:
752: # ------------------------------------------ Fight off request when overloaded
753:
754: sub overloaderror {
755: my ($r,$checkserver)=@_;
756: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
757: my $loadavg;
758: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 759: open(my $loadfile,'/proc/loadavg');
1.283 www 760: $loadavg=<$loadfile>;
761: $loadavg =~ s/\s.*//g;
1.285 matthew 762: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 763: close($loadfile);
1.283 www 764: } else {
765: $loadavg=&reply('load',$checkserver);
766: }
1.285 matthew 767: my $overload=$loadavg-100;
1.283 www 768: if ($overload>0) {
1.285 matthew 769: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 770: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 771: return 413;
1.283 www 772: }
773: return '';
1.5 www 774: }
1.1 albertel 775:
776: # ------------------------------ Find server with least workload from spare.tab
1.11 www 777:
1.1 albertel 778: sub spareserver {
1.1056.4.23 raeburn 779: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 780: my $spare_server;
1.370 albertel 781: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 782: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
783: : $userloadpercent;
1.1056.4.8 raeburn 784: my ($uint_dom,$remotesessions);
1.1056.4.23 raeburn 785: if (($udom ne '') && (&domain($udom) ne '')) {
786: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1.1056.4.8 raeburn 787: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.23 raeburn 788: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
1.1056.4.8 raeburn 789: $remotesessions = $udomdefaults{'remotesessions'};
790: }
1.784 albertel 791: foreach my $try_server (@{ $spareid{'primary'} }) {
1.1056.4.8 raeburn 792: if ($uint_dom) {
1.1056.4.23 raeburn 793: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
794: $try_server));
1.1056.4.8 raeburn 795: }
1.784 albertel 796: ($spare_server, $lowest_load) =
797: &compare_server_load($try_server, $spare_server, $lowest_load);
798: }
799:
800: my $found_server = ($spare_server ne '' && $lowest_load < 100);
801:
802: if (!$found_server) {
803: foreach my $try_server (@{ $spareid{'default'} }) {
1.1056.4.8 raeburn 804: if ($uint_dom) {
1.1056.4.23 raeburn 805: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
806: $try_server));
1.1056.4.8 raeburn 807: }
1.784 albertel 808: ($spare_server, $lowest_load) =
809: &compare_server_load($try_server, $spare_server, $lowest_load);
810: }
811: }
812:
813: if (!$want_server_name) {
1.968 raeburn 814: my $protocol = 'http';
815: if ($protocol{$spare_server} eq 'https') {
816: $protocol = $protocol{$spare_server};
817: }
1.1001 raeburn 818: if (defined($spare_server)) {
819: my $hostname = &hostname($spare_server);
1.1056.4.8 raeburn 820: if (defined($hostname)) {
1.1001 raeburn 821: $spare_server = $protocol.'://'.$hostname;
822: }
823: }
1.784 albertel 824: }
825: return $spare_server;
826: }
827:
828: sub compare_server_load {
1.1056.4.38! raeburn 829: my ($try_server, $spare_server, $lowest_load, $required) = @_;
! 830:
! 831: if ($required) {
! 832: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
! 833: my $remoterev = &get_server_loncaparev(undef,$try_server);
! 834: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
! 835: if (($major eq '' && $minor eq '') ||
! 836: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
! 837: return ($spare_server,$lowest_load);
! 838: }
! 839: }
1.784 albertel 840:
841: my $loadans = &reply('load', $try_server);
842: my $userloadans = &reply('userload',$try_server);
843:
844: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1056.4.29 raeburn 845: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 846: }
847:
848: my $load;
849: if ($loadans =~ /\d/) {
850: if ($userloadans =~ /\d/) {
851: #both are numbers, pick the bigger one
852: $load = ($loadans > $userloadans) ? $loadans
853: : $userloadans;
1.411 albertel 854: } else {
1.784 albertel 855: $load = $loadans;
1.411 albertel 856: }
1.784 albertel 857: } else {
858: $load = $userloadans;
859: }
860:
861: if (($load =~ /\d/) && ($load < $lowest_load)) {
862: $spare_server = $try_server;
863: $lowest_load = $load;
1.370 albertel 864: }
1.784 albertel 865: return ($spare_server,$lowest_load);
1.202 matthew 866: }
1.914 albertel 867:
868: # --------------------------- ask offload servers if user already has a session
869: sub find_existing_session {
870: my ($udom,$uname) = @_;
871: foreach my $try_server (@{ $spareid{'primary'} },
872: @{ $spareid{'default'} }) {
873: return $try_server if (&has_user_session($try_server, $udom, $uname));
874: }
875: return;
876: }
877:
878: # -------------------------------- ask if server already has a session for user
879: sub has_user_session {
880: my ($lonid,$udom,$uname) = @_;
881: my $result = &reply(join(':','userhassession',
882: map {&escape($_)} ($udom,$uname)),$lonid);
883: return 1 if ($result eq 'ok');
884:
885: return 0;
886: }
887:
1.1056.4.3 raeburn 888: # --------- determine least loaded server in a user's domain which allows login
889:
890: sub choose_server {
1.1056.4.38! raeburn 891: my ($udom,$checkloginvia,$required) = @_;
1.1056.4.3 raeburn 892: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
893: my %servers = &get_servers($udom);
894: my $lowest_load = 30000;
1.1056.4.36 raeburn 895: my ($login_host,$hostname,$portal_path,$isredirect);
1.1056.4.3 raeburn 896: foreach my $lonhost (keys(%servers)) {
1.1056.4.31 raeburn 897: my $loginvia;
898: if ($checkloginvia) {
899: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
900: if ($loginvia) {
901: my ($server,$path) = split(/:/,$loginvia);
902: ($login_host, $lowest_load) =
1.1056.4.38! raeburn 903: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.31 raeburn 904: if ($login_host eq $server) {
905: $portal_path = $path;
1.1056.4.36 raeburn 906: $isredirect = 1;
1.1056.4.31 raeburn 907: }
908: } else {
909: ($login_host, $lowest_load) =
1.1056.4.38! raeburn 910: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.31 raeburn 911: if ($login_host eq $lonhost) {
912: $portal_path = '';
1.1056.4.36 raeburn 913: $isredirect = '';
1.1056.4.31 raeburn 914: }
915: }
916: } else {
1.1056.4.3 raeburn 917: ($login_host, $lowest_load) =
1.1056.4.38! raeburn 918: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.3 raeburn 919: }
920: }
921: if ($login_host ne '') {
1.1056.4.31 raeburn 922: $hostname = &hostname($login_host);
1.1056.4.3 raeburn 923: }
1.1056.4.36 raeburn 924: return ($login_host,$hostname,$portal_path,$isredirect);
1.1056.4.3 raeburn 925: }
926:
1.202 matthew 927: # --------------------------------------------- Try to change a user's password
928:
929: sub changepass {
1.799 raeburn 930: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 931: $currentpass = &escape($currentpass);
932: $newpass = &escape($newpass);
1.1030 raeburn 933: my $lonhost = $perlvar{'lonHostID'};
934: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 935: $server);
936: if (! $answer) {
937: &logthis("No reply on password change request to $server ".
938: "by $uname in domain $udom.");
939: } elsif ($answer =~ "^ok") {
940: &logthis("$uname in $udom successfully changed their password ".
941: "on $server.");
942: } elsif ($answer =~ "^pwchange_failure") {
943: &logthis("$uname in $udom was unable to change their password ".
944: "on $server. The action was blocked by either lcpasswd ".
945: "or pwchange");
946: } elsif ($answer =~ "^non_authorized") {
947: &logthis("$uname in $udom did not get their password correct when ".
948: "attempting to change it on $server.");
949: } elsif ($answer =~ "^auth_mode_error") {
950: &logthis("$uname in $udom attempted to change their password despite ".
951: "not being locally or internally authenticated on $server.");
952: } elsif ($answer =~ "^unknown_user") {
953: &logthis("$uname in $udom attempted to change their password ".
954: "on $server but were unable to because $server is not ".
955: "their home server.");
956: } elsif ($answer =~ "^refused") {
957: &logthis("$server refused to change $uname in $udom password because ".
958: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 959: } elsif ($answer =~ "invalid_client") {
960: &logthis("$server refused to change $uname in $udom password because ".
961: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 962: }
963: return $answer;
1.1 albertel 964: }
965:
1.169 harris41 966: # ----------------------- Try to determine user's current authentication scheme
967:
968: sub queryauthenticate {
969: my ($uname,$udom)=@_;
1.456 albertel 970: my $uhome=&homeserver($uname,$udom);
971: if (!$uhome) {
972: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
973: return 'no_host';
974: }
975: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
976: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
977: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 978: }
1.456 albertel 979: return $answer;
1.169 harris41 980: }
981:
1.1 albertel 982: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 983:
1.1 albertel 984: sub authenticate {
1.1056.4.3 raeburn 985: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 986: $upass=&escape($upass);
987: $uname= &LONCAPA::clean_username($uname);
1.836 www 988: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 989: my $newhome;
1.836 www 990: if ((!$uhome) || ($uhome eq 'no_host')) {
991: # Maybe the machine was offline and only re-appeared again recently?
992: &reconlonc();
993: # One more
1.952 raeburn 994: $uhome=&homeserver($uname,$udom,1);
995: if (($uhome eq 'no_host') && $checkdefauth) {
996: if (defined(&domain($udom,'primary'))) {
997: $newhome=&domain($udom,'primary');
998: }
999: if ($newhome ne '') {
1000: $uhome = $newhome;
1001: }
1002: }
1.836 www 1003: if ((!$uhome) || ($uhome eq 'no_host')) {
1004: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1005: return 'no_host';
1006: }
1.1 albertel 1007: }
1.1056.4.3 raeburn 1008: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1009: if ($answer eq 'authorized') {
1.952 raeburn 1010: if ($newhome) {
1011: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1012: return 'no_account_on_host';
1013: } else {
1014: &logthis("User $uname at $udom authorized by $uhome");
1015: return $uhome;
1016: }
1.471 albertel 1017: }
1018: if ($answer eq 'non_authorized') {
1019: &logthis("User $uname at $udom rejected by $uhome");
1020: return 'no_host';
1.9 www 1021: }
1.471 albertel 1022: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1023: return 'no_host';
1024: }
1025:
1.1056.4.3 raeburn 1026: sub can_host_session {
1027: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1028: my $canhost = 1;
1029: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1030: if (ref($remotesessions) eq 'HASH') {
1031: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1032: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1033: $canhost = 0;
1034: } else {
1035: $canhost = 1;
1036: }
1037: }
1038: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1039: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1040: $canhost = 1;
1041: } else {
1042: $canhost = 0;
1043: }
1044: }
1045: if ($canhost) {
1046: if ($remotesessions->{'version'} ne '') {
1047: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1048: if ($reqmajor ne '' && $reqminor ne '') {
1049: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1050: my $major = $1;
1051: my $minor = $2;
1052: if (($major < $reqmajor ) ||
1053: (($major == $reqmajor) && ($minor < $reqminor))) {
1054: $canhost = 0;
1055: }
1056: } else {
1057: $canhost = 0;
1058: }
1059: }
1060: }
1061: }
1062: }
1063: if ($canhost) {
1064: if (ref($hostedsessions) eq 'HASH') {
1.1056.4.28 raeburn 1065: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1066: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.3 raeburn 1067: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1068: if (($uint_dom ne '') &&
1069: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1056.4.3 raeburn 1070: $canhost = 0;
1071: } else {
1072: $canhost = 1;
1073: }
1074: }
1075: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1076: if (($uint_dom ne '') &&
1077: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1056.4.3 raeburn 1078: $canhost = 1;
1079: } else {
1080: $canhost = 0;
1081: }
1082: }
1083: }
1084: }
1085: return $canhost;
1086: }
1087:
1.1056.4.8 raeburn 1088: sub spare_can_host {
1089: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1090: my $canhost=1;
1091: my @intdoms;
1092: my $internet_names = &Apache::lonnet::get_internet_names($try_server);
1093: if (ref($internet_names) eq 'ARRAY') {
1094: @intdoms = @{$internet_names};
1095: }
1096: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1097: my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
1098: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
1099: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
1100: my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
1101: $canhost = &can_host_session($udom,$try_server,$remoterev,
1102: $remotesessions,
1103: $defdomdefaults{'hostedsessions'});
1104: }
1105: return $canhost;
1106: }
1107:
1.1 albertel 1108: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1109:
1.599 albertel 1110: my %homecache;
1.1 albertel 1111: sub homeserver {
1.230 stredwic 1112: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1113: my $index="$uname:$udom";
1.426 albertel 1114:
1.599 albertel 1115: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1116:
1117: my %servers = &get_servers($udom,'library');
1118: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1119: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1120: exists($badServerCache{$tryserver}));
1.841 albertel 1121:
1122: my $answer=reply("home:$udom:$uname",$tryserver);
1123: if ($answer eq 'found') {
1124: delete($badServerCache{$tryserver});
1125: return $homecache{$index}=$tryserver;
1126: } elsif ($answer eq 'no_host') {
1127: $badServerCache{$tryserver}=1;
1128: }
1.1 albertel 1129: }
1130: return 'no_host';
1.70 www 1131: }
1132:
1133: # ------------------------------------- Find the usernames behind a list of IDs
1134:
1135: sub idget {
1136: my ($udom,@ids)=@_;
1137: my %returnhash=();
1138:
1.841 albertel 1139: my %servers = &get_servers($udom,'library');
1140: foreach my $tryserver (keys(%servers)) {
1141: my $idlist=join('&',@ids);
1142: $idlist=~tr/A-Z/a-z/;
1143: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1144: my @answer=();
1145: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1146: @answer=split(/\&/,$reply);
1147: } ;
1148: my $i;
1149: for ($i=0;$i<=$#ids;$i++) {
1150: if ($answer[$i]) {
1151: $returnhash{$ids[$i]}=$answer[$i];
1152: }
1153: }
1154: }
1.70 www 1155: return %returnhash;
1156: }
1157:
1158: # ------------------------------------- Find the IDs behind a list of usernames
1159:
1160: sub idrget {
1161: my ($udom,@unames)=@_;
1162: my %returnhash=();
1.800 albertel 1163: foreach my $uname (@unames) {
1164: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1165: }
1.70 www 1166: return %returnhash;
1167: }
1168:
1169: # ------------------------------- Store away a list of names and associated IDs
1170:
1171: sub idput {
1172: my ($udom,%ids)=@_;
1173: my %servers=();
1.800 albertel 1174: foreach my $uname (keys(%ids)) {
1175: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1176: my $uhom=&homeserver($uname,$udom);
1.70 www 1177: if ($uhom ne 'no_host') {
1.800 albertel 1178: my $id=&escape($ids{$uname});
1.70 www 1179: $id=~tr/A-Z/a-z/;
1.800 albertel 1180: my $esc_unam=&escape($uname);
1.70 www 1181: if ($servers{$uhom}) {
1.800 albertel 1182: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1183: } else {
1.800 albertel 1184: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1185: }
1186: }
1.191 harris41 1187: }
1.800 albertel 1188: foreach my $server (keys(%servers)) {
1189: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1190: }
1.344 www 1191: }
1192:
1.1023 raeburn 1193: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1194: sub dump_dom {
1.1023 raeburn 1195: my ($namespace,$udom,$regexp,$range)=@_;
1.1012 raeburn 1196: if (!$udom) {
1197: $udom=$env{'user.domain'};
1198: }
1199: my %returnhash;
1.1023 raeburn 1200: if ($udom) {
1201: my $uname = &get_domainconfiguser($udom);
1202: %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012 raeburn 1203: }
1204: return %returnhash;
1205: }
1206:
1.1023 raeburn 1207: # ------------------------------------------ get items from domain db files
1.806 raeburn 1208:
1209: sub get_dom {
1.860 raeburn 1210: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 1211: my $items='';
1212: foreach my $item (@$storearr) {
1213: $items.=&escape($item).'&';
1214: }
1215: $items=~s/\&$//;
1.860 raeburn 1216: if (!$udom) {
1217: $udom=$env{'user.domain'};
1218: if (defined(&domain($udom,'primary'))) {
1219: $uhome=&domain($udom,'primary');
1220: } else {
1.874 albertel 1221: undef($uhome);
1.860 raeburn 1222: }
1223: } else {
1224: if (!$uhome) {
1225: if (defined(&domain($udom,'primary'))) {
1226: $uhome=&domain($udom,'primary');
1227: }
1228: }
1229: }
1230: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1231: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1232: my %returnhash;
1.875 albertel 1233: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1234: return %returnhash;
1235: }
1.806 raeburn 1236: my @pairs=split(/\&/,$rep);
1237: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1238: return @pairs;
1239: }
1240: my $i=0;
1241: foreach my $item (@$storearr) {
1242: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1243: $i++;
1244: }
1245: return %returnhash;
1246: } else {
1.880 banghart 1247: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1248: }
1249: }
1250:
1251: # -------------------------------------------- put items in domain db files
1252:
1253: sub put_dom {
1.860 raeburn 1254: my ($namespace,$storehash,$udom,$uhome)=@_;
1255: if (!$udom) {
1256: $udom=$env{'user.domain'};
1257: if (defined(&domain($udom,'primary'))) {
1258: $uhome=&domain($udom,'primary');
1259: } else {
1.874 albertel 1260: undef($uhome);
1.860 raeburn 1261: }
1262: } else {
1263: if (!$uhome) {
1264: if (defined(&domain($udom,'primary'))) {
1265: $uhome=&domain($udom,'primary');
1266: }
1267: }
1268: }
1269: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1270: my $items='';
1271: foreach my $item (keys(%$storehash)) {
1272: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1273: }
1274: $items=~s/\&$//;
1275: return &reply("putdom:$udom:$namespace:$items",$uhome);
1276: } else {
1.860 raeburn 1277: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1278: }
1279: }
1280:
1.1023 raeburn 1281: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1282: sub newput_dom {
1.1023 raeburn 1283: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1284: my $result;
1285: if (!$udom) {
1286: $udom=$env{'user.domain'};
1287: }
1.1023 raeburn 1288: if ($udom) {
1289: my $uname = &get_domainconfiguser($udom);
1290: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1291: }
1292: return $result;
1293: }
1294:
1.1023 raeburn 1295: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1296: sub del_dom {
1.1023 raeburn 1297: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1298: if (ref($storearr) eq 'ARRAY') {
1299: if (!$udom) {
1300: $udom=$env{'user.domain'};
1301: }
1.1023 raeburn 1302: if ($udom) {
1303: my $uname = &get_domainconfiguser($udom);
1304: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1305: }
1306: }
1307: }
1308:
1.1023 raeburn 1309: # ----------------------------------construct domainconfig user for a domain
1310: sub get_domainconfiguser {
1311: my ($udom) = @_;
1312: return $udom.'-domainconfig';
1313: }
1314:
1.837 raeburn 1315: sub retrieve_inst_usertypes {
1316: my ($udom) = @_;
1317: my (%returnhash,@order);
1.989 raeburn 1318: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1319: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1320: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1321: %returnhash = %{$domdefs{'inststatustypes'}};
1322: @order = @{$domdefs{'inststatusorder'}};
1323: } else {
1324: if (defined(&domain($udom,'primary'))) {
1325: my $uhome=&domain($udom,'primary');
1326: my $rep=&reply("inst_usertypes:$udom",$uhome);
1327: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1328: &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
1329: return (\%returnhash,\@order);
1330: }
1331: my ($hashitems,$orderitems) = split(/:/,$rep);
1332: my @pairs=split(/\&/,$hashitems);
1333: foreach my $item (@pairs) {
1334: my ($key,$value)=split(/=/,$item,2);
1335: $key = &unescape($key);
1336: next if ($key =~ /^error: 2 /);
1337: $returnhash{$key}=&thaw_unescape($value);
1338: }
1339: my @esc_order = split(/\&/,$orderitems);
1340: foreach my $item (@esc_order) {
1341: push(@order,&unescape($item));
1342: }
1343: } else {
1344: &logthis("get_dom failed - no primary domain server for $udom");
1.837 raeburn 1345: }
1346: }
1347: return (\%returnhash,\@order);
1348: }
1349:
1.868 raeburn 1350: sub is_domainimage {
1351: my ($url) = @_;
1352: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
1353: if (&domain($1) ne '') {
1354: return '1';
1355: }
1356: }
1357: return;
1358: }
1359:
1.899 raeburn 1360: sub inst_directory_query {
1361: my ($srch) = @_;
1362: my $udom = $srch->{'srchdomain'};
1363: my %results;
1364: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1365: my $outcome;
1.899 raeburn 1366: if ($homeserver ne '') {
1.904 albertel 1367: my $queryid=&reply("querysend:instdirsearch:".
1368: &escape($srch->{'srchby'}).':'.
1369: &escape($srch->{'srchterm'}).':'.
1370: &escape($srch->{'srchtype'}),$homeserver);
1371: my $host=&hostname($homeserver);
1372: if ($queryid !~/^\Q$host\E\_/) {
1373: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1374: return;
1375: }
1376: my $response = &get_query_reply($queryid);
1377: my $maxtries = 5;
1378: my $tries = 1;
1379: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1380: $response = &get_query_reply($queryid);
1381: $tries ++;
1382: }
1383:
1384: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 1385: if ($response eq 'unavailable') {
1386: $outcome = $response;
1387: } else {
1388: $outcome = 'ok';
1389: my @matches = split(/\n/,$response);
1390: foreach my $match (@matches) {
1391: my ($key,$value) = split(/=/,$match);
1392: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
1393: }
1.899 raeburn 1394: }
1395: }
1396: }
1.909 raeburn 1397: return ($outcome,%results);
1.899 raeburn 1398: }
1399:
1400: sub usersearch {
1401: my ($srch) = @_;
1402: my $dom = $srch->{'srchdomain'};
1403: my %results;
1404: my %libserv = &all_library();
1405: my $query = 'usersearch';
1406: foreach my $tryserver (keys(%libserv)) {
1407: if (&host_domain($tryserver) eq $dom) {
1408: my $host=&hostname($tryserver);
1409: my $queryid=
1.911 raeburn 1410: &reply("querysend:".&escape($query).':'.
1411: &escape($srch->{'srchby'}).':'.
1.899 raeburn 1412: &escape($srch->{'srchtype'}).':'.
1413: &escape($srch->{'srchterm'}),$tryserver);
1414: if ($queryid !~/^\Q$host\E\_/) {
1415: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 1416: next;
1.899 raeburn 1417: }
1418: my $reply = &get_query_reply($queryid);
1419: my $maxtries = 1;
1420: my $tries = 1;
1421: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1422: $reply = &get_query_reply($queryid);
1423: $tries ++;
1424: }
1425: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1426: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
1427: } else {
1.911 raeburn 1428: my @matches;
1429: if ($reply =~ /\n/) {
1430: @matches = split(/\n/,$reply);
1431: } else {
1432: @matches = split(/\&/,$reply);
1433: }
1.899 raeburn 1434: foreach my $match (@matches) {
1435: my ($uname,$udom,%userhash);
1.911 raeburn 1436: foreach my $entry (split(/:/,$match)) {
1437: my ($key,$value) =
1438: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 1439: $userhash{$key} = $value;
1440: if ($key eq 'username') {
1441: $uname = $value;
1442: } elsif ($key eq 'domain') {
1443: $udom = $value;
1.911 raeburn 1444: }
1.899 raeburn 1445: }
1446: $results{$uname.':'.$udom} = \%userhash;
1447: }
1448: }
1449: }
1450: }
1451: return %results;
1452: }
1453:
1.912 raeburn 1454: sub get_instuser {
1455: my ($udom,$uname,$id) = @_;
1456: my $homeserver = &domain($udom,'primary');
1457: my ($outcome,%results);
1458: if ($homeserver ne '') {
1459: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
1460: &escape($id).':'.&escape($udom),$homeserver);
1461: my $host=&hostname($homeserver);
1462: if ($queryid !~/^\Q$host\E\_/) {
1463: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1464: return;
1465: }
1466: my $response = &get_query_reply($queryid);
1467: my $maxtries = 5;
1468: my $tries = 1;
1469: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1470: $response = &get_query_reply($queryid);
1471: $tries ++;
1472: }
1473: if (!&error($response) && $response ne 'refused') {
1474: if ($response eq 'unavailable') {
1475: $outcome = $response;
1476: } else {
1477: $outcome = 'ok';
1478: my @matches = split(/\n/,$response);
1479: foreach my $match (@matches) {
1480: my ($key,$value) = split(/=/,$match);
1481: $results{&unescape($key)} = &thaw_unescape($value);
1482: }
1483: }
1484: }
1485: }
1486: my %userinfo;
1487: if (ref($results{$uname}) eq 'HASH') {
1488: %userinfo = %{$results{$uname}};
1489: }
1490: return ($outcome,%userinfo);
1491: }
1492:
1493: sub inst_rulecheck {
1.923 raeburn 1494: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 1495: my %returnhash;
1496: if ($udom ne '') {
1497: if (ref($rules) eq 'ARRAY') {
1498: @{$rules} = map {&escape($_);} (@{$rules});
1499: my $rulestr = join(':',@{$rules});
1500: my $homeserver=&domain($udom,'primary');
1501: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1502: my $response;
1503: if ($item eq 'username') {
1504: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
1505: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 1506: $homeserver));
1.923 raeburn 1507: } elsif ($item eq 'id') {
1508: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
1509: ':'.&escape($id).':'.$rulestr,
1510: $homeserver));
1.945 raeburn 1511: } elsif ($item eq 'selfcreate') {
1512: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 1513: &escape($udom).':'.&escape($uname).
1514: ':'.$rulestr,$homeserver));
1.923 raeburn 1515: }
1.912 raeburn 1516: if ($response ne 'refused') {
1517: my @pairs=split(/\&/,$response);
1518: foreach my $item (@pairs) {
1519: my ($key,$value)=split(/=/,$item,2);
1520: $key = &unescape($key);
1521: next if ($key =~ /^error: 2 /);
1522: $returnhash{$key}=&thaw_unescape($value);
1523: }
1524: }
1525: }
1526: }
1527: }
1528: return %returnhash;
1529: }
1530:
1531: sub inst_userrules {
1.923 raeburn 1532: my ($udom,$check) = @_;
1.912 raeburn 1533: my (%ruleshash,@ruleorder);
1534: if ($udom ne '') {
1535: my $homeserver=&domain($udom,'primary');
1536: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1537: my $response;
1538: if ($check eq 'id') {
1539: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 1540: $homeserver);
1.943 raeburn 1541: } elsif ($check eq 'email') {
1542: $response=&reply('instemailrules:'.&escape($udom),
1543: $homeserver);
1.923 raeburn 1544: } else {
1545: $response=&reply('instuserrules:'.&escape($udom),
1546: $homeserver);
1547: }
1.912 raeburn 1548: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 1549: ($response ne 'unknown_cmd') &&
1.912 raeburn 1550: ($response ne 'no_such_host')) {
1551: my ($hashitems,$orderitems) = split(/:/,$response);
1552: my @pairs=split(/\&/,$hashitems);
1553: foreach my $item (@pairs) {
1554: my ($key,$value)=split(/=/,$item,2);
1555: $key = &unescape($key);
1556: next if ($key =~ /^error: 2 /);
1557: $ruleshash{$key}=&thaw_unescape($value);
1558: }
1559: my @esc_order = split(/\&/,$orderitems);
1560: foreach my $item (@esc_order) {
1561: push(@ruleorder,&unescape($item));
1562: }
1563: }
1564: }
1565: }
1566: return (\%ruleshash,\@ruleorder);
1567: }
1568:
1.976 raeburn 1569: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 1570:
1571: sub get_domain_defaults {
1572: my ($domain) = @_;
1573: my $cachetime = 60*60*24;
1574: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
1575: if (defined($cached)) {
1576: if (ref($result) eq 'HASH') {
1577: return %{$result};
1578: }
1579: }
1580: my %domdefaults;
1581: my %domconfig =
1.989 raeburn 1582: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 1583: 'requestcourses','inststatus',
1.1056.4.3 raeburn 1584: 'coursedefaults','usersessions'],$domain);
1.943 raeburn 1585: if (ref($domconfig{'defaults'}) eq 'HASH') {
1586: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
1587: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
1588: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 1589: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 1590: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1056.4.34 raeburn 1591: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943 raeburn 1592: } else {
1593: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
1594: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
1595: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
1596: }
1.976 raeburn 1597: if (ref($domconfig{'quotas'}) eq 'HASH') {
1598: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
1599: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
1600: } else {
1601: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1602: }
1603: my @usertools = ('aboutme','blog','portfolio');
1604: foreach my $item (@usertools) {
1605: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
1606: $domdefaults{$item} = $domconfig{'quotas'}{$item};
1607: }
1608: }
1609: }
1.985 raeburn 1610: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006 raeburn 1611: foreach my $item ('official','unofficial','community') {
1.985 raeburn 1612: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
1613: }
1614: }
1.989 raeburn 1615: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1616: foreach my $item ('inststatustypes','inststatusorder') {
1617: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
1618: }
1619: }
1.1047 raeburn 1620: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1621: foreach my $item ('canuse_pdfforms') {
1622: $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
1623: }
1624: }
1.1056.4.3 raeburn 1625: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1626: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
1627: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
1628: }
1629: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
1630: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
1631: }
1632: }
1.943 raeburn 1633: &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
1634: $cachetime);
1635: return %domdefaults;
1636: }
1637:
1.344 www 1638: # --------------------------------------------------- Assign a key to a student
1639:
1640: sub assign_access_key {
1.364 www 1641: #
1642: # a valid key looks like uname:udom#comments
1643: # comments are being appended
1644: #
1.498 www 1645: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
1646: $kdom=
1.620 albertel 1647: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 1648: $knum=
1.620 albertel 1649: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 1650: $cdom=
1.620 albertel 1651: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1652: $cnum=
1.620 albertel 1653: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1654: $udom=$env{'user.name'} unless (defined($udom));
1655: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 1656: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 1657: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 1658: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 1659: # assigned to this person
1660: # - this should not happen,
1.345 www 1661: # unless something went wrong
1662: # the first time around
1663: # ready to assign
1.364 www 1664: $logentry=$1.'; '.$logentry;
1.496 www 1665: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 1666: $kdom,$knum) eq 'ok') {
1.345 www 1667: # key now belongs to user
1.346 www 1668: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 1669: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 1670: &appenv({'environment.'.$envkey => $ckey});
1.345 www 1671: return 'ok';
1672: } else {
1673: return
1674: 'error: Count not permanently assign key, will need to be re-entered later.';
1675: }
1676: } else {
1677: return 'error: Could not assign key, try again later.';
1678: }
1.364 www 1679: } elsif (!$existing{$ckey}) {
1.345 www 1680: # the key does not exist
1681: return 'error: The key does not exist';
1682: } else {
1683: # the key is somebody else's
1684: return 'error: The key is already in use';
1685: }
1.344 www 1686: }
1687:
1.364 www 1688: # ------------------------------------------ put an additional comment on a key
1689:
1690: sub comment_access_key {
1691: #
1692: # a valid key looks like uname:udom#comments
1693: # comments are being appended
1694: #
1695: my ($ckey,$cdom,$cnum,$logentry)=@_;
1696: $cdom=
1.620 albertel 1697: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 1698: $cnum=
1.620 albertel 1699: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 1700: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1701: if ($existing{$ckey}) {
1702: $existing{$ckey}.='; '.$logentry;
1703: # ready to assign
1.367 www 1704: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 1705: $cdom,$cnum) eq 'ok') {
1706: return 'ok';
1707: } else {
1708: return 'error: Count not store comment.';
1709: }
1710: } else {
1711: # the key does not exist
1712: return 'error: The key does not exist';
1713: }
1714: }
1715:
1.344 www 1716: # ------------------------------------------------------ Generate a set of keys
1717:
1718: sub generate_access_keys {
1.364 www 1719: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 1720: $cdom=
1.620 albertel 1721: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1722: $cnum=
1.620 albertel 1723: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 1724: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 1725: unless (($cdom) && ($cnum)) { return 0; }
1726: if ($number>10000) { return 0; }
1727: sleep(2); # make sure don't get same seed twice
1728: srand(time()^($$+($$<<15))); # from "Programming Perl"
1729: my $total=0;
1730: for (my $i=1;$i<=$number;$i++) {
1731: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
1732: sprintf("%lx",int(100000*rand)).'-'.
1733: sprintf("%lx",int(100000*rand));
1734: $newkey=~s/1/g/g; # folks mix up 1 and l
1735: $newkey=~s/0/h/g; # and also 0 and O
1736: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
1737: if ($existing{$newkey}) {
1738: $i--;
1739: } else {
1.364 www 1740: if (&put('accesskeys',
1741: { $newkey => '# generated '.localtime().
1.620 albertel 1742: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 1743: '; '.$logentry },
1744: $cdom,$cnum) eq 'ok') {
1.344 www 1745: $total++;
1746: }
1747: }
1748: }
1.620 albertel 1749: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 1750: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
1751: return $total;
1752: }
1753:
1754: # ------------------------------------------------------- Validate an accesskey
1755:
1756: sub validate_access_key {
1757: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
1758: $cdom=
1.620 albertel 1759: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1760: $cnum=
1.620 albertel 1761: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1762: $udom=$env{'user.domain'} unless (defined($udom));
1763: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 1764: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 1765: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 1766: }
1767:
1768: # ------------------------------------- Find the section of student in a course
1.652 albertel 1769: sub devalidate_getsection_cache {
1770: my ($udom,$unam,$courseid)=@_;
1771: my $hashid="$udom:$unam:$courseid";
1772: &devalidate_cache_new('getsection',$hashid);
1773: }
1.298 matthew 1774:
1.815 albertel 1775: sub courseid_to_courseurl {
1776: my ($courseid) = @_;
1777: #already url style courseid
1778: return $courseid if ($courseid =~ m{^/});
1779:
1780: if (exists($env{'course.'.$courseid.'.num'})) {
1781: my $cnum = $env{'course.'.$courseid.'.num'};
1782: my $cdom = $env{'course.'.$courseid.'.domain'};
1783: return "/$cdom/$cnum";
1784: }
1785:
1786: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
1787: if (exists($courseinfo{'num'})) {
1788: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
1789: }
1790:
1791: return undef;
1792: }
1793:
1.298 matthew 1794: sub getsection {
1795: my ($udom,$unam,$courseid)=@_;
1.599 albertel 1796: my $cachetime=1800;
1.551 albertel 1797:
1798: my $hashid="$udom:$unam:$courseid";
1.599 albertel 1799: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 1800: if (defined($cached)) { return $result; }
1801:
1.298 matthew 1802: my %Pending;
1803: my %Expired;
1804: #
1805: # Each role can either have not started yet (pending), be active,
1806: # or have expired.
1807: #
1808: # If there is an active role, we are done.
1809: #
1810: # If there is more than one role which has not started yet,
1811: # choose the one which will start sooner
1812: # If there is one role which has not started yet, return it.
1813: #
1814: # If there is more than one expired role, choose the one which ended last.
1815: # If there is a role which has expired, return it.
1816: #
1.815 albertel 1817: $courseid = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 1818: my $extra = &freeze_escape({'skipcheck' => 1});
1819: my %roleshash = &dump('roles',$udom,$unam,$courseid,undef,$extra);
1.817 raeburn 1820: foreach my $key (keys(%roleshash)) {
1.479 albertel 1821: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1822: my $section=$1;
1823: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1824: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1825: my $now=time;
1.548 albertel 1826: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1827: $Expired{$end}=$section;
1828: next;
1829: }
1.548 albertel 1830: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1831: $Pending{$start}=$section;
1832: next;
1833: }
1.599 albertel 1834: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1835: }
1836: #
1837: # Presumedly there will be few matching roles from the above
1838: # loop and the sorting time will be negligible.
1839: if (scalar(keys(%Pending))) {
1840: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1841: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1842: }
1843: if (scalar(keys(%Expired))) {
1844: my @sorted = sort {$a <=> $b} keys(%Expired);
1845: my $time = pop(@sorted);
1.599 albertel 1846: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1847: }
1.599 albertel 1848: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1849: }
1.70 www 1850:
1.599 albertel 1851: sub save_cache {
1852: &purge_remembered();
1.722 albertel 1853: #&Apache::loncommon::validate_page();
1.620 albertel 1854: undef(%env);
1.780 albertel 1855: undef($env_loaded);
1.599 albertel 1856: }
1.452 albertel 1857:
1.599 albertel 1858: my $to_remember=-1;
1859: my %remembered;
1860: my %accessed;
1861: my $kicks=0;
1862: my $hits=0;
1.849 albertel 1863: sub make_key {
1864: my ($name,$id) = @_;
1.872 albertel 1865: if (length($id) > 65
1866: && length(&escape($id)) > 200) {
1867: $id=length($id).':'.&Digest::MD5::md5_hex($id);
1868: }
1.849 albertel 1869: return &escape($name.':'.$id);
1870: }
1871:
1.599 albertel 1872: sub devalidate_cache_new {
1873: my ($name,$id,$debug) = @_;
1874: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1875: $id=&make_key($name,$id);
1.599 albertel 1876: $memcache->delete($id);
1877: delete($remembered{$id});
1878: delete($accessed{$id});
1879: }
1880:
1881: sub is_cached_new {
1882: my ($name,$id,$debug) = @_;
1.849 albertel 1883: $id=&make_key($name,$id);
1.599 albertel 1884: if (exists($remembered{$id})) {
1885: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1886: $accessed{$id}=[&gettimeofday()];
1887: $hits++;
1888: return ($remembered{$id},1);
1889: }
1890: my $value = $memcache->get($id);
1891: if (!(defined($value))) {
1892: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1893: return (undef,undef);
1.416 albertel 1894: }
1.599 albertel 1895: if ($value eq '__undef__') {
1896: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1897: $value=undef;
1898: }
1899: &make_room($id,$value,$debug);
1900: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1901: return ($value,1);
1902: }
1903:
1904: sub do_cache_new {
1905: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1906: $id=&make_key($name,$id);
1.599 albertel 1907: my $setvalue=$value;
1908: if (!defined($setvalue)) {
1909: $setvalue='__undef__';
1910: }
1.623 albertel 1911: if (!defined($time) ) {
1912: $time=600;
1913: }
1.599 albertel 1914: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 1915: my $result = $memcache->set($id,$setvalue,$time);
1916: if (! $result) {
1.872 albertel 1917: &logthis("caching of id -> $id failed");
1.910 albertel 1918: $memcache->disconnect_all();
1.872 albertel 1919: }
1.600 albertel 1920: # need to make a copy of $value
1.919 albertel 1921: &make_room($id,$value,$debug);
1.599 albertel 1922: return $value;
1923: }
1924:
1925: sub make_room {
1926: my ($id,$value,$debug)=@_;
1.919 albertel 1927:
1928: $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
1929: : $value;
1.599 albertel 1930: if ($to_remember<0) { return; }
1931: $accessed{$id}=[&gettimeofday()];
1932: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1933: my $to_kick;
1934: my $max_time=0;
1935: foreach my $other (keys(%accessed)) {
1936: if (&tv_interval($accessed{$other}) > $max_time) {
1937: $to_kick=$other;
1938: $max_time=&tv_interval($accessed{$other});
1939: }
1940: }
1941: delete($remembered{$to_kick});
1942: delete($accessed{$to_kick});
1943: $kicks++;
1944: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1945: return;
1946: }
1947:
1.599 albertel 1948: sub purge_remembered {
1.604 albertel 1949: #&logthis("Tossing ".scalar(keys(%remembered)));
1950: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1951: undef(%remembered);
1952: undef(%accessed);
1.428 albertel 1953: }
1.70 www 1954: # ------------------------------------- Read an entry from a user's environment
1955:
1956: sub userenvironment {
1957: my ($udom,$unam,@what)=@_;
1.976 raeburn 1958: my $items;
1959: foreach my $item (@what) {
1960: $items.=&escape($item).'&';
1961: }
1962: $items=~s/\&$//;
1.70 www 1963: my %returnhash=();
1.1009 raeburn 1964: my $uhome = &homeserver($unam,$udom);
1965: unless ($uhome eq 'no_host') {
1966: my @answer=split(/\&/,
1967: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 1968: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
1969: return %returnhash;
1970: }
1.1009 raeburn 1971: my $i;
1972: for ($i=0;$i<=$#what;$i++) {
1973: $returnhash{$what[$i]}=&unescape($answer[$i]);
1974: }
1.70 www 1975: }
1976: return %returnhash;
1.1 albertel 1977: }
1978:
1.617 albertel 1979: # ---------------------------------------------------------- Get a studentphoto
1980: sub studentphoto {
1981: my ($udom,$unam,$ext) = @_;
1982: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 1983: if (defined($env{'request.course.id'})) {
1.708 raeburn 1984: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 1985: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1986: return(&retrievestudentphoto($udom,$unam,$ext));
1987: } else {
1988: my ($result,$perm_reqd)=
1.707 albertel 1989: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1990: if ($result eq 'ok') {
1991: if (!($perm_reqd eq 'yes')) {
1992: return(&retrievestudentphoto($udom,$unam,$ext));
1993: }
1994: }
1995: }
1996: }
1997: } else {
1998: my ($result,$perm_reqd) =
1.707 albertel 1999: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2000: if ($result eq 'ok') {
2001: if (!($perm_reqd eq 'yes')) {
2002: return(&retrievestudentphoto($udom,$unam,$ext));
2003: }
2004: }
2005: }
2006: return '/adm/lonKaputt/lonlogo_broken.gif';
2007: }
2008:
2009: sub retrievestudentphoto {
2010: my ($udom,$unam,$ext,$type) = @_;
2011: my $home=&Apache::lonnet::homeserver($unam,$udom);
2012: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2013: if ($ret eq 'ok') {
2014: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2015: if ($type eq 'thumbnail') {
2016: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2017: }
2018: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2019: return $tokenurl;
2020: } else {
2021: if ($type eq 'thumbnail') {
2022: return '/adm/lonKaputt/genericstudent_tn.gif';
2023: } else {
2024: return '/adm/lonKaputt/lonlogo_broken.gif';
2025: }
1.617 albertel 2026: }
2027: }
2028:
1.263 www 2029: # -------------------------------------------------------------------- New chat
2030:
2031: sub chatsend {
1.724 raeburn 2032: my ($newentry,$anon,$group)=@_;
1.620 albertel 2033: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2034: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2035: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 2036: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 2037: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 2038: &escape($newentry)).':'.$group,$chome);
1.292 www 2039: }
2040:
2041: # ------------------------------------------ Find current version of a resource
2042:
2043: sub getversion {
2044: my $fname=&clutter(shift);
2045: unless ($fname=~/^\/res\//) { return -1; }
2046: return ¤tversion(&filelocation('',$fname));
2047: }
2048:
2049: sub currentversion {
2050: my $fname=shift;
1.599 albertel 2051: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 2052: if (defined($cached)) { return $result; }
1.292 www 2053: my $author=$fname;
2054: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2055: my ($udom,$uname)=split(/\//,$author);
2056: my $home=homeserver($uname,$udom);
2057: if ($home eq 'no_host') {
2058: return -1;
2059: }
2060: my $answer=reply("currentversion:$fname",$home);
2061: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2062: return -1;
2063: }
1.599 albertel 2064: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 2065: }
2066:
1.1 albertel 2067: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 2068:
1.1 albertel 2069: sub subscribe {
2070: my $fname=shift;
1.761 raeburn 2071: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 2072: $fname=~s/[\n\r]//g;
1.1 albertel 2073: my $author=$fname;
2074: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2075: my ($udom,$uname)=split(/\//,$author);
2076: my $home=homeserver($uname,$udom);
1.335 albertel 2077: if ($home eq 'no_host') {
2078: return 'not_found';
1.1 albertel 2079: }
2080: my $answer=reply("sub:$fname",$home);
1.64 www 2081: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2082: $answer.=' by '.$home;
2083: }
1.1 albertel 2084: return $answer;
2085: }
2086:
1.8 www 2087: # -------------------------------------------------------------- Replicate file
2088:
2089: sub repcopy {
2090: my $filename=shift;
1.23 www 2091: $filename=~s/\/+/\//g;
1.607 raeburn 2092: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
2093: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 2094: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 2095: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 2096: return &repcopy_userfile($filename);
2097: }
1.532 albertel 2098: $filename=~s/[\n\r]//g;
1.8 www 2099: my $transname="$filename.in.transfer";
1.828 www 2100: # FIXME: this should flock
1.607 raeburn 2101: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 2102: my $remoteurl=subscribe($filename);
1.64 www 2103: if ($remoteurl =~ /^con_lost by/) {
2104: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2105: return 'unavailable';
1.8 www 2106: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 2107: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 2108: return 'not_found';
1.64 www 2109: } elsif ($remoteurl =~ /^rejected by/) {
2110: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2111: return 'forbidden';
1.20 www 2112: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 2113: return 'ok';
1.8 www 2114: } else {
1.290 www 2115: my $author=$filename;
2116: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2117: my ($udom,$uname)=split(/\//,$author);
2118: my $home=homeserver($uname,$udom);
2119: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 2120: my @parts=split(/\//,$filename);
2121: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
2122: if ($path ne "$perlvar{'lonDocRoot'}/res") {
2123: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 2124: return 'bad_request';
1.8 www 2125: }
2126: my $count;
2127: for ($count=5;$count<$#parts;$count++) {
2128: $path.="/$parts[$count]";
2129: if ((-e $path)!=1) {
2130: mkdir($path,0777);
2131: }
2132: }
2133: my $ua=new LWP::UserAgent;
2134: my $request=new HTTP::Request('GET',"$remoteurl");
2135: my $response=$ua->request($request,$transname);
2136: if ($response->is_error()) {
2137: unlink($transname);
2138: my $message=$response->status_line;
1.672 albertel 2139: &logthis("<font color=\"blue\">WARNING:"
1.12 www 2140: ." LWP get: $message: $filename</font>");
1.607 raeburn 2141: return 'unavailable';
1.8 www 2142: } else {
1.16 www 2143: if ($remoteurl!~/\.meta$/) {
2144: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2145: my $mresponse=$ua->request($mrequest,$filename.'.meta');
2146: if ($mresponse->is_error()) {
2147: unlink($filename.'.meta');
2148: &logthis(
1.672 albertel 2149: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 2150: }
2151: }
1.8 www 2152: rename($transname,$filename);
1.607 raeburn 2153: return 'ok';
1.8 www 2154: }
1.290 www 2155: }
1.8 www 2156: }
1.330 www 2157: }
2158:
2159: # ------------------------------------------------ Get server side include body
2160: sub ssi_body {
1.381 albertel 2161: my ($filelink,%form)=@_;
1.606 matthew 2162: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
2163: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
2164: }
1.953 www 2165: my $output='';
2166: my $response;
1.980 raeburn 2167: if ($filelink=~/^https?\:/) {
1.954 raeburn 2168: ($output,$response)=&externalssi($filelink);
1.953 www 2169: } else {
1.1004 droeschl 2170: $filelink .= $filelink=~/\?/ ? '&' : '?';
2171: $filelink .= 'inhibitmenu=yes';
1.953 www 2172: ($output,$response)=&ssi($filelink,%form);
2173: }
1.778 albertel 2174: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2175: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2176: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2177: if (wantarray) {
2178: return ($output, $response);
2179: } else {
2180: return $output;
2181: }
1.8 www 2182: }
2183:
1.15 www 2184: # --------------------------------------------------------- Server Side Include
2185:
1.782 albertel 2186: sub absolute_url {
2187: my ($host_name) = @_;
2188: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
2189: if ($host_name eq '') {
2190: $host_name = $ENV{'SERVER_NAME'};
2191: }
2192: return $protocol.$host_name;
2193: }
2194:
1.942 foxr 2195: #
2196: # Server side include.
2197: # Parameters:
2198: # fn Possibly encrypted resource name/id.
2199: # form Hash that describes how the rendering should be done
2200: # and other things.
1.944 foxr 2201: # Returns:
1.950 raeburn 2202: # Scalar context: The content of the response.
2203: # Array context: 2 element list of the content and the full response object.
1.942 foxr 2204: #
1.15 www 2205: sub ssi {
2206:
1.944 foxr 2207: my ($fn,%form)=@_;
1.15 www 2208: my $ua=new LWP::UserAgent;
1.23 www 2209: my $request;
1.711 albertel 2210:
2211: $form{'no_update_last_known'}=1;
1.895 albertel 2212: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 2213: if (%form) {
1.782 albertel 2214: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000 raeburn 2215: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23 www 2216: } else {
1.782 albertel 2217: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 2218: }
2219:
1.15 www 2220: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
2221: my $response=$ua->request($request);
2222:
1.944 foxr 2223: if (wantarray) {
2224: return ($response->content, $response);
2225: } else {
2226: return $response->content;
1.942 foxr 2227: }
1.324 www 2228: }
2229:
2230: sub externalssi {
2231: my ($url)=@_;
2232: my $ua=new LWP::UserAgent;
2233: my $request=new HTTP::Request('GET',$url);
2234: my $response=$ua->request($request);
1.954 raeburn 2235: if (wantarray) {
2236: return ($response->content, $response);
2237: } else {
2238: return $response->content;
2239: }
1.15 www 2240: }
1.254 www 2241:
1.492 albertel 2242: # -------------------------------- Allow a /uploaded/ URI to be vouched for
2243:
2244: sub allowuploaded {
2245: my ($srcurl,$url)=@_;
2246: $url=&clutter(&declutter($url));
2247: my $dir=$url;
2248: $dir=~s/\/[^\/]+$//;
2249: my %httpref=();
2250: my $httpurl=&hreflocation('',$url);
2251: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 2252: &Apache::lonnet::appenv(\%httpref);
1.254 www 2253: }
1.477 raeburn 2254:
1.478 albertel 2255: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 2256: # input: action, courseID, current domain, intended
1.637 raeburn 2257: # path to file, source of file, instruction to parse file for objects,
2258: # ref to hash for embedded objects,
2259: # ref to hash for codebase of java objects.
1.1056.4.16 raeburn 2260: # reference to scalar to accommodate mime type determined
2261: # from File::MMagic if $parser = parse.
1.637 raeburn 2262: #
1.485 raeburn 2263: # output: url to file (if action was uploaddoc),
2264: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 2265: #
1.478 albertel 2266: # Allows directory structure to be used within lonUsers/../userfiles/ for a
2267: # course.
1.477 raeburn 2268: #
1.478 albertel 2269: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2270: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
2271: # course's home server.
1.477 raeburn 2272: #
1.478 albertel 2273: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
2274: # be copied from $source (current location) to
2275: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2276: # and will then be copied to
2277: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
2278: # course's home server.
1.485 raeburn 2279: #
1.481 raeburn 2280: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 2281: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 2282: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2283: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
2284: # in course's home server.
1.637 raeburn 2285: #
1.477 raeburn 2286:
2287: sub process_coursefile {
1.1056.4.16 raeburn 2288: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
2289: $mimetype)=@_;
1.477 raeburn 2290: my $fetchresult;
1.638 albertel 2291: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 2292: if ($action eq 'propagate') {
1.638 albertel 2293: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
2294: $home);
1.481 raeburn 2295: } else {
1.477 raeburn 2296: my $fpath = '';
2297: my $fname = $file;
1.478 albertel 2298: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 2299: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 2300: my $filepath = &build_filepath($fpath);
1.481 raeburn 2301: if ($action eq 'copy') {
2302: if ($source eq '') {
2303: $fetchresult = 'no source file';
2304: return $fetchresult;
2305: } else {
2306: my $destination = $filepath.'/'.$fname;
2307: rename($source,$destination);
2308: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2309: $home);
1.481 raeburn 2310: }
2311: } elsif ($action eq 'uploaddoc') {
2312: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 2313: print $fh $env{'form.'.$source};
1.481 raeburn 2314: close($fh);
1.637 raeburn 2315: if ($parser eq 'parse') {
1.1024 raeburn 2316: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2317: my $type = $mm->checktype_filename($filepath.'/'.$fname);
2318: if ($type eq 'text/html') {
1.1024 raeburn 2319: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
2320: unless ($parse_result eq 'ok') {
2321: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
2322: }
1.637 raeburn 2323: }
1.1056.4.16 raeburn 2324: if (ref($mimetype)) {
2325: $$mimetype = $type;
2326: }
1.637 raeburn 2327: }
1.477 raeburn 2328: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2329: $home);
1.481 raeburn 2330: if ($fetchresult eq 'ok') {
2331: return '/uploaded/'.$fpath.'/'.$fname;
2332: } else {
2333: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2334: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 2335: return '/adm/notfound.html';
2336: }
1.477 raeburn 2337: }
2338: }
1.485 raeburn 2339: unless ( $fetchresult eq 'ok') {
1.477 raeburn 2340: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2341: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 2342: }
2343: return $fetchresult;
2344: }
2345:
1.637 raeburn 2346: sub build_filepath {
2347: my ($fpath) = @_;
2348: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
2349: unless ($fpath eq '') {
2350: my @parts=split('/',$fpath);
2351: foreach my $part (@parts) {
2352: $filepath.= '/'.$part;
2353: if ((-e $filepath)!=1) {
2354: mkdir($filepath,0777);
2355: }
2356: }
2357: }
2358: return $filepath;
2359: }
2360:
2361: sub store_edited_file {
1.638 albertel 2362: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 2363: my $file = $primary_url;
2364: $file =~ s#^/uploaded/$docudom/$docuname/##;
2365: my $fpath = '';
2366: my $fname = $file;
2367: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
2368: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
2369: my $filepath = &build_filepath($fpath);
2370: open(my $fh,'>'.$filepath.'/'.$fname);
2371: print $fh $content;
2372: close($fh);
1.638 albertel 2373: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 2374: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2375: $home);
1.637 raeburn 2376: if ($$fetchresult eq 'ok') {
2377: return '/uploaded/'.$fpath.'/'.$fname;
2378: } else {
1.638 albertel 2379: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
2380: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 2381: return '/adm/notfound.html';
2382: }
2383: }
2384:
1.531 albertel 2385: sub clean_filename {
1.831 albertel 2386: my ($fname,$args)=@_;
1.315 www 2387: # Replace Windows backslashes by forward slashes
1.257 www 2388: $fname=~s/\\/\//g;
1.831 albertel 2389: if (!$args->{'keep_path'}) {
2390: # Get rid of everything but the actual filename
2391: $fname=~s/^.*\/([^\/]+)$/$1/;
2392: }
1.315 www 2393: # Replace spaces by underscores
2394: $fname=~s/\s+/\_/g;
2395: # Replace all other weird characters by nothing
1.831 albertel 2396: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 2397: # Replace all .\d. sequences with _\d. so they no longer look like version
2398: # numbers
2399: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 2400: return $fname;
2401: }
1.1051 raeburn 2402: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
2403: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
2404: # image with the same aspect ratio as the original, but with dimensions which do
2405: # not exceed $resizewidth and $resizeheight.
2406:
1.984 neumanie 2407: sub resizeImage {
1.1051 raeburn 2408: my ($img_path,$resizewidth,$resizeheight) = @_;
2409: my $ima = Image::Magick->new;
2410: my $resized;
2411: if (-e $img_path) {
2412: $ima->Read($img_path);
2413: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
2414: my $width = $ima->Get('width');
2415: my $height = $ima->Get('height');
2416: if ($width > $resizewidth) {
2417: my $factor = $width/$resizewidth;
2418: my $newheight = $height/$factor;
2419: $ima->Scale(width=>$resizewidth,height=>$newheight);
2420: $resized = 1;
2421: }
2422: }
2423: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
2424: my $width = $ima->Get('width');
2425: my $height = $ima->Get('height');
2426: if ($height > $resizeheight) {
2427: my $factor = $height/$resizeheight;
2428: my $newwidth = $width/$factor;
2429: $ima->Scale(width=>$newwidth,height=>$resizeheight);
2430: $resized = 1;
2431: }
2432: }
2433: if ($resized) {
2434: $ima->Write($img_path);
2435: }
2436: }
2437: return;
1.977 amueller 2438: }
2439:
1.608 albertel 2440: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 2441: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1056.4.13 raeburn 2442: # the desired filename is in $env{"form.$formname.filename"}
2443: # $context - possible values: coursedoc, existingfile, overwrite,
2444: # canceloverwrite, or ''.
2445: # if 'coursedoc': upload to the current course
2446: # if 'existingfile': write file to tmp/overwrites directory
2447: # if 'canceloverwrite': delete file written to tmp/overwrites directory
2448: # $context is passed as argument to &finishuserfileupload
1.686 albertel 2449: # $subdir - directory in userfile to store the file into
1.858 raeburn 2450: # $parser - instruction to parse file for objects ($parser = parse)
2451: # $allfiles - reference to hash for embedded objects
2452: # $codebase - reference to hash for codebase of java objects
2453: # $desuname - username for permanent storage of uploaded file
2454: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 2455: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
2456: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 2457: # $resizewidth - width (pixels) to which to resize uploaded image
2458: # $resizeheight - height (pixels) to which to resize uploaded image
1.1056.4.16 raeburn 2459: # $mimetype - reference to scalar to accommodate mime type determined
2460: # from File::MMagic if $parser = parse.
1.858 raeburn 2461: #
1.686 albertel 2462: # output: url of file in userspace, or error: <message>
2463: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 2464:
1.531 albertel 2465: sub userfileupload {
1.1056.4.13 raeburn 2466: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1056.4.16 raeburn 2467: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 2468: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 2469: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 2470: $fname=&clean_filename($fname);
1.1056.4.13 raeburn 2471: # See if there is anything left
1.257 www 2472: unless ($fname) { return 'error: no uploaded file'; }
1.1056.4.13 raeburn 2473: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
2474: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
2475: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
2476: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 2477: my $now = time;
1.1056.4.13 raeburn 2478: my $filepath;
2479: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
2480: $filepath = 'tmp/helprequests/'.$now;
2481: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
2482: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
2483: '_'.$env{'user.domain'}.'/pending';
2484: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
2485: my ($docuname,$docudom);
2486: if ($destudom) {
2487: $docudom = $destudom;
2488: } else {
2489: $docudom = $env{'user.domain'};
2490: }
2491: if ($destuname) {
2492: $docuname = $destuname;
2493: } else {
2494: $docuname = $env{'user.name'};
2495: }
2496: if (exists($env{'form.group'})) {
2497: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2498: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2499: }
2500: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
2501: if ($context eq 'canceloverwrite') {
2502: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
2503: if (-e $tempfile) {
2504: my @info = stat($tempfile);
2505: if ($info[9] eq $env{'form.timestamp'}) {
2506: unlink($tempfile);
2507: }
2508: }
2509: return;
1.523 raeburn 2510: }
2511: }
1.1056.4.13 raeburn 2512: # Create the directory if not present
1.741 raeburn 2513: my @parts=split(/\//,$filepath);
2514: my $fullpath = $perlvar{'lonDaemons'};
2515: for (my $i=0;$i<@parts;$i++) {
2516: $fullpath .= '/'.$parts[$i];
2517: if ((-e $fullpath)!=1) {
2518: mkdir($fullpath,0777);
2519: }
2520: }
2521: open(my $fh,'>'.$fullpath.'/'.$fname);
2522: print $fh $env{'form.'.$formname};
2523: close($fh);
1.1056.4.13 raeburn 2524: if ($context eq 'existingfile') {
2525: my @info = stat($fullpath.'/'.$fname);
2526: return ($fullpath.'/'.$fname,$info[9]);
2527: } else {
2528: return $fullpath.'/'.$fname;
2529: }
1.523 raeburn 2530: }
1.995 raeburn 2531: if ($subdir eq 'scantron') {
2532: $fname = 'scantron_orig_'.$fname;
1.1056.4.14 raeburn 2533: } else {
1.995 raeburn 2534: $fname="$subdir/$fname";
2535: }
1.1056.4.13 raeburn 2536: if ($context eq 'coursedoc') {
1.638 albertel 2537: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2538: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 2539: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 2540: return &finishuserfileupload($docuname,$docudom,
2541: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 2542: $codebase,$thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2543: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 2544: } else {
1.620 albertel 2545: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 2546: return &process_coursefile('uploaddoc',$docuname,$docudom,
2547: $fname,$formname,$parser,
1.1056.4.16 raeburn 2548: $allfiles,$codebase,$mimetype);
1.481 raeburn 2549: }
1.719 banghart 2550: } elsif (defined($destuname)) {
2551: my $docuname=$destuname;
2552: my $docudom=$destudom;
1.860 raeburn 2553: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2554: $parser,$allfiles,$codebase,
1.1051 raeburn 2555: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2556: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2557: } else {
1.638 albertel 2558: my $docuname=$env{'user.name'};
2559: my $docudom=$env{'user.domain'};
1.714 raeburn 2560: if (exists($env{'form.group'})) {
2561: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2562: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2563: }
1.860 raeburn 2564: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2565: $parser,$allfiles,$codebase,
1.1051 raeburn 2566: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2567: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2568: }
1.271 www 2569: }
2570:
2571: sub finishuserfileupload {
1.860 raeburn 2572: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1056.4.16 raeburn 2573: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 2574: my $path=$docudom.'/'.$docuname.'/';
1.258 www 2575: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 2576:
1.860 raeburn 2577: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 2578: $file=$fname;
2579: if ($fname=~m|/|) {
2580: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
2581: $path.=$fnamepath.'/';
2582: }
1.259 www 2583: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 2584: my $count;
2585: for ($count=4;$count<=$#parts;$count++) {
2586: $filepath.="/$parts[$count]";
2587: if ((-e $filepath)!=1) {
2588: mkdir($filepath,0777);
2589: }
2590: }
1.984 neumanie 2591:
1.258 www 2592: # Save the file
2593: {
1.701 albertel 2594: if (!open(FH,'>'.$filepath.'/'.$file)) {
2595: &logthis('Failed to create '.$filepath.'/'.$file);
2596: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
2597: return '/adm/notfound.html';
2598: }
1.1056.4.13 raeburn 2599: if ($context eq 'overwrite') {
2600: my $source = $perlvar{'lonDaemons'}.'/tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
2601: my $target = $filepath.'/'.$file;
2602: if (-e $source) {
2603: my @info = stat($source);
2604: if ($info[9] eq $env{'form.timestamp'}) {
2605: unless (&File::Copy::move($source,$target)) {
2606: &logthis('Failed to overwrite '.$filepath.'/'.$file);
2607: return "Moving from $source failed";
2608: }
2609: } else {
2610: return "Temporary file: $source had unexpected date/time for last modification";
2611: }
2612: } else {
2613: return "Temporary file: $source missing";
2614: }
2615: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 2616: &logthis('Failed to write to '.$filepath.'/'.$file);
2617: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
2618: return '/adm/notfound.html';
2619: }
1.570 albertel 2620: close(FH);
1.1051 raeburn 2621: if ($resizewidth && $resizeheight) {
2622: my $mm = new File::MMagic;
2623: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
2624: if ($mime_type =~ m{^image/}) {
2625: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
2626: }
1.977 amueller 2627: }
1.258 www 2628: }
1.637 raeburn 2629: if ($parser eq 'parse') {
1.1024 raeburn 2630: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2631: my $type = $mm->checktype_filename($filepath.'/'.$file);
2632: if ($type eq 'text/html') {
1.1024 raeburn 2633: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
2634: $allfiles,$codebase);
2635: unless ($parse_result eq 'ok') {
2636: &logthis('Failed to parse '.$filepath.$file.
2637: ' for embedded media: '.$parse_result);
2638: }
1.637 raeburn 2639: }
1.1056.4.16 raeburn 2640: if (ref($mimetype)) {
2641: $$mimetype = $type;
2642: }
1.637 raeburn 2643: }
1.860 raeburn 2644: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
2645: my $input = $filepath.'/'.$file;
2646: my $output = $filepath.'/'.'tn-'.$file;
2647: my $thumbsize = $thumbwidth.'x'.$thumbheight;
2648: system("convert -sample $thumbsize $input $output");
2649: if (-e $filepath.'/'.'tn-'.$file) {
2650: $fetchthumb = 1;
2651: }
2652: }
1.858 raeburn 2653:
1.259 www 2654: # Notify homeserver to grep it
2655: #
1.984 neumanie 2656: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 2657: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 2658: if ($fetchresult eq 'ok') {
1.860 raeburn 2659: if ($fetchthumb) {
2660: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
2661: if ($thumbresult ne 'ok') {
2662: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
2663: $docuhome.': '.$thumbresult);
2664: }
2665: }
1.259 www 2666: #
1.258 www 2667: # Return the URL to it
1.494 albertel 2668: return '/uploaded/'.$path.$file;
1.263 www 2669: } else {
1.494 albertel 2670: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
2671: ': '.$fetchresult);
1.263 www 2672: return '/adm/notfound.html';
1.858 raeburn 2673: }
1.493 albertel 2674: }
2675:
1.637 raeburn 2676: sub extract_embedded_items {
1.961 raeburn 2677: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 2678: my @state = ();
2679: my %javafiles = (
2680: codebase => '',
2681: code => '',
2682: archive => ''
2683: );
2684: my %mediafiles = (
2685: src => '',
2686: movie => '',
2687: );
1.648 raeburn 2688: my $p;
2689: if ($content) {
2690: $p = HTML::LCParser->new($content);
2691: } else {
1.961 raeburn 2692: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 2693: }
1.641 albertel 2694: while (my $t=$p->get_token()) {
1.640 albertel 2695: if ($t->[0] eq 'S') {
2696: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 2697: push(@state, $tagname);
1.648 raeburn 2698: if (lc($tagname) eq 'allow') {
2699: &add_filetype($allfiles,$attr->{'src'},'src');
2700: }
1.640 albertel 2701: if (lc($tagname) eq 'img') {
2702: &add_filetype($allfiles,$attr->{'src'},'src');
2703: }
1.886 albertel 2704: if (lc($tagname) eq 'a') {
2705: &add_filetype($allfiles,$attr->{'href'},'href');
2706: }
1.645 raeburn 2707: if (lc($tagname) eq 'script') {
2708: if ($attr->{'archive'} =~ /\.jar$/i) {
2709: &add_filetype($allfiles,$attr->{'archive'},'archive');
2710: } else {
2711: &add_filetype($allfiles,$attr->{'src'},'src');
2712: }
2713: }
2714: if (lc($tagname) eq 'link') {
2715: if (lc($attr->{'rel'}) eq 'stylesheet') {
2716: &add_filetype($allfiles,$attr->{'href'},'href');
2717: }
2718: }
1.640 albertel 2719: if (lc($tagname) eq 'object' ||
2720: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
2721: foreach my $item (keys(%javafiles)) {
2722: $javafiles{$item} = '';
2723: }
2724: }
2725: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
2726: my $name = lc($attr->{'name'});
2727: foreach my $item (keys(%javafiles)) {
2728: if ($name eq $item) {
2729: $javafiles{$item} = $attr->{'value'};
2730: last;
2731: }
2732: }
2733: foreach my $item (keys(%mediafiles)) {
2734: if ($name eq $item) {
2735: &add_filetype($allfiles, $attr->{'value'}, 'value');
2736: last;
2737: }
2738: }
2739: }
2740: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
2741: foreach my $item (keys(%javafiles)) {
2742: if ($attr->{$item}) {
2743: $javafiles{$item} = $attr->{$item};
2744: last;
2745: }
2746: }
2747: foreach my $item (keys(%mediafiles)) {
2748: if ($attr->{$item}) {
2749: &add_filetype($allfiles,$attr->{$item},$item);
2750: last;
2751: }
2752: }
2753: }
2754: } elsif ($t->[0] eq 'E') {
2755: my ($tagname) = ($t->[1]);
2756: if ($javafiles{'codebase'} ne '') {
2757: $javafiles{'codebase'} .= '/';
2758: }
2759: if (lc($tagname) eq 'applet' ||
2760: lc($tagname) eq 'object' ||
2761: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
2762: ) {
2763: foreach my $item (keys(%javafiles)) {
2764: if ($item ne 'codebase' && $javafiles{$item} ne '') {
2765: my $file=$javafiles{'codebase'}.$javafiles{$item};
2766: &add_filetype($allfiles,$file,$item);
2767: }
2768: }
2769: }
2770: pop @state;
2771: }
2772: }
1.637 raeburn 2773: return 'ok';
2774: }
2775:
1.639 albertel 2776: sub add_filetype {
2777: my ($allfiles,$file,$type)=@_;
2778: if (exists($allfiles->{$file})) {
2779: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
2780: push(@{$allfiles->{$file}}, &escape($type));
2781: }
2782: } else {
2783: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 2784: }
2785: }
2786:
1.493 albertel 2787: sub removeuploadedurl {
1.984 neumanie 2788: my ($url)=@_;
2789: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 2790: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 2791: }
2792:
2793: sub removeuserfile {
2794: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 2795: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2796: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 2797: if ($result eq 'ok') {
1.798 raeburn 2798: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
2799: my $metafile = $fname.'.meta';
2800: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 2801: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 2802: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2803: my $sqlresult =
1.823 albertel 2804: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2805: 'portfolio_metadata',$group,
2806: 'delete');
1.798 raeburn 2807: }
2808: }
2809: return $result;
1.257 www 2810: }
1.15 www 2811:
1.530 albertel 2812: sub mkdiruserfile {
2813: my ($docuname,$docudom,$dir)=@_;
2814: my $home=&homeserver($docuname,$docudom);
2815: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
2816: }
2817:
1.531 albertel 2818: sub renameuserfile {
2819: my ($docuname,$docudom,$old,$new)=@_;
2820: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2821: my $result = &reply("renameuserfile:$docudom:$docuname:".
2822: &escape("$old").':'.&escape("$new"),$home);
2823: if ($result eq 'ok') {
2824: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
2825: my $oldmeta = $old.'.meta';
2826: my $newmeta = $new.'.meta';
2827: my $metaresult =
2828: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 2829: my $url = "/uploaded/$docudom/$docuname/$old";
2830: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2831: my $sqlresult =
1.823 albertel 2832: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2833: 'portfolio_metadata',$group,
2834: 'delete');
1.798 raeburn 2835: }
2836: }
2837: return $result;
1.531 albertel 2838: }
2839:
1.14 www 2840: # ------------------------------------------------------------------------- Log
2841:
2842: sub log {
2843: my ($dom,$nam,$hom,$what)=@_;
1.47 www 2844: return critical("log:$dom:$nam:$what",$hom);
1.157 www 2845: }
2846:
2847: # ------------------------------------------------------------------ Course Log
1.352 www 2848: #
2849: # This routine flushes several buffers of non-mission-critical nature
2850: #
1.157 www 2851:
2852: sub flushcourselogs {
1.352 www 2853: &logthis('Flushing log buffers');
2854: #
2855: # course logs
2856: # This is a log of all transactions in a course, which can be used
2857: # for data mining purposes
2858: #
2859: # It also collects the courseid database, which lists last transaction
2860: # times and course titles for all courseids
2861: #
2862: my %courseidbuffer=();
1.921 raeburn 2863: foreach my $crsid (keys(%courselogs)) {
1.352 www 2864: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 2865: &escape($courselogs{$crsid}),
2866: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 2867: delete $courselogs{$crsid};
2868: } else {
2869: &logthis('Failed to flush log buffer for '.$crsid);
2870: if (length($courselogs{$crsid})>40000) {
1.672 albertel 2871: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 2872: " exceeded maximum size, deleting.</font>");
2873: delete $courselogs{$crsid};
2874: }
1.352 www 2875: }
1.920 raeburn 2876: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 2877: 'description' => $coursedescrbuf{$crsid},
2878: 'inst_code' => $courseinstcodebuf{$crsid},
2879: 'type' => $coursetypebuf{$crsid},
2880: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 2881: };
1.191 harris41 2882: }
1.352 www 2883: #
2884: # Write course id database (reverse lookup) to homeserver of courses
2885: # Is used in pickcourse
2886: #
1.840 albertel 2887: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 2888: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 2889: $courseidbuffer{$crs_home},
2890: $crs_home,'timeonly');
1.352 www 2891: }
2892: #
2893: # File accesses
2894: # Writes to the dynamic metadata of resources to get hit counts, etc.
2895: #
1.449 matthew 2896: foreach my $entry (keys(%accesshash)) {
1.458 matthew 2897: if ($entry =~ /___count$/) {
2898: my ($dom,$name);
1.807 albertel 2899: ($dom,$name,undef)=
1.811 albertel 2900: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 2901: if (! defined($dom) || $dom eq '' ||
2902: ! defined($name) || $name eq '') {
1.620 albertel 2903: my $cid = $env{'request.course.id'};
2904: $dom = $env{'request.'.$cid.'.domain'};
2905: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 2906: }
1.450 matthew 2907: my $value = $accesshash{$entry};
2908: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
2909: my %temphash=($url => $value);
1.449 matthew 2910: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
2911: if ($result eq 'ok') {
2912: delete $accesshash{$entry};
2913: } elsif ($result eq 'unknown_cmd') {
2914: # Target server has old code running on it.
1.450 matthew 2915: my %temphash=($entry => $value);
1.449 matthew 2916: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2917: delete $accesshash{$entry};
2918: }
2919: }
2920: } else {
1.811 albertel 2921: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 2922: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 2923: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2924: delete $accesshash{$entry};
2925: }
1.185 www 2926: }
1.191 harris41 2927: }
1.352 www 2928: #
2929: # Roles
2930: # Reverse lookup of user roles for course faculty/staff and co-authorship
2931: #
1.800 albertel 2932: foreach my $entry (keys(%userrolehash)) {
1.351 www 2933: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 2934: split(/\:/,$entry);
2935: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 2936: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 2937: $rudom,$runame) eq 'ok') {
2938: delete $userrolehash{$entry};
2939: }
2940: }
1.662 raeburn 2941: #
2942: # Reverse lookup of domain roles (dc, ad, li, sc, au)
2943: #
2944: my %domrolebuffer = ();
1.1000 raeburn 2945: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 2946: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 2947: if ($domrolebuffer{$rudom}) {
2948: $domrolebuffer{$rudom}.='&'.&escape($entry).
2949: '='.&escape($domainrolehash{$entry});
2950: } else {
2951: $domrolebuffer{$rudom}.=&escape($entry).
2952: '='.&escape($domainrolehash{$entry});
2953: }
2954: delete $domainrolehash{$entry};
2955: }
2956: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 2957: my %servers = &get_servers($dom,'library');
2958: foreach my $tryserver (keys(%servers)) {
2959: unless (&reply('domroleput:'.$dom.':'.
2960: $domrolebuffer{$dom},$tryserver) eq 'ok') {
2961: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
2962: }
1.662 raeburn 2963: }
2964: }
1.186 www 2965: $dumpcount++;
1.157 www 2966: }
2967:
2968: sub courselog {
2969: my $what=shift;
1.158 www 2970: $what=time.':'.$what;
1.620 albertel 2971: unless ($env{'request.course.id'}) { return ''; }
2972: $coursedombuf{$env{'request.course.id'}}=
2973: $env{'course.'.$env{'request.course.id'}.'.domain'};
2974: $coursenumbuf{$env{'request.course.id'}}=
2975: $env{'course.'.$env{'request.course.id'}.'.num'};
2976: $coursehombuf{$env{'request.course.id'}}=
2977: $env{'course.'.$env{'request.course.id'}.'.home'};
2978: $coursedescrbuf{$env{'request.course.id'}}=
2979: $env{'course.'.$env{'request.course.id'}.'.description'};
2980: $courseinstcodebuf{$env{'request.course.id'}}=
2981: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2982: $courseownerbuf{$env{'request.course.id'}}=
2983: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 2984: $coursetypebuf{$env{'request.course.id'}}=
2985: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 2986: if (defined $courselogs{$env{'request.course.id'}}) {
2987: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 2988: } else {
1.620 albertel 2989: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 2990: }
1.620 albertel 2991: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 2992: &flushcourselogs();
2993: }
1.158 www 2994: }
2995:
2996: sub courseacclog {
2997: my $fnsymb=shift;
1.620 albertel 2998: unless ($env{'request.course.id'}) { return ''; }
2999: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 3000: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 3001: $what.=':POST';
1.583 matthew 3002: # FIXME: Probably ought to escape things....
1.800 albertel 3003: foreach my $key (keys(%env)) {
3004: if ($key=~/^form\.(.*)/) {
1.975 raeburn 3005: my $formitem = $1;
3006: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
3007: $what.=':'.$formitem.'='.$env{$key};
3008: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
3009: $what.=':'.$formitem.'='.$env{$key};
3010: }
1.158 www 3011: }
1.191 harris41 3012: }
1.583 matthew 3013: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
3014: # FIXME: We should not be depending on a form parameter that someone
3015: # editing lonsearchcat.pm might change in the future.
1.620 albertel 3016: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 3017: $what.= ':POST';
3018: # FIXME: Probably ought to escape things....
3019: foreach my $element ('courseexp','crsfulltext','crsrelated',
3020: 'crsdiscuss') {
1.620 albertel 3021: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 3022: }
3023: }
1.158 www 3024: }
3025: &courselog($what);
1.149 www 3026: }
3027:
1.185 www 3028: sub countacc {
3029: my $url=&declutter(shift);
1.458 matthew 3030: return if (! defined($url) || $url eq '');
1.620 albertel 3031: unless ($env{'request.course.id'}) { return ''; }
3032: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 3033: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 3034: $accesshash{$key}++;
1.185 www 3035: }
1.349 www 3036:
1.361 www 3037: sub linklog {
3038: my ($from,$to)=@_;
3039: $from=&declutter($from);
3040: $to=&declutter($to);
3041: $accesshash{$from.'___'.$to.'___comefrom'}=1;
3042: $accesshash{$to.'___'.$from.'___goto'}=1;
3043: }
3044:
1.349 www 3045: sub userrolelog {
3046: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 3047: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 3048: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 3049: ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041 raeburn 3050: ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350 www 3051: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3052: $userrolehash
3053: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 3054: =$tend.':'.$tstart;
1.662 raeburn 3055: }
1.898 albertel 3056: if (($env{'request.role'} =~ /dc\./) &&
3057: (($trole=~/^au/) || ($trole=~/^in/) ||
3058: ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041 raeburn 3059: ($trole=~/^cr/) || ($trole=~/^ta/) ||
3060: ($trole=~/^co/))) {
1.898 albertel 3061: $userrolehash
3062: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
3063: =$tend.':'.$tstart;
3064: }
1.662 raeburn 3065: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
3066: ($trole=~/^li/) || ($trole=~/^li/) ||
3067: ($trole=~/^au/) || ($trole=~/^dg/) ||
3068: ($trole=~/^sc/)) {
3069: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3070: $domainrolehash
3071: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
3072: = $tend.':'.$tstart;
3073: }
1.351 www 3074: }
3075:
1.957 raeburn 3076: sub courserolelog {
3077: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
3078: if (($trole eq 'cc') || ($trole eq 'in') ||
3079: ($trole eq 'ep') || ($trole eq 'ad') ||
3080: ($trole eq 'ta') || ($trole eq 'st') ||
1.1044 raeburn 3081: ($trole=~/^cr/) || ($trole eq 'gr') ||
3082: ($trole eq 'co')) {
1.957 raeburn 3083: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
3084: my $cdom = $1;
3085: my $cnum = $2;
3086: my $sec = $3;
3087: my $namespace = 'rolelog';
3088: my %storehash = (
3089: role => $trole,
3090: start => $tstart,
3091: end => $tend,
3092: selfenroll => $selfenroll,
3093: context => $context,
3094: );
3095: if ($trole eq 'gr') {
3096: $namespace = 'groupslog';
3097: $storehash{'group'} = $sec;
3098: } else {
3099: $storehash{'section'} = $sec;
3100: }
3101: &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996 raeburn 3102: if (($trole ne 'st') || ($sec ne '')) {
3103: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
3104: }
1.957 raeburn 3105: }
3106: }
3107: return;
3108: }
3109:
1.351 www 3110: sub get_course_adv_roles {
1.948 raeburn 3111: my ($cid,$codes) = @_;
1.620 albertel 3112: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 3113: my %coursehash=&coursedescription($cid);
1.988 raeburn 3114: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 3115: my %nothide=();
1.800 albertel 3116: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 3117: if ($user !~ /:/) {
3118: $nothide{join(':',split(/[\@]/,$user))}=1;
3119: } else {
3120: $nothide{$user}=1;
3121: }
1.470 www 3122: }
1.351 www 3123: my %returnhash=();
3124: my %dumphash=
3125: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
3126: my $now=time;
1.997 raeburn 3127: my %privileged;
1.1000 raeburn 3128: foreach my $entry (keys(%dumphash)) {
1.800 albertel 3129: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 3130: if (($tstart) && ($tstart<0)) { next; }
3131: if (($tend) && ($tend<$now)) { next; }
3132: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 3133: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 3134: if ($username eq '' || $domain eq '') { next; }
1.997 raeburn 3135: unless (ref($privileged{$domain}) eq 'HASH') {
3136: my %dompersonnel =
1.998 raeburn 3137: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997 raeburn 3138: $privileged{$domain} = {};
3139: foreach my $server (keys(%dompersonnel)) {
1.999 raeburn 3140: if (ref($dompersonnel{$server}) eq 'HASH') {
1.997 raeburn 3141: foreach my $user (keys(%{$dompersonnel{$server}})) {
3142: my ($trole,$uname,$udom) = split(/:/,$user);
3143: $privileged{$udom}{$uname} = 1;
3144: }
3145: }
3146: }
3147: }
3148: if ((exists($privileged{$domain}{$username})) &&
3149: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 3150: if ($role eq 'cr') { next; }
1.948 raeburn 3151: if ($codes) {
3152: if ($section) { $role .= ':'.$section; }
3153: if ($returnhash{$role}) {
3154: $returnhash{$role}.=','.$username.':'.$domain;
3155: } else {
3156: $returnhash{$role}=$username.':'.$domain;
3157: }
1.351 www 3158: } else {
1.988 raeburn 3159: my $key=&plaintext($role,$crstype);
1.973 bisitz 3160: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 3161: if ($returnhash{$key}) {
3162: $returnhash{$key}.=','.$username.':'.$domain;
3163: } else {
3164: $returnhash{$key}=$username.':'.$domain;
3165: }
1.351 www 3166: }
1.948 raeburn 3167: }
1.400 www 3168: return %returnhash;
3169: }
3170:
3171: sub get_my_roles {
1.937 raeburn 3172: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 3173: unless (defined($uname)) { $uname=$env{'user.name'}; }
3174: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 3175: my (%dumphash,%nothide);
1.858 raeburn 3176: if ($context eq 'userroles') {
1.1056.4.10 raeburn 3177: my $extra = &freeze_escape({'skipcheck' => 1});
3178: %dumphash = &dump('roles',$udom,$uname,'.',undef,$extra);
1.858 raeburn 3179: } else {
3180: %dumphash=
1.400 www 3181: &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 3182: if ($hidepriv) {
3183: my %coursehash=&coursedescription($udom.'_'.$uname);
3184: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3185: if ($user !~ /:/) {
3186: $nothide{join(':',split(/[\@]/,$user))} = 1;
3187: } else {
3188: $nothide{$user} = 1;
3189: }
3190: }
3191: }
1.858 raeburn 3192: }
1.400 www 3193: my %returnhash=();
3194: my $now=time;
1.999 raeburn 3195: my %privileged;
1.800 albertel 3196: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 3197: my ($role,$tend,$tstart);
3198: if ($context eq 'userroles') {
3199: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
3200: } else {
3201: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
3202: }
1.400 www 3203: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 3204: my $status = 'active';
1.939 raeburn 3205: if (($tend) && ($tend<=$now)) {
1.832 raeburn 3206: $status = 'previous';
3207: }
3208: if (($tstart) && ($now<$tstart)) {
3209: $status = 'future';
3210: }
3211: if (ref($types) eq 'ARRAY') {
3212: if (!grep(/^\Q$status\E$/,@{$types})) {
3213: next;
3214: }
3215: } else {
3216: if ($status ne 'active') {
3217: next;
3218: }
3219: }
1.867 raeburn 3220: my ($rolecode,$username,$domain,$section,$area);
3221: if ($context eq 'userroles') {
3222: ($area,$rolecode) = split(/_/,$entry);
3223: (undef,$domain,$username,$section) = split(/\//,$area);
3224: } else {
3225: ($role,$username,$domain,$section) = split(/\:/,$entry);
3226: }
1.832 raeburn 3227: if (ref($roledoms) eq 'ARRAY') {
3228: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
3229: next;
3230: }
3231: }
3232: if (ref($roles) eq 'ARRAY') {
3233: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 3234: if ($role =~ /^cr\//) {
3235: if (!grep(/^cr$/,@{$roles})) {
3236: next;
3237: }
3238: } else {
3239: next;
3240: }
1.832 raeburn 3241: }
1.867 raeburn 3242: }
1.937 raeburn 3243: if ($hidepriv) {
1.999 raeburn 3244: if ($context eq 'userroles') {
3245: if ((&privileged($username,$domain)) &&
3246: (!$nothide{$username.':'.$domain})) {
3247: next;
3248: }
3249: } else {
3250: unless (ref($privileged{$domain}) eq 'HASH') {
3251: my %dompersonnel =
3252: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
3253: $privileged{$domain} = {};
3254: if (keys(%dompersonnel)) {
3255: foreach my $server (keys(%dompersonnel)) {
3256: if (ref($dompersonnel{$server}) eq 'HASH') {
3257: foreach my $user (keys(%{$dompersonnel{$server}})) {
3258: my ($trole,$uname,$udom) = split(/:/,$user);
3259: $privileged{$udom}{$uname} = $trole;
3260: }
3261: }
3262: }
3263: }
3264: }
3265: if (exists($privileged{$domain}{$username})) {
3266: if (!$nothide{$username.':'.$domain}) {
3267: next;
3268: }
3269: }
1.937 raeburn 3270: }
3271: }
1.933 raeburn 3272: if ($withsec) {
3273: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
3274: $tstart.':'.$tend;
3275: } else {
3276: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
3277: }
1.832 raeburn 3278: }
1.373 www 3279: return %returnhash;
1.399 www 3280: }
3281:
3282: # ----------------------------------------------------- Frontpage Announcements
3283: #
3284: #
3285:
3286: sub postannounce {
3287: my ($server,$text)=@_;
1.844 albertel 3288: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 3289: unless ($text=~/\w/) { $text=''; }
3290: return &reply('setannounce:'.&escape($text),$server);
3291: }
3292:
3293: sub getannounce {
1.448 albertel 3294:
3295: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 3296: my $announcement='';
1.800 albertel 3297: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 3298: close($fh);
1.399 www 3299: if ($announcement=~/\w/) {
3300: return
3301: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 3302: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 3303: } else {
3304: return '';
3305: }
3306: } else {
3307: return '';
3308: }
1.351 www 3309: }
1.353 www 3310:
3311: # ---------------------------------------------------------- Course ID routines
3312: # Deal with domain's nohist_courseid.db files
3313: #
3314:
3315: sub courseidput {
1.921 raeburn 3316: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 3317: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 3318: my $outcome;
3319: if ($caller eq 'timeonly') {
3320: my $cids = '';
3321: foreach my $item (keys(%$storehash)) {
3322: $cids.=&escape($item).'&';
3323: }
3324: $cids=~s/\&$//;
3325: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
3326: $coursehome);
3327: } else {
3328: my $items = '';
3329: foreach my $item (keys(%$storehash)) {
3330: $items.= &escape($item).'='.
3331: &freeze_escape($$storehash{$item}).'&';
3332: }
3333: $items=~s/\&$//;
3334: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
3335: $coursehome);
1.918 raeburn 3336: }
3337: if ($outcome eq 'unknown_cmd') {
3338: my $what;
3339: foreach my $cid (keys(%$storehash)) {
3340: $what .= &escape($cid).'=';
1.921 raeburn 3341: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 3342: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 3343: }
3344: $what =~ s/\:$/&/;
3345: }
3346: $what =~ s/\&$//;
3347: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
3348: } else {
3349: return $outcome;
3350: }
1.353 www 3351: }
3352:
3353: sub courseiddump {
1.921 raeburn 3354: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 3355: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 3356: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1056.4.3 raeburn 3357: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918 raeburn 3358: my $as_hash = 1;
3359: my %returnhash;
3360: if (!$domfilter) { $domfilter=''; }
1.845 albertel 3361: my %libserv = &all_library();
3362: foreach my $tryserver (keys(%libserv)) {
3363: if ( ( $hostidflag == 1
3364: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
3365: || (!defined($hostidflag)) ) {
3366:
1.918 raeburn 3367: if (($domfilter eq '') ||
3368: (&host_domain($tryserver) eq $domfilter)) {
3369: my $rep =
3370: &reply('courseiddump:'.&host_domain($tryserver).':'.
3371: $sincefilter.':'.&escape($descfilter).':'.
3372: &escape($instcodefilter).':'.&escape($ownerfilter).
3373: ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947 raeburn 3374: ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962 raeburn 3375: &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008 raeburn 3376: $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029 raeburn 3377: &escape($cc_clone).':'.$cloneonly.':'.
3378: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1056.4.3 raeburn 3379: &escape($creationcontext).':'.$domcloner,
3380: $tryserver);
1.918 raeburn 3381: my @pairs=split(/\&/,$rep);
3382: foreach my $item (@pairs) {
3383: my ($key,$value)=split(/\=/,$item,2);
3384: $key = &unescape($key);
3385: next if ($key =~ /^error: 2 /);
3386: my $result = &thaw_unescape($value);
3387: if (ref($result) eq 'HASH') {
3388: $returnhash{$key}=$result;
3389: } else {
1.921 raeburn 3390: my @responses = split(/:/,$value);
3391: my @items = ('description','inst_code','owner','type');
1.918 raeburn 3392: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 3393: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 3394: }
1.1008 raeburn 3395: }
1.353 www 3396: }
3397: }
3398: }
3399: }
3400: return %returnhash;
3401: }
3402:
1.1055 raeburn 3403: sub courselastaccess {
3404: my ($cdom,$cnum,$hostidref) = @_;
3405: my %returnhash;
3406: if ($cdom && $cnum) {
3407: my $chome = &homeserver($cnum,$cdom);
3408: if ($chome ne 'no_host') {
3409: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
3410: &extract_lastaccess(\%returnhash,$rep);
3411: }
3412: } else {
3413: if (!$cdom) { $cdom=''; }
3414: my %libserv = &all_library();
3415: foreach my $tryserver (keys(%libserv)) {
3416: if (ref($hostidref) eq 'ARRAY') {
3417: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
3418: }
3419: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
3420: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
3421: &extract_lastaccess(\%returnhash,$rep);
3422: }
3423: }
3424: }
3425: return %returnhash;
3426: }
3427:
3428: sub extract_lastaccess {
3429: my ($returnhash,$rep) = @_;
3430: if (ref($returnhash) eq 'HASH') {
3431: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
3432: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
3433: $rep eq '') {
3434: my @pairs=split(/\&/,$rep);
3435: foreach my $item (@pairs) {
3436: my ($key,$value)=split(/\=/,$item,2);
3437: $key = &unescape($key);
3438: next if ($key =~ /^error: 2 /);
3439: $returnhash->{$key} = &thaw_unescape($value);
3440: }
3441: }
3442: }
3443: return;
3444: }
3445:
1.658 raeburn 3446: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 3447:
3448: sub dcmailput {
1.685 raeburn 3449: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 3450: my $status = &Apache::lonnet::critical(
1.740 www 3451: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
3452: &escape($message),$server);
1.662 raeburn 3453: return $status;
3454: }
3455:
1.658 raeburn 3456: sub dcmaildump {
3457: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 3458: my %returnhash=();
1.846 albertel 3459:
3460: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 3461: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
3462: &escape($enddate).':';
3463: my @esc_senders=map { &escape($_)} @$senders;
3464: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 3465: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 3466: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 3467: if (($key) && ($value)) {
3468: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 3469: }
3470: }
3471: }
3472: return %returnhash;
3473: }
1.662 raeburn 3474: # ---------------------------------------------------------- Domain roles
3475:
3476: sub get_domain_roles {
3477: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 3478: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 3479: $startdate = '.';
3480: }
1.1018 raeburn 3481: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 3482: $enddate = '.';
3483: }
1.922 raeburn 3484: my $rolelist;
3485: if (ref($roles) eq 'ARRAY') {
3486: $rolelist = join(':',@{$roles});
3487: }
1.662 raeburn 3488: my %personnel = ();
1.841 albertel 3489:
3490: my %servers = &get_servers($dom,'library');
3491: foreach my $tryserver (keys(%servers)) {
3492: %{$personnel{$tryserver}}=();
3493: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
3494: &escape($startdate).':'.
3495: &escape($enddate).':'.
3496: &escape($rolelist), $tryserver))) {
3497: my ($key,$value) = split(/\=/,$line,2);
3498: if (($key) && ($value)) {
3499: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
3500: }
3501: }
1.662 raeburn 3502: }
3503: return %personnel;
3504: }
1.658 raeburn 3505:
1.149 www 3506: # ----------------------------------------------------------- Check out an item
3507:
1.504 albertel 3508: sub get_first_access {
3509: my ($type,$argsymb)=@_;
1.790 albertel 3510: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3511: if ($argsymb) { $symb=$argsymb; }
3512: my ($map,$id,$res)=&decode_symb($symb);
1.926 albertel 3513: if ($type eq 'course') {
3514: $res='course';
3515: } elsif ($type eq 'map') {
1.588 albertel 3516: $res=&symbread($map);
3517: } else {
3518: $res=$symb;
3519: }
3520: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
3521: return $times{"$courseid\0$res"};
1.504 albertel 3522: }
3523:
3524: sub set_first_access {
3525: my ($type)=@_;
1.790 albertel 3526: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3527: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 3528: if ($type eq 'course') {
3529: $res='course';
3530: } elsif ($type eq 'map') {
1.588 albertel 3531: $res=&symbread($map);
3532: } else {
3533: $res=$symb;
3534: }
3535: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 3536: if (!$firstaccess) {
1.588 albertel 3537: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 3538: }
3539: return 'already_set';
1.504 albertel 3540: }
3541:
1.149 www 3542: sub checkout {
3543: my ($symb,$tuname,$tudom,$tcrsid)=@_;
3544: my $now=time;
3545: my $lonhost=$perlvar{'lonHostID'};
3546: my $infostr=&escape(
1.234 www 3547: 'CHECKOUTTOKEN&'.
1.149 www 3548: $tuname.'&'.
3549: $tudom.'&'.
3550: $tcrsid.'&'.
3551: $symb.'&'.
3552: $now.'&'.$ENV{'REMOTE_ADDR'});
3553: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 3554: if ($token=~/^error\:/) {
1.672 albertel 3555: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3556: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
3557: "</font>");
3558: return '';
3559: }
3560:
1.149 www 3561: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
3562: $token=~tr/a-z/A-Z/;
3563:
1.153 www 3564: my %infohash=('resource.0.outtoken' => $token,
3565: 'resource.0.checkouttime' => $now,
3566: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 3567:
3568: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3569: return '';
1.151 www 3570: } else {
1.672 albertel 3571: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3572: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
3573: "</font>");
1.149 www 3574: }
3575:
3576: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3577: &escape('Checkout '.$infostr.' - '.
3578: $token)) ne 'ok') {
3579: return '';
1.151 www 3580: } else {
1.672 albertel 3581: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3582: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
3583: "</font>");
1.149 www 3584: }
1.151 www 3585: return $token;
1.149 www 3586: }
3587:
3588: # ------------------------------------------------------------ Check in an item
3589:
3590: sub checkin {
3591: my $token=shift;
1.150 www 3592: my $now=time;
3593: my ($ta,$tb,$lonhost)=split(/\*/,$token);
3594: $lonhost=~tr/A-Z/a-z/;
1.838 albertel 3595: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150 www 3596: $dtoken=~s/\W/\_/g;
1.234 www 3597: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 3598: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
3599:
1.154 www 3600: unless (($tuname) && ($tudom)) {
3601: &logthis('Check in '.$token.' ('.$dtoken.') failed');
3602: return '';
3603: }
3604:
3605: unless (&allowed('mgr',$tcrsid)) {
3606: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 3607: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 3608: return '';
3609: }
3610:
1.153 www 3611: my %infohash=('resource.0.intoken' => $token,
3612: 'resource.0.checkintime' => $now,
3613: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 3614:
3615: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3616: return '';
3617: }
3618:
3619: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3620: &escape('Checkin - '.$token)) ne 'ok') {
3621: return '';
3622: }
3623:
3624: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 3625: }
3626:
3627: # --------------------------------------------- Set Expire Date for Spreadsheet
3628:
3629: sub expirespread {
3630: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 3631: my $cid=$env{'request.course.id'};
1.110 www 3632: if ($cid) {
3633: my $now=time;
3634: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 3635: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
3636: $env{'course.'.$cid.'.num'}.
1.110 www 3637: ':nohist_expirationdates:'.
3638: &escape($key).'='.$now,
1.620 albertel 3639: $env{'course.'.$cid.'.home'})
1.110 www 3640: }
3641: return 'ok';
1.14 www 3642: }
3643:
1.109 www 3644: # ----------------------------------------------------- Devalidate Spreadsheets
3645:
3646: sub devalidate {
1.325 www 3647: my ($symb,$uname,$udom)=@_;
1.620 albertel 3648: my $cid=$env{'request.course.id'};
1.109 www 3649: if ($cid) {
1.391 matthew 3650: # delete the stored spreadsheets for
3651: # - the student level sheet of this user in course's homespace
3652: # - the assessment level sheet for this resource
3653: # for this user in user's homespace
1.553 albertel 3654: # - current conditional state info
1.325 www 3655: my $key=$uname.':'.$udom.':';
1.109 www 3656: my $status=
1.299 matthew 3657: &del('nohist_calculatedsheets',
1.391 matthew 3658: [$key.'studentcalc:'],
1.620 albertel 3659: $env{'course.'.$cid.'.domain'},
3660: $env{'course.'.$cid.'.num'})
1.133 albertel 3661: .' '.
3662: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 3663: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 3664: unless ($status eq 'ok ok') {
3665: &logthis('Could not devalidate spreadsheet '.
1.325 www 3666: $uname.' at '.$udom.' for '.
1.109 www 3667: $symb.': '.$status);
1.133 albertel 3668: }
1.553 albertel 3669: &delenv('user.state.'.$cid);
1.109 www 3670: }
3671: }
3672:
1.265 albertel 3673: sub get_scalar {
3674: my ($string,$end) = @_;
3675: my $value;
3676: if ($$string =~ s/^([^&]*?)($end)/$2/) {
3677: $value = $1;
3678: } elsif ($$string =~ s/^([^&]*?)&//) {
3679: $value = $1;
3680: }
3681: return &unescape($value);
3682: }
3683:
3684: sub array2str {
3685: my (@array) = @_;
3686: my $result=&arrayref2str(\@array);
3687: $result=~s/^__ARRAY_REF__//;
3688: $result=~s/__END_ARRAY_REF__$//;
3689: return $result;
3690: }
3691:
1.204 albertel 3692: sub arrayref2str {
3693: my ($arrayref) = @_;
1.265 albertel 3694: my $result='__ARRAY_REF__';
1.204 albertel 3695: foreach my $elem (@$arrayref) {
1.265 albertel 3696: if(ref($elem) eq 'ARRAY') {
3697: $result.=&arrayref2str($elem).'&';
3698: } elsif(ref($elem) eq 'HASH') {
3699: $result.=&hashref2str($elem).'&';
3700: } elsif(ref($elem)) {
3701: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 3702: } else {
3703: $result.=&escape($elem).'&';
3704: }
3705: }
3706: $result=~s/\&$//;
1.265 albertel 3707: $result .= '__END_ARRAY_REF__';
1.204 albertel 3708: return $result;
3709: }
3710:
1.168 albertel 3711: sub hash2str {
1.204 albertel 3712: my (%hash) = @_;
3713: my $result=&hashref2str(\%hash);
1.265 albertel 3714: $result=~s/^__HASH_REF__//;
3715: $result=~s/__END_HASH_REF__$//;
1.204 albertel 3716: return $result;
3717: }
3718:
3719: sub hashref2str {
3720: my ($hashref)=@_;
1.265 albertel 3721: my $result='__HASH_REF__';
1.800 albertel 3722: foreach my $key (sort(keys(%$hashref))) {
3723: if (ref($key) eq 'ARRAY') {
3724: $result.=&arrayref2str($key).'=';
3725: } elsif (ref($key) eq 'HASH') {
3726: $result.=&hashref2str($key).'=';
3727: } elsif (ref($key)) {
1.265 albertel 3728: $result.='=';
1.800 albertel 3729: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 3730: } else {
1.1056.4.32 raeburn 3731: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 3732: }
3733:
1.800 albertel 3734: if(ref($hashref->{$key}) eq 'ARRAY') {
3735: $result.=&arrayref2str($hashref->{$key}).'&';
3736: } elsif(ref($hashref->{$key}) eq 'HASH') {
3737: $result.=&hashref2str($hashref->{$key}).'&';
3738: } elsif(ref($hashref->{$key})) {
1.265 albertel 3739: $result.='&';
1.800 albertel 3740: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 3741: } else {
1.800 albertel 3742: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 3743: }
3744: }
1.168 albertel 3745: $result=~s/\&$//;
1.265 albertel 3746: $result .= '__END_HASH_REF__';
1.168 albertel 3747: return $result;
3748: }
3749:
3750: sub str2hash {
1.265 albertel 3751: my ($string)=@_;
3752: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
3753: return %$hash;
3754: }
3755:
3756: sub str2hashref {
1.168 albertel 3757: my ($string) = @_;
1.265 albertel 3758:
3759: my %hash;
3760:
3761: if($string !~ /^__HASH_REF__/) {
3762: if (! ($string eq '' || !defined($string))) {
3763: $hash{'error'}='Not hash reference';
3764: }
3765: return (\%hash, $string);
3766: }
3767:
3768: $string =~ s/^__HASH_REF__//;
3769:
3770: while($string !~ /^__END_HASH_REF__/) {
3771: #key
3772: my $key='';
3773: if($string =~ /^__HASH_REF__/) {
3774: ($key, $string)=&str2hashref($string);
3775: if(defined($key->{'error'})) {
3776: $hash{'error'}='Bad data';
3777: return (\%hash, $string);
3778: }
3779: } elsif($string =~ /^__ARRAY_REF__/) {
3780: ($key, $string)=&str2arrayref($string);
3781: if($key->[0] eq 'Array reference error') {
3782: $hash{'error'}='Bad data';
3783: return (\%hash, $string);
3784: }
3785: } else {
3786: $string =~ s/^(.*?)=//;
1.267 albertel 3787: $key=&unescape($1);
1.265 albertel 3788: }
3789: $string =~ s/^=//;
3790:
3791: #value
3792: my $value='';
3793: if($string =~ /^__HASH_REF__/) {
3794: ($value, $string)=&str2hashref($string);
3795: if(defined($value->{'error'})) {
3796: $hash{'error'}='Bad data';
3797: return (\%hash, $string);
3798: }
3799: } elsif($string =~ /^__ARRAY_REF__/) {
3800: ($value, $string)=&str2arrayref($string);
3801: if($value->[0] eq 'Array reference error') {
3802: $hash{'error'}='Bad data';
3803: return (\%hash, $string);
3804: }
3805: } else {
3806: $value=&get_scalar(\$string,'__END_HASH_REF__');
3807: }
3808: $string =~ s/^&//;
3809:
3810: $hash{$key}=$value;
1.204 albertel 3811: }
1.265 albertel 3812:
3813: $string =~ s/^__END_HASH_REF__//;
3814:
3815: return (\%hash, $string);
1.204 albertel 3816: }
3817:
3818: sub str2array {
1.265 albertel 3819: my ($string)=@_;
3820: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
3821: return @$array;
3822: }
3823:
3824: sub str2arrayref {
1.204 albertel 3825: my ($string) = @_;
1.265 albertel 3826: my @array;
3827:
3828: if($string !~ /^__ARRAY_REF__/) {
3829: if (! ($string eq '' || !defined($string))) {
3830: $array[0]='Array reference error';
3831: }
3832: return (\@array, $string);
3833: }
3834:
3835: $string =~ s/^__ARRAY_REF__//;
3836:
3837: while($string !~ /^__END_ARRAY_REF__/) {
3838: my $value='';
3839: if($string =~ /^__HASH_REF__/) {
3840: ($value, $string)=&str2hashref($string);
3841: if(defined($value->{'error'})) {
3842: $array[0] ='Array reference error';
3843: return (\@array, $string);
3844: }
3845: } elsif($string =~ /^__ARRAY_REF__/) {
3846: ($value, $string)=&str2arrayref($string);
3847: if($value->[0] eq 'Array reference error') {
3848: $array[0] ='Array reference error';
3849: return (\@array, $string);
3850: }
3851: } else {
3852: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
3853: }
3854: $string =~ s/^&//;
3855:
3856: push(@array, $value);
1.191 harris41 3857: }
1.265 albertel 3858:
3859: $string =~ s/^__END_ARRAY_REF__//;
3860:
3861: return (\@array, $string);
1.168 albertel 3862: }
3863:
1.167 albertel 3864: # -------------------------------------------------------------------Temp Store
3865:
1.168 albertel 3866: sub tmpreset {
3867: my ($symb,$namespace,$domain,$stuname) = @_;
3868: if (!$symb) {
3869: $symb=&symbread();
1.620 albertel 3870: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3871: }
3872: $symb=escape($symb);
3873:
1.620 albertel 3874: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 3875: $namespace=~s/\//\_/g;
3876: $namespace=~s/\W//g;
3877:
1.620 albertel 3878: if (!$domain) { $domain=$env{'user.domain'}; }
3879: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3880: if ($domain eq 'public' && $stuname eq 'public') {
3881: $stuname=$ENV{'REMOTE_ADDR'};
3882: }
1.168 albertel 3883: my $path=$perlvar{'lonDaemons'}.'/tmp';
3884: my %hash;
3885: if (tie(%hash,'GDBM_File',
3886: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3887: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 3888: foreach my $key (keys(%hash)) {
1.180 albertel 3889: if ($key=~ /:$symb/) {
1.168 albertel 3890: delete($hash{$key});
3891: }
3892: }
3893: }
3894: }
3895:
1.167 albertel 3896: sub tmpstore {
1.168 albertel 3897: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3898:
3899: if (!$symb) {
3900: $symb=&symbread();
1.620 albertel 3901: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3902: }
3903: $symb=escape($symb);
3904:
3905: if (!$namespace) {
3906: # I don't think we would ever want to store this for a course.
3907: # it seems this will only be used if we don't have a course.
1.620 albertel 3908: #$namespace=$env{'request.course.id'};
1.168 albertel 3909: #if (!$namespace) {
1.620 albertel 3910: $namespace=$env{'request.state'};
1.168 albertel 3911: #}
3912: }
3913: $namespace=~s/\//\_/g;
3914: $namespace=~s/\W//g;
1.620 albertel 3915: if (!$domain) { $domain=$env{'user.domain'}; }
3916: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3917: if ($domain eq 'public' && $stuname eq 'public') {
3918: $stuname=$ENV{'REMOTE_ADDR'};
3919: }
1.168 albertel 3920: my $now=time;
3921: my %hash;
3922: my $path=$perlvar{'lonDaemons'}.'/tmp';
3923: if (tie(%hash,'GDBM_File',
3924: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3925: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3926: $hash{"version:$symb"}++;
3927: my $version=$hash{"version:$symb"};
3928: my $allkeys='';
3929: foreach my $key (keys(%$storehash)) {
3930: $allkeys.=$key.':';
1.591 albertel 3931: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 3932: }
3933: $hash{"$version:$symb:timestamp"}=$now;
3934: $allkeys.='timestamp';
3935: $hash{"$version:keys:$symb"}=$allkeys;
3936: if (untie(%hash)) {
3937: return 'ok';
3938: } else {
3939: return "error:$!";
3940: }
3941: } else {
3942: return "error:$!";
3943: }
3944: }
1.167 albertel 3945:
1.168 albertel 3946: # -----------------------------------------------------------------Temp Restore
1.167 albertel 3947:
1.168 albertel 3948: sub tmprestore {
3949: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 3950:
1.168 albertel 3951: if (!$symb) {
3952: $symb=&symbread();
1.620 albertel 3953: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3954: }
3955: $symb=escape($symb);
3956:
1.620 albertel 3957: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 3958:
1.620 albertel 3959: if (!$domain) { $domain=$env{'user.domain'}; }
3960: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3961: if ($domain eq 'public' && $stuname eq 'public') {
3962: $stuname=$ENV{'REMOTE_ADDR'};
3963: }
1.168 albertel 3964: my %returnhash;
3965: $namespace=~s/\//\_/g;
3966: $namespace=~s/\W//g;
3967: my %hash;
3968: my $path=$perlvar{'lonDaemons'}.'/tmp';
3969: if (tie(%hash,'GDBM_File',
3970: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3971: &GDBM_READER(),0640)) {
1.168 albertel 3972: my $version=$hash{"version:$symb"};
3973: $returnhash{'version'}=$version;
3974: my $scope;
3975: for ($scope=1;$scope<=$version;$scope++) {
3976: my $vkeys=$hash{"$scope:keys:$symb"};
3977: my @keys=split(/:/,$vkeys);
3978: my $key;
3979: $returnhash{"$scope:keys"}=$vkeys;
3980: foreach $key (@keys) {
1.591 albertel 3981: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
3982: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 3983: }
3984: }
1.168 albertel 3985: if (!(untie(%hash))) {
3986: return "error:$!";
3987: }
3988: } else {
3989: return "error:$!";
3990: }
3991: return %returnhash;
1.167 albertel 3992: }
3993:
1.9 www 3994: # ----------------------------------------------------------------------- Store
3995:
3996: sub store {
1.124 www 3997: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3998: my $home='';
3999:
1.168 albertel 4000: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4001:
1.213 www 4002: $symb=&symbclean($symb);
1.122 albertel 4003: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4004:
1.620 albertel 4005: if (!$domain) { $domain=$env{'user.domain'}; }
4006: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4007:
4008: &devalidate($symb,$stuname,$domain);
1.109 www 4009:
4010: $symb=escape($symb);
1.187 www 4011: if (!$namespace) {
1.620 albertel 4012: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4013: return '';
4014: }
4015: }
1.620 albertel 4016: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4017:
4018: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4019: $$storehash{'host'}=$perlvar{'lonHostID'};
4020:
1.12 www 4021: my $namevalue='';
1.800 albertel 4022: foreach my $key (keys(%$storehash)) {
4023: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4024: }
1.12 www 4025: $namevalue=~s/\&$//;
1.187 www 4026: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 4027: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 4028: }
4029:
1.47 www 4030: # -------------------------------------------------------------- Critical Store
4031:
4032: sub cstore {
1.124 www 4033: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
4034: my $home='';
4035:
1.168 albertel 4036: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4037:
1.213 www 4038: $symb=&symbclean($symb);
1.122 albertel 4039: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4040:
1.620 albertel 4041: if (!$domain) { $domain=$env{'user.domain'}; }
4042: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4043:
4044: &devalidate($symb,$stuname,$domain);
1.109 www 4045:
4046: $symb=escape($symb);
1.187 www 4047: if (!$namespace) {
1.620 albertel 4048: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4049: return '';
4050: }
4051: }
1.620 albertel 4052: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4053:
4054: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4055: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 4056:
1.47 www 4057: my $namevalue='';
1.800 albertel 4058: foreach my $key (keys(%$storehash)) {
4059: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4060: }
1.47 www 4061: $namevalue=~s/\&$//;
1.187 www 4062: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 4063: return critical
4064: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 4065: }
4066:
1.9 www 4067: # --------------------------------------------------------------------- Restore
4068:
4069: sub restore {
1.124 www 4070: my ($symb,$namespace,$domain,$stuname) = @_;
4071: my $home='';
4072:
1.168 albertel 4073: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4074:
1.122 albertel 4075: if (!$symb) {
1.1056.4.37 raeburn 4076: return if ($namespace eq 'courserequests');
4077: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 4078: } else {
1.1056.4.37 raeburn 4079: unless ($namespace eq 'courserequests') {
4080: $symb=&escape(&symbclean($symb));
4081: }
1.122 albertel 4082: }
1.188 www 4083: if (!$namespace) {
1.620 albertel 4084: unless ($namespace=$env{'request.course.id'}) {
1.188 www 4085: return '';
4086: }
4087: }
1.620 albertel 4088: if (!$domain) { $domain=$env{'user.domain'}; }
4089: if (!$stuname) { $stuname=$env{'user.name'}; }
4090: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 4091: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
4092:
1.12 www 4093: my %returnhash=();
1.800 albertel 4094: foreach my $line (split(/\&/,$answer)) {
4095: my ($name,$value)=split(/\=/,$line);
1.591 albertel 4096: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 4097: }
1.75 www 4098: my $version;
4099: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 4100: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
4101: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 4102: }
1.75 www 4103: }
1.13 www 4104: return %returnhash;
1.34 www 4105: }
4106:
4107: # ---------------------------------------------------------- Course Description
4108:
4109: sub coursedescription {
1.731 albertel 4110: my ($courseid,$args)=@_;
1.34 www 4111: $courseid=~s/^\///;
1.49 www 4112: $courseid=~s/\_/\//g;
1.34 www 4113: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 4114: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 4115: my $normalid=$cdomain.'_'.$cnum;
4116: # need to always cache even if we get errors otherwise we keep
4117: # trying and trying and trying to get the course description.
4118: my %envhash=();
4119: my %returnhash=();
1.731 albertel 4120:
4121: my $expiretime=600;
4122: if ($env{'request.course.id'} eq $normalid) {
4123: $expiretime=120;
4124: }
4125:
4126: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
4127: if (!$args->{'freshen_cache'}
4128: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
4129: foreach my $key (keys(%env)) {
4130: next if ($key !~ /^\Q$prefix\E(.*)/);
4131: my ($setting) = $1;
4132: $returnhash{$setting} = $env{$key};
4133: }
4134: return %returnhash;
4135: }
4136:
4137: # get the data agin
4138: if (!$args->{'one_time'}) {
4139: $envhash{'course.'.$normalid.'.last_cache'}=time;
4140: }
1.811 albertel 4141:
1.34 www 4142: if ($chome ne 'no_host') {
1.302 albertel 4143: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 4144: if (!exists($returnhash{'con_lost'})) {
4145: $returnhash{'home'}= $chome;
4146: $returnhash{'domain'} = $cdomain;
4147: $returnhash{'num'} = $cnum;
1.741 raeburn 4148: if (!defined($returnhash{'type'})) {
4149: $returnhash{'type'} = 'Course';
4150: }
1.130 albertel 4151: while (my ($name,$value) = each %returnhash) {
1.53 www 4152: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 4153: }
1.270 www 4154: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 4155: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 4156: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 4157: $envhash{'course.'.$normalid.'.home'}=$chome;
4158: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
4159: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 4160: }
4161: }
1.731 albertel 4162: if (!$args->{'one_time'}) {
1.949 raeburn 4163: &appenv(\%envhash);
1.731 albertel 4164: }
1.302 albertel 4165: return %returnhash;
1.461 www 4166: }
4167:
1.1056.4.5 raeburn 4168: sub update_released_required {
4169: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
4170: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
4171: $cid = $env{'request.course.id'};
4172: $cdom = $env{'course.'.$cid.'.domain'};
4173: $cnum = $env{'course.'.$cid.'.num'};
4174: $chome = $env{'course.'.$cid.'.home'};
4175: }
4176: if ($needsrelease) {
4177: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
4178: my $needsupdate;
4179: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
4180: $needsupdate = 1;
4181: } else {
4182: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
4183: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
4184: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
4185: $needsupdate = 1;
4186: }
4187: }
4188: if ($needsupdate) {
4189: my %needshash = (
4190: 'internal.releaserequired' => $needsrelease,
4191: );
4192: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
4193: if ($putresult eq 'ok') {
4194: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
4195: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
4196: if (ref($crsinfo{$cid}) eq 'HASH') {
4197: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
4198: &courseidput($cdom,\%crsinfo,$chome,'notime');
4199: }
4200: }
4201: }
4202: }
4203: return;
4204: }
4205:
1.461 www 4206: # -------------------------------------------------See if a user is privileged
4207:
4208: sub privileged {
4209: my ($username,$domain)=@_;
4210: my $rolesdump=&reply("dump:$domain:$username:roles",
4211: &homeserver($username,$domain));
1.1033 raeburn 4212: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
4213: ($rolesdump =~ /^error:/)) {
4214: return 0;
4215: }
1.461 www 4216: my $now=time;
4217: if ($rolesdump ne '') {
1.800 albertel 4218: foreach my $entry (split(/&/,$rolesdump)) {
4219: if ($entry!~/^rolesdef_/) {
4220: my ($area,$role)=split(/=/,$entry);
1.461 www 4221: $area=~s/\_\w\w$//;
4222: my ($trole,$tend,$tstart)=split(/_/,$role);
4223: if (($trole eq 'dc') || ($trole eq 'su')) {
4224: my $active=1;
4225: if ($tend) {
4226: if ($tend<$now) { $active=0; }
4227: }
4228: if ($tstart) {
4229: if ($tstart>$now) { $active=0; }
4230: }
4231: if ($active) { return 1; }
4232: }
4233: }
4234: }
4235: }
4236: return 0;
1.9 www 4237: }
1.1 albertel 4238:
1.103 harris41 4239: # -------------------------------------------------------- Get user privileges
1.11 www 4240:
4241: sub rolesinit {
4242: my ($domain,$username,$authhost)=@_;
1.1034 raeburn 4243: my $now=time;
4244: my %userroles = ('user.login.time' => $now);
1.1056.4.10 raeburn 4245: my $extra = &freeze_escape({'skipcheck' => 1});
1.1056.4.3 raeburn 4246: my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033 raeburn 4247: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
1.1056.4.3 raeburn 4248: ($rolesdump =~ /^error:/)) {
1.1033 raeburn 4249: return \%userroles;
4250: }
1.11 www 4251: my %allroles=();
1.678 raeburn 4252: my %allgroups=();
4253: my $group_privs;
1.11 www 4254:
4255: if ($rolesdump ne '') {
1.800 albertel 4256: foreach my $entry (split(/&/,$rolesdump)) {
4257: if ($entry!~/^rolesdef_/) {
4258: my ($area,$role)=split(/=/,$entry);
1.587 albertel 4259: $area=~s/\_\w\w$//;
1.678 raeburn 4260: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 4261: if ($role=~/^cr/) {
1.807 albertel 4262: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
4263: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 4264: ($tend,$tstart)=split('_',$trest);
4265: } else {
4266: $trole=$role;
4267: }
1.678 raeburn 4268: } elsif ($role =~ m|^gr/|) {
4269: ($trole,$tend,$tstart) = split(/_/,$role);
4270: ($trole,$group_privs) = split(/\//,$trole);
4271: $group_privs = &unescape($group_privs);
1.587 albertel 4272: } else {
4273: ($trole,$tend,$tstart)=split(/_/,$role);
4274: }
1.743 albertel 4275: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
4276: $username);
4277: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 4278: if (($tend!=0) && ($tend<$now)) { $trole=''; }
4279: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 4280: if (($area ne '') && ($trole ne '')) {
1.347 albertel 4281: my $spec=$trole.'.'.$area;
4282: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
4283: if ($trole =~ /^cr\//) {
1.567 raeburn 4284: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 4285: } elsif ($trole eq 'gr') {
4286: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 4287: } else {
1.567 raeburn 4288: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 4289: }
1.12 www 4290: }
1.662 raeburn 4291: }
1.191 harris41 4292: }
1.743 albertel 4293: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
4294: $userroles{'user.adv'} = $adv;
4295: $userroles{'user.author'} = $author;
1.620 albertel 4296: $env{'user.adv'}=$adv;
1.11 www 4297: }
1.743 albertel 4298: return \%userroles;
1.11 www 4299: }
4300:
1.567 raeburn 4301: sub set_arearole {
4302: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
4303: # log the associated role with the area
4304: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 4305: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 4306: }
4307:
4308: sub custom_roleprivs {
4309: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
4310: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
4311: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 4312: if (&hostname($homsvr) ne '') {
1.567 raeburn 4313: my ($rdummy,$roledef)=
4314: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
4315: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
4316: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
4317: if (defined($syspriv)) {
1.1043 raeburn 4318: if ($trest =~ /^$match_community$/) {
4319: $syspriv =~ s/bre\&S//;
4320: }
1.567 raeburn 4321: $$allroles{'cm./'}.=':'.$syspriv;
4322: $$allroles{$spec.'./'}.=':'.$syspriv;
4323: }
4324: if ($tdomain ne '') {
4325: if (defined($dompriv)) {
4326: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
4327: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
4328: }
4329: if (($trest ne '') && (defined($coursepriv))) {
4330: $$allroles{'cm.'.$area}.=':'.$coursepriv;
4331: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
4332: }
4333: }
4334: }
4335: }
4336: }
4337:
1.678 raeburn 4338: sub group_roleprivs {
4339: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
4340: my $access = 1;
4341: my $now = time;
4342: if (($tend!=0) && ($tend<$now)) { $access = 0; }
4343: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
4344: if ($access) {
1.811 albertel 4345: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 4346: $$allgroups{$course}{$group} .=':'.$group_privs;
4347: }
4348: }
1.567 raeburn 4349:
4350: sub standard_roleprivs {
4351: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
4352: if (defined($pr{$trole.':s'})) {
4353: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
4354: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
4355: }
4356: if ($tdomain ne '') {
4357: if (defined($pr{$trole.':d'})) {
4358: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4359: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4360: }
4361: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
4362: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
4363: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
4364: }
4365: }
4366: }
4367:
4368: sub set_userprivs {
1.1056.4.2 raeburn 4369: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 4370: my $author=0;
4371: my $adv=0;
1.678 raeburn 4372: my %grouproles = ();
4373: if (keys(%{$allgroups}) > 0) {
1.1056.4.2 raeburn 4374: my @groupkeys;
1.1000 raeburn 4375: foreach my $role (keys(%{$allroles})) {
1.1056.4.2 raeburn 4376: push(@groupkeys,$role);
4377: }
4378: if (ref($groups_roles) eq 'HASH') {
4379: foreach my $key (keys(%{$groups_roles})) {
4380: unless (grep(/^\Q$key\E$/,@groupkeys)) {
4381: push(@groupkeys,$key);
4382: }
4383: }
4384: }
4385: if (@groupkeys > 0) {
4386: foreach my $role (@groupkeys) {
4387: my ($trole,$area,$sec,$extendedarea);
4388: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
4389: $trole = $1;
4390: $area = $2;
4391: $sec = $3;
4392: $extendedarea = $area.$sec;
4393: if (exists($$allgroups{$area})) {
4394: foreach my $group (keys(%{$$allgroups{$area}})) {
4395: my $spec = $trole.'.'.$extendedarea;
4396: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 4397: $$allgroups{$area}{$group};
1.1056.4.2 raeburn 4398: }
1.678 raeburn 4399: }
4400: }
4401: }
4402: }
4403: }
1.800 albertel 4404: foreach my $group (keys(%grouproles)) {
4405: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 4406: }
1.800 albertel 4407: foreach my $role (keys(%{$allroles})) {
4408: my %thesepriv;
1.941 raeburn 4409: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 4410: foreach my $item (split(/:/,$$allroles{$role})) {
4411: if ($item ne '') {
4412: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 4413: if ($restrictions eq '') {
4414: $thesepriv{$privilege}='F';
4415: } elsif ($thesepriv{$privilege} ne 'F') {
4416: $thesepriv{$privilege}.=$restrictions;
4417: }
4418: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
4419: }
4420: }
4421: my $thesestr='';
1.800 albertel 4422: foreach my $priv (keys(%thesepriv)) {
4423: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
4424: }
4425: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 4426: }
4427: return ($author,$adv);
4428: }
4429:
1.994 raeburn 4430: sub role_status {
1.1002 raeburn 4431: my ($rolekey,$then,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 4432: my @pwhere = ();
4433: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
4434: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
4435: unless (!defined($$role) || $$role eq '') {
4436: $$where=join('.',@pwhere);
4437: $$trolecode=$$role.'.'.$$where;
4438: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
4439: $$tstatus='is';
4440: if ($$tstart && $$tstart>$then) {
4441: $$tstatus='future';
1.1034 raeburn 4442: if ($$tstart<$now) {
4443: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 4444: if (($$where ne '') && ($$role ne '')) {
1.1056.4.2 raeburn 4445: my (%allroles,%allgroups,$group_privs,
4446: %groups_roles,@rolecodes);
1.1002 raeburn 4447: my %userroles = (
4448: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
4449: );
1.1056.4.2 raeburn 4450: @rolecodes = ('cm');
1.1002 raeburn 4451: my $spec=$$role.'.'.$$where;
4452: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
4453: if ($$role =~ /^cr\//) {
4454: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1056.4.2 raeburn 4455: push(@rolecodes,'cr');
1.1002 raeburn 4456: } elsif ($$role eq 'gr') {
1.1056.4.2 raeburn 4457: push(@rolecodes,$$role);
1.1002 raeburn 4458: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
4459: $env{'user.name'});
1.1056.4.2 raeburn 4460: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 4461: (undef,my $group_privs) = split(/\//,$trole);
4462: $group_privs = &unescape($group_privs);
4463: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1056.4.2 raeburn 4464: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
4465: if (keys(%course_roles) > 0) {
4466: my ($tnum) = ($trest =~ /^($match_courseid)/);
4467: if ($tdomain ne '' && $tnum ne '') {
4468: foreach my $key (keys(%course_roles)) {
4469: if ($key =~ /^\Q$tnum\E:\Q$tdomain\E:([^:]+):?([^:]*)/) {
4470: my $crsrole = $1;
4471: my $crssec = $2;
4472: if ($crsrole =~ /^cr/) {
4473: unless (grep(/^cr$/,@rolecodes)) {
4474: push(@rolecodes,'cr');
4475: }
4476: } else {
4477: unless(grep(/^\Q$crsrole\E$/,@rolecodes)) {
4478: push(@rolecodes,$crsrole);
4479: }
4480: }
4481: my $rolekey = $crsrole.'./'.$tdomain.'/'.$tnum;
4482: if ($crssec ne '') {
4483: $rolekey .= '/'.$crssec;
4484: }
4485: $rolekey .= './';
4486: $groups_roles{$rolekey} = \@rolecodes;
4487: }
4488: }
4489: }
4490: }
1.1002 raeburn 4491: } else {
1.1056.4.2 raeburn 4492: push(@rolecodes,$$role);
1.1002 raeburn 4493: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
4494: }
1.1056.4.2 raeburn 4495: my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
4496: &appenv(\%userroles,\@rolecodes);
1.1002 raeburn 4497: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
4498: }
4499: }
1.1034 raeburn 4500: $$tstatus = 'is';
1.1002 raeburn 4501: }
1.994 raeburn 4502: }
4503: if ($$tend) {
4504: if ($$tend<$then) {
4505: $$tstatus='expired';
4506: } elsif ($$tend<$now) {
4507: $$tstatus='will_not';
4508: }
4509: }
4510: }
4511: }
4512: }
4513:
4514: sub check_adhoc_privs {
1.1056.4.12 raeburn 4515: my ($cdom,$cnum,$then,$refresh,$now,$checkrole,$caller) = @_;
1.994 raeburn 4516: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
4517: if ($env{$cckey}) {
4518: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1002 raeburn 4519: &role_status($cckey,$then,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 4520: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1056.4.12 raeburn 4521: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4522: }
4523: } else {
1.1056.4.12 raeburn 4524: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4525: }
4526: }
4527:
4528: sub set_adhoc_privileges {
4529: # role can be cc or ca
1.1056.4.12 raeburn 4530: my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994 raeburn 4531: my $area = '/'.$dcdom.'/'.$pickedcourse;
4532: my $spec = $role.'.'.$area;
4533: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
4534: $env{'user.name'});
4535: my %ccrole = ();
4536: &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
4537: my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
4538: &appenv(\%userroles,[$role,'cm']);
4539: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1056.4.12 raeburn 4540: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
4541: &appenv( {'request.role' => $spec,
4542: 'request.role.domain' => $dcdom,
4543: 'request.course.sec' => ''
4544: }
4545: );
4546: my $tadv=0;
4547: if (&allowed('adv') eq 'F') { $tadv=1; }
4548: &appenv({'request.role.adv' => $tadv});
4549: }
1.994 raeburn 4550: }
4551:
1.12 www 4552: # --------------------------------------------------------------- get interface
4553:
4554: sub get {
1.131 albertel 4555: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4556: my $items='';
1.800 albertel 4557: foreach my $item (@$storearr) {
4558: $items.=&escape($item).'&';
1.191 harris41 4559: }
1.12 www 4560: $items=~s/\&$//;
1.620 albertel 4561: if (!$udomain) { $udomain=$env{'user.domain'}; }
4562: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 4563: my $uhome=&homeserver($uname,$udomain);
4564:
1.133 albertel 4565: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4566: my @pairs=split(/\&/,$rep);
1.273 albertel 4567: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
4568: return @pairs;
4569: }
1.15 www 4570: my %returnhash=();
1.42 www 4571: my $i=0;
1.800 albertel 4572: foreach my $item (@$storearr) {
4573: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4574: $i++;
1.191 harris41 4575: }
1.15 www 4576: return %returnhash;
1.27 www 4577: }
4578:
4579: # --------------------------------------------------------------- del interface
4580:
4581: sub del {
1.133 albertel 4582: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 4583: my $items='';
1.800 albertel 4584: foreach my $item (@$storearr) {
4585: $items.=&escape($item).'&';
1.191 harris41 4586: }
1.984 neumanie 4587:
1.27 www 4588: $items=~s/\&$//;
1.620 albertel 4589: if (!$udomain) { $udomain=$env{'user.domain'}; }
4590: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4591: my $uhome=&homeserver($uname,$udomain);
4592: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4593: }
4594:
4595: # -------------------------------------------------------------- dump interface
4596:
4597: sub dump {
1.1056.4.10 raeburn 4598: my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755 albertel 4599: if (!$udomain) { $udomain=$env{'user.domain'}; }
4600: if (!$uname) { $uname=$env{'user.name'}; }
4601: my $uhome=&homeserver($uname,$udomain);
4602: if ($regexp) {
4603: $regexp=&escape($regexp);
4604: } else {
4605: $regexp='.';
4606: }
1.1056.4.10 raeburn 4607: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755 albertel 4608: my @pairs=split(/\&/,$rep);
4609: my %returnhash=();
4610: foreach my $item (@pairs) {
4611: my ($key,$value)=split(/=/,$item,2);
4612: $key = &unescape($key);
4613: next if ($key =~ /^error: 2 /);
4614: $returnhash{$key}=&thaw_unescape($value);
4615: }
4616: return %returnhash;
1.407 www 4617: }
4618:
1.717 albertel 4619: # --------------------------------------------------------- dumpstore interface
4620:
4621: sub dumpstore {
4622: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 4623: if (!$udomain) { $udomain=$env{'user.domain'}; }
4624: if (!$uname) { $uname=$env{'user.name'}; }
4625: my $uhome=&homeserver($uname,$udomain);
4626: if ($regexp) {
4627: $regexp=&escape($regexp);
4628: } else {
4629: $regexp='.';
4630: }
4631: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
4632: my @pairs=split(/\&/,$rep);
4633: my %returnhash=();
4634: foreach my $item (@pairs) {
4635: my ($key,$value)=split(/=/,$item,2);
4636: next if ($key =~ /^error: 2 /);
4637: $returnhash{$key}=&thaw_unescape($value);
4638: }
4639: return %returnhash;
1.717 albertel 4640: }
4641:
1.407 www 4642: # -------------------------------------------------------------- keys interface
4643:
4644: sub getkeys {
4645: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 4646: if (!$udomain) { $udomain=$env{'user.domain'}; }
4647: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 4648: my $uhome=&homeserver($uname,$udomain);
4649: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
4650: my @keyarray=();
1.800 albertel 4651: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 4652: next if ($key =~ /^error: 2 /);
1.800 albertel 4653: push(@keyarray,&unescape($key));
1.407 www 4654: }
4655: return @keyarray;
1.318 matthew 4656: }
4657:
1.319 matthew 4658: # --------------------------------------------------------------- currentdump
4659: sub currentdump {
1.328 matthew 4660: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 4661: $courseid = $env{'request.course.id'} if (! defined($courseid));
4662: $sdom = $env{'user.domain'} if (! defined($sdom));
4663: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 4664: my $uhome = &homeserver($sname,$sdom);
4665: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 4666: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 4667: #
1.318 matthew 4668: my %returnhash=();
1.319 matthew 4669: #
4670: if ($rep eq "unknown_cmd") {
4671: # an old lond will not know currentdump
4672: # Do a dump and make it look like a currentdump
1.822 albertel 4673: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 4674: return if ($tmp[0] =~ /^(error:|no_such_host)/);
4675: my %hash = @tmp;
4676: @tmp=();
1.424 matthew 4677: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 4678: } else {
4679: my @pairs=split(/\&/,$rep);
1.800 albertel 4680: foreach my $pair (@pairs) {
4681: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 4682: my ($symb,$param) = split(/:/,$key);
4683: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 4684: &thaw_unescape($value);
1.319 matthew 4685: }
1.191 harris41 4686: }
1.12 www 4687: return %returnhash;
1.424 matthew 4688: }
4689:
4690: sub convert_dump_to_currentdump{
4691: my %hash = %{shift()};
4692: my %returnhash;
4693: # Code ripped from lond, essentially. The only difference
4694: # here is the unescaping done by lonnet::dump(). Conceivably
4695: # we might run in to problems with parameter names =~ /^v\./
4696: while (my ($key,$value) = each(%hash)) {
4697: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 4698: $symb = &unescape($symb);
4699: $param = &unescape($param);
1.424 matthew 4700: next if ($v eq 'version' || $symb eq 'keys');
4701: next if (exists($returnhash{$symb}) &&
4702: exists($returnhash{$symb}->{$param}) &&
4703: $returnhash{$symb}->{'v.'.$param} > $v);
4704: $returnhash{$symb}->{$param}=$value;
4705: $returnhash{$symb}->{'v.'.$param}=$v;
4706: }
4707: #
4708: # Remove all of the keys in the hashes which keep track of
4709: # the version of the parameter.
4710: while (my ($symb,$param_hash) = each(%returnhash)) {
4711: # use a foreach because we are going to delete from the hash.
4712: foreach my $key (keys(%$param_hash)) {
4713: delete($param_hash->{$key}) if ($key =~ /^v\./);
4714: }
4715: }
4716: return \%returnhash;
1.12 www 4717: }
4718:
1.627 albertel 4719: # ------------------------------------------------------ critical inc interface
4720:
4721: sub cinc {
4722: return &inc(@_,'critical');
4723: }
4724:
1.449 matthew 4725: # --------------------------------------------------------------- inc interface
4726:
4727: sub inc {
1.627 albertel 4728: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 4729: if (!$udomain) { $udomain=$env{'user.domain'}; }
4730: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 4731: my $uhome=&homeserver($uname,$udomain);
4732: my $items='';
4733: if (! ref($store)) {
4734: # got a single value, so use that instead
4735: $items = &escape($store).'=&';
4736: } elsif (ref($store) eq 'SCALAR') {
4737: $items = &escape($$store).'=&';
4738: } elsif (ref($store) eq 'ARRAY') {
4739: $items = join('=&',map {&escape($_);} @{$store});
4740: } elsif (ref($store) eq 'HASH') {
4741: while (my($key,$value) = each(%{$store})) {
4742: $items.= &escape($key).'='.&escape($value).'&';
4743: }
4744: }
4745: $items=~s/\&$//;
1.627 albertel 4746: if ($critical) {
4747: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
4748: } else {
4749: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
4750: }
1.449 matthew 4751: }
4752:
1.12 www 4753: # --------------------------------------------------------------- put interface
4754:
4755: sub put {
1.134 albertel 4756: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4757: if (!$udomain) { $udomain=$env{'user.domain'}; }
4758: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4759: my $uhome=&homeserver($uname,$udomain);
1.12 www 4760: my $items='';
1.800 albertel 4761: foreach my $item (keys(%$storehash)) {
4762: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4763: }
1.12 www 4764: $items=~s/\&$//;
1.134 albertel 4765: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 4766: }
4767:
1.631 albertel 4768: # ------------------------------------------------------------ newput interface
4769:
4770: sub newput {
4771: my ($namespace,$storehash,$udomain,$uname)=@_;
4772: if (!$udomain) { $udomain=$env{'user.domain'}; }
4773: if (!$uname) { $uname=$env{'user.name'}; }
4774: my $uhome=&homeserver($uname,$udomain);
4775: my $items='';
4776: foreach my $key (keys(%$storehash)) {
4777: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
4778: }
4779: $items=~s/\&$//;
4780: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
4781: }
4782:
4783: # --------------------------------------------------------- putstore interface
4784:
1.524 raeburn 4785: sub putstore {
1.715 albertel 4786: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 4787: if (!$udomain) { $udomain=$env{'user.domain'}; }
4788: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 4789: my $uhome=&homeserver($uname,$udomain);
4790: my $items='';
1.715 albertel 4791: foreach my $key (keys(%$storehash)) {
4792: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 4793: }
1.715 albertel 4794: $items=~s/\&$//;
1.716 albertel 4795: my $esc_symb=&escape($symb);
4796: my $esc_v=&escape($version);
1.715 albertel 4797: my $reply =
1.716 albertel 4798: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 4799: $uhome);
4800: if ($reply eq 'unknown_cmd') {
1.716 albertel 4801: # gfall back to way things use to be done
1.715 albertel 4802: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
4803: $uname);
1.524 raeburn 4804: }
1.715 albertel 4805: return $reply;
4806: }
4807:
4808: sub old_putstore {
1.716 albertel 4809: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
4810: if (!$udomain) { $udomain=$env{'user.domain'}; }
4811: if (!$uname) { $uname=$env{'user.name'}; }
4812: my $uhome=&homeserver($uname,$udomain);
4813: my %newstorehash;
1.800 albertel 4814: foreach my $item (keys(%$storehash)) {
4815: my $key = $version.':'.&escape($symb).':'.$item;
4816: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 4817: }
4818: my $items='';
4819: my %allitems = ();
1.800 albertel 4820: foreach my $item (keys(%newstorehash)) {
4821: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 4822: my $key = $1.':keys:'.$2;
4823: $allitems{$key} .= $3.':';
4824: }
1.800 albertel 4825: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 4826: }
1.800 albertel 4827: foreach my $item (keys(%allitems)) {
4828: $allitems{$item} =~ s/\:$//;
4829: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 4830: }
4831: $items=~s/\&$//;
4832: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 4833: }
4834:
1.47 www 4835: # ------------------------------------------------------ critical put interface
4836:
4837: sub cput {
1.134 albertel 4838: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4839: if (!$udomain) { $udomain=$env{'user.domain'}; }
4840: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4841: my $uhome=&homeserver($uname,$udomain);
1.47 www 4842: my $items='';
1.800 albertel 4843: foreach my $item (keys(%$storehash)) {
4844: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4845: }
1.47 www 4846: $items=~s/\&$//;
1.134 albertel 4847: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4848: }
4849:
4850: # -------------------------------------------------------------- eget interface
4851:
4852: sub eget {
1.133 albertel 4853: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4854: my $items='';
1.800 albertel 4855: foreach my $item (@$storearr) {
4856: $items.=&escape($item).'&';
1.191 harris41 4857: }
1.12 www 4858: $items=~s/\&$//;
1.620 albertel 4859: if (!$udomain) { $udomain=$env{'user.domain'}; }
4860: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4861: my $uhome=&homeserver($uname,$udomain);
4862: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4863: my @pairs=split(/\&/,$rep);
4864: my %returnhash=();
1.42 www 4865: my $i=0;
1.800 albertel 4866: foreach my $item (@$storearr) {
4867: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4868: $i++;
1.191 harris41 4869: }
1.12 www 4870: return %returnhash;
4871: }
4872:
1.667 albertel 4873: # ------------------------------------------------------------ tmpput interface
4874: sub tmpput {
1.802 raeburn 4875: my ($storehash,$server,$context)=@_;
1.667 albertel 4876: my $items='';
1.800 albertel 4877: foreach my $item (keys(%$storehash)) {
4878: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 4879: }
4880: $items=~s/\&$//;
1.802 raeburn 4881: if (defined($context)) {
4882: $items .= ':'.&escape($context);
4883: }
1.667 albertel 4884: return &reply("tmpput:$items",$server);
4885: }
4886:
4887: # ------------------------------------------------------------ tmpget interface
4888: sub tmpget {
1.688 albertel 4889: my ($token,$server)=@_;
4890: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4891: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 4892: my %returnhash;
4893: foreach my $item (split(/\&/,$rep)) {
4894: my ($key,$value)=split(/=/,$item);
1.951 raeburn 4895: next if ($key =~ /^error: 2 /);
1.667 albertel 4896: $returnhash{&unescape($key)}=&thaw_unescape($value);
4897: }
4898: return %returnhash;
4899: }
4900:
1.688 albertel 4901: # ------------------------------------------------------------ tmpget interface
4902: sub tmpdel {
4903: my ($token,$server)=@_;
4904: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4905: return &reply("tmpdel:$token",$server);
4906: }
4907:
1.765 albertel 4908: # -------------------------------------------------- portfolio access checking
4909:
4910: sub portfolio_access {
1.766 albertel 4911: my ($requrl) = @_;
1.765 albertel 4912: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
4913: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 4914: if ($result) {
4915: my %setters;
4916: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4917: my ($startblock,$endblock) =
4918: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
4919: if ($startblock && $endblock) {
4920: return 'B';
4921: }
4922: } else {
4923: my ($startblock,$endblock) =
4924: &Apache::loncommon::blockcheck(\%setters,'port');
4925: if ($startblock && $endblock) {
4926: return 'B';
4927: }
4928: }
4929: }
1.765 albertel 4930: if ($result eq 'ok') {
1.766 albertel 4931: return 'F';
1.765 albertel 4932: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 4933: return 'A';
1.765 albertel 4934: }
1.766 albertel 4935: return '';
1.765 albertel 4936: }
4937:
4938: sub get_portfolio_access {
1.767 albertel 4939: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
4940:
4941: if (!ref($access_hash)) {
4942: my $current_perms = &get_portfile_permissions($udom,$unum);
4943: my %access_controls = &get_access_controls($current_perms,$group,
4944: $file_name);
4945: $access_hash = $access_controls{$file_name};
4946: }
4947:
1.765 albertel 4948: my ($public,$guest,@domains,@users,@courses,@groups);
4949: my $now = time;
4950: if (ref($access_hash) eq 'HASH') {
4951: foreach my $key (keys(%{$access_hash})) {
4952: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
4953: if ($start > $now) {
4954: next;
4955: }
4956: if ($end && $end<$now) {
4957: next;
4958: }
4959: if ($scope eq 'public') {
4960: $public = $key;
4961: last;
4962: } elsif ($scope eq 'guest') {
4963: $guest = $key;
4964: } elsif ($scope eq 'domains') {
4965: push(@domains,$key);
4966: } elsif ($scope eq 'users') {
4967: push(@users,$key);
4968: } elsif ($scope eq 'course') {
4969: push(@courses,$key);
4970: } elsif ($scope eq 'group') {
4971: push(@groups,$key);
4972: }
4973: }
4974: if ($public) {
4975: return 'ok';
4976: }
4977: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4978: if ($guest) {
4979: return $guest;
4980: }
4981: } else {
4982: if (@domains > 0) {
4983: foreach my $domkey (@domains) {
4984: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
4985: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
4986: return 'ok';
4987: }
4988: }
4989: }
4990: }
4991: if (@users > 0) {
4992: foreach my $userkey (@users) {
1.865 raeburn 4993: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
4994: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
4995: if (ref($item) eq 'HASH') {
4996: if (($item->{'uname'} eq $env{'user.name'}) &&
4997: ($item->{'udom'} eq $env{'user.domain'})) {
4998: return 'ok';
4999: }
5000: }
5001: }
5002: }
1.765 albertel 5003: }
5004: }
5005: my %roleshash;
5006: my @courses_and_groups = @courses;
5007: push(@courses_and_groups,@groups);
5008: if (@courses_and_groups > 0) {
5009: my (%allgroups,%allroles);
5010: my ($start,$end,$role,$sec,$group);
5011: foreach my $envkey (%env) {
1.1056.4.1 raeburn 5012: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5013: my $cid = $2.'_'.$3;
5014: if ($1 eq 'gr') {
5015: $group = $4;
5016: $allgroups{$cid}{$group} = $env{$envkey};
5017: } else {
5018: if ($4 eq '') {
5019: $sec = 'none';
5020: } else {
5021: $sec = $4;
5022: }
5023: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5024: }
1.811 albertel 5025: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5026: my $cid = $2.'_'.$3;
5027: if ($4 eq '') {
5028: $sec = 'none';
5029: } else {
5030: $sec = $4;
5031: }
5032: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5033: }
5034: }
5035: if (keys(%allroles) == 0) {
5036: return;
5037: }
5038: foreach my $key (@courses_and_groups) {
5039: my %content = %{$$access_hash{$key}};
5040: my $cnum = $content{'number'};
5041: my $cdom = $content{'domain'};
5042: my $cid = $cdom.'_'.$cnum;
5043: if (!exists($allroles{$cid})) {
5044: next;
5045: }
5046: foreach my $role_id (keys(%{$content{'roles'}})) {
5047: my @sections = @{$content{'roles'}{$role_id}{'section'}};
5048: my @groups = @{$content{'roles'}{$role_id}{'group'}};
5049: my @status = @{$content{'roles'}{$role_id}{'access'}};
5050: my @roles = @{$content{'roles'}{$role_id}{'role'}};
5051: foreach my $role (keys(%{$allroles{$cid}})) {
5052: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
5053: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
5054: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
5055: if (grep/^all$/,@sections) {
5056: return 'ok';
5057: } else {
5058: if (grep/^$sec$/,@sections) {
5059: return 'ok';
5060: }
5061: }
5062: }
5063: }
5064: if (keys(%{$allgroups{$cid}}) == 0) {
5065: if (grep/^none$/,@groups) {
5066: return 'ok';
5067: }
5068: } else {
5069: if (grep/^all$/,@groups) {
5070: return 'ok';
5071: }
5072: foreach my $group (keys(%{$allgroups{$cid}})) {
5073: if (grep/^$group$/,@groups) {
5074: return 'ok';
5075: }
5076: }
5077: }
5078: }
5079: }
5080: }
5081: }
5082: }
5083: if ($guest) {
5084: return $guest;
5085: }
5086: }
5087: }
5088: return;
5089: }
5090:
5091: sub course_group_datechecker {
5092: my ($dates,$now,$status) = @_;
5093: my ($start,$end) = split(/\./,$dates);
5094: if (!$start && !$end) {
5095: return 'ok';
5096: }
5097: if (grep/^active$/,@{$status}) {
5098: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
5099: return 'ok';
5100: }
5101: }
5102: if (grep/^previous$/,@{$status}) {
5103: if ($end > $now ) {
5104: return 'ok';
5105: }
5106: }
5107: if (grep/^future$/,@{$status}) {
5108: if ($start > $now) {
5109: return 'ok';
5110: }
5111: }
5112: return;
5113: }
5114:
5115: sub parse_portfolio_url {
5116: my ($url) = @_;
5117:
5118: my ($type,$udom,$unum,$group,$file_name);
5119:
1.823 albertel 5120: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 5121: $type = 1;
5122: $udom = $1;
5123: $unum = $2;
5124: $file_name = $3;
1.823 albertel 5125: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 5126: $type = 2;
5127: $udom = $1;
5128: $unum = $2;
5129: $group = $3;
5130: $file_name = $3.'/'.$4;
5131: }
5132: if (wantarray) {
5133: return ($type,$udom,$unum,$file_name,$group);
5134: }
5135: return $type;
5136: }
5137:
5138: sub is_portfolio_url {
5139: my ($url) = @_;
5140: return scalar(&parse_portfolio_url($url));
5141: }
5142:
1.798 raeburn 5143: sub is_portfolio_file {
5144: my ($file) = @_;
1.820 raeburn 5145: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 5146: return 1;
5147: }
5148: return;
5149: }
5150:
1.976 raeburn 5151: sub usertools_access {
1.1056.4.9 raeburn 5152: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref) = @_;
1.985 raeburn 5153: my ($access,%tools);
5154: if ($context eq '') {
5155: $context = 'tools';
5156: }
5157: if ($context eq 'requestcourses') {
5158: %tools = (
5159: official => 1,
5160: unofficial => 1,
1.1006 raeburn 5161: community => 1,
1.985 raeburn 5162: );
5163: } else {
5164: %tools = (
5165: aboutme => 1,
5166: blog => 1,
5167: portfolio => 1,
5168: );
5169: }
1.976 raeburn 5170: return if (!defined($tools{$tool}));
5171:
5172: if ((!defined($udom)) || (!defined($uname))) {
5173: $udom = $env{'user.domain'};
5174: $uname = $env{'user.name'};
5175: }
5176:
1.978 raeburn 5177: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5178: if ($action ne 'reload') {
1.985 raeburn 5179: if ($context eq 'requestcourses') {
5180: return $env{'environment.canrequest.'.$tool};
5181: } else {
5182: return $env{'environment.availabletools.'.$tool};
5183: }
5184: }
1.976 raeburn 5185: }
5186:
5187: my ($toolstatus,$inststatus);
5188:
1.985 raeburn 5189: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
5190: ($action ne 'reload')) {
5191: $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976 raeburn 5192: $inststatus = $env{'environment.inststatus'};
5193: } else {
1.1056.4.9 raeburn 5194: if (ref($userenvref) eq 'HASH') {
5195: $toolstatus = $userenvref->{$context.'.'.$tool};
5196: $inststatus = $userenvref->{'inststatus'};
5197: } else {
5198: my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
5199: $toolstatus = $userenv{$context.'.'.$tool};
5200: $inststatus = $userenv{'inststatus'};
5201: }
1.976 raeburn 5202: }
5203:
5204: if ($toolstatus ne '') {
5205: if ($toolstatus) {
5206: $access = 1;
5207: } else {
5208: $access = 0;
5209: }
5210: return $access;
5211: }
5212:
1.1056.4.9 raeburn 5213: my ($is_adv,%domdef);
5214: if (ref($is_advref) eq 'HASH') {
5215: $is_adv = $is_advref->{'is_adv'};
5216: } else {
5217: $is_adv = &is_advanced_user($udom,$uname);
5218: }
5219: if (ref($domdefref) eq 'HASH') {
5220: %domdef = %{$domdefref};
5221: } else {
5222: %domdef = &get_domain_defaults($udom);
5223: }
1.976 raeburn 5224: if (ref($domdef{$tool}) eq 'HASH') {
5225: if ($is_adv) {
5226: if ($domdef{$tool}{'_LC_adv'} ne '') {
5227: if ($domdef{$tool}{'_LC_adv'}) {
5228: $access = 1;
5229: } else {
5230: $access = 0;
5231: }
5232: return $access;
5233: }
5234: }
5235: if ($inststatus ne '') {
5236: my ($hasaccess,$hasnoaccess);
5237: foreach my $affiliation (split(/:/,$inststatus)) {
5238: if ($domdef{$tool}{$affiliation} ne '') {
5239: if ($domdef{$tool}{$affiliation}) {
5240: $hasaccess = 1;
5241: } else {
5242: $hasnoaccess = 1;
5243: }
5244: }
5245: }
5246: if ($hasaccess || $hasnoaccess) {
5247: if ($hasaccess) {
5248: $access = 1;
5249: } elsif ($hasnoaccess) {
5250: $access = 0;
5251: }
5252: return $access;
5253: }
5254: } else {
5255: if ($domdef{$tool}{'default'} ne '') {
5256: if ($domdef{$tool}{'default'}) {
5257: $access = 1;
5258: } elsif ($domdef{$tool}{'default'} == 0) {
5259: $access = 0;
5260: }
5261: return $access;
5262: }
5263: }
5264: } else {
1.985 raeburn 5265: if ($context eq 'tools') {
5266: $access = 1;
5267: } else {
5268: $access = 0;
5269: }
1.976 raeburn 5270: return $access;
5271: }
5272: }
5273:
1.1050 raeburn 5274: sub is_course_owner {
5275: my ($cdom,$cnum,$udom,$uname) = @_;
5276: if (($udom eq '') || ($uname eq '')) {
5277: $udom = $env{'user.domain'};
5278: $uname = $env{'user.name'};
5279: }
5280: unless (($udom eq '') || ($uname eq '')) {
5281: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
5282: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
5283: return 1;
5284: } else {
5285: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
5286: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
5287: return 1;
5288: }
5289: }
5290: }
5291: }
5292: return;
5293: }
5294:
1.976 raeburn 5295: sub is_advanced_user {
5296: my ($udom,$uname) = @_;
1.1056.4.10 raeburn 5297: if ($udom ne '' && $uname ne '') {
5298: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5299: return $env{'user.adv'};
5300: }
5301: }
1.976 raeburn 5302: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
5303: my %allroles;
5304: my $is_adv;
5305: foreach my $role (keys(%roleshash)) {
5306: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
5307: my $area = '/'.$tdomain.'/'.$trest;
5308: if ($sec ne '') {
5309: $area .= '/'.$sec;
5310: }
5311: if (($area ne '') && ($trole ne '')) {
5312: my $spec=$trole.'.'.$area;
5313: if ($trole =~ /^cr\//) {
5314: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5315: } elsif ($trole ne 'gr') {
5316: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5317: }
5318: }
5319: }
5320: foreach my $role (keys(%allroles)) {
5321: last if ($is_adv);
5322: foreach my $item (split(/:/,$allroles{$role})) {
5323: if ($item ne '') {
5324: my ($privilege,$restrictions)=split(/&/,$item);
5325: if ($privilege eq 'adv') {
5326: $is_adv = 1;
5327: last;
5328: }
5329: }
5330: }
5331: }
5332: return $is_adv;
5333: }
1.798 raeburn 5334:
1.1035 raeburn 5335: sub check_can_request {
1.1036 raeburn 5336: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 5337: my $canreq = 0;
5338: my ($types,$typename) = &Apache::loncommon::course_types();
5339: my @options = ('approval','validate','autolimit');
5340: my $optregex = join('|',@options);
5341: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
5342: foreach my $type (@{$types}) {
5343: if (&usertools_access($env{'user.name'},
5344: $env{'user.domain'},
5345: $type,undef,'requestcourses')) {
5346: $canreq ++;
1.1036 raeburn 5347: if (ref($request_domains) eq 'HASH') {
5348: push(@{$request_domains->{$type}},$env{'user.domain'});
5349: }
1.1035 raeburn 5350: if ($dom eq $env{'user.domain'}) {
5351: $can_request->{$type} = 1;
5352: }
5353: }
5354: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
5355: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
5356: if (@curr > 0) {
1.1036 raeburn 5357: foreach my $item (@curr) {
5358: if (ref($request_domains) eq 'HASH') {
5359: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
5360: if ($otherdom ne '') {
5361: if (ref($request_domains->{$type}) eq 'ARRAY') {
5362: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
5363: push(@{$request_domains->{$type}},$otherdom);
5364: }
5365: } else {
5366: push(@{$request_domains->{$type}},$otherdom);
5367: }
5368: }
5369: }
5370: }
5371: unless($dom eq $env{'user.domain'}) {
5372: $canreq ++;
1.1035 raeburn 5373: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
5374: $can_request->{$type} = 1;
5375: }
5376: }
5377: }
5378: }
5379: }
5380: }
5381: return $canreq;
5382: }
5383:
1.341 www 5384: # ---------------------------------------------- Custom access rule evaluation
5385:
5386: sub customaccess {
5387: my ($priv,$uri)=@_;
1.807 albertel 5388: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 5389: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 5390: $udom = &LONCAPA::clean_domain($udom);
5391: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 5392: my $access=0;
1.800 albertel 5393: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 5394: my ($effect,$realm,$role,$type)=split(/\:/,$right);
5395: if ($type eq 'user') {
5396: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 5397: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 5398: if ($tdom) {
5399: if ($tdom ne $env{'user.domain'}) { next; }
5400: }
1.896 albertel 5401: if ($tuname) {
5402: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 5403: }
5404: $access=($effect eq 'allow');
5405: last;
5406: }
5407: } else {
5408: if ($role) {
5409: if ($role ne $urole) { next; }
5410: }
5411: foreach my $scope (split(/\s*\,\s*/,$realm)) {
5412: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
5413: if ($tdom) {
5414: if ($tdom ne $udom) { next; }
5415: }
5416: if ($tcrs) {
5417: if ($tcrs ne $ucrs) { next; }
5418: }
5419: if ($tsec) {
5420: if ($tsec ne $usec) { next; }
5421: }
5422: $access=($effect eq 'allow');
5423: last;
5424: }
5425: if ($realm eq '' && $role eq '') {
5426: $access=($effect eq 'allow');
5427: }
1.402 bowersj2 5428: }
1.341 www 5429: }
5430: return $access;
5431: }
5432:
1.103 harris41 5433: # ------------------------------------------------- Check for a user privilege
1.12 www 5434:
5435: sub allowed {
1.810 raeburn 5436: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 5437: my $ver_orguri=$uri;
1.439 www 5438: $uri=&deversion($uri);
1.152 www 5439: my $orguri=$uri;
1.52 www 5440: $uri=&declutter($uri);
1.809 raeburn 5441:
1.810 raeburn 5442: if ($priv eq 'evb') {
5443: # Evade communication block restrictions for specified role in a course
5444: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
5445: return $1;
5446: } else {
5447: return;
5448: }
5449: }
5450:
1.620 albertel 5451: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 5452: # Free bre access to adm and meta resources
1.775 albertel 5453: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 5454: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
5455: && ($priv eq 'bre')) {
1.14 www 5456: return 'F';
1.159 www 5457: }
5458:
1.545 banghart 5459: # Free bre access to user's own portfolio contents
1.714 raeburn 5460: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 5461: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 5462: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 5463: my %setters;
5464: my ($startblock,$endblock) =
5465: &Apache::loncommon::blockcheck(\%setters,'port');
5466: if ($startblock && $endblock) {
5467: return 'B';
5468: } else {
5469: return 'F';
5470: }
1.545 banghart 5471: }
5472:
1.762 raeburn 5473: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 5474: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
5475: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
5476: if (exists($env{'request.course.id'})) {
5477: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
5478: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
5479: if (($domain eq $cdom) && ($name eq $cnum)) {
5480: my $courseprivid=$env{'request.course.id'};
5481: $courseprivid=~s/\_/\//;
5482: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
5483: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
5484: return $1;
1.762 raeburn 5485: } else {
5486: if ($env{'request.course.sec'}) {
5487: $courseprivid.='/'.$env{'request.course.sec'};
5488: }
5489: if ($env{'user.priv.'.$env{'request.role'}.'./'.
5490: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
5491: return $2;
5492: }
1.714 raeburn 5493: }
5494: }
5495: }
5496: }
5497:
1.159 www 5498: # Free bre to public access
5499:
5500: if ($priv eq 'bre') {
1.238 www 5501: my $copyright=&metadata($uri,'copyright');
1.620 albertel 5502: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 5503: return 'F';
5504: }
1.238 www 5505: if ($copyright eq 'priv') {
5506: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5507: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 5508: return '';
5509: }
5510: }
5511: if ($copyright eq 'domain') {
5512: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5513: unless (($env{'user.domain'} eq $1) ||
5514: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 5515: return '';
5516: }
1.262 matthew 5517: }
1.620 albertel 5518: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 5519: # Library role, so allow browsing of resources in this domain.
5520: return 'F';
1.238 www 5521: }
1.341 www 5522: if ($copyright eq 'custom') {
5523: unless (&customaccess($priv,$uri)) { return ''; }
5524: }
1.14 www 5525: }
1.264 matthew 5526: # Domain coordinator is trying to create a course
1.620 albertel 5527: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 5528: # uri is the requested domain in this case.
5529: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 5530: # a role of dc for the domain in question.
1.620 albertel 5531: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 5532: }
1.29 www 5533:
1.52 www 5534: my $thisallowed='';
5535: my $statecond=0;
5536: my $courseprivid='';
5537:
1.1039 raeburn 5538: my $ownaccess;
1.1043 raeburn 5539: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 5540: if (($priv eq 'bro') && ($env{'user.author'})) {
5541: if ($uri eq '') {
5542: $ownaccess = 1;
5543: } else {
5544: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
5545: my $udom = $env{'user.domain'};
5546: my $uname = $env{'user.name'};
5547: if ($uri =~ m{^\Q$udom\E/?$}) {
5548: $ownaccess = 1;
1.1040 raeburn 5549: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 5550: unless ($uri =~ m{\.\./}) {
5551: $ownaccess = 1;
5552: }
5553: } elsif (($udom ne 'public') && ($uname ne 'public')) {
5554: my $now = time;
5555: if ($uri =~ m{^([^/]+)/?$}) {
5556: my $adom = $1;
5557: foreach my $key (keys(%env)) {
1.1042 raeburn 5558: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 5559: my ($start,$end) = split('.',$env{$key});
5560: if (($now >= $start) && (!$end || $end < $now)) {
5561: $ownaccess = 1;
5562: last;
5563: }
5564: }
5565: }
5566: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
5567: my $adom = $1;
5568: my $aname = $2;
1.1042 raeburn 5569: foreach my $role ('ca','aa') {
5570: if ($env{"user.role.$role./$adom/$aname"}) {
5571: my ($start,$end) =
5572: split('.',$env{"user.role.$role./$adom/$aname"});
5573: if (($now >= $start) && (!$end || $end < $now)) {
5574: $ownaccess = 1;
5575: last;
5576: }
1.1039 raeburn 5577: }
5578: }
5579: }
5580: }
5581: }
5582: }
5583: }
5584:
1.52 www 5585: # Course
5586:
1.620 albertel 5587: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5588: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5589: $thisallowed.=$1;
5590: }
1.52 www 5591: }
1.29 www 5592:
1.52 www 5593: # Domain
5594:
1.620 albertel 5595: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 5596: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5597: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5598: $thisallowed.=$1;
5599: }
1.12 www 5600: }
1.52 www 5601:
5602: # Course: uri itself is a course
1.66 www 5603: my $courseuri=$uri;
5604: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 5605: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 5606:
1.620 albertel 5607: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 5608: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5609: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5610: $thisallowed.=$1;
5611: }
1.12 www 5612: }
1.29 www 5613:
1.665 albertel 5614: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 5615: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 5616: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 5617: $thisallowed='';
1.671 raeburn 5618: my ($match)=&is_on_map($uri);
5619: if ($match) {
5620: if ($env{'user.priv.'.$env{'request.role'}.'./'}
5621: =~/\Q$priv\E\&([^\:]*)/) {
5622: $thisallowed.=$1;
5623: }
5624: } else {
1.705 albertel 5625: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 5626: if ($refuri) {
5627: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 5628: $thisallowed='F';
1.671 raeburn 5629: } else {
5630: $refuri=&declutter($refuri);
5631: my ($match) = &is_on_map($refuri);
5632: if ($match) {
5633: $thisallowed='F';
5634: }
1.669 raeburn 5635: }
1.671 raeburn 5636: }
5637: }
1.314 www 5638: }
1.492 albertel 5639:
1.766 albertel 5640: if ($priv eq 'bre'
5641: && $thisallowed ne 'F'
5642: && $thisallowed ne '2'
5643: && &is_portfolio_url($uri)) {
5644: $thisallowed = &portfolio_access($uri);
5645: }
5646:
1.52 www 5647: # Full access at system, domain or course-wide level? Exit.
1.29 www 5648: if ($thisallowed=~/F/) {
5649: return 'F';
5650: }
5651:
1.52 www 5652: # If this is generating or modifying users, exit with special codes
1.29 www 5653:
1.643 www 5654: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
5655: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 5656: my ($audom,$auname)=split('/',$uri);
1.643 www 5657: # no author name given, so this just checks on the general right to make a co-author in this domain
5658: unless ($auname) { return $thisallowed; }
5659: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 5660: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
5661: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
5662: ($audom ne $env{'request.role.domain'}))) { return ''; }
5663: }
1.52 www 5664: return $thisallowed;
5665: }
5666: #
1.103 harris41 5667: # Gathered so far: system, domain and course wide privileges
1.52 www 5668: #
5669: # Course: See if uri or referer is an individual resource that is part of
5670: # the course
5671:
1.620 albertel 5672: if ($env{'request.course.id'}) {
1.232 www 5673:
1.620 albertel 5674: $courseprivid=$env{'request.course.id'};
5675: if ($env{'request.course.sec'}) {
5676: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 5677: }
5678: $courseprivid=~s/\_/\//;
5679: my $checkreferer=1;
1.232 www 5680: my ($match,$cond)=&is_on_map($uri);
5681: if ($match) {
5682: $statecond=$cond;
1.620 albertel 5683: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5684: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5685: $thisallowed.=$1;
5686: $checkreferer=0;
5687: }
1.29 www 5688: }
1.83 www 5689:
1.148 www 5690: if ($checkreferer) {
1.620 albertel 5691: my $refuri=$env{'httpref.'.$orguri};
1.148 www 5692: unless ($refuri) {
1.800 albertel 5693: foreach my $key (keys(%env)) {
5694: if ($key=~/^httpref\..*\*/) {
5695: my $pattern=$key;
1.156 www 5696: $pattern=~s/^httpref\.\/res\///;
1.148 www 5697: $pattern=~s/\*/\[\^\/\]\+/g;
5698: $pattern=~s/\//\\\//g;
1.152 www 5699: if ($orguri=~/$pattern/) {
1.800 albertel 5700: $refuri=$env{$key};
1.148 www 5701: }
5702: }
1.191 harris41 5703: }
1.148 www 5704: }
1.232 www 5705:
1.148 www 5706: if ($refuri) {
1.152 www 5707: $refuri=&declutter($refuri);
1.232 www 5708: my ($match,$cond)=&is_on_map($refuri);
5709: if ($match) {
5710: my $refstatecond=$cond;
1.620 albertel 5711: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5712: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5713: $thisallowed.=$1;
1.53 www 5714: $uri=$refuri;
5715: $statecond=$refstatecond;
1.52 www 5716: }
5717: }
1.148 www 5718: }
1.29 www 5719: }
1.52 www 5720: }
1.29 www 5721:
1.52 www 5722: #
1.103 harris41 5723: # Gathered now: all privileges that could apply, and condition number
1.52 www 5724: #
5725: #
5726: # Full or no access?
5727: #
1.29 www 5728:
1.52 www 5729: if ($thisallowed=~/F/) {
5730: return 'F';
5731: }
1.29 www 5732:
1.52 www 5733: unless ($thisallowed) {
5734: return '';
5735: }
1.29 www 5736:
1.52 www 5737: # Restrictions exist, deal with them
5738: #
5739: # C:according to course preferences
5740: # R:according to resource settings
5741: # L:unless locked
5742: # X:according to user session state
5743: #
5744:
5745: # Possibly locked functionality, check all courses
1.54 www 5746: # Locks might take effect only after 10 minutes cache expiration for other
5747: # courses, and 2 minutes for current course
1.52 www 5748:
5749: my $envkey;
5750: if ($thisallowed=~/L/) {
1.1000 raeburn 5751: foreach $envkey (keys(%env)) {
1.54 www 5752: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
5753: my $courseid=$2;
5754: my $roleid=$1.'.'.$2;
1.92 www 5755: $courseid=~s/^\///;
1.54 www 5756: my $expiretime=600;
1.620 albertel 5757: if ($env{'request.role'} eq $roleid) {
1.54 www 5758: $expiretime=120;
5759: }
5760: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
5761: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 5762: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 5763: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 5764: }
1.620 albertel 5765: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
5766: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
5767: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
5768: &log($env{'user.domain'},$env{'user.name'},
5769: $env{'user.home'},
1.57 www 5770: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 5771: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5772: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5773: return '';
5774: }
5775: }
1.620 albertel 5776: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
5777: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
5778: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
5779: &log($env{'user.domain'},$env{'user.name'},
5780: $env{'user.home'},
1.57 www 5781: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 5782: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5783: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5784: return '';
5785: }
5786: }
5787: }
1.29 www 5788: }
1.52 www 5789: }
5790:
5791: #
5792: # Rest of the restrictions depend on selected course
5793: #
5794:
1.620 albertel 5795: unless ($env{'request.course.id'}) {
1.766 albertel 5796: if ($thisallowed eq 'A') {
5797: return 'A';
1.814 raeburn 5798: } elsif ($thisallowed eq 'B') {
5799: return 'B';
1.766 albertel 5800: } else {
5801: return '1';
5802: }
1.52 www 5803: }
1.29 www 5804:
1.52 www 5805: #
5806: # Now user is definitely in a course
5807: #
1.53 www 5808:
5809:
5810: # Course preferences
5811:
5812: if ($thisallowed=~/C/) {
1.620 albertel 5813: my $rolecode=(split(/\./,$env{'request.role'}))[0];
5814: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
5815: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 5816: =~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5817: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5818: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5819: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
5820: $env{'request.course.id'});
5821: }
1.237 www 5822: return '';
5823: }
5824:
1.620 albertel 5825: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 5826: =~/\Q$unamedom\E/) {
1.1056.4.22 raeburn 5827: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5828: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
5829: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
5830: $env{'request.course.id'});
5831: }
1.54 www 5832: return '';
5833: }
1.53 www 5834: }
5835:
5836: # Resource preferences
5837:
5838: if ($thisallowed=~/R/) {
1.620 albertel 5839: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 5840: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5841: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5842: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5843: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
5844: }
5845: return '';
1.54 www 5846: }
1.53 www 5847: }
1.30 www 5848:
1.246 www 5849: # Restricted by state or randomout?
1.30 www 5850:
1.52 www 5851: if ($thisallowed=~/X/) {
1.620 albertel 5852: if ($env{'acc.randomout'}) {
1.579 albertel 5853: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 5854: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 5855: return '';
5856: }
1.247 www 5857: }
5858: if (&condval($statecond)) {
1.52 www 5859: return '2';
5860: } else {
5861: return '';
5862: }
5863: }
1.30 www 5864:
1.766 albertel 5865: if ($thisallowed eq 'A') {
5866: return 'A';
1.814 raeburn 5867: } elsif ($thisallowed eq 'B') {
5868: return 'B';
1.766 albertel 5869: }
1.52 www 5870: return 'F';
1.232 www 5871: }
5872:
1.710 albertel 5873: sub split_uri_for_cond {
5874: my $uri=&deversion(&declutter(shift));
5875: my @uriparts=split(/\//,$uri);
5876: my $filename=pop(@uriparts);
5877: my $pathname=join('/',@uriparts);
5878: return ($pathname,$filename);
5879: }
1.232 www 5880: # --------------------------------------------------- Is a resource on the map?
5881:
5882: sub is_on_map {
1.710 albertel 5883: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 5884: #Trying to find the conditional for the file
1.620 albertel 5885: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 5886: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 5887: if ($match) {
1.289 bowersj2 5888: return (1,$1);
5889: } else {
1.434 www 5890: return (0,0);
1.289 bowersj2 5891: }
1.12 www 5892: }
5893:
1.427 www 5894: # --------------------------------------------------------- Get symb from alias
5895:
5896: sub get_symb_from_alias {
5897: my $symb=shift;
5898: my ($map,$resid,$url)=&decode_symb($symb);
5899: # Already is a symb
5900: if ($url) { return $symb; }
5901: # Must be an alias
5902: my $aliassymb='';
5903: my %bighash;
1.620 albertel 5904: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 5905: &GDBM_READER(),0640)) {
5906: my $rid=$bighash{'mapalias_'.$symb};
5907: if ($rid) {
5908: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 5909: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
5910: $resid,$bighash{'src_'.$rid});
1.427 www 5911: }
5912: untie %bighash;
5913: }
5914: return $aliassymb;
5915: }
5916:
1.12 www 5917: # ----------------------------------------------------------------- Define Role
5918:
5919: sub definerole {
5920: if (allowed('mcr','/')) {
5921: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 5922: foreach my $role (split(':',$sysrole)) {
5923: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5924: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
5925: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
5926: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5927: return "refused:s:$crole&$cqual";
5928: }
5929: }
1.191 harris41 5930: }
1.800 albertel 5931: foreach my $role (split(':',$domrole)) {
5932: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5933: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
5934: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
5935: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 5936: return "refused:d:$crole&$cqual";
5937: }
5938: }
1.191 harris41 5939: }
1.800 albertel 5940: foreach my $role (split(':',$courole)) {
5941: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5942: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
5943: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
5944: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5945: return "refused:c:$crole&$cqual";
5946: }
5947: }
1.191 harris41 5948: }
1.620 albertel 5949: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
5950: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 5951: "rolesdef_$rolename=".
5952: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 5953: return reply($command,$env{'user.home'});
1.12 www 5954: } else {
5955: return 'refused';
5956: }
1.105 harris41 5957: }
5958:
5959: # ---------------- Make a metadata query against the network of library servers
5960:
5961: sub metadata_query {
1.244 matthew 5962: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 5963: my %rhash;
1.845 albertel 5964: my %libserv = &all_library();
1.244 matthew 5965: my @server_list = (defined($server_array) ? @$server_array
5966: : keys(%libserv) );
5967: for my $server (@server_list) {
1.118 harris41 5968: unless ($custom or $customshow) {
5969: my $reply=&reply("querysend:".&escape($query),$server);
5970: $rhash{$server}=$reply;
5971: }
5972: else {
5973: my $reply=&reply("querysend:".&escape($query).':'.
5974: &escape($custom).':'.&escape($customshow),
5975: $server);
5976: $rhash{$server}=$reply;
5977: }
1.112 harris41 5978: }
1.118 harris41 5979: return \%rhash;
1.240 www 5980: }
5981:
5982: # ----------------------------------------- Send log queries and wait for reply
5983:
5984: sub log_query {
5985: my ($uname,$udom,$query,%filters)=@_;
5986: my $uhome=&homeserver($uname,$udom);
5987: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 5988: my $uhost=&hostname($uhome);
1.800 albertel 5989: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 5990: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
5991: $uhome);
1.479 albertel 5992: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 5993: return get_query_reply($queryid);
5994: }
5995:
1.818 raeburn 5996: # -------------------------- Update MySQL table for portfolio file
5997:
5998: sub update_portfolio_table {
1.821 raeburn 5999: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 6000: if ($group ne '') {
6001: $file_name =~s /^\Q$group\E//;
6002: }
1.818 raeburn 6003: my $homeserver = &homeserver($uname,$udom);
6004: my $queryid=
1.821 raeburn 6005: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
6006: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 6007: my $reply = &get_query_reply($queryid);
6008: return $reply;
6009: }
6010:
1.899 raeburn 6011: # -------------------------- Update MySQL allusers table
6012:
6013: sub update_allusers_table {
6014: my ($uname,$udom,$names) = @_;
6015: my $homeserver = &homeserver($uname,$udom);
6016: my $queryid=
6017: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
6018: 'lastname='.&escape($names->{'lastname'}).'%%'.
6019: 'firstname='.&escape($names->{'firstname'}).'%%'.
6020: 'middlename='.&escape($names->{'middlename'}).'%%'.
6021: 'generation='.&escape($names->{'generation'}).'%%'.
6022: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
6023: 'id='.&escape($names->{'id'}),$homeserver);
1.1056.4.4 raeburn 6024: return;
1.899 raeburn 6025: }
6026:
1.508 raeburn 6027: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 6028:
6029: sub fetch_enrollment_query {
1.511 raeburn 6030: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 6031: my $homeserver;
1.547 raeburn 6032: my $maxtries = 1;
1.508 raeburn 6033: if ($context eq 'automated') {
6034: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 6035: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 6036: } else {
6037: $homeserver = &homeserver($cnum,$dom);
6038: }
1.838 albertel 6039: my $host=&hostname($homeserver);
1.506 raeburn 6040: my $cmd = '';
1.1000 raeburn 6041: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 6042: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 6043: }
6044: $cmd =~ s/%%$//;
6045: $cmd = &escape($cmd);
6046: my $query = 'fetchenrollment';
1.620 albertel 6047: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 6048: unless ($queryid=~/^\Q$host\E\_/) {
6049: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
6050: return 'error: '.$queryid;
6051: }
1.506 raeburn 6052: my $reply = &get_query_reply($queryid);
1.547 raeburn 6053: my $tries = 1;
6054: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6055: $reply = &get_query_reply($queryid);
6056: $tries ++;
6057: }
1.526 raeburn 6058: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 6059: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 6060: } else {
1.901 albertel 6061: my @responses = split(/:/,$reply);
1.515 raeburn 6062: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 6063: foreach my $line (@responses) {
6064: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 6065: $$replyref{$key} = $value;
6066: }
6067: } else {
1.506 raeburn 6068: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 6069: foreach my $line (@responses) {
6070: my ($key,$value) = split(/=/,$line);
1.506 raeburn 6071: $$replyref{$key} = $value;
6072: if ($value > 0) {
1.800 albertel 6073: foreach my $item (@{$$affiliatesref{$key}}) {
6074: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 6075: my $destname = $pathname.'/'.$filename;
6076: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 6077: if ($xml_classlist =~ /^error/) {
6078: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
6079: } else {
1.506 raeburn 6080: if ( open(FILE,">$destname") ) {
6081: print FILE &unescape($xml_classlist);
6082: close(FILE);
1.526 raeburn 6083: } else {
6084: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 6085: }
6086: }
6087: }
6088: }
6089: }
6090: }
6091: return 'ok';
6092: }
6093: return 'error';
6094: }
6095:
1.242 www 6096: sub get_query_reply {
6097: my $queryid=shift;
1.240 www 6098: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
6099: my $reply='';
6100: for (1..100) {
6101: sleep 2;
6102: if (-e $replyfile.'.end') {
1.448 albertel 6103: if (open(my $fh,$replyfile)) {
1.904 albertel 6104: $reply = join('',<$fh>);
6105: close($fh);
1.240 www 6106: } else { return 'error: reply_file_error'; }
1.242 www 6107: return &unescape($reply);
6108: }
1.240 www 6109: }
1.242 www 6110: return 'timeout:'.$queryid;
1.240 www 6111: }
6112:
6113: sub courselog_query {
1.241 www 6114: #
6115: # possible filters:
6116: # url: url or symb
6117: # username
6118: # domain
6119: # action: view, submit, grade
6120: # start: timestamp
6121: # end: timestamp
6122: #
1.240 www 6123: my (%filters)=@_;
1.620 albertel 6124: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 6125: if ($filters{'url'}) {
6126: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
6127: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
6128: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
6129: }
1.620 albertel 6130: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
6131: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 6132: return &log_query($cname,$cdom,'courselog',%filters);
6133: }
6134:
6135: sub userlog_query {
1.858 raeburn 6136: #
6137: # possible filters:
6138: # action: log check role
6139: # start: timestamp
6140: # end: timestamp
6141: #
1.240 www 6142: my ($uname,$udom,%filters)=@_;
6143: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 6144: }
6145:
1.506 raeburn 6146: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
6147:
6148: sub auto_run {
1.508 raeburn 6149: my ($cnum,$cdom) = @_;
1.876 raeburn 6150: my $response = 0;
6151: my $settings;
6152: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
6153: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
6154: $settings = $domconfig{'autoenroll'};
6155: if ($settings->{'run'} eq '1') {
6156: $response = 1;
6157: }
6158: } else {
1.934 raeburn 6159: my $homeserver;
6160: if (&is_course($cdom,$cnum)) {
6161: $homeserver = &homeserver($cnum,$cdom);
6162: } else {
6163: $homeserver = &domain($cdom,'primary');
6164: }
6165: if ($homeserver ne 'no_host') {
6166: $response = &reply('autorun:'.$cdom,$homeserver);
6167: }
1.876 raeburn 6168: }
1.506 raeburn 6169: return $response;
6170: }
1.776 albertel 6171:
1.506 raeburn 6172: sub auto_get_sections {
1.508 raeburn 6173: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 6174: my $homeserver;
6175: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6176: $homeserver = &homeserver($cnum,$cdom);
6177: }
6178: if (!defined($homeserver)) {
6179: if ($cdom =~ /^$match_domain$/) {
6180: $homeserver = &domain($cdom,'primary');
6181: }
6182: }
6183: my @secs;
6184: if (defined($homeserver)) {
6185: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
6186: unless ($response eq 'refused') {
6187: @secs = split(/:/,$response);
6188: }
1.506 raeburn 6189: }
6190: return @secs;
6191: }
1.776 albertel 6192:
1.506 raeburn 6193: sub auto_new_course {
1.1056.4.19 raeburn 6194: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 6195: my $homeserver = &homeserver($cnum,$cdom);
1.1056.4.19 raeburn 6196: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 6197: return $response;
6198: }
1.776 albertel 6199:
1.506 raeburn 6200: sub auto_validate_courseID {
1.508 raeburn 6201: my ($cnum,$cdom,$inst_course_id) = @_;
6202: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 6203: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 6204: return $response;
6205: }
1.776 albertel 6206:
1.1007 raeburn 6207: sub auto_validate_instcode {
1.1020 raeburn 6208: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 6209: my ($homeserver,$response);
6210: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6211: $homeserver = &homeserver($cnum,$cdom);
6212: }
6213: if (!defined($homeserver)) {
6214: if ($cdom =~ /^$match_domain$/) {
6215: $homeserver = &domain($cdom,'primary');
6216: }
6217: }
1.1056.4.2 raeburn 6218: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
6219: &escape($instcode).':'.&escape($owner),$homeserver));
1.1027 raeburn 6220: my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
6221: return ($outcome,$description);
1.1007 raeburn 6222: }
6223:
1.506 raeburn 6224: sub auto_create_password {
1.873 raeburn 6225: my ($cnum,$cdom,$authparam,$udom) = @_;
6226: my ($homeserver,$response);
1.506 raeburn 6227: my $create_passwd = 0;
6228: my $authchk = '';
1.873 raeburn 6229: if ($udom =~ /^$match_domain$/) {
6230: $homeserver = &domain($udom,'primary');
6231: }
6232: if ($homeserver eq '') {
6233: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6234: $homeserver = &homeserver($cnum,$cdom);
6235: }
6236: }
6237: if ($homeserver eq '') {
6238: $authchk = 'nodomain';
1.506 raeburn 6239: } else {
1.873 raeburn 6240: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
6241: if ($response eq 'refused') {
6242: $authchk = 'refused';
6243: } else {
1.901 albertel 6244: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 6245: }
1.506 raeburn 6246: }
6247: return ($authparam,$create_passwd,$authchk);
6248: }
6249:
1.706 raeburn 6250: sub auto_photo_permission {
6251: my ($cnum,$cdom,$students) = @_;
6252: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 6253: my ($outcome,$perm_reqd,$conditions) =
6254: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 6255: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6256: return (undef,undef);
6257: }
1.706 raeburn 6258: return ($outcome,$perm_reqd,$conditions);
6259: }
6260:
6261: sub auto_checkphotos {
6262: my ($uname,$udom,$pid) = @_;
6263: my $homeserver = &homeserver($uname,$udom);
6264: my ($result,$resulttype);
6265: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 6266: &escape($uname).':'.&escape($pid),
6267: $homeserver));
1.709 albertel 6268: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6269: return (undef,undef);
6270: }
1.706 raeburn 6271: if ($outcome) {
6272: ($result,$resulttype) = split(/:/,$outcome);
6273: }
6274: return ($result,$resulttype);
6275: }
6276:
6277: sub auto_photochoice {
6278: my ($cnum,$cdom) = @_;
6279: my $homeserver = &homeserver($cnum,$cdom);
6280: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 6281: &escape($cdom),
6282: $homeserver)));
1.709 albertel 6283: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6284: return (undef,undef);
6285: }
1.706 raeburn 6286: return ($update,$comment);
6287: }
6288:
6289: sub auto_photoupdate {
6290: my ($affiliatesref,$dom,$cnum,$photo) = @_;
6291: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 6292: my $host=&hostname($homeserver);
1.706 raeburn 6293: my $cmd = '';
6294: my $maxtries = 1;
1.800 albertel 6295: foreach my $affiliate (keys(%{$affiliatesref})) {
6296: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 6297: }
6298: $cmd =~ s/%%$//;
6299: $cmd = &escape($cmd);
6300: my $query = 'institutionalphotos';
6301: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
6302: unless ($queryid=~/^\Q$host\E\_/) {
6303: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
6304: return 'error: '.$queryid;
6305: }
6306: my $reply = &get_query_reply($queryid);
6307: my $tries = 1;
6308: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6309: $reply = &get_query_reply($queryid);
6310: $tries ++;
6311: }
6312: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
6313: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
6314: } else {
6315: my @responses = split(/:/,$reply);
6316: my $outcome = shift(@responses);
6317: foreach my $item (@responses) {
6318: my ($key,$value) = split(/=/,$item);
6319: $$photo{$key} = $value;
6320: }
6321: return $outcome;
6322: }
6323: return 'error';
6324: }
6325:
1.521 raeburn 6326: sub auto_instcode_format {
1.793 albertel 6327: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
6328: $cat_order) = @_;
1.521 raeburn 6329: my $courses = '';
1.772 raeburn 6330: my @homeservers;
1.521 raeburn 6331: if ($caller eq 'global') {
1.841 albertel 6332: my %servers = &get_servers($codedom,'library');
6333: foreach my $tryserver (keys(%servers)) {
6334: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6335: push(@homeservers,$tryserver);
6336: }
1.584 raeburn 6337: }
1.1022 raeburn 6338: } elsif ($caller eq 'requests') {
6339: if ($codedom =~ /^$match_domain$/) {
6340: my $chome = &domain($codedom,'primary');
6341: unless ($chome eq 'no_host') {
6342: push(@homeservers,$chome);
6343: }
6344: }
1.521 raeburn 6345: } else {
1.772 raeburn 6346: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 6347: }
1.793 albertel 6348: foreach my $code (keys(%{$instcodes})) {
6349: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 6350: }
6351: chop($courses);
1.772 raeburn 6352: my $ok_response = 0;
6353: my $response;
6354: while (@homeservers > 0 && $ok_response == 0) {
6355: my $server = shift(@homeservers);
6356: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
6357: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
6358: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 6359: split(/:/,$response);
1.772 raeburn 6360: %{$codes} = (%{$codes},&str2hash($codes_str));
6361: push(@{$codetitles},&str2array($codetitles_str));
6362: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
6363: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
6364: $ok_response = 1;
6365: }
6366: }
6367: if ($ok_response) {
1.521 raeburn 6368: return 'ok';
1.772 raeburn 6369: } else {
6370: return $response;
1.521 raeburn 6371: }
6372: }
6373:
1.792 raeburn 6374: sub auto_instcode_defaults {
6375: my ($domain,$returnhash,$code_order) = @_;
6376: my @homeservers;
1.841 albertel 6377:
6378: my %servers = &get_servers($domain,'library');
6379: foreach my $tryserver (keys(%servers)) {
6380: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6381: push(@homeservers,$tryserver);
6382: }
1.792 raeburn 6383: }
1.841 albertel 6384:
1.792 raeburn 6385: my $response;
1.841 albertel 6386: foreach my $server (@homeservers) {
1.792 raeburn 6387: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 6388: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
6389:
6390: foreach my $pair (split(/\&/,$response)) {
6391: my ($name,$value)=split(/\=/,$pair);
6392: if ($name eq 'code_order') {
6393: @{$code_order} = split(/\&/,&unescape($value));
6394: } else {
6395: $returnhash->{&unescape($name)}=&unescape($value);
6396: }
6397: }
6398: return 'ok';
1.792 raeburn 6399: }
1.841 albertel 6400:
6401: return $response;
1.1003 raeburn 6402: }
6403:
6404: sub auto_possible_instcodes {
1.1007 raeburn 6405: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
6406: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
6407: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
6408: return;
6409: }
1.1003 raeburn 6410: my (@homeservers,$uhome);
6411: if (defined(&domain($domain,'primary'))) {
6412: $uhome=&domain($domain,'primary');
6413: push(@homeservers,&domain($domain,'primary'));
6414: } else {
6415: my %servers = &get_servers($domain,'library');
6416: foreach my $tryserver (keys(%servers)) {
6417: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6418: push(@homeservers,$tryserver);
6419: }
6420: }
6421: }
6422: my $response;
6423: foreach my $server (@homeservers) {
6424: $response=&reply('autopossibleinstcodes:'.$domain,$server);
6425: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 6426: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
6427: split(':',$response);
6428: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
6429: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 6430: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 6431: my ($name,$value)=split('=',$item);
6432: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6433: }
6434: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 6435: my ($name,$value)=split('=',$item);
6436: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6437: }
6438: return 'ok';
6439: }
6440: return $response;
6441: }
1.792 raeburn 6442:
1.1010 raeburn 6443: sub auto_courserequest_checks {
6444: my ($dom) = @_;
1.1020 raeburn 6445: my ($homeserver,%validations);
6446: if ($dom =~ /^$match_domain$/) {
6447: $homeserver = &domain($dom,'primary');
6448: }
6449: unless ($homeserver eq 'no_host') {
6450: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
6451: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
6452: my @items = split(/&/,$response);
6453: foreach my $item (@items) {
6454: my ($key,$value) = split('=',$item);
6455: $validations{&unescape($key)} = &thaw_unescape($value);
6456: }
6457: }
6458: }
1.1010 raeburn 6459: return %validations;
6460: }
6461:
1.1020 raeburn 6462: sub auto_courserequest_validation {
6463: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
6464: my ($homeserver,$response);
6465: if ($dom =~ /^$match_domain$/) {
6466: $homeserver = &domain($dom,'primary');
6467: }
6468: unless ($homeserver eq 'no_host') {
1.1021 raeburn 6469:
1.1020 raeburn 6470: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 6471: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020 raeburn 6472: ':'.&escape($instcode).':'.&escape($instseclist),
6473: $homeserver));
6474: }
6475: return $response;
6476: }
6477:
1.777 albertel 6478: sub auto_validate_class_sec {
1.918 raeburn 6479: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 6480: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 6481: my $ownerlist;
6482: if (ref($owners) eq 'ARRAY') {
6483: $ownerlist = join(',',@{$owners});
6484: } else {
6485: $ownerlist = $owners;
6486: }
1.773 raeburn 6487: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 6488: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 6489: return $response;
6490: }
6491:
1.679 raeburn 6492: # ------------------------------------------------------- Course Group routines
6493:
6494: sub get_coursegroups {
1.809 raeburn 6495: my ($cdom,$cnum,$group,$namespace) = @_;
6496: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 6497: }
6498:
1.679 raeburn 6499: sub modify_coursegroup {
6500: my ($cdom,$cnum,$groupsettings) = @_;
6501: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
6502: }
6503:
1.809 raeburn 6504: sub toggle_coursegroup_status {
6505: my ($cdom,$cnum,$group,$action) = @_;
6506: my ($from_namespace,$to_namespace);
6507: if ($action eq 'delete') {
6508: $from_namespace = 'coursegroups';
6509: $to_namespace = 'deleted_groups';
6510: } else {
6511: $from_namespace = 'deleted_groups';
6512: $to_namespace = 'coursegroups';
6513: }
6514: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 6515: if (my $tmp = &error(%curr_group)) {
6516: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
6517: return ('read error',$tmp);
6518: } else {
6519: my %savedsettings = %curr_group;
1.809 raeburn 6520: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 6521: my $deloutcome;
6522: if ($result eq 'ok') {
1.809 raeburn 6523: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 6524: } else {
6525: return ('write error',$result);
6526: }
6527: if ($deloutcome eq 'ok') {
6528: return 'ok';
6529: } else {
6530: return ('delete error',$deloutcome);
6531: }
6532: }
6533: }
6534:
1.679 raeburn 6535: sub modify_group_roles {
1.957 raeburn 6536: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 6537: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
6538: my $role = 'gr/'.&escape($userprivs);
6539: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 6540: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 6541: if ($result eq 'ok') {
6542: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
6543: }
1.679 raeburn 6544: return $result;
6545: }
6546:
6547: sub modify_coursegroup_membership {
6548: my ($cdom,$cnum,$membership) = @_;
6549: my $result = &put('groupmembership',$membership,$cdom,$cnum);
6550: return $result;
6551: }
6552:
1.682 raeburn 6553: sub get_active_groups {
6554: my ($udom,$uname,$cdom,$cnum) = @_;
6555: my $now = time;
6556: my %groups = ();
6557: foreach my $key (keys(%env)) {
1.811 albertel 6558: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 6559: my ($start,$end) = split(/\./,$env{$key});
6560: if (($end!=0) && ($end<$now)) { next; }
6561: if (($start!=0) && ($start>$now)) { next; }
6562: if ($1 eq $cdom && $2 eq $cnum) {
6563: $groups{$3} = $env{$key} ;
6564: }
6565: }
6566: }
6567: return %groups;
6568: }
6569:
1.683 raeburn 6570: sub get_group_membership {
6571: my ($cdom,$cnum,$group) = @_;
6572: return(&dump('groupmembership',$cdom,$cnum,$group));
6573: }
6574:
6575: sub get_users_groups {
6576: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 6577: my @usersgroups;
1.683 raeburn 6578: my $cachetime=1800;
6579:
6580: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 6581: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
6582: if (defined($cached)) {
1.734 albertel 6583: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 6584: } else {
6585: $grouplist = '';
1.816 raeburn 6586: my $courseurl = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 6587: my $extra = &freeze_escape({'skipcheck' => 1});
6588: my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817 raeburn 6589: my $access_end = $env{'course.'.$courseid.
6590: '.default_enrollment_end_date'};
6591: my $now = time;
6592: foreach my $key (keys(%roleshash)) {
6593: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
6594: my $group = $1;
6595: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
6596: my $start = $2;
6597: my $end = $1;
6598: if ($start == -1) { next; } # deleted from group
6599: if (($start!=0) && ($start>$now)) { next; }
6600: if (($end!=0) && ($end<$now)) {
6601: if ($access_end && $access_end < $now) {
6602: if ($access_end - $end < 86400) {
6603: push(@usersgroups,$group);
1.733 raeburn 6604: }
6605: }
1.817 raeburn 6606: next;
1.733 raeburn 6607: }
1.817 raeburn 6608: push(@usersgroups,$group);
1.683 raeburn 6609: }
6610: }
6611: }
1.817 raeburn 6612: @usersgroups = &sort_course_groups($courseid,@usersgroups);
6613: $grouplist = join(':',@usersgroups);
6614: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 6615: }
1.733 raeburn 6616: return @usersgroups;
1.683 raeburn 6617: }
6618:
6619: sub devalidate_getgroups_cache {
6620: my ($udom,$uname,$cdom,$cnum)=@_;
6621: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 6622:
1.683 raeburn 6623: my $hashid="$udom:$uname:$courseid";
6624: &devalidate_cache_new('getgroups',$hashid);
6625: }
6626:
1.12 www 6627: # ------------------------------------------------------------------ Plain Text
6628:
6629: sub plaintext {
1.988 raeburn 6630: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 6631: if ($short =~ m{^cr/}) {
1.758 albertel 6632: return (split('/',$short))[-1];
6633: }
1.742 raeburn 6634: if (!defined($cid)) {
6635: $cid = $env{'request.course.id'};
6636: }
6637: my %rolenames = (
1.1008 raeburn 6638: Course => 'std',
6639: Community => 'alt1',
1.742 raeburn 6640: );
1.1037 raeburn 6641: if ($cid ne '') {
6642: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
6643: unless ($forcedefault) {
6644: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
6645: &Apache::lonlocal::mt_escape(\$roletext);
6646: return &Apache::lonlocal::mt($roletext);
6647: }
6648: }
6649: }
6650: if ((defined($type)) && (defined($rolenames{$type})) &&
6651: (defined($rolenames{$type})) &&
6652: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 6653: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 6654: } elsif ($cid ne '') {
6655: my $crstype = $env{'course.'.$cid.'.type'};
6656: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
6657: (defined($prp{$short}{$rolenames{$crstype}}))) {
6658: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
6659: }
1.742 raeburn 6660: }
1.1037 raeburn 6661: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 6662: }
6663:
6664: # ----------------------------------------------------------------- Assign Role
6665:
6666: sub assignrole {
1.957 raeburn 6667: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
6668: $context)=@_;
1.21 www 6669: my $mrole;
6670: if ($role =~ /^cr\//) {
1.393 www 6671: my $cwosec=$url;
1.811 albertel 6672: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 6673: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 6674: my $refused = 1;
6675: if ($context eq 'requestcourses') {
6676: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
6677: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
6678: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
6679: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6680: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6681: if ($crsenv{'internal.courseowner'} eq
6682: $env{'user.name'}.':'.$env{'user.domain'}) {
6683: $refused = '';
6684: }
6685: }
6686: }
6687: }
6688: }
6689: if ($refused) {
6690: &logthis('Refused custom assignrole: '.
6691: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
6692: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
6693: return 'refused';
6694: }
1.104 www 6695: }
1.21 www 6696: $mrole='cr';
1.678 raeburn 6697: } elsif ($role =~ /^gr\//) {
6698: my $cwogrp=$url;
1.811 albertel 6699: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 6700: unless (&allowed('mdg',$cwogrp)) {
6701: &logthis('Refused group assignrole: '.
6702: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
6703: $env{'user.name'}.' at '.$env{'user.domain'});
6704: return 'refused';
6705: }
6706: $mrole='gr';
1.21 www 6707: } else {
1.82 www 6708: my $cwosec=$url;
1.811 albertel 6709: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 6710: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
6711: my $refused;
6712: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
6713: if (!(&allowed('c'.$role,$url))) {
6714: $refused = 1;
6715: }
6716: } else {
6717: $refused = 1;
6718: }
1.947 raeburn 6719: if ($refused) {
1.1045 raeburn 6720: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6721: if (!$selfenroll && $context eq 'course') {
6722: my %crsenv;
6723: if ($role eq 'cc' || $role eq 'co') {
6724: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6725: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
6726: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
6727: if ($crsenv{'internal.courseowner'} eq
6728: $env{'user.name'}.':'.$env{'user.domain'}) {
6729: $refused = '';
6730: }
6731: }
6732: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
6733: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
6734: if ($crsenv{'internal.courseowner'} eq
6735: $env{'user.name'}.':'.$env{'user.domain'}) {
6736: $refused = '';
6737: }
6738: }
6739: }
6740: }
6741: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 6742: $refused = '';
1.1017 raeburn 6743: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 6744: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 6745: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 6746: my $wrongcc;
6747: if ($cnum =~ /^$match_community$/) {
6748: $wrongcc = 1 if ($role eq 'cc');
6749: } else {
6750: $wrongcc = 1 if ($role eq 'co');
6751: }
6752: unless ($wrongcc) {
6753: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6754: if ($crsenv{'internal.courseowner'} eq
6755: $env{'user.name'}.':'.$env{'user.domain'}) {
6756: $refused = '';
6757: }
1.1017 raeburn 6758: }
6759: }
6760: }
6761: if ($refused) {
1.947 raeburn 6762: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
6763: ' '.$role.' '.$end.' '.$start.' by '.
6764: $env{'user.name'}.' at '.$env{'user.domain'});
6765: return 'refused';
6766: }
1.932 raeburn 6767: }
1.1056.4.30 raeburn 6768: } elsif ($role eq 'au') {
6769: if ($url ne '/'.$udom.'/') {
6770: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
6771: ' to assign author role for '.$uname.':'.$udom.
6772: ' in domain: '.$url.' refused (wrong domain).');
6773: return 'refused';
6774: }
1.104 www 6775: }
1.21 www 6776: $mrole=$role;
6777: }
1.620 albertel 6778: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 6779: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 6780: if ($end) { $command.='_'.$end; }
1.21 www 6781: if ($start) {
6782: if ($end) {
1.81 www 6783: $command.='_'.$start;
1.21 www 6784: } else {
1.81 www 6785: $command.='_0_'.$start;
1.21 www 6786: }
6787: }
1.739 raeburn 6788: my $origstart = $start;
6789: my $origend = $end;
1.957 raeburn 6790: my $delflag;
1.357 www 6791: # actually delete
6792: if ($deleteflag) {
1.373 www 6793: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 6794: # modify command to delete the role
1.620 albertel 6795: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 6796: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 6797: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 6798: # set start and finish to negative values for userrolelog
6799: $start=-1;
6800: $end=-1;
1.957 raeburn 6801: $delflag = 1;
1.357 www 6802: }
6803: }
6804: # send command
1.349 www 6805: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 6806: # log new user role if status is ok
1.349 www 6807: if ($answer eq 'ok') {
1.663 raeburn 6808: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 6809: # for course roles, perform group memberships changes triggered by role change.
1.957 raeburn 6810: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739 raeburn 6811: unless ($role =~ /^gr/) {
6812: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957 raeburn 6813: $origstart,$selfenroll,$context);
1.739 raeburn 6814: }
1.1053 raeburn 6815: if ($role eq 'cc') {
6816: &autoupdate_coowners($url,$end,$start,$uname,$udom);
6817: }
1.349 www 6818: }
6819: return $answer;
1.169 harris41 6820: }
6821:
1.1053 raeburn 6822: sub autoupdate_coowners {
6823: my ($url,$end,$start,$uname,$udom) = @_;
6824: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
6825: if (($cdom ne '') && ($cnum ne '')) {
6826: my $now = time;
6827: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
6828: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
6829: my %coursehash = &coursedescription($cdom.'_'.$cnum);
6830: my $instcode = $coursehash{'internal.coursecode'};
6831: if ($instcode ne '') {
1.1056 raeburn 6832: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
6833: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 6834: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 6835: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
6836: if ($result eq 'valid') {
6837: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 6838: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6839: push(@newcoowners,$coowner);
6840: }
6841: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
6842: push(@newcoowners,$uname.':'.$udom);
6843: }
6844: @newcoowners = sort(@newcoowners);
6845: } else {
6846: push(@newcoowners,$uname.':'.$udom);
6847: }
6848: } else {
6849: if ($coursehash{'internal.co-owners'}) {
6850: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6851: unless ($coowner eq $uname.':'.$udom) {
6852: push(@newcoowners,$coowner);
6853: }
6854: }
6855: unless (@newcoowners > 0) {
6856: $delcoowners = 1;
6857: $coowners = '';
6858: }
6859: }
6860: }
6861: if (@newcoowners || $delcoowners) {
6862: &store_coowners($cdom,$cnum,$coursehash{'home'},
6863: $delcoowners,@newcoowners);
6864: }
6865: }
6866: }
6867: }
6868: }
6869: }
6870: }
6871:
6872: sub store_coowners {
6873: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
6874: my $cid = $cdom.'_'.$cnum;
6875: my ($coowners,$delresult,$putresult);
6876: if (@newcoowners) {
6877: $coowners = join(',',@newcoowners);
6878: my %coownershash = (
6879: 'internal.co-owners' => $coowners,
6880: );
6881: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
6882: if ($putresult eq 'ok') {
6883: if ($env{'course.'.$cid.'.num'} eq $cnum) {
6884: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
6885: }
6886: }
6887: }
6888: if ($delcoowners) {
6889: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
6890: if ($delresult eq 'ok') {
6891: if ($env{'course.'.$cid.'.internal.co-owners'}) {
6892: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
6893: }
6894: }
6895: }
6896: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
6897: my %crsinfo =
6898: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6899: if (ref($crsinfo{$cid}) eq 'HASH') {
6900: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
6901: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
6902: }
6903: }
6904: }
6905:
1.169 harris41 6906: # -------------------------------------------------- Modify user authentication
1.197 www 6907: # Overrides without validation
6908:
1.169 harris41 6909: sub modifyuserauth {
6910: my ($udom,$uname,$umode,$upass)=@_;
6911: my $uhome=&homeserver($uname,$udom);
1.197 www 6912: unless (&allowed('mau',$udom)) { return 'refused'; }
6913: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 6914: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6915: ' in domain '.$env{'request.role.domain'});
1.169 harris41 6916: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
6917: &escape($upass),$uhome);
1.620 albertel 6918: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 6919: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
6920: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
6921: &log($udom,,$uname,$uhome,
1.620 albertel 6922: 'Authentication changed by '.$env{'user.domain'}.', '.
6923: $env{'user.name'}.', '.$umode.
1.197 www 6924: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 6925: unless ($reply eq 'ok') {
1.197 www 6926: &logthis('Authentication mode error: '.$reply);
1.169 harris41 6927: return 'error: '.$reply;
6928: }
1.170 harris41 6929: return 'ok';
1.80 www 6930: }
6931:
1.81 www 6932: # --------------------------------------------------------------- Modify a user
1.80 www 6933:
1.81 www 6934: sub modifyuser {
1.206 matthew 6935: my ($udom, $uname, $uid,
6936: $umode, $upass, $first,
6937: $middle, $last, $gene,
1.1056.4.1 raeburn 6938: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 6939: $udom= &LONCAPA::clean_domain($udom);
6940: $uname=&LONCAPA::clean_username($uname);
1.1056.4.1 raeburn 6941: my $showcandelete = 'none';
6942: if (ref($candelete) eq 'ARRAY') {
6943: if (@{$candelete} > 0) {
6944: $showcandelete = join(', ',@{$candelete});
6945: }
6946: }
1.81 www 6947: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 6948: $umode.', '.$first.', '.$middle.', '.
1.1056.4.1 raeburn 6949: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 6950: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
6951: ' desiredhome not specified').
1.620 albertel 6952: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6953: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 6954: my $uhome=&homeserver($uname,$udom,'true');
1.1056.4.4 raeburn 6955: my $newuser;
6956: if ($uhome eq 'no_host') {
6957: $newuser = 1;
6958: }
1.80 www 6959: # ----------------------------------------------------------------- Create User
1.406 albertel 6960: if (($uhome eq 'no_host') &&
6961: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 6962: my $unhome='';
1.844 albertel 6963: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 6964: $unhome = $desiredhome;
1.620 albertel 6965: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
6966: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 6967: } else { # load balancing routine for determining $unhome
1.81 www 6968: my $loadm=10000000;
1.841 albertel 6969: my %servers = &get_servers($udom,'library');
6970: foreach my $tryserver (keys(%servers)) {
6971: my $answer=reply('load',$tryserver);
6972: if (($answer=~/\d+/) && ($answer<$loadm)) {
6973: $loadm=$answer;
6974: $unhome=$tryserver;
6975: }
1.80 www 6976: }
6977: }
6978: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 6979: return 'error: unable to find a home server for '.$uname.
6980: ' in domain '.$udom;
1.80 www 6981: }
6982: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
6983: &escape($upass),$unhome);
6984: unless ($reply eq 'ok') {
6985: return 'error: '.$reply;
6986: }
1.230 stredwic 6987: $uhome=&homeserver($uname,$udom,'true');
1.80 www 6988: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 6989: return 'error: unable verify users home machine.';
1.80 www 6990: }
1.209 matthew 6991: } # End of creation of new user
1.80 www 6992: # ---------------------------------------------------------------------- Add ID
6993: if ($uid) {
6994: $uid=~tr/A-Z/a-z/;
6995: my %uidhash=&idrget($udom,$uname);
1.196 www 6996: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
6997: && (!$forceid)) {
1.80 www 6998: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 6999: return 'error: user id "'.$uid.'" does not match '.
7000: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 7001: }
7002: } else {
7003: &idput($udom,($uname => $uid));
7004: }
7005: }
7006: # -------------------------------------------------------------- Add names, etc
1.313 matthew 7007: my @tmp=&get('environment',
1.899 raeburn 7008: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 7009: 'permanentemail','inststatus'],
1.134 albertel 7010: $udom,$uname);
1.1056.4.4 raeburn 7011: my (%names,%oldnames);
1.313 matthew 7012: if ($tmp[0] =~ m/^error:.*/) {
7013: %names=();
7014: } else {
7015: %names = @tmp;
1.1056.4.4 raeburn 7016: %oldnames = %names;
1.313 matthew 7017: }
1.1056.4.6 raeburn 7018: #
1.1056.4.1 raeburn 7019: # If name, email and/or uid are blank (e.g., because an uploaded file
7020: # of users did not contain them), do not overwrite existing values
7021: # unless field is in $candelete array ref.
7022: #
7023:
7024: my @fields = ('firstname','middlename','lastname','generation',
7025: 'permanentemail','id');
7026: my %newvalues;
7027: if (ref($candelete) eq 'ARRAY') {
7028: foreach my $field (@fields) {
7029: if (grep(/^\Q$field\E$/,@{$candelete})) {
7030: if ($field eq 'firstname') {
7031: $names{$field} = $first;
7032: } elsif ($field eq 'middlename') {
7033: $names{$field} = $middle;
7034: } elsif ($field eq 'lastname') {
7035: $names{$field} = $last;
7036: } elsif ($field eq 'generation') {
7037: $names{$field} = $gene;
7038: } elsif ($field eq 'permanentemail') {
7039: $names{$field} = $email;
7040: } elsif ($field eq 'id') {
7041: $names{$field} = $uid;
7042: }
7043: }
7044: }
7045: }
1.388 www 7046: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 7047: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 7048: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 7049: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 7050: if ($email) {
7051: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 7052: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 7053: }
1.899 raeburn 7054: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 7055: if (defined($inststatus)) {
7056: $names{'inststatus'} = '';
7057: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
7058: if (ref($usertypes) eq 'HASH') {
7059: my @okstatuses;
7060: foreach my $item (split(/:/,$inststatus)) {
7061: if (defined($usertypes->{$item})) {
7062: push(@okstatuses,$item);
7063: }
7064: }
7065: if (@okstatuses) {
7066: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
7067: }
7068: }
7069: }
1.1056.4.4 raeburn 7070: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 7071: $umode.', '.$first.', '.$middle.', '.
1.1056.4.4 raeburn 7072: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 7073: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
7074: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
7075: } else {
7076: $logmsg .= ' during self creation';
7077: }
1.1056.4.4 raeburn 7078: my $changed;
7079: if ($newuser) {
7080: $changed = 1;
7081: } else {
7082: foreach my $field (@fields) {
7083: if ($names{$field} ne $oldnames{$field}) {
7084: $changed = 1;
7085: last;
7086: }
7087: }
7088: }
7089: unless ($changed) {
7090: $logmsg = 'No changes in user information needed for: '.$logmsg;
7091: &logthis($logmsg);
7092: return 'ok';
7093: }
7094: my $reply = &put('environment', \%names, $udom,$uname);
7095: if ($reply ne 'ok') {
7096: return 'error: '.$reply;
7097: }
1.1056.4.11 raeburn 7098: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
7099: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
7100: }
1.1056.4.4 raeburn 7101: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
7102: &devalidate_cache_new('namescache',$uname.':'.$udom);
7103: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 7104: &logthis($logmsg);
1.134 albertel 7105: return 'ok';
1.80 www 7106: }
7107:
1.81 www 7108: # -------------------------------------------------------------- Modify student
1.80 www 7109:
1.81 www 7110: sub modifystudent {
7111: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 7112: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990 raeburn 7113: $selfenroll,$context,$inststatus)=@_;
1.455 albertel 7114: if (!$cid) {
1.620 albertel 7115: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7116: return 'not_in_class';
7117: }
1.80 www 7118: }
7119: # --------------------------------------------------------------- Make the user
1.81 www 7120: my $reply=&modifyuser
1.209 matthew 7121: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 7122: $desiredhome,$email,$inststatus);
1.80 www 7123: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 7124: # This will cause &modify_student_enrollment to get the uid from the
7125: # students environment
7126: $uid = undef if (!$forceid);
1.455 albertel 7127: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957 raeburn 7128: $gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297 matthew 7129: return $reply;
7130: }
7131:
7132: sub modify_student_enrollment {
1.957 raeburn 7133: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455 albertel 7134: my ($cdom,$cnum,$chome);
7135: if (!$cid) {
1.620 albertel 7136: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7137: return 'not_in_class';
7138: }
1.620 albertel 7139: $cdom=$env{'course.'.$cid.'.domain'};
7140: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 7141: } else {
7142: ($cdom,$cnum)=split(/_/,$cid);
7143: }
1.620 albertel 7144: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 7145: if (!$chome) {
1.457 raeburn 7146: $chome=&homeserver($cnum,$cdom);
1.297 matthew 7147: }
1.455 albertel 7148: if (!$chome) { return 'unknown_course'; }
1.297 matthew 7149: # Make sure the user exists
1.81 www 7150: my $uhome=&homeserver($uname,$udom);
7151: if (($uhome eq '') || ($uhome eq 'no_host')) {
7152: return 'error: no such user';
7153: }
1.297 matthew 7154: # Get student data if we were not given enough information
7155: if (!defined($first) || $first eq '' ||
7156: !defined($last) || $last eq '' ||
7157: !defined($uid) || $uid eq '' ||
7158: !defined($middle) || $middle eq '' ||
7159: !defined($gene) || $gene eq '') {
1.294 matthew 7160: # They did not supply us with enough data to enroll the student, so
7161: # we need to pick up more information.
1.297 matthew 7162: my %tmp = &get('environment',
1.294 matthew 7163: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 7164: ,$udom,$uname);
7165:
1.800 albertel 7166: #foreach my $key (keys(%tmp)) {
7167: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 7168: #}
1.294 matthew 7169: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
7170: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
7171: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 7172: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 7173: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
7174: }
1.556 albertel 7175: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 7176: my $reply=cput('classlist',
7177: {"$uname:$udom" =>
1.515 raeburn 7178: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 7179: $cdom,$cnum);
1.81 www 7180: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
7181: return 'error: '.$reply;
1.652 albertel 7182: } else {
7183: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 7184: }
1.297 matthew 7185: # Add student role to user
1.83 www 7186: my $uurl='/'.$cid;
1.81 www 7187: $uurl=~s/\_/\//g;
7188: if ($usec) {
7189: $uurl.='/'.$usec;
7190: }
1.957 raeburn 7191: return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21 www 7192: }
7193:
1.556 albertel 7194: sub format_name {
7195: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
7196: my $name;
7197: if ($first ne 'lastname') {
7198: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
7199: } else {
7200: if ($lastname=~/\S/) {
7201: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
7202: $name=~s/\s+,/,/;
7203: } else {
7204: $name.= $firstname.' '.$middlename.' '.$generation;
7205: }
7206: }
7207: $name=~s/^\s+//;
7208: $name=~s/\s+$//;
7209: $name=~s/\s+/ /g;
7210: return $name;
7211: }
7212:
1.84 www 7213: # ------------------------------------------------- Write to course preferences
7214:
7215: sub writecoursepref {
7216: my ($courseid,%prefs)=@_;
7217: $courseid=~s/^\///;
7218: $courseid=~s/\_/\//g;
7219: my ($cdomain,$cnum)=split(/\//,$courseid);
7220: my $chome=homeserver($cnum,$cdomain);
7221: if (($chome eq '') || ($chome eq 'no_host')) {
7222: return 'error: no such course';
7223: }
7224: my $cstring='';
1.800 albertel 7225: foreach my $pref (keys(%prefs)) {
7226: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 7227: }
1.84 www 7228: $cstring=~s/\&$//;
7229: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
7230: }
7231:
7232: # ---------------------------------------------------------- Make/modify course
7233:
7234: sub createcourse {
1.741 raeburn 7235: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 7236: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 7237: $url=&declutter($url);
7238: my $cid='';
1.1028 raeburn 7239: if ($context eq 'requestcourses') {
7240: my $can_create = 0;
7241: my ($ownername,$ownerdom) = split(':',$course_owner);
7242: if ($udom eq $ownerdom) {
7243: if (&usertools_access($ownername,$ownerdom,$category,undef,
7244: $context)) {
7245: $can_create = 1;
7246: }
7247: } else {
7248: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
7249: $category);
7250: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
7251: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
7252: if (@curr > 0) {
7253: my @options = qw(approval validate autolimit);
7254: my $optregex = join('|',@options);
7255: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
7256: $can_create = 1;
7257: }
7258: }
7259: }
7260: }
7261: if ($can_create) {
7262: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
7263: unless (&allowed('ccc',$udom)) {
7264: return 'refused';
7265: }
1.1017 raeburn 7266: }
7267: } else {
7268: return 'refused';
7269: }
1.1028 raeburn 7270: } elsif (!&allowed('ccc',$udom)) {
7271: return 'refused';
1.84 www 7272: }
1.1011 raeburn 7273: # --------------------------------------------------------------- Get Unique ID
7274: my $uname;
7275: if ($cnum =~ /^$match_courseid$/) {
7276: my $chome=&homeserver($cnum,$udom,'true');
7277: if (($chome eq '') || ($chome eq 'no_host')) {
7278: $uname = $cnum;
7279: } else {
1.1038 raeburn 7280: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7281: }
7282: } else {
1.1038 raeburn 7283: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7284: }
7285: return $uname if ($uname =~ /^error/);
7286: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 7287: if (!defined($course_server)) {
7288: if (defined(&domain($udom,'primary'))) {
7289: $course_server = &domain($udom,'primary');
7290: } else {
7291: $course_server = $env{'user.home'};
7292: }
7293: }
7294: my %host_servers =
7295: &Apache::lonnet::get_servers($udom,'library');
7296: unless ($host_servers{$course_server}) {
7297: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 7298: }
1.84 www 7299: # ------------------------------------------------------------- Make the course
7300: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 7301: $course_server);
1.84 www 7302: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 7303: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 7304: if (($uhome eq '') || ($uhome eq 'no_host')) {
7305: return 'error: no such course';
7306: }
1.271 www 7307: # ----------------------------------------------------------------- Course made
1.516 raeburn 7308: # log existence
1.1029 raeburn 7309: my $now = time;
1.918 raeburn 7310: my $newcourse = {
7311: $udom.'_'.$uname => {
1.921 raeburn 7312: description => $description,
7313: inst_code => $inst_code,
7314: owner => $course_owner,
7315: type => $crstype,
1.1029 raeburn 7316: creator => $env{'user.name'}.':'.
7317: $env{'user.domain'},
7318: created => $now,
7319: context => $context,
1.918 raeburn 7320: },
7321: };
1.921 raeburn 7322: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 7323: # set toplevel url
1.271 www 7324: my $topurl=$url;
7325: unless ($nonstandard) {
7326: # ------------------------------------------ For standard courses, make top url
7327: my $mapurl=&clutter($url);
1.278 www 7328: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 7329: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 7330: <map>
7331: <resource id="1" type="start"></resource>
7332: <resource id="2" src="$mapurl"></resource>
7333: <resource id="3" type="finish"></resource>
7334: <link index="1" from="1" to="2"></link>
7335: <link index="2" from="2" to="3"></link>
7336: </map>
7337: ENDINITMAP
7338: $topurl=&declutter(
1.638 albertel 7339: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 7340: );
7341: }
7342: # ----------------------------------------------------------- Write preferences
1.84 www 7343: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 7344: ('description' => $description,
7345: 'url' => $topurl,
7346: 'internal.creator' => $env{'user.name'}.':'.
7347: $env{'user.domain'},
7348: 'internal.created' => $now,
7349: 'internal.creationcontext' => $context)
7350: );
1.84 www 7351: return '/'.$udom.'/'.$uname;
7352: }
7353:
1.1011 raeburn 7354: # ------------------------------------------------------------------- Create ID
7355: sub generate_coursenum {
1.1038 raeburn 7356: my ($udom,$crstype) = @_;
1.1011 raeburn 7357: my $domdesc = &domain($udom);
7358: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 7359: my $first;
7360: if ($crstype eq 'Community') {
7361: $first = '0';
7362: } else {
7363: $first = int(1+rand(9));
7364: }
7365: my $uname=$first.
1.1011 raeburn 7366: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7367: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7368: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7369: # ----------------------------------------------- Make sure that does not exist
7370: my $uhome=&homeserver($uname,$udom,'true');
7371: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 7372: if ($crstype eq 'Community') {
7373: $first = '0';
7374: } else {
7375: $first = int(1+rand(9));
7376: }
7377: $uname=$first.
1.1011 raeburn 7378: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7379: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7380: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7381: $uhome=&homeserver($uname,$udom,'true');
7382: unless (($uhome eq '') || ($uhome eq 'no_host')) {
7383: return 'error: unable to generate unique course-ID';
7384: }
7385: }
7386: return $uname;
7387: }
7388:
1.813 albertel 7389: sub is_course {
7390: my ($cdom,$cnum) = @_;
7391: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946 raeburn 7392: undef,'.');
1.813 albertel 7393: if (exists($courses{$cdom.'_'.$cnum})) {
7394: return 1;
7395: }
7396: return 0;
7397: }
7398:
1.1015 raeburn 7399: sub store_userdata {
7400: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 7401: my $result;
1.1016 raeburn 7402: if ($datakey ne '') {
1.1013 raeburn 7403: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 7404: if ($udom eq '' || $uname eq '') {
7405: $udom = $env{'user.domain'};
7406: $uname = $env{'user.name'};
7407: }
7408: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 7409: if (($uhome eq '') || ($uhome eq 'no_host')) {
7410: $result = 'error: no_host';
7411: } else {
7412: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
7413: $storehash->{'host'} = $perlvar{'lonHostID'};
7414:
7415: my $namevalue='';
7416: foreach my $key (keys(%{$storehash})) {
7417: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7418: }
7419: $namevalue=~s/\&$//;
1.1056.4.37 raeburn 7420: unless ($namespace eq 'courserequests') {
7421: $datakey = &escape($datakey);
7422: }
1.1056.4.24 raeburn 7423: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
7424: $namevalue,$uhome);
1.1013 raeburn 7425: }
7426: } else {
7427: $result = 'error: data to store was not a hash reference';
7428: }
7429: } else {
7430: $result= 'error: invalid requestkey';
7431: }
7432: return $result;
7433: }
7434:
1.21 www 7435: # ---------------------------------------------------------- Assign Custom Role
7436:
7437: sub assigncustomrole {
1.957 raeburn 7438: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7439: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 7440: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 7441: }
7442:
7443: # ----------------------------------------------------------------- Revoke Role
7444:
7445: sub revokerole {
1.957 raeburn 7446: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7447: my $now=time;
1.965 raeburn 7448: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 7449: }
7450:
7451: # ---------------------------------------------------------- Revoke Custom Role
7452:
7453: sub revokecustomrole {
1.957 raeburn 7454: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7455: my $now=time;
1.357 www 7456: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 7457: $deleteflag,$selfenroll,$context);
1.17 www 7458: }
7459:
1.533 banghart 7460: # ------------------------------------------------------------ Disk usage
1.535 albertel 7461: sub diskusage {
1.955 raeburn 7462: my ($udom,$uname,$directorypath,$getpropath)=@_;
7463: $directorypath =~ s/\/$//;
7464: my $listing=&reply('du2:'.&escape($directorypath).':'
7465: .&escape($getpropath).':'.&escape($uname).':'
7466: .&escape($udom),homeserver($uname,$udom));
7467: if ($listing eq 'unknown_cmd') {
7468: if ($getpropath) {
7469: $directorypath = &propath($udom,$uname).'/'.$directorypath;
7470: }
7471: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
7472: }
1.514 albertel 7473: return $listing;
1.512 banghart 7474: }
7475:
1.566 banghart 7476: sub is_locked {
1.1056.4.17 raeburn 7477: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 7478: my @check;
7479: my $is_locked;
1.1056.4.14 raeburn 7480: push(@check,$file_name);
1.613 albertel 7481: my %locked = &get('file_permissions',\@check,
1.620 albertel 7482: $env{'user.domain'},$env{'user.name'});
1.615 albertel 7483: my ($tmp)=keys(%locked);
7484: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 7485:
1.566 banghart 7486: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 7487: $is_locked = 'false';
7488: foreach my $entry (@{$locked{$file_name}}) {
1.1056.4.17 raeburn 7489: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 7490: $is_locked = 'true';
1.1056.4.17 raeburn 7491: if (ref($which) eq 'ARRAY') {
7492: push(@{$which},$entry);
7493: } else {
7494: last;
7495: }
1.745 raeburn 7496: }
7497: }
1.566 banghart 7498: } else {
7499: $is_locked = 'false';
7500: }
1.1056.4.14 raeburn 7501: return $is_locked;
1.566 banghart 7502: }
7503:
1.759 albertel 7504: sub declutter_portfile {
7505: my ($file) = @_;
1.833 albertel 7506: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 7507: return $file;
7508: }
7509:
1.559 banghart 7510: # ------------------------------------------------------------- Mark as Read Only
7511:
7512: sub mark_as_readonly {
7513: my ($domain,$user,$files,$what) = @_;
1.613 albertel 7514: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7515: my ($tmp)=keys(%current_permissions);
7516: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 7517: foreach my $file (@{$files}) {
1.759 albertel 7518: $file = &declutter_portfile($file);
1.561 banghart 7519: push(@{$current_permissions{$file}},$what);
1.559 banghart 7520: }
1.613 albertel 7521: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7522: return;
7523: }
7524:
1.572 banghart 7525: # ------------------------------------------------------------Save Selected Files
7526:
7527: sub save_selected_files {
7528: my ($user, $path, @files) = @_;
7529: my $filename = $user."savedfiles";
1.573 banghart 7530: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 7531: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 7532: foreach my $file (@files) {
1.620 albertel 7533: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 7534: }
7535: foreach my $file (@other_files) {
1.574 banghart 7536: print (OUT $file."\n");
1.572 banghart 7537: }
1.574 banghart 7538: close (OUT);
1.572 banghart 7539: return 'ok';
7540: }
7541:
1.574 banghart 7542: sub clear_selected_files {
7543: my ($user) = @_;
7544: my $filename = $user."savedfiles";
7545: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7546: print (OUT undef);
7547: close (OUT);
7548: return ("ok");
7549: }
7550:
1.572 banghart 7551: sub files_in_path {
7552: my ($user, $path) = @_;
7553: my $filename = $user."savedfiles";
7554: my %return_files;
1.574 banghart 7555: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 7556: while (my $line_in = <IN>) {
1.574 banghart 7557: chomp ($line_in);
7558: my @paths_and_file = split (m!/!, $line_in);
7559: my $file_part = pop (@paths_and_file);
7560: my $path_part = join ('/', @paths_and_file);
1.573 banghart 7561: $path_part.='/';
7562: my $path_and_file = $path_part.$file_part;
7563: if ($path_part eq $path) {
7564: $return_files{$file_part}= 'selected';
7565: }
7566: }
1.574 banghart 7567: close (IN);
7568: return (\%return_files);
1.572 banghart 7569: }
7570:
7571: # called in portfolio select mode, to show files selected NOT in current directory
7572: sub files_not_in_path {
7573: my ($user, $path) = @_;
7574: my $filename = $user."savedfiles";
7575: my @return_files;
7576: my $path_part;
1.800 albertel 7577: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7578: while (my $line = <IN>) {
1.572 banghart 7579: #ok, I know it's clunky, but I want it to work
1.800 albertel 7580: my @paths_and_file = split(m|/|, $line);
7581: my $file_part = pop(@paths_and_file);
7582: chomp($file_part);
7583: my $path_part = join('/', @paths_and_file);
1.572 banghart 7584: $path_part .= '/';
7585: my $path_and_file = $path_part.$file_part;
7586: if ($path_part ne $path) {
1.800 albertel 7587: push(@return_files, ($path_and_file));
1.572 banghart 7588: }
7589: }
1.800 albertel 7590: close(OUT);
1.574 banghart 7591: return (@return_files);
1.572 banghart 7592: }
7593:
1.745 raeburn 7594: #----------------------------------------------Get portfolio file permissions
1.629 banghart 7595:
1.745 raeburn 7596: sub get_portfile_permissions {
7597: my ($domain,$user) = @_;
1.613 albertel 7598: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7599: my ($tmp)=keys(%current_permissions);
7600: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7601: return \%current_permissions;
7602: }
7603:
7604: #---------------------------------------------Get portfolio file access controls
7605:
1.749 raeburn 7606: sub get_access_controls {
1.745 raeburn 7607: my ($current_permissions,$group,$file) = @_;
1.769 albertel 7608: my %access;
7609: my $real_file = $file;
7610: $file =~ s/\.meta$//;
1.745 raeburn 7611: if (defined($file)) {
1.749 raeburn 7612: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
7613: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 7614: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 7615: }
7616: }
1.745 raeburn 7617: } else {
1.749 raeburn 7618: foreach my $key (keys(%{$current_permissions})) {
7619: if ($key =~ /\0accesscontrol$/) {
7620: if (defined($group)) {
7621: if ($key !~ m-^\Q$group\E/-) {
7622: next;
7623: }
7624: }
7625: my ($fullpath) = split(/\0/,$key);
7626: if (ref($$current_permissions{$key}) eq 'HASH') {
7627: foreach my $control (keys(%{$$current_permissions{$key}})) {
7628: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
7629: }
7630: }
7631: }
7632: }
7633: }
7634: return %access;
7635: }
7636:
7637: sub modify_access_controls {
7638: my ($file_name,$changes,$domain,$user)=@_;
7639: my ($outcome,$deloutcome);
7640: my %store_permissions;
7641: my %new_values;
7642: my %new_control;
7643: my %translation;
7644: my @deletions = ();
7645: my $now = time;
7646: if (exists($$changes{'activate'})) {
7647: if (ref($$changes{'activate'}) eq 'HASH') {
7648: my @newitems = sort(keys(%{$$changes{'activate'}}));
7649: my $numnew = scalar(@newitems);
7650: for (my $i=0; $i<$numnew; $i++) {
7651: my $newkey = $newitems[$i];
7652: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 7653: if ($newkey =~ /^\d+:/) {
7654: $newkey =~ s/^(\d+)/$newid/;
7655: $translation{$1} = $newid;
7656: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
7657: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
7658: $translation{$1} = $newid;
7659: }
1.749 raeburn 7660: $new_values{$file_name."\0".$newkey} =
7661: $$changes{'activate'}{$newitems[$i]};
7662: $new_control{$newkey} = $now;
7663: }
7664: }
7665: }
7666: my %todelete;
7667: my %changed_items;
7668: foreach my $action ('delete','update') {
7669: if (exists($$changes{$action})) {
7670: if (ref($$changes{$action}) eq 'HASH') {
7671: foreach my $key (keys(%{$$changes{$action}})) {
7672: my ($itemnum) = ($key =~ /^([^:]+):/);
7673: if ($action eq 'delete') {
7674: $todelete{$itemnum} = 1;
7675: } else {
7676: $changed_items{$itemnum} = $key;
7677: }
7678: }
1.745 raeburn 7679: }
7680: }
1.749 raeburn 7681: }
7682: # get lock on access controls for file.
7683: my $lockhash = {
7684: $file_name."\0".'locked_access_records' => $env{'user.name'}.
7685: ':'.$env{'user.domain'},
7686: };
7687: my $tries = 0;
7688: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7689:
7690: while (($gotlock ne 'ok') && $tries <3) {
7691: $tries ++;
7692: sleep 1;
7693: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7694: }
7695: if ($gotlock eq 'ok') {
7696: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
7697: my ($tmp)=keys(%curr_permissions);
7698: if ($tmp=~/^error:/) { undef(%curr_permissions); }
7699: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
7700: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
7701: if (ref($curr_controls) eq 'HASH') {
7702: foreach my $control_item (keys(%{$curr_controls})) {
7703: my ($itemnum) = ($control_item =~ /^([^:]+):/);
7704: if (defined($todelete{$itemnum})) {
7705: push(@deletions,$file_name."\0".$control_item);
7706: } else {
7707: if (defined($changed_items{$itemnum})) {
7708: $new_control{$changed_items{$itemnum}} = $now;
7709: push(@deletions,$file_name."\0".$control_item);
7710: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
7711: } else {
7712: $new_control{$control_item} = $$curr_controls{$control_item};
7713: }
7714: }
1.745 raeburn 7715: }
7716: }
7717: }
1.970 raeburn 7718: my ($group);
7719: if (&is_course($domain,$user)) {
7720: ($group,my $file) = split(/\//,$file_name,2);
7721: }
1.749 raeburn 7722: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
7723: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
7724: $outcome = &put('file_permissions',\%new_values,$domain,$user);
7725: # remove lock
7726: my @del_lock = ($file_name."\0".'locked_access_records');
7727: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 7728: my $sqlresult =
1.970 raeburn 7729: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 7730: $group);
1.749 raeburn 7731: } else {
7732: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 7733: }
1.749 raeburn 7734: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 7735: }
7736:
1.827 raeburn 7737: sub make_public_indefinitely {
7738: my ($requrl) = @_;
7739: my $now = time;
7740: my $action = 'activate';
7741: my $aclnum = 0;
7742: if (&is_portfolio_url($requrl)) {
7743: my (undef,$udom,$unum,$file_name,$group) =
7744: &parse_portfolio_url($requrl);
7745: my $current_perms = &get_portfile_permissions($udom,$unum);
7746: my %access_controls = &get_access_controls($current_perms,
7747: $group,$file_name);
7748: foreach my $key (keys(%{$access_controls{$file_name}})) {
7749: my ($num,$scope,$end,$start) =
7750: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
7751: if ($scope eq 'public') {
7752: if ($start <= $now && $end == 0) {
7753: $action = 'none';
7754: } else {
7755: $action = 'update';
7756: $aclnum = $num;
7757: }
7758: last;
7759: }
7760: }
7761: if ($action eq 'none') {
7762: return 'ok';
7763: } else {
7764: my %changes;
7765: my $newend = 0;
7766: my $newstart = $now;
7767: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
7768: $changes{$action}{$newkey} = {
7769: type => 'public',
7770: time => {
7771: start => $newstart,
7772: end => $newend,
7773: },
7774: };
7775: my ($outcome,$deloutcome,$new_values,$translation) =
7776: &modify_access_controls($file_name,\%changes,$udom,$unum);
7777: return $outcome;
7778: }
7779: } else {
7780: return 'invalid';
7781: }
7782: }
7783:
1.745 raeburn 7784: #------------------------------------------------------Get Marked as Read Only
7785:
7786: sub get_marked_as_readonly {
7787: my ($domain,$user,$what,$group) = @_;
7788: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 7789: my @readonly_files;
1.629 banghart 7790: my $cmp1=$what;
7791: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 7792: while (my ($file_name,$value) = each(%{$current_permissions})) {
7793: if (defined($group)) {
7794: if ($file_name !~ m-^\Q$group\E/-) {
7795: next;
7796: }
7797: }
1.561 banghart 7798: if (ref($value) eq "ARRAY"){
7799: foreach my $stored_what (@{$value}) {
1.629 banghart 7800: my $cmp2=$stored_what;
1.759 albertel 7801: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 7802: $cmp2=join('',@{$stored_what});
1.745 raeburn 7803: }
1.629 banghart 7804: if ($cmp1 eq $cmp2) {
1.561 banghart 7805: push(@readonly_files, $file_name);
1.745 raeburn 7806: last;
1.563 banghart 7807: } elsif (!defined($what)) {
7808: push(@readonly_files, $file_name);
1.745 raeburn 7809: last;
1.561 banghart 7810: }
7811: }
1.745 raeburn 7812: }
1.561 banghart 7813: }
7814: return @readonly_files;
7815: }
1.577 banghart 7816: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 7817:
1.577 banghart 7818: sub get_marked_as_readonly_hash {
1.745 raeburn 7819: my ($current_permissions,$group,$what) = @_;
1.577 banghart 7820: my %readonly_files;
1.745 raeburn 7821: while (my ($file_name,$value) = each(%{$current_permissions})) {
7822: if (defined($group)) {
7823: if ($file_name !~ m-^\Q$group\E/-) {
7824: next;
7825: }
7826: }
1.577 banghart 7827: if (ref($value) eq "ARRAY"){
7828: foreach my $stored_what (@{$value}) {
1.745 raeburn 7829: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 7830: foreach my $lock_descriptor(@{$stored_what}) {
7831: if ($lock_descriptor eq 'graded') {
7832: $readonly_files{$file_name} = 'graded';
7833: } elsif ($lock_descriptor eq 'handback') {
7834: $readonly_files{$file_name} = 'handback';
7835: } else {
7836: if (!exists($readonly_files{$file_name})) {
7837: $readonly_files{$file_name} = 'locked';
7838: }
7839: }
1.745 raeburn 7840: }
1.750 banghart 7841: }
1.577 banghart 7842: }
7843: }
7844: }
7845: return %readonly_files;
7846: }
1.559 banghart 7847: # ------------------------------------------------------------ Unmark as Read Only
7848:
7849: sub unmark_as_readonly {
1.629 banghart 7850: # unmarks $file_name (if $file_name is defined), or all files locked by $what
7851: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 7852: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 7853: $file_name = &declutter_portfile($file_name);
1.634 albertel 7854: my $symb_crs = $what;
7855: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 7856: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 7857: my ($tmp)=keys(%current_permissions);
7858: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7859: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 7860: foreach my $file (@readonly_files) {
1.759 albertel 7861: my $clean_file = &declutter_portfile($file);
7862: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 7863: my $current_locks = $current_permissions{$file};
1.563 banghart 7864: my @new_locks;
7865: my @del_keys;
7866: if (ref($current_locks) eq "ARRAY"){
7867: foreach my $locker (@{$current_locks}) {
1.632 albertel 7868: my $compare=$locker;
1.749 raeburn 7869: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 7870: $compare=join('',@{$locker});
1.746 raeburn 7871: if ($compare ne $symb_crs) {
7872: push(@new_locks, $locker);
7873: }
1.563 banghart 7874: }
7875: }
1.650 albertel 7876: if (scalar(@new_locks) > 0) {
1.563 banghart 7877: $current_permissions{$file} = \@new_locks;
7878: } else {
7879: push(@del_keys, $file);
1.613 albertel 7880: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 7881: delete($current_permissions{$file});
1.563 banghart 7882: }
7883: }
1.561 banghart 7884: }
1.613 albertel 7885: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7886: return;
7887: }
1.512 banghart 7888:
1.17 www 7889: # ------------------------------------------------------------ Directory lister
7890:
7891: sub dirlist {
1.955 raeburn 7892: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 7893: $uri=~s/^\///;
7894: $uri=~s/\/$//;
1.253 stredwic 7895: my ($udom, $uname);
1.955 raeburn 7896: if ($getuserdir) {
1.253 stredwic 7897: $udom = $userdomain;
7898: $uname = $username;
1.955 raeburn 7899: } else {
7900: (undef,$udom,$uname)=split(/\//,$uri);
7901: if(defined($userdomain)) {
7902: $udom = $userdomain;
7903: }
7904: if(defined($username)) {
7905: $uname = $username;
7906: }
1.253 stredwic 7907: }
1.955 raeburn 7908: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 7909:
1.955 raeburn 7910: $dirRoot = $perlvar{'lonDocRoot'};
7911: if (defined($getpropath)) {
7912: $dirRoot = &propath($udom,$uname);
1.253 stredwic 7913: $dirRoot =~ s/\/$//;
1.955 raeburn 7914: } elsif (defined($getuserdir)) {
7915: my $subdir=$uname.'__';
7916: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
7917: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
7918: ."/$udom/$subdir/$uname";
7919: } elsif (defined($alternateRoot)) {
7920: $dirRoot = $alternateRoot;
1.751 banghart 7921: }
1.253 stredwic 7922:
7923: if($udom) {
7924: if($uname) {
1.955 raeburn 7925: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 7926: .$getuserdir.':'.&escape($dirRoot)
1.955 raeburn 7927: .':'.&escape($uname).':'.&escape($udom),
7928: &homeserver($uname,$udom));
7929: if ($listing eq 'unknown_cmd') {
7930: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
7931: &homeserver($uname,$udom));
7932: } else {
7933: @listing_results = map { &unescape($_); } split(/:/,$listing);
7934: }
1.605 matthew 7935: if ($listing eq 'unknown_cmd') {
1.800 albertel 7936: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
7937: &homeserver($uname,$udom));
1.605 matthew 7938: @listing_results = split(/:/,$listing);
7939: } else {
7940: @listing_results = map { &unescape($_); } split(/:/,$listing);
7941: }
7942: return @listing_results;
1.955 raeburn 7943: } elsif(!$alternateRoot) {
1.800 albertel 7944: my %allusers;
1.841 albertel 7945: my %servers = &get_servers($udom,'library');
1.955 raeburn 7946: foreach my $tryserver (keys(%servers)) {
7947: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
7948: &escape($udom),$tryserver);
7949: if ($listing eq 'unknown_cmd') {
7950: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
7951: $udom, $tryserver);
7952: } else {
7953: @listing_results = map { &unescape($_); } split(/:/,$listing);
7954: }
1.841 albertel 7955: if ($listing eq 'unknown_cmd') {
7956: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
7957: $udom, $tryserver);
7958: @listing_results = split(/:/,$listing);
7959: } else {
7960: @listing_results =
7961: map { &unescape($_); } split(/:/,$listing);
7962: }
7963: if ($listing_results[0] ne 'no_such_dir' &&
7964: $listing_results[0] ne 'empty' &&
7965: $listing_results[0] ne 'con_lost') {
7966: foreach my $line (@listing_results) {
7967: my ($entry) = split(/&/,$line,2);
7968: $allusers{$entry} = 1;
7969: }
7970: }
1.253 stredwic 7971: }
7972: my $alluserstr='';
1.800 albertel 7973: foreach my $user (sort(keys(%allusers))) {
7974: $alluserstr.=$user.'&user:';
1.253 stredwic 7975: }
7976: $alluserstr=~s/:$//;
7977: return split(/:/,$alluserstr);
7978: } else {
1.800 albertel 7979: return ('missing user name');
1.253 stredwic 7980: }
1.955 raeburn 7981: } elsif(!defined($getpropath)) {
1.841 albertel 7982: my @all_domains = sort(&all_domains());
1.955 raeburn 7983: foreach my $domain (@all_domains) {
7984: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
7985: }
7986: return @all_domains;
7987: } else {
1.800 albertel 7988: return ('missing domain');
1.275 stredwic 7989: }
7990: }
7991:
7992: # --------------------------------------------- GetFileTimestamp
7993: # This function utilizes dirlist and returns the date stamp for
7994: # when it was last modified. It will also return an error of -1
7995: # if an error occurs
7996:
7997: sub GetFileTimestamp {
1.955 raeburn 7998: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 7999: $studentDomain = &LONCAPA::clean_domain($studentDomain);
8000: $studentName = &LONCAPA::clean_username($studentName);
1.955 raeburn 8001: my ($fileStat) =
8002: &Apache::lonnet::dirlist($filename,$studentDomain,$studentName,
8003: undef,$getuserdir);
1.275 stredwic 8004: my @stats = split('&', $fileStat);
8005: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 8006: # @stats contains first the filename, then the stat output
8007: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 8008: } else {
8009: return -1;
1.253 stredwic 8010: }
1.26 www 8011: }
8012:
1.712 albertel 8013: sub stat_file {
8014: my ($uri) = @_;
1.787 albertel 8015: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 8016:
1.955 raeburn 8017: my ($udom,$uname,$file);
1.712 albertel 8018: if ($uri =~ m-^/(uploaded|editupload)/-) {
8019: ($udom,$uname,$file) =
1.811 albertel 8020: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 8021: $file = 'userfiles/'.$file;
8022: }
8023: if ($uri =~ m-^/res/-) {
8024: ($udom,$uname) =
1.807 albertel 8025: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 8026: $file = $uri;
8027: }
8028:
8029: if (!$udom || !$uname || !$file) {
8030: # unable to handle the uri
8031: return ();
8032: }
1.956 raeburn 8033: my $getpropath;
8034: if ($file =~ /^userfiles\//) {
8035: $getpropath = 1;
8036: }
1.955 raeburn 8037: my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712 albertel 8038: my @stats = split('&', $result);
1.721 banghart 8039:
1.712 albertel 8040: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
8041: shift(@stats); #filename is first
8042: return @stats;
8043: }
8044: return ();
8045: }
8046:
1.26 www 8047: # -------------------------------------------------------- Value of a Condition
8048:
1.713 albertel 8049: # gets the value of a specific preevaluated condition
8050: # stored in the string $env{user.state.<cid>}
8051: # or looks up a condition reference in the bighash and if if hasn't
8052: # already been evaluated recurses into docondval to get the value of
8053: # the condition, then memoizing it to
8054: # $env{user.state.<cid>.<condition>}
1.40 www 8055: sub directcondval {
8056: my $number=shift;
1.620 albertel 8057: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 8058: &Apache::lonuserstate::evalstate();
8059: }
1.713 albertel 8060: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
8061: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
8062: } elsif ($number =~ /^_/) {
8063: my $sub_condition;
8064: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8065: &GDBM_READER(),0640)) {
8066: $sub_condition=$bighash{'conditions'.$number};
8067: untie(%bighash);
8068: }
8069: my $value = &docondval($sub_condition);
1.949 raeburn 8070: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 8071: return $value;
8072: }
1.620 albertel 8073: if ($env{'user.state.'.$env{'request.course.id'}}) {
8074: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 8075: } else {
8076: return 2;
8077: }
8078: }
8079:
1.713 albertel 8080: # get the collection of conditions for this resource
1.26 www 8081: sub condval {
8082: my $condidx=shift;
1.54 www 8083: my $allpathcond='';
1.713 albertel 8084: foreach my $cond (split(/\|/,$condidx)) {
8085: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
8086: $allpathcond.=
8087: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
8088: }
1.191 harris41 8089: }
1.54 www 8090: $allpathcond=~s/\|$//;
1.713 albertel 8091: return &docondval($allpathcond);
8092: }
8093:
8094: #evaluates an expression of conditions
8095: sub docondval {
8096: my ($allpathcond) = @_;
8097: my $result=0;
8098: if ($env{'request.course.id'}
8099: && defined($allpathcond)) {
8100: my $operand='|';
8101: my @stack;
8102: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
8103: if ($chunk eq '(') {
8104: push @stack,($operand,$result);
8105: } elsif ($chunk eq ')') {
8106: my $before=pop @stack;
8107: if (pop @stack eq '&') {
8108: $result=$result>$before?$before:$result;
8109: } else {
8110: $result=$result>$before?$result:$before;
8111: }
8112: } elsif (($chunk eq '&') || ($chunk eq '|')) {
8113: $operand=$chunk;
8114: } else {
8115: my $new=directcondval($chunk);
8116: if ($operand eq '&') {
8117: $result=$result>$new?$new:$result;
8118: } else {
8119: $result=$result>$new?$result:$new;
8120: }
8121: }
8122: }
1.26 www 8123: }
8124: return $result;
1.421 albertel 8125: }
8126:
8127: # ---------------------------------------------------- Devalidate courseresdata
8128:
8129: sub devalidatecourseresdata {
8130: my ($coursenum,$coursedomain)=@_;
8131: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8132: &devalidate_cache_new('courseres',$hashid);
1.28 www 8133: }
8134:
1.763 www 8135:
1.200 www 8136: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 8137: #
8138: # Parameters:
8139: # $coursenum - Number of the course.
8140: # $coursedomain - Domain at which the course was created.
8141: # Returns:
8142: # A hash of the course parameters along (I think) with timestamps
8143: # and version info.
1.877 foxr 8144:
1.624 albertel 8145: sub get_courseresdata {
8146: my ($coursenum,$coursedomain)=@_;
1.200 www 8147: my $coursehom=&homeserver($coursenum,$coursedomain);
8148: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8149: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 8150: my %dumpreply;
1.417 albertel 8151: unless (defined($cached)) {
1.624 albertel 8152: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 8153: $result=\%dumpreply;
1.251 albertel 8154: my ($tmp) = keys(%dumpreply);
8155: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 8156: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 8157: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
8158: return $tmp;
1.416 albertel 8159: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 8160: $result=undef;
1.599 albertel 8161: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 8162: }
8163: }
1.624 albertel 8164: return $result;
8165: }
8166:
1.633 albertel 8167: sub devalidateuserresdata {
8168: my ($uname,$udom)=@_;
8169: my $hashid="$udom:$uname";
8170: &devalidate_cache_new('userres',$hashid);
8171: }
8172:
1.624 albertel 8173: sub get_userresdata {
8174: my ($uname,$udom)=@_;
8175: #most student don\'t have any data set, check if there is some data
8176: if (&EXT_cache_status($udom,$uname)) { return undef; }
8177:
8178: my $hashid="$udom:$uname";
8179: my ($result,$cached)=&is_cached_new('userres',$hashid);
8180: if (!defined($cached)) {
8181: my %resourcedata=&dump('resourcedata',$udom,$uname);
8182: $result=\%resourcedata;
8183: &do_cache_new('userres',$hashid,$result,600);
8184: }
8185: my ($tmp)=keys(%$result);
8186: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
8187: return $result;
8188: }
8189: #error 2 occurs when the .db doesn't exist
8190: if ($tmp!~/error: 2 /) {
1.672 albertel 8191: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 8192: " Trying to get resource data for ".
8193: $uname." at ".$udom.": ".
8194: $tmp."</font>");
8195: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 8196: #&EXT_cache_set($udom,$uname);
8197: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 8198: undef($tmp); # not really an error so don't send it back
1.624 albertel 8199: }
8200: return $tmp;
8201: }
1.879 foxr 8202: #----------------------------------------------- resdata - return resource data
8203: # Purpose:
8204: # Return resource data for either users or for a course.
8205: # Parameters:
8206: # $name - Course/user name.
8207: # $domain - Name of the domain the user/course is registered on.
8208: # $type - Type of thing $name is (must be 'course' or 'user'
8209: # @which - Array of names of resources desired.
8210: # Returns:
8211: # The value of the first reasource in @which that is found in the
8212: # resource hash.
8213: # Exceptional Conditions:
8214: # If the $type passed in is not valid (not the string 'course' or
8215: # 'user', an undefined reference is returned.
8216: # If none of the resources are found, an undef is returned
1.624 albertel 8217: sub resdata {
8218: my ($name,$domain,$type,@which)=@_;
8219: my $result;
8220: if ($type eq 'course') {
8221: $result=&get_courseresdata($name,$domain);
8222: } elsif ($type eq 'user') {
8223: $result=&get_userresdata($name,$domain);
8224: }
8225: if (!ref($result)) { return $result; }
1.251 albertel 8226: foreach my $item (@which) {
1.927 albertel 8227: if (defined($result->{$item->[0]})) {
8228: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 8229: }
1.250 albertel 8230: }
1.291 albertel 8231: return undef;
1.200 www 8232: }
8233:
1.379 matthew 8234: #
8235: # EXT resource caching routines
8236: #
8237:
8238: sub clear_EXT_cache_status {
1.383 albertel 8239: &delenv('cache.EXT.');
1.379 matthew 8240: }
8241:
8242: sub EXT_cache_status {
8243: my ($target_domain,$target_user) = @_;
1.383 albertel 8244: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 8245: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 8246: # We know already the user has no data
8247: return 1;
8248: } else {
8249: return 0;
8250: }
8251: }
8252:
8253: sub EXT_cache_set {
8254: my ($target_domain,$target_user) = @_;
1.383 albertel 8255: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 8256: #&appenv({$cachename => time});
1.379 matthew 8257: }
8258:
1.28 www 8259: # --------------------------------------------------------- Value of a Variable
1.58 www 8260: sub EXT {
1.715 albertel 8261:
1.395 albertel 8262: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 8263: unless ($varname) { return ''; }
1.218 albertel 8264: #get real user name/domain, courseid and symb
8265: my $courseid;
1.359 albertel 8266: my $publicuser;
1.427 www 8267: if ($symbparm) {
8268: $symbparm=&get_symb_from_alias($symbparm);
8269: }
1.218 albertel 8270: if (!($uname && $udom)) {
1.790 albertel 8271: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 8272: if (!$symbparm) { $symbparm=$cursymb; }
8273: } else {
1.620 albertel 8274: $courseid=$env{'request.course.id'};
1.218 albertel 8275: }
1.48 www 8276: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
8277: my $rest;
1.320 albertel 8278: if (defined($therest[0])) {
1.48 www 8279: $rest=join('.',@therest);
8280: } else {
8281: $rest='';
8282: }
1.320 albertel 8283:
1.57 www 8284: my $qualifierrest=$qualifier;
8285: if ($rest) { $qualifierrest.='.'.$rest; }
8286: my $spacequalifierrest=$space;
8287: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 8288: if ($realm eq 'user') {
1.48 www 8289: # --------------------------------------------------------------- user.resource
8290: if ($space eq 'resource') {
1.651 albertel 8291: if ( (defined($Apache::lonhomework::parsing_a_problem)
8292: || defined($Apache::lonhomework::parsing_a_task))
8293: &&
1.744 albertel 8294: ($symbparm eq &symbread()) ) {
8295: # if we are in the middle of processing the resource the
8296: # get the value we are planning on committing
8297: if (defined($Apache::lonhomework::results{$qualifierrest})) {
8298: return $Apache::lonhomework::results{$qualifierrest};
8299: } else {
8300: return $Apache::lonhomework::history{$qualifierrest};
8301: }
1.335 albertel 8302: } else {
1.359 albertel 8303: my %restored;
1.620 albertel 8304: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 8305: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
8306: } else {
8307: %restored=&restore($symbparm,$courseid,$udom,$uname);
8308: }
1.335 albertel 8309: return $restored{$qualifierrest};
8310: }
1.48 www 8311: # ----------------------------------------------------------------- user.access
8312: } elsif ($space eq 'access') {
1.218 albertel 8313: # FIXME - not supporting calls for a specific user
1.48 www 8314: return &allowed($qualifier,$rest);
8315: # ------------------------------------------ user.preferences, user.environment
8316: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 8317: if (($uname eq $env{'user.name'}) &&
8318: ($udom eq $env{'user.domain'})) {
8319: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 8320: } else {
1.359 albertel 8321: my %returnhash;
8322: if (!$publicuser) {
8323: %returnhash=&userenvironment($udom,$uname,
8324: $qualifierrest);
8325: }
1.218 albertel 8326: return $returnhash{$qualifierrest};
8327: }
1.48 www 8328: # ----------------------------------------------------------------- user.course
8329: } elsif ($space eq 'course') {
1.218 albertel 8330: # FIXME - not supporting calls for a specific user
1.620 albertel 8331: return $env{join('.',('request.course',$qualifier))};
1.48 www 8332: # ------------------------------------------------------------------- user.role
8333: } elsif ($space eq 'role') {
1.218 albertel 8334: # FIXME - not supporting calls for a specific user
1.620 albertel 8335: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 8336: if ($qualifier eq 'value') {
8337: return $role;
8338: } elsif ($qualifier eq 'extent') {
8339: return $where;
8340: }
8341: # ----------------------------------------------------------------- user.domain
8342: } elsif ($space eq 'domain') {
1.218 albertel 8343: return $udom;
1.48 www 8344: # ------------------------------------------------------------------- user.name
8345: } elsif ($space eq 'name') {
1.218 albertel 8346: return $uname;
1.48 www 8347: # ---------------------------------------------------- Any other user namespace
1.29 www 8348: } else {
1.359 albertel 8349: my %reply;
8350: if (!$publicuser) {
8351: %reply=&get($space,[$qualifierrest],$udom,$uname);
8352: }
8353: return $reply{$qualifierrest};
1.48 www 8354: }
1.236 www 8355: } elsif ($realm eq 'query') {
8356: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 8357: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
8358: [$spacequalifierrest]);
1.620 albertel 8359: return $env{'form.'.$spacequalifierrest};
1.236 www 8360: } elsif ($realm eq 'request') {
1.48 www 8361: # ------------------------------------------------------------- request.browser
8362: if ($space eq 'browser') {
1.430 www 8363: if ($qualifier eq 'textremote') {
1.676 albertel 8364: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 8365: return 1;
8366: } else {
8367: return 0;
8368: }
8369: } else {
1.620 albertel 8370: return $env{'browser.'.$qualifier};
1.430 www 8371: }
1.57 www 8372: # ------------------------------------------------------------ request.filename
8373: } else {
1.620 albertel 8374: return $env{'request.'.$spacequalifierrest};
1.29 www 8375: }
1.28 www 8376: } elsif ($realm eq 'course') {
1.48 www 8377: # ---------------------------------------------------------- course.description
1.620 albertel 8378: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 8379: } elsif ($realm eq 'resource') {
1.165 www 8380:
1.620 albertel 8381: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 8382: if (!$symbparm) { $symbparm=&symbread(); }
8383: }
1.693 albertel 8384:
8385: if ($space eq 'title') {
8386: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
8387: return &gettitle($symbparm);
8388: }
8389:
8390: if ($space eq 'map') {
8391: my ($map) = &decode_symb($symbparm);
8392: return &symbread($map);
8393: }
1.905 albertel 8394: if ($space eq 'filename') {
8395: if ($symbparm) {
8396: return &clutter((&decode_symb($symbparm))[2]);
8397: }
8398: return &hreflocation('',$env{'request.filename'});
8399: }
1.693 albertel 8400:
8401: my ($section, $group, @groups);
1.593 albertel 8402: my ($courselevelm,$courselevel);
1.539 albertel 8403: if ($symbparm && defined($courseid) &&
1.620 albertel 8404: $courseid eq $env{'request.course.id'}) {
1.165 www 8405:
1.218 albertel 8406: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 8407:
1.60 www 8408: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 8409: my $symbp=$symbparm;
1.735 albertel 8410: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 8411:
8412: my $symbparm=$symbp.'.'.$spacequalifierrest;
8413: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
8414:
1.620 albertel 8415: if (($env{'user.name'} eq $uname) &&
8416: ($env{'user.domain'} eq $udom)) {
8417: $section=$env{'request.course.sec'};
1.733 raeburn 8418: @groups = split(/:/,$env{'request.course.groups'});
8419: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 8420: } else {
1.539 albertel 8421: if (! defined($usection)) {
1.551 albertel 8422: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 8423: } else {
8424: $section = $usection;
8425: }
1.733 raeburn 8426: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 8427: }
8428:
8429: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
8430: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
8431: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
8432:
1.593 albertel 8433: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 8434: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 8435: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 8436:
1.60 www 8437: # ----------------------------------------------------------- first, check user
1.624 albertel 8438:
8439: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 8440: ([$courselevelr,'resource'],
8441: [$courselevelm,'map' ],
8442: [$courselevel, 'course' ]));
1.931 albertel 8443: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 8444:
1.594 albertel 8445: # ------------------------------------------------ second, check some of course
1.684 raeburn 8446: my $coursereply;
1.691 raeburn 8447: if (@groups > 0) {
8448: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
8449: $mapparm,$spacequalifierrest);
1.927 albertel 8450: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 8451: }
1.96 www 8452:
1.684 raeburn 8453: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 8454: $env{'course.'.$courseid.'.domain'},
8455: 'course',
8456: ([$seclevelr, 'resource'],
8457: [$seclevelm, 'map' ],
8458: [$seclevel, 'course' ],
8459: [$courselevelr,'resource']));
8460: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 8461:
1.60 www 8462: # ------------------------------------------------------ third, check map parms
1.218 albertel 8463: my %parmhash=();
8464: my $thisparm='';
8465: if (tie(%parmhash,'GDBM_File',
1.620 albertel 8466: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 8467: &GDBM_READER(),0640)) {
1.218 albertel 8468: $thisparm=$parmhash{$symbparm};
8469: untie(%parmhash);
8470: }
1.927 albertel 8471: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 8472: }
1.594 albertel 8473: # ------------------------------------------ fourth, look in resource metadata
1.71 www 8474:
1.218 albertel 8475: $spacequalifierrest=~s/\./\_/;
1.282 albertel 8476: my $filename;
8477: if (!$symbparm) { $symbparm=&symbread(); }
8478: if ($symbparm) {
1.409 www 8479: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 8480: } else {
1.620 albertel 8481: $filename=$env{'request.filename'};
1.282 albertel 8482: }
8483: my $metadata=&metadata($filename,$spacequalifierrest);
1.927 albertel 8484: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282 albertel 8485: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927 albertel 8486: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 8487:
1.927 albertel 8488: # ---------------------------------------------- fourth, look in rest of course
1.593 albertel 8489: if ($symbparm && defined($courseid) &&
1.620 albertel 8490: $courseid eq $env{'request.course.id'}) {
1.624 albertel 8491: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
8492: $env{'course.'.$courseid.'.domain'},
8493: 'course',
1.927 albertel 8494: ([$courselevelm,'map' ],
8495: [$courselevel, 'course']));
8496: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 8497: }
1.145 www 8498: # ------------------------------------------------------------------ Cascade up
1.218 albertel 8499: unless ($space eq '0') {
1.336 albertel 8500: my @parts=split(/_/,$space);
8501: my $id=pop(@parts);
8502: my $part=join('_',@parts);
8503: if ($part eq '') { $part='0'; }
1.927 albertel 8504: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 8505: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 8506: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 8507: }
1.395 albertel 8508: if ($recurse) { return undef; }
8509: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 8510: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 8511: # ---------------------------------------------------- Any other user namespace
8512: } elsif ($realm eq 'environment') {
8513: # ----------------------------------------------------------------- environment
1.620 albertel 8514: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
8515: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 8516: } else {
1.770 albertel 8517: if ($uname eq 'anonymous' && $udom eq '') {
8518: return '';
8519: }
1.219 albertel 8520: my %returnhash=&userenvironment($udom,$uname,
8521: $spacequalifierrest);
8522: return $returnhash{$spacequalifierrest};
8523: }
1.28 www 8524: } elsif ($realm eq 'system') {
1.48 www 8525: # ----------------------------------------------------------------- system.time
8526: if ($space eq 'time') {
8527: return time;
8528: }
1.696 albertel 8529: } elsif ($realm eq 'server') {
8530: # ----------------------------------------------------------------- system.time
8531: if ($space eq 'name') {
8532: return $ENV{'SERVER_NAME'};
8533: }
1.28 www 8534: }
1.48 www 8535: return '';
1.61 www 8536: }
8537:
1.927 albertel 8538: sub get_reply {
8539: my ($reply_value) = @_;
1.940 raeburn 8540: if (ref($reply_value) eq 'ARRAY') {
8541: if (wantarray) {
8542: return @$reply_value;
8543: }
8544: return $reply_value->[0];
8545: } else {
8546: return $reply_value;
1.927 albertel 8547: }
8548: }
8549:
1.691 raeburn 8550: sub check_group_parms {
8551: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
8552: my @groupitems = ();
8553: my $resultitem;
1.927 albertel 8554: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 8555: foreach my $group (@{$groups}) {
8556: foreach my $level (@levels) {
1.927 albertel 8557: my $item = $courseid.'.['.$group.'].'.$level->[0];
8558: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 8559: }
8560: }
8561: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
8562: $env{'course.'.$courseid.'.domain'},
8563: 'course',@groupitems);
8564: return $coursereply;
8565: }
8566:
8567: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 8568: my ($courseid,@groups) = @_;
8569: @groups = sort(@groups);
1.691 raeburn 8570: return @groups;
8571: }
8572:
1.395 albertel 8573: sub packages_tab_default {
8574: my ($uri,$varname)=@_;
8575: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 8576:
8577: my (@extension,@specifics,$do_default);
8578: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 8579: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 8580: if ($pack_type eq 'default') {
8581: $do_default=1;
8582: } elsif ($pack_type eq 'extension') {
8583: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 8584: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 8585: # only look at packages defaults for packages that this id is
1.738 albertel 8586: push(@specifics,[$package,$pack_type,$pack_part]);
8587: }
8588: }
8589: # first look for a package that matches the requested part id
8590: foreach my $package (@specifics) {
8591: my (undef,$pack_type,$pack_part)=@{$package};
8592: next if ($pack_part ne $part);
8593: if (defined($packagetab{"$pack_type&$name&default"})) {
8594: return $packagetab{"$pack_type&$name&default"};
8595: }
8596: }
8597: # look for any possible matching non extension_ package
8598: foreach my $package (@specifics) {
8599: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 8600: if (defined($packagetab{"$pack_type&$name&default"})) {
8601: return $packagetab{"$pack_type&$name&default"};
8602: }
1.585 albertel 8603: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 8604: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
8605: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 8606: }
8607: }
1.738 albertel 8608: # look for any posible extension_ match
8609: foreach my $package (@extension) {
8610: my ($package,$pack_type)=@{$package};
8611: if (defined($packagetab{"$pack_type&$name&default"})) {
8612: return $packagetab{"$pack_type&$name&default"};
8613: }
8614: if (defined($packagetab{$package."&$name&default"})) {
8615: return $packagetab{$package."&$name&default"};
8616: }
8617: }
8618: # look for a global default setting
8619: if ($do_default && defined($packagetab{"default&$name&default"})) {
8620: return $packagetab{"default&$name&default"};
8621: }
1.395 albertel 8622: return undef;
8623: }
8624:
1.334 albertel 8625: sub add_prefix_and_part {
8626: my ($prefix,$part)=@_;
8627: my $keyroot;
8628: if (defined($prefix) && $prefix !~ /^__/) {
8629: # prefix that has a part already
8630: $keyroot=$prefix;
8631: } elsif (defined($prefix)) {
8632: # prefix that is missing a part
8633: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
8634: } else {
8635: # no prefix at all
8636: if (defined($part)) { $keyroot='_'.$part; }
8637: }
8638: return $keyroot;
8639: }
8640:
1.71 www 8641: # ---------------------------------------------------------------- Get metadata
8642:
1.599 albertel 8643: my %metaentry;
1.71 www 8644: sub metadata {
1.176 www 8645: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 8646: $uri=&declutter($uri);
1.288 albertel 8647: # if it is a non metadata possible uri return quickly
1.529 albertel 8648: if (($uri eq '') ||
8649: (($uri =~ m|^/*adm/|) &&
1.698 albertel 8650: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1056.4.26 raeburn 8651: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 8652: return undef;
8653: }
8654: if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/})
8655: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 8656: return undef;
1.288 albertel 8657: }
1.73 www 8658: my $filename=$uri;
8659: $uri=~s/\.meta$//;
1.172 www 8660: #
8661: # Is the metadata already cached?
1.177 www 8662: # Look at timestamp of caching
1.172 www 8663: # Everything is cached by the main uri, libraries are never directly cached
8664: #
1.428 albertel 8665: if (!defined($liburi)) {
1.599 albertel 8666: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 8667: if (defined($cached)) { return $result->{':'.$what}; }
8668: }
8669: {
1.172 www 8670: #
8671: # Is this a recursive call for a library?
8672: #
1.599 albertel 8673: # if (! exists($metacache{$uri})) {
8674: # $metacache{$uri}={};
8675: # }
1.924 albertel 8676: my $cachetime = 60*60;
1.171 www 8677: if ($liburi) {
8678: $liburi=&declutter($liburi);
8679: $filename=$liburi;
1.401 bowersj2 8680: } else {
1.599 albertel 8681: &devalidate_cache_new('meta',$uri);
8682: undef(%metaentry);
1.401 bowersj2 8683: }
1.140 www 8684: my %metathesekeys=();
1.73 www 8685: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 8686: my $metastring;
1.924 albertel 8687: if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929 albertel 8688: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 8689: $metastring =
1.929 albertel 8690: &Apache::lonnet::ssi_body($which,
1.924 albertel 8691: ('grade_target' => 'meta'));
8692: $cachetime = 1; # only want this cached in the child not long term
1.1056.4.26 raeburn 8693: } elsif (($uri !~ m -^(editupload)/-) &&
8694: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 8695: my $file=&filelocation('',&clutter($filename));
1.599 albertel 8696: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 8697: $metastring=&getfile($file);
1.489 albertel 8698: }
1.208 albertel 8699: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 8700: my $token;
1.140 www 8701: undef %metathesekeys;
1.71 www 8702: while ($token=$parser->get_token) {
1.339 albertel 8703: if ($token->[0] eq 'S') {
8704: if (defined($token->[2]->{'package'})) {
1.172 www 8705: #
8706: # This is a package - get package info
8707: #
1.339 albertel 8708: my $package=$token->[2]->{'package'};
8709: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8710: if (defined($token->[2]->{'id'})) {
8711: $keyroot.='_'.$token->[2]->{'id'};
8712: }
1.599 albertel 8713: if ($metaentry{':packages'}) {
8714: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 8715: } else {
1.599 albertel 8716: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 8717: }
1.736 albertel 8718: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 8719: my $part=$keyroot;
8720: $part=~s/^\_//;
1.736 albertel 8721: if ($pack_entry=~/^\Q$package\E\&/ ||
8722: $pack_entry=~/^\Q$package\E_0\&/) {
8723: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 8724: # ignore package.tab specified default values
8725: # here &package_tab_default() will fetch those
8726: if ($subp eq 'default') { next; }
1.736 albertel 8727: my $value=$packagetab{$pack_entry};
1.432 albertel 8728: my $unikey;
8729: if ($pack =~ /_0$/) {
8730: $unikey='parameter_0_'.$name;
8731: $part=0;
8732: } else {
8733: $unikey='parameter'.$keyroot.'_'.$name;
8734: }
1.339 albertel 8735: if ($subp eq 'display') {
8736: $value.=' [Part: '.$part.']';
8737: }
1.599 albertel 8738: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 8739: $metathesekeys{$unikey}=1;
1.599 albertel 8740: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8741: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 8742: }
1.599 albertel 8743: if (defined($metaentry{':'.$unikey.'.default'})) {
8744: $metaentry{':'.$unikey}=
8745: $metaentry{':'.$unikey.'.default'};
1.356 albertel 8746: }
1.339 albertel 8747: }
8748: }
8749: } else {
1.172 www 8750: #
8751: # This is not a package - some other kind of start tag
1.339 albertel 8752: #
8753: my $entry=$token->[1];
8754: my $unikey;
8755: if ($entry eq 'import') {
8756: $unikey='';
8757: } else {
8758: $unikey=$entry;
8759: }
8760: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8761:
8762: if (defined($token->[2]->{'id'})) {
8763: $unikey.='_'.$token->[2]->{'id'};
8764: }
1.175 www 8765:
1.339 albertel 8766: if ($entry eq 'import') {
1.175 www 8767: #
8768: # Importing a library here
1.339 albertel 8769: #
8770: if ($depthcount<20) {
8771: my $location=$parser->get_text('/import');
8772: my $dir=$filename;
8773: $dir=~s|[^/]*$||;
8774: $location=&filelocation($dir,$location);
1.736 albertel 8775: my $metadata =
8776: &metadata($uri,'keys', $location,$unikey,
8777: $depthcount+1);
8778: foreach my $meta (split(',',$metadata)) {
8779: $metaentry{':'.$meta}=$metaentry{':'.$meta};
8780: $metathesekeys{$meta}=1;
1.339 albertel 8781: }
8782: }
8783: } else {
8784: if (defined($token->[2]->{'name'})) {
8785: $unikey.='_'.$token->[2]->{'name'};
8786: }
8787: $metathesekeys{$unikey}=1;
1.736 albertel 8788: foreach my $param (@{$token->[3]}) {
8789: $metaentry{':'.$unikey.'.'.$param} =
8790: $token->[2]->{$param};
1.339 albertel 8791: }
8792: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 8793: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 8794: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
8795: # only ws inside the tag, and not in default, so use default
8796: # as value
1.599 albertel 8797: $metaentry{':'.$unikey}=$default;
1.908 albertel 8798: } elsif ( $internaltext =~ /\S/ ) {
8799: # something interesting inside the tag
8800: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 8801: } else {
1.908 albertel 8802: # no interesting values, don't set a default
1.339 albertel 8803: }
1.172 www 8804: # end of not-a-package not-a-library import
1.339 albertel 8805: }
1.172 www 8806: # end of not-a-package start tag
1.339 albertel 8807: }
1.172 www 8808: # the next is the end of "start tag"
1.339 albertel 8809: }
8810: }
1.483 albertel 8811: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 8812: $extension = lc($extension);
8813: if ($extension eq 'htm') { $extension='html'; }
8814:
1.737 albertel 8815: foreach my $key (keys(%packagetab)) {
1.483 albertel 8816: #no specific packages #how's our extension
8817: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 8818: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 8819: \%metathesekeys);
8820: }
1.883 albertel 8821:
8822: if (!exists($metaentry{':packages'})
8823: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 8824: foreach my $key (keys(%packagetab)) {
1.483 albertel 8825: #no specific packages well let's get default then
8826: if ($key!~/^default&/) { next; }
1.488 albertel 8827: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 8828: \%metathesekeys);
8829: }
8830: }
1.338 www 8831: # are there custom rights to evaluate
1.599 albertel 8832: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 8833:
1.338 www 8834: #
8835: # Importing a rights file here
1.339 albertel 8836: #
8837: unless ($depthcount) {
1.599 albertel 8838: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 8839: my $dir=$filename;
8840: $dir=~s|[^/]*$||;
8841: $location=&filelocation($dir,$location);
1.736 albertel 8842: my $rights_metadata =
8843: &metadata($uri,'keys',$location,'_rights',
8844: $depthcount+1);
8845: foreach my $rights (split(',',$rights_metadata)) {
8846: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
8847: $metathesekeys{$rights}=1;
1.339 albertel 8848: }
8849: }
8850: }
1.737 albertel 8851: # uniqifiy package listing
8852: my %seen;
8853: my @uniq_packages =
8854: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
8855: $metaentry{':packages'} = join(',',@uniq_packages);
8856:
8857: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 8858: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
8859: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924 albertel 8860: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 8861: # this is the end of "was not already recently cached
1.71 www 8862: }
1.599 albertel 8863: return $metaentry{':'.$what};
1.261 albertel 8864: }
8865:
1.488 albertel 8866: sub metadata_create_package_def {
1.483 albertel 8867: my ($uri,$key,$package,$metathesekeys)=@_;
8868: my ($pack,$name,$subp)=split(/\&/,$key);
8869: if ($subp eq 'default') { next; }
8870:
1.599 albertel 8871: if (defined($metaentry{':packages'})) {
8872: $metaentry{':packages'}.=','.$package;
1.483 albertel 8873: } else {
1.599 albertel 8874: $metaentry{':packages'}=$package;
1.483 albertel 8875: }
8876: my $value=$packagetab{$key};
8877: my $unikey;
8878: $unikey='parameter_0_'.$name;
1.599 albertel 8879: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 8880: $$metathesekeys{$unikey}=1;
1.599 albertel 8881: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8882: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 8883: }
1.599 albertel 8884: if (defined($metaentry{':'.$unikey.'.default'})) {
8885: $metaentry{':'.$unikey}=
8886: $metaentry{':'.$unikey.'.default'};
1.483 albertel 8887: }
8888: }
8889:
1.261 albertel 8890: sub metadata_generate_part0 {
8891: my ($metadata,$metacache,$uri) = @_;
8892: my %allnames;
1.737 albertel 8893: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 8894: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 8895: my $part=$$metacache{':'.$metakey.'.part'};
8896: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 8897: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 8898: $allnames{$name}=$part;
8899: }
8900: }
8901: }
8902: foreach my $name (keys(%allnames)) {
8903: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 8904: my $key=":parameter_0_$name";
1.261 albertel 8905: $$metacache{"$key.part"}='0';
8906: $$metacache{"$key.name"}=$name;
1.428 albertel 8907: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 8908: $allnames{$name}.'_'.$name.
8909: '.type'};
1.428 albertel 8910: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 8911: '.display'};
1.644 www 8912: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 8913: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 8914: $$metacache{"$key.display"}=$olddis;
8915: }
1.71 www 8916: }
8917:
1.764 albertel 8918: # ------------------------------------------------------ Devalidate title cache
8919:
8920: sub devalidate_title_cache {
8921: my ($url)=@_;
8922: if (!$env{'request.course.id'}) { return; }
8923: my $symb=&symbread($url);
8924: if (!$symb) { return; }
8925: my $key=$env{'request.course.id'}."\0".$symb;
8926: &devalidate_cache_new('title',$key);
8927: }
8928:
1.1014 droeschl 8929: # ------------------------------------------------- Get the title of a course
8930:
8931: sub current_course_title {
8932: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
8933: }
1.301 www 8934: # ------------------------------------------------- Get the title of a resource
8935:
8936: sub gettitle {
8937: my $urlsymb=shift;
8938: my $symb=&symbread($urlsymb);
1.534 albertel 8939: if ($symb) {
1.620 albertel 8940: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 8941: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 8942: if (defined($cached)) {
8943: return $result;
8944: }
1.534 albertel 8945: my ($map,$resid,$url)=&decode_symb($symb);
8946: my $title='';
1.907 albertel 8947: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
8948: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
8949: } else {
8950: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8951: &GDBM_READER(),0640)) {
8952: my $mapid=$bighash{'map_pc_'.&clutter($map)};
8953: $title=$bighash{'title_'.$mapid.'.'.$resid};
8954: untie(%bighash);
8955: }
1.534 albertel 8956: }
8957: $title=~s/\&colon\;/\:/gs;
8958: if ($title) {
1.599 albertel 8959: return &do_cache_new('title',$key,$title,600);
1.534 albertel 8960: }
8961: $urlsymb=$url;
8962: }
8963: my $title=&metadata($urlsymb,'title');
8964: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
8965: return $title;
1.301 www 8966: }
1.613 albertel 8967:
1.614 albertel 8968: sub get_slot {
8969: my ($which,$cnum,$cdom)=@_;
8970: if (!$cnum || !$cdom) {
1.790 albertel 8971: (undef,my $courseid)=&whichuser();
1.620 albertel 8972: $cdom=$env{'course.'.$courseid.'.domain'};
8973: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 8974: }
1.703 albertel 8975: my $key=join("\0",'slots',$cdom,$cnum,$which);
8976: my %slotinfo;
8977: if (exists($remembered{$key})) {
8978: $slotinfo{$which} = $remembered{$key};
8979: } else {
8980: %slotinfo=&get('slots',[$which],$cdom,$cnum);
8981: &Apache::lonhomework::showhash(%slotinfo);
8982: my ($tmp)=keys(%slotinfo);
8983: if ($tmp=~/^error:/) { return (); }
8984: $remembered{$key} = $slotinfo{$which};
8985: }
1.616 albertel 8986: if (ref($slotinfo{$which}) eq 'HASH') {
8987: return %{$slotinfo{$which}};
8988: }
8989: return $slotinfo{$which};
1.614 albertel 8990: }
1.31 www 8991: # ------------------------------------------------- Update symbolic store links
8992:
8993: sub symblist {
8994: my ($mapname,%newhash)=@_;
1.438 www 8995: $mapname=&deversion(&declutter($mapname));
1.31 www 8996: my %hash;
1.620 albertel 8997: if (($env{'request.course.fn'}) && (%newhash)) {
8998: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 8999: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 9000: foreach my $url (keys(%newhash)) {
1.711 albertel 9001: next if ($url eq 'last_known'
9002: && $env{'form.no_update_last_known'});
9003: $hash{declutter($url)}=&encode_symb($mapname,
9004: $newhash{$url}->[1],
9005: $newhash{$url}->[0]);
1.191 harris41 9006: }
1.31 www 9007: if (untie(%hash)) {
9008: return 'ok';
9009: }
9010: }
9011: }
9012: return 'error';
1.212 www 9013: }
9014:
9015: # --------------------------------------------------------------- Verify a symb
9016:
9017: sub symbverify {
1.510 www 9018: my ($symb,$thisurl)=@_;
9019: my $thisfn=$thisurl;
1.439 www 9020: $thisfn=&declutter($thisfn);
1.215 www 9021: # direct jump to resource in page or to a sequence - will construct own symbs
9022: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
9023: # check URL part
1.409 www 9024: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 9025:
1.431 www 9026: unless ($url eq $thisfn) { return 0; }
1.213 www 9027:
1.216 www 9028: $symb=&symbclean($symb);
1.510 www 9029: $thisurl=&deversion($thisurl);
1.439 www 9030: $thisfn=&deversion($thisfn);
1.213 www 9031:
9032: my %bighash;
9033: my $okay=0;
1.431 www 9034:
1.620 albertel 9035: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9036: &GDBM_READER(),0640)) {
1.1032 raeburn 9037: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
9038: $thisurl =~ s/\?.+$//;
9039: }
1.510 www 9040: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 9041: unless ($ids) {
1.1056.4.21 raeburn 9042: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
9043: $ids=$bighash{$idkey};
1.216 www 9044: }
9045: if ($ids) {
9046: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 9047: foreach my $id (split(/\,/,$ids)) {
9048: my ($mapid,$resid)=split(/\./,$id);
1.1032 raeburn 9049: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
9050: $symb =~ s/\?.+$//;
9051: }
1.216 www 9052: if (
9053: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
9054: eq $symb) {
1.620 albertel 9055: if (($env{'request.role.adv'}) ||
1.1056.4.20 raeburn 9056: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
9057: ($thisurl eq '/adm/navmaps')) {
1.582 albertel 9058: $okay=1;
9059: }
9060: }
1.216 www 9061: }
9062: }
1.213 www 9063: untie(%bighash);
9064: }
9065: return $okay;
1.31 www 9066: }
9067:
1.210 www 9068: # --------------------------------------------------------------- Clean-up symb
9069:
9070: sub symbclean {
9071: my $symb=shift;
1.568 albertel 9072: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 9073: # remove version from map
9074: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 9075:
1.210 www 9076: # remove version from URL
9077: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 9078:
1.507 www 9079: # remove wrapper
9080:
1.510 www 9081: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 9082: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 9083: return $symb;
1.409 www 9084: }
9085:
9086: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 9087:
9088: sub encode_symb {
9089: my ($map,$resid,$url)=@_;
9090: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
9091: }
1.409 www 9092:
9093: sub decode_symb {
1.568 albertel 9094: my $symb=shift;
9095: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
9096: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 9097: return (&fixversion($map),$resid,&fixversion($url));
9098: }
9099:
9100: sub fixversion {
9101: my $fn=shift;
1.609 banghart 9102: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 9103: my %bighash;
9104: my $uri=&clutter($fn);
1.620 albertel 9105: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 9106: # is this cached?
1.599 albertel 9107: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 9108: if (defined($cached)) { return $result; }
9109: # unfortunately not cached, or expired
1.620 albertel 9110: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 9111: &GDBM_READER(),0640)) {
9112: if ($bighash{'version_'.$uri}) {
9113: my $version=$bighash{'version_'.$uri};
1.444 www 9114: unless (($version eq 'mostrecent') ||
9115: ($version==&getversion($uri))) {
1.440 www 9116: $uri=~s/\.(\w+)$/\.$version\.$1/;
9117: }
9118: }
9119: untie %bighash;
1.413 www 9120: }
1.599 albertel 9121: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 9122: }
9123:
9124: sub deversion {
9125: my $url=shift;
9126: $url=~s/\.\d+\.(\w+)$/\.$1/;
9127: return $url;
1.210 www 9128: }
9129:
1.31 www 9130: # ------------------------------------------------------ Return symb list entry
9131:
9132: sub symbread {
1.249 www 9133: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 9134: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 9135: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 9136: # no filename provided? try from environment
1.44 www 9137: unless ($thisfn) {
1.620 albertel 9138: if ($env{'request.symb'}) {
9139: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 9140: }
1.620 albertel 9141: $thisfn=$env{'request.filename'};
1.44 www 9142: }
1.569 albertel 9143: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 9144: # is that filename actually a symb? Verify, clean, and return
9145: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 9146: if (&symbverify($thisfn,$1)) {
1.620 albertel 9147: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 9148: }
1.242 www 9149: }
1.44 www 9150: $thisfn=declutter($thisfn);
1.31 www 9151: my %hash;
1.37 www 9152: my %bighash;
9153: my $syval='';
1.620 albertel 9154: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 9155: my $targetfn = $thisfn;
1.609 banghart 9156: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 9157: $targetfn = 'adm/wrapper/'.$thisfn;
9158: }
1.687 albertel 9159: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
9160: $targetfn=$1;
9161: }
1.620 albertel 9162: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 9163: &GDBM_READER(),0640)) {
1.481 raeburn 9164: $syval=$hash{$targetfn};
1.37 www 9165: untie(%hash);
9166: }
9167: # ---------------------------------------------------------- There was an entry
9168: if ($syval) {
1.601 albertel 9169: #unless ($syval=~/\_\d+$/) {
1.620 albertel 9170: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 9171: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9172: #return $env{$cache_str}='';
1.601 albertel 9173: #}
9174: #$syval.=$1;
9175: #}
1.37 www 9176: } else {
9177: # ------------------------------------------------------- Was not in symb table
1.620 albertel 9178: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9179: &GDBM_READER(),0640)) {
1.37 www 9180: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 9181: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 9182: unless ($ids) {
9183: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 9184: }
9185: unless ($ids) {
9186: # alias?
9187: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 9188: }
1.37 www 9189: if ($ids) {
9190: # ------------------------------------------------------------------- Has ID(s)
9191: my @possibilities=split(/\,/,$ids);
1.39 www 9192: if ($#possibilities==0) {
9193: # ----------------------------------------------- There is only one possibility
1.37 www 9194: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 9195: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9196: $resid,$thisfn);
1.249 www 9197: } elsif (!$donotrecurse) {
1.39 www 9198: # ------------------------------------------ There is more than one possibility
9199: my $realpossible=0;
1.800 albertel 9200: foreach my $id (@possibilities) {
9201: my $file=$bighash{'src_'.$id};
1.39 www 9202: if (&allowed('bre',$file)) {
1.800 albertel 9203: my ($mapid,$resid)=split(/\./,$id);
1.39 www 9204: if ($bighash{'map_type_'.$mapid} ne 'page') {
9205: $realpossible++;
1.626 albertel 9206: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9207: $resid,$thisfn);
1.39 www 9208: }
9209: }
1.191 harris41 9210: }
1.39 www 9211: if ($realpossible!=1) { $syval=''; }
1.249 www 9212: } else {
9213: $syval='';
1.37 www 9214: }
9215: }
9216: untie(%bighash)
1.481 raeburn 9217: }
1.31 www 9218: }
1.62 www 9219: if ($syval) {
1.620 albertel 9220: return $env{$cache_str}=$syval;
1.62 www 9221: }
1.31 www 9222: }
1.949 raeburn 9223: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9224: return $env{$cache_str}='';
1.31 www 9225: }
9226:
9227: # ---------------------------------------------------------- Return random seed
9228:
1.32 www 9229: sub numval {
9230: my $txt=shift;
9231: $txt=~tr/A-J/0-9/;
9232: $txt=~tr/a-j/0-9/;
9233: $txt=~tr/K-T/0-9/;
9234: $txt=~tr/k-t/0-9/;
9235: $txt=~tr/U-Z/0-5/;
9236: $txt=~tr/u-z/0-5/;
9237: $txt=~s/\D//g;
1.564 albertel 9238: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 9239: return int($txt);
1.368 albertel 9240: }
9241:
1.484 albertel 9242: sub numval2 {
9243: my $txt=shift;
9244: $txt=~tr/A-J/0-9/;
9245: $txt=~tr/a-j/0-9/;
9246: $txt=~tr/K-T/0-9/;
9247: $txt=~tr/k-t/0-9/;
9248: $txt=~tr/U-Z/0-5/;
9249: $txt=~tr/u-z/0-5/;
9250: $txt=~s/\D//g;
9251: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9252: my $total;
9253: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 9254: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 9255: return int($total);
9256: }
9257:
1.575 albertel 9258: sub numval3 {
9259: use integer;
9260: my $txt=shift;
9261: $txt=~tr/A-J/0-9/;
9262: $txt=~tr/a-j/0-9/;
9263: $txt=~tr/K-T/0-9/;
9264: $txt=~tr/k-t/0-9/;
9265: $txt=~tr/U-Z/0-5/;
9266: $txt=~tr/u-z/0-5/;
9267: $txt=~s/\D//g;
9268: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9269: my $total;
9270: foreach my $val (@txts) { $total+=$val; }
9271: if ($_64bit) { $total=(($total<<32)>>32); }
9272: return $total;
9273: }
9274:
1.675 albertel 9275: sub digest {
9276: my ($data)=@_;
9277: my $digest=&Digest::MD5::md5($data);
9278: my ($a,$b,$c,$d)=unpack("iiii",$digest);
9279: my ($e,$f);
9280: {
9281: use integer;
9282: $e=($a+$b);
9283: $f=($c+$d);
9284: if ($_64bit) {
9285: $e=(($e<<32)>>32);
9286: $f=(($f<<32)>>32);
9287: }
9288: }
9289: if (wantarray) {
9290: return ($e,$f);
9291: } else {
9292: my $g;
9293: {
9294: use integer;
9295: $g=($e+$f);
9296: if ($_64bit) {
9297: $g=(($g<<32)>>32);
9298: }
9299: }
9300: return $g;
9301: }
9302: }
9303:
1.368 albertel 9304: sub latest_rnd_algorithm_id {
1.675 albertel 9305: return '64bit5';
1.366 albertel 9306: }
1.32 www 9307:
1.503 albertel 9308: sub get_rand_alg {
9309: my ($courseid)=@_;
1.790 albertel 9310: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 9311: if ($courseid) {
1.620 albertel 9312: return $env{"course.$courseid.rndseed"};
1.503 albertel 9313: }
9314: return &latest_rnd_algorithm_id();
9315: }
9316:
1.562 albertel 9317: sub validCODE {
9318: my ($CODE)=@_;
9319: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
9320: return 0;
9321: }
9322:
1.491 albertel 9323: sub getCODE {
1.620 albertel 9324: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 9325: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
9326: defined($Apache::lonhomework::parsing_a_task) ) &&
9327: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 9328: return $Apache::lonhomework::history{'resource.CODE'};
9329: }
9330: return undef;
9331: }
9332:
1.31 www 9333: sub rndseed {
1.155 albertel 9334: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 9335: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 9336: if (!defined($symb)) {
1.366 albertel 9337: unless ($symb=$wsymb) { return time; }
9338: }
9339: if (!$courseid) { $courseid=$wcourseid; }
9340: if (!$domain) { $domain=$wdomain; }
9341: if (!$username) { $username=$wusername }
1.503 albertel 9342: my $which=&get_rand_alg();
1.803 albertel 9343:
1.491 albertel 9344: if (defined(&getCODE())) {
1.675 albertel 9345: if ($which eq '64bit5') {
9346: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
9347: } elsif ($which eq '64bit4') {
1.575 albertel 9348: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
9349: } else {
9350: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
9351: }
1.675 albertel 9352: } elsif ($which eq '64bit5') {
9353: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 9354: } elsif ($which eq '64bit4') {
9355: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 9356: } elsif ($which eq '64bit3') {
9357: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 9358: } elsif ($which eq '64bit2') {
9359: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 9360: } elsif ($which eq '64bit') {
9361: return &rndseed_64bit($symb,$courseid,$domain,$username);
9362: }
9363: return &rndseed_32bit($symb,$courseid,$domain,$username);
9364: }
9365:
9366: sub rndseed_32bit {
9367: my ($symb,$courseid,$domain,$username)=@_;
9368: {
9369: use integer;
9370: my $symbchck=unpack("%32C*",$symb) << 27;
9371: my $symbseed=numval($symb) << 22;
9372: my $namechck=unpack("%32C*",$username) << 17;
9373: my $nameseed=numval($username) << 12;
9374: my $domainseed=unpack("%32C*",$domain) << 7;
9375: my $courseseed=unpack("%32C*",$courseid);
9376: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 9377: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9378: #&logthis("rndseed :$num:$symb");
1.564 albertel 9379: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 9380: return $num;
9381: }
9382: }
9383:
9384: sub rndseed_64bit {
9385: my ($symb,$courseid,$domain,$username)=@_;
9386: {
9387: use integer;
9388: my $symbchck=unpack("%32S*",$symb) << 21;
9389: my $symbseed=numval($symb) << 10;
9390: my $namechck=unpack("%32S*",$username);
9391:
9392: my $nameseed=numval($username) << 21;
9393: my $domainseed=unpack("%32S*",$domain) << 10;
9394: my $courseseed=unpack("%32S*",$courseid);
9395:
9396: my $num1=$symbchck+$symbseed+$namechck;
9397: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9398: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9399: #&logthis("rndseed :$num:$symb");
1.564 albertel 9400: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 9401: return "$num1,$num2";
1.155 albertel 9402: }
1.366 albertel 9403: }
9404:
1.443 albertel 9405: sub rndseed_64bit2 {
9406: my ($symb,$courseid,$domain,$username)=@_;
9407: {
9408: use integer;
9409: # strings need to be an even # of cahracters long, it it is odd the
9410: # last characters gets thrown away
9411: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9412: my $symbseed=numval($symb) << 10;
9413: my $namechck=unpack("%32S*",$username.' ');
9414:
9415: my $nameseed=numval($username) << 21;
1.501 albertel 9416: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9417: my $courseseed=unpack("%32S*",$courseid.' ');
9418:
9419: my $num1=$symbchck+$symbseed+$namechck;
9420: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9421: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9422: #&logthis("rndseed :$num:$symb");
1.803 albertel 9423: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 9424: return "$num1,$num2";
9425: }
9426: }
9427:
9428: sub rndseed_64bit3 {
9429: my ($symb,$courseid,$domain,$username)=@_;
9430: {
9431: use integer;
9432: # strings need to be an even # of cahracters long, it it is odd the
9433: # last characters gets thrown away
9434: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9435: my $symbseed=numval2($symb) << 10;
9436: my $namechck=unpack("%32S*",$username.' ');
9437:
9438: my $nameseed=numval2($username) << 21;
1.443 albertel 9439: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9440: my $courseseed=unpack("%32S*",$courseid.' ');
9441:
9442: my $num1=$symbchck+$symbseed+$namechck;
9443: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9444: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9445: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 9446: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9447:
1.503 albertel 9448: return "$num1:$num2";
1.443 albertel 9449: }
9450: }
9451:
1.575 albertel 9452: sub rndseed_64bit4 {
9453: my ($symb,$courseid,$domain,$username)=@_;
9454: {
9455: use integer;
9456: # strings need to be an even # of cahracters long, it it is odd the
9457: # last characters gets thrown away
9458: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9459: my $symbseed=numval3($symb) << 10;
9460: my $namechck=unpack("%32S*",$username.' ');
9461:
9462: my $nameseed=numval3($username) << 21;
9463: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9464: my $courseseed=unpack("%32S*",$courseid.' ');
9465:
9466: my $num1=$symbchck+$symbseed+$namechck;
9467: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9468: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9469: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 9470: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9471:
9472: return "$num1:$num2";
9473: }
9474: }
9475:
1.675 albertel 9476: sub rndseed_64bit5 {
9477: my ($symb,$courseid,$domain,$username)=@_;
9478: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
9479: return "$num1:$num2";
9480: }
9481:
1.366 albertel 9482: sub rndseed_CODE_64bit {
9483: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 9484: {
1.366 albertel 9485: use integer;
1.443 albertel 9486: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 9487: my $symbseed=numval2($symb);
1.491 albertel 9488: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9489: my $CODEseed=numval(&getCODE());
1.443 albertel 9490: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 9491: my $num1=$symbseed+$CODEchck;
9492: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9493: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9494: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 9495: if ($_64bit) { $num1=(($num1<<32)>>32); }
9496: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 9497: return "$num1:$num2";
1.366 albertel 9498: }
9499: }
9500:
1.575 albertel 9501: sub rndseed_CODE_64bit4 {
9502: my ($symb,$courseid,$domain,$username)=@_;
9503: {
9504: use integer;
9505: my $symbchck=unpack("%32S*",$symb.' ') << 16;
9506: my $symbseed=numval3($symb);
9507: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9508: my $CODEseed=numval3(&getCODE());
9509: my $courseseed=unpack("%32S*",$courseid.' ');
9510: my $num1=$symbseed+$CODEchck;
9511: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9512: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9513: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 9514: if ($_64bit) { $num1=(($num1<<32)>>32); }
9515: if ($_64bit) { $num2=(($num2<<32)>>32); }
9516: return "$num1:$num2";
9517: }
9518: }
9519:
1.675 albertel 9520: sub rndseed_CODE_64bit5 {
9521: my ($symb,$courseid,$domain,$username)=@_;
9522: my $code = &getCODE();
9523: my ($num1,$num2)=&digest("$symb,$courseid,$code");
9524: return "$num1:$num2";
9525: }
9526:
1.366 albertel 9527: sub setup_random_from_rndseed {
9528: my ($rndseed)=@_;
1.503 albertel 9529: if ($rndseed =~/([,:])/) {
9530: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 9531: &Math::Random::random_set_seed(abs($num1),abs($num2));
9532: } else {
9533: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 9534: }
1.36 albertel 9535: }
9536:
1.474 albertel 9537: sub latest_receipt_algorithm_id {
1.835 albertel 9538: return 'receipt3';
1.474 albertel 9539: }
9540:
1.480 www 9541: sub recunique {
9542: my $fucourseid=shift;
9543: my $unique;
1.835 albertel 9544: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
9545: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9546: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 9547: } else {
9548: $unique=$perlvar{'lonReceipt'};
9549: }
9550: return unpack("%32C*",$unique);
9551: }
9552:
9553: sub recprefix {
9554: my $fucourseid=shift;
9555: my $prefix;
1.835 albertel 9556: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
9557: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9558: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 9559: } else {
9560: $prefix=$perlvar{'lonHostID'};
9561: }
9562: return unpack("%32C*",$prefix);
9563: }
9564:
1.76 www 9565: sub ireceipt {
1.474 albertel 9566: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 9567:
9568: my $return =&recprefix($fucourseid).'-';
9569:
9570: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
9571: $env{'request.state'} eq 'construct') {
9572: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
9573: return $return;
9574: }
9575:
1.76 www 9576: my $cuname=unpack("%32C*",$funame);
9577: my $cudom=unpack("%32C*",$fudom);
9578: my $cucourseid=unpack("%32C*",$fucourseid);
9579: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 9580: my $cunique=&recunique($fucourseid);
1.474 albertel 9581: my $cpart=unpack("%32S*",$part);
1.835 albertel 9582: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
9583:
1.790 albertel 9584: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 9585:
9586: $return.= ($cunique%$cuname+
9587: $cunique%$cudom+
9588: $cusymb%$cuname+
9589: $cusymb%$cudom+
9590: $cucourseid%$cuname+
9591: $cucourseid%$cudom+
9592: $cpart%$cuname+
9593: $cpart%$cudom);
9594: } else {
9595: $return.= ($cunique%$cuname+
9596: $cunique%$cudom+
9597: $cusymb%$cuname+
9598: $cusymb%$cudom+
9599: $cucourseid%$cuname+
9600: $cucourseid%$cudom);
9601: }
9602: return $return;
1.76 www 9603: }
9604:
9605: sub receipt {
1.474 albertel 9606: my ($part)=@_;
1.790 albertel 9607: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 9608: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 9609: }
1.260 ng 9610:
1.790 albertel 9611: sub whichuser {
9612: my ($passedsymb)=@_;
9613: my ($symb,$courseid,$domain,$name,$publicuser);
9614: if (defined($env{'form.grade_symb'})) {
9615: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
9616: my $allowed=&allowed('vgr',$tmp_courseid);
9617: if (!$allowed &&
9618: exists($env{'request.course.sec'}) &&
9619: $env{'request.course.sec'} !~ /^\s*$/) {
9620: $allowed=&allowed('vgr',$tmp_courseid.
9621: '/'.$env{'request.course.sec'});
9622: }
9623: if ($allowed) {
9624: ($symb)=&get_env_multiple('form.grade_symb');
9625: $courseid=$tmp_courseid;
9626: ($domain)=&get_env_multiple('form.grade_domain');
9627: ($name)=&get_env_multiple('form.grade_username');
9628: return ($symb,$courseid,$domain,$name,$publicuser);
9629: }
9630: }
9631: if (!$passedsymb) {
9632: $symb=&symbread();
9633: } else {
9634: $symb=$passedsymb;
9635: }
9636: $courseid=$env{'request.course.id'};
9637: $domain=$env{'user.domain'};
9638: $name=$env{'user.name'};
9639: if ($name eq 'public' && $domain eq 'public') {
9640: if (!defined($env{'form.username'})) {
9641: $env{'form.username'}.=time.rand(10000000);
9642: }
9643: $name.=$env{'form.username'};
9644: }
9645: return ($symb,$courseid,$domain,$name,$publicuser);
9646:
9647: }
9648:
1.36 albertel 9649: # ------------------------------------------------------------ Serves up a file
1.472 albertel 9650: # returns either the contents of the file or
9651: # -1 if the file doesn't exist
1.481 raeburn 9652: #
9653: # if the target is a file that was uploaded via DOCS,
9654: # a check will be made to see if a current copy exists on the local server,
9655: # if it does this will be served, otherwise a copy will be retrieved from
9656: # the home server for the course and stored in /home/httpd/html/userfiles on
9657: # the local server.
1.472 albertel 9658:
1.36 albertel 9659: sub getfile {
1.538 albertel 9660: my ($file) = @_;
1.609 banghart 9661: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 9662: &repcopy($file);
9663: return &readfile($file);
9664: }
9665:
9666: sub repcopy_userfile {
9667: my ($file)=@_;
1.609 banghart 9668: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 9669: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 9670: my ($cdom,$cnum,$filename) =
1.811 albertel 9671: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 9672: my $uri="/uploaded/$cdom/$cnum/$filename";
9673: if (-e "$file") {
1.828 www 9674: # we already have a local copy, check it out
1.538 albertel 9675: my @fileinfo = stat($file);
1.828 www 9676: my $rtncode;
9677: my $info;
1.538 albertel 9678: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 9679: if ($lwpresp ne 'ok') {
1.828 www 9680: # there is no such file anymore, even though we had a local copy
1.482 albertel 9681: if ($rtncode eq '404') {
1.538 albertel 9682: unlink($file);
1.482 albertel 9683: }
9684: return -1;
9685: }
9686: if ($info < $fileinfo[9]) {
1.828 www 9687: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 9688: return 'ok';
1.828 www 9689: } else {
9690: # the file is outdated, get rid of it
9691: unlink($file);
1.482 albertel 9692: }
1.828 www 9693: }
9694: # one way or the other, at this point, we don't have the file
9695: # construct the correct path for the file
9696: my @parts = ($cdom,$cnum);
9697: if ($filename =~ m|^(.+)/[^/]+$|) {
9698: push @parts, split(/\//,$1);
9699: }
9700: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
9701: foreach my $part (@parts) {
9702: $path .= '/'.$part;
9703: if (!-e $path) {
9704: mkdir($path,0770);
1.482 albertel 9705: }
9706: }
1.828 www 9707: # now the path exists for sure
9708: # get a user agent
9709: my $ua=new LWP::UserAgent;
9710: my $transferfile=$file.'.in.transfer';
9711: # FIXME: this should flock
9712: if (-e $transferfile) { return 'ok'; }
9713: my $request;
9714: $uri=~s/^\///;
1.980 raeburn 9715: my $homeserver = &homeserver($cnum,$cdom);
9716: my $protocol = $protocol{$homeserver};
9717: $protocol = 'http' if ($protocol ne 'https');
9718: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 9719: my $response=$ua->request($request,$transferfile);
9720: # did it work?
9721: if ($response->is_error()) {
9722: unlink($transferfile);
9723: &logthis("Userfile repcopy failed for $uri");
9724: return -1;
9725: }
9726: # worked, rename the transfer file
9727: rename($transferfile,$file);
1.607 raeburn 9728: return 'ok';
1.481 raeburn 9729: }
9730:
1.517 albertel 9731: sub tokenwrapper {
9732: my $uri=shift;
1.980 raeburn 9733: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 9734: $uri=~s|^/||;
1.620 albertel 9735: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 9736: my $token=$1;
1.552 albertel 9737: my (undef,$udom,$uname,$file)=split('/',$uri,4);
9738: if ($udom && $uname && $file) {
9739: $file=~s|(\?\.*)*$||;
1.949 raeburn 9740: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 9741: my $homeserver = &homeserver($uname,$udom);
9742: my $protocol = $protocol{$homeserver};
9743: $protocol = 'http' if ($protocol ne 'https');
9744: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 9745: (($uri=~/\?/)?'&':'?').'token='.$token.
9746: '&tokenissued='.$perlvar{'lonHostID'};
9747: } else {
9748: return '/adm/notfound.html';
9749: }
9750: }
9751:
1.828 www 9752: # call with reqtype HEAD: get last modification time
9753: # call with reqtype GET: get the file contents
9754: # Do not call this with reqtype GET for large files! It loads everything into memory
9755: #
1.481 raeburn 9756: sub getuploaded {
9757: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
9758: $uri=~s/^\///;
1.980 raeburn 9759: my $homeserver = &homeserver($cnum,$cdom);
9760: my $protocol = $protocol{$homeserver};
9761: $protocol = 'http' if ($protocol ne 'https');
9762: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 9763: my $ua=new LWP::UserAgent;
9764: my $request=new HTTP::Request($reqtype,$uri);
9765: my $response=$ua->request($request);
9766: $$rtncode = $response->code;
1.482 albertel 9767: if (! $response->is_success()) {
9768: return 'failed';
9769: }
9770: if ($reqtype eq 'HEAD') {
1.486 www 9771: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 9772: } elsif ($reqtype eq 'GET') {
9773: $$info = $response->content;
1.472 albertel 9774: }
1.482 albertel 9775: return 'ok';
1.36 albertel 9776: }
9777:
1.481 raeburn 9778: sub readfile {
9779: my $file = shift;
9780: if ( (! -e $file ) || ($file eq '') ) { return -1; };
9781: my $fh;
9782: open($fh,"<$file");
9783: my $a='';
1.800 albertel 9784: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 9785: return $a;
9786: }
9787:
1.36 albertel 9788: sub filelocation {
1.590 banghart 9789: my ($dir,$file) = @_;
9790: my $location;
9791: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 9792:
9793: if ($file =~ m-^/adm/-) {
9794: $file=~s-^/adm/wrapper/-/-;
9795: $file=~s-^/adm/coursedocs/showdoc/-/-;
9796: }
1.882 albertel 9797:
1.590 banghart 9798: if ($file=~m:^/~:) { # is a contruction space reference
9799: $location = $file;
9800: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 9801: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 9802: # is a correct contruction space reference
9803: $location = $file;
1.956 raeburn 9804: } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
9805: $location = $file;
1.609 banghart 9806: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 9807: my ($udom,$uname,$filename)=
1.811 albertel 9808: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 9809: my $home=&homeserver($uname,$udom);
9810: my $is_me=0;
9811: my @ids=¤t_machine_ids();
9812: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
9813: if ($is_me) {
1.955 raeburn 9814: $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 9815: } else {
9816: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
9817: $udom.'/'.$uname.'/'.$filename;
9818: }
1.882 albertel 9819: } elsif ($file =~ m-^/adm/-) {
9820: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 9821: } else {
9822: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
9823: $file=~s:^/res/:/:;
9824: if ( !( $file =~ m:^/:) ) {
9825: $location = $dir. '/'.$file;
9826: } else {
9827: $location = '/home/httpd/html/res'.$file;
9828: }
1.59 albertel 9829: }
1.590 banghart 9830: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 9831: while ($location=~m{/\.\./}) {
9832: if ($location =~ m{/[^/]+/\.\./}) {
9833: $location=~ s{/[^/]+/\.\./}{/}g;
9834: } else {
9835: $location=~ s{/\.\./}{/}g;
9836: }
9837: } #remove dir/..
1.590 banghart 9838: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
9839: return $location;
1.46 www 9840: }
1.36 albertel 9841:
1.46 www 9842: sub hreflocation {
9843: my ($dir,$file)=@_;
1.980 raeburn 9844: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 9845: $file=filelocation($dir,$file);
1.700 albertel 9846: } elsif ($file=~m-^/adm/-) {
9847: $file=~s-^/adm/wrapper/-/-;
9848: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 9849: }
9850: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
9851: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 9852: } elsif ($file=~m-/home/($match_username)/public_html/-) {
9853: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 9854: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 9855: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 9856: -/uploaded/$1/$2/-x;
1.46 www 9857: }
1.913 albertel 9858: if ($file=~ m{^/userfiles/}) {
9859: $file =~ s{^/userfiles/}{/uploaded/};
9860: }
1.462 albertel 9861: return $file;
1.465 albertel 9862: }
9863:
9864: sub current_machine_domains {
1.853 albertel 9865: return &machine_domains(&hostname($perlvar{'lonHostID'}));
9866: }
9867:
9868: sub machine_domains {
9869: my ($hostname) = @_;
1.465 albertel 9870: my @domains;
1.838 albertel 9871: my %hostname = &all_hostnames();
1.465 albertel 9872: while( my($id, $name) = each(%hostname)) {
1.467 matthew 9873: # &logthis("-$id-$name-$hostname-");
1.465 albertel 9874: if ($hostname eq $name) {
1.844 albertel 9875: push(@domains,&host_domain($id));
1.465 albertel 9876: }
9877: }
9878: return @domains;
9879: }
9880:
9881: sub current_machine_ids {
1.853 albertel 9882: return &machine_ids(&hostname($perlvar{'lonHostID'}));
9883: }
9884:
9885: sub machine_ids {
9886: my ($hostname) = @_;
9887: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 9888: my @ids;
1.888 albertel 9889: my %name_to_host = &all_names();
1.889 albertel 9890: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
9891: return @{ $name_to_host{$hostname} };
9892: }
9893: return;
1.31 www 9894: }
9895:
1.824 raeburn 9896: sub additional_machine_domains {
9897: my @domains;
9898: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
9899: while( my $line = <$fh>) {
9900: $line =~ s/\s//g;
9901: push(@domains,$line);
9902: }
9903: return @domains;
9904: }
9905:
9906: sub default_login_domain {
9907: my $domain = $perlvar{'lonDefDomain'};
9908: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
9909: foreach my $posdom (¤t_machine_domains(),
9910: &additional_machine_domains()) {
9911: if (lc($posdom) eq lc($testdomain)) {
9912: $domain=$posdom;
9913: last;
9914: }
9915: }
9916: return $domain;
9917: }
9918:
1.31 www 9919: # ------------------------------------------------------------- Declutters URLs
9920:
9921: sub declutter {
9922: my $thisfn=shift;
1.569 albertel 9923: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 9924: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 9925: $thisfn=~s/^\///;
1.697 albertel 9926: $thisfn=~s|^adm/wrapper/||;
9927: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 9928: $thisfn=~s/^res\///;
1.1032 raeburn 9929: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
9930: $thisfn=~s/\?.+$//;
9931: }
1.268 www 9932: return $thisfn;
9933: }
9934:
9935: # ------------------------------------------------------------- Clutter up URLs
9936:
9937: sub clutter {
9938: my $thisfn='/'.&declutter(shift);
1.887 albertel 9939: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 9940: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 9941: $thisfn='/res'.$thisfn;
9942: }
1.1031 raeburn 9943: if ($thisfn !~m|^/adm|) {
9944: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 9945: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 9946: } else {
9947: my ($ext) = ($thisfn =~ /\.(\w+)$/);
9948: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 9949: if ($embstyle eq 'ssi'
9950: || ($embstyle eq 'hdn')
9951: || ($embstyle eq 'rat')
9952: || ($embstyle eq 'prv')
9953: || ($embstyle eq 'ign')) {
9954: #do nothing with these
9955: } elsif (($embstyle eq 'img')
1.695 albertel 9956: || ($embstyle eq 'emb')
9957: || ($embstyle eq 'wrp')) {
9958: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 9959: } elsif ($embstyle eq 'unk'
9960: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 9961: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 9962: } else {
1.718 www 9963: # &logthis("Got a blank emb style");
1.695 albertel 9964: }
1.694 albertel 9965: }
9966: }
1.31 www 9967: return $thisfn;
1.12 www 9968: }
9969:
1.787 albertel 9970: sub clutter_with_no_wrapper {
9971: my $uri = &clutter(shift);
9972: if ($uri =~ m-^/adm/-) {
9973: $uri =~ s-^/adm/wrapper/-/-;
9974: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
9975: }
9976: return $uri;
9977: }
9978:
1.557 albertel 9979: sub freeze_escape {
9980: my ($value)=@_;
9981: if (ref($value)) {
9982: $value=&nfreeze($value);
9983: return '__FROZEN__'.&escape($value);
9984: }
9985: return &escape($value);
9986: }
9987:
1.11 www 9988:
1.557 albertel 9989: sub thaw_unescape {
9990: my ($value)=@_;
9991: if ($value =~ /^__FROZEN__/) {
9992: substr($value,0,10,undef);
9993: $value=&unescape($value);
9994: return &thaw($value);
9995: }
9996: return &unescape($value);
9997: }
9998:
1.436 albertel 9999: sub correct_line_ends {
10000: my ($result)=@_;
10001: $$result =~s/\r\n/\n/mg;
10002: $$result =~s/\r/\n/mg;
1.415 albertel 10003: }
1.1 albertel 10004: # ================================================================ Main Program
10005:
1.184 www 10006: sub goodbye {
1.204 albertel 10007: &logthis("Starting Shut down");
1.443 albertel 10008: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 10009: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 10010: #converted
1.599 albertel 10011: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 10012: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
10013: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
10014: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 10015: #1.1 only
1.870 albertel 10016: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
10017: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
10018: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
10019: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
10020: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 10021: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
10022: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 10023: &flushcourselogs();
10024: &logthis("Shutting down");
10025: }
10026:
1.852 albertel 10027: sub get_dns {
1.869 albertel 10028: my ($url,$func,$ignore_cache) = @_;
10029: if (!$ignore_cache) {
10030: my ($content,$cached)=
10031: &Apache::lonnet::is_cached_new('dns',$url);
10032: if ($cached) {
10033: &$func($content);
10034: return;
10035: }
10036: }
10037:
10038: my %alldns;
1.852 albertel 10039: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10040: foreach my $dns (<$config>) {
10041: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 10042: my $line = $1;
10043: my ($host,$protocol) = split(/:/,$line);
10044: if ($protocol ne 'https') {
10045: $protocol = 'http';
10046: }
10047: $alldns{$host} = $protocol;
1.869 albertel 10048: }
10049: while (%alldns) {
10050: my ($dns) = keys(%alldns);
1.852 albertel 10051: my $ua=new LWP::UserAgent;
1.1056.4.33 raeburn 10052: $ua->timeout(30);
1.979 raeburn 10053: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 10054: my $response=$ua->request($request);
1.979 raeburn 10055: delete($alldns{$dns});
1.852 albertel 10056: next if ($response->is_error());
10057: my @content = split("\n",$response->content);
1.869 albertel 10058: &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852 albertel 10059: &$func(\@content);
1.869 albertel 10060: return;
1.852 albertel 10061: }
10062: close($config);
1.871 albertel 10063: my $which = (split('/',$url))[3];
10064: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
10065: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 10066: my @content = <$config>;
10067: &$func(\@content);
10068: return;
1.852 albertel 10069: }
1.327 albertel 10070: # ------------------------------------------------------------ Read domain file
10071: {
1.852 albertel 10072: my $loaded;
1.846 albertel 10073: my %domain;
10074:
1.852 albertel 10075: sub parse_domain_tab {
10076: my ($lines) = @_;
10077: foreach my $line (@$lines) {
10078: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 10079:
1.846 albertel 10080: chomp($line);
1.852 albertel 10081: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 10082: my %this_domain;
10083: foreach my $field ('description', 'auth_def', 'auth_arg_def',
10084: 'lang_def', 'city', 'longi', 'lati',
10085: 'primary') {
10086: $this_domain{$field} = shift(@elements);
10087: }
10088: $domain{$name} = \%this_domain;
1.852 albertel 10089: }
10090: }
1.864 albertel 10091:
10092: sub reset_domain_info {
10093: undef($loaded);
10094: undef(%domain);
10095: }
10096:
1.852 albertel 10097: sub load_domain_tab {
1.869 albertel 10098: my ($ignore_cache) = @_;
10099: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852 albertel 10100: my $fh;
10101: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
10102: my @lines = <$fh>;
10103: &parse_domain_tab(\@lines);
1.448 albertel 10104: }
1.852 albertel 10105: close($fh);
10106: $loaded = 1;
1.327 albertel 10107: }
1.846 albertel 10108:
10109: sub domain {
1.852 albertel 10110: &load_domain_tab() if (!$loaded);
10111:
1.846 albertel 10112: my ($name,$what) = @_;
10113: return if ( !exists($domain{$name}) );
10114:
10115: if (!$what) {
10116: return $domain{$name}{'description'};
10117: }
10118: return $domain{$name}{$what};
10119: }
1.974 raeburn 10120:
10121: sub domain_info {
10122: &load_domain_tab() if (!$loaded);
10123: return %domain;
10124: }
10125:
1.327 albertel 10126: }
10127:
10128:
1.1 albertel 10129: # ------------------------------------------------------------- Read hosts file
10130: {
1.838 albertel 10131: my %hostname;
1.844 albertel 10132: my %hostdom;
1.845 albertel 10133: my %libserv;
1.852 albertel 10134: my $loaded;
1.888 albertel 10135: my %name_to_host;
1.1056.4.6 raeburn 10136: my %internetdom;
1.1056.4.25 raeburn 10137: my %LC_dns_serv;
1.852 albertel 10138:
10139: sub parse_hosts_tab {
10140: my ($file) = @_;
10141: foreach my $configline (@$file) {
10142: next if ($configline =~ /^(\#|\s*$ )/x);
1.1056.4.25 raeburn 10143: chomp($configline);
10144: if ($configline =~ /^\^/) {
10145: if ($configline =~ /^\^([\w.\-]+)/) {
10146: $LC_dns_serv{$1} = 1;
10147: }
10148: next;
10149: }
1.1056.4.6 raeburn 10150: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 10151: $name=~s/\s//g;
10152: if ($id && $domain && $role && $name) {
10153: $hostname{$id}=$name;
1.888 albertel 10154: push(@{$name_to_host{$name}}, $id);
1.852 albertel 10155: $hostdom{$id}=$domain;
10156: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 10157: if (defined($protocol)) {
10158: if ($protocol eq 'https') {
10159: $protocol{$id} = $protocol;
10160: } else {
10161: $protocol{$id} = 'http';
10162: }
1.968 raeburn 10163: } else {
1.969 raeburn 10164: $protocol{$id} = 'http';
1.968 raeburn 10165: }
1.1056.4.6 raeburn 10166: if (defined($intdom)) {
10167: $internetdom{$id} = $intdom;
10168: }
1.852 albertel 10169: }
10170: }
10171: }
1.864 albertel 10172:
10173: sub reset_hosts_info {
1.897 albertel 10174: &purge_remembered();
1.864 albertel 10175: &reset_domain_info();
10176: &reset_hosts_ip_info();
1.892 albertel 10177: undef(%name_to_host);
1.864 albertel 10178: undef(%hostname);
10179: undef(%hostdom);
10180: undef(%libserv);
10181: undef($loaded);
10182: }
1.1 albertel 10183:
1.852 albertel 10184: sub load_hosts_tab {
1.869 albertel 10185: my ($ignore_cache) = @_;
10186: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852 albertel 10187: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10188: my @config = <$config>;
10189: &parse_hosts_tab(\@config);
10190: close($config);
10191: $loaded=1;
1.1 albertel 10192: }
1.852 albertel 10193:
1.838 albertel 10194: sub hostname {
1.852 albertel 10195: &load_hosts_tab() if (!$loaded);
10196:
1.838 albertel 10197: my ($lonid) = @_;
10198: return $hostname{$lonid};
10199: }
1.845 albertel 10200:
1.838 albertel 10201: sub all_hostnames {
1.852 albertel 10202: &load_hosts_tab() if (!$loaded);
10203:
1.838 albertel 10204: return %hostname;
10205: }
1.845 albertel 10206:
1.888 albertel 10207: sub all_names {
10208: &load_hosts_tab() if (!$loaded);
10209:
10210: return %name_to_host;
10211: }
10212:
1.974 raeburn 10213: sub all_host_domain {
10214: &load_hosts_tab() if (!$loaded);
10215: return %hostdom;
10216: }
10217:
1.845 albertel 10218: sub is_library {
1.852 albertel 10219: &load_hosts_tab() if (!$loaded);
10220:
1.845 albertel 10221: return exists($libserv{$_[0]});
10222: }
10223:
10224: sub all_library {
1.852 albertel 10225: &load_hosts_tab() if (!$loaded);
10226:
1.845 albertel 10227: return %libserv;
10228: }
10229:
1.1056.4.2 raeburn 10230: sub unique_library {
10231: #2x reverse removes all hostnames that appear more than once
10232: my %unique = reverse &all_library();
10233: return reverse %unique;
10234: }
10235:
1.841 albertel 10236: sub get_servers {
1.852 albertel 10237: &load_hosts_tab() if (!$loaded);
10238:
1.841 albertel 10239: my ($domain,$type) = @_;
10240: my %possible_hosts = ($type eq 'library') ? %libserv
10241: : %hostname;
10242: my %result;
1.842 albertel 10243: if (ref($domain) eq 'ARRAY') {
10244: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 10245: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 10246: $result{$host} = $hostname;
10247: }
10248: }
10249: } else {
10250: while ( my ($host,$hostname) = each(%possible_hosts)) {
10251: if ($hostdom{$host} eq $domain) {
10252: $result{$host} = $hostname;
10253: }
1.841 albertel 10254: }
10255: }
10256: return %result;
10257: }
1.845 albertel 10258:
1.1056.4.2 raeburn 10259: sub get_unique_servers {
10260: my %unique = reverse &get_servers(@_);
10261: return reverse %unique;
10262: }
10263:
1.844 albertel 10264: sub host_domain {
1.852 albertel 10265: &load_hosts_tab() if (!$loaded);
10266:
1.844 albertel 10267: my ($lonid) = @_;
10268: return $hostdom{$lonid};
10269: }
10270:
1.841 albertel 10271: sub all_domains {
1.852 albertel 10272: &load_hosts_tab() if (!$loaded);
10273:
1.841 albertel 10274: my %seen;
10275: my @uniq = grep(!$seen{$_}++, values(%hostdom));
10276: return @uniq;
10277: }
1.1056.4.3 raeburn 10278:
10279: sub internet_dom {
10280: &load_hosts_tab() if (!$loaded);
10281:
10282: my ($lonid) = @_;
10283: return $internetdom{$lonid};
10284: }
1.1056.4.27 raeburn 10285:
10286: sub is_LC_dns {
10287: &load_hosts_tab() if (!$loaded);
10288:
10289: my ($hostname) = @_;
10290: return exists($LC_dns_serv{$hostname});
10291: }
10292:
1.1 albertel 10293: }
10294:
1.847 albertel 10295: {
10296: my %iphost;
1.856 albertel 10297: my %name_to_ip;
10298: my %lonid_to_ip;
1.869 albertel 10299:
1.847 albertel 10300: sub get_hosts_from_ip {
10301: my ($ip) = @_;
10302: my %iphosts = &get_iphost();
10303: if (ref($iphosts{$ip})) {
10304: return @{$iphosts{$ip}};
10305: }
10306: return;
1.839 albertel 10307: }
1.864 albertel 10308:
10309: sub reset_hosts_ip_info {
10310: undef(%iphost);
10311: undef(%name_to_ip);
10312: undef(%lonid_to_ip);
10313: }
1.856 albertel 10314:
10315: sub get_host_ip {
10316: my ($lonid) = @_;
10317: if (exists($lonid_to_ip{$lonid})) {
10318: return $lonid_to_ip{$lonid};
10319: }
10320: my $name=&hostname($lonid);
10321: my $ip = gethostbyname($name);
10322: return if (!$ip || length($ip) ne 4);
10323: $ip=inet_ntoa($ip);
10324: $name_to_ip{$name} = $ip;
10325: $lonid_to_ip{$lonid} = $ip;
10326: return $ip;
10327: }
1.847 albertel 10328:
10329: sub get_iphost {
1.869 albertel 10330: my ($ignore_cache) = @_;
1.894 albertel 10331:
1.869 albertel 10332: if (!$ignore_cache) {
10333: if (%iphost) {
10334: return %iphost;
10335: }
10336: my ($ip_info,$cached)=
10337: &Apache::lonnet::is_cached_new('iphost','iphost');
10338: if ($cached) {
10339: %iphost = %{$ip_info->[0]};
10340: %name_to_ip = %{$ip_info->[1]};
10341: %lonid_to_ip = %{$ip_info->[2]};
10342: return %iphost;
10343: }
10344: }
1.894 albertel 10345:
10346: # get yesterday's info for fallback
10347: my %old_name_to_ip;
10348: my ($ip_info,$cached)=
10349: &Apache::lonnet::is_cached_new('iphost','iphost');
10350: if ($cached) {
10351: %old_name_to_ip = %{$ip_info->[1]};
10352: }
10353:
1.888 albertel 10354: my %name_to_host = &all_names();
10355: foreach my $name (keys(%name_to_host)) {
1.847 albertel 10356: my $ip;
10357: if (!exists($name_to_ip{$name})) {
10358: $ip = gethostbyname($name);
10359: if (!$ip || length($ip) ne 4) {
1.894 albertel 10360: if (defined($old_name_to_ip{$name})) {
10361: $ip = $old_name_to_ip{$name};
10362: &logthis("Can't find $name defaulting to old $ip");
10363: } else {
10364: &logthis("Name $name no IP found");
10365: next;
10366: }
10367: } else {
10368: $ip=inet_ntoa($ip);
1.847 albertel 10369: }
10370: $name_to_ip{$name} = $ip;
10371: } else {
10372: $ip = $name_to_ip{$name};
1.653 albertel 10373: }
1.888 albertel 10374: foreach my $id (@{ $name_to_host{$name} }) {
10375: $lonid_to_ip{$id} = $ip;
10376: }
10377: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 10378: }
1.869 albertel 10379: &Apache::lonnet::do_cache_new('iphost','iphost',
10380: [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894 albertel 10381: 48*60*60);
1.869 albertel 10382:
1.847 albertel 10383: return %iphost;
1.598 albertel 10384: }
10385:
1.992 raeburn 10386: #
10387: # Given a DNS returns the loncapa host name for that DNS
10388: #
10389: sub host_from_dns {
10390: my ($dns) = @_;
10391: my @hosts;
10392: my $ip;
10393:
1.993 raeburn 10394: if (exists($name_to_ip{$dns})) {
1.992 raeburn 10395: $ip = $name_to_ip{$dns};
10396: }
10397: if (!$ip) {
10398: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
10399: if (length($ip) == 4) {
10400: $ip = &IO::Socket::inet_ntoa($ip);
10401: }
10402: }
10403: if ($ip) {
10404: @hosts = get_hosts_from_ip($ip);
10405: return $hosts[0];
10406: }
10407: return undef;
1.986 foxr 10408: }
1.992 raeburn 10409:
1.1056.4.3 raeburn 10410: sub get_internet_names {
10411: my ($lonid) = @_;
10412: return if ($lonid eq '');
10413: my ($idnref,$cached)=
10414: &Apache::lonnet::is_cached_new('internetnames',$lonid);
10415: if ($cached) {
10416: return $idnref;
10417: }
10418: my $ip = &get_host_ip($lonid);
10419: my @hosts = &get_hosts_from_ip($ip);
10420: my %iphost = &get_iphost();
10421: my (@idns,%seen);
10422: foreach my $id (@hosts) {
10423: my $dom = &host_domain($id);
10424: my $prim_id = &domain($dom,'primary');
10425: my $prim_ip = &get_host_ip($prim_id);
10426: next if ($seen{$prim_ip});
10427: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
10428: foreach my $id (@{$iphost{$prim_ip}}) {
10429: my $intdom = &internet_dom($id);
10430: unless (grep(/^\Q$intdom\E$/,@idns)) {
10431: push(@idns,$intdom);
10432: }
10433: }
10434: }
10435: $seen{$prim_ip} = 1;
10436: }
10437: return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
10438: }
10439:
10440: }
10441:
10442: sub all_loncaparevs {
10443: return qw(1.1 1.2 1.3 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10);
1.986 foxr 10444: }
10445:
1.862 albertel 10446: BEGIN {
10447:
10448: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
10449: unless ($readit) {
10450: {
10451: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
10452: %perlvar = (%perlvar,%{$configvars});
10453: }
10454:
10455:
1.1 albertel 10456: # ------------------------------------------------------ Read spare server file
10457: {
1.448 albertel 10458: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 10459:
10460: while (my $configline=<$config>) {
10461: chomp($configline);
1.284 matthew 10462: if ($configline) {
1.784 albertel 10463: my ($host,$type) = split(':',$configline,2);
1.785 albertel 10464: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 10465: push(@{ $spareid{$type} }, $host);
1.1 albertel 10466: }
10467: }
1.448 albertel 10468: close($config);
1.1 albertel 10469: }
1.11 www 10470: # ------------------------------------------------------------ Read permissions
10471: {
1.448 albertel 10472: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 10473:
10474: while (my $configline=<$config>) {
1.448 albertel 10475: chomp($configline);
10476: if ($configline) {
10477: my ($role,$perm)=split(/ /,$configline);
10478: if ($perm ne '') { $pr{$role}=$perm; }
10479: }
1.11 www 10480: }
1.448 albertel 10481: close($config);
1.11 www 10482: }
10483:
10484: # -------------------------------------------- Read plain texts for permissions
10485: {
1.448 albertel 10486: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 10487:
10488: while (my $configline=<$config>) {
1.448 albertel 10489: chomp($configline);
10490: if ($configline) {
1.742 raeburn 10491: my ($short,@plain)=split(/:/,$configline);
10492: %{$prp{$short}} = ();
10493: if (@plain > 0) {
10494: $prp{$short}{'std'} = $plain[0];
10495: for (my $i=1; $i<@plain; $i++) {
10496: $prp{$short}{'alt'.$i} = $plain[$i];
10497: }
10498: }
1.448 albertel 10499: }
1.135 www 10500: }
1.448 albertel 10501: close($config);
1.135 www 10502: }
10503:
10504: # ---------------------------------------------------------- Read package table
10505: {
1.448 albertel 10506: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 10507:
10508: while (my $configline=<$config>) {
1.483 albertel 10509: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 10510: chomp($configline);
10511: my ($short,$plain)=split(/:/,$configline);
10512: my ($pack,$name)=split(/\&/,$short);
10513: if ($plain ne '') {
10514: $packagetab{$pack.'&'.$name.'&name'}=$name;
10515: $packagetab{$short}=$plain;
10516: }
1.11 www 10517: }
1.448 albertel 10518: close($config);
1.329 matthew 10519: }
10520:
1.1056.4.3 raeburn 10521: # ---------------------------------------------------------- Read loncaparev table
10522: {
10523: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
10524: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
10525: while (my $configline=<$config>) {
10526: chomp($configline);
10527: my ($hostid,$loncaparev)=split(/:/,$configline);
10528: $loncaparevs{$hostid}=$loncaparev;
10529: }
10530: close($config);
10531: }
10532: }
10533: }
10534:
10535: # ---------------------------------------------------------- Read serverhostID table
10536: {
10537: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
10538: if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
10539: while (my $configline=<$config>) {
10540: chomp($configline);
10541: my ($name,$id)=split(/:/,$configline);
10542: $serverhomeIDs{$name}=$id;
10543: }
10544: close($config);
10545: }
10546: }
10547: }
10548:
1.1056.4.5 raeburn 10549: {
10550: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
10551: if (-e $file) {
10552: my $parser = HTML::LCParser->new($file);
10553: while (my $token = $parser->get_token()) {
10554: if ($token->[0] eq 'S') {
10555: my $item = $token->[1];
10556: my $name = $token->[2]{'name'};
10557: my $value = $token->[2]{'value'};
10558: if ($item ne '' && $name ne '' && $value ne '') {
10559: my $release = $parser->get_text();
10560: $release =~ s/(^\s*|\s*$ )//gx;
10561: $needsrelease{$item.':'.$name.':'.$value} = $release;
10562: }
10563: }
10564: }
10565: }
10566: }
10567:
1.1056.4.33 raeburn 10568: # ---------------------------------------------------------- Read managers table
10569: {
10570: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
10571: if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
10572: while (my $configline=<$config>) {
10573: chomp($configline);
10574: next if ($configline =~ /^\#/);
10575: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
10576: $managerstab{$configline} = 1;
10577: }
10578: }
10579: close($config);
10580: }
10581: }
10582: }
10583:
1.329 matthew 10584: # ------------- set up temporary directory
10585: {
10586: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
10587:
1.11 www 10588: }
10589:
1.794 albertel 10590: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
10591: 'compress_threshold'=> 20_000,
10592: });
1.185 www 10593:
1.281 www 10594: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 10595: $dumpcount=0;
1.958 www 10596: $locknum=0;
1.22 www 10597:
1.163 harris41 10598: &logtouch();
1.672 albertel 10599: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 10600: $readit=1;
1.564 albertel 10601: {
10602: use integer;
10603: my $test=(2**32)+1;
1.568 albertel 10604: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 10605: &logthis(" Detected 64bit platform ($_64bit)");
10606: }
1.195 www 10607: }
1.1 albertel 10608: }
1.179 www 10609:
1.1 albertel 10610: 1;
1.191 harris41 10611: __END__
10612:
1.243 albertel 10613: =pod
10614:
1.191 harris41 10615: =head1 NAME
10616:
1.243 albertel 10617: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 10618:
10619: =head1 SYNOPSIS
10620:
1.243 albertel 10621: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 10622:
10623: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
10624:
1.243 albertel 10625: Common parameters:
10626:
10627: =over 4
10628:
10629: =item *
10630:
10631: $uname : an internal username (if $cname expecting a course Id specifically)
10632:
10633: =item *
10634:
10635: $udom : a domain (if $cdom expecting a course's domain specifically)
10636:
10637: =item *
10638:
10639: $symb : a resource instance identifier
10640:
10641: =item *
10642:
10643: $namespace : the name of a .db file that contains the data needed or
10644: being set.
10645:
10646: =back
10647:
1.394 bowersj2 10648: =head1 OVERVIEW
1.191 harris41 10649:
1.394 bowersj2 10650: lonnet provides subroutines which interact with the
10651: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
10652: about classes, users, and resources.
1.243 albertel 10653:
10654: For many of these objects you can also use this to store data about
10655: them or modify them in various ways.
1.191 harris41 10656:
1.394 bowersj2 10657: =head2 Symbs
1.191 harris41 10658:
1.394 bowersj2 10659: To identify a specific instance of a resource, LON-CAPA uses symbols
10660: or "symbs"X<symb>. These identifiers are built from the URL of the
10661: map, the resource number of the resource in the map, and the URL of
10662: the resource itself. The latter is somewhat redundant, but might help
10663: if maps change.
10664:
10665: An example is
10666:
10667: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
10668:
10669: The respective map entry is
10670:
10671: <resource id="19" src="/res/msu/korte/tests/part12.problem"
10672: title="Problem 2">
10673: </resource>
10674:
10675: Symbs are used by the random number generator, as well as to store and
10676: restore data specific to a certain instance of for example a problem.
10677:
10678: =head2 Storing And Retrieving Data
10679:
10680: X<store()>X<cstore()>X<restore()>Three of the most important functions
10681: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
10682: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
10683: is is the non-critical message twin of cstore. These functions are for
10684: handlers to store a perl hash to a user's permanent data space in an
10685: easy manner, and to retrieve it again on another call. It is expected
10686: that a handler would use this once at the beginning to retrieve data,
10687: and then again once at the end to send only the new data back.
10688:
10689: The data is stored in the user's data directory on the user's
10690: homeserver under the ID of the course.
10691:
10692: The hash that is returned by restore will have all of the previous
10693: value for all of the elements of the hash.
10694:
10695: Example:
10696:
10697: #creating a hash
10698: my %hash;
10699: $hash{'foo'}='bar';
10700:
10701: #storing it
10702: &Apache::lonnet::cstore(\%hash);
10703:
10704: #changing a value
10705: $hash{'foo'}='notbar';
10706:
10707: #adding a new value
10708: $hash{'bar'}='foo';
10709: &Apache::lonnet::cstore(\%hash);
10710:
10711: #retrieving the hash
10712: my %history=&Apache::lonnet::restore();
10713:
10714: #print the hash
10715: foreach my $key (sort(keys(%history))) {
10716: print("\%history{$key} = $history{$key}");
10717: }
10718:
10719: Will print out:
1.191 harris41 10720:
1.394 bowersj2 10721: %history{1:foo} = bar
10722: %history{1:keys} = foo:timestamp
10723: %history{1:timestamp} = 990455579
10724: %history{2:bar} = foo
10725: %history{2:foo} = notbar
10726: %history{2:keys} = foo:bar:timestamp
10727: %history{2:timestamp} = 990455580
10728: %history{bar} = foo
10729: %history{foo} = notbar
10730: %history{timestamp} = 990455580
10731: %history{version} = 2
10732:
10733: Note that the special hash entries C<keys>, C<version> and
10734: C<timestamp> were added to the hash. C<version> will be equal to the
10735: total number of versions of the data that have been stored. The
10736: C<timestamp> attribute will be the UNIX time the hash was
10737: stored. C<keys> is available in every historical section to list which
10738: keys were added or changed at a specific historical revision of a
10739: hash.
10740:
10741: B<Warning>: do not store the hash that restore returns directly. This
10742: will cause a mess since it will restore the historical keys as if the
10743: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 10744:
1.394 bowersj2 10745: Calling convention:
1.191 harris41 10746:
1.394 bowersj2 10747: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
10748: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 10749:
1.394 bowersj2 10750: For more detailed information, see lonnet specific documentation.
1.191 harris41 10751:
1.394 bowersj2 10752: =head1 RETURN MESSAGES
1.191 harris41 10753:
1.394 bowersj2 10754: =over 4
1.191 harris41 10755:
1.394 bowersj2 10756: =item * B<con_lost>: unable to contact remote host
1.191 harris41 10757:
1.394 bowersj2 10758: =item * B<con_delayed>: unable to contact remote host, message will be delivered
10759: when the connection is brought back up
1.191 harris41 10760:
1.394 bowersj2 10761: =item * B<con_failed>: unable to contact remote host and unable to save message
10762: for later delivery
1.191 harris41 10763:
1.967 bisitz 10764: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 10765:
1.394 bowersj2 10766: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 10767: that was requested
1.191 harris41 10768:
1.243 albertel 10769: =back
1.191 harris41 10770:
1.243 albertel 10771: =head1 PUBLIC SUBROUTINES
1.191 harris41 10772:
1.243 albertel 10773: =head2 Session Environment Functions
1.191 harris41 10774:
1.243 albertel 10775: =over 4
1.191 harris41 10776:
1.394 bowersj2 10777: =item *
10778: X<appenv()>
1.949 raeburn 10779: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 10780: the user envirnoment file, and will be restored for each access this
1.620 albertel 10781: user makes during this session, also modifies the %env for the current
1.949 raeburn 10782: process. Optional rolesarrayref - if defined contains a reference to an array
10783: of roles which are exempt from the restriction on modifying user.role entries
10784: in the user's environment.db and in %env.
1.191 harris41 10785:
10786: =item *
1.394 bowersj2 10787: X<delenv()>
1.987 raeburn 10788: B<delenv($delthis,$regexp)>: removes all items from the session
10789: environment file that begin with $delthis. If the
10790: optional second arg - $regexp - is true, $delthis is treated as a
10791: regular expression, otherwise \Q$delthis\E is used.
10792: The values are also deleted from the current processes %env.
1.191 harris41 10793:
1.795 albertel 10794: =item * get_env_multiple($name)
10795:
10796: gets $name from the %env hash, it seemlessly handles the cases where multiple
10797: values may be defined and end up as an array ref.
10798:
10799: returns an array of values
10800:
1.243 albertel 10801: =back
10802:
10803: =head2 User Information
1.191 harris41 10804:
1.243 albertel 10805: =over 4
1.191 harris41 10806:
10807: =item *
1.394 bowersj2 10808: X<queryauthenticate()>
10809: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 10810: authentication scheme
10811:
10812: =item *
1.394 bowersj2 10813: X<authenticate()>
1.1056.4.3 raeburn 10814: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 10815: authenticate user from domain's lib servers (first use the current
10816: one). C<$upass> should be the users password.
1.1056.4.3 raeburn 10817: $checkdefauth is optional (value is 1 if a check should be made to
10818: authenticate user using default authentication method, and allow
10819: account creation if username does not have account in the domain).
10820: $clientcancheckhost is optional (value is 1 if checking whether the
10821: server can host will occur on the client side in lonauth.pm).
1.191 harris41 10822:
10823: =item *
1.394 bowersj2 10824: X<homeserver()>
10825: B<homeserver($uname,$udom)>: find the server which has
10826: the user's directory and files (there must be only one), this caches
10827: the answer, and also caches if there is a borken connection.
1.191 harris41 10828:
10829: =item *
1.394 bowersj2 10830: X<idget()>
10831: B<idget($udom,@ids)>: find the usernames behind a list of IDs
10832: (IDs are a unique resource in a domain, there must be only 1 ID per
10833: username, and only 1 username per ID in a specific domain) (returns
10834: hash: id=>name,id=>name)
1.191 harris41 10835:
10836: =item *
1.394 bowersj2 10837: X<idrget()>
10838: B<idrget($udom,@unames)>: find the IDs behind a list of
10839: usernames (returns hash: name=>id,name=>id)
1.191 harris41 10840:
10841: =item *
1.394 bowersj2 10842: X<idput()>
10843: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 10844:
10845: =item *
1.394 bowersj2 10846: X<rolesinit()>
10847: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 10848:
10849: =item *
1.551 albertel 10850: X<getsection()>
10851: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 10852: course $cname, return section name/number or '' for "not in course"
10853: and '-1' for "no section"
10854:
10855: =item *
1.394 bowersj2 10856: X<userenvironment()>
10857: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 10858: passed in @what from the requested user's environment, returns a hash
10859:
1.858 raeburn 10860: =item *
10861: X<userlog_query()>
1.859 albertel 10862: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
10863: activity.log file. %filters defines filters applied when parsing the
10864: log file. These can be start or end timestamps, or the type of action
10865: - log to look for Login or Logout events, check for Checkin or
10866: Checkout, role for role selection. The response is in the form
10867: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
10868: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 10869:
1.243 albertel 10870: =back
10871:
10872: =head2 User Roles
10873:
10874: =over 4
10875:
10876: =item *
10877:
1.810 raeburn 10878: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 10879: F: full access
10880: U,I,K: authentication modes (cxx only)
10881: '': forbidden
10882: 1: user needs to choose course
10883: 2: browse allowed
1.766 albertel 10884: A: passphrase authentication needed
1.243 albertel 10885:
10886: =item *
10887:
10888: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
10889: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
10890: and course level
10891:
10892: =item *
10893:
1.988 raeburn 10894: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
10895: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 10896: $type is Course (default) or Community.
1.988 raeburn 10897: If $forcedefault evaluates to true, text returned will be default
10898: text for $type. Otherwise, if this is a course, the text returned
10899: will be a custom name for the role (if defined in the course's
10900: environment). If no custom name is defined the default is returned.
10901:
1.832 raeburn 10902: =item *
10903:
1.935 raeburn 10904: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858 raeburn 10905: All arguments are optional. Returns a hash of a roles, either for
10906: co-author/assistant author roles for a user's Construction Space
1.906 albertel 10907: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 10908: In the hash, keys are set to colon-separated $uname,$udom,$role, and
10909: (optionally) if $withsec is true, a fourth colon-separated item - $section.
10910: For each key, value is set to colon-separated start and end times for
10911: the role. If no username and domain are specified, will default to
1.934 raeburn 10912: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 10913: of role statuses (active, future or previous), roles
10914: (e.g., cc,in, st etc.) and domains of the roles which can be used
10915: to restrict the list of roles reported. If no array ref is
10916: provided for types, will default to return only active roles.
1.834 albertel 10917:
1.243 albertel 10918: =back
10919:
10920: =head2 User Modification
10921:
10922: =over 4
10923:
10924: =item *
10925:
1.957 raeburn 10926: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 10927: user for the level given by URL. Optional start and end dates (leave empty
10928: string or zero for "no date")
1.191 harris41 10929:
10930: =item *
10931:
1.243 albertel 10932: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
10933: change a users, password, possible return values are: ok,
10934: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
10935: refused
1.191 harris41 10936:
10937: =item *
10938:
1.243 albertel 10939: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 10940:
10941: =item *
10942:
1.1056.4.1 raeburn 10943: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
10944: $forceid,$desiredhome,$email,$inststatus,$candelete) :
10945:
10946: will update user information (firstname,middlename,lastname,generation,
10947: permanentemail), and if forceid is true, student/employee ID also.
10948: A user's institutional affiliation(s) can also be updated.
10949: User information fields will not be overwritten with empty entries
10950: unless the field is included in the $candelete array reference.
10951: This array is included when a single user is modified via "Manage Users",
10952: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 10953:
10954: =item *
10955:
1.286 matthew 10956: modifystudent
10957:
1.957 raeburn 10958: modify a student's enrollment and identification information.
1.286 matthew 10959: The course id is resolved based on the current users environment.
10960: This means the envoking user must be a course coordinator or otherwise
10961: associated with a course.
10962:
1.297 matthew 10963: This call is essentially a wrapper for lonnet::modifyuser and
10964: lonnet::modify_student_enrollment
1.286 matthew 10965:
10966: Inputs:
10967:
10968: =over 4
10969:
1.957 raeburn 10970: =item B<$udom> Student's loncapa domain
1.286 matthew 10971:
1.957 raeburn 10972: =item B<$uname> Student's loncapa login name
1.286 matthew 10973:
1.964 bisitz 10974: =item B<$uid> Student/Employee ID
1.286 matthew 10975:
1.957 raeburn 10976: =item B<$umode> Student's authentication mode
1.286 matthew 10977:
1.957 raeburn 10978: =item B<$upass> Student's password
1.286 matthew 10979:
1.957 raeburn 10980: =item B<$first> Student's first name
1.286 matthew 10981:
1.957 raeburn 10982: =item B<$middle> Student's middle name
1.286 matthew 10983:
1.957 raeburn 10984: =item B<$last> Student's last name
1.286 matthew 10985:
1.957 raeburn 10986: =item B<$gene> Student's generation
1.286 matthew 10987:
1.957 raeburn 10988: =item B<$usec> Student's section in course
1.286 matthew 10989:
10990: =item B<$end> Unix time of the roles expiration
10991:
10992: =item B<$start> Unix time of the roles start date
10993:
10994: =item B<$forceid> If defined, allow $uid to be changed
10995:
10996: =item B<$desiredhome> server to use as home server for student
10997:
1.957 raeburn 10998: =item B<$email> Student's permanent e-mail address
10999:
11000: =item B<$type> Type of enrollment (auto or manual)
11001:
1.963 raeburn 11002: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
11003:
11004: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 11005:
1.963 raeburn 11006: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 11007:
1.963 raeburn 11008: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 11009:
1.963 raeburn 11010: =item B<$inststatus> institutional status of user - : separated string of escaped status types
1.957 raeburn 11011:
1.286 matthew 11012: =back
1.297 matthew 11013:
11014: =item *
11015:
11016: modify_student_enrollment
11017:
11018: Change a students enrollment status in a class. The environment variable
11019: 'role.request.course' must be defined for this function to proceed.
11020:
11021: Inputs:
11022:
11023: =over 4
11024:
11025: =item $udom, students domain
11026:
11027: =item $uname, students name
11028:
11029: =item $uid, students user id
11030:
11031: =item $first, students first name
11032:
11033: =item $middle
11034:
11035: =item $last
11036:
11037: =item $gene
11038:
11039: =item $usec
11040:
11041: =item $end
11042:
11043: =item $start
11044:
1.957 raeburn 11045: =item $type
11046:
11047: =item $locktype
11048:
11049: =item $cid
11050:
11051: =item $selfenroll
11052:
11053: =item $context
11054:
1.297 matthew 11055: =back
11056:
1.191 harris41 11057:
11058: =item *
11059:
1.243 albertel 11060: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
11061: custom role; give a custom role to a user for the level given by URL. Specify
11062: name and domain of role author, and role name
1.191 harris41 11063:
11064: =item *
11065:
1.243 albertel 11066: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 11067:
11068: =item *
11069:
1.243 albertel 11070: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
11071:
11072: =back
11073:
11074: =head2 Course Infomation
11075:
11076: =over 4
1.191 harris41 11077:
11078: =item *
11079:
1.631 albertel 11080: coursedescription($courseid) : returns a hash of information about the
11081: specified course id, including all environment settings for the
11082: course, the description of the course will be in the hash under the
11083: key 'description'
1.191 harris41 11084:
11085: =item *
11086:
1.624 albertel 11087: resdata($name,$domain,$type,@which) : request for current parameter
11088: setting for a specific $type, where $type is either 'course' or 'user',
11089: @what should be a list of parameters to ask about. This routine caches
11090: answers for 5 minutes.
1.243 albertel 11091:
1.877 foxr 11092: =item *
11093:
11094: get_courseresdata($courseid, $domain) : dump the entire course resource
11095: data base, returning a hash that is keyed by the resource name and has
11096: values that are the resource value. I believe that the timestamps and
11097: versions are also returned.
11098:
11099:
1.243 albertel 11100: =back
11101:
11102: =head2 Course Modification
11103:
11104: =over 4
1.191 harris41 11105:
11106: =item *
11107:
1.243 albertel 11108: writecoursepref($courseid,%prefs) : write preferences (environment
11109: database) for a course
1.191 harris41 11110:
11111: =item *
11112:
1.1011 raeburn 11113: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
11114:
11115: =item *
11116:
1.1038 raeburn 11117: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 11118:
11119: =back
11120:
11121: =head2 Resource Subroutines
11122:
11123: =over 4
1.191 harris41 11124:
11125: =item *
11126:
1.243 albertel 11127: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 11128:
11129: =item *
11130:
1.243 albertel 11131: repcopy($filename) : subscribes to the requested file, and attempts to
11132: replicate from the owning library server, Might return
1.607 raeburn 11133: 'unavailable', 'not_found', 'forbidden', 'ok', or
11134: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 11135: resource. Expects the local filesystem pathname
11136: (/home/httpd/html/res/....)
11137:
11138: =back
11139:
11140: =head2 Resource Information
11141:
11142: =over 4
1.191 harris41 11143:
11144: =item *
11145:
1.243 albertel 11146: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
11147: a vairety of different possible values, $varname should be a request
11148: string, and the other parameters can be used to specify who and what
11149: one is asking about.
11150:
11151: Possible values for $varname are environment.lastname (or other item
11152: from the envirnment hash), user.name (or someother aspect about the
11153: user), resource.0.maxtries (or some other part and parameter of a
11154: resource)
1.204 albertel 11155:
11156: =item *
11157:
1.243 albertel 11158: directcondval($number) : get current value of a condition; reads from a state
11159: string
1.204 albertel 11160:
11161: =item *
11162:
1.243 albertel 11163: condval($condidx) : value of condition index based on state
1.204 albertel 11164:
11165: =item *
11166:
1.243 albertel 11167: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
11168: resource's metadata, $what should be either a specific key, or either
11169: 'keys' (to get a list of possible keys) or 'packages' to get a list of
11170: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
11171:
11172: this function automatically caches all requests
1.191 harris41 11173:
11174: =item *
11175:
1.243 albertel 11176: metadata_query($query,$custom,$customshow) : make a metadata query against the
11177: network of library servers; returns file handle of where SQL and regex results
11178: will be stored for query
1.191 harris41 11179:
11180: =item *
11181:
1.243 albertel 11182: symbread($filename) : return symbolic list entry (filename argument optional);
11183: returns the data handle
1.191 harris41 11184:
11185: =item *
11186:
1.243 albertel 11187: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 11188: a possible symb for the URL in $thisfn, and if is an encryypted
11189: resource that the user accessed using /enc/ returns a 1 on success, 0
11190: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 11191: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 11192:
1.191 harris41 11193:
11194: =item *
11195:
1.243 albertel 11196: symbclean($symb) : removes versions numbers from a symb, returns the
11197: cleaned symb
1.191 harris41 11198:
11199: =item *
11200:
1.243 albertel 11201: is_on_map($uri) : checks if the $uri is somewhere on the current
11202: course map, user must be in a course for it to work.
1.191 harris41 11203:
11204: =item *
11205:
1.243 albertel 11206: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 11207:
11208: =item *
11209:
1.243 albertel 11210: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
11211: a random seed, all arguments are optional, if they aren't sent it uses the
11212: environment to derive them. Note: if symb isn't sent and it can't get one
11213: from &symbread it will use the current time as its return value
1.191 harris41 11214:
11215: =item *
11216:
1.243 albertel 11217: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
11218: unfakeable, receipt
1.191 harris41 11219:
11220: =item *
11221:
1.620 albertel 11222: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 11223:
11224: =item *
11225:
1.243 albertel 11226: countacc($url) : count the number of accesses to a given URL
1.191 harris41 11227:
11228: =item *
11229:
1.243 albertel 11230: 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 11231:
11232: =item *
11233:
1.243 albertel 11234: 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 11235:
11236: =item *
11237:
1.243 albertel 11238: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 11239:
11240: =item *
11241:
1.243 albertel 11242: devalidate($symb) : devalidate temporary spreadsheet calculations,
11243: forcing spreadsheet to reevaluate the resource scores next time.
11244:
11245: =back
11246:
11247: =head2 Storing/Retreiving Data
11248:
11249: =over 4
1.191 harris41 11250:
11251: =item *
11252:
1.243 albertel 11253: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
11254: for this url; hashref needs to be given and should be a \%hashname; the
11255: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 11256: be derived from the env
1.191 harris41 11257:
11258: =item *
11259:
1.243 albertel 11260: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
11261: uses critical subroutine
1.191 harris41 11262:
11263: =item *
11264:
1.243 albertel 11265: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
11266: all args are optional
1.191 harris41 11267:
11268: =item *
11269:
1.717 albertel 11270: dumpstore($namespace,$udom,$uname,$regexp,$range) :
11271: dumps the complete (or key matching regexp) namespace into a hash
11272: ($udom, $uname, $regexp, $range are optional) for a namespace that is
11273: normally &store()ed into
11274:
11275: $range should be either an integer '100' (give me the first 100
11276: matching records)
11277: or be two integers sperated by a - with no spaces
11278: '30-50' (give me the 30th through the 50th matching
11279: records)
11280:
11281:
11282: =item *
11283:
11284: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
11285: replaces a &store() version of data with a replacement set of data
11286: for a particular resource in a namespace passed in the $storehash hash
11287: reference
11288:
11289: =item *
11290:
1.243 albertel 11291: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
11292: works very similar to store/cstore, but all data is stored in a
11293: temporary location and can be reset using tmpreset, $storehash should
11294: be a hash reference, returns nothing on success
1.191 harris41 11295:
11296: =item *
11297:
1.243 albertel 11298: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
11299: similar to restore, but all data is stored in a temporary location and
11300: can be reset using tmpreset. Returns a hash of values on success,
11301: error string otherwise.
1.191 harris41 11302:
11303: =item *
11304:
1.243 albertel 11305: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
11306: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 11307:
11308: =item *
11309:
1.243 albertel 11310: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11311: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 11312:
11313: =item *
11314:
1.243 albertel 11315: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
11316: namesp ($udom and $uname are optional)
1.191 harris41 11317:
11318: =item *
11319:
1.702 albertel 11320: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 11321: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 11322: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 11323:
1.702 albertel 11324: $range should be either an integer '100' (give me the first 100
11325: matching records)
11326: or be two integers sperated by a - with no spaces
11327: '30-50' (give me the 30th through the 50th matching
11328: records)
1.449 matthew 11329: =item *
11330:
11331: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
11332: $store can be a scalar, an array reference, or if the amount to be
11333: incremented is > 1, a hash reference.
11334:
11335: ($udom and $uname are optional)
1.191 harris41 11336:
11337: =item *
11338:
1.243 albertel 11339: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
11340: ($udom and $uname are optional)
1.191 harris41 11341:
11342: =item *
11343:
1.243 albertel 11344: cput($namespace,$storehash,$udom,$uname) : critical put
11345: ($udom and $uname are optional)
1.191 harris41 11346:
11347: =item *
11348:
1.748 albertel 11349: newput($namespace,$storehash,$udom,$uname) :
11350:
11351: Attempts to store the items in the $storehash, but only if they don't
11352: currently exist, if this succeeds you can be certain that you have
11353: successfully created a new key value pair in the $namespace db.
11354:
11355:
11356: Args:
11357: $namespace: name of database to store values to
11358: $storehash: hashref to store to the db
11359: $udom: (optional) domain of user containing the db
11360: $uname: (optional) name of user caontaining the db
11361:
11362: Returns:
11363: 'ok' -> succeeded in storing all keys of $storehash
11364: 'key_exists: <key>' -> failed to anything out of $storehash, as at
11365: least <key> already existed in the db (other
11366: requested keys may also already exist)
1.967 bisitz 11367: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 11368: 'con_lost' -> unable to contact request server
11369: 'refused' -> action was not allowed by remote machine
11370:
11371:
11372: =item *
11373:
1.243 albertel 11374: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11375: reference filled in from namesp (encrypts the return communication)
11376: ($udom and $uname are optional)
1.191 harris41 11377:
11378: =item *
11379:
1.243 albertel 11380: log($udom,$name,$home,$message) : write to permanent log for user; use
11381: critical subroutine
11382:
1.806 raeburn 11383: =item *
11384:
1.860 raeburn 11385: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
11386: array reference filled in from namespace found in domain level on either
11387: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 11388:
11389: =item *
11390:
1.860 raeburn 11391: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
11392: domain level either on specified domain server ($uhome) or primary domain
11393: server ($udom and $uhome are optional)
1.806 raeburn 11394:
1.943 raeburn 11395: =item *
11396:
11397: get_domain_defaults($target_domain) : returns hash with defaults for
11398: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
11399: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
11400: or localauth), initial password or a kerberos realm, language (e.g., en-us).
11401: Values are retrieved from cache (if current), or from domain's configuration.db
11402: (if available), or lastly from values in lonTabs/dns_domain,tab,
11403: or lonTabs/domain.tab.
11404:
11405: %domdefaults = &get_auth_defaults($target_domain);
11406:
1.243 albertel 11407: =back
11408:
11409: =head2 Network Status Functions
11410:
11411: =over 4
1.191 harris41 11412:
11413: =item *
11414:
11415: dirlist($uri) : return directory list based on URI
11416:
11417: =item *
11418:
1.243 albertel 11419: spareserver() : find server with least workload from spare.tab
11420:
1.986 foxr 11421:
11422: =item *
11423:
11424: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
11425: if there is no corresponding loncapa host.
11426:
1.243 albertel 11427: =back
11428:
1.986 foxr 11429:
1.243 albertel 11430: =head2 Apache Request
11431:
11432: =over 4
1.191 harris41 11433:
11434: =item *
11435:
1.243 albertel 11436: ssi($url,%hash) : server side include, does a complete request cycle on url to
11437: localhost, posts hash
11438:
11439: =back
11440:
11441: =head2 Data to String to Data
11442:
11443: =over 4
1.191 harris41 11444:
11445: =item *
11446:
1.243 albertel 11447: hash2str(%hash) : convert a hash into a string complete with escaping and '='
11448: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 11449:
11450: =item *
11451:
1.243 albertel 11452: hashref2str($hashref) : convert a hashref into a string complete with
11453: escaping and '=' and '&' separators, supports elements that are
11454: arrayrefs and hashrefs
1.191 harris41 11455:
11456: =item *
11457:
1.243 albertel 11458: arrayref2str($arrayref) : convert an arrayref into a string complete
11459: with escaping and '&' separators, supports elements that are arrayrefs
11460: and hashrefs
1.191 harris41 11461:
11462: =item *
11463:
1.243 albertel 11464: str2hash($string) : convert string to hash using unescaping and
11465: splitting on '=' and '&', supports elements that are arrayrefs and
11466: hashrefs
1.191 harris41 11467:
11468: =item *
11469:
1.243 albertel 11470: str2array($string) : convert string to hash using unescaping and
11471: splitting on '&', supports elements that are arrayrefs and hashrefs
11472:
11473: =back
11474:
11475: =head2 Logging Routines
11476:
11477: These routines allow one to make log messages in the lonnet.log and
11478: lonnet.perm logfiles.
1.191 harris41 11479:
1.1056.4.31 raeburn 11480: =over 4
11481:
1.191 harris41 11482: =item *
11483:
1.243 albertel 11484: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 11485:
11486: =item *
11487:
1.243 albertel 11488: logthis() : append message to the normal lonnet.log file, it gets
11489: preiodically rolled over and deleted.
1.191 harris41 11490:
11491: =item *
11492:
1.243 albertel 11493: logperm() : append a permanent message to lonnet.perm.log, this log
11494: file never gets deleted by any automated portion of the system, only
11495: messages of critical importance should go in here.
11496:
11497: =back
11498:
11499: =head2 General File Helper Routines
11500:
11501: =over 4
1.191 harris41 11502:
11503: =item *
11504:
1.481 raeburn 11505: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
11506: (a) files in /uploaded
11507: (i) If a local copy of the file exists -
11508: compares modification date of local copy with last-modified date for
11509: definitive version stored on home server for course. If local copy is
11510: stale, requests a new version from the home server and stores it.
11511: If the original has been removed from the home server, then local copy
11512: is unlinked.
11513: (ii) If local copy does not exist -
11514: requests the file from the home server and stores it.
11515:
11516: If $caller is 'uploadrep':
11517: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
11518: for request for files originally uploaded via DOCS.
11519: - returns 'ok' if fresh local copy now available, -1 otherwise.
11520:
11521: Otherwise:
11522: This indicates a call from the content generation phase of the request.
11523: - returns the entire contents of the file or -1.
11524:
11525: (b) files in /res
11526: - returns the entire contents of a file or -1;
11527: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 11528:
1.712 albertel 11529:
11530: =item *
11531:
11532: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
11533: reference
11534:
11535: returns either a stat() list of data about the file or an empty list
11536: if the file doesn't exist or couldn't find out about it (connection
11537: problems or user unknown)
11538:
1.191 harris41 11539: =item *
11540:
1.243 albertel 11541: filelocation($dir,$file) : returns file system location of a file
11542: based on URI; meant to be "fairly clean" absolute reference, $dir is a
11543: directory that relative $file lookups are to looked in ($dir of /a/dir
11544: and a file of ../bob will become /a/bob)
1.191 harris41 11545:
11546: =item *
11547:
11548: hreflocation($dir,$file) : returns file system location or a URL; same as
11549: filelocation except for hrefs
11550:
11551: =item *
11552:
11553: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
11554:
1.243 albertel 11555: =back
11556:
1.608 albertel 11557: =head2 Usererfile file routines (/uploaded*)
11558:
11559: =over 4
11560:
11561: =item *
11562:
11563: userfileupload(): main rotine for putting a file in a user or course's
11564: filespace, arguments are,
11565:
1.620 albertel 11566: formname - required - this is the name of the element in $env where the
1.608 albertel 11567: filename, and the contents of the file to create/modifed exist
1.620 albertel 11568: the filename is in $env{'form.'.$formname.'.filename'} and the
11569: contents of the file is located in $env{'form.'.$formname}
1.1056.4.13 raeburn 11570: context - if coursedoc, store the file in the course of the active role
11571: of the current user;
11572: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
11573: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 11574: subdir - required - subdirectory to put the file in under ../userfiles/
11575: if undefined, it will be placed in "unknown"
11576:
11577: (This routine calls clean_filename() to remove any dangerous
11578: characters from the filename, and then calls finuserfileupload() to
11579: complete the transaction)
11580:
11581: returns either the url of the uploaded file (/uploaded/....) if successful
11582: and /adm/notfound.html if unsuccessful
11583:
11584: =item *
11585:
11586: clean_filename(): routine for cleaing a filename up for storage in
11587: userfile space, argument is:
11588:
11589: filename - proposed filename
11590:
11591: returns: the new clean filename
11592:
11593: =item *
11594:
1.1056.4.13 raeburn 11595: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 11596: userspace, probably shouldn't be called directly
11597:
11598: docuname: username or courseid of destination for the file
11599: docudom: domain of user/course of destination for the file
11600: formname: same as for userfileupload()
1.1056.4.13 raeburn 11601: fname: filename (including subdirectories) for the file
11602: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
11603: allfiles: reference to hash used to store objects found by parser
11604: codebase: reference to hash used for codebases of java objects found by parser
11605: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
11606: thumbheight: height (pixels) of thumbnail to be created for uploaded image
11607: resizewidth: width to be used to resize image using resizeImage from ImageMagick
11608: resizeheight: height to be used to resize image using resizeImage from ImageMagick
11609: context: if 'overwrite', will move the uploaded file from its temporary location to
11610: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1056.4.16 raeburn 11611: mimetype: reference to scalar to accommodate mime type determined
11612: from File::MMagic if $parser = parse.
1.608 albertel 11613:
11614: returns either the url of the uploaded file (/uploaded/....) if successful
1.1056.4.13 raeburn 11615: and /adm/notfound.html if unsuccessful (or an error message if context
11616: was 'overwrite').
11617:
1.608 albertel 11618:
11619: =item *
11620:
11621: renameuserfile(): renames an existing userfile to a new name
11622:
11623: Args:
11624: docuname: username or courseid of destination for the file
11625: docudom: domain of user/course of destination for the file
11626: old: current file name (including any subdirs under userfiles)
11627: new: desired file name (including any subdirs under userfiles)
11628:
11629: =item *
11630:
11631: mkdiruserfile(): creates a directory is a userfiles dir
11632:
11633: Args:
11634: docuname: username or courseid of destination for the file
11635: docudom: domain of user/course of destination for the file
11636: dir: dir to create (including any subdirs under userfiles)
11637:
11638: =item *
11639:
11640: removeuserfile(): removes a file that exists in userfiles
11641:
11642: Args:
11643: docuname: username or courseid of destination for the file
11644: docudom: domain of user/course of destination for the file
11645: fname: filname to delete (including any subdirs under userfiles)
11646:
11647: =item *
11648:
11649: removeuploadedurl(): convience function for removeuserfile()
11650:
11651: Args:
11652: url: a full /uploaded/... url to delete
11653:
1.747 albertel 11654: =item *
11655:
11656: get_portfile_permissions():
11657: Args:
11658: domain: domain of user or course contain the portfolio files
11659: user: name of user or num of course contain the portfolio files
11660: Returns:
11661: hashref of a dump of the proper file_permissions.db
11662:
11663:
11664: =item *
11665:
11666: get_access_controls():
11667:
11668: Args:
11669: current_permissions: the hash ref returned from get_portfile_permissions()
11670: group: (optional) the group you want the files associated with
11671: file: (optional) the file you want access info on
11672:
11673: Returns:
1.749 raeburn 11674: a hash (keys are file names) of hashes containing
11675: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
11676: values are XML containing access control settings (see below)
1.747 albertel 11677:
11678: Internal notes:
11679:
1.749 raeburn 11680: access controls are stored in file_permissions.db as key=value pairs.
11681: key -> path to file/file_name\0uniqueID:scope_end_start
11682: where scope -> public,guest,course,group,domains or users.
11683: end -> UNIX time for end of access (0 -> no end date)
11684: start -> UNIX time for start of access
11685:
11686: value -> XML description of access control
11687: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
11688: <start></start>
11689: <end></end>
11690:
11691: <password></password> for scope type = guest
11692:
11693: <domain></domain> for scope type = course or group
11694: <number></number>
11695: <roles id="">
11696: <role></role>
11697: <access></access>
11698: <section></section>
11699: <group></group>
11700: </roles>
11701:
11702: <dom></dom> for scope type = domains
11703:
11704: <users> for scope type = users
11705: <user>
11706: <uname></uname>
11707: <udom></udom>
11708: </user>
11709: </users>
11710: </scope>
11711:
11712: Access data is also aggregated for each file in an additional key=value pair:
11713: key -> path to file/file_name\0accesscontrol
11714: value -> reference to hash
11715: hash contains key = value pairs
11716: where key = uniqueID:scope_end_start
11717: value = UNIX time record was last updated
11718:
11719: Used to improve speed of look-ups of access controls for each file.
11720:
11721: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
11722:
11723: modify_access_controls():
11724:
11725: Modifies access controls for a portfolio file
11726: Args
11727: 1. file name
11728: 2. reference to hash of required changes,
11729: 3. domain
11730: 4. username
11731: where domain,username are the domain of the portfolio owner
11732: (either a user or a course)
11733:
11734: Returns:
11735: 1. result of additions or updates ('ok' or 'error', with error message).
11736: 2. result of deletions ('ok' or 'error', with error message).
11737: 3. reference to hash of any new or updated access controls.
11738: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
11739: key = integer (inbound ID)
11740: value = uniqueID
1.747 albertel 11741:
1.608 albertel 11742: =back
11743:
1.243 albertel 11744: =head2 HTTP Helper Routines
11745:
11746: =over 4
11747:
1.191 harris41 11748: =item *
11749:
11750: escape() : unpack non-word characters into CGI-compatible hex codes
11751:
11752: =item *
11753:
11754: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
11755:
1.243 albertel 11756: =back
11757:
11758: =head1 PRIVATE SUBROUTINES
11759:
11760: =head2 Underlying communication routines (Shouldn't call)
11761:
11762: =over 4
11763:
11764: =item *
11765:
11766: subreply() : tries to pass a message to lonc, returns con_lost if incapable
11767:
11768: =item *
11769:
11770: reply() : uses subreply to send a message to remote machine, logs all failures
11771:
11772: =item *
11773:
11774: critical() : passes a critical message to another server; if cannot
11775: get through then place message in connection buffer directory and
11776: returns con_delayed, if incapable of saving message, returns
11777: con_failed
11778:
11779: =item *
11780:
11781: reconlonc() : tries to reconnect lonc client processes.
11782:
11783: =back
11784:
11785: =head2 Resource Access Logging
11786:
11787: =over 4
11788:
11789: =item *
11790:
11791: flushcourselogs() : flush (save) buffer logs and access logs
11792:
11793: =item *
11794:
11795: courselog($what) : save message for course in hash
11796:
11797: =item *
11798:
11799: courseacclog($what) : save message for course using &courselog(). Perform
11800: special processing for specific resource types (problems, exams, quizzes, etc).
11801:
1.191 harris41 11802: =item *
11803:
11804: goodbye() : flush course logs and log shutting down; it is called in srm.conf
11805: as a PerlChildExitHandler
1.243 albertel 11806:
11807: =back
11808:
11809: =head2 Other
11810:
11811: =over 4
11812:
11813: =item *
11814:
11815: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 11816:
11817: =back
11818:
11819: =cut
1.877 foxr 11820:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>