Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1172.2.146.2.28
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1172.2.146. .28(raeb 4:-25): # $Id: lonnet.pm,v 1.1172.2.146.2.27 2025/01/16 22:22:40 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.1172.2.146. .27(raeb 6525:-25): unless (($symb eq '_feedback') || ($symb eq '_discussion')) {
6526:-25): $symb=&symbclean($symb);
6527:-25): }
1.122 albertel 6528: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6529:
1.620 albertel 6530: if (!$domain) { $domain=$env{'user.domain'}; }
6531: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6532:
1.1172.2.146. .27(raeb 6533:-25): unless (($symb eq '_feedback') || ($symb eq '_discussion')) {
6534:-25): &devalidate($symb,$stuname,$domain);
6535:-25): }
1.109 www 6536:
6537: $symb=escape($symb);
1.187 www 6538: if (!$namespace) {
1.620 albertel 6539: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6540: return '';
6541: }
6542: }
1.620 albertel 6543: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6544:
1.1172.2.146. .27(raeb 6545:-25): $$storehash{'ip'} = &get_requestor_ip();
1.447 www 6546: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 6547:
1.47 www 6548: my $namevalue='';
1.800 albertel 6549: foreach my $key (keys(%$storehash)) {
6550: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6551: }
1.47 www 6552: $namevalue=~s/\&$//;
1.187 www 6553: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 6554: return critical
1.1172.2.64 raeburn 6555: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47 www 6556: }
6557:
1.9 www 6558: # --------------------------------------------------------------------- Restore
6559:
6560: sub restore {
1.124 www 6561: my ($symb,$namespace,$domain,$stuname) = @_;
6562: my $home='';
6563:
1.168 albertel 6564: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6565:
1.122 albertel 6566: if (!$symb) {
1.1172.2.26 raeburn 6567: return if ($namespace eq 'courserequests');
6568: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 6569: } else {
1.1172.2.26 raeburn 6570: unless ($namespace eq 'courserequests') {
6571: $symb=&escape(&symbclean($symb));
6572: }
1.122 albertel 6573: }
1.188 www 6574: if (!$namespace) {
1.620 albertel 6575: unless ($namespace=$env{'request.course.id'}) {
1.188 www 6576: return '';
6577: }
6578: }
1.620 albertel 6579: if (!$domain) { $domain=$env{'user.domain'}; }
6580: if (!$stuname) { $stuname=$env{'user.name'}; }
6581: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 6582: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
6583:
1.12 www 6584: my %returnhash=();
1.800 albertel 6585: foreach my $line (split(/\&/,$answer)) {
6586: my ($name,$value)=split(/\=/,$line);
1.591 albertel 6587: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 6588: }
1.75 www 6589: my $version;
6590: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 6591: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
6592: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 6593: }
1.75 www 6594: }
1.13 www 6595: return %returnhash;
1.34 www 6596: }
6597:
6598: # ---------------------------------------------------------- Course Description
1.1118 foxr 6599: #
6600: #
1.34 www 6601:
6602: sub coursedescription {
1.731 albertel 6603: my ($courseid,$args)=@_;
1.34 www 6604: $courseid=~s/^\///;
1.49 www 6605: $courseid=~s/\_/\//g;
1.34 www 6606: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 6607: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 6608: my $normalid=$cdomain.'_'.$cnum;
6609: # need to always cache even if we get errors otherwise we keep
6610: # trying and trying and trying to get the course description.
6611: my %envhash=();
6612: my %returnhash=();
1.731 albertel 6613:
6614: my $expiretime=600;
6615: if ($env{'request.course.id'} eq $normalid) {
6616: $expiretime=120;
6617: }
6618:
6619: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
6620: if (!$args->{'freshen_cache'}
6621: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
6622: foreach my $key (keys(%env)) {
6623: next if ($key !~ /^\Q$prefix\E(.*)/);
6624: my ($setting) = $1;
6625: $returnhash{$setting} = $env{$key};
6626: }
6627: return %returnhash;
6628: }
6629:
1.1118 foxr 6630: # get the data again
6631:
1.731 albertel 6632: if (!$args->{'one_time'}) {
6633: $envhash{'course.'.$normalid.'.last_cache'}=time;
6634: }
1.811 albertel 6635:
1.34 www 6636: if ($chome ne 'no_host') {
1.302 albertel 6637: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 6638: if (!exists($returnhash{'con_lost'})) {
1.1118 foxr 6639: my $username = $env{'user.name'}; # Defult username
6640: if(defined $args->{'user'}) {
6641: $username = $args->{'user'};
6642: }
1.129 albertel 6643: $returnhash{'home'}= $chome;
6644: $returnhash{'domain'} = $cdomain;
6645: $returnhash{'num'} = $cnum;
1.741 raeburn 6646: if (!defined($returnhash{'type'})) {
6647: $returnhash{'type'} = 'Course';
6648: }
1.130 albertel 6649: while (my ($name,$value) = each %returnhash) {
1.53 www 6650: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 6651: }
1.270 www 6652: $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117 foxr 6653: $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118 foxr 6654: $username.'_'.$cdomain.'_'.$cnum;
1.60 www 6655: $envhash{'course.'.$normalid.'.home'}=$chome;
6656: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
6657: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 6658: }
6659: }
1.731 albertel 6660: if (!$args->{'one_time'}) {
1.949 raeburn 6661: &appenv(\%envhash);
1.731 albertel 6662: }
1.302 albertel 6663: return %returnhash;
1.461 www 6664: }
6665:
1.1080 raeburn 6666: sub update_released_required {
6667: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
6668: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
6669: $cid = $env{'request.course.id'};
6670: $cdom = $env{'course.'.$cid.'.domain'};
6671: $cnum = $env{'course.'.$cid.'.num'};
6672: $chome = $env{'course.'.$cid.'.home'};
6673: }
6674: if ($needsrelease) {
6675: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
6676: my $needsupdate;
6677: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
6678: $needsupdate = 1;
6679: } else {
6680: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
6681: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
6682: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
6683: $needsupdate = 1;
6684: }
6685: }
6686: if ($needsupdate) {
6687: my %needshash = (
6688: 'internal.releaserequired' => $needsrelease,
6689: );
6690: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
6691: if ($putresult eq 'ok') {
6692: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
6693: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6694: if (ref($crsinfo{$cid}) eq 'HASH') {
6695: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
6696: &courseidput($cdom,\%crsinfo,$chome,'notime');
6697: }
6698: }
6699: }
6700: }
6701: return;
6702: }
6703:
1.461 www 6704: # -------------------------------------------------See if a user is privileged
6705:
6706: sub privileged {
1.1172.2.23 raeburn 6707: my ($username,$domain,$possdomains,$possroles)=@_;
1.1170 droeschl 6708: my $now = time;
1.1172.2.23 raeburn 6709: my $roles;
6710: if (ref($possroles) eq 'ARRAY') {
6711: $roles = $possroles;
6712: } else {
6713: $roles = ['dc','su'];
6714: }
6715: if (ref($possdomains) eq 'ARRAY') {
6716: my %privileged = &privileged_by_domain($possdomains,$roles);
6717: foreach my $dom (@{$possdomains}) {
6718: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
6719: (ref($privileged{$dom}) eq 'HASH')) {
6720: foreach my $role (@{$roles}) {
6721: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6722: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
6723: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
6724: return 1 unless (($end && $end < $now) ||
6725: ($start && $start > $now));
6726: }
6727: }
6728: }
6729: }
6730: }
6731: } else {
6732: my %rolesdump = &dump("roles", $domain, $username) or return 0;
6733: my $now = time;
1.1170 droeschl 6734:
1.1172.2.58 raeburn 6735: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170 droeschl 6736: my ($trole, $tend, $tstart) = split(/_/, $role);
1.1172.2.23 raeburn 6737: if (grep(/^\Q$trole\E$/,@{$roles})) {
6738: return 1 unless ($tend && $tend < $now)
6739: or ($tstart && $tstart > $now);
1.1170 droeschl 6740: }
1.1172.2.23 raeburn 6741: }
6742: }
1.461 www 6743: return 0;
1.9 www 6744: }
1.1 albertel 6745:
1.1172.2.23 raeburn 6746: sub privileged_by_domain {
6747: my ($domains,$roles) = @_;
6748: my %privileged = ();
6749: my $cachetime = 60*60*24;
6750: my $now = time;
6751: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
6752: return %privileged;
6753: }
6754: foreach my $dom (@{$domains}) {
6755: next if (ref($privileged{$dom}) eq 'HASH');
6756: my $needroles;
6757: foreach my $role (@{$roles}) {
6758: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
6759: if (defined($cached)) {
6760: if (ref($result) eq 'HASH') {
6761: $privileged{$dom}{$role} = $result;
6762: }
6763: } else {
6764: $needroles = 1;
6765: }
6766: }
6767: if ($needroles) {
6768: my %dompersonnel = &get_domain_roles($dom,$roles);
6769: $privileged{$dom} = {};
6770: foreach my $server (keys(%dompersonnel)) {
6771: if (ref($dompersonnel{$server}) eq 'HASH') {
6772: foreach my $item (keys(%{$dompersonnel{$server}})) {
6773: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
6774: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
6775: next if ($end && $end < $now);
6776: $privileged{$dom}{$trole}{$uname.':'.$udom} =
6777: $dompersonnel{$server}{$item};
6778: }
6779: }
6780: }
6781: if (ref($privileged{$dom}) eq 'HASH') {
6782: foreach my $role (@{$roles}) {
6783: if (ref($privileged{$dom}{$role}) eq 'HASH') {
6784: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
6785: } else {
6786: my %hash = ();
6787: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
6788: }
6789: }
6790: }
6791: }
6792: }
6793: return %privileged;
6794: }
6795:
1.103 harris41 6796: # -------------------------------------------------------- Get user privileges
1.11 www 6797:
6798: sub rolesinit {
1.1169 droeschl 6799: my ($domain, $username) = @_;
6800: my %userroles = ('user.login.time' => time);
6801: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
6802:
6803: # firstaccess and timerinterval are related to timed maps/resources.
6804: # also, blocking can be triggered by an activating timer
6805: # it's saved in the user's %env.
6806: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
6807: my %timerinterval = &dump('timerinterval', $domain, $username);
6808: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
1.1172.2.146. .18(raeb 6809:-24): %timerintchk, %timerintenv,%coauthorenv);
1.1169 droeschl 6810:
1.1162 raeburn 6811: foreach my $key (keys(%firstaccess)) {
1.1169 droeschl 6812: my ($cid, $rest) = split(/\0/, $key);
1.1162 raeburn 6813: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
6814: }
1.1169 droeschl 6815:
1.1162 raeburn 6816: foreach my $key (keys(%timerinterval)) {
6817: my ($cid,$rest) = split(/\0/,$key);
6818: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
6819: }
1.1169 droeschl 6820:
1.11 www 6821: my %allroles=();
1.1162 raeburn 6822: my %allgroups=();
1.1172.2.146. .18(raeb 6823:-24): my %gotcoauconfig=();
.20(raeb 6824:-24): my %domdefaults=();
1.11 www 6825:
1.1172.2.58 raeburn 6826: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169 droeschl 6827: my $role = $rolesdump{$area};
6828: $area =~ s/\_\w\w$//;
6829:
6830: my ($trole, $tend, $tstart, $group_privs);
6831:
6832: if ($role =~ /^cr/) {
6833: # Custom role, defined by a user
6834: # e.g., user.role.cr/msu/smith/mynewrole
6835: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
6836: $trole = $1;
6837: ($tend, $tstart) = split('_', $2);
6838: } else {
6839: $trole = $role;
6840: }
6841: } elsif ($role =~ m|^gr/|) {
6842: # Role of member in a group, defined within a course/community
6843: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
6844: ($trole, $tend, $tstart) = split(/_/, $role);
6845: next if $tstart eq '-1';
6846: ($trole, $group_privs) = split(/\//, $trole);
6847: $group_privs = &unescape($group_privs);
6848: } else {
6849: # Just a normal role, defined in roles.tab
6850: ($trole, $tend, $tstart) = split(/_/,$role);
6851: }
6852:
6853: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
6854: $username);
6855: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
6856:
6857: # role expired or not available yet?
6858: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
6859: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
6860:
6861: next if $area eq '' or $trole eq '';
6862:
6863: my $spec = "$trole.$area";
6864: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
6865:
6866: if ($trole =~ /^cr\//) {
6867: # Custom role, defined by a user
6868: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
6869: } elsif ($trole eq 'gr') {
6870: # Role of a member in a group, defined within a course/community
6871: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
6872: next;
6873: } else {
6874: # Normal role, defined in roles.tab
6875: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.1172.2.146. .18(raeb 6876:-24): if (($trole eq 'ca') || ($trole eq 'aa')) {
6877:-24): (undef,my ($audom,$auname)) = split(/\//,$area);
6878:-24): unless ($gotcoauconfig{$area}) {
.22(raeb 6879:-24): my @ca_settings = ('authoreditors','coauthorlist','coauthoroptin');
.18(raeb 6880:-24): my %info = &userenvironment($audom,$auname,@ca_settings);
6881:-24): $gotcoauconfig{$area} = 1;
6882:-24): foreach my $item (@ca_settings) {
6883:-24): if (exists($info{$item})) {
6884:-24): my $name = $item;
6885:-24): if ($item eq 'authoreditors') {
6886:-24): $name = 'editors';
.20(raeb 6887:-24): unless ($info{'authoreditors'}) {
6888:-24): my %domdefs;
6889:-24): if (ref($domdefaults{$audom}) eq 'HASH') {
6890:-24): %domdefs = %{$domdefaults{$audom}};
6891:-24): } else {
6892:-24): %domdefs = &get_domain_defaults($audom);
6893:-24): $domdefaults{$audom} = \%domdefs;
6894:-24): }
6895:-24): if ($domdefs{$name} ne '') {
6896:-24): $info{'authoreditors'} = $domdefs{$name};
6897:-24): } else {
6898:-24): $info{'authoreditors'} = 'edit,xml';
6899:-24): }
6900:-24): }
.18(raeb 6901:-24): }
6902:-24): $coauthorenv{"environment.internal.$name.$area"} = $info{$item};
6903:-24): }
6904:-24): }
6905:-24): }
6906:-24): }
1.1169 droeschl 6907: }
6908:
6909: my $cid = $tdomain.'_'.$trest;
6910: unless ($firstaccchk{$cid}) {
6911: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
6912: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
6913: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
6914: $coursetimerstarts{$cid}{$item};
6915: }
6916: }
6917: $firstaccchk{$cid} = 1;
6918: }
6919: unless ($timerintchk{$cid}) {
6920: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
6921: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
6922: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
6923: $coursetimerintervals{$cid}{$item};
1.1162 raeburn 6924: }
1.12 www 6925: }
1.1169 droeschl 6926: $timerintchk{$cid} = 1;
1.191 harris41 6927: }
1.11 www 6928: }
1.1169 droeschl 6929:
1.1172.2.91 raeburn 6930: @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
6931: \%allroles, \%allgroups);
1.1169 droeschl 6932: $env{'user.adv'} = $userroles{'user.adv'};
1.1172.2.91 raeburn 6933: $env{'user.rar'} = $userroles{'user.rar'};
1.1169 droeschl 6934:
1.1172.2.146. .18(raeb 6935:-24): return (\%userroles,\%firstaccenv,\%timerintenv,\%coauthorenv);
1.11 www 6936: }
6937:
1.567 raeburn 6938: sub set_arearole {
1.1172.2.19 raeburn 6939: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
6940: unless ($nolog) {
1.567 raeburn 6941: # log the associated role with the area
1.1172.2.19 raeburn 6942: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
6943: }
1.743 albertel 6944: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 6945: }
6946:
6947: sub custom_roleprivs {
6948: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
6949: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1172.2.40 raeburn 6950: my $homsvr = &homeserver($rauthor,$rdomain);
1.838 albertel 6951: if (&hostname($homsvr) ne '') {
1.567 raeburn 6952: my ($rdummy,$roledef)=
6953: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
6954: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
6955: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
6956: if (defined($syspriv)) {
1.1043 raeburn 6957: if ($trest =~ /^$match_community$/) {
6958: $syspriv =~ s/bre\&S//;
6959: }
1.567 raeburn 6960: $$allroles{'cm./'}.=':'.$syspriv;
6961: $$allroles{$spec.'./'}.=':'.$syspriv;
6962: }
6963: if ($tdomain ne '') {
6964: if (defined($dompriv)) {
6965: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
6966: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
6967: }
6968: if (($trest ne '') && (defined($coursepriv))) {
1.1172.2.89 raeburn 6969: if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
6970: my $rolename = $1;
6971: $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
6972: }
1.567 raeburn 6973: $$allroles{'cm.'.$area}.=':'.$coursepriv;
6974: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
6975: }
6976: }
6977: }
6978: }
6979: }
6980:
1.1172.2.89 raeburn 6981: sub course_adhocrole_privs {
6982: my ($rolename,$cdom,$cnum,$coursepriv) = @_;
6983: my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
6984: if ($overrides{"internal.adhocpriv.$rolename"}) {
6985: my (%currprivs,%storeprivs);
6986: foreach my $item (split(/:/,$coursepriv)) {
6987: my ($priv,$restrict) = split(/\&/,$item);
6988: $currprivs{$priv} = $restrict;
6989: }
6990: my (%possadd,%possremove,%full);
6991: foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
6992: my ($priv,$restrict)=split(/\&/,$item);
6993: $full{$priv} = $restrict;
6994: }
6995: foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
1.1172.2.146. .6(raebu 6996:22): next if ($item eq '');
6997:22): my ($rule,$rest) = split(/=/,$item);
6998:22): next unless (($rule eq 'off') || ($rule eq 'on'));
6999:22): foreach my $priv (split(/:/,$rest)) {
7000:22): if ($priv ne '') {
7001:22): if ($rule eq 'off') {
7002:22): $possremove{$priv} = 1;
7003:22): } else {
7004:22): $possadd{$priv} = 1;
7005:22): }
7006:22): }
7007:22): }
7008:22): }
7009:22): foreach my $priv (sort(keys(%full))) {
7010:22): if (exists($currprivs{$priv})) {
7011:22): unless (exists($possremove{$priv})) {
7012:22): $storeprivs{$priv} = $currprivs{$priv};
7013:22): }
7014:22): } elsif (exists($possadd{$priv})) {
7015:22): $storeprivs{$priv} = $full{$priv};
7016:22): }
7017:22): }
7018:22): $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
7019:22): }
7020:22): return $coursepriv;
1.1172.2.89 raeburn 7021: }
7022:
1.678 raeburn 7023: sub group_roleprivs {
7024: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
7025: my $access = 1;
7026: my $now = time;
7027: if (($tend!=0) && ($tend<$now)) { $access = 0; }
7028: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
7029: if ($access) {
1.811 albertel 7030: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 7031: $$allgroups{$course}{$group} .=':'.$group_privs;
7032: }
7033: }
1.567 raeburn 7034:
7035: sub standard_roleprivs {
7036: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
7037: if (defined($pr{$trole.':s'})) {
7038: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
7039: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
7040: }
7041: if ($tdomain ne '') {
7042: if (defined($pr{$trole.':d'})) {
7043: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7044: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
7045: }
7046: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
7047: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
7048: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
7049: }
7050: }
7051: }
7052:
7053: sub set_userprivs {
1.1064 raeburn 7054: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 7055: my $author=0;
7056: my $adv=0;
1.1172.2.91 raeburn 7057: my $rar=0;
1.678 raeburn 7058: my %grouproles = ();
7059: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 7060: my @groupkeys;
1.1000 raeburn 7061: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 7062: push(@groupkeys,$role);
7063: }
7064: if (ref($groups_roles) eq 'HASH') {
7065: foreach my $key (keys(%{$groups_roles})) {
7066: unless (grep(/^\Q$key\E$/,@groupkeys)) {
7067: push(@groupkeys,$key);
7068: }
7069: }
7070: }
7071: if (@groupkeys > 0) {
7072: foreach my $role (@groupkeys) {
7073: my ($trole,$area,$sec,$extendedarea);
7074: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
7075: $trole = $1;
7076: $area = $2;
7077: $sec = $3;
7078: $extendedarea = $area.$sec;
7079: if (exists($$allgroups{$area})) {
7080: foreach my $group (keys(%{$$allgroups{$area}})) {
7081: my $spec = $trole.'.'.$extendedarea;
7082: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 7083: $$allgroups{$area}{$group};
1.1064 raeburn 7084: }
1.678 raeburn 7085: }
7086: }
7087: }
7088: }
7089: }
1.800 albertel 7090: foreach my $group (keys(%grouproles)) {
7091: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 7092: }
1.800 albertel 7093: foreach my $role (keys(%{$allroles})) {
7094: my %thesepriv;
1.941 raeburn 7095: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 7096: foreach my $item (split(/:/,$$allroles{$role})) {
7097: if ($item ne '') {
7098: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 7099: if ($restrictions eq '') {
7100: $thesepriv{$privilege}='F';
7101: } elsif ($thesepriv{$privilege} ne 'F') {
7102: $thesepriv{$privilege}.=$restrictions;
7103: }
7104: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1172.2.91 raeburn 7105: if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567 raeburn 7106: }
7107: }
7108: my $thesestr='';
1.1104 raeburn 7109: foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 7110: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
7111: }
7112: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 7113: }
1.1172.2.91 raeburn 7114: return ($author,$adv,$rar);
1.567 raeburn 7115: }
7116:
1.994 raeburn 7117: sub role_status {
1.1104 raeburn 7118: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 7119: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1172.2.40 raeburn 7120: my ($one,$two) = split(m{\./},$rolekey,2);
7121: (undef,undef,$$role) = split(/\./,$one,3);
1.994 raeburn 7122: unless (!defined($$role) || $$role eq '') {
1.1172.2.40 raeburn 7123: $$where = '/'.$two;
1.994 raeburn 7124: $$trolecode=$$role.'.'.$$where;
7125: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
7126: $$tstatus='is';
1.1104 raeburn 7127: if ($$tstart && $$tstart>$update) {
1.994 raeburn 7128: $$tstatus='future';
1.1034 raeburn 7129: if ($$tstart<$now) {
7130: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 7131: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 7132: my (%allroles,%allgroups,$group_privs,
7133: %groups_roles,@rolecodes);
1.1002 raeburn 7134: my %userroles = (
7135: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
7136: );
1.1064 raeburn 7137: @rolecodes = ('cm');
1.1002 raeburn 7138: my $spec=$$role.'.'.$$where;
7139: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
7140: if ($$role =~ /^cr\//) {
7141: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 7142: push(@rolecodes,'cr');
1.1002 raeburn 7143: } elsif ($$role eq 'gr') {
1.1064 raeburn 7144: push(@rolecodes,$$role);
1.1002 raeburn 7145: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
7146: $env{'user.name'});
1.1064 raeburn 7147: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 7148: (undef,my $group_privs) = split(/\//,$trole);
7149: $group_privs = &unescape($group_privs);
7150: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 7151: 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 7152: &get_groups_roles($tdomain,$trest,
7153: \%course_roles,\@rolecodes,
7154: \%groups_roles);
1.1002 raeburn 7155: } else {
1.1064 raeburn 7156: push(@rolecodes,$$role);
1.1002 raeburn 7157: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
7158: }
1.1172.2.91 raeburn 7159: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
7160: \%groups_roles);
1.1064 raeburn 7161: &appenv(\%userroles,\@rolecodes);
1.1172.2.92 raeburn 7162: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002 raeburn 7163: }
7164: }
1.1034 raeburn 7165: $$tstatus = 'is';
1.1002 raeburn 7166: }
1.994 raeburn 7167: }
7168: if ($$tend) {
1.1104 raeburn 7169: if ($$tend<$update) {
1.994 raeburn 7170: $$tstatus='expired';
7171: } elsif ($$tend<$now) {
7172: $$tstatus='will_not';
7173: }
7174: }
7175: }
7176: }
7177: }
7178:
1.1104 raeburn 7179: sub get_groups_roles {
7180: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
7181: return unless((ref($cdom_courseroles) eq 'HASH') &&
7182: (ref($rolecodes) eq 'ARRAY') &&
7183: (ref($groups_roles) eq 'HASH'));
7184: if (keys(%{$cdom_courseroles}) > 0) {
7185: my ($cnum) = ($rest =~ /^($match_courseid)/);
7186: if ($cdom ne '' && $cnum ne '') {
7187: foreach my $key (keys(%{$cdom_courseroles})) {
7188: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
7189: my $crsrole = $1;
7190: my $crssec = $2;
7191: if ($crsrole =~ /^cr/) {
7192: unless (grep(/^cr$/,@{$rolecodes})) {
7193: push(@{$rolecodes},'cr');
7194: }
7195: } else {
7196: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
7197: push(@{$rolecodes},$crsrole);
7198: }
7199: }
7200: my $rolekey = "$crsrole./$cdom/$cnum";
7201: if ($crssec ne '') {
7202: $rolekey .= "/$crssec";
7203: }
7204: $rolekey .= './';
7205: $groups_roles->{$rolekey} = $rolecodes;
7206: }
7207: }
7208: }
7209: }
7210: return;
7211: }
7212:
7213: sub delete_env_groupprivs {
7214: my ($where,$courseroles,$possroles) = @_;
7215: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
7216: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
7217: unless (ref($courseroles->{$udom}) eq 'HASH') {
7218: %{$courseroles->{$udom}} =
7219: &get_my_roles('','','userroles',['active'],
7220: $possroles,[$udom],1);
7221: }
7222: if (ref($courseroles->{$udom}) eq 'HASH') {
7223: foreach my $item (keys(%{$courseroles->{$udom}})) {
7224: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
7225: my $area = '/'.$cdom.'/'.$cnum;
7226: my $privkey = "user.priv.$crsrole.$area";
7227: if ($crssec ne '') {
7228: $privkey .= '/'.$crssec;
7229: }
7230: $privkey .= ".$area/$group";
7231: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
7232: }
7233: }
7234: return;
7235: }
7236:
1.994 raeburn 7237: sub check_adhoc_privs {
1.1172.2.86 raeburn 7238: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994 raeburn 7239: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1172.2.86 raeburn 7240: if ($sec) {
7241: $cckey .= '/'.$sec;
7242: }
1.1172.2.9 raeburn 7243: my $setprivs;
1.994 raeburn 7244: if ($env{$cckey}) {
7245: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104 raeburn 7246: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 7247: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1172.2.86 raeburn 7248: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7249: $setprivs = 1;
1.994 raeburn 7250: }
7251: } else {
1.1172.2.87 raeburn 7252: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1172.2.9 raeburn 7253: $setprivs = 1;
1.994 raeburn 7254: }
1.1172.2.9 raeburn 7255: return $setprivs;
1.994 raeburn 7256: }
7257:
7258: sub set_adhoc_privileges {
1.1172.2.84 raeburn 7259: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1172.2.86 raeburn 7260: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994 raeburn 7261: my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1172.2.86 raeburn 7262: if ($sec ne '') {
7263: $area .= '/'.$sec;
7264: }
1.994 raeburn 7265: my $spec = $role.'.'.$area;
7266: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1172.2.19 raeburn 7267: $env{'user.name'},1);
1.1172.2.84 raeburn 7268: my %rolehash = ();
1.1172.2.89 raeburn 7269: if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
7270: my $rolename = $1;
1.1172.2.84 raeburn 7271: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1172.2.89 raeburn 7272: my %domdef = &get_domain_defaults($dcdom);
7273: if (ref($domdef{'adhocroles'}) eq 'HASH') {
7274: if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
7275: &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
7276: }
7277: }
1.1172.2.84 raeburn 7278: } else {
7279: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
7280: }
1.1172.2.91 raeburn 7281: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994 raeburn 7282: &appenv(\%userroles,[$role,'cm']);
1.1172.2.92 raeburn 7283: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1172.2.146. .1(raebu 7284:22): unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
7285:22): ($caller eq 'tiny')) {
1.1088 raeburn 7286: &appenv( {'request.role' => $spec,
7287: 'request.role.domain' => $dcdom,
1.1172.2.89 raeburn 7288: 'request.course.sec' => $sec,
1.1088 raeburn 7289: }
7290: );
7291: my $tadv=0;
7292: if (&allowed('adv') eq 'F') { $tadv=1; }
7293: &appenv({'request.role.adv' => $tadv});
7294: }
1.1172.2.146. .28(raeb 7295:-25): if ($role eq 'ca') {
7296:-25): my @ca_settings = ('authoreditors','coauthorlist');
7297:-25): my %info = &userenvironment($dcdom,$pickedcourse,@ca_settings);
7298:-25): foreach my $item (@ca_settings) {
7299:-25): if (exists($info{$item})) {
7300:-25): my $name = $item;
7301:-25): if ($item eq 'authoreditors') {
7302:-25): $name = 'editors';
7303:-25): unless ($info{'authoreditors'}) {
7304:-25): my %domdefs = &get_domain_defaults($dcdom);
7305:-25): if ($domdefs{$name} ne '') {
7306:-25): $info{'authoreditors'} = $domdefs{$name};
7307:-25): } else {
7308:-25): $info{'authoreditors'} = 'edit,xml';
7309:-25): }
7310:-25): }
7311:-25): }
7312:-25): &appenv({"environment.internal.$name./$dcdom/$pickedcourse" => $info{$item}});
7313:-25): }
7314:-25): }
7315:-25): }
1.994 raeburn 7316: }
7317:
1.12 www 7318: # --------------------------------------------------------------- get interface
7319:
7320: sub get {
1.131 albertel 7321: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7322: my $items='';
1.800 albertel 7323: foreach my $item (@$storearr) {
7324: $items.=&escape($item).'&';
1.191 harris41 7325: }
1.12 www 7326: $items=~s/\&$//;
1.620 albertel 7327: if (!$udomain) { $udomain=$env{'user.domain'}; }
7328: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 7329: my $uhome=&homeserver($uname,$udomain);
7330:
1.133 albertel 7331: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7332: my @pairs=split(/\&/,$rep);
1.273 albertel 7333: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
7334: return @pairs;
7335: }
1.15 www 7336: my %returnhash=();
1.42 www 7337: my $i=0;
1.800 albertel 7338: foreach my $item (@$storearr) {
7339: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7340: $i++;
1.191 harris41 7341: }
1.15 www 7342: return %returnhash;
1.27 www 7343: }
7344:
7345: # --------------------------------------------------------------- del interface
7346:
7347: sub del {
1.133 albertel 7348: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 7349: my $items='';
1.800 albertel 7350: foreach my $item (@$storearr) {
7351: $items.=&escape($item).'&';
1.191 harris41 7352: }
1.984 neumanie 7353:
1.27 www 7354: $items=~s/\&$//;
1.620 albertel 7355: if (!$udomain) { $udomain=$env{'user.domain'}; }
7356: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7357: my $uhome=&homeserver($uname,$udomain);
7358: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7359: }
7360:
7361: # -------------------------------------------------------------- dump interface
7362:
1.1172.2.22 raeburn 7363: sub unserialize {
7364: my ($rep, $escapedkeys) = @_;
7365:
7366: return {} if $rep =~ /^error/;
7367:
7368: my %returnhash=();
7369: foreach my $item (split(/\&/,$rep)) {
7370: my ($key, $value) = split(/=/, $item, 2);
7371: $key = unescape($key) unless $escapedkeys;
7372: next if $key =~ /^error: 2 /;
7373: $returnhash{$key} = &thaw_unescape($value);
7374: }
7375: return \%returnhash;
7376: }
7377:
7378: # see Lond::dump_with_regexp
7379: # if $escapedkeys hash keys won't get unescaped.
1.15 www 7380: sub dump {
1.1172.2.146. .1(raebu 7381:22): my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755 albertel 7382: if (!$udomain) { $udomain=$env{'user.domain'}; }
7383: if (!$uname) { $uname=$env{'user.name'}; }
7384: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 7385:
1.1172.2.55 raeburn 7386: if ($regexp) {
7387: $regexp=&escape($regexp);
7388: } else {
7389: $regexp='.';
7390: }
1.1172.2.22 raeburn 7391: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
7392: # user is hosted on this machine
1.1172.2.55 raeburn 7393: my $reply = LONCAPA::Lond::dump_with_regexp(join(':', ($udomain,
1.1172.2.27 raeburn 7394: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1172.2.22 raeburn 7395: return %{&unserialize($reply, $escapedkeys)};
7396: }
1.1172.2.146. .1(raebu 7397:22): my $rep;
7398:22): if ($encrypt) {
7399:22): $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7400:22): } else {
7401:22): $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7402:22): }
1.755 albertel 7403: my @pairs=split(/\&/,$rep);
7404: my %returnhash=();
1.1098 foxr 7405: if (!($rep =~ /^error/ )) {
7406: foreach my $item (@pairs) {
7407: my ($key,$value)=split(/=/,$item,2);
1.1172.2.22 raeburn 7408: $key = &unescape($key) unless ($escapedkeys);
1.1098 foxr 7409: next if ($key =~ /^error: 2 /);
7410: $returnhash{$key}=&thaw_unescape($value);
7411: }
1.755 albertel 7412: }
7413: return %returnhash;
1.407 www 7414: }
7415:
1.1098 foxr 7416:
1.717 albertel 7417: # --------------------------------------------------------- dumpstore interface
7418:
7419: sub dumpstore {
7420: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1172.2.22 raeburn 7421: # same as dump but keys must be escaped. They may contain colon separated
7422: # lists of values that may themself contain colons (e.g. symbs).
7423: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 7424: }
7425:
1.407 www 7426: # -------------------------------------------------------------- keys interface
7427:
7428: sub getkeys {
7429: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 7430: if (!$udomain) { $udomain=$env{'user.domain'}; }
7431: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 7432: my $uhome=&homeserver($uname,$udomain);
7433: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
7434: my @keyarray=();
1.800 albertel 7435: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 7436: next if ($key =~ /^error: 2 /);
1.800 albertel 7437: push(@keyarray,&unescape($key));
1.407 www 7438: }
7439: return @keyarray;
1.318 matthew 7440: }
7441:
1.319 matthew 7442: # --------------------------------------------------------------- currentdump
7443: sub currentdump {
1.328 matthew 7444: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 7445: $courseid = $env{'request.course.id'} if (! defined($courseid));
7446: $sdom = $env{'user.domain'} if (! defined($sdom));
7447: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 7448: my $uhome = &homeserver($sname,$sdom);
1.1172.2.50 raeburn 7449: my $rep;
7450:
7451: if (grep { $_ eq $uhome } current_machine_ids()) {
7452: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
7453: $courseid)));
7454: } else {
7455: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
7456: }
7457:
1.318 matthew 7458: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 7459: #
1.318 matthew 7460: my %returnhash=();
1.319 matthew 7461: #
7462: if ($rep eq "unknown_cmd") {
7463: # an old lond will not know currentdump
7464: # Do a dump and make it look like a currentdump
1.822 albertel 7465: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 7466: return if ($tmp[0] =~ /^(error:|no_such_host)/);
7467: my %hash = @tmp;
7468: @tmp=();
1.424 matthew 7469: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 7470: } else {
7471: my @pairs=split(/\&/,$rep);
1.800 albertel 7472: foreach my $pair (@pairs) {
7473: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 7474: my ($symb,$param) = split(/:/,$key);
7475: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 7476: &thaw_unescape($value);
1.319 matthew 7477: }
1.191 harris41 7478: }
1.12 www 7479: return %returnhash;
1.424 matthew 7480: }
7481:
7482: sub convert_dump_to_currentdump{
7483: my %hash = %{shift()};
7484: my %returnhash;
7485: # Code ripped from lond, essentially. The only difference
7486: # here is the unescaping done by lonnet::dump(). Conceivably
7487: # we might run in to problems with parameter names =~ /^v\./
7488: while (my ($key,$value) = each(%hash)) {
7489: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 7490: $symb = &unescape($symb);
7491: $param = &unescape($param);
1.424 matthew 7492: next if ($v eq 'version' || $symb eq 'keys');
7493: next if (exists($returnhash{$symb}) &&
7494: exists($returnhash{$symb}->{$param}) &&
7495: $returnhash{$symb}->{'v.'.$param} > $v);
7496: $returnhash{$symb}->{$param}=$value;
7497: $returnhash{$symb}->{'v.'.$param}=$v;
7498: }
7499: #
7500: # Remove all of the keys in the hashes which keep track of
7501: # the version of the parameter.
7502: while (my ($symb,$param_hash) = each(%returnhash)) {
7503: # use a foreach because we are going to delete from the hash.
7504: foreach my $key (keys(%$param_hash)) {
7505: delete($param_hash->{$key}) if ($key =~ /^v\./);
7506: }
7507: }
7508: return \%returnhash;
1.12 www 7509: }
7510:
1.627 albertel 7511: # ------------------------------------------------------ critical inc interface
7512:
7513: sub cinc {
7514: return &inc(@_,'critical');
7515: }
7516:
1.449 matthew 7517: # --------------------------------------------------------------- inc interface
7518:
7519: sub inc {
1.627 albertel 7520: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 7521: if (!$udomain) { $udomain=$env{'user.domain'}; }
7522: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 7523: my $uhome=&homeserver($uname,$udomain);
7524: my $items='';
7525: if (! ref($store)) {
7526: # got a single value, so use that instead
7527: $items = &escape($store).'=&';
7528: } elsif (ref($store) eq 'SCALAR') {
7529: $items = &escape($$store).'=&';
7530: } elsif (ref($store) eq 'ARRAY') {
7531: $items = join('=&',map {&escape($_);} @{$store});
7532: } elsif (ref($store) eq 'HASH') {
7533: while (my($key,$value) = each(%{$store})) {
7534: $items.= &escape($key).'='.&escape($value).'&';
7535: }
7536: }
7537: $items=~s/\&$//;
1.627 albertel 7538: if ($critical) {
7539: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
7540: } else {
7541: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
7542: }
1.449 matthew 7543: }
7544:
1.12 www 7545: # --------------------------------------------------------------- put interface
7546:
7547: sub put {
1.1172.2.146. .1(raebu 7548:22): my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620 albertel 7549: if (!$udomain) { $udomain=$env{'user.domain'}; }
7550: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7551: my $uhome=&homeserver($uname,$udomain);
1.12 www 7552: my $items='';
1.800 albertel 7553: foreach my $item (keys(%$storehash)) {
7554: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7555: }
1.12 www 7556: $items=~s/\&$//;
1.1172.2.146. .1(raebu 7557:22): if ($encrypt) {
7558:22): return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
7559:22): } else {
7560:22): return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
7561:22): }
1.47 www 7562: }
7563:
1.631 albertel 7564: # ------------------------------------------------------------ newput interface
7565:
7566: sub newput {
7567: my ($namespace,$storehash,$udomain,$uname)=@_;
7568: if (!$udomain) { $udomain=$env{'user.domain'}; }
7569: if (!$uname) { $uname=$env{'user.name'}; }
7570: my $uhome=&homeserver($uname,$udomain);
7571: my $items='';
7572: foreach my $key (keys(%$storehash)) {
7573: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7574: }
7575: $items=~s/\&$//;
7576: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
7577: }
7578:
7579: # --------------------------------------------------------- putstore interface
7580:
1.524 raeburn 7581: sub putstore {
1.1172.2.59 raeburn 7582: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 7583: if (!$udomain) { $udomain=$env{'user.domain'}; }
7584: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 7585: my $uhome=&homeserver($uname,$udomain);
7586: my $items='';
1.715 albertel 7587: foreach my $key (keys(%$storehash)) {
7588: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 7589: }
1.715 albertel 7590: $items=~s/\&$//;
1.716 albertel 7591: my $esc_symb=&escape($symb);
7592: my $esc_v=&escape($version);
1.715 albertel 7593: my $reply =
1.716 albertel 7594: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 7595: $uhome);
1.1172.2.59 raeburn 7596: if (($tolog) && ($reply eq 'ok')) {
7597: my $namevalue='';
7598: foreach my $key (keys(%{$storehash})) {
7599: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
7600: }
1.1172.2.134 raeburn 7601: my $ip = &get_requestor_ip();
1.1172.2.135 raeburn 7602: $namevalue .= 'ip='.&escape($ip).
1.1172.2.59 raeburn 7603: '&host='.&escape($perlvar{'lonHostID'}).
7604: '&version='.$esc_v.
7605: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
7606: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
7607: }
1.715 albertel 7608: if ($reply eq 'unknown_cmd') {
1.716 albertel 7609: # gfall back to way things use to be done
1.715 albertel 7610: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
7611: $uname);
1.524 raeburn 7612: }
1.715 albertel 7613: return $reply;
7614: }
7615:
7616: sub old_putstore {
1.716 albertel 7617: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
7618: if (!$udomain) { $udomain=$env{'user.domain'}; }
7619: if (!$uname) { $uname=$env{'user.name'}; }
7620: my $uhome=&homeserver($uname,$udomain);
7621: my %newstorehash;
1.800 albertel 7622: foreach my $item (keys(%$storehash)) {
7623: my $key = $version.':'.&escape($symb).':'.$item;
7624: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 7625: }
7626: my $items='';
7627: my %allitems = ();
1.800 albertel 7628: foreach my $item (keys(%newstorehash)) {
7629: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 7630: my $key = $1.':keys:'.$2;
7631: $allitems{$key} .= $3.':';
7632: }
1.800 albertel 7633: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 7634: }
1.800 albertel 7635: foreach my $item (keys(%allitems)) {
7636: $allitems{$item} =~ s/\:$//;
7637: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 7638: }
7639: $items=~s/\&$//;
7640: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 7641: }
7642:
1.47 www 7643: # ------------------------------------------------------ critical put interface
7644:
7645: sub cput {
1.134 albertel 7646: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 7647: if (!$udomain) { $udomain=$env{'user.domain'}; }
7648: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7649: my $uhome=&homeserver($uname,$udomain);
1.47 www 7650: my $items='';
1.800 albertel 7651: foreach my $item (keys(%$storehash)) {
7652: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7653: }
1.47 www 7654: $items=~s/\&$//;
1.134 albertel 7655: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7656: }
7657:
7658: # -------------------------------------------------------------- eget interface
7659:
7660: sub eget {
1.133 albertel 7661: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7662: my $items='';
1.800 albertel 7663: foreach my $item (@$storearr) {
7664: $items.=&escape($item).'&';
1.191 harris41 7665: }
1.12 www 7666: $items=~s/\&$//;
1.620 albertel 7667: if (!$udomain) { $udomain=$env{'user.domain'}; }
7668: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7669: my $uhome=&homeserver($uname,$udomain);
7670: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7671: my @pairs=split(/\&/,$rep);
7672: my %returnhash=();
1.42 www 7673: my $i=0;
1.800 albertel 7674: foreach my $item (@$storearr) {
7675: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7676: $i++;
1.191 harris41 7677: }
1.12 www 7678: return %returnhash;
7679: }
7680:
1.667 albertel 7681: # ------------------------------------------------------------ tmpput interface
7682: sub tmpput {
1.802 raeburn 7683: my ($storehash,$server,$context)=@_;
1.667 albertel 7684: my $items='';
1.800 albertel 7685: foreach my $item (keys(%$storehash)) {
7686: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 7687: }
7688: $items=~s/\&$//;
1.802 raeburn 7689: if (defined($context)) {
7690: $items .= ':'.&escape($context);
7691: }
1.667 albertel 7692: return &reply("tmpput:$items",$server);
7693: }
7694:
7695: # ------------------------------------------------------------ tmpget interface
7696: sub tmpget {
1.688 albertel 7697: my ($token,$server)=@_;
7698: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7699: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 7700: my %returnhash;
1.1172.2.85 raeburn 7701: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
7702: return %returnhash;
7703: }
1.667 albertel 7704: foreach my $item (split(/\&/,$rep)) {
7705: my ($key,$value)=split(/=/,$item);
7706: $returnhash{&unescape($key)}=&thaw_unescape($value);
7707: }
7708: return %returnhash;
7709: }
7710:
1.1113 raeburn 7711: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 7712: sub tmpdel {
7713: my ($token,$server)=@_;
7714: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7715: return &reply("tmpdel:$token",$server);
7716: }
7717:
1.1172.2.13 raeburn 7718: # ------------------------------------------------------------ get_timebased_id
7719:
7720: sub get_timebased_id {
7721: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
7722: $maxtries) = @_;
7723: my ($newid,$error,$dellock);
7724: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
7725: return ('','ok','invalid call to get suffix');
7726: }
7727:
7728: # set defaults for any optional args for which values were not supplied
7729: if ($who eq '') {
7730: $who = $env{'user.name'}.':'.$env{'user.domain'};
7731: }
7732: if (!$locktries) {
7733: $locktries = 3;
7734: }
7735: if (!$maxtries) {
7736: $maxtries = 10;
7737: }
7738:
7739: if (($cdom eq '') || ($cnum eq '')) {
7740: if ($env{'request.course.id'}) {
7741: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7742: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7743: }
7744: if (($cdom eq '') || ($cnum eq '')) {
7745: return ('','ok','call to get suffix not in course context');
7746: }
7747: }
7748:
7749: # construct locking item
7750: my $lockhash = {
7751: $prefix."\0".'locked_'.$keyid => $who,
7752: };
7753: my $tries = 0;
7754:
7755: # attempt to get lock on nohist_$namespace file
7756: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7757: while (($gotlock ne 'ok') && $tries <$locktries) {
7758: $tries ++;
7759: sleep 1;
7760: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7761: }
7762:
7763: # attempt to get unique identifier, based on current timestamp
7764: if ($gotlock eq 'ok') {
7765: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
7766: my $id = time;
7767: $newid = $id;
1.1172.2.56 raeburn 7768: if ($idtype eq 'addcode') {
7769: $newid .= &sixnum_code();
7770: }
1.1172.2.13 raeburn 7771: my $idtries = 0;
7772: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
7773: if ($idtype eq 'concat') {
7774: $newid = $id.$idtries;
1.1172.2.56 raeburn 7775: } elsif ($idtype eq 'addcode') {
7776: $newid = $newid.&sixnum_code();
1.1172.2.13 raeburn 7777: } else {
7778: $newid ++;
7779: }
7780: $idtries ++;
7781: }
7782: if (!exists($inuse{$prefix."\0".$newid})) {
7783: my %new_item = (
7784: $prefix."\0".$newid => $who,
7785: );
7786: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
7787: $cdom,$cnum);
7788: if ($putresult ne 'ok') {
7789: undef($newid);
7790: $error = 'error saving new item: '.$putresult;
7791: }
7792: } else {
1.1172.2.56 raeburn 7793: undef($newid);
1.1172.2.13 raeburn 7794: $error = ('error: no unique suffix available for the new item ');
7795: }
7796: # remove lock
7797: my @del_lock = ($prefix."\0".'locked_'.$keyid);
7798: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
7799: } else {
7800: $error = "error: could not obtain lockfile\n";
7801: $dellock = 'ok';
1.1172.2.58 raeburn 7802: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
7803: $dellock = 'nolock';
7804: }
1.1172.2.13 raeburn 7805: }
7806: return ($newid,$dellock,$error);
7807: }
7808:
1.1172.2.56 raeburn 7809: sub sixnum_code {
7810: my $code;
7811: for (0..6) {
7812: $code .= int( rand(9) );
7813: }
7814: return $code;
7815: }
7816:
1.765 albertel 7817: # -------------------------------------------------- portfolio access checking
7818:
7819: sub portfolio_access {
1.1172.2.77 raeburn 7820: my ($requrl,$clientip) = @_;
1.765 albertel 7821: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1172.2.77 raeburn 7822: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 7823: if ($result) {
7824: my %setters;
7825: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1172.2.142 raeburn 7826: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
7827: &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
7828: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7829: return 'B';
7830: }
7831: } else {
1.1172.2.142 raeburn 7832: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdo) =
7833: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
7834: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7835: return 'B';
7836: }
7837: }
7838: }
1.765 albertel 7839: if ($result eq 'ok') {
1.766 albertel 7840: return 'F';
1.765 albertel 7841: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 7842: return 'A';
1.765 albertel 7843: }
1.766 albertel 7844: return '';
1.765 albertel 7845: }
7846:
7847: sub get_portfolio_access {
1.1172.2.146. .23(raeb 7848:-24): my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
1.767 albertel 7849:
7850: if (!ref($access_hash)) {
7851: my $current_perms = &get_portfile_permissions($udom,$unum);
7852: my %access_controls = &get_access_controls($current_perms,$group,
7853: $file_name);
7854: $access_hash = $access_controls{$file_name};
7855: }
7856:
1.1172.2.146. .23(raeb 7857:-24): my $portaccess;
7858:-24): if (ref($portaccess) eq 'SCALAR') {
7859:-24): $portaccess = $$portaccessref;
7860:-24): } else {
7861:-24): $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
7862:-24): }
7863:-24):
7864:-24): my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
1.765 albertel 7865: my $now = time;
7866: if (ref($access_hash) eq 'HASH') {
7867: foreach my $key (keys(%{$access_hash})) {
7868: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1172.2.146. .23(raeb 7869:-24): next if (($scope ne 'ip') && ($portaccess == 0));
1.765 albertel 7870: if ($start > $now) {
7871: next;
7872: }
7873: if ($end && $end<$now) {
7874: next;
7875: }
7876: if ($scope eq 'public') {
7877: $public = $key;
7878: last;
7879: } elsif ($scope eq 'guest') {
7880: $guest = $key;
7881: } elsif ($scope eq 'domains') {
7882: push(@domains,$key);
7883: } elsif ($scope eq 'users') {
7884: push(@users,$key);
7885: } elsif ($scope eq 'course') {
7886: push(@courses,$key);
7887: } elsif ($scope eq 'group') {
7888: push(@groups,$key);
1.1172.2.77 raeburn 7889: } elsif ($scope eq 'ip') {
7890: push(@ips,$key);
1.1172.2.146. .23(raeb 7891:-24): } elsif ($scope eq 'userip') {
7892:-24): push(@userips,$key);
1.765 albertel 7893: }
7894: }
7895: if ($public) {
7896: return 'ok';
1.1172.2.77 raeburn 7897: } elsif (@ips > 0) {
7898: my $allowed;
7899: foreach my $ipkey (@ips) {
7900: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
7901: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
7902: $allowed = 1;
7903: last;
7904: }
7905: }
7906: }
7907: if ($allowed) {
7908: return 'ok';
7909: }
1.1172.2.146. .23(raeb 7910:-24): } elsif (@userips > 0) {
7911:-24): my $allowed;
7912:-24): foreach my $useripkey (@userips) {
7913:-24): if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
7914:-24): if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
7915:-24): $allowed = 1;
7916:-24): last;
7917:-24): }
7918:-24): }
7919:-24): }
7920:-24): if ($allowed) {
7921:-24): return 'ok';
7922:-24): }
1.765 albertel 7923: }
7924: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
7925: if ($guest) {
7926: return $guest;
7927: }
7928: } else {
7929: if (@domains > 0) {
7930: foreach my $domkey (@domains) {
7931: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
7932: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
7933: return 'ok';
7934: }
7935: }
7936: }
7937: }
7938: if (@users > 0) {
7939: foreach my $userkey (@users) {
1.865 raeburn 7940: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
7941: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
7942: if (ref($item) eq 'HASH') {
7943: if (($item->{'uname'} eq $env{'user.name'}) &&
7944: ($item->{'udom'} eq $env{'user.domain'})) {
7945: return 'ok';
7946: }
7947: }
7948: }
7949: }
1.765 albertel 7950: }
7951: }
7952: my %roleshash;
7953: my @courses_and_groups = @courses;
7954: push(@courses_and_groups,@groups);
7955: if (@courses_and_groups > 0) {
7956: my (%allgroups,%allroles);
7957: my ($start,$end,$role,$sec,$group);
7958: foreach my $envkey (%env) {
1.1060 raeburn 7959: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7960: my $cid = $2.'_'.$3;
7961: if ($1 eq 'gr') {
7962: $group = $4;
7963: $allgroups{$cid}{$group} = $env{$envkey};
7964: } else {
7965: if ($4 eq '') {
7966: $sec = 'none';
7967: } else {
7968: $sec = $4;
7969: }
7970: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7971: }
1.811 albertel 7972: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7973: my $cid = $2.'_'.$3;
7974: if ($4 eq '') {
7975: $sec = 'none';
7976: } else {
7977: $sec = $4;
7978: }
7979: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7980: }
7981: }
7982: if (keys(%allroles) == 0) {
7983: return;
7984: }
7985: foreach my $key (@courses_and_groups) {
7986: my %content = %{$$access_hash{$key}};
7987: my $cnum = $content{'number'};
7988: my $cdom = $content{'domain'};
7989: my $cid = $cdom.'_'.$cnum;
7990: if (!exists($allroles{$cid})) {
7991: next;
7992: }
7993: foreach my $role_id (keys(%{$content{'roles'}})) {
7994: my @sections = @{$content{'roles'}{$role_id}{'section'}};
7995: my @groups = @{$content{'roles'}{$role_id}{'group'}};
7996: my @status = @{$content{'roles'}{$role_id}{'access'}};
7997: my @roles = @{$content{'roles'}{$role_id}{'role'}};
7998: foreach my $role (keys(%{$allroles{$cid}})) {
7999: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
8000: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
8001: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
8002: if (grep/^all$/,@sections) {
8003: return 'ok';
8004: } else {
8005: if (grep/^$sec$/,@sections) {
8006: return 'ok';
8007: }
8008: }
8009: }
8010: }
8011: if (keys(%{$allgroups{$cid}}) == 0) {
8012: if (grep/^none$/,@groups) {
8013: return 'ok';
8014: }
8015: } else {
8016: if (grep/^all$/,@groups) {
8017: return 'ok';
8018: }
8019: foreach my $group (keys(%{$allgroups{$cid}})) {
8020: if (grep/^$group$/,@groups) {
8021: return 'ok';
8022: }
8023: }
8024: }
8025: }
8026: }
8027: }
8028: }
8029: }
8030: if ($guest) {
8031: return $guest;
8032: }
8033: }
8034: }
8035: return;
8036: }
8037:
8038: sub course_group_datechecker {
8039: my ($dates,$now,$status) = @_;
8040: my ($start,$end) = split(/\./,$dates);
8041: if (!$start && !$end) {
8042: return 'ok';
8043: }
8044: if (grep/^active$/,@{$status}) {
8045: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
8046: return 'ok';
8047: }
8048: }
8049: if (grep/^previous$/,@{$status}) {
8050: if ($end > $now ) {
8051: return 'ok';
8052: }
8053: }
8054: if (grep/^future$/,@{$status}) {
8055: if ($start > $now) {
8056: return 'ok';
8057: }
8058: }
8059: return;
8060: }
8061:
8062: sub parse_portfolio_url {
8063: my ($url) = @_;
8064:
8065: my ($type,$udom,$unum,$group,$file_name);
8066:
1.823 albertel 8067: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 8068: $type = 1;
8069: $udom = $1;
8070: $unum = $2;
8071: $file_name = $3;
1.823 albertel 8072: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 8073: $type = 2;
8074: $udom = $1;
8075: $unum = $2;
8076: $group = $3;
8077: $file_name = $3.'/'.$4;
8078: }
8079: if (wantarray) {
8080: return ($type,$udom,$unum,$file_name,$group);
8081: }
8082: return $type;
8083: }
8084:
8085: sub is_portfolio_url {
8086: my ($url) = @_;
8087: return scalar(&parse_portfolio_url($url));
8088: }
8089:
1.798 raeburn 8090: sub is_portfolio_file {
8091: my ($file) = @_;
1.820 raeburn 8092: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 8093: return 1;
8094: }
8095: return;
8096: }
8097:
1.1172.2.146. .13(raeb 8098:-23): sub is_coursetool_logo {
8099:-23): my ($uri) = @_;
8100:-23): if ($env{'request.course.id'}) {
8101:-23): my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
8102:-23): if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
8103:-23): return 1;
8104:-23): }
8105:-23): }
8106:-23): return;
8107:-23): }
8108:-23):
1.976 raeburn 8109: sub usertools_access {
1.1084 raeburn 8110: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 8111: my ($access,%tools);
8112: if ($context eq '') {
8113: $context = 'tools';
8114: }
8115: if ($context eq 'requestcourses') {
8116: %tools = (
8117: official => 1,
8118: unofficial => 1,
1.1006 raeburn 8119: community => 1,
1.1172.2.37 raeburn 8120: textbook => 1,
1.1172.2.146. .13(raeb 8121:-23): lti => 1,
1.985 raeburn 8122: );
1.1172.2.9 raeburn 8123: } elsif ($context eq 'requestauthor') {
8124: %tools = (
8125: requestauthor => 1,
8126: );
1.1172.2.146. .18(raeb 8127:-24): } elsif ($context eq 'authordefaults') {
8128:-24): %tools = (
8129:-24): webdav => 1,
8130:-24): );
1.985 raeburn 8131: } else {
8132: %tools = (
8133: aboutme => 1,
8134: blog => 1,
1.1172.2.146. .24(raeb 8135:-24): webdav => 1,
1.985 raeburn 8136: portfolio => 1,
1.1172.2.146. .22(raeb 8137:-24): portaccess => 1,
.6(raebu 8138:22): timezone => 1,
1.985 raeburn 8139: );
8140: }
1.976 raeburn 8141: return if (!defined($tools{$tool}));
8142:
1.1172.2.35 raeburn 8143: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 8144: $udom = $env{'user.domain'};
8145: $uname = $env{'user.name'};
8146: }
8147:
1.978 raeburn 8148: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
8149: if ($action ne 'reload') {
1.985 raeburn 8150: if ($context eq 'requestcourses') {
8151: return $env{'environment.canrequest.'.$tool};
1.1172.2.9 raeburn 8152: } elsif ($context eq 'requestauthor') {
8153: return $env{'environment.canrequest.author'};
1.1172.2.146. .18(raeb 8154:-24): } elsif ($context eq 'authordefaults') {
8155:-24): if ($tool eq 'webdav') {
8156:-24): return $env{'environment.availabletools.'.$tool};
8157:-24): }
1.985 raeburn 8158: } else {
8159: return $env{'environment.availabletools.'.$tool};
8160: }
8161: }
1.976 raeburn 8162: }
8163:
1.1172.2.9 raeburn 8164: my ($toolstatus,$inststatus,$envkey);
8165: if ($context eq 'requestauthor') {
8166: $envkey = $context;
1.1172.2.146. .18(raeb 8167:-24): } elsif ($context eq 'authordefaults') {
8168:-24): if ($tool eq 'webdav') {
8169:-24): $envkey = 'tools.'.$tool;
8170:-24): }
1.1172.2.9 raeburn 8171: } else {
8172: $envkey = $context.'.'.$tool;
8173: }
1.976 raeburn 8174:
1.985 raeburn 8175: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8176: ($action ne 'reload')) {
1.1172.2.9 raeburn 8177: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 8178: $inststatus = $env{'environment.inststatus'};
8179: } else {
1.1084 raeburn 8180: if (ref($userenvref) eq 'HASH') {
1.1172.2.9 raeburn 8181: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 8182: $inststatus = $userenvref->{'inststatus'};
8183: } else {
1.1172.2.9 raeburn 8184: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
8185: $toolstatus = $userenv{$envkey};
1.1084 raeburn 8186: $inststatus = $userenv{'inststatus'};
8187: }
1.976 raeburn 8188: }
8189:
8190: if ($toolstatus ne '') {
8191: if ($toolstatus) {
8192: $access = 1;
8193: } else {
8194: $access = 0;
8195: }
8196: return $access;
8197: }
8198:
1.1084 raeburn 8199: my ($is_adv,%domdef);
8200: if (ref($is_advref) eq 'HASH') {
8201: $is_adv = $is_advref->{'is_adv'};
8202: } else {
8203: $is_adv = &is_advanced_user($udom,$uname);
8204: }
8205: if (ref($domdefref) eq 'HASH') {
8206: %domdef = %{$domdefref};
8207: } else {
8208: %domdef = &get_domain_defaults($udom);
8209: }
1.976 raeburn 8210: if (ref($domdef{$tool}) eq 'HASH') {
8211: if ($is_adv) {
8212: if ($domdef{$tool}{'_LC_adv'} ne '') {
8213: if ($domdef{$tool}{'_LC_adv'}) {
8214: $access = 1;
8215: } else {
8216: $access = 0;
8217: }
8218: return $access;
8219: }
8220: }
8221: if ($inststatus ne '') {
8222: my ($hasaccess,$hasnoaccess);
8223: foreach my $affiliation (split(/:/,$inststatus)) {
8224: if ($domdef{$tool}{$affiliation} ne '') {
8225: if ($domdef{$tool}{$affiliation}) {
8226: $hasaccess = 1;
8227: } else {
8228: $hasnoaccess = 1;
8229: }
8230: }
8231: }
8232: if ($hasaccess || $hasnoaccess) {
8233: if ($hasaccess) {
8234: $access = 1;
8235: } elsif ($hasnoaccess) {
8236: $access = 0;
8237: }
8238: return $access;
8239: }
8240: } else {
8241: if ($domdef{$tool}{'default'} ne '') {
8242: if ($domdef{$tool}{'default'}) {
8243: $access = 1;
8244: } elsif ($domdef{$tool}{'default'} == 0) {
8245: $access = 0;
8246: }
8247: return $access;
8248: }
8249: }
8250: } else {
1.1172.2.6 raeburn 8251: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 8252: $access = 1;
8253: } else {
8254: $access = 0;
8255: }
1.976 raeburn 8256: return $access;
8257: }
8258: }
8259:
1.1050 raeburn 8260: sub is_course_owner {
8261: my ($cdom,$cnum,$udom,$uname) = @_;
8262: if (($udom eq '') || ($uname eq '')) {
8263: $udom = $env{'user.domain'};
8264: $uname = $env{'user.name'};
8265: }
8266: unless (($udom eq '') || ($uname eq '')) {
8267: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
8268: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
8269: return 1;
8270: } else {
8271: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
8272: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
8273: return 1;
8274: }
8275: }
8276: }
8277: }
8278: return;
8279: }
8280:
1.976 raeburn 8281: sub is_advanced_user {
8282: my ($udom,$uname) = @_;
1.1085 raeburn 8283: if ($udom ne '' && $uname ne '') {
8284: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 8285: if (wantarray) {
8286: return ($env{'user.adv'},$env{'user.author'});
8287: } else {
8288: return $env{'user.adv'};
8289: }
1.1085 raeburn 8290: }
8291: }
1.976 raeburn 8292: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
8293: my %allroles;
1.1128 raeburn 8294: my ($is_adv,$is_author);
1.976 raeburn 8295: foreach my $role (keys(%roleshash)) {
8296: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
8297: my $area = '/'.$tdomain.'/'.$trest;
8298: if ($sec ne '') {
8299: $area .= '/'.$sec;
8300: }
8301: if (($area ne '') && ($trole ne '')) {
8302: my $spec=$trole.'.'.$area;
8303: if ($trole =~ /^cr\//) {
8304: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
8305: } elsif ($trole ne 'gr') {
8306: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
8307: }
1.1128 raeburn 8308: if ($trole eq 'au') {
8309: $is_author = 1;
8310: }
1.976 raeburn 8311: }
8312: }
8313: foreach my $role (keys(%allroles)) {
8314: last if ($is_adv);
8315: foreach my $item (split(/:/,$allroles{$role})) {
8316: if ($item ne '') {
8317: my ($privilege,$restrictions)=split(/&/,$item);
8318: if ($privilege eq 'adv') {
8319: $is_adv = 1;
8320: last;
8321: }
8322: }
8323: }
8324: }
1.1128 raeburn 8325: if (wantarray) {
8326: return ($is_adv,$is_author);
8327: }
1.976 raeburn 8328: return $is_adv;
8329: }
1.798 raeburn 8330:
1.1035 raeburn 8331: sub check_can_request {
1.1172.2.146. .13(raeb 8332:-23): my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035 raeburn 8333: my $canreq = 0;
1.1172.2.146. .13(raeb 8334:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8335:-23): $uname = $env{'user.name'};
8336:-23): $udom = $env{'user.domain'};
8337:-23): }
1.1035 raeburn 8338: my ($types,$typename) = &Apache::loncommon::course_types();
8339: my @options = ('approval','validate','autolimit');
8340: my $optregex = join('|',@options);
8341: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
8342: foreach my $type (@{$types}) {
1.1172.2.146. .13(raeb 8343:-23): if (&usertools_access($uname,$udom,$type,undef,
8344:-23): 'requestcourses')) {
1.1035 raeburn 8345: $canreq ++;
1.1036 raeburn 8346: if (ref($request_domains) eq 'HASH') {
1.1172.2.146. .13(raeb 8347:-23): push(@{$request_domains->{$type}},$udom);
1.1036 raeburn 8348: }
1.1172.2.146. .13(raeb 8349:-23): if ($dom eq $udom) {
1.1035 raeburn 8350: $can_request->{$type} = 1;
8351: }
8352: }
1.1172.2.146. .13(raeb 8353:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
8354:-23): ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035 raeburn 8355: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
8356: if (@curr > 0) {
1.1036 raeburn 8357: foreach my $item (@curr) {
8358: if (ref($request_domains) eq 'HASH') {
8359: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
8360: if ($otherdom ne '') {
8361: if (ref($request_domains->{$type}) eq 'ARRAY') {
8362: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
8363: push(@{$request_domains->{$type}},$otherdom);
8364: }
8365: } else {
8366: push(@{$request_domains->{$type}},$otherdom);
8367: }
8368: }
8369: }
8370: }
1.1172.2.146. .13(raeb 8371:-23): unless ($dom eq $env{'user.domain'}) {
1.1036 raeburn 8372: $canreq ++;
1.1035 raeburn 8373: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
8374: $can_request->{$type} = 1;
8375: }
8376: }
8377: }
8378: }
8379: }
8380: }
8381: return $canreq;
8382: }
8383:
1.341 www 8384: # ---------------------------------------------- Custom access rule evaluation
8385:
8386: sub customaccess {
8387: my ($priv,$uri)=@_;
1.807 albertel 8388: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 8389: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 8390: $udom = &LONCAPA::clean_domain($udom);
8391: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 8392: my $access=0;
1.800 albertel 8393: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 8394: my ($effect,$realm,$role,$type)=split(/\:/,$right);
8395: if ($type eq 'user') {
8396: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 8397: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 8398: if ($tdom) {
8399: if ($tdom ne $env{'user.domain'}) { next; }
8400: }
1.896 albertel 8401: if ($tuname) {
8402: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 8403: }
8404: $access=($effect eq 'allow');
8405: last;
8406: }
8407: } else {
8408: if ($role) {
8409: if ($role ne $urole) { next; }
8410: }
8411: foreach my $scope (split(/\s*\,\s*/,$realm)) {
8412: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
8413: if ($tdom) {
8414: if ($tdom ne $udom) { next; }
8415: }
8416: if ($tcrs) {
8417: if ($tcrs ne $ucrs) { next; }
8418: }
8419: if ($tsec) {
8420: if ($tsec ne $usec) { next; }
8421: }
8422: $access=($effect eq 'allow');
8423: last;
8424: }
8425: if ($realm eq '' && $role eq '') {
8426: $access=($effect eq 'allow');
8427: }
1.402 bowersj2 8428: }
1.341 www 8429: }
8430: return $access;
8431: }
8432:
1.103 harris41 8433: # ------------------------------------------------- Check for a user privilege
1.12 www 8434:
8435: sub allowed {
1.1172.2.146. .1(raebu 8436:22): my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705 albertel 8437: my $ver_orguri=$uri;
1.439 www 8438: $uri=&deversion($uri);
1.152 www 8439: my $orguri=$uri;
1.52 www 8440: $uri=&declutter($uri);
1.809 raeburn 8441:
1.810 raeburn 8442: if ($priv eq 'evb') {
1.1172.2.146. .3(raebu 8443:22): # Evade communication block restrictions for specified role in a course or domain
1.810 raeburn 8444: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
8445: return $1;
8446: } else {
8447: return;
8448: }
8449: }
8450:
1.620 albertel 8451: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 8452: # Free bre access to adm and meta resources
1.1172.2.146. .1(raebu 8453:22): if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$}))
1.769 albertel 8454: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
8455: && ($priv eq 'bre')) {
1.14 www 8456: return 'F';
1.159 www 8457: }
8458:
1.545 banghart 8459: # Free bre access to user's own portfolio contents
1.714 raeburn 8460: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 8461: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 8462: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 8463: my %setters;
1.1172.2.142 raeburn 8464: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
8465: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
8466: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 8467: return 'B';
8468: } else {
8469: return 'F';
8470: }
1.545 banghart 8471: }
8472:
1.762 raeburn 8473: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 8474: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
8475: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
8476: if (exists($env{'request.course.id'})) {
8477: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8478: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8479: if (($domain eq $cdom) && ($name eq $cnum)) {
8480: my $courseprivid=$env{'request.course.id'};
8481: $courseprivid=~s/\_/\//;
8482: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
8483: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
8484: return $1;
1.762 raeburn 8485: } else {
8486: if ($env{'request.course.sec'}) {
8487: $courseprivid.='/'.$env{'request.course.sec'};
8488: }
8489: if ($env{'user.priv.'.$env{'request.role'}.'./'.
8490: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
8491: return $2;
8492: }
1.714 raeburn 8493: }
8494: }
8495: }
8496: }
8497:
1.159 www 8498: # Free bre to public access
8499:
8500: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8501:22): my $copyright;
8502:22): unless ($uri =~ /ext\.tool/) {
8503:22): $copyright=&metadata($uri,'copyright');
8504:22): }
1.620 albertel 8505: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 8506: return 'F';
8507: }
1.238 www 8508: if ($copyright eq 'priv') {
8509: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8510: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 8511: return '';
8512: }
8513: }
8514: if ($copyright eq 'domain') {
8515: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8516: unless (($env{'user.domain'} eq $1) ||
8517: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 8518: return '';
8519: }
1.262 matthew 8520: }
1.620 albertel 8521: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 8522: # Library role, so allow browsing of resources in this domain.
8523: return 'F';
1.238 www 8524: }
1.341 www 8525: if ($copyright eq 'custom') {
8526: unless (&customaccess($priv,$uri)) { return ''; }
8527: }
1.14 www 8528: }
1.264 matthew 8529: # Domain coordinator is trying to create a course
1.620 albertel 8530: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 8531: # uri is the requested domain in this case.
8532: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 8533: # a role of dc for the domain in question.
1.620 albertel 8534: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 8535: }
1.29 www 8536:
1.52 www 8537: my $thisallowed='';
8538: my $statecond=0;
8539: my $courseprivid='';
8540:
1.1039 raeburn 8541: my $ownaccess;
1.1043 raeburn 8542: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 8543: if (($priv eq 'bro') && ($env{'user.author'})) {
8544: if ($uri eq '') {
8545: $ownaccess = 1;
8546: } else {
8547: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
8548: my $udom = $env{'user.domain'};
8549: my $uname = $env{'user.name'};
8550: if ($uri =~ m{^\Q$udom\E/?$}) {
8551: $ownaccess = 1;
1.1040 raeburn 8552: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 8553: unless ($uri =~ m{\.\./}) {
8554: $ownaccess = 1;
8555: }
8556: } elsif (($udom ne 'public') && ($uname ne 'public')) {
8557: my $now = time;
8558: if ($uri =~ m{^([^/]+)/?$}) {
8559: my $adom = $1;
8560: foreach my $key (keys(%env)) {
1.1042 raeburn 8561: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1172.2.142 raeburn 8562: my ($start,$end) = split(/\./,$env{$key});
8563: if (($now >= $start) && (!$end || $end > $now)) {
1.1039 raeburn 8564: $ownaccess = 1;
8565: last;
8566: }
8567: }
8568: }
8569: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
8570: my $adom = $1;
8571: my $aname = $2;
1.1042 raeburn 8572: foreach my $role ('ca','aa') {
8573: if ($env{"user.role.$role./$adom/$aname"}) {
8574: my ($start,$end) =
1.1172.2.142 raeburn 8575: split(/\./,$env{"user.role.$role./$adom/$aname"});
8576: if (($now >= $start) && (!$end || $end > $now)) {
1.1042 raeburn 8577: $ownaccess = 1;
8578: last;
8579: }
1.1039 raeburn 8580: }
8581: }
8582: }
8583: }
8584: }
8585: }
8586: }
8587:
1.52 www 8588: # Course
8589:
1.620 albertel 8590: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8591: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8592: $thisallowed.=$1;
8593: }
1.52 www 8594: }
1.29 www 8595:
1.52 www 8596: # Domain
8597:
1.620 albertel 8598: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 8599: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8600: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8601: $thisallowed.=$1;
8602: }
1.12 www 8603: }
1.52 www 8604:
1.1141 raeburn 8605: # User who is not author or co-author might still be able to edit
8606: # resource of an author in the domain (e.g., if Domain Coordinator).
8607: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
8608: (&allowed('mdc',$env{'request.course.id'}))) {
8609: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
8610: $thisallowed.=$1;
8611: }
8612: }
8613:
1.52 www 8614: # Course: uri itself is a course
1.66 www 8615: my $courseuri=$uri;
8616: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 8617: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 8618:
1.620 albertel 8619: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 8620: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.115 raeburn 8621: if ($priv eq 'mip') {
8622: my $rem = $1;
8623: if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
8624: ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
8625: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8626: if ($cdom ne '') {
8627: my %passwdconf = &get_passwdconf($cdom);
8628: if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
8629: if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
8630: if (@{$passwdconf{'crsownerchg'}{'by'}}) {
8631: my @inststatuses = split(':',$env{'environment.inststatus'});
8632: unless (@inststatuses) {
8633: @inststatuses = ('default');
8634: }
8635: foreach my $status (@inststatuses) {
8636: if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
8637: $thisallowed.=$rem;
8638: }
8639: }
8640: }
8641: }
8642: }
8643: }
8644: }
8645: } else {
8646: unless (($priv eq 'bro') && (!$ownaccess)) {
8647: $thisallowed.=$1;
8648: }
1.1039 raeburn 8649: }
1.12 www 8650: }
1.29 www 8651:
1.665 albertel 8652: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 8653: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 8654: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 8655: $thisallowed='';
1.671 raeburn 8656: my ($match)=&is_on_map($uri);
8657: if ($match) {
8658: if ($env{'user.priv.'.$env{'request.role'}.'./'}
8659: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.65 raeburn 8660: my $value = $1;
1.1172.2.146. .1(raebu 8661:22): my $deeplinkblock;
8662:22): unless ($nodeeplinkcheck) {
8663:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8664:22): }
8665:22): if ($deeplinkblock) {
8666:22): $thisallowed='D';
8667:22): } elsif ($noblockcheck) {
1.1172.2.65 raeburn 8668: $thisallowed.=$value;
1.1162 raeburn 8669: } else {
1.1172.2.126 raeburn 8670: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8671: if (@blockers > 0) {
8672: $thisallowed = 'B';
8673: } else {
8674: $thisallowed.=$value;
8675: }
1.1162 raeburn 8676: }
1.671 raeburn 8677: }
8678: } else {
1.705 albertel 8679: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 8680: if ($refuri) {
8681: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 8682: $thisallowed='F';
1.671 raeburn 8683: } else {
8684: $refuri=&declutter($refuri);
8685: my ($match) = &is_on_map($refuri);
8686: if ($match) {
1.1172.2.146. .1(raebu 8687:22): my $deeplinkblock;
8688:22): unless ($nodeeplinkcheck) {
8689:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8690:22): }
8691:22): if ($deeplinkblock) {
8692:22): $thisallowed='D';
8693:22): } elsif ($noblockcheck) {
1.1162 raeburn 8694: $thisallowed='F';
1.1172.2.65 raeburn 8695: } else {
1.1172.2.127 raeburn 8696: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8697: if (@blockers > 0) {
8698: $thisallowed = 'B';
8699: } else {
8700: $thisallowed='F';
8701: }
1.1162 raeburn 8702: }
1.671 raeburn 8703: }
1.669 raeburn 8704: }
1.671 raeburn 8705: }
8706: }
1.314 www 8707: }
1.492 albertel 8708:
1.766 albertel 8709: if ($priv eq 'bre'
8710: && $thisallowed ne 'F'
8711: && $thisallowed ne '2'
8712: && &is_portfolio_url($uri)) {
1.1172.2.77 raeburn 8713: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 8714: }
8715:
1.52 www 8716: # Full access at system, domain or course-wide level? Exit.
1.29 www 8717: if ($thisallowed=~/F/) {
8718: return 'F';
8719: }
8720:
1.52 www 8721: # If this is generating or modifying users, exit with special codes
1.29 www 8722:
1.1172.2.146. .22(raeb 8723:-24): if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa::vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643 www 8724: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 8725: my ($audom,$auname)=split('/',$uri);
1.643 www 8726: # no author name given, so this just checks on the general right to make a co-author in this domain
8727: unless ($auname) { return $thisallowed; }
8728: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 8729: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
8730: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
8731: ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1172.2.146. .22(raeb 8732:-24): } elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
8733:-24): my ($audom,$auname)=split('/',$uri);
8734:-24): unless ($auname) { return $thisallowed; }
8735:-24): unless (($env{'request.role'} eq "dc./$audom") ||
8736:-24): ($env{'request.role'} eq "ca./$uri")) {
8737:-24): return '';
8738:-24): }
1.642 albertel 8739: }
1.52 www 8740: return $thisallowed;
8741: }
8742: #
1.103 harris41 8743: # Gathered so far: system, domain and course wide privileges
1.52 www 8744: #
8745: # Course: See if uri or referer is an individual resource that is part of
8746: # the course
8747:
1.620 albertel 8748: if ($env{'request.course.id'}) {
1.232 www 8749:
1.1172.2.146. .13(raeb 8750:-23): if ($priv eq 'bre') {
8751:-23): if (&is_coursetool_logo($uri)) {
8752:-23): return 'F';
8753:-23): }
8754:-23): }
8755:-23):
1.1172.2.115 raeburn 8756: # If this is modifying password (internal auth) domains must match for user and user's role.
8757:
8758: if ($priv eq 'mip') {
8759: if ($env{'user.domain'} eq $env{'request.role.domain'}) {
8760: return $thisallowed;
8761: } else {
8762: return '';
8763: }
8764: }
8765:
1.620 albertel 8766: $courseprivid=$env{'request.course.id'};
8767: if ($env{'request.course.sec'}) {
8768: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 8769: }
8770: $courseprivid=~s/\_/\//;
8771: my $checkreferer=1;
1.232 www 8772: my ($match,$cond)=&is_on_map($uri);
8773: if ($match) {
8774: $statecond=$cond;
1.620 albertel 8775: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8776: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8777: my $value = $1;
8778: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8779:22): my $deeplinkblock;
8780:22): unless ($nodeeplinkcheck) {
8781:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8782:22): }
8783:22): if ($deeplinkblock) {
8784:22): $thisallowed = 'D';
8785:22): } elsif ($noblockcheck) {
1.1162 raeburn 8786: $thisallowed.=$value;
1.1172.2.65 raeburn 8787: } else {
1.1172.2.126 raeburn 8788: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8789: if (@blockers > 0) {
8790: $thisallowed = 'B';
8791: } else {
8792: $thisallowed.=$value;
8793: }
1.1162 raeburn 8794: }
8795: } else {
8796: $thisallowed.=$value;
8797: }
1.52 www 8798: $checkreferer=0;
8799: }
1.29 www 8800: }
1.1172.2.126 raeburn 8801:
1.148 www 8802: if ($checkreferer) {
1.620 albertel 8803: my $refuri=$env{'httpref.'.$orguri};
1.148 www 8804: unless ($refuri) {
1.800 albertel 8805: foreach my $key (keys(%env)) {
8806: if ($key=~/^httpref\..*\*/) {
8807: my $pattern=$key;
1.156 www 8808: $pattern=~s/^httpref\.\/res\///;
1.148 www 8809: $pattern=~s/\*/\[\^\/\]\+/g;
8810: $pattern=~s/\//\\\//g;
1.152 www 8811: if ($orguri=~/$pattern/) {
1.800 albertel 8812: $refuri=$env{$key};
1.148 www 8813: }
8814: }
1.191 harris41 8815: }
1.148 www 8816: }
1.232 www 8817:
1.148 www 8818: if ($refuri) {
1.152 www 8819: $refuri=&declutter($refuri);
1.232 www 8820: my ($match,$cond)=&is_on_map($refuri);
8821: if ($match) {
8822: my $refstatecond=$cond;
1.620 albertel 8823: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8824: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8825: my $value = $1;
8826: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8827:22): my $deeplinkblock;
8828:22): unless ($nodeeplinkcheck) {
8829:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8830:22): }
8831:22): if ($deeplinkblock) {
8832:22): $thisallowed = 'D';
8833:22): } elsif ($noblockcheck) {
1.1162 raeburn 8834: $thisallowed.=$value;
1.1172.2.65 raeburn 8835: } else {
1.1172.2.127 raeburn 8836: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8837: if (@blockers > 0) {
8838: $thisallowed = 'B';
8839: } else {
8840: $thisallowed.=$value;
8841: }
1.1162 raeburn 8842: }
8843: } else {
8844: $thisallowed.=$value;
8845: }
1.53 www 8846: $uri=$refuri;
8847: $statecond=$refstatecond;
1.52 www 8848: }
8849: }
1.148 www 8850: }
1.29 www 8851: }
1.52 www 8852: }
1.29 www 8853:
1.52 www 8854: #
1.103 harris41 8855: # Gathered now: all privileges that could apply, and condition number
1.52 www 8856: #
8857: #
8858: # Full or no access?
8859: #
1.29 www 8860:
1.52 www 8861: if ($thisallowed=~/F/) {
8862: return 'F';
8863: }
1.29 www 8864:
1.52 www 8865: unless ($thisallowed) {
8866: return '';
8867: }
1.29 www 8868:
1.52 www 8869: # Restrictions exist, deal with them
8870: #
8871: # C:according to course preferences
8872: # R:according to resource settings
8873: # L:unless locked
8874: # X:according to user session state
8875: #
8876:
8877: # Possibly locked functionality, check all courses
1.1172.2.142 raeburn 8878: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54 www 8879: # Locks might take effect only after 10 minutes cache expiration for other
1.1172.2.142 raeburn 8880: # courses, and 2 minutes for current course, in which user has st or ta role
8881: # which is neither expired nor a future role (unless current course).
1.52 www 8882:
1.1172.2.142 raeburn 8883: my ($needlockcheck,$now,$crsonly);
1.52 www 8884: if ($thisallowed=~/L/) {
1.1172.2.142 raeburn 8885: $now = time;
8886: if ($priv eq 'bre') {
8887: if ($uri ne '') {
8888: if ($orguri =~ m{^/+res/}) {
8889: if ($uri =~ m{^lib/templates/}) {
8890: if ($env{'request.course.id'}) {
8891: $crsonly = 1;
8892: $needlockcheck = 1;
8893: }
8894: } else {
8895: $needlockcheck = 1;
8896: }
8897: } elsif ($env{'request.course.id'}) {
8898: my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
8899: if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
8900: ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
8901: $crsonly = 1;
8902: }
8903: $needlockcheck = 1;
8904: }
8905: }
8906: } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
8907: $needlockcheck = 1;
8908: }
8909: }
8910: if ($needlockcheck) {
8911: foreach my $envkey (keys(%env)) {
1.54 www 8912: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
8913: my $courseid=$2;
8914: my $roleid=$1.'.'.$2;
1.92 www 8915: $courseid=~s/^\///;
1.1172.2.142 raeburn 8916: unless ($env{'request.role'} eq $roleid) {
8917: my ($start,$end) = split(/\./,$env{$envkey});
8918: next unless (($now >= $start) && (!$end || $end > $now));
8919: }
1.54 www 8920: my $expiretime=600;
1.620 albertel 8921: if ($env{'request.role'} eq $roleid) {
1.54 www 8922: $expiretime=120;
8923: }
8924: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
8925: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 8926: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 8927: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 8928: }
1.620 albertel 8929: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
8930: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
8931: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
8932: &log($env{'user.domain'},$env{'user.name'},
8933: $env{'user.home'},
1.57 www 8934: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 8935: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8936: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8937: return '';
8938: }
8939: }
1.620 albertel 8940: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
8941: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1172.2.142 raeburn 8942: if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620 albertel 8943: &log($env{'user.domain'},$env{'user.name'},
8944: $env{'user.home'},
1.57 www 8945: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 8946: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8947: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8948: return '';
8949: }
8950: }
8951: }
1.29 www 8952: }
1.52 www 8953: }
1.1172.2.126 raeburn 8954:
1.52 www 8955: #
8956: # Rest of the restrictions depend on selected course
8957: #
8958:
1.620 albertel 8959: unless ($env{'request.course.id'}) {
1.766 albertel 8960: if ($thisallowed eq 'A') {
8961: return 'A';
1.814 raeburn 8962: } elsif ($thisallowed eq 'B') {
8963: return 'B';
1.766 albertel 8964: } else {
8965: return '1';
8966: }
1.52 www 8967: }
1.29 www 8968:
1.52 www 8969: #
8970: # Now user is definitely in a course
8971: #
1.53 www 8972:
8973:
8974: # Course preferences
8975:
8976: if ($thisallowed=~/C/) {
1.620 albertel 8977: my $rolecode=(split(/\./,$env{'request.role'}))[0];
8978: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
8979: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 8980: =~/\Q$rolecode\E/) {
1.1103 raeburn 8981: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8982: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8983: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
8984: $env{'request.course.id'});
8985: }
1.237 www 8986: return '';
8987: }
8988:
1.620 albertel 8989: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 8990: =~/\Q$unamedom\E/) {
1.1103 raeburn 8991: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8992: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
8993: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
8994: $env{'request.course.id'});
8995: }
1.54 www 8996: return '';
8997: }
1.53 www 8998: }
8999:
9000: # Resource preferences
9001:
9002: if ($thisallowed=~/R/) {
1.620 albertel 9003: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 9004: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 9005: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 9006: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
9007: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
9008: }
9009: return '';
1.54 www 9010: }
1.53 www 9011: }
1.30 www 9012:
1.1172.2.146. .1(raebu 9013:22): # Restricted for deeplinked session?
9014:22):
9015:22): if ($env{'request.deeplink.login'}) {
9016:22): if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
9017:22): if (!$symb) { $symb=&symbread($uri,1); }
9018:22): if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
9019:22): return '';
9020:22): }
9021:22): }
9022:22): }
9023:22):
1.246 www 9024: # Restricted by state or randomout?
1.30 www 9025:
1.52 www 9026: if ($thisallowed=~/X/) {
1.620 albertel 9027: if ($env{'acc.randomout'}) {
1.579 albertel 9028: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 9029: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 9030: return '';
9031: }
1.247 www 9032: }
9033: if (&condval($statecond)) {
1.52 www 9034: return '2';
9035: } else {
9036: return '';
9037: }
9038: }
1.30 www 9039:
1.766 albertel 9040: if ($thisallowed eq 'A') {
9041: return 'A';
1.814 raeburn 9042: } elsif ($thisallowed eq 'B') {
9043: return 'B';
1.1172.2.146. .1(raebu 9044:22): } elsif ($thisallowed eq 'D') {
9045:22): return 'D';
1.766 albertel 9046: }
1.52 www 9047: return 'F';
1.232 www 9048: }
1.1162 raeburn 9049:
1.1172.2.13 raeburn 9050: # ------------------------------------------- Check construction space access
9051:
9052: sub constructaccess {
9053: my ($url,$setpriv)=@_;
9054:
9055: # We do not allow editing of previous versions of files
9056: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
9057:
9058: # Get username and domain from URL
9059: my ($ownername,$ownerdomain,$ownerhome);
9060:
9061: ($ownerdomain,$ownername) =
1.1172.2.146. .19(raeb 9062:-24): ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1172.2.13 raeburn 9063:
9064: # The URL does not really point to any authorspace, forget it
9065: unless (($ownername) && ($ownerdomain)) { return ''; }
9066:
9067: # Now we need to see if the user has access to the authorspace of
9068: # $ownername at $ownerdomain
9069:
9070: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
9071: # Real author for this?
9072: $ownerhome = $env{'user.home'};
9073: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
9074: return ($ownername,$ownerdomain,$ownerhome);
9075: }
9076: } else {
9077: # Co-author for this?
9078: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
9079: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
9080: $ownerhome = &homeserver($ownername,$ownerdomain);
9081: return ($ownername,$ownerdomain,$ownerhome);
9082: }
9083: }
9084:
9085: # We don't have any access right now. If we are not possibly going to do anything about this,
9086: # we might as well leave
9087: unless ($setpriv) { return ''; }
9088:
9089: # Backdoor access?
9090: my $allowed=&allowed('eco',$ownerdomain);
9091: # Nope
9092: unless ($allowed) { return ''; }
9093: # Looks like we may have access, but could be locked by the owner of the construction space
9094: if ($allowed eq 'U') {
9095: my %blocked=&get('environment',['domcoord.author'],
9096: $ownerdomain,$ownername);
9097: # Is blocked by owner
9098: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
9099: }
9100: if (($allowed eq 'F') || ($allowed eq 'U')) {
9101: # Grant temporary access
9102: my $then=$env{'user.login.time'};
1.1172.2.16 raeburn 9103: my $update=$env{'user.update.time'};
1.1172.2.13 raeburn 9104: if (!$update) { $update = $then; }
9105: my $refresh=$env{'user.refresh.time'};
9106: if (!$refresh) { $refresh = $update; }
9107: my $now = time;
9108: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
9109: $now,'ca','constructaccess');
9110: $ownerhome = &homeserver($ownername,$ownerdomain);
9111: return($ownername,$ownerdomain,$ownerhome);
9112: }
9113: # No business here
9114: return '';
9115: }
9116:
1.1172.2.66 raeburn 9117: # ----------------------------------------------------------- Content Blocking
9118:
9119: {
9120: # Caches for faster Course Contents display where content blocking
9121: # is in operation (i.e., interval param set) for timed quiz.
9122: #
9123: # User for whom data are being temporarily cached.
9124: my $cacheduser='';
1.1172.2.126 raeburn 9125: # Course for which data are being temporarily cached.
9126: my $cachedcid='';
1.1172.2.66 raeburn 9127: # Cached blockers for this user (a hash of blocking items).
9128: my %cachedblockers=();
9129: # When the data were last cached.
9130: my $cachedlast='';
9131:
9132: sub load_all_blockers {
1.1172.2.128 raeburn 9133: my ($uname,$udom)=@_;
1.1172.2.66 raeburn 9134: if (($uname ne '') && ($udom ne '')) {
9135: if (($cacheduser eq $uname.':'.$udom) &&
1.1172.2.126 raeburn 9136: ($cachedcid eq $env{'request.course.id'}) &&
1.1172.2.128 raeburn 9137: (abs($cachedlast-time)<5)) {
1.1172.2.66 raeburn 9138: return;
9139: }
9140: }
9141: $cachedlast=time;
9142: $cacheduser=$uname.':'.$udom;
1.1172.2.126 raeburn 9143: $cachedcid=$env{'request.course.id'};
1.1172.2.128 raeburn 9144: %cachedblockers = &get_commblock_resources();
1.1172.2.126 raeburn 9145: return;
1.1172.2.66 raeburn 9146: }
9147:
1.1162 raeburn 9148: sub get_comm_blocks {
9149: my ($cdom,$cnum) = @_;
9150: if ($cdom eq '' || $cnum eq '') {
9151: return unless ($env{'request.course.id'});
9152: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9153: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9154: }
9155: my %commblocks;
9156: my $hashid=$cdom.'_'.$cnum;
9157: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
9158: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
9159: %commblocks = %{$blocksref};
9160: } else {
9161: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
9162: my $cachetime = 600;
9163: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
9164: }
9165: return %commblocks;
9166: }
9167:
1.1172.2.66 raeburn 9168: sub get_commblock_resources {
9169: my ($blocks) = @_;
9170: my %blockers = ();
9171: return %blockers unless ($env{'request.course.id'});
1.1172.2.142 raeburn 9172: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9173: if ($env{'request.course.sec'}) {
9174: $courseurl .= '/'.$env{'request.course.sec'};
9175: }
9176: return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162 raeburn 9177: my %commblocks;
9178: if (ref($blocks) eq 'HASH') {
9179: %commblocks = %{$blocks};
9180: } else {
9181: %commblocks = &get_comm_blocks();
9182: }
1.1172.2.66 raeburn 9183: return %blockers unless (keys(%commblocks) > 0);
1.1163 raeburn 9184: my $navmap = Apache::lonnavmaps::navmap->new();
1.1172.2.66 raeburn 9185: return %blockers unless (ref($navmap));
9186: my $now = time;
1.1162 raeburn 9187: foreach my $block (keys(%commblocks)) {
9188: if ($block =~ /^(\d+)____(\d+)$/) {
9189: my ($start,$end) = ($1,$2);
9190: if ($start <= $now && $end >= $now) {
9191: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9192: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
9193: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1172.2.66 raeburn 9194: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9195: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 9196: }
9197: }
9198: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1172.2.66 raeburn 9199: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9200: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 9201: }
9202: }
9203: }
9204: }
9205: }
9206: } elsif ($block =~ /^firstaccess____(.+)$/) {
9207: my $item = $1;
9208: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9209: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1172.2.142 raeburn 9210: my (@interval,$mapname);
1.1172.2.66 raeburn 9211: my $type = 'map';
9212: if ($item eq 'course') {
9213: $type = 'course';
9214: @interval=&EXT("resource.0.interval");
9215: } else {
9216: if ($item =~ /___\d+___/) {
9217: $type = 'resource';
9218: @interval=&EXT("resource.0.interval",$item);
1.1162 raeburn 9219: } else {
1.1172.2.142 raeburn 9220: $mapname = &deversion($item);
9221: if (ref($navmap)) {
9222: my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
9223: @interval = ($timelimit,'map');
1.1162 raeburn 9224: }
9225: }
1.1172.2.66 raeburn 9226: }
1.1172.2.146. .1(raebu 9227:22): if ($interval[0] =~ /^(\d+)/) {
9228:22): my $timelimit = $1;
1.1172.2.66 raeburn 9229: my $first_access;
9230: if ($type eq 'resource') {
9231: $first_access=&get_first_access($interval[1],$item);
9232: } elsif ($type eq 'map') {
9233: $first_access=&get_first_access($interval[1],undef,$item);
9234: } else {
9235: $first_access=&get_first_access($interval[1]);
9236: }
9237: if ($first_access) {
1.1172.2.146. .1(raebu 9238:22): my $timesup = $first_access+$timelimit;
1.1172.2.66 raeburn 9239: if ($timesup > $now) {
9240: my $activeblock;
1.1172.2.142 raeburn 9241: if ($type eq 'resource') {
9242: if (ref($navmap)) {
9243: my $res = $navmap->getBySymb($item);
9244: if ($res->answerable()) {
9245: $activeblock = 1;
9246: }
9247: }
9248: } elsif ($type eq 'map') {
9249: my $mapsymb = &symbread($mapname,1);
9250: if (($mapsymb) && (ref($navmap))) {
9251: my $mapres = $navmap->getBySymb($mapsymb);
9252: if (ref($mapres)) {
9253: my $first = $mapres->map_start();
9254: my $finish = $mapres->map_finish();
9255: my $it = $navmap->getIterator($first,$finish,undef,0,0);
9256: if (ref($it)) {
9257: my $res;
9258: while ($res = $it->next(undef,1)) {
9259: next unless (ref($res));
9260: my $symb = $res->symb();
9261: next if (($symb eq $mapsymb) || ($symb eq ''));
9262: @interval=&EXT("resource.0.interval",$symb);
9263: if ($interval[1] eq 'map') {
9264: if ($res->answerable()) {
9265: $activeblock = 1;
9266: last;
9267: }
9268: }
9269: }
9270: }
9271: }
1.1172.2.66 raeburn 9272: }
9273: }
9274: if ($activeblock) {
9275: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
9276: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9277: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
9278: }
9279: }
9280: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
9281: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9282: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 9283: }
1.1162 raeburn 9284: }
9285: }
9286: }
9287: }
9288: }
9289: }
9290: }
9291: }
9292: }
1.1172.2.66 raeburn 9293: return %blockers;
1.1162 raeburn 9294: }
9295:
1.1172.2.66 raeburn 9296: sub has_comm_blocking {
1.1172.2.128 raeburn 9297: my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1172.2.66 raeburn 9298: my @blockers;
9299: return unless ($env{'request.course.id'});
9300: return unless ($priv eq 'bre');
9301: return if ($env{'request.state'} eq 'construct');
1.1172.2.142 raeburn 9302: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9303: if ($env{'request.course.sec'}) {
9304: $courseurl .= '/'.$env{'request.course.sec'};
9305: }
9306: return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1172.2.128 raeburn 9307: my %blockinfo;
9308: if (ref($blocks) eq 'HASH') {
9309: %blockinfo = &get_commblock_resources($blocks);
9310: } else {
9311: &load_all_blockers($env{'user.name'},$env{'user.domain'});
9312: %blockinfo = %cachedblockers;
9313: }
9314: return unless (keys(%blockinfo) > 0);
1.1172.2.66 raeburn 9315: my (%possibles,@symbs);
9316: if (!$symb) {
1.1172.2.128 raeburn 9317: $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162 raeburn 9318: }
1.1172.2.66 raeburn 9319: if ($symb) {
9320: @symbs = ($symb);
9321: } elsif (keys(%possibles)) {
9322: @symbs = keys(%possibles);
9323: }
9324: my $noblock;
9325: foreach my $symb (@symbs) {
9326: last if ($noblock);
9327: my ($map,$resid,$resurl)=&decode_symb($symb);
1.1172.2.128 raeburn 9328: foreach my $block (keys(%blockinfo)) {
1.1172.2.66 raeburn 9329: if ($block =~ /^firstaccess____(.+)$/) {
9330: my $item = $1;
1.1172.2.126 raeburn 9331: unless ($blocked) {
9332: if (($item eq $map) || ($item eq $symb)) {
9333: $noblock = 1;
9334: last;
9335: }
1.1172.2.66 raeburn 9336: }
1.1162 raeburn 9337: }
1.1172.2.128 raeburn 9338: if (ref($blockinfo{$block}) eq 'HASH') {
9339: if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
9340: if ($blockinfo{$block}{'resources'}{$symb}) {
1.1172.2.66 raeburn 9341: unless (grep(/^\Q$block\E$/,@blockers)) {
9342: push(@blockers,$block);
9343: }
9344: }
9345: }
1.1172.2.128 raeburn 9346: if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
9347: if ($blockinfo{$block}{'maps'}{$map}) {
1.1172.2.126 raeburn 9348: unless (grep(/^\Q$block\E$/,@blockers)) {
9349: push(@blockers,$block);
9350: }
1.1172.2.66 raeburn 9351: }
9352: }
1.1162 raeburn 9353: }
9354: }
9355: }
1.1172.2.126 raeburn 9356: unless ($noblock) {
9357: return @blockers;
9358: }
9359: return;
1.1172.2.66 raeburn 9360: }
1.1162 raeburn 9361: }
9362:
1.1172.2.146. .1(raebu 9363:22): sub deeplink_check {
9364:22): my ($priv,$symb,$uri) = @_;
9365:22): return unless ($env{'request.course.id'});
9366:22): return unless ($priv eq 'bre');
9367:22): return if ($env{'request.state'} eq 'construct');
9368:22): return if ($env{'request.role.adv'});
9369:22): my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9370:22): my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9371:22): my (%possibles,@symbs);
9372:22): if (!$symb) {
9373:22): $symb = &symbread($uri,1,1,1,\%possibles);
9374:22): }
9375:22): if ($symb) {
9376:22): @symbs = ($symb);
9377:22): } elsif (keys(%possibles)) {
9378:22): @symbs = keys(%possibles);
9379:22): }
9380:22):
9381:22): my ($deeplink_symb,$allow);
9382:22): if ($env{'request.deeplink.login'}) {
9383:22): $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
9384:22): }
9385:22): foreach my $symb (@symbs) {
9386:22): last if ($allow);
9387:22): my $deeplink = &EXT("resource.0.deeplink",$symb);
9388:22): if ($deeplink eq '') {
9389:22): $allow = 1;
9390:22): } else {
9391:22): my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
9392:22): if ($state ne 'only') {
9393:22): $allow = 1;
9394:22): } else {
9395:22): my $check_deeplink_entry;
9396:22): if ($protect ne 'none') {
9397:22): my ($acctype,$item) = split(/:/,$protect);
9398:22): if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
9399:22): if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
9400:22): $check_deeplink_entry = 1
9401:22): }
9402:22): } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
9403:22): if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
9404:22): $check_deeplink_entry = 1;
9405:22): }
9406:22): } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
9407:22): if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
9408:22): $check_deeplink_entry = 1;
9409:22): }
9410:22): }
9411:22): }
9412:22): if (($protect eq 'none') || ($check_deeplink_entry)) {
9413:22): if ($scope eq 'res') {
9414:22): if ($symb eq $deeplink_symb) {
9415:22): $allow = 1;
9416:22): }
9417:22): } elsif (($scope eq 'map') || ($scope eq 'rec')) {
9418:22): my ($map_from_symb,$map_from_login);
9419:22): $map_from_symb = &deversion((&decode_symb($symb))[0]);
9420:22): if ($deeplink_symb =~ /\.(page|sequence)$/) {
9421:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
9422:22): } else {
9423:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
9424:22): }
9425:22): if (($map_from_symb) && ($map_from_login)) {
9426:22): if ($map_from_symb eq $map_from_login) {
9427:22): $allow = 1;
9428:22): } elsif ($scope eq 'rec') {
9429:22): my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
9430:22): if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
9431:22): $allow = 1;
9432:22): }
9433:22): }
9434:22): }
9435:22): }
9436:22): }
9437:22): }
9438:22): }
9439:22): }
9440:22): return if ($allow);
9441:22): return 1;
9442:22): }
9443:22):
1.1172.2.66 raeburn 9444: # -------------------------------- Deversion and split uri into path an filename
9445:
1.1133 foxr 9446: #
1.1172.2.66 raeburn 9447: # Removes the version from a URI and
1.1133 foxr 9448: # splits it in to its filename and path to the filename.
9449: # Seems like File::Basename could have done this more clearly.
9450: # Parameters:
9451: # $uri - input URI
9452: # Returns:
9453: # Two element list consisting of
9454: # $pathname - the URI up to and excluding the trailing /
9455: # $filename - The part of the URI following the last /
9456: # NOTE:
9457: # Another realization of this is simply:
9458: # use File::Basename;
9459: # ...
9460: # $uri = shift;
9461: # $filename = basename($uri);
9462: # $path = dirname($uri);
9463: # return ($filename, $path);
9464: #
9465: # The implementation below is probably faster however.
9466: #
1.710 albertel 9467: sub split_uri_for_cond {
9468: my $uri=&deversion(&declutter(shift));
9469: my @uriparts=split(/\//,$uri);
9470: my $filename=pop(@uriparts);
9471: my $pathname=join('/',@uriparts);
9472: return ($pathname,$filename);
9473: }
1.232 www 9474: # --------------------------------------------------- Is a resource on the map?
9475:
9476: sub is_on_map {
1.710 albertel 9477: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 9478: #Trying to find the conditional for the file
1.620 albertel 9479: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 9480: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 9481: if ($match) {
1.289 bowersj2 9482: return (1,$1);
9483: } else {
1.434 www 9484: return (0,0);
1.289 bowersj2 9485: }
1.12 www 9486: }
9487:
1.427 www 9488: # --------------------------------------------------------- Get symb from alias
9489:
9490: sub get_symb_from_alias {
9491: my $symb=shift;
9492: my ($map,$resid,$url)=&decode_symb($symb);
9493: # Already is a symb
9494: if ($url) { return $symb; }
9495: # Must be an alias
9496: my $aliassymb='';
9497: my %bighash;
1.620 albertel 9498: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 9499: &GDBM_READER(),0640)) {
9500: my $rid=$bighash{'mapalias_'.$symb};
9501: if ($rid) {
9502: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 9503: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
9504: $resid,$bighash{'src_'.$rid});
1.427 www 9505: }
9506: untie %bighash;
9507: }
9508: return $aliassymb;
9509: }
9510:
1.12 www 9511: # ----------------------------------------------------------------- Define Role
9512:
9513: sub definerole {
9514: if (allowed('mcr','/')) {
1.1172.2.84 raeburn 9515: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 9516: foreach my $role (split(':',$sysrole)) {
9517: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9518: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
9519: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
9520: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9521: return "refused:s:$crole&$cqual";
9522: }
9523: }
1.191 harris41 9524: }
1.800 albertel 9525: foreach my $role (split(':',$domrole)) {
9526: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9527: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
9528: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
9529: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 9530: return "refused:d:$crole&$cqual";
9531: }
9532: }
1.191 harris41 9533: }
1.800 albertel 9534: foreach my $role (split(':',$courole)) {
9535: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9536: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
9537: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
9538: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9539: return "refused:c:$crole&$cqual";
9540: }
9541: }
1.191 harris41 9542: }
1.1172.2.84 raeburn 9543: my $uhome;
9544: if (($uname ne '') && ($udom ne '')) {
9545: $uhome = &homeserver($uname,$udom);
9546: return $uhome if ($uhome eq 'no_host');
9547: } else {
9548: $uname = $env{'user.name'};
9549: $udom = $env{'user.domain'};
9550: $uhome = $env{'user.home'};
9551: }
1.620 albertel 9552: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1172.2.84 raeburn 9553: "$udom:$uname:rolesdef_$rolename=".
1.21 www 9554: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1172.2.84 raeburn 9555: return reply($command,$uhome);
1.12 www 9556: } else {
9557: return 'refused';
9558: }
1.105 harris41 9559: }
9560:
9561: # ---------------- Make a metadata query against the network of library servers
9562:
9563: sub metadata_query {
1.1172.2.33 raeburn 9564: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 9565: my %rhash;
1.845 albertel 9566: my %libserv = &all_library();
1.244 matthew 9567: my @server_list = (defined($server_array) ? @$server_array
9568: : keys(%libserv) );
9569: for my $server (@server_list) {
1.1172.2.33 raeburn 9570: my $domains = '';
9571: if (ref($domains_hash) eq 'HASH') {
9572: $domains = $domains_hash->{$server};
9573: }
1.118 harris41 9574: unless ($custom or $customshow) {
1.1172.2.33 raeburn 9575: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 9576: $rhash{$server}=$reply;
9577: }
9578: else {
9579: my $reply=&reply("querysend:".&escape($query).':'.
1.1172.2.33 raeburn 9580: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 9581: $server);
9582: $rhash{$server}=$reply;
9583: }
1.112 harris41 9584: }
1.118 harris41 9585: return \%rhash;
1.240 www 9586: }
9587:
9588: # ----------------------------------------- Send log queries and wait for reply
9589:
9590: sub log_query {
9591: my ($uname,$udom,$query,%filters)=@_;
9592: my $uhome=&homeserver($uname,$udom);
9593: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 9594: my $uhost=&hostname($uhome);
1.800 albertel 9595: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 9596: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
9597: $uhome);
1.479 albertel 9598: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 9599: return get_query_reply($queryid);
9600: }
9601:
1.818 raeburn 9602: # -------------------------- Update MySQL table for portfolio file
9603:
9604: sub update_portfolio_table {
1.821 raeburn 9605: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 9606: if ($group ne '') {
9607: $file_name =~s /^\Q$group\E//;
9608: }
1.818 raeburn 9609: my $homeserver = &homeserver($uname,$udom);
9610: my $queryid=
1.821 raeburn 9611: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
9612: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 9613: my $reply = &get_query_reply($queryid);
9614: return $reply;
9615: }
9616:
1.899 raeburn 9617: # -------------------------- Update MySQL allusers table
9618:
9619: sub update_allusers_table {
9620: my ($uname,$udom,$names) = @_;
9621: my $homeserver = &homeserver($uname,$udom);
9622: my $queryid=
9623: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
9624: 'lastname='.&escape($names->{'lastname'}).'%%'.
9625: 'firstname='.&escape($names->{'firstname'}).'%%'.
9626: 'middlename='.&escape($names->{'middlename'}).'%%'.
9627: 'generation='.&escape($names->{'generation'}).'%%'.
9628: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
9629: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 9630: return;
1.899 raeburn 9631: }
9632:
1.508 raeburn 9633: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 9634:
9635: sub fetch_enrollment_query {
1.511 raeburn 9636: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1172.2.79 raeburn 9637: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 9638: my $maxtries = 1;
1.508 raeburn 9639: if ($context eq 'automated') {
9640: $homeserver = $perlvar{'lonHostID'};
1.1172.2.79 raeburn 9641: $sleep = 2;
9642: $loopmax = 100;
1.547 raeburn 9643: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 9644: } else {
9645: $homeserver = &homeserver($cnum,$dom);
9646: }
1.838 albertel 9647: my $host=&hostname($homeserver);
1.506 raeburn 9648: my $cmd = '';
1.1000 raeburn 9649: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 9650: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 9651: }
9652: $cmd =~ s/%%$//;
9653: $cmd = &escape($cmd);
9654: my $query = 'fetchenrollment';
1.620 albertel 9655: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 9656: unless ($queryid=~/^\Q$host\E\_/) {
9657: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
9658: return 'error: '.$queryid;
9659: }
1.1172.2.93 raeburn 9660: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9661: my $tries = 1;
9662: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1172.2.79 raeburn 9663: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9664: $tries ++;
9665: }
1.526 raeburn 9666: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 9667: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 9668: } else {
1.901 albertel 9669: my @responses = split(/:/,$reply);
1.1172.2.82 raeburn 9670: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 9671: foreach my $line (@responses) {
9672: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 9673: $$replyref{$key} = $value;
9674: }
9675: } else {
1.1117 foxr 9676: my $pathname = LONCAPA::tempdir();
1.800 albertel 9677: foreach my $line (@responses) {
9678: my ($key,$value) = split(/=/,$line);
1.506 raeburn 9679: $$replyref{$key} = $value;
9680: if ($value > 0) {
1.800 albertel 9681: foreach my $item (@{$$affiliatesref{$key}}) {
9682: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 9683: my $destname = $pathname.'/'.$filename;
9684: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 9685: if ($xml_classlist =~ /^error/) {
9686: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
9687: } else {
1.1172.2.96 raeburn 9688: if ( open(FILE,">",$destname) ) {
1.506 raeburn 9689: print FILE &unescape($xml_classlist);
9690: close(FILE);
1.526 raeburn 9691: } else {
9692: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 9693: }
9694: }
9695: }
9696: }
9697: }
9698: }
9699: return 'ok';
9700: }
9701: return 'error';
9702: }
9703:
1.242 www 9704: sub get_query_reply {
1.1172.2.79 raeburn 9705: my ($queryid,$sleep,$loopmax) = @_;
9706: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
9707: $sleep = 0.2;
9708: }
9709: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
9710: $loopmax = 100;
9711: }
1.1117 foxr 9712: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 9713: my $reply='';
1.1172.2.79 raeburn 9714: for (1..$loopmax) {
9715: sleep($sleep);
1.240 www 9716: if (-e $replyfile.'.end') {
1.1172.2.96 raeburn 9717: if (open(my $fh,"<",$replyfile)) {
1.904 albertel 9718: $reply = join('',<$fh>);
9719: close($fh);
1.240 www 9720: } else { return 'error: reply_file_error'; }
1.242 www 9721: return &unescape($reply);
9722: }
1.240 www 9723: }
1.242 www 9724: return 'timeout:'.$queryid;
1.240 www 9725: }
9726:
9727: sub courselog_query {
1.241 www 9728: #
9729: # possible filters:
9730: # url: url or symb
9731: # username
9732: # domain
9733: # action: view, submit, grade
9734: # start: timestamp
9735: # end: timestamp
9736: #
1.240 www 9737: my (%filters)=@_;
1.620 albertel 9738: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 9739: if ($filters{'url'}) {
9740: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
9741: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
9742: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
9743: }
1.620 albertel 9744: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
9745: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 9746: return &log_query($cname,$cdom,'courselog',%filters);
9747: }
9748:
9749: sub userlog_query {
1.858 raeburn 9750: #
9751: # possible filters:
9752: # action: log check role
9753: # start: timestamp
9754: # end: timestamp
9755: #
1.240 www 9756: my ($uname,$udom,%filters)=@_;
9757: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 9758: }
9759:
1.506 raeburn 9760: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
9761:
9762: sub auto_run {
1.508 raeburn 9763: my ($cnum,$cdom) = @_;
1.876 raeburn 9764: my $response = 0;
9765: my $settings;
9766: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
9767: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
9768: $settings = $domconfig{'autoenroll'};
9769: if ($settings->{'run'} eq '1') {
9770: $response = 1;
9771: }
9772: } else {
1.934 raeburn 9773: my $homeserver;
9774: if (&is_course($cdom,$cnum)) {
9775: $homeserver = &homeserver($cnum,$cdom);
9776: } else {
9777: $homeserver = &domain($cdom,'primary');
9778: }
9779: if ($homeserver ne 'no_host') {
9780: $response = &reply('autorun:'.$cdom,$homeserver);
9781: }
1.876 raeburn 9782: }
1.506 raeburn 9783: return $response;
9784: }
1.776 albertel 9785:
1.506 raeburn 9786: sub auto_get_sections {
1.508 raeburn 9787: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 9788: my $homeserver;
9789: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9790: $homeserver = &homeserver($cnum,$cdom);
9791: }
9792: if (!defined($homeserver)) {
9793: if ($cdom =~ /^$match_domain$/) {
9794: $homeserver = &domain($cdom,'primary');
9795: }
9796: }
9797: my @secs;
9798: if (defined($homeserver)) {
9799: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
9800: unless ($response eq 'refused') {
9801: @secs = split(/:/,$response);
9802: }
1.506 raeburn 9803: }
9804: return @secs;
9805: }
1.776 albertel 9806:
1.506 raeburn 9807: sub auto_new_course {
1.1099 raeburn 9808: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 9809: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 9810: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 9811: return $response;
9812: }
1.776 albertel 9813:
1.506 raeburn 9814: sub auto_validate_courseID {
1.508 raeburn 9815: my ($cnum,$cdom,$inst_course_id) = @_;
9816: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 9817: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 9818: return $response;
9819: }
1.776 albertel 9820:
1.1007 raeburn 9821: sub auto_validate_instcode {
1.1020 raeburn 9822: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 9823: my ($homeserver,$response);
9824: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9825: $homeserver = &homeserver($cnum,$cdom);
9826: }
9827: if (!defined($homeserver)) {
9828: if ($cdom =~ /^$match_domain$/) {
9829: $homeserver = &domain($cdom,'primary');
9830: }
9831: }
1.1065 raeburn 9832: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
9833: &escape($instcode).':'.&escape($owner),$homeserver));
1.1172.2.19 raeburn 9834: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
9835: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 9836: }
9837:
1.1172.2.141 raeburn 9838: sub auto_validate_inst_crosslist {
9839: my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
9840: my ($homeserver,$response);
9841: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9842: $homeserver = &homeserver($cnum,$cdom);
9843: }
9844: if (!defined($homeserver)) {
9845: if ($cdom =~ /^$match_domain$/) {
9846: $homeserver = &domain($cdom,'primary');
9847: }
9848: }
9849: unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
9850: $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
9851: &escape($instcode).':'.&escape($inst_xlist).':'.
9852: &escape($coowner),$homeserver);
9853: }
9854: return $response;
9855: }
9856:
1.506 raeburn 9857: sub auto_create_password {
1.873 raeburn 9858: my ($cnum,$cdom,$authparam,$udom) = @_;
9859: my ($homeserver,$response);
1.506 raeburn 9860: my $create_passwd = 0;
9861: my $authchk = '';
1.873 raeburn 9862: if ($udom =~ /^$match_domain$/) {
9863: $homeserver = &domain($udom,'primary');
9864: }
9865: if ($homeserver eq '') {
9866: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9867: $homeserver = &homeserver($cnum,$cdom);
9868: }
9869: }
9870: if ($homeserver eq '') {
9871: $authchk = 'nodomain';
1.506 raeburn 9872: } else {
1.873 raeburn 9873: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
9874: if ($response eq 'refused') {
9875: $authchk = 'refused';
9876: } else {
1.901 albertel 9877: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 9878: }
1.506 raeburn 9879: }
9880: return ($authparam,$create_passwd,$authchk);
9881: }
9882:
1.706 raeburn 9883: sub auto_photo_permission {
9884: my ($cnum,$cdom,$students) = @_;
9885: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 9886: my ($outcome,$perm_reqd,$conditions) =
9887: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 9888: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9889: return (undef,undef);
9890: }
1.706 raeburn 9891: return ($outcome,$perm_reqd,$conditions);
9892: }
9893:
9894: sub auto_checkphotos {
9895: my ($uname,$udom,$pid) = @_;
9896: my $homeserver = &homeserver($uname,$udom);
9897: my ($result,$resulttype);
9898: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 9899: &escape($uname).':'.&escape($pid),
9900: $homeserver));
1.709 albertel 9901: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9902: return (undef,undef);
9903: }
1.706 raeburn 9904: if ($outcome) {
9905: ($result,$resulttype) = split(/:/,$outcome);
9906: }
9907: return ($result,$resulttype);
9908: }
9909:
9910: sub auto_photochoice {
9911: my ($cnum,$cdom) = @_;
9912: my $homeserver = &homeserver($cnum,$cdom);
9913: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 9914: &escape($cdom),
9915: $homeserver)));
1.709 albertel 9916: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9917: return (undef,undef);
9918: }
1.706 raeburn 9919: return ($update,$comment);
9920: }
9921:
9922: sub auto_photoupdate {
9923: my ($affiliatesref,$dom,$cnum,$photo) = @_;
9924: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 9925: my $host=&hostname($homeserver);
1.706 raeburn 9926: my $cmd = '';
9927: my $maxtries = 1;
1.800 albertel 9928: foreach my $affiliate (keys(%{$affiliatesref})) {
9929: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 9930: }
9931: $cmd =~ s/%%$//;
9932: $cmd = &escape($cmd);
9933: my $query = 'institutionalphotos';
9934: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
9935: unless ($queryid=~/^\Q$host\E\_/) {
9936: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
9937: return 'error: '.$queryid;
9938: }
9939: my $reply = &get_query_reply($queryid);
9940: my $tries = 1;
9941: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
9942: $reply = &get_query_reply($queryid);
9943: $tries ++;
9944: }
9945: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
9946: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
9947: } else {
9948: my @responses = split(/:/,$reply);
9949: my $outcome = shift(@responses);
9950: foreach my $item (@responses) {
9951: my ($key,$value) = split(/=/,$item);
9952: $$photo{$key} = $value;
9953: }
9954: return $outcome;
9955: }
9956: return 'error';
9957: }
9958:
1.521 raeburn 9959: sub auto_instcode_format {
1.793 albertel 9960: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
9961: $cat_order) = @_;
1.521 raeburn 9962: my $courses = '';
1.772 raeburn 9963: my @homeservers;
1.521 raeburn 9964: if ($caller eq 'global') {
1.841 albertel 9965: my %servers = &get_servers($codedom,'library');
9966: foreach my $tryserver (keys(%servers)) {
9967: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9968: push(@homeservers,$tryserver);
9969: }
1.584 raeburn 9970: }
1.1022 raeburn 9971: } elsif ($caller eq 'requests') {
9972: if ($codedom =~ /^$match_domain$/) {
9973: my $chome = &domain($codedom,'primary');
9974: unless ($chome eq 'no_host') {
9975: push(@homeservers,$chome);
9976: }
9977: }
1.521 raeburn 9978: } else {
1.772 raeburn 9979: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 9980: }
1.793 albertel 9981: foreach my $code (keys(%{$instcodes})) {
9982: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 9983: }
9984: chop($courses);
1.772 raeburn 9985: my $ok_response = 0;
9986: my $response;
9987: while (@homeservers > 0 && $ok_response == 0) {
9988: my $server = shift(@homeservers);
9989: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
9990: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
9991: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 9992: split(/:/,$response);
1.772 raeburn 9993: %{$codes} = (%{$codes},&str2hash($codes_str));
9994: push(@{$codetitles},&str2array($codetitles_str));
9995: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
9996: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
9997: $ok_response = 1;
9998: }
9999: }
10000: if ($ok_response) {
1.521 raeburn 10001: return 'ok';
1.772 raeburn 10002: } else {
10003: return $response;
1.521 raeburn 10004: }
10005: }
10006:
1.792 raeburn 10007: sub auto_instcode_defaults {
10008: my ($domain,$returnhash,$code_order) = @_;
10009: my @homeservers;
1.841 albertel 10010:
10011: my %servers = &get_servers($domain,'library');
10012: foreach my $tryserver (keys(%servers)) {
10013: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10014: push(@homeservers,$tryserver);
10015: }
1.792 raeburn 10016: }
1.841 albertel 10017:
1.792 raeburn 10018: my $response;
1.841 albertel 10019: foreach my $server (@homeservers) {
1.792 raeburn 10020: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 10021: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10022:
10023: foreach my $pair (split(/\&/,$response)) {
10024: my ($name,$value)=split(/\=/,$pair);
10025: if ($name eq 'code_order') {
10026: @{$code_order} = split(/\&/,&unescape($value));
10027: } else {
10028: $returnhash->{&unescape($name)}=&unescape($value);
10029: }
10030: }
10031: return 'ok';
1.792 raeburn 10032: }
1.841 albertel 10033:
10034: return $response;
1.1003 raeburn 10035: }
10036:
10037: sub auto_possible_instcodes {
1.1007 raeburn 10038: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10039: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
10040: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10041: return;
10042: }
1.1003 raeburn 10043: my (@homeservers,$uhome);
10044: if (defined(&domain($domain,'primary'))) {
10045: $uhome=&domain($domain,'primary');
10046: push(@homeservers,&domain($domain,'primary'));
10047: } else {
10048: my %servers = &get_servers($domain,'library');
10049: foreach my $tryserver (keys(%servers)) {
10050: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10051: push(@homeservers,$tryserver);
10052: }
10053: }
10054: }
10055: my $response;
10056: foreach my $server (@homeservers) {
10057: $response=&reply('autopossibleinstcodes:'.$domain,$server);
10058: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 10059: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
10060: split(':',$response);
10061: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10062: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 10063: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 10064: my ($name,$value)=split('=',$item);
10065: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10066: }
10067: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 10068: my ($name,$value)=split('=',$item);
10069: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10070: }
10071: return 'ok';
10072: }
10073: return $response;
10074: }
1.792 raeburn 10075:
1.1010 raeburn 10076: sub auto_courserequest_checks {
10077: my ($dom) = @_;
1.1020 raeburn 10078: my ($homeserver,%validations);
10079: if ($dom =~ /^$match_domain$/) {
10080: $homeserver = &domain($dom,'primary');
10081: }
10082: unless ($homeserver eq 'no_host') {
10083: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10084: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10085: my @items = split(/&/,$response);
10086: foreach my $item (@items) {
10087: my ($key,$value) = split('=',$item);
10088: $validations{&unescape($key)} = &thaw_unescape($value);
10089: }
10090: }
10091: }
1.1010 raeburn 10092: return %validations;
10093: }
10094:
1.1020 raeburn 10095: sub auto_courserequest_validation {
1.1172.2.43 raeburn 10096: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 10097: my ($homeserver,$response);
10098: if ($dom =~ /^$match_domain$/) {
10099: $homeserver = &domain($dom,'primary');
10100: }
1.1172.2.43 raeburn 10101: unless ($homeserver eq 'no_host') {
10102: my $customdata;
10103: if (ref($custominfo) eq 'HASH') {
10104: $customdata = &freeze_escape($custominfo);
10105: }
1.1020 raeburn 10106: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 10107: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1172.2.43 raeburn 10108: ':'.&escape($instcode).':'.&escape($instseclist).':'.
10109: $customdata,$homeserver));
1.1020 raeburn 10110: }
10111: return $response;
10112: }
10113:
1.777 albertel 10114: sub auto_validate_class_sec {
1.918 raeburn 10115: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 10116: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 10117: my $ownerlist;
10118: if (ref($owners) eq 'ARRAY') {
10119: $ownerlist = join(',',@{$owners});
10120: } else {
10121: $ownerlist = $owners;
10122: }
1.773 raeburn 10123: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 10124: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 10125: return $response;
10126: }
10127:
1.1172.2.141 raeburn 10128: sub auto_instsec_reformat {
10129: my ($cdom,$action,$instsecref) = @_;
10130: return unless(($action eq 'clutter') || ($action eq 'declutter'));
10131: my @homeservers;
10132: if (defined(&domain($cdom,'primary'))) {
10133: push(@homeservers,&domain($cdom,'primary'));
10134: } else {
10135: my %servers = &get_servers($cdom,'library');
10136: foreach my $tryserver (keys(%servers)) {
10137: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10138: push(@homeservers,$tryserver);
10139: }
10140: }
10141: }
10142: my $response;
10143: my %reformatted = %{$instsecref};
10144: foreach my $server (@homeservers) {
10145: if (ref($instsecref) eq 'HASH') {
10146: my $info = &freeze_escape($instsecref);
10147: my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10148: $action.':'.$info,$server);
1.1172.2.146. .26(raeb 10149:-25): next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/);
1.1172.2.141 raeburn 10150: my @items = split(/&/,$response);
10151: foreach my $item (@items) {
10152: my ($key,$value) = split(/=/,$item);
10153: $reformatted{&unescape($key)} = &thaw_unescape($value);
10154: }
10155: }
10156: }
10157: return %reformatted;
10158: }
10159:
1.1172.2.94 raeburn 10160: sub auto_validate_instclasses {
10161: my ($cdom,$cnum,$owners,$classesref) = @_;
10162: my ($homeserver,%validations);
10163: $homeserver = &homeserver($cnum,$cdom);
10164: unless ($homeserver eq 'no_host') {
10165: my $ownerlist;
10166: if (ref($owners) eq 'ARRAY') {
10167: $ownerlist = join(',',@{$owners});
10168: } else {
10169: $ownerlist = $owners;
10170: }
10171: if (ref($classesref) eq 'HASH') {
10172: my $classes = &freeze_escape($classesref);
10173: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10174: ':'.$cdom.':'.$classes,$homeserver);
10175: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10176: my @items = split(/&/,$response);
10177: foreach my $item (@items) {
10178: my ($key,$value) = split('=',$item);
10179: $validations{&unescape($key)} = &thaw_unescape($value);
10180: }
10181: }
10182: }
10183: }
10184: return %validations;
10185: }
10186:
1.1172.2.38 raeburn 10187: sub auto_crsreq_update {
10188: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1172.2.45 raeburn 10189: $code,$accessstart,$accessend,$inbound) = @_;
1.1172.2.38 raeburn 10190: my ($homeserver,%crsreqresponse);
10191: if ($cdom =~ /^$match_domain$/) {
10192: $homeserver = &domain($cdom,'primary');
10193: }
10194: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10195: my $info;
10196: if (ref($inbound) eq 'HASH') {
10197: $info = &freeze_escape($inbound);
10198: }
10199: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10200: ':'.&escape($action).':'.&escape($ownername).':'.
10201: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1172.2.45 raeburn 10202: &escape($title).':'.&escape($code).':'.
10203: &escape($accessstart).':'.&escape($accessend).':'.$info,$homeserver);
1.1172.2.38 raeburn 10204: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10205: my @items = split(/&/,$response);
10206: foreach my $item (@items) {
10207: my ($key,$value) = split('=',$item);
10208: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10209: }
10210: }
10211: }
10212: return \%crsreqresponse;
10213: }
10214:
1.1172.2.78 raeburn 10215: sub auto_export_grades {
10216: my ($cdom,$cnum,$inforef,$gradesref) = @_;
10217: my ($homeserver,%exportresponse);
10218: if ($cdom =~ /^$match_domain$/) {
10219: $homeserver = &domain($cdom,'primary');
10220: }
10221: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10222: my $info;
10223: if (ref($inforef) eq 'HASH') {
10224: $info = &freeze_escape($inforef);
10225: }
10226: if (ref($gradesref) eq 'HASH') {
10227: my $grades = &freeze_escape($gradesref);
10228: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10229: $info.':'.$grades,$homeserver);
1.1172.2.146. .26(raeb 10230:-25): unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/) {
1.1172.2.78 raeburn 10231: my @items = split(/&/,$response);
10232: foreach my $item (@items) {
10233: my ($key,$value) = split('=',$item);
10234: $exportresponse{&unescape($key)} = &thaw_unescape($value);
10235: }
10236: }
10237: }
10238: }
10239: return \%exportresponse;
10240: }
10241:
1.1172.2.68 raeburn 10242: sub check_instcode_cloning {
10243: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10244: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10245: return;
10246: }
10247: my $canclone;
10248: if (@{$code_order} > 0) {
10249: my $instcoderegexp ='^';
10250: my @clonecodes = split(/\&/,$cloner);
10251: foreach my $item (@{$code_order}) {
10252: if (grep(/^\Q$item\E=/,@clonecodes)) {
10253: foreach my $pair (@clonecodes) {
10254: my ($key,$val) = split(/\=/,$pair,2);
10255: $val = &unescape($val);
10256: if ($key eq $item) {
10257: $instcoderegexp .= '('.$val.')';
10258: last;
10259: }
10260: }
10261: } else {
10262: $instcoderegexp .= $codedefaults->{$item};
10263: }
10264: }
10265: $instcoderegexp .= '$';
10266: my (@from,@to);
10267: eval {
10268: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10269: (@to) = ($clonetocode =~ /$instcoderegexp/);
10270: };
10271: if ((@from > 0) && (@to > 0)) {
10272: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10273: if (!@diffs) {
10274: $canclone = 1;
10275: }
10276: }
10277: }
10278: return $canclone;
10279: }
10280:
10281: sub default_instcode_cloning {
10282: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10283: my (%codedefaults,@code_order,$canclone);
10284: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10285: %codedefaults = %{$codedefaultsref};
10286: @code_order = @{$codeorderref};
10287: } elsif ($clonedom) {
10288: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10289: }
10290: if (($domdefclone) && (@code_order)) {
10291: my @clonecodes = split(/\+/,$domdefclone);
10292: my $instcoderegexp ='^';
10293: foreach my $item (@code_order) {
10294: if (grep(/^\Q$item\E$/,@clonecodes)) {
10295: $instcoderegexp .= '('.$codedefaults{$item}.')';
10296: } else {
10297: $instcoderegexp .= $codedefaults{$item};
10298: }
10299: }
10300: $instcoderegexp .= '$';
10301: my (@from,@to);
10302: eval {
10303: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10304: (@to) = ($clonetocode =~ /$instcoderegexp/);
10305: };
10306: if ((@from > 0) && (@to > 0)) {
10307: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10308: if (!@diffs) {
10309: $canclone = 1;
10310: }
10311: }
10312: }
10313: return $canclone;
10314: }
10315:
1.679 raeburn 10316: # ------------------------------------------------------- Course Group routines
10317:
10318: sub get_coursegroups {
1.809 raeburn 10319: my ($cdom,$cnum,$group,$namespace) = @_;
10320: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 10321: }
10322:
1.679 raeburn 10323: sub modify_coursegroup {
10324: my ($cdom,$cnum,$groupsettings) = @_;
10325: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10326: }
10327:
1.809 raeburn 10328: sub toggle_coursegroup_status {
10329: my ($cdom,$cnum,$group,$action) = @_;
10330: my ($from_namespace,$to_namespace);
10331: if ($action eq 'delete') {
10332: $from_namespace = 'coursegroups';
10333: $to_namespace = 'deleted_groups';
10334: } else {
10335: $from_namespace = 'deleted_groups';
10336: $to_namespace = 'coursegroups';
10337: }
10338: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 10339: if (my $tmp = &error(%curr_group)) {
10340: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10341: return ('read error',$tmp);
10342: } else {
10343: my %savedsettings = %curr_group;
1.809 raeburn 10344: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 10345: my $deloutcome;
10346: if ($result eq 'ok') {
1.809 raeburn 10347: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 10348: } else {
10349: return ('write error',$result);
10350: }
10351: if ($deloutcome eq 'ok') {
10352: return 'ok';
10353: } else {
10354: return ('delete error',$deloutcome);
10355: }
10356: }
10357: }
10358:
1.679 raeburn 10359: sub modify_group_roles {
1.957 raeburn 10360: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 10361: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10362: my $role = 'gr/'.&escape($userprivs);
10363: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 10364: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 10365: if ($result eq 'ok') {
10366: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10367: }
1.679 raeburn 10368: return $result;
10369: }
10370:
10371: sub modify_coursegroup_membership {
10372: my ($cdom,$cnum,$membership) = @_;
10373: my $result = &put('groupmembership',$membership,$cdom,$cnum);
10374: return $result;
10375: }
10376:
1.682 raeburn 10377: sub get_active_groups {
10378: my ($udom,$uname,$cdom,$cnum) = @_;
10379: my $now = time;
10380: my %groups = ();
10381: foreach my $key (keys(%env)) {
1.811 albertel 10382: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 10383: my ($start,$end) = split(/\./,$env{$key});
10384: if (($end!=0) && ($end<$now)) { next; }
10385: if (($start!=0) && ($start>$now)) { next; }
10386: if ($1 eq $cdom && $2 eq $cnum) {
10387: $groups{$3} = $env{$key} ;
10388: }
10389: }
10390: }
10391: return %groups;
10392: }
10393:
1.683 raeburn 10394: sub get_group_membership {
10395: my ($cdom,$cnum,$group) = @_;
10396: return(&dump('groupmembership',$cdom,$cnum,$group));
10397: }
10398:
10399: sub get_users_groups {
10400: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 10401: my @usersgroups;
1.683 raeburn 10402: my $cachetime=1800;
10403:
10404: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 10405: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10406: if (defined($cached)) {
1.734 albertel 10407: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 10408: } else {
10409: $grouplist = '';
1.816 raeburn 10410: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 10411: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 10412: my $access_end = $env{'course.'.$courseid.
10413: '.default_enrollment_end_date'};
10414: my $now = time;
10415: foreach my $key (keys(%roleshash)) {
10416: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10417: my $group = $1;
10418: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10419: my $start = $2;
10420: my $end = $1;
10421: if ($start == -1) { next; } # deleted from group
10422: if (($start!=0) && ($start>$now)) { next; }
10423: if (($end!=0) && ($end<$now)) {
10424: if ($access_end && $access_end < $now) {
10425: if ($access_end - $end < 86400) {
10426: push(@usersgroups,$group);
1.733 raeburn 10427: }
10428: }
1.817 raeburn 10429: next;
1.733 raeburn 10430: }
1.817 raeburn 10431: push(@usersgroups,$group);
1.683 raeburn 10432: }
10433: }
10434: }
1.817 raeburn 10435: @usersgroups = &sort_course_groups($courseid,@usersgroups);
10436: $grouplist = join(':',@usersgroups);
10437: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 10438: }
1.733 raeburn 10439: return @usersgroups;
1.683 raeburn 10440: }
10441:
10442: sub devalidate_getgroups_cache {
10443: my ($udom,$uname,$cdom,$cnum)=@_;
10444: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 10445:
1.683 raeburn 10446: my $hashid="$udom:$uname:$courseid";
10447: &devalidate_cache_new('getgroups',$hashid);
10448: }
10449:
1.12 www 10450: # ------------------------------------------------------------------ Plain Text
10451:
10452: sub plaintext {
1.988 raeburn 10453: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 10454: if ($short =~ m{^cr/}) {
1.758 albertel 10455: return (split('/',$short))[-1];
10456: }
1.742 raeburn 10457: if (!defined($cid)) {
10458: $cid = $env{'request.course.id'};
10459: }
10460: my %rolenames = (
1.1008 raeburn 10461: Course => 'std',
10462: Community => 'alt1',
1.742 raeburn 10463: );
1.1037 raeburn 10464: if ($cid ne '') {
10465: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10466: unless ($forcedefault) {
10467: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
10468: &Apache::lonlocal::mt_escape(\$roletext);
10469: return &Apache::lonlocal::mt($roletext);
10470: }
10471: }
10472: }
10473: if ((defined($type)) && (defined($rolenames{$type})) &&
10474: (defined($rolenames{$type})) &&
10475: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 10476: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 10477: } elsif ($cid ne '') {
10478: my $crstype = $env{'course.'.$cid.'.type'};
10479: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10480: (defined($prp{$short}{$rolenames{$crstype}}))) {
10481: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10482: }
1.742 raeburn 10483: }
1.1037 raeburn 10484: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 10485: }
10486:
10487: # ----------------------------------------------------------------- Assign Role
10488:
10489: sub assignrole {
1.957 raeburn 10490: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10491: $context)=@_;
1.1172.2.146. .22(raeb 10492:-24): my ($mrole,$rolelogcontext);
1.21 www 10493: if ($role =~ /^cr\//) {
1.393 www 10494: my $cwosec=$url;
1.811 albertel 10495: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 10496: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 10497: my $refused = 1;
10498: if ($context eq 'requestcourses') {
10499: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10500: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10501: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10502: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10503: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10504: if ($crsenv{'internal.courseowner'} eq
10505: $env{'user.name'}.':'.$env{'user.domain'}) {
10506: $refused = '';
10507: }
10508: }
10509: }
10510: }
10511: }
10512: if ($refused) {
10513: &logthis('Refused custom assignrole: '.
10514: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10515: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10516: return 'refused';
10517: }
1.104 www 10518: }
1.21 www 10519: $mrole='cr';
1.678 raeburn 10520: } elsif ($role =~ /^gr\//) {
10521: my $cwogrp=$url;
1.811 albertel 10522: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 10523: unless (&allowed('mdg',$cwogrp)) {
10524: &logthis('Refused group assignrole: '.
10525: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10526: $env{'user.name'}.' at '.$env{'user.domain'});
10527: return 'refused';
10528: }
10529: $mrole='gr';
1.21 www 10530: } else {
1.82 www 10531: my $cwosec=$url;
1.811 albertel 10532: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 10533: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10534: my $refused;
10535: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
10536: if (!(&allowed('c'.$role,$url))) {
10537: $refused = 1;
10538: }
10539: } else {
10540: $refused = 1;
10541: }
1.947 raeburn 10542: if ($refused) {
1.1045 raeburn 10543: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10544: if (!$selfenroll && $context eq 'course') {
10545: my %crsenv;
10546: if ($role eq 'cc' || $role eq 'co') {
10547: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10548: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10549: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10550: if ($crsenv{'internal.courseowner'} eq
10551: $env{'user.name'}.':'.$env{'user.domain'}) {
10552: $refused = '';
10553: }
10554: }
10555: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
10556: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10557: if ($crsenv{'internal.courseowner'} eq
10558: $env{'user.name'}.':'.$env{'user.domain'}) {
10559: $refused = '';
10560: }
10561: }
10562: }
10563: }
1.1172.2.146. .13(raeb 10564:-23): } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10565:-23): if ($role eq 'st') {
10566:-23): $refused = '';
10567:-23): } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10568:-23): $refused = '';
10569:-23): }
1.1017 raeburn 10570: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 10571: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 10572: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 10573: my $wrongcc;
10574: if ($cnum =~ /^$match_community$/) {
10575: $wrongcc = 1 if ($role eq 'cc');
10576: } else {
10577: $wrongcc = 1 if ($role eq 'co');
10578: }
10579: unless ($wrongcc) {
10580: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10581: if ($crsenv{'internal.courseowner'} eq
10582: $env{'user.name'}.':'.$env{'user.domain'}) {
10583: $refused = '';
10584: }
1.1017 raeburn 10585: }
10586: }
1.1172.2.9 raeburn 10587: } elsif ($context eq 'requestauthor') {
10588: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
10589: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10590: if ($env{'environment.requestauthor'} eq 'automatic') {
10591: $refused = '';
10592: } else {
10593: my %domdefaults = &get_domain_defaults($udom);
10594: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10595: my $checkbystatus;
10596: if ($env{'user.adv'}) {
10597: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10598: if ($disposition eq 'automatic') {
10599: $refused = '';
10600: } elsif ($disposition eq '') {
10601: $checkbystatus = 1;
10602: }
10603: } else {
10604: $checkbystatus = 1;
10605: }
10606: if ($checkbystatus) {
10607: if ($env{'environment.inststatus'}) {
10608: my @inststatuses = split(/,/,$env{'environment.inststatus'});
10609: foreach my $type (@inststatuses) {
10610: if (($type ne '') &&
10611: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10612: $refused = '';
10613: }
10614: }
10615: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10616: $refused = '';
10617: }
10618: }
10619: }
10620: }
10621: }
1.1172.2.146. .22(raeb 10622:-24): } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10623:-24): if ($url =~ m{^/($match_domain)/($match_username)$}) {
10624:-24): my ($audom,$auname) = ($1,$2);
10625:-24): if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10626:-24): ($env{"environment.internal.manager.$url"})) {
10627:-24): $refused = '';
10628:-24): $rolelogcontext = 'coauthor';
10629:-24): }
10630:-24): }
1.1017 raeburn 10631: }
10632: if ($refused) {
1.947 raeburn 10633: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10634: ' '.$role.' '.$end.' '.$start.' by '.
10635: $env{'user.name'}.' at '.$env{'user.domain'});
10636: return 'refused';
10637: }
1.932 raeburn 10638: }
1.1131 raeburn 10639: } elsif ($role eq 'au') {
10640: if ($url ne '/'.$udom.'/') {
10641: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10642: ' to assign author role for '.$uname.':'.$udom.
10643: ' in domain: '.$url.' refused (wrong domain).');
10644: return 'refused';
10645: }
1.104 www 10646: }
1.21 www 10647: $mrole=$role;
10648: }
1.620 albertel 10649: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 10650: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 10651: if ($end) { $command.='_'.$end; }
1.21 www 10652: if ($start) {
10653: if ($end) {
1.81 www 10654: $command.='_'.$start;
1.21 www 10655: } else {
1.81 www 10656: $command.='_0_'.$start;
1.21 www 10657: }
10658: }
1.739 raeburn 10659: my $origstart = $start;
10660: my $origend = $end;
1.957 raeburn 10661: my $delflag;
1.357 www 10662: # actually delete
10663: if ($deleteflag) {
1.373 www 10664: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 10665: # modify command to delete the role
1.620 albertel 10666: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 10667: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 10668: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 10669: # set start and finish to negative values for userrolelog
10670: $start=-1;
10671: $end=-1;
1.957 raeburn 10672: $delflag = 1;
1.357 www 10673: }
10674: }
10675: # send command
1.349 www 10676: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 10677: # log new user role if status is ok
1.349 www 10678: if ($answer eq 'ok') {
1.663 raeburn 10679: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1172.2.9 raeburn 10680: if (($role eq 'cc') || ($role eq 'in') ||
10681: ($role eq 'ep') || ($role eq 'ad') ||
10682: ($role eq 'ta') || ($role eq 'st') ||
10683: ($role=~/^cr/) || ($role eq 'gr') ||
10684: ($role eq 'co')) {
1.1172.2.13 raeburn 10685: # for course roles, perform group memberships changes triggered by role change.
10686: unless ($role =~ /^gr/) {
10687: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10688: $origstart,$selfenroll,$context);
10689: }
1.1172.2.9 raeburn 10690: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10691: $selfenroll,$context);
10692: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1172.2.90 raeburn 10693: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10694: ($role eq 'da')) {
1.1172.2.9 raeburn 10695: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10696: $context);
10697: } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1172.2.146. .22(raeb 10698:-24): if ($rolelogcontext eq '') {
10699:-24): $rolelogcontext = $context;
10700:-24): }
1.1172.2.9 raeburn 10701: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1172.2.146. .22(raeb 10702:-24): $rolelogcontext);
1.1172.2.9 raeburn 10703: }
1.1053 raeburn 10704: if ($role eq 'cc') {
10705: &autoupdate_coowners($url,$end,$start,$uname,$udom);
10706: }
1.349 www 10707: }
10708: return $answer;
1.169 harris41 10709: }
10710:
1.1053 raeburn 10711: sub autoupdate_coowners {
10712: my ($url,$end,$start,$uname,$udom) = @_;
10713: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10714: if (($cdom ne '') && ($cnum ne '')) {
10715: my $now = time;
10716: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10717: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10718: my %coursehash = &coursedescription($cdom.'_'.$cnum);
10719: my $instcode = $coursehash{'internal.coursecode'};
1.1172.2.141 raeburn 10720: my $xlists = $coursehash{'internal.crosslistings'};
1.1053 raeburn 10721: if ($instcode ne '') {
1.1056 raeburn 10722: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10723: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 10724: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 10725: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1172.2.141 raeburn 10726: unless ($result eq 'valid') {
10727: if ($xlists ne '') {
10728: foreach my $xlist (split(',',$xlists)) {
10729: my ($inst_crosslist,$lcsec) = split(':',$xlist);
10730: $result =
10731: &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10732: $inst_crosslist,$uname.':'.$udom);
10733: last if ($result eq 'valid');
10734: }
10735: }
10736: }
1.1056 raeburn 10737: if ($result eq 'valid') {
10738: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 10739: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10740: push(@newcoowners,$coowner);
10741: }
10742: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10743: push(@newcoowners,$uname.':'.$udom);
10744: }
10745: @newcoowners = sort(@newcoowners);
10746: } else {
10747: push(@newcoowners,$uname.':'.$udom);
10748: }
1.1172.2.141 raeburn 10749: } elsif ($coursehash{'internal.co-owners'}) {
10750: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10751: unless ($coowner eq $uname.':'.$udom) {
10752: push(@newcoowners,$coowner);
1.1053 raeburn 10753: }
10754: }
1.1172.2.141 raeburn 10755: unless (@newcoowners > 0) {
10756: $delcoowners = 1;
10757: $coowners = '';
10758: }
1.1053 raeburn 10759: }
10760: if (@newcoowners || $delcoowners) {
10761: &store_coowners($cdom,$cnum,$coursehash{'home'},
10762: $delcoowners,@newcoowners);
10763: }
10764: }
10765: }
10766: }
10767: }
10768: }
10769: }
10770:
10771: sub store_coowners {
10772: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10773: my $cid = $cdom.'_'.$cnum;
10774: my ($coowners,$delresult,$putresult);
10775: if (@newcoowners) {
10776: $coowners = join(',',@newcoowners);
10777: my %coownershash = (
10778: 'internal.co-owners' => $coowners,
10779: );
10780: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10781: if ($putresult eq 'ok') {
10782: if ($env{'course.'.$cid.'.num'} eq $cnum) {
10783: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10784: }
10785: }
10786: }
10787: if ($delcoowners) {
10788: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10789: if ($delresult eq 'ok') {
10790: if ($env{'course.'.$cid.'.internal.co-owners'}) {
10791: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10792: }
10793: }
10794: }
10795: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10796: my %crsinfo =
10797: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10798: if (ref($crsinfo{$cid}) eq 'HASH') {
10799: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10800: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10801: }
10802: }
10803: }
10804:
1.169 harris41 10805: # -------------------------------------------------- Modify user authentication
1.197 www 10806: # Overrides without validation
10807:
1.169 harris41 10808: sub modifyuserauth {
10809: my ($udom,$uname,$umode,$upass)=@_;
10810: my $uhome=&homeserver($uname,$udom);
1.1172.2.115 raeburn 10811: my $allowed;
10812: if (&allowed('mau',$udom)) {
10813: $allowed = 1;
10814: } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10815: ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10816: (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10817: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10818: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10819: if (($cdom ne '') && ($cnum ne '')) {
10820: my $is_owner = &is_course_owner($cdom,$cnum);
10821: if ($is_owner) {
10822: $allowed = 1;
10823: }
10824: }
10825: }
10826: unless ($allowed) { return 'refused'; }
1.197 www 10827: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 10828: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10829: ' in domain '.$env{'request.role.domain'});
1.169 harris41 10830: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10831: &escape($upass),$uhome);
1.1172.2.134 raeburn 10832: my $ip = &get_requestor_ip();
1.620 albertel 10833: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 10834: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1172.2.134 raeburn 10835: '(Remote '.$ip.'): '.$reply);
1.197 www 10836: &log($udom,,$uname,$uhome,
1.620 albertel 10837: 'Authentication changed by '.$env{'user.domain'}.', '.
10838: $env{'user.name'}.', '.$umode.
1.1172.2.134 raeburn 10839: '(Remote '.$ip.'): '.$reply);
1.169 harris41 10840: unless ($reply eq 'ok') {
1.197 www 10841: &logthis('Authentication mode error: '.$reply);
1.169 harris41 10842: return 'error: '.$reply;
10843: }
1.170 harris41 10844: return 'ok';
1.80 www 10845: }
10846:
1.81 www 10847: # --------------------------------------------------------------- Modify a user
1.80 www 10848:
1.81 www 10849: sub modifyuser {
1.206 matthew 10850: my ($udom, $uname, $uid,
10851: $umode, $upass, $first,
10852: $middle, $last, $gene,
1.1058 raeburn 10853: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 10854: $udom= &LONCAPA::clean_domain($udom);
10855: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 10856: my $showcandelete = 'none';
10857: if (ref($candelete) eq 'ARRAY') {
10858: if (@{$candelete} > 0) {
10859: $showcandelete = join(', ',@{$candelete});
10860: }
10861: }
1.81 www 10862: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 10863: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 10864: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 10865: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10866: ' desiredhome not specified').
1.620 albertel 10867: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10868: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 10869: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 10870: my $newuser;
10871: if ($uhome eq 'no_host') {
10872: $newuser = 1;
1.1172.2.146. .13(raeb 10873:-23): unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10874:-23): ($umode eq 'lti')) {
10875:-23): return 'error: more information needed to create new user';
10876:-23): }
1.1075 raeburn 10877: }
1.80 www 10878: # ----------------------------------------------------------------- Create User
1.406 albertel 10879: if (($uhome eq 'no_host') &&
1.1172.2.146. .13(raeb 10880:-23): (($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80 www 10881: my $unhome='';
1.844 albertel 10882: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 10883: $unhome = $desiredhome;
1.620 albertel 10884: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10885: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 10886: } else { # load balancing routine for determining $unhome
1.81 www 10887: my $loadm=10000000;
1.841 albertel 10888: my %servers = &get_servers($udom,'library');
10889: foreach my $tryserver (keys(%servers)) {
10890: my $answer=reply('load',$tryserver);
10891: if (($answer=~/\d+/) && ($answer<$loadm)) {
10892: $loadm=$answer;
10893: $unhome=$tryserver;
10894: }
1.80 www 10895: }
10896: }
10897: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 10898: return 'error: unable to find a home server for '.$uname.
10899: ' in domain '.$udom;
1.80 www 10900: }
10901: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10902: &escape($upass),$unhome);
10903: unless ($reply eq 'ok') {
10904: return 'error: '.$reply;
10905: }
1.230 stredwic 10906: $uhome=&homeserver($uname,$udom,'true');
1.80 www 10907: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 10908: return 'error: unable verify users home machine.';
1.80 www 10909: }
1.209 matthew 10910: } # End of creation of new user
1.80 www 10911: # ---------------------------------------------------------------------- Add ID
10912: if ($uid) {
10913: $uid=~tr/A-Z/a-z/;
10914: my %uidhash=&idrget($udom,$uname);
1.196 www 10915: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
10916: && (!$forceid)) {
1.80 www 10917: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 10918: return 'error: user id "'.$uid.'" does not match '.
10919: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 10920: }
10921: } else {
10922: &idput($udom,($uname => $uid));
10923: }
10924: }
10925: # -------------------------------------------------------------- Add names, etc
1.313 matthew 10926: my @tmp=&get('environment',
1.899 raeburn 10927: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 10928: 'permanentemail','inststatus'],
1.134 albertel 10929: $udom,$uname);
1.1075 raeburn 10930: my (%names,%oldnames);
1.313 matthew 10931: if ($tmp[0] =~ m/^error:.*/) {
10932: %names=();
10933: } else {
10934: %names = @tmp;
1.1075 raeburn 10935: %oldnames = %names;
1.313 matthew 10936: }
1.388 www 10937: #
1.1058 raeburn 10938: # If name, email and/or uid are blank (e.g., because an uploaded file
10939: # of users did not contain them), do not overwrite existing values
10940: # unless field is in $candelete array ref.
10941: #
10942:
10943: my @fields = ('firstname','middlename','lastname','generation',
10944: 'permanentemail','id');
10945: my %newvalues;
10946: if (ref($candelete) eq 'ARRAY') {
10947: foreach my $field (@fields) {
10948: if (grep(/^\Q$field\E$/,@{$candelete})) {
10949: if ($field eq 'firstname') {
10950: $names{$field} = $first;
10951: } elsif ($field eq 'middlename') {
10952: $names{$field} = $middle;
10953: } elsif ($field eq 'lastname') {
10954: $names{$field} = $last;
10955: } elsif ($field eq 'generation') {
10956: $names{$field} = $gene;
10957: } elsif ($field eq 'permanentemail') {
10958: $names{$field} = $email;
10959: } elsif ($field eq 'id') {
10960: $names{$field} = $uid;
10961: }
10962: }
10963: }
10964: }
1.388 www 10965: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 10966: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 10967: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 10968: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 10969: if ($email) {
10970: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 10971: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 10972: }
1.899 raeburn 10973: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 10974: if (defined($inststatus)) {
10975: $names{'inststatus'} = '';
10976: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10977: if (ref($usertypes) eq 'HASH') {
10978: my @okstatuses;
10979: foreach my $item (split(/:/,$inststatus)) {
10980: if (defined($usertypes->{$item})) {
10981: push(@okstatuses,$item);
10982: }
10983: }
10984: if (@okstatuses) {
10985: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10986: }
10987: }
10988: }
1.1075 raeburn 10989: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 10990: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 10991: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 10992: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10993: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10994: } else {
10995: $logmsg .= ' during self creation';
10996: }
1.1075 raeburn 10997: my $changed;
10998: if ($newuser) {
10999: $changed = 1;
11000: } else {
11001: foreach my $field (@fields) {
11002: if ($names{$field} ne $oldnames{$field}) {
11003: $changed = 1;
11004: last;
11005: }
11006: }
11007: }
11008: unless ($changed) {
11009: $logmsg = 'No changes in user information needed for: '.$logmsg;
11010: &logthis($logmsg);
11011: return 'ok';
11012: }
11013: my $reply = &put('environment', \%names, $udom,$uname);
11014: if ($reply ne 'ok') {
11015: return 'error: '.$reply;
11016: }
1.1087 raeburn 11017: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
11018: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
11019: }
1.1075 raeburn 11020: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
11021: &devalidate_cache_new('namescache',$uname.':'.$udom);
11022: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 11023: &logthis($logmsg);
1.134 albertel 11024: return 'ok';
1.80 www 11025: }
11026:
1.81 www 11027: # -------------------------------------------------------------- Modify student
1.80 www 11028:
1.81 www 11029: sub modifystudent {
11030: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 11031: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1172.2.76 raeburn 11032: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 11033: if (!$cid) {
1.620 albertel 11034: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11035: return 'not_in_class';
11036: }
1.80 www 11037: }
11038: # --------------------------------------------------------------- Make the user
1.81 www 11039: my $reply=&modifyuser
1.209 matthew 11040: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 11041: $desiredhome,$email,$inststatus);
1.80 www 11042: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 11043: # This will cause &modify_student_enrollment to get the uid from the
1.1172.2.31 raeburn 11044: # student's environment
1.297 matthew 11045: $uid = undef if (!$forceid);
1.455 albertel 11046: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1172.2.19 raeburn 11047: $gene,$usec,$end,$start,$type,$locktype,
1.1172.2.76 raeburn 11048: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 11049: return $reply;
11050: }
11051:
11052: sub modify_student_enrollment {
1.1172.2.23 raeburn 11053: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1172.2.76 raeburn 11054: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 11055: my ($cdom,$cnum,$chome);
11056: if (!$cid) {
1.620 albertel 11057: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11058: return 'not_in_class';
11059: }
1.620 albertel 11060: $cdom=$env{'course.'.$cid.'.domain'};
11061: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 11062: } else {
11063: ($cdom,$cnum)=split(/_/,$cid);
11064: }
1.620 albertel 11065: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 11066: if (!$chome) {
1.457 raeburn 11067: $chome=&homeserver($cnum,$cdom);
1.297 matthew 11068: }
1.455 albertel 11069: if (!$chome) { return 'unknown_course'; }
1.297 matthew 11070: # Make sure the user exists
1.81 www 11071: my $uhome=&homeserver($uname,$udom);
11072: if (($uhome eq '') || ($uhome eq 'no_host')) {
11073: return 'error: no such user';
11074: }
1.297 matthew 11075: # Get student data if we were not given enough information
11076: if (!defined($first) || $first eq '' ||
11077: !defined($last) || $last eq '' ||
11078: !defined($uid) || $uid eq '' ||
11079: !defined($middle) || $middle eq '' ||
11080: !defined($gene) || $gene eq '') {
1.294 matthew 11081: # They did not supply us with enough data to enroll the student, so
11082: # we need to pick up more information.
1.297 matthew 11083: my %tmp = &get('environment',
1.294 matthew 11084: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 11085: ,$udom,$uname);
11086:
1.800 albertel 11087: #foreach my $key (keys(%tmp)) {
11088: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 11089: #}
1.294 matthew 11090: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
11091: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11092: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 11093: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 11094: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
11095: }
1.556 albertel 11096: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 11097: my $user = "$uname:$udom";
11098: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 11099: my $reply=cput('classlist',
1.1148 raeburn 11100: {$user =>
1.1172.2.76 raeburn 11101: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 11102: $cdom,$cnum);
1.1148 raeburn 11103: if (($reply eq 'ok') || ($reply eq 'delayed')) {
11104: &devalidate_getsection_cache($udom,$uname,$cid);
11105: } else {
1.81 www 11106: return 'error: '.$reply;
11107: }
1.297 matthew 11108: # Add student role to user
1.83 www 11109: my $uurl='/'.$cid;
1.81 www 11110: $uurl=~s/\_/\//g;
11111: if ($usec) {
11112: $uurl.='/'.$usec;
11113: }
1.1148 raeburn 11114: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11115: $selfenroll,$context);
11116: if ($result ne 'ok') {
11117: if ($old_entry{$user} ne '') {
11118: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11119: } else {
11120: $reply = &del('classlist',[$user],$cdom,$cnum);
11121: }
11122: }
11123: return $result;
1.21 www 11124: }
11125:
1.556 albertel 11126: sub format_name {
11127: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11128: my $name;
11129: if ($first ne 'lastname') {
11130: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11131: } else {
11132: if ($lastname=~/\S/) {
11133: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11134: $name=~s/\s+,/,/;
11135: } else {
11136: $name.= $firstname.' '.$middlename.' '.$generation;
11137: }
11138: }
11139: $name=~s/^\s+//;
11140: $name=~s/\s+$//;
11141: $name=~s/\s+/ /g;
11142: return $name;
11143: }
11144:
1.84 www 11145: # ------------------------------------------------- Write to course preferences
11146:
11147: sub writecoursepref {
11148: my ($courseid,%prefs)=@_;
11149: $courseid=~s/^\///;
11150: $courseid=~s/\_/\//g;
11151: my ($cdomain,$cnum)=split(/\//,$courseid);
11152: my $chome=homeserver($cnum,$cdomain);
11153: if (($chome eq '') || ($chome eq 'no_host')) {
11154: return 'error: no such course';
11155: }
11156: my $cstring='';
1.800 albertel 11157: foreach my $pref (keys(%prefs)) {
11158: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 11159: }
1.84 www 11160: $cstring=~s/\&$//;
11161: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11162: }
11163:
11164: # ---------------------------------------------------------- Make/modify course
11165:
11166: sub createcourse {
1.741 raeburn 11167: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1172.2.146. .1(raebu 11168:22): $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84 www 11169: $url=&declutter($url);
11170: my $cid='';
1.1028 raeburn 11171: if ($context eq 'requestcourses') {
11172: my $can_create = 0;
11173: my ($ownername,$ownerdom) = split(':',$course_owner);
11174: if ($udom eq $ownerdom) {
1.1172.2.146. .1(raebu 11175:22): my $reload;
11176:22): if (($callercontext eq 'auto') &&
11177:22): ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11178:22): $reload = 'reload';
11179:22): }
11180:22): if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028 raeburn 11181: $context)) {
11182: $can_create = 1;
11183: }
11184: } else {
11185: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11186: $category);
11187: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11188: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11189: if (@curr > 0) {
11190: my @options = qw(approval validate autolimit);
11191: my $optregex = join('|',@options);
11192: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11193: $can_create = 1;
11194: }
11195: }
11196: }
11197: }
11198: if ($can_create) {
11199: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11200: unless (&allowed('ccc',$udom)) {
11201: return 'refused';
11202: }
1.1017 raeburn 11203: }
11204: } else {
11205: return 'refused';
11206: }
1.1028 raeburn 11207: } elsif (!&allowed('ccc',$udom)) {
11208: return 'refused';
1.84 www 11209: }
1.1011 raeburn 11210: # --------------------------------------------------------------- Get Unique ID
11211: my $uname;
11212: if ($cnum =~ /^$match_courseid$/) {
11213: my $chome=&homeserver($cnum,$udom,'true');
11214: if (($chome eq '') || ($chome eq 'no_host')) {
11215: $uname = $cnum;
11216: } else {
1.1038 raeburn 11217: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11218: }
11219: } else {
1.1038 raeburn 11220: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11221: }
11222: return $uname if ($uname =~ /^error/);
11223: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 11224: if (!defined($course_server)) {
11225: if (defined(&domain($udom,'primary'))) {
11226: $course_server = &domain($udom,'primary');
11227: } else {
11228: $course_server = $env{'user.home'};
11229: }
11230: }
11231: my %host_servers =
11232: &Apache::lonnet::get_servers($udom,'library');
11233: unless ($host_servers{$course_server}) {
11234: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 11235: }
1.84 www 11236: # ------------------------------------------------------------- Make the course
11237: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 11238: $course_server);
1.84 www 11239: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 11240: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 11241: if (($uhome eq '') || ($uhome eq 'no_host')) {
11242: return 'error: no such course';
11243: }
1.271 www 11244: # ----------------------------------------------------------------- Course made
1.516 raeburn 11245: # log existence
1.1029 raeburn 11246: my $now = time;
1.918 raeburn 11247: my $newcourse = {
11248: $udom.'_'.$uname => {
1.921 raeburn 11249: description => $description,
11250: inst_code => $inst_code,
11251: owner => $course_owner,
11252: type => $crstype,
1.1029 raeburn 11253: creator => $env{'user.name'}.':'.
11254: $env{'user.domain'},
11255: created => $now,
11256: context => $context,
1.918 raeburn 11257: },
11258: };
1.921 raeburn 11259: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 11260: # set toplevel url
1.271 www 11261: my $topurl=$url;
11262: unless ($nonstandard) {
11263: # ------------------------------------------ For standard courses, make top url
11264: my $mapurl=&clutter($url);
1.278 www 11265: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 11266: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 11267: <map>
11268: <resource id="1" type="start"></resource>
11269: <resource id="2" src="$mapurl"></resource>
11270: <resource id="3" type="finish"></resource>
11271: <link index="1" from="1" to="2"></link>
11272: <link index="2" from="2" to="3"></link>
11273: </map>
11274: ENDINITMAP
11275: $topurl=&declutter(
1.638 albertel 11276: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 11277: );
11278: }
11279: # ----------------------------------------------------------- Write preferences
1.84 www 11280: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 11281: ('description' => $description,
11282: 'url' => $topurl,
11283: 'internal.creator' => $env{'user.name'}.':'.
11284: $env{'user.domain'},
11285: 'internal.created' => $now,
11286: 'internal.creationcontext' => $context)
11287: );
1.84 www 11288: return '/'.$udom.'/'.$uname;
11289: }
11290:
1.1011 raeburn 11291: # ------------------------------------------------------------------- Create ID
11292: sub generate_coursenum {
1.1038 raeburn 11293: my ($udom,$crstype) = @_;
1.1011 raeburn 11294: my $domdesc = &domain($udom);
11295: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 11296: my $first;
11297: if ($crstype eq 'Community') {
11298: $first = '0';
11299: } else {
11300: $first = int(1+rand(9));
11301: }
11302: my $uname=$first.
1.1011 raeburn 11303: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11304: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11305: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11306: # ----------------------------------------------- Make sure that does not exist
11307: my $uhome=&homeserver($uname,$udom,'true');
11308: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 11309: if ($crstype eq 'Community') {
11310: $first = '0';
11311: } else {
11312: $first = int(1+rand(9));
11313: }
11314: $uname=$first.
1.1011 raeburn 11315: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11316: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11317: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11318: $uhome=&homeserver($uname,$udom,'true');
11319: unless (($uhome eq '') || ($uhome eq 'no_host')) {
11320: return 'error: unable to generate unique course-ID';
11321: }
11322: }
11323: return $uname;
11324: }
11325:
1.813 albertel 11326: sub is_course {
1.1167 droeschl 11327: my ($cdom, $cnum) = scalar(@_) == 1 ?
11328: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
1.1172.2.95 raeburn 11329: return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11330: my $uhome=&homeserver($cnum,$cdom);
11331: my $iscourse;
11332: if (grep { $_ eq $uhome } current_machine_ids()) {
11333: $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11334: } else {
11335: my $hashid = $cdom.':'.$cnum;
11336: ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11337: unless (defined($cached)) {
11338: my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11339: $cnum,undef,undef,'.');
11340: $iscourse = 0;
11341: if (exists($courses{$cdom.'_'.$cnum})) {
11342: $iscourse = 1;
11343: }
11344: &do_cache_new('iscourse',$hashid,$iscourse,3600);
11345: }
11346: }
11347: return unless($iscourse);
1.1167 droeschl 11348: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 11349: }
11350:
1.1015 raeburn 11351: sub store_userdata {
1.1172.2.146. .27(raeb 11352:-25): my ($storehash,$datakey,$namespace,$udom,$uname,$ip) = @_;
1.1013 raeburn 11353: my $result;
1.1016 raeburn 11354: if ($datakey ne '') {
1.1013 raeburn 11355: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 11356: if ($udom eq '' || $uname eq '') {
11357: $udom = $env{'user.domain'};
11358: $uname = $env{'user.name'};
11359: }
11360: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 11361: if (($uhome eq '') || ($uhome eq 'no_host')) {
11362: $result = 'error: no_host';
11363: } else {
1.1172.2.146. .27(raeb 11364:-25): if ($ip ne '') {
11365:-25): $storehash->{'ip'} = $ip;
11366:-25): } else {
11367:-25): $storehash->{'ip'} = &get_requestor_ip();
11368:-25): }
1.1013 raeburn 11369: $storehash->{'host'} = $perlvar{'lonHostID'};
11370:
11371: my $namevalue='';
11372: foreach my $key (keys(%{$storehash})) {
11373: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11374: }
11375: $namevalue=~s/\&$//;
1.1172.2.26 raeburn 11376: unless ($namespace eq 'courserequests') {
11377: $datakey = &escape($datakey);
11378: }
1.1105 raeburn 11379: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
11380: $namevalue,$uhome);
1.1013 raeburn 11381: }
11382: } else {
11383: $result = 'error: data to store was not a hash reference';
11384: }
11385: } else {
11386: $result= 'error: invalid requestkey';
11387: }
11388: return $result;
11389: }
11390:
1.21 www 11391: # ---------------------------------------------------------- Assign Custom Role
11392:
11393: sub assigncustomrole {
1.957 raeburn 11394: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11395: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 11396: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 11397: }
11398:
11399: # ----------------------------------------------------------------- Revoke Role
11400:
11401: sub revokerole {
1.957 raeburn 11402: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11403: my $now=time;
1.965 raeburn 11404: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 11405: }
11406:
11407: # ---------------------------------------------------------- Revoke Custom Role
11408:
11409: sub revokecustomrole {
1.957 raeburn 11410: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11411: my $now=time;
1.357 www 11412: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 11413: $deleteflag,$selfenroll,$context);
1.17 www 11414: }
11415:
1.533 banghart 11416: # ------------------------------------------------------------ Disk usage
1.535 albertel 11417: sub diskusage {
1.955 raeburn 11418: my ($udom,$uname,$directorypath,$getpropath)=@_;
11419: $directorypath =~ s/\/$//;
11420: my $listing=&reply('du2:'.&escape($directorypath).':'
11421: .&escape($getpropath).':'.&escape($uname).':'
11422: .&escape($udom),homeserver($uname,$udom));
11423: if ($listing eq 'unknown_cmd') {
11424: if ($getpropath) {
11425: $directorypath = &propath($udom,$uname).'/'.$directorypath;
11426: }
11427: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11428: }
1.514 albertel 11429: return $listing;
1.512 banghart 11430: }
11431:
1.566 banghart 11432: sub is_locked {
1.1096 raeburn 11433: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 11434: my @check;
11435: my $is_locked;
1.1093 raeburn 11436: push (@check,$file_name);
1.613 albertel 11437: my %locked = &get('file_permissions',\@check,
1.620 albertel 11438: $env{'user.domain'},$env{'user.name'});
1.615 albertel 11439: my ($tmp)=keys(%locked);
11440: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 11441:
1.566 banghart 11442: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 11443: $is_locked = 'false';
11444: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 11445: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 11446: $is_locked = 'true';
1.1096 raeburn 11447: if (ref($which) eq 'ARRAY') {
11448: push(@{$which},$entry);
11449: } else {
11450: last;
11451: }
1.745 raeburn 11452: }
11453: }
1.566 banghart 11454: } else {
11455: $is_locked = 'false';
11456: }
1.1093 raeburn 11457: return $is_locked;
1.566 banghart 11458: }
11459:
1.759 albertel 11460: sub declutter_portfile {
11461: my ($file) = @_;
1.833 albertel 11462: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 11463: return $file;
11464: }
11465:
1.559 banghart 11466: # ------------------------------------------------------------- Mark as Read Only
11467:
11468: sub mark_as_readonly {
11469: my ($domain,$user,$files,$what) = @_;
1.613 albertel 11470: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11471: my ($tmp)=keys(%current_permissions);
11472: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 11473: foreach my $file (@{$files}) {
1.759 albertel 11474: $file = &declutter_portfile($file);
1.561 banghart 11475: push(@{$current_permissions{$file}},$what);
1.559 banghart 11476: }
1.613 albertel 11477: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11478: return;
11479: }
11480:
1.572 banghart 11481: # ------------------------------------------------------------Save Selected Files
11482:
11483: sub save_selected_files {
11484: my ($user, $path, @files) = @_;
11485: my $filename = $user."savedfiles";
1.573 banghart 11486: my @other_files = &files_not_in_path($user, $path);
1.1172.2.96 raeburn 11487: open (OUT,'>',LONCAPA::tempdir().$filename);
1.573 banghart 11488: foreach my $file (@files) {
1.620 albertel 11489: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 11490: }
11491: foreach my $file (@other_files) {
1.574 banghart 11492: print (OUT $file."\n");
1.572 banghart 11493: }
1.574 banghart 11494: close (OUT);
1.572 banghart 11495: return 'ok';
11496: }
11497:
1.574 banghart 11498: sub clear_selected_files {
11499: my ($user) = @_;
11500: my $filename = $user."savedfiles";
1.1172.2.96 raeburn 11501: open (OUT,'>',LONCAPA::tempdir().$filename);
1.574 banghart 11502: print (OUT undef);
11503: close (OUT);
11504: return ("ok");
11505: }
11506:
1.572 banghart 11507: sub files_in_path {
11508: my ($user, $path) = @_;
11509: my $filename = $user."savedfiles";
11510: my %return_files;
1.1172.2.96 raeburn 11511: open (IN,'<',LONCAPA::tempdir().$filename);
1.573 banghart 11512: while (my $line_in = <IN>) {
1.574 banghart 11513: chomp ($line_in);
11514: my @paths_and_file = split (m!/!, $line_in);
11515: my $file_part = pop (@paths_and_file);
11516: my $path_part = join ('/', @paths_and_file);
1.573 banghart 11517: $path_part.='/';
11518: my $path_and_file = $path_part.$file_part;
11519: if ($path_part eq $path) {
11520: $return_files{$file_part}= 'selected';
11521: }
11522: }
1.574 banghart 11523: close (IN);
11524: return (\%return_files);
1.572 banghart 11525: }
11526:
11527: # called in portfolio select mode, to show files selected NOT in current directory
11528: sub files_not_in_path {
11529: my ($user, $path) = @_;
11530: my $filename = $user."savedfiles";
11531: my @return_files;
11532: my $path_part;
1.1172.2.96 raeburn 11533: open(IN, '<',LONCAPA::tempdir().$filename);
1.800 albertel 11534: while (my $line = <IN>) {
1.572 banghart 11535: #ok, I know it's clunky, but I want it to work
1.800 albertel 11536: my @paths_and_file = split(m|/|, $line);
11537: my $file_part = pop(@paths_and_file);
11538: chomp($file_part);
11539: my $path_part = join('/', @paths_and_file);
1.572 banghart 11540: $path_part .= '/';
11541: my $path_and_file = $path_part.$file_part;
11542: if ($path_part ne $path) {
1.800 albertel 11543: push(@return_files, ($path_and_file));
1.572 banghart 11544: }
11545: }
1.800 albertel 11546: close(OUT);
1.574 banghart 11547: return (@return_files);
1.572 banghart 11548: }
11549:
1.745 raeburn 11550: #----------------------------------------------Get portfolio file permissions
1.629 banghart 11551:
1.745 raeburn 11552: sub get_portfile_permissions {
11553: my ($domain,$user) = @_;
1.613 albertel 11554: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11555: my ($tmp)=keys(%current_permissions);
11556: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11557: return \%current_permissions;
11558: }
11559:
11560: #---------------------------------------------Get portfolio file access controls
11561:
1.749 raeburn 11562: sub get_access_controls {
1.745 raeburn 11563: my ($current_permissions,$group,$file) = @_;
1.769 albertel 11564: my %access;
11565: my $real_file = $file;
11566: $file =~ s/\.meta$//;
1.745 raeburn 11567: if (defined($file)) {
1.749 raeburn 11568: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11569: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 11570: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 11571: }
11572: }
1.745 raeburn 11573: } else {
1.749 raeburn 11574: foreach my $key (keys(%{$current_permissions})) {
11575: if ($key =~ /\0accesscontrol$/) {
11576: if (defined($group)) {
11577: if ($key !~ m-^\Q$group\E/-) {
11578: next;
11579: }
11580: }
11581: my ($fullpath) = split(/\0/,$key);
11582: if (ref($$current_permissions{$key}) eq 'HASH') {
11583: foreach my $control (keys(%{$$current_permissions{$key}})) {
11584: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11585: }
11586: }
11587: }
11588: }
11589: }
11590: return %access;
11591: }
11592:
11593: sub modify_access_controls {
11594: my ($file_name,$changes,$domain,$user)=@_;
11595: my ($outcome,$deloutcome);
11596: my %store_permissions;
11597: my %new_values;
11598: my %new_control;
11599: my %translation;
11600: my @deletions = ();
11601: my $now = time;
11602: if (exists($$changes{'activate'})) {
11603: if (ref($$changes{'activate'}) eq 'HASH') {
11604: my @newitems = sort(keys(%{$$changes{'activate'}}));
11605: my $numnew = scalar(@newitems);
11606: for (my $i=0; $i<$numnew; $i++) {
11607: my $newkey = $newitems[$i];
11608: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 11609: if ($newkey =~ /^\d+:/) {
11610: $newkey =~ s/^(\d+)/$newid/;
11611: $translation{$1} = $newid;
11612: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11613: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11614: $translation{$1} = $newid;
11615: }
1.749 raeburn 11616: $new_values{$file_name."\0".$newkey} =
11617: $$changes{'activate'}{$newitems[$i]};
11618: $new_control{$newkey} = $now;
11619: }
11620: }
11621: }
11622: my %todelete;
11623: my %changed_items;
11624: foreach my $action ('delete','update') {
11625: if (exists($$changes{$action})) {
11626: if (ref($$changes{$action}) eq 'HASH') {
11627: foreach my $key (keys(%{$$changes{$action}})) {
11628: my ($itemnum) = ($key =~ /^([^:]+):/);
11629: if ($action eq 'delete') {
11630: $todelete{$itemnum} = 1;
11631: } else {
11632: $changed_items{$itemnum} = $key;
11633: }
11634: }
1.745 raeburn 11635: }
11636: }
1.749 raeburn 11637: }
11638: # get lock on access controls for file.
11639: my $lockhash = {
11640: $file_name."\0".'locked_access_records' => $env{'user.name'}.
11641: ':'.$env{'user.domain'},
11642: };
11643: my $tries = 0;
11644: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11645:
1.1172.2.79 raeburn 11646: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 11647: $tries ++;
1.1172.2.79 raeburn 11648: sleep(0.1);
1.749 raeburn 11649: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11650: }
11651: if ($gotlock eq 'ok') {
11652: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11653: my ($tmp)=keys(%curr_permissions);
11654: if ($tmp=~/^error:/) { undef(%curr_permissions); }
11655: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11656: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11657: if (ref($curr_controls) eq 'HASH') {
11658: foreach my $control_item (keys(%{$curr_controls})) {
11659: my ($itemnum) = ($control_item =~ /^([^:]+):/);
11660: if (defined($todelete{$itemnum})) {
11661: push(@deletions,$file_name."\0".$control_item);
11662: } else {
11663: if (defined($changed_items{$itemnum})) {
11664: $new_control{$changed_items{$itemnum}} = $now;
11665: push(@deletions,$file_name."\0".$control_item);
11666: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11667: } else {
11668: $new_control{$control_item} = $$curr_controls{$control_item};
11669: }
11670: }
1.745 raeburn 11671: }
11672: }
11673: }
1.970 raeburn 11674: my ($group);
11675: if (&is_course($domain,$user)) {
11676: ($group,my $file) = split(/\//,$file_name,2);
11677: }
1.749 raeburn 11678: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11679: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11680: $outcome = &put('file_permissions',\%new_values,$domain,$user);
11681: # remove lock
11682: my @del_lock = ($file_name."\0".'locked_access_records');
11683: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 11684: my $sqlresult =
1.970 raeburn 11685: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 11686: $group);
1.749 raeburn 11687: } else {
11688: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 11689: }
1.749 raeburn 11690: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 11691: }
11692:
1.827 raeburn 11693: sub make_public_indefinitely {
11694: my ($requrl) = @_;
11695: my $now = time;
11696: my $action = 'activate';
11697: my $aclnum = 0;
11698: if (&is_portfolio_url($requrl)) {
11699: my (undef,$udom,$unum,$file_name,$group) =
11700: &parse_portfolio_url($requrl);
11701: my $current_perms = &get_portfile_permissions($udom,$unum);
11702: my %access_controls = &get_access_controls($current_perms,
11703: $group,$file_name);
11704: foreach my $key (keys(%{$access_controls{$file_name}})) {
11705: my ($num,$scope,$end,$start) =
11706: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11707: if ($scope eq 'public') {
11708: if ($start <= $now && $end == 0) {
11709: $action = 'none';
11710: } else {
11711: $action = 'update';
11712: $aclnum = $num;
11713: }
11714: last;
11715: }
11716: }
11717: if ($action eq 'none') {
11718: return 'ok';
11719: } else {
11720: my %changes;
11721: my $newend = 0;
11722: my $newstart = $now;
11723: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
11724: $changes{$action}{$newkey} = {
11725: type => 'public',
11726: time => {
11727: start => $newstart,
11728: end => $newend,
11729: },
11730: };
11731: my ($outcome,$deloutcome,$new_values,$translation) =
11732: &modify_access_controls($file_name,\%changes,$udom,$unum);
11733: return $outcome;
11734: }
11735: } else {
11736: return 'invalid';
11737: }
11738: }
11739:
1.745 raeburn 11740: #------------------------------------------------------Get Marked as Read Only
11741:
11742: sub get_marked_as_readonly {
11743: my ($domain,$user,$what,$group) = @_;
11744: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 11745: my @readonly_files;
1.629 banghart 11746: my $cmp1=$what;
11747: if (ref($what)) { $cmp1=join('',@{$what}) };
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.561 banghart 11754: if (ref($value) eq "ARRAY"){
11755: foreach my $stored_what (@{$value}) {
1.629 banghart 11756: my $cmp2=$stored_what;
1.759 albertel 11757: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 11758: $cmp2=join('',@{$stored_what});
1.745 raeburn 11759: }
1.629 banghart 11760: if ($cmp1 eq $cmp2) {
1.561 banghart 11761: push(@readonly_files, $file_name);
1.745 raeburn 11762: last;
1.563 banghart 11763: } elsif (!defined($what)) {
11764: push(@readonly_files, $file_name);
1.745 raeburn 11765: last;
1.561 banghart 11766: }
11767: }
1.745 raeburn 11768: }
1.561 banghart 11769: }
11770: return @readonly_files;
11771: }
1.577 banghart 11772: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 11773:
1.577 banghart 11774: sub get_marked_as_readonly_hash {
1.745 raeburn 11775: my ($current_permissions,$group,$what) = @_;
1.577 banghart 11776: my %readonly_files;
1.745 raeburn 11777: while (my ($file_name,$value) = each(%{$current_permissions})) {
11778: if (defined($group)) {
11779: if ($file_name !~ m-^\Q$group\E/-) {
11780: next;
11781: }
11782: }
1.577 banghart 11783: if (ref($value) eq "ARRAY"){
11784: foreach my $stored_what (@{$value}) {
1.745 raeburn 11785: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 11786: foreach my $lock_descriptor(@{$stored_what}) {
11787: if ($lock_descriptor eq 'graded') {
11788: $readonly_files{$file_name} = 'graded';
11789: } elsif ($lock_descriptor eq 'handback') {
11790: $readonly_files{$file_name} = 'handback';
11791: } else {
11792: if (!exists($readonly_files{$file_name})) {
11793: $readonly_files{$file_name} = 'locked';
11794: }
11795: }
1.745 raeburn 11796: }
1.750 banghart 11797: }
1.577 banghart 11798: }
11799: }
11800: }
11801: return %readonly_files;
11802: }
1.559 banghart 11803: # ------------------------------------------------------------ Unmark as Read Only
11804:
11805: sub unmark_as_readonly {
1.629 banghart 11806: # unmarks $file_name (if $file_name is defined), or all files locked by $what
11807: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 11808: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 11809: $file_name = &declutter_portfile($file_name);
1.634 albertel 11810: my $symb_crs = $what;
11811: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 11812: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 11813: my ($tmp)=keys(%current_permissions);
11814: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11815: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 11816: foreach my $file (@readonly_files) {
1.759 albertel 11817: my $clean_file = &declutter_portfile($file);
11818: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 11819: my $current_locks = $current_permissions{$file};
1.563 banghart 11820: my @new_locks;
11821: my @del_keys;
11822: if (ref($current_locks) eq "ARRAY"){
11823: foreach my $locker (@{$current_locks}) {
1.632 albertel 11824: my $compare=$locker;
1.749 raeburn 11825: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 11826: $compare=join('',@{$locker});
1.746 raeburn 11827: if ($compare ne $symb_crs) {
11828: push(@new_locks, $locker);
11829: }
1.563 banghart 11830: }
11831: }
1.650 albertel 11832: if (scalar(@new_locks) > 0) {
1.563 banghart 11833: $current_permissions{$file} = \@new_locks;
11834: } else {
11835: push(@del_keys, $file);
1.613 albertel 11836: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 11837: delete($current_permissions{$file});
1.563 banghart 11838: }
11839: }
1.561 banghart 11840: }
1.613 albertel 11841: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11842: return;
11843: }
1.512 banghart 11844:
1.17 www 11845: # ------------------------------------------------------------ Directory lister
11846:
11847: sub dirlist {
1.955 raeburn 11848: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 11849: $uri=~s/^\///;
11850: $uri=~s/\/$//;
1.253 stredwic 11851: my ($udom, $uname);
1.955 raeburn 11852: if ($getuserdir) {
1.253 stredwic 11853: $udom = $userdomain;
11854: $uname = $username;
1.955 raeburn 11855: } else {
11856: (undef,$udom,$uname)=split(/\//,$uri);
11857: if(defined($userdomain)) {
11858: $udom = $userdomain;
11859: }
11860: if(defined($username)) {
11861: $uname = $username;
11862: }
1.253 stredwic 11863: }
1.955 raeburn 11864: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 11865:
1.955 raeburn 11866: $dirRoot = $perlvar{'lonDocRoot'};
11867: if (defined($getpropath)) {
11868: $dirRoot = &propath($udom,$uname);
1.253 stredwic 11869: $dirRoot =~ s/\/$//;
1.955 raeburn 11870: } elsif (defined($getuserdir)) {
11871: my $subdir=$uname.'__';
11872: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11873: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11874: ."/$udom/$subdir/$uname";
11875: } elsif (defined($alternateRoot)) {
11876: $dirRoot = $alternateRoot;
1.751 banghart 11877: }
1.253 stredwic 11878:
11879: if($udom) {
11880: if($uname) {
1.1135 raeburn 11881: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 11882: if ($uhome eq 'no_host') {
11883: return ([],'no_host');
11884: }
1.955 raeburn 11885: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 11886: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 11887: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 11888: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11889: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 11890: } else {
11891: @listing_results = map { &unescape($_); } split(/:/,$listing);
11892: }
1.605 matthew 11893: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11894: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 11895: @listing_results = split(/:/,$listing);
11896: } else {
11897: @listing_results = map { &unescape($_); } split(/:/,$listing);
11898: }
1.1135 raeburn 11899: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 11900: ($listing eq 'rejected') || ($listing eq 'refused') ||
11901: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11902: return ([],$listing);
11903: } else {
11904: return (\@listing_results);
1.1135 raeburn 11905: }
1.955 raeburn 11906: } elsif(!$alternateRoot) {
1.1136 raeburn 11907: my (%allusers,%listerror);
1.841 albertel 11908: my %servers = &get_servers($udom,'library');
1.955 raeburn 11909: foreach my $tryserver (keys(%servers)) {
11910: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11911: &escape($udom),$tryserver);
11912: if ($listing eq 'unknown_cmd') {
11913: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11914: $udom, $tryserver);
11915: } else {
11916: @listing_results = map { &unescape($_); } split(/:/,$listing);
11917: }
1.841 albertel 11918: if ($listing eq 'unknown_cmd') {
11919: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11920: $udom, $tryserver);
11921: @listing_results = split(/:/,$listing);
11922: } else {
11923: @listing_results =
11924: map { &unescape($_); } split(/:/,$listing);
11925: }
1.1136 raeburn 11926: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11927: ($listing eq 'rejected') || ($listing eq 'refused') ||
11928: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11929: $listerror{$tryserver} = $listing;
11930: } else {
1.841 albertel 11931: foreach my $line (@listing_results) {
11932: my ($entry) = split(/&/,$line,2);
11933: $allusers{$entry} = 1;
11934: }
11935: }
1.253 stredwic 11936: }
1.1136 raeburn 11937: my @alluserslist=();
1.800 albertel 11938: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 11939: push(@alluserslist,$user.'&user');
1.253 stredwic 11940: }
1.1172.2.80 raeburn 11941: if (!%listerror) {
11942: # no errors
11943: return (\@alluserslist);
11944: } elsif (scalar(keys(%servers)) == 1) {
11945: # one library server, one error
11946: my ($key) = keys(%listerror);
11947: return (\@alluserslist, $listerror{$key});
11948: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11949: # con_lost indicates that we might miss data from at least one
11950: # library server
11951: return (\@alluserslist, 'con_lost');
11952: } else {
11953: # multiple library servers and no con_lost -> data should be
11954: # complete.
11955: return (\@alluserslist);
11956: }
11957:
1.253 stredwic 11958: } else {
1.1136 raeburn 11959: return ([],'missing username');
1.253 stredwic 11960: }
1.955 raeburn 11961: } elsif(!defined($getpropath)) {
1.1136 raeburn 11962: my $path = $perlvar{'lonDocRoot'}.'/res/';
11963: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11964: return (\@all_domains);
1.955 raeburn 11965: } else {
1.1136 raeburn 11966: return ([],'missing domain');
1.275 stredwic 11967: }
11968: }
11969:
11970: # --------------------------------------------- GetFileTimestamp
11971: # This function utilizes dirlist and returns the date stamp for
11972: # when it was last modified. It will also return an error of -1
11973: # if an error occurs
11974:
11975: sub GetFileTimestamp {
1.955 raeburn 11976: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 11977: $studentDomain = &LONCAPA::clean_domain($studentDomain);
11978: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 11979: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11980: undef,$getuserdir);
11981: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11982: return -1;
11983: }
11984: if (ref($fileref) eq 'ARRAY') {
11985: my @stats = split('&',$fileref->[0]);
1.375 matthew 11986: # @stats contains first the filename, then the stat output
11987: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 11988: } else {
11989: return -1;
1.253 stredwic 11990: }
1.26 www 11991: }
11992:
1.712 albertel 11993: sub stat_file {
11994: my ($uri) = @_;
1.787 albertel 11995: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 11996:
1.955 raeburn 11997: my ($udom,$uname,$file);
1.712 albertel 11998: if ($uri =~ m-^/(uploaded|editupload)/-) {
11999: ($udom,$uname,$file) =
1.811 albertel 12000: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 12001: $file = 'userfiles/'.$file;
12002: }
12003: if ($uri =~ m-^/res/-) {
12004: ($udom,$uname) =
1.807 albertel 12005: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 12006: $file = $uri;
12007: }
12008:
12009: if (!$udom || !$uname || !$file) {
12010: # unable to handle the uri
12011: return ();
12012: }
1.956 raeburn 12013: my $getpropath;
12014: if ($file =~ /^userfiles\//) {
12015: $getpropath = 1;
12016: }
1.1136 raeburn 12017: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
12018: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12019: return ();
12020: } else {
12021: if (ref($listref) eq 'ARRAY') {
12022: my @stats = split('&',$listref->[0]);
12023: shift(@stats); #filename is first
12024: return @stats;
12025: }
1.712 albertel 12026: }
12027: return ();
12028: }
12029:
1.26 www 12030: # -------------------------------------------------------- Value of a Condition
12031:
1.713 albertel 12032: # gets the value of a specific preevaluated condition
12033: # stored in the string $env{user.state.<cid>}
12034: # or looks up a condition reference in the bighash and if if hasn't
12035: # already been evaluated recurses into docondval to get the value of
12036: # the condition, then memoizing it to
12037: # $env{user.state.<cid>.<condition>}
1.40 www 12038: sub directcondval {
12039: my $number=shift;
1.620 albertel 12040: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 12041: &Apache::lonuserstate::evalstate();
12042: }
1.713 albertel 12043: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12044: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12045: } elsif ($number =~ /^_/) {
12046: my $sub_condition;
12047: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12048: &GDBM_READER(),0640)) {
12049: $sub_condition=$bighash{'conditions'.$number};
12050: untie(%bighash);
12051: }
12052: my $value = &docondval($sub_condition);
1.949 raeburn 12053: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 12054: return $value;
12055: }
1.620 albertel 12056: if ($env{'user.state.'.$env{'request.course.id'}}) {
12057: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 12058: } else {
12059: return 2;
12060: }
12061: }
12062:
1.713 albertel 12063: # get the collection of conditions for this resource
1.26 www 12064: sub condval {
12065: my $condidx=shift;
1.54 www 12066: my $allpathcond='';
1.713 albertel 12067: foreach my $cond (split(/\|/,$condidx)) {
12068: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12069: $allpathcond.=
12070: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12071: }
1.191 harris41 12072: }
1.54 www 12073: $allpathcond=~s/\|$//;
1.713 albertel 12074: return &docondval($allpathcond);
12075: }
12076:
12077: #evaluates an expression of conditions
12078: sub docondval {
12079: my ($allpathcond) = @_;
12080: my $result=0;
12081: if ($env{'request.course.id'}
12082: && defined($allpathcond)) {
12083: my $operand='|';
12084: my @stack;
12085: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12086: if ($chunk eq '(') {
12087: push @stack,($operand,$result);
12088: } elsif ($chunk eq ')') {
12089: my $before=pop @stack;
12090: if (pop @stack eq '&') {
12091: $result=$result>$before?$before:$result;
12092: } else {
12093: $result=$result>$before?$result:$before;
12094: }
12095: } elsif (($chunk eq '&') || ($chunk eq '|')) {
12096: $operand=$chunk;
12097: } else {
12098: my $new=directcondval($chunk);
12099: if ($operand eq '&') {
12100: $result=$result>$new?$new:$result;
12101: } else {
12102: $result=$result>$new?$result:$new;
12103: }
12104: }
12105: }
1.26 www 12106: }
12107: return $result;
1.421 albertel 12108: }
12109:
12110: # ---------------------------------------------------- Devalidate courseresdata
12111:
12112: sub devalidatecourseresdata {
12113: my ($coursenum,$coursedomain)=@_;
12114: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12115: &devalidate_cache_new('courseres',$hashid);
1.28 www 12116: }
12117:
1.763 www 12118:
1.200 www 12119: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 12120: #
12121: # Parameters:
12122: # $coursenum - Number of the course.
12123: # $coursedomain - Domain at which the course was created.
12124: # Returns:
12125: # A hash of the course parameters along (I think) with timestamps
12126: # and version info.
1.877 foxr 12127:
1.624 albertel 12128: sub get_courseresdata {
12129: my ($coursenum,$coursedomain)=@_;
1.200 www 12130: my $coursehom=&homeserver($coursenum,$coursedomain);
12131: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12132: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 12133: my %dumpreply;
1.417 albertel 12134: unless (defined($cached)) {
1.624 albertel 12135: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 12136: $result=\%dumpreply;
1.251 albertel 12137: my ($tmp) = keys(%dumpreply);
12138: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 12139: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 12140: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12141: return $tmp;
1.416 albertel 12142: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 12143: $result=undef;
1.599 albertel 12144: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 12145: }
12146: }
1.624 albertel 12147: return $result;
12148: }
12149:
1.633 albertel 12150: sub devalidateuserresdata {
12151: my ($uname,$udom)=@_;
12152: my $hashid="$udom:$uname";
12153: &devalidate_cache_new('userres',$hashid);
12154: }
12155:
1.624 albertel 12156: sub get_userresdata {
12157: my ($uname,$udom)=@_;
12158: #most student don\'t have any data set, check if there is some data
12159: if (&EXT_cache_status($udom,$uname)) { return undef; }
12160:
12161: my $hashid="$udom:$uname";
12162: my ($result,$cached)=&is_cached_new('userres',$hashid);
12163: if (!defined($cached)) {
12164: my %resourcedata=&dump('resourcedata',$udom,$uname);
12165: $result=\%resourcedata;
12166: &do_cache_new('userres',$hashid,$result,600);
12167: }
12168: my ($tmp)=keys(%$result);
12169: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12170: return $result;
12171: }
12172: #error 2 occurs when the .db doesn't exist
12173: if ($tmp!~/error: 2 /) {
1.1172.2.71 raeburn 12174: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12175: &logthis("<font color=\"blue\">WARNING:".
12176: " Trying to get resource data for ".
12177: $uname." at ".$udom.": ".
12178: $tmp."</font>");
12179: }
1.624 albertel 12180: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 12181: #&EXT_cache_set($udom,$uname);
12182: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 12183: undef($tmp); # not really an error so don't send it back
1.624 albertel 12184: }
12185: return $tmp;
12186: }
1.879 foxr 12187: #----------------------------------------------- resdata - return resource data
12188: # Purpose:
12189: # Return resource data for either users or for a course.
12190: # Parameters:
12191: # $name - Course/user name.
12192: # $domain - Name of the domain the user/course is registered on.
12193: # $type - Type of thing $name is (must be 'course' or 'user'
12194: # @which - Array of names of resources desired.
12195: # Returns:
12196: # The value of the first reasource in @which that is found in the
12197: # resource hash.
12198: # Exceptional Conditions:
12199: # If the $type passed in is not valid (not the string 'course' or
12200: # 'user', an undefined reference is returned.
12201: # If none of the resources are found, an undef is returned
1.624 albertel 12202: sub resdata {
12203: my ($name,$domain,$type,@which)=@_;
12204: my $result;
12205: if ($type eq 'course') {
12206: $result=&get_courseresdata($name,$domain);
12207: } elsif ($type eq 'user') {
12208: $result=&get_userresdata($name,$domain);
12209: }
12210: if (!ref($result)) { return $result; }
1.251 albertel 12211: foreach my $item (@which) {
1.927 albertel 12212: if (defined($result->{$item->[0]})) {
12213: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 12214: }
1.250 albertel 12215: }
1.291 albertel 12216: return undef;
1.200 www 12217: }
12218:
1.1172.2.146. .1(raebu 12219:22): sub get_domain_lti {
12220:22): my ($cdom,$context) = @_;
.4(raebu 12221:22): my ($name,$cachename,%lti);
.1(raebu 12222:22): if ($context eq 'consumer') {
12223:22): $name = 'ltitools';
12224:22): } elsif ($context eq 'provider') {
12225:22): $name = 'lti';
.4(raebu 12226:22): } elsif ($context eq 'linkprot') {
12227:22): $name = 'ltisec';
.1(raebu 12228:22): } else {
12229:22): return %lti;
12230:22): }
.4(raebu 12231:22): if ($context eq 'linkprot') {
12232:22): $cachename = $context;
12233:22): } else {
12234:22): $cachename = $name;
12235:22): }
12236:22): my ($result,$cached)=&is_cached_new($cachename,$cdom);
.1(raebu 12237:22): if (defined($cached)) {
12238:22): if (ref($result) eq 'HASH') {
12239:22): %lti = %{$result};
12240:22): }
12241:22): } else {
12242:22): my %domconfig = &get_dom('configuration',[$name],$cdom);
12243:22): if (ref($domconfig{$name}) eq 'HASH') {
.4(raebu 12244:22): if ($context eq 'linkprot') {
12245:22): if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12246:22): %lti = %{$domconfig{$name}{'linkprot'}};
12247:22): }
12248:22): } else {
12249:22): %lti = %{$domconfig{$name}};
12250:22): }
.1(raebu 12251:22): }
12252:22): my $cachetime = 24*60*60;
.4(raebu 12253:22): &do_cache_new($cachename,$cdom,\%lti,$cachetime);
.1(raebu 12254:22): }
12255:22): return %lti;
12256:22): }
12257:22):
12258:22): sub get_course_lti {
.13(raeb 12259:-23): my ($cnum,$cdom,$context) = @_;
12260:-23): my ($name,$cachename,%lti);
12261:-23): if ($context eq 'consumer') {
12262:-23): $name = 'ltitools';
12263:-23): $cachename = 'courseltitools';
12264:-23): } elsif ($context eq 'provider') {
12265:-23): $name = 'lti';
12266:-23): $cachename = 'courselti';
12267:-23): } else {
12268:-23): return %lti;
12269:-23): }
.1(raebu 12270:22): my $hashid=$cdom.'_'.$cnum;
.13(raeb 12271:-23): my ($result,$cached)=&is_cached_new($cachename,$hashid);
.1(raebu 12272:22): if (defined($cached)) {
12273:22): if (ref($result) eq 'HASH') {
.13(raeb 12274:-23): %lti = %{$result};
.1(raebu 12275:22): }
12276:22): } else {
.13(raeb 12277:-23): %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
.1(raebu 12278:22): my $cachetime = 24*60*60;
.13(raeb 12279:-23): &do_cache_new($cachename,$hashid,\%lti,$cachetime);
.1(raebu 12280:22): }
.13(raeb 12281:-23): return %lti;
.1(raebu 12282:22): }
12283:22):
.3(raebu 12284:22): sub courselti_itemid {
12285:22): my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12286:22): my ($chome,$itemid);
12287:22): $chome = &homeserver($cnum,$cdom);
12288:22): return if ($chome eq 'no_host');
12289:22): if (ref($params) eq 'HASH') {
12290:22): my $rep;
12291:22): if (grep { $_ eq $chome } current_machine_ids()) {
12292:22): $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12293:22): } else {
12294:22): my $escurl = &escape($url);
12295:22): my $escmethod = &escape($method);
12296:22): my $items = &freeze_escape($params);
12297:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12298:22): }
12299:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12300:22): ($rep eq 'unknown_cmd')) {
12301:22): $itemid = $rep;
12302:22): }
12303:22): }
12304:22): return $itemid;
12305:22): }
12306:22):
12307:22): sub domainlti_itemid {
12308:22): my ($cdom,$url,$method,$params,$context) = @_;
12309:22): my ($primary_id,$itemid);
12310:22): $primary_id = &domain($cdom,'primary');
12311:22): return if ($primary_id eq '');
12312:22): if (ref($params) eq 'HASH') {
12313:22): my $rep;
12314:22): if (grep { $_ eq $primary_id } current_machine_ids()) {
12315:22): $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12316:22): } else {
12317:22): my $cnum = '';
12318:22): my $escurl = &escape($url);
12319:22): my $escmethod = &escape($method);
12320:22): my $items = &freeze_escape($params);
12321:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12322:22): }
12323:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12324:22): ($rep eq 'unknown_cmd')) {
12325:22): $itemid = $rep;
12326:22): }
12327:22): }
12328:22): return $itemid;
12329:22): }
12330:22):
.15(raeb 12331:-23): sub get_ltitools_id {
12332:-23): my ($context,$cdom,$cnum,$title) = @_;
12333:-23): my ($lockhash,$tries,$gotlock,$id,$error);
12334:-23):
12335:-23): # get lock on ltitools db
12336:-23): $lockhash = {
12337:-23): lock => $env{'user.name'}.
12338:-23): ':'.$env{'user.domain'},
12339:-23): };
12340:-23): $tries = 0;
12341:-23): if ($context eq 'domain') {
12342:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12343:-23): } else {
12344:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12345:-23): }
12346:-23): while (($gotlock ne 'ok') && ($tries<10)) {
12347:-23): $tries ++;
12348:-23): sleep (0.1);
12349:-23): if ($context eq 'domain') {
12350:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12351:-23): } else {
12352:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12353:-23): }
12354:-23): }
12355:-23): if ($gotlock eq 'ok') {
12356:-23): my %currids;
12357:-23): if ($context eq 'domain') {
12358:-23): %currids = &dump_dom('ltitools',$cdom);
12359:-23): } else {
12360:-23): %currids = &dump('ltitools',$cdom,$cnum);
12361:-23): }
12362:-23): if ($currids{'lock'}) {
12363:-23): delete($currids{'lock'});
12364:-23): if (keys(%currids)) {
12365:-23): my @curr = sort { $a <=> $b } keys(%currids);
12366:-23): if ($curr[-1] =~ /^\d+$/) {
12367:-23): $id = 1 + $curr[-1];
12368:-23): }
12369:-23): } else {
12370:-23): $id = 1;
12371:-23): }
12372:-23): if ($id) {
12373:-23): if ($context eq 'domain') {
12374:-23): unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12375:-23): $error = 'nostore';
12376:-23): }
12377:-23): } else {
12378:-23): unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12379:-23): $error = 'nostore';
12380:-23): }
12381:-23): }
12382:-23): } else {
12383:-23): $error = 'nonumber';
12384:-23): }
12385:-23): }
12386:-23): my $dellockoutcome;
12387:-23): if ($context eq 'domain') {
12388:-23): $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12389:-23): } else {
12390:-23): $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12391:-23): }
12392:-23): } else {
12393:-23): $error = 'nolock';
12394:-23): }
12395:-23): return ($id,$error);
12396:-23): }
12397:-23):
.9(raebu 12398:23): sub count_supptools {
12399:23): my ($cnum,$cdom,$ignorecache,$reload)=@_;
1.1172.2.31 raeburn 12400: my $hashid=$cnum.':'.$cdom;
1.1172.2.146. .9(raebu 12401:23): my ($numexttools,$cached);
1.1172.2.31 raeburn 12402: unless ($ignorecache) {
1.1172.2.146. .9(raebu 12403:23): ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
1.1172.2.31 raeburn 12404: }
12405: unless (defined($cached)) {
12406: my $chome=&homeserver($cnum,$cdom);
1.1172.2.146. .9(raebu 12407:23): $numexttools = 0;
1.1172.2.31 raeburn 12408: unless ($chome eq 'no_host') {
1.1172.2.146. .9(raebu 12409:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12410:23): if (ref($supplemental) eq 'HASH') {
12411:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12412:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12413:23): if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12414:23): $numexttools ++;
12415:23): }
12416:23): }
12417:23): }
12418:23): }
1.1172.2.31 raeburn 12419: }
1.1172.2.146. .9(raebu 12420:23): &do_cache_new('supptools',$hashid,$numexttools,600);
1.1172.2.31 raeburn 12421: }
1.1172.2.146. .9(raebu 12422:23): return $numexttools;
12423:23): }
12424:23):
12425:23): sub has_unhidden_suppfiles {
12426:23): my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12427:23): my $hashid=$cnum.':'.$cdom;
12428:23): my ($showsupp,$cached);
12429:23): unless ($ignorecache) {
12430:23): ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12431:23): }
12432:23): unless (defined($cached)) {
12433:23): my $chome=&homeserver($cnum,$cdom);
12434:23): unless ($chome eq 'no_host') {
12435:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12436:23): if (ref($supplemental) eq 'HASH') {
12437:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12438:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12439:23): next if ($key =~ /\.sequence$/);
12440:23): if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12441:23): foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12442:23): unless ($supplemental->{'hidden'}->{$id}) {
12443:23): $showsupp = 1;
12444:23): last;
12445:23): }
12446:23): }
12447:23): }
12448:23): last if ($showsupp);
12449:23): }
12450:23): }
12451:23): }
12452:23): }
12453:23): &do_cache_new('showsupp',$hashid,$showsupp,600);
12454:23): }
12455:23): return $showsupp;
1.1172.2.31 raeburn 12456: }
12457:
1.379 matthew 12458: #
12459: # EXT resource caching routines
12460: #
12461:
1.1172.2.146. .1(raebu 12462:22): {
12463:22): # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12464:22): #
12465:22): # The course for which we cache
12466:22): my $cachedmapkey='';
12467:22): # The cached recursive maps for this course
12468:22): my %cachedmaps=();
12469:22): # When this was last done
12470:22): my $cachedmaptime='';
12471:22):
1.379 matthew 12472: sub clear_EXT_cache_status {
1.383 albertel 12473: &delenv('cache.EXT.');
1.379 matthew 12474: }
12475:
12476: sub EXT_cache_status {
12477: my ($target_domain,$target_user) = @_;
1.383 albertel 12478: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 12479: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 12480: # We know already the user has no data
12481: return 1;
12482: } else {
12483: return 0;
12484: }
12485: }
12486:
12487: sub EXT_cache_set {
12488: my ($target_domain,$target_user) = @_;
1.383 albertel 12489: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 12490: #&appenv({$cachename => time});
1.379 matthew 12491: }
12492:
1.28 www 12493: # --------------------------------------------------------- Value of a Variable
1.58 www 12494: sub EXT {
1.715 albertel 12495:
1.1172.2.28 raeburn 12496: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 12497: unless ($varname) { return ''; }
1.218 albertel 12498: #get real user name/domain, courseid and symb
12499: my $courseid;
1.359 albertel 12500: my $publicuser;
1.427 www 12501: if ($symbparm) {
12502: $symbparm=&get_symb_from_alias($symbparm);
12503: }
1.218 albertel 12504: if (!($uname && $udom)) {
1.790 albertel 12505: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 12506: if (!$symbparm) { $symbparm=$cursymb; }
12507: } else {
1.620 albertel 12508: $courseid=$env{'request.course.id'};
1.218 albertel 12509: }
1.48 www 12510: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12511: my $rest;
1.320 albertel 12512: if (defined($therest[0])) {
1.48 www 12513: $rest=join('.',@therest);
12514: } else {
12515: $rest='';
12516: }
1.320 albertel 12517:
1.57 www 12518: my $qualifierrest=$qualifier;
12519: if ($rest) { $qualifierrest.='.'.$rest; }
12520: my $spacequalifierrest=$space;
12521: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 12522: if ($realm eq 'user') {
1.48 www 12523: # --------------------------------------------------------------- user.resource
12524: if ($space eq 'resource') {
1.651 albertel 12525: if ( (defined($Apache::lonhomework::parsing_a_problem)
12526: || defined($Apache::lonhomework::parsing_a_task))
12527: &&
1.1172.2.142 raeburn 12528: ($symbparm eq &symbread()) ) {
1.744 albertel 12529: # if we are in the middle of processing the resource the
12530: # get the value we are planning on committing
12531: if (defined($Apache::lonhomework::results{$qualifierrest})) {
12532: return $Apache::lonhomework::results{$qualifierrest};
12533: } else {
12534: return $Apache::lonhomework::history{$qualifierrest};
12535: }
1.335 albertel 12536: } else {
1.359 albertel 12537: my %restored;
1.620 albertel 12538: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 12539: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12540: } else {
12541: %restored=&restore($symbparm,$courseid,$udom,$uname);
12542: }
1.335 albertel 12543: return $restored{$qualifierrest};
12544: }
1.48 www 12545: # ----------------------------------------------------------------- user.access
12546: } elsif ($space eq 'access') {
1.218 albertel 12547: # FIXME - not supporting calls for a specific user
1.48 www 12548: return &allowed($qualifier,$rest);
12549: # ------------------------------------------ user.preferences, user.environment
12550: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 12551: if (($uname eq $env{'user.name'}) &&
12552: ($udom eq $env{'user.domain'})) {
12553: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 12554: } else {
1.359 albertel 12555: my %returnhash;
12556: if (!$publicuser) {
12557: %returnhash=&userenvironment($udom,$uname,
12558: $qualifierrest);
12559: }
1.218 albertel 12560: return $returnhash{$qualifierrest};
12561: }
1.48 www 12562: # ----------------------------------------------------------------- user.course
12563: } elsif ($space eq 'course') {
1.218 albertel 12564: # FIXME - not supporting calls for a specific user
1.620 albertel 12565: return $env{join('.',('request.course',$qualifier))};
1.48 www 12566: # ------------------------------------------------------------------- user.role
12567: } elsif ($space eq 'role') {
1.218 albertel 12568: # FIXME - not supporting calls for a specific user
1.620 albertel 12569: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 12570: if ($qualifier eq 'value') {
12571: return $role;
12572: } elsif ($qualifier eq 'extent') {
12573: return $where;
12574: }
12575: # ----------------------------------------------------------------- user.domain
12576: } elsif ($space eq 'domain') {
1.218 albertel 12577: return $udom;
1.48 www 12578: # ------------------------------------------------------------------- user.name
12579: } elsif ($space eq 'name') {
1.218 albertel 12580: return $uname;
1.48 www 12581: # ---------------------------------------------------- Any other user namespace
1.29 www 12582: } else {
1.359 albertel 12583: my %reply;
12584: if (!$publicuser) {
12585: %reply=&get($space,[$qualifierrest],$udom,$uname);
12586: }
12587: return $reply{$qualifierrest};
1.48 www 12588: }
1.236 www 12589: } elsif ($realm eq 'query') {
12590: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 12591: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12592: [$spacequalifierrest]);
1.620 albertel 12593: return $env{'form.'.$spacequalifierrest};
1.236 www 12594: } elsif ($realm eq 'request') {
1.48 www 12595: # ------------------------------------------------------------- request.browser
12596: if ($space eq 'browser') {
1.1145 bisitz 12597: return $env{'browser.'.$qualifier};
1.57 www 12598: # ------------------------------------------------------------ request.filename
12599: } else {
1.620 albertel 12600: return $env{'request.'.$spacequalifierrest};
1.29 www 12601: }
1.28 www 12602: } elsif ($realm eq 'course') {
1.48 www 12603: # ---------------------------------------------------------- course.description
1.620 albertel 12604: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 12605: } elsif ($realm eq 'resource') {
1.165 www 12606:
1.620 albertel 12607: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 12608: if (!$symbparm) { $symbparm=&symbread(); }
12609: }
1.693 albertel 12610:
1.1172.2.30 raeburn 12611: if ($qualifier eq '') {
12612: if ($space eq 'title') {
12613: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12614: return &gettitle($symbparm);
12615: }
1.693 albertel 12616:
1.1172.2.30 raeburn 12617: if ($space eq 'map') {
12618: my ($map) = &decode_symb($symbparm);
12619: return &symbread($map);
12620: }
12621: if ($space eq 'maptitle') {
12622: my ($map) = &decode_symb($symbparm);
12623: return &gettitle($map);
12624: }
12625: if ($space eq 'filename') {
12626: if ($symbparm) {
12627: return &clutter((&decode_symb($symbparm))[2]);
12628: }
12629: return &hreflocation('',$env{'request.filename'});
1.905 albertel 12630: }
1.1172.2.30 raeburn 12631:
12632: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12633: if ($space eq 'visibleparts') {
12634: my $navmap = Apache::lonnavmaps::navmap->new();
12635: my $item;
12636: if (ref($navmap)) {
12637: my $res = $navmap->getBySymb($symbparm);
12638: my $parts = $res->parts();
12639: if (ref($parts) eq 'ARRAY') {
12640: $item = join(',',@{$parts});
12641: }
12642: undef($navmap);
12643: }
12644: return $item;
12645: }
12646: }
12647: }
1.693 albertel 12648:
12649: my ($section, $group, @groups);
1.593 albertel 12650: my ($courselevelm,$courselevel);
1.1172.2.28 raeburn 12651: if (($courseid eq '') && ($cid)) {
12652: $courseid = $cid;
12653: }
12654: if (($symbparm && $courseid) &&
12655: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 12656:
1.218 albertel 12657: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 12658:
1.60 www 12659: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 12660: my $symbp=$symbparm;
1.735 albertel 12661: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 12662:
12663: my $symbparm=$symbp.'.'.$spacequalifierrest;
12664: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12665:
1.620 albertel 12666: if (($env{'user.name'} eq $uname) &&
12667: ($env{'user.domain'} eq $udom)) {
12668: $section=$env{'request.course.sec'};
1.733 raeburn 12669: @groups = split(/:/,$env{'request.course.groups'});
12670: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 12671: } else {
1.539 albertel 12672: if (! defined($usection)) {
1.551 albertel 12673: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 12674: } else {
12675: $section = $usection;
12676: }
1.733 raeburn 12677: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 12678: }
12679:
12680: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12681: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12682: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12683:
1.593 albertel 12684: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 12685: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 12686: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 12687:
1.60 www 12688: # ----------------------------------------------------------- first, check user
1.624 albertel 12689:
12690: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 12691: ([$courselevelr,'resource'],
12692: [$courselevelm,'map' ],
12693: [$courselevel, 'course' ]));
1.931 albertel 12694: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 12695:
1.594 albertel 12696: # ------------------------------------------------ second, check some of course
1.684 raeburn 12697: my $coursereply;
1.691 raeburn 12698: if (@groups > 0) {
12699: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12700: $mapparm,$spacequalifierrest);
1.927 albertel 12701: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 12702: }
1.96 www 12703:
1.684 raeburn 12704: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 12705: $env{'course.'.$courseid.'.domain'},
12706: 'course',
12707: ([$seclevelr, 'resource'],
12708: [$seclevelm, 'map' ],
12709: [$seclevel, 'course' ],
12710: [$courselevelr,'resource']));
12711: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 12712:
1.60 www 12713: # ------------------------------------------------------ third, check map parms
1.218 albertel 12714: my %parmhash=();
12715: my $thisparm='';
12716: if (tie(%parmhash,'GDBM_File',
1.620 albertel 12717: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 12718: &GDBM_READER(),0640)) {
1.218 albertel 12719: $thisparm=$parmhash{$symbparm};
12720: untie(%parmhash);
12721: }
1.927 albertel 12722: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 12723: }
1.594 albertel 12724: # ------------------------------------------ fourth, look in resource metadata
1.71 www 12725:
1.1172.2.146. .13(raeb 12726:-23): my $what = $spacequalifierrest;
12727:-23): $what=~s/\./\_/;
12728:-23): my $filename;
1.282 albertel 12729: if (!$symbparm) { $symbparm=&symbread(); }
12730: if ($symbparm) {
1.409 www 12731: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 12732: } else {
1.620 albertel 12733: $filename=$env{'request.filename'};
1.282 albertel 12734: }
1.1172.2.146. .13(raeb 12735:-23): my $toolsymb;
12736:-23): if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12737:-23): $toolsymb = $symbparm;
12738:-23): }
12739:-23): my $metadata=&metadata($filename,$what,$toolsymb);
1.927 albertel 12740: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1172.2.146. .13(raeb 12741:-23): $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927 albertel 12742: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 12743:
1.1172.2.146. .13(raeb 12744:-23): # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 12745: if ($symbparm && defined($courseid) &&
1.620 albertel 12746: $courseid eq $env{'request.course.id'}) {
1.624 albertel 12747: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12748: $env{'course.'.$courseid.'.domain'},
12749: 'course',
1.927 albertel 12750: ([$courselevelm,'map' ],
12751: [$courselevel, 'course']));
12752: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 12753: }
1.145 www 12754: # ------------------------------------------------------------------ Cascade up
1.218 albertel 12755: unless ($space eq '0') {
1.336 albertel 12756: my @parts=split(/_/,$space);
12757: my $id=pop(@parts);
12758: my $part=join('_',@parts);
12759: if ($part eq '') { $part='0'; }
1.927 albertel 12760: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 12761: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 12762: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 12763: }
1.395 albertel 12764: if ($recurse) { return undef; }
1.1172.2.146. .13(raeb 12765:-23): my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927 albertel 12766: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 12767: # ---------------------------------------------------- Any other user namespace
12768: } elsif ($realm eq 'environment') {
12769: # ----------------------------------------------------------------- environment
1.620 albertel 12770: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12771: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 12772: } else {
1.770 albertel 12773: if ($uname eq 'anonymous' && $udom eq '') {
12774: return '';
12775: }
1.219 albertel 12776: my %returnhash=&userenvironment($udom,$uname,
12777: $spacequalifierrest);
12778: return $returnhash{$spacequalifierrest};
12779: }
1.28 www 12780: } elsif ($realm eq 'system') {
1.48 www 12781: # ----------------------------------------------------------------- system.time
12782: if ($space eq 'time') {
12783: return time;
12784: }
1.696 albertel 12785: } elsif ($realm eq 'server') {
12786: # ----------------------------------------------------------------- system.time
12787: if ($space eq 'name') {
12788: return $ENV{'SERVER_NAME'};
12789: }
1.1172.2.146. .1(raebu 12790:22): } elsif ($realm eq 'client') {
12791:22): if ($space eq 'remote_addr') {
12792:22): return &get_requestor_ip();
12793:22): }
1.28 www 12794: }
1.48 www 12795: return '';
1.61 www 12796: }
12797:
1.927 albertel 12798: sub get_reply {
12799: my ($reply_value) = @_;
1.940 raeburn 12800: if (ref($reply_value) eq 'ARRAY') {
12801: if (wantarray) {
12802: return @$reply_value;
12803: }
12804: return $reply_value->[0];
12805: } else {
12806: return $reply_value;
1.927 albertel 12807: }
12808: }
12809:
1.691 raeburn 12810: sub check_group_parms {
12811: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
12812: my @groupitems = ();
12813: my $resultitem;
1.927 albertel 12814: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 12815: foreach my $group (@{$groups}) {
12816: foreach my $level (@levels) {
1.927 albertel 12817: my $item = $courseid.'.['.$group.'].'.$level->[0];
12818: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 12819: }
12820: }
12821: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12822: $env{'course.'.$courseid.'.domain'},
12823: 'course',@groupitems);
12824: return $coursereply;
12825: }
12826:
1.1172.2.146. .1(raebu 12827:22): sub get_map_hierarchy {
12828:22): my ($mapname,$courseid) = @_;
12829:22): my @recurseup = ();
12830:22): if ($mapname) {
12831:22): if (($cachedmapkey eq $courseid) &&
12832:22): (abs($cachedmaptime-time)<5)) {
12833:22): if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12834:22): return @{$cachedmaps{$mapname}};
12835:22): }
12836:22): }
12837:22): my $navmap = Apache::lonnavmaps::navmap->new();
12838:22): if (ref($navmap)) {
12839:22): @recurseup = $navmap->recurseup_maps($mapname);
12840:22): undef($navmap);
12841:22): $cachedmaps{$mapname} = \@recurseup;
12842:22): $cachedmaptime=time;
12843:22): $cachedmapkey=$courseid;
12844:22): }
12845:22): }
12846:22): return @recurseup;
12847:22): }
12848:22):
.2(raebu 12849:22): }
.1(raebu 12850:22):
1.691 raeburn 12851: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 12852: my ($courseid,@groups) = @_;
12853: @groups = sort(@groups);
1.691 raeburn 12854: return @groups;
12855: }
12856:
1.395 albertel 12857: sub packages_tab_default {
1.1172.2.146. .13(raeb 12858:-23): my ($uri,$varname,$toolsymb)=@_;
1.395 albertel 12859: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 12860:
12861: my (@extension,@specifics,$do_default);
1.1172.2.146. .13(raeb 12862:-23): foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395 albertel 12863: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 12864: if ($pack_type eq 'default') {
12865: $do_default=1;
12866: } elsif ($pack_type eq 'extension') {
12867: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 12868: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 12869: # only look at packages defaults for packages that this id is
1.738 albertel 12870: push(@specifics,[$package,$pack_type,$pack_part]);
12871: }
12872: }
12873: # first look for a package that matches the requested part id
12874: foreach my $package (@specifics) {
12875: my (undef,$pack_type,$pack_part)=@{$package};
12876: next if ($pack_part ne $part);
12877: if (defined($packagetab{"$pack_type&$name&default"})) {
12878: return $packagetab{"$pack_type&$name&default"};
12879: }
12880: }
12881: # look for any possible matching non extension_ package
12882: foreach my $package (@specifics) {
12883: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 12884: if (defined($packagetab{"$pack_type&$name&default"})) {
12885: return $packagetab{"$pack_type&$name&default"};
12886: }
1.585 albertel 12887: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 12888: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12889: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 12890: }
12891: }
1.738 albertel 12892: # look for any posible extension_ match
12893: foreach my $package (@extension) {
12894: my ($package,$pack_type)=@{$package};
12895: if (defined($packagetab{"$pack_type&$name&default"})) {
12896: return $packagetab{"$pack_type&$name&default"};
12897: }
12898: if (defined($packagetab{$package."&$name&default"})) {
12899: return $packagetab{$package."&$name&default"};
12900: }
12901: }
12902: # look for a global default setting
12903: if ($do_default && defined($packagetab{"default&$name&default"})) {
12904: return $packagetab{"default&$name&default"};
12905: }
1.395 albertel 12906: return undef;
12907: }
12908:
1.334 albertel 12909: sub add_prefix_and_part {
12910: my ($prefix,$part)=@_;
12911: my $keyroot;
12912: if (defined($prefix) && $prefix !~ /^__/) {
12913: # prefix that has a part already
12914: $keyroot=$prefix;
12915: } elsif (defined($prefix)) {
12916: # prefix that is missing a part
12917: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12918: } else {
12919: # no prefix at all
12920: if (defined($part)) { $keyroot='_'.$part; }
12921: }
12922: return $keyroot;
12923: }
12924:
1.71 www 12925: # ---------------------------------------------------------------- Get metadata
12926:
1.599 albertel 12927: my %metaentry;
1.1070 www 12928: my %importedpartids;
1.1172.2.99 raeburn 12929: my %importedrespids;
1.71 www 12930: sub metadata {
1.1172.2.146. .13(raeb 12931:-23): my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71 www 12932: $uri=&declutter($uri);
1.288 albertel 12933: # if it is a non metadata possible uri return quickly
1.529 albertel 12934: if (($uri eq '') ||
12935: (($uri =~ m|^/*adm/|) &&
1.1172.2.146. .1(raebu 12936:22): ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 12937: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 12938: return undef;
12939: }
1.1172.2.49 raeburn 12940: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 12941: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 12942: return undef;
1.288 albertel 12943: }
1.73 www 12944: my $filename=$uri;
12945: $uri=~s/\.meta$//;
1.172 www 12946: #
12947: # Is the metadata already cached?
1.177 www 12948: # Look at timestamp of caching
1.172 www 12949: # Everything is cached by the main uri, libraries are never directly cached
12950: #
1.428 albertel 12951: if (!defined($liburi)) {
1.599 albertel 12952: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 12953: if (defined($cached)) { return $result->{':'.$what}; }
12954: }
1.1172.2.146. .13(raeb 12955:-23):
12956:-23): #
12957:-23): # If the uri is for an external tool the file from
12958:-23): # which metadata should be retrieved depends on whether
12959:-23): # the tool had been configured to be gradable (set in the Course
12960:-23): # Editor or Resource Editor).
12961:-23): #
12962:-23): # If a valid symb has been included as the third arg in the call
12963:-23): # to &metadata() that can be used to retrieve the value of
12964:-23): # parameter_0_gradable set for the resource, and included in the
12965:-23): # uploaded map containing the tool. The value is retrieved via
12966:-23): # &EXT(), if a valid symb is available. Otherwise the value of
12967:-23): # gradable in the exttool_$marker.db file for the tool instance
12968:-23): # is retrieved via &get().
12969:-23): #
12970:-23): # When lonuserstate::traceroute() calls lonnet::EXT() for
12971:-23): # hiddenresource and encrypturl (during course initialization)
12972:-23): # the map-level parameter for resource.0.gradable included in the
12973:-23): # uploaded map containing the tool will not yet have been stored
12974:-23): # in the user_course_parms.db file for the user's session, so in
12975:-23): # this case fall back to retrieving gradable status from the
12976:-23): # exttool_$marker.db file.
12977:-23): #
12978:-23): # In order to avoid an infinite loop, &metadata() will return
12979:-23): # before a call to &EXT(), if the uri is for an external tool
12980:-23): # and the $what for which metadata is being requested is
12981:-23): # parameter_0_gradable or 0_gradable.
12982:-23): #
12983:-23):
12984:-23): if ($uri =~ /ext\.tool$/) {
12985:-23): if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12986:-23): return;
12987:-23): } else {
12988:-23): my ($checked,$use_passback);
12989:-23): if ($toolsymb ne '') {
12990:-23): (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12991:-23): if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12992:-23): $checked = 1;
12993:-23): if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12994:-23): $use_passback = 1;
12995:-23): }
12996:-23): }
12997:-23): }
12998:-23): unless ($checked) {
12999:-23): my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
13000:-23): $marker=~s/\D//g;
13001:-23): if ($marker) {
13002:-23): my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
13003:-23): $use_passback = $toolsettings{'gradable'};
13004:-23): }
13005:-23): }
13006:-23): if ($use_passback) {
13007:-23): $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
13008:-23): } else {
13009:-23): $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
13010:-23): }
13011:-23): }
13012:-23): }
13013:-23):
1.428 albertel 13014: {
1.1069 www 13015: # Imported parts would go here
1.1172.2.99 raeburn 13016: my @origfiletagids=();
1.1069 www 13017: my $importedparts=0;
1.1172.2.99 raeburn 13018:
13019: # Imported responseids would go here
13020: my $importedresponses=0;
1.172 www 13021: #
13022: # Is this a recursive call for a library?
13023: #
1.599 albertel 13024: # if (! exists($metacache{$uri})) {
13025: # $metacache{$uri}={};
13026: # }
1.924 albertel 13027: my $cachetime = 60*60;
1.171 www 13028: if ($liburi) {
13029: $liburi=&declutter($liburi);
13030: $filename=$liburi;
1.401 bowersj2 13031: } else {
1.599 albertel 13032: &devalidate_cache_new('meta',$uri);
13033: undef(%metaentry);
1.401 bowersj2 13034: }
1.140 www 13035: my %metathesekeys=();
1.73 www 13036: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 13037: my $metastring;
1.1140 www 13038: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 13039: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 13040: $metastring =
1.929 albertel 13041: &Apache::lonnet::ssi_body($which,
1.924 albertel 13042: ('grade_target' => 'meta'));
13043: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 13044: } elsif (($uri !~ m -^(editupload)/-) &&
13045: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 13046: my $file=&filelocation('',&clutter($filename));
1.599 albertel 13047: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 13048: $metastring=&getfile($file);
1.489 albertel 13049: }
1.208 albertel 13050: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 13051: my $token;
1.140 www 13052: undef %metathesekeys;
1.71 www 13053: while ($token=$parser->get_token) {
1.339 albertel 13054: if ($token->[0] eq 'S') {
13055: if (defined($token->[2]->{'package'})) {
1.172 www 13056: #
13057: # This is a package - get package info
13058: #
1.339 albertel 13059: my $package=$token->[2]->{'package'};
13060: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13061: if (defined($token->[2]->{'id'})) {
13062: $keyroot.='_'.$token->[2]->{'id'};
13063: }
1.599 albertel 13064: if ($metaentry{':packages'}) {
13065: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 13066: } else {
1.599 albertel 13067: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 13068: }
1.736 albertel 13069: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 13070: my $part=$keyroot;
13071: $part=~s/^\_//;
1.736 albertel 13072: if ($pack_entry=~/^\Q$package\E\&/ ||
13073: $pack_entry=~/^\Q$package\E_0\&/) {
13074: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 13075: # ignore package.tab specified default values
13076: # here &package_tab_default() will fetch those
13077: if ($subp eq 'default') { next; }
1.736 albertel 13078: my $value=$packagetab{$pack_entry};
1.432 albertel 13079: my $unikey;
13080: if ($pack =~ /_0$/) {
13081: $unikey='parameter_0_'.$name;
13082: $part=0;
13083: } else {
13084: $unikey='parameter'.$keyroot.'_'.$name;
13085: }
1.339 albertel 13086: if ($subp eq 'display') {
13087: $value.=' [Part: '.$part.']';
13088: }
1.599 albertel 13089: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 13090: $metathesekeys{$unikey}=1;
1.599 albertel 13091: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13092: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 13093: }
1.599 albertel 13094: if (defined($metaentry{':'.$unikey.'.default'})) {
13095: $metaentry{':'.$unikey}=
13096: $metaentry{':'.$unikey.'.default'};
1.356 albertel 13097: }
1.339 albertel 13098: }
13099: }
13100: } else {
1.172 www 13101: #
13102: # This is not a package - some other kind of start tag
1.339 albertel 13103: #
13104: my $entry=$token->[1];
1.1068 www 13105: my $unikey='';
1.175 www 13106:
1.339 albertel 13107: if ($entry eq 'import') {
1.175 www 13108: #
13109: # Importing a library here
1.339 albertel 13110: #
1.1067 www 13111: my $location=$parser->get_text('/import');
13112: my $dir=$filename;
13113: $dir=~s|[^/]*$||;
13114: $location=&filelocation($dir,$location);
1.1172.2.99 raeburn 13115:
13116: my $importid=$token->[2]->{'id'};
1.1068 www 13117: my $importmode=$token->[2]->{'importmode'};
1.1172.2.99 raeburn 13118: #
13119: # Check metadata for imported file to
13120: # see if it contained response items
13121: #
13122: my %currmetaentry = %metaentry;
13123: my $libresponseorder = &metadata($location,'responseorder');
13124: my $origfile;
13125: if ($libresponseorder ne '') {
13126: if ($#origfiletagids<0) {
13127: undef(%importedrespids);
13128: undef(%importedpartids);
13129: }
13130: @{$importedrespids{$importid}} = split(/\s*,\s*/,$libresponseorder);
13131: if (@{$importedrespids{$importid}} > 0) {
13132: $importedresponses = 1;
13133: # We need to get the original file and the imported file to get the response order correct
13134: # Load and inspect original file
13135: if ($#origfiletagids<0) {
13136: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13137: $origfile=&getfile($origfilelocation);
13138: @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13139: }
13140: }
13141: }
13142: # Do not overwrite contents of %metaentry hash for resource itself with
13143: # hash populated for imported library file
13144: %metaentry = %currmetaentry;
13145: undef(%currmetaentry);
1.1068 www 13146: if ($importmode eq 'problem') {
1.1069 www 13147: # Import as problem/response
1.1068 www 13148: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13149: } elsif ($importmode eq 'part') {
13150: # Import as part(s)
1.1069 www 13151: $importedparts=1;
13152: # We need to get the original file and the imported file to get the part order correct
13153: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1172.2.99 raeburn 13154: # Load and inspect original file if we didn't do that already
13155: if ($#origfiletagids<0) {
13156: undef(%importedrespids);
13157: undef(%importedpartids);
13158: if ($origfile eq '') {
13159: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13160: $origfile=&getfile($origfilelocation);
13161: @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13162: }
1.1070 www 13163: }
13164:
1.1069 www 13165: # Load and inspect imported file
13166: my $impfile=&getfile($location);
13167: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13168: if ($#impfilepartids>=0) {
13169: # This problem had parts
1.1070 www 13170: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 13171: } else {
13172: # Importing by turning a single problem into a problem part
13173: # It gets the import-tags ID as part-ID
13174: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 13175: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 13176: }
1.1068 www 13177: } else {
13178: # Normal import
13179: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13180: if (defined($token->[2]->{'id'})) {
13181: $unikey.='_'.$token->[2]->{'id'};
13182: }
1.1067 www 13183: }
13184:
1.339 albertel 13185: if ($depthcount<20) {
1.736 albertel 13186: my $metadata =
1.1172.2.146. .13(raeb 13187:-23): &metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736 albertel 13188: $depthcount+1);
13189: foreach my $meta (split(',',$metadata)) {
13190: $metaentry{':'.$meta}=$metaentry{':'.$meta};
13191: $metathesekeys{$meta}=1;
1.339 albertel 13192: }
1.1068 www 13193:
13194: }
1.1067 www 13195: } else {
13196: #
13197: # Not importing, some other kind of non-package, non-library start tag
13198: #
13199: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13200: if (defined($token->[2]->{'id'})) {
13201: $unikey.='_'.$token->[2]->{'id'};
13202: }
1.339 albertel 13203: if (defined($token->[2]->{'name'})) {
13204: $unikey.='_'.$token->[2]->{'name'};
13205: }
13206: $metathesekeys{$unikey}=1;
1.736 albertel 13207: foreach my $param (@{$token->[3]}) {
13208: $metaentry{':'.$unikey.'.'.$param} =
13209: $token->[2]->{$param};
1.339 albertel 13210: }
13211: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 13212: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 13213: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13214: # only ws inside the tag, and not in default, so use default
13215: # as value
1.599 albertel 13216: $metaentry{':'.$unikey}=$default;
1.908 albertel 13217: } elsif ( $internaltext =~ /\S/ ) {
13218: # something interesting inside the tag
13219: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 13220: } else {
1.908 albertel 13221: # no interesting values, don't set a default
1.339 albertel 13222: }
1.172 www 13223: # end of not-a-package not-a-library import
1.339 albertel 13224: }
1.172 www 13225: # end of not-a-package start tag
1.339 albertel 13226: }
1.172 www 13227: # the next is the end of "start tag"
1.339 albertel 13228: }
13229: }
1.483 albertel 13230: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 13231: $extension = lc($extension);
13232: if ($extension eq 'htm') { $extension='html'; }
13233:
1.737 albertel 13234: foreach my $key (keys(%packagetab)) {
1.483 albertel 13235: #no specific packages #how's our extension
13236: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 13237: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 13238: \%metathesekeys);
13239: }
1.883 albertel 13240:
13241: if (!exists($metaentry{':packages'})
13242: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 13243: foreach my $key (keys(%packagetab)) {
1.483 albertel 13244: #no specific packages well let's get default then
13245: if ($key!~/^default&/) { next; }
1.488 albertel 13246: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 13247: \%metathesekeys);
13248: }
13249: }
1.338 www 13250: # are there custom rights to evaluate
1.599 albertel 13251: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 13252:
1.338 www 13253: #
13254: # Importing a rights file here
1.339 albertel 13255: #
13256: unless ($depthcount) {
1.599 albertel 13257: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 13258: my $dir=$filename;
13259: $dir=~s|[^/]*$||;
13260: $location=&filelocation($dir,$location);
1.736 albertel 13261: my $rights_metadata =
1.1172.2.146. .13(raeb 13262:-23): &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736 albertel 13263: $depthcount+1);
13264: foreach my $rights (split(',',$rights_metadata)) {
13265: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13266: $metathesekeys{$rights}=1;
1.339 albertel 13267: }
13268: }
13269: }
1.737 albertel 13270: # uniqifiy package listing
13271: my %seen;
13272: my @uniq_packages =
13273: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13274: $metaentry{':packages'} = join(',',@uniq_packages);
13275:
1.1172.2.99 raeburn 13276: if (($importedresponses) || ($importedparts)) {
13277: if ($importedparts) {
1.1070 www 13278: # We had imported parts and need to rebuild partorder
1.1172.2.99 raeburn 13279: $metaentry{':partorder'}='';
13280: $metathesekeys{'partorder'}=1;
13281: }
13282: if ($importedresponses) {
13283: # We had imported responses and need to rebuild responseorder
13284: $metaentry{':responseorder'}='';
13285: $metathesekeys{'responseorder'}=1;
13286: }
13287: for (my $index=0;$index<$#origfiletagids;$index+=2) {
13288: my $origid = $origfiletagids[$index+1];
13289: if ($origfiletagids[$index] eq 'part') {
13290: # Original part, part of the problem
13291: if ($importedparts) {
13292: $metaentry{':partorder'}.=','.$origid;
13293: }
13294: } elsif ($origfiletagids[$index] eq 'import') {
13295: if ($importedparts) {
13296: # We have imported parts at this position
13297: $metaentry{':partorder'}.=','.$importedpartids{$origid};
13298: }
13299: if ($importedresponses) {
13300: # We have imported responses at this position
13301: if (ref($importedrespids{$origid}) eq 'ARRAY') {
13302: $metaentry{':responseorder'}.=','.join(',',map { $origid.'_'.$_ } @{$importedrespids{$origid}});
13303: }
13304: }
13305: } else {
13306: # Original response item, part of the problem
13307: if ($importedresponses) {
13308: $metaentry{':responseorder'}.=','.$origid;
13309: }
13310: }
13311: }
13312: if ($importedparts) {
13313: $metaentry{':partorder'}=~s/^\,//;
13314: }
13315: if ($importedresponses) {
13316: $metaentry{':responseorder'}=~s/^\,//;
13317: }
1.1070 www 13318: }
13319:
1.737 albertel 13320: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 13321: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1172.2.58 raeburn 13322: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 13323: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 13324: # this is the end of "was not already recently cached
1.71 www 13325: }
1.599 albertel 13326: return $metaentry{':'.$what};
1.261 albertel 13327: }
13328:
1.488 albertel 13329: sub metadata_create_package_def {
1.483 albertel 13330: my ($uri,$key,$package,$metathesekeys)=@_;
13331: my ($pack,$name,$subp)=split(/\&/,$key);
13332: if ($subp eq 'default') { next; }
13333:
1.599 albertel 13334: if (defined($metaentry{':packages'})) {
13335: $metaentry{':packages'}.=','.$package;
1.483 albertel 13336: } else {
1.599 albertel 13337: $metaentry{':packages'}=$package;
1.483 albertel 13338: }
13339: my $value=$packagetab{$key};
13340: my $unikey;
13341: $unikey='parameter_0_'.$name;
1.599 albertel 13342: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 13343: $$metathesekeys{$unikey}=1;
1.599 albertel 13344: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13345: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 13346: }
1.599 albertel 13347: if (defined($metaentry{':'.$unikey.'.default'})) {
13348: $metaentry{':'.$unikey}=
13349: $metaentry{':'.$unikey.'.default'};
1.483 albertel 13350: }
13351: }
13352:
1.261 albertel 13353: sub metadata_generate_part0 {
13354: my ($metadata,$metacache,$uri) = @_;
13355: my %allnames;
1.737 albertel 13356: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 13357: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 13358: my $part=$$metacache{':'.$metakey.'.part'};
13359: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 13360: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 13361: $allnames{$name}=$part;
13362: }
13363: }
13364: }
13365: foreach my $name (keys(%allnames)) {
13366: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 13367: my $key=":parameter_0_$name";
1.261 albertel 13368: $$metacache{"$key.part"}='0';
13369: $$metacache{"$key.name"}=$name;
1.428 albertel 13370: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 13371: $allnames{$name}.'_'.$name.
13372: '.type'};
1.428 albertel 13373: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 13374: '.display'};
1.644 www 13375: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 13376: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 13377: $$metacache{"$key.display"}=$olddis;
13378: }
1.71 www 13379: }
13380:
1.764 albertel 13381: # ------------------------------------------------------ Devalidate title cache
13382:
13383: sub devalidate_title_cache {
13384: my ($url)=@_;
13385: if (!$env{'request.course.id'}) { return; }
13386: my $symb=&symbread($url);
13387: if (!$symb) { return; }
13388: my $key=$env{'request.course.id'}."\0".$symb;
13389: &devalidate_cache_new('title',$key);
13390: }
13391:
1.1014 droeschl 13392: # ------------------------------------------------- Get the title of a course
13393:
13394: sub current_course_title {
13395: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13396: }
1.301 www 13397: # ------------------------------------------------- Get the title of a resource
13398:
13399: sub gettitle {
13400: my $urlsymb=shift;
13401: my $symb=&symbread($urlsymb);
1.534 albertel 13402: if ($symb) {
1.620 albertel 13403: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 13404: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 13405: if (defined($cached)) {
13406: return $result;
13407: }
1.534 albertel 13408: my ($map,$resid,$url)=&decode_symb($symb);
13409: my $title='';
1.907 albertel 13410: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13411: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13412: } else {
13413: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13414: &GDBM_READER(),0640)) {
13415: my $mapid=$bighash{'map_pc_'.&clutter($map)};
13416: $title=$bighash{'title_'.$mapid.'.'.$resid};
13417: untie(%bighash);
13418: }
1.534 albertel 13419: }
13420: $title=~s/\&colon\;/\:/gs;
13421: if ($title) {
1.1159 www 13422: # Remember both $symb and $title for dynamic metadata
13423: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 13424: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 13425: # Cache this title and then return it
1.599 albertel 13426: return &do_cache_new('title',$key,$title,600);
1.534 albertel 13427: }
13428: $urlsymb=$url;
13429: }
13430: my $title=&metadata($urlsymb,'title');
13431: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
13432: return $title;
1.301 www 13433: }
1.613 albertel 13434:
1.614 albertel 13435: sub get_slot {
13436: my ($which,$cnum,$cdom)=@_;
13437: if (!$cnum || !$cdom) {
1.790 albertel 13438: (undef,my $courseid)=&whichuser();
1.620 albertel 13439: $cdom=$env{'course.'.$courseid.'.domain'};
13440: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 13441: }
1.703 albertel 13442: my $key=join("\0",'slots',$cdom,$cnum,$which);
13443: my %slotinfo;
13444: if (exists($remembered{$key})) {
13445: $slotinfo{$which} = $remembered{$key};
13446: } else {
13447: %slotinfo=&get('slots',[$which],$cdom,$cnum);
13448: &Apache::lonhomework::showhash(%slotinfo);
13449: my ($tmp)=keys(%slotinfo);
13450: if ($tmp=~/^error:/) { return (); }
13451: $remembered{$key} = $slotinfo{$which};
13452: }
1.616 albertel 13453: if (ref($slotinfo{$which}) eq 'HASH') {
13454: return %{$slotinfo{$which}};
13455: }
13456: return $slotinfo{$which};
1.614 albertel 13457: }
1.1150 raeburn 13458:
13459: sub get_reservable_slots {
13460: my ($cnum,$cdom,$uname,$udom) = @_;
13461: my $now = time;
13462: my $reservable_info;
13463: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13464: if (exists($remembered{$key})) {
13465: $reservable_info = $remembered{$key};
13466: } else {
13467: my %resv;
13468: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13469: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13470: $reservable_info = \%resv;
13471: $remembered{$key} = $reservable_info;
13472: }
13473: return $reservable_info;
13474: }
13475:
13476: sub get_course_slots {
13477: my ($cnum,$cdom) = @_;
13478: my $hashid=$cnum.':'.$cdom;
13479: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13480: if (defined($cached)) {
13481: if (ref($result) eq 'HASH') {
13482: return %{$result};
13483: }
13484: } else {
13485: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13486: my ($tmp) = keys(%slots);
13487: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1172.2.23 raeburn 13488: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 13489: return %slots;
13490: }
13491: }
13492: return;
13493: }
13494:
13495: sub devalidate_slots_cache {
13496: my ($cnum,$cdom)=@_;
13497: my $hashid=$cnum.':'.$cdom;
13498: &devalidate_cache_new('allslots',$hashid);
13499: }
13500:
1.1172.2.8 raeburn 13501: sub get_coursechange {
13502: my ($cdom,$cnum) = @_;
13503: if ($cdom eq '' || $cnum eq '') {
13504: return unless ($env{'request.course.id'});
13505: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13506: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13507: }
13508: my $hashid=$cdom.'_'.$cnum;
13509: my ($change,$cached)=&is_cached_new('crschange',$hashid);
13510: if ((defined($cached)) && ($change ne '')) {
13511: return $change;
13512: } else {
13513: my %crshash;
13514: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13515: if ($crshash{'internal.contentchange'} eq '') {
13516: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13517: if ($change eq '') {
13518: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13519: $change = $crshash{'internal.created'};
13520: }
13521: } else {
13522: $change = $crshash{'internal.contentchange'};
13523: }
13524: my $cachetime = 600;
13525: &do_cache_new('crschange',$hashid,$change,$cachetime);
13526: }
13527: return $change;
13528: }
13529:
13530: sub devalidate_coursechange_cache {
1.1172.2.146. .9(raebu 13531:23): my ($cdom,$cnum)=@_;
13532:23): my $hashid=$cdom.'_'.$cnum;
1.1172.2.8 raeburn 13533: &devalidate_cache_new('crschange',$hashid);
13534: }
13535:
1.1172.2.146. .9(raebu 13536:23): sub get_suppchange {
13537:23): my ($cdom,$cnum) = @_;
13538:23): if ($cdom eq '' || $cnum eq '') {
13539:23): return unless ($env{'request.course.id'});
13540:23): $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13541:23): $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13542:23): }
13543:23): my $hashid=$cdom.'_'.$cnum;
13544:23): my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13545:23): if ((defined($cached)) && ($change ne '')) {
13546:23): return $change;
13547:23): } else {
13548:23): my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13549:23): if ($crshash{'internal.supplementalchange'} eq '') {
13550:23): $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13551:23): if ($change eq '') {
13552:23): %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13553:23): $change = $crshash{'internal.created'};
13554:23): }
13555:23): } else {
13556:23): $change = $crshash{'internal.supplementalchange'};
13557:23): }
13558:23): my $cachetime = 600;
13559:23): &do_cache_new('suppchange',$hashid,$change,$cachetime);
13560:23): }
13561:23): return $change;
13562:23): }
13563:23):
13564:23): sub devalidate_suppchange_cache {
13565:23): my ($cdom,$cnum)=@_;
13566:23): my $hashid=$cdom.'_'.$cnum;
13567:23): &devalidate_cache_new('suppchange',$hashid);
13568:23): }
13569:23):
13570:23): sub update_supp_caches {
13571:23): my ($cdom,$cnum) = @_;
13572:23): my %servers = &internet_dom_servers($cdom);
13573:23): my @ids=¤t_machine_ids();
13574:23): foreach my $server (keys(%servers)) {
13575:23): next if (grep(/^\Q$server\E$/,@ids));
13576:23): my $hashid=$cnum.':'.$cdom;
13577:23): my $cachekey = &escape('showsupp').':'.&escape($hashid);
13578:23): &remote_devalidate_cache($server,[$cachekey]);
13579:23): }
13580:23): &has_unhidden_suppfiles($cnum,$cdom,1,1);
13581:23): &count_supptools($cnum,$cdom,1);
13582:23): my $now = time;
13583:23): if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13584:23): &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13585:23): }
13586:23): &put('environment',{'internal.supplementalchange' => $now},
13587:23): $cdom,$cnum);
13588:23): &Apache::lonnet::appenv(
13589:23): {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13590:23): &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13591:23): }
13592:23):
1.31 www 13593: # ------------------------------------------------- Update symbolic store links
13594:
13595: sub symblist {
13596: my ($mapname,%newhash)=@_;
1.438 www 13597: $mapname=&deversion(&declutter($mapname));
1.31 www 13598: my %hash;
1.620 albertel 13599: if (($env{'request.course.fn'}) && (%newhash)) {
13600: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 13601: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 13602: foreach my $url (keys(%newhash)) {
1.711 albertel 13603: next if ($url eq 'last_known'
13604: && $env{'form.no_update_last_known'});
13605: $hash{declutter($url)}=&encode_symb($mapname,
13606: $newhash{$url}->[1],
13607: $newhash{$url}->[0]);
1.191 harris41 13608: }
1.31 www 13609: if (untie(%hash)) {
13610: return 'ok';
13611: }
13612: }
13613: }
13614: return 'error';
1.212 www 13615: }
13616:
13617: # --------------------------------------------------------------- Verify a symb
13618:
13619: sub symbverify {
1.1172.2.11 raeburn 13620: my ($symb,$thisurl,$encstate)=@_;
1.510 www 13621: my $thisfn=$thisurl;
1.439 www 13622: $thisfn=&declutter($thisfn);
1.215 www 13623: # direct jump to resource in page or to a sequence - will construct own symbs
13624: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13625: # check URL part
1.409 www 13626: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 13627:
1.431 www 13628: unless ($url eq $thisfn) { return 0; }
1.213 www 13629:
1.216 www 13630: $symb=&symbclean($symb);
1.510 www 13631: $thisurl=&deversion($thisurl);
1.439 www 13632: $thisfn=&deversion($thisfn);
1.213 www 13633:
13634: my %bighash;
13635: my $okay=0;
1.431 www 13636:
1.620 albertel 13637: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13638: &GDBM_READER(),0640)) {
1.1032 raeburn 13639: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13640: $thisurl =~ s/\?.+$//;
1.1172.2.13 raeburn 13641: if ($map =~ m{^uploaded/.+\.page$}) {
13642: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13643: $thisurl =~ s{^\Qhttp://https://\E}{https://};
13644: }
13645: }
13646: my $ids;
1.1172.2.122 raeburn 13647: if ($map =~ m{^uploaded/.+\.page$}) {
13648: $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1172.2.13 raeburn 13649: } else {
13650: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 13651: }
1.1102 raeburn 13652: unless ($ids) {
1.1172.2.13 raeburn 13653: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
1.1102 raeburn 13654: $ids=$bighash{$idkey};
1.216 www 13655: }
13656: if ($ids) {
13657: # ------------------------------------------------------------------- Has ID(s)
1.1172.2.13 raeburn 13658: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13659: $symb =~ s/\?.+$//;
13660: }
1.800 albertel 13661: foreach my $id (split(/\,/,$ids)) {
13662: my ($mapid,$resid)=split(/\./,$id);
1.216 www 13663: if (
13664: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1172.2.13 raeburn 13665: eq $symb) {
1.1172.2.11 raeburn 13666: if (ref($encstate)) {
13667: $$encstate = $bighash{'encrypted_'.$id};
13668: }
1.1172.2.13 raeburn 13669: if (($env{'request.role.adv'}) ||
13670: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
1.1101 raeburn 13671: ($thisurl eq '/adm/navmaps')) {
1.1172.2.13 raeburn 13672: $okay=1;
13673: last;
13674: }
13675: }
13676: }
1.216 www 13677: }
1.213 www 13678: untie(%bighash);
13679: }
13680: return $okay;
1.31 www 13681: }
13682:
1.210 www 13683: # --------------------------------------------------------------- Clean-up symb
13684:
13685: sub symbclean {
13686: my $symb=shift;
1.568 albertel 13687: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 13688: # remove version from map
13689: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 13690:
1.210 www 13691: # remove version from URL
13692: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 13693:
1.507 www 13694: # remove wrapper
13695:
1.510 www 13696: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 13697: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 13698: return $symb;
1.409 www 13699: }
13700:
13701: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 13702:
13703: sub encode_symb {
13704: my ($map,$resid,$url)=@_;
13705: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13706: }
1.409 www 13707:
13708: sub decode_symb {
1.568 albertel 13709: my $symb=shift;
13710: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13711: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 13712: return (&fixversion($map),$resid,&fixversion($url));
13713: }
13714:
13715: sub fixversion {
13716: my $fn=shift;
1.609 banghart 13717: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 13718: my %bighash;
13719: my $uri=&clutter($fn);
1.620 albertel 13720: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 13721: # is this cached?
1.599 albertel 13722: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 13723: if (defined($cached)) { return $result; }
13724: # unfortunately not cached, or expired
1.620 albertel 13725: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 13726: &GDBM_READER(),0640)) {
13727: if ($bighash{'version_'.$uri}) {
13728: my $version=$bighash{'version_'.$uri};
1.444 www 13729: unless (($version eq 'mostrecent') ||
13730: ($version==&getversion($uri))) {
1.440 www 13731: $uri=~s/\.(\w+)$/\.$version\.$1/;
13732: }
13733: }
13734: untie %bighash;
1.413 www 13735: }
1.599 albertel 13736: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 13737: }
13738:
13739: sub deversion {
13740: my $url=shift;
13741: $url=~s/\.\d+\.(\w+)$/\.$1/;
13742: return $url;
1.210 www 13743: }
13744:
1.31 www 13745: # ------------------------------------------------------ Return symb list entry
13746:
13747: sub symbread {
1.1172.2.126 raeburn 13748: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1172.2.128 raeburn 13749: $ignoresymbdb,$noenccheck)=@_;
1.1172.2.54 raeburn 13750: my $cache_str='request.symbread.cached.'.$thisfn;
1.1172.2.128 raeburn 13751: if (defined($env{$cache_str})) {
1.1172.2.126 raeburn 13752: unless (ref($possibles) eq 'HASH') {
13753: if ($ignorecachednull) {
13754: return $env{$cache_str} unless ($env{$cache_str} eq '');
13755: } else {
13756: return $env{$cache_str};
13757: }
1.1172.2.66 raeburn 13758: }
13759: }
1.242 www 13760: # no filename provided? try from environment
1.1172.2.54 raeburn 13761: unless ($thisfn) {
1.620 albertel 13762: if ($env{'request.symb'}) {
1.1172.2.128 raeburn 13763: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.1172.2.13 raeburn 13764: }
13765: $thisfn=$env{'request.filename'};
1.44 www 13766: }
1.569 albertel 13767: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 13768: # is that filename actually a symb? Verify, clean, and return
13769: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 13770: if (&symbverify($thisfn,$1)) {
1.1172.2.128 raeburn 13771: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 13772: }
1.242 www 13773: }
1.44 www 13774: $thisfn=declutter($thisfn);
1.31 www 13775: my %hash;
1.37 www 13776: my %bighash;
13777: my $syval='';
1.620 albertel 13778: if (($env{'request.course.fn'}) && ($thisfn)) {
1.1172.2.128 raeburn 13779: unless ($ignoresymbdb) {
1.1172.2.126 raeburn 13780: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13781: &GDBM_READER(),0640)) {
1.1172.2.146. .7(raebu 13782:22): $syval=$hash{$thisfn};
1.1172.2.126 raeburn 13783: untie(%hash);
13784: }
1.1172.2.128 raeburn 13785: if ($syval && $checkforblock) {
13786: my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1172.2.126 raeburn 13787: if (@blockers) {
13788: $syval='';
13789: }
13790: }
1.37 www 13791: }
13792: # ---------------------------------------------------------- There was an entry
13793: if ($syval) {
1.601 albertel 13794: #unless ($syval=~/\_\d+$/) {
1.620 albertel 13795: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 13796: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13797: #return $env{$cache_str}='';
1.601 albertel 13798: #}
13799: #$syval.=$1;
13800: #}
1.37 www 13801: } else {
13802: # ------------------------------------------------------- Was not in symb table
1.620 albertel 13803: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13804: &GDBM_READER(),0640)) {
1.37 www 13805: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 13806: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 13807: unless ($ids) {
13808: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 13809: }
13810: unless ($ids) {
13811: # alias?
13812: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 13813: }
1.37 www 13814: if ($ids) {
13815: # ------------------------------------------------------------------- Has ID(s)
13816: my @possibilities=split(/\,/,$ids);
1.39 www 13817: if ($#possibilities==0) {
13818: # ----------------------------------------------- There is only one possibility
1.37 www 13819: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 13820: $syval=&encode_symb($bighash{'map_id_'.$mapid},
13821: $resid,$thisfn);
1.1172.2.66 raeburn 13822: if (ref($possibles) eq 'HASH') {
1.1172.2.126 raeburn 13823: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13824: $possibles->{$syval} = 1;
13825: }
1.1172.2.66 raeburn 13826: }
13827: if ($checkforblock) {
1.1172.2.126 raeburn 13828: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1172.2.127 raeburn 13829: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1172.2.126 raeburn 13830: if (@blockers) {
13831: $syval = '';
13832: untie(%bighash);
13833: return $env{$cache_str}='';
13834: }
1.1172.2.66 raeburn 13835: }
13836: }
13837: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 13838: # ------------------------------------------ There is more than one possibility
13839: my $realpossible=0;
1.800 albertel 13840: foreach my $id (@possibilities) {
13841: my $file=$bighash{'src_'.$id};
1.1172.2.66 raeburn 13842: my $canaccess;
13843: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13844: $canaccess = 1;
13845: } else {
13846: $canaccess = &allowed('bre',$file);
13847: }
13848: if ($canaccess) {
13849: my ($mapid,$resid)=split(/\./,$id);
13850: if ($bighash{'map_type_'.$mapid} ne 'page') {
13851: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13852: $resid,$thisfn);
1.1172.2.126 raeburn 13853: next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1172.2.127 raeburn 13854: next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1172.2.66 raeburn 13855: if ($checkforblock) {
1.1172.2.127 raeburn 13856: my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1172.2.126 raeburn 13857: if (@blockers > 0) {
13858: $syval = '';
13859: } else {
1.1172.2.66 raeburn 13860: $syval = $poss_syval;
13861: $realpossible++;
13862: }
13863: } else {
13864: $syval = $poss_syval;
13865: $realpossible++;
13866: }
1.1172.2.126 raeburn 13867: if ($syval) {
13868: if (ref($possibles) eq 'HASH') {
13869: $possibles->{$syval} = 1;
13870: }
13871: }
1.1172.2.66 raeburn 13872: }
1.39 www 13873: }
1.191 harris41 13874: }
1.39 www 13875: if ($realpossible!=1) { $syval=''; }
1.249 www 13876: } else {
13877: $syval='';
1.37 www 13878: }
13879: }
1.1172.2.66 raeburn 13880: untie(%bighash);
1.481 raeburn 13881: }
1.31 www 13882: }
1.62 www 13883: if ($syval) {
1.1172.2.128 raeburn 13884: return $env{$cache_str}=$syval;
1.62 www 13885: }
1.31 www 13886: }
1.949 raeburn 13887: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13888: return $env{$cache_str}='';
1.31 www 13889: }
13890:
13891: # ---------------------------------------------------------- Return random seed
13892:
1.32 www 13893: sub numval {
13894: my $txt=shift;
13895: $txt=~tr/A-J/0-9/;
13896: $txt=~tr/a-j/0-9/;
13897: $txt=~tr/K-T/0-9/;
13898: $txt=~tr/k-t/0-9/;
13899: $txt=~tr/U-Z/0-5/;
13900: $txt=~tr/u-z/0-5/;
13901: $txt=~s/\D//g;
1.564 albertel 13902: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 13903: return int($txt);
1.368 albertel 13904: }
13905:
1.484 albertel 13906: sub numval2 {
13907: my $txt=shift;
13908: $txt=~tr/A-J/0-9/;
13909: $txt=~tr/a-j/0-9/;
13910: $txt=~tr/K-T/0-9/;
13911: $txt=~tr/k-t/0-9/;
13912: $txt=~tr/U-Z/0-5/;
13913: $txt=~tr/u-z/0-5/;
13914: $txt=~s/\D//g;
13915: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13916: my $total;
13917: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 13918: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 13919: return int($total);
13920: }
13921:
1.575 albertel 13922: sub numval3 {
13923: use integer;
13924: my $txt=shift;
13925: $txt=~tr/A-J/0-9/;
13926: $txt=~tr/a-j/0-9/;
13927: $txt=~tr/K-T/0-9/;
13928: $txt=~tr/k-t/0-9/;
13929: $txt=~tr/U-Z/0-5/;
13930: $txt=~tr/u-z/0-5/;
13931: $txt=~s/\D//g;
13932: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13933: my $total;
13934: foreach my $val (@txts) { $total+=$val; }
13935: if ($_64bit) { $total=(($total<<32)>>32); }
13936: return $total;
13937: }
13938:
1.675 albertel 13939: sub digest {
13940: my ($data)=@_;
13941: my $digest=&Digest::MD5::md5($data);
13942: my ($a,$b,$c,$d)=unpack("iiii",$digest);
13943: my ($e,$f);
13944: {
13945: use integer;
13946: $e=($a+$b);
13947: $f=($c+$d);
13948: if ($_64bit) {
13949: $e=(($e<<32)>>32);
13950: $f=(($f<<32)>>32);
13951: }
13952: }
13953: if (wantarray) {
13954: return ($e,$f);
13955: } else {
13956: my $g;
13957: {
13958: use integer;
13959: $g=($e+$f);
13960: if ($_64bit) {
13961: $g=(($g<<32)>>32);
13962: }
13963: }
13964: return $g;
13965: }
13966: }
13967:
1.368 albertel 13968: sub latest_rnd_algorithm_id {
1.675 albertel 13969: return '64bit5';
1.366 albertel 13970: }
1.32 www 13971:
1.503 albertel 13972: sub get_rand_alg {
13973: my ($courseid)=@_;
1.790 albertel 13974: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 13975: if ($courseid) {
1.620 albertel 13976: return $env{"course.$courseid.rndseed"};
1.503 albertel 13977: }
13978: return &latest_rnd_algorithm_id();
13979: }
13980:
1.562 albertel 13981: sub validCODE {
13982: my ($CODE)=@_;
13983: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13984: return 0;
13985: }
13986:
1.491 albertel 13987: sub getCODE {
1.620 albertel 13988: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 13989: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13990: defined($Apache::lonhomework::parsing_a_task) ) &&
13991: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 13992: return $Apache::lonhomework::history{'resource.CODE'};
13993: }
13994: return undef;
13995: }
1.1133 foxr 13996: #
13997: # Determines the random seed for a specific context:
13998: #
13999: # parameters:
14000: # symb - in course context the symb for the seed.
14001: # course_id - The course id of the form domain_coursenum.
14002: # domain - Domain for the user.
14003: # course - Course for the user.
14004: # cenv - environment of the course.
14005: #
14006: # NOTE:
14007: # All parameters are picked out of the environment if missing
14008: # or not defined.
14009: # If a symb cannot be determined the current time is used instead.
14010: #
14011: # For a given well defined symb, courside, domain, username,
14012: # and course environment, the seed is reproducible.
14013: #
1.31 www 14014: sub rndseed {
1.1133 foxr 14015: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 14016: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 14017: if (!defined($symb)) {
1.366 albertel 14018: unless ($symb=$wsymb) { return time; }
14019: }
1.1146 foxr 14020: if (!defined $courseid) {
14021: $courseid=$wcourseid;
14022: }
14023: if (!defined $domain) { $domain=$wdomain; }
14024: if (!defined $username) { $username=$wusername }
1.1133 foxr 14025:
14026: my $which;
14027: if (defined($cenv->{'rndseed'})) {
14028: $which = $cenv->{'rndseed'};
14029: } else {
14030: $which =&get_rand_alg($courseid);
14031: }
1.491 albertel 14032: if (defined(&getCODE())) {
1.675 albertel 14033: if ($which eq '64bit5') {
14034: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14035: } elsif ($which eq '64bit4') {
1.575 albertel 14036: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14037: } else {
14038: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14039: }
1.675 albertel 14040: } elsif ($which eq '64bit5') {
14041: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 14042: } elsif ($which eq '64bit4') {
14043: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 14044: } elsif ($which eq '64bit3') {
14045: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 14046: } elsif ($which eq '64bit2') {
14047: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 14048: } elsif ($which eq '64bit') {
14049: return &rndseed_64bit($symb,$courseid,$domain,$username);
14050: }
14051: return &rndseed_32bit($symb,$courseid,$domain,$username);
14052: }
14053:
14054: sub rndseed_32bit {
14055: my ($symb,$courseid,$domain,$username)=@_;
14056: {
14057: use integer;
14058: my $symbchck=unpack("%32C*",$symb) << 27;
14059: my $symbseed=numval($symb) << 22;
14060: my $namechck=unpack("%32C*",$username) << 17;
14061: my $nameseed=numval($username) << 12;
14062: my $domainseed=unpack("%32C*",$domain) << 7;
14063: my $courseseed=unpack("%32C*",$courseid);
14064: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 14065: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14066: #&logthis("rndseed :$num:$symb");
1.564 albertel 14067: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 14068: return $num;
14069: }
14070: }
14071:
14072: sub rndseed_64bit {
14073: my ($symb,$courseid,$domain,$username)=@_;
14074: {
14075: use integer;
14076: my $symbchck=unpack("%32S*",$symb) << 21;
14077: my $symbseed=numval($symb) << 10;
14078: my $namechck=unpack("%32S*",$username);
14079:
14080: my $nameseed=numval($username) << 21;
14081: my $domainseed=unpack("%32S*",$domain) << 10;
14082: my $courseseed=unpack("%32S*",$courseid);
14083:
14084: my $num1=$symbchck+$symbseed+$namechck;
14085: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14086: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14087: #&logthis("rndseed :$num:$symb");
1.564 albertel 14088: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 14089: return "$num1,$num2";
1.155 albertel 14090: }
1.366 albertel 14091: }
14092:
1.443 albertel 14093: sub rndseed_64bit2 {
14094: my ($symb,$courseid,$domain,$username)=@_;
14095: {
14096: use integer;
14097: # strings need to be an even # of cahracters long, it it is odd the
14098: # last characters gets thrown away
14099: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14100: my $symbseed=numval($symb) << 10;
14101: my $namechck=unpack("%32S*",$username.' ');
14102:
14103: my $nameseed=numval($username) << 21;
1.501 albertel 14104: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14105: my $courseseed=unpack("%32S*",$courseid.' ');
14106:
14107: my $num1=$symbchck+$symbseed+$namechck;
14108: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14109: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14110: #&logthis("rndseed :$num:$symb");
1.803 albertel 14111: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 14112: return "$num1,$num2";
14113: }
14114: }
14115:
14116: sub rndseed_64bit3 {
14117: my ($symb,$courseid,$domain,$username)=@_;
14118: {
14119: use integer;
14120: # strings need to be an even # of cahracters long, it it is odd the
14121: # last characters gets thrown away
14122: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14123: my $symbseed=numval2($symb) << 10;
14124: my $namechck=unpack("%32S*",$username.' ');
14125:
14126: my $nameseed=numval2($username) << 21;
1.443 albertel 14127: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14128: my $courseseed=unpack("%32S*",$courseid.' ');
14129:
14130: my $num1=$symbchck+$symbseed+$namechck;
14131: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14132: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14133: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 14134: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14135:
1.503 albertel 14136: return "$num1:$num2";
1.443 albertel 14137: }
14138: }
14139:
1.575 albertel 14140: sub rndseed_64bit4 {
14141: my ($symb,$courseid,$domain,$username)=@_;
14142: {
14143: use integer;
14144: # strings need to be an even # of cahracters long, it it is odd the
14145: # last characters gets thrown away
14146: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14147: my $symbseed=numval3($symb) << 10;
14148: my $namechck=unpack("%32S*",$username.' ');
14149:
14150: my $nameseed=numval3($username) << 21;
14151: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14152: my $courseseed=unpack("%32S*",$courseid.' ');
14153:
14154: my $num1=$symbchck+$symbseed+$namechck;
14155: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14156: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14157: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 14158: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14159:
1.575 albertel 14160: return "$num1:$num2";
14161: }
14162: }
14163:
1.675 albertel 14164: sub rndseed_64bit5 {
14165: my ($symb,$courseid,$domain,$username)=@_;
14166: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14167: return "$num1:$num2";
14168: }
14169:
1.366 albertel 14170: sub rndseed_CODE_64bit {
14171: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 14172: {
1.366 albertel 14173: use integer;
1.443 albertel 14174: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 14175: my $symbseed=numval2($symb);
1.491 albertel 14176: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14177: my $CODEseed=numval(&getCODE());
1.443 albertel 14178: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 14179: my $num1=$symbseed+$CODEchck;
14180: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14181: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14182: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 14183: if ($_64bit) { $num1=(($num1<<32)>>32); }
14184: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 14185: return "$num1:$num2";
1.366 albertel 14186: }
14187: }
14188:
1.575 albertel 14189: sub rndseed_CODE_64bit4 {
14190: my ($symb,$courseid,$domain,$username)=@_;
14191: {
14192: use integer;
14193: my $symbchck=unpack("%32S*",$symb.' ') << 16;
14194: my $symbseed=numval3($symb);
14195: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14196: my $CODEseed=numval3(&getCODE());
14197: my $courseseed=unpack("%32S*",$courseid.' ');
14198: my $num1=$symbseed+$CODEchck;
14199: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14200: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14201: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 14202: if ($_64bit) { $num1=(($num1<<32)>>32); }
14203: if ($_64bit) { $num2=(($num2<<32)>>32); }
14204: return "$num1:$num2";
14205: }
14206: }
14207:
1.675 albertel 14208: sub rndseed_CODE_64bit5 {
14209: my ($symb,$courseid,$domain,$username)=@_;
14210: my $code = &getCODE();
14211: my ($num1,$num2)=&digest("$symb,$courseid,$code");
14212: return "$num1:$num2";
14213: }
14214:
1.366 albertel 14215: sub setup_random_from_rndseed {
14216: my ($rndseed)=@_;
1.503 albertel 14217: if ($rndseed =~/([,:])/) {
1.1172.2.51 raeburn 14218: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14219: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14220: &Math::Random::random_set_seed_from_phrase($rndseed);
14221: } else {
14222: &Math::Random::random_set_seed($num1,$num2);
14223: }
1.366 albertel 14224: } else {
14225: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 14226: }
1.36 albertel 14227: }
14228:
1.474 albertel 14229: sub latest_receipt_algorithm_id {
1.835 albertel 14230: return 'receipt3';
1.474 albertel 14231: }
14232:
1.480 www 14233: sub recunique {
14234: my $fucourseid=shift;
14235: my $unique;
1.835 albertel 14236: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14237: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14238: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 14239: } else {
14240: $unique=$perlvar{'lonReceipt'};
14241: }
14242: return unpack("%32C*",$unique);
14243: }
14244:
14245: sub recprefix {
14246: my $fucourseid=shift;
14247: my $prefix;
1.835 albertel 14248: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14249: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14250: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 14251: } else {
14252: $prefix=$perlvar{'lonHostID'};
14253: }
14254: return unpack("%32C*",$prefix);
14255: }
14256:
1.76 www 14257: sub ireceipt {
1.474 albertel 14258: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 14259:
14260: my $return =&recprefix($fucourseid).'-';
14261:
14262: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14263: $env{'request.state'} eq 'construct') {
14264: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14265: return $return;
14266: }
14267:
1.76 www 14268: my $cuname=unpack("%32C*",$funame);
14269: my $cudom=unpack("%32C*",$fudom);
14270: my $cucourseid=unpack("%32C*",$fucourseid);
14271: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 14272: my $cunique=&recunique($fucourseid);
1.474 albertel 14273: my $cpart=unpack("%32S*",$part);
1.835 albertel 14274: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14275:
1.790 albertel 14276: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 14277:
14278: $return.= ($cunique%$cuname+
14279: $cunique%$cudom+
14280: $cusymb%$cuname+
14281: $cusymb%$cudom+
14282: $cucourseid%$cuname+
14283: $cucourseid%$cudom+
14284: $cpart%$cuname+
14285: $cpart%$cudom);
14286: } else {
14287: $return.= ($cunique%$cuname+
14288: $cunique%$cudom+
14289: $cusymb%$cuname+
14290: $cusymb%$cudom+
14291: $cucourseid%$cuname+
14292: $cucourseid%$cudom);
14293: }
14294: return $return;
1.76 www 14295: }
14296:
14297: sub receipt {
1.474 albertel 14298: my ($part)=@_;
1.790 albertel 14299: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 14300: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 14301: }
1.260 ng 14302:
1.790 albertel 14303: sub whichuser {
14304: my ($passedsymb)=@_;
14305: my ($symb,$courseid,$domain,$name,$publicuser);
14306: if (defined($env{'form.grade_symb'})) {
14307: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14308: my $allowed=&allowed('vgr',$tmp_courseid);
14309: if (!$allowed &&
14310: exists($env{'request.course.sec'}) &&
14311: $env{'request.course.sec'} !~ /^\s*$/) {
14312: $allowed=&allowed('vgr',$tmp_courseid.
14313: '/'.$env{'request.course.sec'});
14314: }
14315: if ($allowed) {
14316: ($symb)=&get_env_multiple('form.grade_symb');
14317: $courseid=$tmp_courseid;
14318: ($domain)=&get_env_multiple('form.grade_domain');
14319: ($name)=&get_env_multiple('form.grade_username');
14320: return ($symb,$courseid,$domain,$name,$publicuser);
14321: }
14322: }
14323: if (!$passedsymb) {
14324: $symb=&symbread();
14325: } else {
14326: $symb=$passedsymb;
14327: }
14328: $courseid=$env{'request.course.id'};
14329: $domain=$env{'user.domain'};
14330: $name=$env{'user.name'};
14331: if ($name eq 'public' && $domain eq 'public') {
14332: if (!defined($env{'form.username'})) {
14333: $env{'form.username'}.=time.rand(10000000);
14334: }
14335: $name.=$env{'form.username'};
14336: }
14337: return ($symb,$courseid,$domain,$name,$publicuser);
14338:
14339: }
14340:
1.36 albertel 14341: # ------------------------------------------------------------ Serves up a file
1.472 albertel 14342: # returns either the contents of the file or
14343: # -1 if the file doesn't exist
1.481 raeburn 14344: #
14345: # if the target is a file that was uploaded via DOCS,
14346: # a check will be made to see if a current copy exists on the local server,
14347: # if it does this will be served, otherwise a copy will be retrieved from
14348: # the home server for the course and stored in /home/httpd/html/userfiles on
14349: # the local server.
1.472 albertel 14350:
1.36 albertel 14351: sub getfile {
1.538 albertel 14352: my ($file) = @_;
1.609 banghart 14353: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 14354: &repcopy($file);
14355: return &readfile($file);
14356: }
14357:
14358: sub repcopy_userfile {
14359: my ($file)=@_;
1.1142 raeburn 14360: my $londocroot = $perlvar{'lonDocRoot'};
14361: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 14362: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 14363: my ($cdom,$cnum,$filename) =
1.811 albertel 14364: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 14365: my $uri="/uploaded/$cdom/$cnum/$filename";
14366: if (-e "$file") {
1.828 www 14367: # we already have a local copy, check it out
1.538 albertel 14368: my @fileinfo = stat($file);
1.828 www 14369: my $rtncode;
14370: my $info;
1.538 albertel 14371: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 14372: if ($lwpresp ne 'ok') {
1.828 www 14373: # there is no such file anymore, even though we had a local copy
1.482 albertel 14374: if ($rtncode eq '404') {
1.538 albertel 14375: unlink($file);
1.482 albertel 14376: }
14377: return -1;
14378: }
14379: if ($info < $fileinfo[9]) {
1.828 www 14380: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 14381: return 'ok';
1.828 www 14382: } else {
14383: # the file is outdated, get rid of it
14384: unlink($file);
1.482 albertel 14385: }
1.828 www 14386: }
14387: # one way or the other, at this point, we don't have the file
14388: # construct the correct path for the file
14389: my @parts = ($cdom,$cnum);
14390: if ($filename =~ m|^(.+)/[^/]+$|) {
14391: push @parts, split(/\//,$1);
14392: }
14393: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14394: foreach my $part (@parts) {
14395: $path .= '/'.$part;
14396: if (!-e $path) {
14397: mkdir($path,0770);
1.482 albertel 14398: }
14399: }
1.828 www 14400: # now the path exists for sure
14401: # get a user agent
14402: my $ua=new LWP::UserAgent;
14403: my $transferfile=$file.'.in.transfer';
14404: # FIXME: this should flock
14405: if (-e $transferfile) { return 'ok'; }
14406: my $request;
14407: $uri=~s/^\///;
1.980 raeburn 14408: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14409: my $hostname = &hostname($homeserver);
1.980 raeburn 14410: my $protocol = $protocol{$homeserver};
14411: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14412: $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.828 www 14413: my $response=$ua->request($request,$transferfile);
14414: # did it work?
14415: if ($response->is_error()) {
14416: unlink($transferfile);
14417: &logthis("Userfile repcopy failed for $uri");
14418: return -1;
14419: }
14420: # worked, rename the transfer file
14421: rename($transferfile,$file);
1.607 raeburn 14422: return 'ok';
1.481 raeburn 14423: }
14424:
1.517 albertel 14425: sub tokenwrapper {
14426: my $uri=shift;
1.980 raeburn 14427: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 14428: $uri=~s|^/||;
1.620 albertel 14429: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 14430: my $token=$1;
1.552 albertel 14431: my (undef,$udom,$uname,$file)=split('/',$uri,4);
14432: if ($udom && $uname && $file) {
14433: $file=~s|(\?\.*)*$||;
1.949 raeburn 14434: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 14435: my $homeserver = &homeserver($uname,$udom);
1.1172.2.120 raeburn 14436: my $hostname = &hostname($homeserver);
1.980 raeburn 14437: my $protocol = $protocol{$homeserver};
14438: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14439: return $protocol.'://'.$hostname.'/'.$uri.
1.517 albertel 14440: (($uri=~/\?/)?'&':'?').'token='.$token.
14441: '&tokenissued='.$perlvar{'lonHostID'};
14442: } else {
14443: return '/adm/notfound.html';
14444: }
14445: }
14446:
1.828 www 14447: # call with reqtype HEAD: get last modification time
14448: # call with reqtype GET: get the file contents
14449: # Do not call this with reqtype GET for large files! It loads everything into memory
14450: #
1.481 raeburn 14451: sub getuploaded {
14452: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14453: $uri=~s/^\///;
1.980 raeburn 14454: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14455: my $hostname = &hostname($homeserver);
1.980 raeburn 14456: my $protocol = $protocol{$homeserver};
14457: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14458: $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481 raeburn 14459: my $ua=new LWP::UserAgent;
14460: my $request=new HTTP::Request($reqtype,$uri);
14461: my $response=$ua->request($request);
14462: $$rtncode = $response->code;
1.482 albertel 14463: if (! $response->is_success()) {
14464: return 'failed';
14465: }
14466: if ($reqtype eq 'HEAD') {
1.486 www 14467: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 14468: } elsif ($reqtype eq 'GET') {
14469: $$info = $response->content;
1.472 albertel 14470: }
1.482 albertel 14471: return 'ok';
1.36 albertel 14472: }
14473:
1.481 raeburn 14474: sub readfile {
14475: my $file = shift;
14476: if ( (! -e $file ) || ($file eq '') ) { return -1; };
14477: my $fh;
1.1172.2.96 raeburn 14478: open($fh,"<",$file);
1.481 raeburn 14479: my $a='';
1.800 albertel 14480: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 14481: return $a;
14482: }
14483:
1.36 albertel 14484: sub filelocation {
1.590 banghart 14485: my ($dir,$file) = @_;
14486: my $location;
14487: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 14488:
14489: if ($file =~ m-^/adm/-) {
14490: $file=~s-^/adm/wrapper/-/-;
14491: $file=~s-^/adm/coursedocs/showdoc/-/-;
14492: }
1.882 albertel 14493:
1.1139 www 14494: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 14495: $location = $file;
1.609 banghart 14496: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 14497: my ($udom,$uname,$filename)=
1.811 albertel 14498: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 14499: my $home=&homeserver($uname,$udom);
14500: my $is_me=0;
14501: my @ids=¤t_machine_ids();
14502: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14503: if ($is_me) {
1.1117 foxr 14504: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 14505: } else {
14506: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14507: $udom.'/'.$uname.'/'.$filename;
14508: }
1.882 albertel 14509: } elsif ($file =~ m-^/adm/-) {
14510: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 14511: } else {
14512: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 14513: $file=~s:^/(res|priv)/:/:;
14514: my $space=$1;
1.590 banghart 14515: if ( !( $file =~ m:^/:) ) {
14516: $location = $dir. '/'.$file;
14517: } else {
1.1142 raeburn 14518: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 14519: }
1.59 albertel 14520: }
1.590 banghart 14521: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 14522: while ($location=~m{/\.\./}) {
14523: if ($location =~ m{/[^/]+/\.\./}) {
14524: $location=~ s{/[^/]+/\.\./}{/}g;
14525: } else {
14526: $location=~ s{/\.\./}{/}g;
14527: }
14528: } #remove dir/..
1.590 banghart 14529: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14530: return $location;
1.46 www 14531: }
1.36 albertel 14532:
1.46 www 14533: sub hreflocation {
14534: my ($dir,$file)=@_;
1.980 raeburn 14535: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 14536: $file=filelocation($dir,$file);
1.700 albertel 14537: } elsif ($file=~m-^/adm/-) {
14538: $file=~s-^/adm/wrapper/-/-;
14539: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 14540: }
14541: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14542: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14543: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 14544: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14545: {/uploaded/$1/$2/}x;
1.46 www 14546: }
1.913 albertel 14547: if ($file=~ m{^/userfiles/}) {
14548: $file =~ s{^/userfiles/}{/uploaded/};
14549: }
1.462 albertel 14550: return $file;
1.465 albertel 14551: }
14552:
1.1139 www 14553:
14554:
14555:
14556:
1.465 albertel 14557: sub current_machine_domains {
1.853 albertel 14558: return &machine_domains(&hostname($perlvar{'lonHostID'}));
14559: }
14560:
14561: sub machine_domains {
14562: my ($hostname) = @_;
1.465 albertel 14563: my @domains;
1.838 albertel 14564: my %hostname = &all_hostnames();
1.465 albertel 14565: while( my($id, $name) = each(%hostname)) {
1.467 matthew 14566: # &logthis("-$id-$name-$hostname-");
1.465 albertel 14567: if ($hostname eq $name) {
1.844 albertel 14568: push(@domains,&host_domain($id));
1.465 albertel 14569: }
14570: }
14571: return @domains;
14572: }
14573:
14574: sub current_machine_ids {
1.853 albertel 14575: return &machine_ids(&hostname($perlvar{'lonHostID'}));
14576: }
14577:
14578: sub machine_ids {
14579: my ($hostname) = @_;
14580: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 14581: my @ids;
1.888 albertel 14582: my %name_to_host = &all_names();
1.889 albertel 14583: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14584: return @{ $name_to_host{$hostname} };
14585: }
14586: return;
1.31 www 14587: }
14588:
1.824 raeburn 14589: sub additional_machine_domains {
14590: my @domains;
1.1172.2.142 raeburn 14591: if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14592: if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14593: while( my $line = <$fh>) {
14594: chomp($line);
14595: $line =~ s/\s//g;
14596: push(@domains,$line);
14597: }
14598: close($fh);
14599: }
1.824 raeburn 14600: }
14601: return @domains;
14602: }
14603:
14604: sub default_login_domain {
14605: my $domain = $perlvar{'lonDefDomain'};
14606: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14607: foreach my $posdom (¤t_machine_domains(),
14608: &additional_machine_domains()) {
14609: if (lc($posdom) eq lc($testdomain)) {
14610: $domain=$posdom;
14611: last;
14612: }
14613: }
14614: return $domain;
14615: }
14616:
1.1172.2.106 raeburn 14617: sub shared_institution {
1.1172.2.136 raeburn 14618: my ($dom,$lonhost) = @_;
14619: if ($lonhost eq '') {
14620: $lonhost = $perlvar{'lonHostID'};
14621: }
1.1172.2.106 raeburn 14622: my $same_intdom;
1.1172.2.136 raeburn 14623: my $hostintdom = &internet_dom($lonhost);
1.1172.2.106 raeburn 14624: if ($hostintdom ne '') {
14625: my %iphost = &get_iphost();
14626: my $primary_id = &domain($dom,'primary');
14627: my $primary_ip = &get_host_ip($primary_id);
14628: if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14629: foreach my $id (@{$iphost{$primary_ip}}) {
14630: my $intdom = &internet_dom($id);
14631: if ($intdom eq $hostintdom) {
14632: $same_intdom = 1;
14633: last;
14634: }
14635: }
14636: }
14637: }
14638: return $same_intdom;
14639: }
14640:
1.1172.2.120 raeburn 14641: sub uses_sts {
14642: my ($ignore_cache) = @_;
14643: my $lonhost = $perlvar{'lonHostID'};
14644: my $hostname = &hostname($lonhost);
14645: my $sts_on;
14646: if ($protocol{$lonhost} eq 'https') {
14647: my $cachetime = 12*3600;
14648: if (!$ignore_cache) {
14649: ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14650: if (defined($cached)) {
14651: return $sts_on;
14652: }
14653: }
1.1172.2.121 raeburn 14654: my $ua=new LWP::UserAgent;
1.1172.2.120 raeburn 14655: my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14656: my $request=new HTTP::Request('HEAD',$url);
1.1172.2.121 raeburn 14657: my $response=$ua->request($request);
1.1172.2.120 raeburn 14658: if ($response->is_success) {
14659: my $has_sts = $response->header('Strict-Transport-Security');
14660: if ($has_sts eq '') {
14661: $sts_on = 0;
14662: } else {
14663: if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14664: my $maxage = $1;
14665: if ($maxage) {
14666: $sts_on = 1;
14667: } else {
14668: $sts_on = 0;
14669: }
14670: } else {
14671: $sts_on = 0;
14672: }
14673: }
14674: return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14675: }
14676: }
14677: return;
14678: }
14679:
1.1172.2.142 raeburn 14680: sub waf_allssl {
14681: my ($host_name) = @_;
14682: my $alias = &get_proxy_alias();
14683: if ($host_name eq '') {
14684: $host_name = $ENV{'SERVER_NAME'};
14685: }
14686: if (($host_name ne '') && ($alias eq $host_name)) {
14687: my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14688: my %defdomdefaults = &get_domain_defaults($serverhomedom);
14689: if ($defdomdefaults{'waf_sslopt'}) {
14690: return $defdomdefaults{'waf_sslopt'};
14691: }
14692: }
14693: return;
14694: }
14695:
1.1172.2.134 raeburn 14696: sub get_requestor_ip {
14697: my ($r,$nolookup,$noproxy) = @_;
14698: my $from_ip;
14699: if (ref($r)) {
1.1172.2.142 raeburn 14700: if ($r->can('useragent_ip')) {
14701: if ($noproxy && $r->can('client_ip')) {
14702: $from_ip = $r->client_ip();
14703: } else {
14704: $from_ip = $r->useragent_ip();
14705: }
14706: } elsif ($r->connection->can('remote_ip')) {
14707: $from_ip = $r->connection->remote_ip();
14708: } else {
14709: $from_ip = $r->get_remote_host($nolookup);
14710: }
1.1172.2.134 raeburn 14711: } else {
14712: $from_ip = $ENV{'REMOTE_ADDR'};
14713: }
1.1172.2.142 raeburn 14714: return $from_ip if ($noproxy);
14715: # Who controls proxy settings for server
14716: my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14717: my $proxyinfo = &get_proxy_settings($dom_in_use);
14718: if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14719: if ($proxyinfo->{'vpnint'}) {
14720: if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14721: return $from_ip;
14722: }
14723: }
14724: if ($proxyinfo->{'trusted'}) {
14725: if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14726: my $ipheader = $proxyinfo->{'ipheader'};
14727: my ($ip,$xfor);
14728: if (ref($r)) {
14729: if ($ipheader) {
14730: $ip = $r->headers_in->{$ipheader};
14731: }
14732: $xfor = $r->headers_in->{'X-Forwarded-For'};
14733: } else {
14734: if ($ipheader) {
14735: $ip = $ENV{'HTTP_'.uc($ipheader)};
14736: }
14737: $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14738: }
14739: if (($ip eq '') && ($xfor ne '')) {
14740: foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14741: unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14742: $ip = $poss_ip;
14743: last;
14744: }
14745: }
14746: }
14747: if ($ip ne '') {
14748: return $ip;
14749: }
14750: }
14751: }
14752: }
1.1172.2.134 raeburn 14753: return $from_ip;
14754: }
14755:
1.1172.2.142 raeburn 14756: sub get_proxy_settings {
14757: my ($dom_in_use) = @_;
14758: my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14759: my $proxyinfo = {
14760: ipheader => $domdefaults{'waf_ipheader'},
14761: trusted => $domdefaults{'waf_trusted'},
14762: vpnint => $domdefaults{'waf_vpnint'},
14763: vpnext => $domdefaults{'waf_vpnext'},
14764: sslopt => $domdefaults{'waf_sslopt'},
14765: };
14766: return $proxyinfo;
14767: }
14768:
14769: sub ip_match {
14770: my ($ip,$pattern_str) = @_;
14771: $ip=Net::CIDR::cidrvalidate($ip);
14772: if ($ip) {
14773: return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14774: }
14775: return;
14776: }
14777:
14778: sub get_proxy_alias {
14779: my ($lonid) = @_;
14780: if ($lonid eq '') {
14781: $lonid = $perlvar{'lonHostID'};
14782: }
14783: if (!defined(&hostname($lonid))) {
14784: return;
14785: }
14786: if ($lonid ne '') {
14787: my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14788: if ($cached) {
14789: return $alias;
14790: }
14791: my $dom = &Apache::lonnet::host_domain($lonid);
14792: if ($dom ne '') {
14793: my $cachetime = 60*60*24;
14794: my %domconfig =
14795: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14796: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14797: if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14798: $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14799: }
14800: }
14801: return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14802: }
14803: }
14804: return;
14805: }
14806:
14807: sub use_proxy_alias {
14808: my ($r,$lonid) = @_;
14809: my $alias = &get_proxy_alias($lonid);
14810: if ($alias) {
14811: my $dom = &host_domain($lonid);
14812: if ($dom ne '') {
14813: my $proxyinfo = &get_proxy_settings($dom);
14814: my ($vpnint,$remote_ip);
14815: if (ref($proxyinfo) eq 'HASH') {
14816: $vpnint = $proxyinfo->{'vpnint'};
14817: if ($vpnint) {
14818: $remote_ip = &get_requestor_ip($r,1,1);
14819: }
14820: }
14821: unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14822: return $alias;
14823: }
14824: }
14825: }
14826: return;
14827: }
14828:
14829: sub alias_sso {
14830: my ($lonid) = @_;
14831: if ($lonid eq '') {
14832: $lonid = $perlvar{'lonHostID'};
14833: }
14834: if (!defined(&hostname($lonid))) {
14835: return;
14836: }
14837: if ($lonid ne '') {
14838: my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14839: if ($cached) {
14840: return $use_alias;
14841: }
14842: my $dom = &Apache::lonnet::host_domain($lonid);
14843: if ($dom ne '') {
14844: my $cachetime = 60*60*24;
14845: my %domconfig =
14846: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14847: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14848: if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14849: $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14850: }
14851: }
14852: return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14853: }
14854: }
14855: return;
14856: }
14857:
14858: sub get_saml_landing {
14859: my ($lonid) = @_;
14860: if ($lonid eq '') {
14861: my $defdom = &default_login_domain();
14862: my @hosts = ¤t_machine_ids();
14863: if (@hosts > 1) {
14864: foreach my $hostid (@hosts) {
14865: if (&host_domain($hostid) eq $defdom) {
14866: $lonid = $hostid;
14867: last;
14868: }
14869: }
14870: } else {
14871: $lonid = $perlvar{'lonHostID'};
14872: }
14873: if ($lonid) {
14874: unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14875: return;
14876: }
14877: } else {
14878: return;
14879: }
14880: } elsif (!defined(&hostname($lonid))) {
14881: return;
14882: }
14883: my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14884: if ($cached) {
14885: return $landing;
14886: }
14887: my $dom = &Apache::lonnet::host_domain($lonid);
14888: if ($dom ne '') {
14889: my $cachetime = 60*60*24;
14890: my %domconfig =
14891: &Apache::lonnet::get_dom('configuration',['login'],$dom);
14892: if (ref($domconfig{'login'}) eq 'HASH') {
14893: if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14894: if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14895: $landing = 1;
14896: }
14897: }
14898: }
14899: return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14900: }
14901: return;
14902: }
14903:
1.31 www 14904: # ------------------------------------------------------------- Declutters URLs
14905:
14906: sub declutter {
14907: my $thisfn=shift;
1.569 albertel 14908: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1172.2.49 raeburn 14909: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14910: $thisfn=~s{^/home/httpd/html}{};
14911: }
1.31 www 14912: $thisfn=~s/^\///;
1.697 albertel 14913: $thisfn=~s|^adm/wrapper/||;
14914: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 14915: $thisfn=~s/^res\///;
1.1172 bisitz 14916: $thisfn=~s/^priv\///;
1.1032 raeburn 14917: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14918: $thisfn=~s/\?.+$//;
14919: }
1.268 www 14920: return $thisfn;
14921: }
14922:
14923: # ------------------------------------------------------------- Clutter up URLs
14924:
14925: sub clutter {
14926: my $thisfn='/'.&declutter(shift);
1.887 albertel 14927: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 14928: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 14929: $thisfn='/res'.$thisfn;
14930: }
1.1031 raeburn 14931: if ($thisfn !~m|^/adm|) {
14932: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 14933: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 14934: } else {
14935: my ($ext) = ($thisfn =~ /\.(\w+)$/);
14936: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 14937: if ($embstyle eq 'ssi'
14938: || ($embstyle eq 'hdn')
14939: || ($embstyle eq 'rat')
14940: || ($embstyle eq 'prv')
14941: || ($embstyle eq 'ign')) {
14942: #do nothing with these
14943: } elsif (($embstyle eq 'img')
1.695 albertel 14944: || ($embstyle eq 'emb')
14945: || ($embstyle eq 'wrp')) {
14946: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 14947: } elsif ($embstyle eq 'unk'
14948: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 14949: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 14950: } else {
1.718 www 14951: # &logthis("Got a blank emb style");
1.695 albertel 14952: }
1.694 albertel 14953: }
1.1172.2.146. .1(raebu 14954:22): } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14955:22): $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 14956: }
1.31 www 14957: return $thisfn;
1.12 www 14958: }
14959:
1.787 albertel 14960: sub clutter_with_no_wrapper {
14961: my $uri = &clutter(shift);
14962: if ($uri =~ m-^/adm/-) {
14963: $uri =~ s-^/adm/wrapper/-/-;
14964: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
14965: }
14966: return $uri;
14967: }
14968:
1.557 albertel 14969: sub freeze_escape {
14970: my ($value)=@_;
14971: if (ref($value)) {
14972: $value=&nfreeze($value);
14973: return '__FROZEN__'.&escape($value);
14974: }
14975: return &escape($value);
14976: }
14977:
1.11 www 14978:
1.557 albertel 14979: sub thaw_unescape {
14980: my ($value)=@_;
14981: if ($value =~ /^__FROZEN__/) {
14982: substr($value,0,10,undef);
14983: $value=&unescape($value);
14984: return &thaw($value);
14985: }
14986: return &unescape($value);
14987: }
14988:
1.436 albertel 14989: sub correct_line_ends {
14990: my ($result)=@_;
14991: $$result =~s/\r\n/\n/mg;
14992: $$result =~s/\r/\n/mg;
1.415 albertel 14993: }
1.1 albertel 14994: # ================================================================ Main Program
14995:
1.184 www 14996: sub goodbye {
1.204 albertel 14997: &logthis("Starting Shut down");
1.443 albertel 14998: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 14999: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 15000: #converted
1.599 albertel 15001: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 15002: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
15003: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
15004: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 15005: #1.1 only
1.870 albertel 15006: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
15007: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
15008: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
15009: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
15010: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 15011: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
15012: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 15013: &flushcourselogs();
15014: &logthis("Shutting down");
15015: }
15016:
1.852 albertel 15017: sub get_dns {
1.1172.2.17 raeburn 15018: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 15019: if (!$ignore_cache) {
15020: my ($content,$cached)=
15021: &Apache::lonnet::is_cached_new('dns',$url);
15022: if ($cached) {
1.1172.2.17 raeburn 15023: &$func($content,$hashref);
1.869 albertel 15024: return;
15025: }
15026: }
15027:
15028: my %alldns;
1.1172.2.96 raeburn 15029: if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15030: foreach my $dns (<$config>) {
15031: next if ($dns !~ /^\^(\S*)/x);
15032: my $line = $1;
15033: my ($host,$protocol) = split(/:/,$line);
15034: if ($protocol ne 'https') {
15035: $protocol = 'http';
15036: }
15037: $alldns{$host} = $protocol;
1.979 raeburn 15038: }
1.1172.2.96 raeburn 15039: close($config);
1.869 albertel 15040: }
15041: while (%alldns) {
1.1172.2.52 raeburn 15042: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1172.2.142 raeburn 15043: my @content;
15044: if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15045: my $command = (split('/',$url))[3];
15046: my ($dir,$file) = &parse_getdns_url($command,$url);
15047: delete($alldns{$dns});
15048: next if (($dir eq '') || ($file eq ''));
15049: if (open(my $config,'<',"$dir/$file")) {
15050: @content = <$config>;
15051: close($config);
15052: }
15053: } else {
15054: my $ua=new LWP::UserAgent;
15055: $ua->timeout(30);
15056: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15057: my $response=$ua->request($request);
15058: delete($alldns{$dns});
15059: next if ($response->is_error());
15060: @content = split("\n",$response->content);
15061: }
1.1172.2.17 raeburn 15062: unless ($nocache) {
1.1172.2.23 raeburn 15063: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1172.2.17 raeburn 15064: }
15065: &$func(\@content,$hashref);
1.869 albertel 15066: return;
1.852 albertel 15067: }
1.871 albertel 15068: my $which = (split('/',$url))[3];
15069: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
1.1172.2.105 raeburn 15070: if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15071: my @content = <$config>;
15072: &$func(\@content,$hashref);
15073: }
1.1172.2.17 raeburn 15074: return;
15075: }
15076:
15077: # ------------------------------------------------------Get DNS checksums file
15078: sub parse_dns_checksums_tab {
15079: my ($lines,$hashref) = @_;
1.1172.2.53 raeburn 15080: my $lonhost = $perlvar{'lonHostID'};
15081: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1172.2.17 raeburn 15082: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1172.2.53 raeburn 15083: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15084: my $webconfdir = '/etc/httpd/conf';
15085: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15086: $webconfdir = '/etc/apache2';
15087: } elsif ($distro =~ /^sles(\d+)$/) {
15088: if ($1 >= 10) {
15089: $webconfdir = '/etc/apache2';
15090: }
15091: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15092: if ($1 >= 10.0) {
15093: $webconfdir = '/etc/apache2';
15094: }
15095: }
1.1172.2.17 raeburn 15096: my ($release,$timestamp) = split(/\-/,$loncaparev);
15097: my (%chksum,%revnum);
15098: if (ref($lines) eq 'ARRAY') {
15099: chomp(@{$lines});
1.1172.2.34 raeburn 15100: my $version = shift(@{$lines});
15101: if ($version eq $release) {
1.1172.2.17 raeburn 15102: foreach my $line (@{$lines}) {
1.1172.2.34 raeburn 15103: my ($file,$version,$shasum) = split(/,/,$line);
1.1172.2.53 raeburn 15104: if ($file =~ m{^/etc/httpd/conf}) {
15105: if ($webconfdir eq '/etc/apache2') {
15106: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15107: }
15108: }
1.1172.2.34 raeburn 15109: $chksum{$file} = $shasum;
15110: $revnum{$file} = $version;
1.1172.2.17 raeburn 15111: }
15112: if (ref($hashref) eq 'HASH') {
15113: %{$hashref} = (
15114: sums => \%chksum,
15115: versions => \%revnum,
15116: );
15117: }
15118: }
15119: }
1.869 albertel 15120: return;
1.852 albertel 15121: }
1.1172.2.17 raeburn 15122:
15123: sub fetch_dns_checksums {
15124: my %checksums;
1.1172.2.34 raeburn 15125: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1172.2.48 raeburn 15126: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1172.2.34 raeburn 15127: my ($release,$timestamp) = split(/\-/,$loncaparev);
15128: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1172.2.17 raeburn 15129: \%checksums);
15130: return \%checksums;
15131: }
15132:
1.1172.2.142 raeburn 15133: sub parse_getdns_url {
15134: my ($command,$url) = @_;
15135: my $dir = $perlvar{'lonTabDir'};
15136: my $file;
15137: if ($command eq 'hosts') {
15138: $file = 'dns_hosts.tab';
15139: } elsif ($command eq 'domain') {
15140: $file = 'dns_domain.tab';
15141: } elsif ($command eq 'checksums') {
15142: my $version = (split('/',$url))[4];
15143: $file = "dns_checksums/$version.tab",
15144: }
15145: return ($dir,$file);
15146: }
15147:
1.327 albertel 15148: # ------------------------------------------------------------ Read domain file
15149: {
1.852 albertel 15150: my $loaded;
1.846 albertel 15151: my %domain;
15152:
1.852 albertel 15153: sub parse_domain_tab {
15154: my ($lines) = @_;
15155: foreach my $line (@$lines) {
15156: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 15157:
1.846 albertel 15158: chomp($line);
1.852 albertel 15159: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 15160: my %this_domain;
15161: foreach my $field ('description', 'auth_def', 'auth_arg_def',
15162: 'lang_def', 'city', 'longi', 'lati',
15163: 'primary') {
15164: $this_domain{$field} = shift(@elements);
15165: }
15166: $domain{$name} = \%this_domain;
1.852 albertel 15167: }
15168: }
1.864 albertel 15169:
15170: sub reset_domain_info {
15171: undef($loaded);
15172: undef(%domain);
15173: }
15174:
1.852 albertel 15175: sub load_domain_tab {
1.1172.2.70 raeburn 15176: my ($ignore_cache,$nocache) = @_;
15177: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 15178: my $fh;
1.1172.2.96 raeburn 15179: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852 albertel 15180: my @lines = <$fh>;
15181: &parse_domain_tab(\@lines);
1.448 albertel 15182: }
1.852 albertel 15183: close($fh);
15184: $loaded = 1;
1.327 albertel 15185: }
1.846 albertel 15186:
15187: sub domain {
1.852 albertel 15188: &load_domain_tab() if (!$loaded);
15189:
1.846 albertel 15190: my ($name,$what) = @_;
15191: return if ( !exists($domain{$name}) );
15192:
15193: if (!$what) {
15194: return $domain{$name}{'description'};
15195: }
15196: return $domain{$name}{$what};
15197: }
1.974 raeburn 15198:
15199: sub domain_info {
15200: &load_domain_tab() if (!$loaded);
15201: return %domain;
15202: }
15203:
1.327 albertel 15204: }
15205:
15206:
1.1 albertel 15207: # ------------------------------------------------------------- Read hosts file
15208: {
1.838 albertel 15209: my %hostname;
1.844 albertel 15210: my %hostdom;
1.845 albertel 15211: my %libserv;
1.852 albertel 15212: my $loaded;
1.888 albertel 15213: my %name_to_host;
1.1074 raeburn 15214: my %internetdom;
1.1107 raeburn 15215: my %LC_dns_serv;
1.852 albertel 15216:
15217: sub parse_hosts_tab {
15218: my ($file) = @_;
15219: foreach my $configline (@$file) {
15220: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 15221: chomp($configline);
15222: if ($configline =~ /^\^/) {
15223: if ($configline =~ /^\^([\w.\-]+)/) {
15224: $LC_dns_serv{$1} = 1;
15225: }
15226: next;
15227: }
1.1074 raeburn 15228: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 15229: $name=~s/\s//g;
15230: if ($id && $domain && $role && $name) {
1.1172.2.96 raeburn 15231: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15232: my $curr = $hostname{$id};
15233: my $skip;
15234: if (ref($name_to_host{$curr}) eq 'ARRAY') {
15235: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15236: $skip = 1;
15237: } else {
15238: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15239: }
15240: }
15241: unless ($skip) {
15242: push(@{$name_to_host{$name}},$id);
15243: }
15244: } else {
15245: push(@{$name_to_host{$name}},$id);
15246: }
1.852 albertel 15247: $hostname{$id}=$name;
15248: $hostdom{$id}=$domain;
15249: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 15250: if (defined($protocol)) {
15251: if ($protocol eq 'https') {
15252: $protocol{$id} = $protocol;
15253: } else {
15254: $protocol{$id} = 'http';
15255: }
1.968 raeburn 15256: } else {
1.969 raeburn 15257: $protocol{$id} = 'http';
1.968 raeburn 15258: }
1.1074 raeburn 15259: if (defined($intdom)) {
15260: $internetdom{$id} = $intdom;
15261: }
1.852 albertel 15262: }
15263: }
15264: }
1.864 albertel 15265:
15266: sub reset_hosts_info {
1.897 albertel 15267: &purge_remembered();
1.864 albertel 15268: &reset_domain_info();
15269: &reset_hosts_ip_info();
1.892 albertel 15270: undef(%name_to_host);
1.864 albertel 15271: undef(%hostname);
15272: undef(%hostdom);
15273: undef(%libserv);
15274: undef($loaded);
15275: }
1.1 albertel 15276:
1.852 albertel 15277: sub load_hosts_tab {
1.1172.2.70 raeburn 15278: my ($ignore_cache,$nocache) = @_;
15279: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1172.2.96 raeburn 15280: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852 albertel 15281: my @config = <$config>;
15282: &parse_hosts_tab(\@config);
15283: close($config);
15284: $loaded=1;
1.1 albertel 15285: }
1.852 albertel 15286:
1.838 albertel 15287: sub hostname {
1.852 albertel 15288: &load_hosts_tab() if (!$loaded);
15289:
1.838 albertel 15290: my ($lonid) = @_;
15291: return $hostname{$lonid};
15292: }
1.845 albertel 15293:
1.838 albertel 15294: sub all_hostnames {
1.852 albertel 15295: &load_hosts_tab() if (!$loaded);
15296:
1.838 albertel 15297: return %hostname;
15298: }
1.845 albertel 15299:
1.888 albertel 15300: sub all_names {
1.1172.2.70 raeburn 15301: my ($ignore_cache,$nocache) = @_;
15302: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 15303:
15304: return %name_to_host;
15305: }
15306:
1.974 raeburn 15307: sub all_host_domain {
15308: &load_hosts_tab() if (!$loaded);
15309: return %hostdom;
15310: }
15311:
1.845 albertel 15312: sub is_library {
1.852 albertel 15313: &load_hosts_tab() if (!$loaded);
15314:
1.845 albertel 15315: return exists($libserv{$_[0]});
15316: }
15317:
15318: sub all_library {
1.852 albertel 15319: &load_hosts_tab() if (!$loaded);
15320:
1.845 albertel 15321: return %libserv;
15322: }
15323:
1.1062 droeschl 15324: sub unique_library {
15325: #2x reverse removes all hostnames that appear more than once
15326: my %unique = reverse &all_library();
15327: return reverse %unique;
15328: }
15329:
1.841 albertel 15330: sub get_servers {
1.852 albertel 15331: &load_hosts_tab() if (!$loaded);
15332:
1.841 albertel 15333: my ($domain,$type) = @_;
15334: my %possible_hosts = ($type eq 'library') ? %libserv
15335: : %hostname;
15336: my %result;
1.842 albertel 15337: if (ref($domain) eq 'ARRAY') {
15338: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 15339: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 15340: $result{$host} = $hostname;
15341: }
15342: }
15343: } else {
15344: while ( my ($host,$hostname) = each(%possible_hosts)) {
15345: if ($hostdom{$host} eq $domain) {
15346: $result{$host} = $hostname;
15347: }
1.841 albertel 15348: }
15349: }
15350: return %result;
15351: }
1.845 albertel 15352:
1.1062 droeschl 15353: sub get_unique_servers {
15354: my %unique = reverse &get_servers(@_);
15355: return reverse %unique;
15356: }
15357:
1.844 albertel 15358: sub host_domain {
1.852 albertel 15359: &load_hosts_tab() if (!$loaded);
15360:
1.844 albertel 15361: my ($lonid) = @_;
15362: return $hostdom{$lonid};
15363: }
15364:
1.841 albertel 15365: sub all_domains {
1.852 albertel 15366: &load_hosts_tab() if (!$loaded);
15367:
1.841 albertel 15368: my %seen;
15369: my @uniq = grep(!$seen{$_}++, values(%hostdom));
15370: return @uniq;
15371: }
1.1074 raeburn 15372:
15373: sub internet_dom {
15374: &load_hosts_tab() if (!$loaded);
15375:
15376: my ($lonid) = @_;
15377: return $internetdom{$lonid};
15378: }
1.1107 raeburn 15379:
15380: sub is_LC_dns {
15381: &load_hosts_tab() if (!$loaded);
15382:
15383: my ($hostname) = @_;
15384: return exists($LC_dns_serv{$hostname});
15385: }
15386:
1.1 albertel 15387: }
15388:
1.847 albertel 15389: {
15390: my %iphost;
1.856 albertel 15391: my %name_to_ip;
15392: my %lonid_to_ip;
1.869 albertel 15393:
1.847 albertel 15394: sub get_hosts_from_ip {
15395: my ($ip) = @_;
15396: my %iphosts = &get_iphost();
15397: if (ref($iphosts{$ip})) {
15398: return @{$iphosts{$ip}};
15399: }
15400: return;
1.839 albertel 15401: }
1.864 albertel 15402:
15403: sub reset_hosts_ip_info {
15404: undef(%iphost);
15405: undef(%name_to_ip);
15406: undef(%lonid_to_ip);
15407: }
1.856 albertel 15408:
15409: sub get_host_ip {
15410: my ($lonid) = @_;
15411: if (exists($lonid_to_ip{$lonid})) {
15412: return $lonid_to_ip{$lonid};
15413: }
15414: my $name=&hostname($lonid);
15415: my $ip = gethostbyname($name);
15416: return if (!$ip || length($ip) ne 4);
15417: $ip=inet_ntoa($ip);
15418: $name_to_ip{$name} = $ip;
15419: $lonid_to_ip{$lonid} = $ip;
15420: return $ip;
15421: }
1.847 albertel 15422:
15423: sub get_iphost {
1.1172.2.70 raeburn 15424: my ($ignore_cache,$nocache) = @_;
1.894 albertel 15425:
1.869 albertel 15426: if (!$ignore_cache) {
15427: if (%iphost) {
15428: return %iphost;
15429: }
15430: my ($ip_info,$cached)=
15431: &Apache::lonnet::is_cached_new('iphost','iphost');
15432: if ($cached) {
15433: %iphost = %{$ip_info->[0]};
15434: %name_to_ip = %{$ip_info->[1]};
15435: %lonid_to_ip = %{$ip_info->[2]};
15436: return %iphost;
15437: }
15438: }
1.894 albertel 15439:
15440: # get yesterday's info for fallback
15441: my %old_name_to_ip;
15442: my ($ip_info,$cached)=
15443: &Apache::lonnet::is_cached_new('iphost','iphost');
15444: if ($cached) {
15445: %old_name_to_ip = %{$ip_info->[1]};
15446: }
15447:
1.1172.2.70 raeburn 15448: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 15449: foreach my $name (keys(%name_to_host)) {
1.847 albertel 15450: my $ip;
15451: if (!exists($name_to_ip{$name})) {
15452: $ip = gethostbyname($name);
15453: if (!$ip || length($ip) ne 4) {
1.894 albertel 15454: if (defined($old_name_to_ip{$name})) {
15455: $ip = $old_name_to_ip{$name};
15456: &logthis("Can't find $name defaulting to old $ip");
15457: } else {
15458: &logthis("Name $name no IP found");
15459: next;
15460: }
15461: } else {
15462: $ip=inet_ntoa($ip);
1.847 albertel 15463: }
15464: $name_to_ip{$name} = $ip;
15465: } else {
15466: $ip = $name_to_ip{$name};
1.653 albertel 15467: }
1.888 albertel 15468: foreach my $id (@{ $name_to_host{$name} }) {
15469: $lonid_to_ip{$id} = $ip;
15470: }
15471: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 15472: }
1.1172.2.70 raeburn 15473: unless ($nocache) {
15474: &do_cache_new('iphost','iphost',
15475: [\%iphost,\%name_to_ip,\%lonid_to_ip],
15476: 48*60*60);
15477: }
1.869 albertel 15478:
1.847 albertel 15479: return %iphost;
1.598 albertel 15480: }
15481:
1.992 raeburn 15482: #
15483: # Given a DNS returns the loncapa host name for that DNS
15484: #
15485: sub host_from_dns {
15486: my ($dns) = @_;
15487: my @hosts;
15488: my $ip;
15489:
1.993 raeburn 15490: if (exists($name_to_ip{$dns})) {
1.992 raeburn 15491: $ip = $name_to_ip{$dns};
15492: }
15493: if (!$ip) {
15494: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15495: if (length($ip) == 4) {
15496: $ip = &IO::Socket::inet_ntoa($ip);
15497: }
15498: }
15499: if ($ip) {
15500: @hosts = get_hosts_from_ip($ip);
15501: return $hosts[0];
15502: }
15503: return undef;
1.986 foxr 15504: }
1.992 raeburn 15505:
1.1074 raeburn 15506: sub get_internet_names {
15507: my ($lonid) = @_;
15508: return if ($lonid eq '');
15509: my ($idnref,$cached)=
15510: &Apache::lonnet::is_cached_new('internetnames',$lonid);
15511: if ($cached) {
15512: return $idnref;
15513: }
15514: my $ip = &get_host_ip($lonid);
15515: my @hosts = &get_hosts_from_ip($ip);
15516: my %iphost = &get_iphost();
15517: my (@idns,%seen);
15518: foreach my $id (@hosts) {
15519: my $dom = &host_domain($id);
15520: my $prim_id = &domain($dom,'primary');
15521: my $prim_ip = &get_host_ip($prim_id);
15522: next if ($seen{$prim_ip});
15523: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15524: foreach my $id (@{$iphost{$prim_ip}}) {
15525: my $intdom = &internet_dom($id);
15526: unless (grep(/^\Q$intdom\E$/,@idns)) {
15527: push(@idns,$intdom);
15528: }
15529: }
15530: }
15531: $seen{$prim_ip} = 1;
15532: }
1.1172.2.23 raeburn 15533: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 15534: }
15535:
1.986 foxr 15536: }
15537:
1.1079 raeburn 15538: sub all_loncaparevs {
1.1172.2.39 raeburn 15539: 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 15540: }
15541:
1.1172.2.27 raeburn 15542: # ------------------------------------------------------- Read loncaparev table
15543: {
15544: sub load_loncaparevs {
15545: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1172.2.96 raeburn 15546: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1172.2.27 raeburn 15547: while (my $configline=<$config>) {
15548: chomp($configline);
15549: my ($hostid,$loncaparev)=split(/:/,$configline);
15550: $loncaparevs{$hostid}=$loncaparev;
15551: }
15552: close($config);
15553: }
15554: }
15555: }
15556: }
15557:
15558: # ----------------------------------------------------- Read serverhostID table
15559: {
15560: sub load_serverhomeIDs {
15561: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1172.2.96 raeburn 15562: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1172.2.27 raeburn 15563: while (my $configline=<$config>) {
15564: chomp($configline);
15565: my ($name,$id)=split(/:/,$configline);
15566: $serverhomeIDs{$name}=$id;
15567: }
15568: close($config);
15569: }
15570: }
15571: }
15572: }
15573:
15574:
1.862 albertel 15575: BEGIN {
15576:
15577: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15578: unless ($readit) {
15579: {
15580: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15581: %perlvar = (%perlvar,%{$configvars});
15582: }
15583:
15584:
1.1 albertel 15585: # ------------------------------------------------------ Read spare server file
15586: {
1.1172.2.96 raeburn 15587: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 15588:
15589: while (my $configline=<$config>) {
15590: chomp($configline);
1.284 matthew 15591: if ($configline) {
1.784 albertel 15592: my ($host,$type) = split(':',$configline,2);
1.785 albertel 15593: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 15594: push(@{ $spareid{$type} }, $host);
1.1 albertel 15595: }
15596: }
1.448 albertel 15597: close($config);
1.1 albertel 15598: }
1.11 www 15599: # ------------------------------------------------------------ Read permissions
15600: {
1.1172.2.96 raeburn 15601: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11 www 15602:
15603: while (my $configline=<$config>) {
1.448 albertel 15604: chomp($configline);
15605: if ($configline) {
15606: my ($role,$perm)=split(/ /,$configline);
15607: if ($perm ne '') { $pr{$role}=$perm; }
15608: }
1.11 www 15609: }
1.448 albertel 15610: close($config);
1.11 www 15611: }
15612:
15613: # -------------------------------------------- Read plain texts for permissions
15614: {
1.1172.2.96 raeburn 15615: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 15616:
15617: while (my $configline=<$config>) {
1.448 albertel 15618: chomp($configline);
15619: if ($configline) {
1.742 raeburn 15620: my ($short,@plain)=split(/:/,$configline);
15621: %{$prp{$short}} = ();
15622: if (@plain > 0) {
15623: $prp{$short}{'std'} = $plain[0];
15624: for (my $i=1; $i<@plain; $i++) {
15625: $prp{$short}{'alt'.$i} = $plain[$i];
15626: }
15627: }
1.448 albertel 15628: }
1.135 www 15629: }
1.448 albertel 15630: close($config);
1.135 www 15631: }
15632:
15633: # ---------------------------------------------------------- Read package table
15634: {
1.1172.2.96 raeburn 15635: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135 www 15636:
15637: while (my $configline=<$config>) {
1.483 albertel 15638: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 15639: chomp($configline);
15640: my ($short,$plain)=split(/:/,$configline);
15641: my ($pack,$name)=split(/\&/,$short);
15642: if ($plain ne '') {
15643: $packagetab{$pack.'&'.$name.'&name'}=$name;
15644: $packagetab{$short}=$plain;
15645: }
1.11 www 15646: }
1.448 albertel 15647: close($config);
1.329 matthew 15648: }
15649:
1.1172.2.27 raeburn 15650: # --------------------------------------------------------- Read loncaparev table
1.1073 raeburn 15651:
1.1172.2.27 raeburn 15652: &load_loncaparevs();
15653:
15654: # ------------------------------------------------------- Read serverhostID table
15655:
15656: &load_serverhomeIDs();
1.1074 raeburn 15657:
1.1172.2.27 raeburn 15658: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 15659: {
15660: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15661: if (-e $file) {
15662: my $parser = HTML::LCParser->new($file);
15663: while (my $token = $parser->get_token()) {
15664: if ($token->[0] eq 'S') {
15665: my $item = $token->[1];
15666: my $name = $token->[2]{'name'};
15667: my $value = $token->[2]{'value'};
15668: if ($item ne '' && $name ne '' && $value ne '') {
15669: my $release = $parser->get_text();
15670: $release =~ s/(^\s*|\s*$ )//gx;
15671: $needsrelease{$item.':'.$name.':'.$value} = $release;
15672: }
15673: }
15674: }
15675: }
1.1073 raeburn 15676: }
15677:
1.1138 raeburn 15678: # ---------------------------------------------------------- Read managers table
15679: {
15680: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1172.2.96 raeburn 15681: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138 raeburn 15682: while (my $configline=<$config>) {
15683: chomp($configline);
15684: next if ($configline =~ /^\#/);
15685: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15686: $managerstab{$configline} = 1;
15687: }
15688: }
15689: close($config);
15690: }
15691: }
15692: }
15693:
1.329 matthew 15694: # ------------- set up temporary directory
15695: {
1.1117 foxr 15696: $tmpdir = LONCAPA::tempdir();
1.329 matthew 15697:
1.11 www 15698: }
15699:
1.1172.2.104 raeburn 15700: # ------------- set default texengine (domain default overrides this)
15701: {
15702: $deftex = LONCAPA::texengine();
15703: }
15704:
1.1172.2.114 raeburn 15705: # ------------- set default minimum length for passwords for internal auth users
15706: {
15707: $passwdmin = LONCAPA::passwd_min();
15708: }
15709:
1.794 albertel 15710: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
15711: 'compress_threshold'=> 20_000,
15712: });
1.185 www 15713:
1.281 www 15714: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 15715: $dumpcount=0;
1.958 www 15716: $locknum=0;
1.22 www 15717:
1.163 harris41 15718: &logtouch();
1.672 albertel 15719: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 15720: $readit=1;
1.564 albertel 15721: {
15722: use integer;
15723: my $test=(2**32)+1;
1.568 albertel 15724: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 15725: &logthis(" Detected 64bit platform ($_64bit)");
15726: }
1.195 www 15727: }
1.1 albertel 15728: }
1.179 www 15729:
1.1 albertel 15730: 1;
1.191 harris41 15731: __END__
15732:
1.243 albertel 15733: =pod
15734:
1.191 harris41 15735: =head1 NAME
15736:
1.243 albertel 15737: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 15738:
15739: =head1 SYNOPSIS
15740:
1.243 albertel 15741: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 15742:
15743: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15744:
1.243 albertel 15745: Common parameters:
15746:
15747: =over 4
15748:
15749: =item *
15750:
15751: $uname : an internal username (if $cname expecting a course Id specifically)
15752:
15753: =item *
15754:
15755: $udom : a domain (if $cdom expecting a course's domain specifically)
15756:
15757: =item *
15758:
15759: $symb : a resource instance identifier
15760:
15761: =item *
15762:
15763: $namespace : the name of a .db file that contains the data needed or
15764: being set.
15765:
15766: =back
15767:
1.394 bowersj2 15768: =head1 OVERVIEW
1.191 harris41 15769:
1.394 bowersj2 15770: lonnet provides subroutines which interact with the
15771: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15772: about classes, users, and resources.
1.243 albertel 15773:
15774: For many of these objects you can also use this to store data about
15775: them or modify them in various ways.
1.191 harris41 15776:
1.394 bowersj2 15777: =head2 Symbs
1.191 harris41 15778:
1.394 bowersj2 15779: To identify a specific instance of a resource, LON-CAPA uses symbols
15780: or "symbs"X<symb>. These identifiers are built from the URL of the
15781: map, the resource number of the resource in the map, and the URL of
15782: the resource itself. The latter is somewhat redundant, but might help
15783: if maps change.
15784:
15785: An example is
15786:
15787: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15788:
15789: The respective map entry is
15790:
15791: <resource id="19" src="/res/msu/korte/tests/part12.problem"
15792: title="Problem 2">
15793: </resource>
15794:
15795: Symbs are used by the random number generator, as well as to store and
15796: restore data specific to a certain instance of for example a problem.
15797:
15798: =head2 Storing And Retrieving Data
15799:
15800: X<store()>X<cstore()>X<restore()>Three of the most important functions
15801: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15802: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15803: is is the non-critical message twin of cstore. These functions are for
15804: handlers to store a perl hash to a user's permanent data space in an
15805: easy manner, and to retrieve it again on another call. It is expected
15806: that a handler would use this once at the beginning to retrieve data,
15807: and then again once at the end to send only the new data back.
15808:
15809: The data is stored in the user's data directory on the user's
15810: homeserver under the ID of the course.
15811:
15812: The hash that is returned by restore will have all of the previous
15813: value for all of the elements of the hash.
15814:
15815: Example:
15816:
15817: #creating a hash
15818: my %hash;
15819: $hash{'foo'}='bar';
15820:
15821: #storing it
15822: &Apache::lonnet::cstore(\%hash);
15823:
15824: #changing a value
15825: $hash{'foo'}='notbar';
15826:
15827: #adding a new value
15828: $hash{'bar'}='foo';
15829: &Apache::lonnet::cstore(\%hash);
15830:
15831: #retrieving the hash
15832: my %history=&Apache::lonnet::restore();
15833:
15834: #print the hash
15835: foreach my $key (sort(keys(%history))) {
15836: print("\%history{$key} = $history{$key}");
15837: }
15838:
15839: Will print out:
1.191 harris41 15840:
1.394 bowersj2 15841: %history{1:foo} = bar
15842: %history{1:keys} = foo:timestamp
15843: %history{1:timestamp} = 990455579
15844: %history{2:bar} = foo
15845: %history{2:foo} = notbar
15846: %history{2:keys} = foo:bar:timestamp
15847: %history{2:timestamp} = 990455580
15848: %history{bar} = foo
15849: %history{foo} = notbar
15850: %history{timestamp} = 990455580
15851: %history{version} = 2
15852:
15853: Note that the special hash entries C<keys>, C<version> and
15854: C<timestamp> were added to the hash. C<version> will be equal to the
15855: total number of versions of the data that have been stored. The
15856: C<timestamp> attribute will be the UNIX time the hash was
15857: stored. C<keys> is available in every historical section to list which
15858: keys were added or changed at a specific historical revision of a
15859: hash.
15860:
15861: B<Warning>: do not store the hash that restore returns directly. This
15862: will cause a mess since it will restore the historical keys as if the
15863: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 15864:
1.394 bowersj2 15865: Calling convention:
1.191 harris41 15866:
1.1172.2.27 raeburn 15867: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1172.2.64 raeburn 15868: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 15869:
1.394 bowersj2 15870: For more detailed information, see lonnet specific documentation.
1.191 harris41 15871:
1.394 bowersj2 15872: =head1 RETURN MESSAGES
1.191 harris41 15873:
1.394 bowersj2 15874: =over 4
1.191 harris41 15875:
1.394 bowersj2 15876: =item * B<con_lost>: unable to contact remote host
1.191 harris41 15877:
1.394 bowersj2 15878: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15879: when the connection is brought back up
1.191 harris41 15880:
1.394 bowersj2 15881: =item * B<con_failed>: unable to contact remote host and unable to save message
15882: for later delivery
1.191 harris41 15883:
1.967 bisitz 15884: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 15885:
1.394 bowersj2 15886: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 15887: that was requested
1.191 harris41 15888:
1.243 albertel 15889: =back
1.191 harris41 15890:
1.243 albertel 15891: =head1 PUBLIC SUBROUTINES
1.191 harris41 15892:
1.243 albertel 15893: =head2 Session Environment Functions
1.191 harris41 15894:
1.243 albertel 15895: =over 4
1.191 harris41 15896:
1.394 bowersj2 15897: =item *
15898: X<appenv()>
1.949 raeburn 15899: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 15900: the user envirnoment file, and will be restored for each access this
1.620 albertel 15901: user makes during this session, also modifies the %env for the current
1.949 raeburn 15902: process. Optional rolesarrayref - if defined contains a reference to an array
15903: of roles which are exempt from the restriction on modifying user.role entries
15904: in the user's environment.db and in %env.
1.191 harris41 15905:
15906: =item *
1.394 bowersj2 15907: X<delenv()>
1.987 raeburn 15908: B<delenv($delthis,$regexp)>: removes all items from the session
15909: environment file that begin with $delthis. If the
15910: optional second arg - $regexp - is true, $delthis is treated as a
15911: regular expression, otherwise \Q$delthis\E is used.
15912: The values are also deleted from the current processes %env.
1.191 harris41 15913:
1.795 albertel 15914: =item * get_env_multiple($name)
15915:
15916: gets $name from the %env hash, it seemlessly handles the cases where multiple
15917: values may be defined and end up as an array ref.
15918:
15919: returns an array of values
15920:
1.243 albertel 15921: =back
15922:
15923: =head2 User Information
1.191 harris41 15924:
1.243 albertel 15925: =over 4
1.191 harris41 15926:
15927: =item *
1.394 bowersj2 15928: X<queryauthenticate()>
15929: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 15930: authentication scheme
15931:
15932: =item *
1.394 bowersj2 15933: X<authenticate()>
1.1073 raeburn 15934: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 15935: authenticate user from domain's lib servers (first use the current
15936: one). C<$upass> should be the users password.
1.1073 raeburn 15937: $checkdefauth is optional (value is 1 if a check should be made to
15938: authenticate user using default authentication method, and allow
15939: account creation if username does not have account in the domain).
15940: $clientcancheckhost is optional (value is 1 if checking whether the
15941: server can host will occur on the client side in lonauth.pm).
1.191 harris41 15942:
15943: =item *
1.394 bowersj2 15944: X<homeserver()>
15945: B<homeserver($uname,$udom)>: find the server which has
15946: the user's directory and files (there must be only one), this caches
15947: the answer, and also caches if there is a borken connection.
1.191 harris41 15948:
15949: =item *
1.394 bowersj2 15950: X<idget()>
15951: B<idget($udom,@ids)>: find the usernames behind a list of IDs
15952: (IDs are a unique resource in a domain, there must be only 1 ID per
15953: username, and only 1 username per ID in a specific domain) (returns
15954: hash: id=>name,id=>name)
1.191 harris41 15955:
15956: =item *
1.394 bowersj2 15957: X<idrget()>
15958: B<idrget($udom,@unames)>: find the IDs behind a list of
15959: usernames (returns hash: name=>id,name=>id)
1.191 harris41 15960:
15961: =item *
1.394 bowersj2 15962: X<idput()>
15963: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 15964:
15965: =item *
1.394 bowersj2 15966: X<rolesinit()>
1.1169 droeschl 15967: B<rolesinit($udom,$username)>: get user privileges.
15968: returns user role, first access and timer interval hashes
1.243 albertel 15969:
15970: =item *
1.1171 droeschl 15971: X<privileged()>
15972: B<privileged($username,$domain)>: returns a true if user has a
15973: privileged and active role (i.e. su or dc), false otherwise.
15974:
15975: =item *
1.551 albertel 15976: X<getsection()>
15977: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 15978: course $cname, return section name/number or '' for "not in course"
15979: and '-1' for "no section"
15980:
15981: =item *
1.394 bowersj2 15982: X<userenvironment()>
15983: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 15984: passed in @what from the requested user's environment, returns a hash
15985:
1.858 raeburn 15986: =item *
15987: X<userlog_query()>
1.859 albertel 15988: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15989: activity.log file. %filters defines filters applied when parsing the
15990: log file. These can be start or end timestamps, or the type of action
15991: - log to look for Login or Logout events, check for Checkin or
15992: Checkout, role for role selection. The response is in the form
15993: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
15994: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 15995:
1.243 albertel 15996: =back
15997:
15998: =head2 User Roles
15999:
16000: =over 4
16001:
16002: =item *
16003:
1.1172.2.65 raeburn 16004: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
16005: returns codes for allowed actions.
16006:
16007: The first argument is required, all others are optional.
16008:
16009: $priv is the privilege being checked.
16010: $uri contains additional information about what is being checked for access (e.g.,
16011: URL, course ID etc.).
16012: $symb is the unique resource instance identifier in a course; if needed,
16013: but not provided, it will be retrieved via a call to &symbread().
16014: $role is the role for which a priv is being checked (only used if priv is evb).
16015: $clientip is the user's IP address (only used when checking for access to portfolio
16016: files).
16017: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
16018: prevents recursive calls to &allowed.
16019:
1.243 albertel 16020: F: full access
16021: U,I,K: authentication modes (cxx only)
16022: '': forbidden
16023: 1: user needs to choose course
16024: 2: browse allowed
1.766 albertel 16025: A: passphrase authentication needed
1.1172.2.65 raeburn 16026: B: access temporarily blocked because of a blocking event in a course.
1.1172.2.146. .1(raebu 16027:22): D: access blocked because access is required via session initiated via deep-link
1.243 albertel 16028:
16029: =item *
16030:
1.1172.2.13 raeburn 16031: constructaccess($url,$setpriv) : check for access to construction space URL
16032:
16033: See if the owner domain and name in the URL match those in the
16034: expected environment. If so, return three element list
16035: ($ownername,$ownerdomain,$ownerhome).
16036:
16037: Otherwise return the null string.
16038:
16039: If second argument 'setpriv' is true, it assigns the privileges,
16040: and returns the same three element list, unless the owner has
16041: blocked "ad hoc" Domain Coordinator access to the Author Space,
16042: in which case the null string is returned.
16043:
16044: =item *
16045:
1.1172.2.84 raeburn 16046: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16047: define a custom role rolename set privileges in format of lonTabs/roles.tab
16048: for system, domain, and course level. $uname and $udom are optional (current
16049: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 16050:
16051: =item *
16052:
1.988 raeburn 16053: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
16054: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 16055: $type is Course (default) or Community.
1.988 raeburn 16056: If $forcedefault evaluates to true, text returned will be default
16057: text for $type. Otherwise, if this is a course, the text returned
16058: will be a custom name for the role (if defined in the course's
16059: environment). If no custom name is defined the default is returned.
16060:
1.832 raeburn 16061: =item *
16062:
1.1172.2.23 raeburn 16063: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 16064: All arguments are optional. Returns a hash of a roles, either for
16065: co-author/assistant author roles for a user's Construction Space
1.906 albertel 16066: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 16067: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16068: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16069: For each key, value is set to colon-separated start and end times for
16070: the role. If no username and domain are specified, will default to
1.934 raeburn 16071: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 16072: of role statuses (active, future or previous), roles
16073: (e.g., cc,in, st etc.) and domains of the roles which can be used
16074: to restrict the list of roles reported. If no array ref is
16075: provided for types, will default to return only active roles.
1.834 albertel 16076:
1.1172.2.13 raeburn 16077: =item *
16078:
16079: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16080: user: $uname:$udom has a role in the course: $cdom_$cnum.
16081:
16082: Additional optional arguments are: $type (if role checking is to be restricted
16083: to certain user status types -- previous (expired roles), active (currently
16084: available roles) or future (roles available in the future), and
16085: $hideprivileged -- if true will not report course roles for users who
1.1172.2.23 raeburn 16086: have active Domain Coordinator role in course's domain or in additional
16087: domains (specified in 'Domains to check for privileged users' in course
16088: environment -- set via: Course Settings -> Classlists and staff listing).
16089:
16090: =item *
16091:
16092: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16093: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16094: $possdomains and $possroles are optional array refs -- to domains to check and
16095: roles to check. If $possdomains is not specified, a dump will be done of the
16096: users' roles.db to check for a dc or su role in any domain. This can be
16097: time consuming if &privileged is called repeatedly (e.g., when displaying a
16098: classlist), so in such cases, supplying a $possdomains array is preferred, as
16099: this then allows &privileged_by_domain() to be used, which caches the identity
16100: of privileged users, eliminating the need for repeated calls to &dump().
16101:
16102: =item *
16103:
16104: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16105: where the outer hash keys are domains specified in the $possdomains array ref,
16106: next inner hash keys are privileged roles specified in the $roles array ref,
16107: and the innermost hash contains key = value pairs for username:domain = end:start
16108: for active or future "privileged" users with that role in that domain. To avoid
16109: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16110: innerhash are cached using priv_$role and $dom as the identifiers.
1.1172.2.13 raeburn 16111:
1.243 albertel 16112: =back
16113:
16114: =head2 User Modification
16115:
16116: =over 4
16117:
16118: =item *
16119:
1.957 raeburn 16120: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 16121: user for the level given by URL. Optional start and end dates (leave empty
16122: string or zero for "no date")
1.191 harris41 16123:
16124: =item *
16125:
1.243 albertel 16126: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16127: change a users, password, possible return values are: ok,
16128: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16129: refused
1.191 harris41 16130:
16131: =item *
16132:
1.243 albertel 16133: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 16134:
16135: =item *
16136:
1.1058 raeburn 16137: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16138: $forceid,$desiredhome,$email,$inststatus,$candelete) :
16139:
16140: will update user information (firstname,middlename,lastname,generation,
16141: permanentemail), and if forceid is true, student/employee ID also.
16142: A user's institutional affiliation(s) can also be updated.
16143: User information fields will not be overwritten with empty entries
16144: unless the field is included in the $candelete array reference.
16145: This array is included when a single user is modified via "Manage Users",
16146: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 16147:
16148: =item *
16149:
1.286 matthew 16150: modifystudent
16151:
1.957 raeburn 16152: modify a student's enrollment and identification information.
1.1172.2.31 raeburn 16153: The course id is resolved based on the current user's environment.
16154: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 16155: associated with a course.
16156:
1.297 matthew 16157: This call is essentially a wrapper for lonnet::modifyuser and
16158: lonnet::modify_student_enrollment
1.286 matthew 16159:
16160: Inputs:
16161:
16162: =over 4
16163:
1.957 raeburn 16164: =item B<$udom> Student's loncapa domain
1.286 matthew 16165:
1.957 raeburn 16166: =item B<$uname> Student's loncapa login name
1.286 matthew 16167:
1.964 bisitz 16168: =item B<$uid> Student/Employee ID
1.286 matthew 16169:
1.957 raeburn 16170: =item B<$umode> Student's authentication mode
1.286 matthew 16171:
1.957 raeburn 16172: =item B<$upass> Student's password
1.286 matthew 16173:
1.957 raeburn 16174: =item B<$first> Student's first name
1.286 matthew 16175:
1.957 raeburn 16176: =item B<$middle> Student's middle name
1.286 matthew 16177:
1.957 raeburn 16178: =item B<$last> Student's last name
1.286 matthew 16179:
1.957 raeburn 16180: =item B<$gene> Student's generation
1.286 matthew 16181:
1.957 raeburn 16182: =item B<$usec> Student's section in course
1.286 matthew 16183:
16184: =item B<$end> Unix time of the roles expiration
16185:
16186: =item B<$start> Unix time of the roles start date
16187:
16188: =item B<$forceid> If defined, allow $uid to be changed
16189:
16190: =item B<$desiredhome> server to use as home server for student
16191:
1.957 raeburn 16192: =item B<$email> Student's permanent e-mail address
16193:
16194: =item B<$type> Type of enrollment (auto or manual)
16195:
1.963 raeburn 16196: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
16197:
16198: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 16199:
1.963 raeburn 16200: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 16201:
1.963 raeburn 16202: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 16203:
1.1172.2.19 raeburn 16204: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16205:
16206: =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 16207:
1.286 matthew 16208: =back
1.297 matthew 16209:
16210: =item *
16211:
16212: modify_student_enrollment
16213:
1.1172.2.31 raeburn 16214: Change a student's enrollment status in a class. The environment variable
1.297 matthew 16215: 'role.request.course' must be defined for this function to proceed.
16216:
16217: Inputs:
16218:
16219: =over 4
16220:
1.1172.2.31 raeburn 16221: =item $udom, student's domain
1.297 matthew 16222:
1.1172.2.31 raeburn 16223: =item $uname, student's name
1.297 matthew 16224:
1.1172.2.31 raeburn 16225: =item $uid, student's user id
1.297 matthew 16226:
1.1172.2.31 raeburn 16227: =item $first, student's first name
1.297 matthew 16228:
16229: =item $middle
16230:
16231: =item $last
16232:
16233: =item $gene
16234:
16235: =item $usec
16236:
16237: =item $end
16238:
16239: =item $start
16240:
1.957 raeburn 16241: =item $type
16242:
16243: =item $locktype
16244:
16245: =item $cid
16246:
16247: =item $selfenroll
16248:
16249: =item $context
16250:
1.1172.2.19 raeburn 16251: =item $credits, number of credits student will earn from this class
16252:
1.1172.2.76 raeburn 16253: =item $instsec, institutional course section code for student
16254:
1.297 matthew 16255: =back
16256:
1.191 harris41 16257:
16258: =item *
16259:
1.243 albertel 16260: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16261: custom role; give a custom role to a user for the level given by URL. Specify
16262: name and domain of role author, and role name
1.191 harris41 16263:
16264: =item *
16265:
1.243 albertel 16266: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 16267:
16268: =item *
16269:
1.243 albertel 16270: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16271:
16272: =back
16273:
16274: =head2 Course Infomation
16275:
16276: =over 4
1.191 harris41 16277:
16278: =item *
16279:
1.1118 foxr 16280: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 16281: specified course id, including all environment settings for the
16282: course, the description of the course will be in the hash under the
16283: key 'description'
1.191 harris41 16284:
1.1118 foxr 16285: $options is an optional parameter that if supplied is a hash reference that controls
16286: what how this function works. It has the following key/values:
16287:
16288: =over 4
16289:
16290: =item freshen_cache
16291:
16292: If defined, and the environment cache for the course is valid, it is
16293: returned in the returned hash.
16294:
16295: =item one_time
16296:
16297: If defined, the last cache time is set to _now_
16298:
16299: =item user
16300:
16301: If defined, the supplied username is used instead of the current user.
16302:
16303:
16304: =back
16305:
1.191 harris41 16306: =item *
16307:
1.624 albertel 16308: resdata($name,$domain,$type,@which) : request for current parameter
16309: setting for a specific $type, where $type is either 'course' or 'user',
16310: @what should be a list of parameters to ask about. This routine caches
1.1172.2.31 raeburn 16311: answers for 10 minutes.
1.243 albertel 16312:
1.877 foxr 16313: =item *
16314:
16315: get_courseresdata($courseid, $domain) : dump the entire course resource
16316: data base, returning a hash that is keyed by the resource name and has
16317: values that are the resource value. I believe that the timestamps and
16318: versions are also returned.
16319:
1.243 albertel 16320: =back
16321:
16322: =head2 Course Modification
16323:
16324: =over 4
1.191 harris41 16325:
16326: =item *
16327:
1.243 albertel 16328: writecoursepref($courseid,%prefs) : write preferences (environment
16329: database) for a course
1.191 harris41 16330:
16331: =item *
16332:
1.1011 raeburn 16333: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16334:
16335: =item *
16336:
1.1038 raeburn 16337: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 16338:
1.1167 droeschl 16339: =item *
16340:
16341: is_course($courseid), is_course($cdom, $cnum)
16342:
16343: Accepts either a combined $courseid (in the form of domain_courseid) or the
16344: two component version $cdom, $cnum. It checks if the specified course exists.
16345:
16346: Returns:
16347: undef if the course doesn't exist, otherwise
16348: in scalar context the combined courseid.
16349: in list context the two components of the course identifier, domain and
16350: courseid.
16351:
1.243 albertel 16352: =back
16353:
1.1172.2.109 raeburn 16354: =head2 Bubblesheet Configuration
16355:
16356: =over 4
16357:
16358: =item *
16359:
16360: get_scantron_config($which)
16361:
16362: $which - the name of the configuration to parse from the file.
16363:
16364: Parses and returns the bubblesheet configuration line selected as a
16365: hash of configuration file fields.
16366:
16367:
16368: Returns:
16369: If the named configuration is not in the file, an empty
16370: hash is returned.
16371:
16372: a hash with the fields
16373: name - internal name for the this configuration setup
16374: description - text to display to operator that describes this config
16375: CODElocation - if 0 or the string 'none'
16376: - no CODE exists for this config
16377: if -1 || the string 'letter'
16378: - a CODE exists for this config and is
16379: a string of letters
16380: Unsupported value (but planned for future support)
16381: if a positive integer
16382: - The CODE exists as the first n items from
16383: the question section of the form
16384: if the string 'number'
16385: - The CODE exists for this config and is
16386: a string of numbers
16387: CODEstart - (only matter if a CODE exists) column in the line where
16388: the CODE starts
16389: CODElength - length of the CODE
16390: IDstart - column where the student/employee ID starts
16391: IDlength - length of the student/employee ID info
16392: Qstart - column where the information from the bubbled
16393: 'questions' start
16394: Qlength - number of columns comprising a single bubble line from
16395: the sheet. (usually either 1 or 10)
16396: Qon - either a single character representing the character used
16397: to signal a bubble was chosen in the positional setup, or
16398: the string 'letter' if the letter of the chosen bubble is
16399: in the final, or 'number' if a number representing the
16400: chosen bubble is in the file (1->A 0->J)
16401: Qoff - the character used to represent that a bubble was
16402: left blank
16403: PaperID - if the scanning process generates a unique number for each
16404: sheet scanned the column that this ID number starts in
16405: PaperIDlength - number of columns that comprise the unique ID number
16406: for the sheet of paper
16407: FirstName - column that the first name starts in
16408: FirstNameLength - number of columns that the first name spans
16409: LastName - column that the last name starts in
16410: LastNameLength - number of columns that the last name spans
16411: BubblesPerRow - number of bubbles available in each row used to
16412: bubble an answer. (If not specified, 10 assumed).
16413:
16414:
16415: =item *
16416:
16417: get_scantronformat_file($cdom)
16418:
16419: $cdom - the course's domain (optional); if not supplied, uses
16420: domain for current $env{'request.course.id'}.
16421:
16422: Returns an array containing lines from the scantron format file for
16423: the domain of the course.
16424:
16425: If a url for a custom.tab file is listed in domain's configuration.db,
16426: lines are from this file.
16427:
16428: Otherwise, if a default.tab has been published in RES space by the
16429: domainconfig user, lines are from this file.
16430:
16431: Otherwise, fall back to getting lines from the legacy file on the
16432: local server: /home/httpd/lonTabs/default_scantronformat.tab
16433:
16434: =back
16435:
1.243 albertel 16436: =head2 Resource Subroutines
16437:
16438: =over 4
1.191 harris41 16439:
16440: =item *
16441:
1.243 albertel 16442: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 16443:
16444: =item *
16445:
1.243 albertel 16446: repcopy($filename) : subscribes to the requested file, and attempts to
16447: replicate from the owning library server, Might return
1.607 raeburn 16448: 'unavailable', 'not_found', 'forbidden', 'ok', or
16449: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 16450: resource. Expects the local filesystem pathname
16451: (/home/httpd/html/res/....)
16452:
16453: =back
16454:
16455: =head2 Resource Information
16456:
16457: =over 4
1.191 harris41 16458:
16459: =item *
16460:
1.1172.2.28 raeburn 16461: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
16462: and returns the value of a variety of different possible values,
16463: $varname should be a request string, and the other parameters can be
16464: used to specify who and what one is asking about. Ordinarily, $cid
16465: does not need to be specified, as it is retrived from
16466: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16467: within lonuserstate::loadmap() when initializing a course, before
16468: $env{'request.course.id'} has been set, so it needs to be provided
16469: in that one case.
1.243 albertel 16470:
16471: Possible values for $varname are environment.lastname (or other item
16472: from the envirnment hash), user.name (or someother aspect about the
16473: user), resource.0.maxtries (or some other part and parameter of a
16474: resource)
1.204 albertel 16475:
16476: =item *
16477:
1.243 albertel 16478: directcondval($number) : get current value of a condition; reads from a state
16479: string
1.204 albertel 16480:
16481: =item *
16482:
1.243 albertel 16483: condval($condidx) : value of condition index based on state
1.204 albertel 16484:
16485: =item *
16486:
1.1172.2.146. .13(raeb 16487:-23): metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243 albertel 16488: resource's metadata, $what should be either a specific key, or either
16489: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1172.2.146. .13(raeb 16490:-23): packages that this resource currently uses, the last 3 arguments are
16491:-23): only used internally for recursive metadata.
16492:-23):
16493:-23): the toolsymb is only used where the uri is for an external tool (for which
16494:-23): the uri as well as the symb are guaranteed to be unique).
1.243 albertel 16495:
16496: this function automatically caches all requests
1.191 harris41 16497:
16498: =item *
16499:
1.243 albertel 16500: metadata_query($query,$custom,$customshow) : make a metadata query against the
16501: network of library servers; returns file handle of where SQL and regex results
16502: will be stored for query
1.191 harris41 16503:
16504: =item *
16505:
1.1172.2.66 raeburn 16506: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
16507: return symbolic list entry (all arguments optional).
16508:
16509: Args: filename is the filename (including path) for the file for which a symb
16510: is required; donotrecurse, if true will prevent calls to allowed() being made
16511: to check access status if more than one resource was found in the bighash
16512: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
16513: a randompick); ignorecachednull, if true will prevent a symb of '' being
16514: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16515: cause possible symbs to be checked to determine if they are subject to content
16516: blocking, if so they will not be included as possible symbs; possibles is a
16517: ref to a hash, which, as a side effect, will be populated with all possible
16518: symbs (content blocking not tested).
16519:
1.243 albertel 16520: returns the data handle
1.191 harris41 16521:
16522: =item *
16523:
1.1172.2.17 raeburn 16524: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
1.1172.2.11 raeburn 16525: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 16526: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1172.2.11 raeburn 16527: on failure, user must be in a course, as it assumes the existence of
16528: the course initial hash, and uses $env('request.course.id'}. The third
16529: arg is an optional reference to a scalar. If this arg is passed in the
16530: call to symbverify, it will be set to 1 if the symb has been set to be
16531: encrypted; otherwise it will be null.
1.243 albertel 16532:
1.191 harris41 16533: =item *
16534:
1.243 albertel 16535: symbclean($symb) : removes versions numbers from a symb, returns the
16536: cleaned symb
1.191 harris41 16537:
16538: =item *
16539:
1.243 albertel 16540: is_on_map($uri) : checks if the $uri is somewhere on the current
16541: course map, user must be in a course for it to work.
1.191 harris41 16542:
16543: =item *
16544:
1.243 albertel 16545: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 16546:
16547: =item *
16548:
1.243 albertel 16549: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16550: a random seed, all arguments are optional, if they aren't sent it uses the
16551: environment to derive them. Note: if symb isn't sent and it can't get one
16552: from &symbread it will use the current time as its return value
1.191 harris41 16553:
16554: =item *
16555:
1.243 albertel 16556: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16557: unfakeable, receipt
1.191 harris41 16558:
16559: =item *
16560:
1.620 albertel 16561: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 16562:
16563: =item *
16564:
1.243 albertel 16565: countacc($url) : count the number of accesses to a given URL
1.191 harris41 16566:
16567: =item *
16568:
1.243 albertel 16569: 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 16570:
16571: =item *
16572:
1.243 albertel 16573: 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 16574:
16575: =item *
16576:
1.243 albertel 16577: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 16578:
16579: =item *
16580:
1.243 albertel 16581: devalidate($symb) : devalidate temporary spreadsheet calculations,
16582: forcing spreadsheet to reevaluate the resource scores next time.
16583:
1.1172.2.13 raeburn 16584: =item *
16585:
16586: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16587: when viewing in course context.
16588:
16589: input: six args -- filename (decluttered), course number, course domain,
16590: url, symb (if registered) and group (if this is a
16591: group item -- e.g., bulletin board, group page etc.).
16592:
16593: output: array of five scalars --
16594: $cfile -- url for file editing if editable on current server
16595: $home -- homeserver of resource (i.e., for author if published,
16596: or course if uploaded.).
16597: $switchserver -- 1 if server switch will be needed.
16598: $forceedit -- 1 if icon/link should be to go to edit mode
16599: $forceview -- 1 if icon/link should be to go to view mode
16600:
16601: =item *
16602:
16603: is_course_upload($file,$cnum,$cdom)
16604:
16605: Used in course context to determine if current file was uploaded to
16606: the course (i.e., would be found in /userfiles/docs on the course's
16607: homeserver.
16608:
16609: input: 3 args -- filename (decluttered), course number and course domain.
16610: output: boolean -- 1 if file was uploaded.
16611:
1.243 albertel 16612: =back
16613:
16614: =head2 Storing/Retreiving Data
16615:
16616: =over 4
1.191 harris41 16617:
16618: =item *
16619:
1.1172.2.64 raeburn 16620: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16621: permanently for this url; hashref needs to be given and should be a \%hashname;
16622: the remaining args aren't required and if they aren't passed or are '' they will
16623: be derived from the env (with the exception of $laststore, which is an
16624: optional arg used when a user's submission is stored in grading).
16625: $laststore is $version=$timestamp, where $version is the most recent version
16626: number retrieved for the corresponding $symb in the $namespace db file, and
16627: $timestamp is the timestamp for that transaction (UNIX time).
16628: $laststore is currently only passed when cstore() is called by
16629: structuretags::finalize_storage().
1.191 harris41 16630:
16631: =item *
16632:
1.1172.2.64 raeburn 16633: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16634: but uses critical subroutine
1.191 harris41 16635:
16636: =item *
16637:
1.243 albertel 16638: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16639: all args are optional
1.191 harris41 16640:
16641: =item *
16642:
1.717 albertel 16643: dumpstore($namespace,$udom,$uname,$regexp,$range) :
16644: dumps the complete (or key matching regexp) namespace into a hash
16645: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16646: normally &store()ed into
16647:
16648: $range should be either an integer '100' (give me the first 100
16649: matching records)
16650: or be two integers sperated by a - with no spaces
16651: '30-50' (give me the 30th through the 50th matching
16652: records)
16653:
16654:
16655: =item *
16656:
1.1172.2.59 raeburn 16657: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 16658: replaces a &store() version of data with a replacement set of data
16659: for a particular resource in a namespace passed in the $storehash hash
1.1172.2.59 raeburn 16660: reference. If $tolog is true, the transaction is logged in the courselog
16661: with an action=PUTSTORE.
1.717 albertel 16662:
16663: =item *
16664:
1.243 albertel 16665: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16666: works very similar to store/cstore, but all data is stored in a
16667: temporary location and can be reset using tmpreset, $storehash should
16668: be a hash reference, returns nothing on success
1.191 harris41 16669:
16670: =item *
16671:
1.243 albertel 16672: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16673: similar to restore, but all data is stored in a temporary location and
16674: can be reset using tmpreset. Returns a hash of values on success,
16675: error string otherwise.
1.191 harris41 16676:
16677: =item *
16678:
1.243 albertel 16679: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16680: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 16681:
16682: =item *
16683:
1.243 albertel 16684: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16685: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 16686:
16687: =item *
16688:
1.243 albertel 16689: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16690: namesp ($udom and $uname are optional)
1.191 harris41 16691:
16692: =item *
16693:
1.702 albertel 16694: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 16695: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 16696: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 16697:
1.702 albertel 16698: $range should be either an integer '100' (give me the first 100
16699: matching records)
16700: or be two integers sperated by a - with no spaces
16701: '30-50' (give me the 30th through the 50th matching
16702: records)
1.449 matthew 16703: =item *
16704:
16705: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16706: $store can be a scalar, an array reference, or if the amount to be
16707: incremented is > 1, a hash reference.
16708:
16709: ($udom and $uname are optional)
1.191 harris41 16710:
16711: =item *
16712:
1.243 albertel 16713: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16714: ($udom and $uname are optional)
1.191 harris41 16715:
16716: =item *
16717:
1.243 albertel 16718: cput($namespace,$storehash,$udom,$uname) : critical put
16719: ($udom and $uname are optional)
1.191 harris41 16720:
16721: =item *
16722:
1.748 albertel 16723: newput($namespace,$storehash,$udom,$uname) :
16724:
16725: Attempts to store the items in the $storehash, but only if they don't
16726: currently exist, if this succeeds you can be certain that you have
16727: successfully created a new key value pair in the $namespace db.
16728:
16729:
16730: Args:
16731: $namespace: name of database to store values to
16732: $storehash: hashref to store to the db
16733: $udom: (optional) domain of user containing the db
16734: $uname: (optional) name of user caontaining the db
16735:
16736: Returns:
16737: 'ok' -> succeeded in storing all keys of $storehash
16738: 'key_exists: <key>' -> failed to anything out of $storehash, as at
16739: least <key> already existed in the db (other
16740: requested keys may also already exist)
1.967 bisitz 16741: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 16742: 'con_lost' -> unable to contact request server
16743: 'refused' -> action was not allowed by remote machine
16744:
16745:
16746: =item *
16747:
1.243 albertel 16748: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16749: reference filled in from namesp (encrypts the return communication)
16750: ($udom and $uname are optional)
1.191 harris41 16751:
16752: =item *
16753:
1.243 albertel 16754: log($udom,$name,$home,$message) : write to permanent log for user; use
16755: critical subroutine
16756:
1.806 raeburn 16757: =item *
16758:
1.860 raeburn 16759: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16760: array reference filled in from namespace found in domain level on either
16761: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 16762:
16763: =item *
16764:
1.860 raeburn 16765: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
16766: domain level either on specified domain server ($uhome) or primary domain
16767: server ($udom and $uhome are optional)
1.806 raeburn 16768:
1.943 raeburn 16769: =item *
16770:
1.1172.2.35 raeburn 16771: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
16772: for: authentication, language, quotas, timezone, date locale, and portal URL in
16773: the target domain.
16774:
16775: May also include additional key => value pairs for the following groups:
16776:
16777: =over
16778:
16779: =item
16780: disk quotas (MB allocated by default to portfolios and authoring spaces).
16781:
16782: =over
16783:
16784: =item defaultquota, authorquota
16785:
16786: =back
16787:
16788: =item
16789: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16790: portfolio for users).
16791:
16792: =over
16793:
16794: =item
16795: aboutme, blog, webdav, portfolio
16796:
16797: =back
16798:
16799: =item
16800: requestcourses: ability to request courses, and how requests are processed.
16801:
16802: =over
16803:
16804: =item
1.1172.2.37 raeburn 16805: official, unofficial, community, textbook
1.1172.2.35 raeburn 16806:
16807: =back
16808:
16809: =item
16810: inststatus: types of institutional affiliation, and order in which they are displayed.
16811:
16812: =over
16813:
16814: =item
1.1172.2.44 raeburn 16815: inststatustypes, inststatusorder, inststatusguest
1.1172.2.35 raeburn 16816:
16817: =back
16818:
16819: =item
16820: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16821: for course's uploaded content.
16822:
16823: =over
16824:
16825: =item
1.1172.2.68 raeburn 16826: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
16827: communityquota, textbookquota
1.1172.2.35 raeburn 16828:
16829: =back
16830:
16831: =item
16832: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16833: on your servers.
16834:
16835: =over
16836:
16837: =item
16838: remotesessions, hostedsessions
16839:
16840: =back
16841:
16842: =back
16843:
16844: In cases where a domain coordinator has never used the "Set Domain Configuration"
16845: utility to create a configuration.db file on a domain's primary library server
16846: only the following domain defaults: auth_def, auth_arg_def, lang_def
16847: -- corresponding values are authentication type (internal, krb4, krb5,
16848: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
16849: will be available. Values are retrieved from cache (if current), unless the
16850: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16851: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16852:
16853: Typical usage:
1.943 raeburn 16854:
1.1172.2.35 raeburn 16855: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 16856:
1.243 albertel 16857: =back
16858:
16859: =head2 Network Status Functions
16860:
16861: =over 4
1.191 harris41 16862:
16863: =item *
16864:
1.1137 raeburn 16865: dirlist() : return directory list based on URI (first arg).
16866:
16867: Inputs: 1 required, 5 optional.
16868:
16869: =over
16870:
16871: =item
16872: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16873:
16874: =item
16875: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
16876:
16877: =item
16878: $username - username of user/course to be listed. Extracted from $uri if absent.
16879:
16880: =item
16881: $getpropath - boolean: 1 if prepend path using &propath().
16882:
16883: =item
16884: $getuserdir - boolean: 1 if prepend path for "userfiles".
16885:
16886: =item
16887: $alternateRoot - path to prepend in place of path from $uri.
16888:
16889: =back
16890:
16891: Returns: Array of up to two items.
16892:
16893: =over
16894:
16895: a reference to an array of files/subdirectories
16896:
16897: =over
16898:
16899: Each element in the array of files/subdirectories is a & separated list of
16900: item name and the result of running stat on the item. If dirlist was requested
16901: for a file instead of a directory, the item name will be ''. For a directory
16902: listing, if the item is a metadata file, the element will end &N&M
16903: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16904: default copyright set (1).
16905:
16906: =back
16907:
16908: a scalar containing error condition (if encountered).
16909:
16910: =over
16911:
16912: =item
16913: no_host (no homeserver identified for $username:$domain).
16914:
16915: =item
16916: no_such_host (server contacted for listing not identified as valid host).
16917:
16918: =item
16919: con_lost (connection to remote server failed).
16920:
16921: =item
16922: refused (invalid $username:$domain received on lond side).
16923:
16924: =item
16925: no_such_dir (directory at specified path on lond side does not exist).
16926:
16927: =item
16928: empty (directory at specified path on lond side is empty).
16929:
16930: =over
16931:
16932: This is currently not encountered because the &ls3, &ls2,
16933: &ls (_handler) routines on the lond side do not filter out
16934: . and .. from a directory listing.
16935:
16936: =back
16937:
16938: =back
16939:
16940: =back
1.191 harris41 16941:
16942: =item *
16943:
1.243 albertel 16944: spareserver() : find server with least workload from spare.tab
16945:
1.986 foxr 16946:
16947: =item *
16948:
16949: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16950: if there is no corresponding loncapa host.
16951:
1.243 albertel 16952: =back
16953:
1.986 foxr 16954:
1.243 albertel 16955: =head2 Apache Request
16956:
16957: =over 4
1.191 harris41 16958:
16959: =item *
16960:
1.243 albertel 16961: ssi($url,%hash) : server side include, does a complete request cycle on url to
16962: localhost, posts hash
16963:
16964: =back
16965:
16966: =head2 Data to String to Data
16967:
16968: =over 4
1.191 harris41 16969:
16970: =item *
16971:
1.243 albertel 16972: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16973: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 16974:
16975: =item *
16976:
1.243 albertel 16977: hashref2str($hashref) : convert a hashref into a string complete with
16978: escaping and '=' and '&' separators, supports elements that are
16979: arrayrefs and hashrefs
1.191 harris41 16980:
16981: =item *
16982:
1.243 albertel 16983: arrayref2str($arrayref) : convert an arrayref into a string complete
16984: with escaping and '&' separators, supports elements that are arrayrefs
16985: and hashrefs
1.191 harris41 16986:
16987: =item *
16988:
1.243 albertel 16989: str2hash($string) : convert string to hash using unescaping and
16990: splitting on '=' and '&', supports elements that are arrayrefs and
16991: hashrefs
1.191 harris41 16992:
16993: =item *
16994:
1.243 albertel 16995: str2array($string) : convert string to hash using unescaping and
16996: splitting on '&', supports elements that are arrayrefs and hashrefs
16997:
16998: =back
16999:
17000: =head2 Logging Routines
17001:
17002:
17003: These routines allow one to make log messages in the lonnet.log and
17004: lonnet.perm logfiles.
1.191 harris41 17005:
1.1119 foxr 17006: =over 4
17007:
1.191 harris41 17008: =item *
17009:
1.243 albertel 17010: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 17011:
17012: =item *
17013:
1.243 albertel 17014: logthis() : append message to the normal lonnet.log file, it gets
17015: preiodically rolled over and deleted.
1.191 harris41 17016:
17017: =item *
17018:
1.243 albertel 17019: logperm() : append a permanent message to lonnet.perm.log, this log
17020: file never gets deleted by any automated portion of the system, only
17021: messages of critical importance should go in here.
17022:
1.1119 foxr 17023:
1.243 albertel 17024: =back
17025:
17026: =head2 General File Helper Routines
17027:
17028: =over 4
1.191 harris41 17029:
17030: =item *
17031:
1.481 raeburn 17032: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17033: (a) files in /uploaded
17034: (i) If a local copy of the file exists -
17035: compares modification date of local copy with last-modified date for
17036: definitive version stored on home server for course. If local copy is
17037: stale, requests a new version from the home server and stores it.
17038: If the original has been removed from the home server, then local copy
17039: is unlinked.
17040: (ii) If local copy does not exist -
17041: requests the file from the home server and stores it.
17042:
17043: If $caller is 'uploadrep':
17044: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17045: for request for files originally uploaded via DOCS.
17046: - returns 'ok' if fresh local copy now available, -1 otherwise.
17047:
17048: Otherwise:
17049: This indicates a call from the content generation phase of the request.
17050: - returns the entire contents of the file or -1.
17051:
17052: (b) files in /res
17053: - returns the entire contents of a file or -1;
17054: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 17055:
1.712 albertel 17056:
17057: =item *
17058:
17059: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17060: reference
17061:
17062: returns either a stat() list of data about the file or an empty list
17063: if the file doesn't exist or couldn't find out about it (connection
17064: problems or user unknown)
17065:
1.191 harris41 17066: =item *
17067:
1.243 albertel 17068: filelocation($dir,$file) : returns file system location of a file
17069: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17070: directory that relative $file lookups are to looked in ($dir of /a/dir
17071: and a file of ../bob will become /a/bob)
1.191 harris41 17072:
17073: =item *
17074:
17075: hreflocation($dir,$file) : returns file system location or a URL; same as
17076: filelocation except for hrefs
17077:
17078: =item *
17079:
1.1172.2.49 raeburn 17080: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17081: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 17082:
1.243 albertel 17083: =back
17084:
1.608 albertel 17085: =head2 Usererfile file routines (/uploaded*)
17086:
17087: =over 4
17088:
17089: =item *
17090:
17091: userfileupload(): main rotine for putting a file in a user or course's
17092: filespace, arguments are,
17093:
1.620 albertel 17094: formname - required - this is the name of the element in $env where the
1.608 albertel 17095: filename, and the contents of the file to create/modifed exist
1.620 albertel 17096: the filename is in $env{'form.'.$formname.'.filename'} and the
17097: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 17098: context - if coursedoc, store the file in the course of the active role
17099: of the current user;
17100: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17101: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 17102: subdir - required - subdirectory to put the file in under ../userfiles/
17103: if undefined, it will be placed in "unknown"
17104:
17105: (This routine calls clean_filename() to remove any dangerous
17106: characters from the filename, and then calls finuserfileupload() to
17107: complete the transaction)
17108:
17109: returns either the url of the uploaded file (/uploaded/....) if successful
17110: and /adm/notfound.html if unsuccessful
17111:
17112: =item *
17113:
17114: clean_filename(): routine for cleaing a filename up for storage in
17115: userfile space, argument is:
17116:
17117: filename - proposed filename
17118:
17119: returns: the new clean filename
17120:
17121: =item *
17122:
1.1090 raeburn 17123: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 17124: userspace, probably shouldn't be called directly
17125:
17126: docuname: username or courseid of destination for the file
17127: docudom: domain of user/course of destination for the file
17128: formname: same as for userfileupload()
1.1090 raeburn 17129: fname: filename (including subdirectories) for the file
17130: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1172.2.109 raeburn 17131: if hashref, and context is scantron, will convert csv format to standard format
1.1090 raeburn 17132: allfiles: reference to hash used to store objects found by parser
17133: codebase: reference to hash used for codebases of java objects found by parser
17134: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17135: thumbheight: height (pixels) of thumbnail to be created for uploaded image
17136: resizewidth: width to be used to resize image using resizeImage from ImageMagick
17137: resizeheight: height to be used to resize image using resizeImage from ImageMagick
17138: context: if 'overwrite', will move the uploaded file from its temporary location to
17139: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 17140: mimetype: reference to scalar to accommodate mime type determined
17141: from File::MMagic if $parser = parse.
1.608 albertel 17142:
17143: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 17144: and /adm/notfound.html if unsuccessful (or an error message if context
17145: was 'overwrite').
17146:
1.608 albertel 17147:
17148: =item *
17149:
17150: renameuserfile(): renames an existing userfile to a new name
17151:
17152: Args:
17153: docuname: username or courseid of destination for the file
17154: docudom: domain of user/course of destination for the file
17155: old: current file name (including any subdirs under userfiles)
17156: new: desired file name (including any subdirs under userfiles)
17157:
17158: =item *
17159:
17160: mkdiruserfile(): creates a directory is a userfiles dir
17161:
17162: Args:
17163: docuname: username or courseid of destination for the file
17164: docudom: domain of user/course of destination for the file
17165: dir: dir to create (including any subdirs under userfiles)
17166:
17167: =item *
17168:
17169: removeuserfile(): removes a file that exists in userfiles
17170:
17171: Args:
17172: docuname: username or courseid of destination for the file
17173: docudom: domain of user/course of destination for the file
17174: fname: filname to delete (including any subdirs under userfiles)
17175:
17176: =item *
17177:
17178: removeuploadedurl(): convience function for removeuserfile()
17179:
17180: Args:
17181: url: a full /uploaded/... url to delete
17182:
1.747 albertel 17183: =item *
17184:
17185: get_portfile_permissions():
17186: Args:
17187: domain: domain of user or course contain the portfolio files
17188: user: name of user or num of course contain the portfolio files
17189: Returns:
17190: hashref of a dump of the proper file_permissions.db
17191:
17192:
17193: =item *
17194:
17195: get_access_controls():
17196:
17197: Args:
17198: current_permissions: the hash ref returned from get_portfile_permissions()
17199: group: (optional) the group you want the files associated with
17200: file: (optional) the file you want access info on
17201:
17202: Returns:
1.749 raeburn 17203: a hash (keys are file names) of hashes containing
17204: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17205: values are XML containing access control settings (see below)
1.747 albertel 17206:
17207: Internal notes:
17208:
1.749 raeburn 17209: access controls are stored in file_permissions.db as key=value pairs.
17210: key -> path to file/file_name\0uniqueID:scope_end_start
17211: where scope -> public,guest,course,group,domains or users.
17212: end -> UNIX time for end of access (0 -> no end date)
17213: start -> UNIX time for start of access
17214:
17215: value -> XML description of access control
17216: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17217: <start></start>
17218: <end></end>
17219:
17220: <password></password> for scope type = guest
17221:
17222: <domain></domain> for scope type = course or group
17223: <number></number>
17224: <roles id="">
17225: <role></role>
17226: <access></access>
17227: <section></section>
17228: <group></group>
17229: </roles>
17230:
17231: <dom></dom> for scope type = domains
17232:
17233: <users> for scope type = users
17234: <user>
17235: <uname></uname>
17236: <udom></udom>
17237: </user>
17238: </users>
17239: </scope>
17240:
17241: Access data is also aggregated for each file in an additional key=value pair:
17242: key -> path to file/file_name\0accesscontrol
17243: value -> reference to hash
17244: hash contains key = value pairs
17245: where key = uniqueID:scope_end_start
17246: value = UNIX time record was last updated
17247:
17248: Used to improve speed of look-ups of access controls for each file.
17249:
17250: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17251:
1.1172.2.13 raeburn 17252: =item *
17253:
1.749 raeburn 17254: modify_access_controls():
17255:
17256: Modifies access controls for a portfolio file
17257: Args
17258: 1. file name
17259: 2. reference to hash of required changes,
17260: 3. domain
17261: 4. username
17262: where domain,username are the domain of the portfolio owner
17263: (either a user or a course)
17264:
17265: Returns:
17266: 1. result of additions or updates ('ok' or 'error', with error message).
17267: 2. result of deletions ('ok' or 'error', with error message).
17268: 3. reference to hash of any new or updated access controls.
17269: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17270: key = integer (inbound ID)
1.1172.2.13 raeburn 17271: value = uniqueID
17272:
17273: =item *
17274:
17275: get_timebased_id():
17276:
17277: Attempts to get a unique timestamp-based suffix for use with items added to a
17278: course via the Course Editor (e.g., folders, composite pages,
17279: group bulletin boards).
17280:
17281: Args: (first three required; six others optional)
17282:
17283: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17284: docssequence, or name of group
17285:
17286: 2. keyid (alphanumeric): name of temporary locking key in hash,
17287: e.g., num, boardids
17288:
17289: 3. namespace: name of gdbm file used to store suffixes already assigned;
17290: file will be named nohist_namespace.db
17291:
17292: 4. cdom: domain of course; default is current course domain from %env
17293:
17294: 5. cnum: course number; default is current course number from %env
17295:
17296: 6. idtype: set to concat if an additional digit is to be appended to the
17297: unix timestamp to form the suffix, if the plain timestamp is already
17298: in use. Default is to not do this, but simply increment the unix
17299: timestamp by 1 until a unique key is obtained.
17300:
17301: 7. who: holder of locking key; defaults to user:domain for user.
17302:
17303: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
17304: retrying); default is 3.
17305:
17306: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
17307:
17308: Returns:
17309:
17310: 1. suffix obtained (numeric)
17311:
17312: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17313:
17314: 3. error: contains (localized) error message if an error occurred.
17315:
1.747 albertel 17316:
1.608 albertel 17317: =back
17318:
1.243 albertel 17319: =head2 HTTP Helper Routines
17320:
17321: =over 4
17322:
1.191 harris41 17323: =item *
17324:
17325: escape() : unpack non-word characters into CGI-compatible hex codes
17326:
17327: =item *
17328:
17329: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17330:
1.243 albertel 17331: =back
17332:
17333: =head1 PRIVATE SUBROUTINES
17334:
17335: =head2 Underlying communication routines (Shouldn't call)
17336:
17337: =over 4
17338:
17339: =item *
17340:
17341: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17342:
17343: =item *
17344:
17345: reply() : uses subreply to send a message to remote machine, logs all failures
17346:
17347: =item *
17348:
17349: critical() : passes a critical message to another server; if cannot
17350: get through then place message in connection buffer directory and
17351: returns con_delayed, if incapable of saving message, returns
17352: con_failed
17353:
17354: =item *
17355:
17356: reconlonc() : tries to reconnect lonc client processes.
17357:
17358: =back
17359:
17360: =head2 Resource Access Logging
17361:
17362: =over 4
17363:
17364: =item *
17365:
17366: flushcourselogs() : flush (save) buffer logs and access logs
17367:
17368: =item *
17369:
17370: courselog($what) : save message for course in hash
17371:
17372: =item *
17373:
17374: courseacclog($what) : save message for course using &courselog(). Perform
17375: special processing for specific resource types (problems, exams, quizzes, etc).
17376:
1.191 harris41 17377: =item *
17378:
17379: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17380: as a PerlChildExitHandler
1.243 albertel 17381:
17382: =back
17383:
17384: =head2 Other
17385:
17386: =over 4
17387:
17388: =item *
17389:
17390: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 17391:
17392: =back
17393:
17394: =cut
1.877 foxr 17395:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>