Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1172.2.146.2.26
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1172.2.146. .26(raeb 4:-25): # $Id: lonnet.pm,v 1.1172.2.146.2.25 2024/09/25 17:33:08 raeburn Exp $
1.178 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.169 harris41 28: ###
29:
1.971 jms 30: =pod
31:
1.972 jms 32: =head1 NAME
33:
34: Apache::lonnet.pm
35:
36: =head1 SYNOPSIS
37:
38: This file is an interface to the lonc processes of
39: the LON-CAPA network as well as set of elaborated functions for handling information
40: necessary for navigating through a given cluster of LON-CAPA machines within a
41: domain. There are over 40 specialized functions in this module which handle the
42: reading and transmission of metadata, user information (ids, names, environments, roles,
43: logs), file information (storage, reading, directories, extensions, replication, embedded
44: styles and descriptors), educational resources (course descriptions, section names and
45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
46: and from more descriptive phrases or explanations.
47:
48: This is part of the LearningOnline Network with CAPA project
49: described at http://www.lon-capa.org.
50:
1.971 jms 51: =head1 Package Variables
52:
53: These are largely undocumented, so if you decipher one please note it here.
54:
55: =over 4
56:
57: =item $processmarker
58:
59: Contains the time this process was started and this servers host id.
60:
61: =item $dumpcount
62:
63: Counts the number of times a message log flush has been attempted (regardless
64: of success) by this process. Used as part of the filename when messages are
65: delayed.
66:
67: =back
68:
69: =cut
70:
1.1 albertel 71: package Apache::lonnet;
72:
73: use strict;
1.8 www 74: use LWP::UserAgent();
1.486 www 75: use HTTP::Date;
1.977 amueller 76: use Image::Magick;
1.1172.2.107 raeburn 77: use CGI::Cookie;
1.977 amueller 78:
1.1172.2.104 raeburn 79: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
1.1138 raeburn 80: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
1.1172.2.114 raeburn 81: %managerstab $passwdmin);
1.871 albertel 82:
83: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
84: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
85: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 86: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 87:
1.1 albertel 88: use IO::Socket;
1.31 www 89: use GDBM_File;
1.208 albertel 90: use HTML::LCParser;
1.88 www 91: use Fcntl qw(:flock);
1.870 albertel 92: use Storable qw(thaw nfreeze);
1.1172.2.79 raeburn 93: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599 albertel 94: use Cache::Memcached;
1.676 albertel 95: use Digest::MD5;
1.790 albertel 96: use Math::Random;
1.1024 raeburn 97: use File::MMagic;
1.1172.2.142 raeburn 98: use Net::CIDR;
99: use Sys::Hostname::FQDN();
1.807 albertel 100: use LONCAPA qw(:DEFAULT :match);
1.740 www 101: use LONCAPA::Configuration;
1.1160 www 102: use LONCAPA::lonmetadata;
1.1172.2.22 raeburn 103: use LONCAPA::Lond;
1.1172.2.110 raeburn 104: use LONCAPA::transliterate;
1.1117 foxr 105:
1.1090 raeburn 106: use File::Copy;
1.676 albertel 107:
1.195 www 108: my $readit;
1.1172.2.79 raeburn 109: my $max_connection_retries = 20; # Or some such value.
1.1 albertel 110:
1.619 albertel 111: require Exporter;
112:
113: our @ISA = qw (Exporter);
114: our @EXPORT = qw(%env);
115:
1.1172.2.9 raeburn 116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729 www 117: {
118: my $logid;
1.1172.2.9 raeburn 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: }
1.957 raeburn 126: }
1.1172.2.13 raeburn 127: $logid ++;
1.957 raeburn 128: my $now = time();
129: my $id=$now.'00000'.$$.'00000'.$logid;
1.1172.2.146. .1(raebu 130:22): my $ip = &get_requestor_ip();
1.1172.2.9 raeburn 131: my $logentry = {
132: $id => {
133: 'exe_uname' => $env{'user.name'},
134: 'exe_udom' => $env{'user.domain'},
135: 'exe_time' => $now,
1.1172.2.134 raeburn 136: 'exe_ip' => $ip,
1.1172.2.9 raeburn 137: 'delflag' => $delflag,
138: 'logentry' => $storehash,
139: 'uname' => $uname,
140: 'udom' => $udom,
141: }
142: };
143: return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729 www 144: }
145: }
1.1 albertel 146:
1.163 harris41 147: sub logtouch {
148: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 149: unless (-e "$execdir/logs/lonnet.log") {
1.1172.2.96 raeburn 150: open(my $fh,">>","$execdir/logs/lonnet.log");
1.163 harris41 151: close $fh;
152: }
153: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
154: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
155: }
156:
1.1 albertel 157: sub logthis {
158: my $message=shift;
159: my $execdir=$perlvar{'lonDaemons'};
160: my $now=time;
161: my $local=localtime($now);
1.1172.2.96 raeburn 162: if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986 foxr 163: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
164: print $fh $logstring;
1.448 albertel 165: close($fh);
166: }
1.1 albertel 167: return 1;
168: }
169:
170: sub logperm {
171: my $message=shift;
172: my $execdir=$perlvar{'lonDaemons'};
173: my $now=time;
174: my $local=localtime($now);
1.1172.2.96 raeburn 175: if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448 albertel 176: print $fh "$now:$message:$local\n";
177: close($fh);
178: }
1.1 albertel 179: return 1;
180: }
181:
1.850 albertel 182: sub create_connection {
1.853 albertel 183: my ($hostname,$lonid) = @_;
1.851 albertel 184: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 185: Type => SOCK_STREAM,
186: Timeout => 10);
187: return 0 if (!$client);
1.890 albertel 188: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 189: my $result = <$client>;
190: chomp($result);
191: return 1 if ($result eq 'done');
192: return 0;
193: }
194:
1.983 raeburn 195: sub get_server_timezone {
196: my ($cnum,$cdom) = @_;
197: my $home=&homeserver($cnum,$cdom);
198: if ($home ne 'no_host') {
199: my $cachetime = 24*3600;
200: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
201: if (defined($cached)) {
202: return $timezone;
203: } else {
204: my $timezone = &reply('servertimezone',$home);
205: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
206: }
207: }
208: }
1.850 albertel 209:
1.1106 raeburn 210: sub get_server_distarch {
211: my ($lonhost,$ignore_cache) = @_;
212: if (defined($lonhost)) {
213: if (!defined(&hostname($lonhost))) {
214: return;
215: }
216: my $cachetime = 12*3600;
217: if (!$ignore_cache) {
218: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
219: if (defined($cached)) {
220: return $distarch;
221: }
222: }
223: my $rep = &reply('serverdistarch',$lonhost);
1.1172.2.146. .26(raeb 224:-25): unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' ||
1.1106 raeburn 225: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
226: $rep eq '') {
227: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
228: }
229: }
230: return;
231: }
232:
1.993 raeburn 233: sub get_server_loncaparev {
1.1073 raeburn 234: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 235: if (defined($lonhost)) {
236: if (!defined(&hostname($lonhost))) {
237: undef($lonhost);
238: }
239: }
240: if (!defined($lonhost)) {
241: if (defined(&domain($dom,'primary'))) {
242: $lonhost=&domain($dom,'primary');
243: if ($lonhost eq 'no_host') {
244: undef($lonhost);
245: }
246: }
247: }
248: if (defined($lonhost)) {
1.1073 raeburn 249: my $cachetime = 12*3600;
250: if (!$ignore_cache) {
251: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
252: if (defined($cached)) {
253: return $loncaparev;
254: }
255: }
256: my ($answer,$loncaparev);
257: my @ids=¤t_machine_ids();
258: if (grep(/^\Q$lonhost\E$/,@ids)) {
259: $answer = $perlvar{'lonVersion'};
1.1081 raeburn 260: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 261: $loncaparev = $1;
262: }
263: } else {
264: $answer = &reply('serverloncaparev',$lonhost);
265: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
266: if ($caller eq 'loncron') {
267: my $ua=new LWP::UserAgent;
1.1082 raeburn 268: $ua->timeout(4);
1.1172.2.120 raeburn 269: my $hostname = &hostname($lonhost);
1.1073 raeburn 270: my $protocol = $protocol{$lonhost};
271: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 272: my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073 raeburn 273: my $request=new HTTP::Request('GET',$url);
274: my $response=$ua->request($request);
275: unless ($response->is_error()) {
276: my $content = $response->content;
1.1081 raeburn 277: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073 raeburn 278: $loncaparev = $1;
279: }
280: }
281: } else {
282: $loncaparev = $loncaparevs{$lonhost};
283: }
1.1081 raeburn 284: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 285: $loncaparev = $1;
286: }
1.993 raeburn 287: }
1.1073 raeburn 288: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993 raeburn 289: }
290: }
291:
1.1074 raeburn 292: sub get_server_homeID {
293: my ($hostname,$ignore_cache,$caller) = @_;
294: unless ($ignore_cache) {
295: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
296: if (defined($cached)) {
297: return $serverhomeID;
298: }
299: }
300: my $cachetime = 12*3600;
301: my $serverhomeID;
302: if ($caller eq 'loncron') {
303: my @machine_ids = &machine_ids($hostname);
304: foreach my $id (@machine_ids) {
305: my $response = &reply('serverhomeID',$id);
306: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
307: $serverhomeID = $response;
308: last;
309: }
310: }
311: if ($serverhomeID eq '') {
312: $serverhomeID = $machine_ids[-1];
313: }
314: } else {
315: $serverhomeID = $serverhomeIDs{$hostname};
316: }
317: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
318: }
319:
1.1121 raeburn 320: sub get_remote_globals {
321: my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125 raeburn 322: my ($result,%returnhash,%whatneeded);
323: if (ref($whathash) eq 'HASH') {
1.1121 raeburn 324: foreach my $what (sort(keys(%{$whathash}))) {
325: my $hashid = $lonhost.'-'.$what;
1.1125 raeburn 326: my ($response,$cached);
1.1121 raeburn 327: unless ($ignore_cache) {
1.1125 raeburn 328: ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121 raeburn 329: }
330: if (defined($cached)) {
1.1125 raeburn 331: $returnhash{$what} = $response;
1.1121 raeburn 332: } else {
1.1125 raeburn 333: $whatneeded{$what} = 1;
1.1121 raeburn 334: }
335: }
1.1125 raeburn 336: if (keys(%whatneeded) == 0) {
337: $result = 'ok';
338: } else {
1.1121 raeburn 339: my $requested = &freeze_escape(\%whatneeded);
340: my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125 raeburn 341: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
342: ($rep eq 'unknown_cmd')) {
343: $result = $rep;
344: } else {
345: $result = 'ok';
1.1121 raeburn 346: my @pairs=split(/\&/,$rep);
1.1125 raeburn 347: foreach my $item (@pairs) {
348: my ($key,$value)=split(/=/,$item,2);
349: my $what = &unescape($key);
350: my $hashid = $lonhost.'-'.$what;
351: $returnhash{$what}=&thaw_unescape($value);
352: &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121 raeburn 353: }
354: }
355: }
356: }
1.1125 raeburn 357: return ($result,\%returnhash);
1.1121 raeburn 358: }
359:
1.1124 raeburn 360: sub remote_devalidate_cache {
1.1172.2.35 raeburn 361: my ($lonhost,$cachekeys) = @_;
362: my $items;
363: return unless (ref($cachekeys) eq 'ARRAY');
364: my $cachestr = join('&',@{$cachekeys});
365: return &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124 raeburn 366: }
367:
1.1172.2.146. .13(raeb 368:-23): sub sign_lti {
369:-23): my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
370:-23): my $chome;
371:-23): if (&domain($cdom) ne '') {
372:-23): if ($crsdef) {
373:-23): $chome = &homeserver($cnum,$cdom);
374:-23): } else {
375:-23): $chome = &domain($cdom,'primary');
376:-23): }
377:-23): }
378:-23): if ($cdom && $chome && ($chome ne 'no_host')) {
379:-23): if ((ref($paramsref) eq 'HASH') &&
380:-23): (ref($inforef) eq 'HASH')) {
381:-23): my $rep;
382:-23): if (grep { $_ eq $chome } ¤t_machine_ids()) {
383:-23): # domain information is hosted on this machine
384:-23): $rep =
385:-23): &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
386:-23): $context,$url,$ltinum,$keynum,
387:-23): $perlvar{'lonVersion'},
388:-23): $paramsref,$inforef);
389:-23): if (ref($rep) eq 'HASH') {
390:-23): return ('ok',$rep);
391:-23): }
392:-23): } else {
393:-23): my ($escurl,$params,$info);
394:-23): $escurl = &escape($url);
395:-23): if (ref($paramsref) eq 'HASH') {
396:-23): $params = &freeze_escape($paramsref);
397:-23): }
398:-23): if (ref($inforef) eq 'HASH') {
399:-23): $info = &freeze_escape($inforef);
400:-23): }
401:-23): $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
402:-23): }
403:-23): if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
404:-23): return ();
405:-23): } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
406:-23): ($inforef->{'respfmt'} eq 'to_authorization_header')) {
407:-23): return ('ok',$rep);
408:-23): } else {
409:-23): my %returnhash;
410:-23): foreach my $item (split(/\&/,$rep)) {
411:-23): my ($name,$value)=split(/\=/,$item);
412:-23): $returnhash{&unescape($name)}=&thaw_unescape($value);
413:-23): }
414:-23): return('ok',\%returnhash);
415:-23): }
416:-23): } else {
417:-23): return ();
418:-23): }
419:-23): } else {
420:-23): return ();
421:-23): &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
422:-23): }
423:-23): }
424:-23):
1.1 albertel 425: # -------------------------------------------------- Non-critical communication
426: sub subreply {
427: my ($cmd,$server)=@_;
1.838 albertel 428: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 429: #
430: # With loncnew process trimming, there's a timing hole between lonc server
431: # process exit and the master server picking up the listen on the AF_UNIX
432: # socket. In that time interval, a lock file will exist:
433:
434: my $lockfile=$peerfile.".lock";
435: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
1.1172.2.79 raeburn 436: sleep(0.1);
1.549 foxr 437: }
438: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 439: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 440: #
1.550 foxr 441: # We'll give the connection a few tries before abandoning it. If
442: # connection is not possible, we'll con_lost back to the client.
443: #
444: my $client;
445: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
446: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
447: Type => SOCK_STREAM,
448: Timeout => 10);
1.869 albertel 449: if ($client) {
1.550 foxr 450: last; # Connected!
1.850 albertel 451: } else {
1.853 albertel 452: &create_connection(&hostname($server),$server);
1.550 foxr 453: }
1.1172.2.79 raeburn 454: sleep(0.1); # Try again later if failed connection.
1.550 foxr 455: }
456: my $answer;
457: if ($client) {
1.704 albertel 458: print $client "sethost:$server:$cmd\n";
1.550 foxr 459: $answer=<$client>;
460: if (!$answer) { $answer="con_lost"; }
461: chomp($answer);
462: } else {
463: $answer = 'con_lost'; # Failed connection.
464: }
1.1 albertel 465: return $answer;
466: }
467:
468: sub reply {
469: my ($cmd,$server)=@_;
1.838 albertel 470: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 471: my $answer=subreply($cmd,$server);
1.65 www 472: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1172.2.111 raeburn 473: my $logged = $cmd;
474: if ($cmd =~ /^encrypt:([^:]+):/) {
475: my $subcmd = $1;
476: if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
477: ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 478:-23): ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
479:-23): ($subcmd eq 'put')) {
1.1172.2.111 raeburn 480: (undef,undef,my @rest) = split(/:/,$cmd);
481: if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
482: splice(@rest,2,1,'Hidden');
483: } elsif ($subcmd eq 'passwd') {
484: splice(@rest,2,2,('Hidden','Hidden'));
485: } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 486:-23): ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
1.1172.2.111 raeburn 487: splice(@rest,3,1,'Hidden');
488: }
489: $logged = join(':',('encrypt:'.$subcmd,@rest));
490: }
491: }
492: &logthis("<font color=\"blue\">WARNING:".
493: " $logged to $server returned $answer</font>");
1.12 www 494: }
1.1 albertel 495: return $answer;
496: }
497:
498: # ----------------------------------------------------------- Send USR1 to lonc
499:
500: sub reconlonc {
1.891 albertel 501: my ($lonid) = @_;
502: if ($lonid) {
1.1172.2.72 raeburn 503: my $hostname = &hostname($lonid);
1.891 albertel 504: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
505: if ($hostname && -e $peerfile) {
506: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
507: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
508: Type => SOCK_STREAM,
509: Timeout => 10);
510: if ($client) {
511: print $client ("reset_retries\n");
512: my $answer=<$client>;
513: #reset just this one.
514: }
515: }
516: return;
517: }
518:
1.836 www 519: &logthis("Trying to reconnect lonc");
1.1 albertel 520: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1172.2.96 raeburn 521: if (open(my $fh,"<",$loncfile)) {
1.1 albertel 522: my $loncpid=<$fh>;
523: chomp($loncpid);
524: if (kill 0 => $loncpid) {
525: &logthis("lonc at pid $loncpid responding, sending USR1");
526: kill USR1 => $loncpid;
527: sleep 1;
1.836 www 528: } else {
1.12 www 529: &logthis(
1.672 albertel 530: "<font color=\"blue\">WARNING:".
1.12 www 531: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 532: }
533: } else {
1.836 www 534: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 535: }
536: }
537:
538: # ------------------------------------------------------ Critical communication
1.12 www 539:
1.1 albertel 540: sub critical {
541: my ($cmd,$server)=@_;
1.838 albertel 542: unless (&hostname($server)) {
1.672 albertel 543: &logthis("<font color=\"blue\">WARNING:".
1.89 www 544: " Critical message to unknown server ($server)</font>");
545: return 'no_such_host';
546: }
1.1 albertel 547: my $answer=reply($cmd,$server);
548: if ($answer eq 'con_lost') {
1.1172.2.72 raeburn 549: &reconlonc($server);
1.589 albertel 550: my $answer=reply($cmd,$server);
1.1 albertel 551: if ($answer eq 'con_lost') {
552: my $now=time;
553: my $middlename=$cmd;
1.5 www 554: $middlename=substr($middlename,0,16);
1.1 albertel 555: $middlename=~s/\W//g;
556: my $dfilename=
1.305 www 557: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
558: $dumpcount++;
1.1 albertel 559: {
1.448 albertel 560: my $dfh;
1.1172.2.96 raeburn 561: if (open($dfh,">",$dfilename)) {
1.448 albertel 562: print $dfh "$cmd\n";
563: close($dfh);
564: }
1.1 albertel 565: }
1.1172.2.79 raeburn 566: sleep 1;
1.1 albertel 567: my $wcmd='';
568: {
1.448 albertel 569: my $dfh;
1.1172.2.96 raeburn 570: if (open($dfh,"<",$dfilename)) {
1.448 albertel 571: $wcmd=<$dfh>;
572: close($dfh);
573: }
1.1 albertel 574: }
575: chomp($wcmd);
1.7 www 576: if ($wcmd eq $cmd) {
1.672 albertel 577: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 578: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 579: &logperm("D:$server:$cmd");
580: return 'con_delayed';
581: } else {
1.672 albertel 582: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 583: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 584: &logperm("F:$server:$cmd");
585: return 'con_failed';
586: }
587: }
588: }
589: return $answer;
1.405 albertel 590: }
591:
1.755 albertel 592: # ------------------------------------------- check if return value is an error
593:
594: sub error {
595: my ($result) = @_;
1.756 albertel 596: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 597: if ($2 == 2) { return undef; }
598: return $1;
599: }
600: return undef;
601: }
602:
1.783 albertel 603: sub convert_and_load_session_env {
604: my ($lonidsdir,$handle)=@_;
605: my @profile;
606: {
1.917 albertel 607: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
608: if (!$opened) {
1.915 albertel 609: return 0;
610: }
1.783 albertel 611: flock($idf,LOCK_SH);
612: @profile=<$idf>;
613: close($idf);
614: }
615: my %temp_env;
616: foreach my $line (@profile) {
1.786 albertel 617: if ($line !~ m/=/) {
618: return 0;
619: }
1.783 albertel 620: chomp($line);
621: my ($envname,$envvalue)=split(/=/,$line,2);
622: $temp_env{&unescape($envname)} = &unescape($envvalue);
623: }
624: unlink("$lonidsdir/$handle.id");
625: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
626: 0640)) {
627: %disk_env = %temp_env;
628: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
629: untie(%disk_env);
630: }
1.786 albertel 631: return 1;
1.783 albertel 632: }
633:
1.374 www 634: # ------------------------------------------- Transfer profile into environment
1.780 albertel 635: my $env_loaded;
636: sub transfer_profile_to_env {
1.788 albertel 637: my ($lonidsdir,$handle,$force_transfer) = @_;
638: if (!$force_transfer && $env_loaded) { return; }
1.374 www 639:
1.720 albertel 640: if (!defined($lonidsdir)) {
641: $lonidsdir = $perlvar{'lonIDsDir'};
642: }
643: if (!defined($handle)) {
644: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
645: }
646:
1.786 albertel 647: my $convert;
648: {
1.917 albertel 649: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
650: if (!$opened) {
1.915 albertel 651: return;
652: }
1.786 albertel 653: flock($idf,LOCK_SH);
654: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
655: &GDBM_READER(),0640)) {
656: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
657: untie(%disk_env);
658: } else {
659: $convert = 1;
660: }
661: }
662: if ($convert) {
663: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
664: &logthis("Failed to load session, or convert session.");
665: }
1.374 www 666: }
1.783 albertel 667:
1.786 albertel 668: my %remove;
1.783 albertel 669: while ( my $envname = each(%env) ) {
1.433 matthew 670: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
671: if ($time < time-300) {
1.783 albertel 672: $remove{$key}++;
1.433 matthew 673: }
674: }
675: }
1.783 albertel 676:
1.619 albertel 677: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 678: $env_loaded=1;
1.783 albertel 679: foreach my $expired_key (keys(%remove)) {
1.433 matthew 680: &delenv($expired_key);
1.374 www 681: }
1.1 albertel 682: }
683:
1.916 albertel 684: # ---------------------------------------------------- Check for valid session
685: sub check_for_valid_session {
1.1172.2.96 raeburn 686: my ($r,$name,$userhashref,$domref) = @_;
1.916 albertel 687: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1172.2.108 raeburn 688: my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
1.1155 raeburn 689: if ($name eq 'lonDAV') {
690: $lonidsdir=$r->dir_config('lonDAVsessDir');
691: } else {
692: $lonidsdir=$r->dir_config('lonIDsDir');
1.1172.2.108 raeburn 693: if ($name eq '') {
694: $name = 'lonID';
695: }
696: }
697: if ($name eq 'lonID') {
698: $secure = 'lonSID';
699: $linkname = 'lonLinkID';
700: $pubname = 'lonPubID';
701: if (exists($cookies{$secure})) {
702: $lonid=$cookies{$secure};
703: } elsif (exists($cookies{$name})) {
704: $lonid=$cookies{$name};
705: } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
706: $lonid=$cookies{$linkname};
707: } elsif (exists($cookies{$pubname})) {
708: $lonid=$cookies{$pubname};
709: }
710: } else {
711: $lonid=$cookies{$name};
712: }
713: return undef if (!$lonid);
714:
715: my $handle=&LONCAPA::clean_handle($lonid->value);
716: if (-l "$lonidsdir/$handle.id") {
717: my $link = readlink("$lonidsdir/$handle.id");
718: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
719: $handle = $1;
720: }
1.1155 raeburn 721: }
1.1172.2.96 raeburn 722: if (!-e "$lonidsdir/$handle.id") {
723: if ((ref($domref)) && ($name eq 'lonID') &&
724: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
725: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
726: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
727: $$domref = $possudom;
728: }
729: }
730: return undef;
731: }
1.916 albertel 732:
1.917 albertel 733: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
734: return undef if (!$opened);
1.916 albertel 735:
736: flock($idf,LOCK_SH);
737: my %disk_env;
738: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
739: &GDBM_READER(),0640)) {
740: return undef;
741: }
742:
743: if (!defined($disk_env{'user.name'})
744: || !defined($disk_env{'user.domain'})) {
1.1172.2.107 raeburn 745: untie(%disk_env);
1.916 albertel 746: return undef;
747: }
1.1172.2.18 raeburn 748:
1.1172.2.36 raeburn 749: if (ref($userhashref) eq 'HASH') {
750: $userhashref->{'name'} = $disk_env{'user.name'};
751: $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1172.2.142 raeburn 752: if ($disk_env{'request.role'}) {
753: $userhashref->{'role'} = $disk_env{'request.role'};
754: }
1.1172.2.146. .13(raeb 755:-23): $userhashref->{'lti'} = $disk_env{'request.lti.login'};
756:-23): if ($userhashref->{'lti'}) {
757:-23): $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
758:-23): $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
759:-23): }
1.1172.2.18 raeburn 760: }
1.1172.2.107 raeburn 761: untie(%disk_env);
1.1172.2.18 raeburn 762:
1.916 albertel 763: return $handle;
764: }
765:
1.830 albertel 766: sub timed_flock {
767: my ($file,$lock_type) = @_;
768: my $failed=0;
769: eval {
770: local $SIG{__DIE__}='DEFAULT';
771: local $SIG{ALRM}=sub {
772: $failed=1;
773: die("failed lock");
774: };
775: alarm(13);
776: flock($file,$lock_type);
777: alarm(0);
778: };
779: if ($failed) {
780: return undef;
781: } else {
782: return 1;
783: }
784: }
785:
1.1172.2.107 raeburn 786: sub get_sessionfile_vars {
787: my ($handle,$lonidsdir,$storearr) = @_;
788: my %returnhash;
789: unless (ref($storearr) eq 'ARRAY') {
790: return %returnhash;
791: }
792: if (-l "$lonidsdir/$handle.id") {
793: my $link = readlink("$lonidsdir/$handle.id");
794: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
795: $handle = $1;
796: }
797: }
798: if ((-e "$lonidsdir/$handle.id") &&
799: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
800: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
801: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
802: if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
803: flock($idf,LOCK_SH);
804: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
805: &GDBM_READER(),0640)) {
806: foreach my $item (@{$storearr}) {
807: $returnhash{$item} = $disk_env{$item};
808: }
809: untie(%disk_env);
810: }
811: }
812: }
813: }
814: return %returnhash;
815: }
816:
1.5 www 817: # ---------------------------------------------------------- Append Environment
818:
819: sub appenv {
1.949 raeburn 820: my ($newenv,$roles) = @_;
821: if (ref($newenv) eq 'HASH') {
822: foreach my $key (keys(%{$newenv})) {
823: my $refused = 0;
824: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
825: $refused = 1;
826: if (ref($roles) eq 'ARRAY') {
1.1172.2.40 raeburn 827: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949 raeburn 828: if (grep(/^\Q$role\E$/,@{$roles})) {
829: $refused = 0;
830: }
831: }
832: }
833: if ($refused) {
834: &logthis("<font color=\"blue\">WARNING: ".
835: "Attempt to modify environment ".$key." to ".$newenv->{$key}
836: .'</font>');
837: delete($newenv->{$key});
838: } else {
839: $env{$key}=$newenv->{$key};
840: }
841: }
1.1172.2.96 raeburn 842: my $lonids = $perlvar{'lonIDsDir'};
843: if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
844: my $opened = open(my $env_file,'+<',$env{'user.environment'});
845: if ($opened
846: && &timed_flock($env_file,LOCK_EX)
847: &&
848: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
849: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
850: while (my ($key,$value) = each(%{$newenv})) {
851: $disk_env{$key} = $value;
852: }
853: untie(%disk_env);
854: }
1.35 www 855: }
1.191 harris41 856: }
1.56 www 857: return 'ok';
858: }
859: # ----------------------------------------------------- Delete from Environment
860:
861: sub delenv {
1.1104 raeburn 862: my ($delthis,$regexp,$roles) = @_;
863: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
864: my $refused = 1;
865: if (ref($roles) eq 'ARRAY') {
866: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
867: if (grep(/^\Q$role\E$/,@{$roles})) {
868: $refused = 0;
869: }
870: }
871: if ($refused) {
872: &logthis("<font color=\"blue\">WARNING: ".
873: "Attempt to delete from environment ".$delthis);
874: return 'error';
875: }
1.56 www 876: }
1.917 albertel 877: my $opened = open(my $env_file,'+<',$env{'user.environment'});
878: if ($opened
1.915 albertel 879: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 880: &&
881: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
882: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 883: foreach my $key (keys(%disk_env)) {
1.987 raeburn 884: if ($regexp) {
885: if ($key=~/^$delthis/) {
886: delete($env{$key});
887: delete($disk_env{$key});
888: }
889: } else {
890: if ($key=~/^\Q$delthis\E/) {
891: delete($env{$key});
892: delete($disk_env{$key});
893: }
894: }
1.448 albertel 895: }
1.783 albertel 896: untie(%disk_env);
1.5 www 897: }
898: return 'ok';
1.369 albertel 899: }
900:
1.790 albertel 901: sub get_env_multiple {
902: my ($name) = @_;
903: my @values;
904: if (defined($env{$name})) {
905: # exists is it an array
906: if (ref($env{$name})) {
907: @values=@{ $env{$name} };
908: } else {
909: $values[0]=$env{$name};
910: }
911: }
912: return(@values);
913: }
914:
1.958 www 915: # ------------------------------------------------------------------- Locking
916:
917: sub set_lock {
918: my ($text)=@_;
919: $locknum++;
920: my $id=$$.'-'.$locknum;
921: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
922: 'session.lock.'.$id => $text});
923: return $id;
924: }
925:
926: sub get_locks {
927: my $num=0;
928: my %texts=();
929: foreach my $lock (split(/\,/,$env{'session.locks'})) {
930: if ($lock=~/\w/) {
931: $num++;
932: $texts{$lock}=$env{'session.lock.'.$lock};
933: }
934: }
935: return ($num,%texts);
936: }
937:
938: sub remove_lock {
939: my ($id)=@_;
940: my $newlocks='';
941: foreach my $lock (split(/\,/,$env{'session.locks'})) {
942: if (($lock=~/\w/) && ($lock ne $id)) {
943: $newlocks.=','.$lock;
944: }
945: }
946: &appenv({'session.locks' => $newlocks});
947: &delenv('session.lock.'.$id);
948: }
949:
950: sub remove_all_locks {
951: my $activelocks=$env{'session.locks'};
952: foreach my $lock (split(/\,/,$env{'session.locks'})) {
953: if ($lock=~/\w/) {
954: &remove_lock($lock);
955: }
956: }
957: }
958:
959:
1.369 albertel 960: # ------------------------------------------ Find out current server userload
961: sub userload {
962: my $numusers=0;
963: {
964: opendir(LONIDS,$perlvar{'lonIDsDir'});
965: my $filename;
966: my $curtime=time;
967: while ($filename=readdir(LONIDS)) {
1.925 albertel 968: next if ($filename eq '.' || $filename eq '..');
969: next if ($filename =~ /publicuser_\d+\.id/);
1.1172.2.112 raeburn 970: next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404 albertel 971: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 972: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 973: }
974: closedir(LONIDS);
975: }
976: my $userloadpercent=0;
977: my $maxuserload=$perlvar{'lonUserLoadLim'};
978: if ($maxuserload) {
1.371 albertel 979: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 980: }
1.372 albertel 981: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 982: return $userloadpercent;
1.283 www 983: }
984:
1.1 albertel 985: # ------------------------------ Find server with least workload from spare.tab
1.11 www 986:
1.1 albertel 987: sub spareserver {
1.1172.2.142 raeburn 988: my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 989: my $spare_server;
1.370 albertel 990: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 991: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
992: : $userloadpercent;
1.1083 raeburn 993: my ($uint_dom,$remotesessions);
994: if (($udom ne '') && (&domain($udom) ne '')) {
995: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
996: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
997: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
998: $remotesessions = $udomdefaults{'remotesessions'};
999: }
1.1123 raeburn 1000: my $spareshash = &this_host_spares($udom);
1001: if (ref($spareshash) eq 'HASH') {
1002: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1003: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1172.2.62 raeburn 1004: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
1005: $try_server));
1.1123 raeburn 1006: ($spare_server, $lowest_load) =
1007: &compare_server_load($try_server, $spare_server, $lowest_load);
1008: }
1.1083 raeburn 1009: }
1.784 albertel 1010:
1.1123 raeburn 1011: my $found_server = ($spare_server ne '' && $lowest_load < 100);
1012:
1013: if (!$found_server) {
1014: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1015: foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1172.2.62 raeburn 1016: next unless (&spare_can_host($udom,$uint_dom,
1017: $remotesessions,$try_server));
1.1123 raeburn 1018: ($spare_server, $lowest_load) =
1019: &compare_server_load($try_server, $spare_server, $lowest_load);
1020: }
1021: }
1022: }
1.784 albertel 1023: }
1024:
1025: if (!$want_server_name) {
1.1001 raeburn 1026: if (defined($spare_server)) {
1027: my $hostname = &hostname($spare_server);
1.1083 raeburn 1028: if (defined($hostname)) {
1.1172.2.120 raeburn 1029: my $protocol = 'http';
1030: if ($protocol{$spare_server} eq 'https') {
1031: $protocol = $protocol{$spare_server};
1032: }
1.1172.2.142 raeburn 1033: my $alias = &Apache::lonnet::use_proxy_alias($r,$spare_server);
1034: $hostname = $alias if ($alias ne '');
1.1001 raeburn 1035: $spare_server = $protocol.'://'.$hostname;
1036: }
1037: }
1.784 albertel 1038: }
1039: return $spare_server;
1040: }
1041:
1042: sub compare_server_load {
1.1172.2.41 raeburn 1043: my ($try_server, $spare_server, $lowest_load, $required) = @_;
1044:
1045: if ($required) {
1046: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
1047: my $remoterev = &get_server_loncaparev(undef,$try_server);
1048: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
1049: if (($major eq '' && $minor eq '') ||
1050: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
1051: return ($spare_server,$lowest_load);
1052: }
1053: }
1.784 albertel 1054:
1055: my $loadans = &reply('load', $try_server);
1056: my $userloadans = &reply('userload',$try_server);
1057:
1058: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114 raeburn 1059: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 1060: }
1061:
1062: my $load;
1063: if ($loadans =~ /\d/) {
1064: if ($userloadans =~ /\d/) {
1065: #both are numbers, pick the bigger one
1066: $load = ($loadans > $userloadans) ? $loadans
1067: : $userloadans;
1.411 albertel 1068: } else {
1.784 albertel 1069: $load = $loadans;
1.411 albertel 1070: }
1.784 albertel 1071: } else {
1072: $load = $userloadans;
1073: }
1074:
1075: if (($load =~ /\d/) && ($load < $lowest_load)) {
1076: $spare_server = $try_server;
1077: $lowest_load = $load;
1.370 albertel 1078: }
1.784 albertel 1079: return ($spare_server,$lowest_load);
1.202 matthew 1080: }
1.914 albertel 1081:
1082: # --------------------------- ask offload servers if user already has a session
1083: sub find_existing_session {
1084: my ($udom,$uname) = @_;
1.1123 raeburn 1085: my $spareshash = &this_host_spares($udom);
1086: if (ref($spareshash) eq 'HASH') {
1087: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1088: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1089: return $try_server if (&has_user_session($try_server, $udom, $uname));
1090: }
1091: }
1092: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1093: foreach my $try_server (@{ $spareshash->{'default'} }) {
1094: return $try_server if (&has_user_session($try_server, $udom, $uname));
1095: }
1096: }
1.914 albertel 1097: }
1098: return;
1099: }
1100:
1.1172.2.146. .13(raeb 1101:-23): sub delusersession {
1102:-23): my ($lonid,$udom,$uname) = @_;
1103:-23): my $uprimary_id = &domain($udom,'primary');
1104:-23): my $uintdom = &internet_dom($uprimary_id);
1105:-23): my $intdom = &internet_dom($lonid);
1106:-23): my $serverhomedom = &host_domain($lonid);
1107:-23): if (($uintdom ne '') && ($uintdom eq $intdom)) {
1108:-23): return &reply(join(':','delusersession',
1109:-23): map {&escape($_)} ($udom,$uname)),$lonid);
1110:-23): }
1111:-23): return;
1112:-23): }
1113:-23):
1114:-23):
1.1172.2.107 raeburn 1115: # check if user's browser sent load balancer cookie and server still has session
1116: # and is not overloaded.
1117: sub check_for_balancer_cookie {
1118: my ($r,$update_mtime) = @_;
1119: my ($otherserver,$cookie);
1120: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1121: if (exists($cookies{'balanceID'})) {
1122: my $balid = $cookies{'balanceID'};
1123: $cookie=&LONCAPA::clean_handle($balid->value);
1124: my $balancedir=$r->dir_config('lonBalanceDir');
1125: if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
1126: if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
1127: my ($possudom,$possuname) = ($1,$2);
1128: my $has_session = 0;
1129: if ((&domain($possudom) ne '') &&
1130: (&homeserver($possuname,$possudom) ne 'no_host')) {
1131: my $try_server;
1132: my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
1133: if ($opened) {
1134: flock($idf,LOCK_SH);
1135: while (my $line = <$idf>) {
1136: chomp($line);
1137: if (&hostname($line) ne '') {
1138: $try_server = $line;
1139: last;
1140: }
1141: }
1142: close($idf);
1143: if (($try_server) &&
1144: (&has_user_session($try_server,$possudom,$possuname))) {
1145: my $lowest_load = 30000;
1146: ($otherserver,$lowest_load) =
1147: &compare_server_load($try_server,undef,$lowest_load);
1148: if ($otherserver ne '' && $lowest_load < 100) {
1149: $has_session = 1;
1150: } else {
1151: undef($otherserver);
1152: }
1153: }
1154: }
1155: }
1156: if ($has_session) {
1157: if ($update_mtime) {
1158: my $atime = my $mtime = time;
1159: utime($atime,$mtime,"$balancedir/$cookie.id");
1160: }
1161: } else {
1162: unlink("$balancedir/$cookie.id");
1163: }
1164: }
1165: }
1166: }
1167: return ($otherserver,$cookie);
1168: }
1169:
1.1172.2.130 raeburn 1170: sub updatebalcookie {
1171: my ($cookie,$balancer,$lastentry)=@_;
1172: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1173: my ($udom,$uname) = ($1,$2);
1174: my $uprimary_id = &domain($udom,'primary');
1175: my $uintdom = &internet_dom($uprimary_id);
1176: my $intdom = &internet_dom($balancer);
1177: my $serverhomedom = &host_domain($balancer);
1178: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1179: return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
1180: }
1181: }
1182: return;
1183: }
1184:
1.1172.2.107 raeburn 1185: sub delbalcookie {
1186: my ($cookie,$balancer) =@_;
1187: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1188: my ($udom,$uname) = ($1,$2);
1189: my $uprimary_id = &domain($udom,'primary');
1190: my $uintdom = &internet_dom($uprimary_id);
1191: my $intdom = &internet_dom($balancer);
1192: my $serverhomedom = &host_domain($balancer);
1193: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1172.2.130 raeburn 1194: return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1172.2.107 raeburn 1195: }
1196: }
1197: }
1198:
1.914 albertel 1199: # -------------------------------- ask if server already has a session for user
1200: sub has_user_session {
1201: my ($lonid,$udom,$uname) = @_;
1202: my $result = &reply(join(':','userhassession',
1203: map {&escape($_)} ($udom,$uname)),$lonid);
1204: return 1 if ($result eq 'ok');
1205:
1206: return 0;
1207: }
1208:
1.1076 raeburn 1209: # --------- determine least loaded server in a user's domain which allows login
1210:
1211: sub choose_server {
1.1172.2.47 raeburn 1212: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076 raeburn 1213: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077 raeburn 1214: my %servers = &get_servers($udom);
1.1076 raeburn 1215: my $lowest_load = 30000;
1.1172.2.47 raeburn 1216: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1217: if ($skiploadbal) {
1218: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1219: unless (defined($cached)) {
1220: my $cachetime = 60*60*24;
1221: my %domconfig =
1222: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1223: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1224: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1225: $cachetime);
1226: }
1227: }
1228: }
1.1076 raeburn 1229: foreach my $lonhost (keys(%servers)) {
1.1115 raeburn 1230: my $loginvia;
1.1172.2.47 raeburn 1231: if ($skiploadbal) {
1232: if (ref($balancers) eq 'HASH') {
1233: next if (exists($balancers->{$lonhost}));
1234: }
1235: }
1.1115 raeburn 1236: if ($checkloginvia) {
1237: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116 raeburn 1238: if ($loginvia) {
1239: my ($server,$path) = split(/:/,$loginvia);
1240: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1241: &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116 raeburn 1242: if ($login_host eq $server) {
1243: $portal_path = $path;
1.1151 raeburn 1244: $isredirect = 1;
1.1116 raeburn 1245: }
1246: } else {
1247: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1248: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116 raeburn 1249: if ($login_host eq $lonhost) {
1250: $portal_path = '';
1.1151 raeburn 1251: $isredirect = '';
1.1116 raeburn 1252: }
1253: }
1254: } else {
1.1076 raeburn 1255: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1256: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076 raeburn 1257: }
1258: }
1259: if ($login_host ne '') {
1.1116 raeburn 1260: $hostname = &hostname($login_host);
1.1076 raeburn 1261: }
1.1172.2.88 raeburn 1262: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076 raeburn 1263: }
1264:
1.1172.2.123 raeburn 1265: sub get_course_sessions {
1266: my ($cnum,$cdom,$lastactivity) = @_;
1267: my %servers = &internet_dom_servers($cdom);
1268: my %returnhash;
1269: foreach my $server (sort(keys(%servers))) {
1270: my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
1271: my @pairs=split(/\&/,$rep);
1272: unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
1273: foreach my $item (@pairs) {
1274: my ($key,$value)=split(/=/,$item,2);
1275: $key = &unescape($key);
1276: next if ($key =~ /^error: 2 /);
1277: if (exists($returnhash{$key})) {
1278: next if ($value < $returnhash{$key});
1279: }
1280: $returnhash{$key}=$value;
1281: }
1282: }
1283: }
1284: return %returnhash;
1285: }
1286:
1.202 matthew 1287: # --------------------------------------------- Try to change a user's password
1288:
1289: sub changepass {
1.799 raeburn 1290: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 1291: $currentpass = &escape($currentpass);
1292: $newpass = &escape($newpass);
1.1030 raeburn 1293: my $lonhost = $perlvar{'lonHostID'};
1294: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 1295: $server);
1296: if (! $answer) {
1297: &logthis("No reply on password change request to $server ".
1298: "by $uname in domain $udom.");
1299: } elsif ($answer =~ "^ok") {
1300: &logthis("$uname in $udom successfully changed their password ".
1301: "on $server.");
1302: } elsif ($answer =~ "^pwchange_failure") {
1303: &logthis("$uname in $udom was unable to change their password ".
1304: "on $server. The action was blocked by either lcpasswd ".
1305: "or pwchange");
1306: } elsif ($answer =~ "^non_authorized") {
1307: &logthis("$uname in $udom did not get their password correct when ".
1308: "attempting to change it on $server.");
1309: } elsif ($answer =~ "^auth_mode_error") {
1310: &logthis("$uname in $udom attempted to change their password despite ".
1311: "not being locally or internally authenticated on $server.");
1312: } elsif ($answer =~ "^unknown_user") {
1313: &logthis("$uname in $udom attempted to change their password ".
1314: "on $server but were unable to because $server is not ".
1315: "their home server.");
1316: } elsif ($answer =~ "^refused") {
1317: &logthis("$server refused to change $uname in $udom password because ".
1318: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 1319: } elsif ($answer =~ "invalid_client") {
1320: &logthis("$server refused to change $uname in $udom password because ".
1321: "it was a reset by e-mail originating from an invalid server.");
1.1172.2.113 raeburn 1322: } elsif ($answer =~ "^prioruse") {
1323: &logthis("$server refused to change $uname in $udom password because ".
1324: "the password had been used before");
1.202 matthew 1325: }
1326: return $answer;
1.1 albertel 1327: }
1328:
1.169 harris41 1329: # ----------------------- Try to determine user's current authentication scheme
1330:
1331: sub queryauthenticate {
1332: my ($uname,$udom)=@_;
1.456 albertel 1333: my $uhome=&homeserver($uname,$udom);
1.1172.2.146. .5(raebu 1334:22): if ((!$uhome) || ($uhome eq 'no_host')) {
1.456 albertel 1335: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1336: return 'no_host';
1337: }
1338: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1339: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1340: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 1341: }
1.456 albertel 1342: return $answer;
1.169 harris41 1343: }
1344:
1.1 albertel 1345: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1346:
1.1 albertel 1347: sub authenticate {
1.1073 raeburn 1348: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1349: $upass=&escape($upass);
1350: $uname= &LONCAPA::clean_username($uname);
1.836 www 1351: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1352: my $newhome;
1.836 www 1353: if ((!$uhome) || ($uhome eq 'no_host')) {
1354: # Maybe the machine was offline and only re-appeared again recently?
1355: &reconlonc();
1356: # One more
1.952 raeburn 1357: $uhome=&homeserver($uname,$udom,1);
1358: if (($uhome eq 'no_host') && $checkdefauth) {
1359: if (defined(&domain($udom,'primary'))) {
1360: $newhome=&domain($udom,'primary');
1361: }
1362: if ($newhome ne '') {
1363: $uhome = $newhome;
1364: }
1365: }
1.836 www 1366: if ((!$uhome) || ($uhome eq 'no_host')) {
1367: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1368: return 'no_host';
1369: }
1.1 albertel 1370: }
1.1073 raeburn 1371: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1372: if ($answer eq 'authorized') {
1.952 raeburn 1373: if ($newhome) {
1374: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1375: return 'no_account_on_host';
1376: } else {
1377: &logthis("User $uname at $udom authorized by $uhome");
1378: return $uhome;
1379: }
1.471 albertel 1380: }
1381: if ($answer eq 'non_authorized') {
1382: &logthis("User $uname at $udom rejected by $uhome");
1.1172.2.146. .5(raebu 1383:22): return 'no_host';
1.9 www 1384: }
1.471 albertel 1385: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1386: return 'no_host';
1387: }
1388:
1.1172.2.146. .6(raebu 1389:22): sub can_switchserver {
1390:22): my ($udom,$home) = @_;
1391:22): my ($canswitch,@intdoms);
1392:22): my $internet_names = &get_internet_names($home);
1393:22): if (ref($internet_names) eq 'ARRAY') {
1394:22): @intdoms = @{$internet_names};
1395:22): }
1396:22): my $uint_dom = &internet_dom(&domain($udom,'primary'));
1397:22): if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
1398:22): $canswitch = 1;
1399:22): } else {
1400:22): my $serverhomeID = &get_server_homeID(&hostname($home));
1401:22): my $serverhomedom = &host_domain($serverhomeID);
1402:22): my %defdomdefaults = &get_domain_defaults($serverhomedom);
1403:22): my %udomdefaults = &get_domain_defaults($udom);
1404:22): my $remoterev = &get_server_loncaparev('',$home);
1405:22): $canswitch = &can_host_session($udom,$home,$remoterev,
1406:22): $udomdefaults{'remotesessions'},
1407:22): $defdomdefaults{'hostedsessions'});
1408:22): }
1409:22): return $canswitch;
1410:22): }
1411:22):
1.1073 raeburn 1412: sub can_host_session {
1.1074 raeburn 1413: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073 raeburn 1414: my $canhost = 1;
1.1074 raeburn 1415: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073 raeburn 1416: if (ref($remotesessions) eq 'HASH') {
1417: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074 raeburn 1418: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073 raeburn 1419: $canhost = 0;
1420: } else {
1421: $canhost = 1;
1422: }
1423: }
1424: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074 raeburn 1425: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073 raeburn 1426: $canhost = 1;
1427: } else {
1428: $canhost = 0;
1429: }
1430: }
1431: if ($canhost) {
1432: if ($remotesessions->{'version'} ne '') {
1433: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1434: if ($reqmajor ne '' && $reqminor ne '') {
1435: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1436: my $major = $1;
1437: my $minor = $2;
1438: if (($major < $reqmajor ) ||
1439: (($major == $reqmajor) && ($minor < $reqminor))) {
1440: $canhost = 0;
1441: }
1442: } else {
1443: $canhost = 0;
1444: }
1445: }
1446: }
1447: }
1448: }
1449: if ($canhost) {
1450: if (ref($hostedsessions) eq 'HASH') {
1.1120 raeburn 1451: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1452: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073 raeburn 1453: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120 raeburn 1454: if (($uint_dom ne '') &&
1455: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073 raeburn 1456: $canhost = 0;
1457: } else {
1458: $canhost = 1;
1459: }
1460: }
1461: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120 raeburn 1462: if (($uint_dom ne '') &&
1463: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073 raeburn 1464: $canhost = 1;
1465: } else {
1466: $canhost = 0;
1467: }
1468: }
1469: }
1470: }
1471: return $canhost;
1472: }
1473:
1.1083 raeburn 1474: sub spare_can_host {
1475: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1476: my $canhost=1;
1.1172.2.62 raeburn 1477: my $try_server_hostname = &hostname($try_server);
1478: my $serverhomeID = &get_server_homeID($try_server_hostname);
1479: my $serverhomedom = &host_domain($serverhomeID);
1480: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1481: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1482: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1483: $canhost = 0;
1484: }
1485: }
1.1172.2.136 raeburn 1486: if ($canhost) {
1487: if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
1488: if ($defdomdefaults{'offloadoth'}{$try_server}) {
1489: unless (&shared_institution($udom,$try_server)) {
1490: $canhost = 0;
1491: }
1492: }
1493: }
1494: }
1.1172.2.62 raeburn 1495: if (($canhost) && ($uint_dom)) {
1496: my @intdoms;
1497: my $internet_names = &get_internet_names($try_server);
1498: if (ref($internet_names) eq 'ARRAY') {
1499: @intdoms = @{$internet_names};
1500: }
1501: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1502: my $remoterev = &get_server_loncaparev(undef,$try_server);
1503: $canhost = &can_host_session($udom,$try_server,$remoterev,
1504: $remotesessions,
1505: $defdomdefaults{'hostedsessions'});
1506: }
1.1083 raeburn 1507: }
1508: return $canhost;
1509: }
1510:
1.1123 raeburn 1511: sub this_host_spares {
1512: my ($dom) = @_;
1.1126 raeburn 1513: my ($dom_in_use,$lonhost_in_use,$result);
1.1123 raeburn 1514: my @hosts = ¤t_machine_ids();
1515: foreach my $lonhost (@hosts) {
1516: if (&host_domain($lonhost) eq $dom) {
1.1126 raeburn 1517: $dom_in_use = $dom;
1518: $lonhost_in_use = $lonhost;
1.1123 raeburn 1519: last;
1520: }
1521: }
1.1126 raeburn 1522: if ($dom_in_use ne '') {
1523: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1524: }
1525: if (ref($result) ne 'HASH') {
1526: $lonhost_in_use = $perlvar{'lonHostID'};
1527: $dom_in_use = &host_domain($lonhost_in_use);
1528: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1529: if (ref($result) ne 'HASH') {
1530: $result = \%spareid;
1531: }
1532: }
1533: return $result;
1534: }
1535:
1536: sub spares_for_offload {
1537: my ($dom_in_use,$lonhost_in_use) = @_;
1538: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123 raeburn 1539: if (defined($cached)) {
1540: return $result;
1541: } else {
1.1126 raeburn 1542: my $cachetime = 60*60*24;
1543: my %domconfig =
1544: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1545: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1546: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1547: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1548: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123 raeburn 1549: }
1550: }
1551: }
1552: }
1.1126 raeburn 1553: return;
1.1123 raeburn 1554: }
1555:
1.1129 raeburn 1556: sub get_lonbalancer_config {
1557: my ($servers) = @_;
1558: my ($currbalancer,$currtargets);
1559: if (ref($servers) eq 'HASH') {
1560: foreach my $server (keys(%{$servers})) {
1561: my %what = (
1562: spareid => 1,
1563: perlvar => 1,
1564: );
1565: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1566: if ($result eq 'ok') {
1567: if (ref($returnhash) eq 'HASH') {
1568: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1569: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1570: $currbalancer = $server;
1571: $currtargets = {};
1572: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1573: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1574: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1575: }
1576: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1577: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1578: }
1579: }
1580: last;
1581: }
1582: }
1583: }
1584: }
1585: }
1586: }
1587: return ($currbalancer,$currtargets);
1588: }
1589:
1590: sub check_loadbalancing {
1.1172.2.88 raeburn 1591: my ($uname,$udom,$caller) = @_;
1.1172.2.12 raeburn 1592: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1172.2.107 raeburn 1593: $rule_in_effect,$offloadto,$otherserver,$setcookie);
1.1129 raeburn 1594: my $lonhost = $perlvar{'lonHostID'};
1.1172.2.4 raeburn 1595: my @hosts = ¤t_machine_ids();
1.1129 raeburn 1596: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1597: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1598: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1599: my $serverhomedom = &host_domain($lonhost);
1.1172.2.75 raeburn 1600: my $domneedscache;
1.1129 raeburn 1601: my $cachetime = 60*60*24;
1602:
1603: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1604: $dom_in_use = $udom;
1605: $homeintdom = 1;
1606: } else {
1607: $dom_in_use = $serverhomedom;
1608: }
1609: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1610: unless (defined($cached)) {
1611: my %domconfig =
1612: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1613: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130 raeburn 1614: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1172.2.75 raeburn 1615: } else {
1616: $domneedscache = $dom_in_use;
1.1129 raeburn 1617: }
1618: }
1619: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1620: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1621: &check_balancer_result($result,@hosts);
1.1129 raeburn 1622: if ($is_balancer) {
1623: if (ref($currrules) eq 'HASH') {
1624: if ($homeintdom) {
1625: if ($uname ne '') {
1626: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1627: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1628: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1629: $rule_in_effect = $currrules->{'_LC_author'};
1630: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1631: $rule_in_effect = $currrules->{'_LC_adv'}
1632: }
1633: }
1634: if ($rule_in_effect eq '') {
1635: my %userenv = &userenvironment($udom,$uname,'inststatus');
1636: if ($userenv{'inststatus'} ne '') {
1637: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1638: my ($othertitle,$usertypes,$types) =
1639: &Apache::loncommon::sorted_inst_types($udom);
1640: if (ref($types) eq 'ARRAY') {
1641: foreach my $type (@{$types}) {
1642: if (grep(/^\Q$type\E$/,@statuses)) {
1643: if (exists($currrules->{$type})) {
1644: $rule_in_effect = $currrules->{$type};
1645: }
1646: }
1647: }
1648: }
1649: } else {
1650: if (exists($currrules->{'default'})) {
1651: $rule_in_effect = $currrules->{'default'};
1652: }
1653: }
1654: }
1655: } else {
1656: if (exists($currrules->{'default'})) {
1657: $rule_in_effect = $currrules->{'default'};
1658: }
1659: }
1660: } else {
1661: if ($currrules->{'_LC_external'} ne '') {
1662: $rule_in_effect = $currrules->{'_LC_external'};
1663: }
1664: }
1665: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1666: $uname,$udom);
1667: }
1668: }
1669: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1172.2.35 raeburn 1670: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129 raeburn 1671: unless (defined($cached)) {
1672: my %domconfig =
1673: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1674: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1172.2.75 raeburn 1675: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1676: } else {
1677: $domneedscache = $serverhomedom;
1.1129 raeburn 1678: }
1679: }
1680: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1681: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1682: &check_balancer_result($result,@hosts);
1683: if ($is_balancer) {
1.1129 raeburn 1684: if (ref($currrules) eq 'HASH') {
1685: if ($currrules->{'_LC_internetdom'} ne '') {
1686: $rule_in_effect = $currrules->{'_LC_internetdom'};
1687: }
1688: }
1689: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1690: $uname,$udom);
1691: }
1692: } else {
1693: if ($perlvar{'lonBalancer'} eq 'yes') {
1694: $is_balancer = 1;
1695: $offloadto = &this_host_spares($dom_in_use);
1696: }
1.1172.2.75 raeburn 1697: unless (defined($cached)) {
1698: $domneedscache = $serverhomedom;
1699: }
1.1129 raeburn 1700: }
1701: } else {
1702: if ($perlvar{'lonBalancer'} eq 'yes') {
1703: $is_balancer = 1;
1704: $offloadto = &this_host_spares($dom_in_use);
1705: }
1.1172.2.75 raeburn 1706: unless (defined($cached)) {
1707: $domneedscache = $serverhomedom;
1708: }
1709: }
1710: if ($domneedscache) {
1711: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129 raeburn 1712: }
1.1172.2.130 raeburn 1713: if (($is_balancer) && ($caller ne 'switchserver')) {
1.1172.2.5 raeburn 1714: my $lowest_load = 30000;
1715: if (ref($offloadto) eq 'HASH') {
1716: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1717: foreach my $try_server (@{$offloadto->{'primary'}}) {
1718: ($otherserver,$lowest_load) =
1719: &compare_server_load($try_server,$otherserver,$lowest_load);
1720: }
1.1129 raeburn 1721: }
1.1172.2.5 raeburn 1722: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129 raeburn 1723:
1.1172.2.5 raeburn 1724: if (!$found_server) {
1725: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1726: foreach my $try_server (@{$offloadto->{'default'}}) {
1727: ($otherserver,$lowest_load) =
1728: &compare_server_load($try_server,$otherserver,$lowest_load);
1729: }
1730: }
1731: }
1732: } elsif (ref($offloadto) eq 'ARRAY') {
1733: if (@{$offloadto} == 1) {
1734: $otherserver = $offloadto->[0];
1735: } elsif (@{$offloadto} > 1) {
1736: foreach my $try_server (@{$offloadto}) {
1.1129 raeburn 1737: ($otherserver,$lowest_load) =
1738: &compare_server_load($try_server,$otherserver,$lowest_load);
1739: }
1740: }
1741: }
1.1172.2.88 raeburn 1742: unless ($caller eq 'login') {
1743: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
1744: $is_balancer = 0;
1745: if ($uname ne '' && $udom ne '') {
1746: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1747: &appenv({'user.loadbalexempt' => $lonhost,
1748: 'user.loadbalcheck.time' => time});
1749: }
1.1172.2.5 raeburn 1750: }
1.1129 raeburn 1751: }
1752: }
1.1172.2.130 raeburn 1753: }
1754: if (($is_balancer) && (!$homeintdom)) {
1755: undef($setcookie);
1.1129 raeburn 1756: }
1.1172.2.107 raeburn 1757: return ($is_balancer,$otherserver,$setcookie);
1.1129 raeburn 1758: }
1759:
1.1172.2.12 raeburn 1760: sub check_balancer_result {
1761: my ($result,@hosts) = @_;
1.1172.2.107 raeburn 1762: my ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1763: if (ref($result) eq 'HASH') {
1764: if ($result->{'lonhost'} ne '') {
1765: my $currbalancer = $result->{'lonhost'};
1766: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1767: $is_balancer = 1;
1768: $currtargets = $result->{'targets'};
1769: $currrules = $result->{'rules'};
1770: }
1771: } else {
1772: foreach my $key (keys(%{$result})) {
1773: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1774: (ref($result->{$key}) eq 'HASH')) {
1775: $is_balancer = 1;
1776: $currrules = $result->{$key}{'rules'};
1777: $currtargets = $result->{$key}{'targets'};
1.1172.2.107 raeburn 1778: $setcookie = $result->{$key}{'cookie'};
1.1172.2.12 raeburn 1779: last;
1780: }
1781: }
1782: }
1783: }
1.1172.2.107 raeburn 1784: return ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1785: }
1786:
1.1129 raeburn 1787: sub get_loadbalancer_targets {
1788: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1789: my $offloadto;
1.1172.2.4 raeburn 1790: if ($rule_in_effect eq 'none') {
1791: return [$perlvar{'lonHostID'}];
1792: } elsif ($rule_in_effect eq '') {
1.1129 raeburn 1793: $offloadto = $currtargets;
1794: } else {
1795: if ($rule_in_effect eq 'homeserver') {
1796: my $homeserver = &homeserver($uname,$udom);
1797: if ($homeserver ne 'no_host') {
1798: $offloadto = [$homeserver];
1799: }
1800: } elsif ($rule_in_effect eq 'externalbalancer') {
1801: my %domconfig =
1802: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1803: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1804: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1805: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1806: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1807: }
1808: }
1809: } else {
1.1172.2.7 raeburn 1810: my %servers = &internet_dom_servers($udom);
1.1129 raeburn 1811: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1812: if (&hostname($remotebalancer) ne '') {
1813: $offloadto = [$remotebalancer];
1814: }
1815: }
1816: } elsif (&hostname($rule_in_effect) ne '') {
1817: $offloadto = [$rule_in_effect];
1818: }
1819: }
1820: return $offloadto;
1821: }
1822:
1.1127 raeburn 1823: sub internet_dom_servers {
1824: my ($dom) = @_;
1825: my (%uniqservers,%servers);
1826: my $primaryserver = &hostname(&domain($dom,'primary'));
1827: my @machinedoms = &machine_domains($primaryserver);
1828: foreach my $mdom (@machinedoms) {
1829: my %currservers = %servers;
1830: my %server = &get_servers($mdom);
1831: %servers = (%currservers,%server);
1832: }
1833: my %by_hostname;
1834: foreach my $id (keys(%servers)) {
1835: push(@{$by_hostname{$servers{$id}}},$id);
1836: }
1837: foreach my $hostname (sort(keys(%by_hostname))) {
1838: if (@{$by_hostname{$hostname}} > 1) {
1839: my $match = 0;
1840: foreach my $id (@{$by_hostname{$hostname}}) {
1841: if (&host_domain($id) eq $dom) {
1842: $uniqservers{$id} = $hostname;
1843: $match = 1;
1844: }
1845: }
1846: unless ($match) {
1847: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1848: }
1849: } else {
1850: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1851: }
1852: }
1853: return %uniqservers;
1854: }
1855:
1.1 albertel 1856: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1857:
1.599 albertel 1858: my %homecache;
1.1 albertel 1859: sub homeserver {
1.230 stredwic 1860: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1861: my $index="$uname:$udom";
1.426 albertel 1862:
1.599 albertel 1863: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1864:
1865: my %servers = &get_servers($udom,'library');
1866: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1867: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1868: exists($badServerCache{$tryserver}));
1.841 albertel 1869:
1870: my $answer=reply("home:$udom:$uname",$tryserver);
1871: if ($answer eq 'found') {
1872: delete($badServerCache{$tryserver});
1873: return $homecache{$index}=$tryserver;
1874: } elsif ($answer eq 'no_host') {
1875: $badServerCache{$tryserver}=1;
1876: }
1.1 albertel 1877: }
1878: return 'no_host';
1.70 www 1879: }
1880:
1881: # ------------------------------------- Find the usernames behind a list of IDs
1882:
1883: sub idget {
1884: my ($udom,@ids)=@_;
1885: my %returnhash=();
1886:
1.841 albertel 1887: my %servers = &get_servers($udom,'library');
1888: foreach my $tryserver (keys(%servers)) {
1.1172.2.73 raeburn 1889: my $idlist=join('&', map { &escape($_); } @ids);
1.841 albertel 1890: $idlist=~tr/A-Z/a-z/;
1891: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1892: my @answer=();
1893: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1894: @answer=split(/\&/,$reply);
1895: } ;
1896: my $i;
1897: for ($i=0;$i<=$#ids;$i++) {
1898: if ($answer[$i]) {
1.1172.2.73 raeburn 1899: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1.841 albertel 1900: }
1901: }
1902: }
1.70 www 1903: return %returnhash;
1904: }
1905:
1906: # ------------------------------------- Find the IDs behind a list of usernames
1907:
1908: sub idrget {
1909: my ($udom,@unames)=@_;
1910: my %returnhash=();
1.800 albertel 1911: foreach my $uname (@unames) {
1912: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1913: }
1.70 www 1914: return %returnhash;
1915: }
1916:
1917: # ------------------------------- Store away a list of names and associated IDs
1918:
1919: sub idput {
1920: my ($udom,%ids)=@_;
1921: my %servers=();
1.800 albertel 1922: foreach my $uname (keys(%ids)) {
1923: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1924: my $uhom=&homeserver($uname,$udom);
1.70 www 1925: if ($uhom ne 'no_host') {
1.800 albertel 1926: my $id=&escape($ids{$uname});
1.70 www 1927: $id=~tr/A-Z/a-z/;
1.800 albertel 1928: my $esc_unam=&escape($uname);
1.70 www 1929: if ($servers{$uhom}) {
1.800 albertel 1930: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1931: } else {
1.800 albertel 1932: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1933: }
1934: }
1.191 harris41 1935: }
1.800 albertel 1936: foreach my $server (keys(%servers)) {
1937: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1938: }
1.344 www 1939: }
1940:
1.1172.2.30 raeburn 1941: # ---------------------------------------- Delete unwanted IDs from ids.db file
1942:
1943: sub iddel {
1944: my ($udom,$idshashref,$uhome)=@_;
1945: my %result=();
1946: unless (ref($idshashref) eq 'HASH') {
1947: return %result;
1948: }
1949: my %servers=();
1950: while (my ($id,$uname) = each(%{$idshashref})) {
1951: my $uhom;
1952: if ($uhome) {
1953: $uhom = $uhome;
1954: } else {
1955: $uhom=&homeserver($uname,$udom);
1956: }
1957: if ($uhom ne 'no_host') {
1958: if ($servers{$uhom}) {
1959: $servers{$uhom}.='&'.&escape($id);
1960: } else {
1961: $servers{$uhom}=&escape($id);
1962: }
1963: }
1964: }
1965: foreach my $server (keys(%servers)) {
1966: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1967: }
1968: return %result;
1969: }
1970:
1.1023 raeburn 1971: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1972: sub dump_dom {
1.1165 droeschl 1973: my ($namespace, $udom, $regexp) = @_;
1974:
1975: $udom ||= $env{'user.domain'};
1976:
1977: return () unless $udom;
1978:
1979: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012 raeburn 1980: }
1981:
1.1023 raeburn 1982: # ------------------------------------------ get items from domain db files
1.806 raeburn 1983:
1984: sub get_dom {
1.1172.2.146. .1(raebu 1985:22): my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
1.1172.2.57 raeburn 1986: return if ($udom eq 'public');
1.806 raeburn 1987: my $items='';
1988: foreach my $item (@$storearr) {
1989: $items.=&escape($item).'&';
1990: }
1991: $items=~s/\&$//;
1.860 raeburn 1992: if (!$udom) {
1993: $udom=$env{'user.domain'};
1.1172.2.57 raeburn 1994: return if ($udom eq 'public');
1.860 raeburn 1995: if (defined(&domain($udom,'primary'))) {
1996: $uhome=&domain($udom,'primary');
1997: } else {
1.874 albertel 1998: undef($uhome);
1.860 raeburn 1999: }
2000: } else {
2001: if (!$uhome) {
2002: if (defined(&domain($udom,'primary'))) {
2003: $uhome=&domain($udom,'primary');
2004: }
2005: }
2006: }
2007: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1172.2.139 raeburn 2008: my $rep;
2009: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
2010: # domain information is hosted on this machine
2011: $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
1.1172.2.146. .1(raebu 2012:22): } else {
2013:22): if ($encrypt) {
2014:22): $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
2015:22): } else {
2016:22): $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
2017:22): }
1.1172.2.139 raeburn 2018: }
1.866 raeburn 2019: my %returnhash;
1.875 albertel 2020: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 2021: return %returnhash;
2022: }
1.806 raeburn 2023: my @pairs=split(/\&/,$rep);
2024: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
2025: return @pairs;
2026: }
2027: my $i=0;
2028: foreach my $item (@$storearr) {
2029: $returnhash{$item}=&thaw_unescape($pairs[$i]);
2030: $i++;
2031: }
2032: return %returnhash;
2033: } else {
1.880 banghart 2034: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 2035: }
2036: }
2037:
2038: # -------------------------------------------- put items in domain db files
2039:
2040: sub put_dom {
1.1172.2.146. .1(raebu 2041:22): my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
1.860 raeburn 2042: if (!$udom) {
2043: $udom=$env{'user.domain'};
2044: if (defined(&domain($udom,'primary'))) {
2045: $uhome=&domain($udom,'primary');
2046: } else {
1.874 albertel 2047: undef($uhome);
1.860 raeburn 2048: }
2049: } else {
2050: if (!$uhome) {
2051: if (defined(&domain($udom,'primary'))) {
2052: $uhome=&domain($udom,'primary');
2053: }
2054: }
2055: }
2056: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 2057: my $items='';
2058: foreach my $item (keys(%$storehash)) {
2059: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
2060: }
2061: $items=~s/\&$//;
1.1172.2.146. .1(raebu 2062:22): if ($encrypt) {
2063:22): return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
2064:22): } else {
2065:22): return &reply("putdom:$udom:$namespace:$items",$uhome);
2066:22): }
1.806 raeburn 2067: } else {
1.860 raeburn 2068: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 2069: }
2070: }
2071:
1.1023 raeburn 2072: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 2073: sub newput_dom {
1.1023 raeburn 2074: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 2075: my $result;
2076: if (!$udom) {
2077: $udom=$env{'user.domain'};
2078: }
1.1023 raeburn 2079: if ($udom) {
2080: my $uname = &get_domainconfiguser($udom);
2081: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 2082: }
2083: return $result;
2084: }
2085:
1.1023 raeburn 2086: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 2087: sub del_dom {
1.1023 raeburn 2088: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 2089: if (ref($storearr) eq 'ARRAY') {
2090: if (!$udom) {
2091: $udom=$env{'user.domain'};
2092: }
1.1023 raeburn 2093: if ($udom) {
2094: my $uname = &get_domainconfiguser($udom);
2095: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 2096: }
2097: }
2098: }
2099:
1.1172.2.146. .4(raebu 2100:22): sub store_dom {
2101:22): my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
2102:22): $$storehash{'ip'}=&get_requestor_ip();
2103:22): $$storehash{'host'}=$perlvar{'lonHostID'};
2104:22): my $namevalue='';
2105:22): foreach my $key (keys(%{$storehash})) {
2106:22): $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
2107:22): }
2108:22): $namevalue=~s/\&$//;
2109:22): if (grep { $_ eq $home } current_machine_ids()) {
2110:22): return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
2111:22): } else {
2112:22): if ($namespace eq 'private') {
2113:22): return 'refused';
2114:22): } elsif ($encrypt) {
2115:22): return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
2116:22): } else {
2117:22): return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
2118:22): }
2119:22): }
2120:22): }
2121:22):
2122:22): sub restore_dom {
2123:22): my ($id,$namespace,$dom,$home,$encrypt) = @_;
2124:22): my $answer;
2125:22): if (grep { $_ eq $home } current_machine_ids()) {
2126:22): $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
2127:22): } elsif ($namespace ne 'private') {
2128:22): if ($encrypt) {
2129:22): $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
2130:22): } else {
2131:22): $answer=&reply("restoredom:$dom:$namespace:$id",$home);
2132:22): }
2133:22): }
2134:22): my %returnhash=();
2135:22): unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') ||
2136:22): ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
2137:22): foreach my $line (split(/\&/,$answer)) {
2138:22): my ($name,$value)=split(/\=/,$line);
2139:22): $returnhash{&unescape($name)}=&thaw_unescape($value);
2140:22): }
2141:22): my $version;
2142:22): for ($version=1;$version<=$returnhash{'version'};$version++) {
2143:22): foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
2144:22): $returnhash{$item}=$returnhash{$version.':'.$item};
2145:22): }
2146:22): }
2147:22): }
2148:22): return %returnhash;
2149:22): }
2150:22):
1.1023 raeburn 2151: # ----------------------------------construct domainconfig user for a domain
2152: sub get_domainconfiguser {
2153: my ($udom) = @_;
2154: return $udom.'-domainconfig';
2155: }
2156:
1.837 raeburn 2157: sub retrieve_inst_usertypes {
2158: my ($udom) = @_;
2159: my (%returnhash,@order);
1.989 raeburn 2160: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
2161: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
2162: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1172.2.44 raeburn 2163: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989 raeburn 2164: } else {
2165: if (defined(&domain($udom,'primary'))) {
2166: my $uhome=&domain($udom,'primary');
2167: my $rep=&reply("inst_usertypes:$udom",$uhome);
2168: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1172.2.44 raeburn 2169: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989 raeburn 2170: return (\%returnhash,\@order);
2171: }
2172: my ($hashitems,$orderitems) = split(/:/,$rep);
2173: my @pairs=split(/\&/,$hashitems);
2174: foreach my $item (@pairs) {
2175: my ($key,$value)=split(/=/,$item,2);
2176: $key = &unescape($key);
2177: next if ($key =~ /^error: 2 /);
2178: $returnhash{$key}=&thaw_unescape($value);
2179: }
2180: my @esc_order = split(/\&/,$orderitems);
2181: foreach my $item (@esc_order) {
2182: push(@order,&unescape($item));
2183: }
2184: } else {
1.1172.2.44 raeburn 2185: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837 raeburn 2186: }
1.1172.2.44 raeburn 2187: return (\%returnhash,\@order);
1.837 raeburn 2188: }
2189: }
2190:
1.868 raeburn 2191: sub is_domainimage {
2192: my ($url) = @_;
1.1172.2.142 raeburn 2193: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
1.868 raeburn 2194: if (&domain($1) ne '') {
2195: return '1';
2196: }
2197: }
2198: return;
2199: }
2200:
1.899 raeburn 2201: sub inst_directory_query {
2202: my ($srch) = @_;
2203: my $udom = $srch->{'srchdomain'};
2204: my %results;
2205: my $homeserver = &domain($udom,'primary');
1.909 raeburn 2206: my $outcome;
1.899 raeburn 2207: if ($homeserver ne '') {
1.1172.2.116 raeburn 2208: unless ($homeserver eq $perlvar{'lonHostID'}) {
2209: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2210: my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1172.2.116 raeburn 2211: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
2212: if (($major eq '' && $minor eq '') || ($major < 2) ||
2213: (($major == 2) && ($minor < 11)) ||
2214: (($major == 2) && ($minor == 11) && ($subver < 3))) {
2215: return;
2216: }
2217: }
2218: }
1.904 albertel 2219: my $queryid=&reply("querysend:instdirsearch:".
2220: &escape($srch->{'srchby'}).':'.
2221: &escape($srch->{'srchterm'}).':'.
2222: &escape($srch->{'srchtype'}),$homeserver);
2223: my $host=&hostname($homeserver);
2224: if ($queryid !~/^\Q$host\E\_/) {
1.1172.2.96 raeburn 2225: &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904 albertel 2226: return;
2227: }
2228: my $response = &get_query_reply($queryid);
2229: my $maxtries = 5;
2230: my $tries = 1;
2231: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2232: $response = &get_query_reply($queryid);
2233: $tries ++;
2234: }
2235:
2236: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 2237: if ($response eq 'unavailable') {
2238: $outcome = $response;
2239: } else {
2240: $outcome = 'ok';
2241: my @matches = split(/\n/,$response);
2242: foreach my $match (@matches) {
2243: my ($key,$value) = split(/=/,$match);
2244: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
2245: }
1.899 raeburn 2246: }
2247: }
2248: }
1.909 raeburn 2249: return ($outcome,%results);
1.899 raeburn 2250: }
2251:
2252: sub usersearch {
2253: my ($srch) = @_;
2254: my $dom = $srch->{'srchdomain'};
2255: my %results;
2256: my %libserv = &all_library();
2257: my $query = 'usersearch';
2258: foreach my $tryserver (keys(%libserv)) {
2259: if (&host_domain($tryserver) eq $dom) {
1.1172.2.116 raeburn 2260: unless ($tryserver eq $perlvar{'lonHostID'}) {
2261: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2262: my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1172.2.119 raeburn 2263: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
1.1172.2.116 raeburn 2264: next if (($major eq '' && $minor eq '') || ($major < 2) ||
2265: (($major == 2) && ($minor < 11)) ||
2266: (($major == 2) && ($minor == 11) && ($subver < 3)));
2267: }
2268: }
1.899 raeburn 2269: my $host=&hostname($tryserver);
2270: my $queryid=
1.911 raeburn 2271: &reply("querysend:".&escape($query).':'.
2272: &escape($srch->{'srchby'}).':'.
1.899 raeburn 2273: &escape($srch->{'srchtype'}).':'.
2274: &escape($srch->{'srchterm'}),$tryserver);
2275: if ($queryid !~/^\Q$host\E\_/) {
2276: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 2277: next;
1.899 raeburn 2278: }
2279: my $reply = &get_query_reply($queryid);
2280: my $maxtries = 1;
2281: my $tries = 1;
2282: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2283: $reply = &get_query_reply($queryid);
2284: $tries ++;
2285: }
2286: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2287: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2288: } else {
1.911 raeburn 2289: my @matches;
2290: if ($reply =~ /\n/) {
2291: @matches = split(/\n/,$reply);
2292: } else {
2293: @matches = split(/\&/,$reply);
2294: }
1.899 raeburn 2295: foreach my $match (@matches) {
2296: my ($uname,$udom,%userhash);
1.911 raeburn 2297: foreach my $entry (split(/:/,$match)) {
2298: my ($key,$value) =
2299: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 2300: $userhash{$key} = $value;
2301: if ($key eq 'username') {
2302: $uname = $value;
2303: } elsif ($key eq 'domain') {
2304: $udom = $value;
1.911 raeburn 2305: }
1.899 raeburn 2306: }
2307: $results{$uname.':'.$udom} = \%userhash;
2308: }
2309: }
2310: }
2311: }
2312: return %results;
2313: }
2314:
1.912 raeburn 2315: sub get_instuser {
2316: my ($udom,$uname,$id) = @_;
2317: my $homeserver = &domain($udom,'primary');
2318: my ($outcome,%results);
2319: if ($homeserver ne '') {
2320: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2321: &escape($id).':'.&escape($udom),$homeserver);
2322: my $host=&hostname($homeserver);
2323: if ($queryid !~/^\Q$host\E\_/) {
2324: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2325: return;
2326: }
2327: my $response = &get_query_reply($queryid);
2328: my $maxtries = 5;
2329: my $tries = 1;
2330: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2331: $response = &get_query_reply($queryid);
2332: $tries ++;
2333: }
2334: if (!&error($response) && $response ne 'refused') {
2335: if ($response eq 'unavailable') {
2336: $outcome = $response;
2337: } else {
2338: $outcome = 'ok';
2339: my @matches = split(/\n/,$response);
2340: foreach my $match (@matches) {
2341: my ($key,$value) = split(/=/,$match);
2342: $results{&unescape($key)} = &thaw_unescape($value);
2343: }
2344: }
2345: }
2346: }
2347: my %userinfo;
2348: if (ref($results{$uname}) eq 'HASH') {
2349: %userinfo = %{$results{$uname}};
2350: }
2351: return ($outcome,%userinfo);
2352: }
2353:
1.1172.2.69 raeburn 2354: sub get_multiple_instusers {
2355: my ($udom,$users,$caller) = @_;
2356: my ($outcome,$results);
2357: if (ref($users) eq 'HASH') {
2358: my $count = keys(%{$users});
2359: my $requested = &freeze_escape($users);
2360: my $homeserver = &domain($udom,'primary');
2361: if ($homeserver ne '') {
2362: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2363: my $host=&hostname($homeserver);
2364: if ($queryid !~/^\Q$host\E\_/) {
2365: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2366: ' for host: '.$homeserver.'in domain '.$udom);
2367: return ($outcome,$results);
2368: }
2369: my $response = &get_query_reply($queryid);
2370: my $maxtries = 5;
2371: if ($count > 100) {
2372: $maxtries = 1+int($count/20);
2373: }
2374: my $tries = 1;
2375: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2376: $response = &get_query_reply($queryid);
2377: $tries ++;
2378: }
2379: if ($response eq '') {
2380: $results = {};
2381: foreach my $key (keys(%{$users})) {
2382: my ($uname,$id);
2383: if ($caller eq 'id') {
2384: $id = $key;
2385: } else {
2386: $uname = $key;
2387: }
2388: my ($resp,%info) = &get_instuser($udom,$uname,$id);
2389: $outcome = $resp;
2390: if ($resp eq 'ok') {
2391: %{$results} = (%{$results}, %info);
2392: } else {
2393: last;
2394: }
2395: }
2396: } elsif(!&error($response) && ($response ne 'refused')) {
2397: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2398: $outcome = $response;
2399: } else {
2400: ($outcome,my $userdata) = split(/=/,$response,2);
2401: if ($outcome eq 'ok') {
2402: $results = &thaw_unescape($userdata);
2403: }
2404: }
2405: }
2406: }
2407: }
2408: return ($outcome,$results);
2409: }
2410:
1.912 raeburn 2411: sub inst_rulecheck {
1.923 raeburn 2412: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 2413: my %returnhash;
2414: if ($udom ne '') {
2415: if (ref($rules) eq 'ARRAY') {
2416: @{$rules} = map {&escape($_);} (@{$rules});
2417: my $rulestr = join(':',@{$rules});
2418: my $homeserver=&domain($udom,'primary');
2419: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2420: my $response;
2421: if ($item eq 'username') {
2422: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2423: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 2424: $homeserver));
1.923 raeburn 2425: } elsif ($item eq 'id') {
2426: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2427: ':'.&escape($id).':'.$rulestr,
2428: $homeserver));
1.1172.2.146. .5(raebu 2429:22): } elsif ($item eq 'unamemap') {
2430:22): $response=&unescape(&reply('instunamemapcheck:'.
2431:22): &escape($udom).':'.&escape($uname).
2432:22): ':'.$rulestr,$homeserver));
1.945 raeburn 2433: } elsif ($item eq 'selfcreate') {
2434: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 2435: &escape($udom).':'.&escape($uname).
2436: ':'.$rulestr,$homeserver));
1.923 raeburn 2437: }
1.912 raeburn 2438: if ($response ne 'refused') {
2439: my @pairs=split(/\&/,$response);
2440: foreach my $item (@pairs) {
2441: my ($key,$value)=split(/=/,$item,2);
2442: $key = &unescape($key);
2443: next if ($key =~ /^error: 2 /);
2444: $returnhash{$key}=&thaw_unescape($value);
2445: }
2446: }
2447: }
2448: }
2449: }
2450: return %returnhash;
2451: }
2452:
2453: sub inst_userrules {
1.923 raeburn 2454: my ($udom,$check) = @_;
1.912 raeburn 2455: my (%ruleshash,@ruleorder);
2456: if ($udom ne '') {
2457: my $homeserver=&domain($udom,'primary');
2458: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2459: my $response;
2460: if ($check eq 'id') {
2461: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 2462: $homeserver);
1.943 raeburn 2463: } elsif ($check eq 'email') {
2464: $response=&reply('instemailrules:'.&escape($udom),
2465: $homeserver);
1.1172.2.146. .5(raebu 2466:22): } elsif ($check eq 'unamemap') {
2467:22): $response=&reply('unamemaprules:'.&escape($udom),
2468:22): $homeserver);
1.923 raeburn 2469: } else {
2470: $response=&reply('instuserrules:'.&escape($udom),
2471: $homeserver);
2472: }
1.912 raeburn 2473: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 2474: ($response ne 'unknown_cmd') &&
1.912 raeburn 2475: ($response ne 'no_such_host')) {
2476: my ($hashitems,$orderitems) = split(/:/,$response);
2477: my @pairs=split(/\&/,$hashitems);
2478: foreach my $item (@pairs) {
2479: my ($key,$value)=split(/=/,$item,2);
2480: $key = &unescape($key);
2481: next if ($key =~ /^error: 2 /);
2482: $ruleshash{$key}=&thaw_unescape($value);
2483: }
2484: my @esc_order = split(/\&/,$orderitems);
2485: foreach my $item (@esc_order) {
2486: push(@ruleorder,&unescape($item));
2487: }
2488: }
2489: }
2490: }
2491: return (\%ruleshash,\@ruleorder);
2492: }
2493:
1.976 raeburn 2494: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 2495:
2496: sub get_domain_defaults {
1.1172.2.35 raeburn 2497: my ($domain,$ignore_cache) = @_;
2498: return if (($domain eq '') || ($domain eq 'public'));
1.943 raeburn 2499: my $cachetime = 60*60*24;
1.1172.2.35 raeburn 2500: unless ($ignore_cache) {
2501: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2502: if (defined($cached)) {
2503: if (ref($result) eq 'HASH') {
2504: return %{$result};
2505: }
1.943 raeburn 2506: }
2507: }
2508: my %domdefaults;
2509: my %domconfig =
1.989 raeburn 2510: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 2511: 'requestcourses','inststatus',
1.1172.2.9 raeburn 2512: 'coursedefaults','usersessions',
1.1172.2.146. .18(raeb 2513:-24): 'requestauthor','authordefaults',
2514:-24): 'selfenrollment','coursecategories',
2515:-24): 'autoenroll','helpsettings',
2516:-24): 'wafproxy','ltisec','toolsec',
2517:-24): 'domexttool','exttool'],$domain);
1.1172.2.41 raeburn 2518: my @coursetypes = ('official','unofficial','community','textbook');
1.943 raeburn 2519: if (ref($domconfig{'defaults'}) eq 'HASH') {
2520: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2521: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2522: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 2523: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 2524: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147 raeburn 2525: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1172.2.146. .12(raeb 2526:-23): $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
2527:-23): $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
1.1172.2.91 raeburn 2528: $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
2529: $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
2530: $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.1172.2.146. .5(raebu 2531:22): $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
1.943 raeburn 2532: } else {
2533: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2534: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2535: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2536: }
1.976 raeburn 2537: if (ref($domconfig{'quotas'}) eq 'HASH') {
2538: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2539: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2540: } else {
2541: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1172.2.29 raeburn 2542: }
1.1172.2.146. .23(raeb 2543:-24): my @usertools = ('aboutme','blog','webdav','portfolio','portaccess');
1.976 raeburn 2544: foreach my $item (@usertools) {
2545: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2546: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2547: }
2548: }
1.1172.2.29 raeburn 2549: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2550: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2551: }
1.976 raeburn 2552: }
1.985 raeburn 2553: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1172.2.37 raeburn 2554: foreach my $item ('official','unofficial','community','textbook') {
1.985 raeburn 2555: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2556: }
2557: }
1.1172.2.146. .18(raeb 2558:-24): if (ref($domconfig{'authordefaults'}) eq 'HASH') {
.23(raeb 2559:-24): foreach my $item ('nocodemirror','copyright','sourceavail','domcoordacc','editors','archive') {
.18(raeb 2560:-24): if ($item eq 'editors') {
2561:-24): if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
2562:-24): $domdefaults{$item} = join(',',@{$domconfig{'authordefaults'}{'editors'}});
2563:-24): }
2564:-24): } else {
2565:-24): $domdefaults{$item} = $domconfig{'authordefaults'}{$item};
2566:-24): }
2567:-24): }
2568:-24): }
1.1172.2.9 raeburn 2569: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2570: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2571: }
1.989 raeburn 2572: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1172.2.44 raeburn 2573: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989 raeburn 2574: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2575: }
2576: }
1.1047 raeburn 2577: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1172.2.60 raeburn 2578: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
1.1172.2.144 raeburn 2579: $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
1.1172.2.60 raeburn 2580: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2581: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2582: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2583: }
1.1172.2.146. .25(raeb 2584:-24): if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
2585:-24): $domdefaults{'crseditors'}=join(',',@{$domconfig{'coursedefaults'}{'crseditors'}});
2586:-24): }
1.1172.2.41 raeburn 2587: foreach my $type (@coursetypes) {
2588: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2589: unless ($type eq 'community') {
2590: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2591: }
2592: }
2593: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2594: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2595: }
1.1172.2.146. .14(raeb 2596:-23): if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
2597:-23): $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
2598:-23): }
1.1172.2.60 raeburn 2599: if ($domdefaults{'postsubmit'} eq 'on') {
2600: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2601: $domdefaults{$type.'postsubtimeout'} =
2602: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2603: }
2604: }
1.1172.2.146. .13(raeb 2605:-23): if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
2606:-23): $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
2607:-23): } else {
2608:-23): $domdefaults{$type.'domexttool'} = 1;
2609:-23): }
2610:-23): if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
2611:-23): $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
2612:-23): } else {
2613:-23): $domdefaults{$type.'exttool'} = 0;
2614:-23): }
1.1172.2.30 raeburn 2615: }
1.1172.2.67 raeburn 2616: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2617: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2618: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2619: if (@clonecodes) {
2620: $domdefaults{'canclone'} = join('+',@clonecodes);
2621: }
2622: }
2623: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2624: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2625: }
1.1172.2.103 raeburn 2626: if ($domconfig{'coursedefaults'}{'texengine'}) {
2627: $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
2628: }
1.1172.2.146. .3(raebu 2629:22): if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
2630:22): $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
2631:22): }
1.1047 raeburn 2632: }
1.1073 raeburn 2633: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2634: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2635: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2636: }
2637: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2638: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2639: }
1.1172.2.62 raeburn 2640: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2641: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2642: }
1.1172.2.137 raeburn 2643: if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1172.2.136 raeburn 2644: $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
2645: }
1.1073 raeburn 2646: }
1.1172.2.41 raeburn 2647: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2648: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2649: my @settings = ('types','registered','enroll_dates','access_dates','section',
2650: 'approval','limit');
2651: foreach my $type (@coursetypes) {
2652: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2653: my @mgrdc = ();
2654: foreach my $item (@settings) {
2655: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2656: push(@mgrdc,$item);
2657: }
2658: }
2659: if (@mgrdc) {
2660: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2661: }
2662: }
2663: }
2664: }
2665: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2666: foreach my $type (@coursetypes) {
2667: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2668: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2669: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2670: }
2671: }
2672: }
2673: }
2674: }
1.1172.2.46 raeburn 2675: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2676: $domdefaults{'catauth'} = 'std';
2677: $domdefaults{'catunauth'} = 'std';
2678: if ($domconfig{'coursecategories'}{'auth'}) {
2679: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2680: }
2681: if ($domconfig{'coursecategories'}{'unauth'}) {
2682: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2683: }
2684: }
1.1172.2.76 raeburn 2685: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2686: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
1.1172.2.146 raeburn 2687: $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
1.1172.2.76 raeburn 2688: }
1.1172.2.89 raeburn 2689: if (ref($domconfig{'helpsettings'}) eq 'HASH') {
2690: $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
2691: if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
2692: $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
2693: }
2694: }
1.1172.2.142 raeburn 2695: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
2696: foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
2697: if ($domconfig{'wafproxy'}{$item}) {
2698: $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
2699: }
2700: }
2701: }
1.1172.2.146. .4(raebu 2702:22): if (ref($domconfig{'ltisec'}) eq 'HASH') {
2703:22): if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
2704:22): $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
2705:22): $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
2706:22): $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
2707:22): }
2708:22): if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
2709:22): if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
.13(raeb 2710:-23): $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
2711:-23): }
2712:-23): }
.17(raeb 2713:-24): if (ref($domconfig{'ltisec'}{'suggested'}) eq 'HASH') {
2714:-24): my %suggestions = %{$domconfig{'ltisec'}{'suggested'}};
2715:-24): foreach my $item (keys(%{$domconfig{'ltisec'}{'suggested'}})) {
2716:-24): unless (ref($domconfig{'ltisec'}{'suggested'}{$item}) eq 'HASH') {
2717:-24): delete($suggestions{$item});
2718:-24): }
2719:-24): }
2720:-24): if (keys(%suggestions)) {
2721:-24): $domdefaults{'linkprotsuggested'} = \%suggestions;
2722:-24): }
2723:-24): }
.13(raeb 2724:-23): }
2725:-23): if (ref($domconfig{'toolsec'}) eq 'HASH') {
2726:-23): if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
2727:-23): $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
2728:-23): $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
2729:-23): }
2730:-23): if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
2731:-23): if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
2732:-23): $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
.4(raebu 2733:22): }
2734:22): }
2735:22): }
1.1172.2.23 raeburn 2736: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943 raeburn 2737: return %domdefaults;
2738: }
2739:
1.1172.2.106 raeburn 2740: sub get_dom_cats {
2741: my ($dom) = @_;
2742: return unless (&domain($dom));
2743: my ($cats,$cached)=&is_cached_new('cats',$dom);
2744: unless (defined($cached)) {
2745: my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
2746: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2747: if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
2748: %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
2749: } else {
2750: $cats = {};
2751: }
2752: } else {
2753: $cats = {};
2754: }
2755: &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
2756: }
2757: return $cats;
2758: }
2759:
2760: sub get_dom_instcats {
2761: my ($dom) = @_;
2762: return unless (&domain($dom));
2763: my ($instcats,$cached)=&is_cached_new('instcats',$dom);
2764: unless (defined($cached)) {
2765: my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
2766: my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
2767: if ($totcodes > 0) {
2768: my $caller = 'global';
2769: if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
2770: \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
2771: $instcats = {
1.1172.2.146. .10(raeb 2772:-23): totcodes => $totcodes,
1.1172.2.106 raeburn 2773: codes => \%codes,
2774: codetitles => \@codetitles,
2775: cat_titles => \%cat_titles,
2776: cat_order => \%cat_order,
2777: };
2778: &do_cache_new('instcats',$dom,$instcats,3600);
2779: }
2780: }
2781: }
2782: return $instcats;
2783: }
2784:
2785: sub retrieve_instcodes {
2786: my ($coursecodes,$dom) = @_;
2787: my $totcodes;
2788: my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
2789: foreach my $course (keys(%courses)) {
2790: if (ref($courses{$course}) eq 'HASH') {
2791: if ($courses{$course}{'inst_code'} ne '') {
2792: $$coursecodes{$course} = $courses{$course}{'inst_code'};
2793: $totcodes ++;
2794: }
2795: }
2796: }
2797: return $totcodes;
2798: }
2799:
1.1172.2.113 raeburn 2800: # --------------------------------------------- Get domain config for passwords
2801:
2802: sub get_passwdconf {
2803: my ($dom) = @_;
2804: my (%passwdconf,$gotconf,$lookup);
2805: my ($result,$cached)=&is_cached_new('passwdconf',$dom);
2806: if (defined($cached)) {
2807: if (ref($result) eq 'HASH') {
2808: %passwdconf = %{$result};
2809: $gotconf = 1;
2810: }
2811: }
2812: unless ($gotconf) {
2813: my %domconfig = &get_dom('configuration',['passwords'],$dom);
2814: if (ref($domconfig{'passwords'}) eq 'HASH') {
2815: %passwdconf = %{$domconfig{'passwords'}};
2816: }
2817: my $cachetime = 24*60*60;
2818: &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
2819: }
2820: return %passwdconf;
2821: }
2822:
1.1172.2.146. .1(raebu 2823:22): sub course_portal_url {
2824:22): my ($cnum,$cdom,$r) = @_;
2825:22): my $chome = &homeserver($cnum,$cdom);
2826:22): my $hostname = &hostname($chome);
2827:22): my $protocol = $protocol{$chome};
2828:22): $protocol = 'http' if ($protocol ne 'https');
2829:22): my %domdefaults = &get_domain_defaults($cdom);
2830:22): my $firsturl;
2831:22): if ($domdefaults{'portal_def'}) {
2832:22): $firsturl = $domdefaults{'portal_def'};
2833:22): } else {
2834:22): my $alias = &Apache::lonnet::use_proxy_alias($r,$chome);
2835:22): $hostname = $alias if ($alias ne '');
2836:22): $firsturl = $protocol.'://'.$hostname;
2837:22): }
2838:22): return $firsturl;
2839:22): }
2840:22):
.12(raeb 2841:-23): sub url_prefix {
2842:-23): my ($r,$dom,$home,$context) = @_;
2843:-23): my $prefix;
2844:-23): my %domdefs = &get_domain_defaults($dom);
2845:-23): if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
2846:-23): if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
2847:-23): $prefix = $1;
2848:-23): }
2849:-23): }
2850:-23): if ($prefix eq '') {
2851:-23): my $hostname = &hostname($home);
2852:-23): my $protocol = $protocol{$home};
2853:-23): $protocol = 'http' if ($protocol{$home} ne 'https');
2854:-23): my $alias = &use_proxy_alias($r,$home);
2855:-23): $hostname = $alias if ($alias ne '');
2856:-23): $prefix = $protocol.'://'.$hostname;
2857:-23): }
2858:-23): return $prefix;
2859:-23): }
2860:-23):
1.344 www 2861: # --------------------------------------------------- Assign a key to a student
2862:
2863: sub assign_access_key {
1.364 www 2864: #
2865: # a valid key looks like uname:udom#comments
2866: # comments are being appended
2867: #
1.498 www 2868: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2869: $kdom=
1.620 albertel 2870: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 2871: $knum=
1.620 albertel 2872: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 2873: $cdom=
1.620 albertel 2874: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2875: $cnum=
1.620 albertel 2876: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2877: $udom=$env{'user.name'} unless (defined($udom));
2878: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 2879: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 2880: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 2881: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 2882: # assigned to this person
2883: # - this should not happen,
1.345 www 2884: # unless something went wrong
2885: # the first time around
2886: # ready to assign
1.364 www 2887: $logentry=$1.'; '.$logentry;
1.496 www 2888: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 2889: $kdom,$knum) eq 'ok') {
1.345 www 2890: # key now belongs to user
1.346 www 2891: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 2892: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 2893: &appenv({'environment.'.$envkey => $ckey});
1.345 www 2894: return 'ok';
2895: } else {
2896: return
2897: 'error: Count not permanently assign key, will need to be re-entered later.';
2898: }
2899: } else {
2900: return 'error: Could not assign key, try again later.';
2901: }
1.364 www 2902: } elsif (!$existing{$ckey}) {
1.345 www 2903: # the key does not exist
2904: return 'error: The key does not exist';
2905: } else {
2906: # the key is somebody else's
2907: return 'error: The key is already in use';
2908: }
1.344 www 2909: }
2910:
1.364 www 2911: # ------------------------------------------ put an additional comment on a key
2912:
2913: sub comment_access_key {
2914: #
2915: # a valid key looks like uname:udom#comments
2916: # comments are being appended
2917: #
2918: my ($ckey,$cdom,$cnum,$logentry)=@_;
2919: $cdom=
1.620 albertel 2920: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 2921: $cnum=
1.620 albertel 2922: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 2923: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2924: if ($existing{$ckey}) {
2925: $existing{$ckey}.='; '.$logentry;
2926: # ready to assign
1.367 www 2927: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 2928: $cdom,$cnum) eq 'ok') {
2929: return 'ok';
2930: } else {
2931: return 'error: Count not store comment.';
2932: }
2933: } else {
2934: # the key does not exist
2935: return 'error: The key does not exist';
2936: }
2937: }
2938:
1.344 www 2939: # ------------------------------------------------------ Generate a set of keys
2940:
2941: sub generate_access_keys {
1.364 www 2942: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 2943: $cdom=
1.620 albertel 2944: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2945: $cnum=
1.620 albertel 2946: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 2947: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 2948: unless (($cdom) && ($cnum)) { return 0; }
2949: if ($number>10000) { return 0; }
2950: sleep(2); # make sure don't get same seed twice
2951: srand(time()^($$+($$<<15))); # from "Programming Perl"
2952: my $total=0;
2953: for (my $i=1;$i<=$number;$i++) {
2954: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2955: sprintf("%lx",int(100000*rand)).'-'.
2956: sprintf("%lx",int(100000*rand));
2957: $newkey=~s/1/g/g; # folks mix up 1 and l
2958: $newkey=~s/0/h/g; # and also 0 and O
2959: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2960: if ($existing{$newkey}) {
2961: $i--;
2962: } else {
1.364 www 2963: if (&put('accesskeys',
2964: { $newkey => '# generated '.localtime().
1.620 albertel 2965: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 2966: '; '.$logentry },
2967: $cdom,$cnum) eq 'ok') {
1.344 www 2968: $total++;
2969: }
2970: }
2971: }
1.620 albertel 2972: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 2973: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2974: return $total;
2975: }
2976:
2977: # ------------------------------------------------------- Validate an accesskey
2978:
2979: sub validate_access_key {
2980: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2981: $cdom=
1.620 albertel 2982: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2983: $cnum=
1.620 albertel 2984: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2985: $udom=$env{'user.domain'} unless (defined($udom));
2986: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 2987: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 2988: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 2989: }
2990:
2991: # ------------------------------------- Find the section of student in a course
1.652 albertel 2992: sub devalidate_getsection_cache {
2993: my ($udom,$unam,$courseid)=@_;
2994: my $hashid="$udom:$unam:$courseid";
2995: &devalidate_cache_new('getsection',$hashid);
2996: }
1.298 matthew 2997:
1.815 albertel 2998: sub courseid_to_courseurl {
2999: my ($courseid) = @_;
3000: #already url style courseid
3001: return $courseid if ($courseid =~ m{^/});
3002:
3003: if (exists($env{'course.'.$courseid.'.num'})) {
3004: my $cnum = $env{'course.'.$courseid.'.num'};
3005: my $cdom = $env{'course.'.$courseid.'.domain'};
3006: return "/$cdom/$cnum";
3007: }
3008:
3009: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
3010: if (exists($courseinfo{'num'})) {
3011: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
3012: }
3013:
3014: return undef;
3015: }
3016:
1.298 matthew 3017: sub getsection {
3018: my ($udom,$unam,$courseid)=@_;
1.599 albertel 3019: my $cachetime=1800;
1.551 albertel 3020:
3021: my $hashid="$udom:$unam:$courseid";
1.599 albertel 3022: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 3023: if (defined($cached)) { return $result; }
3024:
1.298 matthew 3025: my %Pending;
3026: my %Expired;
3027: #
3028: # Each role can either have not started yet (pending), be active,
3029: # or have expired.
3030: #
3031: # If there is an active role, we are done.
3032: #
3033: # If there is more than one role which has not started yet,
3034: # choose the one which will start sooner
3035: # If there is one role which has not started yet, return it.
3036: #
3037: # If there is more than one expired role, choose the one which ended last.
3038: # If there is a role which has expired, return it.
3039: #
1.815 albertel 3040: $courseid = &courseid_to_courseurl($courseid);
1.1166 raeburn 3041: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817 raeburn 3042: foreach my $key (keys(%roleshash)) {
1.479 albertel 3043: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 3044: my $section=$1;
3045: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 3046: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 3047: my $now=time;
1.548 albertel 3048: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 3049: $Expired{$end}=$section;
3050: next;
3051: }
1.548 albertel 3052: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 3053: $Pending{$start}=$section;
3054: next;
3055: }
1.599 albertel 3056: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 3057: }
3058: #
3059: # Presumedly there will be few matching roles from the above
3060: # loop and the sorting time will be negligible.
3061: if (scalar(keys(%Pending))) {
3062: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 3063: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 3064: }
3065: if (scalar(keys(%Expired))) {
3066: my @sorted = sort {$a <=> $b} keys(%Expired);
3067: my $time = pop(@sorted);
1.599 albertel 3068: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 3069: }
1.599 albertel 3070: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 3071: }
1.70 www 3072:
1.599 albertel 3073: sub save_cache {
3074: &purge_remembered();
1.722 albertel 3075: #&Apache::loncommon::validate_page();
1.620 albertel 3076: undef(%env);
1.780 albertel 3077: undef($env_loaded);
1.599 albertel 3078: }
1.452 albertel 3079:
1.599 albertel 3080: my $to_remember=-1;
3081: my %remembered;
3082: my %accessed;
3083: my $kicks=0;
3084: my $hits=0;
1.849 albertel 3085: sub make_key {
3086: my ($name,$id) = @_;
1.872 albertel 3087: if (length($id) > 65
3088: && length(&escape($id)) > 200) {
3089: $id=length($id).':'.&Digest::MD5::md5_hex($id);
3090: }
1.849 albertel 3091: return &escape($name.':'.$id);
3092: }
3093:
1.599 albertel 3094: sub devalidate_cache_new {
3095: my ($name,$id,$debug) = @_;
3096: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1172.2.81 raeburn 3097: my $remembered_id=$name.':'.$id;
1.849 albertel 3098: $id=&make_key($name,$id);
1.599 albertel 3099: $memcache->delete($id);
1.1172.2.81 raeburn 3100: delete($remembered{$remembered_id});
3101: delete($accessed{$remembered_id});
1.599 albertel 3102: }
3103:
3104: sub is_cached_new {
3105: my ($name,$id,$debug) = @_;
1.1172.2.81 raeburn 3106: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) for
3107: # keys in %remembered hash, which persists for
3108: # duration of request (no restriction on key length).
3109: if (exists($remembered{$remembered_id})) {
3110: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
3111: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3112: $hits++;
1.1172.2.81 raeburn 3113: return ($remembered{$remembered_id},1);
1.599 albertel 3114: }
1.1172.2.81 raeburn 3115: $id=&make_key($name,$id);
1.599 albertel 3116: my $value = $memcache->get($id);
3117: if (!(defined($value))) {
3118: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 3119: return (undef,undef);
1.416 albertel 3120: }
1.599 albertel 3121: if ($value eq '__undef__') {
3122: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
3123: $value=undef;
3124: }
1.1172.2.81 raeburn 3125: &make_room($remembered_id,$value,$debug);
1.599 albertel 3126: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
3127: return ($value,1);
3128: }
3129:
3130: sub do_cache_new {
3131: my ($name,$id,$value,$time,$debug) = @_;
1.1172.2.81 raeburn 3132: my $remembered_id=$name.':'.$id;
1.849 albertel 3133: $id=&make_key($name,$id);
1.599 albertel 3134: my $setvalue=$value;
3135: if (!defined($setvalue)) {
3136: $setvalue='__undef__';
3137: }
1.623 albertel 3138: if (!defined($time) ) {
3139: $time=600;
3140: }
1.599 albertel 3141: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 3142: my $result = $memcache->set($id,$setvalue,$time);
3143: if (! $result) {
1.872 albertel 3144: &logthis("caching of id -> $id failed");
1.910 albertel 3145: $memcache->disconnect_all();
1.872 albertel 3146: }
1.600 albertel 3147: # need to make a copy of $value
1.1172.2.81 raeburn 3148: &make_room($remembered_id,$value,$debug);
1.599 albertel 3149: return $value;
3150: }
3151:
3152: sub make_room {
1.1172.2.81 raeburn 3153: my ($remembered_id,$value,$debug)=@_;
1.919 albertel 3154:
1.1172.2.81 raeburn 3155: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919 albertel 3156: : $value;
1.599 albertel 3157: if ($to_remember<0) { return; }
1.1172.2.81 raeburn 3158: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3159: if (scalar(keys(%remembered)) <= $to_remember) { return; }
3160: my $to_kick;
3161: my $max_time=0;
3162: foreach my $other (keys(%accessed)) {
3163: if (&tv_interval($accessed{$other}) > $max_time) {
3164: $to_kick=$other;
3165: $max_time=&tv_interval($accessed{$other});
3166: }
3167: }
3168: delete($remembered{$to_kick});
3169: delete($accessed{$to_kick});
3170: $kicks++;
3171: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 3172: return;
3173: }
3174:
1.599 albertel 3175: sub purge_remembered {
1.604 albertel 3176: #&logthis("Tossing ".scalar(keys(%remembered)));
3177: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 3178: undef(%remembered);
3179: undef(%accessed);
1.428 albertel 3180: }
1.70 www 3181: # ------------------------------------- Read an entry from a user's environment
3182:
3183: sub userenvironment {
3184: my ($udom,$unam,@what)=@_;
1.976 raeburn 3185: my $items;
3186: foreach my $item (@what) {
3187: $items.=&escape($item).'&';
3188: }
3189: $items=~s/\&$//;
1.70 www 3190: my %returnhash=();
1.1009 raeburn 3191: my $uhome = &homeserver($unam,$udom);
3192: unless ($uhome eq 'no_host') {
3193: my @answer=split(/\&/,
3194: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 3195: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
3196: return %returnhash;
3197: }
1.1009 raeburn 3198: my $i;
3199: for ($i=0;$i<=$#what;$i++) {
3200: $returnhash{$what[$i]}=&unescape($answer[$i]);
3201: }
1.70 www 3202: }
3203: return %returnhash;
1.1 albertel 3204: }
3205:
1.617 albertel 3206: # ---------------------------------------------------------- Get a studentphoto
3207: sub studentphoto {
3208: my ($udom,$unam,$ext) = @_;
3209: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 3210: if (defined($env{'request.course.id'})) {
1.708 raeburn 3211: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 3212: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
3213: return(&retrievestudentphoto($udom,$unam,$ext));
3214: } else {
3215: my ($result,$perm_reqd)=
1.707 albertel 3216: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3217: if ($result eq 'ok') {
3218: if (!($perm_reqd eq 'yes')) {
3219: return(&retrievestudentphoto($udom,$unam,$ext));
3220: }
3221: }
3222: }
3223: }
3224: } else {
3225: my ($result,$perm_reqd) =
1.707 albertel 3226: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3227: if ($result eq 'ok') {
3228: if (!($perm_reqd eq 'yes')) {
3229: return(&retrievestudentphoto($udom,$unam,$ext));
3230: }
3231: }
3232: }
3233: return '/adm/lonKaputt/lonlogo_broken.gif';
3234: }
3235:
3236: sub retrievestudentphoto {
3237: my ($udom,$unam,$ext,$type) = @_;
3238: my $home=&Apache::lonnet::homeserver($unam,$udom);
3239: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
3240: if ($ret eq 'ok') {
3241: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
3242: if ($type eq 'thumbnail') {
3243: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
3244: }
3245: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
3246: return $tokenurl;
3247: } else {
3248: if ($type eq 'thumbnail') {
3249: return '/adm/lonKaputt/genericstudent_tn.gif';
3250: } else {
3251: return '/adm/lonKaputt/lonlogo_broken.gif';
3252: }
1.617 albertel 3253: }
3254: }
3255:
1.263 www 3256: # -------------------------------------------------------------------- New chat
3257:
3258: sub chatsend {
1.724 raeburn 3259: my ($newentry,$anon,$group)=@_;
1.620 albertel 3260: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
3261: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3262: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 3263: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 3264: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 3265: &escape($newentry)).':'.$group,$chome);
1.292 www 3266: }
3267:
3268: # ------------------------------------------ Find current version of a resource
3269:
3270: sub getversion {
3271: my $fname=&clutter(shift);
1.1172.2.10 raeburn 3272: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292 www 3273: return ¤tversion(&filelocation('',$fname));
3274: }
3275:
3276: sub currentversion {
3277: my $fname=shift;
3278: my $author=$fname;
3279: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3280: my ($udom,$uname)=split(/\//,$author);
1.1112 www 3281: my $home=&homeserver($uname,$udom);
1.292 www 3282: if ($home eq 'no_host') {
3283: return -1;
3284: }
1.1112 www 3285: my $answer=&reply("currentversion:$fname",$home);
1.292 www 3286: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3287: return -1;
3288: }
1.1112 www 3289: return $answer;
1.263 www 3290: }
3291:
1.1111 www 3292: #
3293: # Return special version number of resource if set by override, empty otherwise
3294: #
3295: sub usedversion {
3296: my $fname=shift;
3297: unless ($fname) { $fname=$env{'request.uri'}; }
3298: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
3299: if ($urlversion) { return $urlversion; }
3300: return '';
3301: }
3302:
1.1 albertel 3303: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 3304:
1.1 albertel 3305: sub subscribe {
3306: my $fname=shift;
1.761 raeburn 3307: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 3308: $fname=~s/[\n\r]//g;
1.1 albertel 3309: my $author=$fname;
3310: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3311: my ($udom,$uname)=split(/\//,$author);
3312: my $home=homeserver($uname,$udom);
1.335 albertel 3313: if ($home eq 'no_host') {
3314: return 'not_found';
1.1 albertel 3315: }
3316: my $answer=reply("sub:$fname",$home);
1.64 www 3317: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3318: $answer.=' by '.$home;
3319: }
1.1 albertel 3320: return $answer;
3321: }
3322:
1.8 www 3323: # -------------------------------------------------------------- Replicate file
3324:
3325: sub repcopy {
3326: my $filename=shift;
1.23 www 3327: $filename=~s/\/+/\//g;
1.1142 raeburn 3328: my $londocroot = $perlvar{'lonDocRoot'};
3329: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164 raeburn 3330: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142 raeburn 3331: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
3332: $filename=~m{^/*(uploaded|editupload)/}) {
1.538 albertel 3333: return &repcopy_userfile($filename);
3334: }
1.532 albertel 3335: $filename=~s/[\n\r]//g;
1.8 www 3336: my $transname="$filename.in.transfer";
1.828 www 3337: # FIXME: this should flock
1.607 raeburn 3338: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 3339: my $remoteurl=subscribe($filename);
1.64 www 3340: if ($remoteurl =~ /^con_lost by/) {
3341: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3342: return 'unavailable';
1.8 www 3343: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 3344: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 3345: return 'not_found';
1.64 www 3346: } elsif ($remoteurl =~ /^rejected by/) {
3347: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3348: return 'forbidden';
1.20 www 3349: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 3350: return 'ok';
1.8 www 3351: } else {
1.290 www 3352: my $author=$filename;
3353: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3354: my ($udom,$uname)=split(/\//,$author);
3355: my $home=homeserver($uname,$udom);
3356: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 3357: my @parts=split(/\//,$filename);
3358: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142 raeburn 3359: if ($path ne "$londocroot/res") {
1.8 www 3360: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 3361: return 'bad_request';
1.8 www 3362: }
3363: my $count;
3364: for ($count=5;$count<$#parts;$count++) {
3365: $path.="/$parts[$count]";
3366: if ((-e $path)!=1) {
3367: mkdir($path,0777);
3368: }
3369: }
3370: my $ua=new LWP::UserAgent;
3371: my $request=new HTTP::Request('GET',"$remoteurl");
3372: my $response=$ua->request($request,$transname);
3373: if ($response->is_error()) {
3374: unlink($transname);
3375: my $message=$response->status_line;
1.672 albertel 3376: &logthis("<font color=\"blue\">WARNING:"
1.12 www 3377: ." LWP get: $message: $filename</font>");
1.607 raeburn 3378: return 'unavailable';
1.8 www 3379: } else {
1.16 www 3380: if ($remoteurl!~/\.meta$/) {
3381: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
3382: my $mresponse=$ua->request($mrequest,$filename.'.meta');
3383: if ($mresponse->is_error()) {
3384: unlink($filename.'.meta');
3385: &logthis(
1.672 albertel 3386: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 3387: }
3388: }
1.8 www 3389: rename($transname,$filename);
1.607 raeburn 3390: return 'ok';
1.8 www 3391: }
1.290 www 3392: }
1.8 www 3393: }
1.330 www 3394: }
3395:
1.1172.2.124 raeburn 3396: # ------------------------------------------------- Unsubscribe from a resource
3397:
3398: sub unsubscribe {
3399: my ($fname) = @_;
3400: my $answer;
3401: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
3402: $fname=~s/[\n\r]//g;
3403: my $author=$fname;
3404: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3405: my ($udom,$uname)=split(/\//,$author);
3406: my $home=homeserver($uname,$udom);
3407: if ($home eq 'no_host') {
3408: $answer = 'no_host';
3409: } elsif (grep { $_ eq $home } ¤t_machine_ids()) {
3410: $answer = 'home';
3411: } else {
1.1172.2.125 raeburn 3412: $answer = reply("unsub:$fname",$home);
1.1172.2.124 raeburn 3413: }
3414: return $answer;
3415: }
3416:
1.330 www 3417: # ------------------------------------------------ Get server side include body
3418: sub ssi_body {
1.381 albertel 3419: my ($filelink,%form)=@_;
1.606 matthew 3420: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
3421: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
3422: }
1.953 www 3423: my $output='';
3424: my $response;
1.980 raeburn 3425: if ($filelink=~/^https?\:/) {
1.954 raeburn 3426: ($output,$response)=&externalssi($filelink);
1.953 www 3427: } else {
1.1004 droeschl 3428: $filelink .= $filelink=~/\?/ ? '&' : '?';
3429: $filelink .= 'inhibitmenu=yes';
1.953 www 3430: ($output,$response)=&ssi($filelink,%form);
3431: }
1.778 albertel 3432: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 3433: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 3434: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 3435: if (wantarray) {
3436: return ($output, $response);
3437: } else {
3438: return $output;
3439: }
1.8 www 3440: }
3441:
1.15 www 3442: # --------------------------------------------------------- Server Side Include
3443:
1.782 albertel 3444: sub absolute_url {
1.1172.2.145 raeburn 3445: my ($host_name,$unalias,$keep_proto) = @_;
1.782 albertel 3446: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
3447: if ($host_name eq '') {
3448: $host_name = $ENV{'SERVER_NAME'};
3449: }
1.1172.2.145 raeburn 3450: if ($unalias) {
3451: my $alias = &get_proxy_alias();
3452: if ($alias eq $host_name) {
3453: my $lonhost = $perlvar{'lonHostID'};
3454: my $hostname = &hostname($lonhost);
3455: my $lcproto;
3456: if (($keep_proto) || ($hostname eq '')) {
3457: $lcproto = $protocol;
3458: } else {
3459: $lcproto = $protocol{$lonhost};
3460: $lcproto = 'http' if ($lcproto ne 'https');
3461: $lcproto .= '://';
3462: }
3463: unless ($hostname eq '') {
3464: return $lcproto.$hostname;
3465: }
3466: }
3467: }
1.782 albertel 3468: return $protocol.$host_name;
3469: }
3470:
1.942 foxr 3471: #
3472: # Server side include.
3473: # Parameters:
3474: # fn Possibly encrypted resource name/id.
3475: # form Hash that describes how the rendering should be done
3476: # and other things.
1.944 foxr 3477: # Returns:
1.950 raeburn 3478: # Scalar context: The content of the response.
3479: # Array context: 2 element list of the content and the full response object.
1.942 foxr 3480: #
1.15 www 3481: sub ssi {
3482:
1.944 foxr 3483: my ($fn,%form)=@_;
1.1172.2.145 raeburn 3484: my ($host,$request,$response);
3485: $host = &absolute_url('',1);
1.711 albertel 3486:
3487: $form{'no_update_last_known'}=1;
1.895 albertel 3488: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 3489: if (%form) {
1.1172.2.145 raeburn 3490: $request=new HTTP::Request('POST',$host.$fn);
1.1172.2.58 raeburn 3491: $request->content(join('&',map {
3492: my $name = escape($_);
3493: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3494: ? join("&$name=", map {escape($_) } @{$form{$_}})
3495: : &escape($form{$_}) );
3496: } keys(%form)));
1.23 www 3497: } else {
1.1172.2.145 raeburn 3498: $request=new HTTP::Request('GET',$host.$fn);
1.23 www 3499: }
3500:
1.15 www 3501: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1172.2.100 raeburn 3502:
1.1172.2.101 raeburn 3503: if (($env{'request.course.id'}) &&
3504: ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
3505: ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
3506: ($form{'grade_symb'} ne '') &&
3507: (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
3508: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
1.1172.2.100 raeburn 3509: if (LWP::UserAgent->VERSION >= 5.834) {
3510: my $ua=new LWP::UserAgent;
3511: $ua->local_address('127.0.0.1');
3512: $response = $ua->request($request);
3513: } else {
3514: {
3515: require LWP::Protocol::http;
3516: local @LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => '127.0.0.1');
3517: my $ua=new LWP::UserAgent;
3518: $response = $ua->request($request);
3519: @LWP::Protocol::http::EXTRA_SOCK_OPTS = ();
3520: }
3521: }
3522: } else {
3523: my $ua=new LWP::UserAgent;
3524: $response = $ua->request($request);
3525: }
1.944 foxr 3526: if (wantarray) {
1.1172.2.3 raeburn 3527: return ($response->content, $response);
1.944 foxr 3528: } else {
1.1172.2.3 raeburn 3529: return $response->content;
1.942 foxr 3530: }
1.324 www 3531: }
3532:
3533: sub externalssi {
3534: my ($url)=@_;
3535: my $ua=new LWP::UserAgent;
3536: my $request=new HTTP::Request('GET',$url);
3537: my $response=$ua->request($request);
1.954 raeburn 3538: if (wantarray) {
3539: return ($response->content, $response);
3540: } else {
3541: return $response->content;
3542: }
1.15 www 3543: }
1.254 www 3544:
1.1172.2.98 raeburn 3545: # If the local copy of a replicated resource is outdated, trigger a
3546: # connection from the homeserver to flush the delayed queue. If no update
3547: # happens, remove local copies of outdated resource (and corresponding
3548: # metadata file).
3549:
3550: sub remove_stale_resfile {
3551: my ($url) = @_;
3552: my $removed;
3553: if ($url=~m{^/res/($match_domain)/($match_username)/}) {
3554: my $audom = $1;
3555: my $auname = $2;
3556: unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
3557: my $homeserver = &homeserver($auname,$audom);
3558: unless (($homeserver eq 'no_host') ||
3559: (grep { $_ eq $homeserver } ¤t_machine_ids())) {
3560: my $fname = &filelocation('',$url);
3561: if (-e $fname) {
3562: my $hostname = &hostname($homeserver);
3563: if ($hostname) {
1.1172.2.120 raeburn 3564: my $protocol = $protocol{$homeserver};
3565: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.98 raeburn 3566: my $uri = $protocol.'://'.$hostname.'/raw/'.&declutter($url);
1.1172.2.120 raeburn 3567: my $ua=new LWP::UserAgent;
3568: $ua->timeout(5);
1.1172.2.98 raeburn 3569: my $request=new HTTP::Request('HEAD',$uri);
3570: my $response=$ua->request($request);
3571: if ($response->is_success()) {
3572: my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
3573: my $locmodtime = (stat($fname))[9];
3574: if ($locmodtime < $remmodtime) {
3575: my $stale;
3576: my $answer = &reply('pong',$homeserver);
3577: if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
3578: sleep(0.2);
3579: $locmodtime = (stat($fname))[9];
3580: if ($locmodtime < $remmodtime) {
3581: my $posstransfer = $fname.'.in.transfer';
3582: if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
3583: $removed = 1;
3584: } else {
3585: $stale = 1;
3586: }
3587: } else {
3588: $removed = 1;
3589: }
3590: } else {
3591: $stale = 1;
3592: }
3593: if ($stale) {
1.1172.2.124 raeburn 3594: if (unlink($fname)) {
3595: if ($uri!~/\.meta$/) {
3596: if (-e $fname.'.meta') {
3597: unlink($fname.'.meta');
3598: }
3599: }
3600: my $unsubresult = &unsubscribe($fname);
3601: unless ($unsubresult eq 'ok') {
3602: &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
3603: }
3604: $removed = 1;
1.1172.2.98 raeburn 3605: }
3606: }
3607: }
3608: }
3609: }
3610: }
3611: }
3612: }
3613: }
3614: return $removed;
3615: }
3616:
1.492 albertel 3617: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3618:
3619: sub allowuploaded {
3620: my ($srcurl,$url)=@_;
3621: $url=&clutter(&declutter($url));
3622: my $dir=$url;
3623: $dir=~s/\/[^\/]+$//;
3624: my %httpref=();
3625: my $httpurl=&hreflocation('',$url);
3626: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 3627: &Apache::lonnet::appenv(\%httpref);
1.254 www 3628: }
1.477 raeburn 3629:
1.1172.2.13 raeburn 3630: #
3631: # Determine if the current user should be able to edit a particular resource,
3632: # when viewing in course context.
3633: # (a) When viewing resource used to determine if "Edit" item is included in
3634: # Functions.
3635: # (b) When displaying folder contents in course editor, used to determine if
3636: # "Edit" link will be displayed alongside resource.
3637: #
3638: # input: six args -- filename (decluttered), course number, course domain,
3639: # url, symb (if registered) and group (if this is a group
3640: # item -- e.g., bulletin board, group page etc.).
3641: # output: array of five scalars --
3642: # $cfile -- url for file editing if editable on current server
3643: # $home -- homeserver of resource (i.e., for author if published,
3644: # or course if uploaded.).
3645: # $switchserver -- 1 if server switch will be needed.
3646: # $forceedit -- 1 if icon/link should be to go to edit mode
3647: # $forceview -- 1 if icon/link should be to go to view mode
3648: #
3649:
3650: sub can_edit_resource {
3651: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3652: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3653: #
3654: # For aboutme pages user can only edit his/her own.
3655: #
3656: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
3657: my ($sdom,$sname) = ($1,$2);
3658: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3659: $home = $env{'user.home'};
3660: $cfile = $resurl;
3661: if ($env{'form.forceedit'}) {
3662: $forceview = 1;
3663: } else {
3664: $forceedit = 1;
3665: }
3666: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3667: } else {
3668: return;
3669: }
3670: }
3671:
1.1172.2.146. .22(raeb 3672:-24): #
3673:-24): # For /adm/viewcoauthors can only edit if author or co-author who is manager.
3674:-24): #
3675:-24):
3676:-24): if (($resurl eq '/adm/viewcoauthors') && ($cnum ne '') && ($cdom ne '')) {
3677:-24): if (((&allowed('cca',"$cdom/$cnum")) ||
3678:-24): (&allowed('caa',"$cdom/$cnum"))) ||
3679:-24): ((&allowed('vca',"$cdom/$cnum") ||
3680:-24): &allowed('vaa',"$cdom/$cnum")) &&
3681:-24): ($env{"environment.internal.manager./$cdom/$cnum"}))) {
3682:-24): $home = $env{'user.home'};
3683:-24): $cfile = $resurl;
3684:-24): if ($env{'form.forceedit'}) {
3685:-24): $forceview = 1;
3686:-24): } else {
3687:-24): $forceedit = 1;
3688:-24): }
3689:-24): return ($cfile,$home,$switchserver,$forceedit,$forceview);
3690:-24): } else {
3691:-24): return;
3692:-24): }
3693:-24): }
3694:-24):
1.1172.2.13 raeburn 3695: if ($env{'request.course.id'}) {
3696: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3697: if ($group ne '') {
3698: # if this is a group homepage or group bulletin board, check group privs
3699: my $allowed = 0;
3700: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3701: if ((&allowed('mdg',$env{'request.course.id'}.
3702: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3703: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3704: $allowed = 1;
3705: }
3706: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3707: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3708: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3709: $allowed = 1;
3710: }
3711: }
3712: if ($allowed) {
3713: $home=&homeserver($cnum,$cdom);
3714: if ($env{'form.forceedit'}) {
3715: $forceview = 1;
3716: } else {
3717: $forceedit = 1;
3718: }
3719: $cfile = $resurl;
3720: } else {
3721: return;
3722: }
3723: } else {
1.1172.2.15 raeburn 3724: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3725: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3726: return;
3727: }
3728: } elsif (!$crsedit) {
1.1172.2.146. .21(raeb 3729:-24): if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
1.1172.2.13 raeburn 3730: #
3731: # No edit allowed where CC has switched to student role.
3732: #
1.1172.2.146. .21(raeb 3733:-24): return;
3734:-24): } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
3735:-24): ($resurl =~ m{^/res/lib/templates/})) {
3736:-24): return;
3737:-24): }
1.1172.2.13 raeburn 3738: }
3739: }
3740: }
3741:
3742: if ($file ne '') {
3743: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
3744: if (&is_course_upload($file,$cnum,$cdom)) {
3745: $uploaded = 1;
3746: $incourse = 1;
3747: if ($file =~/\.(htm|html|css|js|txt)$/) {
3748: $cfile = &hreflocation('',$file);
3749: if ($env{'form.forceedit'}) {
3750: $forceview = 1;
3751: } else {
3752: $forceedit = 1;
3753: }
3754: }
3755: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3756: $incourse = 1;
3757: if ($env{'form.forceedit'}) {
3758: $forceview = 1;
3759: } else {
3760: $forceedit = 1;
3761: }
3762: $cfile = $resurl;
3763: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3764: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3765: $incourse = 1;
3766: if ($env{'form.forceedit'}) {
3767: $forceview = 1;
3768: } else {
3769: $forceedit = 1;
3770: }
3771: $cfile = $resurl;
3772: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
3773: $incourse = 1;
3774: $cfile = $resurl.'/smpedit';
3775: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
3776: $incourse = 1;
3777: if ($env{'form.forceedit'}) {
3778: $forceview = 1;
3779: } else {
3780: $forceedit = 1;
3781: }
3782: $cfile = $resurl;
1.1172.2.122 raeburn 3783: } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
3784: my ($map,$id,$res) = &decode_symb($symb);
3785: if ($map =~ /\.page$/) {
3786: $incourse = 1;
3787: if ($env{'form.forceedit'}) {
3788: $forceview = 1;
3789: $cfile = $map;
3790: } else {
3791: $forceedit = 1;
3792: $cfile = '/adm/wrapper'.$resurl;
3793: }
3794: }
1.1172.2.146. .1(raebu 3795:22): } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3796:22): $incourse = 1;
3797:22): if ($env{'form.forceedit'}) {
3798:22): $forceview = 1;
3799:22): } else {
3800:22): $forceedit = 1;
3801:22): }
3802:22): $cfile = $resurl;
1.1172.2.15 raeburn 3803: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3804: $incourse = 1;
3805: if ($env{'form.forceedit'}) {
3806: $forceview = 1;
3807: } else {
3808: $forceedit = 1;
3809: }
3810: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3811: }
3812: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3813: my $template = '/res/lib/templates/simpleproblem.problem';
3814: if (&is_on_map($template)) {
3815: $incourse = 1;
3816: $forceview = 1;
3817: $cfile = $template;
3818: }
3819: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1172.2.118 raeburn 3820: $incourse = 1;
3821: if ($env{'form.forceedit'}) {
3822: $forceview = 1;
3823: } else {
3824: $forceedit = 1;
3825: }
3826: $cfile = $resurl;
1.1172.2.146. .1(raebu 3827:22): } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3828:22): $incourse = 1;
3829:22): if ($env{'form.forceedit'}) {
3830:22): $forceview = 1;
3831:22): } else {
3832:22): $forceedit = 1;
3833:22): }
3834:22): $cfile = $resurl;
1.1172.2.13 raeburn 3835: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3836: $incourse = 1;
3837: $forceview = 1;
3838: if ($symb) {
3839: my ($map,$id,$res)=&decode_symb($symb);
3840: $env{'request.symb'} = $symb;
3841: $cfile = &clutter($res);
3842: } else {
3843: $cfile = $env{'form.suppurl'};
1.1172.2.146. .1(raebu 3844:22): my $escfile = &unescape($cfile);
3845:22): if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3846:22): $cfile = '/adm/wrapper'.$escfile;
3847:22): } else {
3848:22): $escfile =~ s{^http://}{};
3849:22): $cfile = &escape("/adm/wrapper/ext/$escfile");
3850:22): }
1.1172.2.13 raeburn 3851: }
1.1172.2.31 raeburn 3852: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3853: if ($env{'form.forceedit'}) {
3854: $forceview = 1;
3855: } else {
3856: $forceedit = 1;
3857: }
3858: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3859: }
3860: }
3861: if ($uploaded || $incourse) {
3862: $home=&homeserver($cnum,$cdom);
1.1172.2.14 raeburn 3863: } elsif ($file !~ m{/$}) {
1.1172.2.13 raeburn 3864: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3865: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3866: # Check that the user has permission to edit this resource
3867: my $setpriv = 1;
3868: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3869: if (defined($cfudom)) {
3870: $home=&homeserver($cfuname,$cfudom);
3871: $cfile=$file;
3872: }
3873: }
3874: if (($cfile ne '') && (!$incourse || $uploaded) &&
3875: (($home ne '') && ($home ne 'no_host'))) {
3876: my @ids=¤t_machine_ids();
3877: unless (grep(/^\Q$home\E$/,@ids)) {
3878: $switchserver=1;
3879: }
3880: }
3881: }
3882: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3883: }
3884:
3885: sub is_course_upload {
3886: my ($file,$cnum,$cdom) = @_;
3887: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3888: $uploadpath =~ s{^\/}{};
3889: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3890: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
3891: return 1;
3892: }
3893: return;
3894: }
3895:
3896: sub in_course {
3897: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3898: if ($hideprivileged) {
3899: my $skipuser;
1.1172.2.23 raeburn 3900: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3901: my @possdoms = ($cdom);
3902: if ($coursehash{'checkforpriv'}) {
3903: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3904: }
3905: if (&privileged($uname,$udom,\@possdoms)) {
1.1172.2.13 raeburn 3906: $skipuser = 1;
3907: if ($coursehash{'nothideprivileged'}) {
3908: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3909: my $user;
3910: if ($item =~ /:/) {
3911: $user = $item;
3912: } else {
3913: $user = join(':',split(/[\@]/,$item));
3914: }
3915: if ($user eq $uname.':'.$udom) {
3916: undef($skipuser);
3917: last;
3918: }
3919: }
3920: }
3921: if ($skipuser) {
3922: return 0;
3923: }
3924: }
3925: }
3926: $type ||= 'any';
3927: if (!defined($cdom) || !defined($cnum)) {
3928: my $cid = $env{'request.course.id'};
3929: $cdom = $env{'course.'.$cid.'.domain'};
3930: $cnum = $env{'course.'.$cid.'.num'};
3931: }
3932: my $typesref;
3933: if (($type eq 'any') || ($type eq 'all')) {
3934: $typesref = ['active','previous','future'];
3935: } elsif ($type eq 'previous' || $type eq 'future') {
3936: $typesref = [$type];
3937: }
3938: my %roles = &get_my_roles($uname,$udom,'userroles',
3939: $typesref,undef,[$cdom]);
3940: my ($tmp) = keys(%roles);
3941: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3942: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3943: if (@course_roles > 0) {
3944: return 1;
3945: }
3946: return 0;
3947: }
3948:
1.478 albertel 3949: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 3950: # input: action, courseID, current domain, intended
1.637 raeburn 3951: # path to file, source of file, instruction to parse file for objects,
3952: # ref to hash for embedded objects,
3953: # ref to hash for codebase of java objects.
1.1095 raeburn 3954: # reference to scalar to accommodate mime type determined
3955: # from File::MMagic if $parser = parse.
1.637 raeburn 3956: #
1.485 raeburn 3957: # output: url to file (if action was uploaddoc),
3958: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 3959: #
1.478 albertel 3960: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3961: # course.
1.477 raeburn 3962: #
1.478 albertel 3963: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3964: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3965: # course's home server.
1.477 raeburn 3966: #
1.478 albertel 3967: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3968: # be copied from $source (current location) to
3969: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3970: # and will then be copied to
3971: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3972: # course's home server.
1.485 raeburn 3973: #
1.481 raeburn 3974: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 3975: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 3976: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3977: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3978: # in course's home server.
1.637 raeburn 3979: #
1.477 raeburn 3980:
3981: sub process_coursefile {
1.1095 raeburn 3982: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3983: $mimetype)=@_;
1.477 raeburn 3984: my $fetchresult;
1.638 albertel 3985: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 3986: if ($action eq 'propagate') {
1.638 albertel 3987: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3988: $home);
1.481 raeburn 3989: } else {
1.477 raeburn 3990: my $fpath = '';
3991: my $fname = $file;
1.478 albertel 3992: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 3993: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 3994: my $filepath = &build_filepath($fpath);
1.481 raeburn 3995: if ($action eq 'copy') {
3996: if ($source eq '') {
3997: $fetchresult = 'no source file';
3998: return $fetchresult;
3999: } else {
4000: my $destination = $filepath.'/'.$fname;
4001: rename($source,$destination);
4002: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4003: $home);
1.481 raeburn 4004: }
4005: } elsif ($action eq 'uploaddoc') {
1.1172.2.96 raeburn 4006: open(my $fh,'>',$filepath.'/'.$fname);
1.620 albertel 4007: print $fh $env{'form.'.$source};
1.481 raeburn 4008: close($fh);
1.637 raeburn 4009: if ($parser eq 'parse') {
1.1024 raeburn 4010: my $mm = new File::MMagic;
1.1095 raeburn 4011: my $type = $mm->checktype_filename($filepath.'/'.$fname);
4012: if ($type eq 'text/html') {
1.1024 raeburn 4013: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
4014: unless ($parse_result eq 'ok') {
4015: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
4016: }
1.637 raeburn 4017: }
1.1095 raeburn 4018: if (ref($mimetype)) {
4019: $$mimetype = $type;
4020: }
1.637 raeburn 4021: }
1.477 raeburn 4022: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4023: $home);
1.481 raeburn 4024: if ($fetchresult eq 'ok') {
4025: return '/uploaded/'.$fpath.'/'.$fname;
4026: } else {
4027: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4028: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 4029: return '/adm/notfound.html';
4030: }
1.477 raeburn 4031: }
4032: }
1.485 raeburn 4033: unless ( $fetchresult eq 'ok') {
1.477 raeburn 4034: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4035: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 4036: }
4037: return $fetchresult;
4038: }
4039:
1.637 raeburn 4040: sub build_filepath {
4041: my ($fpath) = @_;
4042: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
4043: unless ($fpath eq '') {
4044: my @parts=split('/',$fpath);
4045: foreach my $part (@parts) {
4046: $filepath.= '/'.$part;
4047: if ((-e $filepath)!=1) {
4048: mkdir($filepath,0777);
4049: }
4050: }
4051: }
4052: return $filepath;
4053: }
4054:
4055: sub store_edited_file {
1.638 albertel 4056: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 4057: my $file = $primary_url;
4058: $file =~ s#^/uploaded/$docudom/$docuname/##;
4059: my $fpath = '';
4060: my $fname = $file;
4061: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
4062: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
4063: my $filepath = &build_filepath($fpath);
1.1172.2.96 raeburn 4064: open(my $fh,'>',$filepath.'/'.$fname);
1.637 raeburn 4065: print $fh $content;
4066: close($fh);
1.638 albertel 4067: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 4068: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4069: $home);
1.637 raeburn 4070: if ($$fetchresult eq 'ok') {
4071: return '/uploaded/'.$fpath.'/'.$fname;
4072: } else {
1.638 albertel 4073: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
4074: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 4075: return '/adm/notfound.html';
4076: }
4077: }
4078:
1.531 albertel 4079: sub clean_filename {
1.831 albertel 4080: my ($fname,$args)=@_;
1.315 www 4081: # Replace Windows backslashes by forward slashes
1.257 www 4082: $fname=~s/\\/\//g;
1.831 albertel 4083: if (!$args->{'keep_path'}) {
4084: # Get rid of everything but the actual filename
4085: $fname=~s/^.*\/([^\/]+)$/$1/;
4086: }
1.315 www 4087: # Replace spaces by underscores
4088: $fname=~s/\s+/\_/g;
1.1172.2.110 raeburn 4089: # Transliterate non-ascii text to ascii
4090: my $lang = &Apache::lonlocal::current_language();
4091: $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315 www 4092: # Replace all other weird characters by nothing
1.831 albertel 4093: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 4094: # Replace all .\d. sequences with _\d. so they no longer look like version
4095: # numbers
4096: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1172.2.140 raeburn 4097: # Replace three or more adjacent underscores with one for consistency
4098: # with loncfile::filename_check() so complete url can be extracted by
4099: # lonnet::decode_symb()
4100: $fname=~s/_{3,}/_/g;
1.531 albertel 4101: return $fname;
4102: }
1.1172.2.110 raeburn 4103:
1.1051 raeburn 4104: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
4105: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
4106: # image with the same aspect ratio as the original, but with dimensions which do
4107: # not exceed $resizewidth and $resizeheight.
4108:
1.984 neumanie 4109: sub resizeImage {
1.1051 raeburn 4110: my ($img_path,$resizewidth,$resizeheight) = @_;
4111: my $ima = Image::Magick->new;
4112: my $resized;
4113: if (-e $img_path) {
4114: $ima->Read($img_path);
4115: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
4116: my $width = $ima->Get('width');
4117: my $height = $ima->Get('height');
4118: if ($width > $resizewidth) {
4119: my $factor = $width/$resizewidth;
4120: my $newheight = $height/$factor;
4121: $ima->Scale(width=>$resizewidth,height=>$newheight);
4122: $resized = 1;
4123: }
4124: }
4125: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
4126: my $width = $ima->Get('width');
4127: my $height = $ima->Get('height');
4128: if ($height > $resizeheight) {
4129: my $factor = $height/$resizeheight;
4130: my $newwidth = $width/$factor;
4131: $ima->Scale(width=>$newwidth,height=>$resizeheight);
4132: $resized = 1;
4133: }
4134: }
4135: if ($resized) {
4136: $ima->Write($img_path);
4137: }
4138: }
4139: return;
1.977 amueller 4140: }
4141:
1.608 albertel 4142: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 4143: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093 raeburn 4144: # the desired filename is in $env{"form.$formname.filename"}
1.1090 raeburn 4145: # $context - possible values: coursedoc, existingfile, overwrite,
1.1172.2.146. .13(raeb 4146:-23): # canceloverwrite, scantron, toollogo or ''.
1.1090 raeburn 4147: # if 'coursedoc': upload to the current course
4148: # if 'existingfile': write file to tmp/overwrites directory
4149: # if 'canceloverwrite': delete file written to tmp/overwrites directory
4150: # $context is passed as argument to &finishuserfileupload
1.686 albertel 4151: # $subdir - directory in userfile to store the file into
1.1172.2.109 raeburn 4152: # $parser - instruction to parse file for objects ($parser = parse) or
4153: # if context is 'scantron', $parser is hashref of csv column mapping
4154: # (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3,
4155: # Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858 raeburn 4156: # $allfiles - reference to hash for embedded objects
4157: # $codebase - reference to hash for codebase of java objects
1.1172.2.146. .13(raeb 4158:-23): # $destuname - username for permanent storage of uploaded file
4159:-23): # $destudom - domain for permanaent storage of uploaded file
1.860 raeburn 4160: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
4161: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 4162: # $resizewidth - width (pixels) to which to resize uploaded image
4163: # $resizeheight - height (pixels) to which to resize uploaded image
1.1095 raeburn 4164: # $mimetype - reference to scalar to accommodate mime type determined
1.1152 raeburn 4165: # from File::MMagic.
1.858 raeburn 4166: #
1.686 albertel 4167: # output: url of file in userspace, or error: <message>
4168: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 4169:
1.531 albertel 4170: sub userfileupload {
1.1090 raeburn 4171: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095 raeburn 4172: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 4173: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 4174: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 4175: $fname=&clean_filename($fname);
1.1090 raeburn 4176: # See if there is anything left
1.257 www 4177: unless ($fname) { return 'error: no uploaded file'; }
1.1172.2.110 raeburn 4178: # If filename now begins with a . prepend unix timestamp _ milliseconds
4179: if ($fname =~ /^\./) {
4180: my ($s,$usec) = &gettimeofday();
4181: while (length($usec) < 6) {
4182: $usec = '0'.$usec;
4183: }
4184: $fname = $s.'_'.substr($usec,0,3).$fname;
4185: }
1.1090 raeburn 4186: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
4187: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
4188: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
4189: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 4190: my $now = time;
1.1090 raeburn 4191: my $filepath;
1.1095 raeburn 4192: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090 raeburn 4193: $filepath = 'tmp/helprequests/'.$now;
4194: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
4195: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
4196: '_'.$env{'user.domain'}.'/pending';
4197: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
4198: my ($docuname,$docudom);
1.1172.2.96 raeburn 4199: if ($destudom =~ /^$match_domain$/) {
1.1090 raeburn 4200: $docudom = $destudom;
4201: } else {
4202: $docudom = $env{'user.domain'};
4203: }
1.1172.2.96 raeburn 4204: if ($destuname =~ /^$match_username$/) {
1.1090 raeburn 4205: $docuname = $destuname;
4206: } else {
4207: $docuname = $env{'user.name'};
4208: }
4209: if (exists($env{'form.group'})) {
4210: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4211: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4212: }
4213: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
4214: if ($context eq 'canceloverwrite') {
4215: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
4216: if (-e $tempfile) {
4217: my @info = stat($tempfile);
4218: if ($info[9] eq $env{'form.timestamp'}) {
4219: unlink($tempfile);
4220: }
4221: }
4222: return;
1.523 raeburn 4223: }
4224: }
1.1090 raeburn 4225: # Create the directory if not present
1.741 raeburn 4226: my @parts=split(/\//,$filepath);
4227: my $fullpath = $perlvar{'lonDaemons'};
4228: for (my $i=0;$i<@parts;$i++) {
4229: $fullpath .= '/'.$parts[$i];
4230: if ((-e $fullpath)!=1) {
4231: mkdir($fullpath,0777);
4232: }
4233: }
1.1172.2.96 raeburn 4234: open(my $fh,'>',$fullpath.'/'.$fname);
1.741 raeburn 4235: print $fh $env{'form.'.$formname};
4236: close($fh);
1.1090 raeburn 4237: if ($context eq 'existingfile') {
4238: my @info = stat($fullpath.'/'.$fname);
4239: return ($fullpath.'/'.$fname,$info[9]);
4240: } else {
4241: return $fullpath.'/'.$fname;
4242: }
1.523 raeburn 4243: }
1.995 raeburn 4244: if ($subdir eq 'scantron') {
4245: $fname = 'scantron_orig_'.$fname;
1.1093 raeburn 4246: } else {
1.995 raeburn 4247: $fname="$subdir/$fname";
4248: }
1.1090 raeburn 4249: if ($context eq 'coursedoc') {
1.638 albertel 4250: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4251: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 4252: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 4253: return &finishuserfileupload($docuname,$docudom,
4254: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 4255: $codebase,$thumbwidth,$thumbheight,
1.1095 raeburn 4256: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 4257: } else {
1.1172.2.21 raeburn 4258: if ($env{'form.folder'}) {
4259: $fname=$env{'form.folder'}.'/'.$fname;
4260: }
1.638 albertel 4261: return &process_coursefile('uploaddoc',$docuname,$docudom,
4262: $fname,$formname,$parser,
1.1095 raeburn 4263: $allfiles,$codebase,$mimetype);
1.481 raeburn 4264: }
1.719 banghart 4265: } elsif (defined($destuname)) {
4266: my $docuname=$destuname;
4267: my $docudom=$destudom;
1.860 raeburn 4268: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4269: $parser,$allfiles,$codebase,
1.1051 raeburn 4270: $thumbwidth,$thumbheight,
1.1095 raeburn 4271: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4272: } else {
1.638 albertel 4273: my $docuname=$env{'user.name'};
4274: my $docudom=$env{'user.domain'};
1.1172.2.23 raeburn 4275: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714 raeburn 4276: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4277: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4278: }
1.860 raeburn 4279: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4280: $parser,$allfiles,$codebase,
1.1051 raeburn 4281: $thumbwidth,$thumbheight,
1.1095 raeburn 4282: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4283: }
1.271 www 4284: }
4285:
4286: sub finishuserfileupload {
1.860 raeburn 4287: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095 raeburn 4288: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 4289: my $path=$docudom.'/'.$docuname.'/';
1.258 www 4290: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 4291:
1.860 raeburn 4292: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 4293: $file=$fname;
4294: if ($fname=~m|/|) {
4295: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
4296: $path.=$fnamepath.'/';
4297: }
1.259 www 4298: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 4299: my $count;
4300: for ($count=4;$count<=$#parts;$count++) {
4301: $filepath.="/$parts[$count]";
4302: if ((-e $filepath)!=1) {
4303: mkdir($filepath,0777);
4304: }
4305: }
1.984 neumanie 4306:
1.258 www 4307: # Save the file
4308: {
1.1172.2.96 raeburn 4309: if (!open(FH,'>',$filepath.'/'.$file)) {
1.701 albertel 4310: &logthis('Failed to create '.$filepath.'/'.$file);
4311: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
4312: return '/adm/notfound.html';
4313: }
1.1090 raeburn 4314: if ($context eq 'overwrite') {
1.1117 foxr 4315: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090 raeburn 4316: my $target = $filepath.'/'.$file;
4317: if (-e $source) {
4318: my @info = stat($source);
4319: if ($info[9] eq $env{'form.timestamp'}) {
4320: unless (&File::Copy::move($source,$target)) {
4321: &logthis('Failed to overwrite '.$filepath.'/'.$file);
4322: return "Moving from $source failed";
4323: }
4324: } else {
4325: return "Temporary file: $source had unexpected date/time for last modification";
4326: }
4327: } else {
4328: return "Temporary file: $source missing";
4329: }
4330: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 4331: &logthis('Failed to write to '.$filepath.'/'.$file);
4332: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
4333: return '/adm/notfound.html';
4334: }
1.570 albertel 4335: close(FH);
1.1051 raeburn 4336: if ($resizewidth && $resizeheight) {
4337: my $mm = new File::MMagic;
4338: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
4339: if ($mime_type =~ m{^image/}) {
4340: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
4341: }
1.977 amueller 4342: }
1.258 www 4343: }
1.1152 raeburn 4344: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
4345: if (ref($mimetype)) {
4346: if ($$mimetype eq '') {
4347: my $mm = new File::MMagic;
4348: my $type = $mm->checktype_filename($filepath.'/'.$file);
4349: $$mimetype = $type;
4350: }
4351: }
4352: }
1.1172.2.109 raeburn 4353: if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152 raeburn 4354: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024 raeburn 4355: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
4356: $allfiles,$codebase);
4357: unless ($parse_result eq 'ok') {
4358: &logthis('Failed to parse '.$filepath.$file.
4359: ' for embedded media: '.$parse_result);
4360: }
1.637 raeburn 4361: }
1.1172.2.109 raeburn 4362: } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
4363: my $format = $env{'form.scantron_format'};
4364: &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637 raeburn 4365: }
1.860 raeburn 4366: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
4367: my $input = $filepath.'/'.$file;
4368: my $output = $filepath.'/'.'tn-'.$file;
1.1172.2.146. .13(raeb 4369:-23): my $makethumb;
1.860 raeburn 4370: my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1172.2.146. .13(raeb 4371:-23): if ($context eq 'toollogo') {
4372:-23): my ($fullwidth,$fullheight) = &check_dimensions($input);
4373:-23): if ($fullwidth ne '' && $fullheight ne '') {
4374:-23): if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
4375:-23): $makethumb = 1;
4376:-23): }
4377:-23): }
4378:-23): } else {
4379:-23): $makethumb = 1;
4380:-23): }
4381:-23): if ($makethumb) {
4382:-23): my @args = ('convert','-sample',$thumbsize,$input,$output);
4383:-23): system({$args[0]} @args);
4384:-23): if (-e $filepath.'/'.'tn-'.$file) {
4385:-23): $fetchthumb = 1;
4386:-23): }
1.860 raeburn 4387: }
4388: }
1.858 raeburn 4389:
1.259 www 4390: # Notify homeserver to grep it
4391: #
1.984 neumanie 4392: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 4393: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 4394: if ($fetchresult eq 'ok') {
1.860 raeburn 4395: if ($fetchthumb) {
4396: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
4397: if ($thumbresult ne 'ok') {
4398: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
4399: $docuhome.': '.$thumbresult);
4400: }
4401: }
1.259 www 4402: #
1.258 www 4403: # Return the URL to it
1.494 albertel 4404: return '/uploaded/'.$path.$file;
1.263 www 4405: } else {
1.494 albertel 4406: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
4407: ': '.$fetchresult);
1.263 www 4408: return '/adm/notfound.html';
1.858 raeburn 4409: }
1.493 albertel 4410: }
4411:
1.637 raeburn 4412: sub extract_embedded_items {
1.961 raeburn 4413: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 4414: my @state = ();
1.1164 raeburn 4415: my (%lastids,%related,%shockwave,%flashvars);
1.637 raeburn 4416: my %javafiles = (
4417: codebase => '',
4418: code => '',
4419: archive => ''
4420: );
4421: my %mediafiles = (
4422: src => '',
4423: movie => '',
4424: );
1.648 raeburn 4425: my $p;
4426: if ($content) {
4427: $p = HTML::LCParser->new($content);
4428: } else {
1.961 raeburn 4429: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 4430: }
1.641 albertel 4431: while (my $t=$p->get_token()) {
1.640 albertel 4432: if ($t->[0] eq 'S') {
4433: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 4434: push(@state, $tagname);
1.648 raeburn 4435: if (lc($tagname) eq 'allow') {
4436: &add_filetype($allfiles,$attr->{'src'},'src');
4437: }
1.640 albertel 4438: if (lc($tagname) eq 'img') {
4439: &add_filetype($allfiles,$attr->{'src'},'src');
4440: }
1.886 albertel 4441: if (lc($tagname) eq 'a') {
1.1172.2.24 raeburn 4442: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
4443: &add_filetype($allfiles,$attr->{'href'},'href');
4444: }
1.886 albertel 4445: }
1.645 raeburn 4446: if (lc($tagname) eq 'script') {
1.1164 raeburn 4447: my $src;
1.645 raeburn 4448: if ($attr->{'archive'} =~ /\.jar$/i) {
4449: &add_filetype($allfiles,$attr->{'archive'},'archive');
4450: } else {
1.1164 raeburn 4451: if ($attr->{'src'} ne '') {
4452: $src = $attr->{'src'};
4453: &add_filetype($allfiles,$src,'src');
4454: }
4455: }
4456: my $text = $p->get_trimmed_text();
4457: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
4458: my @swfargs = split(/,/,$1);
4459: foreach my $item (@swfargs) {
4460: $item =~ s/["']//g;
4461: $item =~ s/^\s+//;
4462: $item =~ s/\s+$//;
4463: }
4464: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
4465: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
4466: push(@{$related{$swfargs[0]}},$swfargs[2]);
4467: } else {
4468: $related{$swfargs[0]} = [$swfargs[2]];
4469: }
4470: }
1.645 raeburn 4471: }
4472: }
4473: if (lc($tagname) eq 'link') {
4474: if (lc($attr->{'rel'}) eq 'stylesheet') {
4475: &add_filetype($allfiles,$attr->{'href'},'href');
4476: }
4477: }
1.640 albertel 4478: if (lc($tagname) eq 'object' ||
4479: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
4480: foreach my $item (keys(%javafiles)) {
4481: $javafiles{$item} = '';
4482: }
1.1164 raeburn 4483: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
4484: $lastids{lc($tagname)} = $attr->{'id'};
4485: }
1.640 albertel 4486: }
4487: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
4488: my $name = lc($attr->{'name'});
4489: foreach my $item (keys(%javafiles)) {
4490: if ($name eq $item) {
4491: $javafiles{$item} = $attr->{'value'};
4492: last;
4493: }
4494: }
1.1164 raeburn 4495: my $pathfrom;
1.640 albertel 4496: foreach my $item (keys(%mediafiles)) {
4497: if ($name eq $item) {
1.1164 raeburn 4498: $pathfrom = $attr->{'value'};
4499: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
4500: &add_filetype($allfiles,$pathfrom,$name);
1.640 albertel 4501: last;
4502: }
4503: }
1.1164 raeburn 4504: if ($name eq 'flashvars') {
4505: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
4506: }
4507: if ($pathfrom ne '') {
4508: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
4509: $pathfrom);
4510: }
1.640 albertel 4511: }
4512: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
4513: foreach my $item (keys(%javafiles)) {
4514: if ($attr->{$item}) {
4515: $javafiles{$item} = $attr->{$item};
4516: last;
4517: }
4518: }
4519: foreach my $item (keys(%mediafiles)) {
4520: if ($attr->{$item}) {
4521: &add_filetype($allfiles,$attr->{$item},$item);
4522: last;
4523: }
4524: }
1.1164 raeburn 4525: if (lc($tagname) eq 'embed') {
4526: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
4527: &embedded_dependency($allfiles,\%related,$attr->{'name'},
4528: $attr->{'src'});
4529: }
4530: }
1.640 albertel 4531: }
1.1172.2.35 raeburn 4532: if (lc($tagname) eq 'iframe') {
4533: my $src = $attr->{'src'} ;
4534: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
4535: &add_filetype($allfiles,$src,'src');
4536: } elsif ($src =~ m{^/}) {
4537: if ($env{'request.course.id'}) {
4538: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
4539: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
4540: my $url = &hreflocation('',$fullpath);
4541: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
4542: my $relpath = $1;
4543: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
4544: &add_filetype($allfiles,$1,'src');
4545: }
4546: }
4547: }
4548: }
4549: }
1.1164 raeburn 4550: if ($t->[4] =~ m{/>$}) {
1.1172.2.35 raeburn 4551: pop(@state);
1.1164 raeburn 4552: }
1.640 albertel 4553: } elsif ($t->[0] eq 'E') {
4554: my ($tagname) = ($t->[1]);
4555: if ($javafiles{'codebase'} ne '') {
4556: $javafiles{'codebase'} .= '/';
4557: }
4558: if (lc($tagname) eq 'applet' ||
4559: lc($tagname) eq 'object' ||
4560: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
4561: ) {
4562: foreach my $item (keys(%javafiles)) {
4563: if ($item ne 'codebase' && $javafiles{$item} ne '') {
4564: my $file=$javafiles{'codebase'}.$javafiles{$item};
4565: &add_filetype($allfiles,$file,$item);
4566: }
4567: }
4568: }
4569: pop @state;
4570: }
4571: }
1.1164 raeburn 4572: foreach my $id (sort(keys(%flashvars))) {
4573: if ($shockwave{$id} ne '') {
4574: my @pairs = split(/\&/,$flashvars{$id});
4575: foreach my $pair (@pairs) {
4576: my ($key,$value) = split(/\=/,$pair);
4577: if ($key eq 'thumb') {
4578: &add_filetype($allfiles,$value,$key);
4579: } elsif ($key eq 'content') {
4580: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
4581: my ($ext) = ($value =~ /\.([^.]+)$/);
4582: if ($ext ne '') {
4583: &add_filetype($allfiles,$path.$value,$ext);
4584: }
4585: }
4586: }
4587: }
4588: }
1.637 raeburn 4589: return 'ok';
4590: }
4591:
1.639 albertel 4592: sub add_filetype {
4593: my ($allfiles,$file,$type)=@_;
4594: if (exists($allfiles->{$file})) {
4595: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
4596: push(@{$allfiles->{$file}}, &escape($type));
4597: }
4598: } else {
4599: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 4600: }
4601: }
4602:
1.1164 raeburn 4603: sub embedded_dependency {
4604: my ($allfiles,$related,$identifier,$pathfrom) = @_;
4605: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
4606: if (($identifier ne '') &&
4607: (ref($related->{$identifier}) eq 'ARRAY') &&
4608: ($pathfrom ne '')) {
4609: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
4610: foreach my $dep (@{$related->{$identifier}}) {
4611: &add_filetype($allfiles,$path.$dep,'object');
4612: }
4613: }
4614: }
4615: return;
4616: }
4617:
1.1172.2.146. .13(raeb 4618:-23): sub check_dimensions {
4619:-23): my ($inputfile) = @_;
4620:-23): my ($fullwidth,$fullheight);
4621:-23): if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
4622:-23): my $mm = new File::MMagic;
4623:-23): my $mime_type = $mm->checktype_filename($inputfile);
4624:-23): if ($mime_type =~ m{^image/}) {
4625:-23): if (open(PIPE,"identify $inputfile 2>&1 |")) {
4626:-23): my $imageinfo = <PIPE>;
4627:-23): if (!close(PIPE)) {
4628:-23): &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
4629:-23): }
4630:-23): chomp($imageinfo);
4631:-23): my ($fullsize) =
4632:-23): ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
4633:-23): if ($fullsize) {
4634:-23): ($fullwidth,$fullheight) = split(/x/,$fullsize);
4635:-23): }
4636:-23): }
4637:-23): }
4638:-23): }
4639:-23): return ($fullwidth,$fullheight);
4640:-23): }
4641:-23):
1.1172.2.109 raeburn 4642: sub bubblesheet_converter {
4643: my ($cdom,$fullpath,$config,$format) = @_;
4644: if ((&domain($cdom) ne '') &&
4645: ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
4646: (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
4647: my (%csvcols,%csvoptions);
4648: if (ref($config->{'fields'}) eq 'HASH') {
4649: %csvcols = %{$config->{'fields'}};
4650: }
4651: if (ref($config->{'options'}) eq 'HASH') {
4652: %csvoptions = %{$config->{'options'}};
4653: }
4654: my %csvbynum = reverse(%csvcols);
4655: my %scantronconf = &get_scantron_config($format,$cdom);
4656: if (keys(%scantronconf)) {
4657: my %bynum = (
4658: $scantronconf{CODEstart} => 'CODEstart',
4659: $scantronconf{IDstart} => 'IDstart',
4660: $scantronconf{PaperID} => 'PaperID',
4661: $scantronconf{FirstName} => 'FirstName',
4662: $scantronconf{LastName} => 'LastName',
4663: $scantronconf{Qstart} => 'Qstart',
4664: );
4665: my @ordered;
4666: foreach my $item (sort { $a <=> $b } keys(%bynum)) {
4667: push(@ordered,$bynum{$item});
4668: }
4669: my %mapstart = (
4670: CODEstart => 'CODE',
4671: IDstart => 'ID',
4672: PaperID => 'PaperID',
4673: FirstName => 'FirstName',
4674: LastName => 'LastName',
4675: Qstart => 'FirstQuestion',
4676: );
4677: my %maplength = (
4678: CODEstart => 'CODElength',
4679: IDstart => 'IDlength',
4680: PaperID => 'PaperIDlength',
4681: FirstName => 'FirstNamelength',
4682: LastName => 'LastNamelength',
4683: );
4684: if (open(my $fh,'<',$fullpath)) {
4685: my $output;
4686: my %lettdig = &letter_to_digits();
4687: my %diglett = reverse(%lettdig);
4688: my $numletts = scalar(keys(%lettdig));
4689: my $num = 0;
4690: while (my $line=<$fh>) {
4691: $num ++;
4692: next if (($num == 1) && ($csvoptions{'hdr'} == 1));
4693: $line =~ s{[\r\n]+$}{};
4694: my %found;
1.1172.2.143 raeburn 4695: my @values = split(/,/,$line,-1);
1.1172.2.109 raeburn 4696: my ($qstart,$record);
4697: for (my $i=0; $i<@values; $i++) {
4698: if ((($qstart ne '') && ($i > $qstart)) ||
4699: ($csvbynum{$i} eq 'FirstQuestion')) {
4700: if ($values[$i] eq '') {
4701: $values[$i] = $scantronconf{'Qoff'};
4702: } elsif ($scantronconf{'Qon'} eq 'number') {
4703: if ($values[$i] =~ /^[A-Ja-j]$/) {
4704: $values[$i] = $lettdig{uc($values[$i])};
4705: }
4706: } elsif ($scantronconf{'Qon'} eq 'letter') {
4707: if ($values[$i] =~ /^[0-9]$/) {
4708: $values[$i] = $diglett{$values[$i]};
4709: }
4710: } else {
4711: if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
4712: my $digit;
4713: if ($values[$i] =~ /^[A-Ja-j]$/) {
4714: $digit = $lettdig{uc($values[$i])}-1;
4715: if ($values[$i] eq 'J') {
4716: $digit += $numletts;
4717: }
4718: } elsif ($values[$i] =~ /^[0-9]$/) {
4719: $digit = $values[$i]-1;
4720: if ($values[$i] eq '0') {
4721: $digit += $numletts;
4722: }
4723: }
4724: my $qval='';
4725: for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
4726: if ($j == $digit) {
4727: $qval .= $scantronconf{'Qon'};
4728: } else {
4729: $qval .= $scantronconf{'Qoff'};
4730: }
4731: }
4732: $values[$i] = $qval;
4733: }
4734: }
4735: if (length($values[$i]) > $scantronconf{'Qlength'}) {
4736: $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
4737: }
4738: my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
4739: if ($numblank > 0) {
4740: $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
4741: }
4742: if ($csvbynum{$i} eq 'FirstQuestion') {
4743: $qstart = $i;
4744: $found{$csvbynum{$i}} = $values[$i];
4745: } else {
4746: $found{'FirstQuestion'} .= $values[$i];
4747: }
4748: } elsif (exists($csvbynum{$i})) {
4749: if ($csvoptions{'rem'}) {
4750: $values[$i] =~ s/^\s+//;
4751: }
4752: if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
4753: while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
4754: $values[$i] = '0'.$values[$i];
4755: }
4756: }
4757: $found{$csvbynum{$i}} = $values[$i];
4758: }
4759: }
4760: foreach my $item (@ordered) {
4761: my $currlength = 1+length($record);
4762: my $numspaces = $scantronconf{$item} - $currlength;
4763: if ($numspaces > 0) {
4764: $record .= (' ' x $numspaces);
4765: }
4766: if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
4767: unless ($item eq 'Qstart') {
4768: if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
4769: $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
4770: }
4771: }
4772: $record .= $found{$mapstart{$item}};
4773: }
4774: }
4775: $output .= "$record\n";
4776: }
4777: close($fh);
4778: if ($output) {
4779: if (open(my $fh,'>',$fullpath)) {
4780: print $fh $output;
4781: close($fh);
4782: }
4783: }
4784: }
4785: }
4786: return;
4787: }
4788: }
4789:
4790: sub letter_to_digits {
4791: my %lettdig = (
4792: A => 1,
4793: B => 2,
4794: C => 3,
4795: D => 4,
4796: E => 5,
4797: F => 6,
4798: G => 7,
4799: H => 8,
4800: I => 9,
4801: J => 0,
4802: );
4803: return %lettdig;
4804: }
4805:
4806: sub get_scantron_config {
4807: my ($which,$cdom) = @_;
4808: my @lines = &get_scantronformat_file($cdom);
4809: my %config;
4810: #FIXME probably should move to XML it has already gotten a bit much now
4811: foreach my $line (@lines) {
4812: my ($name,$descrip)=split(/:/,$line);
4813: if ($name ne $which ) { next; }
4814: chomp($line);
4815: my @config=split(/:/,$line);
4816: $config{'name'}=$config[0];
4817: $config{'description'}=$config[1];
4818: $config{'CODElocation'}=$config[2];
4819: $config{'CODEstart'}=$config[3];
4820: $config{'CODElength'}=$config[4];
4821: $config{'IDstart'}=$config[5];
4822: $config{'IDlength'}=$config[6];
4823: $config{'Qstart'}=$config[7];
4824: $config{'Qlength'}=$config[8];
4825: $config{'Qoff'}=$config[9];
4826: $config{'Qon'}=$config[10];
4827: $config{'PaperID'}=$config[11];
4828: $config{'PaperIDlength'}=$config[12];
4829: $config{'FirstName'}=$config[13];
4830: $config{'FirstNamelength'}=$config[14];
4831: $config{'LastName'}=$config[15];
4832: $config{'LastNamelength'}=$config[16];
4833: $config{'BubblesPerRow'}=$config[17];
4834: last;
4835: }
4836: return %config;
4837: }
4838:
4839: sub get_scantronformat_file {
4840: my ($cdom) = @_;
4841: if ($cdom eq '') {
4842: $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
4843: }
4844: my %domconfig = &get_dom('configuration',['scantron'],$cdom);
4845: my $gottab = 0;
4846: my @lines;
4847: if (ref($domconfig{'scantron'}) eq 'HASH') {
4848: if ($domconfig{'scantron'}{'scantronformat'} ne '') {
4849: my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
4850: if ($formatfile ne '-1') {
4851: @lines = split("\n",$formatfile,-1);
4852: $gottab = 1;
4853: }
4854: }
4855: }
4856: if (!$gottab) {
4857: my $confname = $cdom.'-domainconfig';
4858: my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
4859: my $formatfile = &getfile($default);
4860: if ($formatfile ne '-1') {
4861: @lines = split("\n",$formatfile,-1);
4862: $gottab = 1;
4863: }
4864: }
4865: if (!$gottab) {
4866: my @domains = ¤t_machine_domains();
4867: if (grep(/^\Q$cdom\E$/,@domains)) {
4868: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
4869: @lines = <$fh>;
4870: close($fh);
4871: }
4872: } else {
4873: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
4874: @lines = <$fh>;
4875: close($fh);
4876: }
4877: }
1.1172.2.146. .8(raebu 4878:23): chomp(@lines);
1.1172.2.109 raeburn 4879: }
4880: return @lines;
4881: }
4882:
1.493 albertel 4883: sub removeuploadedurl {
1.984 neumanie 4884: my ($url)=@_;
4885: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 4886: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 4887: }
4888:
4889: sub removeuserfile {
4890: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 4891: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4892: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 4893: if ($result eq 'ok') {
1.798 raeburn 4894: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
4895: my $metafile = $fname.'.meta';
4896: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 4897: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 4898: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4899: my $sqlresult =
1.823 albertel 4900: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4901: 'portfolio_metadata',$group,
4902: 'delete');
1.798 raeburn 4903: }
4904: }
4905: return $result;
1.257 www 4906: }
1.15 www 4907:
1.530 albertel 4908: sub mkdiruserfile {
4909: my ($docuname,$docudom,$dir)=@_;
4910: my $home=&homeserver($docuname,$docudom);
4911: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
4912: }
4913:
1.531 albertel 4914: sub renameuserfile {
4915: my ($docuname,$docudom,$old,$new)=@_;
4916: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4917: my $result = &reply("renameuserfile:$docudom:$docuname:".
4918: &escape("$old").':'.&escape("$new"),$home);
4919: if ($result eq 'ok') {
4920: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4921: my $oldmeta = $old.'.meta';
4922: my $newmeta = $new.'.meta';
4923: my $metaresult =
4924: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 4925: my $url = "/uploaded/$docudom/$docuname/$old";
4926: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4927: my $sqlresult =
1.823 albertel 4928: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4929: 'portfolio_metadata',$group,
4930: 'delete');
1.798 raeburn 4931: }
4932: }
4933: return $result;
1.531 albertel 4934: }
4935:
1.14 www 4936: # ------------------------------------------------------------------------- Log
4937:
4938: sub log {
4939: my ($dom,$nam,$hom,$what)=@_;
1.47 www 4940: return critical("log:$dom:$nam:$what",$hom);
1.157 www 4941: }
4942:
4943: # ------------------------------------------------------------------ Course Log
1.352 www 4944: #
4945: # This routine flushes several buffers of non-mission-critical nature
4946: #
1.157 www 4947:
4948: sub flushcourselogs {
1.352 www 4949: &logthis('Flushing log buffers');
4950: #
4951: # course logs
4952: # This is a log of all transactions in a course, which can be used
4953: # for data mining purposes
4954: #
4955: # It also collects the courseid database, which lists last transaction
4956: # times and course titles for all courseids
4957: #
4958: my %courseidbuffer=();
1.921 raeburn 4959: foreach my $crsid (keys(%courselogs)) {
1.352 www 4960: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 4961: &escape($courselogs{$crsid}),
4962: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 4963: delete $courselogs{$crsid};
4964: } else {
4965: &logthis('Failed to flush log buffer for '.$crsid);
4966: if (length($courselogs{$crsid})>40000) {
1.672 albertel 4967: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 4968: " exceeded maximum size, deleting.</font>");
4969: delete $courselogs{$crsid};
4970: }
1.352 www 4971: }
1.920 raeburn 4972: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 4973: 'description' => $coursedescrbuf{$crsid},
4974: 'inst_code' => $courseinstcodebuf{$crsid},
4975: 'type' => $coursetypebuf{$crsid},
4976: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 4977: };
1.191 harris41 4978: }
1.352 www 4979: #
4980: # Write course id database (reverse lookup) to homeserver of courses
4981: # Is used in pickcourse
4982: #
1.840 albertel 4983: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 4984: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 4985: $courseidbuffer{$crs_home},
4986: $crs_home,'timeonly');
1.352 www 4987: }
4988: #
4989: # File accesses
4990: # Writes to the dynamic metadata of resources to get hit counts, etc.
4991: #
1.449 matthew 4992: foreach my $entry (keys(%accesshash)) {
1.458 matthew 4993: if ($entry =~ /___count$/) {
4994: my ($dom,$name);
1.807 albertel 4995: ($dom,$name,undef)=
1.811 albertel 4996: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 4997: if (! defined($dom) || $dom eq '' ||
4998: ! defined($name) || $name eq '') {
1.620 albertel 4999: my $cid = $env{'request.course.id'};
1.1172.2.142 raeburn 5000: #
5001: # FIXME 11/29/2021
5002: # Typo in rev. 1.458 (2003/12/09)??
5003: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
5004: #
1.1172.2.146. .13(raeb 5005:-23): # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
1.1172.2.142 raeburn 5006: # $dom and $name will always be null, so the &inc() call will default to storing this data
5007: # in a nohist_accesscount.db file for the user rather than the course.
5008: #
5009: # That said there is a lot of noise in the data being stored.
5010: # So counts for prtspool/ and adm/ etc. are recorded.
5011: #
5012: # A review of which items ending '___count' are written to %accesshash should likely be
5013: # made before deciding whether to set these to 'course.' instead of 'request.'
5014: #
5015: # Under the current scheme each user receives a nohist_accesscount.db file listing
5016: # accesses for things which are not published resources, regardless of course, and
5017: # there is not a nohist_accesscount.db file in a course, which might log accesses from
5018: # anyone in the course for things which are not published resources.
5019: #
5020: # For an author, nohist_accesscount.db ends up having records for other items
5021: # mixed up with the legitimate access counts for the author's published resources.
5022: #
1.620 albertel 5023: $dom = $env{'request.'.$cid.'.domain'};
5024: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 5025: }
1.450 matthew 5026: my $value = $accesshash{$entry};
5027: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
5028: my %temphash=($url => $value);
1.449 matthew 5029: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
5030: if ($result eq 'ok') {
5031: delete $accesshash{$entry};
5032: }
5033: } else {
1.811 albertel 5034: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159 www 5035: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450 matthew 5036: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 5037: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
5038: delete $accesshash{$entry};
5039: }
1.185 www 5040: }
1.191 harris41 5041: }
1.352 www 5042: #
5043: # Roles
5044: # Reverse lookup of user roles for course faculty/staff and co-authorship
5045: #
1.800 albertel 5046: foreach my $entry (keys(%userrolehash)) {
1.351 www 5047: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 5048: split(/\:/,$entry);
5049: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 5050: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 5051: $rudom,$runame) eq 'ok') {
5052: delete $userrolehash{$entry};
5053: }
5054: }
1.662 raeburn 5055: #
1.1172.2.90 raeburn 5056: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662 raeburn 5057: #
5058: my %domrolebuffer = ();
1.1000 raeburn 5059: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 5060: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 5061: if ($domrolebuffer{$rudom}) {
5062: $domrolebuffer{$rudom}.='&'.&escape($entry).
5063: '='.&escape($domainrolehash{$entry});
5064: } else {
5065: $domrolebuffer{$rudom}.=&escape($entry).
5066: '='.&escape($domainrolehash{$entry});
5067: }
5068: delete $domainrolehash{$entry};
5069: }
5070: foreach my $dom (keys(%domrolebuffer)) {
1.1172.2.82 raeburn 5071: my %servers;
5072: if (defined(&domain($dom,'primary'))) {
5073: my $primary=&domain($dom,'primary');
5074: my $hostname=&hostname($primary);
5075: $servers{$primary} = $hostname;
5076: } else {
5077: %servers = &get_servers($dom,'library');
5078: }
1.841 albertel 5079: foreach my $tryserver (keys(%servers)) {
1.1172.2.82 raeburn 5080: if (&reply('domroleput:'.$dom.':'.
5081: $domrolebuffer{$dom},$tryserver) eq 'ok') {
5082: last;
5083: } else {
1.841 albertel 5084: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
5085: }
1.662 raeburn 5086: }
5087: }
1.186 www 5088: $dumpcount++;
1.157 www 5089: }
5090:
5091: sub courselog {
5092: my $what=shift;
1.158 www 5093: $what=time.':'.$what;
1.620 albertel 5094: unless ($env{'request.course.id'}) { return ''; }
5095: $coursedombuf{$env{'request.course.id'}}=
5096: $env{'course.'.$env{'request.course.id'}.'.domain'};
5097: $coursenumbuf{$env{'request.course.id'}}=
5098: $env{'course.'.$env{'request.course.id'}.'.num'};
5099: $coursehombuf{$env{'request.course.id'}}=
5100: $env{'course.'.$env{'request.course.id'}.'.home'};
5101: $coursedescrbuf{$env{'request.course.id'}}=
5102: $env{'course.'.$env{'request.course.id'}.'.description'};
5103: $courseinstcodebuf{$env{'request.course.id'}}=
5104: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
5105: $courseownerbuf{$env{'request.course.id'}}=
5106: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 5107: $coursetypebuf{$env{'request.course.id'}}=
5108: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 5109: if (defined $courselogs{$env{'request.course.id'}}) {
5110: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 5111: } else {
1.620 albertel 5112: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 5113: }
1.620 albertel 5114: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 5115: &flushcourselogs();
5116: }
1.158 www 5117: }
5118:
5119: sub courseacclog {
5120: my $fnsymb=shift;
1.620 albertel 5121: unless ($env{'request.course.id'}) { return ''; }
5122: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144 www 5123: if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187 www 5124: $what.=':POST';
1.583 matthew 5125: # FIXME: Probably ought to escape things....
1.800 albertel 5126: foreach my $key (keys(%env)) {
5127: if ($key=~/^form\.(.*)/) {
1.975 raeburn 5128: my $formitem = $1;
5129: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
5130: $what.=':'.$formitem.'='.$env{$key};
5131: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1172.2.131 raeburn 5132: if ($formitem eq 'proctorpassword') {
1.1172.2.132 raeburn 5133: $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1172.2.131 raeburn 5134: } else {
5135: $what.=':'.$formitem.'='.$env{$key};
5136: }
1.975 raeburn 5137: }
1.158 www 5138: }
1.191 harris41 5139: }
1.583 matthew 5140: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
5141: # FIXME: We should not be depending on a form parameter that someone
5142: # editing lonsearchcat.pm might change in the future.
1.620 albertel 5143: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 5144: $what.= ':POST';
5145: # FIXME: Probably ought to escape things....
5146: foreach my $element ('courseexp','crsfulltext','crsrelated',
5147: 'crsdiscuss') {
1.620 albertel 5148: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 5149: }
5150: }
1.158 www 5151: }
5152: &courselog($what);
1.149 www 5153: }
5154:
1.185 www 5155: sub countacc {
5156: my $url=&declutter(shift);
1.458 matthew 5157: return if (! defined($url) || $url eq '');
1.620 albertel 5158: unless ($env{'request.course.id'}) { return ''; }
1.1158 www 5159: #
5160: # Mark that this url was used in this course
5161: #
1.620 albertel 5162: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158 www 5163: #
5164: # Increase the access count for this resource in this child process
5165: #
1.281 www 5166: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 5167: $accesshash{$key}++;
1.185 www 5168: }
1.349 www 5169:
1.361 www 5170: sub linklog {
5171: my ($from,$to)=@_;
5172: $from=&declutter($from);
5173: $to=&declutter($to);
5174: $accesshash{$from.'___'.$to.'___comefrom'}=1;
5175: $accesshash{$to.'___'.$from.'___goto'}=1;
5176: }
1.1160 www 5177:
5178: sub statslog {
5179: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
5180: if ($users<2) { return; }
5181: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
5182: 'course' => $env{'request.course.id'},
5183: 'sections' => '"all"',
5184: 'num_students' => $users,
5185: 'part' => $part,
5186: 'symb' => $symb,
5187: 'mean_tries' => $av_attempts,
5188: 'deg_of_diff' => $degdiff});
5189: foreach my $key (keys(%dynstore)) {
5190: $accesshash{$key}=$dynstore{$key};
5191: }
5192: }
1.361 www 5193:
1.349 www 5194: sub userrolelog {
5195: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169 droeschl 5196: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350 www 5197: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5198: $userrolehash
5199: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 5200: =$tend.':'.$tstart;
1.662 raeburn 5201: }
1.1169 droeschl 5202: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898 albertel 5203: $userrolehash
5204: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
5205: =$tend.':'.$tstart;
5206: }
1.1172.2.90 raeburn 5207: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662 raeburn 5208: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5209: $domainrolehash
5210: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
5211: = $tend.':'.$tstart;
5212: }
1.351 www 5213: }
5214:
1.957 raeburn 5215: sub courserolelog {
5216: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1172.2.9 raeburn 5217: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
5218: my $cdom = $1;
5219: my $cnum = $2;
5220: my $sec = $3;
5221: my $namespace = 'rolelog';
5222: my %storehash = (
5223: role => $trole,
5224: start => $tstart,
5225: end => $tend,
5226: selfenroll => $selfenroll,
5227: context => $context,
5228: );
5229: if ($trole eq 'gr') {
5230: $namespace = 'groupslog';
5231: $storehash{'group'} = $sec;
5232: } else {
5233: $storehash{'section'} = $sec;
1.1172.2.146. .16(raeb 5234:-23): my ($curruserdomstr,$newuserdomstr);
5235:-23): if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'})) {
5236:-23): $curruserdomstr = $env{'course.'.$env{'request.course.id'}.'.internal.userdomains'};
5237:-23): } else {
5238:-23): my %courseinfo = &coursedescription($cdom.'/'.$cnum);
5239:-23): $curruserdomstr = $courseinfo{'internal.userdomains'};
5240:-23): }
5241:-23): if ($curruserdomstr ne '') {
5242:-23): my @udoms = split(/,/,$curruserdomstr);
5243:-23): unless (grep(/^\Q$domain\E/,@udoms)) {
5244:-23): push(@udoms,$domain);
5245:-23): $newuserdomstr = join(',',sort(@udoms));
5246:-23): }
5247:-23): } else {
5248:-23): $newuserdomstr = $domain;
5249:-23): }
5250:-23): if ($newuserdomstr ne '') {
5251:-23): my $putresult = &put('environment',{ 'internal.userdomains' => $newuserdomstr },
5252:-23): $cdom,$cnum);
5253:-23): if ($putresult eq 'ok') {
5254:-23): unless (($selfenroll) || ($context eq 'selfenroll')) {
5255:-23): if (($context eq 'createcourse') || ($context eq 'requestcourses') ||
5256:-23): ($context eq 'automated') || ($context eq 'domain')) {
5257:-23): $env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'} = $newuserdomstr;
5258:-23): } elsif ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
5259:-23): &appenv({'course.'.$cdom.'_'.$cnum.'.internal.userdomains' => $newuserdomstr});
5260:-23): }
5261:-23): }
5262:-23): }
5263:-23): }
1.1172.2.9 raeburn 5264: }
5265: &write_log('course',$namespace,\%storehash,$delflag,$username,
5266: $domain,$cnum,$cdom);
5267: if (($trole ne 'st') || ($sec ne '')) {
5268: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957 raeburn 5269: }
5270: }
5271: return;
5272: }
5273:
1.1172.2.9 raeburn 5274: sub domainrolelog {
5275: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5276: if ($area =~ m{^/($match_domain)/$}) {
5277: my $cdom = $1;
5278: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
5279: my $namespace = 'rolelog';
5280: my %storehash = (
5281: role => $trole,
5282: start => $tstart,
5283: end => $tend,
5284: context => $context,
5285: );
5286: &write_log('domain',$namespace,\%storehash,$delflag,$username,
5287: $domain,$domconfiguser,$cdom);
5288: }
5289: return;
5290:
5291: }
5292:
5293: sub coauthorrolelog {
5294: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5295: if ($area =~ m{^/($match_domain)/($match_username)$}) {
5296: my $audom = $1;
5297: my $auname = $2;
5298: my $namespace = 'rolelog';
5299: my %storehash = (
5300: role => $trole,
5301: start => $tstart,
5302: end => $tend,
5303: context => $context,
5304: );
5305: &write_log('author',$namespace,\%storehash,$delflag,$username,
5306: $domain,$auname,$audom);
5307: }
5308: return;
5309: }
5310:
1.1172.2.146. .23(raeb 5311:-24): sub authorarchivelog {
5312:-24): my ($hashref,$size,$filesdest,$action) = @_;
5313:-24): my $lonprtdir = $Apache::lonnet::perlvar{'lonPrtDir'};
5314:-24): my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
5315:-24): $filesdest =~ s{^\Q$lonprtdir/\E}{};
5316:-24): if ($filesdest =~ m{^($match_username)_($match_domain)_archive_(\d+_\d+_\d+(|[.\w]+))$}) {
5317:-24): my ($auname,$audom,$id) = ($1,$2,$3);
5318:-24): if (ref($hashref) eq 'HASH') {
5319:-24): my $namespace = 'archivelog';
5320:-24): my $dir;
5321:-24): if ($hashref->{dir} =~ m{^\Q$londocroot/priv/$audom/$auname\E(.*)$}) {
5322:-24): $dir = $1;
5323:-24): }
5324:-24): my $delflag = 0;
5325:-24): my %storehash = (
5326:-24): id => $id,
5327:-24): dir => $dir,
5328:-24): files => $hashref->{numfiles},
5329:-24): subdirs => $hashref->{numdirs},
5330:-24): bytes => $hashref->{bytes},
5331:-24): size => $size,
5332:-24): action => $action,
5333:-24): );
5334:-24): if ($action eq 'delete') {
5335:-24): $delflag = 1;
5336:-24): }
5337:-24): &write_log('author',$namespace,\%storehash,$delflag,$auname,
5338:-24): $audom,$auname,$audom);
5339:-24): }
5340:-24): }
5341:-24): return;
5342:-24): }
5343:-24):
1.351 www 5344: sub get_course_adv_roles {
1.948 raeburn 5345: my ($cid,$codes) = @_;
1.620 albertel 5346: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 5347: my %coursehash=&coursedescription($cid);
1.988 raeburn 5348: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 5349: my %nothide=();
1.800 albertel 5350: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 5351: if ($user !~ /:/) {
5352: $nothide{join(':',split(/[\@]/,$user))}=1;
5353: } else {
5354: $nothide{$user}=1;
5355: }
1.470 www 5356: }
1.1172.2.23 raeburn 5357: my @possdoms = ($coursehash{'domain'});
5358: if ($coursehash{'checkforpriv'}) {
5359: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
5360: }
1.351 www 5361: my %returnhash=();
5362: my %dumphash=
5363: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
5364: my $now=time;
1.997 raeburn 5365: my %privileged;
1.1000 raeburn 5366: foreach my $entry (keys(%dumphash)) {
1.800 albertel 5367: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 5368: if (($tstart) && ($tstart<0)) { next; }
5369: if (($tend) && ($tend<$now)) { next; }
5370: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 5371: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 5372: if ($username eq '' || $domain eq '') { next; }
1.1172.2.23 raeburn 5373: if ((&privileged($username,$domain,\@possdoms)) &&
1.997 raeburn 5374: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 5375: if ($role eq 'cr') { next; }
1.948 raeburn 5376: if ($codes) {
5377: if ($section) { $role .= ':'.$section; }
5378: if ($returnhash{$role}) {
5379: $returnhash{$role}.=','.$username.':'.$domain;
5380: } else {
5381: $returnhash{$role}=$username.':'.$domain;
5382: }
1.351 www 5383: } else {
1.988 raeburn 5384: my $key=&plaintext($role,$crstype);
1.973 bisitz 5385: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 5386: if ($returnhash{$key}) {
5387: $returnhash{$key}.=','.$username.':'.$domain;
5388: } else {
5389: $returnhash{$key}=$username.':'.$domain;
5390: }
1.351 www 5391: }
1.948 raeburn 5392: }
1.400 www 5393: return %returnhash;
5394: }
5395:
5396: sub get_my_roles {
1.937 raeburn 5397: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 5398: unless (defined($uname)) { $uname=$env{'user.name'}; }
5399: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 5400: my (%dumphash,%nothide);
1.1086 raeburn 5401: if ($context eq 'userroles') {
1.1166 raeburn 5402: %dumphash = &dump('roles',$udom,$uname);
1.858 raeburn 5403: } else {
1.1172.2.23 raeburn 5404: %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 5405: if ($hidepriv) {
5406: my %coursehash=&coursedescription($udom.'_'.$uname);
5407: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
5408: if ($user !~ /:/) {
5409: $nothide{join(':',split(/[\@]/,$user))} = 1;
5410: } else {
5411: $nothide{$user} = 1;
5412: }
5413: }
5414: }
1.858 raeburn 5415: }
1.400 www 5416: my %returnhash=();
5417: my $now=time;
1.999 raeburn 5418: my %privileged;
1.800 albertel 5419: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 5420: my ($role,$tend,$tstart);
5421: if ($context eq 'userroles') {
1.1149 raeburn 5422: next if ($entry =~ /^rolesdef/);
1.867 raeburn 5423: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
5424: } else {
5425: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
5426: }
1.400 www 5427: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 5428: my $status = 'active';
1.939 raeburn 5429: if (($tend) && ($tend<=$now)) {
1.832 raeburn 5430: $status = 'previous';
5431: }
5432: if (($tstart) && ($now<$tstart)) {
5433: $status = 'future';
5434: }
5435: if (ref($types) eq 'ARRAY') {
5436: if (!grep(/^\Q$status\E$/,@{$types})) {
5437: next;
5438: }
5439: } else {
5440: if ($status ne 'active') {
5441: next;
5442: }
5443: }
1.867 raeburn 5444: my ($rolecode,$username,$domain,$section,$area);
5445: if ($context eq 'userroles') {
1.1172.2.9 raeburn 5446: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867 raeburn 5447: (undef,$domain,$username,$section) = split(/\//,$area);
5448: } else {
5449: ($role,$username,$domain,$section) = split(/\:/,$entry);
5450: }
1.832 raeburn 5451: if (ref($roledoms) eq 'ARRAY') {
5452: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
5453: next;
5454: }
5455: }
5456: if (ref($roles) eq 'ARRAY') {
5457: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 5458: if ($role =~ /^cr\//) {
5459: if (!grep(/^cr$/,@{$roles})) {
5460: next;
5461: }
1.1104 raeburn 5462: } elsif ($role =~ /^gr\//) {
5463: if (!grep(/^gr$/,@{$roles})) {
5464: next;
5465: }
1.922 raeburn 5466: } else {
5467: next;
5468: }
1.832 raeburn 5469: }
1.867 raeburn 5470: }
1.937 raeburn 5471: if ($hidepriv) {
1.1172.2.23 raeburn 5472: my @privroles = ('dc','su');
1.999 raeburn 5473: if ($context eq 'userroles') {
1.1172.2.23 raeburn 5474: next if (grep(/^\Q$role\E$/,@privroles));
1.999 raeburn 5475: } else {
1.1172.2.23 raeburn 5476: my $possdoms = [$domain];
5477: if (ref($roledoms) eq 'ARRAY') {
5478: push(@{$possdoms},@{$roledoms});
1.999 raeburn 5479: }
1.1172.2.23 raeburn 5480: if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999 raeburn 5481: if (!$nothide{$username.':'.$domain}) {
5482: next;
5483: }
5484: }
1.937 raeburn 5485: }
5486: }
1.933 raeburn 5487: if ($withsec) {
5488: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
5489: $tstart.':'.$tend;
5490: } else {
5491: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
5492: }
1.832 raeburn 5493: }
1.373 www 5494: return %returnhash;
1.399 www 5495: }
5496:
1.1172.2.89 raeburn 5497: sub get_all_adhocroles {
5498: my ($dom) = @_;
5499: my @roles_by_num = ();
5500: my %domdefaults = &get_domain_defaults($dom);
5501: my (%description,%access_in_dom,%access_info);
5502: if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
5503: my $count = 0;
5504: my %domcurrent = %{$domdefaults{'adhocroles'}};
5505: my %ordered;
5506: foreach my $role (sort(keys(%domcurrent))) {
5507: my ($order,$desc,$access_in_dom);
5508: if (ref($domcurrent{$role}) eq 'HASH') {
5509: $order = $domcurrent{$role}{'order'};
5510: $desc = $domcurrent{$role}{'desc'};
5511: $access_in_dom{$role} = $domcurrent{$role}{'access'};
5512: $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
5513: }
5514: if ($order eq '') {
5515: $order = $count;
5516: }
5517: $ordered{$order} = $role;
5518: if ($desc ne '') {
5519: $description{$role} = $desc;
5520: } else {
5521: $description{$role}= $role;
5522: }
5523: $count++;
5524: }
5525: foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
5526: push(@roles_by_num,$ordered{$item});
5527: }
5528: }
5529: return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
5530: }
5531:
5532: sub get_my_adhocroles {
5533: my ($cid,$checkreg) = @_;
5534: my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
5535: if ($env{'request.course.id'} eq $cid) {
5536: $cdom = $env{'course.'.$cid.'.domain'};
5537: $cnum = $env{'course.'.$cid.'.num'};
5538: $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
5539: } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
5540: $cdom = $1;
5541: $cnum = $2;
5542: %info = &Apache::lonnet::get('environment',['internal.coursecode'],
5543: $cdom,$cnum);
5544: }
5545: if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
5546: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5547: my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
5548: if ($rosterhash{$user} ne '') {
5549: my $type = (split(/:/,$rosterhash{$user}))[5];
5550: return ([],{}) if ($type eq 'auto');
5551: }
5552: }
5553: if (($cdom ne '') && ($cnum ne '')) {
1.1172.2.90 raeburn 5554: if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1172.2.89 raeburn 5555: my $then=$env{'user.login.time'};
5556: my $update=$env{'user.update.time'};
1.1172.2.90 raeburn 5557: if (!$update) {
5558: $update = $then;
5559: }
5560: my @liveroles;
5561: foreach my $role ('dh','da') {
5562: if ($env{"user.role.$role./$cdom/"}) {
5563: my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
5564: my $limit = $update;
5565: if ($env{'request.role'} eq "$role./$cdom/") {
5566: $limit = $then;
5567: }
5568: my $activerole = 1;
5569: if ($tstart && $tstart>$limit) { $activerole = 0; }
5570: if ($tend && $tend <$limit) { $activerole = 0; }
5571: if ($activerole) {
5572: push(@liveroles,$role);
5573: }
5574: }
5575: }
5576: if (@liveroles) {
1.1172.2.89 raeburn 5577: if (&homeserver($cnum,$cdom) ne 'no_host') {
5578: my ($accessref,$accessinfo,%access_in_dom);
5579: ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
5580: if (ref($roles_by_num) eq 'ARRAY') {
5581: if (@{$roles_by_num}) {
5582: my %settings;
5583: if ($env{'request.course.id'} eq $cid) {
5584: foreach my $envkey (keys(%env)) {
5585: if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
5586: $settings{$1} = $env{$envkey};
5587: }
5588: }
5589: } else {
5590: %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
5591: }
5592: my %setincrs;
5593: if ($settings{'internal.adhocaccess'}) {
5594: map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
5595: }
5596: my @statuses;
5597: if ($env{'environment.inststatus'}) {
5598: @statuses = split(/,/,$env{'environment.inststatus'});
5599: }
5600: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5601: if (ref($accessref) eq 'HASH') {
5602: %access_in_dom = %{$accessref};
5603: }
5604: foreach my $role (@{$roles_by_num}) {
5605: my ($curraccess,@okstatus,@personnel);
5606: if ($setincrs{$role}) {
5607: ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
5608: if ($curraccess eq 'status') {
5609: @okstatus = split(/\&/,$rest);
5610: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5611: @personnel = split(/\&/,$rest);
5612: }
5613: } else {
5614: $curraccess = $access_in_dom{$role};
5615: if (ref($accessinfo) eq 'HASH') {
5616: if ($curraccess eq 'status') {
5617: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5618: @okstatus = @{$accessinfo->{$role}};
5619: }
5620: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5621: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5622: @personnel = @{$accessinfo->{$role}};
5623: }
5624: }
5625: }
5626: }
5627: if ($curraccess eq 'none') {
5628: next;
5629: } elsif ($curraccess eq 'all') {
5630: push(@possroles,$role);
1.1172.2.90 raeburn 5631: } elsif ($curraccess eq 'dh') {
5632: if (grep(/^dh$/,@liveroles)) {
5633: push(@possroles,$role);
5634: } else {
5635: next;
5636: }
5637: } elsif ($curraccess eq 'da') {
5638: if (grep(/^da$/,@liveroles)) {
5639: push(@possroles,$role);
5640: } else {
5641: next;
5642: }
1.1172.2.89 raeburn 5643: } elsif ($curraccess eq 'status') {
5644: if (@okstatus) {
5645: if (!@statuses) {
5646: if (grep(/^default$/,@okstatus)) {
5647: push(@possroles,$role);
5648: }
5649: } else {
5650: foreach my $status (@okstatus) {
5651: if (grep(/^\Q$status\E$/,@statuses)) {
5652: push(@possroles,$role);
5653: last;
5654: }
5655: }
5656: }
5657: }
5658: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5659: if (grep(/^\Q$user\E$/,@personnel)) {
5660: if ($curraccess eq 'exc') {
5661: push(@possroles,$role);
5662: }
5663: } elsif ($curraccess eq 'inc') {
5664: push(@possroles,$role);
5665: }
5666: }
5667: }
5668: }
5669: }
5670: }
5671: }
5672: }
5673: }
5674: unless (ref($description) eq 'HASH') {
5675: if (ref($roles_by_num) eq 'ARRAY') {
5676: my %desc;
5677: map { $desc{$_} = $_; } (@{$roles_by_num});
5678: $description = \%desc;
5679: } else {
5680: $description = {};
5681: }
5682: }
5683: return (\@possroles,$description);
5684: }
5685:
1.399 www 5686: # ----------------------------------------------------- Frontpage Announcements
5687: #
5688: #
5689:
5690: sub postannounce {
5691: my ($server,$text)=@_;
1.844 albertel 5692: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 5693: unless ($text=~/\w/) { $text=''; }
5694: return &reply('setannounce:'.&escape($text),$server);
5695: }
5696:
5697: sub getannounce {
1.448 albertel 5698:
1.1172.2.96 raeburn 5699: if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 5700: my $announcement='';
1.800 albertel 5701: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 5702: close($fh);
1.399 www 5703: if ($announcement=~/\w/) {
5704: return
5705: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 5706: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 5707: } else {
5708: return '';
5709: }
5710: } else {
5711: return '';
5712: }
1.351 www 5713: }
1.353 www 5714:
5715: # ---------------------------------------------------------- Course ID routines
5716: # Deal with domain's nohist_courseid.db files
5717: #
5718:
5719: sub courseidput {
1.921 raeburn 5720: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 5721: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 5722: my $outcome;
5723: if ($caller eq 'timeonly') {
5724: my $cids = '';
5725: foreach my $item (keys(%$storehash)) {
5726: $cids.=&escape($item).'&';
5727: }
5728: $cids=~s/\&$//;
5729: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
5730: $coursehome);
5731: } else {
5732: my $items = '';
5733: foreach my $item (keys(%$storehash)) {
5734: $items.= &escape($item).'='.
5735: &freeze_escape($$storehash{$item}).'&';
5736: }
5737: $items=~s/\&$//;
5738: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
5739: $coursehome);
1.918 raeburn 5740: }
5741: if ($outcome eq 'unknown_cmd') {
5742: my $what;
5743: foreach my $cid (keys(%$storehash)) {
5744: $what .= &escape($cid).'=';
1.921 raeburn 5745: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 5746: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 5747: }
5748: $what =~ s/\:$/&/;
5749: }
5750: $what =~ s/\&$//;
5751: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
5752: } else {
5753: return $outcome;
5754: }
1.353 www 5755: }
5756:
5757: sub courseiddump {
1.921 raeburn 5758: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 5759: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 5760: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1172.2.38 raeburn 5761: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1172.2.68 raeburn 5762: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918 raeburn 5763: my $as_hash = 1;
5764: my %returnhash;
5765: if (!$domfilter) { $domfilter=''; }
1.845 albertel 5766: my %libserv = &all_library();
5767: foreach my $tryserver (keys(%libserv)) {
5768: if ( ( $hostidflag == 1
5769: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
5770: || (!defined($hostidflag)) ) {
5771:
1.918 raeburn 5772: if (($domfilter eq '') ||
5773: (&host_domain($tryserver) eq $domfilter)) {
1.1172.2.22 raeburn 5774: my $rep;
5775: if (grep { $_ eq $tryserver } ¤t_machine_ids()) {
5776: $rep = &LONCAPA::Lond::dump_course_id_handler(
5777: join(":", (&host_domain($tryserver), $sincefilter,
5778: &escape($descfilter), &escape($instcodefilter),
5779: &escape($ownerfilter), &escape($coursefilter),
5780: &escape($typefilter), &escape($regexp_ok),
5781: $as_hash, &escape($selfenrollonly),
5782: &escape($catfilter), $showhidden, $caller,
5783: &escape($cloner), &escape($cc_clone), $cloneonly,
5784: &escape($createdbefore), &escape($createdafter),
1.1172.2.68 raeburn 5785: &escape($creationcontext),$domcloner,$hasuniquecode,
5786: $reqcrsdom,&escape($reqinstcode))));
1.1172.2.22 raeburn 5787: } else {
5788: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
5789: $sincefilter.':'.&escape($descfilter).':'.
5790: &escape($instcodefilter).':'.&escape($ownerfilter).
5791: ':'.&escape($coursefilter).':'.&escape($typefilter).
5792: ':'.&escape($regexp_ok).':'.$as_hash.':'.
5793: &escape($selfenrollonly).':'.&escape($catfilter).':'.
5794: $showhidden.':'.$caller.':'.&escape($cloner).':'.
5795: &escape($cc_clone).':'.$cloneonly.':'.
5796: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1172.2.68 raeburn 5797: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
5798: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1172.2.22 raeburn 5799: }
5800:
1.918 raeburn 5801: my @pairs=split(/\&/,$rep);
5802: foreach my $item (@pairs) {
5803: my ($key,$value)=split(/\=/,$item,2);
5804: $key = &unescape($key);
5805: next if ($key =~ /^error: 2 /);
5806: my $result = &thaw_unescape($value);
5807: if (ref($result) eq 'HASH') {
5808: $returnhash{$key}=$result;
5809: } else {
1.921 raeburn 5810: my @responses = split(/:/,$value);
5811: my @items = ('description','inst_code','owner','type');
1.918 raeburn 5812: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 5813: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 5814: }
1.1008 raeburn 5815: }
1.353 www 5816: }
5817: }
5818: }
5819: }
5820: return %returnhash;
5821: }
5822:
1.1055 raeburn 5823: sub courselastaccess {
5824: my ($cdom,$cnum,$hostidref) = @_;
5825: my %returnhash;
5826: if ($cdom && $cnum) {
5827: my $chome = &homeserver($cnum,$cdom);
5828: if ($chome ne 'no_host') {
5829: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
5830: &extract_lastaccess(\%returnhash,$rep);
5831: }
5832: } else {
5833: if (!$cdom) { $cdom=''; }
5834: my %libserv = &all_library();
5835: foreach my $tryserver (keys(%libserv)) {
5836: if (ref($hostidref) eq 'ARRAY') {
5837: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
5838: }
5839: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
5840: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
5841: &extract_lastaccess(\%returnhash,$rep);
5842: }
5843: }
5844: }
5845: return %returnhash;
5846: }
5847:
5848: sub extract_lastaccess {
5849: my ($returnhash,$rep) = @_;
5850: if (ref($returnhash) eq 'HASH') {
1.1172.2.146. .26(raeb 5851:-25): unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' ||
1.1055 raeburn 5852: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
5853: $rep eq '') {
5854: my @pairs=split(/\&/,$rep);
5855: foreach my $item (@pairs) {
5856: my ($key,$value)=split(/\=/,$item,2);
5857: $key = &unescape($key);
5858: next if ($key =~ /^error: 2 /);
5859: $returnhash->{$key} = &thaw_unescape($value);
5860: }
5861: }
5862: }
5863: return;
5864: }
5865:
1.658 raeburn 5866: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 5867:
5868: sub dcmailput {
1.685 raeburn 5869: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 5870: my $status = &Apache::lonnet::critical(
1.740 www 5871: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
5872: &escape($message),$server);
1.662 raeburn 5873: return $status;
5874: }
5875:
1.658 raeburn 5876: sub dcmaildump {
5877: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 5878: my %returnhash=();
1.846 albertel 5879:
5880: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 5881: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
5882: &escape($enddate).':';
5883: my @esc_senders=map { &escape($_)} @$senders;
5884: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 5885: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 5886: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 5887: if (($key) && ($value)) {
5888: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 5889: }
5890: }
5891: }
5892: return %returnhash;
5893: }
1.662 raeburn 5894: # ---------------------------------------------------------- Domain roles
5895:
5896: sub get_domain_roles {
5897: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 5898: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 5899: $startdate = '.';
5900: }
1.1018 raeburn 5901: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 5902: $enddate = '.';
5903: }
1.922 raeburn 5904: my $rolelist;
5905: if (ref($roles) eq 'ARRAY') {
1.1172.2.23 raeburn 5906: $rolelist = join('&',@{$roles});
1.922 raeburn 5907: }
1.662 raeburn 5908: my %personnel = ();
1.841 albertel 5909:
5910: my %servers = &get_servers($dom,'library');
5911: foreach my $tryserver (keys(%servers)) {
5912: %{$personnel{$tryserver}}=();
5913: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
5914: &escape($startdate).':'.
5915: &escape($enddate).':'.
5916: &escape($rolelist), $tryserver))) {
5917: my ($key,$value) = split(/\=/,$line,2);
5918: if (($key) && ($value)) {
5919: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
5920: }
5921: }
1.662 raeburn 5922: }
5923: return %personnel;
5924: }
1.658 raeburn 5925:
1.1172.2.89 raeburn 5926: sub get_active_domroles {
5927: my ($dom,$roles) = @_;
5928: return () unless (ref($roles) eq 'ARRAY');
5929: my $now = time;
5930: my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
5931: my %domroles;
5932: foreach my $server (keys(%dompersonnel)) {
5933: foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
5934: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
5935: $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
5936: }
5937: }
5938: return %domroles;
5939: }
5940:
1.1057 www 5941: # ----------------------------------------------------------- Interval timing
1.149 www 5942:
1.1153 www 5943: {
5944: # Caches needed for speedup of navmaps
5945: # We don't want to cache this for very long at all (5 seconds at most)
5946: #
5947: # The user for whom we cache
5948: my $cachedkey='';
5949: # The cached times for this user
5950: my %cachedtimes=();
5951: # When this was last done
1.1172.2.66 raeburn 5952: my $cachedtime='';
1.1153 www 5953:
5954: sub load_all_first_access {
1.1172.2.146. .1(raebu 5955:22): my ($uname,$udom,$ignorecache)=@_;
1.1156 www 5956: if (($cachedkey eq $uname.':'.$udom) &&
1.1172.2.146. .1(raebu 5957:22): (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
5958:22): (!$ignorecache)) {
1.1154 raeburn 5959: return;
5960: }
5961: $cachedtime=time;
5962: $cachedkey=$uname.':'.$udom;
5963: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153 www 5964: }
5965:
1.504 albertel 5966: sub get_first_access {
1.1172.2.146. .1(raebu 5967:22): my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790 albertel 5968: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5969: if ($argsymb) { $symb=$argsymb; }
5970: my ($map,$id,$res)=&decode_symb($symb);
1.1162 raeburn 5971: if ($argmap) { $map = $argmap; }
1.926 albertel 5972: if ($type eq 'course') {
5973: $res='course';
5974: } elsif ($type eq 'map') {
1.588 albertel 5975: $res=&symbread($map);
5976: } else {
5977: $res=$symb;
5978: }
1.1172.2.146. .1(raebu 5979:22): &load_all_first_access($uname,$udom,$ignorecache);
1.1153 www 5980: return $cachedtimes{"$courseid\0$res"};
1.504 albertel 5981: }
5982:
5983: sub set_first_access {
1.1162 raeburn 5984: my ($type,$interval)=@_;
1.790 albertel 5985: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5986: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 5987: if ($type eq 'course') {
5988: $res='course';
5989: } elsif ($type eq 'map') {
1.588 albertel 5990: $res=&symbread($map);
5991: } else {
5992: $res=$symb;
5993: }
1.1153 www 5994: $cachedkey='';
1.1162 raeburn 5995: my $firstaccess=&get_first_access($type,$symb,$map);
1.1172.2.102 raeburn 5996: if ($firstaccess) {
5997: &logthis("First access time already set ($firstaccess) when attempting ".
5998: "to set new value (type: $type, extent: $res) for $uname:$udom ".
5999: "in $courseid");
6000: return 'already_set';
6001: } else {
1.1162 raeburn 6002: my $start = time;
6003: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
6004: $udom,$uname);
6005: if ($putres eq 'ok') {
6006: &put('timerinterval',{"$courseid\0$res"=>$interval},
6007: $udom,$uname);
6008: &appenv(
6009: {
6010: 'course.'.$courseid.'.firstaccess.'.$res => $start,
6011: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
6012: }
6013: );
1.1172.2.97 raeburn 6014: if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
6015: $cachedtimes{"$courseid\0$res"} = $start;
6016: }
1.1172.2.102 raeburn 6017: } elsif ($putres ne 'refused') {
6018: &logthis("Result: $putres when attempting to set first access time ".
6019: "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162 raeburn 6020: }
6021: return $putres;
1.505 albertel 6022: }
6023: return 'already_set';
1.504 albertel 6024: }
1.1153 www 6025: }
1.1172.2.32 raeburn 6026:
6027: sub checkout {
6028: my ($symb,$tuname,$tudom,$tcrsid)=@_;
6029: my $now=time;
6030: my $lonhost=$perlvar{'lonHostID'};
1.1172.2.134 raeburn 6031: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6032: my $infostr=&escape(
6033: 'CHECKOUTTOKEN&'.
6034: $tuname.'&'.
6035: $tudom.'&'.
6036: $tcrsid.'&'.
6037: $symb.'&'.
1.1172.2.134 raeburn 6038: $now.'&'.$ip);
1.1172.2.32 raeburn 6039: my $token=&reply('tmpput:'.$infostr,$lonhost);
6040: if ($token=~/^error\:/) {
6041: &logthis("<font color=\"blue\">WARNING: ".
6042: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
6043: "</font>");
6044: return '';
6045: }
6046:
6047: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
6048: $token=~tr/a-z/A-Z/;
6049:
6050: my %infohash=('resource.0.outtoken' => $token,
6051: 'resource.0.checkouttime' => $now,
1.1172.2.134 raeburn 6052: 'resource.0.outremote' => $ip);
1.1172.2.32 raeburn 6053:
6054: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6055: return '';
6056: } else {
6057: &logthis("<font color=\"blue\">WARNING: ".
6058: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
6059: "</font>");
6060: }
6061:
6062: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6063: &escape('Checkout '.$infostr.' - '.
6064: $token)) ne 'ok') {
6065: return '';
6066: } else {
6067: &logthis("<font color=\"blue\">WARNING: ".
6068: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
6069: "</font>");
6070: }
6071: return $token;
6072: }
6073:
6074: # ------------------------------------------------------------ Check in an item
6075:
6076: sub checkin {
6077: my $token=shift;
6078: my $now=time;
6079: my ($ta,$tb,$lonhost)=split(/\*/,$token);
6080: $lonhost=~tr/A-Z/a-z/;
6081: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
6082: $dtoken=~s/\W/\_/g;
1.1172.2.134 raeburn 6083: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6084: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
6085: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
6086:
6087: unless (($tuname) && ($tudom)) {
6088: &logthis('Check in '.$token.' ('.$dtoken.') failed');
6089: return '';
6090: }
6091:
6092: unless (&allowed('mgr',$tcrsid)) {
6093: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
6094: $env{'user.name'}.' - '.$env{'user.domain'});
6095: return '';
6096: }
6097:
6098: my %infohash=('resource.0.intoken' => $token,
6099: 'resource.0.checkintime' => $now,
1.1172.2.134 raeburn 6100: 'resource.0.inremote' => $ip);
1.1172.2.32 raeburn 6101:
6102: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6103: return '';
6104: }
6105:
6106: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6107: &escape('Checkin - '.$token)) ne 'ok') {
6108: return '';
6109: }
6110:
6111: return ($symb,$tuname,$tudom,$tcrsid);
6112: }
6113:
1.110 www 6114: # --------------------------------------------- Set Expire Date for Spreadsheet
6115:
6116: sub expirespread {
6117: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 6118: my $cid=$env{'request.course.id'};
1.110 www 6119: if ($cid) {
6120: my $now=time;
6121: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 6122: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
6123: $env{'course.'.$cid.'.num'}.
1.110 www 6124: ':nohist_expirationdates:'.
6125: &escape($key).'='.$now,
1.620 albertel 6126: $env{'course.'.$cid.'.home'})
1.110 www 6127: }
6128: return 'ok';
1.14 www 6129: }
6130:
1.109 www 6131: # ----------------------------------------------------- Devalidate Spreadsheets
6132:
6133: sub devalidate {
1.325 www 6134: my ($symb,$uname,$udom)=@_;
1.620 albertel 6135: my $cid=$env{'request.course.id'};
1.109 www 6136: if ($cid) {
1.391 matthew 6137: # delete the stored spreadsheets for
6138: # - the student level sheet of this user in course's homespace
6139: # - the assessment level sheet for this resource
6140: # for this user in user's homespace
1.553 albertel 6141: # - current conditional state info
1.325 www 6142: my $key=$uname.':'.$udom.':';
1.109 www 6143: my $status=
1.299 matthew 6144: &del('nohist_calculatedsheets',
1.391 matthew 6145: [$key.'studentcalc:'],
1.620 albertel 6146: $env{'course.'.$cid.'.domain'},
6147: $env{'course.'.$cid.'.num'})
1.133 albertel 6148: .' '.
6149: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 6150: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 6151: unless ($status eq 'ok ok') {
6152: &logthis('Could not devalidate spreadsheet '.
1.325 www 6153: $uname.' at '.$udom.' for '.
1.109 www 6154: $symb.': '.$status);
1.133 albertel 6155: }
1.553 albertel 6156: &delenv('user.state.'.$cid);
1.109 www 6157: }
6158: }
6159:
1.265 albertel 6160: sub get_scalar {
6161: my ($string,$end) = @_;
6162: my $value;
6163: if ($$string =~ s/^([^&]*?)($end)/$2/) {
6164: $value = $1;
6165: } elsif ($$string =~ s/^([^&]*?)&//) {
6166: $value = $1;
6167: }
6168: return &unescape($value);
6169: }
6170:
6171: sub array2str {
6172: my (@array) = @_;
6173: my $result=&arrayref2str(\@array);
6174: $result=~s/^__ARRAY_REF__//;
6175: $result=~s/__END_ARRAY_REF__$//;
6176: return $result;
6177: }
6178:
1.204 albertel 6179: sub arrayref2str {
6180: my ($arrayref) = @_;
1.265 albertel 6181: my $result='__ARRAY_REF__';
1.204 albertel 6182: foreach my $elem (@$arrayref) {
1.265 albertel 6183: if(ref($elem) eq 'ARRAY') {
6184: $result.=&arrayref2str($elem).'&';
6185: } elsif(ref($elem) eq 'HASH') {
6186: $result.=&hashref2str($elem).'&';
6187: } elsif(ref($elem)) {
6188: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 6189: } else {
6190: $result.=&escape($elem).'&';
6191: }
6192: }
6193: $result=~s/\&$//;
1.265 albertel 6194: $result .= '__END_ARRAY_REF__';
1.204 albertel 6195: return $result;
6196: }
6197:
1.168 albertel 6198: sub hash2str {
1.204 albertel 6199: my (%hash) = @_;
6200: my $result=&hashref2str(\%hash);
1.265 albertel 6201: $result=~s/^__HASH_REF__//;
6202: $result=~s/__END_HASH_REF__$//;
1.204 albertel 6203: return $result;
6204: }
6205:
6206: sub hashref2str {
6207: my ($hashref)=@_;
1.265 albertel 6208: my $result='__HASH_REF__';
1.800 albertel 6209: foreach my $key (sort(keys(%$hashref))) {
6210: if (ref($key) eq 'ARRAY') {
6211: $result.=&arrayref2str($key).'=';
6212: } elsif (ref($key) eq 'HASH') {
6213: $result.=&hashref2str($key).'=';
6214: } elsif (ref($key)) {
1.265 albertel 6215: $result.='=';
1.800 albertel 6216: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 6217: } else {
1.1132 raeburn 6218: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 6219: }
6220:
1.800 albertel 6221: if(ref($hashref->{$key}) eq 'ARRAY') {
6222: $result.=&arrayref2str($hashref->{$key}).'&';
6223: } elsif(ref($hashref->{$key}) eq 'HASH') {
6224: $result.=&hashref2str($hashref->{$key}).'&';
6225: } elsif(ref($hashref->{$key})) {
1.265 albertel 6226: $result.='&';
1.800 albertel 6227: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 6228: } else {
1.800 albertel 6229: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 6230: }
6231: }
1.168 albertel 6232: $result=~s/\&$//;
1.265 albertel 6233: $result .= '__END_HASH_REF__';
1.168 albertel 6234: return $result;
6235: }
6236:
6237: sub str2hash {
1.265 albertel 6238: my ($string)=@_;
6239: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
6240: return %$hash;
6241: }
6242:
6243: sub str2hashref {
1.168 albertel 6244: my ($string) = @_;
1.265 albertel 6245:
6246: my %hash;
6247:
6248: if($string !~ /^__HASH_REF__/) {
6249: if (! ($string eq '' || !defined($string))) {
6250: $hash{'error'}='Not hash reference';
6251: }
6252: return (\%hash, $string);
6253: }
6254:
6255: $string =~ s/^__HASH_REF__//;
6256:
6257: while($string !~ /^__END_HASH_REF__/) {
6258: #key
6259: my $key='';
6260: if($string =~ /^__HASH_REF__/) {
6261: ($key, $string)=&str2hashref($string);
6262: if(defined($key->{'error'})) {
6263: $hash{'error'}='Bad data';
6264: return (\%hash, $string);
6265: }
6266: } elsif($string =~ /^__ARRAY_REF__/) {
6267: ($key, $string)=&str2arrayref($string);
6268: if($key->[0] eq 'Array reference error') {
6269: $hash{'error'}='Bad data';
6270: return (\%hash, $string);
6271: }
6272: } else {
6273: $string =~ s/^(.*?)=//;
1.267 albertel 6274: $key=&unescape($1);
1.265 albertel 6275: }
6276: $string =~ s/^=//;
6277:
6278: #value
6279: my $value='';
6280: if($string =~ /^__HASH_REF__/) {
6281: ($value, $string)=&str2hashref($string);
6282: if(defined($value->{'error'})) {
6283: $hash{'error'}='Bad data';
6284: return (\%hash, $string);
6285: }
6286: } elsif($string =~ /^__ARRAY_REF__/) {
6287: ($value, $string)=&str2arrayref($string);
6288: if($value->[0] eq 'Array reference error') {
6289: $hash{'error'}='Bad data';
6290: return (\%hash, $string);
6291: }
6292: } else {
6293: $value=&get_scalar(\$string,'__END_HASH_REF__');
6294: }
6295: $string =~ s/^&//;
6296:
6297: $hash{$key}=$value;
1.204 albertel 6298: }
1.265 albertel 6299:
6300: $string =~ s/^__END_HASH_REF__//;
6301:
6302: return (\%hash, $string);
1.204 albertel 6303: }
6304:
6305: sub str2array {
1.265 albertel 6306: my ($string)=@_;
6307: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
6308: return @$array;
6309: }
6310:
6311: sub str2arrayref {
1.204 albertel 6312: my ($string) = @_;
1.265 albertel 6313: my @array;
6314:
6315: if($string !~ /^__ARRAY_REF__/) {
6316: if (! ($string eq '' || !defined($string))) {
6317: $array[0]='Array reference error';
6318: }
6319: return (\@array, $string);
6320: }
6321:
6322: $string =~ s/^__ARRAY_REF__//;
6323:
6324: while($string !~ /^__END_ARRAY_REF__/) {
6325: my $value='';
6326: if($string =~ /^__HASH_REF__/) {
6327: ($value, $string)=&str2hashref($string);
6328: if(defined($value->{'error'})) {
6329: $array[0] ='Array reference error';
6330: return (\@array, $string);
6331: }
6332: } elsif($string =~ /^__ARRAY_REF__/) {
6333: ($value, $string)=&str2arrayref($string);
6334: if($value->[0] eq 'Array reference error') {
6335: $array[0] ='Array reference error';
6336: return (\@array, $string);
6337: }
6338: } else {
6339: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
6340: }
6341: $string =~ s/^&//;
6342:
6343: push(@array, $value);
1.191 harris41 6344: }
1.265 albertel 6345:
6346: $string =~ s/^__END_ARRAY_REF__//;
6347:
6348: return (\@array, $string);
1.168 albertel 6349: }
6350:
1.167 albertel 6351: # -------------------------------------------------------------------Temp Store
6352:
1.168 albertel 6353: sub tmpreset {
6354: my ($symb,$namespace,$domain,$stuname) = @_;
6355: if (!$symb) {
6356: $symb=&symbread();
1.620 albertel 6357: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6358: }
6359: $symb=escape($symb);
6360:
1.620 albertel 6361: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 6362: $namespace=~s/\//\_/g;
6363: $namespace=~s/\W//g;
6364:
1.620 albertel 6365: if (!$domain) { $domain=$env{'user.domain'}; }
6366: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6367: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6368: $stuname=&get_requestor_ip();
1.591 albertel 6369: }
1.1117 foxr 6370: my $path=LONCAPA::tempdir();
1.168 albertel 6371: my %hash;
6372: if (tie(%hash,'GDBM_File',
6373: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6374: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 6375: foreach my $key (keys(%hash)) {
1.180 albertel 6376: if ($key=~ /:$symb/) {
1.168 albertel 6377: delete($hash{$key});
6378: }
6379: }
6380: }
6381: }
6382:
1.167 albertel 6383: sub tmpstore {
1.168 albertel 6384: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
6385:
6386: if (!$symb) {
6387: $symb=&symbread();
1.620 albertel 6388: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6389: }
6390: $symb=escape($symb);
6391:
6392: if (!$namespace) {
6393: # I don't think we would ever want to store this for a course.
6394: # it seems this will only be used if we don't have a course.
1.620 albertel 6395: #$namespace=$env{'request.course.id'};
1.168 albertel 6396: #if (!$namespace) {
1.620 albertel 6397: $namespace=$env{'request.state'};
1.168 albertel 6398: #}
6399: }
6400: $namespace=~s/\//\_/g;
6401: $namespace=~s/\W//g;
1.620 albertel 6402: if (!$domain) { $domain=$env{'user.domain'}; }
6403: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6404: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6405: $stuname=&get_requestor_ip();
1.591 albertel 6406: }
1.168 albertel 6407: my $now=time;
6408: my %hash;
1.1117 foxr 6409: my $path=LONCAPA::tempdir();
1.168 albertel 6410: if (tie(%hash,'GDBM_File',
6411: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6412: &GDBM_WRCREAT(),0640)) {
1.168 albertel 6413: $hash{"version:$symb"}++;
6414: my $version=$hash{"version:$symb"};
6415: my $allkeys='';
6416: foreach my $key (keys(%$storehash)) {
6417: $allkeys.=$key.':';
1.591 albertel 6418: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 6419: }
6420: $hash{"$version:$symb:timestamp"}=$now;
6421: $allkeys.='timestamp';
6422: $hash{"$version:keys:$symb"}=$allkeys;
6423: if (untie(%hash)) {
6424: return 'ok';
6425: } else {
6426: return "error:$!";
6427: }
6428: } else {
6429: return "error:$!";
6430: }
6431: }
1.167 albertel 6432:
1.168 albertel 6433: # -----------------------------------------------------------------Temp Restore
1.167 albertel 6434:
1.168 albertel 6435: sub tmprestore {
6436: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 6437:
1.168 albertel 6438: if (!$symb) {
6439: $symb=&symbread();
1.620 albertel 6440: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6441: }
6442: $symb=escape($symb);
6443:
1.620 albertel 6444: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 6445:
1.620 albertel 6446: if (!$domain) { $domain=$env{'user.domain'}; }
6447: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6448: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6449: $stuname=&get_requestor_ip();
1.591 albertel 6450: }
1.168 albertel 6451: my %returnhash;
6452: $namespace=~s/\//\_/g;
6453: $namespace=~s/\W//g;
6454: my %hash;
1.1117 foxr 6455: my $path=LONCAPA::tempdir();
1.168 albertel 6456: if (tie(%hash,'GDBM_File',
6457: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6458: &GDBM_READER(),0640)) {
1.168 albertel 6459: my $version=$hash{"version:$symb"};
6460: $returnhash{'version'}=$version;
6461: my $scope;
6462: for ($scope=1;$scope<=$version;$scope++) {
6463: my $vkeys=$hash{"$scope:keys:$symb"};
6464: my @keys=split(/:/,$vkeys);
6465: my $key;
6466: $returnhash{"$scope:keys"}=$vkeys;
6467: foreach $key (@keys) {
1.591 albertel 6468: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
6469: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 6470: }
6471: }
1.168 albertel 6472: if (!(untie(%hash))) {
6473: return "error:$!";
6474: }
6475: } else {
6476: return "error:$!";
6477: }
6478: return %returnhash;
1.167 albertel 6479: }
6480:
1.9 www 6481: # ----------------------------------------------------------------------- Store
6482:
6483: sub store {
1.1172.2.64 raeburn 6484: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6485: my $home='';
6486:
1.168 albertel 6487: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6488:
1.213 www 6489: $symb=&symbclean($symb);
1.122 albertel 6490: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6491:
1.620 albertel 6492: if (!$domain) { $domain=$env{'user.domain'}; }
6493: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6494:
6495: &devalidate($symb,$stuname,$domain);
1.109 www 6496:
6497: $symb=escape($symb);
1.187 www 6498: if (!$namespace) {
1.620 albertel 6499: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6500: return '';
6501: }
6502: }
1.620 albertel 6503: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6504:
1.1172.2.138 raeburn 6505: $$storehash{'ip'}=&get_requestor_ip();
1.447 www 6506: $$storehash{'host'}=$perlvar{'lonHostID'};
6507:
1.12 www 6508: my $namevalue='';
1.800 albertel 6509: foreach my $key (keys(%$storehash)) {
6510: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6511: }
1.12 www 6512: $namevalue=~s/\&$//;
1.187 www 6513: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1172.2.64 raeburn 6514: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9 www 6515: }
6516:
1.47 www 6517: # -------------------------------------------------------------- Critical Store
6518:
6519: sub cstore {
1.1172.2.64 raeburn 6520: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6521: my $home='';
6522:
1.168 albertel 6523: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6524:
1.213 www 6525: $symb=&symbclean($symb);
1.122 albertel 6526: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6527:
1.620 albertel 6528: if (!$domain) { $domain=$env{'user.domain'}; }
6529: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6530:
6531: &devalidate($symb,$stuname,$domain);
1.109 www 6532:
6533: $symb=escape($symb);
1.187 www 6534: if (!$namespace) {
1.620 albertel 6535: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6536: return '';
6537: }
6538: }
1.620 albertel 6539: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6540:
1.1172.2.138 raeburn 6541: $$storehash{'ip'}=&get_requestor_ip();
1.447 www 6542: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 6543:
1.47 www 6544: my $namevalue='';
1.800 albertel 6545: foreach my $key (keys(%$storehash)) {
6546: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6547: }
1.47 www 6548: $namevalue=~s/\&$//;
1.187 www 6549: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 6550: return critical
1.1172.2.64 raeburn 6551: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47 www 6552: }
6553:
1.9 www 6554: # --------------------------------------------------------------------- Restore
6555:
6556: sub restore {
1.124 www 6557: my ($symb,$namespace,$domain,$stuname) = @_;
6558: my $home='';
6559:
1.168 albertel 6560: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6561:
1.122 albertel 6562: if (!$symb) {
1.1172.2.26 raeburn 6563: return if ($namespace eq 'courserequests');
6564: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 6565: } else {
1.1172.2.26 raeburn 6566: unless ($namespace eq 'courserequests') {
6567: $symb=&escape(&symbclean($symb));
6568: }
1.122 albertel 6569: }
1.188 www 6570: if (!$namespace) {
1.620 albertel 6571: unless ($namespace=$env{'request.course.id'}) {
1.188 www 6572: return '';
6573: }
6574: }
1.620 albertel 6575: if (!$domain) { $domain=$env{'user.domain'}; }
6576: if (!$stuname) { $stuname=$env{'user.name'}; }
6577: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 6578: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
6579:
1.12 www 6580: my %returnhash=();
1.800 albertel 6581: foreach my $line (split(/\&/,$answer)) {
6582: my ($name,$value)=split(/\=/,$line);
1.591 albertel 6583: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 6584: }
1.75 www 6585: my $version;
6586: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 6587: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
6588: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 6589: }
1.75 www 6590: }
1.13 www 6591: return %returnhash;
1.34 www 6592: }
6593:
6594: # ---------------------------------------------------------- Course Description
1.1118 foxr 6595: #
6596: #
1.34 www 6597:
6598: sub coursedescription {
1.731 albertel 6599: my ($courseid,$args)=@_;
1.34 www 6600: $courseid=~s/^\///;
1.49 www 6601: $courseid=~s/\_/\//g;
1.34 www 6602: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 6603: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 6604: my $normalid=$cdomain.'_'.$cnum;
6605: # need to always cache even if we get errors otherwise we keep
6606: # trying and trying and trying to get the course description.
6607: my %envhash=();
6608: my %returnhash=();
1.731 albertel 6609:
6610: my $expiretime=600;
6611: if ($env{'request.course.id'} eq $normalid) {
6612: $expiretime=120;
6613: }
6614:
6615: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
6616: if (!$args->{'freshen_cache'}
6617: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
6618: foreach my $key (keys(%env)) {
6619: next if ($key !~ /^\Q$prefix\E(.*)/);
6620: my ($setting) = $1;
6621: $returnhash{$setting} = $env{$key};
6622: }
6623: return %returnhash;
6624: }
6625:
1.1118 foxr 6626: # get the data again
6627:
1.731 albertel 6628: if (!$args->{'one_time'}) {
6629: $envhash{'course.'.$normalid.'.last_cache'}=time;
6630: }
1.811 albertel 6631:
1.34 www 6632: if ($chome ne 'no_host') {
1.302 albertel 6633: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 6634: if (!exists($returnhash{'con_lost'})) {
1.1118 foxr 6635: my $username = $env{'user.name'}; # Defult username
6636: if(defined $args->{'user'}) {
6637: $username = $args->{'user'};
6638: }
1.129 albertel 6639: $returnhash{'home'}= $chome;
6640: $returnhash{'domain'} = $cdomain;
6641: $returnhash{'num'} = $cnum;
1.741 raeburn 6642: if (!defined($returnhash{'type'})) {
6643: $returnhash{'type'} = 'Course';
6644: }
1.130 albertel 6645: while (my ($name,$value) = each %returnhash) {
1.53 www 6646: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 6647: }
1.270 www 6648: $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117 foxr 6649: $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118 foxr 6650: $username.'_'.$cdomain.'_'.$cnum;
1.60 www 6651: $envhash{'course.'.$normalid.'.home'}=$chome;
6652: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
6653: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 6654: }
6655: }
1.731 albertel 6656: if (!$args->{'one_time'}) {
1.949 raeburn 6657: &appenv(\%envhash);
1.731 albertel 6658: }
1.302 albertel 6659: return %returnhash;
1.461 www 6660: }
6661:
1.1080 raeburn 6662: sub update_released_required {
6663: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
6664: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
6665: $cid = $env{'request.course.id'};
6666: $cdom = $env{'course.'.$cid.'.domain'};
6667: $cnum = $env{'course.'.$cid.'.num'};
6668: $chome = $env{'course.'.$cid.'.home'};
6669: }
6670: if ($needsrelease) {
6671: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
6672: my $needsupdate;
6673: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
6674: $needsupdate = 1;
6675: } else {
6676: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
6677: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
6678: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
6679: $needsupdate = 1;
6680: }
6681: }
6682: if ($needsupdate) {
6683: my %needshash = (
6684: 'internal.releaserequired' => $needsrelease,
6685: );
6686: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
6687: if ($putresult eq 'ok') {
6688: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
6689: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6690: if (ref($crsinfo{$cid}) eq 'HASH') {
6691: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
6692: &courseidput($cdom,\%crsinfo,$chome,'notime');
6693: }
6694: }
6695: }
6696: }
6697: return;
6698: }
6699:
1.461 www 6700: # -------------------------------------------------See if a user is privileged
6701:
6702: sub privileged {
1.1172.2.23 raeburn 6703: my ($username,$domain,$possdomains,$possroles)=@_;
1.1170 droeschl 6704: my $now = time;
1.1172.2.23 raeburn 6705: my $roles;
6706: if (ref($possroles) eq 'ARRAY') {
6707: $roles = $possroles;
6708: } else {
6709: $roles = ['dc','su'];
6710: }
6711: if (ref($possdomains) eq 'ARRAY') {
6712: my %privileged = &privileged_by_domain($possdomains,$roles);
6713: foreach my $dom (@{$possdomains}) {
6714: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
6715: (ref($privileged{$dom}) eq 'HASH')) {
6716: foreach my $role (@{$roles}) {
6717: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6718: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
6719: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
6720: return 1 unless (($end && $end < $now) ||
6721: ($start && $start > $now));
6722: }
6723: }
6724: }
6725: }
6726: }
6727: } else {
6728: my %rolesdump = &dump("roles", $domain, $username) or return 0;
6729: my $now = time;
1.1170 droeschl 6730:
1.1172.2.58 raeburn 6731: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170 droeschl 6732: my ($trole, $tend, $tstart) = split(/_/, $role);
1.1172.2.23 raeburn 6733: if (grep(/^\Q$trole\E$/,@{$roles})) {
6734: return 1 unless ($tend && $tend < $now)
6735: or ($tstart && $tstart > $now);
1.1170 droeschl 6736: }
1.1172.2.23 raeburn 6737: }
6738: }
1.461 www 6739: return 0;
1.9 www 6740: }
1.1 albertel 6741:
1.1172.2.23 raeburn 6742: sub privileged_by_domain {
6743: my ($domains,$roles) = @_;
6744: my %privileged = ();
6745: my $cachetime = 60*60*24;
6746: my $now = time;
6747: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
6748: return %privileged;
6749: }
6750: foreach my $dom (@{$domains}) {
6751: next if (ref($privileged{$dom}) eq 'HASH');
6752: my $needroles;
6753: foreach my $role (@{$roles}) {
6754: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
6755: if (defined($cached)) {
6756: if (ref($result) eq 'HASH') {
6757: $privileged{$dom}{$role} = $result;
6758: }
6759: } else {
6760: $needroles = 1;
6761: }
6762: }
6763: if ($needroles) {
6764: my %dompersonnel = &get_domain_roles($dom,$roles);
6765: $privileged{$dom} = {};
6766: foreach my $server (keys(%dompersonnel)) {
6767: if (ref($dompersonnel{$server}) eq 'HASH') {
6768: foreach my $item (keys(%{$dompersonnel{$server}})) {
6769: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
6770: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
6771: next if ($end && $end < $now);
6772: $privileged{$dom}{$trole}{$uname.':'.$udom} =
6773: $dompersonnel{$server}{$item};
6774: }
6775: }
6776: }
6777: if (ref($privileged{$dom}) eq 'HASH') {
6778: foreach my $role (@{$roles}) {
6779: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6780: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
6781: } else {
6782: my %hash = ();
6783: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
6784: }
6785: }
6786: }
6787: }
6788: }
6789: return %privileged;
6790: }
6791:
1.103 harris41 6792: # -------------------------------------------------------- Get user privileges
1.11 www 6793:
6794: sub rolesinit {
1.1169 droeschl 6795: my ($domain, $username) = @_;
6796: my %userroles = ('user.login.time' => time);
6797: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
6798:
6799: # firstaccess and timerinterval are related to timed maps/resources.
6800: # also, blocking can be triggered by an activating timer
6801: # it's saved in the user's %env.
6802: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
6803: my %timerinterval = &dump('timerinterval', $domain, $username);
6804: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
1.1172.2.146. .18(raeb 6805:-24): %timerintchk, %timerintenv,%coauthorenv);
1.1169 droeschl 6806:
1.1162 raeburn 6807: foreach my $key (keys(%firstaccess)) {
1.1169 droeschl 6808: my ($cid, $rest) = split(/\0/, $key);
1.1162 raeburn 6809: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
6810: }
1.1169 droeschl 6811:
1.1162 raeburn 6812: foreach my $key (keys(%timerinterval)) {
6813: my ($cid,$rest) = split(/\0/,$key);
6814: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
6815: }
1.1169 droeschl 6816:
1.11 www 6817: my %allroles=();
1.1162 raeburn 6818: my %allgroups=();
1.1172.2.146. .18(raeb 6819:-24): my %gotcoauconfig=();
.20(raeb 6820:-24): my %domdefaults=();
1.11 www 6821:
1.1172.2.58 raeburn 6822: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169 droeschl 6823: my $role = $rolesdump{$area};
6824: $area =~ s/\_\w\w$//;
6825:
6826: my ($trole, $tend, $tstart, $group_privs);
6827:
6828: if ($role =~ /^cr/) {
6829: # Custom role, defined by a user
6830: # e.g., user.role.cr/msu/smith/mynewrole
6831: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
6832: $trole = $1;
6833: ($tend, $tstart) = split('_', $2);
6834: } else {
6835: $trole = $role;
6836: }
6837: } elsif ($role =~ m|^gr/|) {
6838: # Role of member in a group, defined within a course/community
6839: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
6840: ($trole, $tend, $tstart) = split(/_/, $role);
6841: next if $tstart eq '-1';
6842: ($trole, $group_privs) = split(/\//, $trole);
6843: $group_privs = &unescape($group_privs);
6844: } else {
6845: # Just a normal role, defined in roles.tab
6846: ($trole, $tend, $tstart) = split(/_/,$role);
6847: }
6848:
6849: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
6850: $username);
6851: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
6852:
6853: # role expired or not available yet?
6854: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
6855: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
6856:
6857: next if $area eq '' or $trole eq '';
6858:
6859: my $spec = "$trole.$area";
6860: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
6861:
6862: if ($trole =~ /^cr\//) {
6863: # Custom role, defined by a user
6864: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
6865: } elsif ($trole eq 'gr') {
6866: # Role of a member in a group, defined within a course/community
6867: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
6868: next;
6869: } else {
6870: # Normal role, defined in roles.tab
6871: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.1172.2.146. .18(raeb 6872:-24): if (($trole eq 'ca') || ($trole eq 'aa')) {
6873:-24): (undef,my ($audom,$auname)) = split(/\//,$area);
6874:-24): unless ($gotcoauconfig{$area}) {
.22(raeb 6875:-24): my @ca_settings = ('authoreditors','coauthorlist','coauthoroptin');
.18(raeb 6876:-24): my %info = &userenvironment($audom,$auname,@ca_settings);
6877:-24): $gotcoauconfig{$area} = 1;
6878:-24): foreach my $item (@ca_settings) {
6879:-24): if (exists($info{$item})) {
6880:-24): my $name = $item;
6881:-24): if ($item eq 'authoreditors') {
6882:-24): $name = 'editors';
.20(raeb 6883:-24): unless ($info{'authoreditors'}) {
6884:-24): my %domdefs;
6885:-24): if (ref($domdefaults{$audom}) eq 'HASH') {
6886:-24): %domdefs = %{$domdefaults{$audom}};
6887:-24): } else {
6888:-24): %domdefs = &get_domain_defaults($audom);
6889:-24): $domdefaults{$audom} = \%domdefs;
6890:-24): }
6891:-24): if ($domdefs{$name} ne '') {
6892:-24): $info{'authoreditors'} = $domdefs{$name};
6893:-24): } else {
6894:-24): $info{'authoreditors'} = 'edit,xml';
6895:-24): }
6896:-24): }
.18(raeb 6897:-24): }
6898:-24): $coauthorenv{"environment.internal.$name.$area"} = $info{$item};
6899:-24): }
6900:-24): }
6901:-24): }
6902:-24): }
1.1169 droeschl 6903: }
6904:
6905: my $cid = $tdomain.'_'.$trest;
6906: unless ($firstaccchk{$cid}) {
6907: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
6908: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
6909: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
6910: $coursetimerstarts{$cid}{$item};
6911: }
6912: }
6913: $firstaccchk{$cid} = 1;
6914: }
6915: unless ($timerintchk{$cid}) {
6916: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
6917: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
6918: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
6919: $coursetimerintervals{$cid}{$item};
1.1162 raeburn 6920: }
1.12 www 6921: }
1.1169 droeschl 6922: $timerintchk{$cid} = 1;
1.191 harris41 6923: }
1.11 www 6924: }
1.1169 droeschl 6925:
1.1172.2.91 raeburn 6926: @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
6927: \%allroles, \%allgroups);
1.1169 droeschl 6928: $env{'user.adv'} = $userroles{'user.adv'};
1.1172.2.91 raeburn 6929: $env{'user.rar'} = $userroles{'user.rar'};
1.1169 droeschl 6930:
1.1172.2.146. .18(raeb 6931:-24): return (\%userroles,\%firstaccenv,\%timerintenv,\%coauthorenv);
1.11 www 6932: }
6933:
1.567 raeburn 6934: sub set_arearole {
1.1172.2.19 raeburn 6935: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
6936: unless ($nolog) {
1.567 raeburn 6937: # log the associated role with the area
1.1172.2.19 raeburn 6938: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
6939: }
1.743 albertel 6940: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 6941: }
6942:
6943: sub custom_roleprivs {
6944: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
6945: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1172.2.40 raeburn 6946: my $homsvr = &homeserver($rauthor,$rdomain);
1.838 albertel 6947: if (&hostname($homsvr) ne '') {
1.567 raeburn 6948: my ($rdummy,$roledef)=
6949: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
6950: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
6951: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
6952: if (defined($syspriv)) {
1.1043 raeburn 6953: if ($trest =~ /^$match_community$/) {
6954: $syspriv =~ s/bre\&S//;
6955: }
1.567 raeburn 6956: $$allroles{'cm./'}.=':'.$syspriv;
6957: $$allroles{$spec.'./'}.=':'.$syspriv;
6958: }
6959: if ($tdomain ne '') {
6960: if (defined($dompriv)) {
6961: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
6962: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
6963: }
6964: if (($trest ne '') && (defined($coursepriv))) {
1.1172.2.89 raeburn 6965: if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
6966: my $rolename = $1;
6967: $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
6968: }
1.567 raeburn 6969: $$allroles{'cm.'.$area}.=':'.$coursepriv;
6970: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
6971: }
6972: }
6973: }
6974: }
6975: }
6976:
1.1172.2.89 raeburn 6977: sub course_adhocrole_privs {
6978: my ($rolename,$cdom,$cnum,$coursepriv) = @_;
6979: my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
6980: if ($overrides{"internal.adhocpriv.$rolename"}) {
6981: my (%currprivs,%storeprivs);
6982: foreach my $item (split(/:/,$coursepriv)) {
6983: my ($priv,$restrict) = split(/\&/,$item);
6984: $currprivs{$priv} = $restrict;
6985: }
6986: my (%possadd,%possremove,%full);
6987: foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
6988: my ($priv,$restrict)=split(/\&/,$item);
6989: $full{$priv} = $restrict;
6990: }
6991: foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
1.1172.2.146. .6(raebu 6992:22): next if ($item eq '');
6993:22): my ($rule,$rest) = split(/=/,$item);
6994:22): next unless (($rule eq 'off') || ($rule eq 'on'));
6995:22): foreach my $priv (split(/:/,$rest)) {
6996:22): if ($priv ne '') {
6997:22): if ($rule eq 'off') {
6998:22): $possremove{$priv} = 1;
6999:22): } else {
7000:22): $possadd{$priv} = 1;
7001:22): }
7002:22): }
7003:22): }
7004:22): }
7005:22): foreach my $priv (sort(keys(%full))) {
7006:22): if (exists($currprivs{$priv})) {
7007:22): unless (exists($possremove{$priv})) {
7008:22): $storeprivs{$priv} = $currprivs{$priv};
7009:22): }
7010:22): } elsif (exists($possadd{$priv})) {
7011:22): $storeprivs{$priv} = $full{$priv};
7012:22): }
7013:22): }
7014:22): $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
7015:22): }
7016:22): return $coursepriv;
1.1172.2.89 raeburn 7017: }
7018:
1.678 raeburn 7019: sub group_roleprivs {
7020: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
7021: my $access = 1;
7022: my $now = time;
7023: if (($tend!=0) && ($tend<$now)) { $access = 0; }
7024: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
7025: if ($access) {
1.811 albertel 7026: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 7027: $$allgroups{$course}{$group} .=':'.$group_privs;
7028: }
7029: }
1.567 raeburn 7030:
7031: sub standard_roleprivs {
7032: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
7033: if (defined($pr{$trole.':s'})) {
7034: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
7035: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
7036: }
7037: if ($tdomain ne '') {
7038: if (defined($pr{$trole.':d'})) {
7039: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7040: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7041: }
7042: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
7043: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
7044: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
7045: }
7046: }
7047: }
7048:
7049: sub set_userprivs {
1.1064 raeburn 7050: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 7051: my $author=0;
7052: my $adv=0;
1.1172.2.91 raeburn 7053: my $rar=0;
1.678 raeburn 7054: my %grouproles = ();
7055: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 7056: my @groupkeys;
1.1000 raeburn 7057: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 7058: push(@groupkeys,$role);
7059: }
7060: if (ref($groups_roles) eq 'HASH') {
7061: foreach my $key (keys(%{$groups_roles})) {
7062: unless (grep(/^\Q$key\E$/,@groupkeys)) {
7063: push(@groupkeys,$key);
7064: }
7065: }
7066: }
7067: if (@groupkeys > 0) {
7068: foreach my $role (@groupkeys) {
7069: my ($trole,$area,$sec,$extendedarea);
7070: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
7071: $trole = $1;
7072: $area = $2;
7073: $sec = $3;
7074: $extendedarea = $area.$sec;
7075: if (exists($$allgroups{$area})) {
7076: foreach my $group (keys(%{$$allgroups{$area}})) {
7077: my $spec = $trole.'.'.$extendedarea;
7078: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 7079: $$allgroups{$area}{$group};
1.1064 raeburn 7080: }
1.678 raeburn 7081: }
7082: }
7083: }
7084: }
7085: }
1.800 albertel 7086: foreach my $group (keys(%grouproles)) {
7087: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 7088: }
1.800 albertel 7089: foreach my $role (keys(%{$allroles})) {
7090: my %thesepriv;
1.941 raeburn 7091: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 7092: foreach my $item (split(/:/,$$allroles{$role})) {
7093: if ($item ne '') {
7094: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 7095: if ($restrictions eq '') {
7096: $thesepriv{$privilege}='F';
7097: } elsif ($thesepriv{$privilege} ne 'F') {
7098: $thesepriv{$privilege}.=$restrictions;
7099: }
7100: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1172.2.91 raeburn 7101: if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567 raeburn 7102: }
7103: }
7104: my $thesestr='';
1.1104 raeburn 7105: foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 7106: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
7107: }
7108: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 7109: }
1.1172.2.91 raeburn 7110: return ($author,$adv,$rar);
1.567 raeburn 7111: }
7112:
1.994 raeburn 7113: sub role_status {
1.1104 raeburn 7114: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 7115: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1172.2.40 raeburn 7116: my ($one,$two) = split(m{\./},$rolekey,2);
7117: (undef,undef,$$role) = split(/\./,$one,3);
1.994 raeburn 7118: unless (!defined($$role) || $$role eq '') {
1.1172.2.40 raeburn 7119: $$where = '/'.$two;
1.994 raeburn 7120: $$trolecode=$$role.'.'.$$where;
7121: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
7122: $$tstatus='is';
1.1104 raeburn 7123: if ($$tstart && $$tstart>$update) {
1.994 raeburn 7124: $$tstatus='future';
1.1034 raeburn 7125: if ($$tstart<$now) {
7126: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 7127: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 7128: my (%allroles,%allgroups,$group_privs,
7129: %groups_roles,@rolecodes);
1.1002 raeburn 7130: my %userroles = (
7131: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
7132: );
1.1064 raeburn 7133: @rolecodes = ('cm');
1.1002 raeburn 7134: my $spec=$$role.'.'.$$where;
7135: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
7136: if ($$role =~ /^cr\//) {
7137: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 7138: push(@rolecodes,'cr');
1.1002 raeburn 7139: } elsif ($$role eq 'gr') {
1.1064 raeburn 7140: push(@rolecodes,$$role);
1.1002 raeburn 7141: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
7142: $env{'user.name'});
1.1064 raeburn 7143: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 7144: (undef,my $group_privs) = split(/\//,$trole);
7145: $group_privs = &unescape($group_privs);
7146: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 7147: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
1.1104 raeburn 7148: &get_groups_roles($tdomain,$trest,
7149: \%course_roles,\@rolecodes,
7150: \%groups_roles);
1.1002 raeburn 7151: } else {
1.1064 raeburn 7152: push(@rolecodes,$$role);
1.1002 raeburn 7153: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
7154: }
1.1172.2.91 raeburn 7155: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
7156: \%groups_roles);
1.1064 raeburn 7157: &appenv(\%userroles,\@rolecodes);
1.1172.2.92 raeburn 7158: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002 raeburn 7159: }
7160: }
1.1034 raeburn 7161: $$tstatus = 'is';
1.1002 raeburn 7162: }
1.994 raeburn 7163: }
7164: if ($$tend) {
1.1104 raeburn 7165: if ($$tend<$update) {
1.994 raeburn 7166: $$tstatus='expired';
7167: } elsif ($$tend<$now) {
7168: $$tstatus='will_not';
7169: }
7170: }
7171: }
7172: }
7173: }
7174:
1.1104 raeburn 7175: sub get_groups_roles {
7176: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
7177: return unless((ref($cdom_courseroles) eq 'HASH') &&
7178: (ref($rolecodes) eq 'ARRAY') &&
7179: (ref($groups_roles) eq 'HASH'));
7180: if (keys(%{$cdom_courseroles}) > 0) {
7181: my ($cnum) = ($rest =~ /^($match_courseid)/);
7182: if ($cdom ne '' && $cnum ne '') {
7183: foreach my $key (keys(%{$cdom_courseroles})) {
7184: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
7185: my $crsrole = $1;
7186: my $crssec = $2;
7187: if ($crsrole =~ /^cr/) {
7188: unless (grep(/^cr$/,@{$rolecodes})) {
7189: push(@{$rolecodes},'cr');
7190: }
7191: } else {
7192: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
7193: push(@{$rolecodes},$crsrole);
7194: }
7195: }
7196: my $rolekey = "$crsrole./$cdom/$cnum";
7197: if ($crssec ne '') {
7198: $rolekey .= "/$crssec";
7199: }
7200: $rolekey .= './';
7201: $groups_roles->{$rolekey} = $rolecodes;
7202: }
7203: }
7204: }
7205: }
7206: return;
7207: }
7208:
7209: sub delete_env_groupprivs {
7210: my ($where,$courseroles,$possroles) = @_;
7211: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
7212: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
7213: unless (ref($courseroles->{$udom}) eq 'HASH') {
7214: %{$courseroles->{$udom}} =
7215: &get_my_roles('','','userroles',['active'],
7216: $possroles,[$udom],1);
7217: }
7218: if (ref($courseroles->{$udom}) eq 'HASH') {
7219: foreach my $item (keys(%{$courseroles->{$udom}})) {
7220: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
7221: my $area = '/'.$cdom.'/'.$cnum;
7222: my $privkey = "user.priv.$crsrole.$area";
7223: if ($crssec ne '') {
7224: $privkey .= '/'.$crssec;
7225: }
7226: $privkey .= ".$area/$group";
7227: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
7228: }
7229: }
7230: return;
7231: }
7232:
1.994 raeburn 7233: sub check_adhoc_privs {
1.1172.2.86 raeburn 7234: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994 raeburn 7235: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1172.2.86 raeburn 7236: if ($sec) {
7237: $cckey .= '/'.$sec;
7238: }
1.1172.2.9 raeburn 7239: my $setprivs;
1.994 raeburn 7240: if ($env{$cckey}) {
7241: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104 raeburn 7242: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 7243: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1172.2.86 raeburn 7244: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7245: $setprivs = 1;
1.994 raeburn 7246: }
7247: } else {
1.1172.2.87 raeburn 7248: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7249: $setprivs = 1;
1.994 raeburn 7250: }
1.1172.2.9 raeburn 7251: return $setprivs;
1.994 raeburn 7252: }
7253:
7254: sub set_adhoc_privileges {
1.1172.2.84 raeburn 7255: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1172.2.86 raeburn 7256: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994 raeburn 7257: my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1172.2.86 raeburn 7258: if ($sec ne '') {
7259: $area .= '/'.$sec;
7260: }
1.994 raeburn 7261: my $spec = $role.'.'.$area;
7262: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1172.2.19 raeburn 7263: $env{'user.name'},1);
1.1172.2.84 raeburn 7264: my %rolehash = ();
1.1172.2.89 raeburn 7265: if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
7266: my $rolename = $1;
1.1172.2.84 raeburn 7267: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1172.2.89 raeburn 7268: my %domdef = &get_domain_defaults($dcdom);
7269: if (ref($domdef{'adhocroles'}) eq 'HASH') {
7270: if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
7271: &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
7272: }
7273: }
1.1172.2.84 raeburn 7274: } else {
7275: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
7276: }
1.1172.2.91 raeburn 7277: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994 raeburn 7278: &appenv(\%userroles,[$role,'cm']);
1.1172.2.92 raeburn 7279: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1172.2.146. .1(raebu 7280:22): unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
7281:22): ($caller eq 'tiny')) {
1.1088 raeburn 7282: &appenv( {'request.role' => $spec,
7283: 'request.role.domain' => $dcdom,
1.1172.2.89 raeburn 7284: 'request.course.sec' => $sec,
1.1088 raeburn 7285: }
7286: );
7287: my $tadv=0;
7288: if (&allowed('adv') eq 'F') { $tadv=1; }
7289: &appenv({'request.role.adv' => $tadv});
7290: }
1.994 raeburn 7291: }
7292:
1.12 www 7293: # --------------------------------------------------------------- get interface
7294:
7295: sub get {
1.131 albertel 7296: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7297: my $items='';
1.800 albertel 7298: foreach my $item (@$storearr) {
7299: $items.=&escape($item).'&';
1.191 harris41 7300: }
1.12 www 7301: $items=~s/\&$//;
1.620 albertel 7302: if (!$udomain) { $udomain=$env{'user.domain'}; }
7303: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 7304: my $uhome=&homeserver($uname,$udomain);
7305:
1.133 albertel 7306: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7307: my @pairs=split(/\&/,$rep);
1.273 albertel 7308: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
7309: return @pairs;
7310: }
1.15 www 7311: my %returnhash=();
1.42 www 7312: my $i=0;
1.800 albertel 7313: foreach my $item (@$storearr) {
7314: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7315: $i++;
1.191 harris41 7316: }
1.15 www 7317: return %returnhash;
1.27 www 7318: }
7319:
7320: # --------------------------------------------------------------- del interface
7321:
7322: sub del {
1.133 albertel 7323: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 7324: my $items='';
1.800 albertel 7325: foreach my $item (@$storearr) {
7326: $items.=&escape($item).'&';
1.191 harris41 7327: }
1.984 neumanie 7328:
1.27 www 7329: $items=~s/\&$//;
1.620 albertel 7330: if (!$udomain) { $udomain=$env{'user.domain'}; }
7331: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7332: my $uhome=&homeserver($uname,$udomain);
7333: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7334: }
7335:
7336: # -------------------------------------------------------------- dump interface
7337:
1.1172.2.22 raeburn 7338: sub unserialize {
7339: my ($rep, $escapedkeys) = @_;
7340:
7341: return {} if $rep =~ /^error/;
7342:
7343: my %returnhash=();
7344: foreach my $item (split(/\&/,$rep)) {
7345: my ($key, $value) = split(/=/, $item, 2);
7346: $key = unescape($key) unless $escapedkeys;
7347: next if $key =~ /^error: 2 /;
7348: $returnhash{$key} = &thaw_unescape($value);
7349: }
7350: return \%returnhash;
7351: }
7352:
7353: # see Lond::dump_with_regexp
7354: # if $escapedkeys hash keys won't get unescaped.
1.15 www 7355: sub dump {
1.1172.2.146. .1(raebu 7356:22): my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755 albertel 7357: if (!$udomain) { $udomain=$env{'user.domain'}; }
7358: if (!$uname) { $uname=$env{'user.name'}; }
7359: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 7360:
1.1172.2.55 raeburn 7361: if ($regexp) {
7362: $regexp=&escape($regexp);
7363: } else {
7364: $regexp='.';
7365: }
1.1172.2.22 raeburn 7366: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
7367: # user is hosted on this machine
1.1172.2.55 raeburn 7368: my $reply = LONCAPA::Lond::dump_with_regexp(join(':', ($udomain,
1.1172.2.27 raeburn 7369: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1172.2.22 raeburn 7370: return %{&unserialize($reply, $escapedkeys)};
7371: }
1.1172.2.146. .1(raebu 7372:22): my $rep;
7373:22): if ($encrypt) {
7374:22): $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7375:22): } else {
7376:22): $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7377:22): }
1.755 albertel 7378: my @pairs=split(/\&/,$rep);
7379: my %returnhash=();
1.1098 foxr 7380: if (!($rep =~ /^error/ )) {
7381: foreach my $item (@pairs) {
7382: my ($key,$value)=split(/=/,$item,2);
1.1172.2.22 raeburn 7383: $key = &unescape($key) unless ($escapedkeys);
1.1098 foxr 7384: next if ($key =~ /^error: 2 /);
7385: $returnhash{$key}=&thaw_unescape($value);
7386: }
1.755 albertel 7387: }
7388: return %returnhash;
1.407 www 7389: }
7390:
1.1098 foxr 7391:
1.717 albertel 7392: # --------------------------------------------------------- dumpstore interface
7393:
7394: sub dumpstore {
7395: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1172.2.22 raeburn 7396: # same as dump but keys must be escaped. They may contain colon separated
7397: # lists of values that may themself contain colons (e.g. symbs).
7398: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 7399: }
7400:
1.407 www 7401: # -------------------------------------------------------------- keys interface
7402:
7403: sub getkeys {
7404: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 7405: if (!$udomain) { $udomain=$env{'user.domain'}; }
7406: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 7407: my $uhome=&homeserver($uname,$udomain);
7408: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
7409: my @keyarray=();
1.800 albertel 7410: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 7411: next if ($key =~ /^error: 2 /);
1.800 albertel 7412: push(@keyarray,&unescape($key));
1.407 www 7413: }
7414: return @keyarray;
1.318 matthew 7415: }
7416:
1.319 matthew 7417: # --------------------------------------------------------------- currentdump
7418: sub currentdump {
1.328 matthew 7419: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 7420: $courseid = $env{'request.course.id'} if (! defined($courseid));
7421: $sdom = $env{'user.domain'} if (! defined($sdom));
7422: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 7423: my $uhome = &homeserver($sname,$sdom);
1.1172.2.50 raeburn 7424: my $rep;
7425:
7426: if (grep { $_ eq $uhome } current_machine_ids()) {
7427: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
7428: $courseid)));
7429: } else {
7430: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
7431: }
7432:
1.318 matthew 7433: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 7434: #
1.318 matthew 7435: my %returnhash=();
1.319 matthew 7436: #
7437: if ($rep eq "unknown_cmd") {
7438: # an old lond will not know currentdump
7439: # Do a dump and make it look like a currentdump
1.822 albertel 7440: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 7441: return if ($tmp[0] =~ /^(error:|no_such_host)/);
7442: my %hash = @tmp;
7443: @tmp=();
1.424 matthew 7444: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 7445: } else {
7446: my @pairs=split(/\&/,$rep);
1.800 albertel 7447: foreach my $pair (@pairs) {
7448: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 7449: my ($symb,$param) = split(/:/,$key);
7450: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 7451: &thaw_unescape($value);
1.319 matthew 7452: }
1.191 harris41 7453: }
1.12 www 7454: return %returnhash;
1.424 matthew 7455: }
7456:
7457: sub convert_dump_to_currentdump{
7458: my %hash = %{shift()};
7459: my %returnhash;
7460: # Code ripped from lond, essentially. The only difference
7461: # here is the unescaping done by lonnet::dump(). Conceivably
7462: # we might run in to problems with parameter names =~ /^v\./
7463: while (my ($key,$value) = each(%hash)) {
7464: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 7465: $symb = &unescape($symb);
7466: $param = &unescape($param);
1.424 matthew 7467: next if ($v eq 'version' || $symb eq 'keys');
7468: next if (exists($returnhash{$symb}) &&
7469: exists($returnhash{$symb}->{$param}) &&
7470: $returnhash{$symb}->{'v.'.$param} > $v);
7471: $returnhash{$symb}->{$param}=$value;
7472: $returnhash{$symb}->{'v.'.$param}=$v;
7473: }
7474: #
7475: # Remove all of the keys in the hashes which keep track of
7476: # the version of the parameter.
7477: while (my ($symb,$param_hash) = each(%returnhash)) {
7478: # use a foreach because we are going to delete from the hash.
7479: foreach my $key (keys(%$param_hash)) {
7480: delete($param_hash->{$key}) if ($key =~ /^v\./);
7481: }
7482: }
7483: return \%returnhash;
1.12 www 7484: }
7485:
1.627 albertel 7486: # ------------------------------------------------------ critical inc interface
7487:
7488: sub cinc {
7489: return &inc(@_,'critical');
7490: }
7491:
1.449 matthew 7492: # --------------------------------------------------------------- inc interface
7493:
7494: sub inc {
1.627 albertel 7495: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 7496: if (!$udomain) { $udomain=$env{'user.domain'}; }
7497: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 7498: my $uhome=&homeserver($uname,$udomain);
7499: my $items='';
7500: if (! ref($store)) {
7501: # got a single value, so use that instead
7502: $items = &escape($store).'=&';
7503: } elsif (ref($store) eq 'SCALAR') {
7504: $items = &escape($$store).'=&';
7505: } elsif (ref($store) eq 'ARRAY') {
7506: $items = join('=&',map {&escape($_);} @{$store});
7507: } elsif (ref($store) eq 'HASH') {
7508: while (my($key,$value) = each(%{$store})) {
7509: $items.= &escape($key).'='.&escape($value).'&';
7510: }
7511: }
7512: $items=~s/\&$//;
1.627 albertel 7513: if ($critical) {
7514: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
7515: } else {
7516: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
7517: }
1.449 matthew 7518: }
7519:
1.12 www 7520: # --------------------------------------------------------------- put interface
7521:
7522: sub put {
1.1172.2.146. .1(raebu 7523:22): my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620 albertel 7524: if (!$udomain) { $udomain=$env{'user.domain'}; }
7525: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7526: my $uhome=&homeserver($uname,$udomain);
1.12 www 7527: my $items='';
1.800 albertel 7528: foreach my $item (keys(%$storehash)) {
7529: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7530: }
1.12 www 7531: $items=~s/\&$//;
1.1172.2.146. .1(raebu 7532:22): if ($encrypt) {
7533:22): return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
7534:22): } else {
7535:22): return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
7536:22): }
1.47 www 7537: }
7538:
1.631 albertel 7539: # ------------------------------------------------------------ newput interface
7540:
7541: sub newput {
7542: my ($namespace,$storehash,$udomain,$uname)=@_;
7543: if (!$udomain) { $udomain=$env{'user.domain'}; }
7544: if (!$uname) { $uname=$env{'user.name'}; }
7545: my $uhome=&homeserver($uname,$udomain);
7546: my $items='';
7547: foreach my $key (keys(%$storehash)) {
7548: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7549: }
7550: $items=~s/\&$//;
7551: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
7552: }
7553:
7554: # --------------------------------------------------------- putstore interface
7555:
1.524 raeburn 7556: sub putstore {
1.1172.2.59 raeburn 7557: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 7558: if (!$udomain) { $udomain=$env{'user.domain'}; }
7559: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 7560: my $uhome=&homeserver($uname,$udomain);
7561: my $items='';
1.715 albertel 7562: foreach my $key (keys(%$storehash)) {
7563: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 7564: }
1.715 albertel 7565: $items=~s/\&$//;
1.716 albertel 7566: my $esc_symb=&escape($symb);
7567: my $esc_v=&escape($version);
1.715 albertel 7568: my $reply =
1.716 albertel 7569: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 7570: $uhome);
1.1172.2.59 raeburn 7571: if (($tolog) && ($reply eq 'ok')) {
7572: my $namevalue='';
7573: foreach my $key (keys(%{$storehash})) {
7574: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
7575: }
1.1172.2.134 raeburn 7576: my $ip = &get_requestor_ip();
1.1172.2.135 raeburn 7577: $namevalue .= 'ip='.&escape($ip).
1.1172.2.59 raeburn 7578: '&host='.&escape($perlvar{'lonHostID'}).
7579: '&version='.$esc_v.
7580: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
7581: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
7582: }
1.715 albertel 7583: if ($reply eq 'unknown_cmd') {
1.716 albertel 7584: # gfall back to way things use to be done
1.715 albertel 7585: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
7586: $uname);
1.524 raeburn 7587: }
1.715 albertel 7588: return $reply;
7589: }
7590:
7591: sub old_putstore {
1.716 albertel 7592: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
7593: if (!$udomain) { $udomain=$env{'user.domain'}; }
7594: if (!$uname) { $uname=$env{'user.name'}; }
7595: my $uhome=&homeserver($uname,$udomain);
7596: my %newstorehash;
1.800 albertel 7597: foreach my $item (keys(%$storehash)) {
7598: my $key = $version.':'.&escape($symb).':'.$item;
7599: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 7600: }
7601: my $items='';
7602: my %allitems = ();
1.800 albertel 7603: foreach my $item (keys(%newstorehash)) {
7604: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 7605: my $key = $1.':keys:'.$2;
7606: $allitems{$key} .= $3.':';
7607: }
1.800 albertel 7608: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 7609: }
1.800 albertel 7610: foreach my $item (keys(%allitems)) {
7611: $allitems{$item} =~ s/\:$//;
7612: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 7613: }
7614: $items=~s/\&$//;
7615: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 7616: }
7617:
1.47 www 7618: # ------------------------------------------------------ critical put interface
7619:
7620: sub cput {
1.134 albertel 7621: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 7622: if (!$udomain) { $udomain=$env{'user.domain'}; }
7623: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7624: my $uhome=&homeserver($uname,$udomain);
1.47 www 7625: my $items='';
1.800 albertel 7626: foreach my $item (keys(%$storehash)) {
7627: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7628: }
1.47 www 7629: $items=~s/\&$//;
1.134 albertel 7630: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7631: }
7632:
7633: # -------------------------------------------------------------- eget interface
7634:
7635: sub eget {
1.133 albertel 7636: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7637: my $items='';
1.800 albertel 7638: foreach my $item (@$storearr) {
7639: $items.=&escape($item).'&';
1.191 harris41 7640: }
1.12 www 7641: $items=~s/\&$//;
1.620 albertel 7642: if (!$udomain) { $udomain=$env{'user.domain'}; }
7643: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7644: my $uhome=&homeserver($uname,$udomain);
7645: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7646: my @pairs=split(/\&/,$rep);
7647: my %returnhash=();
1.42 www 7648: my $i=0;
1.800 albertel 7649: foreach my $item (@$storearr) {
7650: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7651: $i++;
1.191 harris41 7652: }
1.12 www 7653: return %returnhash;
7654: }
7655:
1.667 albertel 7656: # ------------------------------------------------------------ tmpput interface
7657: sub tmpput {
1.802 raeburn 7658: my ($storehash,$server,$context)=@_;
1.667 albertel 7659: my $items='';
1.800 albertel 7660: foreach my $item (keys(%$storehash)) {
7661: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 7662: }
7663: $items=~s/\&$//;
1.802 raeburn 7664: if (defined($context)) {
7665: $items .= ':'.&escape($context);
7666: }
1.667 albertel 7667: return &reply("tmpput:$items",$server);
7668: }
7669:
7670: # ------------------------------------------------------------ tmpget interface
7671: sub tmpget {
1.688 albertel 7672: my ($token,$server)=@_;
7673: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7674: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 7675: my %returnhash;
1.1172.2.85 raeburn 7676: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
7677: return %returnhash;
7678: }
1.667 albertel 7679: foreach my $item (split(/\&/,$rep)) {
7680: my ($key,$value)=split(/=/,$item);
7681: $returnhash{&unescape($key)}=&thaw_unescape($value);
7682: }
7683: return %returnhash;
7684: }
7685:
1.1113 raeburn 7686: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 7687: sub tmpdel {
7688: my ($token,$server)=@_;
7689: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7690: return &reply("tmpdel:$token",$server);
7691: }
7692:
1.1172.2.13 raeburn 7693: # ------------------------------------------------------------ get_timebased_id
7694:
7695: sub get_timebased_id {
7696: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
7697: $maxtries) = @_;
7698: my ($newid,$error,$dellock);
7699: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
7700: return ('','ok','invalid call to get suffix');
7701: }
7702:
7703: # set defaults for any optional args for which values were not supplied
7704: if ($who eq '') {
7705: $who = $env{'user.name'}.':'.$env{'user.domain'};
7706: }
7707: if (!$locktries) {
7708: $locktries = 3;
7709: }
7710: if (!$maxtries) {
7711: $maxtries = 10;
7712: }
7713:
7714: if (($cdom eq '') || ($cnum eq '')) {
7715: if ($env{'request.course.id'}) {
7716: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7717: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7718: }
7719: if (($cdom eq '') || ($cnum eq '')) {
7720: return ('','ok','call to get suffix not in course context');
7721: }
7722: }
7723:
7724: # construct locking item
7725: my $lockhash = {
7726: $prefix."\0".'locked_'.$keyid => $who,
7727: };
7728: my $tries = 0;
7729:
7730: # attempt to get lock on nohist_$namespace file
7731: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7732: while (($gotlock ne 'ok') && $tries <$locktries) {
7733: $tries ++;
7734: sleep 1;
7735: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7736: }
7737:
7738: # attempt to get unique identifier, based on current timestamp
7739: if ($gotlock eq 'ok') {
7740: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
7741: my $id = time;
7742: $newid = $id;
1.1172.2.56 raeburn 7743: if ($idtype eq 'addcode') {
7744: $newid .= &sixnum_code();
7745: }
1.1172.2.13 raeburn 7746: my $idtries = 0;
7747: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
7748: if ($idtype eq 'concat') {
7749: $newid = $id.$idtries;
1.1172.2.56 raeburn 7750: } elsif ($idtype eq 'addcode') {
7751: $newid = $newid.&sixnum_code();
1.1172.2.13 raeburn 7752: } else {
7753: $newid ++;
7754: }
7755: $idtries ++;
7756: }
7757: if (!exists($inuse{$prefix."\0".$newid})) {
7758: my %new_item = (
7759: $prefix."\0".$newid => $who,
7760: );
7761: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
7762: $cdom,$cnum);
7763: if ($putresult ne 'ok') {
7764: undef($newid);
7765: $error = 'error saving new item: '.$putresult;
7766: }
7767: } else {
1.1172.2.56 raeburn 7768: undef($newid);
1.1172.2.13 raeburn 7769: $error = ('error: no unique suffix available for the new item ');
7770: }
7771: # remove lock
7772: my @del_lock = ($prefix."\0".'locked_'.$keyid);
7773: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
7774: } else {
7775: $error = "error: could not obtain lockfile\n";
7776: $dellock = 'ok';
1.1172.2.58 raeburn 7777: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
7778: $dellock = 'nolock';
7779: }
1.1172.2.13 raeburn 7780: }
7781: return ($newid,$dellock,$error);
7782: }
7783:
1.1172.2.56 raeburn 7784: sub sixnum_code {
7785: my $code;
7786: for (0..6) {
7787: $code .= int( rand(9) );
7788: }
7789: return $code;
7790: }
7791:
1.765 albertel 7792: # -------------------------------------------------- portfolio access checking
7793:
7794: sub portfolio_access {
1.1172.2.77 raeburn 7795: my ($requrl,$clientip) = @_;
1.765 albertel 7796: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1172.2.77 raeburn 7797: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 7798: if ($result) {
7799: my %setters;
7800: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1172.2.142 raeburn 7801: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
7802: &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
7803: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7804: return 'B';
7805: }
7806: } else {
1.1172.2.142 raeburn 7807: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdo) =
7808: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
7809: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7810: return 'B';
7811: }
7812: }
7813: }
1.765 albertel 7814: if ($result eq 'ok') {
1.766 albertel 7815: return 'F';
1.765 albertel 7816: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 7817: return 'A';
1.765 albertel 7818: }
1.766 albertel 7819: return '';
1.765 albertel 7820: }
7821:
7822: sub get_portfolio_access {
1.1172.2.146. .23(raeb 7823:-24): my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
1.767 albertel 7824:
7825: if (!ref($access_hash)) {
7826: my $current_perms = &get_portfile_permissions($udom,$unum);
7827: my %access_controls = &get_access_controls($current_perms,$group,
7828: $file_name);
7829: $access_hash = $access_controls{$file_name};
7830: }
7831:
1.1172.2.146. .23(raeb 7832:-24): my $portaccess;
7833:-24): if (ref($portaccess) eq 'SCALAR') {
7834:-24): $portaccess = $$portaccessref;
7835:-24): } else {
7836:-24): $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
7837:-24): }
7838:-24):
7839:-24): my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
1.765 albertel 7840: my $now = time;
7841: if (ref($access_hash) eq 'HASH') {
7842: foreach my $key (keys(%{$access_hash})) {
7843: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1172.2.146. .23(raeb 7844:-24): next if (($scope ne 'ip') && ($portaccess == 0));
1.765 albertel 7845: if ($start > $now) {
7846: next;
7847: }
7848: if ($end && $end<$now) {
7849: next;
7850: }
7851: if ($scope eq 'public') {
7852: $public = $key;
7853: last;
7854: } elsif ($scope eq 'guest') {
7855: $guest = $key;
7856: } elsif ($scope eq 'domains') {
7857: push(@domains,$key);
7858: } elsif ($scope eq 'users') {
7859: push(@users,$key);
7860: } elsif ($scope eq 'course') {
7861: push(@courses,$key);
7862: } elsif ($scope eq 'group') {
7863: push(@groups,$key);
1.1172.2.77 raeburn 7864: } elsif ($scope eq 'ip') {
7865: push(@ips,$key);
1.1172.2.146. .23(raeb 7866:-24): } elsif ($scope eq 'userip') {
7867:-24): push(@userips,$key);
1.765 albertel 7868: }
7869: }
7870: if ($public) {
7871: return 'ok';
1.1172.2.77 raeburn 7872: } elsif (@ips > 0) {
7873: my $allowed;
7874: foreach my $ipkey (@ips) {
7875: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
7876: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
7877: $allowed = 1;
7878: last;
7879: }
7880: }
7881: }
7882: if ($allowed) {
7883: return 'ok';
7884: }
1.1172.2.146. .23(raeb 7885:-24): } elsif (@userips > 0) {
7886:-24): my $allowed;
7887:-24): foreach my $useripkey (@userips) {
7888:-24): if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
7889:-24): if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
7890:-24): $allowed = 1;
7891:-24): last;
7892:-24): }
7893:-24): }
7894:-24): }
7895:-24): if ($allowed) {
7896:-24): return 'ok';
7897:-24): }
1.765 albertel 7898: }
7899: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
7900: if ($guest) {
7901: return $guest;
7902: }
7903: } else {
7904: if (@domains > 0) {
7905: foreach my $domkey (@domains) {
7906: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
7907: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
7908: return 'ok';
7909: }
7910: }
7911: }
7912: }
7913: if (@users > 0) {
7914: foreach my $userkey (@users) {
1.865 raeburn 7915: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
7916: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
7917: if (ref($item) eq 'HASH') {
7918: if (($item->{'uname'} eq $env{'user.name'}) &&
7919: ($item->{'udom'} eq $env{'user.domain'})) {
7920: return 'ok';
7921: }
7922: }
7923: }
7924: }
1.765 albertel 7925: }
7926: }
7927: my %roleshash;
7928: my @courses_and_groups = @courses;
7929: push(@courses_and_groups,@groups);
7930: if (@courses_and_groups > 0) {
7931: my (%allgroups,%allroles);
7932: my ($start,$end,$role,$sec,$group);
7933: foreach my $envkey (%env) {
1.1060 raeburn 7934: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7935: my $cid = $2.'_'.$3;
7936: if ($1 eq 'gr') {
7937: $group = $4;
7938: $allgroups{$cid}{$group} = $env{$envkey};
7939: } else {
7940: if ($4 eq '') {
7941: $sec = 'none';
7942: } else {
7943: $sec = $4;
7944: }
7945: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7946: }
1.811 albertel 7947: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7948: my $cid = $2.'_'.$3;
7949: if ($4 eq '') {
7950: $sec = 'none';
7951: } else {
7952: $sec = $4;
7953: }
7954: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7955: }
7956: }
7957: if (keys(%allroles) == 0) {
7958: return;
7959: }
7960: foreach my $key (@courses_and_groups) {
7961: my %content = %{$$access_hash{$key}};
7962: my $cnum = $content{'number'};
7963: my $cdom = $content{'domain'};
7964: my $cid = $cdom.'_'.$cnum;
7965: if (!exists($allroles{$cid})) {
7966: next;
7967: }
7968: foreach my $role_id (keys(%{$content{'roles'}})) {
7969: my @sections = @{$content{'roles'}{$role_id}{'section'}};
7970: my @groups = @{$content{'roles'}{$role_id}{'group'}};
7971: my @status = @{$content{'roles'}{$role_id}{'access'}};
7972: my @roles = @{$content{'roles'}{$role_id}{'role'}};
7973: foreach my $role (keys(%{$allroles{$cid}})) {
7974: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
7975: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
7976: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
7977: if (grep/^all$/,@sections) {
7978: return 'ok';
7979: } else {
7980: if (grep/^$sec$/,@sections) {
7981: return 'ok';
7982: }
7983: }
7984: }
7985: }
7986: if (keys(%{$allgroups{$cid}}) == 0) {
7987: if (grep/^none$/,@groups) {
7988: return 'ok';
7989: }
7990: } else {
7991: if (grep/^all$/,@groups) {
7992: return 'ok';
7993: }
7994: foreach my $group (keys(%{$allgroups{$cid}})) {
7995: if (grep/^$group$/,@groups) {
7996: return 'ok';
7997: }
7998: }
7999: }
8000: }
8001: }
8002: }
8003: }
8004: }
8005: if ($guest) {
8006: return $guest;
8007: }
8008: }
8009: }
8010: return;
8011: }
8012:
8013: sub course_group_datechecker {
8014: my ($dates,$now,$status) = @_;
8015: my ($start,$end) = split(/\./,$dates);
8016: if (!$start && !$end) {
8017: return 'ok';
8018: }
8019: if (grep/^active$/,@{$status}) {
8020: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
8021: return 'ok';
8022: }
8023: }
8024: if (grep/^previous$/,@{$status}) {
8025: if ($end > $now ) {
8026: return 'ok';
8027: }
8028: }
8029: if (grep/^future$/,@{$status}) {
8030: if ($start > $now) {
8031: return 'ok';
8032: }
8033: }
8034: return;
8035: }
8036:
8037: sub parse_portfolio_url {
8038: my ($url) = @_;
8039:
8040: my ($type,$udom,$unum,$group,$file_name);
8041:
1.823 albertel 8042: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 8043: $type = 1;
8044: $udom = $1;
8045: $unum = $2;
8046: $file_name = $3;
1.823 albertel 8047: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 8048: $type = 2;
8049: $udom = $1;
8050: $unum = $2;
8051: $group = $3;
8052: $file_name = $3.'/'.$4;
8053: }
8054: if (wantarray) {
8055: return ($type,$udom,$unum,$file_name,$group);
8056: }
8057: return $type;
8058: }
8059:
8060: sub is_portfolio_url {
8061: my ($url) = @_;
8062: return scalar(&parse_portfolio_url($url));
8063: }
8064:
1.798 raeburn 8065: sub is_portfolio_file {
8066: my ($file) = @_;
1.820 raeburn 8067: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 8068: return 1;
8069: }
8070: return;
8071: }
8072:
1.1172.2.146. .13(raeb 8073:-23): sub is_coursetool_logo {
8074:-23): my ($uri) = @_;
8075:-23): if ($env{'request.course.id'}) {
8076:-23): my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
8077:-23): if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
8078:-23): return 1;
8079:-23): }
8080:-23): }
8081:-23): return;
8082:-23): }
8083:-23):
1.976 raeburn 8084: sub usertools_access {
1.1084 raeburn 8085: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 8086: my ($access,%tools);
8087: if ($context eq '') {
8088: $context = 'tools';
8089: }
8090: if ($context eq 'requestcourses') {
8091: %tools = (
8092: official => 1,
8093: unofficial => 1,
1.1006 raeburn 8094: community => 1,
1.1172.2.37 raeburn 8095: textbook => 1,
1.1172.2.146. .13(raeb 8096:-23): lti => 1,
1.985 raeburn 8097: );
1.1172.2.9 raeburn 8098: } elsif ($context eq 'requestauthor') {
8099: %tools = (
8100: requestauthor => 1,
8101: );
1.1172.2.146. .18(raeb 8102:-24): } elsif ($context eq 'authordefaults') {
8103:-24): %tools = (
8104:-24): webdav => 1,
8105:-24): );
1.985 raeburn 8106: } else {
8107: %tools = (
8108: aboutme => 1,
8109: blog => 1,
1.1172.2.146. .24(raeb 8110:-24): webdav => 1,
1.985 raeburn 8111: portfolio => 1,
1.1172.2.146. .22(raeb 8112:-24): portaccess => 1,
.6(raebu 8113:22): timezone => 1,
1.985 raeburn 8114: );
8115: }
1.976 raeburn 8116: return if (!defined($tools{$tool}));
8117:
1.1172.2.35 raeburn 8118: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 8119: $udom = $env{'user.domain'};
8120: $uname = $env{'user.name'};
8121: }
8122:
1.978 raeburn 8123: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
8124: if ($action ne 'reload') {
1.985 raeburn 8125: if ($context eq 'requestcourses') {
8126: return $env{'environment.canrequest.'.$tool};
1.1172.2.9 raeburn 8127: } elsif ($context eq 'requestauthor') {
8128: return $env{'environment.canrequest.author'};
1.1172.2.146. .18(raeb 8129:-24): } elsif ($context eq 'authordefaults') {
8130:-24): if ($tool eq 'webdav') {
8131:-24): return $env{'environment.availabletools.'.$tool};
8132:-24): }
1.985 raeburn 8133: } else {
8134: return $env{'environment.availabletools.'.$tool};
8135: }
8136: }
1.976 raeburn 8137: }
8138:
1.1172.2.9 raeburn 8139: my ($toolstatus,$inststatus,$envkey);
8140: if ($context eq 'requestauthor') {
8141: $envkey = $context;
1.1172.2.146. .18(raeb 8142:-24): } elsif ($context eq 'authordefaults') {
8143:-24): if ($tool eq 'webdav') {
8144:-24): $envkey = 'tools.'.$tool;
8145:-24): }
1.1172.2.9 raeburn 8146: } else {
8147: $envkey = $context.'.'.$tool;
8148: }
1.976 raeburn 8149:
1.985 raeburn 8150: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8151: ($action ne 'reload')) {
1.1172.2.9 raeburn 8152: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 8153: $inststatus = $env{'environment.inststatus'};
8154: } else {
1.1084 raeburn 8155: if (ref($userenvref) eq 'HASH') {
1.1172.2.9 raeburn 8156: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 8157: $inststatus = $userenvref->{'inststatus'};
8158: } else {
1.1172.2.9 raeburn 8159: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
8160: $toolstatus = $userenv{$envkey};
1.1084 raeburn 8161: $inststatus = $userenv{'inststatus'};
8162: }
1.976 raeburn 8163: }
8164:
8165: if ($toolstatus ne '') {
8166: if ($toolstatus) {
8167: $access = 1;
8168: } else {
8169: $access = 0;
8170: }
8171: return $access;
8172: }
8173:
1.1084 raeburn 8174: my ($is_adv,%domdef);
8175: if (ref($is_advref) eq 'HASH') {
8176: $is_adv = $is_advref->{'is_adv'};
8177: } else {
8178: $is_adv = &is_advanced_user($udom,$uname);
8179: }
8180: if (ref($domdefref) eq 'HASH') {
8181: %domdef = %{$domdefref};
8182: } else {
8183: %domdef = &get_domain_defaults($udom);
8184: }
1.976 raeburn 8185: if (ref($domdef{$tool}) eq 'HASH') {
8186: if ($is_adv) {
8187: if ($domdef{$tool}{'_LC_adv'} ne '') {
8188: if ($domdef{$tool}{'_LC_adv'}) {
8189: $access = 1;
8190: } else {
8191: $access = 0;
8192: }
8193: return $access;
8194: }
8195: }
8196: if ($inststatus ne '') {
8197: my ($hasaccess,$hasnoaccess);
8198: foreach my $affiliation (split(/:/,$inststatus)) {
8199: if ($domdef{$tool}{$affiliation} ne '') {
8200: if ($domdef{$tool}{$affiliation}) {
8201: $hasaccess = 1;
8202: } else {
8203: $hasnoaccess = 1;
8204: }
8205: }
8206: }
8207: if ($hasaccess || $hasnoaccess) {
8208: if ($hasaccess) {
8209: $access = 1;
8210: } elsif ($hasnoaccess) {
8211: $access = 0;
8212: }
8213: return $access;
8214: }
8215: } else {
8216: if ($domdef{$tool}{'default'} ne '') {
8217: if ($domdef{$tool}{'default'}) {
8218: $access = 1;
8219: } elsif ($domdef{$tool}{'default'} == 0) {
8220: $access = 0;
8221: }
8222: return $access;
8223: }
8224: }
8225: } else {
1.1172.2.6 raeburn 8226: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 8227: $access = 1;
8228: } else {
8229: $access = 0;
8230: }
1.976 raeburn 8231: return $access;
8232: }
8233: }
8234:
1.1050 raeburn 8235: sub is_course_owner {
8236: my ($cdom,$cnum,$udom,$uname) = @_;
8237: if (($udom eq '') || ($uname eq '')) {
8238: $udom = $env{'user.domain'};
8239: $uname = $env{'user.name'};
8240: }
8241: unless (($udom eq '') || ($uname eq '')) {
8242: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
8243: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
8244: return 1;
8245: } else {
8246: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
8247: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
8248: return 1;
8249: }
8250: }
8251: }
8252: }
8253: return;
8254: }
8255:
1.976 raeburn 8256: sub is_advanced_user {
8257: my ($udom,$uname) = @_;
1.1085 raeburn 8258: if ($udom ne '' && $uname ne '') {
8259: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 8260: if (wantarray) {
8261: return ($env{'user.adv'},$env{'user.author'});
8262: } else {
8263: return $env{'user.adv'};
8264: }
1.1085 raeburn 8265: }
8266: }
1.976 raeburn 8267: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
8268: my %allroles;
1.1128 raeburn 8269: my ($is_adv,$is_author);
1.976 raeburn 8270: foreach my $role (keys(%roleshash)) {
8271: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
8272: my $area = '/'.$tdomain.'/'.$trest;
8273: if ($sec ne '') {
8274: $area .= '/'.$sec;
8275: }
8276: if (($area ne '') && ($trole ne '')) {
8277: my $spec=$trole.'.'.$area;
8278: if ($trole =~ /^cr\//) {
8279: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
8280: } elsif ($trole ne 'gr') {
8281: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
8282: }
1.1128 raeburn 8283: if ($trole eq 'au') {
8284: $is_author = 1;
8285: }
1.976 raeburn 8286: }
8287: }
8288: foreach my $role (keys(%allroles)) {
8289: last if ($is_adv);
8290: foreach my $item (split(/:/,$allroles{$role})) {
8291: if ($item ne '') {
8292: my ($privilege,$restrictions)=split(/&/,$item);
8293: if ($privilege eq 'adv') {
8294: $is_adv = 1;
8295: last;
8296: }
8297: }
8298: }
8299: }
1.1128 raeburn 8300: if (wantarray) {
8301: return ($is_adv,$is_author);
8302: }
1.976 raeburn 8303: return $is_adv;
8304: }
1.798 raeburn 8305:
1.1035 raeburn 8306: sub check_can_request {
1.1172.2.146. .13(raeb 8307:-23): my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035 raeburn 8308: my $canreq = 0;
1.1172.2.146. .13(raeb 8309:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8310:-23): $uname = $env{'user.name'};
8311:-23): $udom = $env{'user.domain'};
8312:-23): }
1.1035 raeburn 8313: my ($types,$typename) = &Apache::loncommon::course_types();
8314: my @options = ('approval','validate','autolimit');
8315: my $optregex = join('|',@options);
8316: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
8317: foreach my $type (@{$types}) {
1.1172.2.146. .13(raeb 8318:-23): if (&usertools_access($uname,$udom,$type,undef,
8319:-23): 'requestcourses')) {
1.1035 raeburn 8320: $canreq ++;
1.1036 raeburn 8321: if (ref($request_domains) eq 'HASH') {
1.1172.2.146. .13(raeb 8322:-23): push(@{$request_domains->{$type}},$udom);
1.1036 raeburn 8323: }
1.1172.2.146. .13(raeb 8324:-23): if ($dom eq $udom) {
1.1035 raeburn 8325: $can_request->{$type} = 1;
8326: }
8327: }
1.1172.2.146. .13(raeb 8328:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
8329:-23): ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035 raeburn 8330: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
8331: if (@curr > 0) {
1.1036 raeburn 8332: foreach my $item (@curr) {
8333: if (ref($request_domains) eq 'HASH') {
8334: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
8335: if ($otherdom ne '') {
8336: if (ref($request_domains->{$type}) eq 'ARRAY') {
8337: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
8338: push(@{$request_domains->{$type}},$otherdom);
8339: }
8340: } else {
8341: push(@{$request_domains->{$type}},$otherdom);
8342: }
8343: }
8344: }
8345: }
1.1172.2.146. .13(raeb 8346:-23): unless ($dom eq $env{'user.domain'}) {
1.1036 raeburn 8347: $canreq ++;
1.1035 raeburn 8348: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
8349: $can_request->{$type} = 1;
8350: }
8351: }
8352: }
8353: }
8354: }
8355: }
8356: return $canreq;
8357: }
8358:
1.341 www 8359: # ---------------------------------------------- Custom access rule evaluation
8360:
8361: sub customaccess {
8362: my ($priv,$uri)=@_;
1.807 albertel 8363: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 8364: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 8365: $udom = &LONCAPA::clean_domain($udom);
8366: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 8367: my $access=0;
1.800 albertel 8368: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 8369: my ($effect,$realm,$role,$type)=split(/\:/,$right);
8370: if ($type eq 'user') {
8371: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 8372: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 8373: if ($tdom) {
8374: if ($tdom ne $env{'user.domain'}) { next; }
8375: }
1.896 albertel 8376: if ($tuname) {
8377: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 8378: }
8379: $access=($effect eq 'allow');
8380: last;
8381: }
8382: } else {
8383: if ($role) {
8384: if ($role ne $urole) { next; }
8385: }
8386: foreach my $scope (split(/\s*\,\s*/,$realm)) {
8387: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
8388: if ($tdom) {
8389: if ($tdom ne $udom) { next; }
8390: }
8391: if ($tcrs) {
8392: if ($tcrs ne $ucrs) { next; }
8393: }
8394: if ($tsec) {
8395: if ($tsec ne $usec) { next; }
8396: }
8397: $access=($effect eq 'allow');
8398: last;
8399: }
8400: if ($realm eq '' && $role eq '') {
8401: $access=($effect eq 'allow');
8402: }
1.402 bowersj2 8403: }
1.341 www 8404: }
8405: return $access;
8406: }
8407:
1.103 harris41 8408: # ------------------------------------------------- Check for a user privilege
1.12 www 8409:
8410: sub allowed {
1.1172.2.146. .1(raebu 8411:22): my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705 albertel 8412: my $ver_orguri=$uri;
1.439 www 8413: $uri=&deversion($uri);
1.152 www 8414: my $orguri=$uri;
1.52 www 8415: $uri=&declutter($uri);
1.809 raeburn 8416:
1.810 raeburn 8417: if ($priv eq 'evb') {
1.1172.2.146. .3(raebu 8418:22): # Evade communication block restrictions for specified role in a course or domain
1.810 raeburn 8419: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
8420: return $1;
8421: } else {
8422: return;
8423: }
8424: }
8425:
1.620 albertel 8426: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 8427: # Free bre access to adm and meta resources
1.1172.2.146. .1(raebu 8428:22): if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$}))
1.769 albertel 8429: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
8430: && ($priv eq 'bre')) {
1.14 www 8431: return 'F';
1.159 www 8432: }
8433:
1.545 banghart 8434: # Free bre access to user's own portfolio contents
1.714 raeburn 8435: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 8436: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 8437: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 8438: my %setters;
1.1172.2.142 raeburn 8439: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
8440: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
8441: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 8442: return 'B';
8443: } else {
8444: return 'F';
8445: }
1.545 banghart 8446: }
8447:
1.762 raeburn 8448: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 8449: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
8450: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
8451: if (exists($env{'request.course.id'})) {
8452: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8453: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8454: if (($domain eq $cdom) && ($name eq $cnum)) {
8455: my $courseprivid=$env{'request.course.id'};
8456: $courseprivid=~s/\_/\//;
8457: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
8458: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
8459: return $1;
1.762 raeburn 8460: } else {
8461: if ($env{'request.course.sec'}) {
8462: $courseprivid.='/'.$env{'request.course.sec'};
8463: }
8464: if ($env{'user.priv.'.$env{'request.role'}.'./'.
8465: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
8466: return $2;
8467: }
1.714 raeburn 8468: }
8469: }
8470: }
8471: }
8472:
1.159 www 8473: # Free bre to public access
8474:
8475: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8476:22): my $copyright;
8477:22): unless ($uri =~ /ext\.tool/) {
8478:22): $copyright=&metadata($uri,'copyright');
8479:22): }
1.620 albertel 8480: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 8481: return 'F';
8482: }
1.238 www 8483: if ($copyright eq 'priv') {
8484: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8485: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 8486: return '';
8487: }
8488: }
8489: if ($copyright eq 'domain') {
8490: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8491: unless (($env{'user.domain'} eq $1) ||
8492: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 8493: return '';
8494: }
1.262 matthew 8495: }
1.620 albertel 8496: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 8497: # Library role, so allow browsing of resources in this domain.
8498: return 'F';
1.238 www 8499: }
1.341 www 8500: if ($copyright eq 'custom') {
8501: unless (&customaccess($priv,$uri)) { return ''; }
8502: }
1.14 www 8503: }
1.264 matthew 8504: # Domain coordinator is trying to create a course
1.620 albertel 8505: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 8506: # uri is the requested domain in this case.
8507: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 8508: # a role of dc for the domain in question.
1.620 albertel 8509: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 8510: }
1.29 www 8511:
1.52 www 8512: my $thisallowed='';
8513: my $statecond=0;
8514: my $courseprivid='';
8515:
1.1039 raeburn 8516: my $ownaccess;
1.1043 raeburn 8517: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 8518: if (($priv eq 'bro') && ($env{'user.author'})) {
8519: if ($uri eq '') {
8520: $ownaccess = 1;
8521: } else {
8522: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
8523: my $udom = $env{'user.domain'};
8524: my $uname = $env{'user.name'};
8525: if ($uri =~ m{^\Q$udom\E/?$}) {
8526: $ownaccess = 1;
1.1040 raeburn 8527: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 8528: unless ($uri =~ m{\.\./}) {
8529: $ownaccess = 1;
8530: }
8531: } elsif (($udom ne 'public') && ($uname ne 'public')) {
8532: my $now = time;
8533: if ($uri =~ m{^([^/]+)/?$}) {
8534: my $adom = $1;
8535: foreach my $key (keys(%env)) {
1.1042 raeburn 8536: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1172.2.142 raeburn 8537: my ($start,$end) = split(/\./,$env{$key});
8538: if (($now >= $start) && (!$end || $end > $now)) {
1.1039 raeburn 8539: $ownaccess = 1;
8540: last;
8541: }
8542: }
8543: }
8544: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
8545: my $adom = $1;
8546: my $aname = $2;
1.1042 raeburn 8547: foreach my $role ('ca','aa') {
8548: if ($env{"user.role.$role./$adom/$aname"}) {
8549: my ($start,$end) =
1.1172.2.142 raeburn 8550: split(/\./,$env{"user.role.$role./$adom/$aname"});
8551: if (($now >= $start) && (!$end || $end > $now)) {
1.1042 raeburn 8552: $ownaccess = 1;
8553: last;
8554: }
1.1039 raeburn 8555: }
8556: }
8557: }
8558: }
8559: }
8560: }
8561: }
8562:
1.52 www 8563: # Course
8564:
1.620 albertel 8565: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8566: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8567: $thisallowed.=$1;
8568: }
1.52 www 8569: }
1.29 www 8570:
1.52 www 8571: # Domain
8572:
1.620 albertel 8573: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 8574: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8575: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8576: $thisallowed.=$1;
8577: }
1.12 www 8578: }
1.52 www 8579:
1.1141 raeburn 8580: # User who is not author or co-author might still be able to edit
8581: # resource of an author in the domain (e.g., if Domain Coordinator).
8582: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
8583: (&allowed('mdc',$env{'request.course.id'}))) {
8584: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
8585: $thisallowed.=$1;
8586: }
8587: }
8588:
1.52 www 8589: # Course: uri itself is a course
1.66 www 8590: my $courseuri=$uri;
8591: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 8592: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 8593:
1.620 albertel 8594: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 8595: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.115 raeburn 8596: if ($priv eq 'mip') {
8597: my $rem = $1;
8598: if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
8599: ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
8600: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8601: if ($cdom ne '') {
8602: my %passwdconf = &get_passwdconf($cdom);
8603: if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
8604: if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
8605: if (@{$passwdconf{'crsownerchg'}{'by'}}) {
8606: my @inststatuses = split(':',$env{'environment.inststatus'});
8607: unless (@inststatuses) {
8608: @inststatuses = ('default');
8609: }
8610: foreach my $status (@inststatuses) {
8611: if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
8612: $thisallowed.=$rem;
8613: }
8614: }
8615: }
8616: }
8617: }
8618: }
8619: }
8620: } else {
8621: unless (($priv eq 'bro') && (!$ownaccess)) {
8622: $thisallowed.=$1;
8623: }
1.1039 raeburn 8624: }
1.12 www 8625: }
1.29 www 8626:
1.665 albertel 8627: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 8628: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 8629: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 8630: $thisallowed='';
1.671 raeburn 8631: my ($match)=&is_on_map($uri);
8632: if ($match) {
8633: if ($env{'user.priv.'.$env{'request.role'}.'./'}
8634: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.65 raeburn 8635: my $value = $1;
1.1172.2.146. .1(raebu 8636:22): my $deeplinkblock;
8637:22): unless ($nodeeplinkcheck) {
8638:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8639:22): }
8640:22): if ($deeplinkblock) {
8641:22): $thisallowed='D';
8642:22): } elsif ($noblockcheck) {
1.1172.2.65 raeburn 8643: $thisallowed.=$value;
1.1162 raeburn 8644: } else {
1.1172.2.126 raeburn 8645: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8646: if (@blockers > 0) {
8647: $thisallowed = 'B';
8648: } else {
8649: $thisallowed.=$value;
8650: }
1.1162 raeburn 8651: }
1.671 raeburn 8652: }
8653: } else {
1.705 albertel 8654: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 8655: if ($refuri) {
8656: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 8657: $thisallowed='F';
1.671 raeburn 8658: } else {
8659: $refuri=&declutter($refuri);
8660: my ($match) = &is_on_map($refuri);
8661: if ($match) {
1.1172.2.146. .1(raebu 8662:22): my $deeplinkblock;
8663:22): unless ($nodeeplinkcheck) {
8664:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8665:22): }
8666:22): if ($deeplinkblock) {
8667:22): $thisallowed='D';
8668:22): } elsif ($noblockcheck) {
1.1162 raeburn 8669: $thisallowed='F';
1.1172.2.65 raeburn 8670: } else {
1.1172.2.127 raeburn 8671: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8672: if (@blockers > 0) {
8673: $thisallowed = 'B';
8674: } else {
8675: $thisallowed='F';
8676: }
1.1162 raeburn 8677: }
1.671 raeburn 8678: }
1.669 raeburn 8679: }
1.671 raeburn 8680: }
8681: }
1.314 www 8682: }
1.492 albertel 8683:
1.766 albertel 8684: if ($priv eq 'bre'
8685: && $thisallowed ne 'F'
8686: && $thisallowed ne '2'
8687: && &is_portfolio_url($uri)) {
1.1172.2.77 raeburn 8688: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 8689: }
8690:
1.52 www 8691: # Full access at system, domain or course-wide level? Exit.
1.29 www 8692: if ($thisallowed=~/F/) {
8693: return 'F';
8694: }
8695:
1.52 www 8696: # If this is generating or modifying users, exit with special codes
1.29 www 8697:
1.1172.2.146. .22(raeb 8698:-24): if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa::vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643 www 8699: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 8700: my ($audom,$auname)=split('/',$uri);
1.643 www 8701: # no author name given, so this just checks on the general right to make a co-author in this domain
8702: unless ($auname) { return $thisallowed; }
8703: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 8704: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
8705: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
8706: ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1172.2.146. .22(raeb 8707:-24): } elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
8708:-24): my ($audom,$auname)=split('/',$uri);
8709:-24): unless ($auname) { return $thisallowed; }
8710:-24): unless (($env{'request.role'} eq "dc./$audom") ||
8711:-24): ($env{'request.role'} eq "ca./$uri")) {
8712:-24): return '';
8713:-24): }
1.642 albertel 8714: }
1.52 www 8715: return $thisallowed;
8716: }
8717: #
1.103 harris41 8718: # Gathered so far: system, domain and course wide privileges
1.52 www 8719: #
8720: # Course: See if uri or referer is an individual resource that is part of
8721: # the course
8722:
1.620 albertel 8723: if ($env{'request.course.id'}) {
1.232 www 8724:
1.1172.2.146. .13(raeb 8725:-23): if ($priv eq 'bre') {
8726:-23): if (&is_coursetool_logo($uri)) {
8727:-23): return 'F';
8728:-23): }
8729:-23): }
8730:-23):
1.1172.2.115 raeburn 8731: # If this is modifying password (internal auth) domains must match for user and user's role.
8732:
8733: if ($priv eq 'mip') {
8734: if ($env{'user.domain'} eq $env{'request.role.domain'}) {
8735: return $thisallowed;
8736: } else {
8737: return '';
8738: }
8739: }
8740:
1.620 albertel 8741: $courseprivid=$env{'request.course.id'};
8742: if ($env{'request.course.sec'}) {
8743: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 8744: }
8745: $courseprivid=~s/\_/\//;
8746: my $checkreferer=1;
1.232 www 8747: my ($match,$cond)=&is_on_map($uri);
8748: if ($match) {
8749: $statecond=$cond;
1.620 albertel 8750: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8751: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8752: my $value = $1;
8753: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8754:22): my $deeplinkblock;
8755:22): unless ($nodeeplinkcheck) {
8756:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8757:22): }
8758:22): if ($deeplinkblock) {
8759:22): $thisallowed = 'D';
8760:22): } elsif ($noblockcheck) {
1.1162 raeburn 8761: $thisallowed.=$value;
1.1172.2.65 raeburn 8762: } else {
1.1172.2.126 raeburn 8763: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8764: if (@blockers > 0) {
8765: $thisallowed = 'B';
8766: } else {
8767: $thisallowed.=$value;
8768: }
1.1162 raeburn 8769: }
8770: } else {
8771: $thisallowed.=$value;
8772: }
1.52 www 8773: $checkreferer=0;
8774: }
1.29 www 8775: }
1.1172.2.126 raeburn 8776:
1.148 www 8777: if ($checkreferer) {
1.620 albertel 8778: my $refuri=$env{'httpref.'.$orguri};
1.148 www 8779: unless ($refuri) {
1.800 albertel 8780: foreach my $key (keys(%env)) {
8781: if ($key=~/^httpref\..*\*/) {
8782: my $pattern=$key;
1.156 www 8783: $pattern=~s/^httpref\.\/res\///;
1.148 www 8784: $pattern=~s/\*/\[\^\/\]\+/g;
8785: $pattern=~s/\//\\\//g;
1.152 www 8786: if ($orguri=~/$pattern/) {
1.800 albertel 8787: $refuri=$env{$key};
1.148 www 8788: }
8789: }
1.191 harris41 8790: }
1.148 www 8791: }
1.232 www 8792:
1.148 www 8793: if ($refuri) {
1.152 www 8794: $refuri=&declutter($refuri);
1.232 www 8795: my ($match,$cond)=&is_on_map($refuri);
8796: if ($match) {
8797: my $refstatecond=$cond;
1.620 albertel 8798: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8799: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8800: my $value = $1;
8801: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8802:22): my $deeplinkblock;
8803:22): unless ($nodeeplinkcheck) {
8804:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8805:22): }
8806:22): if ($deeplinkblock) {
8807:22): $thisallowed = 'D';
8808:22): } elsif ($noblockcheck) {
1.1162 raeburn 8809: $thisallowed.=$value;
1.1172.2.65 raeburn 8810: } else {
1.1172.2.127 raeburn 8811: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8812: if (@blockers > 0) {
8813: $thisallowed = 'B';
8814: } else {
8815: $thisallowed.=$value;
8816: }
1.1162 raeburn 8817: }
8818: } else {
8819: $thisallowed.=$value;
8820: }
1.53 www 8821: $uri=$refuri;
8822: $statecond=$refstatecond;
1.52 www 8823: }
8824: }
1.148 www 8825: }
1.29 www 8826: }
1.52 www 8827: }
1.29 www 8828:
1.52 www 8829: #
1.103 harris41 8830: # Gathered now: all privileges that could apply, and condition number
1.52 www 8831: #
8832: #
8833: # Full or no access?
8834: #
1.29 www 8835:
1.52 www 8836: if ($thisallowed=~/F/) {
8837: return 'F';
8838: }
1.29 www 8839:
1.52 www 8840: unless ($thisallowed) {
8841: return '';
8842: }
1.29 www 8843:
1.52 www 8844: # Restrictions exist, deal with them
8845: #
8846: # C:according to course preferences
8847: # R:according to resource settings
8848: # L:unless locked
8849: # X:according to user session state
8850: #
8851:
8852: # Possibly locked functionality, check all courses
1.1172.2.142 raeburn 8853: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54 www 8854: # Locks might take effect only after 10 minutes cache expiration for other
1.1172.2.142 raeburn 8855: # courses, and 2 minutes for current course, in which user has st or ta role
8856: # which is neither expired nor a future role (unless current course).
1.52 www 8857:
1.1172.2.142 raeburn 8858: my ($needlockcheck,$now,$crsonly);
1.52 www 8859: if ($thisallowed=~/L/) {
1.1172.2.142 raeburn 8860: $now = time;
8861: if ($priv eq 'bre') {
8862: if ($uri ne '') {
8863: if ($orguri =~ m{^/+res/}) {
8864: if ($uri =~ m{^lib/templates/}) {
8865: if ($env{'request.course.id'}) {
8866: $crsonly = 1;
8867: $needlockcheck = 1;
8868: }
8869: } else {
8870: $needlockcheck = 1;
8871: }
8872: } elsif ($env{'request.course.id'}) {
8873: my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
8874: if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
8875: ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
8876: $crsonly = 1;
8877: }
8878: $needlockcheck = 1;
8879: }
8880: }
8881: } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
8882: $needlockcheck = 1;
8883: }
8884: }
8885: if ($needlockcheck) {
8886: foreach my $envkey (keys(%env)) {
1.54 www 8887: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
8888: my $courseid=$2;
8889: my $roleid=$1.'.'.$2;
1.92 www 8890: $courseid=~s/^\///;
1.1172.2.142 raeburn 8891: unless ($env{'request.role'} eq $roleid) {
8892: my ($start,$end) = split(/\./,$env{$envkey});
8893: next unless (($now >= $start) && (!$end || $end > $now));
8894: }
1.54 www 8895: my $expiretime=600;
1.620 albertel 8896: if ($env{'request.role'} eq $roleid) {
1.54 www 8897: $expiretime=120;
8898: }
8899: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
8900: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 8901: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 8902: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 8903: }
1.620 albertel 8904: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
8905: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
8906: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
8907: &log($env{'user.domain'},$env{'user.name'},
8908: $env{'user.home'},
1.57 www 8909: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 8910: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8911: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8912: return '';
8913: }
8914: }
1.620 albertel 8915: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
8916: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1172.2.142 raeburn 8917: if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620 albertel 8918: &log($env{'user.domain'},$env{'user.name'},
8919: $env{'user.home'},
1.57 www 8920: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 8921: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8922: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8923: return '';
8924: }
8925: }
8926: }
1.29 www 8927: }
1.52 www 8928: }
1.1172.2.126 raeburn 8929:
1.52 www 8930: #
8931: # Rest of the restrictions depend on selected course
8932: #
8933:
1.620 albertel 8934: unless ($env{'request.course.id'}) {
1.766 albertel 8935: if ($thisallowed eq 'A') {
8936: return 'A';
1.814 raeburn 8937: } elsif ($thisallowed eq 'B') {
8938: return 'B';
1.766 albertel 8939: } else {
8940: return '1';
8941: }
1.52 www 8942: }
1.29 www 8943:
1.52 www 8944: #
8945: # Now user is definitely in a course
8946: #
1.53 www 8947:
8948:
8949: # Course preferences
8950:
8951: if ($thisallowed=~/C/) {
1.620 albertel 8952: my $rolecode=(split(/\./,$env{'request.role'}))[0];
8953: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
8954: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 8955: =~/\Q$rolecode\E/) {
1.1103 raeburn 8956: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8957: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8958: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
8959: $env{'request.course.id'});
8960: }
1.237 www 8961: return '';
8962: }
8963:
1.620 albertel 8964: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 8965: =~/\Q$unamedom\E/) {
1.1103 raeburn 8966: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8967: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
8968: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
8969: $env{'request.course.id'});
8970: }
1.54 www 8971: return '';
8972: }
1.53 www 8973: }
8974:
8975: # Resource preferences
8976:
8977: if ($thisallowed=~/R/) {
1.620 albertel 8978: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 8979: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 8980: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8981: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8982: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
8983: }
8984: return '';
1.54 www 8985: }
1.53 www 8986: }
1.30 www 8987:
1.1172.2.146. .1(raebu 8988:22): # Restricted for deeplinked session?
8989:22):
8990:22): if ($env{'request.deeplink.login'}) {
8991:22): if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
8992:22): if (!$symb) { $symb=&symbread($uri,1); }
8993:22): if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
8994:22): return '';
8995:22): }
8996:22): }
8997:22): }
8998:22):
1.246 www 8999: # Restricted by state or randomout?
1.30 www 9000:
1.52 www 9001: if ($thisallowed=~/X/) {
1.620 albertel 9002: if ($env{'acc.randomout'}) {
1.579 albertel 9003: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 9004: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 9005: return '';
9006: }
1.247 www 9007: }
9008: if (&condval($statecond)) {
1.52 www 9009: return '2';
9010: } else {
9011: return '';
9012: }
9013: }
1.30 www 9014:
1.766 albertel 9015: if ($thisallowed eq 'A') {
9016: return 'A';
1.814 raeburn 9017: } elsif ($thisallowed eq 'B') {
9018: return 'B';
1.1172.2.146. .1(raebu 9019:22): } elsif ($thisallowed eq 'D') {
9020:22): return 'D';
1.766 albertel 9021: }
1.52 www 9022: return 'F';
1.232 www 9023: }
1.1162 raeburn 9024:
1.1172.2.13 raeburn 9025: # ------------------------------------------- Check construction space access
9026:
9027: sub constructaccess {
9028: my ($url,$setpriv)=@_;
9029:
9030: # We do not allow editing of previous versions of files
9031: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
9032:
9033: # Get username and domain from URL
9034: my ($ownername,$ownerdomain,$ownerhome);
9035:
9036: ($ownerdomain,$ownername) =
1.1172.2.146. .19(raeb 9037:-24): ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1172.2.13 raeburn 9038:
9039: # The URL does not really point to any authorspace, forget it
9040: unless (($ownername) && ($ownerdomain)) { return ''; }
9041:
9042: # Now we need to see if the user has access to the authorspace of
9043: # $ownername at $ownerdomain
9044:
9045: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
9046: # Real author for this?
9047: $ownerhome = $env{'user.home'};
9048: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
9049: return ($ownername,$ownerdomain,$ownerhome);
9050: }
9051: } else {
9052: # Co-author for this?
9053: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
9054: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
9055: $ownerhome = &homeserver($ownername,$ownerdomain);
9056: return ($ownername,$ownerdomain,$ownerhome);
9057: }
9058: }
9059:
9060: # We don't have any access right now. If we are not possibly going to do anything about this,
9061: # we might as well leave
9062: unless ($setpriv) { return ''; }
9063:
9064: # Backdoor access?
9065: my $allowed=&allowed('eco',$ownerdomain);
9066: # Nope
9067: unless ($allowed) { return ''; }
9068: # Looks like we may have access, but could be locked by the owner of the construction space
9069: if ($allowed eq 'U') {
9070: my %blocked=&get('environment',['domcoord.author'],
9071: $ownerdomain,$ownername);
9072: # Is blocked by owner
9073: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
9074: }
9075: if (($allowed eq 'F') || ($allowed eq 'U')) {
9076: # Grant temporary access
9077: my $then=$env{'user.login.time'};
1.1172.2.16 raeburn 9078: my $update=$env{'user.update.time'};
1.1172.2.13 raeburn 9079: if (!$update) { $update = $then; }
9080: my $refresh=$env{'user.refresh.time'};
9081: if (!$refresh) { $refresh = $update; }
9082: my $now = time;
9083: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
9084: $now,'ca','constructaccess');
9085: $ownerhome = &homeserver($ownername,$ownerdomain);
9086: return($ownername,$ownerdomain,$ownerhome);
9087: }
9088: # No business here
9089: return '';
9090: }
9091:
1.1172.2.66 raeburn 9092: # ----------------------------------------------------------- Content Blocking
9093:
9094: {
9095: # Caches for faster Course Contents display where content blocking
9096: # is in operation (i.e., interval param set) for timed quiz.
9097: #
9098: # User for whom data are being temporarily cached.
9099: my $cacheduser='';
1.1172.2.126 raeburn 9100: # Course for which data are being temporarily cached.
9101: my $cachedcid='';
1.1172.2.66 raeburn 9102: # Cached blockers for this user (a hash of blocking items).
9103: my %cachedblockers=();
9104: # When the data were last cached.
9105: my $cachedlast='';
9106:
9107: sub load_all_blockers {
1.1172.2.128 raeburn 9108: my ($uname,$udom)=@_;
1.1172.2.66 raeburn 9109: if (($uname ne '') && ($udom ne '')) {
9110: if (($cacheduser eq $uname.':'.$udom) &&
1.1172.2.126 raeburn 9111: ($cachedcid eq $env{'request.course.id'}) &&
1.1172.2.128 raeburn 9112: (abs($cachedlast-time)<5)) {
1.1172.2.66 raeburn 9113: return;
9114: }
9115: }
9116: $cachedlast=time;
9117: $cacheduser=$uname.':'.$udom;
1.1172.2.126 raeburn 9118: $cachedcid=$env{'request.course.id'};
1.1172.2.128 raeburn 9119: %cachedblockers = &get_commblock_resources();
1.1172.2.126 raeburn 9120: return;
1.1172.2.66 raeburn 9121: }
9122:
1.1162 raeburn 9123: sub get_comm_blocks {
9124: my ($cdom,$cnum) = @_;
9125: if ($cdom eq '' || $cnum eq '') {
9126: return unless ($env{'request.course.id'});
9127: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9128: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9129: }
9130: my %commblocks;
9131: my $hashid=$cdom.'_'.$cnum;
9132: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
9133: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
9134: %commblocks = %{$blocksref};
9135: } else {
9136: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
9137: my $cachetime = 600;
9138: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
9139: }
9140: return %commblocks;
9141: }
9142:
1.1172.2.66 raeburn 9143: sub get_commblock_resources {
9144: my ($blocks) = @_;
9145: my %blockers = ();
9146: return %blockers unless ($env{'request.course.id'});
1.1172.2.142 raeburn 9147: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9148: if ($env{'request.course.sec'}) {
9149: $courseurl .= '/'.$env{'request.course.sec'};
9150: }
9151: return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162 raeburn 9152: my %commblocks;
9153: if (ref($blocks) eq 'HASH') {
9154: %commblocks = %{$blocks};
9155: } else {
9156: %commblocks = &get_comm_blocks();
9157: }
1.1172.2.66 raeburn 9158: return %blockers unless (keys(%commblocks) > 0);
1.1163 raeburn 9159: my $navmap = Apache::lonnavmaps::navmap->new();
1.1172.2.66 raeburn 9160: return %blockers unless (ref($navmap));
9161: my $now = time;
1.1162 raeburn 9162: foreach my $block (keys(%commblocks)) {
9163: if ($block =~ /^(\d+)____(\d+)$/) {
9164: my ($start,$end) = ($1,$2);
9165: if ($start <= $now && $end >= $now) {
9166: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9167: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
9168: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1172.2.66 raeburn 9169: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9170: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 9171: }
9172: }
9173: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1172.2.66 raeburn 9174: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9175: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 9176: }
9177: }
9178: }
9179: }
9180: }
9181: } elsif ($block =~ /^firstaccess____(.+)$/) {
9182: my $item = $1;
9183: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9184: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1172.2.142 raeburn 9185: my (@interval,$mapname);
1.1172.2.66 raeburn 9186: my $type = 'map';
9187: if ($item eq 'course') {
9188: $type = 'course';
9189: @interval=&EXT("resource.0.interval");
9190: } else {
9191: if ($item =~ /___\d+___/) {
9192: $type = 'resource';
9193: @interval=&EXT("resource.0.interval",$item);
1.1162 raeburn 9194: } else {
1.1172.2.142 raeburn 9195: $mapname = &deversion($item);
9196: if (ref($navmap)) {
9197: my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
9198: @interval = ($timelimit,'map');
1.1162 raeburn 9199: }
9200: }
1.1172.2.66 raeburn 9201: }
1.1172.2.146. .1(raebu 9202:22): if ($interval[0] =~ /^(\d+)/) {
9203:22): my $timelimit = $1;
1.1172.2.66 raeburn 9204: my $first_access;
9205: if ($type eq 'resource') {
9206: $first_access=&get_first_access($interval[1],$item);
9207: } elsif ($type eq 'map') {
9208: $first_access=&get_first_access($interval[1],undef,$item);
9209: } else {
9210: $first_access=&get_first_access($interval[1]);
9211: }
9212: if ($first_access) {
1.1172.2.146. .1(raebu 9213:22): my $timesup = $first_access+$timelimit;
1.1172.2.66 raeburn 9214: if ($timesup > $now) {
9215: my $activeblock;
1.1172.2.142 raeburn 9216: if ($type eq 'resource') {
9217: if (ref($navmap)) {
9218: my $res = $navmap->getBySymb($item);
9219: if ($res->answerable()) {
9220: $activeblock = 1;
9221: }
9222: }
9223: } elsif ($type eq 'map') {
9224: my $mapsymb = &symbread($mapname,1);
9225: if (($mapsymb) && (ref($navmap))) {
9226: my $mapres = $navmap->getBySymb($mapsymb);
9227: if (ref($mapres)) {
9228: my $first = $mapres->map_start();
9229: my $finish = $mapres->map_finish();
9230: my $it = $navmap->getIterator($first,$finish,undef,0,0);
9231: if (ref($it)) {
9232: my $res;
9233: while ($res = $it->next(undef,1)) {
9234: next unless (ref($res));
9235: my $symb = $res->symb();
9236: next if (($symb eq $mapsymb) || ($symb eq ''));
9237: @interval=&EXT("resource.0.interval",$symb);
9238: if ($interval[1] eq 'map') {
9239: if ($res->answerable()) {
9240: $activeblock = 1;
9241: last;
9242: }
9243: }
9244: }
9245: }
9246: }
1.1172.2.66 raeburn 9247: }
9248: }
9249: if ($activeblock) {
9250: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
9251: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9252: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
9253: }
9254: }
9255: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
9256: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9257: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 9258: }
1.1162 raeburn 9259: }
9260: }
9261: }
9262: }
9263: }
9264: }
9265: }
9266: }
9267: }
1.1172.2.66 raeburn 9268: return %blockers;
1.1162 raeburn 9269: }
9270:
1.1172.2.66 raeburn 9271: sub has_comm_blocking {
1.1172.2.128 raeburn 9272: my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1172.2.66 raeburn 9273: my @blockers;
9274: return unless ($env{'request.course.id'});
9275: return unless ($priv eq 'bre');
9276: return if ($env{'request.state'} eq 'construct');
1.1172.2.142 raeburn 9277: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9278: if ($env{'request.course.sec'}) {
9279: $courseurl .= '/'.$env{'request.course.sec'};
9280: }
9281: return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1172.2.128 raeburn 9282: my %blockinfo;
9283: if (ref($blocks) eq 'HASH') {
9284: %blockinfo = &get_commblock_resources($blocks);
9285: } else {
9286: &load_all_blockers($env{'user.name'},$env{'user.domain'});
9287: %blockinfo = %cachedblockers;
9288: }
9289: return unless (keys(%blockinfo) > 0);
1.1172.2.66 raeburn 9290: my (%possibles,@symbs);
9291: if (!$symb) {
1.1172.2.128 raeburn 9292: $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162 raeburn 9293: }
1.1172.2.66 raeburn 9294: if ($symb) {
9295: @symbs = ($symb);
9296: } elsif (keys(%possibles)) {
9297: @symbs = keys(%possibles);
9298: }
9299: my $noblock;
9300: foreach my $symb (@symbs) {
9301: last if ($noblock);
9302: my ($map,$resid,$resurl)=&decode_symb($symb);
1.1172.2.128 raeburn 9303: foreach my $block (keys(%blockinfo)) {
1.1172.2.66 raeburn 9304: if ($block =~ /^firstaccess____(.+)$/) {
9305: my $item = $1;
1.1172.2.126 raeburn 9306: unless ($blocked) {
9307: if (($item eq $map) || ($item eq $symb)) {
9308: $noblock = 1;
9309: last;
9310: }
1.1172.2.66 raeburn 9311: }
1.1162 raeburn 9312: }
1.1172.2.128 raeburn 9313: if (ref($blockinfo{$block}) eq 'HASH') {
9314: if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
9315: if ($blockinfo{$block}{'resources'}{$symb}) {
1.1172.2.66 raeburn 9316: unless (grep(/^\Q$block\E$/,@blockers)) {
9317: push(@blockers,$block);
9318: }
9319: }
9320: }
1.1172.2.128 raeburn 9321: if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
9322: if ($blockinfo{$block}{'maps'}{$map}) {
1.1172.2.126 raeburn 9323: unless (grep(/^\Q$block\E$/,@blockers)) {
9324: push(@blockers,$block);
9325: }
1.1172.2.66 raeburn 9326: }
9327: }
1.1162 raeburn 9328: }
9329: }
9330: }
1.1172.2.126 raeburn 9331: unless ($noblock) {
9332: return @blockers;
9333: }
9334: return;
1.1172.2.66 raeburn 9335: }
1.1162 raeburn 9336: }
9337:
1.1172.2.146. .1(raebu 9338:22): sub deeplink_check {
9339:22): my ($priv,$symb,$uri) = @_;
9340:22): return unless ($env{'request.course.id'});
9341:22): return unless ($priv eq 'bre');
9342:22): return if ($env{'request.state'} eq 'construct');
9343:22): return if ($env{'request.role.adv'});
9344:22): my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9345:22): my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9346:22): my (%possibles,@symbs);
9347:22): if (!$symb) {
9348:22): $symb = &symbread($uri,1,1,1,\%possibles);
9349:22): }
9350:22): if ($symb) {
9351:22): @symbs = ($symb);
9352:22): } elsif (keys(%possibles)) {
9353:22): @symbs = keys(%possibles);
9354:22): }
9355:22):
9356:22): my ($deeplink_symb,$allow);
9357:22): if ($env{'request.deeplink.login'}) {
9358:22): $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
9359:22): }
9360:22): foreach my $symb (@symbs) {
9361:22): last if ($allow);
9362:22): my $deeplink = &EXT("resource.0.deeplink",$symb);
9363:22): if ($deeplink eq '') {
9364:22): $allow = 1;
9365:22): } else {
9366:22): my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
9367:22): if ($state ne 'only') {
9368:22): $allow = 1;
9369:22): } else {
9370:22): my $check_deeplink_entry;
9371:22): if ($protect ne 'none') {
9372:22): my ($acctype,$item) = split(/:/,$protect);
9373:22): if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
9374:22): if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
9375:22): $check_deeplink_entry = 1
9376:22): }
9377:22): } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
9378:22): if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
9379:22): $check_deeplink_entry = 1;
9380:22): }
9381:22): } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
9382:22): if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
9383:22): $check_deeplink_entry = 1;
9384:22): }
9385:22): }
9386:22): }
9387:22): if (($protect eq 'none') || ($check_deeplink_entry)) {
9388:22): if ($scope eq 'res') {
9389:22): if ($symb eq $deeplink_symb) {
9390:22): $allow = 1;
9391:22): }
9392:22): } elsif (($scope eq 'map') || ($scope eq 'rec')) {
9393:22): my ($map_from_symb,$map_from_login);
9394:22): $map_from_symb = &deversion((&decode_symb($symb))[0]);
9395:22): if ($deeplink_symb =~ /\.(page|sequence)$/) {
9396:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
9397:22): } else {
9398:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
9399:22): }
9400:22): if (($map_from_symb) && ($map_from_login)) {
9401:22): if ($map_from_symb eq $map_from_login) {
9402:22): $allow = 1;
9403:22): } elsif ($scope eq 'rec') {
9404:22): my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
9405:22): if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
9406:22): $allow = 1;
9407:22): }
9408:22): }
9409:22): }
9410:22): }
9411:22): }
9412:22): }
9413:22): }
9414:22): }
9415:22): return if ($allow);
9416:22): return 1;
9417:22): }
9418:22):
1.1172.2.66 raeburn 9419: # -------------------------------- Deversion and split uri into path an filename
9420:
1.1133 foxr 9421: #
1.1172.2.66 raeburn 9422: # Removes the version from a URI and
1.1133 foxr 9423: # splits it in to its filename and path to the filename.
9424: # Seems like File::Basename could have done this more clearly.
9425: # Parameters:
9426: # $uri - input URI
9427: # Returns:
9428: # Two element list consisting of
9429: # $pathname - the URI up to and excluding the trailing /
9430: # $filename - The part of the URI following the last /
9431: # NOTE:
9432: # Another realization of this is simply:
9433: # use File::Basename;
9434: # ...
9435: # $uri = shift;
9436: # $filename = basename($uri);
9437: # $path = dirname($uri);
9438: # return ($filename, $path);
9439: #
9440: # The implementation below is probably faster however.
9441: #
1.710 albertel 9442: sub split_uri_for_cond {
9443: my $uri=&deversion(&declutter(shift));
9444: my @uriparts=split(/\//,$uri);
9445: my $filename=pop(@uriparts);
9446: my $pathname=join('/',@uriparts);
9447: return ($pathname,$filename);
9448: }
1.232 www 9449: # --------------------------------------------------- Is a resource on the map?
9450:
9451: sub is_on_map {
1.710 albertel 9452: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 9453: #Trying to find the conditional for the file
1.620 albertel 9454: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 9455: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 9456: if ($match) {
1.289 bowersj2 9457: return (1,$1);
9458: } else {
1.434 www 9459: return (0,0);
1.289 bowersj2 9460: }
1.12 www 9461: }
9462:
1.427 www 9463: # --------------------------------------------------------- Get symb from alias
9464:
9465: sub get_symb_from_alias {
9466: my $symb=shift;
9467: my ($map,$resid,$url)=&decode_symb($symb);
9468: # Already is a symb
9469: if ($url) { return $symb; }
9470: # Must be an alias
9471: my $aliassymb='';
9472: my %bighash;
1.620 albertel 9473: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 9474: &GDBM_READER(),0640)) {
9475: my $rid=$bighash{'mapalias_'.$symb};
9476: if ($rid) {
9477: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 9478: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
9479: $resid,$bighash{'src_'.$rid});
1.427 www 9480: }
9481: untie %bighash;
9482: }
9483: return $aliassymb;
9484: }
9485:
1.12 www 9486: # ----------------------------------------------------------------- Define Role
9487:
9488: sub definerole {
9489: if (allowed('mcr','/')) {
1.1172.2.84 raeburn 9490: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 9491: foreach my $role (split(':',$sysrole)) {
9492: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9493: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
9494: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
9495: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9496: return "refused:s:$crole&$cqual";
9497: }
9498: }
1.191 harris41 9499: }
1.800 albertel 9500: foreach my $role (split(':',$domrole)) {
9501: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9502: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
9503: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
9504: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 9505: return "refused:d:$crole&$cqual";
9506: }
9507: }
1.191 harris41 9508: }
1.800 albertel 9509: foreach my $role (split(':',$courole)) {
9510: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9511: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
9512: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
9513: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9514: return "refused:c:$crole&$cqual";
9515: }
9516: }
1.191 harris41 9517: }
1.1172.2.84 raeburn 9518: my $uhome;
9519: if (($uname ne '') && ($udom ne '')) {
9520: $uhome = &homeserver($uname,$udom);
9521: return $uhome if ($uhome eq 'no_host');
9522: } else {
9523: $uname = $env{'user.name'};
9524: $udom = $env{'user.domain'};
9525: $uhome = $env{'user.home'};
9526: }
1.620 albertel 9527: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1172.2.84 raeburn 9528: "$udom:$uname:rolesdef_$rolename=".
1.21 www 9529: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1172.2.84 raeburn 9530: return reply($command,$uhome);
1.12 www 9531: } else {
9532: return 'refused';
9533: }
1.105 harris41 9534: }
9535:
9536: # ---------------- Make a metadata query against the network of library servers
9537:
9538: sub metadata_query {
1.1172.2.33 raeburn 9539: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 9540: my %rhash;
1.845 albertel 9541: my %libserv = &all_library();
1.244 matthew 9542: my @server_list = (defined($server_array) ? @$server_array
9543: : keys(%libserv) );
9544: for my $server (@server_list) {
1.1172.2.33 raeburn 9545: my $domains = '';
9546: if (ref($domains_hash) eq 'HASH') {
9547: $domains = $domains_hash->{$server};
9548: }
1.118 harris41 9549: unless ($custom or $customshow) {
1.1172.2.33 raeburn 9550: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 9551: $rhash{$server}=$reply;
9552: }
9553: else {
9554: my $reply=&reply("querysend:".&escape($query).':'.
1.1172.2.33 raeburn 9555: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 9556: $server);
9557: $rhash{$server}=$reply;
9558: }
1.112 harris41 9559: }
1.118 harris41 9560: return \%rhash;
1.240 www 9561: }
9562:
9563: # ----------------------------------------- Send log queries and wait for reply
9564:
9565: sub log_query {
9566: my ($uname,$udom,$query,%filters)=@_;
9567: my $uhome=&homeserver($uname,$udom);
9568: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 9569: my $uhost=&hostname($uhome);
1.800 albertel 9570: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 9571: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
9572: $uhome);
1.479 albertel 9573: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 9574: return get_query_reply($queryid);
9575: }
9576:
1.818 raeburn 9577: # -------------------------- Update MySQL table for portfolio file
9578:
9579: sub update_portfolio_table {
1.821 raeburn 9580: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 9581: if ($group ne '') {
9582: $file_name =~s /^\Q$group\E//;
9583: }
1.818 raeburn 9584: my $homeserver = &homeserver($uname,$udom);
9585: my $queryid=
1.821 raeburn 9586: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
9587: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 9588: my $reply = &get_query_reply($queryid);
9589: return $reply;
9590: }
9591:
1.899 raeburn 9592: # -------------------------- Update MySQL allusers table
9593:
9594: sub update_allusers_table {
9595: my ($uname,$udom,$names) = @_;
9596: my $homeserver = &homeserver($uname,$udom);
9597: my $queryid=
9598: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
9599: 'lastname='.&escape($names->{'lastname'}).'%%'.
9600: 'firstname='.&escape($names->{'firstname'}).'%%'.
9601: 'middlename='.&escape($names->{'middlename'}).'%%'.
9602: 'generation='.&escape($names->{'generation'}).'%%'.
9603: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
9604: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 9605: return;
1.899 raeburn 9606: }
9607:
1.508 raeburn 9608: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 9609:
9610: sub fetch_enrollment_query {
1.511 raeburn 9611: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1172.2.79 raeburn 9612: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 9613: my $maxtries = 1;
1.508 raeburn 9614: if ($context eq 'automated') {
9615: $homeserver = $perlvar{'lonHostID'};
1.1172.2.79 raeburn 9616: $sleep = 2;
9617: $loopmax = 100;
1.547 raeburn 9618: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 9619: } else {
9620: $homeserver = &homeserver($cnum,$dom);
9621: }
1.838 albertel 9622: my $host=&hostname($homeserver);
1.506 raeburn 9623: my $cmd = '';
1.1000 raeburn 9624: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 9625: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 9626: }
9627: $cmd =~ s/%%$//;
9628: $cmd = &escape($cmd);
9629: my $query = 'fetchenrollment';
1.620 albertel 9630: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 9631: unless ($queryid=~/^\Q$host\E\_/) {
9632: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
9633: return 'error: '.$queryid;
9634: }
1.1172.2.93 raeburn 9635: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9636: my $tries = 1;
9637: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1172.2.79 raeburn 9638: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9639: $tries ++;
9640: }
1.526 raeburn 9641: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 9642: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 9643: } else {
1.901 albertel 9644: my @responses = split(/:/,$reply);
1.1172.2.82 raeburn 9645: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 9646: foreach my $line (@responses) {
9647: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 9648: $$replyref{$key} = $value;
9649: }
9650: } else {
1.1117 foxr 9651: my $pathname = LONCAPA::tempdir();
1.800 albertel 9652: foreach my $line (@responses) {
9653: my ($key,$value) = split(/=/,$line);
1.506 raeburn 9654: $$replyref{$key} = $value;
9655: if ($value > 0) {
1.800 albertel 9656: foreach my $item (@{$$affiliatesref{$key}}) {
9657: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 9658: my $destname = $pathname.'/'.$filename;
9659: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 9660: if ($xml_classlist =~ /^error/) {
9661: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
9662: } else {
1.1172.2.96 raeburn 9663: if ( open(FILE,">",$destname) ) {
1.506 raeburn 9664: print FILE &unescape($xml_classlist);
9665: close(FILE);
1.526 raeburn 9666: } else {
9667: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 9668: }
9669: }
9670: }
9671: }
9672: }
9673: }
9674: return 'ok';
9675: }
9676: return 'error';
9677: }
9678:
1.242 www 9679: sub get_query_reply {
1.1172.2.79 raeburn 9680: my ($queryid,$sleep,$loopmax) = @_;
9681: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
9682: $sleep = 0.2;
9683: }
9684: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
9685: $loopmax = 100;
9686: }
1.1117 foxr 9687: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 9688: my $reply='';
1.1172.2.79 raeburn 9689: for (1..$loopmax) {
9690: sleep($sleep);
1.240 www 9691: if (-e $replyfile.'.end') {
1.1172.2.96 raeburn 9692: if (open(my $fh,"<",$replyfile)) {
1.904 albertel 9693: $reply = join('',<$fh>);
9694: close($fh);
1.240 www 9695: } else { return 'error: reply_file_error'; }
1.242 www 9696: return &unescape($reply);
9697: }
1.240 www 9698: }
1.242 www 9699: return 'timeout:'.$queryid;
1.240 www 9700: }
9701:
9702: sub courselog_query {
1.241 www 9703: #
9704: # possible filters:
9705: # url: url or symb
9706: # username
9707: # domain
9708: # action: view, submit, grade
9709: # start: timestamp
9710: # end: timestamp
9711: #
1.240 www 9712: my (%filters)=@_;
1.620 albertel 9713: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 9714: if ($filters{'url'}) {
9715: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
9716: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
9717: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
9718: }
1.620 albertel 9719: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
9720: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 9721: return &log_query($cname,$cdom,'courselog',%filters);
9722: }
9723:
9724: sub userlog_query {
1.858 raeburn 9725: #
9726: # possible filters:
9727: # action: log check role
9728: # start: timestamp
9729: # end: timestamp
9730: #
1.240 www 9731: my ($uname,$udom,%filters)=@_;
9732: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 9733: }
9734:
1.506 raeburn 9735: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
9736:
9737: sub auto_run {
1.508 raeburn 9738: my ($cnum,$cdom) = @_;
1.876 raeburn 9739: my $response = 0;
9740: my $settings;
9741: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
9742: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
9743: $settings = $domconfig{'autoenroll'};
9744: if ($settings->{'run'} eq '1') {
9745: $response = 1;
9746: }
9747: } else {
1.934 raeburn 9748: my $homeserver;
9749: if (&is_course($cdom,$cnum)) {
9750: $homeserver = &homeserver($cnum,$cdom);
9751: } else {
9752: $homeserver = &domain($cdom,'primary');
9753: }
9754: if ($homeserver ne 'no_host') {
9755: $response = &reply('autorun:'.$cdom,$homeserver);
9756: }
1.876 raeburn 9757: }
1.506 raeburn 9758: return $response;
9759: }
1.776 albertel 9760:
1.506 raeburn 9761: sub auto_get_sections {
1.508 raeburn 9762: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 9763: my $homeserver;
9764: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9765: $homeserver = &homeserver($cnum,$cdom);
9766: }
9767: if (!defined($homeserver)) {
9768: if ($cdom =~ /^$match_domain$/) {
9769: $homeserver = &domain($cdom,'primary');
9770: }
9771: }
9772: my @secs;
9773: if (defined($homeserver)) {
9774: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
9775: unless ($response eq 'refused') {
9776: @secs = split(/:/,$response);
9777: }
1.506 raeburn 9778: }
9779: return @secs;
9780: }
1.776 albertel 9781:
1.506 raeburn 9782: sub auto_new_course {
1.1099 raeburn 9783: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 9784: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 9785: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 9786: return $response;
9787: }
1.776 albertel 9788:
1.506 raeburn 9789: sub auto_validate_courseID {
1.508 raeburn 9790: my ($cnum,$cdom,$inst_course_id) = @_;
9791: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 9792: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 9793: return $response;
9794: }
1.776 albertel 9795:
1.1007 raeburn 9796: sub auto_validate_instcode {
1.1020 raeburn 9797: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 9798: my ($homeserver,$response);
9799: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9800: $homeserver = &homeserver($cnum,$cdom);
9801: }
9802: if (!defined($homeserver)) {
9803: if ($cdom =~ /^$match_domain$/) {
9804: $homeserver = &domain($cdom,'primary');
9805: }
9806: }
1.1065 raeburn 9807: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
9808: &escape($instcode).':'.&escape($owner),$homeserver));
1.1172.2.19 raeburn 9809: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
9810: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 9811: }
9812:
1.1172.2.141 raeburn 9813: sub auto_validate_inst_crosslist {
9814: my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
9815: my ($homeserver,$response);
9816: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9817: $homeserver = &homeserver($cnum,$cdom);
9818: }
9819: if (!defined($homeserver)) {
9820: if ($cdom =~ /^$match_domain$/) {
9821: $homeserver = &domain($cdom,'primary');
9822: }
9823: }
9824: unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
9825: $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
9826: &escape($instcode).':'.&escape($inst_xlist).':'.
9827: &escape($coowner),$homeserver);
9828: }
9829: return $response;
9830: }
9831:
1.506 raeburn 9832: sub auto_create_password {
1.873 raeburn 9833: my ($cnum,$cdom,$authparam,$udom) = @_;
9834: my ($homeserver,$response);
1.506 raeburn 9835: my $create_passwd = 0;
9836: my $authchk = '';
1.873 raeburn 9837: if ($udom =~ /^$match_domain$/) {
9838: $homeserver = &domain($udom,'primary');
9839: }
9840: if ($homeserver eq '') {
9841: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9842: $homeserver = &homeserver($cnum,$cdom);
9843: }
9844: }
9845: if ($homeserver eq '') {
9846: $authchk = 'nodomain';
1.506 raeburn 9847: } else {
1.873 raeburn 9848: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
9849: if ($response eq 'refused') {
9850: $authchk = 'refused';
9851: } else {
1.901 albertel 9852: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 9853: }
1.506 raeburn 9854: }
9855: return ($authparam,$create_passwd,$authchk);
9856: }
9857:
1.706 raeburn 9858: sub auto_photo_permission {
9859: my ($cnum,$cdom,$students) = @_;
9860: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 9861: my ($outcome,$perm_reqd,$conditions) =
9862: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 9863: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9864: return (undef,undef);
9865: }
1.706 raeburn 9866: return ($outcome,$perm_reqd,$conditions);
9867: }
9868:
9869: sub auto_checkphotos {
9870: my ($uname,$udom,$pid) = @_;
9871: my $homeserver = &homeserver($uname,$udom);
9872: my ($result,$resulttype);
9873: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 9874: &escape($uname).':'.&escape($pid),
9875: $homeserver));
1.709 albertel 9876: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9877: return (undef,undef);
9878: }
1.706 raeburn 9879: if ($outcome) {
9880: ($result,$resulttype) = split(/:/,$outcome);
9881: }
9882: return ($result,$resulttype);
9883: }
9884:
9885: sub auto_photochoice {
9886: my ($cnum,$cdom) = @_;
9887: my $homeserver = &homeserver($cnum,$cdom);
9888: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 9889: &escape($cdom),
9890: $homeserver)));
1.709 albertel 9891: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9892: return (undef,undef);
9893: }
1.706 raeburn 9894: return ($update,$comment);
9895: }
9896:
9897: sub auto_photoupdate {
9898: my ($affiliatesref,$dom,$cnum,$photo) = @_;
9899: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 9900: my $host=&hostname($homeserver);
1.706 raeburn 9901: my $cmd = '';
9902: my $maxtries = 1;
1.800 albertel 9903: foreach my $affiliate (keys(%{$affiliatesref})) {
9904: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 9905: }
9906: $cmd =~ s/%%$//;
9907: $cmd = &escape($cmd);
9908: my $query = 'institutionalphotos';
9909: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
9910: unless ($queryid=~/^\Q$host\E\_/) {
9911: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
9912: return 'error: '.$queryid;
9913: }
9914: my $reply = &get_query_reply($queryid);
9915: my $tries = 1;
9916: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
9917: $reply = &get_query_reply($queryid);
9918: $tries ++;
9919: }
9920: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
9921: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
9922: } else {
9923: my @responses = split(/:/,$reply);
9924: my $outcome = shift(@responses);
9925: foreach my $item (@responses) {
9926: my ($key,$value) = split(/=/,$item);
9927: $$photo{$key} = $value;
9928: }
9929: return $outcome;
9930: }
9931: return 'error';
9932: }
9933:
1.521 raeburn 9934: sub auto_instcode_format {
1.793 albertel 9935: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
9936: $cat_order) = @_;
1.521 raeburn 9937: my $courses = '';
1.772 raeburn 9938: my @homeservers;
1.521 raeburn 9939: if ($caller eq 'global') {
1.841 albertel 9940: my %servers = &get_servers($codedom,'library');
9941: foreach my $tryserver (keys(%servers)) {
9942: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9943: push(@homeservers,$tryserver);
9944: }
1.584 raeburn 9945: }
1.1022 raeburn 9946: } elsif ($caller eq 'requests') {
9947: if ($codedom =~ /^$match_domain$/) {
9948: my $chome = &domain($codedom,'primary');
9949: unless ($chome eq 'no_host') {
9950: push(@homeservers,$chome);
9951: }
9952: }
1.521 raeburn 9953: } else {
1.772 raeburn 9954: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 9955: }
1.793 albertel 9956: foreach my $code (keys(%{$instcodes})) {
9957: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 9958: }
9959: chop($courses);
1.772 raeburn 9960: my $ok_response = 0;
9961: my $response;
9962: while (@homeservers > 0 && $ok_response == 0) {
9963: my $server = shift(@homeservers);
9964: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
9965: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
9966: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 9967: split(/:/,$response);
1.772 raeburn 9968: %{$codes} = (%{$codes},&str2hash($codes_str));
9969: push(@{$codetitles},&str2array($codetitles_str));
9970: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
9971: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
9972: $ok_response = 1;
9973: }
9974: }
9975: if ($ok_response) {
1.521 raeburn 9976: return 'ok';
1.772 raeburn 9977: } else {
9978: return $response;
1.521 raeburn 9979: }
9980: }
9981:
1.792 raeburn 9982: sub auto_instcode_defaults {
9983: my ($domain,$returnhash,$code_order) = @_;
9984: my @homeservers;
1.841 albertel 9985:
9986: my %servers = &get_servers($domain,'library');
9987: foreach my $tryserver (keys(%servers)) {
9988: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9989: push(@homeservers,$tryserver);
9990: }
1.792 raeburn 9991: }
1.841 albertel 9992:
1.792 raeburn 9993: my $response;
1.841 albertel 9994: foreach my $server (@homeservers) {
1.792 raeburn 9995: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 9996: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
9997:
9998: foreach my $pair (split(/\&/,$response)) {
9999: my ($name,$value)=split(/\=/,$pair);
10000: if ($name eq 'code_order') {
10001: @{$code_order} = split(/\&/,&unescape($value));
10002: } else {
10003: $returnhash->{&unescape($name)}=&unescape($value);
10004: }
10005: }
10006: return 'ok';
1.792 raeburn 10007: }
1.841 albertel 10008:
10009: return $response;
1.1003 raeburn 10010: }
10011:
10012: sub auto_possible_instcodes {
1.1007 raeburn 10013: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10014: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
10015: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10016: return;
10017: }
1.1003 raeburn 10018: my (@homeservers,$uhome);
10019: if (defined(&domain($domain,'primary'))) {
10020: $uhome=&domain($domain,'primary');
10021: push(@homeservers,&domain($domain,'primary'));
10022: } else {
10023: my %servers = &get_servers($domain,'library');
10024: foreach my $tryserver (keys(%servers)) {
10025: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10026: push(@homeservers,$tryserver);
10027: }
10028: }
10029: }
10030: my $response;
10031: foreach my $server (@homeservers) {
10032: $response=&reply('autopossibleinstcodes:'.$domain,$server);
10033: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 10034: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
10035: split(':',$response);
10036: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10037: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 10038: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 10039: my ($name,$value)=split('=',$item);
10040: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10041: }
10042: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 10043: my ($name,$value)=split('=',$item);
10044: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10045: }
10046: return 'ok';
10047: }
10048: return $response;
10049: }
1.792 raeburn 10050:
1.1010 raeburn 10051: sub auto_courserequest_checks {
10052: my ($dom) = @_;
1.1020 raeburn 10053: my ($homeserver,%validations);
10054: if ($dom =~ /^$match_domain$/) {
10055: $homeserver = &domain($dom,'primary');
10056: }
10057: unless ($homeserver eq 'no_host') {
10058: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10059: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10060: my @items = split(/&/,$response);
10061: foreach my $item (@items) {
10062: my ($key,$value) = split('=',$item);
10063: $validations{&unescape($key)} = &thaw_unescape($value);
10064: }
10065: }
10066: }
1.1010 raeburn 10067: return %validations;
10068: }
10069:
1.1020 raeburn 10070: sub auto_courserequest_validation {
1.1172.2.43 raeburn 10071: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 10072: my ($homeserver,$response);
10073: if ($dom =~ /^$match_domain$/) {
10074: $homeserver = &domain($dom,'primary');
10075: }
1.1172.2.43 raeburn 10076: unless ($homeserver eq 'no_host') {
10077: my $customdata;
10078: if (ref($custominfo) eq 'HASH') {
10079: $customdata = &freeze_escape($custominfo);
10080: }
1.1020 raeburn 10081: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 10082: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1172.2.43 raeburn 10083: ':'.&escape($instcode).':'.&escape($instseclist).':'.
10084: $customdata,$homeserver));
1.1020 raeburn 10085: }
10086: return $response;
10087: }
10088:
1.777 albertel 10089: sub auto_validate_class_sec {
1.918 raeburn 10090: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 10091: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 10092: my $ownerlist;
10093: if (ref($owners) eq 'ARRAY') {
10094: $ownerlist = join(',',@{$owners});
10095: } else {
10096: $ownerlist = $owners;
10097: }
1.773 raeburn 10098: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 10099: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 10100: return $response;
10101: }
10102:
1.1172.2.141 raeburn 10103: sub auto_instsec_reformat {
10104: my ($cdom,$action,$instsecref) = @_;
10105: return unless(($action eq 'clutter') || ($action eq 'declutter'));
10106: my @homeservers;
10107: if (defined(&domain($cdom,'primary'))) {
10108: push(@homeservers,&domain($cdom,'primary'));
10109: } else {
10110: my %servers = &get_servers($cdom,'library');
10111: foreach my $tryserver (keys(%servers)) {
10112: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10113: push(@homeservers,$tryserver);
10114: }
10115: }
10116: }
10117: my $response;
10118: my %reformatted = %{$instsecref};
10119: foreach my $server (@homeservers) {
10120: if (ref($instsecref) eq 'HASH') {
10121: my $info = &freeze_escape($instsecref);
10122: my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10123: $action.':'.$info,$server);
1.1172.2.146. .26(raeb 10124:-25): next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/);
1.1172.2.141 raeburn 10125: my @items = split(/&/,$response);
10126: foreach my $item (@items) {
10127: my ($key,$value) = split(/=/,$item);
10128: $reformatted{&unescape($key)} = &thaw_unescape($value);
10129: }
10130: }
10131: }
10132: return %reformatted;
10133: }
10134:
1.1172.2.94 raeburn 10135: sub auto_validate_instclasses {
10136: my ($cdom,$cnum,$owners,$classesref) = @_;
10137: my ($homeserver,%validations);
10138: $homeserver = &homeserver($cnum,$cdom);
10139: unless ($homeserver eq 'no_host') {
10140: my $ownerlist;
10141: if (ref($owners) eq 'ARRAY') {
10142: $ownerlist = join(',',@{$owners});
10143: } else {
10144: $ownerlist = $owners;
10145: }
10146: if (ref($classesref) eq 'HASH') {
10147: my $classes = &freeze_escape($classesref);
10148: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10149: ':'.$cdom.':'.$classes,$homeserver);
10150: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10151: my @items = split(/&/,$response);
10152: foreach my $item (@items) {
10153: my ($key,$value) = split('=',$item);
10154: $validations{&unescape($key)} = &thaw_unescape($value);
10155: }
10156: }
10157: }
10158: }
10159: return %validations;
10160: }
10161:
1.1172.2.38 raeburn 10162: sub auto_crsreq_update {
10163: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1172.2.45 raeburn 10164: $code,$accessstart,$accessend,$inbound) = @_;
1.1172.2.38 raeburn 10165: my ($homeserver,%crsreqresponse);
10166: if ($cdom =~ /^$match_domain$/) {
10167: $homeserver = &domain($cdom,'primary');
10168: }
10169: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10170: my $info;
10171: if (ref($inbound) eq 'HASH') {
10172: $info = &freeze_escape($inbound);
10173: }
10174: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10175: ':'.&escape($action).':'.&escape($ownername).':'.
10176: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1172.2.45 raeburn 10177: &escape($title).':'.&escape($code).':'.
10178: &escape($accessstart).':'.&escape($accessend).':'.$info,$homeserver);
1.1172.2.38 raeburn 10179: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10180: my @items = split(/&/,$response);
10181: foreach my $item (@items) {
10182: my ($key,$value) = split('=',$item);
10183: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10184: }
10185: }
10186: }
10187: return \%crsreqresponse;
10188: }
10189:
1.1172.2.78 raeburn 10190: sub auto_export_grades {
10191: my ($cdom,$cnum,$inforef,$gradesref) = @_;
10192: my ($homeserver,%exportresponse);
10193: if ($cdom =~ /^$match_domain$/) {
10194: $homeserver = &domain($cdom,'primary');
10195: }
10196: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10197: my $info;
10198: if (ref($inforef) eq 'HASH') {
10199: $info = &freeze_escape($inforef);
10200: }
10201: if (ref($gradesref) eq 'HASH') {
10202: my $grades = &freeze_escape($gradesref);
10203: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10204: $info.':'.$grades,$homeserver);
1.1172.2.146. .26(raeb 10205:-25): unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/) {
1.1172.2.78 raeburn 10206: my @items = split(/&/,$response);
10207: foreach my $item (@items) {
10208: my ($key,$value) = split('=',$item);
10209: $exportresponse{&unescape($key)} = &thaw_unescape($value);
10210: }
10211: }
10212: }
10213: }
10214: return \%exportresponse;
10215: }
10216:
1.1172.2.68 raeburn 10217: sub check_instcode_cloning {
10218: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10219: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10220: return;
10221: }
10222: my $canclone;
10223: if (@{$code_order} > 0) {
10224: my $instcoderegexp ='^';
10225: my @clonecodes = split(/\&/,$cloner);
10226: foreach my $item (@{$code_order}) {
10227: if (grep(/^\Q$item\E=/,@clonecodes)) {
10228: foreach my $pair (@clonecodes) {
10229: my ($key,$val) = split(/\=/,$pair,2);
10230: $val = &unescape($val);
10231: if ($key eq $item) {
10232: $instcoderegexp .= '('.$val.')';
10233: last;
10234: }
10235: }
10236: } else {
10237: $instcoderegexp .= $codedefaults->{$item};
10238: }
10239: }
10240: $instcoderegexp .= '$';
10241: my (@from,@to);
10242: eval {
10243: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10244: (@to) = ($clonetocode =~ /$instcoderegexp/);
10245: };
10246: if ((@from > 0) && (@to > 0)) {
10247: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10248: if (!@diffs) {
10249: $canclone = 1;
10250: }
10251: }
10252: }
10253: return $canclone;
10254: }
10255:
10256: sub default_instcode_cloning {
10257: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10258: my (%codedefaults,@code_order,$canclone);
10259: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10260: %codedefaults = %{$codedefaultsref};
10261: @code_order = @{$codeorderref};
10262: } elsif ($clonedom) {
10263: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10264: }
10265: if (($domdefclone) && (@code_order)) {
10266: my @clonecodes = split(/\+/,$domdefclone);
10267: my $instcoderegexp ='^';
10268: foreach my $item (@code_order) {
10269: if (grep(/^\Q$item\E$/,@clonecodes)) {
10270: $instcoderegexp .= '('.$codedefaults{$item}.')';
10271: } else {
10272: $instcoderegexp .= $codedefaults{$item};
10273: }
10274: }
10275: $instcoderegexp .= '$';
10276: my (@from,@to);
10277: eval {
10278: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10279: (@to) = ($clonetocode =~ /$instcoderegexp/);
10280: };
10281: if ((@from > 0) && (@to > 0)) {
10282: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10283: if (!@diffs) {
10284: $canclone = 1;
10285: }
10286: }
10287: }
10288: return $canclone;
10289: }
10290:
1.679 raeburn 10291: # ------------------------------------------------------- Course Group routines
10292:
10293: sub get_coursegroups {
1.809 raeburn 10294: my ($cdom,$cnum,$group,$namespace) = @_;
10295: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 10296: }
10297:
1.679 raeburn 10298: sub modify_coursegroup {
10299: my ($cdom,$cnum,$groupsettings) = @_;
10300: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10301: }
10302:
1.809 raeburn 10303: sub toggle_coursegroup_status {
10304: my ($cdom,$cnum,$group,$action) = @_;
10305: my ($from_namespace,$to_namespace);
10306: if ($action eq 'delete') {
10307: $from_namespace = 'coursegroups';
10308: $to_namespace = 'deleted_groups';
10309: } else {
10310: $from_namespace = 'deleted_groups';
10311: $to_namespace = 'coursegroups';
10312: }
10313: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 10314: if (my $tmp = &error(%curr_group)) {
10315: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10316: return ('read error',$tmp);
10317: } else {
10318: my %savedsettings = %curr_group;
1.809 raeburn 10319: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 10320: my $deloutcome;
10321: if ($result eq 'ok') {
1.809 raeburn 10322: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 10323: } else {
10324: return ('write error',$result);
10325: }
10326: if ($deloutcome eq 'ok') {
10327: return 'ok';
10328: } else {
10329: return ('delete error',$deloutcome);
10330: }
10331: }
10332: }
10333:
1.679 raeburn 10334: sub modify_group_roles {
1.957 raeburn 10335: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 10336: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10337: my $role = 'gr/'.&escape($userprivs);
10338: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 10339: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 10340: if ($result eq 'ok') {
10341: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10342: }
1.679 raeburn 10343: return $result;
10344: }
10345:
10346: sub modify_coursegroup_membership {
10347: my ($cdom,$cnum,$membership) = @_;
10348: my $result = &put('groupmembership',$membership,$cdom,$cnum);
10349: return $result;
10350: }
10351:
1.682 raeburn 10352: sub get_active_groups {
10353: my ($udom,$uname,$cdom,$cnum) = @_;
10354: my $now = time;
10355: my %groups = ();
10356: foreach my $key (keys(%env)) {
1.811 albertel 10357: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 10358: my ($start,$end) = split(/\./,$env{$key});
10359: if (($end!=0) && ($end<$now)) { next; }
10360: if (($start!=0) && ($start>$now)) { next; }
10361: if ($1 eq $cdom && $2 eq $cnum) {
10362: $groups{$3} = $env{$key} ;
10363: }
10364: }
10365: }
10366: return %groups;
10367: }
10368:
1.683 raeburn 10369: sub get_group_membership {
10370: my ($cdom,$cnum,$group) = @_;
10371: return(&dump('groupmembership',$cdom,$cnum,$group));
10372: }
10373:
10374: sub get_users_groups {
10375: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 10376: my @usersgroups;
1.683 raeburn 10377: my $cachetime=1800;
10378:
10379: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 10380: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10381: if (defined($cached)) {
1.734 albertel 10382: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 10383: } else {
10384: $grouplist = '';
1.816 raeburn 10385: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 10386: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 10387: my $access_end = $env{'course.'.$courseid.
10388: '.default_enrollment_end_date'};
10389: my $now = time;
10390: foreach my $key (keys(%roleshash)) {
10391: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10392: my $group = $1;
10393: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10394: my $start = $2;
10395: my $end = $1;
10396: if ($start == -1) { next; } # deleted from group
10397: if (($start!=0) && ($start>$now)) { next; }
10398: if (($end!=0) && ($end<$now)) {
10399: if ($access_end && $access_end < $now) {
10400: if ($access_end - $end < 86400) {
10401: push(@usersgroups,$group);
1.733 raeburn 10402: }
10403: }
1.817 raeburn 10404: next;
1.733 raeburn 10405: }
1.817 raeburn 10406: push(@usersgroups,$group);
1.683 raeburn 10407: }
10408: }
10409: }
1.817 raeburn 10410: @usersgroups = &sort_course_groups($courseid,@usersgroups);
10411: $grouplist = join(':',@usersgroups);
10412: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 10413: }
1.733 raeburn 10414: return @usersgroups;
1.683 raeburn 10415: }
10416:
10417: sub devalidate_getgroups_cache {
10418: my ($udom,$uname,$cdom,$cnum)=@_;
10419: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 10420:
1.683 raeburn 10421: my $hashid="$udom:$uname:$courseid";
10422: &devalidate_cache_new('getgroups',$hashid);
10423: }
10424:
1.12 www 10425: # ------------------------------------------------------------------ Plain Text
10426:
10427: sub plaintext {
1.988 raeburn 10428: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 10429: if ($short =~ m{^cr/}) {
1.758 albertel 10430: return (split('/',$short))[-1];
10431: }
1.742 raeburn 10432: if (!defined($cid)) {
10433: $cid = $env{'request.course.id'};
10434: }
10435: my %rolenames = (
1.1008 raeburn 10436: Course => 'std',
10437: Community => 'alt1',
1.742 raeburn 10438: );
1.1037 raeburn 10439: if ($cid ne '') {
10440: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10441: unless ($forcedefault) {
10442: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
10443: &Apache::lonlocal::mt_escape(\$roletext);
10444: return &Apache::lonlocal::mt($roletext);
10445: }
10446: }
10447: }
10448: if ((defined($type)) && (defined($rolenames{$type})) &&
10449: (defined($rolenames{$type})) &&
10450: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 10451: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 10452: } elsif ($cid ne '') {
10453: my $crstype = $env{'course.'.$cid.'.type'};
10454: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10455: (defined($prp{$short}{$rolenames{$crstype}}))) {
10456: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10457: }
1.742 raeburn 10458: }
1.1037 raeburn 10459: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 10460: }
10461:
10462: # ----------------------------------------------------------------- Assign Role
10463:
10464: sub assignrole {
1.957 raeburn 10465: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10466: $context)=@_;
1.1172.2.146. .22(raeb 10467:-24): my ($mrole,$rolelogcontext);
1.21 www 10468: if ($role =~ /^cr\//) {
1.393 www 10469: my $cwosec=$url;
1.811 albertel 10470: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 10471: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 10472: my $refused = 1;
10473: if ($context eq 'requestcourses') {
10474: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10475: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10476: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10477: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10478: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10479: if ($crsenv{'internal.courseowner'} eq
10480: $env{'user.name'}.':'.$env{'user.domain'}) {
10481: $refused = '';
10482: }
10483: }
10484: }
10485: }
10486: }
10487: if ($refused) {
10488: &logthis('Refused custom assignrole: '.
10489: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10490: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10491: return 'refused';
10492: }
1.104 www 10493: }
1.21 www 10494: $mrole='cr';
1.678 raeburn 10495: } elsif ($role =~ /^gr\//) {
10496: my $cwogrp=$url;
1.811 albertel 10497: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 10498: unless (&allowed('mdg',$cwogrp)) {
10499: &logthis('Refused group assignrole: '.
10500: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10501: $env{'user.name'}.' at '.$env{'user.domain'});
10502: return 'refused';
10503: }
10504: $mrole='gr';
1.21 www 10505: } else {
1.82 www 10506: my $cwosec=$url;
1.811 albertel 10507: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 10508: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10509: my $refused;
10510: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
10511: if (!(&allowed('c'.$role,$url))) {
10512: $refused = 1;
10513: }
10514: } else {
10515: $refused = 1;
10516: }
1.947 raeburn 10517: if ($refused) {
1.1045 raeburn 10518: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10519: if (!$selfenroll && $context eq 'course') {
10520: my %crsenv;
10521: if ($role eq 'cc' || $role eq 'co') {
10522: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10523: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10524: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10525: if ($crsenv{'internal.courseowner'} eq
10526: $env{'user.name'}.':'.$env{'user.domain'}) {
10527: $refused = '';
10528: }
10529: }
10530: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
10531: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10532: if ($crsenv{'internal.courseowner'} eq
10533: $env{'user.name'}.':'.$env{'user.domain'}) {
10534: $refused = '';
10535: }
10536: }
10537: }
10538: }
1.1172.2.146. .13(raeb 10539:-23): } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10540:-23): if ($role eq 'st') {
10541:-23): $refused = '';
10542:-23): } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10543:-23): $refused = '';
10544:-23): }
1.1017 raeburn 10545: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 10546: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 10547: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 10548: my $wrongcc;
10549: if ($cnum =~ /^$match_community$/) {
10550: $wrongcc = 1 if ($role eq 'cc');
10551: } else {
10552: $wrongcc = 1 if ($role eq 'co');
10553: }
10554: unless ($wrongcc) {
10555: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10556: if ($crsenv{'internal.courseowner'} eq
10557: $env{'user.name'}.':'.$env{'user.domain'}) {
10558: $refused = '';
10559: }
1.1017 raeburn 10560: }
10561: }
1.1172.2.9 raeburn 10562: } elsif ($context eq 'requestauthor') {
10563: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
10564: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10565: if ($env{'environment.requestauthor'} eq 'automatic') {
10566: $refused = '';
10567: } else {
10568: my %domdefaults = &get_domain_defaults($udom);
10569: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10570: my $checkbystatus;
10571: if ($env{'user.adv'}) {
10572: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10573: if ($disposition eq 'automatic') {
10574: $refused = '';
10575: } elsif ($disposition eq '') {
10576: $checkbystatus = 1;
10577: }
10578: } else {
10579: $checkbystatus = 1;
10580: }
10581: if ($checkbystatus) {
10582: if ($env{'environment.inststatus'}) {
10583: my @inststatuses = split(/,/,$env{'environment.inststatus'});
10584: foreach my $type (@inststatuses) {
10585: if (($type ne '') &&
10586: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10587: $refused = '';
10588: }
10589: }
10590: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10591: $refused = '';
10592: }
10593: }
10594: }
10595: }
10596: }
1.1172.2.146. .22(raeb 10597:-24): } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10598:-24): if ($url =~ m{^/($match_domain)/($match_username)$}) {
10599:-24): my ($audom,$auname) = ($1,$2);
10600:-24): if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10601:-24): ($env{"environment.internal.manager.$url"})) {
10602:-24): $refused = '';
10603:-24): $rolelogcontext = 'coauthor';
10604:-24): }
10605:-24): }
1.1017 raeburn 10606: }
10607: if ($refused) {
1.947 raeburn 10608: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10609: ' '.$role.' '.$end.' '.$start.' by '.
10610: $env{'user.name'}.' at '.$env{'user.domain'});
10611: return 'refused';
10612: }
1.932 raeburn 10613: }
1.1131 raeburn 10614: } elsif ($role eq 'au') {
10615: if ($url ne '/'.$udom.'/') {
10616: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10617: ' to assign author role for '.$uname.':'.$udom.
10618: ' in domain: '.$url.' refused (wrong domain).');
10619: return 'refused';
10620: }
1.104 www 10621: }
1.21 www 10622: $mrole=$role;
10623: }
1.620 albertel 10624: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 10625: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 10626: if ($end) { $command.='_'.$end; }
1.21 www 10627: if ($start) {
10628: if ($end) {
1.81 www 10629: $command.='_'.$start;
1.21 www 10630: } else {
1.81 www 10631: $command.='_0_'.$start;
1.21 www 10632: }
10633: }
1.739 raeburn 10634: my $origstart = $start;
10635: my $origend = $end;
1.957 raeburn 10636: my $delflag;
1.357 www 10637: # actually delete
10638: if ($deleteflag) {
1.373 www 10639: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 10640: # modify command to delete the role
1.620 albertel 10641: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 10642: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 10643: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 10644: # set start and finish to negative values for userrolelog
10645: $start=-1;
10646: $end=-1;
1.957 raeburn 10647: $delflag = 1;
1.357 www 10648: }
10649: }
10650: # send command
1.349 www 10651: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 10652: # log new user role if status is ok
1.349 www 10653: if ($answer eq 'ok') {
1.663 raeburn 10654: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1172.2.9 raeburn 10655: if (($role eq 'cc') || ($role eq 'in') ||
10656: ($role eq 'ep') || ($role eq 'ad') ||
10657: ($role eq 'ta') || ($role eq 'st') ||
10658: ($role=~/^cr/) || ($role eq 'gr') ||
10659: ($role eq 'co')) {
1.1172.2.13 raeburn 10660: # for course roles, perform group memberships changes triggered by role change.
10661: unless ($role =~ /^gr/) {
10662: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10663: $origstart,$selfenroll,$context);
10664: }
1.1172.2.9 raeburn 10665: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10666: $selfenroll,$context);
10667: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1172.2.90 raeburn 10668: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10669: ($role eq 'da')) {
1.1172.2.9 raeburn 10670: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10671: $context);
10672: } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1172.2.146. .22(raeb 10673:-24): if ($rolelogcontext eq '') {
10674:-24): $rolelogcontext = $context;
10675:-24): }
1.1172.2.9 raeburn 10676: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1172.2.146. .22(raeb 10677:-24): $rolelogcontext);
1.1172.2.9 raeburn 10678: }
1.1053 raeburn 10679: if ($role eq 'cc') {
10680: &autoupdate_coowners($url,$end,$start,$uname,$udom);
10681: }
1.349 www 10682: }
10683: return $answer;
1.169 harris41 10684: }
10685:
1.1053 raeburn 10686: sub autoupdate_coowners {
10687: my ($url,$end,$start,$uname,$udom) = @_;
10688: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10689: if (($cdom ne '') && ($cnum ne '')) {
10690: my $now = time;
10691: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10692: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10693: my %coursehash = &coursedescription($cdom.'_'.$cnum);
10694: my $instcode = $coursehash{'internal.coursecode'};
1.1172.2.141 raeburn 10695: my $xlists = $coursehash{'internal.crosslistings'};
1.1053 raeburn 10696: if ($instcode ne '') {
1.1056 raeburn 10697: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10698: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 10699: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 10700: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1172.2.141 raeburn 10701: unless ($result eq 'valid') {
10702: if ($xlists ne '') {
10703: foreach my $xlist (split(',',$xlists)) {
10704: my ($inst_crosslist,$lcsec) = split(':',$xlist);
10705: $result =
10706: &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10707: $inst_crosslist,$uname.':'.$udom);
10708: last if ($result eq 'valid');
10709: }
10710: }
10711: }
1.1056 raeburn 10712: if ($result eq 'valid') {
10713: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 10714: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10715: push(@newcoowners,$coowner);
10716: }
10717: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10718: push(@newcoowners,$uname.':'.$udom);
10719: }
10720: @newcoowners = sort(@newcoowners);
10721: } else {
10722: push(@newcoowners,$uname.':'.$udom);
10723: }
1.1172.2.141 raeburn 10724: } elsif ($coursehash{'internal.co-owners'}) {
10725: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10726: unless ($coowner eq $uname.':'.$udom) {
10727: push(@newcoowners,$coowner);
1.1053 raeburn 10728: }
10729: }
1.1172.2.141 raeburn 10730: unless (@newcoowners > 0) {
10731: $delcoowners = 1;
10732: $coowners = '';
10733: }
1.1053 raeburn 10734: }
10735: if (@newcoowners || $delcoowners) {
10736: &store_coowners($cdom,$cnum,$coursehash{'home'},
10737: $delcoowners,@newcoowners);
10738: }
10739: }
10740: }
10741: }
10742: }
10743: }
10744: }
10745:
10746: sub store_coowners {
10747: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10748: my $cid = $cdom.'_'.$cnum;
10749: my ($coowners,$delresult,$putresult);
10750: if (@newcoowners) {
10751: $coowners = join(',',@newcoowners);
10752: my %coownershash = (
10753: 'internal.co-owners' => $coowners,
10754: );
10755: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10756: if ($putresult eq 'ok') {
10757: if ($env{'course.'.$cid.'.num'} eq $cnum) {
10758: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10759: }
10760: }
10761: }
10762: if ($delcoowners) {
10763: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10764: if ($delresult eq 'ok') {
10765: if ($env{'course.'.$cid.'.internal.co-owners'}) {
10766: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10767: }
10768: }
10769: }
10770: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10771: my %crsinfo =
10772: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10773: if (ref($crsinfo{$cid}) eq 'HASH') {
10774: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10775: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10776: }
10777: }
10778: }
10779:
1.169 harris41 10780: # -------------------------------------------------- Modify user authentication
1.197 www 10781: # Overrides without validation
10782:
1.169 harris41 10783: sub modifyuserauth {
10784: my ($udom,$uname,$umode,$upass)=@_;
10785: my $uhome=&homeserver($uname,$udom);
1.1172.2.115 raeburn 10786: my $allowed;
10787: if (&allowed('mau',$udom)) {
10788: $allowed = 1;
10789: } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10790: ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10791: (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10792: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10793: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10794: if (($cdom ne '') && ($cnum ne '')) {
10795: my $is_owner = &is_course_owner($cdom,$cnum);
10796: if ($is_owner) {
10797: $allowed = 1;
10798: }
10799: }
10800: }
10801: unless ($allowed) { return 'refused'; }
1.197 www 10802: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 10803: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10804: ' in domain '.$env{'request.role.domain'});
1.169 harris41 10805: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10806: &escape($upass),$uhome);
1.1172.2.134 raeburn 10807: my $ip = &get_requestor_ip();
1.620 albertel 10808: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 10809: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1172.2.134 raeburn 10810: '(Remote '.$ip.'): '.$reply);
1.197 www 10811: &log($udom,,$uname,$uhome,
1.620 albertel 10812: 'Authentication changed by '.$env{'user.domain'}.', '.
10813: $env{'user.name'}.', '.$umode.
1.1172.2.134 raeburn 10814: '(Remote '.$ip.'): '.$reply);
1.169 harris41 10815: unless ($reply eq 'ok') {
1.197 www 10816: &logthis('Authentication mode error: '.$reply);
1.169 harris41 10817: return 'error: '.$reply;
10818: }
1.170 harris41 10819: return 'ok';
1.80 www 10820: }
10821:
1.81 www 10822: # --------------------------------------------------------------- Modify a user
1.80 www 10823:
1.81 www 10824: sub modifyuser {
1.206 matthew 10825: my ($udom, $uname, $uid,
10826: $umode, $upass, $first,
10827: $middle, $last, $gene,
1.1058 raeburn 10828: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 10829: $udom= &LONCAPA::clean_domain($udom);
10830: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 10831: my $showcandelete = 'none';
10832: if (ref($candelete) eq 'ARRAY') {
10833: if (@{$candelete} > 0) {
10834: $showcandelete = join(', ',@{$candelete});
10835: }
10836: }
1.81 www 10837: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 10838: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 10839: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 10840: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10841: ' desiredhome not specified').
1.620 albertel 10842: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10843: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 10844: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 10845: my $newuser;
10846: if ($uhome eq 'no_host') {
10847: $newuser = 1;
1.1172.2.146. .13(raeb 10848:-23): unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10849:-23): ($umode eq 'lti')) {
10850:-23): return 'error: more information needed to create new user';
10851:-23): }
1.1075 raeburn 10852: }
1.80 www 10853: # ----------------------------------------------------------------- Create User
1.406 albertel 10854: if (($uhome eq 'no_host') &&
1.1172.2.146. .13(raeb 10855:-23): (($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80 www 10856: my $unhome='';
1.844 albertel 10857: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 10858: $unhome = $desiredhome;
1.620 albertel 10859: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10860: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 10861: } else { # load balancing routine for determining $unhome
1.81 www 10862: my $loadm=10000000;
1.841 albertel 10863: my %servers = &get_servers($udom,'library');
10864: foreach my $tryserver (keys(%servers)) {
10865: my $answer=reply('load',$tryserver);
10866: if (($answer=~/\d+/) && ($answer<$loadm)) {
10867: $loadm=$answer;
10868: $unhome=$tryserver;
10869: }
1.80 www 10870: }
10871: }
10872: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 10873: return 'error: unable to find a home server for '.$uname.
10874: ' in domain '.$udom;
1.80 www 10875: }
10876: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10877: &escape($upass),$unhome);
10878: unless ($reply eq 'ok') {
10879: return 'error: '.$reply;
10880: }
1.230 stredwic 10881: $uhome=&homeserver($uname,$udom,'true');
1.80 www 10882: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 10883: return 'error: unable verify users home machine.';
1.80 www 10884: }
1.209 matthew 10885: } # End of creation of new user
1.80 www 10886: # ---------------------------------------------------------------------- Add ID
10887: if ($uid) {
10888: $uid=~tr/A-Z/a-z/;
10889: my %uidhash=&idrget($udom,$uname);
1.196 www 10890: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
10891: && (!$forceid)) {
1.80 www 10892: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 10893: return 'error: user id "'.$uid.'" does not match '.
10894: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 10895: }
10896: } else {
10897: &idput($udom,($uname => $uid));
10898: }
10899: }
10900: # -------------------------------------------------------------- Add names, etc
1.313 matthew 10901: my @tmp=&get('environment',
1.899 raeburn 10902: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 10903: 'permanentemail','inststatus'],
1.134 albertel 10904: $udom,$uname);
1.1075 raeburn 10905: my (%names,%oldnames);
1.313 matthew 10906: if ($tmp[0] =~ m/^error:.*/) {
10907: %names=();
10908: } else {
10909: %names = @tmp;
1.1075 raeburn 10910: %oldnames = %names;
1.313 matthew 10911: }
1.388 www 10912: #
1.1058 raeburn 10913: # If name, email and/or uid are blank (e.g., because an uploaded file
10914: # of users did not contain them), do not overwrite existing values
10915: # unless field is in $candelete array ref.
10916: #
10917:
10918: my @fields = ('firstname','middlename','lastname','generation',
10919: 'permanentemail','id');
10920: my %newvalues;
10921: if (ref($candelete) eq 'ARRAY') {
10922: foreach my $field (@fields) {
10923: if (grep(/^\Q$field\E$/,@{$candelete})) {
10924: if ($field eq 'firstname') {
10925: $names{$field} = $first;
10926: } elsif ($field eq 'middlename') {
10927: $names{$field} = $middle;
10928: } elsif ($field eq 'lastname') {
10929: $names{$field} = $last;
10930: } elsif ($field eq 'generation') {
10931: $names{$field} = $gene;
10932: } elsif ($field eq 'permanentemail') {
10933: $names{$field} = $email;
10934: } elsif ($field eq 'id') {
10935: $names{$field} = $uid;
10936: }
10937: }
10938: }
10939: }
1.388 www 10940: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 10941: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 10942: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 10943: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 10944: if ($email) {
10945: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 10946: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 10947: }
1.899 raeburn 10948: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 10949: if (defined($inststatus)) {
10950: $names{'inststatus'} = '';
10951: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10952: if (ref($usertypes) eq 'HASH') {
10953: my @okstatuses;
10954: foreach my $item (split(/:/,$inststatus)) {
10955: if (defined($usertypes->{$item})) {
10956: push(@okstatuses,$item);
10957: }
10958: }
10959: if (@okstatuses) {
10960: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10961: }
10962: }
10963: }
1.1075 raeburn 10964: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 10965: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 10966: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 10967: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10968: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10969: } else {
10970: $logmsg .= ' during self creation';
10971: }
1.1075 raeburn 10972: my $changed;
10973: if ($newuser) {
10974: $changed = 1;
10975: } else {
10976: foreach my $field (@fields) {
10977: if ($names{$field} ne $oldnames{$field}) {
10978: $changed = 1;
10979: last;
10980: }
10981: }
10982: }
10983: unless ($changed) {
10984: $logmsg = 'No changes in user information needed for: '.$logmsg;
10985: &logthis($logmsg);
10986: return 'ok';
10987: }
10988: my $reply = &put('environment', \%names, $udom,$uname);
10989: if ($reply ne 'ok') {
10990: return 'error: '.$reply;
10991: }
1.1087 raeburn 10992: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10993: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10994: }
1.1075 raeburn 10995: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10996: &devalidate_cache_new('namescache',$uname.':'.$udom);
10997: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 10998: &logthis($logmsg);
1.134 albertel 10999: return 'ok';
1.80 www 11000: }
11001:
1.81 www 11002: # -------------------------------------------------------------- Modify student
1.80 www 11003:
1.81 www 11004: sub modifystudent {
11005: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 11006: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1172.2.76 raeburn 11007: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 11008: if (!$cid) {
1.620 albertel 11009: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11010: return 'not_in_class';
11011: }
1.80 www 11012: }
11013: # --------------------------------------------------------------- Make the user
1.81 www 11014: my $reply=&modifyuser
1.209 matthew 11015: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 11016: $desiredhome,$email,$inststatus);
1.80 www 11017: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 11018: # This will cause &modify_student_enrollment to get the uid from the
1.1172.2.31 raeburn 11019: # student's environment
1.297 matthew 11020: $uid = undef if (!$forceid);
1.455 albertel 11021: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1172.2.19 raeburn 11022: $gene,$usec,$end,$start,$type,$locktype,
1.1172.2.76 raeburn 11023: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 11024: return $reply;
11025: }
11026:
11027: sub modify_student_enrollment {
1.1172.2.23 raeburn 11028: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1172.2.76 raeburn 11029: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 11030: my ($cdom,$cnum,$chome);
11031: if (!$cid) {
1.620 albertel 11032: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11033: return 'not_in_class';
11034: }
1.620 albertel 11035: $cdom=$env{'course.'.$cid.'.domain'};
11036: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 11037: } else {
11038: ($cdom,$cnum)=split(/_/,$cid);
11039: }
1.620 albertel 11040: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 11041: if (!$chome) {
1.457 raeburn 11042: $chome=&homeserver($cnum,$cdom);
1.297 matthew 11043: }
1.455 albertel 11044: if (!$chome) { return 'unknown_course'; }
1.297 matthew 11045: # Make sure the user exists
1.81 www 11046: my $uhome=&homeserver($uname,$udom);
11047: if (($uhome eq '') || ($uhome eq 'no_host')) {
11048: return 'error: no such user';
11049: }
1.297 matthew 11050: # Get student data if we were not given enough information
11051: if (!defined($first) || $first eq '' ||
11052: !defined($last) || $last eq '' ||
11053: !defined($uid) || $uid eq '' ||
11054: !defined($middle) || $middle eq '' ||
11055: !defined($gene) || $gene eq '') {
1.294 matthew 11056: # They did not supply us with enough data to enroll the student, so
11057: # we need to pick up more information.
1.297 matthew 11058: my %tmp = &get('environment',
1.294 matthew 11059: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 11060: ,$udom,$uname);
11061:
1.800 albertel 11062: #foreach my $key (keys(%tmp)) {
11063: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 11064: #}
1.294 matthew 11065: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
11066: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11067: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 11068: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 11069: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
11070: }
1.556 albertel 11071: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 11072: my $user = "$uname:$udom";
11073: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 11074: my $reply=cput('classlist',
1.1148 raeburn 11075: {$user =>
1.1172.2.76 raeburn 11076: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 11077: $cdom,$cnum);
1.1148 raeburn 11078: if (($reply eq 'ok') || ($reply eq 'delayed')) {
11079: &devalidate_getsection_cache($udom,$uname,$cid);
11080: } else {
1.81 www 11081: return 'error: '.$reply;
11082: }
1.297 matthew 11083: # Add student role to user
1.83 www 11084: my $uurl='/'.$cid;
1.81 www 11085: $uurl=~s/\_/\//g;
11086: if ($usec) {
11087: $uurl.='/'.$usec;
11088: }
1.1148 raeburn 11089: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11090: $selfenroll,$context);
11091: if ($result ne 'ok') {
11092: if ($old_entry{$user} ne '') {
11093: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11094: } else {
11095: $reply = &del('classlist',[$user],$cdom,$cnum);
11096: }
11097: }
11098: return $result;
1.21 www 11099: }
11100:
1.556 albertel 11101: sub format_name {
11102: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11103: my $name;
11104: if ($first ne 'lastname') {
11105: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11106: } else {
11107: if ($lastname=~/\S/) {
11108: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11109: $name=~s/\s+,/,/;
11110: } else {
11111: $name.= $firstname.' '.$middlename.' '.$generation;
11112: }
11113: }
11114: $name=~s/^\s+//;
11115: $name=~s/\s+$//;
11116: $name=~s/\s+/ /g;
11117: return $name;
11118: }
11119:
1.84 www 11120: # ------------------------------------------------- Write to course preferences
11121:
11122: sub writecoursepref {
11123: my ($courseid,%prefs)=@_;
11124: $courseid=~s/^\///;
11125: $courseid=~s/\_/\//g;
11126: my ($cdomain,$cnum)=split(/\//,$courseid);
11127: my $chome=homeserver($cnum,$cdomain);
11128: if (($chome eq '') || ($chome eq 'no_host')) {
11129: return 'error: no such course';
11130: }
11131: my $cstring='';
1.800 albertel 11132: foreach my $pref (keys(%prefs)) {
11133: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 11134: }
1.84 www 11135: $cstring=~s/\&$//;
11136: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11137: }
11138:
11139: # ---------------------------------------------------------- Make/modify course
11140:
11141: sub createcourse {
1.741 raeburn 11142: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1172.2.146. .1(raebu 11143:22): $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84 www 11144: $url=&declutter($url);
11145: my $cid='';
1.1028 raeburn 11146: if ($context eq 'requestcourses') {
11147: my $can_create = 0;
11148: my ($ownername,$ownerdom) = split(':',$course_owner);
11149: if ($udom eq $ownerdom) {
1.1172.2.146. .1(raebu 11150:22): my $reload;
11151:22): if (($callercontext eq 'auto') &&
11152:22): ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11153:22): $reload = 'reload';
11154:22): }
11155:22): if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028 raeburn 11156: $context)) {
11157: $can_create = 1;
11158: }
11159: } else {
11160: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11161: $category);
11162: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11163: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11164: if (@curr > 0) {
11165: my @options = qw(approval validate autolimit);
11166: my $optregex = join('|',@options);
11167: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11168: $can_create = 1;
11169: }
11170: }
11171: }
11172: }
11173: if ($can_create) {
11174: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11175: unless (&allowed('ccc',$udom)) {
11176: return 'refused';
11177: }
1.1017 raeburn 11178: }
11179: } else {
11180: return 'refused';
11181: }
1.1028 raeburn 11182: } elsif (!&allowed('ccc',$udom)) {
11183: return 'refused';
1.84 www 11184: }
1.1011 raeburn 11185: # --------------------------------------------------------------- Get Unique ID
11186: my $uname;
11187: if ($cnum =~ /^$match_courseid$/) {
11188: my $chome=&homeserver($cnum,$udom,'true');
11189: if (($chome eq '') || ($chome eq 'no_host')) {
11190: $uname = $cnum;
11191: } else {
1.1038 raeburn 11192: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11193: }
11194: } else {
1.1038 raeburn 11195: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11196: }
11197: return $uname if ($uname =~ /^error/);
11198: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 11199: if (!defined($course_server)) {
11200: if (defined(&domain($udom,'primary'))) {
11201: $course_server = &domain($udom,'primary');
11202: } else {
11203: $course_server = $env{'user.home'};
11204: }
11205: }
11206: my %host_servers =
11207: &Apache::lonnet::get_servers($udom,'library');
11208: unless ($host_servers{$course_server}) {
11209: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 11210: }
1.84 www 11211: # ------------------------------------------------------------- Make the course
11212: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 11213: $course_server);
1.84 www 11214: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 11215: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 11216: if (($uhome eq '') || ($uhome eq 'no_host')) {
11217: return 'error: no such course';
11218: }
1.271 www 11219: # ----------------------------------------------------------------- Course made
1.516 raeburn 11220: # log existence
1.1029 raeburn 11221: my $now = time;
1.918 raeburn 11222: my $newcourse = {
11223: $udom.'_'.$uname => {
1.921 raeburn 11224: description => $description,
11225: inst_code => $inst_code,
11226: owner => $course_owner,
11227: type => $crstype,
1.1029 raeburn 11228: creator => $env{'user.name'}.':'.
11229: $env{'user.domain'},
11230: created => $now,
11231: context => $context,
1.918 raeburn 11232: },
11233: };
1.921 raeburn 11234: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 11235: # set toplevel url
1.271 www 11236: my $topurl=$url;
11237: unless ($nonstandard) {
11238: # ------------------------------------------ For standard courses, make top url
11239: my $mapurl=&clutter($url);
1.278 www 11240: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 11241: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 11242: <map>
11243: <resource id="1" type="start"></resource>
11244: <resource id="2" src="$mapurl"></resource>
11245: <resource id="3" type="finish"></resource>
11246: <link index="1" from="1" to="2"></link>
11247: <link index="2" from="2" to="3"></link>
11248: </map>
11249: ENDINITMAP
11250: $topurl=&declutter(
1.638 albertel 11251: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 11252: );
11253: }
11254: # ----------------------------------------------------------- Write preferences
1.84 www 11255: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 11256: ('description' => $description,
11257: 'url' => $topurl,
11258: 'internal.creator' => $env{'user.name'}.':'.
11259: $env{'user.domain'},
11260: 'internal.created' => $now,
11261: 'internal.creationcontext' => $context)
11262: );
1.84 www 11263: return '/'.$udom.'/'.$uname;
11264: }
11265:
1.1011 raeburn 11266: # ------------------------------------------------------------------- Create ID
11267: sub generate_coursenum {
1.1038 raeburn 11268: my ($udom,$crstype) = @_;
1.1011 raeburn 11269: my $domdesc = &domain($udom);
11270: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 11271: my $first;
11272: if ($crstype eq 'Community') {
11273: $first = '0';
11274: } else {
11275: $first = int(1+rand(9));
11276: }
11277: my $uname=$first.
1.1011 raeburn 11278: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11279: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11280: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11281: # ----------------------------------------------- Make sure that does not exist
11282: my $uhome=&homeserver($uname,$udom,'true');
11283: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 11284: if ($crstype eq 'Community') {
11285: $first = '0';
11286: } else {
11287: $first = int(1+rand(9));
11288: }
11289: $uname=$first.
1.1011 raeburn 11290: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11291: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11292: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11293: $uhome=&homeserver($uname,$udom,'true');
11294: unless (($uhome eq '') || ($uhome eq 'no_host')) {
11295: return 'error: unable to generate unique course-ID';
11296: }
11297: }
11298: return $uname;
11299: }
11300:
1.813 albertel 11301: sub is_course {
1.1167 droeschl 11302: my ($cdom, $cnum) = scalar(@_) == 1 ?
11303: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
1.1172.2.95 raeburn 11304: return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11305: my $uhome=&homeserver($cnum,$cdom);
11306: my $iscourse;
11307: if (grep { $_ eq $uhome } current_machine_ids()) {
11308: $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11309: } else {
11310: my $hashid = $cdom.':'.$cnum;
11311: ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11312: unless (defined($cached)) {
11313: my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11314: $cnum,undef,undef,'.');
11315: $iscourse = 0;
11316: if (exists($courses{$cdom.'_'.$cnum})) {
11317: $iscourse = 1;
11318: }
11319: &do_cache_new('iscourse',$hashid,$iscourse,3600);
11320: }
11321: }
11322: return unless($iscourse);
1.1167 droeschl 11323: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 11324: }
11325:
1.1015 raeburn 11326: sub store_userdata {
11327: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 11328: my $result;
1.1016 raeburn 11329: if ($datakey ne '') {
1.1013 raeburn 11330: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 11331: if ($udom eq '' || $uname eq '') {
11332: $udom = $env{'user.domain'};
11333: $uname = $env{'user.name'};
11334: }
11335: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 11336: if (($uhome eq '') || ($uhome eq 'no_host')) {
11337: $result = 'error: no_host';
11338: } else {
1.1172.2.138 raeburn 11339: $storehash->{'ip'} = &get_requestor_ip();
1.1013 raeburn 11340: $storehash->{'host'} = $perlvar{'lonHostID'};
11341:
11342: my $namevalue='';
11343: foreach my $key (keys(%{$storehash})) {
11344: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11345: }
11346: $namevalue=~s/\&$//;
1.1172.2.26 raeburn 11347: unless ($namespace eq 'courserequests') {
11348: $datakey = &escape($datakey);
11349: }
1.1105 raeburn 11350: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
11351: $namevalue,$uhome);
1.1013 raeburn 11352: }
11353: } else {
11354: $result = 'error: data to store was not a hash reference';
11355: }
11356: } else {
11357: $result= 'error: invalid requestkey';
11358: }
11359: return $result;
11360: }
11361:
1.21 www 11362: # ---------------------------------------------------------- Assign Custom Role
11363:
11364: sub assigncustomrole {
1.957 raeburn 11365: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11366: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 11367: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 11368: }
11369:
11370: # ----------------------------------------------------------------- Revoke Role
11371:
11372: sub revokerole {
1.957 raeburn 11373: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11374: my $now=time;
1.965 raeburn 11375: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 11376: }
11377:
11378: # ---------------------------------------------------------- Revoke Custom Role
11379:
11380: sub revokecustomrole {
1.957 raeburn 11381: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11382: my $now=time;
1.357 www 11383: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 11384: $deleteflag,$selfenroll,$context);
1.17 www 11385: }
11386:
1.533 banghart 11387: # ------------------------------------------------------------ Disk usage
1.535 albertel 11388: sub diskusage {
1.955 raeburn 11389: my ($udom,$uname,$directorypath,$getpropath)=@_;
11390: $directorypath =~ s/\/$//;
11391: my $listing=&reply('du2:'.&escape($directorypath).':'
11392: .&escape($getpropath).':'.&escape($uname).':'
11393: .&escape($udom),homeserver($uname,$udom));
11394: if ($listing eq 'unknown_cmd') {
11395: if ($getpropath) {
11396: $directorypath = &propath($udom,$uname).'/'.$directorypath;
11397: }
11398: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11399: }
1.514 albertel 11400: return $listing;
1.512 banghart 11401: }
11402:
1.566 banghart 11403: sub is_locked {
1.1096 raeburn 11404: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 11405: my @check;
11406: my $is_locked;
1.1093 raeburn 11407: push (@check,$file_name);
1.613 albertel 11408: my %locked = &get('file_permissions',\@check,
1.620 albertel 11409: $env{'user.domain'},$env{'user.name'});
1.615 albertel 11410: my ($tmp)=keys(%locked);
11411: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 11412:
1.566 banghart 11413: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 11414: $is_locked = 'false';
11415: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 11416: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 11417: $is_locked = 'true';
1.1096 raeburn 11418: if (ref($which) eq 'ARRAY') {
11419: push(@{$which},$entry);
11420: } else {
11421: last;
11422: }
1.745 raeburn 11423: }
11424: }
1.566 banghart 11425: } else {
11426: $is_locked = 'false';
11427: }
1.1093 raeburn 11428: return $is_locked;
1.566 banghart 11429: }
11430:
1.759 albertel 11431: sub declutter_portfile {
11432: my ($file) = @_;
1.833 albertel 11433: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 11434: return $file;
11435: }
11436:
1.559 banghart 11437: # ------------------------------------------------------------- Mark as Read Only
11438:
11439: sub mark_as_readonly {
11440: my ($domain,$user,$files,$what) = @_;
1.613 albertel 11441: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11442: my ($tmp)=keys(%current_permissions);
11443: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 11444: foreach my $file (@{$files}) {
1.759 albertel 11445: $file = &declutter_portfile($file);
1.561 banghart 11446: push(@{$current_permissions{$file}},$what);
1.559 banghart 11447: }
1.613 albertel 11448: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11449: return;
11450: }
11451:
1.572 banghart 11452: # ------------------------------------------------------------Save Selected Files
11453:
11454: sub save_selected_files {
11455: my ($user, $path, @files) = @_;
11456: my $filename = $user."savedfiles";
1.573 banghart 11457: my @other_files = &files_not_in_path($user, $path);
1.1172.2.96 raeburn 11458: open (OUT,'>',LONCAPA::tempdir().$filename);
1.573 banghart 11459: foreach my $file (@files) {
1.620 albertel 11460: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 11461: }
11462: foreach my $file (@other_files) {
1.574 banghart 11463: print (OUT $file."\n");
1.572 banghart 11464: }
1.574 banghart 11465: close (OUT);
1.572 banghart 11466: return 'ok';
11467: }
11468:
1.574 banghart 11469: sub clear_selected_files {
11470: my ($user) = @_;
11471: my $filename = $user."savedfiles";
1.1172.2.96 raeburn 11472: open (OUT,'>',LONCAPA::tempdir().$filename);
1.574 banghart 11473: print (OUT undef);
11474: close (OUT);
11475: return ("ok");
11476: }
11477:
1.572 banghart 11478: sub files_in_path {
11479: my ($user, $path) = @_;
11480: my $filename = $user."savedfiles";
11481: my %return_files;
1.1172.2.96 raeburn 11482: open (IN,'<',LONCAPA::tempdir().$filename);
1.573 banghart 11483: while (my $line_in = <IN>) {
1.574 banghart 11484: chomp ($line_in);
11485: my @paths_and_file = split (m!/!, $line_in);
11486: my $file_part = pop (@paths_and_file);
11487: my $path_part = join ('/', @paths_and_file);
1.573 banghart 11488: $path_part.='/';
11489: my $path_and_file = $path_part.$file_part;
11490: if ($path_part eq $path) {
11491: $return_files{$file_part}= 'selected';
11492: }
11493: }
1.574 banghart 11494: close (IN);
11495: return (\%return_files);
1.572 banghart 11496: }
11497:
11498: # called in portfolio select mode, to show files selected NOT in current directory
11499: sub files_not_in_path {
11500: my ($user, $path) = @_;
11501: my $filename = $user."savedfiles";
11502: my @return_files;
11503: my $path_part;
1.1172.2.96 raeburn 11504: open(IN, '<',LONCAPA::tempdir().$filename);
1.800 albertel 11505: while (my $line = <IN>) {
1.572 banghart 11506: #ok, I know it's clunky, but I want it to work
1.800 albertel 11507: my @paths_and_file = split(m|/|, $line);
11508: my $file_part = pop(@paths_and_file);
11509: chomp($file_part);
11510: my $path_part = join('/', @paths_and_file);
1.572 banghart 11511: $path_part .= '/';
11512: my $path_and_file = $path_part.$file_part;
11513: if ($path_part ne $path) {
1.800 albertel 11514: push(@return_files, ($path_and_file));
1.572 banghart 11515: }
11516: }
1.800 albertel 11517: close(OUT);
1.574 banghart 11518: return (@return_files);
1.572 banghart 11519: }
11520:
1.745 raeburn 11521: #----------------------------------------------Get portfolio file permissions
1.629 banghart 11522:
1.745 raeburn 11523: sub get_portfile_permissions {
11524: my ($domain,$user) = @_;
1.613 albertel 11525: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11526: my ($tmp)=keys(%current_permissions);
11527: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11528: return \%current_permissions;
11529: }
11530:
11531: #---------------------------------------------Get portfolio file access controls
11532:
1.749 raeburn 11533: sub get_access_controls {
1.745 raeburn 11534: my ($current_permissions,$group,$file) = @_;
1.769 albertel 11535: my %access;
11536: my $real_file = $file;
11537: $file =~ s/\.meta$//;
1.745 raeburn 11538: if (defined($file)) {
1.749 raeburn 11539: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11540: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 11541: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 11542: }
11543: }
1.745 raeburn 11544: } else {
1.749 raeburn 11545: foreach my $key (keys(%{$current_permissions})) {
11546: if ($key =~ /\0accesscontrol$/) {
11547: if (defined($group)) {
11548: if ($key !~ m-^\Q$group\E/-) {
11549: next;
11550: }
11551: }
11552: my ($fullpath) = split(/\0/,$key);
11553: if (ref($$current_permissions{$key}) eq 'HASH') {
11554: foreach my $control (keys(%{$$current_permissions{$key}})) {
11555: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11556: }
11557: }
11558: }
11559: }
11560: }
11561: return %access;
11562: }
11563:
11564: sub modify_access_controls {
11565: my ($file_name,$changes,$domain,$user)=@_;
11566: my ($outcome,$deloutcome);
11567: my %store_permissions;
11568: my %new_values;
11569: my %new_control;
11570: my %translation;
11571: my @deletions = ();
11572: my $now = time;
11573: if (exists($$changes{'activate'})) {
11574: if (ref($$changes{'activate'}) eq 'HASH') {
11575: my @newitems = sort(keys(%{$$changes{'activate'}}));
11576: my $numnew = scalar(@newitems);
11577: for (my $i=0; $i<$numnew; $i++) {
11578: my $newkey = $newitems[$i];
11579: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 11580: if ($newkey =~ /^\d+:/) {
11581: $newkey =~ s/^(\d+)/$newid/;
11582: $translation{$1} = $newid;
11583: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11584: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11585: $translation{$1} = $newid;
11586: }
1.749 raeburn 11587: $new_values{$file_name."\0".$newkey} =
11588: $$changes{'activate'}{$newitems[$i]};
11589: $new_control{$newkey} = $now;
11590: }
11591: }
11592: }
11593: my %todelete;
11594: my %changed_items;
11595: foreach my $action ('delete','update') {
11596: if (exists($$changes{$action})) {
11597: if (ref($$changes{$action}) eq 'HASH') {
11598: foreach my $key (keys(%{$$changes{$action}})) {
11599: my ($itemnum) = ($key =~ /^([^:]+):/);
11600: if ($action eq 'delete') {
11601: $todelete{$itemnum} = 1;
11602: } else {
11603: $changed_items{$itemnum} = $key;
11604: }
11605: }
1.745 raeburn 11606: }
11607: }
1.749 raeburn 11608: }
11609: # get lock on access controls for file.
11610: my $lockhash = {
11611: $file_name."\0".'locked_access_records' => $env{'user.name'}.
11612: ':'.$env{'user.domain'},
11613: };
11614: my $tries = 0;
11615: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11616:
1.1172.2.79 raeburn 11617: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 11618: $tries ++;
1.1172.2.79 raeburn 11619: sleep(0.1);
1.749 raeburn 11620: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11621: }
11622: if ($gotlock eq 'ok') {
11623: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11624: my ($tmp)=keys(%curr_permissions);
11625: if ($tmp=~/^error:/) { undef(%curr_permissions); }
11626: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11627: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11628: if (ref($curr_controls) eq 'HASH') {
11629: foreach my $control_item (keys(%{$curr_controls})) {
11630: my ($itemnum) = ($control_item =~ /^([^:]+):/);
11631: if (defined($todelete{$itemnum})) {
11632: push(@deletions,$file_name."\0".$control_item);
11633: } else {
11634: if (defined($changed_items{$itemnum})) {
11635: $new_control{$changed_items{$itemnum}} = $now;
11636: push(@deletions,$file_name."\0".$control_item);
11637: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11638: } else {
11639: $new_control{$control_item} = $$curr_controls{$control_item};
11640: }
11641: }
1.745 raeburn 11642: }
11643: }
11644: }
1.970 raeburn 11645: my ($group);
11646: if (&is_course($domain,$user)) {
11647: ($group,my $file) = split(/\//,$file_name,2);
11648: }
1.749 raeburn 11649: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11650: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11651: $outcome = &put('file_permissions',\%new_values,$domain,$user);
11652: # remove lock
11653: my @del_lock = ($file_name."\0".'locked_access_records');
11654: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 11655: my $sqlresult =
1.970 raeburn 11656: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 11657: $group);
1.749 raeburn 11658: } else {
11659: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 11660: }
1.749 raeburn 11661: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 11662: }
11663:
1.827 raeburn 11664: sub make_public_indefinitely {
11665: my ($requrl) = @_;
11666: my $now = time;
11667: my $action = 'activate';
11668: my $aclnum = 0;
11669: if (&is_portfolio_url($requrl)) {
11670: my (undef,$udom,$unum,$file_name,$group) =
11671: &parse_portfolio_url($requrl);
11672: my $current_perms = &get_portfile_permissions($udom,$unum);
11673: my %access_controls = &get_access_controls($current_perms,
11674: $group,$file_name);
11675: foreach my $key (keys(%{$access_controls{$file_name}})) {
11676: my ($num,$scope,$end,$start) =
11677: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11678: if ($scope eq 'public') {
11679: if ($start <= $now && $end == 0) {
11680: $action = 'none';
11681: } else {
11682: $action = 'update';
11683: $aclnum = $num;
11684: }
11685: last;
11686: }
11687: }
11688: if ($action eq 'none') {
11689: return 'ok';
11690: } else {
11691: my %changes;
11692: my $newend = 0;
11693: my $newstart = $now;
11694: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
11695: $changes{$action}{$newkey} = {
11696: type => 'public',
11697: time => {
11698: start => $newstart,
11699: end => $newend,
11700: },
11701: };
11702: my ($outcome,$deloutcome,$new_values,$translation) =
11703: &modify_access_controls($file_name,\%changes,$udom,$unum);
11704: return $outcome;
11705: }
11706: } else {
11707: return 'invalid';
11708: }
11709: }
11710:
1.745 raeburn 11711: #------------------------------------------------------Get Marked as Read Only
11712:
11713: sub get_marked_as_readonly {
11714: my ($domain,$user,$what,$group) = @_;
11715: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 11716: my @readonly_files;
1.629 banghart 11717: my $cmp1=$what;
11718: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 11719: while (my ($file_name,$value) = each(%{$current_permissions})) {
11720: if (defined($group)) {
11721: if ($file_name !~ m-^\Q$group\E/-) {
11722: next;
11723: }
11724: }
1.561 banghart 11725: if (ref($value) eq "ARRAY"){
11726: foreach my $stored_what (@{$value}) {
1.629 banghart 11727: my $cmp2=$stored_what;
1.759 albertel 11728: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 11729: $cmp2=join('',@{$stored_what});
1.745 raeburn 11730: }
1.629 banghart 11731: if ($cmp1 eq $cmp2) {
1.561 banghart 11732: push(@readonly_files, $file_name);
1.745 raeburn 11733: last;
1.563 banghart 11734: } elsif (!defined($what)) {
11735: push(@readonly_files, $file_name);
1.745 raeburn 11736: last;
1.561 banghart 11737: }
11738: }
1.745 raeburn 11739: }
1.561 banghart 11740: }
11741: return @readonly_files;
11742: }
1.577 banghart 11743: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 11744:
1.577 banghart 11745: sub get_marked_as_readonly_hash {
1.745 raeburn 11746: my ($current_permissions,$group,$what) = @_;
1.577 banghart 11747: my %readonly_files;
1.745 raeburn 11748: while (my ($file_name,$value) = each(%{$current_permissions})) {
11749: if (defined($group)) {
11750: if ($file_name !~ m-^\Q$group\E/-) {
11751: next;
11752: }
11753: }
1.577 banghart 11754: if (ref($value) eq "ARRAY"){
11755: foreach my $stored_what (@{$value}) {
1.745 raeburn 11756: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 11757: foreach my $lock_descriptor(@{$stored_what}) {
11758: if ($lock_descriptor eq 'graded') {
11759: $readonly_files{$file_name} = 'graded';
11760: } elsif ($lock_descriptor eq 'handback') {
11761: $readonly_files{$file_name} = 'handback';
11762: } else {
11763: if (!exists($readonly_files{$file_name})) {
11764: $readonly_files{$file_name} = 'locked';
11765: }
11766: }
1.745 raeburn 11767: }
1.750 banghart 11768: }
1.577 banghart 11769: }
11770: }
11771: }
11772: return %readonly_files;
11773: }
1.559 banghart 11774: # ------------------------------------------------------------ Unmark as Read Only
11775:
11776: sub unmark_as_readonly {
1.629 banghart 11777: # unmarks $file_name (if $file_name is defined), or all files locked by $what
11778: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 11779: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 11780: $file_name = &declutter_portfile($file_name);
1.634 albertel 11781: my $symb_crs = $what;
11782: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 11783: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 11784: my ($tmp)=keys(%current_permissions);
11785: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11786: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 11787: foreach my $file (@readonly_files) {
1.759 albertel 11788: my $clean_file = &declutter_portfile($file);
11789: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 11790: my $current_locks = $current_permissions{$file};
1.563 banghart 11791: my @new_locks;
11792: my @del_keys;
11793: if (ref($current_locks) eq "ARRAY"){
11794: foreach my $locker (@{$current_locks}) {
1.632 albertel 11795: my $compare=$locker;
1.749 raeburn 11796: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 11797: $compare=join('',@{$locker});
1.746 raeburn 11798: if ($compare ne $symb_crs) {
11799: push(@new_locks, $locker);
11800: }
1.563 banghart 11801: }
11802: }
1.650 albertel 11803: if (scalar(@new_locks) > 0) {
1.563 banghart 11804: $current_permissions{$file} = \@new_locks;
11805: } else {
11806: push(@del_keys, $file);
1.613 albertel 11807: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 11808: delete($current_permissions{$file});
1.563 banghart 11809: }
11810: }
1.561 banghart 11811: }
1.613 albertel 11812: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11813: return;
11814: }
1.512 banghart 11815:
1.17 www 11816: # ------------------------------------------------------------ Directory lister
11817:
11818: sub dirlist {
1.955 raeburn 11819: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 11820: $uri=~s/^\///;
11821: $uri=~s/\/$//;
1.253 stredwic 11822: my ($udom, $uname);
1.955 raeburn 11823: if ($getuserdir) {
1.253 stredwic 11824: $udom = $userdomain;
11825: $uname = $username;
1.955 raeburn 11826: } else {
11827: (undef,$udom,$uname)=split(/\//,$uri);
11828: if(defined($userdomain)) {
11829: $udom = $userdomain;
11830: }
11831: if(defined($username)) {
11832: $uname = $username;
11833: }
1.253 stredwic 11834: }
1.955 raeburn 11835: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 11836:
1.955 raeburn 11837: $dirRoot = $perlvar{'lonDocRoot'};
11838: if (defined($getpropath)) {
11839: $dirRoot = &propath($udom,$uname);
1.253 stredwic 11840: $dirRoot =~ s/\/$//;
1.955 raeburn 11841: } elsif (defined($getuserdir)) {
11842: my $subdir=$uname.'__';
11843: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11844: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11845: ."/$udom/$subdir/$uname";
11846: } elsif (defined($alternateRoot)) {
11847: $dirRoot = $alternateRoot;
1.751 banghart 11848: }
1.253 stredwic 11849:
11850: if($udom) {
11851: if($uname) {
1.1135 raeburn 11852: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 11853: if ($uhome eq 'no_host') {
11854: return ([],'no_host');
11855: }
1.955 raeburn 11856: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 11857: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 11858: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 11859: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11860: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 11861: } else {
11862: @listing_results = map { &unescape($_); } split(/:/,$listing);
11863: }
1.605 matthew 11864: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11865: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 11866: @listing_results = split(/:/,$listing);
11867: } else {
11868: @listing_results = map { &unescape($_); } split(/:/,$listing);
11869: }
1.1135 raeburn 11870: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 11871: ($listing eq 'rejected') || ($listing eq 'refused') ||
11872: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11873: return ([],$listing);
11874: } else {
11875: return (\@listing_results);
1.1135 raeburn 11876: }
1.955 raeburn 11877: } elsif(!$alternateRoot) {
1.1136 raeburn 11878: my (%allusers,%listerror);
1.841 albertel 11879: my %servers = &get_servers($udom,'library');
1.955 raeburn 11880: foreach my $tryserver (keys(%servers)) {
11881: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11882: &escape($udom),$tryserver);
11883: if ($listing eq 'unknown_cmd') {
11884: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11885: $udom, $tryserver);
11886: } else {
11887: @listing_results = map { &unescape($_); } split(/:/,$listing);
11888: }
1.841 albertel 11889: if ($listing eq 'unknown_cmd') {
11890: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11891: $udom, $tryserver);
11892: @listing_results = split(/:/,$listing);
11893: } else {
11894: @listing_results =
11895: map { &unescape($_); } split(/:/,$listing);
11896: }
1.1136 raeburn 11897: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11898: ($listing eq 'rejected') || ($listing eq 'refused') ||
11899: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11900: $listerror{$tryserver} = $listing;
11901: } else {
1.841 albertel 11902: foreach my $line (@listing_results) {
11903: my ($entry) = split(/&/,$line,2);
11904: $allusers{$entry} = 1;
11905: }
11906: }
1.253 stredwic 11907: }
1.1136 raeburn 11908: my @alluserslist=();
1.800 albertel 11909: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 11910: push(@alluserslist,$user.'&user');
1.253 stredwic 11911: }
1.1172.2.80 raeburn 11912: if (!%listerror) {
11913: # no errors
11914: return (\@alluserslist);
11915: } elsif (scalar(keys(%servers)) == 1) {
11916: # one library server, one error
11917: my ($key) = keys(%listerror);
11918: return (\@alluserslist, $listerror{$key});
11919: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11920: # con_lost indicates that we might miss data from at least one
11921: # library server
11922: return (\@alluserslist, 'con_lost');
11923: } else {
11924: # multiple library servers and no con_lost -> data should be
11925: # complete.
11926: return (\@alluserslist);
11927: }
11928:
1.253 stredwic 11929: } else {
1.1136 raeburn 11930: return ([],'missing username');
1.253 stredwic 11931: }
1.955 raeburn 11932: } elsif(!defined($getpropath)) {
1.1136 raeburn 11933: my $path = $perlvar{'lonDocRoot'}.'/res/';
11934: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11935: return (\@all_domains);
1.955 raeburn 11936: } else {
1.1136 raeburn 11937: return ([],'missing domain');
1.275 stredwic 11938: }
11939: }
11940:
11941: # --------------------------------------------- GetFileTimestamp
11942: # This function utilizes dirlist and returns the date stamp for
11943: # when it was last modified. It will also return an error of -1
11944: # if an error occurs
11945:
11946: sub GetFileTimestamp {
1.955 raeburn 11947: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 11948: $studentDomain = &LONCAPA::clean_domain($studentDomain);
11949: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 11950: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11951: undef,$getuserdir);
11952: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11953: return -1;
11954: }
11955: if (ref($fileref) eq 'ARRAY') {
11956: my @stats = split('&',$fileref->[0]);
1.375 matthew 11957: # @stats contains first the filename, then the stat output
11958: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 11959: } else {
11960: return -1;
1.253 stredwic 11961: }
1.26 www 11962: }
11963:
1.712 albertel 11964: sub stat_file {
11965: my ($uri) = @_;
1.787 albertel 11966: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 11967:
1.955 raeburn 11968: my ($udom,$uname,$file);
1.712 albertel 11969: if ($uri =~ m-^/(uploaded|editupload)/-) {
11970: ($udom,$uname,$file) =
1.811 albertel 11971: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 11972: $file = 'userfiles/'.$file;
11973: }
11974: if ($uri =~ m-^/res/-) {
11975: ($udom,$uname) =
1.807 albertel 11976: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 11977: $file = $uri;
11978: }
11979:
11980: if (!$udom || !$uname || !$file) {
11981: # unable to handle the uri
11982: return ();
11983: }
1.956 raeburn 11984: my $getpropath;
11985: if ($file =~ /^userfiles\//) {
11986: $getpropath = 1;
11987: }
1.1136 raeburn 11988: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11989: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11990: return ();
11991: } else {
11992: if (ref($listref) eq 'ARRAY') {
11993: my @stats = split('&',$listref->[0]);
11994: shift(@stats); #filename is first
11995: return @stats;
11996: }
1.712 albertel 11997: }
11998: return ();
11999: }
12000:
1.26 www 12001: # -------------------------------------------------------- Value of a Condition
12002:
1.713 albertel 12003: # gets the value of a specific preevaluated condition
12004: # stored in the string $env{user.state.<cid>}
12005: # or looks up a condition reference in the bighash and if if hasn't
12006: # already been evaluated recurses into docondval to get the value of
12007: # the condition, then memoizing it to
12008: # $env{user.state.<cid>.<condition>}
1.40 www 12009: sub directcondval {
12010: my $number=shift;
1.620 albertel 12011: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 12012: &Apache::lonuserstate::evalstate();
12013: }
1.713 albertel 12014: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12015: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12016: } elsif ($number =~ /^_/) {
12017: my $sub_condition;
12018: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12019: &GDBM_READER(),0640)) {
12020: $sub_condition=$bighash{'conditions'.$number};
12021: untie(%bighash);
12022: }
12023: my $value = &docondval($sub_condition);
1.949 raeburn 12024: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 12025: return $value;
12026: }
1.620 albertel 12027: if ($env{'user.state.'.$env{'request.course.id'}}) {
12028: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 12029: } else {
12030: return 2;
12031: }
12032: }
12033:
1.713 albertel 12034: # get the collection of conditions for this resource
1.26 www 12035: sub condval {
12036: my $condidx=shift;
1.54 www 12037: my $allpathcond='';
1.713 albertel 12038: foreach my $cond (split(/\|/,$condidx)) {
12039: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12040: $allpathcond.=
12041: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12042: }
1.191 harris41 12043: }
1.54 www 12044: $allpathcond=~s/\|$//;
1.713 albertel 12045: return &docondval($allpathcond);
12046: }
12047:
12048: #evaluates an expression of conditions
12049: sub docondval {
12050: my ($allpathcond) = @_;
12051: my $result=0;
12052: if ($env{'request.course.id'}
12053: && defined($allpathcond)) {
12054: my $operand='|';
12055: my @stack;
12056: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12057: if ($chunk eq '(') {
12058: push @stack,($operand,$result);
12059: } elsif ($chunk eq ')') {
12060: my $before=pop @stack;
12061: if (pop @stack eq '&') {
12062: $result=$result>$before?$before:$result;
12063: } else {
12064: $result=$result>$before?$result:$before;
12065: }
12066: } elsif (($chunk eq '&') || ($chunk eq '|')) {
12067: $operand=$chunk;
12068: } else {
12069: my $new=directcondval($chunk);
12070: if ($operand eq '&') {
12071: $result=$result>$new?$new:$result;
12072: } else {
12073: $result=$result>$new?$result:$new;
12074: }
12075: }
12076: }
1.26 www 12077: }
12078: return $result;
1.421 albertel 12079: }
12080:
12081: # ---------------------------------------------------- Devalidate courseresdata
12082:
12083: sub devalidatecourseresdata {
12084: my ($coursenum,$coursedomain)=@_;
12085: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12086: &devalidate_cache_new('courseres',$hashid);
1.28 www 12087: }
12088:
1.763 www 12089:
1.200 www 12090: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 12091: #
12092: # Parameters:
12093: # $coursenum - Number of the course.
12094: # $coursedomain - Domain at which the course was created.
12095: # Returns:
12096: # A hash of the course parameters along (I think) with timestamps
12097: # and version info.
1.877 foxr 12098:
1.624 albertel 12099: sub get_courseresdata {
12100: my ($coursenum,$coursedomain)=@_;
1.200 www 12101: my $coursehom=&homeserver($coursenum,$coursedomain);
12102: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12103: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 12104: my %dumpreply;
1.417 albertel 12105: unless (defined($cached)) {
1.624 albertel 12106: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 12107: $result=\%dumpreply;
1.251 albertel 12108: my ($tmp) = keys(%dumpreply);
12109: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 12110: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 12111: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12112: return $tmp;
1.416 albertel 12113: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 12114: $result=undef;
1.599 albertel 12115: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 12116: }
12117: }
1.624 albertel 12118: return $result;
12119: }
12120:
1.633 albertel 12121: sub devalidateuserresdata {
12122: my ($uname,$udom)=@_;
12123: my $hashid="$udom:$uname";
12124: &devalidate_cache_new('userres',$hashid);
12125: }
12126:
1.624 albertel 12127: sub get_userresdata {
12128: my ($uname,$udom)=@_;
12129: #most student don\'t have any data set, check if there is some data
12130: if (&EXT_cache_status($udom,$uname)) { return undef; }
12131:
12132: my $hashid="$udom:$uname";
12133: my ($result,$cached)=&is_cached_new('userres',$hashid);
12134: if (!defined($cached)) {
12135: my %resourcedata=&dump('resourcedata',$udom,$uname);
12136: $result=\%resourcedata;
12137: &do_cache_new('userres',$hashid,$result,600);
12138: }
12139: my ($tmp)=keys(%$result);
12140: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12141: return $result;
12142: }
12143: #error 2 occurs when the .db doesn't exist
12144: if ($tmp!~/error: 2 /) {
1.1172.2.71 raeburn 12145: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12146: &logthis("<font color=\"blue\">WARNING:".
12147: " Trying to get resource data for ".
12148: $uname." at ".$udom.": ".
12149: $tmp."</font>");
12150: }
1.624 albertel 12151: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 12152: #&EXT_cache_set($udom,$uname);
12153: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 12154: undef($tmp); # not really an error so don't send it back
1.624 albertel 12155: }
12156: return $tmp;
12157: }
1.879 foxr 12158: #----------------------------------------------- resdata - return resource data
12159: # Purpose:
12160: # Return resource data for either users or for a course.
12161: # Parameters:
12162: # $name - Course/user name.
12163: # $domain - Name of the domain the user/course is registered on.
12164: # $type - Type of thing $name is (must be 'course' or 'user'
12165: # @which - Array of names of resources desired.
12166: # Returns:
12167: # The value of the first reasource in @which that is found in the
12168: # resource hash.
12169: # Exceptional Conditions:
12170: # If the $type passed in is not valid (not the string 'course' or
12171: # 'user', an undefined reference is returned.
12172: # If none of the resources are found, an undef is returned
1.624 albertel 12173: sub resdata {
12174: my ($name,$domain,$type,@which)=@_;
12175: my $result;
12176: if ($type eq 'course') {
12177: $result=&get_courseresdata($name,$domain);
12178: } elsif ($type eq 'user') {
12179: $result=&get_userresdata($name,$domain);
12180: }
12181: if (!ref($result)) { return $result; }
1.251 albertel 12182: foreach my $item (@which) {
1.927 albertel 12183: if (defined($result->{$item->[0]})) {
12184: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 12185: }
1.250 albertel 12186: }
1.291 albertel 12187: return undef;
1.200 www 12188: }
12189:
1.1172.2.146. .1(raebu 12190:22): sub get_domain_lti {
12191:22): my ($cdom,$context) = @_;
.4(raebu 12192:22): my ($name,$cachename,%lti);
.1(raebu 12193:22): if ($context eq 'consumer') {
12194:22): $name = 'ltitools';
12195:22): } elsif ($context eq 'provider') {
12196:22): $name = 'lti';
.4(raebu 12197:22): } elsif ($context eq 'linkprot') {
12198:22): $name = 'ltisec';
.1(raebu 12199:22): } else {
12200:22): return %lti;
12201:22): }
.4(raebu 12202:22): if ($context eq 'linkprot') {
12203:22): $cachename = $context;
12204:22): } else {
12205:22): $cachename = $name;
12206:22): }
12207:22): my ($result,$cached)=&is_cached_new($cachename,$cdom);
.1(raebu 12208:22): if (defined($cached)) {
12209:22): if (ref($result) eq 'HASH') {
12210:22): %lti = %{$result};
12211:22): }
12212:22): } else {
12213:22): my %domconfig = &get_dom('configuration',[$name],$cdom);
12214:22): if (ref($domconfig{$name}) eq 'HASH') {
.4(raebu 12215:22): if ($context eq 'linkprot') {
12216:22): if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12217:22): %lti = %{$domconfig{$name}{'linkprot'}};
12218:22): }
12219:22): } else {
12220:22): %lti = %{$domconfig{$name}};
12221:22): }
.1(raebu 12222:22): }
12223:22): my $cachetime = 24*60*60;
.4(raebu 12224:22): &do_cache_new($cachename,$cdom,\%lti,$cachetime);
.1(raebu 12225:22): }
12226:22): return %lti;
12227:22): }
12228:22):
12229:22): sub get_course_lti {
.13(raeb 12230:-23): my ($cnum,$cdom,$context) = @_;
12231:-23): my ($name,$cachename,%lti);
12232:-23): if ($context eq 'consumer') {
12233:-23): $name = 'ltitools';
12234:-23): $cachename = 'courseltitools';
12235:-23): } elsif ($context eq 'provider') {
12236:-23): $name = 'lti';
12237:-23): $cachename = 'courselti';
12238:-23): } else {
12239:-23): return %lti;
12240:-23): }
.1(raebu 12241:22): my $hashid=$cdom.'_'.$cnum;
.13(raeb 12242:-23): my ($result,$cached)=&is_cached_new($cachename,$hashid);
.1(raebu 12243:22): if (defined($cached)) {
12244:22): if (ref($result) eq 'HASH') {
.13(raeb 12245:-23): %lti = %{$result};
.1(raebu 12246:22): }
12247:22): } else {
.13(raeb 12248:-23): %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
.1(raebu 12249:22): my $cachetime = 24*60*60;
.13(raeb 12250:-23): &do_cache_new($cachename,$hashid,\%lti,$cachetime);
.1(raebu 12251:22): }
.13(raeb 12252:-23): return %lti;
.1(raebu 12253:22): }
12254:22):
.3(raebu 12255:22): sub courselti_itemid {
12256:22): my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12257:22): my ($chome,$itemid);
12258:22): $chome = &homeserver($cnum,$cdom);
12259:22): return if ($chome eq 'no_host');
12260:22): if (ref($params) eq 'HASH') {
12261:22): my $rep;
12262:22): if (grep { $_ eq $chome } current_machine_ids()) {
12263:22): $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12264:22): } else {
12265:22): my $escurl = &escape($url);
12266:22): my $escmethod = &escape($method);
12267:22): my $items = &freeze_escape($params);
12268:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12269:22): }
12270:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12271:22): ($rep eq 'unknown_cmd')) {
12272:22): $itemid = $rep;
12273:22): }
12274:22): }
12275:22): return $itemid;
12276:22): }
12277:22):
12278:22): sub domainlti_itemid {
12279:22): my ($cdom,$url,$method,$params,$context) = @_;
12280:22): my ($primary_id,$itemid);
12281:22): $primary_id = &domain($cdom,'primary');
12282:22): return if ($primary_id eq '');
12283:22): if (ref($params) eq 'HASH') {
12284:22): my $rep;
12285:22): if (grep { $_ eq $primary_id } current_machine_ids()) {
12286:22): $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12287:22): } else {
12288:22): my $cnum = '';
12289:22): my $escurl = &escape($url);
12290:22): my $escmethod = &escape($method);
12291:22): my $items = &freeze_escape($params);
12292:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12293:22): }
12294:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12295:22): ($rep eq 'unknown_cmd')) {
12296:22): $itemid = $rep;
12297:22): }
12298:22): }
12299:22): return $itemid;
12300:22): }
12301:22):
.15(raeb 12302:-23): sub get_ltitools_id {
12303:-23): my ($context,$cdom,$cnum,$title) = @_;
12304:-23): my ($lockhash,$tries,$gotlock,$id,$error);
12305:-23):
12306:-23): # get lock on ltitools db
12307:-23): $lockhash = {
12308:-23): lock => $env{'user.name'}.
12309:-23): ':'.$env{'user.domain'},
12310:-23): };
12311:-23): $tries = 0;
12312:-23): if ($context eq 'domain') {
12313:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12314:-23): } else {
12315:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12316:-23): }
12317:-23): while (($gotlock ne 'ok') && ($tries<10)) {
12318:-23): $tries ++;
12319:-23): sleep (0.1);
12320:-23): if ($context eq 'domain') {
12321:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12322:-23): } else {
12323:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12324:-23): }
12325:-23): }
12326:-23): if ($gotlock eq 'ok') {
12327:-23): my %currids;
12328:-23): if ($context eq 'domain') {
12329:-23): %currids = &dump_dom('ltitools',$cdom);
12330:-23): } else {
12331:-23): %currids = &dump('ltitools',$cdom,$cnum);
12332:-23): }
12333:-23): if ($currids{'lock'}) {
12334:-23): delete($currids{'lock'});
12335:-23): if (keys(%currids)) {
12336:-23): my @curr = sort { $a <=> $b } keys(%currids);
12337:-23): if ($curr[-1] =~ /^\d+$/) {
12338:-23): $id = 1 + $curr[-1];
12339:-23): }
12340:-23): } else {
12341:-23): $id = 1;
12342:-23): }
12343:-23): if ($id) {
12344:-23): if ($context eq 'domain') {
12345:-23): unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12346:-23): $error = 'nostore';
12347:-23): }
12348:-23): } else {
12349:-23): unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12350:-23): $error = 'nostore';
12351:-23): }
12352:-23): }
12353:-23): } else {
12354:-23): $error = 'nonumber';
12355:-23): }
12356:-23): }
12357:-23): my $dellockoutcome;
12358:-23): if ($context eq 'domain') {
12359:-23): $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12360:-23): } else {
12361:-23): $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12362:-23): }
12363:-23): } else {
12364:-23): $error = 'nolock';
12365:-23): }
12366:-23): return ($id,$error);
12367:-23): }
12368:-23):
.9(raebu 12369:23): sub count_supptools {
12370:23): my ($cnum,$cdom,$ignorecache,$reload)=@_;
1.1172.2.31 raeburn 12371: my $hashid=$cnum.':'.$cdom;
1.1172.2.146. .9(raebu 12372:23): my ($numexttools,$cached);
1.1172.2.31 raeburn 12373: unless ($ignorecache) {
1.1172.2.146. .9(raebu 12374:23): ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
1.1172.2.31 raeburn 12375: }
12376: unless (defined($cached)) {
12377: my $chome=&homeserver($cnum,$cdom);
1.1172.2.146. .9(raebu 12378:23): $numexttools = 0;
1.1172.2.31 raeburn 12379: unless ($chome eq 'no_host') {
1.1172.2.146. .9(raebu 12380:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12381:23): if (ref($supplemental) eq 'HASH') {
12382:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12383:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12384:23): if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12385:23): $numexttools ++;
12386:23): }
12387:23): }
12388:23): }
12389:23): }
1.1172.2.31 raeburn 12390: }
1.1172.2.146. .9(raebu 12391:23): &do_cache_new('supptools',$hashid,$numexttools,600);
1.1172.2.31 raeburn 12392: }
1.1172.2.146. .9(raebu 12393:23): return $numexttools;
12394:23): }
12395:23):
12396:23): sub has_unhidden_suppfiles {
12397:23): my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12398:23): my $hashid=$cnum.':'.$cdom;
12399:23): my ($showsupp,$cached);
12400:23): unless ($ignorecache) {
12401:23): ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12402:23): }
12403:23): unless (defined($cached)) {
12404:23): my $chome=&homeserver($cnum,$cdom);
12405:23): unless ($chome eq 'no_host') {
12406:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12407:23): if (ref($supplemental) eq 'HASH') {
12408:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12409:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12410:23): next if ($key =~ /\.sequence$/);
12411:23): if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12412:23): foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12413:23): unless ($supplemental->{'hidden'}->{$id}) {
12414:23): $showsupp = 1;
12415:23): last;
12416:23): }
12417:23): }
12418:23): }
12419:23): last if ($showsupp);
12420:23): }
12421:23): }
12422:23): }
12423:23): }
12424:23): &do_cache_new('showsupp',$hashid,$showsupp,600);
12425:23): }
12426:23): return $showsupp;
1.1172.2.31 raeburn 12427: }
12428:
1.379 matthew 12429: #
12430: # EXT resource caching routines
12431: #
12432:
1.1172.2.146. .1(raebu 12433:22): {
12434:22): # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12435:22): #
12436:22): # The course for which we cache
12437:22): my $cachedmapkey='';
12438:22): # The cached recursive maps for this course
12439:22): my %cachedmaps=();
12440:22): # When this was last done
12441:22): my $cachedmaptime='';
12442:22):
1.379 matthew 12443: sub clear_EXT_cache_status {
1.383 albertel 12444: &delenv('cache.EXT.');
1.379 matthew 12445: }
12446:
12447: sub EXT_cache_status {
12448: my ($target_domain,$target_user) = @_;
1.383 albertel 12449: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 12450: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 12451: # We know already the user has no data
12452: return 1;
12453: } else {
12454: return 0;
12455: }
12456: }
12457:
12458: sub EXT_cache_set {
12459: my ($target_domain,$target_user) = @_;
1.383 albertel 12460: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 12461: #&appenv({$cachename => time});
1.379 matthew 12462: }
12463:
1.28 www 12464: # --------------------------------------------------------- Value of a Variable
1.58 www 12465: sub EXT {
1.715 albertel 12466:
1.1172.2.28 raeburn 12467: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 12468: unless ($varname) { return ''; }
1.218 albertel 12469: #get real user name/domain, courseid and symb
12470: my $courseid;
1.359 albertel 12471: my $publicuser;
1.427 www 12472: if ($symbparm) {
12473: $symbparm=&get_symb_from_alias($symbparm);
12474: }
1.218 albertel 12475: if (!($uname && $udom)) {
1.790 albertel 12476: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 12477: if (!$symbparm) { $symbparm=$cursymb; }
12478: } else {
1.620 albertel 12479: $courseid=$env{'request.course.id'};
1.218 albertel 12480: }
1.48 www 12481: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12482: my $rest;
1.320 albertel 12483: if (defined($therest[0])) {
1.48 www 12484: $rest=join('.',@therest);
12485: } else {
12486: $rest='';
12487: }
1.320 albertel 12488:
1.57 www 12489: my $qualifierrest=$qualifier;
12490: if ($rest) { $qualifierrest.='.'.$rest; }
12491: my $spacequalifierrest=$space;
12492: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 12493: if ($realm eq 'user') {
1.48 www 12494: # --------------------------------------------------------------- user.resource
12495: if ($space eq 'resource') {
1.651 albertel 12496: if ( (defined($Apache::lonhomework::parsing_a_problem)
12497: || defined($Apache::lonhomework::parsing_a_task))
12498: &&
1.1172.2.142 raeburn 12499: ($symbparm eq &symbread()) ) {
1.744 albertel 12500: # if we are in the middle of processing the resource the
12501: # get the value we are planning on committing
12502: if (defined($Apache::lonhomework::results{$qualifierrest})) {
12503: return $Apache::lonhomework::results{$qualifierrest};
12504: } else {
12505: return $Apache::lonhomework::history{$qualifierrest};
12506: }
1.335 albertel 12507: } else {
1.359 albertel 12508: my %restored;
1.620 albertel 12509: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 12510: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12511: } else {
12512: %restored=&restore($symbparm,$courseid,$udom,$uname);
12513: }
1.335 albertel 12514: return $restored{$qualifierrest};
12515: }
1.48 www 12516: # ----------------------------------------------------------------- user.access
12517: } elsif ($space eq 'access') {
1.218 albertel 12518: # FIXME - not supporting calls for a specific user
1.48 www 12519: return &allowed($qualifier,$rest);
12520: # ------------------------------------------ user.preferences, user.environment
12521: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 12522: if (($uname eq $env{'user.name'}) &&
12523: ($udom eq $env{'user.domain'})) {
12524: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 12525: } else {
1.359 albertel 12526: my %returnhash;
12527: if (!$publicuser) {
12528: %returnhash=&userenvironment($udom,$uname,
12529: $qualifierrest);
12530: }
1.218 albertel 12531: return $returnhash{$qualifierrest};
12532: }
1.48 www 12533: # ----------------------------------------------------------------- user.course
12534: } elsif ($space eq 'course') {
1.218 albertel 12535: # FIXME - not supporting calls for a specific user
1.620 albertel 12536: return $env{join('.',('request.course',$qualifier))};
1.48 www 12537: # ------------------------------------------------------------------- user.role
12538: } elsif ($space eq 'role') {
1.218 albertel 12539: # FIXME - not supporting calls for a specific user
1.620 albertel 12540: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 12541: if ($qualifier eq 'value') {
12542: return $role;
12543: } elsif ($qualifier eq 'extent') {
12544: return $where;
12545: }
12546: # ----------------------------------------------------------------- user.domain
12547: } elsif ($space eq 'domain') {
1.218 albertel 12548: return $udom;
1.48 www 12549: # ------------------------------------------------------------------- user.name
12550: } elsif ($space eq 'name') {
1.218 albertel 12551: return $uname;
1.48 www 12552: # ---------------------------------------------------- Any other user namespace
1.29 www 12553: } else {
1.359 albertel 12554: my %reply;
12555: if (!$publicuser) {
12556: %reply=&get($space,[$qualifierrest],$udom,$uname);
12557: }
12558: return $reply{$qualifierrest};
1.48 www 12559: }
1.236 www 12560: } elsif ($realm eq 'query') {
12561: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 12562: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12563: [$spacequalifierrest]);
1.620 albertel 12564: return $env{'form.'.$spacequalifierrest};
1.236 www 12565: } elsif ($realm eq 'request') {
1.48 www 12566: # ------------------------------------------------------------- request.browser
12567: if ($space eq 'browser') {
1.1145 bisitz 12568: return $env{'browser.'.$qualifier};
1.57 www 12569: # ------------------------------------------------------------ request.filename
12570: } else {
1.620 albertel 12571: return $env{'request.'.$spacequalifierrest};
1.29 www 12572: }
1.28 www 12573: } elsif ($realm eq 'course') {
1.48 www 12574: # ---------------------------------------------------------- course.description
1.620 albertel 12575: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 12576: } elsif ($realm eq 'resource') {
1.165 www 12577:
1.620 albertel 12578: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 12579: if (!$symbparm) { $symbparm=&symbread(); }
12580: }
1.693 albertel 12581:
1.1172.2.30 raeburn 12582: if ($qualifier eq '') {
12583: if ($space eq 'title') {
12584: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12585: return &gettitle($symbparm);
12586: }
1.693 albertel 12587:
1.1172.2.30 raeburn 12588: if ($space eq 'map') {
12589: my ($map) = &decode_symb($symbparm);
12590: return &symbread($map);
12591: }
12592: if ($space eq 'maptitle') {
12593: my ($map) = &decode_symb($symbparm);
12594: return &gettitle($map);
12595: }
12596: if ($space eq 'filename') {
12597: if ($symbparm) {
12598: return &clutter((&decode_symb($symbparm))[2]);
12599: }
12600: return &hreflocation('',$env{'request.filename'});
1.905 albertel 12601: }
1.1172.2.30 raeburn 12602:
12603: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12604: if ($space eq 'visibleparts') {
12605: my $navmap = Apache::lonnavmaps::navmap->new();
12606: my $item;
12607: if (ref($navmap)) {
12608: my $res = $navmap->getBySymb($symbparm);
12609: my $parts = $res->parts();
12610: if (ref($parts) eq 'ARRAY') {
12611: $item = join(',',@{$parts});
12612: }
12613: undef($navmap);
12614: }
12615: return $item;
12616: }
12617: }
12618: }
1.693 albertel 12619:
12620: my ($section, $group, @groups);
1.593 albertel 12621: my ($courselevelm,$courselevel);
1.1172.2.28 raeburn 12622: if (($courseid eq '') && ($cid)) {
12623: $courseid = $cid;
12624: }
12625: if (($symbparm && $courseid) &&
12626: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 12627:
1.218 albertel 12628: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 12629:
1.60 www 12630: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 12631: my $symbp=$symbparm;
1.735 albertel 12632: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 12633:
12634: my $symbparm=$symbp.'.'.$spacequalifierrest;
12635: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12636:
1.620 albertel 12637: if (($env{'user.name'} eq $uname) &&
12638: ($env{'user.domain'} eq $udom)) {
12639: $section=$env{'request.course.sec'};
1.733 raeburn 12640: @groups = split(/:/,$env{'request.course.groups'});
12641: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 12642: } else {
1.539 albertel 12643: if (! defined($usection)) {
1.551 albertel 12644: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 12645: } else {
12646: $section = $usection;
12647: }
1.733 raeburn 12648: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 12649: }
12650:
12651: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12652: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12653: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12654:
1.593 albertel 12655: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 12656: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 12657: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 12658:
1.60 www 12659: # ----------------------------------------------------------- first, check user
1.624 albertel 12660:
12661: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 12662: ([$courselevelr,'resource'],
12663: [$courselevelm,'map' ],
12664: [$courselevel, 'course' ]));
1.931 albertel 12665: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 12666:
1.594 albertel 12667: # ------------------------------------------------ second, check some of course
1.684 raeburn 12668: my $coursereply;
1.691 raeburn 12669: if (@groups > 0) {
12670: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12671: $mapparm,$spacequalifierrest);
1.927 albertel 12672: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 12673: }
1.96 www 12674:
1.684 raeburn 12675: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 12676: $env{'course.'.$courseid.'.domain'},
12677: 'course',
12678: ([$seclevelr, 'resource'],
12679: [$seclevelm, 'map' ],
12680: [$seclevel, 'course' ],
12681: [$courselevelr,'resource']));
12682: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 12683:
1.60 www 12684: # ------------------------------------------------------ third, check map parms
1.218 albertel 12685: my %parmhash=();
12686: my $thisparm='';
12687: if (tie(%parmhash,'GDBM_File',
1.620 albertel 12688: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 12689: &GDBM_READER(),0640)) {
1.218 albertel 12690: $thisparm=$parmhash{$symbparm};
12691: untie(%parmhash);
12692: }
1.927 albertel 12693: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 12694: }
1.594 albertel 12695: # ------------------------------------------ fourth, look in resource metadata
1.71 www 12696:
1.1172.2.146. .13(raeb 12697:-23): my $what = $spacequalifierrest;
12698:-23): $what=~s/\./\_/;
12699:-23): my $filename;
1.282 albertel 12700: if (!$symbparm) { $symbparm=&symbread(); }
12701: if ($symbparm) {
1.409 www 12702: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 12703: } else {
1.620 albertel 12704: $filename=$env{'request.filename'};
1.282 albertel 12705: }
1.1172.2.146. .13(raeb 12706:-23): my $toolsymb;
12707:-23): if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12708:-23): $toolsymb = $symbparm;
12709:-23): }
12710:-23): my $metadata=&metadata($filename,$what,$toolsymb);
1.927 albertel 12711: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1172.2.146. .13(raeb 12712:-23): $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927 albertel 12713: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 12714:
1.1172.2.146. .13(raeb 12715:-23): # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 12716: if ($symbparm && defined($courseid) &&
1.620 albertel 12717: $courseid eq $env{'request.course.id'}) {
1.624 albertel 12718: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12719: $env{'course.'.$courseid.'.domain'},
12720: 'course',
1.927 albertel 12721: ([$courselevelm,'map' ],
12722: [$courselevel, 'course']));
12723: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 12724: }
1.145 www 12725: # ------------------------------------------------------------------ Cascade up
1.218 albertel 12726: unless ($space eq '0') {
1.336 albertel 12727: my @parts=split(/_/,$space);
12728: my $id=pop(@parts);
12729: my $part=join('_',@parts);
12730: if ($part eq '') { $part='0'; }
1.927 albertel 12731: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 12732: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 12733: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 12734: }
1.395 albertel 12735: if ($recurse) { return undef; }
1.1172.2.146. .13(raeb 12736:-23): my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927 albertel 12737: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 12738: # ---------------------------------------------------- Any other user namespace
12739: } elsif ($realm eq 'environment') {
12740: # ----------------------------------------------------------------- environment
1.620 albertel 12741: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12742: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 12743: } else {
1.770 albertel 12744: if ($uname eq 'anonymous' && $udom eq '') {
12745: return '';
12746: }
1.219 albertel 12747: my %returnhash=&userenvironment($udom,$uname,
12748: $spacequalifierrest);
12749: return $returnhash{$spacequalifierrest};
12750: }
1.28 www 12751: } elsif ($realm eq 'system') {
1.48 www 12752: # ----------------------------------------------------------------- system.time
12753: if ($space eq 'time') {
12754: return time;
12755: }
1.696 albertel 12756: } elsif ($realm eq 'server') {
12757: # ----------------------------------------------------------------- system.time
12758: if ($space eq 'name') {
12759: return $ENV{'SERVER_NAME'};
12760: }
1.1172.2.146. .1(raebu 12761:22): } elsif ($realm eq 'client') {
12762:22): if ($space eq 'remote_addr') {
12763:22): return &get_requestor_ip();
12764:22): }
1.28 www 12765: }
1.48 www 12766: return '';
1.61 www 12767: }
12768:
1.927 albertel 12769: sub get_reply {
12770: my ($reply_value) = @_;
1.940 raeburn 12771: if (ref($reply_value) eq 'ARRAY') {
12772: if (wantarray) {
12773: return @$reply_value;
12774: }
12775: return $reply_value->[0];
12776: } else {
12777: return $reply_value;
1.927 albertel 12778: }
12779: }
12780:
1.691 raeburn 12781: sub check_group_parms {
12782: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
12783: my @groupitems = ();
12784: my $resultitem;
1.927 albertel 12785: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 12786: foreach my $group (@{$groups}) {
12787: foreach my $level (@levels) {
1.927 albertel 12788: my $item = $courseid.'.['.$group.'].'.$level->[0];
12789: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 12790: }
12791: }
12792: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12793: $env{'course.'.$courseid.'.domain'},
12794: 'course',@groupitems);
12795: return $coursereply;
12796: }
12797:
1.1172.2.146. .1(raebu 12798:22): sub get_map_hierarchy {
12799:22): my ($mapname,$courseid) = @_;
12800:22): my @recurseup = ();
12801:22): if ($mapname) {
12802:22): if (($cachedmapkey eq $courseid) &&
12803:22): (abs($cachedmaptime-time)<5)) {
12804:22): if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12805:22): return @{$cachedmaps{$mapname}};
12806:22): }
12807:22): }
12808:22): my $navmap = Apache::lonnavmaps::navmap->new();
12809:22): if (ref($navmap)) {
12810:22): @recurseup = $navmap->recurseup_maps($mapname);
12811:22): undef($navmap);
12812:22): $cachedmaps{$mapname} = \@recurseup;
12813:22): $cachedmaptime=time;
12814:22): $cachedmapkey=$courseid;
12815:22): }
12816:22): }
12817:22): return @recurseup;
12818:22): }
12819:22):
.2(raebu 12820:22): }
.1(raebu 12821:22):
1.691 raeburn 12822: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 12823: my ($courseid,@groups) = @_;
12824: @groups = sort(@groups);
1.691 raeburn 12825: return @groups;
12826: }
12827:
1.395 albertel 12828: sub packages_tab_default {
1.1172.2.146. .13(raeb 12829:-23): my ($uri,$varname,$toolsymb)=@_;
1.395 albertel 12830: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 12831:
12832: my (@extension,@specifics,$do_default);
1.1172.2.146. .13(raeb 12833:-23): foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395 albertel 12834: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 12835: if ($pack_type eq 'default') {
12836: $do_default=1;
12837: } elsif ($pack_type eq 'extension') {
12838: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 12839: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 12840: # only look at packages defaults for packages that this id is
1.738 albertel 12841: push(@specifics,[$package,$pack_type,$pack_part]);
12842: }
12843: }
12844: # first look for a package that matches the requested part id
12845: foreach my $package (@specifics) {
12846: my (undef,$pack_type,$pack_part)=@{$package};
12847: next if ($pack_part ne $part);
12848: if (defined($packagetab{"$pack_type&$name&default"})) {
12849: return $packagetab{"$pack_type&$name&default"};
12850: }
12851: }
12852: # look for any possible matching non extension_ package
12853: foreach my $package (@specifics) {
12854: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 12855: if (defined($packagetab{"$pack_type&$name&default"})) {
12856: return $packagetab{"$pack_type&$name&default"};
12857: }
1.585 albertel 12858: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 12859: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12860: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 12861: }
12862: }
1.738 albertel 12863: # look for any posible extension_ match
12864: foreach my $package (@extension) {
12865: my ($package,$pack_type)=@{$package};
12866: if (defined($packagetab{"$pack_type&$name&default"})) {
12867: return $packagetab{"$pack_type&$name&default"};
12868: }
12869: if (defined($packagetab{$package."&$name&default"})) {
12870: return $packagetab{$package."&$name&default"};
12871: }
12872: }
12873: # look for a global default setting
12874: if ($do_default && defined($packagetab{"default&$name&default"})) {
12875: return $packagetab{"default&$name&default"};
12876: }
1.395 albertel 12877: return undef;
12878: }
12879:
1.334 albertel 12880: sub add_prefix_and_part {
12881: my ($prefix,$part)=@_;
12882: my $keyroot;
12883: if (defined($prefix) && $prefix !~ /^__/) {
12884: # prefix that has a part already
12885: $keyroot=$prefix;
12886: } elsif (defined($prefix)) {
12887: # prefix that is missing a part
12888: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12889: } else {
12890: # no prefix at all
12891: if (defined($part)) { $keyroot='_'.$part; }
12892: }
12893: return $keyroot;
12894: }
12895:
1.71 www 12896: # ---------------------------------------------------------------- Get metadata
12897:
1.599 albertel 12898: my %metaentry;
1.1070 www 12899: my %importedpartids;
1.1172.2.99 raeburn 12900: my %importedrespids;
1.71 www 12901: sub metadata {
1.1172.2.146. .13(raeb 12902:-23): my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71 www 12903: $uri=&declutter($uri);
1.288 albertel 12904: # if it is a non metadata possible uri return quickly
1.529 albertel 12905: if (($uri eq '') ||
12906: (($uri =~ m|^/*adm/|) &&
1.1172.2.146. .1(raebu 12907:22): ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 12908: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 12909: return undef;
12910: }
1.1172.2.49 raeburn 12911: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 12912: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 12913: return undef;
1.288 albertel 12914: }
1.73 www 12915: my $filename=$uri;
12916: $uri=~s/\.meta$//;
1.172 www 12917: #
12918: # Is the metadata already cached?
1.177 www 12919: # Look at timestamp of caching
1.172 www 12920: # Everything is cached by the main uri, libraries are never directly cached
12921: #
1.428 albertel 12922: if (!defined($liburi)) {
1.599 albertel 12923: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 12924: if (defined($cached)) { return $result->{':'.$what}; }
12925: }
1.1172.2.146. .13(raeb 12926:-23):
12927:-23): #
12928:-23): # If the uri is for an external tool the file from
12929:-23): # which metadata should be retrieved depends on whether
12930:-23): # the tool had been configured to be gradable (set in the Course
12931:-23): # Editor or Resource Editor).
12932:-23): #
12933:-23): # If a valid symb has been included as the third arg in the call
12934:-23): # to &metadata() that can be used to retrieve the value of
12935:-23): # parameter_0_gradable set for the resource, and included in the
12936:-23): # uploaded map containing the tool. The value is retrieved via
12937:-23): # &EXT(), if a valid symb is available. Otherwise the value of
12938:-23): # gradable in the exttool_$marker.db file for the tool instance
12939:-23): # is retrieved via &get().
12940:-23): #
12941:-23): # When lonuserstate::traceroute() calls lonnet::EXT() for
12942:-23): # hiddenresource and encrypturl (during course initialization)
12943:-23): # the map-level parameter for resource.0.gradable included in the
12944:-23): # uploaded map containing the tool will not yet have been stored
12945:-23): # in the user_course_parms.db file for the user's session, so in
12946:-23): # this case fall back to retrieving gradable status from the
12947:-23): # exttool_$marker.db file.
12948:-23): #
12949:-23): # In order to avoid an infinite loop, &metadata() will return
12950:-23): # before a call to &EXT(), if the uri is for an external tool
12951:-23): # and the $what for which metadata is being requested is
12952:-23): # parameter_0_gradable or 0_gradable.
12953:-23): #
12954:-23):
12955:-23): if ($uri =~ /ext\.tool$/) {
12956:-23): if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12957:-23): return;
12958:-23): } else {
12959:-23): my ($checked,$use_passback);
12960:-23): if ($toolsymb ne '') {
12961:-23): (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12962:-23): if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12963:-23): $checked = 1;
12964:-23): if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12965:-23): $use_passback = 1;
12966:-23): }
12967:-23): }
12968:-23): }
12969:-23): unless ($checked) {
12970:-23): my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12971:-23): $marker=~s/\D//g;
12972:-23): if ($marker) {
12973:-23): my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12974:-23): $use_passback = $toolsettings{'gradable'};
12975:-23): }
12976:-23): }
12977:-23): if ($use_passback) {
12978:-23): $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12979:-23): } else {
12980:-23): $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12981:-23): }
12982:-23): }
12983:-23): }
12984:-23):
1.428 albertel 12985: {
1.1069 www 12986: # Imported parts would go here
1.1172.2.99 raeburn 12987: my @origfiletagids=();
1.1069 www 12988: my $importedparts=0;
1.1172.2.99 raeburn 12989:
12990: # Imported responseids would go here
12991: my $importedresponses=0;
1.172 www 12992: #
12993: # Is this a recursive call for a library?
12994: #
1.599 albertel 12995: # if (! exists($metacache{$uri})) {
12996: # $metacache{$uri}={};
12997: # }
1.924 albertel 12998: my $cachetime = 60*60;
1.171 www 12999: if ($liburi) {
13000: $liburi=&declutter($liburi);
13001: $filename=$liburi;
1.401 bowersj2 13002: } else {
1.599 albertel 13003: &devalidate_cache_new('meta',$uri);
13004: undef(%metaentry);
1.401 bowersj2 13005: }
1.140 www 13006: my %metathesekeys=();
1.73 www 13007: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 13008: my $metastring;
1.1140 www 13009: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 13010: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 13011: $metastring =
1.929 albertel 13012: &Apache::lonnet::ssi_body($which,
1.924 albertel 13013: ('grade_target' => 'meta'));
13014: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 13015: } elsif (($uri !~ m -^(editupload)/-) &&
13016: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 13017: my $file=&filelocation('',&clutter($filename));
1.599 albertel 13018: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 13019: $metastring=&getfile($file);
1.489 albertel 13020: }
1.208 albertel 13021: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 13022: my $token;
1.140 www 13023: undef %metathesekeys;
1.71 www 13024: while ($token=$parser->get_token) {
1.339 albertel 13025: if ($token->[0] eq 'S') {
13026: if (defined($token->[2]->{'package'})) {
1.172 www 13027: #
13028: # This is a package - get package info
13029: #
1.339 albertel 13030: my $package=$token->[2]->{'package'};
13031: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13032: if (defined($token->[2]->{'id'})) {
13033: $keyroot.='_'.$token->[2]->{'id'};
13034: }
1.599 albertel 13035: if ($metaentry{':packages'}) {
13036: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 13037: } else {
1.599 albertel 13038: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 13039: }
1.736 albertel 13040: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 13041: my $part=$keyroot;
13042: $part=~s/^\_//;
1.736 albertel 13043: if ($pack_entry=~/^\Q$package\E\&/ ||
13044: $pack_entry=~/^\Q$package\E_0\&/) {
13045: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 13046: # ignore package.tab specified default values
13047: # here &package_tab_default() will fetch those
13048: if ($subp eq 'default') { next; }
1.736 albertel 13049: my $value=$packagetab{$pack_entry};
1.432 albertel 13050: my $unikey;
13051: if ($pack =~ /_0$/) {
13052: $unikey='parameter_0_'.$name;
13053: $part=0;
13054: } else {
13055: $unikey='parameter'.$keyroot.'_'.$name;
13056: }
1.339 albertel 13057: if ($subp eq 'display') {
13058: $value.=' [Part: '.$part.']';
13059: }
1.599 albertel 13060: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 13061: $metathesekeys{$unikey}=1;
1.599 albertel 13062: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13063: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 13064: }
1.599 albertel 13065: if (defined($metaentry{':'.$unikey.'.default'})) {
13066: $metaentry{':'.$unikey}=
13067: $metaentry{':'.$unikey.'.default'};
1.356 albertel 13068: }
1.339 albertel 13069: }
13070: }
13071: } else {
1.172 www 13072: #
13073: # This is not a package - some other kind of start tag
1.339 albertel 13074: #
13075: my $entry=$token->[1];
1.1068 www 13076: my $unikey='';
1.175 www 13077:
1.339 albertel 13078: if ($entry eq 'import') {
1.175 www 13079: #
13080: # Importing a library here
1.339 albertel 13081: #
1.1067 www 13082: my $location=$parser->get_text('/import');
13083: my $dir=$filename;
13084: $dir=~s|[^/]*$||;
13085: $location=&filelocation($dir,$location);
1.1172.2.99 raeburn 13086:
13087: my $importid=$token->[2]->{'id'};
1.1068 www 13088: my $importmode=$token->[2]->{'importmode'};
1.1172.2.99 raeburn 13089: #
13090: # Check metadata for imported file to
13091: # see if it contained response items
13092: #
13093: my %currmetaentry = %metaentry;
13094: my $libresponseorder = &metadata($location,'responseorder');
13095: my $origfile;
13096: if ($libresponseorder ne '') {
13097: if ($#origfiletagids<0) {
13098: undef(%importedrespids);
13099: undef(%importedpartids);
13100: }
13101: @{$importedrespids{$importid}} = split(/\s*,\s*/,$libresponseorder);
13102: if (@{$importedrespids{$importid}} > 0) {
13103: $importedresponses = 1;
13104: # We need to get the original file and the imported file to get the response order correct
13105: # Load and inspect original file
13106: if ($#origfiletagids<0) {
13107: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13108: $origfile=&getfile($origfilelocation);
13109: @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13110: }
13111: }
13112: }
13113: # Do not overwrite contents of %metaentry hash for resource itself with
13114: # hash populated for imported library file
13115: %metaentry = %currmetaentry;
13116: undef(%currmetaentry);
1.1068 www 13117: if ($importmode eq 'problem') {
1.1069 www 13118: # Import as problem/response
1.1068 www 13119: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13120: } elsif ($importmode eq 'part') {
13121: # Import as part(s)
1.1069 www 13122: $importedparts=1;
13123: # We need to get the original file and the imported file to get the part order correct
13124: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1172.2.99 raeburn 13125: # Load and inspect original file if we didn't do that already
13126: if ($#origfiletagids<0) {
13127: undef(%importedrespids);
13128: undef(%importedpartids);
13129: if ($origfile eq '') {
13130: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13131: $origfile=&getfile($origfilelocation);
13132: @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13133: }
1.1070 www 13134: }
13135:
1.1069 www 13136: # Load and inspect imported file
13137: my $impfile=&getfile($location);
13138: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13139: if ($#impfilepartids>=0) {
13140: # This problem had parts
1.1070 www 13141: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 13142: } else {
13143: # Importing by turning a single problem into a problem part
13144: # It gets the import-tags ID as part-ID
13145: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 13146: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 13147: }
1.1068 www 13148: } else {
13149: # Normal import
13150: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13151: if (defined($token->[2]->{'id'})) {
13152: $unikey.='_'.$token->[2]->{'id'};
13153: }
1.1067 www 13154: }
13155:
1.339 albertel 13156: if ($depthcount<20) {
1.736 albertel 13157: my $metadata =
1.1172.2.146. .13(raeb 13158:-23): &metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736 albertel 13159: $depthcount+1);
13160: foreach my $meta (split(',',$metadata)) {
13161: $metaentry{':'.$meta}=$metaentry{':'.$meta};
13162: $metathesekeys{$meta}=1;
1.339 albertel 13163: }
1.1068 www 13164:
13165: }
1.1067 www 13166: } else {
13167: #
13168: # Not importing, some other kind of non-package, non-library start tag
13169: #
13170: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13171: if (defined($token->[2]->{'id'})) {
13172: $unikey.='_'.$token->[2]->{'id'};
13173: }
1.339 albertel 13174: if (defined($token->[2]->{'name'})) {
13175: $unikey.='_'.$token->[2]->{'name'};
13176: }
13177: $metathesekeys{$unikey}=1;
1.736 albertel 13178: foreach my $param (@{$token->[3]}) {
13179: $metaentry{':'.$unikey.'.'.$param} =
13180: $token->[2]->{$param};
1.339 albertel 13181: }
13182: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 13183: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 13184: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13185: # only ws inside the tag, and not in default, so use default
13186: # as value
1.599 albertel 13187: $metaentry{':'.$unikey}=$default;
1.908 albertel 13188: } elsif ( $internaltext =~ /\S/ ) {
13189: # something interesting inside the tag
13190: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 13191: } else {
1.908 albertel 13192: # no interesting values, don't set a default
1.339 albertel 13193: }
1.172 www 13194: # end of not-a-package not-a-library import
1.339 albertel 13195: }
1.172 www 13196: # end of not-a-package start tag
1.339 albertel 13197: }
1.172 www 13198: # the next is the end of "start tag"
1.339 albertel 13199: }
13200: }
1.483 albertel 13201: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 13202: $extension = lc($extension);
13203: if ($extension eq 'htm') { $extension='html'; }
13204:
1.737 albertel 13205: foreach my $key (keys(%packagetab)) {
1.483 albertel 13206: #no specific packages #how's our extension
13207: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 13208: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 13209: \%metathesekeys);
13210: }
1.883 albertel 13211:
13212: if (!exists($metaentry{':packages'})
13213: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 13214: foreach my $key (keys(%packagetab)) {
1.483 albertel 13215: #no specific packages well let's get default then
13216: if ($key!~/^default&/) { next; }
1.488 albertel 13217: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 13218: \%metathesekeys);
13219: }
13220: }
1.338 www 13221: # are there custom rights to evaluate
1.599 albertel 13222: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 13223:
1.338 www 13224: #
13225: # Importing a rights file here
1.339 albertel 13226: #
13227: unless ($depthcount) {
1.599 albertel 13228: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 13229: my $dir=$filename;
13230: $dir=~s|[^/]*$||;
13231: $location=&filelocation($dir,$location);
1.736 albertel 13232: my $rights_metadata =
1.1172.2.146. .13(raeb 13233:-23): &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736 albertel 13234: $depthcount+1);
13235: foreach my $rights (split(',',$rights_metadata)) {
13236: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13237: $metathesekeys{$rights}=1;
1.339 albertel 13238: }
13239: }
13240: }
1.737 albertel 13241: # uniqifiy package listing
13242: my %seen;
13243: my @uniq_packages =
13244: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13245: $metaentry{':packages'} = join(',',@uniq_packages);
13246:
1.1172.2.99 raeburn 13247: if (($importedresponses) || ($importedparts)) {
13248: if ($importedparts) {
1.1070 www 13249: # We had imported parts and need to rebuild partorder
1.1172.2.99 raeburn 13250: $metaentry{':partorder'}='';
13251: $metathesekeys{'partorder'}=1;
13252: }
13253: if ($importedresponses) {
13254: # We had imported responses and need to rebuild responseorder
13255: $metaentry{':responseorder'}='';
13256: $metathesekeys{'responseorder'}=1;
13257: }
13258: for (my $index=0;$index<$#origfiletagids;$index+=2) {
13259: my $origid = $origfiletagids[$index+1];
13260: if ($origfiletagids[$index] eq 'part') {
13261: # Original part, part of the problem
13262: if ($importedparts) {
13263: $metaentry{':partorder'}.=','.$origid;
13264: }
13265: } elsif ($origfiletagids[$index] eq 'import') {
13266: if ($importedparts) {
13267: # We have imported parts at this position
13268: $metaentry{':partorder'}.=','.$importedpartids{$origid};
13269: }
13270: if ($importedresponses) {
13271: # We have imported responses at this position
13272: if (ref($importedrespids{$origid}) eq 'ARRAY') {
13273: $metaentry{':responseorder'}.=','.join(',',map { $origid.'_'.$_ } @{$importedrespids{$origid}});
13274: }
13275: }
13276: } else {
13277: # Original response item, part of the problem
13278: if ($importedresponses) {
13279: $metaentry{':responseorder'}.=','.$origid;
13280: }
13281: }
13282: }
13283: if ($importedparts) {
13284: $metaentry{':partorder'}=~s/^\,//;
13285: }
13286: if ($importedresponses) {
13287: $metaentry{':responseorder'}=~s/^\,//;
13288: }
1.1070 www 13289: }
13290:
1.737 albertel 13291: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 13292: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1172.2.58 raeburn 13293: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 13294: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 13295: # this is the end of "was not already recently cached
1.71 www 13296: }
1.599 albertel 13297: return $metaentry{':'.$what};
1.261 albertel 13298: }
13299:
1.488 albertel 13300: sub metadata_create_package_def {
1.483 albertel 13301: my ($uri,$key,$package,$metathesekeys)=@_;
13302: my ($pack,$name,$subp)=split(/\&/,$key);
13303: if ($subp eq 'default') { next; }
13304:
1.599 albertel 13305: if (defined($metaentry{':packages'})) {
13306: $metaentry{':packages'}.=','.$package;
1.483 albertel 13307: } else {
1.599 albertel 13308: $metaentry{':packages'}=$package;
1.483 albertel 13309: }
13310: my $value=$packagetab{$key};
13311: my $unikey;
13312: $unikey='parameter_0_'.$name;
1.599 albertel 13313: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 13314: $$metathesekeys{$unikey}=1;
1.599 albertel 13315: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13316: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 13317: }
1.599 albertel 13318: if (defined($metaentry{':'.$unikey.'.default'})) {
13319: $metaentry{':'.$unikey}=
13320: $metaentry{':'.$unikey.'.default'};
1.483 albertel 13321: }
13322: }
13323:
1.261 albertel 13324: sub metadata_generate_part0 {
13325: my ($metadata,$metacache,$uri) = @_;
13326: my %allnames;
1.737 albertel 13327: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 13328: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 13329: my $part=$$metacache{':'.$metakey.'.part'};
13330: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 13331: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 13332: $allnames{$name}=$part;
13333: }
13334: }
13335: }
13336: foreach my $name (keys(%allnames)) {
13337: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 13338: my $key=":parameter_0_$name";
1.261 albertel 13339: $$metacache{"$key.part"}='0';
13340: $$metacache{"$key.name"}=$name;
1.428 albertel 13341: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 13342: $allnames{$name}.'_'.$name.
13343: '.type'};
1.428 albertel 13344: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 13345: '.display'};
1.644 www 13346: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 13347: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 13348: $$metacache{"$key.display"}=$olddis;
13349: }
1.71 www 13350: }
13351:
1.764 albertel 13352: # ------------------------------------------------------ Devalidate title cache
13353:
13354: sub devalidate_title_cache {
13355: my ($url)=@_;
13356: if (!$env{'request.course.id'}) { return; }
13357: my $symb=&symbread($url);
13358: if (!$symb) { return; }
13359: my $key=$env{'request.course.id'}."\0".$symb;
13360: &devalidate_cache_new('title',$key);
13361: }
13362:
1.1014 droeschl 13363: # ------------------------------------------------- Get the title of a course
13364:
13365: sub current_course_title {
13366: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13367: }
1.301 www 13368: # ------------------------------------------------- Get the title of a resource
13369:
13370: sub gettitle {
13371: my $urlsymb=shift;
13372: my $symb=&symbread($urlsymb);
1.534 albertel 13373: if ($symb) {
1.620 albertel 13374: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 13375: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 13376: if (defined($cached)) {
13377: return $result;
13378: }
1.534 albertel 13379: my ($map,$resid,$url)=&decode_symb($symb);
13380: my $title='';
1.907 albertel 13381: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13382: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13383: } else {
13384: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13385: &GDBM_READER(),0640)) {
13386: my $mapid=$bighash{'map_pc_'.&clutter($map)};
13387: $title=$bighash{'title_'.$mapid.'.'.$resid};
13388: untie(%bighash);
13389: }
1.534 albertel 13390: }
13391: $title=~s/\&colon\;/\:/gs;
13392: if ($title) {
1.1159 www 13393: # Remember both $symb and $title for dynamic metadata
13394: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 13395: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 13396: # Cache this title and then return it
1.599 albertel 13397: return &do_cache_new('title',$key,$title,600);
1.534 albertel 13398: }
13399: $urlsymb=$url;
13400: }
13401: my $title=&metadata($urlsymb,'title');
13402: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
13403: return $title;
1.301 www 13404: }
1.613 albertel 13405:
1.614 albertel 13406: sub get_slot {
13407: my ($which,$cnum,$cdom)=@_;
13408: if (!$cnum || !$cdom) {
1.790 albertel 13409: (undef,my $courseid)=&whichuser();
1.620 albertel 13410: $cdom=$env{'course.'.$courseid.'.domain'};
13411: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 13412: }
1.703 albertel 13413: my $key=join("\0",'slots',$cdom,$cnum,$which);
13414: my %slotinfo;
13415: if (exists($remembered{$key})) {
13416: $slotinfo{$which} = $remembered{$key};
13417: } else {
13418: %slotinfo=&get('slots',[$which],$cdom,$cnum);
13419: &Apache::lonhomework::showhash(%slotinfo);
13420: my ($tmp)=keys(%slotinfo);
13421: if ($tmp=~/^error:/) { return (); }
13422: $remembered{$key} = $slotinfo{$which};
13423: }
1.616 albertel 13424: if (ref($slotinfo{$which}) eq 'HASH') {
13425: return %{$slotinfo{$which}};
13426: }
13427: return $slotinfo{$which};
1.614 albertel 13428: }
1.1150 raeburn 13429:
13430: sub get_reservable_slots {
13431: my ($cnum,$cdom,$uname,$udom) = @_;
13432: my $now = time;
13433: my $reservable_info;
13434: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13435: if (exists($remembered{$key})) {
13436: $reservable_info = $remembered{$key};
13437: } else {
13438: my %resv;
13439: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13440: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13441: $reservable_info = \%resv;
13442: $remembered{$key} = $reservable_info;
13443: }
13444: return $reservable_info;
13445: }
13446:
13447: sub get_course_slots {
13448: my ($cnum,$cdom) = @_;
13449: my $hashid=$cnum.':'.$cdom;
13450: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13451: if (defined($cached)) {
13452: if (ref($result) eq 'HASH') {
13453: return %{$result};
13454: }
13455: } else {
13456: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13457: my ($tmp) = keys(%slots);
13458: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1172.2.23 raeburn 13459: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 13460: return %slots;
13461: }
13462: }
13463: return;
13464: }
13465:
13466: sub devalidate_slots_cache {
13467: my ($cnum,$cdom)=@_;
13468: my $hashid=$cnum.':'.$cdom;
13469: &devalidate_cache_new('allslots',$hashid);
13470: }
13471:
1.1172.2.8 raeburn 13472: sub get_coursechange {
13473: my ($cdom,$cnum) = @_;
13474: if ($cdom eq '' || $cnum eq '') {
13475: return unless ($env{'request.course.id'});
13476: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13477: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13478: }
13479: my $hashid=$cdom.'_'.$cnum;
13480: my ($change,$cached)=&is_cached_new('crschange',$hashid);
13481: if ((defined($cached)) && ($change ne '')) {
13482: return $change;
13483: } else {
13484: my %crshash;
13485: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13486: if ($crshash{'internal.contentchange'} eq '') {
13487: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13488: if ($change eq '') {
13489: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13490: $change = $crshash{'internal.created'};
13491: }
13492: } else {
13493: $change = $crshash{'internal.contentchange'};
13494: }
13495: my $cachetime = 600;
13496: &do_cache_new('crschange',$hashid,$change,$cachetime);
13497: }
13498: return $change;
13499: }
13500:
13501: sub devalidate_coursechange_cache {
1.1172.2.146. .9(raebu 13502:23): my ($cdom,$cnum)=@_;
13503:23): my $hashid=$cdom.'_'.$cnum;
1.1172.2.8 raeburn 13504: &devalidate_cache_new('crschange',$hashid);
13505: }
13506:
1.1172.2.146. .9(raebu 13507:23): sub get_suppchange {
13508:23): my ($cdom,$cnum) = @_;
13509:23): if ($cdom eq '' || $cnum eq '') {
13510:23): return unless ($env{'request.course.id'});
13511:23): $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13512:23): $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13513:23): }
13514:23): my $hashid=$cdom.'_'.$cnum;
13515:23): my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13516:23): if ((defined($cached)) && ($change ne '')) {
13517:23): return $change;
13518:23): } else {
13519:23): my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13520:23): if ($crshash{'internal.supplementalchange'} eq '') {
13521:23): $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13522:23): if ($change eq '') {
13523:23): %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13524:23): $change = $crshash{'internal.created'};
13525:23): }
13526:23): } else {
13527:23): $change = $crshash{'internal.supplementalchange'};
13528:23): }
13529:23): my $cachetime = 600;
13530:23): &do_cache_new('suppchange',$hashid,$change,$cachetime);
13531:23): }
13532:23): return $change;
13533:23): }
13534:23):
13535:23): sub devalidate_suppchange_cache {
13536:23): my ($cdom,$cnum)=@_;
13537:23): my $hashid=$cdom.'_'.$cnum;
13538:23): &devalidate_cache_new('suppchange',$hashid);
13539:23): }
13540:23):
13541:23): sub update_supp_caches {
13542:23): my ($cdom,$cnum) = @_;
13543:23): my %servers = &internet_dom_servers($cdom);
13544:23): my @ids=¤t_machine_ids();
13545:23): foreach my $server (keys(%servers)) {
13546:23): next if (grep(/^\Q$server\E$/,@ids));
13547:23): my $hashid=$cnum.':'.$cdom;
13548:23): my $cachekey = &escape('showsupp').':'.&escape($hashid);
13549:23): &remote_devalidate_cache($server,[$cachekey]);
13550:23): }
13551:23): &has_unhidden_suppfiles($cnum,$cdom,1,1);
13552:23): &count_supptools($cnum,$cdom,1);
13553:23): my $now = time;
13554:23): if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13555:23): &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13556:23): }
13557:23): &put('environment',{'internal.supplementalchange' => $now},
13558:23): $cdom,$cnum);
13559:23): &Apache::lonnet::appenv(
13560:23): {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13561:23): &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13562:23): }
13563:23):
1.31 www 13564: # ------------------------------------------------- Update symbolic store links
13565:
13566: sub symblist {
13567: my ($mapname,%newhash)=@_;
1.438 www 13568: $mapname=&deversion(&declutter($mapname));
1.31 www 13569: my %hash;
1.620 albertel 13570: if (($env{'request.course.fn'}) && (%newhash)) {
13571: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 13572: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 13573: foreach my $url (keys(%newhash)) {
1.711 albertel 13574: next if ($url eq 'last_known'
13575: && $env{'form.no_update_last_known'});
13576: $hash{declutter($url)}=&encode_symb($mapname,
13577: $newhash{$url}->[1],
13578: $newhash{$url}->[0]);
1.191 harris41 13579: }
1.31 www 13580: if (untie(%hash)) {
13581: return 'ok';
13582: }
13583: }
13584: }
13585: return 'error';
1.212 www 13586: }
13587:
13588: # --------------------------------------------------------------- Verify a symb
13589:
13590: sub symbverify {
1.1172.2.11 raeburn 13591: my ($symb,$thisurl,$encstate)=@_;
1.510 www 13592: my $thisfn=$thisurl;
1.439 www 13593: $thisfn=&declutter($thisfn);
1.215 www 13594: # direct jump to resource in page or to a sequence - will construct own symbs
13595: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13596: # check URL part
1.409 www 13597: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 13598:
1.431 www 13599: unless ($url eq $thisfn) { return 0; }
1.213 www 13600:
1.216 www 13601: $symb=&symbclean($symb);
1.510 www 13602: $thisurl=&deversion($thisurl);
1.439 www 13603: $thisfn=&deversion($thisfn);
1.213 www 13604:
13605: my %bighash;
13606: my $okay=0;
1.431 www 13607:
1.620 albertel 13608: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13609: &GDBM_READER(),0640)) {
1.1032 raeburn 13610: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13611: $thisurl =~ s/\?.+$//;
1.1172.2.13 raeburn 13612: if ($map =~ m{^uploaded/.+\.page$}) {
13613: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13614: $thisurl =~ s{^\Qhttp://https://\E}{https://};
13615: }
13616: }
13617: my $ids;
1.1172.2.122 raeburn 13618: if ($map =~ m{^uploaded/.+\.page$}) {
13619: $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1172.2.13 raeburn 13620: } else {
13621: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 13622: }
1.1102 raeburn 13623: unless ($ids) {
1.1172.2.13 raeburn 13624: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
1.1102 raeburn 13625: $ids=$bighash{$idkey};
1.216 www 13626: }
13627: if ($ids) {
13628: # ------------------------------------------------------------------- Has ID(s)
1.1172.2.13 raeburn 13629: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13630: $symb =~ s/\?.+$//;
13631: }
1.800 albertel 13632: foreach my $id (split(/\,/,$ids)) {
13633: my ($mapid,$resid)=split(/\./,$id);
1.216 www 13634: if (
13635: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1172.2.13 raeburn 13636: eq $symb) {
1.1172.2.11 raeburn 13637: if (ref($encstate)) {
13638: $$encstate = $bighash{'encrypted_'.$id};
13639: }
1.1172.2.13 raeburn 13640: if (($env{'request.role.adv'}) ||
13641: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
1.1101 raeburn 13642: ($thisurl eq '/adm/navmaps')) {
1.1172.2.13 raeburn 13643: $okay=1;
13644: last;
13645: }
13646: }
13647: }
1.216 www 13648: }
1.213 www 13649: untie(%bighash);
13650: }
13651: return $okay;
1.31 www 13652: }
13653:
1.210 www 13654: # --------------------------------------------------------------- Clean-up symb
13655:
13656: sub symbclean {
13657: my $symb=shift;
1.568 albertel 13658: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 13659: # remove version from map
13660: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 13661:
1.210 www 13662: # remove version from URL
13663: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 13664:
1.507 www 13665: # remove wrapper
13666:
1.510 www 13667: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 13668: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 13669: return $symb;
1.409 www 13670: }
13671:
13672: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 13673:
13674: sub encode_symb {
13675: my ($map,$resid,$url)=@_;
13676: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13677: }
1.409 www 13678:
13679: sub decode_symb {
1.568 albertel 13680: my $symb=shift;
13681: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13682: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 13683: return (&fixversion($map),$resid,&fixversion($url));
13684: }
13685:
13686: sub fixversion {
13687: my $fn=shift;
1.609 banghart 13688: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 13689: my %bighash;
13690: my $uri=&clutter($fn);
1.620 albertel 13691: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 13692: # is this cached?
1.599 albertel 13693: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 13694: if (defined($cached)) { return $result; }
13695: # unfortunately not cached, or expired
1.620 albertel 13696: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 13697: &GDBM_READER(),0640)) {
13698: if ($bighash{'version_'.$uri}) {
13699: my $version=$bighash{'version_'.$uri};
1.444 www 13700: unless (($version eq 'mostrecent') ||
13701: ($version==&getversion($uri))) {
1.440 www 13702: $uri=~s/\.(\w+)$/\.$version\.$1/;
13703: }
13704: }
13705: untie %bighash;
1.413 www 13706: }
1.599 albertel 13707: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 13708: }
13709:
13710: sub deversion {
13711: my $url=shift;
13712: $url=~s/\.\d+\.(\w+)$/\.$1/;
13713: return $url;
1.210 www 13714: }
13715:
1.31 www 13716: # ------------------------------------------------------ Return symb list entry
13717:
13718: sub symbread {
1.1172.2.126 raeburn 13719: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1172.2.128 raeburn 13720: $ignoresymbdb,$noenccheck)=@_;
1.1172.2.54 raeburn 13721: my $cache_str='request.symbread.cached.'.$thisfn;
1.1172.2.128 raeburn 13722: if (defined($env{$cache_str})) {
1.1172.2.126 raeburn 13723: unless (ref($possibles) eq 'HASH') {
13724: if ($ignorecachednull) {
13725: return $env{$cache_str} unless ($env{$cache_str} eq '');
13726: } else {
13727: return $env{$cache_str};
13728: }
1.1172.2.66 raeburn 13729: }
13730: }
1.242 www 13731: # no filename provided? try from environment
1.1172.2.54 raeburn 13732: unless ($thisfn) {
1.620 albertel 13733: if ($env{'request.symb'}) {
1.1172.2.128 raeburn 13734: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.1172.2.13 raeburn 13735: }
13736: $thisfn=$env{'request.filename'};
1.44 www 13737: }
1.569 albertel 13738: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 13739: # is that filename actually a symb? Verify, clean, and return
13740: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 13741: if (&symbverify($thisfn,$1)) {
1.1172.2.128 raeburn 13742: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 13743: }
1.242 www 13744: }
1.44 www 13745: $thisfn=declutter($thisfn);
1.31 www 13746: my %hash;
1.37 www 13747: my %bighash;
13748: my $syval='';
1.620 albertel 13749: if (($env{'request.course.fn'}) && ($thisfn)) {
1.1172.2.128 raeburn 13750: unless ($ignoresymbdb) {
1.1172.2.126 raeburn 13751: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13752: &GDBM_READER(),0640)) {
1.1172.2.146. .7(raebu 13753:22): $syval=$hash{$thisfn};
1.1172.2.126 raeburn 13754: untie(%hash);
13755: }
1.1172.2.128 raeburn 13756: if ($syval && $checkforblock) {
13757: my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1172.2.126 raeburn 13758: if (@blockers) {
13759: $syval='';
13760: }
13761: }
1.37 www 13762: }
13763: # ---------------------------------------------------------- There was an entry
13764: if ($syval) {
1.601 albertel 13765: #unless ($syval=~/\_\d+$/) {
1.620 albertel 13766: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 13767: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13768: #return $env{$cache_str}='';
1.601 albertel 13769: #}
13770: #$syval.=$1;
13771: #}
1.37 www 13772: } else {
13773: # ------------------------------------------------------- Was not in symb table
1.620 albertel 13774: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13775: &GDBM_READER(),0640)) {
1.37 www 13776: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 13777: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 13778: unless ($ids) {
13779: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 13780: }
13781: unless ($ids) {
13782: # alias?
13783: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 13784: }
1.37 www 13785: if ($ids) {
13786: # ------------------------------------------------------------------- Has ID(s)
13787: my @possibilities=split(/\,/,$ids);
1.39 www 13788: if ($#possibilities==0) {
13789: # ----------------------------------------------- There is only one possibility
1.37 www 13790: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 13791: $syval=&encode_symb($bighash{'map_id_'.$mapid},
13792: $resid,$thisfn);
1.1172.2.66 raeburn 13793: if (ref($possibles) eq 'HASH') {
1.1172.2.126 raeburn 13794: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13795: $possibles->{$syval} = 1;
13796: }
1.1172.2.66 raeburn 13797: }
13798: if ($checkforblock) {
1.1172.2.126 raeburn 13799: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1172.2.127 raeburn 13800: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1172.2.126 raeburn 13801: if (@blockers) {
13802: $syval = '';
13803: untie(%bighash);
13804: return $env{$cache_str}='';
13805: }
1.1172.2.66 raeburn 13806: }
13807: }
13808: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 13809: # ------------------------------------------ There is more than one possibility
13810: my $realpossible=0;
1.800 albertel 13811: foreach my $id (@possibilities) {
13812: my $file=$bighash{'src_'.$id};
1.1172.2.66 raeburn 13813: my $canaccess;
13814: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13815: $canaccess = 1;
13816: } else {
13817: $canaccess = &allowed('bre',$file);
13818: }
13819: if ($canaccess) {
13820: my ($mapid,$resid)=split(/\./,$id);
13821: if ($bighash{'map_type_'.$mapid} ne 'page') {
13822: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13823: $resid,$thisfn);
1.1172.2.126 raeburn 13824: next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1172.2.127 raeburn 13825: next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1172.2.66 raeburn 13826: if ($checkforblock) {
1.1172.2.127 raeburn 13827: my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1172.2.126 raeburn 13828: if (@blockers > 0) {
13829: $syval = '';
13830: } else {
1.1172.2.66 raeburn 13831: $syval = $poss_syval;
13832: $realpossible++;
13833: }
13834: } else {
13835: $syval = $poss_syval;
13836: $realpossible++;
13837: }
1.1172.2.126 raeburn 13838: if ($syval) {
13839: if (ref($possibles) eq 'HASH') {
13840: $possibles->{$syval} = 1;
13841: }
13842: }
1.1172.2.66 raeburn 13843: }
1.39 www 13844: }
1.191 harris41 13845: }
1.39 www 13846: if ($realpossible!=1) { $syval=''; }
1.249 www 13847: } else {
13848: $syval='';
1.37 www 13849: }
13850: }
1.1172.2.66 raeburn 13851: untie(%bighash);
1.481 raeburn 13852: }
1.31 www 13853: }
1.62 www 13854: if ($syval) {
1.1172.2.128 raeburn 13855: return $env{$cache_str}=$syval;
1.62 www 13856: }
1.31 www 13857: }
1.949 raeburn 13858: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13859: return $env{$cache_str}='';
1.31 www 13860: }
13861:
13862: # ---------------------------------------------------------- Return random seed
13863:
1.32 www 13864: sub numval {
13865: my $txt=shift;
13866: $txt=~tr/A-J/0-9/;
13867: $txt=~tr/a-j/0-9/;
13868: $txt=~tr/K-T/0-9/;
13869: $txt=~tr/k-t/0-9/;
13870: $txt=~tr/U-Z/0-5/;
13871: $txt=~tr/u-z/0-5/;
13872: $txt=~s/\D//g;
1.564 albertel 13873: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 13874: return int($txt);
1.368 albertel 13875: }
13876:
1.484 albertel 13877: sub numval2 {
13878: my $txt=shift;
13879: $txt=~tr/A-J/0-9/;
13880: $txt=~tr/a-j/0-9/;
13881: $txt=~tr/K-T/0-9/;
13882: $txt=~tr/k-t/0-9/;
13883: $txt=~tr/U-Z/0-5/;
13884: $txt=~tr/u-z/0-5/;
13885: $txt=~s/\D//g;
13886: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13887: my $total;
13888: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 13889: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 13890: return int($total);
13891: }
13892:
1.575 albertel 13893: sub numval3 {
13894: use integer;
13895: my $txt=shift;
13896: $txt=~tr/A-J/0-9/;
13897: $txt=~tr/a-j/0-9/;
13898: $txt=~tr/K-T/0-9/;
13899: $txt=~tr/k-t/0-9/;
13900: $txt=~tr/U-Z/0-5/;
13901: $txt=~tr/u-z/0-5/;
13902: $txt=~s/\D//g;
13903: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13904: my $total;
13905: foreach my $val (@txts) { $total+=$val; }
13906: if ($_64bit) { $total=(($total<<32)>>32); }
13907: return $total;
13908: }
13909:
1.675 albertel 13910: sub digest {
13911: my ($data)=@_;
13912: my $digest=&Digest::MD5::md5($data);
13913: my ($a,$b,$c,$d)=unpack("iiii",$digest);
13914: my ($e,$f);
13915: {
13916: use integer;
13917: $e=($a+$b);
13918: $f=($c+$d);
13919: if ($_64bit) {
13920: $e=(($e<<32)>>32);
13921: $f=(($f<<32)>>32);
13922: }
13923: }
13924: if (wantarray) {
13925: return ($e,$f);
13926: } else {
13927: my $g;
13928: {
13929: use integer;
13930: $g=($e+$f);
13931: if ($_64bit) {
13932: $g=(($g<<32)>>32);
13933: }
13934: }
13935: return $g;
13936: }
13937: }
13938:
1.368 albertel 13939: sub latest_rnd_algorithm_id {
1.675 albertel 13940: return '64bit5';
1.366 albertel 13941: }
1.32 www 13942:
1.503 albertel 13943: sub get_rand_alg {
13944: my ($courseid)=@_;
1.790 albertel 13945: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 13946: if ($courseid) {
1.620 albertel 13947: return $env{"course.$courseid.rndseed"};
1.503 albertel 13948: }
13949: return &latest_rnd_algorithm_id();
13950: }
13951:
1.562 albertel 13952: sub validCODE {
13953: my ($CODE)=@_;
13954: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13955: return 0;
13956: }
13957:
1.491 albertel 13958: sub getCODE {
1.620 albertel 13959: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 13960: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13961: defined($Apache::lonhomework::parsing_a_task) ) &&
13962: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 13963: return $Apache::lonhomework::history{'resource.CODE'};
13964: }
13965: return undef;
13966: }
1.1133 foxr 13967: #
13968: # Determines the random seed for a specific context:
13969: #
13970: # parameters:
13971: # symb - in course context the symb for the seed.
13972: # course_id - The course id of the form domain_coursenum.
13973: # domain - Domain for the user.
13974: # course - Course for the user.
13975: # cenv - environment of the course.
13976: #
13977: # NOTE:
13978: # All parameters are picked out of the environment if missing
13979: # or not defined.
13980: # If a symb cannot be determined the current time is used instead.
13981: #
13982: # For a given well defined symb, courside, domain, username,
13983: # and course environment, the seed is reproducible.
13984: #
1.31 www 13985: sub rndseed {
1.1133 foxr 13986: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 13987: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 13988: if (!defined($symb)) {
1.366 albertel 13989: unless ($symb=$wsymb) { return time; }
13990: }
1.1146 foxr 13991: if (!defined $courseid) {
13992: $courseid=$wcourseid;
13993: }
13994: if (!defined $domain) { $domain=$wdomain; }
13995: if (!defined $username) { $username=$wusername }
1.1133 foxr 13996:
13997: my $which;
13998: if (defined($cenv->{'rndseed'})) {
13999: $which = $cenv->{'rndseed'};
14000: } else {
14001: $which =&get_rand_alg($courseid);
14002: }
1.491 albertel 14003: if (defined(&getCODE())) {
1.675 albertel 14004: if ($which eq '64bit5') {
14005: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14006: } elsif ($which eq '64bit4') {
1.575 albertel 14007: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14008: } else {
14009: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14010: }
1.675 albertel 14011: } elsif ($which eq '64bit5') {
14012: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 14013: } elsif ($which eq '64bit4') {
14014: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 14015: } elsif ($which eq '64bit3') {
14016: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 14017: } elsif ($which eq '64bit2') {
14018: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 14019: } elsif ($which eq '64bit') {
14020: return &rndseed_64bit($symb,$courseid,$domain,$username);
14021: }
14022: return &rndseed_32bit($symb,$courseid,$domain,$username);
14023: }
14024:
14025: sub rndseed_32bit {
14026: my ($symb,$courseid,$domain,$username)=@_;
14027: {
14028: use integer;
14029: my $symbchck=unpack("%32C*",$symb) << 27;
14030: my $symbseed=numval($symb) << 22;
14031: my $namechck=unpack("%32C*",$username) << 17;
14032: my $nameseed=numval($username) << 12;
14033: my $domainseed=unpack("%32C*",$domain) << 7;
14034: my $courseseed=unpack("%32C*",$courseid);
14035: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 14036: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14037: #&logthis("rndseed :$num:$symb");
1.564 albertel 14038: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 14039: return $num;
14040: }
14041: }
14042:
14043: sub rndseed_64bit {
14044: my ($symb,$courseid,$domain,$username)=@_;
14045: {
14046: use integer;
14047: my $symbchck=unpack("%32S*",$symb) << 21;
14048: my $symbseed=numval($symb) << 10;
14049: my $namechck=unpack("%32S*",$username);
14050:
14051: my $nameseed=numval($username) << 21;
14052: my $domainseed=unpack("%32S*",$domain) << 10;
14053: my $courseseed=unpack("%32S*",$courseid);
14054:
14055: my $num1=$symbchck+$symbseed+$namechck;
14056: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14057: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14058: #&logthis("rndseed :$num:$symb");
1.564 albertel 14059: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 14060: return "$num1,$num2";
1.155 albertel 14061: }
1.366 albertel 14062: }
14063:
1.443 albertel 14064: sub rndseed_64bit2 {
14065: my ($symb,$courseid,$domain,$username)=@_;
14066: {
14067: use integer;
14068: # strings need to be an even # of cahracters long, it it is odd the
14069: # last characters gets thrown away
14070: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14071: my $symbseed=numval($symb) << 10;
14072: my $namechck=unpack("%32S*",$username.' ');
14073:
14074: my $nameseed=numval($username) << 21;
1.501 albertel 14075: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14076: my $courseseed=unpack("%32S*",$courseid.' ');
14077:
14078: my $num1=$symbchck+$symbseed+$namechck;
14079: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14080: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14081: #&logthis("rndseed :$num:$symb");
1.803 albertel 14082: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 14083: return "$num1,$num2";
14084: }
14085: }
14086:
14087: sub rndseed_64bit3 {
14088: my ($symb,$courseid,$domain,$username)=@_;
14089: {
14090: use integer;
14091: # strings need to be an even # of cahracters long, it it is odd the
14092: # last characters gets thrown away
14093: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14094: my $symbseed=numval2($symb) << 10;
14095: my $namechck=unpack("%32S*",$username.' ');
14096:
14097: my $nameseed=numval2($username) << 21;
1.443 albertel 14098: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14099: my $courseseed=unpack("%32S*",$courseid.' ');
14100:
14101: my $num1=$symbchck+$symbseed+$namechck;
14102: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14103: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14104: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 14105: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14106:
1.503 albertel 14107: return "$num1:$num2";
1.443 albertel 14108: }
14109: }
14110:
1.575 albertel 14111: sub rndseed_64bit4 {
14112: my ($symb,$courseid,$domain,$username)=@_;
14113: {
14114: use integer;
14115: # strings need to be an even # of cahracters long, it it is odd the
14116: # last characters gets thrown away
14117: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14118: my $symbseed=numval3($symb) << 10;
14119: my $namechck=unpack("%32S*",$username.' ');
14120:
14121: my $nameseed=numval3($username) << 21;
14122: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14123: my $courseseed=unpack("%32S*",$courseid.' ');
14124:
14125: my $num1=$symbchck+$symbseed+$namechck;
14126: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14127: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14128: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 14129: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14130:
1.575 albertel 14131: return "$num1:$num2";
14132: }
14133: }
14134:
1.675 albertel 14135: sub rndseed_64bit5 {
14136: my ($symb,$courseid,$domain,$username)=@_;
14137: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14138: return "$num1:$num2";
14139: }
14140:
1.366 albertel 14141: sub rndseed_CODE_64bit {
14142: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 14143: {
1.366 albertel 14144: use integer;
1.443 albertel 14145: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 14146: my $symbseed=numval2($symb);
1.491 albertel 14147: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14148: my $CODEseed=numval(&getCODE());
1.443 albertel 14149: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 14150: my $num1=$symbseed+$CODEchck;
14151: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14152: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14153: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 14154: if ($_64bit) { $num1=(($num1<<32)>>32); }
14155: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 14156: return "$num1:$num2";
1.366 albertel 14157: }
14158: }
14159:
1.575 albertel 14160: sub rndseed_CODE_64bit4 {
14161: my ($symb,$courseid,$domain,$username)=@_;
14162: {
14163: use integer;
14164: my $symbchck=unpack("%32S*",$symb.' ') << 16;
14165: my $symbseed=numval3($symb);
14166: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14167: my $CODEseed=numval3(&getCODE());
14168: my $courseseed=unpack("%32S*",$courseid.' ');
14169: my $num1=$symbseed+$CODEchck;
14170: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14171: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14172: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 14173: if ($_64bit) { $num1=(($num1<<32)>>32); }
14174: if ($_64bit) { $num2=(($num2<<32)>>32); }
14175: return "$num1:$num2";
14176: }
14177: }
14178:
1.675 albertel 14179: sub rndseed_CODE_64bit5 {
14180: my ($symb,$courseid,$domain,$username)=@_;
14181: my $code = &getCODE();
14182: my ($num1,$num2)=&digest("$symb,$courseid,$code");
14183: return "$num1:$num2";
14184: }
14185:
1.366 albertel 14186: sub setup_random_from_rndseed {
14187: my ($rndseed)=@_;
1.503 albertel 14188: if ($rndseed =~/([,:])/) {
1.1172.2.51 raeburn 14189: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14190: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14191: &Math::Random::random_set_seed_from_phrase($rndseed);
14192: } else {
14193: &Math::Random::random_set_seed($num1,$num2);
14194: }
1.366 albertel 14195: } else {
14196: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 14197: }
1.36 albertel 14198: }
14199:
1.474 albertel 14200: sub latest_receipt_algorithm_id {
1.835 albertel 14201: return 'receipt3';
1.474 albertel 14202: }
14203:
1.480 www 14204: sub recunique {
14205: my $fucourseid=shift;
14206: my $unique;
1.835 albertel 14207: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14208: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14209: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 14210: } else {
14211: $unique=$perlvar{'lonReceipt'};
14212: }
14213: return unpack("%32C*",$unique);
14214: }
14215:
14216: sub recprefix {
14217: my $fucourseid=shift;
14218: my $prefix;
1.835 albertel 14219: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14220: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14221: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 14222: } else {
14223: $prefix=$perlvar{'lonHostID'};
14224: }
14225: return unpack("%32C*",$prefix);
14226: }
14227:
1.76 www 14228: sub ireceipt {
1.474 albertel 14229: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 14230:
14231: my $return =&recprefix($fucourseid).'-';
14232:
14233: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14234: $env{'request.state'} eq 'construct') {
14235: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14236: return $return;
14237: }
14238:
1.76 www 14239: my $cuname=unpack("%32C*",$funame);
14240: my $cudom=unpack("%32C*",$fudom);
14241: my $cucourseid=unpack("%32C*",$fucourseid);
14242: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 14243: my $cunique=&recunique($fucourseid);
1.474 albertel 14244: my $cpart=unpack("%32S*",$part);
1.835 albertel 14245: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14246:
1.790 albertel 14247: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 14248:
14249: $return.= ($cunique%$cuname+
14250: $cunique%$cudom+
14251: $cusymb%$cuname+
14252: $cusymb%$cudom+
14253: $cucourseid%$cuname+
14254: $cucourseid%$cudom+
14255: $cpart%$cuname+
14256: $cpart%$cudom);
14257: } else {
14258: $return.= ($cunique%$cuname+
14259: $cunique%$cudom+
14260: $cusymb%$cuname+
14261: $cusymb%$cudom+
14262: $cucourseid%$cuname+
14263: $cucourseid%$cudom);
14264: }
14265: return $return;
1.76 www 14266: }
14267:
14268: sub receipt {
1.474 albertel 14269: my ($part)=@_;
1.790 albertel 14270: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 14271: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 14272: }
1.260 ng 14273:
1.790 albertel 14274: sub whichuser {
14275: my ($passedsymb)=@_;
14276: my ($symb,$courseid,$domain,$name,$publicuser);
14277: if (defined($env{'form.grade_symb'})) {
14278: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14279: my $allowed=&allowed('vgr',$tmp_courseid);
14280: if (!$allowed &&
14281: exists($env{'request.course.sec'}) &&
14282: $env{'request.course.sec'} !~ /^\s*$/) {
14283: $allowed=&allowed('vgr',$tmp_courseid.
14284: '/'.$env{'request.course.sec'});
14285: }
14286: if ($allowed) {
14287: ($symb)=&get_env_multiple('form.grade_symb');
14288: $courseid=$tmp_courseid;
14289: ($domain)=&get_env_multiple('form.grade_domain');
14290: ($name)=&get_env_multiple('form.grade_username');
14291: return ($symb,$courseid,$domain,$name,$publicuser);
14292: }
14293: }
14294: if (!$passedsymb) {
14295: $symb=&symbread();
14296: } else {
14297: $symb=$passedsymb;
14298: }
14299: $courseid=$env{'request.course.id'};
14300: $domain=$env{'user.domain'};
14301: $name=$env{'user.name'};
14302: if ($name eq 'public' && $domain eq 'public') {
14303: if (!defined($env{'form.username'})) {
14304: $env{'form.username'}.=time.rand(10000000);
14305: }
14306: $name.=$env{'form.username'};
14307: }
14308: return ($symb,$courseid,$domain,$name,$publicuser);
14309:
14310: }
14311:
1.36 albertel 14312: # ------------------------------------------------------------ Serves up a file
1.472 albertel 14313: # returns either the contents of the file or
14314: # -1 if the file doesn't exist
1.481 raeburn 14315: #
14316: # if the target is a file that was uploaded via DOCS,
14317: # a check will be made to see if a current copy exists on the local server,
14318: # if it does this will be served, otherwise a copy will be retrieved from
14319: # the home server for the course and stored in /home/httpd/html/userfiles on
14320: # the local server.
1.472 albertel 14321:
1.36 albertel 14322: sub getfile {
1.538 albertel 14323: my ($file) = @_;
1.609 banghart 14324: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 14325: &repcopy($file);
14326: return &readfile($file);
14327: }
14328:
14329: sub repcopy_userfile {
14330: my ($file)=@_;
1.1142 raeburn 14331: my $londocroot = $perlvar{'lonDocRoot'};
14332: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 14333: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 14334: my ($cdom,$cnum,$filename) =
1.811 albertel 14335: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 14336: my $uri="/uploaded/$cdom/$cnum/$filename";
14337: if (-e "$file") {
1.828 www 14338: # we already have a local copy, check it out
1.538 albertel 14339: my @fileinfo = stat($file);
1.828 www 14340: my $rtncode;
14341: my $info;
1.538 albertel 14342: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 14343: if ($lwpresp ne 'ok') {
1.828 www 14344: # there is no such file anymore, even though we had a local copy
1.482 albertel 14345: if ($rtncode eq '404') {
1.538 albertel 14346: unlink($file);
1.482 albertel 14347: }
14348: return -1;
14349: }
14350: if ($info < $fileinfo[9]) {
1.828 www 14351: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 14352: return 'ok';
1.828 www 14353: } else {
14354: # the file is outdated, get rid of it
14355: unlink($file);
1.482 albertel 14356: }
1.828 www 14357: }
14358: # one way or the other, at this point, we don't have the file
14359: # construct the correct path for the file
14360: my @parts = ($cdom,$cnum);
14361: if ($filename =~ m|^(.+)/[^/]+$|) {
14362: push @parts, split(/\//,$1);
14363: }
14364: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14365: foreach my $part (@parts) {
14366: $path .= '/'.$part;
14367: if (!-e $path) {
14368: mkdir($path,0770);
1.482 albertel 14369: }
14370: }
1.828 www 14371: # now the path exists for sure
14372: # get a user agent
14373: my $ua=new LWP::UserAgent;
14374: my $transferfile=$file.'.in.transfer';
14375: # FIXME: this should flock
14376: if (-e $transferfile) { return 'ok'; }
14377: my $request;
14378: $uri=~s/^\///;
1.980 raeburn 14379: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14380: my $hostname = &hostname($homeserver);
1.980 raeburn 14381: my $protocol = $protocol{$homeserver};
14382: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14383: $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.828 www 14384: my $response=$ua->request($request,$transferfile);
14385: # did it work?
14386: if ($response->is_error()) {
14387: unlink($transferfile);
14388: &logthis("Userfile repcopy failed for $uri");
14389: return -1;
14390: }
14391: # worked, rename the transfer file
14392: rename($transferfile,$file);
1.607 raeburn 14393: return 'ok';
1.481 raeburn 14394: }
14395:
1.517 albertel 14396: sub tokenwrapper {
14397: my $uri=shift;
1.980 raeburn 14398: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 14399: $uri=~s|^/||;
1.620 albertel 14400: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 14401: my $token=$1;
1.552 albertel 14402: my (undef,$udom,$uname,$file)=split('/',$uri,4);
14403: if ($udom && $uname && $file) {
14404: $file=~s|(\?\.*)*$||;
1.949 raeburn 14405: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 14406: my $homeserver = &homeserver($uname,$udom);
1.1172.2.120 raeburn 14407: my $hostname = &hostname($homeserver);
1.980 raeburn 14408: my $protocol = $protocol{$homeserver};
14409: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14410: return $protocol.'://'.$hostname.'/'.$uri.
1.517 albertel 14411: (($uri=~/\?/)?'&':'?').'token='.$token.
14412: '&tokenissued='.$perlvar{'lonHostID'};
14413: } else {
14414: return '/adm/notfound.html';
14415: }
14416: }
14417:
1.828 www 14418: # call with reqtype HEAD: get last modification time
14419: # call with reqtype GET: get the file contents
14420: # Do not call this with reqtype GET for large files! It loads everything into memory
14421: #
1.481 raeburn 14422: sub getuploaded {
14423: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14424: $uri=~s/^\///;
1.980 raeburn 14425: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14426: my $hostname = &hostname($homeserver);
1.980 raeburn 14427: my $protocol = $protocol{$homeserver};
14428: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14429: $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481 raeburn 14430: my $ua=new LWP::UserAgent;
14431: my $request=new HTTP::Request($reqtype,$uri);
14432: my $response=$ua->request($request);
14433: $$rtncode = $response->code;
1.482 albertel 14434: if (! $response->is_success()) {
14435: return 'failed';
14436: }
14437: if ($reqtype eq 'HEAD') {
1.486 www 14438: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 14439: } elsif ($reqtype eq 'GET') {
14440: $$info = $response->content;
1.472 albertel 14441: }
1.482 albertel 14442: return 'ok';
1.36 albertel 14443: }
14444:
1.481 raeburn 14445: sub readfile {
14446: my $file = shift;
14447: if ( (! -e $file ) || ($file eq '') ) { return -1; };
14448: my $fh;
1.1172.2.96 raeburn 14449: open($fh,"<",$file);
1.481 raeburn 14450: my $a='';
1.800 albertel 14451: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 14452: return $a;
14453: }
14454:
1.36 albertel 14455: sub filelocation {
1.590 banghart 14456: my ($dir,$file) = @_;
14457: my $location;
14458: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 14459:
14460: if ($file =~ m-^/adm/-) {
14461: $file=~s-^/adm/wrapper/-/-;
14462: $file=~s-^/adm/coursedocs/showdoc/-/-;
14463: }
1.882 albertel 14464:
1.1139 www 14465: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 14466: $location = $file;
1.609 banghart 14467: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 14468: my ($udom,$uname,$filename)=
1.811 albertel 14469: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 14470: my $home=&homeserver($uname,$udom);
14471: my $is_me=0;
14472: my @ids=¤t_machine_ids();
14473: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14474: if ($is_me) {
1.1117 foxr 14475: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 14476: } else {
14477: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14478: $udom.'/'.$uname.'/'.$filename;
14479: }
1.882 albertel 14480: } elsif ($file =~ m-^/adm/-) {
14481: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 14482: } else {
14483: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 14484: $file=~s:^/(res|priv)/:/:;
14485: my $space=$1;
1.590 banghart 14486: if ( !( $file =~ m:^/:) ) {
14487: $location = $dir. '/'.$file;
14488: } else {
1.1142 raeburn 14489: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 14490: }
1.59 albertel 14491: }
1.590 banghart 14492: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 14493: while ($location=~m{/\.\./}) {
14494: if ($location =~ m{/[^/]+/\.\./}) {
14495: $location=~ s{/[^/]+/\.\./}{/}g;
14496: } else {
14497: $location=~ s{/\.\./}{/}g;
14498: }
14499: } #remove dir/..
1.590 banghart 14500: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14501: return $location;
1.46 www 14502: }
1.36 albertel 14503:
1.46 www 14504: sub hreflocation {
14505: my ($dir,$file)=@_;
1.980 raeburn 14506: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 14507: $file=filelocation($dir,$file);
1.700 albertel 14508: } elsif ($file=~m-^/adm/-) {
14509: $file=~s-^/adm/wrapper/-/-;
14510: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 14511: }
14512: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14513: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14514: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 14515: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14516: {/uploaded/$1/$2/}x;
1.46 www 14517: }
1.913 albertel 14518: if ($file=~ m{^/userfiles/}) {
14519: $file =~ s{^/userfiles/}{/uploaded/};
14520: }
1.462 albertel 14521: return $file;
1.465 albertel 14522: }
14523:
1.1139 www 14524:
14525:
14526:
14527:
1.465 albertel 14528: sub current_machine_domains {
1.853 albertel 14529: return &machine_domains(&hostname($perlvar{'lonHostID'}));
14530: }
14531:
14532: sub machine_domains {
14533: my ($hostname) = @_;
1.465 albertel 14534: my @domains;
1.838 albertel 14535: my %hostname = &all_hostnames();
1.465 albertel 14536: while( my($id, $name) = each(%hostname)) {
1.467 matthew 14537: # &logthis("-$id-$name-$hostname-");
1.465 albertel 14538: if ($hostname eq $name) {
1.844 albertel 14539: push(@domains,&host_domain($id));
1.465 albertel 14540: }
14541: }
14542: return @domains;
14543: }
14544:
14545: sub current_machine_ids {
1.853 albertel 14546: return &machine_ids(&hostname($perlvar{'lonHostID'}));
14547: }
14548:
14549: sub machine_ids {
14550: my ($hostname) = @_;
14551: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 14552: my @ids;
1.888 albertel 14553: my %name_to_host = &all_names();
1.889 albertel 14554: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14555: return @{ $name_to_host{$hostname} };
14556: }
14557: return;
1.31 www 14558: }
14559:
1.824 raeburn 14560: sub additional_machine_domains {
14561: my @domains;
1.1172.2.142 raeburn 14562: if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14563: if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14564: while( my $line = <$fh>) {
14565: chomp($line);
14566: $line =~ s/\s//g;
14567: push(@domains,$line);
14568: }
14569: close($fh);
14570: }
1.824 raeburn 14571: }
14572: return @domains;
14573: }
14574:
14575: sub default_login_domain {
14576: my $domain = $perlvar{'lonDefDomain'};
14577: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14578: foreach my $posdom (¤t_machine_domains(),
14579: &additional_machine_domains()) {
14580: if (lc($posdom) eq lc($testdomain)) {
14581: $domain=$posdom;
14582: last;
14583: }
14584: }
14585: return $domain;
14586: }
14587:
1.1172.2.106 raeburn 14588: sub shared_institution {
1.1172.2.136 raeburn 14589: my ($dom,$lonhost) = @_;
14590: if ($lonhost eq '') {
14591: $lonhost = $perlvar{'lonHostID'};
14592: }
1.1172.2.106 raeburn 14593: my $same_intdom;
1.1172.2.136 raeburn 14594: my $hostintdom = &internet_dom($lonhost);
1.1172.2.106 raeburn 14595: if ($hostintdom ne '') {
14596: my %iphost = &get_iphost();
14597: my $primary_id = &domain($dom,'primary');
14598: my $primary_ip = &get_host_ip($primary_id);
14599: if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14600: foreach my $id (@{$iphost{$primary_ip}}) {
14601: my $intdom = &internet_dom($id);
14602: if ($intdom eq $hostintdom) {
14603: $same_intdom = 1;
14604: last;
14605: }
14606: }
14607: }
14608: }
14609: return $same_intdom;
14610: }
14611:
1.1172.2.120 raeburn 14612: sub uses_sts {
14613: my ($ignore_cache) = @_;
14614: my $lonhost = $perlvar{'lonHostID'};
14615: my $hostname = &hostname($lonhost);
14616: my $sts_on;
14617: if ($protocol{$lonhost} eq 'https') {
14618: my $cachetime = 12*3600;
14619: if (!$ignore_cache) {
14620: ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14621: if (defined($cached)) {
14622: return $sts_on;
14623: }
14624: }
1.1172.2.121 raeburn 14625: my $ua=new LWP::UserAgent;
1.1172.2.120 raeburn 14626: my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14627: my $request=new HTTP::Request('HEAD',$url);
1.1172.2.121 raeburn 14628: my $response=$ua->request($request);
1.1172.2.120 raeburn 14629: if ($response->is_success) {
14630: my $has_sts = $response->header('Strict-Transport-Security');
14631: if ($has_sts eq '') {
14632: $sts_on = 0;
14633: } else {
14634: if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14635: my $maxage = $1;
14636: if ($maxage) {
14637: $sts_on = 1;
14638: } else {
14639: $sts_on = 0;
14640: }
14641: } else {
14642: $sts_on = 0;
14643: }
14644: }
14645: return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14646: }
14647: }
14648: return;
14649: }
14650:
1.1172.2.142 raeburn 14651: sub waf_allssl {
14652: my ($host_name) = @_;
14653: my $alias = &get_proxy_alias();
14654: if ($host_name eq '') {
14655: $host_name = $ENV{'SERVER_NAME'};
14656: }
14657: if (($host_name ne '') && ($alias eq $host_name)) {
14658: my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14659: my %defdomdefaults = &get_domain_defaults($serverhomedom);
14660: if ($defdomdefaults{'waf_sslopt'}) {
14661: return $defdomdefaults{'waf_sslopt'};
14662: }
14663: }
14664: return;
14665: }
14666:
1.1172.2.134 raeburn 14667: sub get_requestor_ip {
14668: my ($r,$nolookup,$noproxy) = @_;
14669: my $from_ip;
14670: if (ref($r)) {
1.1172.2.142 raeburn 14671: if ($r->can('useragent_ip')) {
14672: if ($noproxy && $r->can('client_ip')) {
14673: $from_ip = $r->client_ip();
14674: } else {
14675: $from_ip = $r->useragent_ip();
14676: }
14677: } elsif ($r->connection->can('remote_ip')) {
14678: $from_ip = $r->connection->remote_ip();
14679: } else {
14680: $from_ip = $r->get_remote_host($nolookup);
14681: }
1.1172.2.134 raeburn 14682: } else {
14683: $from_ip = $ENV{'REMOTE_ADDR'};
14684: }
1.1172.2.142 raeburn 14685: return $from_ip if ($noproxy);
14686: # Who controls proxy settings for server
14687: my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14688: my $proxyinfo = &get_proxy_settings($dom_in_use);
14689: if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14690: if ($proxyinfo->{'vpnint'}) {
14691: if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14692: return $from_ip;
14693: }
14694: }
14695: if ($proxyinfo->{'trusted'}) {
14696: if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14697: my $ipheader = $proxyinfo->{'ipheader'};
14698: my ($ip,$xfor);
14699: if (ref($r)) {
14700: if ($ipheader) {
14701: $ip = $r->headers_in->{$ipheader};
14702: }
14703: $xfor = $r->headers_in->{'X-Forwarded-For'};
14704: } else {
14705: if ($ipheader) {
14706: $ip = $ENV{'HTTP_'.uc($ipheader)};
14707: }
14708: $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14709: }
14710: if (($ip eq '') && ($xfor ne '')) {
14711: foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14712: unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14713: $ip = $poss_ip;
14714: last;
14715: }
14716: }
14717: }
14718: if ($ip ne '') {
14719: return $ip;
14720: }
14721: }
14722: }
14723: }
1.1172.2.134 raeburn 14724: return $from_ip;
14725: }
14726:
1.1172.2.142 raeburn 14727: sub get_proxy_settings {
14728: my ($dom_in_use) = @_;
14729: my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14730: my $proxyinfo = {
14731: ipheader => $domdefaults{'waf_ipheader'},
14732: trusted => $domdefaults{'waf_trusted'},
14733: vpnint => $domdefaults{'waf_vpnint'},
14734: vpnext => $domdefaults{'waf_vpnext'},
14735: sslopt => $domdefaults{'waf_sslopt'},
14736: };
14737: return $proxyinfo;
14738: }
14739:
14740: sub ip_match {
14741: my ($ip,$pattern_str) = @_;
14742: $ip=Net::CIDR::cidrvalidate($ip);
14743: if ($ip) {
14744: return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14745: }
14746: return;
14747: }
14748:
14749: sub get_proxy_alias {
14750: my ($lonid) = @_;
14751: if ($lonid eq '') {
14752: $lonid = $perlvar{'lonHostID'};
14753: }
14754: if (!defined(&hostname($lonid))) {
14755: return;
14756: }
14757: if ($lonid ne '') {
14758: my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14759: if ($cached) {
14760: return $alias;
14761: }
14762: my $dom = &Apache::lonnet::host_domain($lonid);
14763: if ($dom ne '') {
14764: my $cachetime = 60*60*24;
14765: my %domconfig =
14766: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14767: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14768: if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14769: $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14770: }
14771: }
14772: return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14773: }
14774: }
14775: return;
14776: }
14777:
14778: sub use_proxy_alias {
14779: my ($r,$lonid) = @_;
14780: my $alias = &get_proxy_alias($lonid);
14781: if ($alias) {
14782: my $dom = &host_domain($lonid);
14783: if ($dom ne '') {
14784: my $proxyinfo = &get_proxy_settings($dom);
14785: my ($vpnint,$remote_ip);
14786: if (ref($proxyinfo) eq 'HASH') {
14787: $vpnint = $proxyinfo->{'vpnint'};
14788: if ($vpnint) {
14789: $remote_ip = &get_requestor_ip($r,1,1);
14790: }
14791: }
14792: unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14793: return $alias;
14794: }
14795: }
14796: }
14797: return;
14798: }
14799:
14800: sub alias_sso {
14801: my ($lonid) = @_;
14802: if ($lonid eq '') {
14803: $lonid = $perlvar{'lonHostID'};
14804: }
14805: if (!defined(&hostname($lonid))) {
14806: return;
14807: }
14808: if ($lonid ne '') {
14809: my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14810: if ($cached) {
14811: return $use_alias;
14812: }
14813: my $dom = &Apache::lonnet::host_domain($lonid);
14814: if ($dom ne '') {
14815: my $cachetime = 60*60*24;
14816: my %domconfig =
14817: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14818: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14819: if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14820: $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14821: }
14822: }
14823: return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14824: }
14825: }
14826: return;
14827: }
14828:
14829: sub get_saml_landing {
14830: my ($lonid) = @_;
14831: if ($lonid eq '') {
14832: my $defdom = &default_login_domain();
14833: my @hosts = ¤t_machine_ids();
14834: if (@hosts > 1) {
14835: foreach my $hostid (@hosts) {
14836: if (&host_domain($hostid) eq $defdom) {
14837: $lonid = $hostid;
14838: last;
14839: }
14840: }
14841: } else {
14842: $lonid = $perlvar{'lonHostID'};
14843: }
14844: if ($lonid) {
14845: unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14846: return;
14847: }
14848: } else {
14849: return;
14850: }
14851: } elsif (!defined(&hostname($lonid))) {
14852: return;
14853: }
14854: my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14855: if ($cached) {
14856: return $landing;
14857: }
14858: my $dom = &Apache::lonnet::host_domain($lonid);
14859: if ($dom ne '') {
14860: my $cachetime = 60*60*24;
14861: my %domconfig =
14862: &Apache::lonnet::get_dom('configuration',['login'],$dom);
14863: if (ref($domconfig{'login'}) eq 'HASH') {
14864: if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14865: if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14866: $landing = 1;
14867: }
14868: }
14869: }
14870: return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14871: }
14872: return;
14873: }
14874:
1.31 www 14875: # ------------------------------------------------------------- Declutters URLs
14876:
14877: sub declutter {
14878: my $thisfn=shift;
1.569 albertel 14879: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1172.2.49 raeburn 14880: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14881: $thisfn=~s{^/home/httpd/html}{};
14882: }
1.31 www 14883: $thisfn=~s/^\///;
1.697 albertel 14884: $thisfn=~s|^adm/wrapper/||;
14885: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 14886: $thisfn=~s/^res\///;
1.1172 bisitz 14887: $thisfn=~s/^priv\///;
1.1032 raeburn 14888: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14889: $thisfn=~s/\?.+$//;
14890: }
1.268 www 14891: return $thisfn;
14892: }
14893:
14894: # ------------------------------------------------------------- Clutter up URLs
14895:
14896: sub clutter {
14897: my $thisfn='/'.&declutter(shift);
1.887 albertel 14898: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 14899: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 14900: $thisfn='/res'.$thisfn;
14901: }
1.1031 raeburn 14902: if ($thisfn !~m|^/adm|) {
14903: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 14904: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 14905: } else {
14906: my ($ext) = ($thisfn =~ /\.(\w+)$/);
14907: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 14908: if ($embstyle eq 'ssi'
14909: || ($embstyle eq 'hdn')
14910: || ($embstyle eq 'rat')
14911: || ($embstyle eq 'prv')
14912: || ($embstyle eq 'ign')) {
14913: #do nothing with these
14914: } elsif (($embstyle eq 'img')
1.695 albertel 14915: || ($embstyle eq 'emb')
14916: || ($embstyle eq 'wrp')) {
14917: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 14918: } elsif ($embstyle eq 'unk'
14919: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 14920: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 14921: } else {
1.718 www 14922: # &logthis("Got a blank emb style");
1.695 albertel 14923: }
1.694 albertel 14924: }
1.1172.2.146. .1(raebu 14925:22): } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14926:22): $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 14927: }
1.31 www 14928: return $thisfn;
1.12 www 14929: }
14930:
1.787 albertel 14931: sub clutter_with_no_wrapper {
14932: my $uri = &clutter(shift);
14933: if ($uri =~ m-^/adm/-) {
14934: $uri =~ s-^/adm/wrapper/-/-;
14935: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
14936: }
14937: return $uri;
14938: }
14939:
1.557 albertel 14940: sub freeze_escape {
14941: my ($value)=@_;
14942: if (ref($value)) {
14943: $value=&nfreeze($value);
14944: return '__FROZEN__'.&escape($value);
14945: }
14946: return &escape($value);
14947: }
14948:
1.11 www 14949:
1.557 albertel 14950: sub thaw_unescape {
14951: my ($value)=@_;
14952: if ($value =~ /^__FROZEN__/) {
14953: substr($value,0,10,undef);
14954: $value=&unescape($value);
14955: return &thaw($value);
14956: }
14957: return &unescape($value);
14958: }
14959:
1.436 albertel 14960: sub correct_line_ends {
14961: my ($result)=@_;
14962: $$result =~s/\r\n/\n/mg;
14963: $$result =~s/\r/\n/mg;
1.415 albertel 14964: }
1.1 albertel 14965: # ================================================================ Main Program
14966:
1.184 www 14967: sub goodbye {
1.204 albertel 14968: &logthis("Starting Shut down");
1.443 albertel 14969: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 14970: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 14971: #converted
1.599 albertel 14972: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 14973: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14974: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14975: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 14976: #1.1 only
1.870 albertel 14977: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14978: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14979: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14980: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14981: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 14982: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14983: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 14984: &flushcourselogs();
14985: &logthis("Shutting down");
14986: }
14987:
1.852 albertel 14988: sub get_dns {
1.1172.2.17 raeburn 14989: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 14990: if (!$ignore_cache) {
14991: my ($content,$cached)=
14992: &Apache::lonnet::is_cached_new('dns',$url);
14993: if ($cached) {
1.1172.2.17 raeburn 14994: &$func($content,$hashref);
1.869 albertel 14995: return;
14996: }
14997: }
14998:
14999: my %alldns;
1.1172.2.96 raeburn 15000: if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15001: foreach my $dns (<$config>) {
15002: next if ($dns !~ /^\^(\S*)/x);
15003: my $line = $1;
15004: my ($host,$protocol) = split(/:/,$line);
15005: if ($protocol ne 'https') {
15006: $protocol = 'http';
15007: }
15008: $alldns{$host} = $protocol;
1.979 raeburn 15009: }
1.1172.2.96 raeburn 15010: close($config);
1.869 albertel 15011: }
15012: while (%alldns) {
1.1172.2.52 raeburn 15013: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1172.2.142 raeburn 15014: my @content;
15015: if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15016: my $command = (split('/',$url))[3];
15017: my ($dir,$file) = &parse_getdns_url($command,$url);
15018: delete($alldns{$dns});
15019: next if (($dir eq '') || ($file eq ''));
15020: if (open(my $config,'<',"$dir/$file")) {
15021: @content = <$config>;
15022: close($config);
15023: }
15024: } else {
15025: my $ua=new LWP::UserAgent;
15026: $ua->timeout(30);
15027: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15028: my $response=$ua->request($request);
15029: delete($alldns{$dns});
15030: next if ($response->is_error());
15031: @content = split("\n",$response->content);
15032: }
1.1172.2.17 raeburn 15033: unless ($nocache) {
1.1172.2.23 raeburn 15034: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1172.2.17 raeburn 15035: }
15036: &$func(\@content,$hashref);
1.869 albertel 15037: return;
1.852 albertel 15038: }
1.871 albertel 15039: my $which = (split('/',$url))[3];
15040: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
1.1172.2.105 raeburn 15041: if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15042: my @content = <$config>;
15043: &$func(\@content,$hashref);
15044: }
1.1172.2.17 raeburn 15045: return;
15046: }
15047:
15048: # ------------------------------------------------------Get DNS checksums file
15049: sub parse_dns_checksums_tab {
15050: my ($lines,$hashref) = @_;
1.1172.2.53 raeburn 15051: my $lonhost = $perlvar{'lonHostID'};
15052: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1172.2.17 raeburn 15053: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1172.2.53 raeburn 15054: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15055: my $webconfdir = '/etc/httpd/conf';
15056: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15057: $webconfdir = '/etc/apache2';
15058: } elsif ($distro =~ /^sles(\d+)$/) {
15059: if ($1 >= 10) {
15060: $webconfdir = '/etc/apache2';
15061: }
15062: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15063: if ($1 >= 10.0) {
15064: $webconfdir = '/etc/apache2';
15065: }
15066: }
1.1172.2.17 raeburn 15067: my ($release,$timestamp) = split(/\-/,$loncaparev);
15068: my (%chksum,%revnum);
15069: if (ref($lines) eq 'ARRAY') {
15070: chomp(@{$lines});
1.1172.2.34 raeburn 15071: my $version = shift(@{$lines});
15072: if ($version eq $release) {
1.1172.2.17 raeburn 15073: foreach my $line (@{$lines}) {
1.1172.2.34 raeburn 15074: my ($file,$version,$shasum) = split(/,/,$line);
1.1172.2.53 raeburn 15075: if ($file =~ m{^/etc/httpd/conf}) {
15076: if ($webconfdir eq '/etc/apache2') {
15077: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15078: }
15079: }
1.1172.2.34 raeburn 15080: $chksum{$file} = $shasum;
15081: $revnum{$file} = $version;
1.1172.2.17 raeburn 15082: }
15083: if (ref($hashref) eq 'HASH') {
15084: %{$hashref} = (
15085: sums => \%chksum,
15086: versions => \%revnum,
15087: );
15088: }
15089: }
15090: }
1.869 albertel 15091: return;
1.852 albertel 15092: }
1.1172.2.17 raeburn 15093:
15094: sub fetch_dns_checksums {
15095: my %checksums;
1.1172.2.34 raeburn 15096: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1172.2.48 raeburn 15097: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1172.2.34 raeburn 15098: my ($release,$timestamp) = split(/\-/,$loncaparev);
15099: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1172.2.17 raeburn 15100: \%checksums);
15101: return \%checksums;
15102: }
15103:
1.1172.2.142 raeburn 15104: sub parse_getdns_url {
15105: my ($command,$url) = @_;
15106: my $dir = $perlvar{'lonTabDir'};
15107: my $file;
15108: if ($command eq 'hosts') {
15109: $file = 'dns_hosts.tab';
15110: } elsif ($command eq 'domain') {
15111: $file = 'dns_domain.tab';
15112: } elsif ($command eq 'checksums') {
15113: my $version = (split('/',$url))[4];
15114: $file = "dns_checksums/$version.tab",
15115: }
15116: return ($dir,$file);
15117: }
15118:
1.327 albertel 15119: # ------------------------------------------------------------ Read domain file
15120: {
1.852 albertel 15121: my $loaded;
1.846 albertel 15122: my %domain;
15123:
1.852 albertel 15124: sub parse_domain_tab {
15125: my ($lines) = @_;
15126: foreach my $line (@$lines) {
15127: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 15128:
1.846 albertel 15129: chomp($line);
1.852 albertel 15130: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 15131: my %this_domain;
15132: foreach my $field ('description', 'auth_def', 'auth_arg_def',
15133: 'lang_def', 'city', 'longi', 'lati',
15134: 'primary') {
15135: $this_domain{$field} = shift(@elements);
15136: }
15137: $domain{$name} = \%this_domain;
1.852 albertel 15138: }
15139: }
1.864 albertel 15140:
15141: sub reset_domain_info {
15142: undef($loaded);
15143: undef(%domain);
15144: }
15145:
1.852 albertel 15146: sub load_domain_tab {
1.1172.2.70 raeburn 15147: my ($ignore_cache,$nocache) = @_;
15148: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 15149: my $fh;
1.1172.2.96 raeburn 15150: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852 albertel 15151: my @lines = <$fh>;
15152: &parse_domain_tab(\@lines);
1.448 albertel 15153: }
1.852 albertel 15154: close($fh);
15155: $loaded = 1;
1.327 albertel 15156: }
1.846 albertel 15157:
15158: sub domain {
1.852 albertel 15159: &load_domain_tab() if (!$loaded);
15160:
1.846 albertel 15161: my ($name,$what) = @_;
15162: return if ( !exists($domain{$name}) );
15163:
15164: if (!$what) {
15165: return $domain{$name}{'description'};
15166: }
15167: return $domain{$name}{$what};
15168: }
1.974 raeburn 15169:
15170: sub domain_info {
15171: &load_domain_tab() if (!$loaded);
15172: return %domain;
15173: }
15174:
1.327 albertel 15175: }
15176:
15177:
1.1 albertel 15178: # ------------------------------------------------------------- Read hosts file
15179: {
1.838 albertel 15180: my %hostname;
1.844 albertel 15181: my %hostdom;
1.845 albertel 15182: my %libserv;
1.852 albertel 15183: my $loaded;
1.888 albertel 15184: my %name_to_host;
1.1074 raeburn 15185: my %internetdom;
1.1107 raeburn 15186: my %LC_dns_serv;
1.852 albertel 15187:
15188: sub parse_hosts_tab {
15189: my ($file) = @_;
15190: foreach my $configline (@$file) {
15191: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 15192: chomp($configline);
15193: if ($configline =~ /^\^/) {
15194: if ($configline =~ /^\^([\w.\-]+)/) {
15195: $LC_dns_serv{$1} = 1;
15196: }
15197: next;
15198: }
1.1074 raeburn 15199: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 15200: $name=~s/\s//g;
15201: if ($id && $domain && $role && $name) {
1.1172.2.96 raeburn 15202: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15203: my $curr = $hostname{$id};
15204: my $skip;
15205: if (ref($name_to_host{$curr}) eq 'ARRAY') {
15206: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15207: $skip = 1;
15208: } else {
15209: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15210: }
15211: }
15212: unless ($skip) {
15213: push(@{$name_to_host{$name}},$id);
15214: }
15215: } else {
15216: push(@{$name_to_host{$name}},$id);
15217: }
1.852 albertel 15218: $hostname{$id}=$name;
15219: $hostdom{$id}=$domain;
15220: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 15221: if (defined($protocol)) {
15222: if ($protocol eq 'https') {
15223: $protocol{$id} = $protocol;
15224: } else {
15225: $protocol{$id} = 'http';
15226: }
1.968 raeburn 15227: } else {
1.969 raeburn 15228: $protocol{$id} = 'http';
1.968 raeburn 15229: }
1.1074 raeburn 15230: if (defined($intdom)) {
15231: $internetdom{$id} = $intdom;
15232: }
1.852 albertel 15233: }
15234: }
15235: }
1.864 albertel 15236:
15237: sub reset_hosts_info {
1.897 albertel 15238: &purge_remembered();
1.864 albertel 15239: &reset_domain_info();
15240: &reset_hosts_ip_info();
1.892 albertel 15241: undef(%name_to_host);
1.864 albertel 15242: undef(%hostname);
15243: undef(%hostdom);
15244: undef(%libserv);
15245: undef($loaded);
15246: }
1.1 albertel 15247:
1.852 albertel 15248: sub load_hosts_tab {
1.1172.2.70 raeburn 15249: my ($ignore_cache,$nocache) = @_;
15250: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1172.2.96 raeburn 15251: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852 albertel 15252: my @config = <$config>;
15253: &parse_hosts_tab(\@config);
15254: close($config);
15255: $loaded=1;
1.1 albertel 15256: }
1.852 albertel 15257:
1.838 albertel 15258: sub hostname {
1.852 albertel 15259: &load_hosts_tab() if (!$loaded);
15260:
1.838 albertel 15261: my ($lonid) = @_;
15262: return $hostname{$lonid};
15263: }
1.845 albertel 15264:
1.838 albertel 15265: sub all_hostnames {
1.852 albertel 15266: &load_hosts_tab() if (!$loaded);
15267:
1.838 albertel 15268: return %hostname;
15269: }
1.845 albertel 15270:
1.888 albertel 15271: sub all_names {
1.1172.2.70 raeburn 15272: my ($ignore_cache,$nocache) = @_;
15273: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 15274:
15275: return %name_to_host;
15276: }
15277:
1.974 raeburn 15278: sub all_host_domain {
15279: &load_hosts_tab() if (!$loaded);
15280: return %hostdom;
15281: }
15282:
1.845 albertel 15283: sub is_library {
1.852 albertel 15284: &load_hosts_tab() if (!$loaded);
15285:
1.845 albertel 15286: return exists($libserv{$_[0]});
15287: }
15288:
15289: sub all_library {
1.852 albertel 15290: &load_hosts_tab() if (!$loaded);
15291:
1.845 albertel 15292: return %libserv;
15293: }
15294:
1.1062 droeschl 15295: sub unique_library {
15296: #2x reverse removes all hostnames that appear more than once
15297: my %unique = reverse &all_library();
15298: return reverse %unique;
15299: }
15300:
1.841 albertel 15301: sub get_servers {
1.852 albertel 15302: &load_hosts_tab() if (!$loaded);
15303:
1.841 albertel 15304: my ($domain,$type) = @_;
15305: my %possible_hosts = ($type eq 'library') ? %libserv
15306: : %hostname;
15307: my %result;
1.842 albertel 15308: if (ref($domain) eq 'ARRAY') {
15309: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 15310: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 15311: $result{$host} = $hostname;
15312: }
15313: }
15314: } else {
15315: while ( my ($host,$hostname) = each(%possible_hosts)) {
15316: if ($hostdom{$host} eq $domain) {
15317: $result{$host} = $hostname;
15318: }
1.841 albertel 15319: }
15320: }
15321: return %result;
15322: }
1.845 albertel 15323:
1.1062 droeschl 15324: sub get_unique_servers {
15325: my %unique = reverse &get_servers(@_);
15326: return reverse %unique;
15327: }
15328:
1.844 albertel 15329: sub host_domain {
1.852 albertel 15330: &load_hosts_tab() if (!$loaded);
15331:
1.844 albertel 15332: my ($lonid) = @_;
15333: return $hostdom{$lonid};
15334: }
15335:
1.841 albertel 15336: sub all_domains {
1.852 albertel 15337: &load_hosts_tab() if (!$loaded);
15338:
1.841 albertel 15339: my %seen;
15340: my @uniq = grep(!$seen{$_}++, values(%hostdom));
15341: return @uniq;
15342: }
1.1074 raeburn 15343:
15344: sub internet_dom {
15345: &load_hosts_tab() if (!$loaded);
15346:
15347: my ($lonid) = @_;
15348: return $internetdom{$lonid};
15349: }
1.1107 raeburn 15350:
15351: sub is_LC_dns {
15352: &load_hosts_tab() if (!$loaded);
15353:
15354: my ($hostname) = @_;
15355: return exists($LC_dns_serv{$hostname});
15356: }
15357:
1.1 albertel 15358: }
15359:
1.847 albertel 15360: {
15361: my %iphost;
1.856 albertel 15362: my %name_to_ip;
15363: my %lonid_to_ip;
1.869 albertel 15364:
1.847 albertel 15365: sub get_hosts_from_ip {
15366: my ($ip) = @_;
15367: my %iphosts = &get_iphost();
15368: if (ref($iphosts{$ip})) {
15369: return @{$iphosts{$ip}};
15370: }
15371: return;
1.839 albertel 15372: }
1.864 albertel 15373:
15374: sub reset_hosts_ip_info {
15375: undef(%iphost);
15376: undef(%name_to_ip);
15377: undef(%lonid_to_ip);
15378: }
1.856 albertel 15379:
15380: sub get_host_ip {
15381: my ($lonid) = @_;
15382: if (exists($lonid_to_ip{$lonid})) {
15383: return $lonid_to_ip{$lonid};
15384: }
15385: my $name=&hostname($lonid);
15386: my $ip = gethostbyname($name);
15387: return if (!$ip || length($ip) ne 4);
15388: $ip=inet_ntoa($ip);
15389: $name_to_ip{$name} = $ip;
15390: $lonid_to_ip{$lonid} = $ip;
15391: return $ip;
15392: }
1.847 albertel 15393:
15394: sub get_iphost {
1.1172.2.70 raeburn 15395: my ($ignore_cache,$nocache) = @_;
1.894 albertel 15396:
1.869 albertel 15397: if (!$ignore_cache) {
15398: if (%iphost) {
15399: return %iphost;
15400: }
15401: my ($ip_info,$cached)=
15402: &Apache::lonnet::is_cached_new('iphost','iphost');
15403: if ($cached) {
15404: %iphost = %{$ip_info->[0]};
15405: %name_to_ip = %{$ip_info->[1]};
15406: %lonid_to_ip = %{$ip_info->[2]};
15407: return %iphost;
15408: }
15409: }
1.894 albertel 15410:
15411: # get yesterday's info for fallback
15412: my %old_name_to_ip;
15413: my ($ip_info,$cached)=
15414: &Apache::lonnet::is_cached_new('iphost','iphost');
15415: if ($cached) {
15416: %old_name_to_ip = %{$ip_info->[1]};
15417: }
15418:
1.1172.2.70 raeburn 15419: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 15420: foreach my $name (keys(%name_to_host)) {
1.847 albertel 15421: my $ip;
15422: if (!exists($name_to_ip{$name})) {
15423: $ip = gethostbyname($name);
15424: if (!$ip || length($ip) ne 4) {
1.894 albertel 15425: if (defined($old_name_to_ip{$name})) {
15426: $ip = $old_name_to_ip{$name};
15427: &logthis("Can't find $name defaulting to old $ip");
15428: } else {
15429: &logthis("Name $name no IP found");
15430: next;
15431: }
15432: } else {
15433: $ip=inet_ntoa($ip);
1.847 albertel 15434: }
15435: $name_to_ip{$name} = $ip;
15436: } else {
15437: $ip = $name_to_ip{$name};
1.653 albertel 15438: }
1.888 albertel 15439: foreach my $id (@{ $name_to_host{$name} }) {
15440: $lonid_to_ip{$id} = $ip;
15441: }
15442: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 15443: }
1.1172.2.70 raeburn 15444: unless ($nocache) {
15445: &do_cache_new('iphost','iphost',
15446: [\%iphost,\%name_to_ip,\%lonid_to_ip],
15447: 48*60*60);
15448: }
1.869 albertel 15449:
1.847 albertel 15450: return %iphost;
1.598 albertel 15451: }
15452:
1.992 raeburn 15453: #
15454: # Given a DNS returns the loncapa host name for that DNS
15455: #
15456: sub host_from_dns {
15457: my ($dns) = @_;
15458: my @hosts;
15459: my $ip;
15460:
1.993 raeburn 15461: if (exists($name_to_ip{$dns})) {
1.992 raeburn 15462: $ip = $name_to_ip{$dns};
15463: }
15464: if (!$ip) {
15465: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15466: if (length($ip) == 4) {
15467: $ip = &IO::Socket::inet_ntoa($ip);
15468: }
15469: }
15470: if ($ip) {
15471: @hosts = get_hosts_from_ip($ip);
15472: return $hosts[0];
15473: }
15474: return undef;
1.986 foxr 15475: }
1.992 raeburn 15476:
1.1074 raeburn 15477: sub get_internet_names {
15478: my ($lonid) = @_;
15479: return if ($lonid eq '');
15480: my ($idnref,$cached)=
15481: &Apache::lonnet::is_cached_new('internetnames',$lonid);
15482: if ($cached) {
15483: return $idnref;
15484: }
15485: my $ip = &get_host_ip($lonid);
15486: my @hosts = &get_hosts_from_ip($ip);
15487: my %iphost = &get_iphost();
15488: my (@idns,%seen);
15489: foreach my $id (@hosts) {
15490: my $dom = &host_domain($id);
15491: my $prim_id = &domain($dom,'primary');
15492: my $prim_ip = &get_host_ip($prim_id);
15493: next if ($seen{$prim_ip});
15494: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15495: foreach my $id (@{$iphost{$prim_ip}}) {
15496: my $intdom = &internet_dom($id);
15497: unless (grep(/^\Q$intdom\E$/,@idns)) {
15498: push(@idns,$intdom);
15499: }
15500: }
15501: }
15502: $seen{$prim_ip} = 1;
15503: }
1.1172.2.23 raeburn 15504: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 15505: }
15506:
1.986 foxr 15507: }
15508:
1.1079 raeburn 15509: sub all_loncaparevs {
1.1172.2.39 raeburn 15510: 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);
1.1079 raeburn 15511: }
15512:
1.1172.2.27 raeburn 15513: # ------------------------------------------------------- Read loncaparev table
15514: {
15515: sub load_loncaparevs {
15516: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1172.2.96 raeburn 15517: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1172.2.27 raeburn 15518: while (my $configline=<$config>) {
15519: chomp($configline);
15520: my ($hostid,$loncaparev)=split(/:/,$configline);
15521: $loncaparevs{$hostid}=$loncaparev;
15522: }
15523: close($config);
15524: }
15525: }
15526: }
15527: }
15528:
15529: # ----------------------------------------------------- Read serverhostID table
15530: {
15531: sub load_serverhomeIDs {
15532: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1172.2.96 raeburn 15533: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1172.2.27 raeburn 15534: while (my $configline=<$config>) {
15535: chomp($configline);
15536: my ($name,$id)=split(/:/,$configline);
15537: $serverhomeIDs{$name}=$id;
15538: }
15539: close($config);
15540: }
15541: }
15542: }
15543: }
15544:
15545:
1.862 albertel 15546: BEGIN {
15547:
15548: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15549: unless ($readit) {
15550: {
15551: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15552: %perlvar = (%perlvar,%{$configvars});
15553: }
15554:
15555:
1.1 albertel 15556: # ------------------------------------------------------ Read spare server file
15557: {
1.1172.2.96 raeburn 15558: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 15559:
15560: while (my $configline=<$config>) {
15561: chomp($configline);
1.284 matthew 15562: if ($configline) {
1.784 albertel 15563: my ($host,$type) = split(':',$configline,2);
1.785 albertel 15564: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 15565: push(@{ $spareid{$type} }, $host);
1.1 albertel 15566: }
15567: }
1.448 albertel 15568: close($config);
1.1 albertel 15569: }
1.11 www 15570: # ------------------------------------------------------------ Read permissions
15571: {
1.1172.2.96 raeburn 15572: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11 www 15573:
15574: while (my $configline=<$config>) {
1.448 albertel 15575: chomp($configline);
15576: if ($configline) {
15577: my ($role,$perm)=split(/ /,$configline);
15578: if ($perm ne '') { $pr{$role}=$perm; }
15579: }
1.11 www 15580: }
1.448 albertel 15581: close($config);
1.11 www 15582: }
15583:
15584: # -------------------------------------------- Read plain texts for permissions
15585: {
1.1172.2.96 raeburn 15586: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 15587:
15588: while (my $configline=<$config>) {
1.448 albertel 15589: chomp($configline);
15590: if ($configline) {
1.742 raeburn 15591: my ($short,@plain)=split(/:/,$configline);
15592: %{$prp{$short}} = ();
15593: if (@plain > 0) {
15594: $prp{$short}{'std'} = $plain[0];
15595: for (my $i=1; $i<@plain; $i++) {
15596: $prp{$short}{'alt'.$i} = $plain[$i];
15597: }
15598: }
1.448 albertel 15599: }
1.135 www 15600: }
1.448 albertel 15601: close($config);
1.135 www 15602: }
15603:
15604: # ---------------------------------------------------------- Read package table
15605: {
1.1172.2.96 raeburn 15606: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135 www 15607:
15608: while (my $configline=<$config>) {
1.483 albertel 15609: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 15610: chomp($configline);
15611: my ($short,$plain)=split(/:/,$configline);
15612: my ($pack,$name)=split(/\&/,$short);
15613: if ($plain ne '') {
15614: $packagetab{$pack.'&'.$name.'&name'}=$name;
15615: $packagetab{$short}=$plain;
15616: }
1.11 www 15617: }
1.448 albertel 15618: close($config);
1.329 matthew 15619: }
15620:
1.1172.2.27 raeburn 15621: # --------------------------------------------------------- Read loncaparev table
1.1073 raeburn 15622:
1.1172.2.27 raeburn 15623: &load_loncaparevs();
15624:
15625: # ------------------------------------------------------- Read serverhostID table
15626:
15627: &load_serverhomeIDs();
1.1074 raeburn 15628:
1.1172.2.27 raeburn 15629: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 15630: {
15631: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15632: if (-e $file) {
15633: my $parser = HTML::LCParser->new($file);
15634: while (my $token = $parser->get_token()) {
15635: if ($token->[0] eq 'S') {
15636: my $item = $token->[1];
15637: my $name = $token->[2]{'name'};
15638: my $value = $token->[2]{'value'};
15639: if ($item ne '' && $name ne '' && $value ne '') {
15640: my $release = $parser->get_text();
15641: $release =~ s/(^\s*|\s*$ )//gx;
15642: $needsrelease{$item.':'.$name.':'.$value} = $release;
15643: }
15644: }
15645: }
15646: }
1.1073 raeburn 15647: }
15648:
1.1138 raeburn 15649: # ---------------------------------------------------------- Read managers table
15650: {
15651: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1172.2.96 raeburn 15652: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138 raeburn 15653: while (my $configline=<$config>) {
15654: chomp($configline);
15655: next if ($configline =~ /^\#/);
15656: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15657: $managerstab{$configline} = 1;
15658: }
15659: }
15660: close($config);
15661: }
15662: }
15663: }
15664:
1.329 matthew 15665: # ------------- set up temporary directory
15666: {
1.1117 foxr 15667: $tmpdir = LONCAPA::tempdir();
1.329 matthew 15668:
1.11 www 15669: }
15670:
1.1172.2.104 raeburn 15671: # ------------- set default texengine (domain default overrides this)
15672: {
15673: $deftex = LONCAPA::texengine();
15674: }
15675:
1.1172.2.114 raeburn 15676: # ------------- set default minimum length for passwords for internal auth users
15677: {
15678: $passwdmin = LONCAPA::passwd_min();
15679: }
15680:
1.794 albertel 15681: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
15682: 'compress_threshold'=> 20_000,
15683: });
1.185 www 15684:
1.281 www 15685: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 15686: $dumpcount=0;
1.958 www 15687: $locknum=0;
1.22 www 15688:
1.163 harris41 15689: &logtouch();
1.672 albertel 15690: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 15691: $readit=1;
1.564 albertel 15692: {
15693: use integer;
15694: my $test=(2**32)+1;
1.568 albertel 15695: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 15696: &logthis(" Detected 64bit platform ($_64bit)");
15697: }
1.195 www 15698: }
1.1 albertel 15699: }
1.179 www 15700:
1.1 albertel 15701: 1;
1.191 harris41 15702: __END__
15703:
1.243 albertel 15704: =pod
15705:
1.191 harris41 15706: =head1 NAME
15707:
1.243 albertel 15708: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 15709:
15710: =head1 SYNOPSIS
15711:
1.243 albertel 15712: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 15713:
15714: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15715:
1.243 albertel 15716: Common parameters:
15717:
15718: =over 4
15719:
15720: =item *
15721:
15722: $uname : an internal username (if $cname expecting a course Id specifically)
15723:
15724: =item *
15725:
15726: $udom : a domain (if $cdom expecting a course's domain specifically)
15727:
15728: =item *
15729:
15730: $symb : a resource instance identifier
15731:
15732: =item *
15733:
15734: $namespace : the name of a .db file that contains the data needed or
15735: being set.
15736:
15737: =back
15738:
1.394 bowersj2 15739: =head1 OVERVIEW
1.191 harris41 15740:
1.394 bowersj2 15741: lonnet provides subroutines which interact with the
15742: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15743: about classes, users, and resources.
1.243 albertel 15744:
15745: For many of these objects you can also use this to store data about
15746: them or modify them in various ways.
1.191 harris41 15747:
1.394 bowersj2 15748: =head2 Symbs
1.191 harris41 15749:
1.394 bowersj2 15750: To identify a specific instance of a resource, LON-CAPA uses symbols
15751: or "symbs"X<symb>. These identifiers are built from the URL of the
15752: map, the resource number of the resource in the map, and the URL of
15753: the resource itself. The latter is somewhat redundant, but might help
15754: if maps change.
15755:
15756: An example is
15757:
15758: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15759:
15760: The respective map entry is
15761:
15762: <resource id="19" src="/res/msu/korte/tests/part12.problem"
15763: title="Problem 2">
15764: </resource>
15765:
15766: Symbs are used by the random number generator, as well as to store and
15767: restore data specific to a certain instance of for example a problem.
15768:
15769: =head2 Storing And Retrieving Data
15770:
15771: X<store()>X<cstore()>X<restore()>Three of the most important functions
15772: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15773: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15774: is is the non-critical message twin of cstore. These functions are for
15775: handlers to store a perl hash to a user's permanent data space in an
15776: easy manner, and to retrieve it again on another call. It is expected
15777: that a handler would use this once at the beginning to retrieve data,
15778: and then again once at the end to send only the new data back.
15779:
15780: The data is stored in the user's data directory on the user's
15781: homeserver under the ID of the course.
15782:
15783: The hash that is returned by restore will have all of the previous
15784: value for all of the elements of the hash.
15785:
15786: Example:
15787:
15788: #creating a hash
15789: my %hash;
15790: $hash{'foo'}='bar';
15791:
15792: #storing it
15793: &Apache::lonnet::cstore(\%hash);
15794:
15795: #changing a value
15796: $hash{'foo'}='notbar';
15797:
15798: #adding a new value
15799: $hash{'bar'}='foo';
15800: &Apache::lonnet::cstore(\%hash);
15801:
15802: #retrieving the hash
15803: my %history=&Apache::lonnet::restore();
15804:
15805: #print the hash
15806: foreach my $key (sort(keys(%history))) {
15807: print("\%history{$key} = $history{$key}");
15808: }
15809:
15810: Will print out:
1.191 harris41 15811:
1.394 bowersj2 15812: %history{1:foo} = bar
15813: %history{1:keys} = foo:timestamp
15814: %history{1:timestamp} = 990455579
15815: %history{2:bar} = foo
15816: %history{2:foo} = notbar
15817: %history{2:keys} = foo:bar:timestamp
15818: %history{2:timestamp} = 990455580
15819: %history{bar} = foo
15820: %history{foo} = notbar
15821: %history{timestamp} = 990455580
15822: %history{version} = 2
15823:
15824: Note that the special hash entries C<keys>, C<version> and
15825: C<timestamp> were added to the hash. C<version> will be equal to the
15826: total number of versions of the data that have been stored. The
15827: C<timestamp> attribute will be the UNIX time the hash was
15828: stored. C<keys> is available in every historical section to list which
15829: keys were added or changed at a specific historical revision of a
15830: hash.
15831:
15832: B<Warning>: do not store the hash that restore returns directly. This
15833: will cause a mess since it will restore the historical keys as if the
15834: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 15835:
1.394 bowersj2 15836: Calling convention:
1.191 harris41 15837:
1.1172.2.27 raeburn 15838: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1172.2.64 raeburn 15839: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 15840:
1.394 bowersj2 15841: For more detailed information, see lonnet specific documentation.
1.191 harris41 15842:
1.394 bowersj2 15843: =head1 RETURN MESSAGES
1.191 harris41 15844:
1.394 bowersj2 15845: =over 4
1.191 harris41 15846:
1.394 bowersj2 15847: =item * B<con_lost>: unable to contact remote host
1.191 harris41 15848:
1.394 bowersj2 15849: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15850: when the connection is brought back up
1.191 harris41 15851:
1.394 bowersj2 15852: =item * B<con_failed>: unable to contact remote host and unable to save message
15853: for later delivery
1.191 harris41 15854:
1.967 bisitz 15855: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 15856:
1.394 bowersj2 15857: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 15858: that was requested
1.191 harris41 15859:
1.243 albertel 15860: =back
1.191 harris41 15861:
1.243 albertel 15862: =head1 PUBLIC SUBROUTINES
1.191 harris41 15863:
1.243 albertel 15864: =head2 Session Environment Functions
1.191 harris41 15865:
1.243 albertel 15866: =over 4
1.191 harris41 15867:
1.394 bowersj2 15868: =item *
15869: X<appenv()>
1.949 raeburn 15870: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 15871: the user envirnoment file, and will be restored for each access this
1.620 albertel 15872: user makes during this session, also modifies the %env for the current
1.949 raeburn 15873: process. Optional rolesarrayref - if defined contains a reference to an array
15874: of roles which are exempt from the restriction on modifying user.role entries
15875: in the user's environment.db and in %env.
1.191 harris41 15876:
15877: =item *
1.394 bowersj2 15878: X<delenv()>
1.987 raeburn 15879: B<delenv($delthis,$regexp)>: removes all items from the session
15880: environment file that begin with $delthis. If the
15881: optional second arg - $regexp - is true, $delthis is treated as a
15882: regular expression, otherwise \Q$delthis\E is used.
15883: The values are also deleted from the current processes %env.
1.191 harris41 15884:
1.795 albertel 15885: =item * get_env_multiple($name)
15886:
15887: gets $name from the %env hash, it seemlessly handles the cases where multiple
15888: values may be defined and end up as an array ref.
15889:
15890: returns an array of values
15891:
1.243 albertel 15892: =back
15893:
15894: =head2 User Information
1.191 harris41 15895:
1.243 albertel 15896: =over 4
1.191 harris41 15897:
15898: =item *
1.394 bowersj2 15899: X<queryauthenticate()>
15900: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 15901: authentication scheme
15902:
15903: =item *
1.394 bowersj2 15904: X<authenticate()>
1.1073 raeburn 15905: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 15906: authenticate user from domain's lib servers (first use the current
15907: one). C<$upass> should be the users password.
1.1073 raeburn 15908: $checkdefauth is optional (value is 1 if a check should be made to
15909: authenticate user using default authentication method, and allow
15910: account creation if username does not have account in the domain).
15911: $clientcancheckhost is optional (value is 1 if checking whether the
15912: server can host will occur on the client side in lonauth.pm).
1.191 harris41 15913:
15914: =item *
1.394 bowersj2 15915: X<homeserver()>
15916: B<homeserver($uname,$udom)>: find the server which has
15917: the user's directory and files (there must be only one), this caches
15918: the answer, and also caches if there is a borken connection.
1.191 harris41 15919:
15920: =item *
1.394 bowersj2 15921: X<idget()>
15922: B<idget($udom,@ids)>: find the usernames behind a list of IDs
15923: (IDs are a unique resource in a domain, there must be only 1 ID per
15924: username, and only 1 username per ID in a specific domain) (returns
15925: hash: id=>name,id=>name)
1.191 harris41 15926:
15927: =item *
1.394 bowersj2 15928: X<idrget()>
15929: B<idrget($udom,@unames)>: find the IDs behind a list of
15930: usernames (returns hash: name=>id,name=>id)
1.191 harris41 15931:
15932: =item *
1.394 bowersj2 15933: X<idput()>
15934: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 15935:
15936: =item *
1.394 bowersj2 15937: X<rolesinit()>
1.1169 droeschl 15938: B<rolesinit($udom,$username)>: get user privileges.
15939: returns user role, first access and timer interval hashes
1.243 albertel 15940:
15941: =item *
1.1171 droeschl 15942: X<privileged()>
15943: B<privileged($username,$domain)>: returns a true if user has a
15944: privileged and active role (i.e. su or dc), false otherwise.
15945:
15946: =item *
1.551 albertel 15947: X<getsection()>
15948: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 15949: course $cname, return section name/number or '' for "not in course"
15950: and '-1' for "no section"
15951:
15952: =item *
1.394 bowersj2 15953: X<userenvironment()>
15954: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 15955: passed in @what from the requested user's environment, returns a hash
15956:
1.858 raeburn 15957: =item *
15958: X<userlog_query()>
1.859 albertel 15959: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15960: activity.log file. %filters defines filters applied when parsing the
15961: log file. These can be start or end timestamps, or the type of action
15962: - log to look for Login or Logout events, check for Checkin or
15963: Checkout, role for role selection. The response is in the form
15964: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
15965: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 15966:
1.243 albertel 15967: =back
15968:
15969: =head2 User Roles
15970:
15971: =over 4
15972:
15973: =item *
15974:
1.1172.2.65 raeburn 15975: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
15976: returns codes for allowed actions.
15977:
15978: The first argument is required, all others are optional.
15979:
15980: $priv is the privilege being checked.
15981: $uri contains additional information about what is being checked for access (e.g.,
15982: URL, course ID etc.).
15983: $symb is the unique resource instance identifier in a course; if needed,
15984: but not provided, it will be retrieved via a call to &symbread().
15985: $role is the role for which a priv is being checked (only used if priv is evb).
15986: $clientip is the user's IP address (only used when checking for access to portfolio
15987: files).
15988: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
15989: prevents recursive calls to &allowed.
15990:
1.243 albertel 15991: F: full access
15992: U,I,K: authentication modes (cxx only)
15993: '': forbidden
15994: 1: user needs to choose course
15995: 2: browse allowed
1.766 albertel 15996: A: passphrase authentication needed
1.1172.2.65 raeburn 15997: B: access temporarily blocked because of a blocking event in a course.
1.1172.2.146. .1(raebu 15998:22): D: access blocked because access is required via session initiated via deep-link
1.243 albertel 15999:
16000: =item *
16001:
1.1172.2.13 raeburn 16002: constructaccess($url,$setpriv) : check for access to construction space URL
16003:
16004: See if the owner domain and name in the URL match those in the
16005: expected environment. If so, return three element list
16006: ($ownername,$ownerdomain,$ownerhome).
16007:
16008: Otherwise return the null string.
16009:
16010: If second argument 'setpriv' is true, it assigns the privileges,
16011: and returns the same three element list, unless the owner has
16012: blocked "ad hoc" Domain Coordinator access to the Author Space,
16013: in which case the null string is returned.
16014:
16015: =item *
16016:
1.1172.2.84 raeburn 16017: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16018: define a custom role rolename set privileges in format of lonTabs/roles.tab
16019: for system, domain, and course level. $uname and $udom are optional (current
16020: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 16021:
16022: =item *
16023:
1.988 raeburn 16024: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
16025: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 16026: $type is Course (default) or Community.
1.988 raeburn 16027: If $forcedefault evaluates to true, text returned will be default
16028: text for $type. Otherwise, if this is a course, the text returned
16029: will be a custom name for the role (if defined in the course's
16030: environment). If no custom name is defined the default is returned.
16031:
1.832 raeburn 16032: =item *
16033:
1.1172.2.23 raeburn 16034: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 16035: All arguments are optional. Returns a hash of a roles, either for
16036: co-author/assistant author roles for a user's Construction Space
1.906 albertel 16037: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 16038: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16039: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16040: For each key, value is set to colon-separated start and end times for
16041: the role. If no username and domain are specified, will default to
1.934 raeburn 16042: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 16043: of role statuses (active, future or previous), roles
16044: (e.g., cc,in, st etc.) and domains of the roles which can be used
16045: to restrict the list of roles reported. If no array ref is
16046: provided for types, will default to return only active roles.
1.834 albertel 16047:
1.1172.2.13 raeburn 16048: =item *
16049:
16050: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16051: user: $uname:$udom has a role in the course: $cdom_$cnum.
16052:
16053: Additional optional arguments are: $type (if role checking is to be restricted
16054: to certain user status types -- previous (expired roles), active (currently
16055: available roles) or future (roles available in the future), and
16056: $hideprivileged -- if true will not report course roles for users who
1.1172.2.23 raeburn 16057: have active Domain Coordinator role in course's domain or in additional
16058: domains (specified in 'Domains to check for privileged users' in course
16059: environment -- set via: Course Settings -> Classlists and staff listing).
16060:
16061: =item *
16062:
16063: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16064: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16065: $possdomains and $possroles are optional array refs -- to domains to check and
16066: roles to check. If $possdomains is not specified, a dump will be done of the
16067: users' roles.db to check for a dc or su role in any domain. This can be
16068: time consuming if &privileged is called repeatedly (e.g., when displaying a
16069: classlist), so in such cases, supplying a $possdomains array is preferred, as
16070: this then allows &privileged_by_domain() to be used, which caches the identity
16071: of privileged users, eliminating the need for repeated calls to &dump().
16072:
16073: =item *
16074:
16075: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16076: where the outer hash keys are domains specified in the $possdomains array ref,
16077: next inner hash keys are privileged roles specified in the $roles array ref,
16078: and the innermost hash contains key = value pairs for username:domain = end:start
16079: for active or future "privileged" users with that role in that domain. To avoid
16080: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16081: innerhash are cached using priv_$role and $dom as the identifiers.
1.1172.2.13 raeburn 16082:
1.243 albertel 16083: =back
16084:
16085: =head2 User Modification
16086:
16087: =over 4
16088:
16089: =item *
16090:
1.957 raeburn 16091: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 16092: user for the level given by URL. Optional start and end dates (leave empty
16093: string or zero for "no date")
1.191 harris41 16094:
16095: =item *
16096:
1.243 albertel 16097: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16098: change a users, password, possible return values are: ok,
16099: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16100: refused
1.191 harris41 16101:
16102: =item *
16103:
1.243 albertel 16104: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 16105:
16106: =item *
16107:
1.1058 raeburn 16108: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16109: $forceid,$desiredhome,$email,$inststatus,$candelete) :
16110:
16111: will update user information (firstname,middlename,lastname,generation,
16112: permanentemail), and if forceid is true, student/employee ID also.
16113: A user's institutional affiliation(s) can also be updated.
16114: User information fields will not be overwritten with empty entries
16115: unless the field is included in the $candelete array reference.
16116: This array is included when a single user is modified via "Manage Users",
16117: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 16118:
16119: =item *
16120:
1.286 matthew 16121: modifystudent
16122:
1.957 raeburn 16123: modify a student's enrollment and identification information.
1.1172.2.31 raeburn 16124: The course id is resolved based on the current user's environment.
16125: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 16126: associated with a course.
16127:
1.297 matthew 16128: This call is essentially a wrapper for lonnet::modifyuser and
16129: lonnet::modify_student_enrollment
1.286 matthew 16130:
16131: Inputs:
16132:
16133: =over 4
16134:
1.957 raeburn 16135: =item B<$udom> Student's loncapa domain
1.286 matthew 16136:
1.957 raeburn 16137: =item B<$uname> Student's loncapa login name
1.286 matthew 16138:
1.964 bisitz 16139: =item B<$uid> Student/Employee ID
1.286 matthew 16140:
1.957 raeburn 16141: =item B<$umode> Student's authentication mode
1.286 matthew 16142:
1.957 raeburn 16143: =item B<$upass> Student's password
1.286 matthew 16144:
1.957 raeburn 16145: =item B<$first> Student's first name
1.286 matthew 16146:
1.957 raeburn 16147: =item B<$middle> Student's middle name
1.286 matthew 16148:
1.957 raeburn 16149: =item B<$last> Student's last name
1.286 matthew 16150:
1.957 raeburn 16151: =item B<$gene> Student's generation
1.286 matthew 16152:
1.957 raeburn 16153: =item B<$usec> Student's section in course
1.286 matthew 16154:
16155: =item B<$end> Unix time of the roles expiration
16156:
16157: =item B<$start> Unix time of the roles start date
16158:
16159: =item B<$forceid> If defined, allow $uid to be changed
16160:
16161: =item B<$desiredhome> server to use as home server for student
16162:
1.957 raeburn 16163: =item B<$email> Student's permanent e-mail address
16164:
16165: =item B<$type> Type of enrollment (auto or manual)
16166:
1.963 raeburn 16167: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
16168:
16169: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 16170:
1.963 raeburn 16171: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 16172:
1.963 raeburn 16173: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 16174:
1.1172.2.19 raeburn 16175: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16176:
16177: =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.
1.957 raeburn 16178:
1.286 matthew 16179: =back
1.297 matthew 16180:
16181: =item *
16182:
16183: modify_student_enrollment
16184:
1.1172.2.31 raeburn 16185: Change a student's enrollment status in a class. The environment variable
1.297 matthew 16186: 'role.request.course' must be defined for this function to proceed.
16187:
16188: Inputs:
16189:
16190: =over 4
16191:
1.1172.2.31 raeburn 16192: =item $udom, student's domain
1.297 matthew 16193:
1.1172.2.31 raeburn 16194: =item $uname, student's name
1.297 matthew 16195:
1.1172.2.31 raeburn 16196: =item $uid, student's user id
1.297 matthew 16197:
1.1172.2.31 raeburn 16198: =item $first, student's first name
1.297 matthew 16199:
16200: =item $middle
16201:
16202: =item $last
16203:
16204: =item $gene
16205:
16206: =item $usec
16207:
16208: =item $end
16209:
16210: =item $start
16211:
1.957 raeburn 16212: =item $type
16213:
16214: =item $locktype
16215:
16216: =item $cid
16217:
16218: =item $selfenroll
16219:
16220: =item $context
16221:
1.1172.2.19 raeburn 16222: =item $credits, number of credits student will earn from this class
16223:
1.1172.2.76 raeburn 16224: =item $instsec, institutional course section code for student
16225:
1.297 matthew 16226: =back
16227:
1.191 harris41 16228:
16229: =item *
16230:
1.243 albertel 16231: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16232: custom role; give a custom role to a user for the level given by URL. Specify
16233: name and domain of role author, and role name
1.191 harris41 16234:
16235: =item *
16236:
1.243 albertel 16237: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 16238:
16239: =item *
16240:
1.243 albertel 16241: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16242:
16243: =back
16244:
16245: =head2 Course Infomation
16246:
16247: =over 4
1.191 harris41 16248:
16249: =item *
16250:
1.1118 foxr 16251: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 16252: specified course id, including all environment settings for the
16253: course, the description of the course will be in the hash under the
16254: key 'description'
1.191 harris41 16255:
1.1118 foxr 16256: $options is an optional parameter that if supplied is a hash reference that controls
16257: what how this function works. It has the following key/values:
16258:
16259: =over 4
16260:
16261: =item freshen_cache
16262:
16263: If defined, and the environment cache for the course is valid, it is
16264: returned in the returned hash.
16265:
16266: =item one_time
16267:
16268: If defined, the last cache time is set to _now_
16269:
16270: =item user
16271:
16272: If defined, the supplied username is used instead of the current user.
16273:
16274:
16275: =back
16276:
1.191 harris41 16277: =item *
16278:
1.624 albertel 16279: resdata($name,$domain,$type,@which) : request for current parameter
16280: setting for a specific $type, where $type is either 'course' or 'user',
16281: @what should be a list of parameters to ask about. This routine caches
1.1172.2.31 raeburn 16282: answers for 10 minutes.
1.243 albertel 16283:
1.877 foxr 16284: =item *
16285:
16286: get_courseresdata($courseid, $domain) : dump the entire course resource
16287: data base, returning a hash that is keyed by the resource name and has
16288: values that are the resource value. I believe that the timestamps and
16289: versions are also returned.
16290:
1.243 albertel 16291: =back
16292:
16293: =head2 Course Modification
16294:
16295: =over 4
1.191 harris41 16296:
16297: =item *
16298:
1.243 albertel 16299: writecoursepref($courseid,%prefs) : write preferences (environment
16300: database) for a course
1.191 harris41 16301:
16302: =item *
16303:
1.1011 raeburn 16304: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16305:
16306: =item *
16307:
1.1038 raeburn 16308: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 16309:
1.1167 droeschl 16310: =item *
16311:
16312: is_course($courseid), is_course($cdom, $cnum)
16313:
16314: Accepts either a combined $courseid (in the form of domain_courseid) or the
16315: two component version $cdom, $cnum. It checks if the specified course exists.
16316:
16317: Returns:
16318: undef if the course doesn't exist, otherwise
16319: in scalar context the combined courseid.
16320: in list context the two components of the course identifier, domain and
16321: courseid.
16322:
1.243 albertel 16323: =back
16324:
1.1172.2.109 raeburn 16325: =head2 Bubblesheet Configuration
16326:
16327: =over 4
16328:
16329: =item *
16330:
16331: get_scantron_config($which)
16332:
16333: $which - the name of the configuration to parse from the file.
16334:
16335: Parses and returns the bubblesheet configuration line selected as a
16336: hash of configuration file fields.
16337:
16338:
16339: Returns:
16340: If the named configuration is not in the file, an empty
16341: hash is returned.
16342:
16343: a hash with the fields
16344: name - internal name for the this configuration setup
16345: description - text to display to operator that describes this config
16346: CODElocation - if 0 or the string 'none'
16347: - no CODE exists for this config
16348: if -1 || the string 'letter'
16349: - a CODE exists for this config and is
16350: a string of letters
16351: Unsupported value (but planned for future support)
16352: if a positive integer
16353: - The CODE exists as the first n items from
16354: the question section of the form
16355: if the string 'number'
16356: - The CODE exists for this config and is
16357: a string of numbers
16358: CODEstart - (only matter if a CODE exists) column in the line where
16359: the CODE starts
16360: CODElength - length of the CODE
16361: IDstart - column where the student/employee ID starts
16362: IDlength - length of the student/employee ID info
16363: Qstart - column where the information from the bubbled
16364: 'questions' start
16365: Qlength - number of columns comprising a single bubble line from
16366: the sheet. (usually either 1 or 10)
16367: Qon - either a single character representing the character used
16368: to signal a bubble was chosen in the positional setup, or
16369: the string 'letter' if the letter of the chosen bubble is
16370: in the final, or 'number' if a number representing the
16371: chosen bubble is in the file (1->A 0->J)
16372: Qoff - the character used to represent that a bubble was
16373: left blank
16374: PaperID - if the scanning process generates a unique number for each
16375: sheet scanned the column that this ID number starts in
16376: PaperIDlength - number of columns that comprise the unique ID number
16377: for the sheet of paper
16378: FirstName - column that the first name starts in
16379: FirstNameLength - number of columns that the first name spans
16380: LastName - column that the last name starts in
16381: LastNameLength - number of columns that the last name spans
16382: BubblesPerRow - number of bubbles available in each row used to
16383: bubble an answer. (If not specified, 10 assumed).
16384:
16385:
16386: =item *
16387:
16388: get_scantronformat_file($cdom)
16389:
16390: $cdom - the course's domain (optional); if not supplied, uses
16391: domain for current $env{'request.course.id'}.
16392:
16393: Returns an array containing lines from the scantron format file for
16394: the domain of the course.
16395:
16396: If a url for a custom.tab file is listed in domain's configuration.db,
16397: lines are from this file.
16398:
16399: Otherwise, if a default.tab has been published in RES space by the
16400: domainconfig user, lines are from this file.
16401:
16402: Otherwise, fall back to getting lines from the legacy file on the
16403: local server: /home/httpd/lonTabs/default_scantronformat.tab
16404:
16405: =back
16406:
1.243 albertel 16407: =head2 Resource Subroutines
16408:
16409: =over 4
1.191 harris41 16410:
16411: =item *
16412:
1.243 albertel 16413: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 16414:
16415: =item *
16416:
1.243 albertel 16417: repcopy($filename) : subscribes to the requested file, and attempts to
16418: replicate from the owning library server, Might return
1.607 raeburn 16419: 'unavailable', 'not_found', 'forbidden', 'ok', or
16420: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 16421: resource. Expects the local filesystem pathname
16422: (/home/httpd/html/res/....)
16423:
16424: =back
16425:
16426: =head2 Resource Information
16427:
16428: =over 4
1.191 harris41 16429:
16430: =item *
16431:
1.1172.2.28 raeburn 16432: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
16433: and returns the value of a variety of different possible values,
16434: $varname should be a request string, and the other parameters can be
16435: used to specify who and what one is asking about. Ordinarily, $cid
16436: does not need to be specified, as it is retrived from
16437: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16438: within lonuserstate::loadmap() when initializing a course, before
16439: $env{'request.course.id'} has been set, so it needs to be provided
16440: in that one case.
1.243 albertel 16441:
16442: Possible values for $varname are environment.lastname (or other item
16443: from the envirnment hash), user.name (or someother aspect about the
16444: user), resource.0.maxtries (or some other part and parameter of a
16445: resource)
1.204 albertel 16446:
16447: =item *
16448:
1.243 albertel 16449: directcondval($number) : get current value of a condition; reads from a state
16450: string
1.204 albertel 16451:
16452: =item *
16453:
1.243 albertel 16454: condval($condidx) : value of condition index based on state
1.204 albertel 16455:
16456: =item *
16457:
1.1172.2.146. .13(raeb 16458:-23): metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243 albertel 16459: resource's metadata, $what should be either a specific key, or either
16460: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1172.2.146. .13(raeb 16461:-23): packages that this resource currently uses, the last 3 arguments are
16462:-23): only used internally for recursive metadata.
16463:-23):
16464:-23): the toolsymb is only used where the uri is for an external tool (for which
16465:-23): the uri as well as the symb are guaranteed to be unique).
1.243 albertel 16466:
16467: this function automatically caches all requests
1.191 harris41 16468:
16469: =item *
16470:
1.243 albertel 16471: metadata_query($query,$custom,$customshow) : make a metadata query against the
16472: network of library servers; returns file handle of where SQL and regex results
16473: will be stored for query
1.191 harris41 16474:
16475: =item *
16476:
1.1172.2.66 raeburn 16477: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
16478: return symbolic list entry (all arguments optional).
16479:
16480: Args: filename is the filename (including path) for the file for which a symb
16481: is required; donotrecurse, if true will prevent calls to allowed() being made
16482: to check access status if more than one resource was found in the bighash
16483: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
16484: a randompick); ignorecachednull, if true will prevent a symb of '' being
16485: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16486: cause possible symbs to be checked to determine if they are subject to content
16487: blocking, if so they will not be included as possible symbs; possibles is a
16488: ref to a hash, which, as a side effect, will be populated with all possible
16489: symbs (content blocking not tested).
16490:
1.243 albertel 16491: returns the data handle
1.191 harris41 16492:
16493: =item *
16494:
1.1172.2.17 raeburn 16495: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
1.1172.2.11 raeburn 16496: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 16497: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1172.2.11 raeburn 16498: on failure, user must be in a course, as it assumes the existence of
16499: the course initial hash, and uses $env('request.course.id'}. The third
16500: arg is an optional reference to a scalar. If this arg is passed in the
16501: call to symbverify, it will be set to 1 if the symb has been set to be
16502: encrypted; otherwise it will be null.
1.243 albertel 16503:
1.191 harris41 16504: =item *
16505:
1.243 albertel 16506: symbclean($symb) : removes versions numbers from a symb, returns the
16507: cleaned symb
1.191 harris41 16508:
16509: =item *
16510:
1.243 albertel 16511: is_on_map($uri) : checks if the $uri is somewhere on the current
16512: course map, user must be in a course for it to work.
1.191 harris41 16513:
16514: =item *
16515:
1.243 albertel 16516: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 16517:
16518: =item *
16519:
1.243 albertel 16520: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16521: a random seed, all arguments are optional, if they aren't sent it uses the
16522: environment to derive them. Note: if symb isn't sent and it can't get one
16523: from &symbread it will use the current time as its return value
1.191 harris41 16524:
16525: =item *
16526:
1.243 albertel 16527: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16528: unfakeable, receipt
1.191 harris41 16529:
16530: =item *
16531:
1.620 albertel 16532: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 16533:
16534: =item *
16535:
1.243 albertel 16536: countacc($url) : count the number of accesses to a given URL
1.191 harris41 16537:
16538: =item *
16539:
1.243 albertel 16540: checkout($symb,$tuname,$tudom,$tcrsid) : creates a record of a user having looked at an item, most likely printed out or otherwise using a resource
1.191 harris41 16541:
16542: =item *
16543:
1.243 albertel 16544: checkin($token) : updates that a resource has beeen returned (a hard copy version for instance) and returns the data that $token was Checkout with ($symb, $tuname, $tudom, and $tcrsid)
1.191 harris41 16545:
16546: =item *
16547:
1.243 albertel 16548: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 16549:
16550: =item *
16551:
1.243 albertel 16552: devalidate($symb) : devalidate temporary spreadsheet calculations,
16553: forcing spreadsheet to reevaluate the resource scores next time.
16554:
1.1172.2.13 raeburn 16555: =item *
16556:
16557: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16558: when viewing in course context.
16559:
16560: input: six args -- filename (decluttered), course number, course domain,
16561: url, symb (if registered) and group (if this is a
16562: group item -- e.g., bulletin board, group page etc.).
16563:
16564: output: array of five scalars --
16565: $cfile -- url for file editing if editable on current server
16566: $home -- homeserver of resource (i.e., for author if published,
16567: or course if uploaded.).
16568: $switchserver -- 1 if server switch will be needed.
16569: $forceedit -- 1 if icon/link should be to go to edit mode
16570: $forceview -- 1 if icon/link should be to go to view mode
16571:
16572: =item *
16573:
16574: is_course_upload($file,$cnum,$cdom)
16575:
16576: Used in course context to determine if current file was uploaded to
16577: the course (i.e., would be found in /userfiles/docs on the course's
16578: homeserver.
16579:
16580: input: 3 args -- filename (decluttered), course number and course domain.
16581: output: boolean -- 1 if file was uploaded.
16582:
1.243 albertel 16583: =back
16584:
16585: =head2 Storing/Retreiving Data
16586:
16587: =over 4
1.191 harris41 16588:
16589: =item *
16590:
1.1172.2.64 raeburn 16591: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16592: permanently for this url; hashref needs to be given and should be a \%hashname;
16593: the remaining args aren't required and if they aren't passed or are '' they will
16594: be derived from the env (with the exception of $laststore, which is an
16595: optional arg used when a user's submission is stored in grading).
16596: $laststore is $version=$timestamp, where $version is the most recent version
16597: number retrieved for the corresponding $symb in the $namespace db file, and
16598: $timestamp is the timestamp for that transaction (UNIX time).
16599: $laststore is currently only passed when cstore() is called by
16600: structuretags::finalize_storage().
1.191 harris41 16601:
16602: =item *
16603:
1.1172.2.64 raeburn 16604: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16605: but uses critical subroutine
1.191 harris41 16606:
16607: =item *
16608:
1.243 albertel 16609: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16610: all args are optional
1.191 harris41 16611:
16612: =item *
16613:
1.717 albertel 16614: dumpstore($namespace,$udom,$uname,$regexp,$range) :
16615: dumps the complete (or key matching regexp) namespace into a hash
16616: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16617: normally &store()ed into
16618:
16619: $range should be either an integer '100' (give me the first 100
16620: matching records)
16621: or be two integers sperated by a - with no spaces
16622: '30-50' (give me the 30th through the 50th matching
16623: records)
16624:
16625:
16626: =item *
16627:
1.1172.2.59 raeburn 16628: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 16629: replaces a &store() version of data with a replacement set of data
16630: for a particular resource in a namespace passed in the $storehash hash
1.1172.2.59 raeburn 16631: reference. If $tolog is true, the transaction is logged in the courselog
16632: with an action=PUTSTORE.
1.717 albertel 16633:
16634: =item *
16635:
1.243 albertel 16636: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16637: works very similar to store/cstore, but all data is stored in a
16638: temporary location and can be reset using tmpreset, $storehash should
16639: be a hash reference, returns nothing on success
1.191 harris41 16640:
16641: =item *
16642:
1.243 albertel 16643: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16644: similar to restore, but all data is stored in a temporary location and
16645: can be reset using tmpreset. Returns a hash of values on success,
16646: error string otherwise.
1.191 harris41 16647:
16648: =item *
16649:
1.243 albertel 16650: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16651: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 16652:
16653: =item *
16654:
1.243 albertel 16655: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16656: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 16657:
16658: =item *
16659:
1.243 albertel 16660: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16661: namesp ($udom and $uname are optional)
1.191 harris41 16662:
16663: =item *
16664:
1.702 albertel 16665: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 16666: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 16667: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 16668:
1.702 albertel 16669: $range should be either an integer '100' (give me the first 100
16670: matching records)
16671: or be two integers sperated by a - with no spaces
16672: '30-50' (give me the 30th through the 50th matching
16673: records)
1.449 matthew 16674: =item *
16675:
16676: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16677: $store can be a scalar, an array reference, or if the amount to be
16678: incremented is > 1, a hash reference.
16679:
16680: ($udom and $uname are optional)
1.191 harris41 16681:
16682: =item *
16683:
1.243 albertel 16684: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16685: ($udom and $uname are optional)
1.191 harris41 16686:
16687: =item *
16688:
1.243 albertel 16689: cput($namespace,$storehash,$udom,$uname) : critical put
16690: ($udom and $uname are optional)
1.191 harris41 16691:
16692: =item *
16693:
1.748 albertel 16694: newput($namespace,$storehash,$udom,$uname) :
16695:
16696: Attempts to store the items in the $storehash, but only if they don't
16697: currently exist, if this succeeds you can be certain that you have
16698: successfully created a new key value pair in the $namespace db.
16699:
16700:
16701: Args:
16702: $namespace: name of database to store values to
16703: $storehash: hashref to store to the db
16704: $udom: (optional) domain of user containing the db
16705: $uname: (optional) name of user caontaining the db
16706:
16707: Returns:
16708: 'ok' -> succeeded in storing all keys of $storehash
16709: 'key_exists: <key>' -> failed to anything out of $storehash, as at
16710: least <key> already existed in the db (other
16711: requested keys may also already exist)
1.967 bisitz 16712: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 16713: 'con_lost' -> unable to contact request server
16714: 'refused' -> action was not allowed by remote machine
16715:
16716:
16717: =item *
16718:
1.243 albertel 16719: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16720: reference filled in from namesp (encrypts the return communication)
16721: ($udom and $uname are optional)
1.191 harris41 16722:
16723: =item *
16724:
1.243 albertel 16725: log($udom,$name,$home,$message) : write to permanent log for user; use
16726: critical subroutine
16727:
1.806 raeburn 16728: =item *
16729:
1.860 raeburn 16730: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16731: array reference filled in from namespace found in domain level on either
16732: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 16733:
16734: =item *
16735:
1.860 raeburn 16736: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
16737: domain level either on specified domain server ($uhome) or primary domain
16738: server ($udom and $uhome are optional)
1.806 raeburn 16739:
1.943 raeburn 16740: =item *
16741:
1.1172.2.35 raeburn 16742: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
16743: for: authentication, language, quotas, timezone, date locale, and portal URL in
16744: the target domain.
16745:
16746: May also include additional key => value pairs for the following groups:
16747:
16748: =over
16749:
16750: =item
16751: disk quotas (MB allocated by default to portfolios and authoring spaces).
16752:
16753: =over
16754:
16755: =item defaultquota, authorquota
16756:
16757: =back
16758:
16759: =item
16760: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16761: portfolio for users).
16762:
16763: =over
16764:
16765: =item
16766: aboutme, blog, webdav, portfolio
16767:
16768: =back
16769:
16770: =item
16771: requestcourses: ability to request courses, and how requests are processed.
16772:
16773: =over
16774:
16775: =item
1.1172.2.37 raeburn 16776: official, unofficial, community, textbook
1.1172.2.35 raeburn 16777:
16778: =back
16779:
16780: =item
16781: inststatus: types of institutional affiliation, and order in which they are displayed.
16782:
16783: =over
16784:
16785: =item
1.1172.2.44 raeburn 16786: inststatustypes, inststatusorder, inststatusguest
1.1172.2.35 raeburn 16787:
16788: =back
16789:
16790: =item
16791: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16792: for course's uploaded content.
16793:
16794: =over
16795:
16796: =item
1.1172.2.68 raeburn 16797: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
16798: communityquota, textbookquota
1.1172.2.35 raeburn 16799:
16800: =back
16801:
16802: =item
16803: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16804: on your servers.
16805:
16806: =over
16807:
16808: =item
16809: remotesessions, hostedsessions
16810:
16811: =back
16812:
16813: =back
16814:
16815: In cases where a domain coordinator has never used the "Set Domain Configuration"
16816: utility to create a configuration.db file on a domain's primary library server
16817: only the following domain defaults: auth_def, auth_arg_def, lang_def
16818: -- corresponding values are authentication type (internal, krb4, krb5,
16819: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
16820: will be available. Values are retrieved from cache (if current), unless the
16821: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16822: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16823:
16824: Typical usage:
1.943 raeburn 16825:
1.1172.2.35 raeburn 16826: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 16827:
1.243 albertel 16828: =back
16829:
16830: =head2 Network Status Functions
16831:
16832: =over 4
1.191 harris41 16833:
16834: =item *
16835:
1.1137 raeburn 16836: dirlist() : return directory list based on URI (first arg).
16837:
16838: Inputs: 1 required, 5 optional.
16839:
16840: =over
16841:
16842: =item
16843: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16844:
16845: =item
16846: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
16847:
16848: =item
16849: $username - username of user/course to be listed. Extracted from $uri if absent.
16850:
16851: =item
16852: $getpropath - boolean: 1 if prepend path using &propath().
16853:
16854: =item
16855: $getuserdir - boolean: 1 if prepend path for "userfiles".
16856:
16857: =item
16858: $alternateRoot - path to prepend in place of path from $uri.
16859:
16860: =back
16861:
16862: Returns: Array of up to two items.
16863:
16864: =over
16865:
16866: a reference to an array of files/subdirectories
16867:
16868: =over
16869:
16870: Each element in the array of files/subdirectories is a & separated list of
16871: item name and the result of running stat on the item. If dirlist was requested
16872: for a file instead of a directory, the item name will be ''. For a directory
16873: listing, if the item is a metadata file, the element will end &N&M
16874: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16875: default copyright set (1).
16876:
16877: =back
16878:
16879: a scalar containing error condition (if encountered).
16880:
16881: =over
16882:
16883: =item
16884: no_host (no homeserver identified for $username:$domain).
16885:
16886: =item
16887: no_such_host (server contacted for listing not identified as valid host).
16888:
16889: =item
16890: con_lost (connection to remote server failed).
16891:
16892: =item
16893: refused (invalid $username:$domain received on lond side).
16894:
16895: =item
16896: no_such_dir (directory at specified path on lond side does not exist).
16897:
16898: =item
16899: empty (directory at specified path on lond side is empty).
16900:
16901: =over
16902:
16903: This is currently not encountered because the &ls3, &ls2,
16904: &ls (_handler) routines on the lond side do not filter out
16905: . and .. from a directory listing.
16906:
16907: =back
16908:
16909: =back
16910:
16911: =back
1.191 harris41 16912:
16913: =item *
16914:
1.243 albertel 16915: spareserver() : find server with least workload from spare.tab
16916:
1.986 foxr 16917:
16918: =item *
16919:
16920: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16921: if there is no corresponding loncapa host.
16922:
1.243 albertel 16923: =back
16924:
1.986 foxr 16925:
1.243 albertel 16926: =head2 Apache Request
16927:
16928: =over 4
1.191 harris41 16929:
16930: =item *
16931:
1.243 albertel 16932: ssi($url,%hash) : server side include, does a complete request cycle on url to
16933: localhost, posts hash
16934:
16935: =back
16936:
16937: =head2 Data to String to Data
16938:
16939: =over 4
1.191 harris41 16940:
16941: =item *
16942:
1.243 albertel 16943: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16944: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 16945:
16946: =item *
16947:
1.243 albertel 16948: hashref2str($hashref) : convert a hashref into a string complete with
16949: escaping and '=' and '&' separators, supports elements that are
16950: arrayrefs and hashrefs
1.191 harris41 16951:
16952: =item *
16953:
1.243 albertel 16954: arrayref2str($arrayref) : convert an arrayref into a string complete
16955: with escaping and '&' separators, supports elements that are arrayrefs
16956: and hashrefs
1.191 harris41 16957:
16958: =item *
16959:
1.243 albertel 16960: str2hash($string) : convert string to hash using unescaping and
16961: splitting on '=' and '&', supports elements that are arrayrefs and
16962: hashrefs
1.191 harris41 16963:
16964: =item *
16965:
1.243 albertel 16966: str2array($string) : convert string to hash using unescaping and
16967: splitting on '&', supports elements that are arrayrefs and hashrefs
16968:
16969: =back
16970:
16971: =head2 Logging Routines
16972:
16973:
16974: These routines allow one to make log messages in the lonnet.log and
16975: lonnet.perm logfiles.
1.191 harris41 16976:
1.1119 foxr 16977: =over 4
16978:
1.191 harris41 16979: =item *
16980:
1.243 albertel 16981: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 16982:
16983: =item *
16984:
1.243 albertel 16985: logthis() : append message to the normal lonnet.log file, it gets
16986: preiodically rolled over and deleted.
1.191 harris41 16987:
16988: =item *
16989:
1.243 albertel 16990: logperm() : append a permanent message to lonnet.perm.log, this log
16991: file never gets deleted by any automated portion of the system, only
16992: messages of critical importance should go in here.
16993:
1.1119 foxr 16994:
1.243 albertel 16995: =back
16996:
16997: =head2 General File Helper Routines
16998:
16999: =over 4
1.191 harris41 17000:
17001: =item *
17002:
1.481 raeburn 17003: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17004: (a) files in /uploaded
17005: (i) If a local copy of the file exists -
17006: compares modification date of local copy with last-modified date for
17007: definitive version stored on home server for course. If local copy is
17008: stale, requests a new version from the home server and stores it.
17009: If the original has been removed from the home server, then local copy
17010: is unlinked.
17011: (ii) If local copy does not exist -
17012: requests the file from the home server and stores it.
17013:
17014: If $caller is 'uploadrep':
17015: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17016: for request for files originally uploaded via DOCS.
17017: - returns 'ok' if fresh local copy now available, -1 otherwise.
17018:
17019: Otherwise:
17020: This indicates a call from the content generation phase of the request.
17021: - returns the entire contents of the file or -1.
17022:
17023: (b) files in /res
17024: - returns the entire contents of a file or -1;
17025: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 17026:
1.712 albertel 17027:
17028: =item *
17029:
17030: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17031: reference
17032:
17033: returns either a stat() list of data about the file or an empty list
17034: if the file doesn't exist or couldn't find out about it (connection
17035: problems or user unknown)
17036:
1.191 harris41 17037: =item *
17038:
1.243 albertel 17039: filelocation($dir,$file) : returns file system location of a file
17040: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17041: directory that relative $file lookups are to looked in ($dir of /a/dir
17042: and a file of ../bob will become /a/bob)
1.191 harris41 17043:
17044: =item *
17045:
17046: hreflocation($dir,$file) : returns file system location or a URL; same as
17047: filelocation except for hrefs
17048:
17049: =item *
17050:
1.1172.2.49 raeburn 17051: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17052: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 17053:
1.243 albertel 17054: =back
17055:
1.608 albertel 17056: =head2 Usererfile file routines (/uploaded*)
17057:
17058: =over 4
17059:
17060: =item *
17061:
17062: userfileupload(): main rotine for putting a file in a user or course's
17063: filespace, arguments are,
17064:
1.620 albertel 17065: formname - required - this is the name of the element in $env where the
1.608 albertel 17066: filename, and the contents of the file to create/modifed exist
1.620 albertel 17067: the filename is in $env{'form.'.$formname.'.filename'} and the
17068: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 17069: context - if coursedoc, store the file in the course of the active role
17070: of the current user;
17071: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17072: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 17073: subdir - required - subdirectory to put the file in under ../userfiles/
17074: if undefined, it will be placed in "unknown"
17075:
17076: (This routine calls clean_filename() to remove any dangerous
17077: characters from the filename, and then calls finuserfileupload() to
17078: complete the transaction)
17079:
17080: returns either the url of the uploaded file (/uploaded/....) if successful
17081: and /adm/notfound.html if unsuccessful
17082:
17083: =item *
17084:
17085: clean_filename(): routine for cleaing a filename up for storage in
17086: userfile space, argument is:
17087:
17088: filename - proposed filename
17089:
17090: returns: the new clean filename
17091:
17092: =item *
17093:
1.1090 raeburn 17094: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 17095: userspace, probably shouldn't be called directly
17096:
17097: docuname: username or courseid of destination for the file
17098: docudom: domain of user/course of destination for the file
17099: formname: same as for userfileupload()
1.1090 raeburn 17100: fname: filename (including subdirectories) for the file
17101: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1172.2.109 raeburn 17102: if hashref, and context is scantron, will convert csv format to standard format
1.1090 raeburn 17103: allfiles: reference to hash used to store objects found by parser
17104: codebase: reference to hash used for codebases of java objects found by parser
17105: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17106: thumbheight: height (pixels) of thumbnail to be created for uploaded image
17107: resizewidth: width to be used to resize image using resizeImage from ImageMagick
17108: resizeheight: height to be used to resize image using resizeImage from ImageMagick
17109: context: if 'overwrite', will move the uploaded file from its temporary location to
17110: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 17111: mimetype: reference to scalar to accommodate mime type determined
17112: from File::MMagic if $parser = parse.
1.608 albertel 17113:
17114: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 17115: and /adm/notfound.html if unsuccessful (or an error message if context
17116: was 'overwrite').
17117:
1.608 albertel 17118:
17119: =item *
17120:
17121: renameuserfile(): renames an existing userfile to a new name
17122:
17123: Args:
17124: docuname: username or courseid of destination for the file
17125: docudom: domain of user/course of destination for the file
17126: old: current file name (including any subdirs under userfiles)
17127: new: desired file name (including any subdirs under userfiles)
17128:
17129: =item *
17130:
17131: mkdiruserfile(): creates a directory is a userfiles dir
17132:
17133: Args:
17134: docuname: username or courseid of destination for the file
17135: docudom: domain of user/course of destination for the file
17136: dir: dir to create (including any subdirs under userfiles)
17137:
17138: =item *
17139:
17140: removeuserfile(): removes a file that exists in userfiles
17141:
17142: Args:
17143: docuname: username or courseid of destination for the file
17144: docudom: domain of user/course of destination for the file
17145: fname: filname to delete (including any subdirs under userfiles)
17146:
17147: =item *
17148:
17149: removeuploadedurl(): convience function for removeuserfile()
17150:
17151: Args:
17152: url: a full /uploaded/... url to delete
17153:
1.747 albertel 17154: =item *
17155:
17156: get_portfile_permissions():
17157: Args:
17158: domain: domain of user or course contain the portfolio files
17159: user: name of user or num of course contain the portfolio files
17160: Returns:
17161: hashref of a dump of the proper file_permissions.db
17162:
17163:
17164: =item *
17165:
17166: get_access_controls():
17167:
17168: Args:
17169: current_permissions: the hash ref returned from get_portfile_permissions()
17170: group: (optional) the group you want the files associated with
17171: file: (optional) the file you want access info on
17172:
17173: Returns:
1.749 raeburn 17174: a hash (keys are file names) of hashes containing
17175: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17176: values are XML containing access control settings (see below)
1.747 albertel 17177:
17178: Internal notes:
17179:
1.749 raeburn 17180: access controls are stored in file_permissions.db as key=value pairs.
17181: key -> path to file/file_name\0uniqueID:scope_end_start
17182: where scope -> public,guest,course,group,domains or users.
17183: end -> UNIX time for end of access (0 -> no end date)
17184: start -> UNIX time for start of access
17185:
17186: value -> XML description of access control
17187: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17188: <start></start>
17189: <end></end>
17190:
17191: <password></password> for scope type = guest
17192:
17193: <domain></domain> for scope type = course or group
17194: <number></number>
17195: <roles id="">
17196: <role></role>
17197: <access></access>
17198: <section></section>
17199: <group></group>
17200: </roles>
17201:
17202: <dom></dom> for scope type = domains
17203:
17204: <users> for scope type = users
17205: <user>
17206: <uname></uname>
17207: <udom></udom>
17208: </user>
17209: </users>
17210: </scope>
17211:
17212: Access data is also aggregated for each file in an additional key=value pair:
17213: key -> path to file/file_name\0accesscontrol
17214: value -> reference to hash
17215: hash contains key = value pairs
17216: where key = uniqueID:scope_end_start
17217: value = UNIX time record was last updated
17218:
17219: Used to improve speed of look-ups of access controls for each file.
17220:
17221: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17222:
1.1172.2.13 raeburn 17223: =item *
17224:
1.749 raeburn 17225: modify_access_controls():
17226:
17227: Modifies access controls for a portfolio file
17228: Args
17229: 1. file name
17230: 2. reference to hash of required changes,
17231: 3. domain
17232: 4. username
17233: where domain,username are the domain of the portfolio owner
17234: (either a user or a course)
17235:
17236: Returns:
17237: 1. result of additions or updates ('ok' or 'error', with error message).
17238: 2. result of deletions ('ok' or 'error', with error message).
17239: 3. reference to hash of any new or updated access controls.
17240: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17241: key = integer (inbound ID)
1.1172.2.13 raeburn 17242: value = uniqueID
17243:
17244: =item *
17245:
17246: get_timebased_id():
17247:
17248: Attempts to get a unique timestamp-based suffix for use with items added to a
17249: course via the Course Editor (e.g., folders, composite pages,
17250: group bulletin boards).
17251:
17252: Args: (first three required; six others optional)
17253:
17254: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17255: docssequence, or name of group
17256:
17257: 2. keyid (alphanumeric): name of temporary locking key in hash,
17258: e.g., num, boardids
17259:
17260: 3. namespace: name of gdbm file used to store suffixes already assigned;
17261: file will be named nohist_namespace.db
17262:
17263: 4. cdom: domain of course; default is current course domain from %env
17264:
17265: 5. cnum: course number; default is current course number from %env
17266:
17267: 6. idtype: set to concat if an additional digit is to be appended to the
17268: unix timestamp to form the suffix, if the plain timestamp is already
17269: in use. Default is to not do this, but simply increment the unix
17270: timestamp by 1 until a unique key is obtained.
17271:
17272: 7. who: holder of locking key; defaults to user:domain for user.
17273:
17274: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
17275: retrying); default is 3.
17276:
17277: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
17278:
17279: Returns:
17280:
17281: 1. suffix obtained (numeric)
17282:
17283: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17284:
17285: 3. error: contains (localized) error message if an error occurred.
17286:
1.747 albertel 17287:
1.608 albertel 17288: =back
17289:
1.243 albertel 17290: =head2 HTTP Helper Routines
17291:
17292: =over 4
17293:
1.191 harris41 17294: =item *
17295:
17296: escape() : unpack non-word characters into CGI-compatible hex codes
17297:
17298: =item *
17299:
17300: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17301:
1.243 albertel 17302: =back
17303:
17304: =head1 PRIVATE SUBROUTINES
17305:
17306: =head2 Underlying communication routines (Shouldn't call)
17307:
17308: =over 4
17309:
17310: =item *
17311:
17312: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17313:
17314: =item *
17315:
17316: reply() : uses subreply to send a message to remote machine, logs all failures
17317:
17318: =item *
17319:
17320: critical() : passes a critical message to another server; if cannot
17321: get through then place message in connection buffer directory and
17322: returns con_delayed, if incapable of saving message, returns
17323: con_failed
17324:
17325: =item *
17326:
17327: reconlonc() : tries to reconnect lonc client processes.
17328:
17329: =back
17330:
17331: =head2 Resource Access Logging
17332:
17333: =over 4
17334:
17335: =item *
17336:
17337: flushcourselogs() : flush (save) buffer logs and access logs
17338:
17339: =item *
17340:
17341: courselog($what) : save message for course in hash
17342:
17343: =item *
17344:
17345: courseacclog($what) : save message for course using &courselog(). Perform
17346: special processing for specific resource types (problems, exams, quizzes, etc).
17347:
1.191 harris41 17348: =item *
17349:
17350: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17351: as a PerlChildExitHandler
1.243 albertel 17352:
17353: =back
17354:
17355: =head2 Other
17356:
17357: =over 4
17358:
17359: =item *
17360:
17361: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 17362:
17363: =back
17364:
17365: =cut
1.877 foxr 17366:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>