1: # The LearningOnline Network
2: # TCP networking package
3: #
4: # $Id: lonnet.pm,v 1.1367 2018/02/01 04:51:13 raeburn Exp $
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: #
28: ###
29:
30: =pod
31:
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:
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:
71: package Apache::lonnet;
72:
73: use strict;
74: use HTTP::Date;
75: use Image::Magick;
76:
77:
78: use Encode;
79:
80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
81: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
82: %managerstab);
83:
84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
85: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
86: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
87: %courseownerbuf, %coursetypebuf,$locknum);
88:
89: use IO::Socket;
90: use GDBM_File;
91: use HTML::LCParser;
92: use Fcntl qw(:flock);
93: use Storable qw(thaw nfreeze);
94: use Time::HiRes qw( sleep gettimeofday tv_interval );
95: use Cache::Memcached;
96: use Digest::MD5;
97: use Math::Random;
98: use File::MMagic;
99: use LONCAPA qw(:DEFAULT :match);
100: use LONCAPA::Configuration;
101: use LONCAPA::lonmetadata;
102: use LONCAPA::Lond;
103: use LONCAPA::LWPReq;
104:
105: use File::Copy;
106:
107: my $readit;
108: my $max_connection_retries = 20; # Or some such value.
109:
110: require Exporter;
111:
112: our @ISA = qw (Exporter);
113: our @EXPORT = qw(%env);
114:
115:
116: # ------------------------------------ Logging (parameters, docs, slots, roles)
117: {
118: my $logid;
119: sub write_log {
120: my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
121: if ($context eq 'course') {
122: if (($cnum eq '') || ($cdom eq '')) {
123: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
124: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
125: }
126: }
127: $logid ++;
128: my $now = time();
129: my $id=$now.'00000'.$$.'00000'.$logid;
130: my $logentry = {
131: $id => {
132: 'exe_uname' => $env{'user.name'},
133: 'exe_udom' => $env{'user.domain'},
134: 'exe_time' => $now,
135: 'exe_ip' => $ENV{'REMOTE_ADDR'},
136: 'delflag' => $delflag,
137: 'logentry' => $storehash,
138: 'uname' => $uname,
139: 'udom' => $udom,
140: }
141: };
142: return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
143: }
144: }
145:
146: sub logtouch {
147: my $execdir=$perlvar{'lonDaemons'};
148: unless (-e "$execdir/logs/lonnet.log") {
149: open(my $fh,">>","$execdir/logs/lonnet.log");
150: close $fh;
151: }
152: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
153: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
154: }
155:
156: sub logthis {
157: my $message=shift;
158: my $execdir=$perlvar{'lonDaemons'};
159: my $now=time;
160: my $local=localtime($now);
161: if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
162: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
163: print $fh $logstring;
164: close($fh);
165: }
166: return 1;
167: }
168:
169: sub logperm {
170: my $message=shift;
171: my $execdir=$perlvar{'lonDaemons'};
172: my $now=time;
173: my $local=localtime($now);
174: if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
175: print $fh "$now:$message:$local\n";
176: close($fh);
177: }
178: return 1;
179: }
180:
181: sub create_connection {
182: my ($hostname,$lonid) = @_;
183: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
184: Type => SOCK_STREAM,
185: Timeout => 10);
186: return 0 if (!$client);
187: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
188: my $result = <$client>;
189: chomp($result);
190: return 1 if ($result eq 'done');
191: return 0;
192: }
193:
194: sub get_server_timezone {
195: my ($cnum,$cdom) = @_;
196: my $home=&homeserver($cnum,$cdom);
197: if ($home ne 'no_host') {
198: my $cachetime = 24*3600;
199: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
200: if (defined($cached)) {
201: return $timezone;
202: } else {
203: my $timezone = &reply('servertimezone',$home);
204: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
205: }
206: }
207: }
208:
209: sub get_server_distarch {
210: my ($lonhost,$ignore_cache) = @_;
211: if (defined($lonhost)) {
212: if (!defined(&hostname($lonhost))) {
213: return;
214: }
215: my $cachetime = 12*3600;
216: if (!$ignore_cache) {
217: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
218: if (defined($cached)) {
219: return $distarch;
220: }
221: }
222: my $rep = &reply('serverdistarch',$lonhost);
223: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
224: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
225: $rep eq '') {
226: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
227: }
228: }
229: return;
230: }
231:
232: sub get_servercerts_info {
233: my ($lonhost,$context) = @_;
234: my ($rep,$uselocal);
235: if (grep { $_ eq $lonhost } ¤t_machine_ids()) {
236: $uselocal = 1;
237: }
238: if (($context ne 'cgi') && ($uselocal)) {
239: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
240: if ($distro eq '') {
241: $uselocal = 0;
242: } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
243: if ($1 < 6) {
244: $uselocal = 0;
245: }
246: } elsif ($distro =~ /^(?:sles)(\d+)$/) {
247: if ($1 < 12) {
248: $uselocal = 0;
249: }
250: }
251: }
252: if ($uselocal) {
253: $rep = LONCAPA::Lond::server_certs(\%perlvar);
254: } else {
255: $rep=&reply('servercerts',$lonhost);
256: }
257: my ($result,%returnhash);
258: if (defined($lonhost)) {
259: if (!defined(&hostname($lonhost))) {
260: return;
261: }
262: }
263: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
264: ($rep eq 'unknown_cmd')) {
265: $result = $rep;
266: } else {
267: $result = 'ok';
268: my @pairs=split(/\&/,$rep);
269: foreach my $item (@pairs) {
270: my ($key,$value)=split(/=/,$item,2);
271: my $what = &unescape($key);
272: $returnhash{$what}=&thaw_unescape($value);
273: }
274: }
275: return ($result,\%returnhash);
276: }
277:
278: sub get_server_loncaparev {
279: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
280: if (defined($lonhost)) {
281: if (!defined(&hostname($lonhost))) {
282: undef($lonhost);
283: }
284: }
285: if (!defined($lonhost)) {
286: if (defined(&domain($dom,'primary'))) {
287: $lonhost=&domain($dom,'primary');
288: if ($lonhost eq 'no_host') {
289: undef($lonhost);
290: }
291: }
292: }
293: if (defined($lonhost)) {
294: my $cachetime = 12*3600;
295: if (!$ignore_cache) {
296: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
297: if (defined($cached)) {
298: return $loncaparev;
299: }
300: }
301: my ($answer,$loncaparev);
302: my @ids=¤t_machine_ids();
303: if (grep(/^\Q$lonhost\E$/,@ids)) {
304: $answer = $perlvar{'lonVersion'};
305: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
306: $loncaparev = $1;
307: }
308: } else {
309: $answer = &reply('serverloncaparev',$lonhost);
310: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
311: if ($caller eq 'loncron') {
312: my $protocol = $protocol{$lonhost};
313: $protocol = 'http' if ($protocol ne 'https');
314: my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
315: my $request=new HTTP::Request('GET',$url);
316: my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
317: unless ($response->is_error()) {
318: my $content = $response->content;
319: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
320: $loncaparev = $1;
321: }
322: }
323: } else {
324: $loncaparev = $loncaparevs{$lonhost};
325: }
326: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
327: $loncaparev = $1;
328: }
329: }
330: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
331: }
332: }
333:
334: sub get_server_homeID {
335: my ($hostname,$ignore_cache,$caller) = @_;
336: unless ($ignore_cache) {
337: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
338: if (defined($cached)) {
339: return $serverhomeID;
340: }
341: }
342: my $cachetime = 12*3600;
343: my $serverhomeID;
344: if ($caller eq 'loncron') {
345: my @machine_ids = &machine_ids($hostname);
346: foreach my $id (@machine_ids) {
347: my $response = &reply('serverhomeID',$id);
348: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
349: $serverhomeID = $response;
350: last;
351: }
352: }
353: if ($serverhomeID eq '') {
354: $serverhomeID = $machine_ids[-1];
355: }
356: } else {
357: $serverhomeID = $serverhomeIDs{$hostname};
358: }
359: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
360: }
361:
362: sub get_remote_globals {
363: my ($lonhost,$whathash,$ignore_cache) = @_;
364: my ($result,%returnhash,%whatneeded);
365: if (ref($whathash) eq 'HASH') {
366: foreach my $what (sort(keys(%{$whathash}))) {
367: my $hashid = $lonhost.'-'.$what;
368: my ($response,$cached);
369: unless ($ignore_cache) {
370: ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
371: }
372: if (defined($cached)) {
373: $returnhash{$what} = $response;
374: } else {
375: $whatneeded{$what} = 1;
376: }
377: }
378: if (keys(%whatneeded) == 0) {
379: $result = 'ok';
380: } else {
381: my $requested = &freeze_escape(\%whatneeded);
382: my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
383: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
384: ($rep eq 'unknown_cmd')) {
385: $result = $rep;
386: } else {
387: $result = 'ok';
388: my @pairs=split(/\&/,$rep);
389: foreach my $item (@pairs) {
390: my ($key,$value)=split(/=/,$item,2);
391: my $what = &unescape($key);
392: my $hashid = $lonhost.'-'.$what;
393: $returnhash{$what}=&thaw_unescape($value);
394: &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
395: }
396: }
397: }
398: }
399: return ($result,\%returnhash);
400: }
401:
402: sub remote_devalidate_cache {
403: my ($lonhost,$cachekeys) = @_;
404: my $items;
405: return unless (ref($cachekeys) eq 'ARRAY');
406: my $cachestr = join('&',@{$cachekeys});
407: my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
408: return $response;
409: }
410:
411: # -------------------------------------------------- Non-critical communication
412: sub subreply {
413: my ($cmd,$server)=@_;
414: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
415: #
416: # With loncnew process trimming, there's a timing hole between lonc server
417: # process exit and the master server picking up the listen on the AF_UNIX
418: # socket. In that time interval, a lock file will exist:
419:
420: my $lockfile=$peerfile.".lock";
421: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
422: sleep(0.1);
423: }
424: # At this point, either a loncnew parent is listening or an old lonc
425: # or loncnew child is listening so we can connect or everything's dead.
426: #
427: # We'll give the connection a few tries before abandoning it. If
428: # connection is not possible, we'll con_lost back to the client.
429: #
430: my $client;
431: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
432: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
433: Type => SOCK_STREAM,
434: Timeout => 10);
435: if ($client) {
436: last; # Connected!
437: } else {
438: &create_connection(&hostname($server),$server);
439: }
440: sleep(0.1); # Try again later if failed connection.
441: }
442: my $answer;
443: if ($client) {
444: print $client "sethost:$server:$cmd\n";
445: $answer=<$client>;
446: if (!$answer) { $answer="con_lost"; }
447: chomp($answer);
448: } else {
449: $answer = 'con_lost'; # Failed connection.
450: }
451: return $answer;
452: }
453:
454: sub reply {
455: my ($cmd,$server)=@_;
456: unless (defined(&hostname($server))) { return 'no_such_host'; }
457: my $answer=subreply($cmd,$server);
458: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
459: &logthis("<font color=\"blue\">WARNING:".
460: " $cmd to $server returned $answer</font>");
461: }
462: return $answer;
463: }
464:
465: # ----------------------------------------------------------- Send USR1 to lonc
466:
467: sub reconlonc {
468: my ($lonid) = @_;
469: if ($lonid) {
470: my $hostname = &hostname($lonid);
471: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
472: if ($hostname && -e $peerfile) {
473: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
474: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
475: Type => SOCK_STREAM,
476: Timeout => 10);
477: if ($client) {
478: print $client ("reset_retries\n");
479: my $answer=<$client>;
480: #reset just this one.
481: }
482: }
483: return;
484: }
485:
486: &logthis("Trying to reconnect lonc");
487: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
488: if (open(my $fh,"<",$loncfile)) {
489: my $loncpid=<$fh>;
490: chomp($loncpid);
491: if (kill 0 => $loncpid) {
492: &logthis("lonc at pid $loncpid responding, sending USR1");
493: kill USR1 => $loncpid;
494: sleep 1;
495: } else {
496: &logthis(
497: "<font color=\"blue\">WARNING:".
498: " lonc at pid $loncpid not responding, giving up</font>");
499: }
500: } else {
501: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
502: }
503: }
504:
505: # ------------------------------------------------------ Critical communication
506:
507: sub critical {
508: my ($cmd,$server)=@_;
509: unless (&hostname($server)) {
510: &logthis("<font color=\"blue\">WARNING:".
511: " Critical message to unknown server ($server)</font>");
512: return 'no_such_host';
513: }
514: my $answer=reply($cmd,$server);
515: if ($answer eq 'con_lost') {
516: &reconlonc($server);
517: my $answer=reply($cmd,$server);
518: if ($answer eq 'con_lost') {
519: my $now=time;
520: my $middlename=$cmd;
521: $middlename=substr($middlename,0,16);
522: $middlename=~s/\W//g;
523: my $dfilename=
524: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
525: $dumpcount++;
526: {
527: my $dfh;
528: if (open($dfh,">",$dfilename)) {
529: print $dfh "$cmd\n";
530: close($dfh);
531: }
532: }
533: sleep 1;
534: my $wcmd='';
535: {
536: my $dfh;
537: if (open($dfh,"<",$dfilename)) {
538: $wcmd=<$dfh>;
539: close($dfh);
540: }
541: }
542: chomp($wcmd);
543: if ($wcmd eq $cmd) {
544: &logthis("<font color=\"blue\">WARNING: ".
545: "Connection buffer $dfilename: $cmd</font>");
546: &logperm("D:$server:$cmd");
547: return 'con_delayed';
548: } else {
549: &logthis("<font color=\"red\">CRITICAL:"
550: ." Critical connection failed: $server $cmd</font>");
551: &logperm("F:$server:$cmd");
552: return 'con_failed';
553: }
554: }
555: }
556: return $answer;
557: }
558:
559: # ------------------------------------------- check if return value is an error
560:
561: sub error {
562: my ($result) = @_;
563: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
564: if ($2 == 2) { return undef; }
565: return $1;
566: }
567: return undef;
568: }
569:
570: sub convert_and_load_session_env {
571: my ($lonidsdir,$handle)=@_;
572: my @profile;
573: {
574: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
575: if (!$opened) {
576: return 0;
577: }
578: flock($idf,LOCK_SH);
579: @profile=<$idf>;
580: close($idf);
581: }
582: my %temp_env;
583: foreach my $line (@profile) {
584: if ($line !~ m/=/) {
585: return 0;
586: }
587: chomp($line);
588: my ($envname,$envvalue)=split(/=/,$line,2);
589: $temp_env{&unescape($envname)} = &unescape($envvalue);
590: }
591: unlink("$lonidsdir/$handle.id");
592: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
593: 0640)) {
594: %disk_env = %temp_env;
595: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
596: untie(%disk_env);
597: }
598: return 1;
599: }
600:
601: # ------------------------------------------- Transfer profile into environment
602: my $env_loaded;
603: sub transfer_profile_to_env {
604: my ($lonidsdir,$handle,$force_transfer) = @_;
605: if (!$force_transfer && $env_loaded) { return; }
606:
607: if (!defined($lonidsdir)) {
608: $lonidsdir = $perlvar{'lonIDsDir'};
609: }
610: if (!defined($handle)) {
611: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
612: }
613:
614: my $convert;
615: {
616: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
617: if (!$opened) {
618: return;
619: }
620: flock($idf,LOCK_SH);
621: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
622: &GDBM_READER(),0640)) {
623: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
624: untie(%disk_env);
625: } else {
626: $convert = 1;
627: }
628: }
629: if ($convert) {
630: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
631: &logthis("Failed to load session, or convert session.");
632: }
633: }
634:
635: my %remove;
636: while ( my $envname = each(%env) ) {
637: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
638: if ($time < time-300) {
639: $remove{$key}++;
640: }
641: }
642: }
643:
644: $env{'user.environment'} = "$lonidsdir/$handle.id";
645: $env_loaded=1;
646: foreach my $expired_key (keys(%remove)) {
647: &delenv($expired_key);
648: }
649: }
650:
651: # ---------------------------------------------------- Check for valid session
652: sub check_for_valid_session {
653: my ($r,$name,$userhashref,$domref) = @_;
654: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
655: my ($linkname,$pubname);
656: if ($name eq '') {
657: $name = 'lonID';
658: $linkname = 'lonLinkID';
659: $pubname = 'lonPubID';
660: }
661: my $lonid=$cookies{$name};
662: if (!$lonid) {
663: if (($name eq 'lonID') && ($ENV{'SERVER_PORT'} != 443) && ($linkname)) {
664: $lonid=$cookies{$linkname};
665: }
666: if (!$lonid) {
667: if (($name eq 'lonID') && ($pubname)) {
668: $lonid=$cookies{$pubname};
669: }
670: }
671: }
672: return undef if (!$lonid);
673:
674: my $handle=&LONCAPA::clean_handle($lonid->value);
675: my $lonidsdir;
676: if ($name eq 'lonDAV') {
677: $lonidsdir=$r->dir_config('lonDAVsessDir');
678: } else {
679: $lonidsdir=$r->dir_config('lonIDsDir');
680: }
681: if (!-e "$lonidsdir/$handle.id") {
682: if ((ref($domref)) && ($name eq 'lonID') &&
683: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
684: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
685: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
686: $$domref = $possudom;
687: }
688: }
689: return undef;
690: }
691:
692: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
693: return undef if (!$opened);
694:
695: flock($idf,LOCK_SH);
696: my %disk_env;
697: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
698: &GDBM_READER(),0640)) {
699: return undef;
700: }
701:
702: if (!defined($disk_env{'user.name'})
703: || !defined($disk_env{'user.domain'})) {
704: return undef;
705: }
706:
707: if (ref($userhashref) eq 'HASH') {
708: $userhashref->{'name'} = $disk_env{'user.name'};
709: $userhashref->{'domain'} = $disk_env{'user.domain'};
710: $userhashref->{'lti'} = $disk_env{'request.lti.login'};
711: }
712:
713: return $handle;
714: }
715:
716: sub timed_flock {
717: my ($file,$lock_type) = @_;
718: my $failed=0;
719: eval {
720: local $SIG{__DIE__}='DEFAULT';
721: local $SIG{ALRM}=sub {
722: $failed=1;
723: die("failed lock");
724: };
725: alarm(13);
726: flock($file,$lock_type);
727: alarm(0);
728: };
729: if ($failed) {
730: return undef;
731: } else {
732: return 1;
733: }
734: }
735:
736: # ---------------------------------------------------------- Append Environment
737:
738: sub appenv {
739: my ($newenv,$roles) = @_;
740: if (ref($newenv) eq 'HASH') {
741: foreach my $key (keys(%{$newenv})) {
742: my $refused = 0;
743: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
744: $refused = 1;
745: if (ref($roles) eq 'ARRAY') {
746: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
747: if (grep(/^\Q$role\E$/,@{$roles})) {
748: $refused = 0;
749: }
750: }
751: }
752: if ($refused) {
753: &logthis("<font color=\"blue\">WARNING: ".
754: "Attempt to modify environment ".$key." to ".$newenv->{$key}
755: .'</font>');
756: delete($newenv->{$key});
757: } else {
758: $env{$key}=$newenv->{$key};
759: }
760: }
761: my $opened = open(my $env_file,'+<',$env{'user.environment'});
762: if ($opened
763: && &timed_flock($env_file,LOCK_EX)
764: &&
765: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
766: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
767: while (my ($key,$value) = each(%{$newenv})) {
768: $disk_env{$key} = $value;
769: }
770: untie(%disk_env);
771: }
772: }
773: return 'ok';
774: }
775: # ----------------------------------------------------- Delete from Environment
776:
777: sub delenv {
778: my ($delthis,$regexp,$roles) = @_;
779: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
780: my $refused = 1;
781: if (ref($roles) eq 'ARRAY') {
782: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
783: if (grep(/^\Q$role\E$/,@{$roles})) {
784: $refused = 0;
785: }
786: }
787: if ($refused) {
788: &logthis("<font color=\"blue\">WARNING: ".
789: "Attempt to delete from environment ".$delthis);
790: return 'error';
791: }
792: }
793: my $opened = open(my $env_file,'+<',$env{'user.environment'});
794: if ($opened
795: && &timed_flock($env_file,LOCK_EX)
796: &&
797: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
798: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
799: foreach my $key (keys(%disk_env)) {
800: if ($regexp) {
801: if ($key=~/^$delthis/) {
802: delete($env{$key});
803: delete($disk_env{$key});
804: }
805: } else {
806: if ($key=~/^\Q$delthis\E/) {
807: delete($env{$key});
808: delete($disk_env{$key});
809: }
810: }
811: }
812: untie(%disk_env);
813: }
814: return 'ok';
815: }
816:
817: sub get_env_multiple {
818: my ($name) = @_;
819: my @values;
820: if (defined($env{$name})) {
821: # exists is it an array
822: if (ref($env{$name})) {
823: @values=@{ $env{$name} };
824: } else {
825: $values[0]=$env{$name};
826: }
827: }
828: return(@values);
829: }
830:
831: # ------------------------------------------------------------------- Locking
832:
833: sub set_lock {
834: my ($text)=@_;
835: $locknum++;
836: my $id=$$.'-'.$locknum;
837: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
838: 'session.lock.'.$id => $text});
839: return $id;
840: }
841:
842: sub get_locks {
843: my $num=0;
844: my %texts=();
845: foreach my $lock (split(/\,/,$env{'session.locks'})) {
846: if ($lock=~/\w/) {
847: $num++;
848: $texts{$lock}=$env{'session.lock.'.$lock};
849: }
850: }
851: return ($num,%texts);
852: }
853:
854: sub remove_lock {
855: my ($id)=@_;
856: my $newlocks='';
857: foreach my $lock (split(/\,/,$env{'session.locks'})) {
858: if (($lock=~/\w/) && ($lock ne $id)) {
859: $newlocks.=','.$lock;
860: }
861: }
862: &appenv({'session.locks' => $newlocks});
863: &delenv('session.lock.'.$id);
864: }
865:
866: sub remove_all_locks {
867: my $activelocks=$env{'session.locks'};
868: foreach my $lock (split(/\,/,$env{'session.locks'})) {
869: if ($lock=~/\w/) {
870: &remove_lock($lock);
871: }
872: }
873: }
874:
875:
876: # ------------------------------------------ Find out current server userload
877: sub userload {
878: my $numusers=0;
879: {
880: opendir(LONIDS,$perlvar{'lonIDsDir'});
881: my $filename;
882: my $curtime=time;
883: while ($filename=readdir(LONIDS)) {
884: next if ($filename eq '.' || $filename eq '..');
885: next if ($filename =~ /publicuser_\d+\.id/);
886: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
887: if ($curtime-$mtime < 1800) { $numusers++; }
888: }
889: closedir(LONIDS);
890: }
891: my $userloadpercent=0;
892: my $maxuserload=$perlvar{'lonUserLoadLim'};
893: if ($maxuserload) {
894: $userloadpercent=100*$numusers/$maxuserload;
895: }
896: $userloadpercent=sprintf("%.2f",$userloadpercent);
897: return $userloadpercent;
898: }
899:
900: # ------------------------------ Find server with least workload from spare.tab
901:
902: sub spareserver {
903: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
904: my $spare_server;
905: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
906: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
907: : $userloadpercent;
908: my ($uint_dom,$remotesessions);
909: if (($udom ne '') && (&domain($udom) ne '')) {
910: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
911: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
912: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
913: $remotesessions = $udomdefaults{'remotesessions'};
914: }
915: my $spareshash = &this_host_spares($udom);
916: if (ref($spareshash) eq 'HASH') {
917: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
918: foreach my $try_server (@{ $spareshash->{'primary'} }) {
919: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
920: $try_server));
921: ($spare_server, $lowest_load) =
922: &compare_server_load($try_server, $spare_server, $lowest_load);
923: }
924: }
925:
926: my $found_server = ($spare_server ne '' && $lowest_load < 100);
927:
928: if (!$found_server) {
929: if (ref($spareshash->{'default'}) eq 'ARRAY') {
930: foreach my $try_server (@{ $spareshash->{'default'} }) {
931: next unless (&spare_can_host($udom,$uint_dom,
932: $remotesessions,$try_server));
933: ($spare_server, $lowest_load) =
934: &compare_server_load($try_server, $spare_server, $lowest_load);
935: }
936: }
937: }
938: }
939:
940: if (!$want_server_name) {
941: my $protocol = 'http';
942: if ($protocol{$spare_server} eq 'https') {
943: $protocol = $protocol{$spare_server};
944: }
945: if (defined($spare_server)) {
946: my $hostname = &hostname($spare_server);
947: if (defined($hostname)) {
948: $spare_server = $protocol.'://'.$hostname;
949: }
950: }
951: }
952: return $spare_server;
953: }
954:
955: sub compare_server_load {
956: my ($try_server, $spare_server, $lowest_load, $required) = @_;
957:
958: if ($required) {
959: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
960: my $remoterev = &get_server_loncaparev(undef,$try_server);
961: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
962: if (($major eq '' && $minor eq '') ||
963: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
964: return ($spare_server,$lowest_load);
965: }
966: }
967:
968: my $loadans = &reply('load', $try_server);
969: my $userloadans = &reply('userload',$try_server);
970:
971: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
972: return ($spare_server, $lowest_load); #didn't get a number from the server
973: }
974:
975: my $load;
976: if ($loadans =~ /\d/) {
977: if ($userloadans =~ /\d/) {
978: #both are numbers, pick the bigger one
979: $load = ($loadans > $userloadans) ? $loadans
980: : $userloadans;
981: } else {
982: $load = $loadans;
983: }
984: } else {
985: $load = $userloadans;
986: }
987:
988: if (($load =~ /\d/) && ($load < $lowest_load)) {
989: $spare_server = $try_server;
990: $lowest_load = $load;
991: }
992: return ($spare_server,$lowest_load);
993: }
994:
995: # --------------------------- ask offload servers if user already has a session
996: sub find_existing_session {
997: my ($udom,$uname) = @_;
998: my $spareshash = &this_host_spares($udom);
999: if (ref($spareshash) eq 'HASH') {
1000: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1001: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1002: return $try_server if (&has_user_session($try_server, $udom, $uname));
1003: }
1004: }
1005: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1006: foreach my $try_server (@{ $spareshash->{'default'} }) {
1007: return $try_server if (&has_user_session($try_server, $udom, $uname));
1008: }
1009: }
1010: }
1011: return;
1012: }
1013:
1014: # -------------------------------- ask if server already has a session for user
1015: sub has_user_session {
1016: my ($lonid,$udom,$uname) = @_;
1017: my $result = &reply(join(':','userhassession',
1018: map {&escape($_)} ($udom,$uname)),$lonid);
1019: return 1 if ($result eq 'ok');
1020:
1021: return 0;
1022: }
1023:
1024: # --------- determine least loaded server in a user's domain which allows login
1025:
1026: sub choose_server {
1027: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1028: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1029: my %servers = &get_servers($udom);
1030: my $lowest_load = 30000;
1031: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1032: if ($skiploadbal) {
1033: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1034: unless (defined($cached)) {
1035: my $cachetime = 60*60*24;
1036: my %domconfig =
1037: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1038: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1039: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1040: $cachetime);
1041: }
1042: }
1043: }
1044: foreach my $lonhost (keys(%servers)) {
1045: if ($skiploadbal) {
1046: if (ref($balancers) eq 'HASH') {
1047: next if (exists($balancers->{$lonhost}));
1048: }
1049: }
1050: my $loginvia;
1051: if ($checkloginvia) {
1052: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1053: if ($loginvia) {
1054: my ($server,$path) = split(/:/,$loginvia);
1055: ($login_host, $lowest_load) =
1056: &compare_server_load($server, $login_host, $lowest_load, $required);
1057: if ($login_host eq $server) {
1058: $portal_path = $path;
1059: $isredirect = 1;
1060: }
1061: } else {
1062: ($login_host, $lowest_load) =
1063: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1064: if ($login_host eq $lonhost) {
1065: $portal_path = '';
1066: $isredirect = '';
1067: }
1068: }
1069: } else {
1070: ($login_host, $lowest_load) =
1071: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1072: }
1073: }
1074: if ($login_host ne '') {
1075: $hostname = &hostname($login_host);
1076: }
1077: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1078: }
1079:
1080: # --------------------------------------------- Try to change a user's password
1081:
1082: sub changepass {
1083: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1084: $currentpass = &escape($currentpass);
1085: $newpass = &escape($newpass);
1086: my $lonhost = $perlvar{'lonHostID'};
1087: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1088: $server);
1089: if (! $answer) {
1090: &logthis("No reply on password change request to $server ".
1091: "by $uname in domain $udom.");
1092: } elsif ($answer =~ "^ok") {
1093: &logthis("$uname in $udom successfully changed their password ".
1094: "on $server.");
1095: } elsif ($answer =~ "^pwchange_failure") {
1096: &logthis("$uname in $udom was unable to change their password ".
1097: "on $server. The action was blocked by either lcpasswd ".
1098: "or pwchange");
1099: } elsif ($answer =~ "^non_authorized") {
1100: &logthis("$uname in $udom did not get their password correct when ".
1101: "attempting to change it on $server.");
1102: } elsif ($answer =~ "^auth_mode_error") {
1103: &logthis("$uname in $udom attempted to change their password despite ".
1104: "not being locally or internally authenticated on $server.");
1105: } elsif ($answer =~ "^unknown_user") {
1106: &logthis("$uname in $udom attempted to change their password ".
1107: "on $server but were unable to because $server is not ".
1108: "their home server.");
1109: } elsif ($answer =~ "^refused") {
1110: &logthis("$server refused to change $uname in $udom password because ".
1111: "it was sent an unencrypted request to change the password.");
1112: } elsif ($answer =~ "invalid_client") {
1113: &logthis("$server refused to change $uname in $udom password because ".
1114: "it was a reset by e-mail originating from an invalid server.");
1115: }
1116: return $answer;
1117: }
1118:
1119: # ----------------------- Try to determine user's current authentication scheme
1120:
1121: sub queryauthenticate {
1122: my ($uname,$udom)=@_;
1123: my $uhome=&homeserver($uname,$udom);
1124: if (!$uhome) {
1125: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1126: return 'no_host';
1127: }
1128: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1129: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1130: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1131: }
1132: return $answer;
1133: }
1134:
1135: # --------- Try to authenticate user from domain's lib servers (first this one)
1136:
1137: sub authenticate {
1138: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1139: $upass=&escape($upass);
1140: $uname= &LONCAPA::clean_username($uname);
1141: my $uhome=&homeserver($uname,$udom,1);
1142: my $newhome;
1143: if ((!$uhome) || ($uhome eq 'no_host')) {
1144: # Maybe the machine was offline and only re-appeared again recently?
1145: &reconlonc();
1146: # One more
1147: $uhome=&homeserver($uname,$udom,1);
1148: if (($uhome eq 'no_host') && $checkdefauth) {
1149: if (defined(&domain($udom,'primary'))) {
1150: $newhome=&domain($udom,'primary');
1151: }
1152: if ($newhome ne '') {
1153: $uhome = $newhome;
1154: }
1155: }
1156: if ((!$uhome) || ($uhome eq 'no_host')) {
1157: &logthis("User $uname at $udom is unknown in authenticate");
1158: return 'no_host';
1159: }
1160: }
1161: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1162: if ($answer eq 'authorized') {
1163: if ($newhome) {
1164: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1165: return 'no_account_on_host';
1166: } else {
1167: &logthis("User $uname at $udom authorized by $uhome");
1168: return $uhome;
1169: }
1170: }
1171: if ($answer eq 'non_authorized') {
1172: &logthis("User $uname at $udom rejected by $uhome");
1173: return 'no_host';
1174: }
1175: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1176: return 'no_host';
1177: }
1178:
1179: sub can_host_session {
1180: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1181: my $canhost = 1;
1182: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1183: if (ref($remotesessions) eq 'HASH') {
1184: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1185: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1186: $canhost = 0;
1187: } else {
1188: $canhost = 1;
1189: }
1190: }
1191: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1192: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1193: $canhost = 1;
1194: } else {
1195: $canhost = 0;
1196: }
1197: }
1198: if ($canhost) {
1199: if ($remotesessions->{'version'} ne '') {
1200: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1201: if ($reqmajor ne '' && $reqminor ne '') {
1202: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1203: my $major = $1;
1204: my $minor = $2;
1205: if (($major < $reqmajor ) ||
1206: (($major == $reqmajor) && ($minor < $reqminor))) {
1207: $canhost = 0;
1208: }
1209: } else {
1210: $canhost = 0;
1211: }
1212: }
1213: }
1214: }
1215: }
1216: if ($canhost) {
1217: if (ref($hostedsessions) eq 'HASH') {
1218: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1219: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1220: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1221: if (($uint_dom ne '') &&
1222: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1223: $canhost = 0;
1224: } else {
1225: $canhost = 1;
1226: }
1227: }
1228: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1229: if (($uint_dom ne '') &&
1230: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1231: $canhost = 1;
1232: } else {
1233: $canhost = 0;
1234: }
1235: }
1236: }
1237: }
1238: return $canhost;
1239: }
1240:
1241: sub spare_can_host {
1242: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1243: my $canhost=1;
1244: my $try_server_hostname = &hostname($try_server);
1245: my $serverhomeID = &get_server_homeID($try_server_hostname);
1246: my $serverhomedom = &host_domain($serverhomeID);
1247: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1248: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1249: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1250: $canhost = 0;
1251: }
1252: }
1253: if (($canhost) && ($uint_dom)) {
1254: my @intdoms;
1255: my $internet_names = &get_internet_names($try_server);
1256: if (ref($internet_names) eq 'ARRAY') {
1257: @intdoms = @{$internet_names};
1258: }
1259: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1260: my $remoterev = &get_server_loncaparev(undef,$try_server);
1261: $canhost = &can_host_session($udom,$try_server,$remoterev,
1262: $remotesessions,
1263: $defdomdefaults{'hostedsessions'});
1264: }
1265: }
1266: return $canhost;
1267: }
1268:
1269: sub this_host_spares {
1270: my ($dom) = @_;
1271: my ($dom_in_use,$lonhost_in_use,$result);
1272: my @hosts = ¤t_machine_ids();
1273: foreach my $lonhost (@hosts) {
1274: if (&host_domain($lonhost) eq $dom) {
1275: $dom_in_use = $dom;
1276: $lonhost_in_use = $lonhost;
1277: last;
1278: }
1279: }
1280: if ($dom_in_use ne '') {
1281: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1282: }
1283: if (ref($result) ne 'HASH') {
1284: $lonhost_in_use = $perlvar{'lonHostID'};
1285: $dom_in_use = &host_domain($lonhost_in_use);
1286: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1287: if (ref($result) ne 'HASH') {
1288: $result = \%spareid;
1289: }
1290: }
1291: return $result;
1292: }
1293:
1294: sub spares_for_offload {
1295: my ($dom_in_use,$lonhost_in_use) = @_;
1296: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1297: if (defined($cached)) {
1298: return $result;
1299: } else {
1300: my $cachetime = 60*60*24;
1301: my %domconfig =
1302: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1303: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1304: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1305: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1306: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1307: }
1308: }
1309: }
1310: }
1311: return;
1312: }
1313:
1314: sub get_lonbalancer_config {
1315: my ($servers) = @_;
1316: my ($currbalancer,$currtargets);
1317: if (ref($servers) eq 'HASH') {
1318: foreach my $server (keys(%{$servers})) {
1319: my %what = (
1320: spareid => 1,
1321: perlvar => 1,
1322: );
1323: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1324: if ($result eq 'ok') {
1325: if (ref($returnhash) eq 'HASH') {
1326: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1327: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1328: $currbalancer = $server;
1329: $currtargets = {};
1330: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1331: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1332: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1333: }
1334: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1335: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1336: }
1337: }
1338: last;
1339: }
1340: }
1341: }
1342: }
1343: }
1344: }
1345: return ($currbalancer,$currtargets);
1346: }
1347:
1348: sub check_loadbalancing {
1349: my ($uname,$udom,$caller) = @_;
1350: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1351: $rule_in_effect,$offloadto,$otherserver);
1352: my $lonhost = $perlvar{'lonHostID'};
1353: my @hosts = ¤t_machine_ids();
1354: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1355: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1356: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1357: my $serverhomedom = &host_domain($lonhost);
1358: my $domneedscache;
1359: my $cachetime = 60*60*24;
1360:
1361: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1362: $dom_in_use = $udom;
1363: $homeintdom = 1;
1364: } else {
1365: $dom_in_use = $serverhomedom;
1366: }
1367: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1368: unless (defined($cached)) {
1369: my %domconfig =
1370: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1371: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1372: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1373: } else {
1374: $domneedscache = $dom_in_use;
1375: }
1376: }
1377: if (ref($result) eq 'HASH') {
1378: ($is_balancer,$currtargets,$currrules) =
1379: &check_balancer_result($result,@hosts);
1380: if ($is_balancer) {
1381: if (ref($currrules) eq 'HASH') {
1382: if ($homeintdom) {
1383: if ($uname ne '') {
1384: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1385: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1386: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1387: $rule_in_effect = $currrules->{'_LC_author'};
1388: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1389: $rule_in_effect = $currrules->{'_LC_adv'}
1390: }
1391: }
1392: if ($rule_in_effect eq '') {
1393: my %userenv = &userenvironment($udom,$uname,'inststatus');
1394: if ($userenv{'inststatus'} ne '') {
1395: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1396: my ($othertitle,$usertypes,$types) =
1397: &Apache::loncommon::sorted_inst_types($udom);
1398: if (ref($types) eq 'ARRAY') {
1399: foreach my $type (@{$types}) {
1400: if (grep(/^\Q$type\E$/,@statuses)) {
1401: if (exists($currrules->{$type})) {
1402: $rule_in_effect = $currrules->{$type};
1403: }
1404: }
1405: }
1406: }
1407: } else {
1408: if (exists($currrules->{'default'})) {
1409: $rule_in_effect = $currrules->{'default'};
1410: }
1411: }
1412: }
1413: } else {
1414: if (exists($currrules->{'default'})) {
1415: $rule_in_effect = $currrules->{'default'};
1416: }
1417: }
1418: } else {
1419: if ($currrules->{'_LC_external'} ne '') {
1420: $rule_in_effect = $currrules->{'_LC_external'};
1421: }
1422: }
1423: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1424: $uname,$udom);
1425: }
1426: }
1427: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1428: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1429: unless (defined($cached)) {
1430: my %domconfig =
1431: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1432: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1433: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1434: } else {
1435: $domneedscache = $serverhomedom;
1436: }
1437: }
1438: if (ref($result) eq 'HASH') {
1439: ($is_balancer,$currtargets,$currrules) =
1440: &check_balancer_result($result,@hosts);
1441: if ($is_balancer) {
1442: if (ref($currrules) eq 'HASH') {
1443: if ($currrules->{'_LC_internetdom'} ne '') {
1444: $rule_in_effect = $currrules->{'_LC_internetdom'};
1445: }
1446: }
1447: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1448: $uname,$udom);
1449: }
1450: } else {
1451: if ($perlvar{'lonBalancer'} eq 'yes') {
1452: $is_balancer = 1;
1453: $offloadto = &this_host_spares($dom_in_use);
1454: }
1455: unless (defined($cached)) {
1456: $domneedscache = $serverhomedom;
1457: }
1458: }
1459: } else {
1460: if ($perlvar{'lonBalancer'} eq 'yes') {
1461: $is_balancer = 1;
1462: $offloadto = &this_host_spares($dom_in_use);
1463: }
1464: unless (defined($cached)) {
1465: $domneedscache = $serverhomedom;
1466: }
1467: }
1468: if ($domneedscache) {
1469: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1470: }
1471: if ($is_balancer) {
1472: my $lowest_load = 30000;
1473: if (ref($offloadto) eq 'HASH') {
1474: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1475: foreach my $try_server (@{$offloadto->{'primary'}}) {
1476: ($otherserver,$lowest_load) =
1477: &compare_server_load($try_server,$otherserver,$lowest_load);
1478: }
1479: }
1480: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1481:
1482: if (!$found_server) {
1483: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1484: foreach my $try_server (@{$offloadto->{'default'}}) {
1485: ($otherserver,$lowest_load) =
1486: &compare_server_load($try_server,$otherserver,$lowest_load);
1487: }
1488: }
1489: }
1490: } elsif (ref($offloadto) eq 'ARRAY') {
1491: if (@{$offloadto} == 1) {
1492: $otherserver = $offloadto->[0];
1493: } elsif (@{$offloadto} > 1) {
1494: foreach my $try_server (@{$offloadto}) {
1495: ($otherserver,$lowest_load) =
1496: &compare_server_load($try_server,$otherserver,$lowest_load);
1497: }
1498: }
1499: }
1500: unless ($caller eq 'login') {
1501: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
1502: $is_balancer = 0;
1503: if ($uname ne '' && $udom ne '') {
1504: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1505:
1506: &appenv({'user.loadbalexempt' => $lonhost,
1507: 'user.loadbalcheck.time' => time});
1508: }
1509: }
1510: }
1511: }
1512: }
1513: return ($is_balancer,$otherserver);
1514: }
1515:
1516: sub check_balancer_result {
1517: my ($result,@hosts) = @_;
1518: my ($is_balancer,$currtargets,$currrules);
1519: if (ref($result) eq 'HASH') {
1520: if ($result->{'lonhost'} ne '') {
1521: my $currbalancer = $result->{'lonhost'};
1522: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1523: $is_balancer = 1;
1524: $currtargets = $result->{'targets'};
1525: $currrules = $result->{'rules'};
1526: }
1527: } else {
1528: foreach my $key (keys(%{$result})) {
1529: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1530: (ref($result->{$key}) eq 'HASH')) {
1531: $is_balancer = 1;
1532: $currrules = $result->{$key}{'rules'};
1533: $currtargets = $result->{$key}{'targets'};
1534: last;
1535: }
1536: }
1537: }
1538: }
1539: return ($is_balancer,$currtargets,$currrules);
1540: }
1541:
1542: sub get_loadbalancer_targets {
1543: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1544: my $offloadto;
1545: if ($rule_in_effect eq 'none') {
1546: return [$perlvar{'lonHostID'}];
1547: } elsif ($rule_in_effect eq '') {
1548: $offloadto = $currtargets;
1549: } else {
1550: if ($rule_in_effect eq 'homeserver') {
1551: my $homeserver = &homeserver($uname,$udom);
1552: if ($homeserver ne 'no_host') {
1553: $offloadto = [$homeserver];
1554: }
1555: } elsif ($rule_in_effect eq 'externalbalancer') {
1556: my %domconfig =
1557: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1558: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1559: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1560: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1561: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1562: }
1563: }
1564: } else {
1565: my %servers = &internet_dom_servers($udom);
1566: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1567: if (&hostname($remotebalancer) ne '') {
1568: $offloadto = [$remotebalancer];
1569: }
1570: }
1571: } elsif (&hostname($rule_in_effect) ne '') {
1572: $offloadto = [$rule_in_effect];
1573: }
1574: }
1575: return $offloadto;
1576: }
1577:
1578: sub internet_dom_servers {
1579: my ($dom) = @_;
1580: my (%uniqservers,%servers);
1581: my $primaryserver = &hostname(&domain($dom,'primary'));
1582: my @machinedoms = &machine_domains($primaryserver);
1583: foreach my $mdom (@machinedoms) {
1584: my %currservers = %servers;
1585: my %server = &get_servers($mdom);
1586: %servers = (%currservers,%server);
1587: }
1588: my %by_hostname;
1589: foreach my $id (keys(%servers)) {
1590: push(@{$by_hostname{$servers{$id}}},$id);
1591: }
1592: foreach my $hostname (sort(keys(%by_hostname))) {
1593: if (@{$by_hostname{$hostname}} > 1) {
1594: my $match = 0;
1595: foreach my $id (@{$by_hostname{$hostname}}) {
1596: if (&host_domain($id) eq $dom) {
1597: $uniqservers{$id} = $hostname;
1598: $match = 1;
1599: }
1600: }
1601: unless ($match) {
1602: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1603: }
1604: } else {
1605: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1606: }
1607: }
1608: return %uniqservers;
1609: }
1610:
1611: sub trusted_domains {
1612: my ($cmdtype,$calldom) = @_;
1613: my ($trusted,$untrusted);
1614: if (&domain($calldom) eq '') {
1615: return ($trusted,$untrusted);
1616: }
1617: unless ($cmdtype =~ /^(content|shared|enroll|coaurem|domroles|catalog|reqcrs|msg)$/) {
1618: return ($trusted,$untrusted);
1619: }
1620: my $callprimary = &domain($calldom,'primary');
1621: my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
1622: if ($intcalldom eq '') {
1623: return ($trusted,$untrusted);
1624: }
1625:
1626: my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
1627: unless (defined($cached)) {
1628: my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
1629: &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
1630: $trustconfig = $domconfig{'trust'};
1631: }
1632: if (ref($trustconfig)) {
1633: my (%possexc,%possinc,@allexc,@allinc);
1634: if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
1635: if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
1636: map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}};
1637: }
1638: if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
1639: map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
1640: }
1641: }
1642: if (keys(%possexc)) {
1643: if (keys(%possinc)) {
1644: foreach my $key (sort(keys(%possexc))) {
1645: next if ($key eq $intcalldom);
1646: unless ($possinc{$key}) {
1647: push(@allexc,$key);
1648: }
1649: }
1650: } else {
1651: @allexc = sort(keys(%possexc));
1652: }
1653: }
1654: if (keys(%possinc)) {
1655: $possinc{$intcalldom} = 1;
1656: @allinc = sort(keys(%possinc));
1657: }
1658: if ((@allexc > 0) || (@allinc > 0)) {
1659: my %doms_by_intdom;
1660: my %allintdoms = &all_host_intdom();
1661: my %alldoms = &all_host_domain();
1662: foreach my $key (%allintdoms) {
1663: if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
1664: unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
1665: push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
1666: }
1667: } else {
1668: $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}];
1669: }
1670: }
1671: foreach my $exc (@allexc) {
1672: if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
1673: $untrusted = $doms_by_intdom{$exc};
1674: }
1675: }
1676: foreach my $inc (@allinc) {
1677: if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
1678: $trusted = $doms_by_intdom{$inc};
1679: }
1680: }
1681: }
1682: }
1683: return ($trusted,$untrusted);
1684: }
1685:
1686: sub will_trust {
1687: my ($cmdtype,$domain,$possdom) = @_;
1688: return 1 if ($domain eq $possdom);
1689: my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
1690: my $willtrust;
1691: if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
1692: if (grep(/^\Q$domain\E$/,@{$trustedref})) {
1693: $willtrust = 1;
1694: }
1695: } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
1696: unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
1697: $willtrust = 1;
1698: }
1699: } else {
1700: $willtrust = 1;
1701: }
1702: return $willtrust;
1703: }
1704:
1705: # ---------------------- Find the homebase for a user from domain's lib servers
1706:
1707: my %homecache;
1708: sub homeserver {
1709: my ($uname,$udom,$ignoreBadCache)=@_;
1710: my $index="$uname:$udom";
1711:
1712: if (exists($homecache{$index})) { return $homecache{$index}; }
1713:
1714: my %servers = &get_servers($udom,'library');
1715: foreach my $tryserver (keys(%servers)) {
1716: next if ($ignoreBadCache ne 'true' &&
1717: exists($badServerCache{$tryserver}));
1718:
1719: my $answer=reply("home:$udom:$uname",$tryserver);
1720: if ($answer eq 'found') {
1721: delete($badServerCache{$tryserver});
1722: return $homecache{$index}=$tryserver;
1723: } elsif ($answer eq 'no_host') {
1724: $badServerCache{$tryserver}=1;
1725: }
1726: }
1727: return 'no_host';
1728: }
1729:
1730: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1731:
1732: sub idget {
1733: my ($udom,$idsref,$namespace)=@_;
1734: my %returnhash=();
1735: my @ids=();
1736: if (ref($idsref) eq 'ARRAY') {
1737: @ids = @{$idsref};
1738: } else {
1739: return %returnhash;
1740: }
1741: if ($namespace eq '') {
1742: $namespace = 'ids';
1743: }
1744:
1745: my %servers = &get_servers($udom,'library');
1746: foreach my $tryserver (keys(%servers)) {
1747: my $idlist=join('&', map { &escape($_); } @ids);
1748: if ($namespace eq 'ids') {
1749: $idlist=~tr/A-Z/a-z/;
1750: }
1751: my $reply;
1752: if ($namespace eq 'ids') {
1753: $reply=&reply("idget:$udom:".$idlist,$tryserver);
1754: } else {
1755: $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
1756: }
1757: my @answer=();
1758: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1759: @answer=split(/\&/,$reply);
1760: } ;
1761: my $i;
1762: for ($i=0;$i<=$#ids;$i++) {
1763: if ($answer[$i]) {
1764: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1765: }
1766: }
1767: }
1768: return %returnhash;
1769: }
1770:
1771: # ------------------------------------- Find the IDs behind a list of usernames
1772:
1773: sub idrget {
1774: my ($udom,@unames)=@_;
1775: my %returnhash=();
1776: foreach my $uname (@unames) {
1777: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1778: }
1779: return %returnhash;
1780: }
1781:
1782: # Store away a list of names and associated student/employee IDs or clicker IDs
1783:
1784: sub idput {
1785: my ($udom,$idsref,$uhom,$namespace)=@_;
1786: my %servers=();
1787: my %ids=();
1788: my %byid = ();
1789: if (ref($idsref) eq 'HASH') {
1790: %ids=%{$idsref};
1791: }
1792: if ($namespace eq '') {
1793: $namespace = 'ids';
1794: }
1795: foreach my $uname (keys(%ids)) {
1796: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1797: if ($uhom eq '') {
1798: $uhom=&homeserver($uname,$udom);
1799: }
1800: if ($uhom ne 'no_host') {
1801: my $esc_unam=&escape($uname);
1802: if ($namespace eq 'ids') {
1803: my $id=&escape($ids{$uname});
1804: $id=~tr/A-Z/a-z/;
1805: my $esc_unam=&escape($uname);
1806: $servers{$uhom}.=$id.'='.$esc_unam.'&';
1807: } else {
1808: my @currids = split(/,/,$ids{$uname});
1809: foreach my $id (@currids) {
1810: $byid{$uhom}{$id} .= $uname.',';
1811: }
1812: }
1813: }
1814: }
1815: if ($namespace eq 'clickers') {
1816: foreach my $server (keys(%byid)) {
1817: if (ref($byid{$server}) eq 'HASH') {
1818: foreach my $id (keys(%{$byid{$server}})) {
1819: $byid{$server} =~ s/,$//;
1820: $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&';
1821: }
1822: }
1823: }
1824: }
1825: foreach my $server (keys(%servers)) {
1826: $servers{$server} =~ s/\&$//;
1827: if ($namespace eq 'ids') {
1828: &critical('idput:'.$udom.':'.$servers{$server},$server);
1829: } else {
1830: &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
1831: }
1832: }
1833: }
1834:
1835: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1836:
1837: sub iddel {
1838: my ($udom,$idshashref,$uhome,$namespace)=@_;
1839: my %result=();
1840: my %ids=();
1841: my %byid = ();
1842: if (ref($idshashref) eq 'HASH') {
1843: %ids=%{$idshashref};
1844: } else {
1845: return %result;
1846: }
1847: if ($namespace eq '') {
1848: $namespace = 'ids';
1849: }
1850: my %servers=();
1851: while (my ($id,$unamestr) = each(%ids)) {
1852: if ($namespace eq 'ids') {
1853: my $uhom = $uhome;
1854: if ($uhom eq '') {
1855: $uhom=&homeserver($unamestr,$udom);
1856: }
1857: if ($uhom ne 'no_host') {
1858: $servers{$uhom}.='&'.&escape($id);
1859: }
1860: } else {
1861: my @curritems = split(/,/,$ids{$id});
1862: foreach my $uname (@curritems) {
1863: my $uhom = $uhome;
1864: if ($uhom eq '') {
1865: $uhom=&homeserver($uname,$udom);
1866: }
1867: if ($uhom ne 'no_host') {
1868: $byid{$uhom}{$id} .= $uname.',';
1869: }
1870: }
1871: }
1872: }
1873: if ($namespace eq 'clickers') {
1874: foreach my $server (keys(%byid)) {
1875: if (ref($byid{$server}) eq 'HASH') {
1876: foreach my $id (keys(%{$byid{$server}})) {
1877: $byid{$server}{$id} =~ s/,$//;
1878: $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
1879: }
1880: }
1881: }
1882: }
1883: foreach my $server (keys(%servers)) {
1884: $servers{$server} =~ s/\&$//;
1885: if ($namespace eq 'ids') {
1886: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1887: } elsif ($namespace eq 'clickers') {
1888: $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
1889: }
1890: }
1891: return %result;
1892: }
1893:
1894: # ----- Update clicker ID-to-username look-ups in clickers.db on library server
1895:
1896: sub updateclickers {
1897: my ($udom,$action,$idshashref,$uhome,$critical) = @_;
1898: my %clickers;
1899: if (ref($idshashref) eq 'HASH') {
1900: %clickers=%{$idshashref};
1901: } else {
1902: return;
1903: }
1904: my $items='';
1905: foreach my $item (keys(%clickers)) {
1906: $items.=&escape($item).'='.&escape($clickers{$item}).'&';
1907: }
1908: $items=~s/\&$//;
1909: my $request = "updateclickers:$udom:$action:$items";
1910: if ($critical) {
1911: return &critical($request,$uhome);
1912: } else {
1913: return &reply($request,$uhome);
1914: }
1915: }
1916:
1917: # ------------------------------dump from db file owned by domainconfig user
1918: sub dump_dom {
1919: my ($namespace, $udom, $regexp) = @_;
1920:
1921: $udom ||= $env{'user.domain'};
1922:
1923: return () unless $udom;
1924:
1925: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1926: }
1927:
1928: # ------------------------------------------ get items from domain db files
1929:
1930: sub get_dom {
1931: my ($namespace,$storearr,$udom,$uhome)=@_;
1932: return if ($udom eq 'public');
1933: my $items='';
1934: foreach my $item (@$storearr) {
1935: $items.=&escape($item).'&';
1936: }
1937: $items=~s/\&$//;
1938: if (!$udom) {
1939: $udom=$env{'user.domain'};
1940: return if ($udom eq 'public');
1941: if (defined(&domain($udom,'primary'))) {
1942: $uhome=&domain($udom,'primary');
1943: } else {
1944: undef($uhome);
1945: }
1946: } else {
1947: if (!$uhome) {
1948: if (defined(&domain($udom,'primary'))) {
1949: $uhome=&domain($udom,'primary');
1950: }
1951: }
1952: }
1953: if ($udom && $uhome && ($uhome ne 'no_host')) {
1954: my $rep;
1955: if ($namespace =~ /^enc/) {
1956: $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
1957: } else {
1958: $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1959: }
1960: my %returnhash;
1961: if ($rep eq '' || $rep =~ /^error: 2 /) {
1962: return %returnhash;
1963: }
1964: my @pairs=split(/\&/,$rep);
1965: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1966: return @pairs;
1967: }
1968: my $i=0;
1969: foreach my $item (@$storearr) {
1970: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1971: $i++;
1972: }
1973: return %returnhash;
1974: } else {
1975: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1976: }
1977: }
1978:
1979: # -------------------------------------------- put items in domain db files
1980:
1981: sub put_dom {
1982: my ($namespace,$storehash,$udom,$uhome)=@_;
1983: if (!$udom) {
1984: $udom=$env{'user.domain'};
1985: if (defined(&domain($udom,'primary'))) {
1986: $uhome=&domain($udom,'primary');
1987: } else {
1988: undef($uhome);
1989: }
1990: } else {
1991: if (!$uhome) {
1992: if (defined(&domain($udom,'primary'))) {
1993: $uhome=&domain($udom,'primary');
1994: }
1995: }
1996: }
1997: if ($udom && $uhome && ($uhome ne 'no_host')) {
1998: my $items='';
1999: foreach my $item (keys(%$storehash)) {
2000: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
2001: }
2002: $items=~s/\&$//;
2003: if ($namespace =~ /^enc/) {
2004: return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
2005: } else {
2006: return &reply("putdom:$udom:$namespace:$items",$uhome);
2007: }
2008: } else {
2009: &logthis("put_dom failed - no homeserver and/or domain");
2010: }
2011: }
2012:
2013: # --------------------- newput for items in db file owned by domainconfig user
2014: sub newput_dom {
2015: my ($namespace,$storehash,$udom) = @_;
2016: my $result;
2017: if (!$udom) {
2018: $udom=$env{'user.domain'};
2019: }
2020: if ($udom) {
2021: my $uname = &get_domainconfiguser($udom);
2022: $result = &newput($namespace,$storehash,$udom,$uname);
2023: }
2024: return $result;
2025: }
2026:
2027: # --------------------- delete for items in db file owned by domainconfig user
2028: sub del_dom {
2029: my ($namespace,$storearr,$udom)=@_;
2030: if (ref($storearr) eq 'ARRAY') {
2031: if (!$udom) {
2032: $udom=$env{'user.domain'};
2033: }
2034: if ($udom) {
2035: my $uname = &get_domainconfiguser($udom);
2036: return &del($namespace,$storearr,$udom,$uname);
2037: }
2038: }
2039: }
2040:
2041: # ----------------------------------construct domainconfig user for a domain
2042: sub get_domainconfiguser {
2043: my ($udom) = @_;
2044: return $udom.'-domainconfig';
2045: }
2046:
2047: sub retrieve_inst_usertypes {
2048: my ($udom) = @_;
2049: my (%returnhash,@order);
2050: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
2051: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
2052: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
2053: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
2054: } else {
2055: if (defined(&domain($udom,'primary'))) {
2056: my $uhome=&domain($udom,'primary');
2057: my $rep=&reply("inst_usertypes:$udom",$uhome);
2058: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
2059: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
2060: return (\%returnhash,\@order);
2061: }
2062: my ($hashitems,$orderitems) = split(/:/,$rep);
2063: my @pairs=split(/\&/,$hashitems);
2064: foreach my $item (@pairs) {
2065: my ($key,$value)=split(/=/,$item,2);
2066: $key = &unescape($key);
2067: next if ($key =~ /^error: 2 /);
2068: $returnhash{$key}=&thaw_unescape($value);
2069: }
2070: my @esc_order = split(/\&/,$orderitems);
2071: foreach my $item (@esc_order) {
2072: push(@order,&unescape($item));
2073: }
2074: } else {
2075: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
2076: }
2077: return (\%returnhash,\@order);
2078: }
2079: }
2080:
2081: sub is_domainimage {
2082: my ($url) = @_;
2083: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
2084: if (&domain($1) ne '') {
2085: return '1';
2086: }
2087: }
2088: return;
2089: }
2090:
2091: sub inst_directory_query {
2092: my ($srch) = @_;
2093: my $udom = $srch->{'srchdomain'};
2094: my %results;
2095: my $homeserver = &domain($udom,'primary');
2096: my $outcome;
2097: if ($homeserver ne '') {
2098: unless ($homeserver eq $perlvar{'lonHostID'}) {
2099: if ($srch->{'srchby'} eq 'email') {
2100: my $lcrev = &get_server_loncaparev(undef,$homeserver);
2101: my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
2102: if (($major eq '' && $minor eq '') || ($major < 2) ||
2103: (($major == 2) && ($minor < 12))) {
2104: return;
2105: }
2106: }
2107: }
2108: my $queryid=&reply("querysend:instdirsearch:".
2109: &escape($srch->{'srchby'}).':'.
2110: &escape($srch->{'srchterm'}).':'.
2111: &escape($srch->{'srchtype'}),$homeserver);
2112: my $host=&hostname($homeserver);
2113: if ($queryid !~/^\Q$host\E\_/) {
2114: &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
2115: return;
2116: }
2117: my $response = &get_query_reply($queryid);
2118: my $maxtries = 5;
2119: my $tries = 1;
2120: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2121: $response = &get_query_reply($queryid);
2122: $tries ++;
2123: }
2124:
2125: if (!&error($response) && $response ne 'refused') {
2126: if ($response eq 'unavailable') {
2127: $outcome = $response;
2128: } else {
2129: $outcome = 'ok';
2130: my @matches = split(/\n/,$response);
2131: foreach my $match (@matches) {
2132: my ($key,$value) = split(/=/,$match);
2133: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
2134: }
2135: }
2136: }
2137: }
2138: return ($outcome,%results);
2139: }
2140:
2141: sub usersearch {
2142: my ($srch) = @_;
2143: my $dom = $srch->{'srchdomain'};
2144: my %results;
2145: my %libserv = &all_library();
2146: my $query = 'usersearch';
2147: foreach my $tryserver (keys(%libserv)) {
2148: if (&host_domain($tryserver) eq $dom) {
2149: unless ($tryserver eq $perlvar{'lonHostID'}) {
2150: if ($srch->{'srchby'} eq 'email') {
2151: my $lcrev = &get_server_loncaparev(undef,$tryserver);
2152: my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
2153: next if (($major eq '' && $minor eq '') || ($major < 2) ||
2154: (($major == 2) && ($minor < 12)));
2155: }
2156: }
2157: my $host=&hostname($tryserver);
2158: my $queryid=
2159: &reply("querysend:".&escape($query).':'.
2160: &escape($srch->{'srchby'}).':'.
2161: &escape($srch->{'srchtype'}).':'.
2162: &escape($srch->{'srchterm'}),$tryserver);
2163: if ($queryid !~/^\Q$host\E\_/) {
2164: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
2165: next;
2166: }
2167: my $reply = &get_query_reply($queryid);
2168: my $maxtries = 1;
2169: my $tries = 1;
2170: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2171: $reply = &get_query_reply($queryid);
2172: $tries ++;
2173: }
2174: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2175: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2176: } else {
2177: my @matches;
2178: if ($reply =~ /\n/) {
2179: @matches = split(/\n/,$reply);
2180: } else {
2181: @matches = split(/\&/,$reply);
2182: }
2183: foreach my $match (@matches) {
2184: my ($uname,$udom,%userhash);
2185: foreach my $entry (split(/:/,$match)) {
2186: my ($key,$value) =
2187: map {&unescape($_);} split(/=/,$entry);
2188: $userhash{$key} = $value;
2189: if ($key eq 'username') {
2190: $uname = $value;
2191: } elsif ($key eq 'domain') {
2192: $udom = $value;
2193: }
2194: }
2195: $results{$uname.':'.$udom} = \%userhash;
2196: }
2197: }
2198: }
2199: }
2200: return %results;
2201: }
2202:
2203: sub get_instuser {
2204: my ($udom,$uname,$id) = @_;
2205: my $homeserver = &domain($udom,'primary');
2206: my ($outcome,%results);
2207: if ($homeserver ne '') {
2208: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2209: &escape($id).':'.&escape($udom),$homeserver);
2210: my $host=&hostname($homeserver);
2211: if ($queryid !~/^\Q$host\E\_/) {
2212: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2213: return;
2214: }
2215: my $response = &get_query_reply($queryid);
2216: my $maxtries = 5;
2217: my $tries = 1;
2218: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2219: $response = &get_query_reply($queryid);
2220: $tries ++;
2221: }
2222: if (!&error($response) && $response ne 'refused') {
2223: if ($response eq 'unavailable') {
2224: $outcome = $response;
2225: } else {
2226: $outcome = 'ok';
2227: my @matches = split(/\n/,$response);
2228: foreach my $match (@matches) {
2229: my ($key,$value) = split(/=/,$match);
2230: $results{&unescape($key)} = &thaw_unescape($value);
2231: }
2232: }
2233: }
2234: }
2235: my %userinfo;
2236: if (ref($results{$uname}) eq 'HASH') {
2237: %userinfo = %{$results{$uname}};
2238: }
2239: return ($outcome,%userinfo);
2240: }
2241:
2242: sub get_multiple_instusers {
2243: my ($udom,$users,$caller) = @_;
2244: my ($outcome,$results);
2245: if (ref($users) eq 'HASH') {
2246: my $count = keys(%{$users});
2247: my $requested = &freeze_escape($users);
2248: my $homeserver = &domain($udom,'primary');
2249: if ($homeserver ne '') {
2250: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2251: my $host=&hostname($homeserver);
2252: if ($queryid !~/^\Q$host\E\_/) {
2253: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2254: ' for host: '.$homeserver.'in domain '.$udom);
2255: return ($outcome,$results);
2256: }
2257: my $response = &get_query_reply($queryid);
2258: my $maxtries = 5;
2259: if ($count > 100) {
2260: $maxtries = 1+int($count/20);
2261: }
2262: my $tries = 1;
2263: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2264: $response = &get_query_reply($queryid);
2265: $tries ++;
2266: }
2267: if ($response eq '') {
2268: $results = {};
2269: foreach my $key (keys(%{$users})) {
2270: my ($uname,$id);
2271: if ($caller eq 'id') {
2272: $id = $key;
2273: } else {
2274: $uname = $key;
2275: }
2276: my ($resp,%info) = &get_instuser($udom,$uname,$id);
2277: $outcome = $resp;
2278: if ($resp eq 'ok') {
2279: %{$results} = (%{$results}, %info);
2280: } else {
2281: last;
2282: }
2283: }
2284: } elsif(!&error($response) && ($response ne 'refused')) {
2285: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2286: $outcome = $response;
2287: } else {
2288: ($outcome,my $userdata) = split(/=/,$response,2);
2289: if ($outcome eq 'ok') {
2290: $results = &thaw_unescape($userdata);
2291: }
2292: }
2293: }
2294: }
2295: }
2296: return ($outcome,$results);
2297: }
2298:
2299: sub inst_rulecheck {
2300: my ($udom,$uname,$id,$item,$rules) = @_;
2301: my %returnhash;
2302: if ($udom ne '') {
2303: if (ref($rules) eq 'ARRAY') {
2304: @{$rules} = map {&escape($_);} (@{$rules});
2305: my $rulestr = join(':',@{$rules});
2306: my $homeserver=&domain($udom,'primary');
2307: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
2308: my $response;
2309: if ($item eq 'username') {
2310: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2311: ':'.&escape($uname).':'.$rulestr,
2312: $homeserver));
2313: } elsif ($item eq 'id') {
2314: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2315: ':'.&escape($id).':'.$rulestr,
2316: $homeserver));
2317: } elsif ($item eq 'selfcreate') {
2318: $response=&unescape(&reply('instselfcreatecheck:'.
2319: &escape($udom).':'.&escape($uname).
2320: ':'.$rulestr,$homeserver));
2321: }
2322: if ($response ne 'refused') {
2323: my @pairs=split(/\&/,$response);
2324: foreach my $item (@pairs) {
2325: my ($key,$value)=split(/=/,$item,2);
2326: $key = &unescape($key);
2327: next if ($key =~ /^error: 2 /);
2328: $returnhash{$key}=&thaw_unescape($value);
2329: }
2330: }
2331: }
2332: }
2333: }
2334: return %returnhash;
2335: }
2336:
2337: sub inst_userrules {
2338: my ($udom,$check) = @_;
2339: my (%ruleshash,@ruleorder);
2340: if ($udom ne '') {
2341: my $homeserver=&domain($udom,'primary');
2342: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
2343: my $response;
2344: if ($check eq 'id') {
2345: $response=&reply('instidrules:'.&escape($udom),
2346: $homeserver);
2347: } elsif ($check eq 'email') {
2348: $response=&reply('instemailrules:'.&escape($udom),
2349: $homeserver);
2350: } else {
2351: $response=&reply('instuserrules:'.&escape($udom),
2352: $homeserver);
2353: }
2354: if (($response ne 'refused') && ($response ne 'error') &&
2355: ($response ne 'unknown_cmd') &&
2356: ($response ne 'no_such_host')) {
2357: my ($hashitems,$orderitems) = split(/:/,$response);
2358: my @pairs=split(/\&/,$hashitems);
2359: foreach my $item (@pairs) {
2360: my ($key,$value)=split(/=/,$item,2);
2361: $key = &unescape($key);
2362: next if ($key =~ /^error: 2 /);
2363: $ruleshash{$key}=&thaw_unescape($value);
2364: }
2365: my @esc_order = split(/\&/,$orderitems);
2366: foreach my $item (@esc_order) {
2367: push(@ruleorder,&unescape($item));
2368: }
2369: }
2370: }
2371: }
2372: return (\%ruleshash,\@ruleorder);
2373: }
2374:
2375: # ------------- Get Authentication, Language and User Tools Defaults for Domain
2376:
2377: sub get_domain_defaults {
2378: my ($domain,$ignore_cache) = @_;
2379: return if (($domain eq '') || ($domain eq 'public'));
2380: my $cachetime = 60*60*24;
2381: unless ($ignore_cache) {
2382: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2383: if (defined($cached)) {
2384: if (ref($result) eq 'HASH') {
2385: return %{$result};
2386: }
2387: }
2388: }
2389: my %domdefaults;
2390: my %domconfig =
2391: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
2392: 'requestcourses','inststatus',
2393: 'coursedefaults','usersessions',
2394: 'requestauthor','selfenrollment',
2395: 'coursecategories','ssl','autoenroll',
2396: 'trust','helpsettings'],$domain);
2397: my @coursetypes = ('official','unofficial','community','textbook','placement');
2398: if (ref($domconfig{'defaults'}) eq 'HASH') {
2399: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2400: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2401: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
2402: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
2403: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
2404: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
2405: $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
2406: $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
2407: $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
2408: } else {
2409: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2410: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2411: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2412: }
2413: if (ref($domconfig{'quotas'}) eq 'HASH') {
2414: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2415: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2416: } else {
2417: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
2418: }
2419: my @usertools = ('aboutme','blog','webdav','portfolio');
2420: foreach my $item (@usertools) {
2421: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2422: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2423: }
2424: }
2425: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2426: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2427: }
2428: }
2429: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
2430: foreach my $item ('official','unofficial','community','textbook','placement') {
2431: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2432: }
2433: }
2434: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2435: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2436: }
2437: if (ref($domconfig{'inststatus'}) eq 'HASH') {
2438: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
2439: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2440: }
2441: }
2442: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
2443: $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
2444: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
2445: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2446: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2447: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2448: }
2449: foreach my $type (@coursetypes) {
2450: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2451: unless ($type eq 'community') {
2452: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2453: }
2454: }
2455: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2456: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2457: }
2458: if ($domdefaults{'postsubmit'} eq 'on') {
2459: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2460: $domdefaults{$type.'postsubtimeout'} =
2461: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2462: }
2463: }
2464: }
2465: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2466: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2467: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2468: if (@clonecodes) {
2469: $domdefaults{'canclone'} = join('+',@clonecodes);
2470: }
2471: }
2472: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2473: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2474: }
2475: if ($domconfig{'coursedefaults'}{'texengine'}) {
2476: $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
2477: }
2478: }
2479: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2480: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2481: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2482: }
2483: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2484: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2485: }
2486: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2487: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2488: }
2489: }
2490: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2491: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2492: my @settings = ('types','registered','enroll_dates','access_dates','section',
2493: 'approval','limit');
2494: foreach my $type (@coursetypes) {
2495: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2496: my @mgrdc = ();
2497: foreach my $item (@settings) {
2498: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2499: push(@mgrdc,$item);
2500: }
2501: }
2502: if (@mgrdc) {
2503: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2504: }
2505: }
2506: }
2507: }
2508: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2509: foreach my $type (@coursetypes) {
2510: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2511: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2512: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2513: }
2514: }
2515: }
2516: }
2517: }
2518: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2519: $domdefaults{'catauth'} = 'std';
2520: $domdefaults{'catunauth'} = 'std';
2521: if ($domconfig{'coursecategories'}{'auth'}) {
2522: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2523: }
2524: if ($domconfig{'coursecategories'}{'unauth'}) {
2525: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2526: }
2527: }
2528: if (ref($domconfig{'ssl'}) eq 'HASH') {
2529: if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
2530: $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
2531: }
2532: if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
2533: $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
2534: }
2535: if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
2536: $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
2537: }
2538: }
2539: if (ref($domconfig{'trust'}) eq 'HASH') {
2540: my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
2541: foreach my $prefix (@prefixes) {
2542: if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
2543: $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
2544: }
2545: }
2546: }
2547: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2548: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
2549: }
2550: if (ref($domconfig{'helpsettings'}) eq 'HASH') {
2551: $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
2552: if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
2553: $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
2554: }
2555: }
2556: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
2557: return %domdefaults;
2558: }
2559:
2560: sub course_portal_url {
2561: my ($cnum,$cdom) = @_;
2562: my $chome = &homeserver($cnum,$cdom);
2563: my $hostname = &hostname($chome);
2564: my $protocol = $protocol{$chome};
2565: $protocol = 'http' if ($protocol ne 'https');
2566: my %domdefaults = &get_domain_defaults($cdom);
2567: my $firsturl;
2568: if ($domdefaults{'portal_def'}) {
2569: $firsturl = $domdefaults{'portal_def'};
2570: } else {
2571: $firsturl = $protocol.'://'.$hostname;
2572: }
2573: return $firsturl;
2574: }
2575:
2576: # --------------------------------------------------- Assign a key to a student
2577:
2578: sub assign_access_key {
2579: #
2580: # a valid key looks like uname:udom#comments
2581: # comments are being appended
2582: #
2583: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2584: $kdom=
2585: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
2586: $knum=
2587: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
2588: $cdom=
2589: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
2590: $cnum=
2591: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2592: $udom=$env{'user.name'} unless (defined($udom));
2593: $uname=$env{'user.domain'} unless (defined($uname));
2594: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
2595: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
2596: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
2597: # assigned to this person
2598: # - this should not happen,
2599: # unless something went wrong
2600: # the first time around
2601: # ready to assign
2602: $logentry=$1.'; '.$logentry;
2603: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
2604: $kdom,$knum) eq 'ok') {
2605: # key now belongs to user
2606: my $envkey='key.'.$cdom.'_'.$cnum;
2607: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
2608: &appenv({'environment.'.$envkey => $ckey});
2609: return 'ok';
2610: } else {
2611: return
2612: 'error: Count not permanently assign key, will need to be re-entered later.';
2613: }
2614: } else {
2615: return 'error: Could not assign key, try again later.';
2616: }
2617: } elsif (!$existing{$ckey}) {
2618: # the key does not exist
2619: return 'error: The key does not exist';
2620: } else {
2621: # the key is somebody else's
2622: return 'error: The key is already in use';
2623: }
2624: }
2625:
2626: # ------------------------------------------ put an additional comment on a key
2627:
2628: sub comment_access_key {
2629: #
2630: # a valid key looks like uname:udom#comments
2631: # comments are being appended
2632: #
2633: my ($ckey,$cdom,$cnum,$logentry)=@_;
2634: $cdom=
2635: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
2636: $cnum=
2637: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2638: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2639: if ($existing{$ckey}) {
2640: $existing{$ckey}.='; '.$logentry;
2641: # ready to assign
2642: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
2643: $cdom,$cnum) eq 'ok') {
2644: return 'ok';
2645: } else {
2646: return 'error: Count not store comment.';
2647: }
2648: } else {
2649: # the key does not exist
2650: return 'error: The key does not exist';
2651: }
2652: }
2653:
2654: # ------------------------------------------------------ Generate a set of keys
2655:
2656: sub generate_access_keys {
2657: my ($number,$cdom,$cnum,$logentry)=@_;
2658: $cdom=
2659: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
2660: $cnum=
2661: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2662: unless (&allowed('mky',$cdom)) { return 0; }
2663: unless (($cdom) && ($cnum)) { return 0; }
2664: if ($number>10000) { return 0; }
2665: sleep(2); # make sure don't get same seed twice
2666: srand(time()^($$+($$<<15))); # from "Programming Perl"
2667: my $total=0;
2668: for (my $i=1;$i<=$number;$i++) {
2669: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2670: sprintf("%lx",int(100000*rand)).'-'.
2671: sprintf("%lx",int(100000*rand));
2672: $newkey=~s/1/g/g; # folks mix up 1 and l
2673: $newkey=~s/0/h/g; # and also 0 and O
2674: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2675: if ($existing{$newkey}) {
2676: $i--;
2677: } else {
2678: if (&put('accesskeys',
2679: { $newkey => '# generated '.localtime().
2680: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
2681: '; '.$logentry },
2682: $cdom,$cnum) eq 'ok') {
2683: $total++;
2684: }
2685: }
2686: }
2687: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
2688: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2689: return $total;
2690: }
2691:
2692: # ------------------------------------------------------- Validate an accesskey
2693:
2694: sub validate_access_key {
2695: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2696: $cdom=
2697: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
2698: $cnum=
2699: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2700: $udom=$env{'user.domain'} unless (defined($udom));
2701: $uname=$env{'user.name'} unless (defined($uname));
2702: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2703: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
2704: }
2705:
2706: # ------------------------------------- Find the section of student in a course
2707: sub devalidate_getsection_cache {
2708: my ($udom,$unam,$courseid)=@_;
2709: my $hashid="$udom:$unam:$courseid";
2710: &devalidate_cache_new('getsection',$hashid);
2711: }
2712:
2713: sub courseid_to_courseurl {
2714: my ($courseid) = @_;
2715: #already url style courseid
2716: return $courseid if ($courseid =~ m{^/});
2717:
2718: if (exists($env{'course.'.$courseid.'.num'})) {
2719: my $cnum = $env{'course.'.$courseid.'.num'};
2720: my $cdom = $env{'course.'.$courseid.'.domain'};
2721: return "/$cdom/$cnum";
2722: }
2723:
2724: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
2725: if (exists($courseinfo{'num'})) {
2726: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
2727: }
2728:
2729: return undef;
2730: }
2731:
2732: sub getsection {
2733: my ($udom,$unam,$courseid)=@_;
2734: my $cachetime=1800;
2735:
2736: my $hashid="$udom:$unam:$courseid";
2737: my ($result,$cached)=&is_cached_new('getsection',$hashid);
2738: if (defined($cached)) { return $result; }
2739:
2740: my %Pending;
2741: my %Expired;
2742: #
2743: # Each role can either have not started yet (pending), be active,
2744: # or have expired.
2745: #
2746: # If there is an active role, we are done.
2747: #
2748: # If there is more than one role which has not started yet,
2749: # choose the one which will start sooner
2750: # If there is one role which has not started yet, return it.
2751: #
2752: # If there is more than one expired role, choose the one which ended last.
2753: # If there is a role which has expired, return it.
2754: #
2755: $courseid = &courseid_to_courseurl($courseid);
2756: my %roleshash = &dump('roles',$udom,$unam,$courseid);
2757: foreach my $key (keys(%roleshash)) {
2758: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
2759: my $section=$1;
2760: if ($key eq $courseid.'_st') { $section=''; }
2761: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
2762: my $now=time;
2763: if (defined($end) && $end && ($now > $end)) {
2764: $Expired{$end}=$section;
2765: next;
2766: }
2767: if (defined($start) && $start && ($now < $start)) {
2768: $Pending{$start}=$section;
2769: next;
2770: }
2771: return &do_cache_new('getsection',$hashid,$section,$cachetime);
2772: }
2773: #
2774: # Presumedly there will be few matching roles from the above
2775: # loop and the sorting time will be negligible.
2776: if (scalar(keys(%Pending))) {
2777: my ($time) = sort {$a <=> $b} keys(%Pending);
2778: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
2779: }
2780: if (scalar(keys(%Expired))) {
2781: my @sorted = sort {$a <=> $b} keys(%Expired);
2782: my $time = pop(@sorted);
2783: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
2784: }
2785: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
2786: }
2787:
2788: sub save_cache {
2789: &purge_remembered();
2790: #&Apache::loncommon::validate_page();
2791: undef(%env);
2792: undef($env_loaded);
2793: }
2794:
2795: my $to_remember=-1;
2796: my %remembered;
2797: my %accessed;
2798: my $kicks=0;
2799: my $hits=0;
2800: sub make_key {
2801: my ($name,$id) = @_;
2802: if (length($id) > 65
2803: && length(&escape($id)) > 200) {
2804: $id=length($id).':'.&Digest::MD5::md5_hex($id);
2805: }
2806: return &escape($name.':'.$id);
2807: }
2808:
2809: sub devalidate_cache_new {
2810: my ($name,$id,$debug) = @_;
2811: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
2812: my $remembered_id=$name.':'.$id;
2813: $id=&make_key($name,$id);
2814: $memcache->delete($id);
2815: delete($remembered{$remembered_id});
2816: delete($accessed{$remembered_id});
2817: }
2818:
2819: sub is_cached_new {
2820: my ($name,$id,$debug) = @_;
2821: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
2822: if (exists($remembered{$remembered_id})) {
2823: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
2824: $accessed{$remembered_id}=[&gettimeofday()];
2825: $hits++;
2826: return ($remembered{$remembered_id},1);
2827: }
2828: $id=&make_key($name,$id);
2829: my $value = $memcache->get($id);
2830: if (!(defined($value))) {
2831: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
2832: return (undef,undef);
2833: }
2834: if ($value eq '__undef__') {
2835: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
2836: $value=undef;
2837: }
2838: &make_room($remembered_id,$value,$debug);
2839: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
2840: return ($value,1);
2841: }
2842:
2843: sub do_cache_new {
2844: my ($name,$id,$value,$time,$debug) = @_;
2845: my $remembered_id=$name.':'.$id;
2846: $id=&make_key($name,$id);
2847: my $setvalue=$value;
2848: if (!defined($setvalue)) {
2849: $setvalue='__undef__';
2850: }
2851: if (!defined($time) ) {
2852: $time=600;
2853: }
2854: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
2855: my $result = $memcache->set($id,$setvalue,$time);
2856: if (! $result) {
2857: &logthis("caching of id -> $id failed");
2858: $memcache->disconnect_all();
2859: }
2860: # need to make a copy of $value
2861: &make_room($remembered_id,$value,$debug);
2862: return $value;
2863: }
2864:
2865: sub make_room {
2866: my ($remembered_id,$value,$debug)=@_;
2867:
2868: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
2869: : $value;
2870: if ($to_remember<0) { return; }
2871: $accessed{$remembered_id}=[&gettimeofday()];
2872: if (scalar(keys(%remembered)) <= $to_remember) { return; }
2873: my $to_kick;
2874: my $max_time=0;
2875: foreach my $other (keys(%accessed)) {
2876: if (&tv_interval($accessed{$other}) > $max_time) {
2877: $to_kick=$other;
2878: $max_time=&tv_interval($accessed{$other});
2879: }
2880: }
2881: delete($remembered{$to_kick});
2882: delete($accessed{$to_kick});
2883: $kicks++;
2884: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
2885: return;
2886: }
2887:
2888: sub purge_remembered {
2889: #&logthis("Tossing ".scalar(keys(%remembered)));
2890: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
2891: undef(%remembered);
2892: undef(%accessed);
2893: }
2894: # ------------------------------------- Read an entry from a user's environment
2895:
2896: sub userenvironment {
2897: my ($udom,$unam,@what)=@_;
2898: my $items;
2899: foreach my $item (@what) {
2900: $items.=&escape($item).'&';
2901: }
2902: $items=~s/\&$//;
2903: my %returnhash=();
2904: my $uhome = &homeserver($unam,$udom);
2905: unless ($uhome eq 'no_host') {
2906: my @answer=split(/\&/,
2907: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
2908: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
2909: return %returnhash;
2910: }
2911: my $i;
2912: for ($i=0;$i<=$#what;$i++) {
2913: $returnhash{$what[$i]}=&unescape($answer[$i]);
2914: }
2915: }
2916: return %returnhash;
2917: }
2918:
2919: # ---------------------------------------------------------- Get a studentphoto
2920: sub studentphoto {
2921: my ($udom,$unam,$ext) = @_;
2922: my $home=&Apache::lonnet::homeserver($unam,$udom);
2923: if (defined($env{'request.course.id'})) {
2924: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2925: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
2926: return(&retrievestudentphoto($udom,$unam,$ext));
2927: } else {
2928: my ($result,$perm_reqd)=
2929: &Apache::lonnet::auto_photo_permission($unam,$udom);
2930: if ($result eq 'ok') {
2931: if (!($perm_reqd eq 'yes')) {
2932: return(&retrievestudentphoto($udom,$unam,$ext));
2933: }
2934: }
2935: }
2936: }
2937: } else {
2938: my ($result,$perm_reqd) =
2939: &Apache::lonnet::auto_photo_permission($unam,$udom);
2940: if ($result eq 'ok') {
2941: if (!($perm_reqd eq 'yes')) {
2942: return(&retrievestudentphoto($udom,$unam,$ext));
2943: }
2944: }
2945: }
2946: return '/adm/lonKaputt/lonlogo_broken.gif';
2947: }
2948:
2949: sub retrievestudentphoto {
2950: my ($udom,$unam,$ext,$type) = @_;
2951: my $home=&Apache::lonnet::homeserver($unam,$udom);
2952: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2953: if ($ret eq 'ok') {
2954: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2955: if ($type eq 'thumbnail') {
2956: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2957: }
2958: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2959: return $tokenurl;
2960: } else {
2961: if ($type eq 'thumbnail') {
2962: return '/adm/lonKaputt/genericstudent_tn.gif';
2963: } else {
2964: return '/adm/lonKaputt/lonlogo_broken.gif';
2965: }
2966: }
2967: }
2968:
2969: # -------------------------------------------------------------------- New chat
2970:
2971: sub chatsend {
2972: my ($newentry,$anon,$group)=@_;
2973: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2974: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2975: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
2976: &reply('chatsend:'.$cdom.':'.$cnum.':'.
2977: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
2978: &escape($newentry)).':'.$group,$chome);
2979: }
2980:
2981: # ------------------------------------------ Find current version of a resource
2982:
2983: sub getversion {
2984: my $fname=&clutter(shift);
2985: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
2986: return ¤tversion(&filelocation('',$fname));
2987: }
2988:
2989: sub currentversion {
2990: my $fname=shift;
2991: my $author=$fname;
2992: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2993: my ($udom,$uname)=split(/\//,$author);
2994: my $home=&homeserver($uname,$udom);
2995: if ($home eq 'no_host') {
2996: return -1;
2997: }
2998: my $answer=&reply("currentversion:$fname",$home);
2999: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3000: return -1;
3001: }
3002: return $answer;
3003: }
3004:
3005: #
3006: # Return special version number of resource if set by override, empty otherwise
3007: #
3008: sub usedversion {
3009: my $fname=shift;
3010: unless ($fname) { $fname=$env{'request.uri'}; }
3011: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
3012: if ($urlversion) { return $urlversion; }
3013: return '';
3014: }
3015:
3016: # ----------------------------- Subscribe to a resource, return URL if possible
3017:
3018: sub subscribe {
3019: my $fname=shift;
3020: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
3021: $fname=~s/[\n\r]//g;
3022: my $author=$fname;
3023: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3024: my ($udom,$uname)=split(/\//,$author);
3025: my $home=homeserver($uname,$udom);
3026: if ($home eq 'no_host') {
3027: return 'not_found';
3028: }
3029: my $answer=reply("sub:$fname",$home);
3030: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3031: $answer.=' by '.$home;
3032: }
3033: return $answer;
3034: }
3035:
3036: # -------------------------------------------------------------- Replicate file
3037:
3038: sub repcopy {
3039: my $filename=shift;
3040: $filename=~s/\/+/\//g;
3041: my $londocroot = $perlvar{'lonDocRoot'};
3042: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
3043: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
3044: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
3045: $filename=~m{^/*(uploaded|editupload)/}) {
3046: return &repcopy_userfile($filename);
3047: }
3048: $filename=~s/[\n\r]//g;
3049: my $transname="$filename.in.transfer";
3050: # FIXME: this should flock
3051: if ((-e $filename) || (-e $transname)) { return 'ok'; }
3052: my $remoteurl=subscribe($filename);
3053: if ($remoteurl =~ /^con_lost by/) {
3054: &logthis("Subscribe returned $remoteurl: $filename");
3055: return 'unavailable';
3056: } elsif ($remoteurl eq 'not_found') {
3057: #&logthis("Subscribe returned not_found: $filename");
3058: return 'not_found';
3059: } elsif ($remoteurl =~ /^rejected by/) {
3060: &logthis("Subscribe returned $remoteurl: $filename");
3061: return 'forbidden';
3062: } elsif ($remoteurl eq 'directory') {
3063: return 'ok';
3064: } else {
3065: my $author=$filename;
3066: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3067: my ($udom,$uname)=split(/\//,$author);
3068: my $home=homeserver($uname,$udom);
3069: unless ($home eq $perlvar{'lonHostID'}) {
3070: my @parts=split(/\//,$filename);
3071: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
3072: if ($path ne "$londocroot/res") {
3073: &logthis("Malconfiguration for replication: $filename");
3074: return 'bad_request';
3075: }
3076: my $count;
3077: for ($count=5;$count<$#parts;$count++) {
3078: $path.="/$parts[$count]";
3079: if ((-e $path)!=1) {
3080: mkdir($path,0777);
3081: }
3082: }
3083: my $request=new HTTP::Request('GET',"$remoteurl");
3084: my $response;
3085: if ($remoteurl =~ m{/raw/}) {
3086: $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
3087: } else {
3088: $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
3089: }
3090: if ($response->is_error()) {
3091: unlink($transname);
3092: my $message=$response->status_line;
3093: &logthis("<font color=\"blue\">WARNING:"
3094: ." LWP get: $message: $filename</font>");
3095: return 'unavailable';
3096: } else {
3097: if ($remoteurl!~/\.meta$/) {
3098: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
3099: my $mresponse;
3100: if ($remoteurl =~ m{/raw/}) {
3101: $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
3102: } else {
3103: $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
3104: }
3105: if ($mresponse->is_error()) {
3106: unlink($filename.'.meta');
3107: &logthis(
3108: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
3109: }
3110: }
3111: rename($transname,$filename);
3112: return 'ok';
3113: }
3114: }
3115: }
3116: }
3117:
3118: # ------------------------------------------------ Get server side include body
3119: sub ssi_body {
3120: my ($filelink,%form)=@_;
3121: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
3122: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
3123: }
3124: my $output='';
3125: my $response;
3126: if ($filelink=~/^https?\:/) {
3127: ($output,$response)=&externalssi($filelink);
3128: } else {
3129: $filelink .= $filelink=~/\?/ ? '&' : '?';
3130: $filelink .= 'inhibitmenu=yes';
3131: ($output,$response)=&ssi($filelink,%form);
3132: }
3133: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
3134: $output=~s/^.*?\<body[^\>]*\>//si;
3135: $output=~s/\<\/body\s*\>.*?$//si;
3136: if (wantarray) {
3137: return ($output, $response);
3138: } else {
3139: return $output;
3140: }
3141: }
3142:
3143: # --------------------------------------------------------- Server Side Include
3144:
3145: sub absolute_url {
3146: my ($host_name) = @_;
3147: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
3148: if ($host_name eq '') {
3149: $host_name = $ENV{'SERVER_NAME'};
3150: }
3151: return $protocol.$host_name;
3152: }
3153:
3154: #
3155: # Server side include.
3156: # Parameters:
3157: # fn Possibly encrypted resource name/id.
3158: # form Hash that describes how the rendering should be done
3159: # and other things.
3160: # Returns:
3161: # Scalar context: The content of the response.
3162: # Array context: 2 element list of the content and the full response object.
3163: #
3164: sub ssi {
3165:
3166: my ($fn,%form)=@_;
3167: my $request;
3168:
3169: $form{'no_update_last_known'}=1;
3170: &Apache::lonenc::check_encrypt(\$fn);
3171: if (%form) {
3172: $request=new HTTP::Request('POST',&absolute_url().$fn);
3173: $request->content(join('&',map {
3174: my $name = escape($_);
3175: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3176: ? join("&$name=", map {escape($_) } @{$form{$_}})
3177: : &escape($form{$_}) );
3178: } keys(%form)));
3179: } else {
3180: $request=new HTTP::Request('GET',&absolute_url().$fn);
3181: }
3182:
3183: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
3184: my $lonhost = $perlvar{'lonHostID'};
3185: my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar);
3186:
3187: if (wantarray) {
3188: return ($response->content, $response);
3189: } else {
3190: return $response->content;
3191: }
3192: }
3193:
3194: sub externalssi {
3195: my ($url)=@_;
3196: my $request=new HTTP::Request('GET',$url);
3197: my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
3198: if (wantarray) {
3199: return ($response->content, $response);
3200: } else {
3201: return $response->content;
3202: }
3203: }
3204:
3205:
3206: # If the local copy of a replicated resource is outdated, trigger a
3207: # connection from the homeserver to flush the delayed queue. If no update
3208: # happens, remove local copies of outdated resource (and corresponding
3209: # metadata file).
3210:
3211: sub remove_stale_resfile {
3212: my ($url) = @_;
3213: my $removed;
3214: if ($url=~m{^/res/($match_domain)/($match_username)/}) {
3215: my $audom = $1;
3216: my $auname = $2;
3217: unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
3218: my $homeserver = &homeserver($auname,$audom);
3219: unless (($homeserver eq 'no_host') ||
3220: (grep { $_ eq $homeserver } ¤t_machine_ids())) {
3221: my $fname = &filelocation('',$url);
3222: if (-e $fname) {
3223: my $protocol = $protocol{$homeserver};
3224: $protocol = 'http' if ($protocol ne 'https');
3225: my $hostname = &hostname($homeserver);
3226: if ($hostname) {
3227: my $uri = &declutter($url);
3228: my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
3229: my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
3230: if ($response->is_success()) {
3231: my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
3232: my $locmodtime = (stat($fname))[9];
3233: if ($locmodtime < $remmodtime) {
3234: my $stale;
3235: my $answer = &reply('pong',$homeserver);
3236: if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
3237: sleep(0.2);
3238: $locmodtime = (stat($fname))[9];
3239: if ($locmodtime < $remmodtime) {
3240: my $posstransfer = $fname.'.in.transfer';
3241: if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
3242: $removed = 1;
3243: } else {
3244: $stale = 1;
3245: }
3246: } else {
3247: $removed = 1;
3248: }
3249: } else {
3250: $stale = 1;
3251: }
3252: if ($stale) {
3253: unlink($fname);
3254: if ($uri!~/\.meta$/) {
3255: unlink($fname.'.meta');
3256: }
3257: &reply("unsub:$fname",$homeserver);
3258: $removed = 1;
3259: }
3260: }
3261: }
3262: }
3263: }
3264: }
3265: }
3266: }
3267: return $removed;
3268: }
3269:
3270: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3271:
3272: sub allowuploaded {
3273: my ($srcurl,$url)=@_;
3274: $url=&clutter(&declutter($url));
3275: my $dir=$url;
3276: $dir=~s/\/[^\/]+$//;
3277: my %httpref=();
3278: my $httpurl=&hreflocation('',$url);
3279: $httpref{'httpref.'.$httpurl}=$srcurl;
3280: &Apache::lonnet::appenv(\%httpref);
3281: }
3282:
3283: #
3284: # Determine if the current user should be able to edit a particular resource,
3285: # when viewing in course context.
3286: # (a) When viewing resource used to determine if "Edit" item is included in
3287: # Functions.
3288: # (b) When displaying folder contents in course editor, used to determine if
3289: # "Edit" link will be displayed alongside resource.
3290: #
3291: # input: six args -- filename (decluttered), course number, course domain,
3292: # url, symb (if registered) and group (if this is a group
3293: # item -- e.g., bulletin board, group page etc.).
3294: # output: array of five scalars --
3295: # $cfile -- url for file editing if editable on current server
3296: # $home -- homeserver of resource (i.e., for author if published,
3297: # or course if uploaded.).
3298: # $switchserver -- 1 if server switch will be needed.
3299: # $forceedit -- 1 if icon/link should be to go to edit mode
3300: # $forceview -- 1 if icon/link should be to go to view mode
3301: #
3302:
3303: sub can_edit_resource {
3304: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3305: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3306: #
3307: # For aboutme pages user can only edit his/her own.
3308: #
3309: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
3310: my ($sdom,$sname) = ($1,$2);
3311: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3312: $home = $env{'user.home'};
3313: $cfile = $resurl;
3314: if ($env{'form.forceedit'}) {
3315: $forceview = 1;
3316: } else {
3317: $forceedit = 1;
3318: }
3319: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3320: } else {
3321: return;
3322: }
3323: }
3324:
3325: if ($env{'request.course.id'}) {
3326: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3327: if ($group ne '') {
3328: # if this is a group homepage or group bulletin board, check group privs
3329: my $allowed = 0;
3330: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3331: if ((&allowed('mdg',$env{'request.course.id'}.
3332: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3333: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3334: $allowed = 1;
3335: }
3336: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3337: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3338: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3339: $allowed = 1;
3340: }
3341: }
3342: if ($allowed) {
3343: $home=&homeserver($cnum,$cdom);
3344: if ($env{'form.forceedit'}) {
3345: $forceview = 1;
3346: } else {
3347: $forceedit = 1;
3348: }
3349: $cfile = $resurl;
3350: } else {
3351: return;
3352: }
3353: } else {
3354: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3355: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3356: return;
3357: }
3358: } elsif (!$crsedit) {
3359: #
3360: # No edit allowed where CC has switched to student role.
3361: #
3362: return;
3363: }
3364: }
3365: }
3366:
3367: if ($file ne '') {
3368: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
3369: if (&is_course_upload($file,$cnum,$cdom)) {
3370: $uploaded = 1;
3371: $incourse = 1;
3372: if ($file =~/\.(htm|html|css|js|txt)$/) {
3373: $cfile = &hreflocation('',$file);
3374: if ($env{'form.forceedit'}) {
3375: $forceview = 1;
3376: } else {
3377: $forceedit = 1;
3378: }
3379: }
3380: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3381: $incourse = 1;
3382: if ($env{'form.forceedit'}) {
3383: $forceview = 1;
3384: } else {
3385: $forceedit = 1;
3386: }
3387: $cfile = $resurl;
3388: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3389: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3390: $incourse = 1;
3391: if ($env{'form.forceedit'}) {
3392: $forceview = 1;
3393: } else {
3394: $forceedit = 1;
3395: }
3396: $cfile = $resurl;
3397: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
3398: $incourse = 1;
3399: $cfile = $resurl.'/smpedit';
3400: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
3401: $incourse = 1;
3402: if ($env{'form.forceedit'}) {
3403: $forceview = 1;
3404: } else {
3405: $forceedit = 1;
3406: }
3407: $cfile = $resurl;
3408: } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3409: $incourse = 1;
3410: if ($env{'form.forceedit'}) {
3411: $forceview = 1;
3412: } else {
3413: $forceedit = 1;
3414: }
3415: $cfile = $resurl;
3416: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3417: $incourse = 1;
3418: if ($env{'form.forceedit'}) {
3419: $forceview = 1;
3420: } else {
3421: $forceedit = 1;
3422: }
3423: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
3424: }
3425: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3426: my $template = '/res/lib/templates/simpleproblem.problem';
3427: if (&is_on_map($template)) {
3428: $incourse = 1;
3429: $forceview = 1;
3430: $cfile = $template;
3431: }
3432: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3433: $incourse = 1;
3434: if ($env{'form.forceedit'}) {
3435: $forceview = 1;
3436: } else {
3437: $forceedit = 1;
3438: }
3439: $cfile = $resurl;
3440: } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3441: $incourse = 1;
3442: if ($env{'form.forceedit'}) {
3443: $forceview = 1;
3444: } else {
3445: $forceedit = 1;
3446: }
3447: $cfile = $resurl;
3448: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3449: $incourse = 1;
3450: $forceview = 1;
3451: if ($symb) {
3452: my ($map,$id,$res)=&decode_symb($symb);
3453: $env{'request.symb'} = $symb;
3454: $cfile = &clutter($res);
3455: } else {
3456: $cfile = $env{'form.suppurl'};
3457: my $escfile = &unescape($cfile);
3458: if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3459: $cfile = '/adm/wrapper'.$escfile;
3460: } else {
3461: $escfile =~ s{^http://}{};
3462: $cfile = &escape("/adm/wrapper/ext/$escfile");
3463: }
3464: }
3465: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3466: if ($env{'form.forceedit'}) {
3467: $forceview = 1;
3468: } else {
3469: $forceedit = 1;
3470: }
3471: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
3472: }
3473: }
3474: if ($uploaded || $incourse) {
3475: $home=&homeserver($cnum,$cdom);
3476: } elsif ($file !~ m{/$}) {
3477: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3478: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3479: # Check that the user has permission to edit this resource
3480: my $setpriv = 1;
3481: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3482: if (defined($cfudom)) {
3483: $home=&homeserver($cfuname,$cfudom);
3484: $cfile=$file;
3485: }
3486: }
3487: if (($cfile ne '') && (!$incourse || $uploaded) &&
3488: (($home ne '') && ($home ne 'no_host'))) {
3489: my @ids=¤t_machine_ids();
3490: unless (grep(/^\Q$home\E$/,@ids)) {
3491: $switchserver=1;
3492: }
3493: }
3494: }
3495: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3496: }
3497:
3498: sub is_course_upload {
3499: my ($file,$cnum,$cdom) = @_;
3500: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3501: $uploadpath =~ s{^\/}{};
3502: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3503: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
3504: return 1;
3505: }
3506: return;
3507: }
3508:
3509: sub in_course {
3510: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3511: if ($hideprivileged) {
3512: my $skipuser;
3513: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3514: my @possdoms = ($cdom);
3515: if ($coursehash{'checkforpriv'}) {
3516: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3517: }
3518: if (&privileged($uname,$udom,\@possdoms)) {
3519: $skipuser = 1;
3520: if ($coursehash{'nothideprivileged'}) {
3521: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3522: my $user;
3523: if ($item =~ /:/) {
3524: $user = $item;
3525: } else {
3526: $user = join(':',split(/[\@]/,$item));
3527: }
3528: if ($user eq $uname.':'.$udom) {
3529: undef($skipuser);
3530: last;
3531: }
3532: }
3533: }
3534: if ($skipuser) {
3535: return 0;
3536: }
3537: }
3538: }
3539: $type ||= 'any';
3540: if (!defined($cdom) || !defined($cnum)) {
3541: my $cid = $env{'request.course.id'};
3542: $cdom = $env{'course.'.$cid.'.domain'};
3543: $cnum = $env{'course.'.$cid.'.num'};
3544: }
3545: my $typesref;
3546: if (($type eq 'any') || ($type eq 'all')) {
3547: $typesref = ['active','previous','future'];
3548: } elsif ($type eq 'previous' || $type eq 'future') {
3549: $typesref = [$type];
3550: }
3551: my %roles = &get_my_roles($uname,$udom,'userroles',
3552: $typesref,undef,[$cdom]);
3553: my ($tmp) = keys(%roles);
3554: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3555: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3556: if (@course_roles > 0) {
3557: return 1;
3558: }
3559: return 0;
3560: }
3561:
3562: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
3563: # input: action, courseID, current domain, intended
3564: # path to file, source of file, instruction to parse file for objects,
3565: # ref to hash for embedded objects,
3566: # ref to hash for codebase of java objects.
3567: # reference to scalar to accommodate mime type determined
3568: # from File::MMagic if $parser = parse.
3569: #
3570: # output: url to file (if action was uploaddoc),
3571: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
3572: #
3573: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3574: # course.
3575: #
3576: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3577: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3578: # course's home server.
3579: #
3580: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3581: # be copied from $source (current location) to
3582: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3583: # and will then be copied to
3584: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3585: # course's home server.
3586: #
3587: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3588: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
3589: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3590: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3591: # in course's home server.
3592: #
3593:
3594: sub process_coursefile {
3595: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3596: $mimetype)=@_;
3597: my $fetchresult;
3598: my $home=&homeserver($docuname,$docudom);
3599: if ($action eq 'propagate') {
3600: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3601: $home);
3602: } else {
3603: my $fpath = '';
3604: my $fname = $file;
3605: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
3606: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
3607: my $filepath = &build_filepath($fpath);
3608: if ($action eq 'copy') {
3609: if ($source eq '') {
3610: $fetchresult = 'no source file';
3611: return $fetchresult;
3612: } else {
3613: my $destination = $filepath.'/'.$fname;
3614: rename($source,$destination);
3615: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3616: $home);
3617: }
3618: } elsif ($action eq 'uploaddoc') {
3619: open(my $fh,'>',$filepath.'/'.$fname);
3620: print $fh $env{'form.'.$source};
3621: close($fh);
3622: if ($parser eq 'parse') {
3623: my $mm = new File::MMagic;
3624: my $type = $mm->checktype_filename($filepath.'/'.$fname);
3625: if ($type eq 'text/html') {
3626: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
3627: unless ($parse_result eq 'ok') {
3628: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
3629: }
3630: }
3631: if (ref($mimetype)) {
3632: $$mimetype = $type;
3633: }
3634: }
3635: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3636: $home);
3637: if ($fetchresult eq 'ok') {
3638: return '/uploaded/'.$fpath.'/'.$fname;
3639: } else {
3640: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
3641: ' to host '.$home.': '.$fetchresult);
3642: return '/adm/notfound.html';
3643: }
3644: }
3645: }
3646: unless ( $fetchresult eq 'ok') {
3647: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
3648: ' to host '.$home.': '.$fetchresult);
3649: }
3650: return $fetchresult;
3651: }
3652:
3653: sub build_filepath {
3654: my ($fpath) = @_;
3655: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
3656: unless ($fpath eq '') {
3657: my @parts=split('/',$fpath);
3658: foreach my $part (@parts) {
3659: $filepath.= '/'.$part;
3660: if ((-e $filepath)!=1) {
3661: mkdir($filepath,0777);
3662: }
3663: }
3664: }
3665: return $filepath;
3666: }
3667:
3668: sub store_edited_file {
3669: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
3670: my $file = $primary_url;
3671: $file =~ s#^/uploaded/$docudom/$docuname/##;
3672: my $fpath = '';
3673: my $fname = $file;
3674: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
3675: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
3676: my $filepath = &build_filepath($fpath);
3677: open(my $fh,'>',$filepath.'/'.$fname);
3678: print $fh $content;
3679: close($fh);
3680: my $home=&homeserver($docuname,$docudom);
3681: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3682: $home);
3683: if ($$fetchresult eq 'ok') {
3684: return '/uploaded/'.$fpath.'/'.$fname;
3685: } else {
3686: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
3687: ' to host '.$home.': '.$$fetchresult);
3688: return '/adm/notfound.html';
3689: }
3690: }
3691:
3692: sub clean_filename {
3693: my ($fname,$args)=@_;
3694: # Replace Windows backslashes by forward slashes
3695: $fname=~s/\\/\//g;
3696: if (!$args->{'keep_path'}) {
3697: # Get rid of everything but the actual filename
3698: $fname=~s/^.*\/([^\/]+)$/$1/;
3699: }
3700: # Replace spaces by underscores
3701: $fname=~s/\s+/\_/g;
3702: # Replace all other weird characters by nothing
3703: $fname=~s{[^/\w\.\-]}{}g;
3704: # Replace all .\d. sequences with _\d. so they no longer look like version
3705: # numbers
3706: $fname=~s/\.(\d+)(?=\.)/_$1/g;
3707: return $fname;
3708: }
3709: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
3710: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
3711: # image with the same aspect ratio as the original, but with dimensions which do
3712: # not exceed $resizewidth and $resizeheight.
3713:
3714: sub resizeImage {
3715: my ($img_path,$resizewidth,$resizeheight) = @_;
3716: my $ima = Image::Magick->new;
3717: my $resized;
3718: if (-e $img_path) {
3719: $ima->Read($img_path);
3720: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
3721: my $width = $ima->Get('width');
3722: my $height = $ima->Get('height');
3723: if ($width > $resizewidth) {
3724: my $factor = $width/$resizewidth;
3725: my $newheight = $height/$factor;
3726: $ima->Scale(width=>$resizewidth,height=>$newheight);
3727: $resized = 1;
3728: }
3729: }
3730: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
3731: my $width = $ima->Get('width');
3732: my $height = $ima->Get('height');
3733: if ($height > $resizeheight) {
3734: my $factor = $height/$resizeheight;
3735: my $newwidth = $width/$factor;
3736: $ima->Scale(width=>$newwidth,height=>$resizeheight);
3737: $resized = 1;
3738: }
3739: }
3740: if ($resized) {
3741: $ima->Write($img_path);
3742: }
3743: }
3744: return;
3745: }
3746:
3747: # --------------- Take an uploaded file and put it into the userfiles directory
3748: # input: $formname - the contents of the file are in $env{"form.$formname"}
3749: # the desired filename is in $env{"form.$formname.filename"}
3750: # $context - possible values: coursedoc, existingfile, overwrite,
3751: # canceloverwrite, or ''.
3752: # if 'coursedoc': upload to the current course
3753: # if 'existingfile': write file to tmp/overwrites directory
3754: # if 'canceloverwrite': delete file written to tmp/overwrites directory
3755: # $context is passed as argument to &finishuserfileupload
3756: # $subdir - directory in userfile to store the file into
3757: # $parser - instruction to parse file for objects ($parser = parse)
3758: # $allfiles - reference to hash for embedded objects
3759: # $codebase - reference to hash for codebase of java objects
3760: # $desuname - username for permanent storage of uploaded file
3761: # $dsetudom - domain for permanaent storage of uploaded file
3762: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
3763: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
3764: # $resizewidth - width (pixels) to which to resize uploaded image
3765: # $resizeheight - height (pixels) to which to resize uploaded image
3766: # $mimetype - reference to scalar to accommodate mime type determined
3767: # from File::MMagic.
3768: #
3769: # output: url of file in userspace, or error: <message>
3770: # or /adm/notfound.html if failure to upload occurse
3771:
3772: sub userfileupload {
3773: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
3774: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
3775: if (!defined($subdir)) { $subdir='unknown'; }
3776: my $fname=$env{'form.'.$formname.'.filename'};
3777: $fname=&clean_filename($fname);
3778: # See if there is anything left
3779: unless ($fname) { return 'error: no uploaded file'; }
3780: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
3781: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
3782: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
3783: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
3784: my $now = time;
3785: my $filepath;
3786: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
3787: $filepath = 'tmp/helprequests/'.$now;
3788: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
3789: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
3790: '_'.$env{'user.domain'}.'/pending';
3791: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
3792: my ($docuname,$docudom);
3793: if ($destudom =~ /^$match_domain$/) {
3794: $docudom = $destudom;
3795: } else {
3796: $docudom = $env{'user.domain'};
3797: }
3798: if ($destuname =~ /^$match_username$/) {
3799: $docuname = $destuname;
3800: } else {
3801: $docuname = $env{'user.name'};
3802: }
3803: if (exists($env{'form.group'})) {
3804: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3805: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3806: }
3807: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
3808: if ($context eq 'canceloverwrite') {
3809: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
3810: if (-e $tempfile) {
3811: my @info = stat($tempfile);
3812: if ($info[9] eq $env{'form.timestamp'}) {
3813: unlink($tempfile);
3814: }
3815: }
3816: return;
3817: }
3818: }
3819: # Create the directory if not present
3820: my @parts=split(/\//,$filepath);
3821: my $fullpath = $perlvar{'lonDaemons'};
3822: for (my $i=0;$i<@parts;$i++) {
3823: $fullpath .= '/'.$parts[$i];
3824: if ((-e $fullpath)!=1) {
3825: mkdir($fullpath,0777);
3826: }
3827: }
3828: open(my $fh,'>',$fullpath.'/'.$fname);
3829: print $fh $env{'form.'.$formname};
3830: close($fh);
3831: if ($context eq 'existingfile') {
3832: my @info = stat($fullpath.'/'.$fname);
3833: return ($fullpath.'/'.$fname,$info[9]);
3834: } else {
3835: return $fullpath.'/'.$fname;
3836: }
3837: }
3838: if ($subdir eq 'scantron') {
3839: $fname = 'scantron_orig_'.$fname;
3840: } else {
3841: $fname="$subdir/$fname";
3842: }
3843: if ($context eq 'coursedoc') {
3844: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3845: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3846: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
3847: return &finishuserfileupload($docuname,$docudom,
3848: $formname,$fname,$parser,$allfiles,
3849: $codebase,$thumbwidth,$thumbheight,
3850: $resizewidth,$resizeheight,$context,$mimetype);
3851: } else {
3852: if ($env{'form.folder'}) {
3853: $fname=$env{'form.folder'}.'/'.$fname;
3854: }
3855: return &process_coursefile('uploaddoc',$docuname,$docudom,
3856: $fname,$formname,$parser,
3857: $allfiles,$codebase,$mimetype);
3858: }
3859: } elsif (defined($destuname)) {
3860: my $docuname=$destuname;
3861: my $docudom=$destudom;
3862: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3863: $parser,$allfiles,$codebase,
3864: $thumbwidth,$thumbheight,
3865: $resizewidth,$resizeheight,$context,$mimetype);
3866: } else {
3867: my $docuname=$env{'user.name'};
3868: my $docudom=$env{'user.domain'};
3869: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
3870: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3871: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3872: }
3873: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3874: $parser,$allfiles,$codebase,
3875: $thumbwidth,$thumbheight,
3876: $resizewidth,$resizeheight,$context,$mimetype);
3877: }
3878: }
3879:
3880: sub finishuserfileupload {
3881: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
3882: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
3883: my $path=$docudom.'/'.$docuname.'/';
3884: my $filepath=$perlvar{'lonDocRoot'};
3885:
3886: my ($fnamepath,$file,$fetchthumb);
3887: $file=$fname;
3888: if ($fname=~m|/|) {
3889: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
3890: $path.=$fnamepath.'/';
3891: }
3892: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
3893: my $count;
3894: for ($count=4;$count<=$#parts;$count++) {
3895: $filepath.="/$parts[$count]";
3896: if ((-e $filepath)!=1) {
3897: mkdir($filepath,0777);
3898: }
3899: }
3900:
3901: # Save the file
3902: {
3903: if (!open(FH,'>',$filepath.'/'.$file)) {
3904: &logthis('Failed to create '.$filepath.'/'.$file);
3905: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
3906: return '/adm/notfound.html';
3907: }
3908: if ($context eq 'overwrite') {
3909: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
3910: my $target = $filepath.'/'.$file;
3911: if (-e $source) {
3912: my @info = stat($source);
3913: if ($info[9] eq $env{'form.timestamp'}) {
3914: unless (&File::Copy::move($source,$target)) {
3915: &logthis('Failed to overwrite '.$filepath.'/'.$file);
3916: return "Moving from $source failed";
3917: }
3918: } else {
3919: return "Temporary file: $source had unexpected date/time for last modification";
3920: }
3921: } else {
3922: return "Temporary file: $source missing";
3923: }
3924: } elsif (!print FH ($env{'form.'.$formname})) {
3925: &logthis('Failed to write to '.$filepath.'/'.$file);
3926: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
3927: return '/adm/notfound.html';
3928: }
3929: close(FH);
3930: if ($resizewidth && $resizeheight) {
3931: my $mm = new File::MMagic;
3932: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
3933: if ($mime_type =~ m{^image/}) {
3934: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
3935: }
3936: }
3937: }
3938: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
3939: if (ref($mimetype)) {
3940: if ($$mimetype eq '') {
3941: my $mm = new File::MMagic;
3942: my $type = $mm->checktype_filename($filepath.'/'.$file);
3943: $$mimetype = $type;
3944: }
3945: }
3946: }
3947: if ($parser eq 'parse') {
3948: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
3949: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
3950: $allfiles,$codebase);
3951: unless ($parse_result eq 'ok') {
3952: &logthis('Failed to parse '.$filepath.$file.
3953: ' for embedded media: '.$parse_result);
3954: }
3955: }
3956: }
3957: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
3958: my $input = $filepath.'/'.$file;
3959: my $output = $filepath.'/'.'tn-'.$file;
3960: my $thumbsize = $thumbwidth.'x'.$thumbheight;
3961: my @args = ('convert','-sample',$thumbsize,$input,$output);
3962: system({$args[0]} @args);
3963: if (-e $filepath.'/'.'tn-'.$file) {
3964: $fetchthumb = 1;
3965: }
3966: }
3967:
3968: # Notify homeserver to grep it
3969: #
3970: my $docuhome=&homeserver($docuname,$docudom);
3971: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
3972: if ($fetchresult eq 'ok') {
3973: if ($fetchthumb) {
3974: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
3975: if ($thumbresult ne 'ok') {
3976: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
3977: $docuhome.': '.$thumbresult);
3978: }
3979: }
3980: #
3981: # Return the URL to it
3982: return '/uploaded/'.$path.$file;
3983: } else {
3984: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
3985: ': '.$fetchresult);
3986: return '/adm/notfound.html';
3987: }
3988: }
3989:
3990: sub extract_embedded_items {
3991: my ($fullpath,$allfiles,$codebase,$content) = @_;
3992: my @state = ();
3993: my (%lastids,%related,%shockwave,%flashvars);
3994: my %javafiles = (
3995: codebase => '',
3996: code => '',
3997: archive => ''
3998: );
3999: my %mediafiles = (
4000: src => '',
4001: movie => '',
4002: );
4003: my $p;
4004: if ($content) {
4005: $p = HTML::LCParser->new($content);
4006: } else {
4007: $p = HTML::LCParser->new($fullpath);
4008: }
4009: while (my $t=$p->get_token()) {
4010: if ($t->[0] eq 'S') {
4011: my ($tagname, $attr) = ($t->[1],$t->[2]);
4012: push(@state, $tagname);
4013: if (lc($tagname) eq 'allow') {
4014: &add_filetype($allfiles,$attr->{'src'},'src');
4015: }
4016: if (lc($tagname) eq 'img') {
4017: &add_filetype($allfiles,$attr->{'src'},'src');
4018: }
4019: if (lc($tagname) eq 'a') {
4020: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
4021: &add_filetype($allfiles,$attr->{'href'},'href');
4022: }
4023: }
4024: if (lc($tagname) eq 'script') {
4025: my $src;
4026: if ($attr->{'archive'} =~ /\.jar$/i) {
4027: &add_filetype($allfiles,$attr->{'archive'},'archive');
4028: } else {
4029: if ($attr->{'src'} ne '') {
4030: $src = $attr->{'src'};
4031: &add_filetype($allfiles,$src,'src');
4032: }
4033: }
4034: my $text = $p->get_trimmed_text();
4035: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
4036: my @swfargs = split(/,/,$1);
4037: foreach my $item (@swfargs) {
4038: $item =~ s/["']//g;
4039: $item =~ s/^\s+//;
4040: $item =~ s/\s+$//;
4041: }
4042: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
4043: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
4044: push(@{$related{$swfargs[0]}},$swfargs[2]);
4045: } else {
4046: $related{$swfargs[0]} = [$swfargs[2]];
4047: }
4048: }
4049: }
4050: }
4051: if (lc($tagname) eq 'link') {
4052: if (lc($attr->{'rel'}) eq 'stylesheet') {
4053: &add_filetype($allfiles,$attr->{'href'},'href');
4054: }
4055: }
4056: if (lc($tagname) eq 'object' ||
4057: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
4058: foreach my $item (keys(%javafiles)) {
4059: $javafiles{$item} = '';
4060: }
4061: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
4062: $lastids{lc($tagname)} = $attr->{'id'};
4063: }
4064: }
4065: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
4066: my $name = lc($attr->{'name'});
4067: foreach my $item (keys(%javafiles)) {
4068: if ($name eq $item) {
4069: $javafiles{$item} = $attr->{'value'};
4070: last;
4071: }
4072: }
4073: my $pathfrom;
4074: foreach my $item (keys(%mediafiles)) {
4075: if ($name eq $item) {
4076: $pathfrom = $attr->{'value'};
4077: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
4078: &add_filetype($allfiles,$pathfrom,$name);
4079: last;
4080: }
4081: }
4082: if ($name eq 'flashvars') {
4083: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
4084: }
4085: if ($pathfrom ne '') {
4086: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
4087: $pathfrom);
4088: }
4089: }
4090: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
4091: foreach my $item (keys(%javafiles)) {
4092: if ($attr->{$item}) {
4093: $javafiles{$item} = $attr->{$item};
4094: last;
4095: }
4096: }
4097: foreach my $item (keys(%mediafiles)) {
4098: if ($attr->{$item}) {
4099: &add_filetype($allfiles,$attr->{$item},$item);
4100: last;
4101: }
4102: }
4103: if (lc($tagname) eq 'embed') {
4104: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
4105: &embedded_dependency($allfiles,\%related,$attr->{'name'},
4106: $attr->{'src'});
4107: }
4108: }
4109: }
4110: if (lc($tagname) eq 'iframe') {
4111: my $src = $attr->{'src'} ;
4112: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
4113: &add_filetype($allfiles,$src,'src');
4114: } elsif ($src =~ m{^/}) {
4115: if ($env{'request.course.id'}) {
4116: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
4117: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
4118: my $url = &hreflocation('',$fullpath);
4119: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
4120: my $relpath = $1;
4121: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
4122: &add_filetype($allfiles,$1,'src');
4123: }
4124: }
4125: }
4126: }
4127: }
4128: if ($t->[4] =~ m{/>$}) {
4129: pop(@state);
4130: }
4131: } elsif ($t->[0] eq 'E') {
4132: my ($tagname) = ($t->[1]);
4133: if ($javafiles{'codebase'} ne '') {
4134: $javafiles{'codebase'} .= '/';
4135: }
4136: if (lc($tagname) eq 'applet' ||
4137: lc($tagname) eq 'object' ||
4138: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
4139: ) {
4140: foreach my $item (keys(%javafiles)) {
4141: if ($item ne 'codebase' && $javafiles{$item} ne '') {
4142: my $file=$javafiles{'codebase'}.$javafiles{$item};
4143: &add_filetype($allfiles,$file,$item);
4144: }
4145: }
4146: }
4147: pop @state;
4148: }
4149: }
4150: foreach my $id (sort(keys(%flashvars))) {
4151: if ($shockwave{$id} ne '') {
4152: my @pairs = split(/\&/,$flashvars{$id});
4153: foreach my $pair (@pairs) {
4154: my ($key,$value) = split(/\=/,$pair);
4155: if ($key eq 'thumb') {
4156: &add_filetype($allfiles,$value,$key);
4157: } elsif ($key eq 'content') {
4158: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
4159: my ($ext) = ($value =~ /\.([^.]+)$/);
4160: if ($ext ne '') {
4161: &add_filetype($allfiles,$path.$value,$ext);
4162: }
4163: }
4164: }
4165: }
4166: }
4167: return 'ok';
4168: }
4169:
4170: sub add_filetype {
4171: my ($allfiles,$file,$type)=@_;
4172: if (exists($allfiles->{$file})) {
4173: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
4174: push(@{$allfiles->{$file}}, &escape($type));
4175: }
4176: } else {
4177: @{$allfiles->{$file}} = (&escape($type));
4178: }
4179: }
4180:
4181: sub embedded_dependency {
4182: my ($allfiles,$related,$identifier,$pathfrom) = @_;
4183: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
4184: if (($identifier ne '') &&
4185: (ref($related->{$identifier}) eq 'ARRAY') &&
4186: ($pathfrom ne '')) {
4187: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
4188: foreach my $dep (@{$related->{$identifier}}) {
4189: &add_filetype($allfiles,$path.$dep,'object');
4190: }
4191: }
4192: }
4193: return;
4194: }
4195:
4196: sub removeuploadedurl {
4197: my ($url)=@_;
4198: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
4199: return &removeuserfile($uname,$udom,$fname);
4200: }
4201:
4202: sub removeuserfile {
4203: my ($docuname,$docudom,$fname)=@_;
4204: my $home=&homeserver($docuname,$docudom);
4205: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
4206: if ($result eq 'ok') {
4207: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
4208: my $metafile = $fname.'.meta';
4209: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
4210: my $url = "/uploaded/$docudom/$docuname/$fname";
4211: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
4212: my $sqlresult =
4213: &update_portfolio_table($docuname,$docudom,$file,
4214: 'portfolio_metadata',$group,
4215: 'delete');
4216: }
4217: }
4218: return $result;
4219: }
4220:
4221: sub mkdiruserfile {
4222: my ($docuname,$docudom,$dir)=@_;
4223: my $home=&homeserver($docuname,$docudom);
4224: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
4225: }
4226:
4227: sub renameuserfile {
4228: my ($docuname,$docudom,$old,$new)=@_;
4229: my $home=&homeserver($docuname,$docudom);
4230: my $result = &reply("renameuserfile:$docudom:$docuname:".
4231: &escape("$old").':'.&escape("$new"),$home);
4232: if ($result eq 'ok') {
4233: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4234: my $oldmeta = $old.'.meta';
4235: my $newmeta = $new.'.meta';
4236: my $metaresult =
4237: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
4238: my $url = "/uploaded/$docudom/$docuname/$old";
4239: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
4240: my $sqlresult =
4241: &update_portfolio_table($docuname,$docudom,$file,
4242: 'portfolio_metadata',$group,
4243: 'delete');
4244: }
4245: }
4246: return $result;
4247: }
4248:
4249: # ------------------------------------------------------------------------- Log
4250:
4251: sub log {
4252: my ($dom,$nam,$hom,$what)=@_;
4253: return critical("log:$dom:$nam:$what",$hom);
4254: }
4255:
4256: # ------------------------------------------------------------------ Course Log
4257: #
4258: # This routine flushes several buffers of non-mission-critical nature
4259: #
4260:
4261: sub flushcourselogs {
4262: &logthis('Flushing log buffers');
4263: #
4264: # course logs
4265: # This is a log of all transactions in a course, which can be used
4266: # for data mining purposes
4267: #
4268: # It also collects the courseid database, which lists last transaction
4269: # times and course titles for all courseids
4270: #
4271: my %courseidbuffer=();
4272: foreach my $crsid (keys(%courselogs)) {
4273: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
4274: &escape($courselogs{$crsid}),
4275: $coursehombuf{$crsid}) eq 'ok') {
4276: delete $courselogs{$crsid};
4277: } else {
4278: &logthis('Failed to flush log buffer for '.$crsid);
4279: if (length($courselogs{$crsid})>40000) {
4280: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
4281: " exceeded maximum size, deleting.</font>");
4282: delete $courselogs{$crsid};
4283: }
4284: }
4285: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
4286: 'description' => $coursedescrbuf{$crsid},
4287: 'inst_code' => $courseinstcodebuf{$crsid},
4288: 'type' => $coursetypebuf{$crsid},
4289: 'owner' => $courseownerbuf{$crsid},
4290: };
4291: }
4292: #
4293: # Write course id database (reverse lookup) to homeserver of courses
4294: # Is used in pickcourse
4295: #
4296: foreach my $crs_home (keys(%courseidbuffer)) {
4297: my $response = &courseidput(&host_domain($crs_home),
4298: $courseidbuffer{$crs_home},
4299: $crs_home,'timeonly');
4300: }
4301: #
4302: # File accesses
4303: # Writes to the dynamic metadata of resources to get hit counts, etc.
4304: #
4305: foreach my $entry (keys(%accesshash)) {
4306: if ($entry =~ /___count$/) {
4307: my ($dom,$name);
4308: ($dom,$name,undef)=
4309: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
4310: if (! defined($dom) || $dom eq '' ||
4311: ! defined($name) || $name eq '') {
4312: my $cid = $env{'request.course.id'};
4313: $dom = $env{'request.'.$cid.'.domain'};
4314: $name = $env{'request.'.$cid.'.num'};
4315: }
4316: my $value = $accesshash{$entry};
4317: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
4318: my %temphash=($url => $value);
4319: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
4320: if ($result eq 'ok') {
4321: delete $accesshash{$entry};
4322: }
4323: } else {
4324: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
4325: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
4326: my %temphash=($entry => $accesshash{$entry});
4327: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
4328: delete $accesshash{$entry};
4329: }
4330: }
4331: }
4332: #
4333: # Roles
4334: # Reverse lookup of user roles for course faculty/staff and co-authorship
4335: #
4336: foreach my $entry (keys(%userrolehash)) {
4337: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
4338: split(/\:/,$entry);
4339: if (&Apache::lonnet::put('nohist_userroles',
4340: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
4341: $rudom,$runame) eq 'ok') {
4342: delete $userrolehash{$entry};
4343: }
4344: }
4345: #
4346: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
4347: #
4348: my %domrolebuffer = ();
4349: foreach my $entry (keys(%domainrolehash)) {
4350: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
4351: if ($domrolebuffer{$rudom}) {
4352: $domrolebuffer{$rudom}.='&'.&escape($entry).
4353: '='.&escape($domainrolehash{$entry});
4354: } else {
4355: $domrolebuffer{$rudom}.=&escape($entry).
4356: '='.&escape($domainrolehash{$entry});
4357: }
4358: delete $domainrolehash{$entry};
4359: }
4360: foreach my $dom (keys(%domrolebuffer)) {
4361: my %servers;
4362: if (defined(&domain($dom,'primary'))) {
4363: my $primary=&domain($dom,'primary');
4364: my $hostname=&hostname($primary);
4365: $servers{$primary} = $hostname;
4366: } else {
4367: %servers = &get_servers($dom,'library');
4368: }
4369: foreach my $tryserver (keys(%servers)) {
4370: if (&reply('domroleput:'.$dom.':'.
4371: $domrolebuffer{$dom},$tryserver) eq 'ok') {
4372: last;
4373: } else {
4374: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
4375: }
4376: }
4377: }
4378: $dumpcount++;
4379: }
4380:
4381: sub courselog {
4382: my $what=shift;
4383: $what=time.':'.$what;
4384: unless ($env{'request.course.id'}) { return ''; }
4385: $coursedombuf{$env{'request.course.id'}}=
4386: $env{'course.'.$env{'request.course.id'}.'.domain'};
4387: $coursenumbuf{$env{'request.course.id'}}=
4388: $env{'course.'.$env{'request.course.id'}.'.num'};
4389: $coursehombuf{$env{'request.course.id'}}=
4390: $env{'course.'.$env{'request.course.id'}.'.home'};
4391: $coursedescrbuf{$env{'request.course.id'}}=
4392: $env{'course.'.$env{'request.course.id'}.'.description'};
4393: $courseinstcodebuf{$env{'request.course.id'}}=
4394: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
4395: $courseownerbuf{$env{'request.course.id'}}=
4396: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
4397: $coursetypebuf{$env{'request.course.id'}}=
4398: $env{'course.'.$env{'request.course.id'}.'.type'};
4399: if (defined $courselogs{$env{'request.course.id'}}) {
4400: $courselogs{$env{'request.course.id'}}.='&'.$what;
4401: } else {
4402: $courselogs{$env{'request.course.id'}}.=$what;
4403: }
4404: if (length($courselogs{$env{'request.course.id'}})>4048) {
4405: &flushcourselogs();
4406: }
4407: }
4408:
4409: sub courseacclog {
4410: my $fnsymb=shift;
4411: unless ($env{'request.course.id'}) { return ''; }
4412: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
4413: if ($fnsymb=~/$LONCAPA::assess_re/) {
4414: $what.=':POST';
4415: # FIXME: Probably ought to escape things....
4416: foreach my $key (keys(%env)) {
4417: if ($key=~/^form\.(.*)/) {
4418: my $formitem = $1;
4419: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
4420: $what.=':'.$formitem.'='.$env{$key};
4421: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
4422: $what.=':'.$formitem.'='.$env{$key};
4423: }
4424: }
4425: }
4426: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
4427: # FIXME: We should not be depending on a form parameter that someone
4428: # editing lonsearchcat.pm might change in the future.
4429: if ($env{'form.phase'} eq 'course_search') {
4430: $what.= ':POST';
4431: # FIXME: Probably ought to escape things....
4432: foreach my $element ('courseexp','crsfulltext','crsrelated',
4433: 'crsdiscuss') {
4434: $what.=':'.$element.'='.$env{'form.'.$element};
4435: }
4436: }
4437: }
4438: &courselog($what);
4439: }
4440:
4441: sub countacc {
4442: my $url=&declutter(shift);
4443: return if (! defined($url) || $url eq '');
4444: unless ($env{'request.course.id'}) { return ''; }
4445: #
4446: # Mark that this url was used in this course
4447: #
4448: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
4449: #
4450: # Increase the access count for this resource in this child process
4451: #
4452: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
4453: $accesshash{$key}++;
4454: }
4455:
4456: sub linklog {
4457: my ($from,$to)=@_;
4458: $from=&declutter($from);
4459: $to=&declutter($to);
4460: $accesshash{$from.'___'.$to.'___comefrom'}=1;
4461: $accesshash{$to.'___'.$from.'___goto'}=1;
4462: }
4463:
4464: sub statslog {
4465: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
4466: if ($users<2) { return; }
4467: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
4468: 'course' => $env{'request.course.id'},
4469: 'sections' => '"all"',
4470: 'num_students' => $users,
4471: 'part' => $part,
4472: 'symb' => $symb,
4473: 'mean_tries' => $av_attempts,
4474: 'deg_of_diff' => $degdiff});
4475: foreach my $key (keys(%dynstore)) {
4476: $accesshash{$key}=$dynstore{$key};
4477: }
4478: }
4479:
4480: sub userrolelog {
4481: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
4482: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
4483: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4484: $userrolehash
4485: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
4486: =$tend.':'.$tstart;
4487: }
4488: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
4489: $userrolehash
4490: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
4491: =$tend.':'.$tstart;
4492: }
4493: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
4494: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4495: $domainrolehash
4496: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
4497: = $tend.':'.$tstart;
4498: }
4499: }
4500:
4501: sub courserolelog {
4502: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
4503: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
4504: my $cdom = $1;
4505: my $cnum = $2;
4506: my $sec = $3;
4507: my $namespace = 'rolelog';
4508: my %storehash = (
4509: role => $trole,
4510: start => $tstart,
4511: end => $tend,
4512: selfenroll => $selfenroll,
4513: context => $context,
4514: );
4515: if ($trole eq 'gr') {
4516: $namespace = 'groupslog';
4517: $storehash{'group'} = $sec;
4518: } else {
4519: $storehash{'section'} = $sec;
4520: }
4521: &write_log('course',$namespace,\%storehash,$delflag,$username,
4522: $domain,$cnum,$cdom);
4523: if (($trole ne 'st') || ($sec ne '')) {
4524: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
4525: }
4526: }
4527: return;
4528: }
4529:
4530: sub domainrolelog {
4531: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4532: if ($area =~ m{^/($match_domain)/$}) {
4533: my $cdom = $1;
4534: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
4535: my $namespace = 'rolelog';
4536: my %storehash = (
4537: role => $trole,
4538: start => $tstart,
4539: end => $tend,
4540: context => $context,
4541: );
4542: &write_log('domain',$namespace,\%storehash,$delflag,$username,
4543: $domain,$domconfiguser,$cdom);
4544: }
4545: return;
4546:
4547: }
4548:
4549: sub coauthorrolelog {
4550: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4551: if ($area =~ m{^/($match_domain)/($match_username)$}) {
4552: my $audom = $1;
4553: my $auname = $2;
4554: my $namespace = 'rolelog';
4555: my %storehash = (
4556: role => $trole,
4557: start => $tstart,
4558: end => $tend,
4559: context => $context,
4560: );
4561: &write_log('author',$namespace,\%storehash,$delflag,$username,
4562: $domain,$auname,$audom);
4563: }
4564: return;
4565: }
4566:
4567: sub get_course_adv_roles {
4568: my ($cid,$codes) = @_;
4569: $cid=$env{'request.course.id'} unless (defined($cid));
4570: my %coursehash=&coursedescription($cid);
4571: my $crstype = &Apache::loncommon::course_type($cid);
4572: my %nothide=();
4573: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
4574: if ($user !~ /:/) {
4575: $nothide{join(':',split(/[\@]/,$user))}=1;
4576: } else {
4577: $nothide{$user}=1;
4578: }
4579: }
4580: my @possdoms = ($coursehash{'domain'});
4581: if ($coursehash{'checkforpriv'}) {
4582: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
4583: }
4584: my %returnhash=();
4585: my %dumphash=
4586: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
4587: my $now=time;
4588: my %privileged;
4589: foreach my $entry (keys(%dumphash)) {
4590: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
4591: if (($tstart) && ($tstart<0)) { next; }
4592: if (($tend) && ($tend<$now)) { next; }
4593: if (($tstart) && ($now<$tstart)) { next; }
4594: my ($role,$username,$domain,$section)=split(/\:/,$entry);
4595: if ($username eq '' || $domain eq '') { next; }
4596: if ((&privileged($username,$domain,\@possdoms)) &&
4597: (!$nothide{$username.':'.$domain})) { next; }
4598: if ($role eq 'cr') { next; }
4599: if ($codes) {
4600: if ($section) { $role .= ':'.$section; }
4601: if ($returnhash{$role}) {
4602: $returnhash{$role}.=','.$username.':'.$domain;
4603: } else {
4604: $returnhash{$role}=$username.':'.$domain;
4605: }
4606: } else {
4607: my $key=&plaintext($role,$crstype);
4608: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
4609: if ($returnhash{$key}) {
4610: $returnhash{$key}.=','.$username.':'.$domain;
4611: } else {
4612: $returnhash{$key}=$username.':'.$domain;
4613: }
4614: }
4615: }
4616: return %returnhash;
4617: }
4618:
4619: sub get_my_roles {
4620: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
4621: unless (defined($uname)) { $uname=$env{'user.name'}; }
4622: unless (defined($udom)) { $udom=$env{'user.domain'}; }
4623: my (%dumphash,%nothide);
4624: if ($context eq 'userroles') {
4625: %dumphash = &dump('roles',$udom,$uname);
4626: } else {
4627: %dumphash = &dump('nohist_userroles',$udom,$uname);
4628: if ($hidepriv) {
4629: my %coursehash=&coursedescription($udom.'_'.$uname);
4630: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
4631: if ($user !~ /:/) {
4632: $nothide{join(':',split(/[\@]/,$user))} = 1;
4633: } else {
4634: $nothide{$user} = 1;
4635: }
4636: }
4637: }
4638: }
4639: my %returnhash=();
4640: my $now=time;
4641: my %privileged;
4642: foreach my $entry (keys(%dumphash)) {
4643: my ($role,$tend,$tstart);
4644: if ($context eq 'userroles') {
4645: next if ($entry =~ /^rolesdef/);
4646: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
4647: } else {
4648: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
4649: }
4650: if (($tstart) && ($tstart<0)) { next; }
4651: my $status = 'active';
4652: if (($tend) && ($tend<=$now)) {
4653: $status = 'previous';
4654: }
4655: if (($tstart) && ($now<$tstart)) {
4656: $status = 'future';
4657: }
4658: if (ref($types) eq 'ARRAY') {
4659: if (!grep(/^\Q$status\E$/,@{$types})) {
4660: next;
4661: }
4662: } else {
4663: if ($status ne 'active') {
4664: next;
4665: }
4666: }
4667: my ($rolecode,$username,$domain,$section,$area);
4668: if ($context eq 'userroles') {
4669: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
4670: (undef,$domain,$username,$section) = split(/\//,$area);
4671: } else {
4672: ($role,$username,$domain,$section) = split(/\:/,$entry);
4673: }
4674: if (ref($roledoms) eq 'ARRAY') {
4675: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
4676: next;
4677: }
4678: }
4679: if (ref($roles) eq 'ARRAY') {
4680: if (!grep(/^\Q$role\E$/,@{$roles})) {
4681: if ($role =~ /^cr\//) {
4682: if (!grep(/^cr$/,@{$roles})) {
4683: next;
4684: }
4685: } elsif ($role =~ /^gr\//) {
4686: if (!grep(/^gr$/,@{$roles})) {
4687: next;
4688: }
4689: } else {
4690: next;
4691: }
4692: }
4693: }
4694: if ($hidepriv) {
4695: my @privroles = ('dc','su');
4696: if ($context eq 'userroles') {
4697: next if (grep(/^\Q$role\E$/,@privroles));
4698: } else {
4699: my $possdoms = [$domain];
4700: if (ref($roledoms) eq 'ARRAY') {
4701: push(@{$possdoms},@{$roledoms});
4702: }
4703: if (&privileged($username,$domain,$possdoms,\@privroles)) {
4704: if (!$nothide{$username.':'.$domain}) {
4705: next;
4706: }
4707: }
4708: }
4709: }
4710: if ($withsec) {
4711: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
4712: $tstart.':'.$tend;
4713: } else {
4714: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
4715: }
4716: }
4717: return %returnhash;
4718: }
4719:
4720: sub get_all_adhocroles {
4721: my ($dom) = @_;
4722: my @roles_by_num = ();
4723: my %domdefaults = &get_domain_defaults($dom);
4724: my (%description,%access_in_dom,%access_info);
4725: if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
4726: my $count = 0;
4727: my %domcurrent = %{$domdefaults{'adhocroles'}};
4728: my %ordered;
4729: foreach my $role (sort(keys(%domcurrent))) {
4730: my ($order,$desc,$access_in_dom);
4731: if (ref($domcurrent{$role}) eq 'HASH') {
4732: $order = $domcurrent{$role}{'order'};
4733: $desc = $domcurrent{$role}{'desc'};
4734: $access_in_dom{$role} = $domcurrent{$role}{'access'};
4735: $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
4736: }
4737: if ($order eq '') {
4738: $order = $count;
4739: }
4740: $ordered{$order} = $role;
4741: if ($desc ne '') {
4742: $description{$role} = $desc;
4743: } else {
4744: $description{$role}= $role;
4745: }
4746: $count++;
4747: }
4748: foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
4749: push(@roles_by_num,$ordered{$item});
4750: }
4751: }
4752: return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
4753: }
4754:
4755: sub get_my_adhocroles {
4756: my ($cid,$checkreg) = @_;
4757: my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
4758: if ($env{'request.course.id'} eq $cid) {
4759: $cdom = $env{'course.'.$cid.'.domain'};
4760: $cnum = $env{'course.'.$cid.'.num'};
4761: $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
4762: } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
4763: $cdom = $1;
4764: $cnum = $2;
4765: %info = &Apache::lonnet::get('environment',['internal.coursecode'],
4766: $cdom,$cnum);
4767: }
4768: if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
4769: my $user = $env{'user.name'}.':'.$env{'user.domain'};
4770: my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
4771: if ($rosterhash{$user} ne '') {
4772: my $type = (split(/:/,$rosterhash{$user}))[5];
4773: return ([],{}) if ($type eq 'auto');
4774: }
4775: }
4776: if (($cdom ne '') && ($cnum ne '')) {
4777: if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
4778: my $then=$env{'user.login.time'};
4779: my $update=$env{'user.update.time'};
4780: if (!$update) {
4781: $update = $then;
4782: }
4783: my @liveroles;
4784: foreach my $role ('dh','da') {
4785: if ($env{"user.role.$role./$cdom/"}) {
4786: my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
4787: my $limit = $update;
4788: if ($env{'request.role'} eq "$role./$cdom/") {
4789: $limit = $then;
4790: }
4791: my $activerole = 1;
4792: if ($tstart && $tstart>$limit) { $activerole = 0; }
4793: if ($tend && $tend <$limit) { $activerole = 0; }
4794: if ($activerole) {
4795: push(@liveroles,$role);
4796: }
4797: }
4798: }
4799: if (@liveroles) {
4800: if (&homeserver($cnum,$cdom) ne 'no_host') {
4801: my ($accessref,$accessinfo,%access_in_dom);
4802: ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
4803: if (ref($roles_by_num) eq 'ARRAY') {
4804: if (@{$roles_by_num}) {
4805: my %settings;
4806: if ($env{'request.course.id'} eq $cid) {
4807: foreach my $envkey (keys(%env)) {
4808: if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
4809: $settings{$1} = $env{$envkey};
4810: }
4811: }
4812: } else {
4813: %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
4814: }
4815: my %setincrs;
4816: if ($settings{'internal.adhocaccess'}) {
4817: map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
4818: }
4819: my @statuses;
4820: if ($env{'environment.inststatus'}) {
4821: @statuses = split(/,/,$env{'environment.inststatus'});
4822: }
4823: my $user = $env{'user.name'}.':'.$env{'user.domain'};
4824: if (ref($accessref) eq 'HASH') {
4825: %access_in_dom = %{$accessref};
4826: }
4827: foreach my $role (@{$roles_by_num}) {
4828: my ($curraccess,@okstatus,@personnel);
4829: if ($setincrs{$role}) {
4830: ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
4831: if ($curraccess eq 'status') {
4832: @okstatus = split(/\&/,$rest);
4833: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4834: @personnel = split(/\&/,$rest);
4835: }
4836: } else {
4837: $curraccess = $access_in_dom{$role};
4838: if (ref($accessinfo) eq 'HASH') {
4839: if ($curraccess eq 'status') {
4840: if (ref($accessinfo->{$role}) eq 'ARRAY') {
4841: @okstatus = @{$accessinfo->{$role}};
4842: }
4843: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4844: if (ref($accessinfo->{$role}) eq 'ARRAY') {
4845: @personnel = @{$accessinfo->{$role}};
4846: }
4847: }
4848: }
4849: }
4850: if ($curraccess eq 'none') {
4851: next;
4852: } elsif ($curraccess eq 'all') {
4853: push(@possroles,$role);
4854: } elsif ($curraccess eq 'dh') {
4855: if (grep(/^dh$/,@liveroles)) {
4856: push(@possroles,$role);
4857: } else {
4858: next;
4859: }
4860: } elsif ($curraccess eq 'da') {
4861: if (grep(/^da$/,@liveroles)) {
4862: push(@possroles,$role);
4863: } else {
4864: next;
4865: }
4866: } elsif ($curraccess eq 'status') {
4867: if (@okstatus) {
4868: if (!@statuses) {
4869: if (grep(/^default$/,@okstatus)) {
4870: push(@possroles,$role);
4871: }
4872: } else {
4873: foreach my $status (@okstatus) {
4874: if (grep(/^\Q$status\E$/,@statuses)) {
4875: push(@possroles,$role);
4876: last;
4877: }
4878: }
4879: }
4880: }
4881: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4882: if (grep(/^\Q$user\E$/,@personnel)) {
4883: if ($curraccess eq 'exc') {
4884: push(@possroles,$role);
4885: }
4886: } elsif ($curraccess eq 'inc') {
4887: push(@possroles,$role);
4888: }
4889: }
4890: }
4891: }
4892: }
4893: }
4894: }
4895: }
4896: }
4897: unless (ref($description) eq 'HASH') {
4898: if (ref($roles_by_num) eq 'ARRAY') {
4899: my %desc;
4900: map { $desc{$_} = $_; } (@{$roles_by_num});
4901: $description = \%desc;
4902: } else {
4903: $description = {};
4904: }
4905: }
4906: return (\@possroles,$description);
4907: }
4908:
4909: # ----------------------------------------------------- Frontpage Announcements
4910: #
4911: #
4912:
4913: sub postannounce {
4914: my ($server,$text)=@_;
4915: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
4916: unless ($text=~/\w/) { $text=''; }
4917: return &reply('setannounce:'.&escape($text),$server);
4918: }
4919:
4920: sub getannounce {
4921:
4922: if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
4923: my $announcement='';
4924: while (my $line = <$fh>) { $announcement .= $line; }
4925: close($fh);
4926: if ($announcement=~/\w/) {
4927: return
4928: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
4929: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
4930: } else {
4931: return '';
4932: }
4933: } else {
4934: return '';
4935: }
4936: }
4937:
4938: # ---------------------------------------------------------- Course ID routines
4939: # Deal with domain's nohist_courseid.db files
4940: #
4941:
4942: sub courseidput {
4943: my ($domain,$storehash,$coursehome,$caller) = @_;
4944: return unless (ref($storehash) eq 'HASH');
4945: my $outcome;
4946: if ($caller eq 'timeonly') {
4947: my $cids = '';
4948: foreach my $item (keys(%$storehash)) {
4949: $cids.=&escape($item).'&';
4950: }
4951: $cids=~s/\&$//;
4952: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
4953: $coursehome);
4954: } else {
4955: my $items = '';
4956: foreach my $item (keys(%$storehash)) {
4957: $items.= &escape($item).'='.
4958: &freeze_escape($$storehash{$item}).'&';
4959: }
4960: $items=~s/\&$//;
4961: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
4962: $coursehome);
4963: }
4964: if ($outcome eq 'unknown_cmd') {
4965: my $what;
4966: foreach my $cid (keys(%$storehash)) {
4967: $what .= &escape($cid).'=';
4968: foreach my $item ('description','inst_code','owner','type') {
4969: $what .= &escape($storehash->{$cid}{$item}).':';
4970: }
4971: $what =~ s/\:$/&/;
4972: }
4973: $what =~ s/\&$//;
4974: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
4975: } else {
4976: return $outcome;
4977: }
4978: }
4979:
4980: sub courseiddump {
4981: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
4982: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
4983: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
4984: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
4985: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
4986: my $as_hash = 1;
4987: my %returnhash;
4988: if (!$domfilter) { $domfilter=''; }
4989: my %libserv = &all_library();
4990: foreach my $tryserver (keys(%libserv)) {
4991: if ( ( $hostidflag == 1
4992: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
4993: || (!defined($hostidflag)) ) {
4994:
4995: if (($domfilter eq '') ||
4996: (&host_domain($tryserver) eq $domfilter)) {
4997: my $rep;
4998: if (grep { $_ eq $tryserver } current_machine_ids()) {
4999: $rep = LONCAPA::Lond::dump_course_id_handler(
5000: join(":", (&host_domain($tryserver), $sincefilter,
5001: &escape($descfilter), &escape($instcodefilter),
5002: &escape($ownerfilter), &escape($coursefilter),
5003: &escape($typefilter), &escape($regexp_ok),
5004: $as_hash, &escape($selfenrollonly),
5005: &escape($catfilter), $showhidden, $caller,
5006: &escape($cloner), &escape($cc_clone), $cloneonly,
5007: &escape($createdbefore), &escape($createdafter),
5008: &escape($creationcontext),$domcloner,$hasuniquecode,
5009: $reqcrsdom,&escape($reqinstcode))));
5010: } else {
5011: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
5012: $sincefilter.':'.&escape($descfilter).':'.
5013: &escape($instcodefilter).':'.&escape($ownerfilter).
5014: ':'.&escape($coursefilter).':'.&escape($typefilter).
5015: ':'.&escape($regexp_ok).':'.$as_hash.':'.
5016: &escape($selfenrollonly).':'.&escape($catfilter).':'.
5017: $showhidden.':'.$caller.':'.&escape($cloner).':'.
5018: &escape($cc_clone).':'.$cloneonly.':'.
5019: &escape($createdbefore).':'.&escape($createdafter).':'.
5020: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
5021: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
5022: }
5023:
5024: my @pairs=split(/\&/,$rep);
5025: foreach my $item (@pairs) {
5026: my ($key,$value)=split(/\=/,$item,2);
5027: $key = &unescape($key);
5028: next if ($key =~ /^error: 2 /);
5029: my $result = &thaw_unescape($value);
5030: if (ref($result) eq 'HASH') {
5031: $returnhash{$key}=$result;
5032: } else {
5033: my @responses = split(/:/,$value);
5034: my @items = ('description','inst_code','owner','type');
5035: for (my $i=0; $i<@responses; $i++) {
5036: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
5037: }
5038: }
5039: }
5040: }
5041: }
5042: }
5043: return %returnhash;
5044: }
5045:
5046: sub courselastaccess {
5047: my ($cdom,$cnum,$hostidref) = @_;
5048: my %returnhash;
5049: if ($cdom && $cnum) {
5050: my $chome = &homeserver($cnum,$cdom);
5051: if ($chome ne 'no_host') {
5052: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
5053: &extract_lastaccess(\%returnhash,$rep);
5054: }
5055: } else {
5056: if (!$cdom) { $cdom=''; }
5057: my %libserv = &all_library();
5058: foreach my $tryserver (keys(%libserv)) {
5059: if (ref($hostidref) eq 'ARRAY') {
5060: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
5061: }
5062: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
5063: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
5064: &extract_lastaccess(\%returnhash,$rep);
5065: }
5066: }
5067: }
5068: return %returnhash;
5069: }
5070:
5071: sub extract_lastaccess {
5072: my ($returnhash,$rep) = @_;
5073: if (ref($returnhash) eq 'HASH') {
5074: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
5075: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
5076: $rep eq '') {
5077: my @pairs=split(/\&/,$rep);
5078: foreach my $item (@pairs) {
5079: my ($key,$value)=split(/\=/,$item,2);
5080: $key = &unescape($key);
5081: next if ($key =~ /^error: 2 /);
5082: $returnhash->{$key} = &thaw_unescape($value);
5083: }
5084: }
5085: }
5086: return;
5087: }
5088:
5089: # ---------------------------------------------------------- DC e-mail
5090:
5091: sub dcmailput {
5092: my ($domain,$msgid,$message,$server)=@_;
5093: my $status = &Apache::lonnet::critical(
5094: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
5095: &escape($message),$server);
5096: return $status;
5097: }
5098:
5099: sub dcmaildump {
5100: my ($dom,$startdate,$enddate,$senders) = @_;
5101: my %returnhash=();
5102:
5103: if (defined(&domain($dom,'primary'))) {
5104: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
5105: &escape($enddate).':';
5106: my @esc_senders=map { &escape($_)} @$senders;
5107: $cmd.=&escape(join('&',@esc_senders));
5108: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
5109: my ($key,$value) = split(/\=/,$line,2);
5110: if (($key) && ($value)) {
5111: $returnhash{&unescape($key)} = &unescape($value);
5112: }
5113: }
5114: }
5115: return %returnhash;
5116: }
5117: # ---------------------------------------------------------- Domain roles
5118:
5119: sub get_domain_roles {
5120: my ($dom,$roles,$startdate,$enddate)=@_;
5121: if ((!defined($startdate)) || ($startdate eq '')) {
5122: $startdate = '.';
5123: }
5124: if ((!defined($enddate)) || ($enddate eq '')) {
5125: $enddate = '.';
5126: }
5127: my $rolelist;
5128: if (ref($roles) eq 'ARRAY') {
5129: $rolelist = join('&',@{$roles});
5130: }
5131: my %personnel = ();
5132:
5133: my %servers = &get_servers($dom,'library');
5134: foreach my $tryserver (keys(%servers)) {
5135: %{$personnel{$tryserver}}=();
5136: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
5137: &escape($startdate).':'.
5138: &escape($enddate).':'.
5139: &escape($rolelist), $tryserver))) {
5140: my ($key,$value) = split(/\=/,$line,2);
5141: if (($key) && ($value)) {
5142: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
5143: }
5144: }
5145: }
5146: return %personnel;
5147: }
5148:
5149: sub get_active_domroles {
5150: my ($dom,$roles) = @_;
5151: return () unless (ref($roles) eq 'ARRAY');
5152: my $now = time;
5153: my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
5154: my %domroles;
5155: foreach my $server (keys(%dompersonnel)) {
5156: foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
5157: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
5158: $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
5159: }
5160: }
5161: return %domroles;
5162: }
5163:
5164: # ----------------------------------------------------------- Interval timing
5165:
5166: {
5167: # Caches needed for speedup of navmaps
5168: # We don't want to cache this for very long at all (5 seconds at most)
5169: #
5170: # The user for whom we cache
5171: my $cachedkey='';
5172: # The cached times for this user
5173: my %cachedtimes=();
5174: # When this was last done
5175: my $cachedtime='';
5176:
5177: sub load_all_first_access {
5178: my ($uname,$udom,$ignorecache)=@_;
5179: if (($cachedkey eq $uname.':'.$udom) &&
5180: (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
5181: (!$ignorecache)) {
5182: return;
5183: }
5184: $cachedtime=time;
5185: $cachedkey=$uname.':'.$udom;
5186: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
5187: }
5188:
5189: sub get_first_access {
5190: my ($type,$argsymb,$argmap,$ignorecache)=@_;
5191: my ($symb,$courseid,$udom,$uname)=&whichuser();
5192: if ($argsymb) { $symb=$argsymb; }
5193: my ($map,$id,$res)=&decode_symb($symb);
5194: if ($argmap) { $map = $argmap; }
5195: if ($type eq 'course') {
5196: $res='course';
5197: } elsif ($type eq 'map') {
5198: $res=&symbread($map);
5199: } else {
5200: $res=$symb;
5201: }
5202: &load_all_first_access($uname,$udom,$ignorecache);
5203: return $cachedtimes{"$courseid\0$res"};
5204: }
5205:
5206: sub set_first_access {
5207: my ($type,$interval)=@_;
5208: my ($symb,$courseid,$udom,$uname)=&whichuser();
5209: my ($map,$id,$res)=&decode_symb($symb);
5210: if ($type eq 'course') {
5211: $res='course';
5212: } elsif ($type eq 'map') {
5213: $res=&symbread($map);
5214: } else {
5215: $res=$symb;
5216: }
5217: $cachedkey='';
5218: my $firstaccess=&get_first_access($type,$symb,$map);
5219: if (!$firstaccess) {
5220: my $start = time;
5221: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
5222: $udom,$uname);
5223: if ($putres eq 'ok') {
5224: &put('timerinterval',{"$courseid\0$res"=>$interval},
5225: $udom,$uname);
5226: &appenv(
5227: {
5228: 'course.'.$courseid.'.firstaccess.'.$res => $start,
5229: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
5230: }
5231: );
5232: if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
5233: $cachedtimes{"$courseid\0$res"} = $start;
5234: }
5235: }
5236: return $putres;
5237: }
5238: return 'already_set';
5239: }
5240: }
5241:
5242: # --------------------------------------------- Set Expire Date for Spreadsheet
5243:
5244: sub expirespread {
5245: my ($uname,$udom,$stype,$usymb)=@_;
5246: my $cid=$env{'request.course.id'};
5247: if ($cid) {
5248: my $now=time;
5249: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
5250: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
5251: $env{'course.'.$cid.'.num'}.
5252: ':nohist_expirationdates:'.
5253: &escape($key).'='.$now,
5254: $env{'course.'.$cid.'.home'})
5255: }
5256: return 'ok';
5257: }
5258:
5259: # ----------------------------------------------------- Devalidate Spreadsheets
5260:
5261: sub devalidate {
5262: my ($symb,$uname,$udom)=@_;
5263: my $cid=$env{'request.course.id'};
5264: if ($cid) {
5265: # delete the stored spreadsheets for
5266: # - the student level sheet of this user in course's homespace
5267: # - the assessment level sheet for this resource
5268: # for this user in user's homespace
5269: # - current conditional state info
5270: my $key=$uname.':'.$udom.':';
5271: my $status=
5272: &del('nohist_calculatedsheets',
5273: [$key.'studentcalc:'],
5274: $env{'course.'.$cid.'.domain'},
5275: $env{'course.'.$cid.'.num'})
5276: .' '.
5277: &del('nohist_calculatedsheets_'.$cid,
5278: [$key.'assesscalc:'.$symb],$udom,$uname);
5279: unless ($status eq 'ok ok') {
5280: &logthis('Could not devalidate spreadsheet '.
5281: $uname.' at '.$udom.' for '.
5282: $symb.': '.$status);
5283: }
5284: &delenv('user.state.'.$cid);
5285: }
5286: }
5287:
5288: sub get_scalar {
5289: my ($string,$end) = @_;
5290: my $value;
5291: if ($$string =~ s/^([^&]*?)($end)/$2/) {
5292: $value = $1;
5293: } elsif ($$string =~ s/^([^&]*?)&//) {
5294: $value = $1;
5295: }
5296: return &unescape($value);
5297: }
5298:
5299: sub array2str {
5300: my (@array) = @_;
5301: my $result=&arrayref2str(\@array);
5302: $result=~s/^__ARRAY_REF__//;
5303: $result=~s/__END_ARRAY_REF__$//;
5304: return $result;
5305: }
5306:
5307: sub arrayref2str {
5308: my ($arrayref) = @_;
5309: my $result='__ARRAY_REF__';
5310: foreach my $elem (@$arrayref) {
5311: if(ref($elem) eq 'ARRAY') {
5312: $result.=&arrayref2str($elem).'&';
5313: } elsif(ref($elem) eq 'HASH') {
5314: $result.=&hashref2str($elem).'&';
5315: } elsif(ref($elem)) {
5316: #print("Got a ref of ".(ref($elem))." skipping.");
5317: } else {
5318: $result.=&escape($elem).'&';
5319: }
5320: }
5321: $result=~s/\&$//;
5322: $result .= '__END_ARRAY_REF__';
5323: return $result;
5324: }
5325:
5326: sub hash2str {
5327: my (%hash) = @_;
5328: my $result=&hashref2str(\%hash);
5329: $result=~s/^__HASH_REF__//;
5330: $result=~s/__END_HASH_REF__$//;
5331: return $result;
5332: }
5333:
5334: sub hashref2str {
5335: my ($hashref)=@_;
5336: my $result='__HASH_REF__';
5337: foreach my $key (sort(keys(%$hashref))) {
5338: if (ref($key) eq 'ARRAY') {
5339: $result.=&arrayref2str($key).'=';
5340: } elsif (ref($key) eq 'HASH') {
5341: $result.=&hashref2str($key).'=';
5342: } elsif (ref($key)) {
5343: $result.='=';
5344: #print("Got a ref of ".(ref($key))." skipping.");
5345: } else {
5346: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
5347: }
5348:
5349: if(ref($hashref->{$key}) eq 'ARRAY') {
5350: $result.=&arrayref2str($hashref->{$key}).'&';
5351: } elsif(ref($hashref->{$key}) eq 'HASH') {
5352: $result.=&hashref2str($hashref->{$key}).'&';
5353: } elsif(ref($hashref->{$key})) {
5354: $result.='&';
5355: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
5356: } else {
5357: $result.=&escape($hashref->{$key}).'&';
5358: }
5359: }
5360: $result=~s/\&$//;
5361: $result .= '__END_HASH_REF__';
5362: return $result;
5363: }
5364:
5365: sub str2hash {
5366: my ($string)=@_;
5367: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
5368: return %$hash;
5369: }
5370:
5371: sub str2hashref {
5372: my ($string) = @_;
5373:
5374: my %hash;
5375:
5376: if($string !~ /^__HASH_REF__/) {
5377: if (! ($string eq '' || !defined($string))) {
5378: $hash{'error'}='Not hash reference';
5379: }
5380: return (\%hash, $string);
5381: }
5382:
5383: $string =~ s/^__HASH_REF__//;
5384:
5385: while($string !~ /^__END_HASH_REF__/) {
5386: #key
5387: my $key='';
5388: if($string =~ /^__HASH_REF__/) {
5389: ($key, $string)=&str2hashref($string);
5390: if(defined($key->{'error'})) {
5391: $hash{'error'}='Bad data';
5392: return (\%hash, $string);
5393: }
5394: } elsif($string =~ /^__ARRAY_REF__/) {
5395: ($key, $string)=&str2arrayref($string);
5396: if($key->[0] eq 'Array reference error') {
5397: $hash{'error'}='Bad data';
5398: return (\%hash, $string);
5399: }
5400: } else {
5401: $string =~ s/^(.*?)=//;
5402: $key=&unescape($1);
5403: }
5404: $string =~ s/^=//;
5405:
5406: #value
5407: my $value='';
5408: if($string =~ /^__HASH_REF__/) {
5409: ($value, $string)=&str2hashref($string);
5410: if(defined($value->{'error'})) {
5411: $hash{'error'}='Bad data';
5412: return (\%hash, $string);
5413: }
5414: } elsif($string =~ /^__ARRAY_REF__/) {
5415: ($value, $string)=&str2arrayref($string);
5416: if($value->[0] eq 'Array reference error') {
5417: $hash{'error'}='Bad data';
5418: return (\%hash, $string);
5419: }
5420: } else {
5421: $value=&get_scalar(\$string,'__END_HASH_REF__');
5422: }
5423: $string =~ s/^&//;
5424:
5425: $hash{$key}=$value;
5426: }
5427:
5428: $string =~ s/^__END_HASH_REF__//;
5429:
5430: return (\%hash, $string);
5431: }
5432:
5433: sub str2array {
5434: my ($string)=@_;
5435: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
5436: return @$array;
5437: }
5438:
5439: sub str2arrayref {
5440: my ($string) = @_;
5441: my @array;
5442:
5443: if($string !~ /^__ARRAY_REF__/) {
5444: if (! ($string eq '' || !defined($string))) {
5445: $array[0]='Array reference error';
5446: }
5447: return (\@array, $string);
5448: }
5449:
5450: $string =~ s/^__ARRAY_REF__//;
5451:
5452: while($string !~ /^__END_ARRAY_REF__/) {
5453: my $value='';
5454: if($string =~ /^__HASH_REF__/) {
5455: ($value, $string)=&str2hashref($string);
5456: if(defined($value->{'error'})) {
5457: $array[0] ='Array reference error';
5458: return (\@array, $string);
5459: }
5460: } elsif($string =~ /^__ARRAY_REF__/) {
5461: ($value, $string)=&str2arrayref($string);
5462: if($value->[0] eq 'Array reference error') {
5463: $array[0] ='Array reference error';
5464: return (\@array, $string);
5465: }
5466: } else {
5467: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
5468: }
5469: $string =~ s/^&//;
5470:
5471: push(@array, $value);
5472: }
5473:
5474: $string =~ s/^__END_ARRAY_REF__//;
5475:
5476: return (\@array, $string);
5477: }
5478:
5479: # -------------------------------------------------------------------Temp Store
5480:
5481: sub tmpreset {
5482: my ($symb,$namespace,$domain,$stuname) = @_;
5483: if (!$symb) {
5484: $symb=&symbread();
5485: if (!$symb) { $symb= $env{'request.url'}; }
5486: }
5487: $symb=escape($symb);
5488:
5489: if (!$namespace) { $namespace=$env{'request.state'}; }
5490: $namespace=~s/\//\_/g;
5491: $namespace=~s/\W//g;
5492:
5493: if (!$domain) { $domain=$env{'user.domain'}; }
5494: if (!$stuname) { $stuname=$env{'user.name'}; }
5495: if ($domain eq 'public' && $stuname eq 'public') {
5496: $stuname=$ENV{'REMOTE_ADDR'};
5497: }
5498: my $path=LONCAPA::tempdir();
5499: my %hash;
5500: if (tie(%hash,'GDBM_File',
5501: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
5502: &GDBM_WRCREAT(),0640)) {
5503: foreach my $key (keys(%hash)) {
5504: if ($key=~ /:$symb/) {
5505: delete($hash{$key});
5506: }
5507: }
5508: }
5509: }
5510:
5511: sub tmpstore {
5512: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
5513:
5514: if (!$symb) {
5515: $symb=&symbread();
5516: if (!$symb) { $symb= $env{'request.url'}; }
5517: }
5518: $symb=escape($symb);
5519:
5520: if (!$namespace) {
5521: # I don't think we would ever want to store this for a course.
5522: # it seems this will only be used if we don't have a course.
5523: #$namespace=$env{'request.course.id'};
5524: #if (!$namespace) {
5525: $namespace=$env{'request.state'};
5526: #}
5527: }
5528: $namespace=~s/\//\_/g;
5529: $namespace=~s/\W//g;
5530: if (!$domain) { $domain=$env{'user.domain'}; }
5531: if (!$stuname) { $stuname=$env{'user.name'}; }
5532: if ($domain eq 'public' && $stuname eq 'public') {
5533: $stuname=$ENV{'REMOTE_ADDR'};
5534: }
5535: my $now=time;
5536: my %hash;
5537: my $path=LONCAPA::tempdir();
5538: if (tie(%hash,'GDBM_File',
5539: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
5540: &GDBM_WRCREAT(),0640)) {
5541: $hash{"version:$symb"}++;
5542: my $version=$hash{"version:$symb"};
5543: my $allkeys='';
5544: foreach my $key (keys(%$storehash)) {
5545: $allkeys.=$key.':';
5546: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
5547: }
5548: $hash{"$version:$symb:timestamp"}=$now;
5549: $allkeys.='timestamp';
5550: $hash{"$version:keys:$symb"}=$allkeys;
5551: if (untie(%hash)) {
5552: return 'ok';
5553: } else {
5554: return "error:$!";
5555: }
5556: } else {
5557: return "error:$!";
5558: }
5559: }
5560:
5561: # -----------------------------------------------------------------Temp Restore
5562:
5563: sub tmprestore {
5564: my ($symb,$namespace,$domain,$stuname) = @_;
5565:
5566: if (!$symb) {
5567: $symb=&symbread();
5568: if (!$symb) { $symb= $env{'request.url'}; }
5569: }
5570: $symb=escape($symb);
5571:
5572: if (!$namespace) { $namespace=$env{'request.state'}; }
5573:
5574: if (!$domain) { $domain=$env{'user.domain'}; }
5575: if (!$stuname) { $stuname=$env{'user.name'}; }
5576: if ($domain eq 'public' && $stuname eq 'public') {
5577: $stuname=$ENV{'REMOTE_ADDR'};
5578: }
5579: my %returnhash;
5580: $namespace=~s/\//\_/g;
5581: $namespace=~s/\W//g;
5582: my %hash;
5583: my $path=LONCAPA::tempdir();
5584: if (tie(%hash,'GDBM_File',
5585: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
5586: &GDBM_READER(),0640)) {
5587: my $version=$hash{"version:$symb"};
5588: $returnhash{'version'}=$version;
5589: my $scope;
5590: for ($scope=1;$scope<=$version;$scope++) {
5591: my $vkeys=$hash{"$scope:keys:$symb"};
5592: my @keys=split(/:/,$vkeys);
5593: my $key;
5594: $returnhash{"$scope:keys"}=$vkeys;
5595: foreach $key (@keys) {
5596: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
5597: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
5598: }
5599: }
5600: if (!(untie(%hash))) {
5601: return "error:$!";
5602: }
5603: } else {
5604: return "error:$!";
5605: }
5606: return %returnhash;
5607: }
5608:
5609: # ----------------------------------------------------------------------- Store
5610:
5611: sub store {
5612: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
5613: my $home='';
5614:
5615: if ($stuname) { $home=&homeserver($stuname,$domain); }
5616:
5617: $symb=&symbclean($symb);
5618: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
5619:
5620: if (!$domain) { $domain=$env{'user.domain'}; }
5621: if (!$stuname) { $stuname=$env{'user.name'}; }
5622:
5623: &devalidate($symb,$stuname,$domain);
5624:
5625: $symb=escape($symb);
5626: if (!$namespace) {
5627: unless ($namespace=$env{'request.course.id'}) {
5628: return '';
5629: }
5630: }
5631: if (!$home) { $home=$env{'user.home'}; }
5632:
5633: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5634: $$storehash{'host'}=$perlvar{'lonHostID'};
5635:
5636: my $namevalue='';
5637: foreach my $key (keys(%$storehash)) {
5638: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
5639: }
5640: $namevalue=~s/\&$//;
5641: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
5642: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
5643: }
5644:
5645: # -------------------------------------------------------------- Critical Store
5646:
5647: sub cstore {
5648: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
5649: my $home='';
5650:
5651: if ($stuname) { $home=&homeserver($stuname,$domain); }
5652:
5653: $symb=&symbclean($symb);
5654: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
5655:
5656: if (!$domain) { $domain=$env{'user.domain'}; }
5657: if (!$stuname) { $stuname=$env{'user.name'}; }
5658:
5659: &devalidate($symb,$stuname,$domain);
5660:
5661: $symb=escape($symb);
5662: if (!$namespace) {
5663: unless ($namespace=$env{'request.course.id'}) {
5664: return '';
5665: }
5666: }
5667: if (!$home) { $home=$env{'user.home'}; }
5668:
5669: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5670: $$storehash{'host'}=$perlvar{'lonHostID'};
5671:
5672: my $namevalue='';
5673: foreach my $key (keys(%$storehash)) {
5674: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
5675: }
5676: $namevalue=~s/\&$//;
5677: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
5678: return critical
5679: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
5680: }
5681:
5682: # --------------------------------------------------------------------- Restore
5683:
5684: sub restore {
5685: my ($symb,$namespace,$domain,$stuname) = @_;
5686: my $home='';
5687:
5688: if ($stuname) { $home=&homeserver($stuname,$domain); }
5689:
5690: if (!$symb) {
5691: return if ($namespace eq 'courserequests');
5692: unless ($symb=escape(&symbread())) { return ''; }
5693: } else {
5694: unless ($namespace eq 'courserequests') {
5695: $symb=&escape(&symbclean($symb));
5696: }
5697: }
5698: if (!$namespace) {
5699: unless ($namespace=$env{'request.course.id'}) {
5700: return '';
5701: }
5702: }
5703: if (!$domain) { $domain=$env{'user.domain'}; }
5704: if (!$stuname) { $stuname=$env{'user.name'}; }
5705: if (!$home) { $home=$env{'user.home'}; }
5706: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
5707:
5708: my %returnhash=();
5709: foreach my $line (split(/\&/,$answer)) {
5710: my ($name,$value)=split(/\=/,$line);
5711: $returnhash{&unescape($name)}=&thaw_unescape($value);
5712: }
5713: my $version;
5714: for ($version=1;$version<=$returnhash{'version'};$version++) {
5715: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
5716: $returnhash{$item}=$returnhash{$version.':'.$item};
5717: }
5718: }
5719: return %returnhash;
5720: }
5721:
5722: # ---------------------------------------------------------- Course Description
5723: #
5724: #
5725:
5726: sub coursedescription {
5727: my ($courseid,$args)=@_;
5728: $courseid=~s/^\///;
5729: $courseid=~s/\_/\//g;
5730: my ($cdomain,$cnum)=split(/\//,$courseid);
5731: my $chome=&homeserver($cnum,$cdomain);
5732: my $normalid=$cdomain.'_'.$cnum;
5733: # need to always cache even if we get errors otherwise we keep
5734: # trying and trying and trying to get the course description.
5735: my %envhash=();
5736: my %returnhash=();
5737:
5738: my $expiretime=600;
5739: if ($env{'request.course.id'} eq $normalid) {
5740: $expiretime=120;
5741: }
5742:
5743: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
5744: if (!$args->{'freshen_cache'}
5745: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
5746: foreach my $key (keys(%env)) {
5747: next if ($key !~ /^\Q$prefix\E(.*)/);
5748: my ($setting) = $1;
5749: $returnhash{$setting} = $env{$key};
5750: }
5751: return %returnhash;
5752: }
5753:
5754: # get the data again
5755:
5756: if (!$args->{'one_time'}) {
5757: $envhash{'course.'.$normalid.'.last_cache'}=time;
5758: }
5759:
5760: if ($chome ne 'no_host') {
5761: %returnhash=&dump('environment',$cdomain,$cnum);
5762: if (!exists($returnhash{'con_lost'})) {
5763: my $username = $env{'user.name'}; # Defult username
5764: if(defined $args->{'user'}) {
5765: $username = $args->{'user'};
5766: }
5767: $returnhash{'home'}= $chome;
5768: $returnhash{'domain'} = $cdomain;
5769: $returnhash{'num'} = $cnum;
5770: if (!defined($returnhash{'type'})) {
5771: $returnhash{'type'} = 'Course';
5772: }
5773: while (my ($name,$value) = each %returnhash) {
5774: $envhash{'course.'.$normalid.'.'.$name}=$value;
5775: }
5776: $returnhash{'url'}=&clutter($returnhash{'url'});
5777: $returnhash{'fn'}=LONCAPA::tempdir() .
5778: $username.'_'.$cdomain.'_'.$cnum;
5779: $envhash{'course.'.$normalid.'.home'}=$chome;
5780: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
5781: $envhash{'course.'.$normalid.'.num'}=$cnum;
5782: }
5783: }
5784: if (!$args->{'one_time'}) {
5785: &appenv(\%envhash);
5786: }
5787: return %returnhash;
5788: }
5789:
5790: sub update_released_required {
5791: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
5792: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
5793: $cid = $env{'request.course.id'};
5794: $cdom = $env{'course.'.$cid.'.domain'};
5795: $cnum = $env{'course.'.$cid.'.num'};
5796: $chome = $env{'course.'.$cid.'.home'};
5797: }
5798: if ($needsrelease) {
5799: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
5800: my $needsupdate;
5801: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
5802: $needsupdate = 1;
5803: } else {
5804: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
5805: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
5806: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
5807: $needsupdate = 1;
5808: }
5809: }
5810: if ($needsupdate) {
5811: my %needshash = (
5812: 'internal.releaserequired' => $needsrelease,
5813: );
5814: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
5815: if ($putresult eq 'ok') {
5816: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
5817: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
5818: if (ref($crsinfo{$cid}) eq 'HASH') {
5819: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
5820: &courseidput($cdom,\%crsinfo,$chome,'notime');
5821: }
5822: }
5823: }
5824: }
5825: return;
5826: }
5827:
5828: # -------------------------------------------------See if a user is privileged
5829:
5830: sub privileged {
5831: my ($username,$domain,$possdomains,$possroles)=@_;
5832: my $now = time;
5833: my $roles;
5834: if (ref($possroles) eq 'ARRAY') {
5835: $roles = $possroles;
5836: } else {
5837: $roles = ['dc','su'];
5838: }
5839: if (ref($possdomains) eq 'ARRAY') {
5840: my %privileged = &privileged_by_domain($possdomains,$roles);
5841: foreach my $dom (@{$possdomains}) {
5842: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
5843: (ref($privileged{$dom}) eq 'HASH')) {
5844: foreach my $role (@{$roles}) {
5845: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5846: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
5847: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
5848: return 1 unless (($end && $end < $now) ||
5849: ($start && $start > $now));
5850: }
5851: }
5852: }
5853: }
5854: }
5855: } else {
5856: my %rolesdump = &dump("roles", $domain, $username) or return 0;
5857: my $now = time;
5858:
5859: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
5860: my ($trole, $tend, $tstart) = split(/_/, $role);
5861: if (grep(/^\Q$trole\E$/,@{$roles})) {
5862: return 1 unless ($tend && $tend < $now)
5863: or ($tstart && $tstart > $now);
5864: }
5865: }
5866: }
5867: return 0;
5868: }
5869:
5870: sub privileged_by_domain {
5871: my ($domains,$roles) = @_;
5872: my %privileged = ();
5873: my $cachetime = 60*60*24;
5874: my $now = time;
5875: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
5876: return %privileged;
5877: }
5878: foreach my $dom (@{$domains}) {
5879: next if (ref($privileged{$dom}) eq 'HASH');
5880: my $needroles;
5881: foreach my $role (@{$roles}) {
5882: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
5883: if (defined($cached)) {
5884: if (ref($result) eq 'HASH') {
5885: $privileged{$dom}{$role} = $result;
5886: }
5887: } else {
5888: $needroles = 1;
5889: }
5890: }
5891: if ($needroles) {
5892: my %dompersonnel = &get_domain_roles($dom,$roles);
5893: $privileged{$dom} = {};
5894: foreach my $server (keys(%dompersonnel)) {
5895: if (ref($dompersonnel{$server}) eq 'HASH') {
5896: foreach my $item (keys(%{$dompersonnel{$server}})) {
5897: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
5898: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
5899: next if ($end && $end < $now);
5900: $privileged{$dom}{$trole}{$uname.':'.$udom} =
5901: $dompersonnel{$server}{$item};
5902: }
5903: }
5904: }
5905: if (ref($privileged{$dom}) eq 'HASH') {
5906: foreach my $role (@{$roles}) {
5907: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5908: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
5909: } else {
5910: my %hash = ();
5911: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
5912: }
5913: }
5914: }
5915: }
5916: }
5917: return %privileged;
5918: }
5919:
5920: # -------------------------------------------------------- Get user privileges
5921:
5922: sub rolesinit {
5923: my ($domain, $username) = @_;
5924: my %userroles = ('user.login.time' => time);
5925: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
5926:
5927: # firstaccess and timerinterval are related to timed maps/resources.
5928: # also, blocking can be triggered by an activating timer
5929: # it's saved in the user's %env.
5930: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
5931: my %timerinterval = &dump('timerinterval', $domain, $username);
5932: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
5933: %timerintchk, %timerintenv);
5934:
5935: foreach my $key (keys(%firstaccess)) {
5936: my ($cid, $rest) = split(/\0/, $key);
5937: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
5938: }
5939:
5940: foreach my $key (keys(%timerinterval)) {
5941: my ($cid,$rest) = split(/\0/,$key);
5942: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
5943: }
5944:
5945: my %allroles=();
5946: my %allgroups=();
5947:
5948: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
5949: my $role = $rolesdump{$area};
5950: $area =~ s/\_\w\w$//;
5951:
5952: my ($trole, $tend, $tstart, $group_privs);
5953:
5954: if ($role =~ /^cr/) {
5955: # Custom role, defined by a user
5956: # e.g., user.role.cr/msu/smith/mynewrole
5957: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
5958: $trole = $1;
5959: ($tend, $tstart) = split('_', $2);
5960: } else {
5961: $trole = $role;
5962: }
5963: } elsif ($role =~ m|^gr/|) {
5964: # Role of member in a group, defined within a course/community
5965: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
5966: ($trole, $tend, $tstart) = split(/_/, $role);
5967: next if $tstart eq '-1';
5968: ($trole, $group_privs) = split(/\//, $trole);
5969: $group_privs = &unescape($group_privs);
5970: } else {
5971: # Just a normal role, defined in roles.tab
5972: ($trole, $tend, $tstart) = split(/_/,$role);
5973: }
5974:
5975: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
5976: $username);
5977: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
5978:
5979: # role expired or not available yet?
5980: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
5981: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
5982:
5983: next if $area eq '' or $trole eq '';
5984:
5985: my $spec = "$trole.$area";
5986: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
5987:
5988: if ($trole =~ /^cr\//) {
5989: # Custom role, defined by a user
5990: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5991: } elsif ($trole eq 'gr') {
5992: # Role of a member in a group, defined within a course/community
5993: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
5994: next;
5995: } else {
5996: # Normal role, defined in roles.tab
5997: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5998: }
5999:
6000: my $cid = $tdomain.'_'.$trest;
6001: unless ($firstaccchk{$cid}) {
6002: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
6003: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
6004: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
6005: $coursetimerstarts{$cid}{$item};
6006: }
6007: }
6008: $firstaccchk{$cid} = 1;
6009: }
6010: unless ($timerintchk{$cid}) {
6011: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
6012: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
6013: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
6014: $coursetimerintervals{$cid}{$item};
6015: }
6016: }
6017: $timerintchk{$cid} = 1;
6018: }
6019: }
6020:
6021: @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
6022: \%allroles, \%allgroups);
6023: $env{'user.adv'} = $userroles{'user.adv'};
6024: $env{'user.rar'} = $userroles{'user.rar'};
6025:
6026: return (\%userroles,\%firstaccenv,\%timerintenv);
6027: }
6028:
6029: sub set_arearole {
6030: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
6031: unless ($nolog) {
6032: # log the associated role with the area
6033: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
6034: }
6035: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
6036: }
6037:
6038: sub custom_roleprivs {
6039: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
6040: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
6041: my $homsvr = &homeserver($rauthor,$rdomain);
6042: if (&hostname($homsvr) ne '') {
6043: my ($rdummy,$roledef)=
6044: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
6045: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
6046: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
6047: if (defined($syspriv)) {
6048: if ($trest =~ /^$match_community$/) {
6049: $syspriv =~ s/bre\&S//;
6050: }
6051: $$allroles{'cm./'}.=':'.$syspriv;
6052: $$allroles{$spec.'./'}.=':'.$syspriv;
6053: }
6054: if ($tdomain ne '') {
6055: if (defined($dompriv)) {
6056: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
6057: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
6058: }
6059: if (($trest ne '') && (defined($coursepriv))) {
6060: if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
6061: my $rolename = $1;
6062: $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
6063: }
6064: $$allroles{'cm.'.$area}.=':'.$coursepriv;
6065: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
6066: }
6067: }
6068: }
6069: }
6070: }
6071:
6072: sub course_adhocrole_privs {
6073: my ($rolename,$cdom,$cnum,$coursepriv) = @_;
6074: my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
6075: if ($overrides{"internal.adhocpriv.$rolename"}) {
6076: my (%currprivs,%storeprivs);
6077: foreach my $item (split(/:/,$coursepriv)) {
6078: my ($priv,$restrict) = split(/\&/,$item);
6079: $currprivs{$priv} = $restrict;
6080: }
6081: my (%possadd,%possremove,%full);
6082: foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
6083: my ($priv,$restrict)=split(/\&/,$item);
6084: $full{$priv} = $restrict;
6085: }
6086: foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
6087: next if ($item eq '');
6088: my ($rule,$rest) = split(/=/,$item);
6089: next unless (($rule eq 'off') || ($rule eq 'on'));
6090: foreach my $priv (split(/:/,$rest)) {
6091: if ($priv ne '') {
6092: if ($rule eq 'off') {
6093: $possremove{$priv} = 1;
6094: } else {
6095: $possadd{$priv} = 1;
6096: }
6097: }
6098: }
6099: }
6100: foreach my $priv (sort(keys(%full))) {
6101: if (exists($currprivs{$priv})) {
6102: unless (exists($possremove{$priv})) {
6103: $storeprivs{$priv} = $currprivs{$priv};
6104: }
6105: } elsif (exists($possadd{$priv})) {
6106: $storeprivs{$priv} = $full{$priv};
6107: }
6108: }
6109: $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
6110: }
6111: return $coursepriv;
6112: }
6113:
6114: sub group_roleprivs {
6115: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
6116: my $access = 1;
6117: my $now = time;
6118: if (($tend!=0) && ($tend<$now)) { $access = 0; }
6119: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
6120: if ($access) {
6121: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
6122: $$allgroups{$course}{$group} .=':'.$group_privs;
6123: }
6124: }
6125:
6126: sub standard_roleprivs {
6127: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
6128: if (defined($pr{$trole.':s'})) {
6129: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
6130: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
6131: }
6132: if ($tdomain ne '') {
6133: if (defined($pr{$trole.':d'})) {
6134: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
6135: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
6136: }
6137: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
6138: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
6139: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
6140: }
6141: }
6142: }
6143:
6144: sub set_userprivs {
6145: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
6146: my $author=0;
6147: my $adv=0;
6148: my $rar=0;
6149: my %grouproles = ();
6150: if (keys(%{$allgroups}) > 0) {
6151: my @groupkeys;
6152: foreach my $role (keys(%{$allroles})) {
6153: push(@groupkeys,$role);
6154: }
6155: if (ref($groups_roles) eq 'HASH') {
6156: foreach my $key (keys(%{$groups_roles})) {
6157: unless (grep(/^\Q$key\E$/,@groupkeys)) {
6158: push(@groupkeys,$key);
6159: }
6160: }
6161: }
6162: if (@groupkeys > 0) {
6163: foreach my $role (@groupkeys) {
6164: my ($trole,$area,$sec,$extendedarea);
6165: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
6166: $trole = $1;
6167: $area = $2;
6168: $sec = $3;
6169: $extendedarea = $area.$sec;
6170: if (exists($$allgroups{$area})) {
6171: foreach my $group (keys(%{$$allgroups{$area}})) {
6172: my $spec = $trole.'.'.$extendedarea;
6173: $grouproles{$spec.'.'.$area.'/'.$group} =
6174: $$allgroups{$area}{$group};
6175: }
6176: }
6177: }
6178: }
6179: }
6180: }
6181: foreach my $group (keys(%grouproles)) {
6182: $$allroles{$group} = $grouproles{$group};
6183: }
6184: foreach my $role (keys(%{$allroles})) {
6185: my %thesepriv;
6186: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
6187: foreach my $item (split(/:/,$$allroles{$role})) {
6188: if ($item ne '') {
6189: my ($privilege,$restrictions)=split(/&/,$item);
6190: if ($restrictions eq '') {
6191: $thesepriv{$privilege}='F';
6192: } elsif ($thesepriv{$privilege} ne 'F') {
6193: $thesepriv{$privilege}.=$restrictions;
6194: }
6195: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
6196: if ($thesepriv{'rar'} eq 'F') { $rar=1; }
6197: }
6198: }
6199: my $thesestr='';
6200: foreach my $priv (sort(keys(%thesepriv))) {
6201: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
6202: }
6203: $userroles->{'user.priv.'.$role} = $thesestr;
6204: }
6205: return ($author,$adv,$rar);
6206: }
6207:
6208: sub role_status {
6209: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
6210: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
6211: my ($one,$two) = split(m{\./},$rolekey,2);
6212: (undef,undef,$$role) = split(/\./,$one,3);
6213: unless (!defined($$role) || $$role eq '') {
6214: $$where = '/'.$two;
6215: $$trolecode=$$role.'.'.$$where;
6216: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
6217: $$tstatus='is';
6218: if ($$tstart && $$tstart>$update) {
6219: $$tstatus='future';
6220: if ($$tstart<$now) {
6221: if ($$tstart && $$tstart>$refresh) {
6222: if (($$where ne '') && ($$role ne '')) {
6223: my (%allroles,%allgroups,$group_privs,
6224: %groups_roles,@rolecodes);
6225: my %userroles = (
6226: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
6227: );
6228: @rolecodes = ('cm');
6229: my $spec=$$role.'.'.$$where;
6230: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
6231: if ($$role =~ /^cr\//) {
6232: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
6233: push(@rolecodes,'cr');
6234: } elsif ($$role eq 'gr') {
6235: push(@rolecodes,$$role);
6236: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
6237: $env{'user.name'});
6238: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
6239: (undef,my $group_privs) = split(/\//,$trole);
6240: $group_privs = &unescape($group_privs);
6241: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
6242: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
6243: &get_groups_roles($tdomain,$trest,
6244: \%course_roles,\@rolecodes,
6245: \%groups_roles);
6246: } else {
6247: push(@rolecodes,$$role);
6248: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
6249: }
6250: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
6251: \%groups_roles);
6252: &appenv(\%userroles,\@rolecodes);
6253: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
6254: }
6255: }
6256: $$tstatus = 'is';
6257: }
6258: }
6259: if ($$tend) {
6260: if ($$tend<$update) {
6261: $$tstatus='expired';
6262: } elsif ($$tend<$now) {
6263: $$tstatus='will_not';
6264: }
6265: }
6266: }
6267: }
6268: }
6269:
6270: sub get_groups_roles {
6271: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
6272: return unless((ref($cdom_courseroles) eq 'HASH') &&
6273: (ref($rolecodes) eq 'ARRAY') &&
6274: (ref($groups_roles) eq 'HASH'));
6275: if (keys(%{$cdom_courseroles}) > 0) {
6276: my ($cnum) = ($rest =~ /^($match_courseid)/);
6277: if ($cdom ne '' && $cnum ne '') {
6278: foreach my $key (keys(%{$cdom_courseroles})) {
6279: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
6280: my $crsrole = $1;
6281: my $crssec = $2;
6282: if ($crsrole =~ /^cr/) {
6283: unless (grep(/^cr$/,@{$rolecodes})) {
6284: push(@{$rolecodes},'cr');
6285: }
6286: } else {
6287: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
6288: push(@{$rolecodes},$crsrole);
6289: }
6290: }
6291: my $rolekey = "$crsrole./$cdom/$cnum";
6292: if ($crssec ne '') {
6293: $rolekey .= "/$crssec";
6294: }
6295: $rolekey .= './';
6296: $groups_roles->{$rolekey} = $rolecodes;
6297: }
6298: }
6299: }
6300: }
6301: return;
6302: }
6303:
6304: sub delete_env_groupprivs {
6305: my ($where,$courseroles,$possroles) = @_;
6306: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
6307: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
6308: unless (ref($courseroles->{$udom}) eq 'HASH') {
6309: %{$courseroles->{$udom}} =
6310: &get_my_roles('','','userroles',['active'],
6311: $possroles,[$udom],1);
6312: }
6313: if (ref($courseroles->{$udom}) eq 'HASH') {
6314: foreach my $item (keys(%{$courseroles->{$udom}})) {
6315: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
6316: my $area = '/'.$cdom.'/'.$cnum;
6317: my $privkey = "user.priv.$crsrole.$area";
6318: if ($crssec ne '') {
6319: $privkey .= '/'.$crssec;
6320: }
6321: $privkey .= ".$area/$group";
6322: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
6323: }
6324: }
6325: return;
6326: }
6327:
6328: sub check_adhoc_privs {
6329: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
6330: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
6331: if ($sec) {
6332: $cckey .= '/'.$sec;
6333: }
6334: my $setprivs;
6335: if ($env{$cckey}) {
6336: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
6337: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
6338: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
6339: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
6340: $setprivs = 1;
6341: }
6342: } else {
6343: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
6344: $setprivs = 1;
6345: }
6346: return $setprivs;
6347: }
6348:
6349: sub set_adhoc_privileges {
6350: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
6351: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
6352: my $area = '/'.$dcdom.'/'.$pickedcourse;
6353: if ($sec ne '') {
6354: $area .= '/'.$sec;
6355: }
6356: my $spec = $role.'.'.$area;
6357: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
6358: $env{'user.name'},1);
6359: my %rolehash = ();
6360: if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
6361: my $rolename = $1;
6362: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
6363: my %domdef = &get_domain_defaults($dcdom);
6364: if (ref($domdef{'adhocroles'}) eq 'HASH') {
6365: if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
6366: &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
6367: }
6368: }
6369: } else {
6370: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
6371: }
6372: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
6373: &appenv(\%userroles,[$role,'cm']);
6374: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
6375: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
6376: &appenv( {'request.role' => $spec,
6377: 'request.role.domain' => $dcdom,
6378: 'request.course.sec' => $sec,
6379: }
6380: );
6381: my $tadv=0;
6382: if (&allowed('adv') eq 'F') { $tadv=1; }
6383: &appenv({'request.role.adv' => $tadv});
6384: }
6385: }
6386:
6387: # --------------------------------------------------------------- get interface
6388:
6389: sub get {
6390: my ($namespace,$storearr,$udomain,$uname)=@_;
6391: my $items='';
6392: foreach my $item (@$storearr) {
6393: $items.=&escape($item).'&';
6394: }
6395: $items=~s/\&$//;
6396: if (!$udomain) { $udomain=$env{'user.domain'}; }
6397: if (!$uname) { $uname=$env{'user.name'}; }
6398: my $uhome=&homeserver($uname,$udomain);
6399:
6400: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
6401: my @pairs=split(/\&/,$rep);
6402: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
6403: return @pairs;
6404: }
6405: my %returnhash=();
6406: my $i=0;
6407: foreach my $item (@$storearr) {
6408: $returnhash{$item}=&thaw_unescape($pairs[$i]);
6409: $i++;
6410: }
6411: return %returnhash;
6412: }
6413:
6414: # --------------------------------------------------------------- del interface
6415:
6416: sub del {
6417: my ($namespace,$storearr,$udomain,$uname)=@_;
6418: my $items='';
6419: foreach my $item (@$storearr) {
6420: $items.=&escape($item).'&';
6421: }
6422:
6423: $items=~s/\&$//;
6424: if (!$udomain) { $udomain=$env{'user.domain'}; }
6425: if (!$uname) { $uname=$env{'user.name'}; }
6426: my $uhome=&homeserver($uname,$udomain);
6427: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
6428: }
6429:
6430: # -------------------------------------------------------------- dump interface
6431:
6432: sub unserialize {
6433: my ($rep, $escapedkeys) = @_;
6434:
6435: return {} if $rep =~ /^error/;
6436:
6437: my %returnhash=();
6438: foreach my $item (split(/\&/,$rep)) {
6439: my ($key, $value) = split(/=/, $item, 2);
6440: $key = unescape($key) unless $escapedkeys;
6441: next if $key =~ /^error: 2 /;
6442: $returnhash{$key} = &thaw_unescape($value);
6443: }
6444: #return %returnhash;
6445: return \%returnhash;
6446: }
6447:
6448: # see Lond::dump_with_regexp
6449: # if $escapedkeys hash keys won't get unescaped.
6450: sub dump {
6451: my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
6452: if (!$udomain) { $udomain=$env{'user.domain'}; }
6453: if (!$uname) { $uname=$env{'user.name'}; }
6454: my $uhome=&homeserver($uname,$udomain);
6455:
6456: if ($regexp) {
6457: $regexp=&escape($regexp);
6458: } else {
6459: $regexp='.';
6460: }
6461: if (grep { $_ eq $uhome } current_machine_ids()) {
6462: # user is hosted on this machine
6463: my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
6464: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
6465: return %{unserialize($reply, $escapedkeys)};
6466: }
6467: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
6468: my @pairs=split(/\&/,$rep);
6469: my %returnhash=();
6470: if (!($rep =~ /^error/ )) {
6471: foreach my $item (@pairs) {
6472: my ($key,$value)=split(/=/,$item,2);
6473: $key = unescape($key) unless $escapedkeys;
6474: #$key = &unescape($key);
6475: next if ($key =~ /^error: 2 /);
6476: $returnhash{$key}=&thaw_unescape($value);
6477: }
6478: }
6479: return %returnhash;
6480: }
6481:
6482:
6483: # --------------------------------------------------------- dumpstore interface
6484:
6485: sub dumpstore {
6486: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
6487: # same as dump but keys must be escaped. They may contain colon separated
6488: # lists of values that may themself contain colons (e.g. symbs).
6489: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
6490: }
6491:
6492: # -------------------------------------------------------------- keys interface
6493:
6494: sub getkeys {
6495: my ($namespace,$udomain,$uname)=@_;
6496: if (!$udomain) { $udomain=$env{'user.domain'}; }
6497: if (!$uname) { $uname=$env{'user.name'}; }
6498: my $uhome=&homeserver($uname,$udomain);
6499: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
6500: my @keyarray=();
6501: foreach my $key (split(/\&/,$rep)) {
6502: next if ($key =~ /^error: 2 /);
6503: push(@keyarray,&unescape($key));
6504: }
6505: return @keyarray;
6506: }
6507:
6508: # --------------------------------------------------------------- currentdump
6509: sub currentdump {
6510: my ($courseid,$sdom,$sname)=@_;
6511: $courseid = $env{'request.course.id'} if (! defined($courseid));
6512: $sdom = $env{'user.domain'} if (! defined($sdom));
6513: $sname = $env{'user.name'} if (! defined($sname));
6514: my $uhome = &homeserver($sname,$sdom);
6515: my $rep;
6516:
6517: if (grep { $_ eq $uhome } current_machine_ids()) {
6518: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
6519: $courseid)));
6520: } else {
6521: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
6522: }
6523:
6524: return if ($rep =~ /^(error:|no_such_host)/);
6525: #
6526: my %returnhash=();
6527: #
6528: if ($rep eq 'unknown_cmd') {
6529: # an old lond will not know currentdump
6530: # Do a dump and make it look like a currentdump
6531: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
6532: return if ($tmp[0] =~ /^(error:|no_such_host)/);
6533: my %hash = @tmp;
6534: @tmp=();
6535: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
6536: } else {
6537: my @pairs=split(/\&/,$rep);
6538: foreach my $pair (@pairs) {
6539: my ($key,$value)=split(/=/,$pair,2);
6540: my ($symb,$param) = split(/:/,$key);
6541: $returnhash{&unescape($symb)}->{&unescape($param)} =
6542: &thaw_unescape($value);
6543: }
6544: }
6545: return %returnhash;
6546: }
6547:
6548: sub convert_dump_to_currentdump{
6549: my %hash = %{shift()};
6550: my %returnhash;
6551: # Code ripped from lond, essentially. The only difference
6552: # here is the unescaping done by lonnet::dump(). Conceivably
6553: # we might run in to problems with parameter names =~ /^v\./
6554: while (my ($key,$value) = each(%hash)) {
6555: my ($v,$symb,$param) = split(/:/,$key);
6556: $symb = &unescape($symb);
6557: $param = &unescape($param);
6558: next if ($v eq 'version' || $symb eq 'keys');
6559: next if (exists($returnhash{$symb}) &&
6560: exists($returnhash{$symb}->{$param}) &&
6561: $returnhash{$symb}->{'v.'.$param} > $v);
6562: $returnhash{$symb}->{$param}=$value;
6563: $returnhash{$symb}->{'v.'.$param}=$v;
6564: }
6565: #
6566: # Remove all of the keys in the hashes which keep track of
6567: # the version of the parameter.
6568: while (my ($symb,$param_hash) = each(%returnhash)) {
6569: # use a foreach because we are going to delete from the hash.
6570: foreach my $key (keys(%$param_hash)) {
6571: delete($param_hash->{$key}) if ($key =~ /^v\./);
6572: }
6573: }
6574: return \%returnhash;
6575: }
6576:
6577: # ------------------------------------------------------ critical inc interface
6578:
6579: sub cinc {
6580: return &inc(@_,'critical');
6581: }
6582:
6583: # --------------------------------------------------------------- inc interface
6584:
6585: sub inc {
6586: my ($namespace,$store,$udomain,$uname,$critical) = @_;
6587: if (!$udomain) { $udomain=$env{'user.domain'}; }
6588: if (!$uname) { $uname=$env{'user.name'}; }
6589: my $uhome=&homeserver($uname,$udomain);
6590: my $items='';
6591: if (! ref($store)) {
6592: # got a single value, so use that instead
6593: $items = &escape($store).'=&';
6594: } elsif (ref($store) eq 'SCALAR') {
6595: $items = &escape($$store).'=&';
6596: } elsif (ref($store) eq 'ARRAY') {
6597: $items = join('=&',map {&escape($_);} @{$store});
6598: } elsif (ref($store) eq 'HASH') {
6599: while (my($key,$value) = each(%{$store})) {
6600: $items.= &escape($key).'='.&escape($value).'&';
6601: }
6602: }
6603: $items=~s/\&$//;
6604: if ($critical) {
6605: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
6606: } else {
6607: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
6608: }
6609: }
6610:
6611: # --------------------------------------------------------------- put interface
6612:
6613: sub put {
6614: my ($namespace,$storehash,$udomain,$uname)=@_;
6615: if (!$udomain) { $udomain=$env{'user.domain'}; }
6616: if (!$uname) { $uname=$env{'user.name'}; }
6617: my $uhome=&homeserver($uname,$udomain);
6618: my $items='';
6619: foreach my $item (keys(%$storehash)) {
6620: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
6621: }
6622: $items=~s/\&$//;
6623: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
6624: }
6625:
6626: # ------------------------------------------------------------ newput interface
6627:
6628: sub newput {
6629: my ($namespace,$storehash,$udomain,$uname)=@_;
6630: if (!$udomain) { $udomain=$env{'user.domain'}; }
6631: if (!$uname) { $uname=$env{'user.name'}; }
6632: my $uhome=&homeserver($uname,$udomain);
6633: my $items='';
6634: foreach my $key (keys(%$storehash)) {
6635: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
6636: }
6637: $items=~s/\&$//;
6638: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
6639: }
6640:
6641: # --------------------------------------------------------- putstore interface
6642:
6643: sub putstore {
6644: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
6645: if (!$udomain) { $udomain=$env{'user.domain'}; }
6646: if (!$uname) { $uname=$env{'user.name'}; }
6647: my $uhome=&homeserver($uname,$udomain);
6648: my $items='';
6649: foreach my $key (keys(%$storehash)) {
6650: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
6651: }
6652: $items=~s/\&$//;
6653: my $esc_symb=&escape($symb);
6654: my $esc_v=&escape($version);
6655: my $reply =
6656: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
6657: $uhome);
6658: if (($tolog) && ($reply eq 'ok')) {
6659: my $namevalue='';
6660: foreach my $key (keys(%{$storehash})) {
6661: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
6662: }
6663: $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
6664: '&host='.&escape($perlvar{'lonHostID'}).
6665: '&version='.$esc_v.
6666: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
6667: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
6668: }
6669: if ($reply eq 'unknown_cmd') {
6670: # gfall back to way things use to be done
6671: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
6672: $uname);
6673: }
6674: return $reply;
6675: }
6676:
6677: sub old_putstore {
6678: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
6679: if (!$udomain) { $udomain=$env{'user.domain'}; }
6680: if (!$uname) { $uname=$env{'user.name'}; }
6681: my $uhome=&homeserver($uname,$udomain);
6682: my %newstorehash;
6683: foreach my $item (keys(%$storehash)) {
6684: my $key = $version.':'.&escape($symb).':'.$item;
6685: $newstorehash{$key} = $storehash->{$item};
6686: }
6687: my $items='';
6688: my %allitems = ();
6689: foreach my $item (keys(%newstorehash)) {
6690: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
6691: my $key = $1.':keys:'.$2;
6692: $allitems{$key} .= $3.':';
6693: }
6694: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
6695: }
6696: foreach my $item (keys(%allitems)) {
6697: $allitems{$item} =~ s/\:$//;
6698: $items.= $item.'='.$allitems{$item}.'&';
6699: }
6700: $items=~s/\&$//;
6701: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
6702: }
6703:
6704: # ------------------------------------------------------ critical put interface
6705:
6706: sub cput {
6707: my ($namespace,$storehash,$udomain,$uname)=@_;
6708: if (!$udomain) { $udomain=$env{'user.domain'}; }
6709: if (!$uname) { $uname=$env{'user.name'}; }
6710: my $uhome=&homeserver($uname,$udomain);
6711: my $items='';
6712: foreach my $item (keys(%$storehash)) {
6713: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
6714: }
6715: $items=~s/\&$//;
6716: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
6717: }
6718:
6719: # -------------------------------------------------------------- eget interface
6720:
6721: sub eget {
6722: my ($namespace,$storearr,$udomain,$uname)=@_;
6723: my $items='';
6724: foreach my $item (@$storearr) {
6725: $items.=&escape($item).'&';
6726: }
6727: $items=~s/\&$//;
6728: if (!$udomain) { $udomain=$env{'user.domain'}; }
6729: if (!$uname) { $uname=$env{'user.name'}; }
6730: my $uhome=&homeserver($uname,$udomain);
6731: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
6732: my @pairs=split(/\&/,$rep);
6733: my %returnhash=();
6734: my $i=0;
6735: foreach my $item (@$storearr) {
6736: $returnhash{$item}=&thaw_unescape($pairs[$i]);
6737: $i++;
6738: }
6739: return %returnhash;
6740: }
6741:
6742: # ------------------------------------------------------------ tmpput interface
6743: sub tmpput {
6744: my ($storehash,$server,$context)=@_;
6745: my $items='';
6746: foreach my $item (keys(%$storehash)) {
6747: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
6748: }
6749: $items=~s/\&$//;
6750: if (defined($context)) {
6751: $items .= ':'.&escape($context);
6752: }
6753: return &reply("tmpput:$items",$server);
6754: }
6755:
6756: # ------------------------------------------------------------ tmpget interface
6757: sub tmpget {
6758: my ($token,$server)=@_;
6759: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6760: my $rep=&reply("tmpget:$token",$server);
6761: my %returnhash;
6762: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
6763: return %returnhash;
6764: }
6765: foreach my $item (split(/\&/,$rep)) {
6766: my ($key,$value)=split(/=/,$item);
6767: $returnhash{&unescape($key)}=&thaw_unescape($value);
6768: }
6769: return %returnhash;
6770: }
6771:
6772: # ------------------------------------------------------------ tmpdel interface
6773: sub tmpdel {
6774: my ($token,$server)=@_;
6775: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6776: return &reply("tmpdel:$token",$server);
6777: }
6778:
6779: # ------------------------------------------------------------ get_timebased_id
6780:
6781: sub get_timebased_id {
6782: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
6783: $maxtries) = @_;
6784: my ($newid,$error,$dellock);
6785: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
6786: return ('','ok','invalid call to get suffix');
6787: }
6788:
6789: # set defaults for any optional args for which values were not supplied
6790: if ($who eq '') {
6791: $who = $env{'user.name'}.':'.$env{'user.domain'};
6792: }
6793: if (!$locktries) {
6794: $locktries = 3;
6795: }
6796: if (!$maxtries) {
6797: $maxtries = 10;
6798: }
6799:
6800: if (($cdom eq '') || ($cnum eq '')) {
6801: if ($env{'request.course.id'}) {
6802: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
6803: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
6804: }
6805: if (($cdom eq '') || ($cnum eq '')) {
6806: return ('','ok','call to get suffix not in course context');
6807: }
6808: }
6809:
6810: # construct locking item
6811: my $lockhash = {
6812: $prefix."\0".'locked_'.$keyid => $who,
6813: };
6814: my $tries = 0;
6815:
6816: # attempt to get lock on nohist_$namespace file
6817: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6818: while (($gotlock ne 'ok') && $tries <$locktries) {
6819: $tries ++;
6820: sleep 1;
6821: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6822: }
6823:
6824: # attempt to get unique identifier, based on current timestamp
6825: if ($gotlock eq 'ok') {
6826: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
6827: my $id = time;
6828: $newid = $id;
6829: if ($idtype eq 'addcode') {
6830: $newid .= &sixnum_code();
6831: }
6832: my $idtries = 0;
6833: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
6834: if ($idtype eq 'concat') {
6835: $newid = $id.$idtries;
6836: } elsif ($idtype eq 'addcode') {
6837: $newid = $newid.&sixnum_code();
6838: } else {
6839: $newid ++;
6840: }
6841: $idtries ++;
6842: }
6843: if (!exists($inuse{$prefix."\0".$newid})) {
6844: my %new_item = (
6845: $prefix."\0".$newid => $who,
6846: );
6847: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
6848: $cdom,$cnum);
6849: if ($putresult ne 'ok') {
6850: undef($newid);
6851: $error = 'error saving new item: '.$putresult;
6852: }
6853: } else {
6854: undef($newid);
6855: $error = ('error: no unique suffix available for the new item ');
6856: }
6857: # remove lock
6858: my @del_lock = ($prefix."\0".'locked_'.$keyid);
6859: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
6860: } else {
6861: $error = "error: could not obtain lockfile\n";
6862: $dellock = 'ok';
6863: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
6864: $dellock = 'nolock';
6865: }
6866: }
6867: return ($newid,$dellock,$error);
6868: }
6869:
6870: sub sixnum_code {
6871: my $code;
6872: for (0..6) {
6873: $code .= int( rand(9) );
6874: }
6875: return $code;
6876: }
6877:
6878: # -------------------------------------------------- portfolio access checking
6879:
6880: sub portfolio_access {
6881: my ($requrl,$clientip) = @_;
6882: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
6883: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
6884: if ($result) {
6885: my %setters;
6886: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6887: my ($startblock,$endblock) =
6888: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
6889: if ($startblock && $endblock) {
6890: return 'B';
6891: }
6892: } else {
6893: my ($startblock,$endblock) =
6894: &Apache::loncommon::blockcheck(\%setters,'port');
6895: if ($startblock && $endblock) {
6896: return 'B';
6897: }
6898: }
6899: }
6900: if ($result eq 'ok') {
6901: return 'F';
6902: } elsif ($result =~ /^[^:]+:guest_/) {
6903: return 'A';
6904: }
6905: return '';
6906: }
6907:
6908: sub get_portfolio_access {
6909: my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
6910:
6911: if (!ref($access_hash)) {
6912: my $current_perms = &get_portfile_permissions($udom,$unum);
6913: my %access_controls = &get_access_controls($current_perms,$group,
6914: $file_name);
6915: $access_hash = $access_controls{$file_name};
6916: }
6917:
6918: my ($public,$guest,@domains,@users,@courses,@groups,@ips);
6919: my $now = time;
6920: if (ref($access_hash) eq 'HASH') {
6921: foreach my $key (keys(%{$access_hash})) {
6922: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
6923: if ($start > $now) {
6924: next;
6925: }
6926: if ($end && $end<$now) {
6927: next;
6928: }
6929: if ($scope eq 'public') {
6930: $public = $key;
6931: last;
6932: } elsif ($scope eq 'guest') {
6933: $guest = $key;
6934: } elsif ($scope eq 'domains') {
6935: push(@domains,$key);
6936: } elsif ($scope eq 'users') {
6937: push(@users,$key);
6938: } elsif ($scope eq 'course') {
6939: push(@courses,$key);
6940: } elsif ($scope eq 'group') {
6941: push(@groups,$key);
6942: } elsif ($scope eq 'ip') {
6943: push(@ips,$key);
6944: }
6945: }
6946: if ($public) {
6947: return 'ok';
6948: } elsif (@ips > 0) {
6949: my $allowed;
6950: foreach my $ipkey (@ips) {
6951: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
6952: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
6953: $allowed = 1;
6954: last;
6955: }
6956: }
6957: }
6958: if ($allowed) {
6959: return 'ok';
6960: }
6961: }
6962: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6963: if ($guest) {
6964: return $guest;
6965: }
6966: } else {
6967: if (@domains > 0) {
6968: foreach my $domkey (@domains) {
6969: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
6970: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
6971: return 'ok';
6972: }
6973: }
6974: }
6975: }
6976: if (@users > 0) {
6977: foreach my $userkey (@users) {
6978: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
6979: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
6980: if (ref($item) eq 'HASH') {
6981: if (($item->{'uname'} eq $env{'user.name'}) &&
6982: ($item->{'udom'} eq $env{'user.domain'})) {
6983: return 'ok';
6984: }
6985: }
6986: }
6987: }
6988: }
6989: }
6990: my %roleshash;
6991: my @courses_and_groups = @courses;
6992: push(@courses_and_groups,@groups);
6993: if (@courses_and_groups > 0) {
6994: my (%allgroups,%allroles);
6995: my ($start,$end,$role,$sec,$group);
6996: foreach my $envkey (%env) {
6997: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
6998: my $cid = $2.'_'.$3;
6999: if ($1 eq 'gr') {
7000: $group = $4;
7001: $allgroups{$cid}{$group} = $env{$envkey};
7002: } else {
7003: if ($4 eq '') {
7004: $sec = 'none';
7005: } else {
7006: $sec = $4;
7007: }
7008: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7009: }
7010: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
7011: my $cid = $2.'_'.$3;
7012: if ($4 eq '') {
7013: $sec = 'none';
7014: } else {
7015: $sec = $4;
7016: }
7017: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7018: }
7019: }
7020: if (keys(%allroles) == 0) {
7021: return;
7022: }
7023: foreach my $key (@courses_and_groups) {
7024: my %content = %{$$access_hash{$key}};
7025: my $cnum = $content{'number'};
7026: my $cdom = $content{'domain'};
7027: my $cid = $cdom.'_'.$cnum;
7028: if (!exists($allroles{$cid})) {
7029: next;
7030: }
7031: foreach my $role_id (keys(%{$content{'roles'}})) {
7032: my @sections = @{$content{'roles'}{$role_id}{'section'}};
7033: my @groups = @{$content{'roles'}{$role_id}{'group'}};
7034: my @status = @{$content{'roles'}{$role_id}{'access'}};
7035: my @roles = @{$content{'roles'}{$role_id}{'role'}};
7036: foreach my $role (keys(%{$allroles{$cid}})) {
7037: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
7038: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
7039: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
7040: if (grep/^all$/,@sections) {
7041: return 'ok';
7042: } else {
7043: if (grep/^$sec$/,@sections) {
7044: return 'ok';
7045: }
7046: }
7047: }
7048: }
7049: if (keys(%{$allgroups{$cid}}) == 0) {
7050: if (grep/^none$/,@groups) {
7051: return 'ok';
7052: }
7053: } else {
7054: if (grep/^all$/,@groups) {
7055: return 'ok';
7056: }
7057: foreach my $group (keys(%{$allgroups{$cid}})) {
7058: if (grep/^$group$/,@groups) {
7059: return 'ok';
7060: }
7061: }
7062: }
7063: }
7064: }
7065: }
7066: }
7067: }
7068: if ($guest) {
7069: return $guest;
7070: }
7071: }
7072: }
7073: return;
7074: }
7075:
7076: sub course_group_datechecker {
7077: my ($dates,$now,$status) = @_;
7078: my ($start,$end) = split(/\./,$dates);
7079: if (!$start && !$end) {
7080: return 'ok';
7081: }
7082: if (grep/^active$/,@{$status}) {
7083: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
7084: return 'ok';
7085: }
7086: }
7087: if (grep/^previous$/,@{$status}) {
7088: if ($end > $now ) {
7089: return 'ok';
7090: }
7091: }
7092: if (grep/^future$/,@{$status}) {
7093: if ($start > $now) {
7094: return 'ok';
7095: }
7096: }
7097: return;
7098: }
7099:
7100: sub parse_portfolio_url {
7101: my ($url) = @_;
7102:
7103: my ($type,$udom,$unum,$group,$file_name);
7104:
7105: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
7106: $type = 1;
7107: $udom = $1;
7108: $unum = $2;
7109: $file_name = $3;
7110: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
7111: $type = 2;
7112: $udom = $1;
7113: $unum = $2;
7114: $group = $3;
7115: $file_name = $3.'/'.$4;
7116: }
7117: if (wantarray) {
7118: return ($type,$udom,$unum,$file_name,$group);
7119: }
7120: return $type;
7121: }
7122:
7123: sub is_portfolio_url {
7124: my ($url) = @_;
7125: return scalar(&parse_portfolio_url($url));
7126: }
7127:
7128: sub is_portfolio_file {
7129: my ($file) = @_;
7130: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
7131: return 1;
7132: }
7133: return;
7134: }
7135:
7136: sub usertools_access {
7137: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
7138: my ($access,%tools);
7139: if ($context eq '') {
7140: $context = 'tools';
7141: }
7142: if ($context eq 'requestcourses') {
7143: %tools = (
7144: official => 1,
7145: unofficial => 1,
7146: community => 1,
7147: textbook => 1,
7148: placement => 1,
7149: );
7150: } elsif ($context eq 'requestauthor') {
7151: %tools = (
7152: requestauthor => 1,
7153: );
7154: } else {
7155: %tools = (
7156: aboutme => 1,
7157: blog => 1,
7158: webdav => 1,
7159: portfolio => 1,
7160: );
7161: }
7162: return if (!defined($tools{$tool}));
7163:
7164: if (($udom eq '') || ($uname eq '')) {
7165: $udom = $env{'user.domain'};
7166: $uname = $env{'user.name'};
7167: }
7168:
7169: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
7170: if ($action ne 'reload') {
7171: if ($context eq 'requestcourses') {
7172: return $env{'environment.canrequest.'.$tool};
7173: } elsif ($context eq 'requestauthor') {
7174: return $env{'environment.canrequest.author'};
7175: } else {
7176: return $env{'environment.availabletools.'.$tool};
7177: }
7178: }
7179: }
7180:
7181: my ($toolstatus,$inststatus,$envkey);
7182: if ($context eq 'requestauthor') {
7183: $envkey = $context;
7184: } else {
7185: $envkey = $context.'.'.$tool;
7186: }
7187:
7188: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
7189: ($action ne 'reload')) {
7190: $toolstatus = $env{'environment.'.$envkey};
7191: $inststatus = $env{'environment.inststatus'};
7192: } else {
7193: if (ref($userenvref) eq 'HASH') {
7194: $toolstatus = $userenvref->{$envkey};
7195: $inststatus = $userenvref->{'inststatus'};
7196: } else {
7197: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
7198: $toolstatus = $userenv{$envkey};
7199: $inststatus = $userenv{'inststatus'};
7200: }
7201: }
7202:
7203: if ($toolstatus ne '') {
7204: if ($toolstatus) {
7205: $access = 1;
7206: } else {
7207: $access = 0;
7208: }
7209: return $access;
7210: }
7211:
7212: my ($is_adv,%domdef);
7213: if (ref($is_advref) eq 'HASH') {
7214: $is_adv = $is_advref->{'is_adv'};
7215: } else {
7216: $is_adv = &is_advanced_user($udom,$uname);
7217: }
7218: if (ref($domdefref) eq 'HASH') {
7219: %domdef = %{$domdefref};
7220: } else {
7221: %domdef = &get_domain_defaults($udom);
7222: }
7223: if (ref($domdef{$tool}) eq 'HASH') {
7224: if ($is_adv) {
7225: if ($domdef{$tool}{'_LC_adv'} ne '') {
7226: if ($domdef{$tool}{'_LC_adv'}) {
7227: $access = 1;
7228: } else {
7229: $access = 0;
7230: }
7231: return $access;
7232: }
7233: }
7234: if ($inststatus ne '') {
7235: my ($hasaccess,$hasnoaccess);
7236: foreach my $affiliation (split(/:/,$inststatus)) {
7237: if ($domdef{$tool}{$affiliation} ne '') {
7238: if ($domdef{$tool}{$affiliation}) {
7239: $hasaccess = 1;
7240: } else {
7241: $hasnoaccess = 1;
7242: }
7243: }
7244: }
7245: if ($hasaccess || $hasnoaccess) {
7246: if ($hasaccess) {
7247: $access = 1;
7248: } elsif ($hasnoaccess) {
7249: $access = 0;
7250: }
7251: return $access;
7252: }
7253: } else {
7254: if ($domdef{$tool}{'default'} ne '') {
7255: if ($domdef{$tool}{'default'}) {
7256: $access = 1;
7257: } elsif ($domdef{$tool}{'default'} == 0) {
7258: $access = 0;
7259: }
7260: return $access;
7261: }
7262: }
7263: } else {
7264: if (($context eq 'tools') && ($tool ne 'webdav')) {
7265: $access = 1;
7266: } else {
7267: $access = 0;
7268: }
7269: return $access;
7270: }
7271: }
7272:
7273: sub is_course_owner {
7274: my ($cdom,$cnum,$udom,$uname) = @_;
7275: if (($udom eq '') || ($uname eq '')) {
7276: $udom = $env{'user.domain'};
7277: $uname = $env{'user.name'};
7278: }
7279: unless (($udom eq '') || ($uname eq '')) {
7280: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
7281: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
7282: return 1;
7283: } else {
7284: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
7285: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
7286: return 1;
7287: }
7288: }
7289: }
7290: }
7291: return;
7292: }
7293:
7294: sub is_advanced_user {
7295: my ($udom,$uname) = @_;
7296: if ($udom ne '' && $uname ne '') {
7297: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
7298: if (wantarray) {
7299: return ($env{'user.adv'},$env{'user.author'});
7300: } else {
7301: return $env{'user.adv'};
7302: }
7303: }
7304: }
7305: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
7306: my %allroles;
7307: my ($is_adv,$is_author);
7308: foreach my $role (keys(%roleshash)) {
7309: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
7310: my $area = '/'.$tdomain.'/'.$trest;
7311: if ($sec ne '') {
7312: $area .= '/'.$sec;
7313: }
7314: if (($area ne '') && ($trole ne '')) {
7315: my $spec=$trole.'.'.$area;
7316: if ($trole =~ /^cr\//) {
7317: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
7318: } elsif ($trole ne 'gr') {
7319: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
7320: }
7321: if ($trole eq 'au') {
7322: $is_author = 1;
7323: }
7324: }
7325: }
7326: foreach my $role (keys(%allroles)) {
7327: last if ($is_adv);
7328: foreach my $item (split(/:/,$allroles{$role})) {
7329: if ($item ne '') {
7330: my ($privilege,$restrictions)=split(/&/,$item);
7331: if ($privilege eq 'adv') {
7332: $is_adv = 1;
7333: last;
7334: }
7335: }
7336: }
7337: }
7338: if (wantarray) {
7339: return ($is_adv,$is_author);
7340: }
7341: return $is_adv;
7342: }
7343:
7344: sub check_can_request {
7345: my ($dom,$can_request,$request_domains) = @_;
7346: my $canreq = 0;
7347: my ($types,$typename) = &Apache::loncommon::course_types();
7348: my @options = ('approval','validate','autolimit');
7349: my $optregex = join('|',@options);
7350: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
7351: foreach my $type (@{$types}) {
7352: if (&usertools_access($env{'user.name'},
7353: $env{'user.domain'},
7354: $type,undef,'requestcourses')) {
7355: $canreq ++;
7356: if (ref($request_domains) eq 'HASH') {
7357: push(@{$request_domains->{$type}},$env{'user.domain'});
7358: }
7359: if ($dom eq $env{'user.domain'}) {
7360: $can_request->{$type} = 1;
7361: }
7362: }
7363: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
7364: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
7365: if (@curr > 0) {
7366: foreach my $item (@curr) {
7367: if (ref($request_domains) eq 'HASH') {
7368: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
7369: if ($otherdom ne '') {
7370: if (ref($request_domains->{$type}) eq 'ARRAY') {
7371: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
7372: push(@{$request_domains->{$type}},$otherdom);
7373: }
7374: } else {
7375: push(@{$request_domains->{$type}},$otherdom);
7376: }
7377: }
7378: }
7379: }
7380: unless($dom eq $env{'user.domain'}) {
7381: $canreq ++;
7382: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
7383: $can_request->{$type} = 1;
7384: }
7385: }
7386: }
7387: }
7388: }
7389: }
7390: return $canreq;
7391: }
7392:
7393: # ---------------------------------------------- Custom access rule evaluation
7394:
7395: sub customaccess {
7396: my ($priv,$uri)=@_;
7397: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
7398: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
7399: $udom = &LONCAPA::clean_domain($udom);
7400: $ucrs = &LONCAPA::clean_username($ucrs);
7401: my $access=0;
7402: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
7403: my ($effect,$realm,$role,$type)=split(/\:/,$right);
7404: if ($type eq 'user') {
7405: foreach my $scope (split(/\s*\,\s*/,$realm)) {
7406: my ($tdom,$tuname)=split(m{/},$scope);
7407: if ($tdom) {
7408: if ($tdom ne $env{'user.domain'}) { next; }
7409: }
7410: if ($tuname) {
7411: if ($tuname ne $env{'user.name'}) { next; }
7412: }
7413: $access=($effect eq 'allow');
7414: last;
7415: }
7416: } else {
7417: if ($role) {
7418: if ($role ne $urole) { next; }
7419: }
7420: foreach my $scope (split(/\s*\,\s*/,$realm)) {
7421: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
7422: if ($tdom) {
7423: if ($tdom ne $udom) { next; }
7424: }
7425: if ($tcrs) {
7426: if ($tcrs ne $ucrs) { next; }
7427: }
7428: if ($tsec) {
7429: if ($tsec ne $usec) { next; }
7430: }
7431: $access=($effect eq 'allow');
7432: last;
7433: }
7434: if ($realm eq '' && $role eq '') {
7435: $access=($effect eq 'allow');
7436: }
7437: }
7438: }
7439: return $access;
7440: }
7441:
7442: # ------------------------------------------------- Check for a user privilege
7443:
7444: sub allowed {
7445: my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
7446: my $ver_orguri=$uri;
7447: $uri=&deversion($uri);
7448: my $orguri=$uri;
7449: $uri=&declutter($uri);
7450:
7451: if ($priv eq 'evb') {
7452: # Evade communication block restrictions for specified role in a course
7453: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
7454: return $1;
7455: } else {
7456: return;
7457: }
7458: }
7459:
7460: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
7461: # Free bre access to adm and meta resources
7462: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$}))
7463: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
7464: && ($priv eq 'bre')) {
7465: return 'F';
7466: }
7467:
7468: # Free bre access to user's own portfolio contents
7469: my ($space,$domain,$name,@dir)=split('/',$uri);
7470: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
7471: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
7472: my %setters;
7473: my ($startblock,$endblock) =
7474: &Apache::loncommon::blockcheck(\%setters,'port');
7475: if ($startblock && $endblock) {
7476: return 'B';
7477: } else {
7478: return 'F';
7479: }
7480: }
7481:
7482: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
7483: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
7484: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
7485: if (exists($env{'request.course.id'})) {
7486: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7487: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7488: if (($domain eq $cdom) && ($name eq $cnum)) {
7489: my $courseprivid=$env{'request.course.id'};
7490: $courseprivid=~s/\_/\//;
7491: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
7492: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
7493: return $1;
7494: } else {
7495: if ($env{'request.course.sec'}) {
7496: $courseprivid.='/'.$env{'request.course.sec'};
7497: }
7498: if ($env{'user.priv.'.$env{'request.role'}.'./'.
7499: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
7500: return $2;
7501: }
7502: }
7503: }
7504: }
7505: }
7506:
7507: # Free bre to public access
7508:
7509: if ($priv eq 'bre') {
7510: my $copyright;
7511: unless ($uri =~ /ext\.tool/) {
7512: $copyright=&metadata($uri,'copyright');
7513: }
7514: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
7515: return 'F';
7516: }
7517: if ($copyright eq 'priv') {
7518: $uri=~/([^\/]+)\/([^\/]+)\//;
7519: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
7520: return '';
7521: }
7522: }
7523: if ($copyright eq 'domain') {
7524: $uri=~/([^\/]+)\/([^\/]+)\//;
7525: unless (($env{'user.domain'} eq $1) ||
7526: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
7527: return '';
7528: }
7529: }
7530: if ($env{'request.role'}=~ /li\.\//) {
7531: # Library role, so allow browsing of resources in this domain.
7532: return 'F';
7533: }
7534: if ($copyright eq 'custom') {
7535: unless (&customaccess($priv,$uri)) { return ''; }
7536: }
7537: }
7538: # Domain coordinator is trying to create a course
7539: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
7540: # uri is the requested domain in this case.
7541: # comparison to 'request.role.domain' shows if the user has selected
7542: # a role of dc for the domain in question.
7543: return 'F' if ($uri eq $env{'request.role.domain'});
7544: }
7545:
7546: my $thisallowed='';
7547: my $statecond=0;
7548: my $courseprivid='';
7549:
7550: my $ownaccess;
7551: # Community Coordinator or Assistant Co-author browsing resource space.
7552: if (($priv eq 'bro') && ($env{'user.author'})) {
7553: if ($uri eq '') {
7554: $ownaccess = 1;
7555: } else {
7556: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
7557: my $udom = $env{'user.domain'};
7558: my $uname = $env{'user.name'};
7559: if ($uri =~ m{^\Q$udom\E/?$}) {
7560: $ownaccess = 1;
7561: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
7562: unless ($uri =~ m{\.\./}) {
7563: $ownaccess = 1;
7564: }
7565: } elsif (($udom ne 'public') && ($uname ne 'public')) {
7566: my $now = time;
7567: if ($uri =~ m{^([^/]+)/?$}) {
7568: my $adom = $1;
7569: foreach my $key (keys(%env)) {
7570: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
7571: my ($start,$end) = split('.',$env{$key});
7572: if (($now >= $start) && (!$end || $end < $now)) {
7573: $ownaccess = 1;
7574: last;
7575: }
7576: }
7577: }
7578: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
7579: my $adom = $1;
7580: my $aname = $2;
7581: foreach my $role ('ca','aa') {
7582: if ($env{"user.role.$role./$adom/$aname"}) {
7583: my ($start,$end) =
7584: split('.',$env{"user.role.$role./$adom/$aname"});
7585: if (($now >= $start) && (!$end || $end < $now)) {
7586: $ownaccess = 1;
7587: last;
7588: }
7589: }
7590: }
7591: }
7592: }
7593: }
7594: }
7595: }
7596:
7597: # Course
7598:
7599: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
7600: unless (($priv eq 'bro') && (!$ownaccess)) {
7601: $thisallowed.=$1;
7602: }
7603: }
7604:
7605: # Domain
7606:
7607: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
7608: =~/\Q$priv\E\&([^\:]*)/) {
7609: unless (($priv eq 'bro') && (!$ownaccess)) {
7610: $thisallowed.=$1;
7611: }
7612: }
7613:
7614: # User who is not author or co-author might still be able to edit
7615: # resource of an author in the domain (e.g., if Domain Coordinator).
7616: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
7617: (&allowed('mdc',$env{'request.course.id'}))) {
7618: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
7619: $thisallowed.=$1;
7620: }
7621: }
7622:
7623: # Course: uri itself is a course
7624: my $courseuri=$uri;
7625: $courseuri=~s/\_(\d)/\/$1/;
7626: $courseuri=~s/^([^\/])/\/$1/;
7627:
7628: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
7629: =~/\Q$priv\E\&([^\:]*)/) {
7630: unless (($priv eq 'bro') && (!$ownaccess)) {
7631: $thisallowed.=$1;
7632: }
7633: }
7634:
7635: # URI is an uploaded document for this course, default permissions don't matter
7636: # not allowing 'edit' access (editupload) to uploaded course docs
7637: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
7638: $thisallowed='';
7639: my ($match)=&is_on_map($uri);
7640: if ($match) {
7641: if ($env{'user.priv.'.$env{'request.role'}.'./'}
7642: =~/\Q$priv\E\&([^\:]*)/) {
7643: my $value = $1;
7644: if ($noblockcheck) {
7645: $thisallowed.=$value;
7646: } else {
7647: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7648: if (@blockers > 0) {
7649: $thisallowed = 'B';
7650: } else {
7651: $thisallowed.=$value;
7652: }
7653: }
7654: }
7655: } else {
7656: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
7657: if ($refuri) {
7658: if ($refuri =~ m|^/adm/|) {
7659: $thisallowed='F';
7660: } else {
7661: $refuri=&declutter($refuri);
7662: my ($match) = &is_on_map($refuri);
7663: if ($match) {
7664: if ($noblockcheck) {
7665: $thisallowed='F';
7666: } else {
7667: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7668: if (@blockers > 0) {
7669: $thisallowed = 'B';
7670: } else {
7671: $thisallowed='F';
7672: }
7673: }
7674: }
7675: }
7676: }
7677: }
7678: }
7679:
7680: if ($priv eq 'bre'
7681: && $thisallowed ne 'F'
7682: && $thisallowed ne '2'
7683: && &is_portfolio_url($uri)) {
7684: $thisallowed = &portfolio_access($uri,$clientip);
7685: }
7686:
7687: # Full access at system, domain or course-wide level? Exit.
7688: if ($thisallowed=~/F/) {
7689: return 'F';
7690: }
7691:
7692: # If this is generating or modifying users, exit with special codes
7693:
7694: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
7695: if (($priv eq 'cca') || ($priv eq 'caa')) {
7696: my ($audom,$auname)=split('/',$uri);
7697: # no author name given, so this just checks on the general right to make a co-author in this domain
7698: unless ($auname) { return $thisallowed; }
7699: # an author name is given, so we are about to actually make a co-author for a certain account
7700: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
7701: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
7702: ($audom ne $env{'request.role.domain'}))) { return ''; }
7703: }
7704: return $thisallowed;
7705: }
7706: #
7707: # Gathered so far: system, domain and course wide privileges
7708: #
7709: # Course: See if uri or referer is an individual resource that is part of
7710: # the course
7711:
7712: if ($env{'request.course.id'}) {
7713:
7714: $courseprivid=$env{'request.course.id'};
7715: if ($env{'request.course.sec'}) {
7716: $courseprivid.='/'.$env{'request.course.sec'};
7717: }
7718: $courseprivid=~s/\_/\//;
7719: my $checkreferer=1;
7720: my ($match,$cond)=&is_on_map($uri);
7721: if ($match) {
7722: $statecond=$cond;
7723: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
7724: =~/\Q$priv\E\&([^\:]*)/) {
7725: my $value = $1;
7726: if ($priv eq 'bre') {
7727: if ($noblockcheck) {
7728: $thisallowed.=$value;
7729: } else {
7730: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7731: if (@blockers > 0) {
7732: $thisallowed = 'B';
7733: } else {
7734: $thisallowed.=$value;
7735: }
7736: }
7737: } else {
7738: $thisallowed.=$value;
7739: }
7740: $checkreferer=0;
7741: }
7742: }
7743:
7744: if ($checkreferer) {
7745: my $refuri=$env{'httpref.'.$orguri};
7746: unless ($refuri) {
7747: foreach my $key (keys(%env)) {
7748: if ($key=~/^httpref\..*\*/) {
7749: my $pattern=$key;
7750: $pattern=~s/^httpref\.\/res\///;
7751: $pattern=~s/\*/\[\^\/\]\+/g;
7752: $pattern=~s/\//\\\//g;
7753: if ($orguri=~/$pattern/) {
7754: $refuri=$env{$key};
7755: }
7756: }
7757: }
7758: }
7759:
7760: if ($refuri) {
7761: $refuri=&declutter($refuri);
7762: my ($match,$cond)=&is_on_map($refuri);
7763: if ($match) {
7764: my $refstatecond=$cond;
7765: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
7766: =~/\Q$priv\E\&([^\:]*)/) {
7767: my $value = $1;
7768: if ($priv eq 'bre') {
7769: if ($noblockcheck) {
7770: $thisallowed.=$value;
7771: } else {
7772: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7773: if (@blockers > 0) {
7774: $thisallowed = 'B';
7775: } else {
7776: $thisallowed.=$value;
7777: }
7778: }
7779: } else {
7780: $thisallowed.=$value;
7781: }
7782: $uri=$refuri;
7783: $statecond=$refstatecond;
7784: }
7785: }
7786: }
7787: }
7788: }
7789:
7790: #
7791: # Gathered now: all privileges that could apply, and condition number
7792: #
7793: #
7794: # Full or no access?
7795: #
7796:
7797: if ($thisallowed=~/F/) {
7798: return 'F';
7799: }
7800:
7801: unless ($thisallowed) {
7802: return '';
7803: }
7804:
7805: # Restrictions exist, deal with them
7806: #
7807: # C:according to course preferences
7808: # R:according to resource settings
7809: # L:unless locked
7810: # X:according to user session state
7811: #
7812:
7813: # Possibly locked functionality, check all courses
7814: # Locks might take effect only after 10 minutes cache expiration for other
7815: # courses, and 2 minutes for current course
7816:
7817: my $envkey;
7818: if ($thisallowed=~/L/) {
7819: foreach $envkey (keys(%env)) {
7820: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
7821: my $courseid=$2;
7822: my $roleid=$1.'.'.$2;
7823: $courseid=~s/^\///;
7824: my $expiretime=600;
7825: if ($env{'request.role'} eq $roleid) {
7826: $expiretime=120;
7827: }
7828: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
7829: my $prefix='course.'.$cdom.'_'.$cnum.'.';
7830: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
7831: &coursedescription($courseid,{'freshen_cache' => 1});
7832: }
7833: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
7834: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
7835: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
7836: &log($env{'user.domain'},$env{'user.name'},
7837: $env{'user.home'},
7838: 'Locked by res: '.$priv.' for '.$uri.' due to '.
7839: $cdom.'/'.$cnum.'/'.$csec.' expire '.
7840: $env{$prefix.'priv.'.$priv.'.lock.expire'});
7841: return '';
7842: }
7843: }
7844: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
7845: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
7846: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
7847: &log($env{'user.domain'},$env{'user.name'},
7848: $env{'user.home'},
7849: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
7850: $cdom.'/'.$cnum.'/'.$csec.' expire '.
7851: $env{$prefix.'priv.'.$priv.'.lock.expire'});
7852: return '';
7853: }
7854: }
7855: }
7856: }
7857: }
7858:
7859: #
7860: # Rest of the restrictions depend on selected course
7861: #
7862:
7863: unless ($env{'request.course.id'}) {
7864: if ($thisallowed eq 'A') {
7865: return 'A';
7866: } elsif ($thisallowed eq 'B') {
7867: return 'B';
7868: } else {
7869: return '1';
7870: }
7871: }
7872:
7873: #
7874: # Now user is definitely in a course
7875: #
7876:
7877:
7878: # Course preferences
7879:
7880: if ($thisallowed=~/C/) {
7881: my $rolecode=(split(/\./,$env{'request.role'}))[0];
7882: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
7883: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
7884: =~/\Q$rolecode\E/) {
7885: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
7886: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7887: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
7888: $env{'request.course.id'});
7889: }
7890: return '';
7891: }
7892:
7893: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
7894: =~/\Q$unamedom\E/) {
7895: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
7896: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
7897: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
7898: $env{'request.course.id'});
7899: }
7900: return '';
7901: }
7902: }
7903:
7904: # Resource preferences
7905:
7906: if ($thisallowed=~/R/) {
7907: my $rolecode=(split(/\./,$env{'request.role'}))[0];
7908: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
7909: if (($priv ne 'pch') && ($priv ne 'plc')) {
7910: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7911: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
7912: }
7913: return '';
7914: }
7915: }
7916:
7917: # Restricted by state or randomout?
7918:
7919: if ($thisallowed=~/X/) {
7920: if ($env{'acc.randomout'}) {
7921: if (!$symb) { $symb=&symbread($uri,1); }
7922: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
7923: return '';
7924: }
7925: }
7926: if (&condval($statecond)) {
7927: return '2';
7928: } else {
7929: return '';
7930: }
7931: }
7932:
7933: if ($thisallowed eq 'A') {
7934: return 'A';
7935: } elsif ($thisallowed eq 'B') {
7936: return 'B';
7937: }
7938: return 'F';
7939: }
7940:
7941: # ------------------------------------------- Check construction space access
7942:
7943: sub constructaccess {
7944: my ($url,$setpriv)=@_;
7945:
7946: # We do not allow editing of previous versions of files
7947: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
7948:
7949: # Get username and domain from URL
7950: my ($ownername,$ownerdomain,$ownerhome);
7951:
7952: ($ownerdomain,$ownername) =
7953: ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
7954:
7955: # The URL does not really point to any authorspace, forget it
7956: unless (($ownername) && ($ownerdomain)) { return ''; }
7957:
7958: # Now we need to see if the user has access to the authorspace of
7959: # $ownername at $ownerdomain
7960:
7961: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
7962: # Real author for this?
7963: $ownerhome = $env{'user.home'};
7964: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
7965: return ($ownername,$ownerdomain,$ownerhome);
7966: }
7967: } else {
7968: # Co-author for this?
7969: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
7970: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
7971: $ownerhome = &homeserver($ownername,$ownerdomain);
7972: return ($ownername,$ownerdomain,$ownerhome);
7973: }
7974: if ($env{'request.course.id'}) {
7975: if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
7976: ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
7977: if (&allowed('mdc',$env{'request.course.id'})) {
7978: $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
7979: return ($ownername,$ownerdomain,$ownerhome);
7980: }
7981: }
7982: }
7983: }
7984:
7985: # We don't have any access right now. If we are not possibly going to do anything about this,
7986: # we might as well leave
7987: unless ($setpriv) { return ''; }
7988:
7989: # Backdoor access?
7990: my $allowed=&allowed('eco',$ownerdomain);
7991: # Nope
7992: unless ($allowed) { return ''; }
7993: # Looks like we may have access, but could be locked by the owner of the construction space
7994: if ($allowed eq 'U') {
7995: my %blocked=&get('environment',['domcoord.author'],
7996: $ownerdomain,$ownername);
7997: # Is blocked by owner
7998: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
7999: }
8000: if (($allowed eq 'F') || ($allowed eq 'U')) {
8001: # Grant temporary access
8002: my $then=$env{'user.login.time'};
8003: my $update=$env{'user.update.time'};
8004: if (!$update) { $update = $then; }
8005: my $refresh=$env{'user.refresh.time'};
8006: if (!$refresh) { $refresh = $update; }
8007: my $now = time;
8008: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
8009: $now,'ca','constructaccess');
8010: $ownerhome = &homeserver($ownername,$ownerdomain);
8011: return($ownername,$ownerdomain,$ownerhome);
8012: }
8013: # No business here
8014: return '';
8015: }
8016:
8017: # ----------------------------------------------------------- Content Blocking
8018:
8019: {
8020: # Caches for faster Course Contents display where content blocking
8021: # is in operation (i.e., interval param set) for timed quiz.
8022: #
8023: # User for whom data are being temporarily cached.
8024: my $cacheduser='';
8025: # Cached blockers for this user (a hash of blocking items).
8026: my %cachedblockers=();
8027: # When the data were last cached.
8028: my $cachedlast='';
8029:
8030: sub load_all_blockers {
8031: my ($uname,$udom,$blocks)=@_;
8032: if (($uname ne '') && ($udom ne '')) {
8033: if (($cacheduser eq $uname.':'.$udom) &&
8034: (abs($cachedlast-time)<5)) {
8035: return;
8036: }
8037: }
8038: $cachedlast=time;
8039: $cacheduser=$uname.':'.$udom;
8040: %cachedblockers = &get_commblock_resources($blocks);
8041: }
8042:
8043: sub get_comm_blocks {
8044: my ($cdom,$cnum) = @_;
8045: if ($cdom eq '' || $cnum eq '') {
8046: return unless ($env{'request.course.id'});
8047: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8048: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8049: }
8050: my %commblocks;
8051: my $hashid=$cdom.'_'.$cnum;
8052: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
8053: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
8054: %commblocks = %{$blocksref};
8055: } else {
8056: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
8057: my $cachetime = 600;
8058: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
8059: }
8060: return %commblocks;
8061: }
8062:
8063: sub get_commblock_resources {
8064: my ($blocks) = @_;
8065: my %blockers = ();
8066: return %blockers unless ($env{'request.course.id'});
8067: return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
8068: my %commblocks;
8069: if (ref($blocks) eq 'HASH') {
8070: %commblocks = %{$blocks};
8071: } else {
8072: %commblocks = &get_comm_blocks();
8073: }
8074: return %blockers unless (keys(%commblocks) > 0);
8075: my $navmap = Apache::lonnavmaps::navmap->new();
8076: return %blockers unless (ref($navmap));
8077: my $now = time;
8078: foreach my $block (keys(%commblocks)) {
8079: if ($block =~ /^(\d+)____(\d+)$/) {
8080: my ($start,$end) = ($1,$2);
8081: if ($start <= $now && $end >= $now) {
8082: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
8083: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
8084: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
8085: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
8086: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
8087: }
8088: }
8089: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
8090: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
8091: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
8092: }
8093: }
8094: }
8095: }
8096: }
8097: } elsif ($block =~ /^firstaccess____(.+)$/) {
8098: my $item = $1;
8099: my @to_test;
8100: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
8101: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
8102: my @interval;
8103: my $type = 'map';
8104: if ($item eq 'course') {
8105: $type = 'course';
8106: @interval=&EXT("resource.0.interval");
8107: } else {
8108: if ($item =~ /___\d+___/) {
8109: $type = 'resource';
8110: @interval=&EXT("resource.0.interval",$item);
8111: if (ref($navmap)) {
8112: my $res = $navmap->getBySymb($item);
8113: push(@to_test,$res);
8114: }
8115: } else {
8116: my $mapsymb = &symbread($item,1);
8117: if ($mapsymb) {
8118: if (ref($navmap)) {
8119: my $mapres = $navmap->getBySymb($mapsymb);
8120: @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
8121: foreach my $res (@to_test) {
8122: my $symb = $res->symb();
8123: next if ($symb eq $mapsymb);
8124: if ($symb ne '') {
8125: @interval=&EXT("resource.0.interval",$symb);
8126: if ($interval[1] eq 'map') {
8127: last;
8128: }
8129: }
8130: }
8131: }
8132: }
8133: }
8134: }
8135: if ($interval[0] =~ /^(\d+)/) {
8136: my $timelimit = $1;
8137: my $first_access;
8138: if ($type eq 'resource') {
8139: $first_access=&get_first_access($interval[1],$item);
8140: } elsif ($type eq 'map') {
8141: $first_access=&get_first_access($interval[1],undef,$item);
8142: } else {
8143: $first_access=&get_first_access($interval[1]);
8144: }
8145: if ($first_access) {
8146: my $timesup = $first_access+$timelimit;
8147: if ($timesup > $now) {
8148: my $activeblock;
8149: foreach my $res (@to_test) {
8150: if ($res->answerable()) {
8151: $activeblock = 1;
8152: last;
8153: }
8154: }
8155: if ($activeblock) {
8156: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
8157: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
8158: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
8159: }
8160: }
8161: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
8162: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
8163: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
8164: }
8165: }
8166: }
8167: }
8168: }
8169: }
8170: }
8171: }
8172: }
8173: }
8174: return %blockers;
8175: }
8176:
8177: sub has_comm_blocking {
8178: my ($priv,$symb,$uri,$blocks) = @_;
8179: my @blockers;
8180: return unless ($env{'request.course.id'});
8181: return unless ($priv eq 'bre');
8182: return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
8183: return if ($env{'request.state'} eq 'construct');
8184: &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
8185: return unless (keys(%cachedblockers) > 0);
8186: my (%possibles,@symbs);
8187: if (!$symb) {
8188: $symb = &symbread($uri,1,1,1,\%possibles);
8189: }
8190: if ($symb) {
8191: @symbs = ($symb);
8192: } elsif (keys(%possibles)) {
8193: @symbs = keys(%possibles);
8194: }
8195: my $noblock;
8196: foreach my $symb (@symbs) {
8197: last if ($noblock);
8198: my ($map,$resid,$resurl)=&decode_symb($symb);
8199: foreach my $block (keys(%cachedblockers)) {
8200: if ($block =~ /^firstaccess____(.+)$/) {
8201: my $item = $1;
8202: if (($item eq $map) || ($item eq $symb)) {
8203: $noblock = 1;
8204: last;
8205: }
8206: }
8207: if (ref($cachedblockers{$block}) eq 'HASH') {
8208: if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
8209: if ($cachedblockers{$block}{'resources'}{$symb}) {
8210: unless (grep(/^\Q$block\E$/,@blockers)) {
8211: push(@blockers,$block);
8212: }
8213: }
8214: }
8215: }
8216: if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
8217: if ($cachedblockers{$block}{'maps'}{$map}) {
8218: unless (grep(/^\Q$block\E$/,@blockers)) {
8219: push(@blockers,$block);
8220: }
8221: }
8222: }
8223: }
8224: }
8225: return if ($noblock);
8226: return @blockers;
8227: }
8228: }
8229:
8230: # -------------------------------- Deversion and split uri into path an filename
8231:
8232: #
8233: # Removes the version from a URI and
8234: # splits it in to its filename and path to the filename.
8235: # Seems like File::Basename could have done this more clearly.
8236: # Parameters:
8237: # $uri - input URI
8238: # Returns:
8239: # Two element list consisting of
8240: # $pathname - the URI up to and excluding the trailing /
8241: # $filename - The part of the URI following the last /
8242: # NOTE:
8243: # Another realization of this is simply:
8244: # use File::Basename;
8245: # ...
8246: # $uri = shift;
8247: # $filename = basename($uri);
8248: # $path = dirname($uri);
8249: # return ($filename, $path);
8250: #
8251: # The implementation below is probably faster however.
8252: #
8253: sub split_uri_for_cond {
8254: my $uri=&deversion(&declutter(shift));
8255: my @uriparts=split(/\//,$uri);
8256: my $filename=pop(@uriparts);
8257: my $pathname=join('/',@uriparts);
8258: return ($pathname,$filename);
8259: }
8260: # --------------------------------------------------- Is a resource on the map?
8261:
8262: sub is_on_map {
8263: my ($pathname,$filename) = &split_uri_for_cond(shift);
8264: #Trying to find the conditional for the file
8265: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
8266: /\&\Q$filename\E\:([\d\|]+)\&/);
8267: if ($match) {
8268: return (1,$1);
8269: } else {
8270: return (0,0);
8271: }
8272: }
8273:
8274: # --------------------------------------------------------- Get symb from alias
8275:
8276: sub get_symb_from_alias {
8277: my $symb=shift;
8278: my ($map,$resid,$url)=&decode_symb($symb);
8279: # Already is a symb
8280: if ($url) { return $symb; }
8281: # Must be an alias
8282: my $aliassymb='';
8283: my %bighash;
8284: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8285: &GDBM_READER(),0640)) {
8286: my $rid=$bighash{'mapalias_'.$symb};
8287: if ($rid) {
8288: my ($mapid,$resid)=split(/\./,$rid);
8289: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
8290: $resid,$bighash{'src_'.$rid});
8291: }
8292: untie %bighash;
8293: }
8294: return $aliassymb;
8295: }
8296:
8297: # ----------------------------------------------------------------- Define Role
8298:
8299: sub definerole {
8300: if (allowed('mcr','/')) {
8301: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
8302: foreach my $role (split(':',$sysrole)) {
8303: my ($crole,$cqual)=split(/\&/,$role);
8304: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
8305: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
8306: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
8307: return "refused:s:$crole&$cqual";
8308: }
8309: }
8310: }
8311: foreach my $role (split(':',$domrole)) {
8312: my ($crole,$cqual)=split(/\&/,$role);
8313: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
8314: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
8315: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
8316: return "refused:d:$crole&$cqual";
8317: }
8318: }
8319: }
8320: foreach my $role (split(':',$courole)) {
8321: my ($crole,$cqual)=split(/\&/,$role);
8322: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
8323: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
8324: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
8325: return "refused:c:$crole&$cqual";
8326: }
8327: }
8328: }
8329: my $uhome;
8330: if (($uname ne '') && ($udom ne '')) {
8331: $uhome = &homeserver($uname,$udom);
8332: return $uhome if ($uhome eq 'no_host');
8333: } else {
8334: $uname = $env{'user.name'};
8335: $udom = $env{'user.domain'};
8336: $uhome = $env{'user.home'};
8337: }
8338: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
8339: "$udom:$uname:rolesdef_$rolename=".
8340: escape($sysrole.'_'.$domrole.'_'.$courole);
8341: return reply($command,$uhome);
8342: } else {
8343: return 'refused';
8344: }
8345: }
8346:
8347: # ---------------- Make a metadata query against the network of library servers
8348:
8349: sub metadata_query {
8350: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
8351: my %rhash;
8352: my %libserv = &all_library();
8353: my @server_list = (defined($server_array) ? @$server_array
8354: : keys(%libserv) );
8355: for my $server (@server_list) {
8356: my $domains = '';
8357: if (ref($domains_hash) eq 'HASH') {
8358: $domains = $domains_hash->{$server};
8359: }
8360: unless ($custom or $customshow) {
8361: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
8362: $rhash{$server}=$reply;
8363: }
8364: else {
8365: my $reply=&reply("querysend:".&escape($query).':'.
8366: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
8367: $server);
8368: $rhash{$server}=$reply;
8369: }
8370: }
8371: return \%rhash;
8372: }
8373:
8374: # ----------------------------------------- Send log queries and wait for reply
8375:
8376: sub log_query {
8377: my ($uname,$udom,$query,%filters)=@_;
8378: my $uhome=&homeserver($uname,$udom);
8379: if ($uhome eq 'no_host') { return 'error: no_host'; }
8380: my $uhost=&hostname($uhome);
8381: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
8382: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
8383: $uhome);
8384: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
8385: return get_query_reply($queryid);
8386: }
8387:
8388: # -------------------------- Update MySQL table for portfolio file
8389:
8390: sub update_portfolio_table {
8391: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
8392: if ($group ne '') {
8393: $file_name =~s /^\Q$group\E//;
8394: }
8395: my $homeserver = &homeserver($uname,$udom);
8396: my $queryid=
8397: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
8398: ':'.&escape($file_name).':'.$action,$homeserver);
8399: my $reply = &get_query_reply($queryid);
8400: return $reply;
8401: }
8402:
8403: # -------------------------- Update MySQL allusers table
8404:
8405: sub update_allusers_table {
8406: my ($uname,$udom,$names) = @_;
8407: my $homeserver = &homeserver($uname,$udom);
8408: my $queryid=
8409: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
8410: 'lastname='.&escape($names->{'lastname'}).'%%'.
8411: 'firstname='.&escape($names->{'firstname'}).'%%'.
8412: 'middlename='.&escape($names->{'middlename'}).'%%'.
8413: 'generation='.&escape($names->{'generation'}).'%%'.
8414: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
8415: 'id='.&escape($names->{'id'}),$homeserver);
8416: return;
8417: }
8418:
8419: # ------- Request retrieval of institutional classlists for course(s)
8420:
8421: sub fetch_enrollment_query {
8422: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
8423: my ($homeserver,$sleep,$loopmax);
8424: my $maxtries = 1;
8425: if ($context eq 'automated') {
8426: $homeserver = $perlvar{'lonHostID'};
8427: $sleep = 2;
8428: $loopmax = 100;
8429: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
8430: } else {
8431: $homeserver = &homeserver($cnum,$dom);
8432: }
8433: my $host=&hostname($homeserver);
8434: my $cmd = '';
8435: foreach my $affiliate (keys(%{$affiliatesref})) {
8436: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
8437: }
8438: $cmd =~ s/%%$//;
8439: $cmd = &escape($cmd);
8440: my $query = 'fetchenrollment';
8441: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
8442: unless ($queryid=~/^\Q$host\E\_/) {
8443: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
8444: return 'error: '.$queryid;
8445: }
8446: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
8447: my $tries = 1;
8448: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
8449: $reply = &get_query_reply($queryid,$sleep,$loopmax);
8450: $tries ++;
8451: }
8452: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
8453: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
8454: } else {
8455: my @responses = split(/:/,$reply);
8456: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
8457: foreach my $line (@responses) {
8458: my ($key,$value) = split(/=/,$line,2);
8459: $$replyref{$key} = $value;
8460: }
8461: } else {
8462: my $pathname = LONCAPA::tempdir();
8463: foreach my $line (@responses) {
8464: my ($key,$value) = split(/=/,$line);
8465: $$replyref{$key} = $value;
8466: if ($value > 0) {
8467: foreach my $item (@{$$affiliatesref{$key}}) {
8468: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
8469: my $destname = $pathname.'/'.$filename;
8470: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
8471: if ($xml_classlist =~ /^error/) {
8472: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
8473: } else {
8474: if ( open(FILE,">",$destname) ) {
8475: print FILE &unescape($xml_classlist);
8476: close(FILE);
8477: } else {
8478: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
8479: }
8480: }
8481: }
8482: }
8483: }
8484: }
8485: return 'ok';
8486: }
8487: return 'error';
8488: }
8489:
8490: sub get_query_reply {
8491: my ($queryid,$sleep,$loopmax) = @_;;
8492: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
8493: $sleep = 0.2;
8494: }
8495: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
8496: $loopmax = 100;
8497: }
8498: my $replyfile=LONCAPA::tempdir().$queryid;
8499: my $reply='';
8500: for (1..$loopmax) {
8501: sleep($sleep);
8502: if (-e $replyfile.'.end') {
8503: if (open(my $fh,"<",$replyfile)) {
8504: $reply = join('',<$fh>);
8505: close($fh);
8506: } else { return 'error: reply_file_error'; }
8507: return &unescape($reply);
8508: }
8509: }
8510: return 'timeout:'.$queryid;
8511: }
8512:
8513: sub courselog_query {
8514: #
8515: # possible filters:
8516: # url: url or symb
8517: # username
8518: # domain
8519: # action: view, submit, grade
8520: # start: timestamp
8521: # end: timestamp
8522: #
8523: my (%filters)=@_;
8524: unless ($env{'request.course.id'}) { return 'no_course'; }
8525: if ($filters{'url'}) {
8526: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
8527: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
8528: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
8529: }
8530: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
8531: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
8532: return &log_query($cname,$cdom,'courselog',%filters);
8533: }
8534:
8535: sub userlog_query {
8536: #
8537: # possible filters:
8538: # action: log check role
8539: # start: timestamp
8540: # end: timestamp
8541: #
8542: my ($uname,$udom,%filters)=@_;
8543: return &log_query($uname,$udom,'userlog',%filters);
8544: }
8545:
8546: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
8547:
8548: sub auto_run {
8549: my ($cnum,$cdom) = @_;
8550: my $response = 0;
8551: my $settings;
8552: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
8553: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
8554: $settings = $domconfig{'autoenroll'};
8555: if ($settings->{'run'} eq '1') {
8556: $response = 1;
8557: }
8558: } else {
8559: my $homeserver;
8560: if (&is_course($cdom,$cnum)) {
8561: $homeserver = &homeserver($cnum,$cdom);
8562: } else {
8563: $homeserver = &domain($cdom,'primary');
8564: }
8565: if ($homeserver ne 'no_host') {
8566: $response = &reply('autorun:'.$cdom,$homeserver);
8567: }
8568: }
8569: return $response;
8570: }
8571:
8572: sub auto_get_sections {
8573: my ($cnum,$cdom,$inst_coursecode) = @_;
8574: my $homeserver;
8575: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8576: $homeserver = &homeserver($cnum,$cdom);
8577: }
8578: if (!defined($homeserver)) {
8579: if ($cdom =~ /^$match_domain$/) {
8580: $homeserver = &domain($cdom,'primary');
8581: }
8582: }
8583: my @secs;
8584: if (defined($homeserver)) {
8585: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
8586: unless ($response eq 'refused') {
8587: @secs = split(/:/,$response);
8588: }
8589: }
8590: return @secs;
8591: }
8592:
8593: sub auto_new_course {
8594: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
8595: my $homeserver = &homeserver($cnum,$cdom);
8596: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
8597: return $response;
8598: }
8599:
8600: sub auto_validate_courseID {
8601: my ($cnum,$cdom,$inst_course_id) = @_;
8602: my $homeserver = &homeserver($cnum,$cdom);
8603: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
8604: return $response;
8605: }
8606:
8607: sub auto_validate_instcode {
8608: my ($cnum,$cdom,$instcode,$owner) = @_;
8609: my ($homeserver,$response);
8610: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8611: $homeserver = &homeserver($cnum,$cdom);
8612: }
8613: if (!defined($homeserver)) {
8614: if ($cdom =~ /^$match_domain$/) {
8615: $homeserver = &domain($cdom,'primary');
8616: }
8617: }
8618: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
8619: &escape($instcode).':'.&escape($owner),$homeserver));
8620: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
8621: return ($outcome,$description,$defaultcredits);
8622: }
8623:
8624: sub auto_create_password {
8625: my ($cnum,$cdom,$authparam,$udom) = @_;
8626: my ($homeserver,$response);
8627: my $create_passwd = 0;
8628: my $authchk = '';
8629: if ($udom =~ /^$match_domain$/) {
8630: $homeserver = &domain($udom,'primary');
8631: }
8632: if ($homeserver eq '') {
8633: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8634: $homeserver = &homeserver($cnum,$cdom);
8635: }
8636: }
8637: if ($homeserver eq '') {
8638: $authchk = 'nodomain';
8639: } else {
8640: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
8641: if ($response eq 'refused') {
8642: $authchk = 'refused';
8643: } else {
8644: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
8645: }
8646: }
8647: return ($authparam,$create_passwd,$authchk);
8648: }
8649:
8650: sub auto_photo_permission {
8651: my ($cnum,$cdom,$students) = @_;
8652: my $homeserver = &homeserver($cnum,$cdom);
8653: my ($outcome,$perm_reqd,$conditions) =
8654: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
8655: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8656: return (undef,undef);
8657: }
8658: return ($outcome,$perm_reqd,$conditions);
8659: }
8660:
8661: sub auto_checkphotos {
8662: my ($uname,$udom,$pid) = @_;
8663: my $homeserver = &homeserver($uname,$udom);
8664: my ($result,$resulttype);
8665: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
8666: &escape($uname).':'.&escape($pid),
8667: $homeserver));
8668: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8669: return (undef,undef);
8670: }
8671: if ($outcome) {
8672: ($result,$resulttype) = split(/:/,$outcome);
8673: }
8674: return ($result,$resulttype);
8675: }
8676:
8677: sub auto_photochoice {
8678: my ($cnum,$cdom) = @_;
8679: my $homeserver = &homeserver($cnum,$cdom);
8680: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
8681: &escape($cdom),
8682: $homeserver)));
8683: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8684: return (undef,undef);
8685: }
8686: return ($update,$comment);
8687: }
8688:
8689: sub auto_photoupdate {
8690: my ($affiliatesref,$dom,$cnum,$photo) = @_;
8691: my $homeserver = &homeserver($cnum,$dom);
8692: my $host=&hostname($homeserver);
8693: my $cmd = '';
8694: my $maxtries = 1;
8695: foreach my $affiliate (keys(%{$affiliatesref})) {
8696: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
8697: }
8698: $cmd =~ s/%%$//;
8699: $cmd = &escape($cmd);
8700: my $query = 'institutionalphotos';
8701: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
8702: unless ($queryid=~/^\Q$host\E\_/) {
8703: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
8704: return 'error: '.$queryid;
8705: }
8706: my $reply = &get_query_reply($queryid);
8707: my $tries = 1;
8708: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
8709: $reply = &get_query_reply($queryid);
8710: $tries ++;
8711: }
8712: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
8713: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
8714: } else {
8715: my @responses = split(/:/,$reply);
8716: my $outcome = shift(@responses);
8717: foreach my $item (@responses) {
8718: my ($key,$value) = split(/=/,$item);
8719: $$photo{$key} = $value;
8720: }
8721: return $outcome;
8722: }
8723: return 'error';
8724: }
8725:
8726: sub auto_instcode_format {
8727: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
8728: $cat_order) = @_;
8729: my $courses = '';
8730: my @homeservers;
8731: if ($caller eq 'global') {
8732: my %servers = &get_servers($codedom,'library');
8733: foreach my $tryserver (keys(%servers)) {
8734: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8735: push(@homeservers,$tryserver);
8736: }
8737: }
8738: } elsif ($caller eq 'requests') {
8739: if ($codedom =~ /^$match_domain$/) {
8740: my $chome = &domain($codedom,'primary');
8741: unless ($chome eq 'no_host') {
8742: push(@homeservers,$chome);
8743: }
8744: }
8745: } else {
8746: push(@homeservers,&homeserver($caller,$codedom));
8747: }
8748: foreach my $code (keys(%{$instcodes})) {
8749: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
8750: }
8751: chop($courses);
8752: my $ok_response = 0;
8753: my $response;
8754: while (@homeservers > 0 && $ok_response == 0) {
8755: my $server = shift(@homeservers);
8756: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
8757: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
8758: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
8759: split(/:/,$response);
8760: %{$codes} = (%{$codes},&str2hash($codes_str));
8761: push(@{$codetitles},&str2array($codetitles_str));
8762: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
8763: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
8764: $ok_response = 1;
8765: }
8766: }
8767: if ($ok_response) {
8768: return 'ok';
8769: } else {
8770: return $response;
8771: }
8772: }
8773:
8774: sub auto_instcode_defaults {
8775: my ($domain,$returnhash,$code_order) = @_;
8776: my @homeservers;
8777:
8778: my %servers = &get_servers($domain,'library');
8779: foreach my $tryserver (keys(%servers)) {
8780: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8781: push(@homeservers,$tryserver);
8782: }
8783: }
8784:
8785: my $response;
8786: foreach my $server (@homeservers) {
8787: $response=&reply('autoinstcodedefaults:'.$domain,$server);
8788: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
8789:
8790: foreach my $pair (split(/\&/,$response)) {
8791: my ($name,$value)=split(/\=/,$pair);
8792: if ($name eq 'code_order') {
8793: @{$code_order} = split(/\&/,&unescape($value));
8794: } else {
8795: $returnhash->{&unescape($name)}=&unescape($value);
8796: }
8797: }
8798: return 'ok';
8799: }
8800:
8801: return $response;
8802: }
8803:
8804: sub auto_possible_instcodes {
8805: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
8806: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
8807: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8808: return;
8809: }
8810: my (@homeservers,$uhome);
8811: if (defined(&domain($domain,'primary'))) {
8812: $uhome=&domain($domain,'primary');
8813: push(@homeservers,&domain($domain,'primary'));
8814: } else {
8815: my %servers = &get_servers($domain,'library');
8816: foreach my $tryserver (keys(%servers)) {
8817: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8818: push(@homeservers,$tryserver);
8819: }
8820: }
8821: }
8822: my $response;
8823: foreach my $server (@homeservers) {
8824: $response=&reply('autopossibleinstcodes:'.$domain,$server);
8825: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
8826: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
8827: split(':',$response);
8828: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
8829: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
8830: foreach my $item (split('&',$cat_title)) {
8831: my ($name,$value)=split('=',$item);
8832: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
8833: }
8834: foreach my $item (split('&',$cat_order)) {
8835: my ($name,$value)=split('=',$item);
8836: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
8837: }
8838: return 'ok';
8839: }
8840: return $response;
8841: }
8842:
8843: sub auto_courserequest_checks {
8844: my ($dom) = @_;
8845: my ($homeserver,%validations);
8846: if ($dom =~ /^$match_domain$/) {
8847: $homeserver = &domain($dom,'primary');
8848: }
8849: unless ($homeserver eq 'no_host') {
8850: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
8851: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8852: my @items = split(/&/,$response);
8853: foreach my $item (@items) {
8854: my ($key,$value) = split('=',$item);
8855: $validations{&unescape($key)} = &thaw_unescape($value);
8856: }
8857: }
8858: }
8859: return %validations;
8860: }
8861:
8862: sub auto_courserequest_validation {
8863: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
8864: my ($homeserver,$response);
8865: if ($dom =~ /^$match_domain$/) {
8866: $homeserver = &domain($dom,'primary');
8867: }
8868: unless ($homeserver eq 'no_host') {
8869: my $customdata;
8870: if (ref($custominfo) eq 'HASH') {
8871: $customdata = &freeze_escape($custominfo);
8872: }
8873: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
8874: ':'.&escape($crstype).':'.&escape($inststatuslist).
8875: ':'.&escape($instcode).':'.&escape($instseclist).':'.
8876: $customdata,$homeserver));
8877: }
8878: return $response;
8879: }
8880:
8881: sub auto_validate_class_sec {
8882: my ($cdom,$cnum,$owners,$inst_class) = @_;
8883: my $homeserver = &homeserver($cnum,$cdom);
8884: my $ownerlist;
8885: if (ref($owners) eq 'ARRAY') {
8886: $ownerlist = join(',',@{$owners});
8887: } else {
8888: $ownerlist = $owners;
8889: }
8890: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
8891: &escape($ownerlist).':'.$cdom,$homeserver);
8892: return $response;
8893: }
8894:
8895: sub auto_validate_instclasses {
8896: my ($cdom,$cnum,$owners,$classesref) = @_;
8897: my ($homeserver,%validations);
8898: $homeserver = &homeserver($cnum,$cdom);
8899: unless ($homeserver eq 'no_host') {
8900: my $ownerlist;
8901: if (ref($owners) eq 'ARRAY') {
8902: $ownerlist = join(',',@{$owners});
8903: } else {
8904: $ownerlist = $owners;
8905: }
8906: if (ref($classesref) eq 'HASH') {
8907: my $classes = &freeze_escape($classesref);
8908: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
8909: ':'.$cdom.':'.$classes,$homeserver);
8910: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8911: my @items = split(/&/,$response);
8912: foreach my $item (@items) {
8913: my ($key,$value) = split('=',$item);
8914: $validations{&unescape($key)} = &thaw_unescape($value);
8915: }
8916: }
8917: }
8918: }
8919: return %validations;
8920: }
8921:
8922: sub auto_crsreq_update {
8923: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
8924: $code,$accessstart,$accessend,$inbound) = @_;
8925: my ($homeserver,%crsreqresponse);
8926: if ($cdom =~ /^$match_domain$/) {
8927: $homeserver = &domain($cdom,'primary');
8928: }
8929: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8930: my $info;
8931: if (ref($inbound) eq 'HASH') {
8932: $info = &freeze_escape($inbound);
8933: }
8934: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
8935: ':'.&escape($action).':'.&escape($ownername).':'.
8936: &escape($ownerdomain).':'.&escape($fullname).':'.
8937: &escape($title).':'.&escape($code).':'.
8938: &escape($accessstart).':'.&escape($accessend).':'.$info,
8939: $homeserver);
8940: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8941: my @items = split(/&/,$response);
8942: foreach my $item (@items) {
8943: my ($key,$value) = split('=',$item);
8944: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
8945: }
8946: }
8947: }
8948: return \%crsreqresponse;
8949: }
8950:
8951: sub auto_export_grades {
8952: my ($cdom,$cnum,$inforef,$gradesref) = @_;
8953: my ($homeserver,%exportresponse);
8954: if ($cdom =~ /^$match_domain$/) {
8955: $homeserver = &domain($cdom,'primary');
8956: }
8957: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8958: my $info;
8959: if (ref($inforef) eq 'HASH') {
8960: $info = &freeze_escape($inforef);
8961: }
8962: if (ref($gradesref) eq 'HASH') {
8963: my $grades = &freeze_escape($gradesref);
8964: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
8965: $info.':'.$grades,$homeserver);
8966: unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
8967: my @items = split(/&/,$response);
8968: foreach my $item (@items) {
8969: my ($key,$value) = split('=',$item);
8970: $exportresponse{&unescape($key)} = &thaw_unescape($value);
8971: }
8972: }
8973: }
8974: }
8975: return \%exportresponse;
8976: }
8977:
8978: sub check_instcode_cloning {
8979: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
8980: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8981: return;
8982: }
8983: my $canclone;
8984: if (@{$code_order} > 0) {
8985: my $instcoderegexp ='^';
8986: my @clonecodes = split(/\&/,$cloner);
8987: foreach my $item (@{$code_order}) {
8988: if (grep(/^\Q$item\E=/,@clonecodes)) {
8989: foreach my $pair (@clonecodes) {
8990: my ($key,$val) = split(/\=/,$pair,2);
8991: $val = &unescape($val);
8992: if ($key eq $item) {
8993: $instcoderegexp .= '('.$val.')';
8994: last;
8995: }
8996: }
8997: } else {
8998: $instcoderegexp .= $codedefaults->{$item};
8999: }
9000: }
9001: $instcoderegexp .= '$';
9002: my (@from,@to);
9003: eval {
9004: (@from) = ($clonefromcode =~ /$instcoderegexp/);
9005: (@to) = ($clonetocode =~ /$instcoderegexp/);
9006: };
9007: if ((@from > 0) && (@to > 0)) {
9008: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
9009: if (!@diffs) {
9010: $canclone = 1;
9011: }
9012: }
9013: }
9014: return $canclone;
9015: }
9016:
9017: sub default_instcode_cloning {
9018: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
9019: my (%codedefaults,@code_order,$canclone);
9020: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
9021: %codedefaults = %{$codedefaultsref};
9022: @code_order = @{$codeorderref};
9023: } elsif ($clonedom) {
9024: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
9025: }
9026: if (($domdefclone) && (@code_order)) {
9027: my @clonecodes = split(/\+/,$domdefclone);
9028: my $instcoderegexp ='^';
9029: foreach my $item (@code_order) {
9030: if (grep(/^\Q$item\E$/,@clonecodes)) {
9031: $instcoderegexp .= '('.$codedefaults{$item}.')';
9032: } else {
9033: $instcoderegexp .= $codedefaults{$item};
9034: }
9035: }
9036: $instcoderegexp .= '$';
9037: my (@from,@to);
9038: eval {
9039: (@from) = ($clonefromcode =~ /$instcoderegexp/);
9040: (@to) = ($clonetocode =~ /$instcoderegexp/);
9041: };
9042: if ((@from > 0) && (@to > 0)) {
9043: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
9044: if (!@diffs) {
9045: $canclone = 1;
9046: }
9047: }
9048: }
9049: return $canclone;
9050: }
9051:
9052: # ------------------------------------------------------- Course Group routines
9053:
9054: sub get_coursegroups {
9055: my ($cdom,$cnum,$group,$namespace) = @_;
9056: return(&dump($namespace,$cdom,$cnum,$group));
9057: }
9058:
9059: sub modify_coursegroup {
9060: my ($cdom,$cnum,$groupsettings) = @_;
9061: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
9062: }
9063:
9064: sub toggle_coursegroup_status {
9065: my ($cdom,$cnum,$group,$action) = @_;
9066: my ($from_namespace,$to_namespace);
9067: if ($action eq 'delete') {
9068: $from_namespace = 'coursegroups';
9069: $to_namespace = 'deleted_groups';
9070: } else {
9071: $from_namespace = 'deleted_groups';
9072: $to_namespace = 'coursegroups';
9073: }
9074: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
9075: if (my $tmp = &error(%curr_group)) {
9076: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
9077: return ('read error',$tmp);
9078: } else {
9079: my %savedsettings = %curr_group;
9080: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
9081: my $deloutcome;
9082: if ($result eq 'ok') {
9083: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
9084: } else {
9085: return ('write error',$result);
9086: }
9087: if ($deloutcome eq 'ok') {
9088: return 'ok';
9089: } else {
9090: return ('delete error',$deloutcome);
9091: }
9092: }
9093: }
9094:
9095: sub modify_group_roles {
9096: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
9097: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
9098: my $role = 'gr/'.&escape($userprivs);
9099: my ($uname,$udom) = split(/:/,$user);
9100: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
9101: if ($result eq 'ok') {
9102: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
9103: }
9104: return $result;
9105: }
9106:
9107: sub modify_coursegroup_membership {
9108: my ($cdom,$cnum,$membership) = @_;
9109: my $result = &put('groupmembership',$membership,$cdom,$cnum);
9110: return $result;
9111: }
9112:
9113: sub get_active_groups {
9114: my ($udom,$uname,$cdom,$cnum) = @_;
9115: my $now = time;
9116: my %groups = ();
9117: foreach my $key (keys(%env)) {
9118: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
9119: my ($start,$end) = split(/\./,$env{$key});
9120: if (($end!=0) && ($end<$now)) { next; }
9121: if (($start!=0) && ($start>$now)) { next; }
9122: if ($1 eq $cdom && $2 eq $cnum) {
9123: $groups{$3} = $env{$key} ;
9124: }
9125: }
9126: }
9127: return %groups;
9128: }
9129:
9130: sub get_group_membership {
9131: my ($cdom,$cnum,$group) = @_;
9132: return(&dump('groupmembership',$cdom,$cnum,$group));
9133: }
9134:
9135: sub get_users_groups {
9136: my ($udom,$uname,$courseid) = @_;
9137: my @usersgroups;
9138: my $cachetime=1800;
9139:
9140: my $hashid="$udom:$uname:$courseid";
9141: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
9142: if (defined($cached)) {
9143: @usersgroups = split(/:/,$grouplist);
9144: } else {
9145: $grouplist = '';
9146: my $courseurl = &courseid_to_courseurl($courseid);
9147: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
9148: my $access_end = $env{'course.'.$courseid.
9149: '.default_enrollment_end_date'};
9150: my $now = time;
9151: foreach my $key (keys(%roleshash)) {
9152: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
9153: my $group = $1;
9154: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
9155: my $start = $2;
9156: my $end = $1;
9157: if ($start == -1) { next; } # deleted from group
9158: if (($start!=0) && ($start>$now)) { next; }
9159: if (($end!=0) && ($end<$now)) {
9160: if ($access_end && $access_end < $now) {
9161: if ($access_end - $end < 86400) {
9162: push(@usersgroups,$group);
9163: }
9164: }
9165: next;
9166: }
9167: push(@usersgroups,$group);
9168: }
9169: }
9170: }
9171: @usersgroups = &sort_course_groups($courseid,@usersgroups);
9172: $grouplist = join(':',@usersgroups);
9173: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
9174: }
9175: return @usersgroups;
9176: }
9177:
9178: sub devalidate_getgroups_cache {
9179: my ($udom,$uname,$cdom,$cnum)=@_;
9180: my $courseid = $cdom.'_'.$cnum;
9181:
9182: my $hashid="$udom:$uname:$courseid";
9183: &devalidate_cache_new('getgroups',$hashid);
9184: }
9185:
9186: # ------------------------------------------------------------------ Plain Text
9187:
9188: sub plaintext {
9189: my ($short,$type,$cid,$forcedefault) = @_;
9190: if ($short =~ m{^cr/}) {
9191: return (split('/',$short))[-1];
9192: }
9193: if (!defined($cid)) {
9194: $cid = $env{'request.course.id'};
9195: }
9196: my %rolenames = (
9197: Course => 'std',
9198: Community => 'alt1',
9199: Placement => 'std',
9200: );
9201: if ($cid ne '') {
9202: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
9203: unless ($forcedefault) {
9204: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
9205: &Apache::lonlocal::mt_escape(\$roletext);
9206: return &Apache::lonlocal::mt($roletext);
9207: }
9208: }
9209: }
9210: if ((defined($type)) && (defined($rolenames{$type})) &&
9211: (defined($rolenames{$type})) &&
9212: (defined($prp{$short}{$rolenames{$type}}))) {
9213: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
9214: } elsif ($cid ne '') {
9215: my $crstype = $env{'course.'.$cid.'.type'};
9216: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
9217: (defined($prp{$short}{$rolenames{$crstype}}))) {
9218: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
9219: }
9220: }
9221: return &Apache::lonlocal::mt($prp{$short}{'std'});
9222: }
9223:
9224: # ----------------------------------------------------------------- Assign Role
9225:
9226: sub assignrole {
9227: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
9228: $context)=@_;
9229: my $mrole;
9230: if ($role =~ /^cr\//) {
9231: my $cwosec=$url;
9232: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
9233: unless (&allowed('ccr',$cwosec)) {
9234: my $refused = 1;
9235: if ($context eq 'requestcourses') {
9236: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
9237: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
9238: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
9239: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
9240: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9241: if ($crsenv{'internal.courseowner'} eq
9242: $env{'user.name'}.':'.$env{'user.domain'}) {
9243: $refused = '';
9244: }
9245: }
9246: }
9247: }
9248: }
9249: if ($refused) {
9250: &logthis('Refused custom assignrole: '.
9251: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
9252: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
9253: return 'refused';
9254: }
9255: }
9256: $mrole='cr';
9257: } elsif ($role =~ /^gr\//) {
9258: my $cwogrp=$url;
9259: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
9260: unless (&allowed('mdg',$cwogrp)) {
9261: &logthis('Refused group assignrole: '.
9262: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
9263: $env{'user.name'}.' at '.$env{'user.domain'});
9264: return 'refused';
9265: }
9266: $mrole='gr';
9267: } else {
9268: my $cwosec=$url;
9269: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
9270: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
9271: my $refused;
9272: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
9273: if (!(&allowed('c'.$role,$url))) {
9274: $refused = 1;
9275: }
9276: } else {
9277: $refused = 1;
9278: }
9279: if ($refused) {
9280: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
9281: if (!$selfenroll && $context eq 'course') {
9282: my %crsenv;
9283: if ($role eq 'cc' || $role eq 'co') {
9284: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9285: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
9286: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
9287: if ($crsenv{'internal.courseowner'} eq
9288: $env{'user.name'}.':'.$env{'user.domain'}) {
9289: $refused = '';
9290: }
9291: }
9292: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
9293: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
9294: if ($crsenv{'internal.courseowner'} eq
9295: $env{'user.name'}.':'.$env{'user.domain'}) {
9296: $refused = '';
9297: }
9298: }
9299: }
9300: }
9301: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
9302: $refused = '';
9303: } elsif ($context eq 'requestcourses') {
9304: my @possroles = ('st','ta','ep','in','cc','co');
9305: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
9306: my $wrongcc;
9307: if ($cnum =~ /^$match_community$/) {
9308: $wrongcc = 1 if ($role eq 'cc');
9309: } else {
9310: $wrongcc = 1 if ($role eq 'co');
9311: }
9312: unless ($wrongcc) {
9313: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9314: if ($crsenv{'internal.courseowner'} eq
9315: $env{'user.name'}.':'.$env{'user.domain'}) {
9316: $refused = '';
9317: }
9318: }
9319: }
9320: } elsif ($context eq 'requestauthor') {
9321: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
9322: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
9323: if ($env{'environment.requestauthor'} eq 'automatic') {
9324: $refused = '';
9325: } else {
9326: my %domdefaults = &get_domain_defaults($udom);
9327: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
9328: my $checkbystatus;
9329: if ($env{'user.adv'}) {
9330: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
9331: if ($disposition eq 'automatic') {
9332: $refused = '';
9333: } elsif ($disposition eq '') {
9334: $checkbystatus = 1;
9335: }
9336: } else {
9337: $checkbystatus = 1;
9338: }
9339: if ($checkbystatus) {
9340: if ($env{'environment.inststatus'}) {
9341: my @inststatuses = split(/,/,$env{'environment.inststatus'});
9342: foreach my $type (@inststatuses) {
9343: if (($type ne '') &&
9344: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
9345: $refused = '';
9346: }
9347: }
9348: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
9349: $refused = '';
9350: }
9351: }
9352: }
9353: }
9354: }
9355: }
9356: if ($refused) {
9357: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
9358: ' '.$role.' '.$end.' '.$start.' by '.
9359: $env{'user.name'}.' at '.$env{'user.domain'});
9360: return 'refused';
9361: }
9362: }
9363: } elsif ($role eq 'au') {
9364: if ($url ne '/'.$udom.'/') {
9365: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
9366: ' to assign author role for '.$uname.':'.$udom.
9367: ' in domain: '.$url.' refused (wrong domain).');
9368: return 'refused';
9369: }
9370: }
9371: $mrole=$role;
9372: }
9373: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
9374: "$udom:$uname:$url".'_'."$mrole=$role";
9375: if ($end) { $command.='_'.$end; }
9376: if ($start) {
9377: if ($end) {
9378: $command.='_'.$start;
9379: } else {
9380: $command.='_0_'.$start;
9381: }
9382: }
9383: my $origstart = $start;
9384: my $origend = $end;
9385: my $delflag;
9386: # actually delete
9387: if ($deleteflag) {
9388: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
9389: # modify command to delete the role
9390: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
9391: "$udom:$uname:$url".'_'."$mrole";
9392: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
9393: # set start and finish to negative values for userrolelog
9394: $start=-1;
9395: $end=-1;
9396: $delflag = 1;
9397: }
9398: }
9399: # send command
9400: my $answer=&reply($command,&homeserver($uname,$udom));
9401: # log new user role if status is ok
9402: if ($answer eq 'ok') {
9403: &userrolelog($role,$uname,$udom,$url,$start,$end);
9404: if (($role eq 'cc') || ($role eq 'in') ||
9405: ($role eq 'ep') || ($role eq 'ad') ||
9406: ($role eq 'ta') || ($role eq 'st') ||
9407: ($role=~/^cr/) || ($role eq 'gr') ||
9408: ($role eq 'co')) {
9409: # for course roles, perform group memberships changes triggered by role change.
9410: unless ($role =~ /^gr/) {
9411: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
9412: $origstart,$selfenroll,$context);
9413: }
9414: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9415: $selfenroll,$context);
9416: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
9417: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
9418: ($role eq 'da')) {
9419: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9420: $context);
9421: } elsif (($role eq 'ca') || ($role eq 'aa')) {
9422: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9423: $context);
9424: }
9425: if ($role eq 'cc') {
9426: &autoupdate_coowners($url,$end,$start,$uname,$udom);
9427: }
9428: }
9429: return $answer;
9430: }
9431:
9432: sub autoupdate_coowners {
9433: my ($url,$end,$start,$uname,$udom) = @_;
9434: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
9435: if (($cdom ne '') && ($cnum ne '')) {
9436: my $now = time;
9437: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
9438: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
9439: my %coursehash = &coursedescription($cdom.'_'.$cnum);
9440: my $instcode = $coursehash{'internal.coursecode'};
9441: if ($instcode ne '') {
9442: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
9443: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
9444: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
9445: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
9446: if ($result eq 'valid') {
9447: if ($coursehash{'internal.co-owners'}) {
9448: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
9449: push(@newcoowners,$coowner);
9450: }
9451: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
9452: push(@newcoowners,$uname.':'.$udom);
9453: }
9454: @newcoowners = sort(@newcoowners);
9455: } else {
9456: push(@newcoowners,$uname.':'.$udom);
9457: }
9458: } else {
9459: if ($coursehash{'internal.co-owners'}) {
9460: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
9461: unless ($coowner eq $uname.':'.$udom) {
9462: push(@newcoowners,$coowner);
9463: }
9464: }
9465: unless (@newcoowners > 0) {
9466: $delcoowners = 1;
9467: $coowners = '';
9468: }
9469: }
9470: }
9471: if (@newcoowners || $delcoowners) {
9472: &store_coowners($cdom,$cnum,$coursehash{'home'},
9473: $delcoowners,@newcoowners);
9474: }
9475: }
9476: }
9477: }
9478: }
9479: }
9480: }
9481:
9482: sub store_coowners {
9483: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
9484: my $cid = $cdom.'_'.$cnum;
9485: my ($coowners,$delresult,$putresult);
9486: if (@newcoowners) {
9487: $coowners = join(',',@newcoowners);
9488: my %coownershash = (
9489: 'internal.co-owners' => $coowners,
9490: );
9491: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
9492: if ($putresult eq 'ok') {
9493: if ($env{'course.'.$cid.'.num'} eq $cnum) {
9494: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
9495: }
9496: }
9497: }
9498: if ($delcoowners) {
9499: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
9500: if ($delresult eq 'ok') {
9501: if ($env{'course.'.$cid.'.internal.co-owners'}) {
9502: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
9503: }
9504: }
9505: }
9506: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
9507: my %crsinfo =
9508: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
9509: if (ref($crsinfo{$cid}) eq 'HASH') {
9510: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
9511: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
9512: }
9513: }
9514: }
9515:
9516: # -------------------------------------------------- Modify user authentication
9517: # Overrides without validation
9518:
9519: sub modifyuserauth {
9520: my ($udom,$uname,$umode,$upass)=@_;
9521: my $uhome=&homeserver($uname,$udom);
9522: unless (&allowed('mau',$udom)) { return 'refused'; }
9523: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
9524: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
9525: ' in domain '.$env{'request.role.domain'});
9526: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
9527: &escape($upass),$uhome);
9528: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
9529: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
9530: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
9531: &log($udom,,$uname,$uhome,
9532: 'Authentication changed by '.$env{'user.domain'}.', '.
9533: $env{'user.name'}.', '.$umode.
9534: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
9535: unless ($reply eq 'ok') {
9536: &logthis('Authentication mode error: '.$reply);
9537: return 'error: '.$reply;
9538: }
9539: return 'ok';
9540: }
9541:
9542: # --------------------------------------------------------------- Modify a user
9543:
9544: sub modifyuser {
9545: my ($udom, $uname, $uid,
9546: $umode, $upass, $first,
9547: $middle, $last, $gene,
9548: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
9549: $udom= &LONCAPA::clean_domain($udom);
9550: $uname=&LONCAPA::clean_username($uname);
9551: my $showcandelete = 'none';
9552: if (ref($candelete) eq 'ARRAY') {
9553: if (@{$candelete} > 0) {
9554: $showcandelete = join(', ',@{$candelete});
9555: }
9556: }
9557: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
9558: $umode.', '.$first.', '.$middle.', '.
9559: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
9560: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
9561: ' desiredhome not specified').
9562: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
9563: ' in domain '.$env{'request.role.domain'});
9564: my $uhome=&homeserver($uname,$udom,'true');
9565: my $newuser;
9566: if ($uhome eq 'no_host') {
9567: $newuser = 1;
9568: }
9569: # ----------------------------------------------------------------- Create User
9570: if (($uhome eq 'no_host') &&
9571: (($umode && $upass) || ($umode eq 'localauth'))) {
9572: my $unhome='';
9573: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
9574: $unhome = $desiredhome;
9575: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
9576: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
9577: } else { # load balancing routine for determining $unhome
9578: my $loadm=10000000;
9579: my %servers = &get_servers($udom,'library');
9580: foreach my $tryserver (keys(%servers)) {
9581: my $answer=reply('load',$tryserver);
9582: if (($answer=~/\d+/) && ($answer<$loadm)) {
9583: $loadm=$answer;
9584: $unhome=$tryserver;
9585: }
9586: }
9587: }
9588: if (($unhome eq '') || ($unhome eq 'no_host')) {
9589: return 'error: unable to find a home server for '.$uname.
9590: ' in domain '.$udom;
9591: }
9592: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
9593: &escape($upass),$unhome);
9594: unless ($reply eq 'ok') {
9595: return 'error: '.$reply;
9596: }
9597: $uhome=&homeserver($uname,$udom,'true');
9598: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
9599: return 'error: unable verify users home machine.';
9600: }
9601: } # End of creation of new user
9602: # ---------------------------------------------------------------------- Add ID
9603: if ($uid) {
9604: $uid=~tr/A-Z/a-z/;
9605: my %uidhash=&idrget($udom,$uname);
9606: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
9607: && (!$forceid)) {
9608: unless ($uid eq $uidhash{$uname}) {
9609: return 'error: user id "'.$uid.'" does not match '.
9610: 'current user id "'.$uidhash{$uname}.'".';
9611: }
9612: } else {
9613: &idput($udom,{$uname => $uid},$uhome,'ids');
9614: }
9615: }
9616: # -------------------------------------------------------------- Add names, etc
9617: my @tmp=&get('environment',
9618: ['firstname','middlename','lastname','generation','id',
9619: 'permanentemail','inststatus'],
9620: $udom,$uname);
9621: my (%names,%oldnames);
9622: if ($tmp[0] =~ m/^error:.*/) {
9623: %names=();
9624: } else {
9625: %names = @tmp;
9626: %oldnames = %names;
9627: }
9628: #
9629: # If name, email and/or uid are blank (e.g., because an uploaded file
9630: # of users did not contain them), do not overwrite existing values
9631: # unless field is in $candelete array ref.
9632: #
9633:
9634: my @fields = ('firstname','middlename','lastname','generation',
9635: 'permanentemail','id');
9636: my %newvalues;
9637: if (ref($candelete) eq 'ARRAY') {
9638: foreach my $field (@fields) {
9639: if (grep(/^\Q$field\E$/,@{$candelete})) {
9640: if ($field eq 'firstname') {
9641: $names{$field} = $first;
9642: } elsif ($field eq 'middlename') {
9643: $names{$field} = $middle;
9644: } elsif ($field eq 'lastname') {
9645: $names{$field} = $last;
9646: } elsif ($field eq 'generation') {
9647: $names{$field} = $gene;
9648: } elsif ($field eq 'permanentemail') {
9649: $names{$field} = $email;
9650: } elsif ($field eq 'id') {
9651: $names{$field} = $uid;
9652: }
9653: }
9654: }
9655: }
9656: if ($first) { $names{'firstname'} = $first; }
9657: if (defined($middle)) { $names{'middlename'} = $middle; }
9658: if ($last) { $names{'lastname'} = $last; }
9659: if (defined($gene)) { $names{'generation'} = $gene; }
9660: if ($email) {
9661: $email=~s/[^\w\@\.\-\,]//gs;
9662: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
9663: }
9664: if ($uid) { $names{'id'} = $uid; }
9665: if (defined($inststatus)) {
9666: $names{'inststatus'} = '';
9667: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
9668: if (ref($usertypes) eq 'HASH') {
9669: my @okstatuses;
9670: foreach my $item (split(/:/,$inststatus)) {
9671: if (defined($usertypes->{$item})) {
9672: push(@okstatuses,$item);
9673: }
9674: }
9675: if (@okstatuses) {
9676: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
9677: }
9678: }
9679: }
9680: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
9681: $umode.', '.$first.', '.$middle.', '.
9682: $last.', '.$gene.', '.$email.', '.$inststatus;
9683: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
9684: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
9685: } else {
9686: $logmsg .= ' during self creation';
9687: }
9688: my $changed;
9689: if ($newuser) {
9690: $changed = 1;
9691: } else {
9692: foreach my $field (@fields) {
9693: if ($names{$field} ne $oldnames{$field}) {
9694: $changed = 1;
9695: last;
9696: }
9697: }
9698: }
9699: unless ($changed) {
9700: $logmsg = 'No changes in user information needed for: '.$logmsg;
9701: &logthis($logmsg);
9702: return 'ok';
9703: }
9704: my $reply = &put('environment', \%names, $udom,$uname);
9705: if ($reply ne 'ok') {
9706: return 'error: '.$reply;
9707: }
9708: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
9709: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
9710: }
9711: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
9712: &devalidate_cache_new('namescache',$uname.':'.$udom);
9713: $logmsg = 'Success modifying user '.$logmsg;
9714: &logthis($logmsg);
9715: return 'ok';
9716: }
9717:
9718: # -------------------------------------------------------------- Modify student
9719:
9720: sub modifystudent {
9721: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
9722: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
9723: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
9724: if (!$cid) {
9725: unless ($cid=$env{'request.course.id'}) {
9726: return 'not_in_class';
9727: }
9728: }
9729: # --------------------------------------------------------------- Make the user
9730: my $reply=&modifyuser
9731: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
9732: $desiredhome,$email,$inststatus);
9733: unless ($reply eq 'ok') { return $reply; }
9734: # This will cause &modify_student_enrollment to get the uid from the
9735: # student's environment
9736: $uid = undef if (!$forceid);
9737: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
9738: $gene,$usec,$end,$start,$type,$locktype,
9739: $cid,$selfenroll,$context,$credits,$instsec);
9740: return $reply;
9741: }
9742:
9743: sub modify_student_enrollment {
9744: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
9745: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
9746: my ($cdom,$cnum,$chome);
9747: if (!$cid) {
9748: unless ($cid=$env{'request.course.id'}) {
9749: return 'not_in_class';
9750: }
9751: $cdom=$env{'course.'.$cid.'.domain'};
9752: $cnum=$env{'course.'.$cid.'.num'};
9753: } else {
9754: ($cdom,$cnum)=split(/_/,$cid);
9755: }
9756: $chome=$env{'course.'.$cid.'.home'};
9757: if (!$chome) {
9758: $chome=&homeserver($cnum,$cdom);
9759: }
9760: if (!$chome) { return 'unknown_course'; }
9761: # Make sure the user exists
9762: my $uhome=&homeserver($uname,$udom);
9763: if (($uhome eq '') || ($uhome eq 'no_host')) {
9764: return 'error: no such user';
9765: }
9766: # Get student data if we were not given enough information
9767: if (!defined($first) || $first eq '' ||
9768: !defined($last) || $last eq '' ||
9769: !defined($uid) || $uid eq '' ||
9770: !defined($middle) || $middle eq '' ||
9771: !defined($gene) || $gene eq '') {
9772: # They did not supply us with enough data to enroll the student, so
9773: # we need to pick up more information.
9774: my %tmp = &get('environment',
9775: ['firstname','middlename','lastname', 'generation','id']
9776: ,$udom,$uname);
9777:
9778: #foreach my $key (keys(%tmp)) {
9779: # &logthis("key $key = ".$tmp{$key});
9780: #}
9781: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
9782: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
9783: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
9784: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
9785: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
9786: }
9787: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
9788: my $user = "$uname:$udom";
9789: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
9790: my $reply=cput('classlist',
9791: {$user =>
9792: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
9793: $cdom,$cnum);
9794: if (($reply eq 'ok') || ($reply eq 'delayed')) {
9795: &devalidate_getsection_cache($udom,$uname,$cid);
9796: } else {
9797: return 'error: '.$reply;
9798: }
9799: # Add student role to user
9800: my $uurl='/'.$cid;
9801: $uurl=~s/\_/\//g;
9802: if ($usec) {
9803: $uurl.='/'.$usec;
9804: }
9805: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
9806: $selfenroll,$context);
9807: if ($result ne 'ok') {
9808: if ($old_entry{$user} ne '') {
9809: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
9810: } else {
9811: $reply = &del('classlist',[$user],$cdom,$cnum);
9812: }
9813: }
9814: return $result;
9815: }
9816:
9817: sub format_name {
9818: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
9819: my $name;
9820: if ($first ne 'lastname') {
9821: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
9822: } else {
9823: if ($lastname=~/\S/) {
9824: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
9825: $name=~s/\s+,/,/;
9826: } else {
9827: $name.= $firstname.' '.$middlename.' '.$generation;
9828: }
9829: }
9830: $name=~s/^\s+//;
9831: $name=~s/\s+$//;
9832: $name=~s/\s+/ /g;
9833: return $name;
9834: }
9835:
9836: # ------------------------------------------------- Write to course preferences
9837:
9838: sub writecoursepref {
9839: my ($courseid,%prefs)=@_;
9840: $courseid=~s/^\///;
9841: $courseid=~s/\_/\//g;
9842: my ($cdomain,$cnum)=split(/\//,$courseid);
9843: my $chome=homeserver($cnum,$cdomain);
9844: if (($chome eq '') || ($chome eq 'no_host')) {
9845: return 'error: no such course';
9846: }
9847: my $cstring='';
9848: foreach my $pref (keys(%prefs)) {
9849: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
9850: }
9851: $cstring=~s/\&$//;
9852: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
9853: }
9854:
9855: # ---------------------------------------------------------- Make/modify course
9856:
9857: sub createcourse {
9858: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
9859: $course_owner,$crstype,$cnum,$context,$category)=@_;
9860: $url=&declutter($url);
9861: my $cid='';
9862: if ($context eq 'requestcourses') {
9863: my $can_create = 0;
9864: my ($ownername,$ownerdom) = split(':',$course_owner);
9865: if ($udom eq $ownerdom) {
9866: if (&usertools_access($ownername,$ownerdom,$category,undef,
9867: $context)) {
9868: $can_create = 1;
9869: }
9870: } else {
9871: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
9872: $category);
9873: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
9874: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
9875: if (@curr > 0) {
9876: my @options = qw(approval validate autolimit);
9877: my $optregex = join('|',@options);
9878: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
9879: $can_create = 1;
9880: }
9881: }
9882: }
9883: }
9884: if ($can_create) {
9885: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
9886: unless (&allowed('ccc',$udom)) {
9887: return 'refused';
9888: }
9889: }
9890: } else {
9891: return 'refused';
9892: }
9893: } elsif (!&allowed('ccc',$udom)) {
9894: return 'refused';
9895: }
9896: # --------------------------------------------------------------- Get Unique ID
9897: my $uname;
9898: if ($cnum =~ /^$match_courseid$/) {
9899: my $chome=&homeserver($cnum,$udom,'true');
9900: if (($chome eq '') || ($chome eq 'no_host')) {
9901: $uname = $cnum;
9902: } else {
9903: $uname = &generate_coursenum($udom,$crstype);
9904: }
9905: } else {
9906: $uname = &generate_coursenum($udom,$crstype);
9907: }
9908: return $uname if ($uname =~ /^error/);
9909: # -------------------------------------------------- Check supplied server name
9910: if (!defined($course_server)) {
9911: if (defined(&domain($udom,'primary'))) {
9912: $course_server = &domain($udom,'primary');
9913: } else {
9914: $course_server = $env{'user.home'};
9915: }
9916: }
9917: my %host_servers =
9918: &Apache::lonnet::get_servers($udom,'library');
9919: unless ($host_servers{$course_server}) {
9920: return 'error: invalid home server for course: '.$course_server;
9921: }
9922: # ------------------------------------------------------------- Make the course
9923: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
9924: $course_server);
9925: unless ($reply eq 'ok') { return 'error: '.$reply; }
9926: my $uhome=&homeserver($uname,$udom,'true');
9927: if (($uhome eq '') || ($uhome eq 'no_host')) {
9928: return 'error: no such course';
9929: }
9930: # ----------------------------------------------------------------- Course made
9931: # log existence
9932: my $now = time;
9933: my $newcourse = {
9934: $udom.'_'.$uname => {
9935: description => $description,
9936: inst_code => $inst_code,
9937: owner => $course_owner,
9938: type => $crstype,
9939: creator => $env{'user.name'}.':'.
9940: $env{'user.domain'},
9941: created => $now,
9942: context => $context,
9943: },
9944: };
9945: &courseidput($udom,$newcourse,$uhome,'notime');
9946: # set toplevel url
9947: my $topurl=$url;
9948: unless ($nonstandard) {
9949: # ------------------------------------------ For standard courses, make top url
9950: my $mapurl=&clutter($url);
9951: if ($mapurl eq '/res/') { $mapurl=''; }
9952: $env{'form.initmap'}=(<<ENDINITMAP);
9953: <map>
9954: <resource id="1" type="start"></resource>
9955: <resource id="2" src="$mapurl"></resource>
9956: <resource id="3" type="finish"></resource>
9957: <link index="1" from="1" to="2"></link>
9958: <link index="2" from="2" to="3"></link>
9959: </map>
9960: ENDINITMAP
9961: $topurl=&declutter(
9962: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
9963: );
9964: }
9965: # ----------------------------------------------------------- Write preferences
9966: &writecoursepref($udom.'_'.$uname,
9967: ('description' => $description,
9968: 'url' => $topurl,
9969: 'internal.creator' => $env{'user.name'}.':'.
9970: $env{'user.domain'},
9971: 'internal.created' => $now,
9972: 'internal.creationcontext' => $context)
9973: );
9974: return '/'.$udom.'/'.$uname;
9975: }
9976:
9977: # ------------------------------------------------------------------- Create ID
9978: sub generate_coursenum {
9979: my ($udom,$crstype) = @_;
9980: my $domdesc = &domain($udom);
9981: return 'error: invalid domain' if ($domdesc eq '');
9982: my $first;
9983: if ($crstype eq 'Community') {
9984: $first = '0';
9985: } else {
9986: $first = int(1+rand(9));
9987: }
9988: my $uname=$first.
9989: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
9990: substr($$.time,0,5).unpack("H8",pack("I32",time)).
9991: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
9992: # ----------------------------------------------- Make sure that does not exist
9993: my $uhome=&homeserver($uname,$udom,'true');
9994: unless (($uhome eq '') || ($uhome eq 'no_host')) {
9995: if ($crstype eq 'Community') {
9996: $first = '0';
9997: } else {
9998: $first = int(1+rand(9));
9999: }
10000: $uname=$first.
10001: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10002: substr($$.time,0,5).unpack("H8",pack("I32",time)).
10003: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10004: $uhome=&homeserver($uname,$udom,'true');
10005: unless (($uhome eq '') || ($uhome eq 'no_host')) {
10006: return 'error: unable to generate unique course-ID';
10007: }
10008: }
10009: return $uname;
10010: }
10011:
10012: sub is_course {
10013: my ($cdom, $cnum) = scalar(@_) == 1 ?
10014: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
10015:
10016: return unless $cdom and $cnum;
10017:
10018: my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
10019: '.');
10020:
10021: return unless(exists($courses{$cdom.'_'.$cnum}));
10022: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10023: }
10024:
10025: sub store_userdata {
10026: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10027: my $result;
10028: if ($datakey ne '') {
10029: if (ref($storehash) eq 'HASH') {
10030: if ($udom eq '' || $uname eq '') {
10031: $udom = $env{'user.domain'};
10032: $uname = $env{'user.name'};
10033: }
10034: my $uhome=&homeserver($uname,$udom);
10035: if (($uhome eq '') || ($uhome eq 'no_host')) {
10036: $result = 'error: no_host';
10037: } else {
10038: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
10039: $storehash->{'host'} = $perlvar{'lonHostID'};
10040:
10041: my $namevalue='';
10042: foreach my $key (keys(%{$storehash})) {
10043: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10044: }
10045: $namevalue=~s/\&$//;
10046: unless ($namespace eq 'courserequests') {
10047: $datakey = &escape($datakey);
10048: }
10049: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
10050: $namevalue,$uhome);
10051: }
10052: } else {
10053: $result = 'error: data to store was not a hash reference';
10054: }
10055: } else {
10056: $result= 'error: invalid requestkey';
10057: }
10058: return $result;
10059: }
10060:
10061: # ---------------------------------------------------------- Assign Custom Role
10062:
10063: sub assigncustomrole {
10064: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10065: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10066: $end,$start,$deleteflag,$selfenroll,$context);
10067: }
10068:
10069: # ----------------------------------------------------------------- Revoke Role
10070:
10071: sub revokerole {
10072: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10073: my $now=time;
10074: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10075: }
10076:
10077: # ---------------------------------------------------------- Revoke Custom Role
10078:
10079: sub revokecustomrole {
10080: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10081: my $now=time;
10082: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10083: $deleteflag,$selfenroll,$context);
10084: }
10085:
10086: # ------------------------------------------------------------ Disk usage
10087: sub diskusage {
10088: my ($udom,$uname,$directorypath,$getpropath)=@_;
10089: $directorypath =~ s/\/$//;
10090: my $listing=&reply('du2:'.&escape($directorypath).':'
10091: .&escape($getpropath).':'.&escape($uname).':'
10092: .&escape($udom),homeserver($uname,$udom));
10093: if ($listing eq 'unknown_cmd') {
10094: if ($getpropath) {
10095: $directorypath = &propath($udom,$uname).'/'.$directorypath;
10096: }
10097: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10098: }
10099: return $listing;
10100: }
10101:
10102: sub is_locked {
10103: my ($file_name, $domain, $user, $which) = @_;
10104: my @check;
10105: my $is_locked;
10106: push (@check,$file_name);
10107: my %locked = &get('file_permissions',\@check,
10108: $env{'user.domain'},$env{'user.name'});
10109: my ($tmp)=keys(%locked);
10110: if ($tmp=~/^error:/) { undef(%locked); }
10111:
10112: if (ref($locked{$file_name}) eq 'ARRAY') {
10113: $is_locked = 'false';
10114: foreach my $entry (@{$locked{$file_name}}) {
10115: if (ref($entry) eq 'ARRAY') {
10116: $is_locked = 'true';
10117: if (ref($which) eq 'ARRAY') {
10118: push(@{$which},$entry);
10119: } else {
10120: last;
10121: }
10122: }
10123: }
10124: } else {
10125: $is_locked = 'false';
10126: }
10127: return $is_locked;
10128: }
10129:
10130: sub declutter_portfile {
10131: my ($file) = @_;
10132: $file =~ s{^(/portfolio/|portfolio/)}{/};
10133: return $file;
10134: }
10135:
10136: # ------------------------------------------------------------- Mark as Read Only
10137:
10138: sub mark_as_readonly {
10139: my ($domain,$user,$files,$what) = @_;
10140: my %current_permissions = &dump('file_permissions',$domain,$user);
10141: my ($tmp)=keys(%current_permissions);
10142: if ($tmp=~/^error:/) { undef(%current_permissions); }
10143: foreach my $file (@{$files}) {
10144: $file = &declutter_portfile($file);
10145: push(@{$current_permissions{$file}},$what);
10146: }
10147: &put('file_permissions',\%current_permissions,$domain,$user);
10148: return;
10149: }
10150:
10151: # ------------------------------------------------------------Save Selected Files
10152:
10153: sub save_selected_files {
10154: my ($user, $path, @files) = @_;
10155: my $filename = $user."savedfiles";
10156: my @other_files = &files_not_in_path($user, $path);
10157: open (OUT,'>',LONCAPA::tempdir().$filename);
10158: foreach my $file (@files) {
10159: print (OUT $env{'form.currentpath'}.$file."\n");
10160: }
10161: foreach my $file (@other_files) {
10162: print (OUT $file."\n");
10163: }
10164: close (OUT);
10165: return 'ok';
10166: }
10167:
10168: sub clear_selected_files {
10169: my ($user) = @_;
10170: my $filename = $user."savedfiles";
10171: open (OUT,'>',LONCAPA::tempdir().$filename);
10172: print (OUT undef);
10173: close (OUT);
10174: return ("ok");
10175: }
10176:
10177: sub files_in_path {
10178: my ($user, $path) = @_;
10179: my $filename = $user."savedfiles";
10180: my %return_files;
10181: open (IN,'<',LONCAPA::tempdir().$filename);
10182: while (my $line_in = <IN>) {
10183: chomp ($line_in);
10184: my @paths_and_file = split (m!/!, $line_in);
10185: my $file_part = pop (@paths_and_file);
10186: my $path_part = join ('/', @paths_and_file);
10187: $path_part.='/';
10188: my $path_and_file = $path_part.$file_part;
10189: if ($path_part eq $path) {
10190: $return_files{$file_part}= 'selected';
10191: }
10192: }
10193: close (IN);
10194: return (\%return_files);
10195: }
10196:
10197: # called in portfolio select mode, to show files selected NOT in current directory
10198: sub files_not_in_path {
10199: my ($user, $path) = @_;
10200: my $filename = $user."savedfiles";
10201: my @return_files;
10202: my $path_part;
10203: open(IN, '<',LONCAPA::tempdir().$filename);
10204: while (my $line = <IN>) {
10205: #ok, I know it's clunky, but I want it to work
10206: my @paths_and_file = split(m|/|, $line);
10207: my $file_part = pop(@paths_and_file);
10208: chomp($file_part);
10209: my $path_part = join('/', @paths_and_file);
10210: $path_part .= '/';
10211: my $path_and_file = $path_part.$file_part;
10212: if ($path_part ne $path) {
10213: push(@return_files, ($path_and_file));
10214: }
10215: }
10216: close(OUT);
10217: return (@return_files);
10218: }
10219:
10220: #------------------------------Submitted/Handedback Portfolio Files Versioning
10221:
10222: sub portfiles_versioning {
10223: my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
10224: my $portfolio_root = '/userfiles/portfolio';
10225: return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
10226: foreach my $file (@{$portfiles}) {
10227: &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
10228: my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
10229: my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
10230: my $getpropath = 1;
10231: my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
10232: $stu_name,$getpropath);
10233: my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
10234: my $new_answer =
10235: &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
10236: if ($new_answer ne 'problem getting file') {
10237: push(@{$versioned_portfiles}, $directory.$new_answer);
10238: &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
10239: [$symb,$env{'request.course.id'},'graded']);
10240: }
10241: }
10242: }
10243:
10244: sub get_next_version {
10245: my ($answer_name, $answer_ext, $dir_list) = @_;
10246: my $version;
10247: if (ref($dir_list) eq 'ARRAY') {
10248: foreach my $row (@{$dir_list}) {
10249: my ($file) = split(/\&/,$row,2);
10250: my ($file_name,$file_version,$file_ext) =
10251: &file_name_version_ext($file);
10252: if (($file_name eq $answer_name) &&
10253: ($file_ext eq $answer_ext)) {
10254: # gets here if filename and extension match,
10255: # regardless of version
10256: if ($file_version ne '') {
10257: # a versioned file is found so save it for later
10258: if ($file_version > $version) {
10259: $version = $file_version;
10260: }
10261: }
10262: }
10263: }
10264: }
10265: $version ++;
10266: return($version);
10267: }
10268:
10269: sub version_selected_portfile {
10270: my ($domain,$stu_name,$directory,$file_name,$version) = @_;
10271: my ($answer_name,$answer_ver,$answer_ext) =
10272: &file_name_version_ext($file_name);
10273: my $new_answer;
10274: $env{'form.copy'} =
10275: &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
10276: if($env{'form.copy'} eq '-1') {
10277: $new_answer = 'problem getting file';
10278: } else {
10279: $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
10280: my $copy_result =
10281: &finishuserfileupload($stu_name,$domain,'copy',
10282: '/portfolio'.$directory.$new_answer);
10283: }
10284: undef($env{'form.copy'});
10285: return ($new_answer);
10286: }
10287:
10288: sub file_name_version_ext {
10289: my ($file)=@_;
10290: my @file_parts = split(/\./, $file);
10291: my ($name,$version,$ext);
10292: if (@file_parts > 1) {
10293: $ext=pop(@file_parts);
10294: if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
10295: $version=pop(@file_parts);
10296: }
10297: $name=join('.',@file_parts);
10298: } else {
10299: $name=join('.',@file_parts);
10300: }
10301: return($name,$version,$ext);
10302: }
10303:
10304: #----------------------------------------------Get portfolio file permissions
10305:
10306: sub get_portfile_permissions {
10307: my ($domain,$user) = @_;
10308: my %current_permissions = &dump('file_permissions',$domain,$user);
10309: my ($tmp)=keys(%current_permissions);
10310: if ($tmp=~/^error:/) { undef(%current_permissions); }
10311: return \%current_permissions;
10312: }
10313:
10314: #---------------------------------------------Get portfolio file access controls
10315:
10316: sub get_access_controls {
10317: my ($current_permissions,$group,$file) = @_;
10318: my %access;
10319: my $real_file = $file;
10320: $file =~ s/\.meta$//;
10321: if (defined($file)) {
10322: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
10323: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
10324: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
10325: }
10326: }
10327: } else {
10328: foreach my $key (keys(%{$current_permissions})) {
10329: if ($key =~ /\0accesscontrol$/) {
10330: if (defined($group)) {
10331: if ($key !~ m-^\Q$group\E/-) {
10332: next;
10333: }
10334: }
10335: my ($fullpath) = split(/\0/,$key);
10336: if (ref($$current_permissions{$key}) eq 'HASH') {
10337: foreach my $control (keys(%{$$current_permissions{$key}})) {
10338: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
10339: }
10340: }
10341: }
10342: }
10343: }
10344: return %access;
10345: }
10346:
10347: sub modify_access_controls {
10348: my ($file_name,$changes,$domain,$user)=@_;
10349: my ($outcome,$deloutcome);
10350: my %store_permissions;
10351: my %new_values;
10352: my %new_control;
10353: my %translation;
10354: my @deletions = ();
10355: my $now = time;
10356: if (exists($$changes{'activate'})) {
10357: if (ref($$changes{'activate'}) eq 'HASH') {
10358: my @newitems = sort(keys(%{$$changes{'activate'}}));
10359: my $numnew = scalar(@newitems);
10360: for (my $i=0; $i<$numnew; $i++) {
10361: my $newkey = $newitems[$i];
10362: my $newid = &Apache::loncommon::get_cgi_id();
10363: if ($newkey =~ /^\d+:/) {
10364: $newkey =~ s/^(\d+)/$newid/;
10365: $translation{$1} = $newid;
10366: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
10367: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
10368: $translation{$1} = $newid;
10369: }
10370: $new_values{$file_name."\0".$newkey} =
10371: $$changes{'activate'}{$newitems[$i]};
10372: $new_control{$newkey} = $now;
10373: }
10374: }
10375: }
10376: my %todelete;
10377: my %changed_items;
10378: foreach my $action ('delete','update') {
10379: if (exists($$changes{$action})) {
10380: if (ref($$changes{$action}) eq 'HASH') {
10381: foreach my $key (keys(%{$$changes{$action}})) {
10382: my ($itemnum) = ($key =~ /^([^:]+):/);
10383: if ($action eq 'delete') {
10384: $todelete{$itemnum} = 1;
10385: } else {
10386: $changed_items{$itemnum} = $key;
10387: }
10388: }
10389: }
10390: }
10391: }
10392: # get lock on access controls for file.
10393: my $lockhash = {
10394: $file_name."\0".'locked_access_records' => $env{'user.name'}.
10395: ':'.$env{'user.domain'},
10396: };
10397: my $tries = 0;
10398: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10399:
10400: while (($gotlock ne 'ok') && $tries < 10) {
10401: $tries ++;
10402: sleep(0.1);
10403: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10404: }
10405: if ($gotlock eq 'ok') {
10406: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
10407: my ($tmp)=keys(%curr_permissions);
10408: if ($tmp=~/^error:/) { undef(%curr_permissions); }
10409: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
10410: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
10411: if (ref($curr_controls) eq 'HASH') {
10412: foreach my $control_item (keys(%{$curr_controls})) {
10413: my ($itemnum) = ($control_item =~ /^([^:]+):/);
10414: if (defined($todelete{$itemnum})) {
10415: push(@deletions,$file_name."\0".$control_item);
10416: } else {
10417: if (defined($changed_items{$itemnum})) {
10418: $new_control{$changed_items{$itemnum}} = $now;
10419: push(@deletions,$file_name."\0".$control_item);
10420: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
10421: } else {
10422: $new_control{$control_item} = $$curr_controls{$control_item};
10423: }
10424: }
10425: }
10426: }
10427: }
10428: my ($group);
10429: if (&is_course($domain,$user)) {
10430: ($group,my $file) = split(/\//,$file_name,2);
10431: }
10432: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
10433: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
10434: $outcome = &put('file_permissions',\%new_values,$domain,$user);
10435: # remove lock
10436: my @del_lock = ($file_name."\0".'locked_access_records');
10437: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
10438: my $sqlresult =
10439: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
10440: $group);
10441: } else {
10442: $outcome = "error: could not obtain lockfile\n";
10443: }
10444: return ($outcome,$deloutcome,\%new_values,\%translation);
10445: }
10446:
10447: sub make_public_indefinitely {
10448: my (@requrl) = @_;
10449: return &automated_portfile_access('public',\@requrl);
10450: }
10451:
10452: sub automated_portfile_access {
10453: my ($accesstype,$addsref,$delsref,$info) = @_;
10454: unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
10455: return 'invalid';
10456: }
10457: my %urls;
10458: if (ref($addsref) eq 'ARRAY') {
10459: foreach my $requrl (@{$addsref}) {
10460: if (&is_portfolio_url($requrl)) {
10461: unless (exists($urls{$requrl})) {
10462: $urls{$requrl} = 'add';
10463: }
10464: }
10465: }
10466: }
10467: if (ref($delsref) eq 'ARRAY') {
10468: foreach my $requrl (@{$delsref}) {
10469: if (&is_portfolio_url($requrl)) {
10470: unless (exists($urls{$requrl})) {
10471: $urls{$requrl} = 'delete';
10472: }
10473: }
10474: }
10475: }
10476: unless (keys(%urls)) {
10477: return 'invalid';
10478: }
10479: my $ip;
10480: if ($accesstype eq 'ip') {
10481: if (ref($info) eq 'HASH') {
10482: if ($info->{'ip'} ne '') {
10483: $ip = $info->{'ip'};
10484: }
10485: }
10486: if ($ip eq '') {
10487: return 'invalid';
10488: }
10489: }
10490: my $errors;
10491: my $now = time;
10492: my %current_perms;
10493: foreach my $requrl (sort(keys(%urls))) {
10494: my $action;
10495: if ($urls{$requrl} eq 'add') {
10496: $action = 'activate';
10497: } else {
10498: $action = 'none';
10499: }
10500: my $aclnum = 0;
10501: my (undef,$udom,$unum,$file_name,$group) =
10502: &parse_portfolio_url($requrl);
10503: unless (exists($current_perms{$unum.':'.$udom})) {
10504: $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
10505: }
10506: my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
10507: $group,$file_name);
10508: foreach my $key (keys(%{$access_controls{$file_name}})) {
10509: my ($num,$scope,$end,$start) =
10510: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
10511: if ($scope eq $accesstype) {
10512: if (($start <= $now) && ($end == 0)) {
10513: if ($accesstype eq 'ip') {
10514: if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
10515: if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
10516: if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
10517: if ($urls{$requrl} eq 'add') {
10518: $action = 'none';
10519: last;
10520: } else {
10521: $action = 'delete';
10522: $aclnum = $num;
10523: last;
10524: }
10525: }
10526: }
10527: }
10528: } elsif ($accesstype eq 'public') {
10529: if ($urls{$requrl} eq 'add') {
10530: $action = 'none';
10531: last;
10532: } else {
10533: $action = 'delete';
10534: $aclnum = $num;
10535: last;
10536: }
10537: }
10538: } elsif ($accesstype eq 'public') {
10539: $action = 'update';
10540: $aclnum = $num;
10541: last;
10542: }
10543: }
10544: }
10545: if ($action eq 'none') {
10546: next;
10547: } else {
10548: my %changes;
10549: my $newend = 0;
10550: my $newstart = $now;
10551: my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
10552: $changes{$action}{$newkey} = {
10553: type => $accesstype,
10554: time => {
10555: start => $newstart,
10556: end => $newend,
10557: },
10558: };
10559: if ($accesstype eq 'ip') {
10560: $changes{$action}{$newkey}{'ip'} = [$ip];
10561: }
10562: my ($outcome,$deloutcome,$new_values,$translation) =
10563: &modify_access_controls($file_name,\%changes,$udom,$unum);
10564: unless ($outcome eq 'ok') {
10565: $errors .= $outcome.' ';
10566: }
10567: }
10568: }
10569: if ($errors) {
10570: $errors =~ s/\s$//;
10571: return $errors;
10572: } else {
10573: return 'ok';
10574: }
10575: }
10576:
10577: #------------------------------------------------------Get Marked as Read Only
10578:
10579: sub get_marked_as_readonly {
10580: my ($domain,$user,$what,$group) = @_;
10581: my $current_permissions = &get_portfile_permissions($domain,$user);
10582: my @readonly_files;
10583: my $cmp1=$what;
10584: if (ref($what)) { $cmp1=join('',@{$what}) };
10585: while (my ($file_name,$value) = each(%{$current_permissions})) {
10586: if (defined($group)) {
10587: if ($file_name !~ m-^\Q$group\E/-) {
10588: next;
10589: }
10590: }
10591: if (ref($value) eq "ARRAY"){
10592: foreach my $stored_what (@{$value}) {
10593: my $cmp2=$stored_what;
10594: if (ref($stored_what) eq 'ARRAY') {
10595: $cmp2=join('',@{$stored_what});
10596: }
10597: if ($cmp1 eq $cmp2) {
10598: push(@readonly_files, $file_name);
10599: last;
10600: } elsif (!defined($what)) {
10601: push(@readonly_files, $file_name);
10602: last;
10603: }
10604: }
10605: }
10606: }
10607: return @readonly_files;
10608: }
10609: #-----------------------------------------------------------Get Marked as Read Only Hash
10610:
10611: sub get_marked_as_readonly_hash {
10612: my ($current_permissions,$group,$what) = @_;
10613: my %readonly_files;
10614: while (my ($file_name,$value) = each(%{$current_permissions})) {
10615: if (defined($group)) {
10616: if ($file_name !~ m-^\Q$group\E/-) {
10617: next;
10618: }
10619: }
10620: if (ref($value) eq "ARRAY"){
10621: foreach my $stored_what (@{$value}) {
10622: if (ref($stored_what) eq 'ARRAY') {
10623: foreach my $lock_descriptor(@{$stored_what}) {
10624: if ($lock_descriptor eq 'graded') {
10625: $readonly_files{$file_name} = 'graded';
10626: } elsif ($lock_descriptor eq 'handback') {
10627: $readonly_files{$file_name} = 'handback';
10628: } else {
10629: if (!exists($readonly_files{$file_name})) {
10630: $readonly_files{$file_name} = 'locked';
10631: }
10632: }
10633: }
10634: }
10635: }
10636: }
10637: }
10638: return %readonly_files;
10639: }
10640: # ------------------------------------------------------------ Unmark as Read Only
10641:
10642: sub unmark_as_readonly {
10643: # unmarks $file_name (if $file_name is defined), or all files locked by $what
10644: # for portfolio submissions, $what contains [$symb,$crsid]
10645: my ($domain,$user,$what,$file_name,$group) = @_;
10646: $file_name = &declutter_portfile($file_name);
10647: my $symb_crs = $what;
10648: if (ref($what)) { $symb_crs=join('',@$what); }
10649: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
10650: my ($tmp)=keys(%current_permissions);
10651: if ($tmp=~/^error:/) { undef(%current_permissions); }
10652: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
10653: foreach my $file (@readonly_files) {
10654: my $clean_file = &declutter_portfile($file);
10655: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
10656: my $current_locks = $current_permissions{$file};
10657: my @new_locks;
10658: my @del_keys;
10659: if (ref($current_locks) eq "ARRAY"){
10660: foreach my $locker (@{$current_locks}) {
10661: my $compare=$locker;
10662: if (ref($locker) eq 'ARRAY') {
10663: $compare=join('',@{$locker});
10664: if ($compare ne $symb_crs) {
10665: push(@new_locks, $locker);
10666: }
10667: }
10668: }
10669: if (scalar(@new_locks) > 0) {
10670: $current_permissions{$file} = \@new_locks;
10671: } else {
10672: push(@del_keys, $file);
10673: &del('file_permissions',\@del_keys, $domain, $user);
10674: delete($current_permissions{$file});
10675: }
10676: }
10677: }
10678: &put('file_permissions',\%current_permissions,$domain,$user);
10679: return;
10680: }
10681:
10682: # ------------------------------------------------------------ Directory lister
10683:
10684: sub dirlist {
10685: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
10686: $uri=~s/^\///;
10687: $uri=~s/\/$//;
10688: my ($udom, $uname);
10689: if ($getuserdir) {
10690: $udom = $userdomain;
10691: $uname = $username;
10692: } else {
10693: (undef,$udom,$uname)=split(/\//,$uri);
10694: if(defined($userdomain)) {
10695: $udom = $userdomain;
10696: }
10697: if(defined($username)) {
10698: $uname = $username;
10699: }
10700: }
10701: my ($dirRoot,$listing,@listing_results);
10702:
10703: $dirRoot = $perlvar{'lonDocRoot'};
10704: if (defined($getpropath)) {
10705: $dirRoot = &propath($udom,$uname);
10706: $dirRoot =~ s/\/$//;
10707: } elsif (defined($getuserdir)) {
10708: my $subdir=$uname.'__';
10709: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
10710: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
10711: ."/$udom/$subdir/$uname";
10712: } elsif (defined($alternateRoot)) {
10713: $dirRoot = $alternateRoot;
10714: }
10715:
10716: if($udom) {
10717: if($uname) {
10718: my $uhome = &homeserver($uname,$udom);
10719: if ($uhome eq 'no_host') {
10720: return ([],'no_host');
10721: }
10722: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
10723: .$getuserdir.':'.&escape($dirRoot)
10724: .':'.&escape($uname).':'.&escape($udom),$uhome);
10725: if ($listing eq 'unknown_cmd') {
10726: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
10727: } else {
10728: @listing_results = map { &unescape($_); } split(/:/,$listing);
10729: }
10730: if ($listing eq 'unknown_cmd') {
10731: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
10732: @listing_results = split(/:/,$listing);
10733: } else {
10734: @listing_results = map { &unescape($_); } split(/:/,$listing);
10735: }
10736: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
10737: ($listing eq 'rejected') || ($listing eq 'refused') ||
10738: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10739: return ([],$listing);
10740: } else {
10741: return (\@listing_results);
10742: }
10743: } elsif(!$alternateRoot) {
10744: my (%allusers,%listerror);
10745: my %servers = &get_servers($udom,'library');
10746: foreach my $tryserver (keys(%servers)) {
10747: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
10748: &escape($udom),$tryserver);
10749: if ($listing eq 'unknown_cmd') {
10750: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
10751: $udom, $tryserver);
10752: } else {
10753: @listing_results = map { &unescape($_); } split(/:/,$listing);
10754: }
10755: if ($listing eq 'unknown_cmd') {
10756: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
10757: $udom, $tryserver);
10758: @listing_results = split(/:/,$listing);
10759: } else {
10760: @listing_results =
10761: map { &unescape($_); } split(/:/,$listing);
10762: }
10763: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
10764: ($listing eq 'rejected') || ($listing eq 'refused') ||
10765: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10766: $listerror{$tryserver} = $listing;
10767: } else {
10768: foreach my $line (@listing_results) {
10769: my ($entry) = split(/&/,$line,2);
10770: $allusers{$entry} = 1;
10771: }
10772: }
10773: }
10774: my @alluserslist=();
10775: foreach my $user (sort(keys(%allusers))) {
10776: push(@alluserslist,$user.'&user');
10777: }
10778:
10779: if (!%listerror) {
10780: # no errors
10781: return (\@alluserslist);
10782: } elsif (scalar(keys(%servers)) == 1) {
10783: # one library server, one error
10784: my ($key) = keys(%listerror);
10785: return (\@alluserslist, $listerror{$key});
10786: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
10787: # con_lost indicates that we might miss data from at least one
10788: # library server
10789: return (\@alluserslist, 'con_lost');
10790: } else {
10791: # multiple library servers and no con_lost -> data should be
10792: # complete.
10793: return (\@alluserslist);
10794: }
10795:
10796: } else {
10797: return ([],'missing username');
10798: }
10799: } elsif(!defined($getpropath)) {
10800: my $path = $perlvar{'lonDocRoot'}.'/res/';
10801: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
10802: return (\@all_domains);
10803: } else {
10804: return ([],'missing domain');
10805: }
10806: }
10807:
10808: # --------------------------------------------- GetFileTimestamp
10809: # This function utilizes dirlist and returns the date stamp for
10810: # when it was last modified. It will also return an error of -1
10811: # if an error occurs
10812:
10813: sub GetFileTimestamp {
10814: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
10815: $studentDomain = &LONCAPA::clean_domain($studentDomain);
10816: $studentName = &LONCAPA::clean_username($studentName);
10817: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
10818: undef,$getuserdir);
10819: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10820: return -1;
10821: }
10822: if (ref($fileref) eq 'ARRAY') {
10823: my @stats = split('&',$fileref->[0]);
10824: # @stats contains first the filename, then the stat output
10825: return $stats[10]; # so this is 10 instead of 9.
10826: } else {
10827: return -1;
10828: }
10829: }
10830:
10831: sub stat_file {
10832: my ($uri) = @_;
10833: $uri = &clutter_with_no_wrapper($uri);
10834:
10835: my ($udom,$uname,$file);
10836: if ($uri =~ m-^/(uploaded|editupload)/-) {
10837: ($udom,$uname,$file) =
10838: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
10839: $file = 'userfiles/'.$file;
10840: }
10841: if ($uri =~ m-^/res/-) {
10842: ($udom,$uname) =
10843: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
10844: $file = $uri;
10845: }
10846:
10847: if (!$udom || !$uname || !$file) {
10848: # unable to handle the uri
10849: return ();
10850: }
10851: my $getpropath;
10852: if ($file =~ /^userfiles\//) {
10853: $getpropath = 1;
10854: }
10855: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
10856: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10857: return ();
10858: } else {
10859: if (ref($listref) eq 'ARRAY') {
10860: my @stats = split('&',$listref->[0]);
10861: shift(@stats); #filename is first
10862: return @stats;
10863: }
10864: }
10865: return ();
10866: }
10867:
10868: # --------------------------------------------------------- recursedirs
10869: # Recursive function to traverse either a specific user's Authoring Space
10870: # or corresponding Published Resource Space, and populate the hash ref:
10871: # $dirhashref with URLs of all directories, and if $filehashref hash
10872: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
10873: # or .rights files in resource space, and .meta, .save, .log, and .bak
10874: # files in Authoring Space.
10875: #
10876: # Inputs:
10877: #
10878: # $is_home - true if current server is home server for user's space
10879: # $context - either: priv, or res respectively for Authoring or Resource Space.
10880: # $docroot - Document root (i.e., /home/httpd/html
10881: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
10882: # $relpath - Current path (relative to top level).
10883: # $dirhashref - reference to hash to populate with URLs of directories (Required)
10884: # $filehashref - reference to hash to populate with URLs of files (Optional)
10885: #
10886: # Returns: nothing
10887: #
10888: # Side Effects: populates $dirhashref, and $filehashref (if provided).
10889: #
10890: # Currently used by interface/londocs.pm to create linked select boxes for
10891: # directory and filename to import a Course "Author" resource into a course, and
10892: # also to create linked select boxes for Authoring Space and Directory to choose
10893: # save location for creation of a new "standard" problem from the Course Editor.
10894: #
10895:
10896: sub recursedirs {
10897: my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
10898: return unless (ref($dirhashref) eq 'HASH');
10899: my $currpath = $docroot.$toppath;
10900: if ($relpath) {
10901: $currpath .= "/$relpath";
10902: }
10903: my $savefile;
10904: if (ref($filehashref)) {
10905: $savefile = 1;
10906: }
10907: if ($is_home) {
10908: if (opendir(my $dirh,$currpath)) {
10909: foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
10910: next if ($item eq '');
10911: if (-d "$currpath/$item") {
10912: my $newpath;
10913: if ($relpath) {
10914: $newpath = "$relpath/$item";
10915: } else {
10916: $newpath = $item;
10917: }
10918: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10919: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10920: } elsif ($savefile) {
10921: if ($context eq 'priv') {
10922: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10923: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10924: }
10925: } else {
10926: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
10927: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10928: }
10929: }
10930: }
10931: }
10932: closedir($dirh);
10933: }
10934: } else {
10935: my ($dirlistref,$listerror) =
10936: &dirlist($toppath.$relpath);
10937: my @dir_lines;
10938: my $dirptr=16384;
10939: if (ref($dirlistref) eq 'ARRAY') {
10940: foreach my $dir_line (sort
10941: {
10942: my ($afile)=split('&',$a,2);
10943: my ($bfile)=split('&',$b,2);
10944: return (lc($afile) cmp lc($bfile));
10945: } (@{$dirlistref})) {
10946: my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
10947: split(/\&/,$dir_line,16);
10948: $item =~ s/\s+$//;
10949: next if (($item =~ /^\.\.?$/) || ($obs));
10950: if ($dirptr&$testdir) {
10951: my $newpath;
10952: if ($relpath) {
10953: $newpath = "$relpath/$item";
10954: } else {
10955: $relpath = '/';
10956: $newpath = $item;
10957: }
10958: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10959: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10960: } elsif ($savefile) {
10961: if ($context eq 'priv') {
10962: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10963: $filehashref->{$relpath}{$item} = 1;
10964: }
10965: } else {
10966: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
10967: $filehashref->{$relpath}{$item} = 1;
10968: }
10969: }
10970: }
10971: }
10972: }
10973: }
10974: return;
10975: }
10976:
10977: # -------------------------------------------------------- Value of a Condition
10978:
10979: # gets the value of a specific preevaluated condition
10980: # stored in the string $env{user.state.<cid>}
10981: # or looks up a condition reference in the bighash and if if hasn't
10982: # already been evaluated recurses into docondval to get the value of
10983: # the condition, then memoizing it to
10984: # $env{user.state.<cid>.<condition>}
10985: sub directcondval {
10986: my $number=shift;
10987: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
10988: &Apache::lonuserstate::evalstate();
10989: }
10990: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
10991: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
10992: } elsif ($number =~ /^_/) {
10993: my $sub_condition;
10994: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
10995: &GDBM_READER(),0640)) {
10996: $sub_condition=$bighash{'conditions'.$number};
10997: untie(%bighash);
10998: }
10999: my $value = &docondval($sub_condition);
11000: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11001: return $value;
11002: }
11003: if ($env{'user.state.'.$env{'request.course.id'}}) {
11004: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11005: } else {
11006: return 2;
11007: }
11008: }
11009:
11010: # get the collection of conditions for this resource
11011: sub condval {
11012: my $condidx=shift;
11013: my $allpathcond='';
11014: foreach my $cond (split(/\|/,$condidx)) {
11015: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11016: $allpathcond.=
11017: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11018: }
11019: }
11020: $allpathcond=~s/\|$//;
11021: return &docondval($allpathcond);
11022: }
11023:
11024: #evaluates an expression of conditions
11025: sub docondval {
11026: my ($allpathcond) = @_;
11027: my $result=0;
11028: if ($env{'request.course.id'}
11029: && defined($allpathcond)) {
11030: my $operand='|';
11031: my @stack;
11032: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11033: if ($chunk eq '(') {
11034: push @stack,($operand,$result);
11035: } elsif ($chunk eq ')') {
11036: my $before=pop @stack;
11037: if (pop @stack eq '&') {
11038: $result=$result>$before?$before:$result;
11039: } else {
11040: $result=$result>$before?$result:$before;
11041: }
11042: } elsif (($chunk eq '&') || ($chunk eq '|')) {
11043: $operand=$chunk;
11044: } else {
11045: my $new=directcondval($chunk);
11046: if ($operand eq '&') {
11047: $result=$result>$new?$new:$result;
11048: } else {
11049: $result=$result>$new?$result:$new;
11050: }
11051: }
11052: }
11053: }
11054: return $result;
11055: }
11056:
11057: # ---------------------------------------------------- Devalidate courseresdata
11058:
11059: sub devalidatecourseresdata {
11060: my ($coursenum,$coursedomain)=@_;
11061: my $hashid=$coursenum.':'.$coursedomain;
11062: &devalidate_cache_new('courseres',$hashid);
11063: }
11064:
11065:
11066: # --------------------------------------------------- Course Resourcedata Query
11067: #
11068: # Parameters:
11069: # $coursenum - Number of the course.
11070: # $coursedomain - Domain at which the course was created.
11071: # Returns:
11072: # A hash of the course parameters along (I think) with timestamps
11073: # and version info.
11074:
11075: sub get_courseresdata {
11076: my ($coursenum,$coursedomain)=@_;
11077: my $coursehom=&homeserver($coursenum,$coursedomain);
11078: my $hashid=$coursenum.':'.$coursedomain;
11079: my ($result,$cached)=&is_cached_new('courseres',$hashid);
11080: my %dumpreply;
11081: unless (defined($cached)) {
11082: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11083: $result=\%dumpreply;
11084: my ($tmp) = keys(%dumpreply);
11085: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11086: &do_cache_new('courseres',$hashid,$result,600);
11087: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11088: return $tmp;
11089: } elsif ($tmp =~ /^(error)/) {
11090: $result=undef;
11091: &do_cache_new('courseres',$hashid,$result,600);
11092: }
11093: }
11094: return $result;
11095: }
11096:
11097: sub devalidateuserresdata {
11098: my ($uname,$udom)=@_;
11099: my $hashid="$udom:$uname";
11100: &devalidate_cache_new('userres',$hashid);
11101: }
11102:
11103: sub get_userresdata {
11104: my ($uname,$udom)=@_;
11105: #most student don\'t have any data set, check if there is some data
11106: if (&EXT_cache_status($udom,$uname)) { return undef; }
11107:
11108: my $hashid="$udom:$uname";
11109: my ($result,$cached)=&is_cached_new('userres',$hashid);
11110: if (!defined($cached)) {
11111: my %resourcedata=&dump('resourcedata',$udom,$uname);
11112: $result=\%resourcedata;
11113: &do_cache_new('userres',$hashid,$result,600);
11114: }
11115: my ($tmp)=keys(%$result);
11116: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
11117: return $result;
11118: }
11119: #error 2 occurs when the .db doesn't exist
11120: if ($tmp!~/error: 2 /) {
11121: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
11122: &logthis("<font color=\"blue\">WARNING:".
11123: " Trying to get resource data for ".
11124: $uname." at ".$udom.": ".
11125: $tmp."</font>");
11126: }
11127: } elsif ($tmp=~/error: 2 /) {
11128: #&EXT_cache_set($udom,$uname);
11129: &do_cache_new('userres',$hashid,undef,600);
11130: undef($tmp); # not really an error so don't send it back
11131: }
11132: return $tmp;
11133: }
11134: #----------------------------------------------- resdata - return resource data
11135: # Purpose:
11136: # Return resource data for either users or for a course.
11137: # Parameters:
11138: # $name - Course/user name.
11139: # $domain - Name of the domain the user/course is registered on.
11140: # $type - Type of thing $name is (must be 'course' or 'user')
11141: # $mapp - decluttered URL of enclosing map
11142: # $recursed - Ref to scalar -- set to 1, if nested maps have been recursed.
11143: # $recurseup - Ref to array of map URLs, starting with map containing
11144: # $mapp up through hierarchy of nested maps to top level map.
11145: # $courseid - CourseID (first part of param identifier).
11146: # $modifier - Middle part of param identifier.
11147: # $what - Last part of param identifier.
11148: # @which - Array of names of resources desired.
11149: # Returns:
11150: # The value of the first reasource in @which that is found in the
11151: # resource hash.
11152: # Exceptional Conditions:
11153: # If the $type passed in is not valid (not the string 'course' or
11154: # 'user', an undefined reference is returned.
11155: # If none of the resources are found, an undef is returned
11156: sub resdata {
11157: my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
11158: $modifier,$what,@which)=@_;
11159: my $result;
11160: if ($type eq 'course') {
11161: $result=&get_courseresdata($name,$domain);
11162: } elsif ($type eq 'user') {
11163: $result=&get_userresdata($name,$domain);
11164: }
11165: if (!ref($result)) { return $result; }
11166: foreach my $item (@which) {
11167: if ($item->[1] eq 'course') {
11168: if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
11169: unless ($$recursed) {
11170: @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
11171: $$recursed = 1;
11172: }
11173: foreach my $item (@${recurseup}) {
11174: my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
11175: last if (defined($result->{$norecursechk}));
11176: my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
11177: if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
11178: }
11179: }
11180: }
11181: if (defined($result->{$item->[0]})) {
11182: return [$result->{$item->[0]},$item->[1]];
11183: }
11184: }
11185: return undef;
11186: }
11187:
11188: sub get_domain_lti {
11189: my ($cdom,$context) = @_;
11190: my ($name,%lti);
11191: if ($context eq 'consumer') {
11192: $name = 'ltitools';
11193: } elsif ($context eq 'provider') {
11194: $name = 'lti';
11195: } else {
11196: return %lti;
11197: }
11198: my ($result,$cached)=&is_cached_new($name,$cdom);
11199: if (defined($cached)) {
11200: if (ref($result) eq 'HASH') {
11201: %lti = %{$result};
11202: }
11203: } else {
11204: my %domconfig = &get_dom('configuration',[$name],$cdom);
11205: if (ref($domconfig{$name}) eq 'HASH') {
11206: %lti = %{$domconfig{$name}};
11207: my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
11208: if (ref($encdomconfig{$name}) eq 'HASH') {
11209: foreach my $id (keys(%lti)) {
11210: if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
11211: foreach my $item ('key','secret') {
11212: $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
11213: }
11214: }
11215: }
11216: }
11217: }
11218: my $cachetime = 24*60*60;
11219: &do_cache_new($name,$cdom,\%lti,$cachetime);
11220: }
11221: return %lti;
11222: }
11223:
11224: sub get_numsuppfiles {
11225: my ($cnum,$cdom,$ignorecache)=@_;
11226: my $hashid=$cnum.':'.$cdom;
11227: my ($suppcount,$cached);
11228: unless ($ignorecache) {
11229: ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
11230: }
11231: unless (defined($cached)) {
11232: my $chome=&homeserver($cnum,$cdom);
11233: unless ($chome eq 'no_host') {
11234: ($suppcount,my $supptools,my $errors) = (0,0,0);
11235: my $suppmap = 'supplemental.sequence';
11236: ($suppcount,$supptools,$errors) =
11237: &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
11238: $supptools,$errors);
11239: }
11240: &do_cache_new('suppcount',$hashid,$suppcount,600);
11241: }
11242: return $suppcount;
11243: }
11244:
11245: #
11246: # EXT resource caching routines
11247: #
11248:
11249: {
11250: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
11251: #
11252: # The course for which we cache
11253: my $cachedmapkey='';
11254: # The cached recursive maps for this course
11255: my %cachedmaps=();
11256: # When this was last done
11257: my $cachedmaptime='';
11258:
11259: sub clear_EXT_cache_status {
11260: &delenv('cache.EXT.');
11261: }
11262:
11263: sub EXT_cache_status {
11264: my ($target_domain,$target_user) = @_;
11265: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11266: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
11267: # We know already the user has no data
11268: return 1;
11269: } else {
11270: return 0;
11271: }
11272: }
11273:
11274: sub EXT_cache_set {
11275: my ($target_domain,$target_user) = @_;
11276: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11277: #&appenv({$cachename => time});
11278: }
11279:
11280: # --------------------------------------------------------- Value of a Variable
11281: sub EXT {
11282:
11283: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
11284: unless ($varname) { return ''; }
11285: #get real user name/domain, courseid and symb
11286: my $courseid;
11287: my $publicuser;
11288: if ($symbparm) {
11289: $symbparm=&get_symb_from_alias($symbparm);
11290: }
11291: if (!($uname && $udom)) {
11292: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
11293: if (!$symbparm) { $symbparm=$cursymb; }
11294: } else {
11295: $courseid=$env{'request.course.id'};
11296: }
11297: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
11298: my $rest;
11299: if (defined($therest[0])) {
11300: $rest=join('.',@therest);
11301: } else {
11302: $rest='';
11303: }
11304:
11305: my $qualifierrest=$qualifier;
11306: if ($rest) { $qualifierrest.='.'.$rest; }
11307: my $spacequalifierrest=$space;
11308: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
11309: if ($realm eq 'user') {
11310: # --------------------------------------------------------------- user.resource
11311: if ($space eq 'resource') {
11312: if ( (defined($Apache::lonhomework::parsing_a_problem)
11313: || defined($Apache::lonhomework::parsing_a_task))
11314: &&
11315: ($symbparm eq &symbread()) ) {
11316: # if we are in the middle of processing the resource the
11317: # get the value we are planning on committing
11318: if (defined($Apache::lonhomework::results{$qualifierrest})) {
11319: return $Apache::lonhomework::results{$qualifierrest};
11320: } else {
11321: return $Apache::lonhomework::history{$qualifierrest};
11322: }
11323: } else {
11324: my %restored;
11325: if ($publicuser || $env{'request.state'} eq 'construct') {
11326: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
11327: } else {
11328: %restored=&restore($symbparm,$courseid,$udom,$uname);
11329: }
11330: return $restored{$qualifierrest};
11331: }
11332: # ----------------------------------------------------------------- user.access
11333: } elsif ($space eq 'access') {
11334: # FIXME - not supporting calls for a specific user
11335: return &allowed($qualifier,$rest);
11336: # ------------------------------------------ user.preferences, user.environment
11337: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
11338: if (($uname eq $env{'user.name'}) &&
11339: ($udom eq $env{'user.domain'})) {
11340: return $env{join('.',('environment',$qualifierrest))};
11341: } else {
11342: my %returnhash;
11343: if (!$publicuser) {
11344: %returnhash=&userenvironment($udom,$uname,
11345: $qualifierrest);
11346: }
11347: return $returnhash{$qualifierrest};
11348: }
11349: # ----------------------------------------------------------------- user.course
11350: } elsif ($space eq 'course') {
11351: # FIXME - not supporting calls for a specific user
11352: return $env{join('.',('request.course',$qualifier))};
11353: # ------------------------------------------------------------------- user.role
11354: } elsif ($space eq 'role') {
11355: # FIXME - not supporting calls for a specific user
11356: my ($role,$where)=split(/\./,$env{'request.role'});
11357: if ($qualifier eq 'value') {
11358: return $role;
11359: } elsif ($qualifier eq 'extent') {
11360: return $where;
11361: }
11362: # ----------------------------------------------------------------- user.domain
11363: } elsif ($space eq 'domain') {
11364: return $udom;
11365: # ------------------------------------------------------------------- user.name
11366: } elsif ($space eq 'name') {
11367: return $uname;
11368: # ---------------------------------------------------- Any other user namespace
11369: } else {
11370: my %reply;
11371: if (!$publicuser) {
11372: %reply=&get($space,[$qualifierrest],$udom,$uname);
11373: }
11374: return $reply{$qualifierrest};
11375: }
11376: } elsif ($realm eq 'query') {
11377: # ---------------------------------------------- pull stuff out of query string
11378: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
11379: [$spacequalifierrest]);
11380: return $env{'form.'.$spacequalifierrest};
11381: } elsif ($realm eq 'request') {
11382: # ------------------------------------------------------------- request.browser
11383: if ($space eq 'browser') {
11384: return $env{'browser.'.$qualifier};
11385: # ------------------------------------------------------------ request.filename
11386: } else {
11387: return $env{'request.'.$spacequalifierrest};
11388: }
11389: } elsif ($realm eq 'course') {
11390: # ---------------------------------------------------------- course.description
11391: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
11392: } elsif ($realm eq 'resource') {
11393:
11394: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
11395: if (!$symbparm) { $symbparm=&symbread(); }
11396: }
11397:
11398: if ($qualifier eq '') {
11399: if ($space eq 'title') {
11400: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
11401: return &gettitle($symbparm);
11402: }
11403:
11404: if ($space eq 'map') {
11405: my ($map) = &decode_symb($symbparm);
11406: return &symbread($map);
11407: }
11408: if ($space eq 'maptitle') {
11409: my ($map) = &decode_symb($symbparm);
11410: return &gettitle($map);
11411: }
11412: if ($space eq 'filename') {
11413: if ($symbparm) {
11414: return &clutter((&decode_symb($symbparm))[2]);
11415: }
11416: return &hreflocation('',$env{'request.filename'});
11417: }
11418:
11419: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
11420: if ($space eq 'visibleparts') {
11421: my $navmap = Apache::lonnavmaps::navmap->new();
11422: my $item;
11423: if (ref($navmap)) {
11424: my $res = $navmap->getBySymb($symbparm);
11425: my $parts = $res->parts();
11426: if (ref($parts) eq 'ARRAY') {
11427: $item = join(',',@{$parts});
11428: }
11429: undef($navmap);
11430: }
11431: return $item;
11432: }
11433: }
11434: }
11435:
11436: my ($section, $group, @groups, @recurseup, $recursed);
11437: my ($courselevelm,$courseleveli,$courselevel,$mapp);
11438: if (($courseid eq '') && ($cid)) {
11439: $courseid = $cid;
11440: }
11441: if (($symbparm && $courseid) &&
11442: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
11443:
11444: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
11445:
11446: # ----------------------------------------------------- Cascading lookup scheme
11447: my $symbp=$symbparm;
11448: $mapp=&deversion((&decode_symb($symbp))[0]);
11449: my $symbparm=$symbp.'.'.$spacequalifierrest;
11450: my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
11451: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
11452: if (($env{'user.name'} eq $uname) &&
11453: ($env{'user.domain'} eq $udom)) {
11454: $section=$env{'request.course.sec'};
11455: @groups = split(/:/,$env{'request.course.groups'});
11456: @groups=&sort_course_groups($courseid,@groups);
11457: } else {
11458: if (! defined($usection)) {
11459: $section=&getsection($udom,$uname,$courseid);
11460: } else {
11461: $section = $usection;
11462: }
11463: @groups = &get_users_groups($udom,$uname,$courseid);
11464: }
11465:
11466: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
11467: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
11468: my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
11469: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
11470:
11471: $courselevel=$courseid.'.'.$spacequalifierrest;
11472: my $courselevelr=$courseid.'.'.$symbparm;
11473: $courseleveli=$courseid.'.'.$recurseparm;
11474: $courselevelm=$courseid.'.'.$mapparm;
11475:
11476: # ----------------------------------------------------------- first, check user
11477:
11478: my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
11479: \@recurseup,$courseid,'.',$spacequalifierrest,
11480: ([$courselevelr,'resource'],
11481: [$courselevelm,'map' ],
11482: [$courseleveli,'map' ],
11483: [$courselevel, 'course' ]));
11484: if (defined($userreply)) { return &get_reply($userreply); }
11485:
11486: # ------------------------------------------------ second, check some of course
11487: my $coursereply;
11488: if (@groups > 0) {
11489: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
11490: $recurseparm,$mapparm,$spacequalifierrest,
11491: $mapp,\$recursed,\@recurseup);
11492: if (defined($coursereply)) { return &get_reply($coursereply); }
11493: }
11494:
11495: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
11496: $env{'course.'.$courseid.'.domain'},
11497: 'course',$mapp,\$recursed,\@recurseup,
11498: $courseid,'.['.$section.'].',$spacequalifierrest,
11499: ([$seclevelr, 'resource'],
11500: [$seclevelm, 'map' ],
11501: [$secleveli, 'map' ],
11502: [$seclevel, 'course' ],
11503: [$courselevelr,'resource']));
11504: if (defined($coursereply)) { return &get_reply($coursereply); }
11505:
11506: # ------------------------------------------------------ third, check map parms
11507: my %parmhash=();
11508: my $thisparm='';
11509: if (tie(%parmhash,'GDBM_File',
11510: $env{'request.course.fn'}.'_parms.db',
11511: &GDBM_READER(),0640)) {
11512: $thisparm=$parmhash{$symbparm};
11513: untie(%parmhash);
11514: }
11515: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
11516: }
11517: # ------------------------------------------ fourth, look in resource metadata
11518:
11519: my $what = $spacequalifierrest;
11520: $what=~s/\./\_/;
11521: my $filename;
11522: if (!$symbparm) { $symbparm=&symbread(); }
11523: if ($symbparm) {
11524: $filename=(&decode_symb($symbparm))[2];
11525: } else {
11526: $filename=$env{'request.filename'};
11527: }
11528: my $toolsymb;
11529: if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
11530: $toolsymb = $symbparm;
11531: }
11532: my $metadata=&metadata($filename,$what,$toolsymb);
11533: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
11534: $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
11535: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
11536:
11537: # ----------------------------------------------- fifth, look in rest of course
11538: if ($symbparm && defined($courseid) &&
11539: $courseid eq $env{'request.course.id'}) {
11540: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
11541: $env{'course.'.$courseid.'.domain'},
11542: 'course',$mapp,\$recursed,\@recurseup,
11543: $courseid,'.',$spacequalifierrest,
11544: ([$courselevelm,'map' ],
11545: [$courseleveli,'map' ],
11546: [$courselevel, 'course']));
11547: if (defined($coursereply)) { return &get_reply($coursereply); }
11548: }
11549: # ------------------------------------------------------------------ Cascade up
11550: unless ($space eq '0') {
11551: my @parts=split(/_/,$space);
11552: my $id=pop(@parts);
11553: my $part=join('_',@parts);
11554: if ($part eq '') { $part='0'; }
11555: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
11556: $symbparm,$udom,$uname,$section,1);
11557: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
11558: }
11559: if ($recurse) { return undef; }
11560: my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
11561: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
11562: # ---------------------------------------------------- Any other user namespace
11563: } elsif ($realm eq 'environment') {
11564: # ----------------------------------------------------------------- environment
11565: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
11566: return $env{'environment.'.$spacequalifierrest};
11567: } else {
11568: if ($uname eq 'anonymous' && $udom eq '') {
11569: return '';
11570: }
11571: my %returnhash=&userenvironment($udom,$uname,
11572: $spacequalifierrest);
11573: return $returnhash{$spacequalifierrest};
11574: }
11575: } elsif ($realm eq 'system') {
11576: # ----------------------------------------------------------------- system.time
11577: if ($space eq 'time') {
11578: return time;
11579: }
11580: } elsif ($realm eq 'server') {
11581: # ----------------------------------------------------------------- system.time
11582: if ($space eq 'name') {
11583: return $ENV{'SERVER_NAME'};
11584: }
11585: }
11586: return '';
11587: }
11588:
11589: sub get_reply {
11590: my ($reply_value) = @_;
11591: if (ref($reply_value) eq 'ARRAY') {
11592: if (wantarray) {
11593: return @$reply_value;
11594: }
11595: return $reply_value->[0];
11596: } else {
11597: return $reply_value;
11598: }
11599: }
11600:
11601: sub check_group_parms {
11602: my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
11603: $recursed,$recurseupref) = @_;
11604: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
11605: [$what,'course']);
11606: my $coursereply;
11607: foreach my $group (@{$groups}) {
11608: my @groupitems = ();
11609: foreach my $level (@levels) {
11610: my $item = $courseid.'.['.$group.'].'.$level->[0];
11611: push(@groupitems,[$item,$level->[1]]);
11612: }
11613: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
11614: $env{'course.'.$courseid.'.domain'},
11615: 'course',$mapp,$recursed,$recurseupref,
11616: $courseid,'.['.$group.'].',$what,
11617: @groupitems);
11618: last if (defined($coursereply));
11619: }
11620: return $coursereply;
11621: }
11622:
11623: sub get_map_hierarchy {
11624: my ($mapname,$courseid) = @_;
11625: my @recurseup = ();
11626: if ($mapname) {
11627: if (($cachedmapkey eq $courseid) &&
11628: (abs($cachedmaptime-time)<5)) {
11629: if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
11630: return @{$cachedmaps{$mapname}};
11631: }
11632: }
11633: my $navmap = Apache::lonnavmaps::navmap->new();
11634: if (ref($navmap)) {
11635: @recurseup = $navmap->recurseup_maps($mapname);
11636: undef($navmap);
11637: $cachedmaps{$mapname} = \@recurseup;
11638: $cachedmaptime=time;
11639: $cachedmapkey=$courseid;
11640: }
11641: }
11642: return @recurseup;
11643: }
11644:
11645: }
11646:
11647: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
11648: my ($courseid,@groups) = @_;
11649: @groups = sort(@groups);
11650: return @groups;
11651: }
11652:
11653: sub packages_tab_default {
11654: my ($uri,$varname,$toolsymb)=@_;
11655: my (undef,$part,$name)=split(/\./,$varname);
11656:
11657: my (@extension,@specifics,$do_default);
11658: foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
11659: my ($pack_type,$pack_part)=split(/_/,$package,2);
11660: if ($pack_type eq 'default') {
11661: $do_default=1;
11662: } elsif ($pack_type eq 'extension') {
11663: push(@extension,[$package,$pack_type,$pack_part]);
11664: } elsif ($pack_part eq $part || $pack_type eq 'part') {
11665: # only look at packages defaults for packages that this id is
11666: push(@specifics,[$package,$pack_type,$pack_part]);
11667: }
11668: }
11669: # first look for a package that matches the requested part id
11670: foreach my $package (@specifics) {
11671: my (undef,$pack_type,$pack_part)=@{$package};
11672: next if ($pack_part ne $part);
11673: if (defined($packagetab{"$pack_type&$name&default"})) {
11674: return $packagetab{"$pack_type&$name&default"};
11675: }
11676: }
11677: # look for any possible matching non extension_ package
11678: foreach my $package (@specifics) {
11679: my (undef,$pack_type,$pack_part)=@{$package};
11680: if (defined($packagetab{"$pack_type&$name&default"})) {
11681: return $packagetab{"$pack_type&$name&default"};
11682: }
11683: if ($pack_type eq 'part') { $pack_part='0'; }
11684: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
11685: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
11686: }
11687: }
11688: # look for any posible extension_ match
11689: foreach my $package (@extension) {
11690: my ($package,$pack_type)=@{$package};
11691: if (defined($packagetab{"$pack_type&$name&default"})) {
11692: return $packagetab{"$pack_type&$name&default"};
11693: }
11694: if (defined($packagetab{$package."&$name&default"})) {
11695: return $packagetab{$package."&$name&default"};
11696: }
11697: }
11698: # look for a global default setting
11699: if ($do_default && defined($packagetab{"default&$name&default"})) {
11700: return $packagetab{"default&$name&default"};
11701: }
11702: return undef;
11703: }
11704:
11705: sub add_prefix_and_part {
11706: my ($prefix,$part)=@_;
11707: my $keyroot;
11708: if (defined($prefix) && $prefix !~ /^__/) {
11709: # prefix that has a part already
11710: $keyroot=$prefix;
11711: } elsif (defined($prefix)) {
11712: # prefix that is missing a part
11713: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
11714: } else {
11715: # no prefix at all
11716: if (defined($part)) { $keyroot='_'.$part; }
11717: }
11718: return $keyroot;
11719: }
11720:
11721: # ---------------------------------------------------------------- Get metadata
11722:
11723: my %metaentry;
11724: my %importedpartids;
11725: sub metadata {
11726: my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
11727: $uri=&declutter($uri);
11728: # if it is a non metadata possible uri return quickly
11729: if (($uri eq '') ||
11730: (($uri =~ m|^/*adm/|) &&
11731: ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
11732: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
11733: return undef;
11734: }
11735: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
11736: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
11737: return undef;
11738: }
11739: my $filename=$uri;
11740: $uri=~s/\.meta$//;
11741: #
11742: # Is the metadata already cached?
11743: # Look at timestamp of caching
11744: # Everything is cached by the main uri, libraries are never directly cached
11745: #
11746: if (!defined($liburi)) {
11747: my ($result,$cached)=&is_cached_new('meta',$uri);
11748: if (defined($cached)) { return $result->{':'.$what}; }
11749: }
11750:
11751: #
11752: # If the uri is for an external tool the file from
11753: # which metadata should be retrieved depends on whether
11754: # the tool had been configured to be gradable (set in the Course
11755: # Editor or Resource Editor).
11756: #
11757: # If a valid symb has been included as the third arg in the call
11758: # to &metadata() that can be used to retrieve the value of
11759: # parameter_0_gradable set for the resource, and included in the
11760: # uploaded map containing the tool. The value is retrieved via
11761: # &EXT(), if a valid symb is available. Otherwise the value of
11762: # gradable in the exttool_$marker.db file for the tool instance
11763: # is retrieved via &get().
11764: #
11765: # When lonuserstate::traceroute() calls lonnet::EXT() for
11766: # hiddenresource and encrypturl (during course initialization)
11767: # the map-level parameter for resource.0.gradable included in the
11768: # uploaded map containing the tool will not yet have been stored
11769: # in the user_course_parms.db file for the user's session, so in
11770: # this case fall back to retrieving gradable status from the
11771: # exttool_$marker.db file.
11772: #
11773: # In order to avoid an infinite loop, &metadata() will return
11774: # before a call to &EXT(), if the uri is for an external tool
11775: # and the $what for which metadata is being requested is
11776: # parameter_0_gradable or 0_gradable.
11777: #
11778:
11779: if ($uri =~ /ext\.tool$/) {
11780: if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
11781: return;
11782: } else {
11783: my ($checked,$use_passback);
11784: if ($toolsymb ne '') {
11785: (undef,undef,my $tooluri) = &decode_symb($toolsymb);
11786: if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
11787: $checked = 1;
11788: if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
11789: $use_passback = 1;
11790: }
11791: }
11792: }
11793: unless ($checked) {
11794: my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
11795: $marker=~s/\D//g;
11796: if ($marker) {
11797: my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
11798: $use_passback = $toolsettings{'gradable'};
11799: }
11800: }
11801: if ($use_passback) {
11802: $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
11803: } else {
11804: $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
11805: }
11806: }
11807: }
11808:
11809: {
11810: # Imported parts would go here
11811: my %importedids=();
11812: my @origfileimportpartids=();
11813: my $importedparts=0;
11814: #
11815: # Is this a recursive call for a library?
11816: #
11817: # if (! exists($metacache{$uri})) {
11818: # $metacache{$uri}={};
11819: # }
11820: my $cachetime = 60*60;
11821: if ($liburi) {
11822: $liburi=&declutter($liburi);
11823: $filename=$liburi;
11824: } else {
11825: &devalidate_cache_new('meta',$uri);
11826: undef(%metaentry);
11827: }
11828: my %metathesekeys=();
11829: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
11830: my $metastring;
11831: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
11832: my $which = &hreflocation('','/'.($liburi || $uri));
11833: $metastring =
11834: &Apache::lonnet::ssi_body($which,
11835: ('grade_target' => 'meta'));
11836: $cachetime = 1; # only want this cached in the child not long term
11837: } elsif (($uri !~ m -^(editupload)/-) &&
11838: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
11839: my $file=&filelocation('',&clutter($filename));
11840: #push(@{$metaentry{$uri.'.file'}},$file);
11841: $metastring=&getfile($file);
11842: }
11843: my $parser=HTML::LCParser->new(\$metastring);
11844: my $token;
11845: undef %metathesekeys;
11846: while ($token=$parser->get_token) {
11847: if ($token->[0] eq 'S') {
11848: if (defined($token->[2]->{'package'})) {
11849: #
11850: # This is a package - get package info
11851: #
11852: my $package=$token->[2]->{'package'};
11853: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11854: if (defined($token->[2]->{'id'})) {
11855: $keyroot.='_'.$token->[2]->{'id'};
11856: }
11857: if ($metaentry{':packages'}) {
11858: $metaentry{':packages'}.=','.$package.$keyroot;
11859: } else {
11860: $metaentry{':packages'}=$package.$keyroot;
11861: }
11862: foreach my $pack_entry (keys(%packagetab)) {
11863: my $part=$keyroot;
11864: $part=~s/^\_//;
11865: if ($pack_entry=~/^\Q$package\E\&/ ||
11866: $pack_entry=~/^\Q$package\E_0\&/) {
11867: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
11868: # ignore package.tab specified default values
11869: # here &package_tab_default() will fetch those
11870: if ($subp eq 'default') { next; }
11871: my $value=$packagetab{$pack_entry};
11872: my $unikey;
11873: if ($pack =~ /_0$/) {
11874: $unikey='parameter_0_'.$name;
11875: $part=0;
11876: } else {
11877: $unikey='parameter'.$keyroot.'_'.$name;
11878: }
11879: if ($subp eq 'display') {
11880: $value.=' [Part: '.$part.']';
11881: }
11882: $metaentry{':'.$unikey.'.part'}=$part;
11883: $metathesekeys{$unikey}=1;
11884: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11885: $metaentry{':'.$unikey.'.'.$subp}=$value;
11886: }
11887: if (defined($metaentry{':'.$unikey.'.default'})) {
11888: $metaentry{':'.$unikey}=
11889: $metaentry{':'.$unikey.'.default'};
11890: }
11891: }
11892: }
11893: } else {
11894: #
11895: # This is not a package - some other kind of start tag
11896: #
11897: my $entry=$token->[1];
11898: my $unikey='';
11899:
11900: if ($entry eq 'import') {
11901: #
11902: # Importing a library here
11903: #
11904: my $location=$parser->get_text('/import');
11905: my $dir=$filename;
11906: $dir=~s|[^/]*$||;
11907: $location=&filelocation($dir,$location);
11908:
11909: my $importmode=$token->[2]->{'importmode'};
11910: if ($importmode eq 'problem') {
11911: # Import as problem/response
11912: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11913: } elsif ($importmode eq 'part') {
11914: # Import as part(s)
11915: $importedparts=1;
11916: # We need to get the original file and the imported file to get the part order correct
11917: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
11918: # Load and inspect original file
11919: if ($#origfileimportpartids<0) {
11920: undef(%importedpartids);
11921: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
11922: my $origfile=&getfile($origfilelocation);
11923: @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11924: }
11925:
11926: # Load and inspect imported file
11927: my $impfile=&getfile($location);
11928: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11929: if ($#impfilepartids>=0) {
11930: # This problem had parts
11931: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
11932: } else {
11933: # Importing by turning a single problem into a problem part
11934: # It gets the import-tags ID as part-ID
11935: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
11936: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
11937: }
11938: } else {
11939: # Normal import
11940: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11941: if (defined($token->[2]->{'id'})) {
11942: $unikey.='_'.$token->[2]->{'id'};
11943: }
11944: }
11945:
11946: if ($depthcount<20) {
11947: my $metadata =
11948: &metadata($uri,'keys',$toolsymb,$location,$unikey,
11949: $depthcount+1);
11950: foreach my $meta (split(',',$metadata)) {
11951: $metaentry{':'.$meta}=$metaentry{':'.$meta};
11952: $metathesekeys{$meta}=1;
11953: }
11954:
11955: }
11956: } else {
11957: #
11958: # Not importing, some other kind of non-package, non-library start tag
11959: #
11960: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
11961: if (defined($token->[2]->{'id'})) {
11962: $unikey.='_'.$token->[2]->{'id'};
11963: }
11964: if (defined($token->[2]->{'name'})) {
11965: $unikey.='_'.$token->[2]->{'name'};
11966: }
11967: $metathesekeys{$unikey}=1;
11968: foreach my $param (@{$token->[3]}) {
11969: $metaentry{':'.$unikey.'.'.$param} =
11970: $token->[2]->{$param};
11971: }
11972: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
11973: my $default=$metaentry{':'.$unikey.'.default'};
11974: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
11975: # only ws inside the tag, and not in default, so use default
11976: # as value
11977: $metaentry{':'.$unikey}=$default;
11978: } elsif ( $internaltext =~ /\S/ ) {
11979: # something interesting inside the tag
11980: $metaentry{':'.$unikey}=$internaltext;
11981: } else {
11982: # no interesting values, don't set a default
11983: }
11984: # end of not-a-package not-a-library import
11985: }
11986: # end of not-a-package start tag
11987: }
11988: # the next is the end of "start tag"
11989: }
11990: }
11991: my ($extension) = ($uri =~ /\.(\w+)$/);
11992: $extension = lc($extension);
11993: if ($extension eq 'htm') { $extension='html'; }
11994:
11995: foreach my $key (keys(%packagetab)) {
11996: #no specific packages #how's our extension
11997: if ($key!~/^extension_\Q$extension\E&/) { next; }
11998: &metadata_create_package_def($uri,$key,'extension_'.$extension,
11999: \%metathesekeys);
12000: }
12001:
12002: if (!exists($metaentry{':packages'})
12003: || $packagetab{"import_defaults&extension_$extension"}) {
12004: foreach my $key (keys(%packagetab)) {
12005: #no specific packages well let's get default then
12006: if ($key!~/^default&/) { next; }
12007: &metadata_create_package_def($uri,$key,'default',
12008: \%metathesekeys);
12009: }
12010: }
12011: # are there custom rights to evaluate
12012: if ($metaentry{':copyright'} eq 'custom') {
12013:
12014: #
12015: # Importing a rights file here
12016: #
12017: unless ($depthcount) {
12018: my $location=$metaentry{':customdistributionfile'};
12019: my $dir=$filename;
12020: $dir=~s|[^/]*$||;
12021: $location=&filelocation($dir,$location);
12022: my $rights_metadata =
12023: &metadata($uri,'keys',$toolsymb,$location,'_rights',
12024: $depthcount+1);
12025: foreach my $rights (split(',',$rights_metadata)) {
12026: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12027: $metathesekeys{$rights}=1;
12028: }
12029: }
12030: }
12031: # uniqifiy package listing
12032: my %seen;
12033: my @uniq_packages =
12034: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
12035: $metaentry{':packages'} = join(',',@uniq_packages);
12036:
12037: if ($importedparts) {
12038: # We had imported parts and need to rebuild partorder
12039: $metaentry{':partorder'}='';
12040: $metathesekeys{'partorder'}=1;
12041: for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
12042: if ($origfileimportpartids[$index] eq 'part') {
12043: # original part, part of the problem
12044: $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
12045: } else {
12046: # we have imported parts at this position
12047: $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
12048: }
12049: }
12050: $metaentry{':partorder'}=~s/^\,//;
12051: }
12052:
12053: $metaentry{':keys'} = join(',',keys(%metathesekeys));
12054: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
12055: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
12056: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
12057: # this is the end of "was not already recently cached
12058: }
12059: return $metaentry{':'.$what};
12060: }
12061:
12062: sub metadata_create_package_def {
12063: my ($uri,$key,$package,$metathesekeys)=@_;
12064: my ($pack,$name,$subp)=split(/\&/,$key);
12065: if ($subp eq 'default') { next; }
12066:
12067: if (defined($metaentry{':packages'})) {
12068: $metaentry{':packages'}.=','.$package;
12069: } else {
12070: $metaentry{':packages'}=$package;
12071: }
12072: my $value=$packagetab{$key};
12073: my $unikey;
12074: $unikey='parameter_0_'.$name;
12075: $metaentry{':'.$unikey.'.part'}=0;
12076: $$metathesekeys{$unikey}=1;
12077: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12078: $metaentry{':'.$unikey.'.'.$subp}=$value;
12079: }
12080: if (defined($metaentry{':'.$unikey.'.default'})) {
12081: $metaentry{':'.$unikey}=
12082: $metaentry{':'.$unikey.'.default'};
12083: }
12084: }
12085:
12086: sub metadata_generate_part0 {
12087: my ($metadata,$metacache,$uri) = @_;
12088: my %allnames;
12089: foreach my $metakey (keys(%$metadata)) {
12090: if ($metakey=~/^parameter\_(.*)/) {
12091: my $part=$$metacache{':'.$metakey.'.part'};
12092: my $name=$$metacache{':'.$metakey.'.name'};
12093: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
12094: $allnames{$name}=$part;
12095: }
12096: }
12097: }
12098: foreach my $name (keys(%allnames)) {
12099: $$metadata{"parameter_0_$name"}=1;
12100: my $key=":parameter_0_$name";
12101: $$metacache{"$key.part"}='0';
12102: $$metacache{"$key.name"}=$name;
12103: $$metacache{"$key.type"}=$$metacache{':parameter_'.
12104: $allnames{$name}.'_'.$name.
12105: '.type'};
12106: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
12107: '.display'};
12108: my $expr='[Part: '.$allnames{$name}.']';
12109: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
12110: $$metacache{"$key.display"}=$olddis;
12111: }
12112: }
12113:
12114: # ------------------------------------------------------ Devalidate title cache
12115:
12116: sub devalidate_title_cache {
12117: my ($url)=@_;
12118: if (!$env{'request.course.id'}) { return; }
12119: my $symb=&symbread($url);
12120: if (!$symb) { return; }
12121: my $key=$env{'request.course.id'}."\0".$symb;
12122: &devalidate_cache_new('title',$key);
12123: }
12124:
12125: # ------------------------------------------------- Get the title of a course
12126:
12127: sub current_course_title {
12128: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
12129: }
12130: # ------------------------------------------------- Get the title of a resource
12131:
12132: sub gettitle {
12133: my $urlsymb=shift;
12134: my $symb=&symbread($urlsymb);
12135: if ($symb) {
12136: my $key=$env{'request.course.id'}."\0".$symb;
12137: my ($result,$cached)=&is_cached_new('title',$key);
12138: if (defined($cached)) {
12139: return $result;
12140: }
12141: my ($map,$resid,$url)=&decode_symb($symb);
12142: my $title='';
12143: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
12144: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
12145: } else {
12146: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12147: &GDBM_READER(),0640)) {
12148: my $mapid=$bighash{'map_pc_'.&clutter($map)};
12149: $title=$bighash{'title_'.$mapid.'.'.$resid};
12150: untie(%bighash);
12151: }
12152: }
12153: $title=~s/\&colon\;/\:/gs;
12154: if ($title) {
12155: # Remember both $symb and $title for dynamic metadata
12156: $accesshash{$symb.'___crstitle'}=$title;
12157: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
12158: # Cache this title and then return it
12159: return &do_cache_new('title',$key,$title,600);
12160: }
12161: $urlsymb=$url;
12162: }
12163: my $title=&metadata($urlsymb,'title');
12164: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
12165: return $title;
12166: }
12167:
12168: sub get_slot {
12169: my ($which,$cnum,$cdom)=@_;
12170: if (!$cnum || !$cdom) {
12171: (undef,my $courseid)=&whichuser();
12172: $cdom=$env{'course.'.$courseid.'.domain'};
12173: $cnum=$env{'course.'.$courseid.'.num'};
12174: }
12175: my $key=join("\0",'slots',$cdom,$cnum,$which);
12176: my %slotinfo;
12177: if (exists($remembered{$key})) {
12178: $slotinfo{$which} = $remembered{$key};
12179: } else {
12180: %slotinfo=&get('slots',[$which],$cdom,$cnum);
12181: &Apache::lonhomework::showhash(%slotinfo);
12182: my ($tmp)=keys(%slotinfo);
12183: if ($tmp=~/^error:/) { return (); }
12184: $remembered{$key} = $slotinfo{$which};
12185: }
12186: if (ref($slotinfo{$which}) eq 'HASH') {
12187: return %{$slotinfo{$which}};
12188: }
12189: return $slotinfo{$which};
12190: }
12191:
12192: sub get_reservable_slots {
12193: my ($cnum,$cdom,$uname,$udom) = @_;
12194: my $now = time;
12195: my $reservable_info;
12196: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
12197: if (exists($remembered{$key})) {
12198: $reservable_info = $remembered{$key};
12199: } else {
12200: my %resv;
12201: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
12202: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
12203: $reservable_info = \%resv;
12204: $remembered{$key} = $reservable_info;
12205: }
12206: return $reservable_info;
12207: }
12208:
12209: sub get_course_slots {
12210: my ($cnum,$cdom) = @_;
12211: my $hashid=$cnum.':'.$cdom;
12212: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
12213: if (defined($cached)) {
12214: if (ref($result) eq 'HASH') {
12215: return %{$result};
12216: }
12217: } else {
12218: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
12219: my ($tmp) = keys(%slots);
12220: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12221: &do_cache_new('allslots',$hashid,\%slots,600);
12222: return %slots;
12223: }
12224: }
12225: return;
12226: }
12227:
12228: sub devalidate_slots_cache {
12229: my ($cnum,$cdom)=@_;
12230: my $hashid=$cnum.':'.$cdom;
12231: &devalidate_cache_new('allslots',$hashid);
12232: }
12233:
12234: sub get_coursechange {
12235: my ($cdom,$cnum) = @_;
12236: if ($cdom eq '' || $cnum eq '') {
12237: return unless ($env{'request.course.id'});
12238: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
12239: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
12240: }
12241: my $hashid=$cdom.'_'.$cnum;
12242: my ($change,$cached)=&is_cached_new('crschange',$hashid);
12243: if ((defined($cached)) && ($change ne '')) {
12244: return $change;
12245: } else {
12246: my %crshash;
12247: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
12248: if ($crshash{'internal.contentchange'} eq '') {
12249: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
12250: if ($change eq '') {
12251: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
12252: $change = $crshash{'internal.created'};
12253: }
12254: } else {
12255: $change = $crshash{'internal.contentchange'};
12256: }
12257: my $cachetime = 600;
12258: &do_cache_new('crschange',$hashid,$change,$cachetime);
12259: }
12260: return $change;
12261: }
12262:
12263: sub devalidate_coursechange_cache {
12264: my ($cnum,$cdom)=@_;
12265: my $hashid=$cnum.':'.$cdom;
12266: &devalidate_cache_new('crschange',$hashid);
12267: }
12268:
12269: # ------------------------------------------------- Update symbolic store links
12270:
12271: sub symblist {
12272: my ($mapname,%newhash)=@_;
12273: $mapname=&deversion(&declutter($mapname));
12274: my %hash;
12275: if (($env{'request.course.fn'}) && (%newhash)) {
12276: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
12277: &GDBM_WRCREAT(),0640)) {
12278: foreach my $url (keys(%newhash)) {
12279: next if ($url eq 'last_known'
12280: && $env{'form.no_update_last_known'});
12281: $hash{declutter($url)}=&encode_symb($mapname,
12282: $newhash{$url}->[1],
12283: $newhash{$url}->[0]);
12284: }
12285: if (untie(%hash)) {
12286: return 'ok';
12287: }
12288: }
12289: }
12290: return 'error';
12291: }
12292:
12293: # --------------------------------------------------------------- Verify a symb
12294:
12295: sub symbverify {
12296: my ($symb,$thisurl,$encstate)=@_;
12297: my $thisfn=$thisurl;
12298: $thisfn=&declutter($thisfn);
12299: # direct jump to resource in page or to a sequence - will construct own symbs
12300: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
12301: # check URL part
12302: my ($map,$resid,$url)=&decode_symb($symb);
12303:
12304: unless ($url eq $thisfn) { return 0; }
12305:
12306: $symb=&symbclean($symb);
12307: $thisurl=&deversion($thisurl);
12308: $thisfn=&deversion($thisfn);
12309:
12310: my %bighash;
12311: my $okay=0;
12312:
12313: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12314: &GDBM_READER(),0640)) {
12315: my $noclutter;
12316: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
12317: $thisurl =~ s/\?.+$//;
12318: if ($map =~ m{^uploaded/.+\.page$}) {
12319: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
12320: $thisurl =~ s{^\Qhttp://https://\E}{https://};
12321: $noclutter = 1;
12322: }
12323: }
12324: my $ids;
12325: if ($noclutter) {
12326: $ids=$bighash{'ids_'.$thisurl};
12327: } else {
12328: $ids=$bighash{'ids_'.&clutter($thisurl)};
12329: }
12330: unless ($ids) {
12331: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
12332: $ids=$bighash{$idkey};
12333: }
12334: if ($ids) {
12335: # ------------------------------------------------------------------- Has ID(s)
12336: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
12337: $symb =~ s/\?.+$//;
12338: }
12339: foreach my $id (split(/\,/,$ids)) {
12340: my ($mapid,$resid)=split(/\./,$id);
12341: if (
12342: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
12343: eq $symb) {
12344: if (ref($encstate)) {
12345: $$encstate = $bighash{'encrypted_'.$id};
12346: }
12347: if (($env{'request.role.adv'}) ||
12348: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
12349: ($thisurl eq '/adm/navmaps')) {
12350: $okay=1;
12351: last;
12352: }
12353: }
12354: }
12355: }
12356: untie(%bighash);
12357: }
12358: return $okay;
12359: }
12360:
12361: # --------------------------------------------------------------- Clean-up symb
12362:
12363: sub symbclean {
12364: my $symb=shift;
12365: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
12366: # remove version from map
12367: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
12368:
12369: # remove version from URL
12370: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
12371:
12372: # remove wrapper
12373:
12374: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
12375: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
12376: return $symb;
12377: }
12378:
12379: # ---------------------------------------------- Split symb to find map and url
12380:
12381: sub encode_symb {
12382: my ($map,$resid,$url)=@_;
12383: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
12384: }
12385:
12386: sub decode_symb {
12387: my $symb=shift;
12388: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
12389: my ($map,$resid,$url)=split(/___/,$symb);
12390: return (&fixversion($map),$resid,&fixversion($url));
12391: }
12392:
12393: sub fixversion {
12394: my $fn=shift;
12395: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
12396: my %bighash;
12397: my $uri=&clutter($fn);
12398: my $key=$env{'request.course.id'}.'_'.$uri;
12399: # is this cached?
12400: my ($result,$cached)=&is_cached_new('courseresversion',$key);
12401: if (defined($cached)) { return $result; }
12402: # unfortunately not cached, or expired
12403: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12404: &GDBM_READER(),0640)) {
12405: if ($bighash{'version_'.$uri}) {
12406: my $version=$bighash{'version_'.$uri};
12407: unless (($version eq 'mostrecent') ||
12408: ($version==&getversion($uri))) {
12409: $uri=~s/\.(\w+)$/\.$version\.$1/;
12410: }
12411: }
12412: untie %bighash;
12413: }
12414: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
12415: }
12416:
12417: sub deversion {
12418: my $url=shift;
12419: $url=~s/\.\d+\.(\w+)$/\.$1/;
12420: return $url;
12421: }
12422:
12423: # ------------------------------------------------------ Return symb list entry
12424:
12425: sub symbread {
12426: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
12427: my $cache_str='request.symbread.cached.'.$thisfn;
12428: if (defined($env{$cache_str})) {
12429: if ($ignorecachednull) {
12430: return $env{$cache_str} unless ($env{$cache_str} eq '');
12431: } else {
12432: return $env{$cache_str};
12433: }
12434: }
12435: # no filename provided? try from environment
12436: unless ($thisfn) {
12437: if ($env{'request.symb'}) {
12438: return $env{$cache_str}=&symbclean($env{'request.symb'});
12439: }
12440: $thisfn=$env{'request.filename'};
12441: }
12442: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
12443: # is that filename actually a symb? Verify, clean, and return
12444: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
12445: if (&symbverify($thisfn,$1)) {
12446: return $env{$cache_str}=&symbclean($thisfn);
12447: }
12448: }
12449: $thisfn=declutter($thisfn);
12450: my %hash;
12451: my %bighash;
12452: my $syval='';
12453: if (($env{'request.course.fn'}) && ($thisfn)) {
12454: my $targetfn = $thisfn;
12455: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
12456: $targetfn = 'adm/wrapper/'.$thisfn;
12457: }
12458: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
12459: $targetfn=$1;
12460: }
12461: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
12462: &GDBM_READER(),0640)) {
12463: $syval=$hash{$targetfn};
12464: untie(%hash);
12465: }
12466: # ---------------------------------------------------------- There was an entry
12467: if ($syval) {
12468: #unless ($syval=~/\_\d+$/) {
12469: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
12470: #&appenv({'request.ambiguous' => $thisfn});
12471: #return $env{$cache_str}='';
12472: #}
12473: #$syval.=$1;
12474: #}
12475: } else {
12476: # ------------------------------------------------------- Was not in symb table
12477: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12478: &GDBM_READER(),0640)) {
12479: # ---------------------------------------------- Get ID(s) for current resource
12480: my $ids=$bighash{'ids_'.&clutter($thisfn)};
12481: unless ($ids) {
12482: $ids=$bighash{'ids_/'.$thisfn};
12483: }
12484: unless ($ids) {
12485: # alias?
12486: $ids=$bighash{'mapalias_'.$thisfn};
12487: }
12488: if ($ids) {
12489: # ------------------------------------------------------------------- Has ID(s)
12490: my @possibilities=split(/\,/,$ids);
12491: if ($#possibilities==0) {
12492: # ----------------------------------------------- There is only one possibility
12493: my ($mapid,$resid)=split(/\./,$ids);
12494: $syval=&encode_symb($bighash{'map_id_'.$mapid},
12495: $resid,$thisfn);
12496: if (ref($possibles) eq 'HASH') {
12497: $possibles->{$syval} = 1;
12498: }
12499: if ($checkforblock) {
12500: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
12501: if (@blockers) {
12502: $syval = '';
12503: return;
12504: }
12505: }
12506: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
12507: # ------------------------------------------ There is more than one possibility
12508: my $realpossible=0;
12509: foreach my $id (@possibilities) {
12510: my $file=$bighash{'src_'.$id};
12511: my $canaccess;
12512: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
12513: $canaccess = 1;
12514: } else {
12515: $canaccess = &allowed('bre',$file);
12516: }
12517: if ($canaccess) {
12518: my ($mapid,$resid)=split(/\./,$id);
12519: if ($bighash{'map_type_'.$mapid} ne 'page') {
12520: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
12521: $resid,$thisfn);
12522: if (ref($possibles) eq 'HASH') {
12523: $possibles->{$syval} = 1;
12524: }
12525: if ($checkforblock) {
12526: my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
12527: unless (@blockers > 0) {
12528: $syval = $poss_syval;
12529: $realpossible++;
12530: }
12531: } else {
12532: $syval = $poss_syval;
12533: $realpossible++;
12534: }
12535: }
12536: }
12537: }
12538: if ($realpossible!=1) { $syval=''; }
12539: } else {
12540: $syval='';
12541: }
12542: }
12543: untie(%bighash);
12544: }
12545: }
12546: if ($syval) {
12547: return $env{$cache_str}=$syval;
12548: }
12549: }
12550: &appenv({'request.ambiguous' => $thisfn});
12551: return $env{$cache_str}='';
12552: }
12553:
12554: # ---------------------------------------------------------- Return random seed
12555:
12556: sub numval {
12557: my $txt=shift;
12558: $txt=~tr/A-J/0-9/;
12559: $txt=~tr/a-j/0-9/;
12560: $txt=~tr/K-T/0-9/;
12561: $txt=~tr/k-t/0-9/;
12562: $txt=~tr/U-Z/0-5/;
12563: $txt=~tr/u-z/0-5/;
12564: $txt=~s/\D//g;
12565: if ($_64bit) { if ($txt > 2**32) { return -1; } }
12566: return int($txt);
12567: }
12568:
12569: sub numval2 {
12570: my $txt=shift;
12571: $txt=~tr/A-J/0-9/;
12572: $txt=~tr/a-j/0-9/;
12573: $txt=~tr/K-T/0-9/;
12574: $txt=~tr/k-t/0-9/;
12575: $txt=~tr/U-Z/0-5/;
12576: $txt=~tr/u-z/0-5/;
12577: $txt=~s/\D//g;
12578: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12579: my $total;
12580: foreach my $val (@txts) { $total+=$val; }
12581: if ($_64bit) { if ($total > 2**32) { return -1; } }
12582: return int($total);
12583: }
12584:
12585: sub numval3 {
12586: use integer;
12587: my $txt=shift;
12588: $txt=~tr/A-J/0-9/;
12589: $txt=~tr/a-j/0-9/;
12590: $txt=~tr/K-T/0-9/;
12591: $txt=~tr/k-t/0-9/;
12592: $txt=~tr/U-Z/0-5/;
12593: $txt=~tr/u-z/0-5/;
12594: $txt=~s/\D//g;
12595: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12596: my $total;
12597: foreach my $val (@txts) { $total+=$val; }
12598: if ($_64bit) { $total=(($total<<32)>>32); }
12599: return $total;
12600: }
12601:
12602: sub digest {
12603: my ($data)=@_;
12604: my $digest=&Digest::MD5::md5($data);
12605: my ($a,$b,$c,$d)=unpack("iiii",$digest);
12606: my ($e,$f);
12607: {
12608: use integer;
12609: $e=($a+$b);
12610: $f=($c+$d);
12611: if ($_64bit) {
12612: $e=(($e<<32)>>32);
12613: $f=(($f<<32)>>32);
12614: }
12615: }
12616: if (wantarray) {
12617: return ($e,$f);
12618: } else {
12619: my $g;
12620: {
12621: use integer;
12622: $g=($e+$f);
12623: if ($_64bit) {
12624: $g=(($g<<32)>>32);
12625: }
12626: }
12627: return $g;
12628: }
12629: }
12630:
12631: sub latest_rnd_algorithm_id {
12632: return '64bit5';
12633: }
12634:
12635: sub get_rand_alg {
12636: my ($courseid)=@_;
12637: if (!$courseid) { $courseid=(&whichuser())[1]; }
12638: if ($courseid) {
12639: return $env{"course.$courseid.rndseed"};
12640: }
12641: return &latest_rnd_algorithm_id();
12642: }
12643:
12644: sub validCODE {
12645: my ($CODE)=@_;
12646: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
12647: return 0;
12648: }
12649:
12650: sub getCODE {
12651: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
12652: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
12653: defined($Apache::lonhomework::parsing_a_task) ) &&
12654: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
12655: return $Apache::lonhomework::history{'resource.CODE'};
12656: }
12657: return undef;
12658: }
12659: #
12660: # Determines the random seed for a specific context:
12661: #
12662: # parameters:
12663: # symb - in course context the symb for the seed.
12664: # course_id - The course id of the form domain_coursenum.
12665: # domain - Domain for the user.
12666: # course - Course for the user.
12667: # cenv - environment of the course.
12668: #
12669: # NOTE:
12670: # All parameters are picked out of the environment if missing
12671: # or not defined.
12672: # If a symb cannot be determined the current time is used instead.
12673: #
12674: # For a given well defined symb, courside, domain, username,
12675: # and course environment, the seed is reproducible.
12676: #
12677: sub rndseed {
12678: my ($symb,$courseid,$domain,$username, $cenv)=@_;
12679: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
12680: if (!defined($symb)) {
12681: unless ($symb=$wsymb) { return time; }
12682: }
12683: if (!defined $courseid) {
12684: $courseid=$wcourseid;
12685: }
12686: if (!defined $domain) { $domain=$wdomain; }
12687: if (!defined $username) { $username=$wusername }
12688:
12689: my $which;
12690: if (defined($cenv->{'rndseed'})) {
12691: $which = $cenv->{'rndseed'};
12692: } else {
12693: $which =&get_rand_alg($courseid);
12694: }
12695: if (defined(&getCODE())) {
12696:
12697: if ($which eq '64bit5') {
12698: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
12699: } elsif ($which eq '64bit4') {
12700: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
12701: } else {
12702: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
12703: }
12704: } elsif ($which eq '64bit5') {
12705: return &rndseed_64bit5($symb,$courseid,$domain,$username);
12706: } elsif ($which eq '64bit4') {
12707: return &rndseed_64bit4($symb,$courseid,$domain,$username);
12708: } elsif ($which eq '64bit3') {
12709: return &rndseed_64bit3($symb,$courseid,$domain,$username);
12710: } elsif ($which eq '64bit2') {
12711: return &rndseed_64bit2($symb,$courseid,$domain,$username);
12712: } elsif ($which eq '64bit') {
12713: return &rndseed_64bit($symb,$courseid,$domain,$username);
12714: }
12715: return &rndseed_32bit($symb,$courseid,$domain,$username);
12716: }
12717:
12718: sub rndseed_32bit {
12719: my ($symb,$courseid,$domain,$username)=@_;
12720: {
12721: use integer;
12722: my $symbchck=unpack("%32C*",$symb) << 27;
12723: my $symbseed=numval($symb) << 22;
12724: my $namechck=unpack("%32C*",$username) << 17;
12725: my $nameseed=numval($username) << 12;
12726: my $domainseed=unpack("%32C*",$domain) << 7;
12727: my $courseseed=unpack("%32C*",$courseid);
12728: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
12729: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12730: #&logthis("rndseed :$num:$symb");
12731: if ($_64bit) { $num=(($num<<32)>>32); }
12732: return $num;
12733: }
12734: }
12735:
12736: sub rndseed_64bit {
12737: my ($symb,$courseid,$domain,$username)=@_;
12738: {
12739: use integer;
12740: my $symbchck=unpack("%32S*",$symb) << 21;
12741: my $symbseed=numval($symb) << 10;
12742: my $namechck=unpack("%32S*",$username);
12743:
12744: my $nameseed=numval($username) << 21;
12745: my $domainseed=unpack("%32S*",$domain) << 10;
12746: my $courseseed=unpack("%32S*",$courseid);
12747:
12748: my $num1=$symbchck+$symbseed+$namechck;
12749: my $num2=$nameseed+$domainseed+$courseseed;
12750: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12751: #&logthis("rndseed :$num:$symb");
12752: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12753: return "$num1,$num2";
12754: }
12755: }
12756:
12757: sub rndseed_64bit2 {
12758: my ($symb,$courseid,$domain,$username)=@_;
12759: {
12760: use integer;
12761: # strings need to be an even # of cahracters long, it it is odd the
12762: # last characters gets thrown away
12763: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12764: my $symbseed=numval($symb) << 10;
12765: my $namechck=unpack("%32S*",$username.' ');
12766:
12767: my $nameseed=numval($username) << 21;
12768: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12769: my $courseseed=unpack("%32S*",$courseid.' ');
12770:
12771: my $num1=$symbchck+$symbseed+$namechck;
12772: my $num2=$nameseed+$domainseed+$courseseed;
12773: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12774: #&logthis("rndseed :$num:$symb");
12775: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12776: return "$num1,$num2";
12777: }
12778: }
12779:
12780: sub rndseed_64bit3 {
12781: my ($symb,$courseid,$domain,$username)=@_;
12782: {
12783: use integer;
12784: # strings need to be an even # of cahracters long, it it is odd the
12785: # last characters gets thrown away
12786: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12787: my $symbseed=numval2($symb) << 10;
12788: my $namechck=unpack("%32S*",$username.' ');
12789:
12790: my $nameseed=numval2($username) << 21;
12791: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12792: my $courseseed=unpack("%32S*",$courseid.' ');
12793:
12794: my $num1=$symbchck+$symbseed+$namechck;
12795: my $num2=$nameseed+$domainseed+$courseseed;
12796: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12797: #&logthis("rndseed :$num1:$num2:$_64bit");
12798: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12799:
12800: return "$num1:$num2";
12801: }
12802: }
12803:
12804: sub rndseed_64bit4 {
12805: my ($symb,$courseid,$domain,$username)=@_;
12806: {
12807: use integer;
12808: # strings need to be an even # of cahracters long, it it is odd the
12809: # last characters gets thrown away
12810: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12811: my $symbseed=numval3($symb) << 10;
12812: my $namechck=unpack("%32S*",$username.' ');
12813:
12814: my $nameseed=numval3($username) << 21;
12815: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12816: my $courseseed=unpack("%32S*",$courseid.' ');
12817:
12818: my $num1=$symbchck+$symbseed+$namechck;
12819: my $num2=$nameseed+$domainseed+$courseseed;
12820: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12821: #&logthis("rndseed :$num1:$num2:$_64bit");
12822: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12823:
12824: return "$num1:$num2";
12825: }
12826: }
12827:
12828: sub rndseed_64bit5 {
12829: my ($symb,$courseid,$domain,$username)=@_;
12830: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
12831: return "$num1:$num2";
12832: }
12833:
12834: sub rndseed_CODE_64bit {
12835: my ($symb,$courseid,$domain,$username)=@_;
12836: {
12837: use integer;
12838: my $symbchck=unpack("%32S*",$symb.' ') << 16;
12839: my $symbseed=numval2($symb);
12840: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12841: my $CODEseed=numval(&getCODE());
12842: my $courseseed=unpack("%32S*",$courseid.' ');
12843: my $num1=$symbseed+$CODEchck;
12844: my $num2=$CODEseed+$courseseed+$symbchck;
12845: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12846: #&logthis("rndseed :$num1:$num2:$symb");
12847: if ($_64bit) { $num1=(($num1<<32)>>32); }
12848: if ($_64bit) { $num2=(($num2<<32)>>32); }
12849: return "$num1:$num2";
12850: }
12851: }
12852:
12853: sub rndseed_CODE_64bit4 {
12854: my ($symb,$courseid,$domain,$username)=@_;
12855: {
12856: use integer;
12857: my $symbchck=unpack("%32S*",$symb.' ') << 16;
12858: my $symbseed=numval3($symb);
12859: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12860: my $CODEseed=numval3(&getCODE());
12861: my $courseseed=unpack("%32S*",$courseid.' ');
12862: my $num1=$symbseed+$CODEchck;
12863: my $num2=$CODEseed+$courseseed+$symbchck;
12864: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12865: #&logthis("rndseed :$num1:$num2:$symb");
12866: if ($_64bit) { $num1=(($num1<<32)>>32); }
12867: if ($_64bit) { $num2=(($num2<<32)>>32); }
12868: return "$num1:$num2";
12869: }
12870: }
12871:
12872: sub rndseed_CODE_64bit5 {
12873: my ($symb,$courseid,$domain,$username)=@_;
12874: my $code = &getCODE();
12875: my ($num1,$num2)=&digest("$symb,$courseid,$code");
12876: return "$num1:$num2";
12877: }
12878:
12879: sub setup_random_from_rndseed {
12880: my ($rndseed)=@_;
12881: if ($rndseed =~/([,:])/) {
12882: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
12883: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
12884: &Math::Random::random_set_seed_from_phrase($rndseed);
12885: } else {
12886: &Math::Random::random_set_seed($num1,$num2);
12887: }
12888: } else {
12889: &Math::Random::random_set_seed_from_phrase($rndseed);
12890: }
12891: }
12892:
12893: sub latest_receipt_algorithm_id {
12894: return 'receipt3';
12895: }
12896:
12897: sub recunique {
12898: my $fucourseid=shift;
12899: my $unique;
12900: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
12901: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
12902: $unique=$env{"course.$fucourseid.internal.encseed"};
12903: } else {
12904: $unique=$perlvar{'lonReceipt'};
12905: }
12906: return unpack("%32C*",$unique);
12907: }
12908:
12909: sub recprefix {
12910: my $fucourseid=shift;
12911: my $prefix;
12912: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
12913: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
12914: $prefix=$env{"course.$fucourseid.internal.encpref"};
12915: } else {
12916: $prefix=$perlvar{'lonHostID'};
12917: }
12918: return unpack("%32C*",$prefix);
12919: }
12920:
12921: sub ireceipt {
12922: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
12923:
12924: my $return =&recprefix($fucourseid).'-';
12925:
12926: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
12927: $env{'request.state'} eq 'construct') {
12928: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
12929: return $return;
12930: }
12931:
12932: my $cuname=unpack("%32C*",$funame);
12933: my $cudom=unpack("%32C*",$fudom);
12934: my $cucourseid=unpack("%32C*",$fucourseid);
12935: my $cusymb=unpack("%32C*",$fusymb);
12936: my $cunique=&recunique($fucourseid);
12937: my $cpart=unpack("%32S*",$part);
12938: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
12939:
12940: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
12941:
12942: $return.= ($cunique%$cuname+
12943: $cunique%$cudom+
12944: $cusymb%$cuname+
12945: $cusymb%$cudom+
12946: $cucourseid%$cuname+
12947: $cucourseid%$cudom+
12948: $cpart%$cuname+
12949: $cpart%$cudom);
12950: } else {
12951: $return.= ($cunique%$cuname+
12952: $cunique%$cudom+
12953: $cusymb%$cuname+
12954: $cusymb%$cudom+
12955: $cucourseid%$cuname+
12956: $cucourseid%$cudom);
12957: }
12958: return $return;
12959: }
12960:
12961: sub receipt {
12962: my ($part)=@_;
12963: my ($symb,$courseid,$domain,$name) = &whichuser();
12964: return &ireceipt($name,$domain,$courseid,$symb,$part);
12965: }
12966:
12967: sub whichuser {
12968: my ($passedsymb)=@_;
12969: my ($symb,$courseid,$domain,$name,$publicuser);
12970: if (defined($env{'form.grade_symb'})) {
12971: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
12972: my $allowed=&allowed('vgr',$tmp_courseid);
12973: if (!$allowed &&
12974: exists($env{'request.course.sec'}) &&
12975: $env{'request.course.sec'} !~ /^\s*$/) {
12976: $allowed=&allowed('vgr',$tmp_courseid.
12977: '/'.$env{'request.course.sec'});
12978: }
12979: if ($allowed) {
12980: ($symb)=&get_env_multiple('form.grade_symb');
12981: $courseid=$tmp_courseid;
12982: ($domain)=&get_env_multiple('form.grade_domain');
12983: ($name)=&get_env_multiple('form.grade_username');
12984: return ($symb,$courseid,$domain,$name,$publicuser);
12985: }
12986: }
12987: if (!$passedsymb) {
12988: $symb=&symbread();
12989: } else {
12990: $symb=$passedsymb;
12991: }
12992: $courseid=$env{'request.course.id'};
12993: $domain=$env{'user.domain'};
12994: $name=$env{'user.name'};
12995: if ($name eq 'public' && $domain eq 'public') {
12996: if (!defined($env{'form.username'})) {
12997: $env{'form.username'}.=time.rand(10000000);
12998: }
12999: $name.=$env{'form.username'};
13000: }
13001: return ($symb,$courseid,$domain,$name,$publicuser);
13002:
13003: }
13004:
13005: # ------------------------------------------------------------ Serves up a file
13006: # returns either the contents of the file or
13007: # -1 if the file doesn't exist
13008: #
13009: # if the target is a file that was uploaded via DOCS,
13010: # a check will be made to see if a current copy exists on the local server,
13011: # if it does this will be served, otherwise a copy will be retrieved from
13012: # the home server for the course and stored in /home/httpd/html/userfiles on
13013: # the local server.
13014:
13015: sub getfile {
13016: my ($file) = @_;
13017: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
13018: &repcopy($file);
13019: return &readfile($file);
13020: }
13021:
13022: sub repcopy_userfile {
13023: my ($file)=@_;
13024: my $londocroot = $perlvar{'lonDocRoot'};
13025: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
13026: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
13027: my ($cdom,$cnum,$filename) =
13028: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
13029: my $uri="/uploaded/$cdom/$cnum/$filename";
13030: if (-e "$file") {
13031: # we already have a local copy, check it out
13032: my @fileinfo = stat($file);
13033: my $rtncode;
13034: my $info;
13035: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
13036: if ($lwpresp ne 'ok') {
13037: # there is no such file anymore, even though we had a local copy
13038: if ($rtncode eq '404') {
13039: unlink($file);
13040: }
13041: return -1;
13042: }
13043: if ($info < $fileinfo[9]) {
13044: # nice, the file we have is up-to-date, just say okay
13045: return 'ok';
13046: } else {
13047: # the file is outdated, get rid of it
13048: unlink($file);
13049: }
13050: }
13051: # one way or the other, at this point, we don't have the file
13052: # construct the correct path for the file
13053: my @parts = ($cdom,$cnum);
13054: if ($filename =~ m|^(.+)/[^/]+$|) {
13055: push @parts, split(/\//,$1);
13056: }
13057: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
13058: foreach my $part (@parts) {
13059: $path .= '/'.$part;
13060: if (!-e $path) {
13061: mkdir($path,0770);
13062: }
13063: }
13064: # now the path exists for sure
13065: # get a user agent
13066: my $transferfile=$file.'.in.transfer';
13067: # FIXME: this should flock
13068: if (-e $transferfile) { return 'ok'; }
13069: my $request;
13070: $uri=~s/^\///;
13071: my $homeserver = &homeserver($cnum,$cdom);
13072: my $protocol = $protocol{$homeserver};
13073: $protocol = 'http' if ($protocol ne 'https');
13074: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
13075: my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
13076: # did it work?
13077: if ($response->is_error()) {
13078: unlink($transferfile);
13079: &logthis("Userfile repcopy failed for $uri");
13080: return -1;
13081: }
13082: # worked, rename the transfer file
13083: rename($transferfile,$file);
13084: return 'ok';
13085: }
13086:
13087: sub tokenwrapper {
13088: my $uri=shift;
13089: $uri=~s|^https?\://([^/]+)||;
13090: $uri=~s|^/||;
13091: $env{'user.environment'}=~/\/([^\/]+)\.id/;
13092: my $token=$1;
13093: my (undef,$udom,$uname,$file)=split('/',$uri,4);
13094: if ($udom && $uname && $file) {
13095: $file=~s|(\?\.*)*$||;
13096: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
13097: my $homeserver = &homeserver($uname,$udom);
13098: my $protocol = $protocol{$homeserver};
13099: $protocol = 'http' if ($protocol ne 'https');
13100: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
13101: (($uri=~/\?/)?'&':'?').'token='.$token.
13102: '&tokenissued='.$perlvar{'lonHostID'};
13103: } else {
13104: return '/adm/notfound.html';
13105: }
13106: }
13107:
13108: # call with reqtype HEAD: get last modification time
13109: # call with reqtype GET: get the file contents
13110: # Do not call this with reqtype GET for large files! It loads everything into memory
13111: #
13112: sub getuploaded {
13113: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
13114: $uri=~s/^\///;
13115: my $homeserver = &homeserver($cnum,$cdom);
13116: my $protocol = $protocol{$homeserver};
13117: $protocol = 'http' if ($protocol ne 'https');
13118: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
13119: my $request=new HTTP::Request($reqtype,$uri);
13120: my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
13121: $$rtncode = $response->code;
13122: if (! $response->is_success()) {
13123: return 'failed';
13124: }
13125: if ($reqtype eq 'HEAD') {
13126: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
13127: } elsif ($reqtype eq 'GET') {
13128: $$info = $response->content;
13129: }
13130: return 'ok';
13131: }
13132:
13133: sub readfile {
13134: my $file = shift;
13135: if ( (! -e $file ) || ($file eq '') ) { return -1; };
13136: my $fh;
13137: open($fh,"<",$file);
13138: my $a='';
13139: while (my $line = <$fh>) { $a .= $line; }
13140: return $a;
13141: }
13142:
13143: sub filelocation {
13144: my ($dir,$file) = @_;
13145: my $location;
13146: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
13147:
13148: if ($file =~ m-^/adm/-) {
13149: $file=~s-^/adm/wrapper/-/-;
13150: $file=~s-^/adm/coursedocs/showdoc/-/-;
13151: }
13152:
13153: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
13154: $location = $file;
13155: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
13156: my ($udom,$uname,$filename)=
13157: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
13158: my $home=&homeserver($uname,$udom);
13159: my $is_me=0;
13160: my @ids=¤t_machine_ids();
13161: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
13162: if ($is_me) {
13163: $location=propath($udom,$uname).'/userfiles/'.$filename;
13164: } else {
13165: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
13166: $udom.'/'.$uname.'/'.$filename;
13167: }
13168: } elsif ($file =~ m-^/adm/-) {
13169: $location = $perlvar{'lonDocRoot'}.'/'.$file;
13170: } else {
13171: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
13172: $file=~s:^/(res|priv)/:/:;
13173: my $space=$1;
13174: if ( !( $file =~ m:^/:) ) {
13175: $location = $dir. '/'.$file;
13176: } else {
13177: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
13178: }
13179: }
13180: $location=~s://+:/:g; # remove duplicate /
13181: while ($location=~m{/\.\./}) {
13182: if ($location =~ m{/[^/]+/\.\./}) {
13183: $location=~ s{/[^/]+/\.\./}{/}g;
13184: } else {
13185: $location=~ s{/\.\./}{/}g;
13186: }
13187: } #remove dir/..
13188: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
13189: return $location;
13190: }
13191:
13192: sub hreflocation {
13193: my ($dir,$file)=@_;
13194: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
13195: $file=filelocation($dir,$file);
13196: } elsif ($file=~m-^/adm/-) {
13197: $file=~s-^/adm/wrapper/-/-;
13198: $file=~s-^/adm/coursedocs/showdoc/-/-;
13199: }
13200: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
13201: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
13202: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
13203: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
13204: {/uploaded/$1/$2/}x;
13205: }
13206: if ($file=~ m{^/userfiles/}) {
13207: $file =~ s{^/userfiles/}{/uploaded/};
13208: }
13209: return $file;
13210: }
13211:
13212:
13213:
13214:
13215:
13216: sub current_machine_domains {
13217: return &machine_domains(&hostname($perlvar{'lonHostID'}));
13218: }
13219:
13220: sub machine_domains {
13221: my ($hostname) = @_;
13222: my @domains;
13223: my %hostname = &all_hostnames();
13224: while( my($id, $name) = each(%hostname)) {
13225: # &logthis("-$id-$name-$hostname-");
13226: if ($hostname eq $name) {
13227: push(@domains,&host_domain($id));
13228: }
13229: }
13230: return @domains;
13231: }
13232:
13233: sub current_machine_ids {
13234: return &machine_ids(&hostname($perlvar{'lonHostID'}));
13235: }
13236:
13237: sub machine_ids {
13238: my ($hostname) = @_;
13239: $hostname ||= &hostname($perlvar{'lonHostID'});
13240: my @ids;
13241: my %name_to_host = &all_names();
13242: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
13243: return @{ $name_to_host{$hostname} };
13244: }
13245: return;
13246: }
13247:
13248: sub additional_machine_domains {
13249: my @domains;
13250: open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
13251: while( my $line = <$fh>) {
13252: $line =~ s/\s//g;
13253: push(@domains,$line);
13254: }
13255: return @domains;
13256: }
13257:
13258: sub default_login_domain {
13259: my $domain = $perlvar{'lonDefDomain'};
13260: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
13261: foreach my $posdom (¤t_machine_domains(),
13262: &additional_machine_domains()) {
13263: if (lc($posdom) eq lc($testdomain)) {
13264: $domain=$posdom;
13265: last;
13266: }
13267: }
13268: return $domain;
13269: }
13270:
13271: # ------------------------------------------------------------- Declutters URLs
13272:
13273: sub declutter {
13274: my $thisfn=shift;
13275: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13276: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
13277: $thisfn=~s{^/home/httpd/html}{};
13278: }
13279: $thisfn=~s/^\///;
13280: $thisfn=~s|^adm/wrapper/||;
13281: $thisfn=~s|^adm/coursedocs/showdoc/||;
13282: $thisfn=~s/^res\///;
13283: $thisfn=~s/^priv\///;
13284: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
13285: $thisfn=~s/\?.+$//;
13286: }
13287: return $thisfn;
13288: }
13289:
13290: # ------------------------------------------------------------- Clutter up URLs
13291:
13292: sub clutter {
13293: my $thisfn='/'.&declutter(shift);
13294: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
13295: || $thisfn =~ m{^/adm/(includes|pages)} ) {
13296: $thisfn='/res'.$thisfn;
13297: }
13298: if ($thisfn !~m|^/adm|) {
13299: if ($thisfn =~ m|^/ext/|) {
13300: $thisfn='/adm/wrapper'.$thisfn;
13301: } else {
13302: my ($ext) = ($thisfn =~ /\.(\w+)$/);
13303: my $embstyle=&Apache::loncommon::fileembstyle($ext);
13304: if ($embstyle eq 'ssi'
13305: || ($embstyle eq 'hdn')
13306: || ($embstyle eq 'rat')
13307: || ($embstyle eq 'prv')
13308: || ($embstyle eq 'ign')) {
13309: #do nothing with these
13310: } elsif (($embstyle eq 'img')
13311: || ($embstyle eq 'emb')
13312: || ($embstyle eq 'wrp')) {
13313: $thisfn='/adm/wrapper'.$thisfn;
13314: } elsif ($embstyle eq 'unk'
13315: && $thisfn!~/\.(sequence|page)$/) {
13316: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
13317: } else {
13318: # &logthis("Got a blank emb style");
13319: }
13320: }
13321: } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
13322: $thisfn='/adm/wrapper'.$thisfn;
13323: }
13324: return $thisfn;
13325: }
13326:
13327: sub clutter_with_no_wrapper {
13328: my $uri = &clutter(shift);
13329: if ($uri =~ m-^/adm/-) {
13330: $uri =~ s-^/adm/wrapper/-/-;
13331: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
13332: }
13333: return $uri;
13334: }
13335:
13336: sub freeze_escape {
13337: my ($value)=@_;
13338: if (ref($value)) {
13339: $value=&nfreeze($value);
13340: return '__FROZEN__'.&escape($value);
13341: }
13342: return &escape($value);
13343: }
13344:
13345:
13346: sub thaw_unescape {
13347: my ($value)=@_;
13348: if ($value =~ /^__FROZEN__/) {
13349: substr($value,0,10,undef);
13350: $value=&unescape($value);
13351: return &thaw($value);
13352: }
13353: return &unescape($value);
13354: }
13355:
13356: sub correct_line_ends {
13357: my ($result)=@_;
13358: $$result =~s/\r\n/\n/mg;
13359: $$result =~s/\r/\n/mg;
13360: }
13361: # ================================================================ Main Program
13362:
13363: sub goodbye {
13364: &logthis("Starting Shut down");
13365: #not converted to using infrastruture and probably shouldn't be
13366: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
13367: #converted
13368: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
13369: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
13370: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
13371: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
13372: #1.1 only
13373: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
13374: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
13375: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
13376: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
13377: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
13378: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
13379: &logthis(sprintf("%-20s is %s",'hits',$hits));
13380: &flushcourselogs();
13381: &logthis("Shutting down");
13382: }
13383:
13384: sub get_dns {
13385: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
13386: if (!$ignore_cache) {
13387: my ($content,$cached)=
13388: &Apache::lonnet::is_cached_new('dns',$url);
13389: if ($cached) {
13390: &$func($content,$hashref);
13391: return;
13392: }
13393: }
13394:
13395: my %alldns;
13396: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
13397: foreach my $dns (<$config>) {
13398: next if ($dns !~ /^\^(\S*)/x);
13399: my $line = $1;
13400: my ($host,$protocol) = split(/:/,$line);
13401: if ($protocol ne 'https') {
13402: $protocol = 'http';
13403: }
13404: $alldns{$host} = $protocol;
13405: }
13406: while (%alldns) {
13407: my ($dns) = sort { $b cmp $a } keys(%alldns);
13408: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
13409: my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
13410: delete($alldns{$dns});
13411: next if ($response->is_error());
13412: my @content = split("\n",$response->content);
13413: unless ($nocache) {
13414: &do_cache_new('dns',$url,\@content,30*24*60*60);
13415: }
13416: &$func(\@content,$hashref);
13417: return;
13418: }
13419: close($config);
13420: my $which = (split('/',$url))[3];
13421: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
13422: open($config,"<","$perlvar{'lonTabDir'}/dns_$which.tab");
13423: my @content = <$config>;
13424: &$func(\@content,$hashref);
13425: return;
13426: }
13427:
13428: # ------------------------------------------------------Get DNS checksums file
13429: sub parse_dns_checksums_tab {
13430: my ($lines,$hashref) = @_;
13431: my $lonhost = $perlvar{'lonHostID'};
13432: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
13433: my $loncaparev = &get_server_loncaparev($machine_dom);
13434: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
13435: my $webconfdir = '/etc/httpd/conf';
13436: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
13437: $webconfdir = '/etc/apache2';
13438: } elsif ($distro =~ /^sles(\d+)$/) {
13439: if ($1 >= 10) {
13440: $webconfdir = '/etc/apache2';
13441: }
13442: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
13443: if ($1 >= 10.0) {
13444: $webconfdir = '/etc/apache2';
13445: }
13446: }
13447: my ($release,$timestamp) = split(/\-/,$loncaparev);
13448: my (%chksum,%revnum);
13449: if (ref($lines) eq 'ARRAY') {
13450: chomp(@{$lines});
13451: my $version = shift(@{$lines});
13452: if ($version eq $release) {
13453: foreach my $line (@{$lines}) {
13454: my ($file,$version,$shasum) = split(/,/,$line);
13455: if ($file =~ m{^/etc/httpd/conf}) {
13456: if ($webconfdir eq '/etc/apache2') {
13457: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
13458: }
13459: }
13460: $chksum{$file} = $shasum;
13461: $revnum{$file} = $version;
13462: }
13463: if (ref($hashref) eq 'HASH') {
13464: %{$hashref} = (
13465: sums => \%chksum,
13466: versions => \%revnum,
13467: );
13468: }
13469: }
13470: }
13471: return;
13472: }
13473:
13474: sub fetch_dns_checksums {
13475: my %checksums;
13476: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
13477: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
13478: my ($release,$timestamp) = split(/\-/,$loncaparev);
13479: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
13480: \%checksums);
13481: return \%checksums;
13482: }
13483:
13484: # ------------------------------------------------------------ Read domain file
13485: {
13486: my $loaded;
13487: my %domain;
13488:
13489: sub parse_domain_tab {
13490: my ($lines) = @_;
13491: foreach my $line (@$lines) {
13492: next if ($line =~ /^(\#|\s*$ )/x);
13493:
13494: chomp($line);
13495: my ($name,@elements) = split(/:/,$line,9);
13496: my %this_domain;
13497: foreach my $field ('description', 'auth_def', 'auth_arg_def',
13498: 'lang_def', 'city', 'longi', 'lati',
13499: 'primary') {
13500: $this_domain{$field} = shift(@elements);
13501: }
13502: $domain{$name} = \%this_domain;
13503: }
13504: }
13505:
13506: sub reset_domain_info {
13507: undef($loaded);
13508: undef(%domain);
13509: }
13510:
13511: sub load_domain_tab {
13512: my ($ignore_cache,$nocache) = @_;
13513: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
13514: my $fh;
13515: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
13516: my @lines = <$fh>;
13517: &parse_domain_tab(\@lines);
13518: }
13519: close($fh);
13520: $loaded = 1;
13521: }
13522:
13523: sub domain {
13524: &load_domain_tab() if (!$loaded);
13525:
13526: my ($name,$what) = @_;
13527: return if ( !exists($domain{$name}) );
13528:
13529: if (!$what) {
13530: return $domain{$name}{'description'};
13531: }
13532: return $domain{$name}{$what};
13533: }
13534:
13535: sub domain_info {
13536: &load_domain_tab() if (!$loaded);
13537: return %domain;
13538: }
13539:
13540: }
13541:
13542:
13543: # ------------------------------------------------------------- Read hosts file
13544: {
13545: my %hostname;
13546: my %hostdom;
13547: my %libserv;
13548: my $loaded;
13549: my %name_to_host;
13550: my %internetdom;
13551: my %LC_dns_serv;
13552:
13553: sub parse_hosts_tab {
13554: my ($file) = @_;
13555: foreach my $configline (@$file) {
13556: next if ($configline =~ /^(\#|\s*$ )/x);
13557: chomp($configline);
13558: if ($configline =~ /^\^/) {
13559: if ($configline =~ /^\^([\w.\-]+)/) {
13560: $LC_dns_serv{$1} = 1;
13561: }
13562: next;
13563: }
13564: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
13565: $name=~s/\s//g;
13566: if ($id && $domain && $role && $name) {
13567: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
13568: my $curr = $hostname{$id};
13569: my $skip;
13570: if (ref($name_to_host{$curr}) eq 'ARRAY') {
13571: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
13572: $skip = 1;
13573: } else {
13574: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
13575: }
13576: }
13577: unless ($skip) {
13578: push(@{$name_to_host{$name}},$id);
13579: }
13580: } else {
13581: push(@{$name_to_host{$name}},$id);
13582: }
13583: $hostname{$id}=$name;
13584: $hostdom{$id}=$domain;
13585: if ($role eq 'library') { $libserv{$id}=$name; }
13586: if (defined($protocol)) {
13587: if ($protocol eq 'https') {
13588: $protocol{$id} = $protocol;
13589: } else {
13590: $protocol{$id} = 'http';
13591: }
13592: } else {
13593: $protocol{$id} = 'http';
13594: }
13595: if (defined($intdom)) {
13596: $internetdom{$id} = $intdom;
13597: }
13598: }
13599: }
13600: }
13601:
13602: sub reset_hosts_info {
13603: &purge_remembered();
13604: &reset_domain_info();
13605: &reset_hosts_ip_info();
13606: undef(%internetdom);
13607: undef(%name_to_host);
13608: undef(%hostname);
13609: undef(%hostdom);
13610: undef(%libserv);
13611: undef($loaded);
13612: }
13613:
13614: sub load_hosts_tab {
13615: my ($ignore_cache,$nocache) = @_;
13616: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
13617: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
13618: my @config = <$config>;
13619: &parse_hosts_tab(\@config);
13620: close($config);
13621: $loaded=1;
13622: }
13623:
13624: sub hostname {
13625: &load_hosts_tab() if (!$loaded);
13626:
13627: my ($lonid) = @_;
13628: return $hostname{$lonid};
13629: }
13630:
13631: sub all_hostnames {
13632: &load_hosts_tab() if (!$loaded);
13633:
13634: return %hostname;
13635: }
13636:
13637: sub all_names {
13638: my ($ignore_cache,$nocache) = @_;
13639: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
13640:
13641: return %name_to_host;
13642: }
13643:
13644: sub all_host_domain {
13645: &load_hosts_tab() if (!$loaded);
13646: return %hostdom;
13647: }
13648:
13649: sub all_host_intdom {
13650: &load_hosts_tab() if (!$loaded);
13651: return %internetdom;
13652: }
13653:
13654: sub is_library {
13655: &load_hosts_tab() if (!$loaded);
13656:
13657: return exists($libserv{$_[0]});
13658: }
13659:
13660: sub all_library {
13661: &load_hosts_tab() if (!$loaded);
13662:
13663: return %libserv;
13664: }
13665:
13666: sub unique_library {
13667: #2x reverse removes all hostnames that appear more than once
13668: my %unique = reverse &all_library();
13669: return reverse %unique;
13670: }
13671:
13672: sub get_servers {
13673: &load_hosts_tab() if (!$loaded);
13674:
13675: my ($domain,$type) = @_;
13676: my %possible_hosts = ($type eq 'library') ? %libserv
13677: : %hostname;
13678: my %result;
13679: if (ref($domain) eq 'ARRAY') {
13680: while ( my ($host,$hostname) = each(%possible_hosts)) {
13681: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
13682: $result{$host} = $hostname;
13683: }
13684: }
13685: } else {
13686: while ( my ($host,$hostname) = each(%possible_hosts)) {
13687: if ($hostdom{$host} eq $domain) {
13688: $result{$host} = $hostname;
13689: }
13690: }
13691: }
13692: return %result;
13693: }
13694:
13695: sub get_unique_servers {
13696: my %unique = reverse &get_servers(@_);
13697: return reverse %unique;
13698: }
13699:
13700: sub host_domain {
13701: &load_hosts_tab() if (!$loaded);
13702:
13703: my ($lonid) = @_;
13704: return $hostdom{$lonid};
13705: }
13706:
13707: sub all_domains {
13708: &load_hosts_tab() if (!$loaded);
13709:
13710: my %seen;
13711: my @uniq = grep(!$seen{$_}++, values(%hostdom));
13712: return @uniq;
13713: }
13714:
13715: sub internet_dom {
13716: &load_hosts_tab() if (!$loaded);
13717:
13718: my ($lonid) = @_;
13719: return $internetdom{$lonid};
13720: }
13721:
13722: sub is_LC_dns {
13723: &load_hosts_tab() if (!$loaded);
13724:
13725: my ($hostname) = @_;
13726: return exists($LC_dns_serv{$hostname});
13727: }
13728:
13729: }
13730:
13731: {
13732: my %iphost;
13733: my %name_to_ip;
13734: my %lonid_to_ip;
13735:
13736: sub get_hosts_from_ip {
13737: my ($ip) = @_;
13738: my %iphosts = &get_iphost();
13739: if (ref($iphosts{$ip})) {
13740: return @{$iphosts{$ip}};
13741: }
13742: return;
13743: }
13744:
13745: sub reset_hosts_ip_info {
13746: undef(%iphost);
13747: undef(%name_to_ip);
13748: undef(%lonid_to_ip);
13749: }
13750:
13751: sub get_host_ip {
13752: my ($lonid) = @_;
13753: if (exists($lonid_to_ip{$lonid})) {
13754: return $lonid_to_ip{$lonid};
13755: }
13756: my $name=&hostname($lonid);
13757: my $ip = gethostbyname($name);
13758: return if (!$ip || length($ip) ne 4);
13759: $ip=inet_ntoa($ip);
13760: $name_to_ip{$name} = $ip;
13761: $lonid_to_ip{$lonid} = $ip;
13762: return $ip;
13763: }
13764:
13765: sub get_iphost {
13766: my ($ignore_cache,$nocache) = @_;
13767:
13768: if (!$ignore_cache) {
13769: if (%iphost) {
13770: return %iphost;
13771: }
13772: my ($ip_info,$cached)=
13773: &Apache::lonnet::is_cached_new('iphost','iphost');
13774: if ($cached) {
13775: %iphost = %{$ip_info->[0]};
13776: %name_to_ip = %{$ip_info->[1]};
13777: %lonid_to_ip = %{$ip_info->[2]};
13778: return %iphost;
13779: }
13780: }
13781:
13782: # get yesterday's info for fallback
13783: my %old_name_to_ip;
13784: my ($ip_info,$cached)=
13785: &Apache::lonnet::is_cached_new('iphost','iphost');
13786: if ($cached) {
13787: %old_name_to_ip = %{$ip_info->[1]};
13788: }
13789:
13790: my %name_to_host = &all_names($ignore_cache,$nocache);
13791: foreach my $name (keys(%name_to_host)) {
13792: my $ip;
13793: if (!exists($name_to_ip{$name})) {
13794: $ip = gethostbyname($name);
13795: if (!$ip || length($ip) ne 4) {
13796: if (defined($old_name_to_ip{$name})) {
13797: $ip = $old_name_to_ip{$name};
13798: &logthis("Can't find $name defaulting to old $ip");
13799: } else {
13800: &logthis("Name $name no IP found");
13801: next;
13802: }
13803: } else {
13804: $ip=inet_ntoa($ip);
13805: }
13806: $name_to_ip{$name} = $ip;
13807: } else {
13808: $ip = $name_to_ip{$name};
13809: }
13810: foreach my $id (@{ $name_to_host{$name} }) {
13811: $lonid_to_ip{$id} = $ip;
13812: }
13813: push(@{$iphost{$ip}},@{$name_to_host{$name}});
13814: }
13815: unless ($nocache) {
13816: &do_cache_new('iphost','iphost',
13817: [\%iphost,\%name_to_ip,\%lonid_to_ip],
13818: 48*60*60);
13819: }
13820:
13821: return %iphost;
13822: }
13823:
13824: #
13825: # Given a DNS returns the loncapa host name for that DNS
13826: #
13827: sub host_from_dns {
13828: my ($dns) = @_;
13829: my @hosts;
13830: my $ip;
13831:
13832: if (exists($name_to_ip{$dns})) {
13833: $ip = $name_to_ip{$dns};
13834: }
13835: if (!$ip) {
13836: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
13837: if (length($ip) == 4) {
13838: $ip = &IO::Socket::inet_ntoa($ip);
13839: }
13840: }
13841: if ($ip) {
13842: @hosts = get_hosts_from_ip($ip);
13843: return $hosts[0];
13844: }
13845: return undef;
13846: }
13847:
13848: sub get_internet_names {
13849: my ($lonid) = @_;
13850: return if ($lonid eq '');
13851: my ($idnref,$cached)=
13852: &Apache::lonnet::is_cached_new('internetnames',$lonid);
13853: if ($cached) {
13854: return $idnref;
13855: }
13856: my $ip = &get_host_ip($lonid);
13857: my @hosts = &get_hosts_from_ip($ip);
13858: my %iphost = &get_iphost();
13859: my (@idns,%seen);
13860: foreach my $id (@hosts) {
13861: my $dom = &host_domain($id);
13862: my $prim_id = &domain($dom,'primary');
13863: my $prim_ip = &get_host_ip($prim_id);
13864: next if ($seen{$prim_ip});
13865: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
13866: foreach my $id (@{$iphost{$prim_ip}}) {
13867: my $intdom = &internet_dom($id);
13868: unless (grep(/^\Q$intdom\E$/,@idns)) {
13869: push(@idns,$intdom);
13870: }
13871: }
13872: }
13873: $seen{$prim_ip} = 1;
13874: }
13875: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
13876: }
13877:
13878: }
13879:
13880: sub all_loncaparevs {
13881: 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 2.11);
13882: }
13883:
13884: # ---------------------------------------------------------- Read loncaparev table
13885: {
13886: sub load_loncaparevs {
13887: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
13888: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
13889: while (my $configline=<$config>) {
13890: chomp($configline);
13891: my ($hostid,$loncaparev)=split(/:/,$configline);
13892: $loncaparevs{$hostid}=$loncaparev;
13893: }
13894: close($config);
13895: }
13896: }
13897: }
13898: }
13899:
13900: # ---------------------------------------------------------- Read serverhostID table
13901: {
13902: sub load_serverhomeIDs {
13903: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
13904: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
13905: while (my $configline=<$config>) {
13906: chomp($configline);
13907: my ($name,$id)=split(/:/,$configline);
13908: $serverhomeIDs{$name}=$id;
13909: }
13910: close($config);
13911: }
13912: }
13913: }
13914: }
13915:
13916:
13917: BEGIN {
13918:
13919: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
13920: unless ($readit) {
13921: {
13922: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
13923: %perlvar = (%perlvar,%{$configvars});
13924: }
13925:
13926:
13927: # ------------------------------------------------------ Read spare server file
13928: {
13929: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
13930:
13931: while (my $configline=<$config>) {
13932: chomp($configline);
13933: if ($configline) {
13934: my ($host,$type) = split(':',$configline,2);
13935: if (!defined($type) || $type eq '') { $type = 'default' };
13936: push(@{ $spareid{$type} }, $host);
13937: }
13938: }
13939: close($config);
13940: }
13941: # ------------------------------------------------------------ Read permissions
13942: {
13943: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
13944:
13945: while (my $configline=<$config>) {
13946: chomp($configline);
13947: if ($configline) {
13948: my ($role,$perm)=split(/ /,$configline);
13949: if ($perm ne '') { $pr{$role}=$perm; }
13950: }
13951: }
13952: close($config);
13953: }
13954:
13955: # -------------------------------------------- Read plain texts for permissions
13956: {
13957: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
13958:
13959: while (my $configline=<$config>) {
13960: chomp($configline);
13961: if ($configline) {
13962: my ($short,@plain)=split(/:/,$configline);
13963: %{$prp{$short}} = ();
13964: if (@plain > 0) {
13965: $prp{$short}{'std'} = $plain[0];
13966: for (my $i=1; $i<@plain; $i++) {
13967: $prp{$short}{'alt'.$i} = $plain[$i];
13968: }
13969: }
13970: }
13971: }
13972: close($config);
13973: }
13974:
13975: # ---------------------------------------------------------- Read package table
13976: {
13977: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
13978:
13979: while (my $configline=<$config>) {
13980: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
13981: chomp($configline);
13982: my ($short,$plain)=split(/:/,$configline);
13983: my ($pack,$name)=split(/\&/,$short);
13984: if ($plain ne '') {
13985: $packagetab{$pack.'&'.$name.'&name'}=$name;
13986: $packagetab{$short}=$plain;
13987: }
13988: }
13989: close($config);
13990: }
13991:
13992: # ---------------------------------------------------------- Read loncaparev table
13993:
13994: &load_loncaparevs();
13995:
13996: # ---------------------------------------------------------- Read serverhostID table
13997:
13998: &load_serverhomeIDs();
13999:
14000: # ---------------------------------------------------------- Read releaseslist XML
14001: {
14002: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
14003: if (-e $file) {
14004: my $parser = HTML::LCParser->new($file);
14005: while (my $token = $parser->get_token()) {
14006: if ($token->[0] eq 'S') {
14007: my $item = $token->[1];
14008: my $name = $token->[2]{'name'};
14009: my $value = $token->[2]{'value'};
14010: my $valuematch = $token->[2]{'valuematch'};
14011: my $namematch = $token->[2]{'namematch'};
14012: if ($item eq 'parameter') {
14013: if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
14014: my $release = $parser->get_text();
14015: $release =~ s/(^\s*|\s*$ )//gx;
14016: $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
14017: }
14018: } elsif ($item ne '' && $name ne '') {
14019: my $release = $parser->get_text();
14020: $release =~ s/(^\s*|\s*$ )//gx;
14021: $needsrelease{$item.':'.$name.':'.$value} = $release;
14022: }
14023: }
14024: }
14025: }
14026: }
14027:
14028: # ---------------------------------------------------------- Read managers table
14029: {
14030: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
14031: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
14032: while (my $configline=<$config>) {
14033: chomp($configline);
14034: next if ($configline =~ /^\#/);
14035: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
14036: $managerstab{$configline} = 1;
14037: }
14038: }
14039: close($config);
14040: }
14041: }
14042: }
14043:
14044: # ------------- set up temporary directory
14045: {
14046: $tmpdir = LONCAPA::tempdir();
14047:
14048: }
14049:
14050: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
14051: 'compress_threshold'=> 20_000,
14052: });
14053:
14054: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
14055: $dumpcount=0;
14056: $locknum=0;
14057:
14058: &logtouch();
14059: &logthis('<font color="yellow">INFO: Read configuration</font>');
14060: $readit=1;
14061: {
14062: use integer;
14063: my $test=(2**32)+1;
14064: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
14065: &logthis(" Detected 64bit platform ($_64bit)");
14066: }
14067: }
14068: }
14069:
14070: 1;
14071: __END__
14072:
14073: =pod
14074:
14075: =head1 NAME
14076:
14077: Apache::lonnet - Subroutines to ask questions about things in the network.
14078:
14079: =head1 SYNOPSIS
14080:
14081: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
14082:
14083: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
14084:
14085: Common parameters:
14086:
14087: =over 4
14088:
14089: =item *
14090:
14091: $uname : an internal username (if $cname expecting a course Id specifically)
14092:
14093: =item *
14094:
14095: $udom : a domain (if $cdom expecting a course's domain specifically)
14096:
14097: =item *
14098:
14099: $symb : a resource instance identifier
14100:
14101: =item *
14102:
14103: $namespace : the name of a .db file that contains the data needed or
14104: being set.
14105:
14106: =back
14107:
14108: =head1 OVERVIEW
14109:
14110: lonnet provides subroutines which interact with the
14111: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
14112: about classes, users, and resources.
14113:
14114: For many of these objects you can also use this to store data about
14115: them or modify them in various ways.
14116:
14117: =head2 Symbs
14118:
14119: To identify a specific instance of a resource, LON-CAPA uses symbols
14120: or "symbs"X<symb>. These identifiers are built from the URL of the
14121: map, the resource number of the resource in the map, and the URL of
14122: the resource itself. The latter is somewhat redundant, but might help
14123: if maps change.
14124:
14125: An example is
14126:
14127: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
14128:
14129: The respective map entry is
14130:
14131: <resource id="19" src="/res/msu/korte/tests/part12.problem"
14132: title="Problem 2">
14133: </resource>
14134:
14135: Symbs are used by the random number generator, as well as to store and
14136: restore data specific to a certain instance of for example a problem.
14137:
14138: =head2 Storing And Retrieving Data
14139:
14140: X<store()>X<cstore()>X<restore()>Three of the most important functions
14141: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
14142: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
14143: is is the non-critical message twin of cstore. These functions are for
14144: handlers to store a perl hash to a user's permanent data space in an
14145: easy manner, and to retrieve it again on another call. It is expected
14146: that a handler would use this once at the beginning to retrieve data,
14147: and then again once at the end to send only the new data back.
14148:
14149: The data is stored in the user's data directory on the user's
14150: homeserver under the ID of the course.
14151:
14152: The hash that is returned by restore will have all of the previous
14153: value for all of the elements of the hash.
14154:
14155: Example:
14156:
14157: #creating a hash
14158: my %hash;
14159: $hash{'foo'}='bar';
14160:
14161: #storing it
14162: &Apache::lonnet::cstore(\%hash);
14163:
14164: #changing a value
14165: $hash{'foo'}='notbar';
14166:
14167: #adding a new value
14168: $hash{'bar'}='foo';
14169: &Apache::lonnet::cstore(\%hash);
14170:
14171: #retrieving the hash
14172: my %history=&Apache::lonnet::restore();
14173:
14174: #print the hash
14175: foreach my $key (sort(keys(%history))) {
14176: print("\%history{$key} = $history{$key}");
14177: }
14178:
14179: Will print out:
14180:
14181: %history{1:foo} = bar
14182: %history{1:keys} = foo:timestamp
14183: %history{1:timestamp} = 990455579
14184: %history{2:bar} = foo
14185: %history{2:foo} = notbar
14186: %history{2:keys} = foo:bar:timestamp
14187: %history{2:timestamp} = 990455580
14188: %history{bar} = foo
14189: %history{foo} = notbar
14190: %history{timestamp} = 990455580
14191: %history{version} = 2
14192:
14193: Note that the special hash entries C<keys>, C<version> and
14194: C<timestamp> were added to the hash. C<version> will be equal to the
14195: total number of versions of the data that have been stored. The
14196: C<timestamp> attribute will be the UNIX time the hash was
14197: stored. C<keys> is available in every historical section to list which
14198: keys were added or changed at a specific historical revision of a
14199: hash.
14200:
14201: B<Warning>: do not store the hash that restore returns directly. This
14202: will cause a mess since it will restore the historical keys as if the
14203: were new keys. I.E. 1:foo will become 1:1:foo etc.
14204:
14205: Calling convention:
14206:
14207: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
14208: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
14209:
14210: For more detailed information, see lonnet specific documentation.
14211:
14212: =head1 RETURN MESSAGES
14213:
14214: =over 4
14215:
14216: =item * B<con_lost>: unable to contact remote host
14217:
14218: =item * B<con_delayed>: unable to contact remote host, message will be delivered
14219: when the connection is brought back up
14220:
14221: =item * B<con_failed>: unable to contact remote host and unable to save message
14222: for later delivery
14223:
14224: =item * B<error:>: an error a occurred, a description of the error follows the :
14225:
14226: =item * B<no_such_host>: unable to fund a host associated with the user/domain
14227: that was requested
14228:
14229: =back
14230:
14231: =head1 PUBLIC SUBROUTINES
14232:
14233: =head2 Session Environment Functions
14234:
14235: =over 4
14236:
14237: =item *
14238: X<appenv()>
14239: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
14240: the user envirnoment file, and will be restored for each access this
14241: user makes during this session, also modifies the %env for the current
14242: process. Optional rolesarrayref - if defined contains a reference to an array
14243: of roles which are exempt from the restriction on modifying user.role entries
14244: in the user's environment.db and in %env.
14245:
14246: =item *
14247: X<delenv()>
14248: B<delenv($delthis,$regexp)>: removes all items from the session
14249: environment file that begin with $delthis. If the
14250: optional second arg - $regexp - is true, $delthis is treated as a
14251: regular expression, otherwise \Q$delthis\E is used.
14252: The values are also deleted from the current processes %env.
14253:
14254: =item * get_env_multiple($name)
14255:
14256: gets $name from the %env hash, it seemlessly handles the cases where multiple
14257: values may be defined and end up as an array ref.
14258:
14259: returns an array of values
14260:
14261: =back
14262:
14263: =head2 User Information
14264:
14265: =over 4
14266:
14267: =item *
14268: X<queryauthenticate()>
14269: B<queryauthenticate($uname,$udom)>: try to determine user's current
14270: authentication scheme
14271:
14272: =item *
14273: X<authenticate()>
14274: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
14275: authenticate user from domain's lib servers (first use the current
14276: one). C<$upass> should be the users password.
14277: $checkdefauth is optional (value is 1 if a check should be made to
14278: authenticate user using default authentication method, and allow
14279: account creation if username does not have account in the domain).
14280: $clientcancheckhost is optional (value is 1 if checking whether the
14281: server can host will occur on the client side in lonauth.pm).
14282:
14283: =item *
14284: X<homeserver()>
14285: B<homeserver($uname,$udom)>: find the server which has
14286: the user's directory and files (there must be only one), this caches
14287: the answer, and also caches if there is a borken connection.
14288:
14289: =item *
14290: X<idget()>
14291: B<idget($udom,$idsref,$namespace)>: find the usernames behind either
14292: a list of student/employee IDs or clicker IDs
14293: (student/employee IDs are a unique resource in a domain, there must be
14294: only 1 ID per username, and only 1 username per ID in a specific domain).
14295: clickerIDs are not necessarily unique, as students might share clickers.
14296: (returns hash: id=>name,id=>name)
14297:
14298: =item *
14299: X<idrget()>
14300: B<idrget($udom,@unames)>: find the IDs behind a list of
14301: usernames (returns hash: name=>id,name=>id)
14302:
14303: =item *
14304: X<idput()>
14305: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of
14306: names and associated student/employee IDs or clicker IDs.
14307:
14308: =item *
14309: X<iddel()>
14310: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted
14311: student/employee ID or clicker ID username look-ups from domain.
14312: The homeserver ($uhome) and namespace ($namespace) are optional.
14313: If no $uhome is provided, it will be determined usig &homeserver()
14314: for each user. If no $namespace is provided, the default is ids.
14315:
14316: =item *
14317: X<updateclickers()>
14318: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update
14319: clicker ID-to-username look-ups in clickers.db on library server.
14320: Permitted actions are add or del (i.e., add or delete). The
14321: clickers.db contains clickerID as keys (escaped), and each corresponding
14322: value is an escaped comma-separated list of usernames (for whom the
14323: library server is the homeserver), who registered that particular ID.
14324: If $critical is true, the update will be sent via &critical, otherwise
14325: &reply() will be used.
14326:
14327: =item *
14328: X<rolesinit()>
14329: B<rolesinit($udom,$username)>: get user privileges.
14330: returns user role, first access and timer interval hashes
14331:
14332: =item *
14333: X<privileged()>
14334: B<privileged($username,$domain)>: returns a true if user has a
14335: privileged and active role (i.e. su or dc), false otherwise.
14336:
14337: =item *
14338: X<getsection()>
14339: B<getsection($udom,$uname,$cname)>: finds the section of student in the
14340: course $cname, return section name/number or '' for "not in course"
14341: and '-1' for "no section"
14342:
14343: =item *
14344: X<userenvironment()>
14345: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
14346: passed in @what from the requested user's environment, returns a hash
14347:
14348: =item *
14349: X<userlog_query()>
14350: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
14351: activity.log file. %filters defines filters applied when parsing the
14352: log file. These can be start or end timestamps, or the type of action
14353: - log to look for Login or Logout events, check for Checkin or
14354: Checkout, role for role selection. The response is in the form
14355: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
14356: escaped strings of the action recorded in the activity.log file.
14357:
14358: =back
14359:
14360: =head2 User Roles
14361:
14362: =over 4
14363:
14364: =item *
14365:
14366: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
14367: returns codes for allowed actions.
14368:
14369: The first argument is required, all others are optional.
14370:
14371: $priv is the privilege being checked.
14372: $uri contains additional information about what is being checked for access (e.g.,
14373: URL, course ID etc.).
14374: $symb is the unique resource instance identifier in a course; if needed,
14375: but not provided, it will be retrieved via a call to &symbread().
14376: $role is the role for which a priv is being checked (only used if priv is evb).
14377: $clientip is the user's IP address (only used when checking for access to portfolio
14378: files).
14379: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
14380: prevents recursive calls to &allowed.
14381:
14382: F: full access
14383: U,I,K: authentication modes (cxx only)
14384: '': forbidden
14385: 1: user needs to choose course
14386: 2: browse allowed
14387: A: passphrase authentication needed
14388: B: access temporarily blocked because of a blocking event in a course.
14389:
14390: =item *
14391:
14392: constructaccess($url,$setpriv) : check for access to construction space URL
14393:
14394: See if the owner domain and name in the URL match those in the
14395: expected environment. If so, return three element list
14396: ($ownername,$ownerdomain,$ownerhome).
14397:
14398: Otherwise return the null string.
14399:
14400: If second argument 'setpriv' is true, it assigns the privileges,
14401: and returns the same three element list, unless the owner has
14402: blocked "ad hoc" Domain Coordinator access to the Author Space,
14403: in which case the null string is returned.
14404:
14405: =item *
14406:
14407: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
14408: define a custom role rolename set privileges in format of lonTabs/roles.tab
14409: for system, domain, and course level. $uname and $udom are optional (current
14410: user's username and domain will be used when either of $uname or $udom are absent.
14411:
14412: =item *
14413:
14414: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
14415: (rolesplain.tab); plain text explanation of a user role term.
14416: $type is Course (default) or Community.
14417: If $forcedefault evaluates to true, text returned will be default
14418: text for $type. Otherwise, if this is a course, the text returned
14419: will be a custom name for the role (if defined in the course's
14420: environment). If no custom name is defined the default is returned.
14421:
14422: =item *
14423:
14424: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
14425: All arguments are optional. Returns a hash of a roles, either for
14426: co-author/assistant author roles for a user's Construction Space
14427: (default), or if $context is 'userroles', roles for the user himself,
14428: In the hash, keys are set to colon-separated $uname,$udom,$role, and
14429: (optionally) if $withsec is true, a fourth colon-separated item - $section.
14430: For each key, value is set to colon-separated start and end times for
14431: the role. If no username and domain are specified, will default to
14432: current user/domain. Types, roles, and roledoms are references to arrays
14433: of role statuses (active, future or previous), roles
14434: (e.g., cc,in, st etc.) and domains of the roles which can be used
14435: to restrict the list of roles reported. If no array ref is
14436: provided for types, will default to return only active roles.
14437:
14438: =item *
14439:
14440: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
14441: user: $uname:$udom has a role in the course: $cdom_$cnum.
14442:
14443: Additional optional arguments are: $type (if role checking is to be restricted
14444: to certain user status types -- previous (expired roles), active (currently
14445: available roles) or future (roles available in the future), and
14446: $hideprivileged -- if true will not report course roles for users who
14447: have active Domain Coordinator role in course's domain or in additional
14448: domains (specified in 'Domains to check for privileged users' in course
14449: environment -- set via: Course Settings -> Classlists and staff listing).
14450:
14451: =item *
14452:
14453: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
14454: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
14455: $possdomains and $possroles are optional array refs -- to domains to check and
14456: roles to check. If $possdomains is not specified, a dump will be done of the
14457: users' roles.db to check for a dc or su role in any domain. This can be
14458: time consuming if &privileged is called repeatedly (e.g., when displaying a
14459: classlist), so in such cases, supplying a $possdomains array is preferred, as
14460: this then allows &privileged_by_domain() to be used, which caches the identity
14461: of privileged users, eliminating the need for repeated calls to &dump().
14462:
14463: =item *
14464:
14465: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
14466: where the outer hash keys are domains specified in the $possdomains array ref,
14467: next inner hash keys are privileged roles specified in the $roles array ref,
14468: and the innermost hash contains key = value pairs for username:domain = end:start
14469: for active or future "privileged" users with that role in that domain. To avoid
14470: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
14471: innerhash are cached using priv_$role and $dom as the identifiers.
14472:
14473: =back
14474:
14475: =head2 User Modification
14476:
14477: =over 4
14478:
14479: =item *
14480:
14481: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
14482: user for the level given by URL. Optional start and end dates (leave empty
14483: string or zero for "no date")
14484:
14485: =item *
14486:
14487: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
14488: change a users, password, possible return values are: ok,
14489: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
14490: refused
14491:
14492: =item *
14493:
14494: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
14495:
14496: =item *
14497:
14498: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
14499: $forceid,$desiredhome,$email,$inststatus,$candelete) :
14500:
14501: will update user information (firstname,middlename,lastname,generation,
14502: permanentemail), and if forceid is true, student/employee ID also.
14503: A user's institutional affiliation(s) can also be updated.
14504: User information fields will not be overwritten with empty entries
14505: unless the field is included in the $candelete array reference.
14506: This array is included when a single user is modified via "Manage Users",
14507: or when Autoupdate.pl is run by cron in a domain.
14508:
14509: =item *
14510:
14511: modifystudent
14512:
14513: modify a student's enrollment and identification information.
14514: The course id is resolved based on the current user's environment.
14515: This means the invoking user must be a course coordinator or otherwise
14516: associated with a course.
14517:
14518: This call is essentially a wrapper for lonnet::modifyuser and
14519: lonnet::modify_student_enrollment
14520:
14521: Inputs:
14522:
14523: =over 4
14524:
14525: =item B<$udom> Student's loncapa domain
14526:
14527: =item B<$uname> Student's loncapa login name
14528:
14529: =item B<$uid> Student/Employee ID
14530:
14531: =item B<$umode> Student's authentication mode
14532:
14533: =item B<$upass> Student's password
14534:
14535: =item B<$first> Student's first name
14536:
14537: =item B<$middle> Student's middle name
14538:
14539: =item B<$last> Student's last name
14540:
14541: =item B<$gene> Student's generation
14542:
14543: =item B<$usec> Student's section in course
14544:
14545: =item B<$end> Unix time of the roles expiration
14546:
14547: =item B<$start> Unix time of the roles start date
14548:
14549: =item B<$forceid> If defined, allow $uid to be changed
14550:
14551: =item B<$desiredhome> server to use as home server for student
14552:
14553: =item B<$email> Student's permanent e-mail address
14554:
14555: =item B<$type> Type of enrollment (auto or manual)
14556:
14557: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
14558:
14559: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
14560:
14561: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
14562:
14563: =item B<$context> role change context (shown in User Management Logs display in a course)
14564:
14565: =item B<$inststatus> institutional status of user - : separated string of escaped status types
14566:
14567: =item B<$credits> Number of credits student will earn from this class - only needs to be supplied if value needs to be different from default credits for class.
14568:
14569: =back
14570:
14571: =item *
14572:
14573: modify_student_enrollment
14574:
14575: Change a student's enrollment status in a class. The environment variable
14576: 'role.request.course' must be defined for this function to proceed.
14577:
14578: Inputs:
14579:
14580: =over 4
14581:
14582: =item $udom, student's domain
14583:
14584: =item $uname, student's name
14585:
14586: =item $uid, student's user id
14587:
14588: =item $first, student's first name
14589:
14590: =item $middle
14591:
14592: =item $last
14593:
14594: =item $gene
14595:
14596: =item $usec
14597:
14598: =item $end
14599:
14600: =item $start
14601:
14602: =item $type
14603:
14604: =item $locktype
14605:
14606: =item $cid
14607:
14608: =item $selfenroll
14609:
14610: =item $context
14611:
14612: =item $credits, number of credits student will earn from this class
14613:
14614: =item $instsec, institutional course section code for student
14615:
14616: =back
14617:
14618:
14619: =item *
14620:
14621: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
14622: custom role; give a custom role to a user for the level given by URL. Specify
14623: name and domain of role author, and role name
14624:
14625: =item *
14626:
14627: revokerole($udom,$uname,$url,$role) : revoke a role for url
14628:
14629: =item *
14630:
14631: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
14632:
14633: =back
14634:
14635: =head2 Course Infomation
14636:
14637: =over 4
14638:
14639: =item *
14640:
14641: coursedescription($courseid,$options) : returns a hash of information about the
14642: specified course id, including all environment settings for the
14643: course, the description of the course will be in the hash under the
14644: key 'description'
14645:
14646: $options is an optional parameter that if supplied is a hash reference that controls
14647: what how this function works. It has the following key/values:
14648:
14649: =over 4
14650:
14651: =item freshen_cache
14652:
14653: If defined, and the environment cache for the course is valid, it is
14654: returned in the returned hash.
14655:
14656: =item one_time
14657:
14658: If defined, the last cache time is set to _now_
14659:
14660: =item user
14661:
14662: If defined, the supplied username is used instead of the current user.
14663:
14664:
14665: =back
14666:
14667: =item *
14668:
14669: resdata($name,$domain,$type,@which) : request for current parameter
14670: setting for a specific $type, where $type is either 'course' or 'user',
14671: @what should be a list of parameters to ask about. This routine caches
14672: answers for 10 minutes.
14673:
14674: =item *
14675:
14676: get_courseresdata($courseid, $domain) : dump the entire course resource
14677: data base, returning a hash that is keyed by the resource name and has
14678: values that are the resource value. I believe that the timestamps and
14679: versions are also returned.
14680:
14681: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
14682: supplemental content area. This routine caches the number of files for
14683: 10 minutes.
14684:
14685: =back
14686:
14687: =head2 Course Modification
14688:
14689: =over 4
14690:
14691: =item *
14692:
14693: writecoursepref($courseid,%prefs) : write preferences (environment
14694: database) for a course
14695:
14696: =item *
14697:
14698: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
14699:
14700: =item *
14701:
14702: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
14703:
14704: =item *
14705:
14706: is_course($courseid), is_course($cdom, $cnum)
14707:
14708: Accepts either a combined $courseid (in the form of domain_courseid) or the
14709: two component version $cdom, $cnum. It checks if the specified course exists.
14710:
14711: Returns:
14712: undef if the course doesn't exist, otherwise
14713: in scalar context the combined courseid.
14714: in list context the two components of the course identifier, domain and
14715: courseid.
14716:
14717: =back
14718:
14719: =head2 Resource Subroutines
14720:
14721: =over 4
14722:
14723: =item *
14724:
14725: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
14726:
14727: =item *
14728:
14729: repcopy($filename) : subscribes to the requested file, and attempts to
14730: replicate from the owning library server, Might return
14731: 'unavailable', 'not_found', 'forbidden', 'ok', or
14732: 'bad_request', also attempts to grab the metadata for the
14733: resource. Expects the local filesystem pathname
14734: (/home/httpd/html/res/....)
14735:
14736: =back
14737:
14738: =head2 Resource Information
14739:
14740: =over 4
14741:
14742: =item *
14743:
14744: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
14745: and returns the value of a variety of different possible values,
14746: $varname should be a request string, and the other parameters can be
14747: used to specify who and what one is asking about. Ordinarily, $cid
14748: does not need to be specified, as it is retrived from
14749: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
14750: within lonuserstate::loadmap() when initializing a course, before
14751: $env{'request.course.id'} has been set, so it needs to be provided
14752: in that one case.
14753:
14754: Possible values for $varname are environment.lastname (or other item
14755: from the envirnment hash), user.name (or someother aspect about the
14756: user), resource.0.maxtries (or some other part and parameter of a
14757: resource)
14758:
14759: =item *
14760:
14761: directcondval($number) : get current value of a condition; reads from a state
14762: string
14763:
14764: =item *
14765:
14766: condval($condidx) : value of condition index based on state
14767:
14768: =item *
14769:
14770: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
14771: resource's metadata, $what should be either a specific key, or either
14772: 'keys' (to get a list of possible keys) or 'packages' to get a list of
14773: packages that this resource currently uses, the last 3 arguments are
14774: only used internally for recursive metadata.
14775:
14776: the toolsymb is only used where the uri is for an external tool (for which
14777: the uri as well as the symb are guaranteed to be unique).
14778:
14779: this function automatically caches all requests
14780:
14781: =item *
14782:
14783: metadata_query($query,$custom,$customshow) : make a metadata query against the
14784: network of library servers; returns file handle of where SQL and regex results
14785: will be stored for query
14786:
14787: =item *
14788:
14789: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
14790: return symbolic list entry (all arguments optional).
14791:
14792: Args: filename is the filename (including path) for the file for which a symb
14793: is required; donotrecurse, if true will prevent calls to allowed() being made
14794: to check access status if more than one resource was found in the bighash
14795: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
14796: a randompick); ignorecachednull, if true will prevent a symb of '' being
14797: returned if $env{$cache_str} is defined as ''; checkforblock if true will
14798: cause possible symbs to be checked to determine if they are subject to content
14799: blocking, if so they will not be included as possible symbs; possibles is a
14800: ref to a hash, which, as a side effect, will be populated with all possible
14801: symbs (content blocking not tested).
14802:
14803: returns the data handle
14804:
14805: =item *
14806:
14807: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
14808: and is a possible symb for the URL in $thisfn, and if is an encrypted
14809: resource that the user accessed using /enc/ returns a 1 on success, 0
14810: on failure, user must be in a course, as it assumes the existence of
14811: the course initial hash, and uses $env('request.course.id'}. The third
14812: arg is an optional reference to a scalar. If this arg is passed in the
14813: call to symbverify, it will be set to 1 if the symb has been set to be
14814: encrypted; otherwise it will be null.
14815:
14816: =item *
14817:
14818: symbclean($symb) : removes versions numbers from a symb, returns the
14819: cleaned symb
14820:
14821: =item *
14822:
14823: is_on_map($uri) : checks if the $uri is somewhere on the current
14824: course map, user must be in a course for it to work.
14825:
14826: =item *
14827:
14828: numval($salt) : return random seed value (addend for rndseed)
14829:
14830: =item *
14831:
14832: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
14833: a random seed, all arguments are optional, if they aren't sent it uses the
14834: environment to derive them. Note: if symb isn't sent and it can't get one
14835: from &symbread it will use the current time as its return value
14836:
14837: =item *
14838:
14839: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
14840: unfakeable, receipt
14841:
14842: =item *
14843:
14844: receipt() : API to ireceipt working off of env values; given out to users
14845:
14846: =item *
14847:
14848: countacc($url) : count the number of accesses to a given URL
14849:
14850: =item *
14851:
14852: 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
14853:
14854: =item *
14855:
14856: 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)
14857:
14858: =item *
14859:
14860: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
14861:
14862: =item *
14863:
14864: devalidate($symb) : devalidate temporary spreadsheet calculations,
14865: forcing spreadsheet to reevaluate the resource scores next time.
14866:
14867: =item *
14868:
14869: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
14870: when viewing in course context.
14871:
14872: input: six args -- filename (decluttered), course number, course domain,
14873: url, symb (if registered) and group (if this is a
14874: group item -- e.g., bulletin board, group page etc.).
14875:
14876: output: array of five scalars --
14877: $cfile -- url for file editing if editable on current server
14878: $home -- homeserver of resource (i.e., for author if published,
14879: or course if uploaded.).
14880: $switchserver -- 1 if server switch will be needed.
14881: $forceedit -- 1 if icon/link should be to go to edit mode
14882: $forceview -- 1 if icon/link should be to go to view mode
14883:
14884: =item *
14885:
14886: is_course_upload($file,$cnum,$cdom)
14887:
14888: Used in course context to determine if current file was uploaded to
14889: the course (i.e., would be found in /userfiles/docs on the course's
14890: homeserver.
14891:
14892: input: 3 args -- filename (decluttered), course number and course domain.
14893: output: boolean -- 1 if file was uploaded.
14894:
14895: =back
14896:
14897: =head2 Storing/Retreiving Data
14898:
14899: =over 4
14900:
14901: =item *
14902:
14903: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
14904: permanently for this url; hashref needs to be given and should be a \%hashname;
14905: the remaining args aren't required and if they aren't passed or are '' they will
14906: be derived from the env (with the exception of $laststore, which is an
14907: optional arg used when a user's submission is stored in grading).
14908: $laststore is $version=$timestamp, where $version is the most recent version
14909: number retrieved for the corresponding $symb in the $namespace db file, and
14910: $timestamp is the timestamp for that transaction (UNIX time).
14911: $laststore is currently only passed when cstore() is called by
14912: structuretags::finalize_storage().
14913:
14914: =item *
14915:
14916: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
14917: but uses critical subroutine
14918:
14919: =item *
14920:
14921: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
14922: all args are optional
14923:
14924: =item *
14925:
14926: dumpstore($namespace,$udom,$uname,$regexp,$range) :
14927: dumps the complete (or key matching regexp) namespace into a hash
14928: ($udom, $uname, $regexp, $range are optional) for a namespace that is
14929: normally &store()ed into
14930:
14931: $range should be either an integer '100' (give me the first 100
14932: matching records)
14933: or be two integers sperated by a - with no spaces
14934: '30-50' (give me the 30th through the 50th matching
14935: records)
14936:
14937:
14938: =item *
14939:
14940: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
14941: replaces a &store() version of data with a replacement set of data
14942: for a particular resource in a namespace passed in the $storehash hash
14943: reference. If $tolog is true, the transaction is logged in the courselog
14944: with an action=PUTSTORE.
14945:
14946: =item *
14947:
14948: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
14949: works very similar to store/cstore, but all data is stored in a
14950: temporary location and can be reset using tmpreset, $storehash should
14951: be a hash reference, returns nothing on success
14952:
14953: =item *
14954:
14955: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
14956: similar to restore, but all data is stored in a temporary location and
14957: can be reset using tmpreset. Returns a hash of values on success,
14958: error string otherwise.
14959:
14960: =item *
14961:
14962: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
14963: deltes all keys for $symb form the temporary storage hash.
14964:
14965: =item *
14966:
14967: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14968: reference filled in from namesp ($udom and $uname are optional)
14969:
14970: =item *
14971:
14972: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
14973: namesp ($udom and $uname are optional)
14974:
14975: =item *
14976:
14977: dump($namespace,$udom,$uname,$regexp,$range) :
14978: dumps the complete (or key matching regexp) namespace into a hash
14979: ($udom, $uname, $regexp, $range are optional)
14980:
14981: $range should be either an integer '100' (give me the first 100
14982: matching records)
14983: or be two integers sperated by a - with no spaces
14984: '30-50' (give me the 30th through the 50th matching
14985: records)
14986: =item *
14987:
14988: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
14989: $store can be a scalar, an array reference, or if the amount to be
14990: incremented is > 1, a hash reference.
14991:
14992: ($udom and $uname are optional)
14993:
14994: =item *
14995:
14996: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
14997: ($udom and $uname are optional)
14998:
14999: =item *
15000:
15001: cput($namespace,$storehash,$udom,$uname) : critical put
15002: ($udom and $uname are optional)
15003:
15004: =item *
15005:
15006: newput($namespace,$storehash,$udom,$uname) :
15007:
15008: Attempts to store the items in the $storehash, but only if they don't
15009: currently exist, if this succeeds you can be certain that you have
15010: successfully created a new key value pair in the $namespace db.
15011:
15012:
15013: Args:
15014: $namespace: name of database to store values to
15015: $storehash: hashref to store to the db
15016: $udom: (optional) domain of user containing the db
15017: $uname: (optional) name of user caontaining the db
15018:
15019: Returns:
15020: 'ok' -> succeeded in storing all keys of $storehash
15021: 'key_exists: <key>' -> failed to anything out of $storehash, as at
15022: least <key> already existed in the db (other
15023: requested keys may also already exist)
15024: 'error: <msg>' -> unable to tie the DB or other error occurred
15025: 'con_lost' -> unable to contact request server
15026: 'refused' -> action was not allowed by remote machine
15027:
15028:
15029: =item *
15030:
15031: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
15032: reference filled in from namesp (encrypts the return communication)
15033: ($udom and $uname are optional)
15034:
15035: =item *
15036:
15037: log($udom,$name,$home,$message) : write to permanent log for user; use
15038: critical subroutine
15039:
15040: =item *
15041:
15042: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
15043: array reference filled in from namespace found in domain level on either
15044: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
15045:
15046: =item *
15047:
15048: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
15049: domain level either on specified domain server ($uhome) or primary domain
15050: server ($udom and $uhome are optional)
15051:
15052: =item *
15053:
15054: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
15055: for: authentication, language, quotas, timezone, date locale, and portal URL in
15056: the target domain.
15057:
15058: May also include additional key => value pairs for the following groups:
15059:
15060: =over
15061:
15062: =item
15063: disk quotas (MB allocated by default to portfolios and authoring spaces).
15064:
15065: =over
15066:
15067: =item defaultquota, authorquota
15068:
15069: =back
15070:
15071: =item
15072: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
15073: portfolio for users).
15074:
15075: =over
15076:
15077: =item
15078: aboutme, blog, webdav, portfolio
15079:
15080: =back
15081:
15082: =item
15083: requestcourses: ability to request courses, and how requests are processed.
15084:
15085: =over
15086:
15087: =item
15088: official, unofficial, community, textbook, placement
15089:
15090: =back
15091:
15092: =item
15093: inststatus: types of institutional affiliation, and order in which they are displayed.
15094:
15095: =over
15096:
15097: =item
15098: inststatustypes, inststatusorder, inststatusguest
15099:
15100: =back
15101:
15102: =item
15103: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
15104: for course's uploaded content.
15105:
15106: =over
15107:
15108: =item
15109: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
15110: communityquota, textbookquota, placementquota
15111:
15112: =back
15113:
15114: =item
15115: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
15116: on your servers.
15117:
15118: =over
15119:
15120: =item
15121: remotesessions, hostedsessions
15122:
15123: =back
15124:
15125: =back
15126:
15127: In cases where a domain coordinator has never used the "Set Domain Configuration"
15128: utility to create a configuration.db file on a domain's primary library server
15129: only the following domain defaults: auth_def, auth_arg_def, lang_def
15130: -- corresponding values are authentication type (internal, krb4, krb5,
15131: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
15132: will be available. Values are retrieved from cache (if current), unless the
15133: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
15134: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
15135:
15136: Typical usage:
15137:
15138: %domdefaults = &get_domain_defaults($target_domain);
15139:
15140: =back
15141:
15142: =head2 Network Status Functions
15143:
15144: =over 4
15145:
15146: =item *
15147:
15148: dirlist() : return directory list based on URI (first arg).
15149:
15150: Inputs: 1 required, 5 optional.
15151:
15152: =over
15153:
15154: =item
15155: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
15156:
15157: =item
15158: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
15159:
15160: =item
15161: $username - username of user/course to be listed. Extracted from $uri if absent.
15162:
15163: =item
15164: $getpropath - boolean: 1 if prepend path using &propath().
15165:
15166: =item
15167: $getuserdir - boolean: 1 if prepend path for "userfiles".
15168:
15169: =item
15170: $alternateRoot - path to prepend in place of path from $uri.
15171:
15172: =back
15173:
15174: Returns: Array of up to two items.
15175:
15176: =over
15177:
15178: a reference to an array of files/subdirectories
15179:
15180: =over
15181:
15182: Each element in the array of files/subdirectories is a & separated list of
15183: item name and the result of running stat on the item. If dirlist was requested
15184: for a file instead of a directory, the item name will be ''. For a directory
15185: listing, if the item is a metadata file, the element will end &N&M
15186: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
15187: default copyright set (1).
15188:
15189: =back
15190:
15191: a scalar containing error condition (if encountered).
15192:
15193: =over
15194:
15195: =item
15196: no_host (no homeserver identified for $username:$domain).
15197:
15198: =item
15199: no_such_host (server contacted for listing not identified as valid host).
15200:
15201: =item
15202: con_lost (connection to remote server failed).
15203:
15204: =item
15205: refused (invalid $username:$domain received on lond side).
15206:
15207: =item
15208: no_such_dir (directory at specified path on lond side does not exist).
15209:
15210: =item
15211: empty (directory at specified path on lond side is empty).
15212:
15213: =over
15214:
15215: This is currently not encountered because the &ls3, &ls2,
15216: &ls (_handler) routines on the lond side do not filter out
15217: . and .. from a directory listing.
15218:
15219: =back
15220:
15221: =back
15222:
15223: =back
15224:
15225: =item *
15226:
15227: spareserver() : find server with least workload from spare.tab
15228:
15229:
15230: =item *
15231:
15232: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
15233: if there is no corresponding loncapa host.
15234:
15235: =back
15236:
15237:
15238: =head2 Apache Request
15239:
15240: =over 4
15241:
15242: =item *
15243:
15244: ssi($url,%hash) : server side include, does a complete request cycle on url to
15245: localhost, posts hash
15246:
15247: =back
15248:
15249: =head2 Data to String to Data
15250:
15251: =over 4
15252:
15253: =item *
15254:
15255: hash2str(%hash) : convert a hash into a string complete with escaping and '='
15256: and '&' separators, supports elements that are arrayrefs and hashrefs
15257:
15258: =item *
15259:
15260: hashref2str($hashref) : convert a hashref into a string complete with
15261: escaping and '=' and '&' separators, supports elements that are
15262: arrayrefs and hashrefs
15263:
15264: =item *
15265:
15266: arrayref2str($arrayref) : convert an arrayref into a string complete
15267: with escaping and '&' separators, supports elements that are arrayrefs
15268: and hashrefs
15269:
15270: =item *
15271:
15272: str2hash($string) : convert string to hash using unescaping and
15273: splitting on '=' and '&', supports elements that are arrayrefs and
15274: hashrefs
15275:
15276: =item *
15277:
15278: str2array($string) : convert string to hash using unescaping and
15279: splitting on '&', supports elements that are arrayrefs and hashrefs
15280:
15281: =back
15282:
15283: =head2 Logging Routines
15284:
15285:
15286: These routines allow one to make log messages in the lonnet.log and
15287: lonnet.perm logfiles.
15288:
15289: =over 4
15290:
15291: =item *
15292:
15293: logtouch() : make sure the logfile, lonnet.log, exists
15294:
15295: =item *
15296:
15297: logthis() : append message to the normal lonnet.log file, it gets
15298: preiodically rolled over and deleted.
15299:
15300: =item *
15301:
15302: logperm() : append a permanent message to lonnet.perm.log, this log
15303: file never gets deleted by any automated portion of the system, only
15304: messages of critical importance should go in here.
15305:
15306:
15307: =back
15308:
15309: =head2 General File Helper Routines
15310:
15311: =over 4
15312:
15313: =item *
15314:
15315: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
15316: (a) files in /uploaded
15317: (i) If a local copy of the file exists -
15318: compares modification date of local copy with last-modified date for
15319: definitive version stored on home server for course. If local copy is
15320: stale, requests a new version from the home server and stores it.
15321: If the original has been removed from the home server, then local copy
15322: is unlinked.
15323: (ii) If local copy does not exist -
15324: requests the file from the home server and stores it.
15325:
15326: If $caller is 'uploadrep':
15327: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
15328: for request for files originally uploaded via DOCS.
15329: - returns 'ok' if fresh local copy now available, -1 otherwise.
15330:
15331: Otherwise:
15332: This indicates a call from the content generation phase of the request.
15333: - returns the entire contents of the file or -1.
15334:
15335: (b) files in /res
15336: - returns the entire contents of a file or -1;
15337: it properly subscribes to and replicates the file if neccessary.
15338:
15339:
15340: =item *
15341:
15342: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
15343: reference
15344:
15345: returns either a stat() list of data about the file or an empty list
15346: if the file doesn't exist or couldn't find out about it (connection
15347: problems or user unknown)
15348:
15349: =item *
15350:
15351: filelocation($dir,$file) : returns file system location of a file
15352: based on URI; meant to be "fairly clean" absolute reference, $dir is a
15353: directory that relative $file lookups are to looked in ($dir of /a/dir
15354: and a file of ../bob will become /a/bob)
15355:
15356: =item *
15357:
15358: hreflocation($dir,$file) : returns file system location or a URL; same as
15359: filelocation except for hrefs
15360:
15361: =item *
15362:
15363: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
15364: also removes beginning /home/httpd/html unless /priv/ follows it.
15365:
15366: =back
15367:
15368: =head2 Usererfile file routines (/uploaded*)
15369:
15370: =over 4
15371:
15372: =item *
15373:
15374: userfileupload(): main rotine for putting a file in a user or course's
15375: filespace, arguments are,
15376:
15377: formname - required - this is the name of the element in $env where the
15378: filename, and the contents of the file to create/modifed exist
15379: the filename is in $env{'form.'.$formname.'.filename'} and the
15380: contents of the file is located in $env{'form.'.$formname}
15381: context - if coursedoc, store the file in the course of the active role
15382: of the current user;
15383: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
15384: if 'canceloverwrite': delete file in tmp/overwrites directory
15385: subdir - required - subdirectory to put the file in under ../userfiles/
15386: if undefined, it will be placed in "unknown"
15387:
15388: (This routine calls clean_filename() to remove any dangerous
15389: characters from the filename, and then calls finuserfileupload() to
15390: complete the transaction)
15391:
15392: returns either the url of the uploaded file (/uploaded/....) if successful
15393: and /adm/notfound.html if unsuccessful
15394:
15395: =item *
15396:
15397: clean_filename(): routine for cleaing a filename up for storage in
15398: userfile space, argument is:
15399:
15400: filename - proposed filename
15401:
15402: returns: the new clean filename
15403:
15404: =item *
15405:
15406: finishuserfileupload(): routine that creates and sends the file to
15407: userspace, probably shouldn't be called directly
15408:
15409: docuname: username or courseid of destination for the file
15410: docudom: domain of user/course of destination for the file
15411: formname: same as for userfileupload()
15412: fname: filename (including subdirectories) for the file
15413: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
15414: allfiles: reference to hash used to store objects found by parser
15415: codebase: reference to hash used for codebases of java objects found by parser
15416: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
15417: thumbheight: height (pixels) of thumbnail to be created for uploaded image
15418: resizewidth: width to be used to resize image using resizeImage from ImageMagick
15419: resizeheight: height to be used to resize image using resizeImage from ImageMagick
15420: context: if 'overwrite', will move the uploaded file from its temporary location to
15421: userfiles to facilitate overwriting a previously uploaded file with same name.
15422: mimetype: reference to scalar to accommodate mime type determined
15423: from File::MMagic if $parser = parse.
15424:
15425: returns either the url of the uploaded file (/uploaded/....) if successful
15426: and /adm/notfound.html if unsuccessful (or an error message if context
15427: was 'overwrite').
15428:
15429:
15430: =item *
15431:
15432: renameuserfile(): renames an existing userfile to a new name
15433:
15434: Args:
15435: docuname: username or courseid of destination for the file
15436: docudom: domain of user/course of destination for the file
15437: old: current file name (including any subdirs under userfiles)
15438: new: desired file name (including any subdirs under userfiles)
15439:
15440: =item *
15441:
15442: mkdiruserfile(): creates a directory is a userfiles dir
15443:
15444: Args:
15445: docuname: username or courseid of destination for the file
15446: docudom: domain of user/course of destination for the file
15447: dir: dir to create (including any subdirs under userfiles)
15448:
15449: =item *
15450:
15451: removeuserfile(): removes a file that exists in userfiles
15452:
15453: Args:
15454: docuname: username or courseid of destination for the file
15455: docudom: domain of user/course of destination for the file
15456: fname: filname to delete (including any subdirs under userfiles)
15457:
15458: =item *
15459:
15460: removeuploadedurl(): convience function for removeuserfile()
15461:
15462: Args:
15463: url: a full /uploaded/... url to delete
15464:
15465: =item *
15466:
15467: get_portfile_permissions():
15468: Args:
15469: domain: domain of user or course contain the portfolio files
15470: user: name of user or num of course contain the portfolio files
15471: Returns:
15472: hashref of a dump of the proper file_permissions.db
15473:
15474:
15475: =item *
15476:
15477: get_access_controls():
15478:
15479: Args:
15480: current_permissions: the hash ref returned from get_portfile_permissions()
15481: group: (optional) the group you want the files associated with
15482: file: (optional) the file you want access info on
15483:
15484: Returns:
15485: a hash (keys are file names) of hashes containing
15486: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
15487: values are XML containing access control settings (see below)
15488:
15489: Internal notes:
15490:
15491: access controls are stored in file_permissions.db as key=value pairs.
15492: key -> path to file/file_name\0uniqueID:scope_end_start
15493: where scope -> public,guest,course,group,domains or users.
15494: end -> UNIX time for end of access (0 -> no end date)
15495: start -> UNIX time for start of access
15496:
15497: value -> XML description of access control
15498: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
15499: <start></start>
15500: <end></end>
15501:
15502: <password></password> for scope type = guest
15503:
15504: <domain></domain> for scope type = course or group
15505: <number></number>
15506: <roles id="">
15507: <role></role>
15508: <access></access>
15509: <section></section>
15510: <group></group>
15511: </roles>
15512:
15513: <dom></dom> for scope type = domains
15514:
15515: <users> for scope type = users
15516: <user>
15517: <uname></uname>
15518: <udom></udom>
15519: </user>
15520: </users>
15521: </scope>
15522:
15523: Access data is also aggregated for each file in an additional key=value pair:
15524: key -> path to file/file_name\0accesscontrol
15525: value -> reference to hash
15526: hash contains key = value pairs
15527: where key = uniqueID:scope_end_start
15528: value = UNIX time record was last updated
15529:
15530: Used to improve speed of look-ups of access controls for each file.
15531:
15532: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
15533:
15534: =item *
15535:
15536: modify_access_controls():
15537:
15538: Modifies access controls for a portfolio file
15539: Args
15540: 1. file name
15541: 2. reference to hash of required changes,
15542: 3. domain
15543: 4. username
15544: where domain,username are the domain of the portfolio owner
15545: (either a user or a course)
15546:
15547: Returns:
15548: 1. result of additions or updates ('ok' or 'error', with error message).
15549: 2. result of deletions ('ok' or 'error', with error message).
15550: 3. reference to hash of any new or updated access controls.
15551: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
15552: key = integer (inbound ID)
15553: value = uniqueID
15554:
15555: =item *
15556:
15557: get_timebased_id():
15558:
15559: Attempts to get a unique timestamp-based suffix for use with items added to a
15560: course via the Course Editor (e.g., folders, composite pages,
15561: group bulletin boards).
15562:
15563: Args: (first three required; six others optional)
15564:
15565: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
15566: docssequence, or name of group
15567:
15568: 2. keyid (alphanumeric): name of temporary locking key in hash,
15569: e.g., num, boardids
15570:
15571: 3. namespace: name of gdbm file used to store suffixes already assigned;
15572: file will be named nohist_namespace.db
15573:
15574: 4. cdom: domain of course; default is current course domain from %env
15575:
15576: 5. cnum: course number; default is current course number from %env
15577:
15578: 6. idtype: set to concat if an additional digit is to be appended to the
15579: unix timestamp to form the suffix, if the plain timestamp is already
15580: in use. Default is to not do this, but simply increment the unix
15581: timestamp by 1 until a unique key is obtained.
15582:
15583: 7. who: holder of locking key; defaults to user:domain for user.
15584:
15585: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
15586: retrying); default is 3.
15587:
15588: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
15589:
15590: Returns:
15591:
15592: 1. suffix obtained (numeric)
15593:
15594: 2. result of deleting locking key (ok if deleted, or lock never obtained)
15595:
15596: 3. error: contains (localized) error message if an error occurred.
15597:
15598:
15599: =back
15600:
15601: =head2 HTTP Helper Routines
15602:
15603: =over 4
15604:
15605: =item *
15606:
15607: escape() : unpack non-word characters into CGI-compatible hex codes
15608:
15609: =item *
15610:
15611: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
15612:
15613: =back
15614:
15615: =head1 PRIVATE SUBROUTINES
15616:
15617: =head2 Underlying communication routines (Shouldn't call)
15618:
15619: =over 4
15620:
15621: =item *
15622:
15623: subreply() : tries to pass a message to lonc, returns con_lost if incapable
15624:
15625: =item *
15626:
15627: reply() : uses subreply to send a message to remote machine, logs all failures
15628:
15629: =item *
15630:
15631: critical() : passes a critical message to another server; if cannot
15632: get through then place message in connection buffer directory and
15633: returns con_delayed, if incapable of saving message, returns
15634: con_failed
15635:
15636: =item *
15637:
15638: reconlonc() : tries to reconnect lonc client processes.
15639:
15640: =back
15641:
15642: =head2 Resource Access Logging
15643:
15644: =over 4
15645:
15646: =item *
15647:
15648: flushcourselogs() : flush (save) buffer logs and access logs
15649:
15650: =item *
15651:
15652: courselog($what) : save message for course in hash
15653:
15654: =item *
15655:
15656: courseacclog($what) : save message for course using &courselog(). Perform
15657: special processing for specific resource types (problems, exams, quizzes, etc).
15658:
15659: =item *
15660:
15661: goodbye() : flush course logs and log shutting down; it is called in srm.conf
15662: as a PerlChildExitHandler
15663:
15664: =back
15665:
15666: =head2 Other
15667:
15668: =over 4
15669:
15670: =item *
15671:
15672: symblist($mapname,%newhash) : update symbolic storage links
15673:
15674: =back
15675:
15676: =cut
15677:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>