Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1172.2.146.2.27
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1172.2.146. .27(raeb 4:-25): # $Id: lonnet.pm,v 1.1172.2.146.2.26 2025/01/15 18:43:08 raeburn Exp $
1.178 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.169 harris41 28: ###
29:
1.971 jms 30: =pod
31:
1.972 jms 32: =head1 NAME
33:
34: Apache::lonnet.pm
35:
36: =head1 SYNOPSIS
37:
38: This file is an interface to the lonc processes of
39: the LON-CAPA network as well as set of elaborated functions for handling information
40: necessary for navigating through a given cluster of LON-CAPA machines within a
41: domain. There are over 40 specialized functions in this module which handle the
42: reading and transmission of metadata, user information (ids, names, environments, roles,
43: logs), file information (storage, reading, directories, extensions, replication, embedded
44: styles and descriptors), educational resources (course descriptions, section names and
45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
46: and from more descriptive phrases or explanations.
47:
48: This is part of the LearningOnline Network with CAPA project
49: described at http://www.lon-capa.org.
50:
1.971 jms 51: =head1 Package Variables
52:
53: These are largely undocumented, so if you decipher one please note it here.
54:
55: =over 4
56:
57: =item $processmarker
58:
59: Contains the time this process was started and this servers host id.
60:
61: =item $dumpcount
62:
63: Counts the number of times a message log flush has been attempted (regardless
64: of success) by this process. Used as part of the filename when messages are
65: delayed.
66:
67: =back
68:
69: =cut
70:
1.1 albertel 71: package Apache::lonnet;
72:
73: use strict;
1.8 www 74: use LWP::UserAgent();
1.486 www 75: use HTTP::Date;
1.977 amueller 76: use Image::Magick;
1.1172.2.107 raeburn 77: use CGI::Cookie;
1.977 amueller 78:
1.1172.2.104 raeburn 79: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
1.1138 raeburn 80: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
1.1172.2.114 raeburn 81: %managerstab $passwdmin);
1.871 albertel 82:
83: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
84: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
85: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 86: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 87:
1.1 albertel 88: use IO::Socket;
1.31 www 89: use GDBM_File;
1.208 albertel 90: use HTML::LCParser;
1.88 www 91: use Fcntl qw(:flock);
1.870 albertel 92: use Storable qw(thaw nfreeze);
1.1172.2.79 raeburn 93: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599 albertel 94: use Cache::Memcached;
1.676 albertel 95: use Digest::MD5;
1.790 albertel 96: use Math::Random;
1.1024 raeburn 97: use File::MMagic;
1.1172.2.142 raeburn 98: use Net::CIDR;
99: use Sys::Hostname::FQDN();
1.807 albertel 100: use LONCAPA qw(:DEFAULT :match);
1.740 www 101: use LONCAPA::Configuration;
1.1160 www 102: use LONCAPA::lonmetadata;
1.1172.2.22 raeburn 103: use LONCAPA::Lond;
1.1172.2.110 raeburn 104: use LONCAPA::transliterate;
1.1117 foxr 105:
1.1090 raeburn 106: use File::Copy;
1.676 albertel 107:
1.195 www 108: my $readit;
1.1172.2.79 raeburn 109: my $max_connection_retries = 20; # Or some such value.
1.1 albertel 110:
1.619 albertel 111: require Exporter;
112:
113: our @ISA = qw (Exporter);
114: our @EXPORT = qw(%env);
115:
1.1172.2.9 raeburn 116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729 www 117: {
118: my $logid;
1.1172.2.9 raeburn 119: sub write_log {
120: my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
121: if ($context eq 'course') {
122: if (($cnum eq '') || ($cdom eq '')) {
123: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
124: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
125: }
1.957 raeburn 126: }
1.1172.2.13 raeburn 127: $logid ++;
1.957 raeburn 128: my $now = time();
129: my $id=$now.'00000'.$$.'00000'.$logid;
1.1172.2.146. .1(raebu 130:22): my $ip = &get_requestor_ip();
1.1172.2.9 raeburn 131: my $logentry = {
132: $id => {
133: 'exe_uname' => $env{'user.name'},
134: 'exe_udom' => $env{'user.domain'},
135: 'exe_time' => $now,
1.1172.2.134 raeburn 136: 'exe_ip' => $ip,
1.1172.2.9 raeburn 137: 'delflag' => $delflag,
138: 'logentry' => $storehash,
139: 'uname' => $uname,
140: 'udom' => $udom,
141: }
142: };
143: return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729 www 144: }
145: }
1.1 albertel 146:
1.163 harris41 147: sub logtouch {
148: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 149: unless (-e "$execdir/logs/lonnet.log") {
1.1172.2.96 raeburn 150: open(my $fh,">>","$execdir/logs/lonnet.log");
1.163 harris41 151: close $fh;
152: }
153: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
154: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
155: }
156:
1.1 albertel 157: sub logthis {
158: my $message=shift;
159: my $execdir=$perlvar{'lonDaemons'};
160: my $now=time;
161: my $local=localtime($now);
1.1172.2.96 raeburn 162: if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
1.986 foxr 163: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
164: print $fh $logstring;
1.448 albertel 165: close($fh);
166: }
1.1 albertel 167: return 1;
168: }
169:
170: sub logperm {
171: my $message=shift;
172: my $execdir=$perlvar{'lonDaemons'};
173: my $now=time;
174: my $local=localtime($now);
1.1172.2.96 raeburn 175: if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
1.448 albertel 176: print $fh "$now:$message:$local\n";
177: close($fh);
178: }
1.1 albertel 179: return 1;
180: }
181:
1.850 albertel 182: sub create_connection {
1.853 albertel 183: my ($hostname,$lonid) = @_;
1.851 albertel 184: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 185: Type => SOCK_STREAM,
186: Timeout => 10);
187: return 0 if (!$client);
1.890 albertel 188: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 189: my $result = <$client>;
190: chomp($result);
191: return 1 if ($result eq 'done');
192: return 0;
193: }
194:
1.983 raeburn 195: sub get_server_timezone {
196: my ($cnum,$cdom) = @_;
197: my $home=&homeserver($cnum,$cdom);
198: if ($home ne 'no_host') {
199: my $cachetime = 24*3600;
200: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
201: if (defined($cached)) {
202: return $timezone;
203: } else {
204: my $timezone = &reply('servertimezone',$home);
205: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
206: }
207: }
208: }
1.850 albertel 209:
1.1106 raeburn 210: sub get_server_distarch {
211: my ($lonhost,$ignore_cache) = @_;
212: if (defined($lonhost)) {
213: if (!defined(&hostname($lonhost))) {
214: return;
215: }
216: my $cachetime = 12*3600;
217: if (!$ignore_cache) {
218: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
219: if (defined($cached)) {
220: return $distarch;
221: }
222: }
223: my $rep = &reply('serverdistarch',$lonhost);
1.1172.2.146. .26(raeb 224:-25): unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' ||
1.1106 raeburn 225: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
226: $rep eq '') {
227: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
228: }
229: }
230: return;
231: }
232:
1.993 raeburn 233: sub get_server_loncaparev {
1.1073 raeburn 234: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 235: if (defined($lonhost)) {
236: if (!defined(&hostname($lonhost))) {
237: undef($lonhost);
238: }
239: }
240: if (!defined($lonhost)) {
241: if (defined(&domain($dom,'primary'))) {
242: $lonhost=&domain($dom,'primary');
243: if ($lonhost eq 'no_host') {
244: undef($lonhost);
245: }
246: }
247: }
248: if (defined($lonhost)) {
1.1073 raeburn 249: my $cachetime = 12*3600;
250: if (!$ignore_cache) {
251: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
252: if (defined($cached)) {
253: return $loncaparev;
254: }
255: }
256: my ($answer,$loncaparev);
257: my @ids=¤t_machine_ids();
258: if (grep(/^\Q$lonhost\E$/,@ids)) {
259: $answer = $perlvar{'lonVersion'};
1.1081 raeburn 260: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 261: $loncaparev = $1;
262: }
263: } else {
264: $answer = &reply('serverloncaparev',$lonhost);
265: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
266: if ($caller eq 'loncron') {
267: my $ua=new LWP::UserAgent;
1.1082 raeburn 268: $ua->timeout(4);
1.1172.2.120 raeburn 269: my $hostname = &hostname($lonhost);
1.1073 raeburn 270: my $protocol = $protocol{$lonhost};
271: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 272: my $url = $protocol.'://'.$hostname.'/adm/about.html';
1.1073 raeburn 273: my $request=new HTTP::Request('GET',$url);
274: my $response=$ua->request($request);
275: unless ($response->is_error()) {
276: my $content = $response->content;
1.1081 raeburn 277: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073 raeburn 278: $loncaparev = $1;
279: }
280: }
281: } else {
282: $loncaparev = $loncaparevs{$lonhost};
283: }
1.1081 raeburn 284: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 285: $loncaparev = $1;
286: }
1.993 raeburn 287: }
1.1073 raeburn 288: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993 raeburn 289: }
290: }
291:
1.1074 raeburn 292: sub get_server_homeID {
293: my ($hostname,$ignore_cache,$caller) = @_;
294: unless ($ignore_cache) {
295: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
296: if (defined($cached)) {
297: return $serverhomeID;
298: }
299: }
300: my $cachetime = 12*3600;
301: my $serverhomeID;
302: if ($caller eq 'loncron') {
303: my @machine_ids = &machine_ids($hostname);
304: foreach my $id (@machine_ids) {
305: my $response = &reply('serverhomeID',$id);
306: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
307: $serverhomeID = $response;
308: last;
309: }
310: }
311: if ($serverhomeID eq '') {
312: $serverhomeID = $machine_ids[-1];
313: }
314: } else {
315: $serverhomeID = $serverhomeIDs{$hostname};
316: }
317: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
318: }
319:
1.1121 raeburn 320: sub get_remote_globals {
321: my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125 raeburn 322: my ($result,%returnhash,%whatneeded);
323: if (ref($whathash) eq 'HASH') {
1.1121 raeburn 324: foreach my $what (sort(keys(%{$whathash}))) {
325: my $hashid = $lonhost.'-'.$what;
1.1125 raeburn 326: my ($response,$cached);
1.1121 raeburn 327: unless ($ignore_cache) {
1.1125 raeburn 328: ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121 raeburn 329: }
330: if (defined($cached)) {
1.1125 raeburn 331: $returnhash{$what} = $response;
1.1121 raeburn 332: } else {
1.1125 raeburn 333: $whatneeded{$what} = 1;
1.1121 raeburn 334: }
335: }
1.1125 raeburn 336: if (keys(%whatneeded) == 0) {
337: $result = 'ok';
338: } else {
1.1121 raeburn 339: my $requested = &freeze_escape(\%whatneeded);
340: my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125 raeburn 341: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
342: ($rep eq 'unknown_cmd')) {
343: $result = $rep;
344: } else {
345: $result = 'ok';
1.1121 raeburn 346: my @pairs=split(/\&/,$rep);
1.1125 raeburn 347: foreach my $item (@pairs) {
348: my ($key,$value)=split(/=/,$item,2);
349: my $what = &unescape($key);
350: my $hashid = $lonhost.'-'.$what;
351: $returnhash{$what}=&thaw_unescape($value);
352: &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121 raeburn 353: }
354: }
355: }
356: }
1.1125 raeburn 357: return ($result,\%returnhash);
1.1121 raeburn 358: }
359:
1.1124 raeburn 360: sub remote_devalidate_cache {
1.1172.2.35 raeburn 361: my ($lonhost,$cachekeys) = @_;
362: my $items;
363: return unless (ref($cachekeys) eq 'ARRAY');
364: my $cachestr = join('&',@{$cachekeys});
365: return &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124 raeburn 366: }
367:
1.1172.2.146. .13(raeb 368:-23): sub sign_lti {
369:-23): my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
370:-23): my $chome;
371:-23): if (&domain($cdom) ne '') {
372:-23): if ($crsdef) {
373:-23): $chome = &homeserver($cnum,$cdom);
374:-23): } else {
375:-23): $chome = &domain($cdom,'primary');
376:-23): }
377:-23): }
378:-23): if ($cdom && $chome && ($chome ne 'no_host')) {
379:-23): if ((ref($paramsref) eq 'HASH') &&
380:-23): (ref($inforef) eq 'HASH')) {
381:-23): my $rep;
382:-23): if (grep { $_ eq $chome } ¤t_machine_ids()) {
383:-23): # domain information is hosted on this machine
384:-23): $rep =
385:-23): &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
386:-23): $context,$url,$ltinum,$keynum,
387:-23): $perlvar{'lonVersion'},
388:-23): $paramsref,$inforef);
389:-23): if (ref($rep) eq 'HASH') {
390:-23): return ('ok',$rep);
391:-23): }
392:-23): } else {
393:-23): my ($escurl,$params,$info);
394:-23): $escurl = &escape($url);
395:-23): if (ref($paramsref) eq 'HASH') {
396:-23): $params = &freeze_escape($paramsref);
397:-23): }
398:-23): if (ref($inforef) eq 'HASH') {
399:-23): $info = &freeze_escape($inforef);
400:-23): }
401:-23): $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
402:-23): }
403:-23): if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
404:-23): return ();
405:-23): } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
406:-23): ($inforef->{'respfmt'} eq 'to_authorization_header')) {
407:-23): return ('ok',$rep);
408:-23): } else {
409:-23): my %returnhash;
410:-23): foreach my $item (split(/\&/,$rep)) {
411:-23): my ($name,$value)=split(/\=/,$item);
412:-23): $returnhash{&unescape($name)}=&thaw_unescape($value);
413:-23): }
414:-23): return('ok',\%returnhash);
415:-23): }
416:-23): } else {
417:-23): return ();
418:-23): }
419:-23): } else {
420:-23): return ();
421:-23): &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
422:-23): }
423:-23): }
424:-23):
1.1 albertel 425: # -------------------------------------------------- Non-critical communication
426: sub subreply {
427: my ($cmd,$server)=@_;
1.838 albertel 428: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 429: #
430: # With loncnew process trimming, there's a timing hole between lonc server
431: # process exit and the master server picking up the listen on the AF_UNIX
432: # socket. In that time interval, a lock file will exist:
433:
434: my $lockfile=$peerfile.".lock";
435: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
1.1172.2.79 raeburn 436: sleep(0.1);
1.549 foxr 437: }
438: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 439: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 440: #
1.550 foxr 441: # We'll give the connection a few tries before abandoning it. If
442: # connection is not possible, we'll con_lost back to the client.
443: #
444: my $client;
445: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
446: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
447: Type => SOCK_STREAM,
448: Timeout => 10);
1.869 albertel 449: if ($client) {
1.550 foxr 450: last; # Connected!
1.850 albertel 451: } else {
1.853 albertel 452: &create_connection(&hostname($server),$server);
1.550 foxr 453: }
1.1172.2.79 raeburn 454: sleep(0.1); # Try again later if failed connection.
1.550 foxr 455: }
456: my $answer;
457: if ($client) {
1.704 albertel 458: print $client "sethost:$server:$cmd\n";
1.550 foxr 459: $answer=<$client>;
460: if (!$answer) { $answer="con_lost"; }
461: chomp($answer);
462: } else {
463: $answer = 'con_lost'; # Failed connection.
464: }
1.1 albertel 465: return $answer;
466: }
467:
468: sub reply {
469: my ($cmd,$server)=@_;
1.838 albertel 470: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 471: my $answer=subreply($cmd,$server);
1.65 www 472: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.1172.2.111 raeburn 473: my $logged = $cmd;
474: if ($cmd =~ /^encrypt:([^:]+):/) {
475: my $subcmd = $1;
476: if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
477: ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 478:-23): ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
479:-23): ($subcmd eq 'put')) {
1.1172.2.111 raeburn 480: (undef,undef,my @rest) = split(/:/,$cmd);
481: if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
482: splice(@rest,2,1,'Hidden');
483: } elsif ($subcmd eq 'passwd') {
484: splice(@rest,2,2,('Hidden','Hidden'));
485: } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
1.1172.2.146. .13(raeb 486:-23): ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
1.1172.2.111 raeburn 487: splice(@rest,3,1,'Hidden');
488: }
489: $logged = join(':',('encrypt:'.$subcmd,@rest));
490: }
491: }
492: &logthis("<font color=\"blue\">WARNING:".
493: " $logged to $server returned $answer</font>");
1.12 www 494: }
1.1 albertel 495: return $answer;
496: }
497:
498: # ----------------------------------------------------------- Send USR1 to lonc
499:
500: sub reconlonc {
1.891 albertel 501: my ($lonid) = @_;
502: if ($lonid) {
1.1172.2.72 raeburn 503: my $hostname = &hostname($lonid);
1.891 albertel 504: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
505: if ($hostname && -e $peerfile) {
506: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
507: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
508: Type => SOCK_STREAM,
509: Timeout => 10);
510: if ($client) {
511: print $client ("reset_retries\n");
512: my $answer=<$client>;
513: #reset just this one.
514: }
515: }
516: return;
517: }
518:
1.836 www 519: &logthis("Trying to reconnect lonc");
1.1 albertel 520: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.1172.2.96 raeburn 521: if (open(my $fh,"<",$loncfile)) {
1.1 albertel 522: my $loncpid=<$fh>;
523: chomp($loncpid);
524: if (kill 0 => $loncpid) {
525: &logthis("lonc at pid $loncpid responding, sending USR1");
526: kill USR1 => $loncpid;
527: sleep 1;
1.836 www 528: } else {
1.12 www 529: &logthis(
1.672 albertel 530: "<font color=\"blue\">WARNING:".
1.12 www 531: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 532: }
533: } else {
1.836 www 534: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 535: }
536: }
537:
538: # ------------------------------------------------------ Critical communication
1.12 www 539:
1.1 albertel 540: sub critical {
541: my ($cmd,$server)=@_;
1.838 albertel 542: unless (&hostname($server)) {
1.672 albertel 543: &logthis("<font color=\"blue\">WARNING:".
1.89 www 544: " Critical message to unknown server ($server)</font>");
545: return 'no_such_host';
546: }
1.1 albertel 547: my $answer=reply($cmd,$server);
548: if ($answer eq 'con_lost') {
1.1172.2.72 raeburn 549: &reconlonc($server);
1.589 albertel 550: my $answer=reply($cmd,$server);
1.1 albertel 551: if ($answer eq 'con_lost') {
552: my $now=time;
553: my $middlename=$cmd;
1.5 www 554: $middlename=substr($middlename,0,16);
1.1 albertel 555: $middlename=~s/\W//g;
556: my $dfilename=
1.305 www 557: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
558: $dumpcount++;
1.1 albertel 559: {
1.448 albertel 560: my $dfh;
1.1172.2.96 raeburn 561: if (open($dfh,">",$dfilename)) {
1.448 albertel 562: print $dfh "$cmd\n";
563: close($dfh);
564: }
1.1 albertel 565: }
1.1172.2.79 raeburn 566: sleep 1;
1.1 albertel 567: my $wcmd='';
568: {
1.448 albertel 569: my $dfh;
1.1172.2.96 raeburn 570: if (open($dfh,"<",$dfilename)) {
1.448 albertel 571: $wcmd=<$dfh>;
572: close($dfh);
573: }
1.1 albertel 574: }
575: chomp($wcmd);
1.7 www 576: if ($wcmd eq $cmd) {
1.672 albertel 577: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 578: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 579: &logperm("D:$server:$cmd");
580: return 'con_delayed';
581: } else {
1.672 albertel 582: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 583: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 584: &logperm("F:$server:$cmd");
585: return 'con_failed';
586: }
587: }
588: }
589: return $answer;
1.405 albertel 590: }
591:
1.755 albertel 592: # ------------------------------------------- check if return value is an error
593:
594: sub error {
595: my ($result) = @_;
1.756 albertel 596: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 597: if ($2 == 2) { return undef; }
598: return $1;
599: }
600: return undef;
601: }
602:
1.783 albertel 603: sub convert_and_load_session_env {
604: my ($lonidsdir,$handle)=@_;
605: my @profile;
606: {
1.917 albertel 607: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
608: if (!$opened) {
1.915 albertel 609: return 0;
610: }
1.783 albertel 611: flock($idf,LOCK_SH);
612: @profile=<$idf>;
613: close($idf);
614: }
615: my %temp_env;
616: foreach my $line (@profile) {
1.786 albertel 617: if ($line !~ m/=/) {
618: return 0;
619: }
1.783 albertel 620: chomp($line);
621: my ($envname,$envvalue)=split(/=/,$line,2);
622: $temp_env{&unescape($envname)} = &unescape($envvalue);
623: }
624: unlink("$lonidsdir/$handle.id");
625: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
626: 0640)) {
627: %disk_env = %temp_env;
628: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
629: untie(%disk_env);
630: }
1.786 albertel 631: return 1;
1.783 albertel 632: }
633:
1.374 www 634: # ------------------------------------------- Transfer profile into environment
1.780 albertel 635: my $env_loaded;
636: sub transfer_profile_to_env {
1.788 albertel 637: my ($lonidsdir,$handle,$force_transfer) = @_;
638: if (!$force_transfer && $env_loaded) { return; }
1.374 www 639:
1.720 albertel 640: if (!defined($lonidsdir)) {
641: $lonidsdir = $perlvar{'lonIDsDir'};
642: }
643: if (!defined($handle)) {
644: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
645: }
646:
1.786 albertel 647: my $convert;
648: {
1.917 albertel 649: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
650: if (!$opened) {
1.915 albertel 651: return;
652: }
1.786 albertel 653: flock($idf,LOCK_SH);
654: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
655: &GDBM_READER(),0640)) {
656: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
657: untie(%disk_env);
658: } else {
659: $convert = 1;
660: }
661: }
662: if ($convert) {
663: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
664: &logthis("Failed to load session, or convert session.");
665: }
1.374 www 666: }
1.783 albertel 667:
1.786 albertel 668: my %remove;
1.783 albertel 669: while ( my $envname = each(%env) ) {
1.433 matthew 670: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
671: if ($time < time-300) {
1.783 albertel 672: $remove{$key}++;
1.433 matthew 673: }
674: }
675: }
1.783 albertel 676:
1.619 albertel 677: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 678: $env_loaded=1;
1.783 albertel 679: foreach my $expired_key (keys(%remove)) {
1.433 matthew 680: &delenv($expired_key);
1.374 www 681: }
1.1 albertel 682: }
683:
1.916 albertel 684: # ---------------------------------------------------- Check for valid session
685: sub check_for_valid_session {
1.1172.2.96 raeburn 686: my ($r,$name,$userhashref,$domref) = @_;
1.916 albertel 687: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1172.2.108 raeburn 688: my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
1.1155 raeburn 689: if ($name eq 'lonDAV') {
690: $lonidsdir=$r->dir_config('lonDAVsessDir');
691: } else {
692: $lonidsdir=$r->dir_config('lonIDsDir');
1.1172.2.108 raeburn 693: if ($name eq '') {
694: $name = 'lonID';
695: }
696: }
697: if ($name eq 'lonID') {
698: $secure = 'lonSID';
699: $linkname = 'lonLinkID';
700: $pubname = 'lonPubID';
701: if (exists($cookies{$secure})) {
702: $lonid=$cookies{$secure};
703: } elsif (exists($cookies{$name})) {
704: $lonid=$cookies{$name};
705: } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
706: $lonid=$cookies{$linkname};
707: } elsif (exists($cookies{$pubname})) {
708: $lonid=$cookies{$pubname};
709: }
710: } else {
711: $lonid=$cookies{$name};
712: }
713: return undef if (!$lonid);
714:
715: my $handle=&LONCAPA::clean_handle($lonid->value);
716: if (-l "$lonidsdir/$handle.id") {
717: my $link = readlink("$lonidsdir/$handle.id");
718: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
719: $handle = $1;
720: }
1.1155 raeburn 721: }
1.1172.2.96 raeburn 722: if (!-e "$lonidsdir/$handle.id") {
723: if ((ref($domref)) && ($name eq 'lonID') &&
724: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
725: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
726: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
727: $$domref = $possudom;
728: }
729: }
730: return undef;
731: }
1.916 albertel 732:
1.917 albertel 733: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
734: return undef if (!$opened);
1.916 albertel 735:
736: flock($idf,LOCK_SH);
737: my %disk_env;
738: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
739: &GDBM_READER(),0640)) {
740: return undef;
741: }
742:
743: if (!defined($disk_env{'user.name'})
744: || !defined($disk_env{'user.domain'})) {
1.1172.2.107 raeburn 745: untie(%disk_env);
1.916 albertel 746: return undef;
747: }
1.1172.2.18 raeburn 748:
1.1172.2.36 raeburn 749: if (ref($userhashref) eq 'HASH') {
750: $userhashref->{'name'} = $disk_env{'user.name'};
751: $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1172.2.142 raeburn 752: if ($disk_env{'request.role'}) {
753: $userhashref->{'role'} = $disk_env{'request.role'};
754: }
1.1172.2.146. .13(raeb 755:-23): $userhashref->{'lti'} = $disk_env{'request.lti.login'};
756:-23): if ($userhashref->{'lti'}) {
757:-23): $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
758:-23): $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
759:-23): }
1.1172.2.18 raeburn 760: }
1.1172.2.107 raeburn 761: untie(%disk_env);
1.1172.2.18 raeburn 762:
1.916 albertel 763: return $handle;
764: }
765:
1.830 albertel 766: sub timed_flock {
767: my ($file,$lock_type) = @_;
768: my $failed=0;
769: eval {
770: local $SIG{__DIE__}='DEFAULT';
771: local $SIG{ALRM}=sub {
772: $failed=1;
773: die("failed lock");
774: };
775: alarm(13);
776: flock($file,$lock_type);
777: alarm(0);
778: };
779: if ($failed) {
780: return undef;
781: } else {
782: return 1;
783: }
784: }
785:
1.1172.2.107 raeburn 786: sub get_sessionfile_vars {
787: my ($handle,$lonidsdir,$storearr) = @_;
788: my %returnhash;
789: unless (ref($storearr) eq 'ARRAY') {
790: return %returnhash;
791: }
792: if (-l "$lonidsdir/$handle.id") {
793: my $link = readlink("$lonidsdir/$handle.id");
794: if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
795: $handle = $1;
796: }
797: }
798: if ((-e "$lonidsdir/$handle.id") &&
799: ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
800: my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
801: if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
802: if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
803: flock($idf,LOCK_SH);
804: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
805: &GDBM_READER(),0640)) {
806: foreach my $item (@{$storearr}) {
807: $returnhash{$item} = $disk_env{$item};
808: }
809: untie(%disk_env);
810: }
811: }
812: }
813: }
814: return %returnhash;
815: }
816:
1.5 www 817: # ---------------------------------------------------------- Append Environment
818:
819: sub appenv {
1.949 raeburn 820: my ($newenv,$roles) = @_;
821: if (ref($newenv) eq 'HASH') {
822: foreach my $key (keys(%{$newenv})) {
823: my $refused = 0;
824: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
825: $refused = 1;
826: if (ref($roles) eq 'ARRAY') {
1.1172.2.40 raeburn 827: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949 raeburn 828: if (grep(/^\Q$role\E$/,@{$roles})) {
829: $refused = 0;
830: }
831: }
832: }
833: if ($refused) {
834: &logthis("<font color=\"blue\">WARNING: ".
835: "Attempt to modify environment ".$key." to ".$newenv->{$key}
836: .'</font>');
837: delete($newenv->{$key});
838: } else {
839: $env{$key}=$newenv->{$key};
840: }
841: }
1.1172.2.96 raeburn 842: my $lonids = $perlvar{'lonIDsDir'};
843: if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
844: my $opened = open(my $env_file,'+<',$env{'user.environment'});
845: if ($opened
846: && &timed_flock($env_file,LOCK_EX)
847: &&
848: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
849: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
850: while (my ($key,$value) = each(%{$newenv})) {
851: $disk_env{$key} = $value;
852: }
853: untie(%disk_env);
854: }
1.35 www 855: }
1.191 harris41 856: }
1.56 www 857: return 'ok';
858: }
859: # ----------------------------------------------------- Delete from Environment
860:
861: sub delenv {
1.1104 raeburn 862: my ($delthis,$regexp,$roles) = @_;
863: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
864: my $refused = 1;
865: if (ref($roles) eq 'ARRAY') {
866: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
867: if (grep(/^\Q$role\E$/,@{$roles})) {
868: $refused = 0;
869: }
870: }
871: if ($refused) {
872: &logthis("<font color=\"blue\">WARNING: ".
873: "Attempt to delete from environment ".$delthis);
874: return 'error';
875: }
1.56 www 876: }
1.917 albertel 877: my $opened = open(my $env_file,'+<',$env{'user.environment'});
878: if ($opened
1.915 albertel 879: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 880: &&
881: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
882: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 883: foreach my $key (keys(%disk_env)) {
1.987 raeburn 884: if ($regexp) {
885: if ($key=~/^$delthis/) {
886: delete($env{$key});
887: delete($disk_env{$key});
888: }
889: } else {
890: if ($key=~/^\Q$delthis\E/) {
891: delete($env{$key});
892: delete($disk_env{$key});
893: }
894: }
1.448 albertel 895: }
1.783 albertel 896: untie(%disk_env);
1.5 www 897: }
898: return 'ok';
1.369 albertel 899: }
900:
1.790 albertel 901: sub get_env_multiple {
902: my ($name) = @_;
903: my @values;
904: if (defined($env{$name})) {
905: # exists is it an array
906: if (ref($env{$name})) {
907: @values=@{ $env{$name} };
908: } else {
909: $values[0]=$env{$name};
910: }
911: }
912: return(@values);
913: }
914:
1.958 www 915: # ------------------------------------------------------------------- Locking
916:
917: sub set_lock {
918: my ($text)=@_;
919: $locknum++;
920: my $id=$$.'-'.$locknum;
921: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
922: 'session.lock.'.$id => $text});
923: return $id;
924: }
925:
926: sub get_locks {
927: my $num=0;
928: my %texts=();
929: foreach my $lock (split(/\,/,$env{'session.locks'})) {
930: if ($lock=~/\w/) {
931: $num++;
932: $texts{$lock}=$env{'session.lock.'.$lock};
933: }
934: }
935: return ($num,%texts);
936: }
937:
938: sub remove_lock {
939: my ($id)=@_;
940: my $newlocks='';
941: foreach my $lock (split(/\,/,$env{'session.locks'})) {
942: if (($lock=~/\w/) && ($lock ne $id)) {
943: $newlocks.=','.$lock;
944: }
945: }
946: &appenv({'session.locks' => $newlocks});
947: &delenv('session.lock.'.$id);
948: }
949:
950: sub remove_all_locks {
951: my $activelocks=$env{'session.locks'};
952: foreach my $lock (split(/\,/,$env{'session.locks'})) {
953: if ($lock=~/\w/) {
954: &remove_lock($lock);
955: }
956: }
957: }
958:
959:
1.369 albertel 960: # ------------------------------------------ Find out current server userload
961: sub userload {
962: my $numusers=0;
963: {
964: opendir(LONIDS,$perlvar{'lonIDsDir'});
965: my $filename;
966: my $curtime=time;
967: while ($filename=readdir(LONIDS)) {
1.925 albertel 968: next if ($filename eq '.' || $filename eq '..');
969: next if ($filename =~ /publicuser_\d+\.id/);
1.1172.2.112 raeburn 970: next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
1.404 albertel 971: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 972: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 973: }
974: closedir(LONIDS);
975: }
976: my $userloadpercent=0;
977: my $maxuserload=$perlvar{'lonUserLoadLim'};
978: if ($maxuserload) {
1.371 albertel 979: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 980: }
1.372 albertel 981: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 982: return $userloadpercent;
1.283 www 983: }
984:
1.1 albertel 985: # ------------------------------ Find server with least workload from spare.tab
1.11 www 986:
1.1 albertel 987: sub spareserver {
1.1172.2.142 raeburn 988: my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 989: my $spare_server;
1.370 albertel 990: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 991: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
992: : $userloadpercent;
1.1083 raeburn 993: my ($uint_dom,$remotesessions);
994: if (($udom ne '') && (&domain($udom) ne '')) {
995: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
996: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
997: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
998: $remotesessions = $udomdefaults{'remotesessions'};
999: }
1.1123 raeburn 1000: my $spareshash = &this_host_spares($udom);
1001: if (ref($spareshash) eq 'HASH') {
1002: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1003: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1172.2.62 raeburn 1004: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
1005: $try_server));
1.1123 raeburn 1006: ($spare_server, $lowest_load) =
1007: &compare_server_load($try_server, $spare_server, $lowest_load);
1008: }
1.1083 raeburn 1009: }
1.784 albertel 1010:
1.1123 raeburn 1011: my $found_server = ($spare_server ne '' && $lowest_load < 100);
1012:
1013: if (!$found_server) {
1014: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1015: foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1172.2.62 raeburn 1016: next unless (&spare_can_host($udom,$uint_dom,
1017: $remotesessions,$try_server));
1.1123 raeburn 1018: ($spare_server, $lowest_load) =
1019: &compare_server_load($try_server, $spare_server, $lowest_load);
1020: }
1021: }
1022: }
1.784 albertel 1023: }
1024:
1025: if (!$want_server_name) {
1.1001 raeburn 1026: if (defined($spare_server)) {
1027: my $hostname = &hostname($spare_server);
1.1083 raeburn 1028: if (defined($hostname)) {
1.1172.2.120 raeburn 1029: my $protocol = 'http';
1030: if ($protocol{$spare_server} eq 'https') {
1031: $protocol = $protocol{$spare_server};
1032: }
1.1172.2.142 raeburn 1033: my $alias = &Apache::lonnet::use_proxy_alias($r,$spare_server);
1034: $hostname = $alias if ($alias ne '');
1.1001 raeburn 1035: $spare_server = $protocol.'://'.$hostname;
1036: }
1037: }
1.784 albertel 1038: }
1039: return $spare_server;
1040: }
1041:
1042: sub compare_server_load {
1.1172.2.41 raeburn 1043: my ($try_server, $spare_server, $lowest_load, $required) = @_;
1044:
1045: if ($required) {
1046: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
1047: my $remoterev = &get_server_loncaparev(undef,$try_server);
1048: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
1049: if (($major eq '' && $minor eq '') ||
1050: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
1051: return ($spare_server,$lowest_load);
1052: }
1053: }
1.784 albertel 1054:
1055: my $loadans = &reply('load', $try_server);
1056: my $userloadans = &reply('userload',$try_server);
1057:
1058: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114 raeburn 1059: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 1060: }
1061:
1062: my $load;
1063: if ($loadans =~ /\d/) {
1064: if ($userloadans =~ /\d/) {
1065: #both are numbers, pick the bigger one
1066: $load = ($loadans > $userloadans) ? $loadans
1067: : $userloadans;
1.411 albertel 1068: } else {
1.784 albertel 1069: $load = $loadans;
1.411 albertel 1070: }
1.784 albertel 1071: } else {
1072: $load = $userloadans;
1073: }
1074:
1075: if (($load =~ /\d/) && ($load < $lowest_load)) {
1076: $spare_server = $try_server;
1077: $lowest_load = $load;
1.370 albertel 1078: }
1.784 albertel 1079: return ($spare_server,$lowest_load);
1.202 matthew 1080: }
1.914 albertel 1081:
1082: # --------------------------- ask offload servers if user already has a session
1083: sub find_existing_session {
1084: my ($udom,$uname) = @_;
1.1123 raeburn 1085: my $spareshash = &this_host_spares($udom);
1086: if (ref($spareshash) eq 'HASH') {
1087: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
1088: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1089: return $try_server if (&has_user_session($try_server, $udom, $uname));
1090: }
1091: }
1092: if (ref($spareshash->{'default'}) eq 'ARRAY') {
1093: foreach my $try_server (@{ $spareshash->{'default'} }) {
1094: return $try_server if (&has_user_session($try_server, $udom, $uname));
1095: }
1096: }
1.914 albertel 1097: }
1098: return;
1099: }
1100:
1.1172.2.146. .13(raeb 1101:-23): sub delusersession {
1102:-23): my ($lonid,$udom,$uname) = @_;
1103:-23): my $uprimary_id = &domain($udom,'primary');
1104:-23): my $uintdom = &internet_dom($uprimary_id);
1105:-23): my $intdom = &internet_dom($lonid);
1106:-23): my $serverhomedom = &host_domain($lonid);
1107:-23): if (($uintdom ne '') && ($uintdom eq $intdom)) {
1108:-23): return &reply(join(':','delusersession',
1109:-23): map {&escape($_)} ($udom,$uname)),$lonid);
1110:-23): }
1111:-23): return;
1112:-23): }
1113:-23):
1114:-23):
1.1172.2.107 raeburn 1115: # check if user's browser sent load balancer cookie and server still has session
1116: # and is not overloaded.
1117: sub check_for_balancer_cookie {
1118: my ($r,$update_mtime) = @_;
1119: my ($otherserver,$cookie);
1120: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1121: if (exists($cookies{'balanceID'})) {
1122: my $balid = $cookies{'balanceID'};
1123: $cookie=&LONCAPA::clean_handle($balid->value);
1124: my $balancedir=$r->dir_config('lonBalanceDir');
1125: if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
1126: if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
1127: my ($possudom,$possuname) = ($1,$2);
1128: my $has_session = 0;
1129: if ((&domain($possudom) ne '') &&
1130: (&homeserver($possuname,$possudom) ne 'no_host')) {
1131: my $try_server;
1132: my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
1133: if ($opened) {
1134: flock($idf,LOCK_SH);
1135: while (my $line = <$idf>) {
1136: chomp($line);
1137: if (&hostname($line) ne '') {
1138: $try_server = $line;
1139: last;
1140: }
1141: }
1142: close($idf);
1143: if (($try_server) &&
1144: (&has_user_session($try_server,$possudom,$possuname))) {
1145: my $lowest_load = 30000;
1146: ($otherserver,$lowest_load) =
1147: &compare_server_load($try_server,undef,$lowest_load);
1148: if ($otherserver ne '' && $lowest_load < 100) {
1149: $has_session = 1;
1150: } else {
1151: undef($otherserver);
1152: }
1153: }
1154: }
1155: }
1156: if ($has_session) {
1157: if ($update_mtime) {
1158: my $atime = my $mtime = time;
1159: utime($atime,$mtime,"$balancedir/$cookie.id");
1160: }
1161: } else {
1162: unlink("$balancedir/$cookie.id");
1163: }
1164: }
1165: }
1166: }
1167: return ($otherserver,$cookie);
1168: }
1169:
1.1172.2.130 raeburn 1170: sub updatebalcookie {
1171: my ($cookie,$balancer,$lastentry)=@_;
1172: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1173: my ($udom,$uname) = ($1,$2);
1174: my $uprimary_id = &domain($udom,'primary');
1175: my $uintdom = &internet_dom($uprimary_id);
1176: my $intdom = &internet_dom($balancer);
1177: my $serverhomedom = &host_domain($balancer);
1178: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1179: return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
1180: }
1181: }
1182: return;
1183: }
1184:
1.1172.2.107 raeburn 1185: sub delbalcookie {
1186: my ($cookie,$balancer) =@_;
1187: if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
1188: my ($udom,$uname) = ($1,$2);
1189: my $uprimary_id = &domain($udom,'primary');
1190: my $uintdom = &internet_dom($uprimary_id);
1191: my $intdom = &internet_dom($balancer);
1192: my $serverhomedom = &host_domain($balancer);
1193: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1.1172.2.130 raeburn 1194: return &reply('delbalcookie:'.&escape($cookie),$balancer);
1.1172.2.107 raeburn 1195: }
1196: }
1197: }
1198:
1.914 albertel 1199: # -------------------------------- ask if server already has a session for user
1200: sub has_user_session {
1201: my ($lonid,$udom,$uname) = @_;
1202: my $result = &reply(join(':','userhassession',
1203: map {&escape($_)} ($udom,$uname)),$lonid);
1204: return 1 if ($result eq 'ok');
1205:
1206: return 0;
1207: }
1208:
1.1076 raeburn 1209: # --------- determine least loaded server in a user's domain which allows login
1210:
1211: sub choose_server {
1.1172.2.47 raeburn 1212: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076 raeburn 1213: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077 raeburn 1214: my %servers = &get_servers($udom);
1.1076 raeburn 1215: my $lowest_load = 30000;
1.1172.2.47 raeburn 1216: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1217: if ($skiploadbal) {
1218: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1219: unless (defined($cached)) {
1220: my $cachetime = 60*60*24;
1221: my %domconfig =
1222: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1223: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1224: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1225: $cachetime);
1226: }
1227: }
1228: }
1.1076 raeburn 1229: foreach my $lonhost (keys(%servers)) {
1.1115 raeburn 1230: my $loginvia;
1.1172.2.47 raeburn 1231: if ($skiploadbal) {
1232: if (ref($balancers) eq 'HASH') {
1233: next if (exists($balancers->{$lonhost}));
1234: }
1235: }
1.1115 raeburn 1236: if ($checkloginvia) {
1237: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116 raeburn 1238: if ($loginvia) {
1239: my ($server,$path) = split(/:/,$loginvia);
1240: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1241: &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116 raeburn 1242: if ($login_host eq $server) {
1243: $portal_path = $path;
1.1151 raeburn 1244: $isredirect = 1;
1.1116 raeburn 1245: }
1246: } else {
1247: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1248: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116 raeburn 1249: if ($login_host eq $lonhost) {
1250: $portal_path = '';
1.1151 raeburn 1251: $isredirect = '';
1.1116 raeburn 1252: }
1253: }
1254: } else {
1.1076 raeburn 1255: ($login_host, $lowest_load) =
1.1172.2.41 raeburn 1256: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076 raeburn 1257: }
1258: }
1259: if ($login_host ne '') {
1.1116 raeburn 1260: $hostname = &hostname($login_host);
1.1076 raeburn 1261: }
1.1172.2.88 raeburn 1262: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076 raeburn 1263: }
1264:
1.1172.2.123 raeburn 1265: sub get_course_sessions {
1266: my ($cnum,$cdom,$lastactivity) = @_;
1267: my %servers = &internet_dom_servers($cdom);
1268: my %returnhash;
1269: foreach my $server (sort(keys(%servers))) {
1270: my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
1271: my @pairs=split(/\&/,$rep);
1272: unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
1273: foreach my $item (@pairs) {
1274: my ($key,$value)=split(/=/,$item,2);
1275: $key = &unescape($key);
1276: next if ($key =~ /^error: 2 /);
1277: if (exists($returnhash{$key})) {
1278: next if ($value < $returnhash{$key});
1279: }
1280: $returnhash{$key}=$value;
1281: }
1282: }
1283: }
1284: return %returnhash;
1285: }
1286:
1.202 matthew 1287: # --------------------------------------------- Try to change a user's password
1288:
1289: sub changepass {
1.799 raeburn 1290: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 1291: $currentpass = &escape($currentpass);
1292: $newpass = &escape($newpass);
1.1030 raeburn 1293: my $lonhost = $perlvar{'lonHostID'};
1294: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 1295: $server);
1296: if (! $answer) {
1297: &logthis("No reply on password change request to $server ".
1298: "by $uname in domain $udom.");
1299: } elsif ($answer =~ "^ok") {
1300: &logthis("$uname in $udom successfully changed their password ".
1301: "on $server.");
1302: } elsif ($answer =~ "^pwchange_failure") {
1303: &logthis("$uname in $udom was unable to change their password ".
1304: "on $server. The action was blocked by either lcpasswd ".
1305: "or pwchange");
1306: } elsif ($answer =~ "^non_authorized") {
1307: &logthis("$uname in $udom did not get their password correct when ".
1308: "attempting to change it on $server.");
1309: } elsif ($answer =~ "^auth_mode_error") {
1310: &logthis("$uname in $udom attempted to change their password despite ".
1311: "not being locally or internally authenticated on $server.");
1312: } elsif ($answer =~ "^unknown_user") {
1313: &logthis("$uname in $udom attempted to change their password ".
1314: "on $server but were unable to because $server is not ".
1315: "their home server.");
1316: } elsif ($answer =~ "^refused") {
1317: &logthis("$server refused to change $uname in $udom password because ".
1318: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 1319: } elsif ($answer =~ "invalid_client") {
1320: &logthis("$server refused to change $uname in $udom password because ".
1321: "it was a reset by e-mail originating from an invalid server.");
1.1172.2.113 raeburn 1322: } elsif ($answer =~ "^prioruse") {
1323: &logthis("$server refused to change $uname in $udom password because ".
1324: "the password had been used before");
1.202 matthew 1325: }
1326: return $answer;
1.1 albertel 1327: }
1328:
1.169 harris41 1329: # ----------------------- Try to determine user's current authentication scheme
1330:
1331: sub queryauthenticate {
1332: my ($uname,$udom)=@_;
1.456 albertel 1333: my $uhome=&homeserver($uname,$udom);
1.1172.2.146. .5(raebu 1334:22): if ((!$uhome) || ($uhome eq 'no_host')) {
1.456 albertel 1335: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1336: return 'no_host';
1337: }
1338: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1339: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1340: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 1341: }
1.456 albertel 1342: return $answer;
1.169 harris41 1343: }
1344:
1.1 albertel 1345: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1346:
1.1 albertel 1347: sub authenticate {
1.1073 raeburn 1348: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1349: $upass=&escape($upass);
1350: $uname= &LONCAPA::clean_username($uname);
1.836 www 1351: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1352: my $newhome;
1.836 www 1353: if ((!$uhome) || ($uhome eq 'no_host')) {
1354: # Maybe the machine was offline and only re-appeared again recently?
1355: &reconlonc();
1356: # One more
1.952 raeburn 1357: $uhome=&homeserver($uname,$udom,1);
1358: if (($uhome eq 'no_host') && $checkdefauth) {
1359: if (defined(&domain($udom,'primary'))) {
1360: $newhome=&domain($udom,'primary');
1361: }
1362: if ($newhome ne '') {
1363: $uhome = $newhome;
1364: }
1365: }
1.836 www 1366: if ((!$uhome) || ($uhome eq 'no_host')) {
1367: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1368: return 'no_host';
1369: }
1.1 albertel 1370: }
1.1073 raeburn 1371: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1372: if ($answer eq 'authorized') {
1.952 raeburn 1373: if ($newhome) {
1374: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1375: return 'no_account_on_host';
1376: } else {
1377: &logthis("User $uname at $udom authorized by $uhome");
1378: return $uhome;
1379: }
1.471 albertel 1380: }
1381: if ($answer eq 'non_authorized') {
1382: &logthis("User $uname at $udom rejected by $uhome");
1.1172.2.146. .5(raebu 1383:22): return 'no_host';
1.9 www 1384: }
1.471 albertel 1385: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1386: return 'no_host';
1387: }
1388:
1.1172.2.146. .6(raebu 1389:22): sub can_switchserver {
1390:22): my ($udom,$home) = @_;
1391:22): my ($canswitch,@intdoms);
1392:22): my $internet_names = &get_internet_names($home);
1393:22): if (ref($internet_names) eq 'ARRAY') {
1394:22): @intdoms = @{$internet_names};
1395:22): }
1396:22): my $uint_dom = &internet_dom(&domain($udom,'primary'));
1397:22): if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
1398:22): $canswitch = 1;
1399:22): } else {
1400:22): my $serverhomeID = &get_server_homeID(&hostname($home));
1401:22): my $serverhomedom = &host_domain($serverhomeID);
1402:22): my %defdomdefaults = &get_domain_defaults($serverhomedom);
1403:22): my %udomdefaults = &get_domain_defaults($udom);
1404:22): my $remoterev = &get_server_loncaparev('',$home);
1405:22): $canswitch = &can_host_session($udom,$home,$remoterev,
1406:22): $udomdefaults{'remotesessions'},
1407:22): $defdomdefaults{'hostedsessions'});
1408:22): }
1409:22): return $canswitch;
1410:22): }
1411:22):
1.1073 raeburn 1412: sub can_host_session {
1.1074 raeburn 1413: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073 raeburn 1414: my $canhost = 1;
1.1074 raeburn 1415: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073 raeburn 1416: if (ref($remotesessions) eq 'HASH') {
1417: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074 raeburn 1418: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073 raeburn 1419: $canhost = 0;
1420: } else {
1421: $canhost = 1;
1422: }
1423: }
1424: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074 raeburn 1425: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073 raeburn 1426: $canhost = 1;
1427: } else {
1428: $canhost = 0;
1429: }
1430: }
1431: if ($canhost) {
1432: if ($remotesessions->{'version'} ne '') {
1433: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1434: if ($reqmajor ne '' && $reqminor ne '') {
1435: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1436: my $major = $1;
1437: my $minor = $2;
1438: if (($major < $reqmajor ) ||
1439: (($major == $reqmajor) && ($minor < $reqminor))) {
1440: $canhost = 0;
1441: }
1442: } else {
1443: $canhost = 0;
1444: }
1445: }
1446: }
1447: }
1448: }
1449: if ($canhost) {
1450: if (ref($hostedsessions) eq 'HASH') {
1.1120 raeburn 1451: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1452: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073 raeburn 1453: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120 raeburn 1454: if (($uint_dom ne '') &&
1455: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073 raeburn 1456: $canhost = 0;
1457: } else {
1458: $canhost = 1;
1459: }
1460: }
1461: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120 raeburn 1462: if (($uint_dom ne '') &&
1463: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073 raeburn 1464: $canhost = 1;
1465: } else {
1466: $canhost = 0;
1467: }
1468: }
1469: }
1470: }
1471: return $canhost;
1472: }
1473:
1.1083 raeburn 1474: sub spare_can_host {
1475: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1476: my $canhost=1;
1.1172.2.62 raeburn 1477: my $try_server_hostname = &hostname($try_server);
1478: my $serverhomeID = &get_server_homeID($try_server_hostname);
1479: my $serverhomedom = &host_domain($serverhomeID);
1480: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1481: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1482: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1483: $canhost = 0;
1484: }
1485: }
1.1172.2.136 raeburn 1486: if ($canhost) {
1487: if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
1488: if ($defdomdefaults{'offloadoth'}{$try_server}) {
1489: unless (&shared_institution($udom,$try_server)) {
1490: $canhost = 0;
1491: }
1492: }
1493: }
1494: }
1.1172.2.62 raeburn 1495: if (($canhost) && ($uint_dom)) {
1496: my @intdoms;
1497: my $internet_names = &get_internet_names($try_server);
1498: if (ref($internet_names) eq 'ARRAY') {
1499: @intdoms = @{$internet_names};
1500: }
1501: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1502: my $remoterev = &get_server_loncaparev(undef,$try_server);
1503: $canhost = &can_host_session($udom,$try_server,$remoterev,
1504: $remotesessions,
1505: $defdomdefaults{'hostedsessions'});
1506: }
1.1083 raeburn 1507: }
1508: return $canhost;
1509: }
1510:
1.1123 raeburn 1511: sub this_host_spares {
1512: my ($dom) = @_;
1.1126 raeburn 1513: my ($dom_in_use,$lonhost_in_use,$result);
1.1123 raeburn 1514: my @hosts = ¤t_machine_ids();
1515: foreach my $lonhost (@hosts) {
1516: if (&host_domain($lonhost) eq $dom) {
1.1126 raeburn 1517: $dom_in_use = $dom;
1518: $lonhost_in_use = $lonhost;
1.1123 raeburn 1519: last;
1520: }
1521: }
1.1126 raeburn 1522: if ($dom_in_use ne '') {
1523: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1524: }
1525: if (ref($result) ne 'HASH') {
1526: $lonhost_in_use = $perlvar{'lonHostID'};
1527: $dom_in_use = &host_domain($lonhost_in_use);
1528: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1529: if (ref($result) ne 'HASH') {
1530: $result = \%spareid;
1531: }
1532: }
1533: return $result;
1534: }
1535:
1536: sub spares_for_offload {
1537: my ($dom_in_use,$lonhost_in_use) = @_;
1538: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123 raeburn 1539: if (defined($cached)) {
1540: return $result;
1541: } else {
1.1126 raeburn 1542: my $cachetime = 60*60*24;
1543: my %domconfig =
1544: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1545: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1546: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1547: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1548: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123 raeburn 1549: }
1550: }
1551: }
1552: }
1.1126 raeburn 1553: return;
1.1123 raeburn 1554: }
1555:
1.1129 raeburn 1556: sub get_lonbalancer_config {
1557: my ($servers) = @_;
1558: my ($currbalancer,$currtargets);
1559: if (ref($servers) eq 'HASH') {
1560: foreach my $server (keys(%{$servers})) {
1561: my %what = (
1562: spareid => 1,
1563: perlvar => 1,
1564: );
1565: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1566: if ($result eq 'ok') {
1567: if (ref($returnhash) eq 'HASH') {
1568: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1569: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1570: $currbalancer = $server;
1571: $currtargets = {};
1572: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1573: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1574: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1575: }
1576: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1577: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1578: }
1579: }
1580: last;
1581: }
1582: }
1583: }
1584: }
1585: }
1586: }
1587: return ($currbalancer,$currtargets);
1588: }
1589:
1590: sub check_loadbalancing {
1.1172.2.88 raeburn 1591: my ($uname,$udom,$caller) = @_;
1.1172.2.12 raeburn 1592: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1.1172.2.107 raeburn 1593: $rule_in_effect,$offloadto,$otherserver,$setcookie);
1.1129 raeburn 1594: my $lonhost = $perlvar{'lonHostID'};
1.1172.2.4 raeburn 1595: my @hosts = ¤t_machine_ids();
1.1129 raeburn 1596: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1597: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1598: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1599: my $serverhomedom = &host_domain($lonhost);
1.1172.2.75 raeburn 1600: my $domneedscache;
1.1129 raeburn 1601: my $cachetime = 60*60*24;
1602:
1603: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1604: $dom_in_use = $udom;
1605: $homeintdom = 1;
1606: } else {
1607: $dom_in_use = $serverhomedom;
1608: }
1609: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1610: unless (defined($cached)) {
1611: my %domconfig =
1612: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1613: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130 raeburn 1614: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1172.2.75 raeburn 1615: } else {
1616: $domneedscache = $dom_in_use;
1.1129 raeburn 1617: }
1618: }
1619: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1620: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1621: &check_balancer_result($result,@hosts);
1.1129 raeburn 1622: if ($is_balancer) {
1623: if (ref($currrules) eq 'HASH') {
1624: if ($homeintdom) {
1625: if ($uname ne '') {
1626: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1627: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1628: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1629: $rule_in_effect = $currrules->{'_LC_author'};
1630: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1631: $rule_in_effect = $currrules->{'_LC_adv'}
1632: }
1633: }
1634: if ($rule_in_effect eq '') {
1635: my %userenv = &userenvironment($udom,$uname,'inststatus');
1636: if ($userenv{'inststatus'} ne '') {
1637: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1638: my ($othertitle,$usertypes,$types) =
1639: &Apache::loncommon::sorted_inst_types($udom);
1640: if (ref($types) eq 'ARRAY') {
1641: foreach my $type (@{$types}) {
1642: if (grep(/^\Q$type\E$/,@statuses)) {
1643: if (exists($currrules->{$type})) {
1644: $rule_in_effect = $currrules->{$type};
1645: }
1646: }
1647: }
1648: }
1649: } else {
1650: if (exists($currrules->{'default'})) {
1651: $rule_in_effect = $currrules->{'default'};
1652: }
1653: }
1654: }
1655: } else {
1656: if (exists($currrules->{'default'})) {
1657: $rule_in_effect = $currrules->{'default'};
1658: }
1659: }
1660: } else {
1661: if ($currrules->{'_LC_external'} ne '') {
1662: $rule_in_effect = $currrules->{'_LC_external'};
1663: }
1664: }
1665: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1666: $uname,$udom);
1667: }
1668: }
1669: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1172.2.35 raeburn 1670: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129 raeburn 1671: unless (defined($cached)) {
1672: my %domconfig =
1673: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1674: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1172.2.75 raeburn 1675: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1676: } else {
1677: $domneedscache = $serverhomedom;
1.1129 raeburn 1678: }
1679: }
1680: if (ref($result) eq 'HASH') {
1.1172.2.107 raeburn 1681: ($is_balancer,$currtargets,$currrules,$setcookie) =
1.1172.2.12 raeburn 1682: &check_balancer_result($result,@hosts);
1683: if ($is_balancer) {
1.1129 raeburn 1684: if (ref($currrules) eq 'HASH') {
1685: if ($currrules->{'_LC_internetdom'} ne '') {
1686: $rule_in_effect = $currrules->{'_LC_internetdom'};
1687: }
1688: }
1689: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1690: $uname,$udom);
1691: }
1692: } else {
1693: if ($perlvar{'lonBalancer'} eq 'yes') {
1694: $is_balancer = 1;
1695: $offloadto = &this_host_spares($dom_in_use);
1696: }
1.1172.2.75 raeburn 1697: unless (defined($cached)) {
1698: $domneedscache = $serverhomedom;
1699: }
1.1129 raeburn 1700: }
1701: } else {
1702: if ($perlvar{'lonBalancer'} eq 'yes') {
1703: $is_balancer = 1;
1704: $offloadto = &this_host_spares($dom_in_use);
1705: }
1.1172.2.75 raeburn 1706: unless (defined($cached)) {
1707: $domneedscache = $serverhomedom;
1708: }
1709: }
1710: if ($domneedscache) {
1711: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129 raeburn 1712: }
1.1172.2.130 raeburn 1713: if (($is_balancer) && ($caller ne 'switchserver')) {
1.1172.2.5 raeburn 1714: my $lowest_load = 30000;
1715: if (ref($offloadto) eq 'HASH') {
1716: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1717: foreach my $try_server (@{$offloadto->{'primary'}}) {
1718: ($otherserver,$lowest_load) =
1719: &compare_server_load($try_server,$otherserver,$lowest_load);
1720: }
1.1129 raeburn 1721: }
1.1172.2.5 raeburn 1722: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129 raeburn 1723:
1.1172.2.5 raeburn 1724: if (!$found_server) {
1725: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1726: foreach my $try_server (@{$offloadto->{'default'}}) {
1727: ($otherserver,$lowest_load) =
1728: &compare_server_load($try_server,$otherserver,$lowest_load);
1729: }
1730: }
1731: }
1732: } elsif (ref($offloadto) eq 'ARRAY') {
1733: if (@{$offloadto} == 1) {
1734: $otherserver = $offloadto->[0];
1735: } elsif (@{$offloadto} > 1) {
1736: foreach my $try_server (@{$offloadto}) {
1.1129 raeburn 1737: ($otherserver,$lowest_load) =
1738: &compare_server_load($try_server,$otherserver,$lowest_load);
1739: }
1740: }
1741: }
1.1172.2.88 raeburn 1742: unless ($caller eq 'login') {
1743: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
1744: $is_balancer = 0;
1745: if ($uname ne '' && $udom ne '') {
1746: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1747: &appenv({'user.loadbalexempt' => $lonhost,
1748: 'user.loadbalcheck.time' => time});
1749: }
1.1172.2.5 raeburn 1750: }
1.1129 raeburn 1751: }
1752: }
1.1172.2.130 raeburn 1753: }
1754: if (($is_balancer) && (!$homeintdom)) {
1755: undef($setcookie);
1.1129 raeburn 1756: }
1.1172.2.107 raeburn 1757: return ($is_balancer,$otherserver,$setcookie);
1.1129 raeburn 1758: }
1759:
1.1172.2.12 raeburn 1760: sub check_balancer_result {
1761: my ($result,@hosts) = @_;
1.1172.2.107 raeburn 1762: my ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1763: if (ref($result) eq 'HASH') {
1764: if ($result->{'lonhost'} ne '') {
1765: my $currbalancer = $result->{'lonhost'};
1766: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1767: $is_balancer = 1;
1768: $currtargets = $result->{'targets'};
1769: $currrules = $result->{'rules'};
1770: }
1771: } else {
1772: foreach my $key (keys(%{$result})) {
1773: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1774: (ref($result->{$key}) eq 'HASH')) {
1775: $is_balancer = 1;
1776: $currrules = $result->{$key}{'rules'};
1777: $currtargets = $result->{$key}{'targets'};
1.1172.2.107 raeburn 1778: $setcookie = $result->{$key}{'cookie'};
1.1172.2.12 raeburn 1779: last;
1780: }
1781: }
1782: }
1783: }
1.1172.2.107 raeburn 1784: return ($is_balancer,$currtargets,$currrules,$setcookie);
1.1172.2.12 raeburn 1785: }
1786:
1.1129 raeburn 1787: sub get_loadbalancer_targets {
1788: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1789: my $offloadto;
1.1172.2.4 raeburn 1790: if ($rule_in_effect eq 'none') {
1791: return [$perlvar{'lonHostID'}];
1792: } elsif ($rule_in_effect eq '') {
1.1129 raeburn 1793: $offloadto = $currtargets;
1794: } else {
1795: if ($rule_in_effect eq 'homeserver') {
1796: my $homeserver = &homeserver($uname,$udom);
1797: if ($homeserver ne 'no_host') {
1798: $offloadto = [$homeserver];
1799: }
1800: } elsif ($rule_in_effect eq 'externalbalancer') {
1801: my %domconfig =
1802: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1803: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1804: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1805: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1806: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1807: }
1808: }
1809: } else {
1.1172.2.7 raeburn 1810: my %servers = &internet_dom_servers($udom);
1.1129 raeburn 1811: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1812: if (&hostname($remotebalancer) ne '') {
1813: $offloadto = [$remotebalancer];
1814: }
1815: }
1816: } elsif (&hostname($rule_in_effect) ne '') {
1817: $offloadto = [$rule_in_effect];
1818: }
1819: }
1820: return $offloadto;
1821: }
1822:
1.1127 raeburn 1823: sub internet_dom_servers {
1824: my ($dom) = @_;
1825: my (%uniqservers,%servers);
1826: my $primaryserver = &hostname(&domain($dom,'primary'));
1827: my @machinedoms = &machine_domains($primaryserver);
1828: foreach my $mdom (@machinedoms) {
1829: my %currservers = %servers;
1830: my %server = &get_servers($mdom);
1831: %servers = (%currservers,%server);
1832: }
1833: my %by_hostname;
1834: foreach my $id (keys(%servers)) {
1835: push(@{$by_hostname{$servers{$id}}},$id);
1836: }
1837: foreach my $hostname (sort(keys(%by_hostname))) {
1838: if (@{$by_hostname{$hostname}} > 1) {
1839: my $match = 0;
1840: foreach my $id (@{$by_hostname{$hostname}}) {
1841: if (&host_domain($id) eq $dom) {
1842: $uniqservers{$id} = $hostname;
1843: $match = 1;
1844: }
1845: }
1846: unless ($match) {
1847: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1848: }
1849: } else {
1850: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1851: }
1852: }
1853: return %uniqservers;
1854: }
1855:
1.1 albertel 1856: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1857:
1.599 albertel 1858: my %homecache;
1.1 albertel 1859: sub homeserver {
1.230 stredwic 1860: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1861: my $index="$uname:$udom";
1.426 albertel 1862:
1.599 albertel 1863: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1864:
1865: my %servers = &get_servers($udom,'library');
1866: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1867: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1868: exists($badServerCache{$tryserver}));
1.841 albertel 1869:
1870: my $answer=reply("home:$udom:$uname",$tryserver);
1871: if ($answer eq 'found') {
1872: delete($badServerCache{$tryserver});
1873: return $homecache{$index}=$tryserver;
1874: } elsif ($answer eq 'no_host') {
1875: $badServerCache{$tryserver}=1;
1876: }
1.1 albertel 1877: }
1878: return 'no_host';
1.70 www 1879: }
1880:
1881: # ------------------------------------- Find the usernames behind a list of IDs
1882:
1883: sub idget {
1884: my ($udom,@ids)=@_;
1885: my %returnhash=();
1886:
1.841 albertel 1887: my %servers = &get_servers($udom,'library');
1888: foreach my $tryserver (keys(%servers)) {
1.1172.2.73 raeburn 1889: my $idlist=join('&', map { &escape($_); } @ids);
1.841 albertel 1890: $idlist=~tr/A-Z/a-z/;
1891: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1892: my @answer=();
1893: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1894: @answer=split(/\&/,$reply);
1895: } ;
1896: my $i;
1897: for ($i=0;$i<=$#ids;$i++) {
1898: if ($answer[$i]) {
1.1172.2.73 raeburn 1899: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1.841 albertel 1900: }
1901: }
1902: }
1.70 www 1903: return %returnhash;
1904: }
1905:
1906: # ------------------------------------- Find the IDs behind a list of usernames
1907:
1908: sub idrget {
1909: my ($udom,@unames)=@_;
1910: my %returnhash=();
1.800 albertel 1911: foreach my $uname (@unames) {
1912: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1913: }
1.70 www 1914: return %returnhash;
1915: }
1916:
1917: # ------------------------------- Store away a list of names and associated IDs
1918:
1919: sub idput {
1920: my ($udom,%ids)=@_;
1921: my %servers=();
1.800 albertel 1922: foreach my $uname (keys(%ids)) {
1923: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1924: my $uhom=&homeserver($uname,$udom);
1.70 www 1925: if ($uhom ne 'no_host') {
1.800 albertel 1926: my $id=&escape($ids{$uname});
1.70 www 1927: $id=~tr/A-Z/a-z/;
1.800 albertel 1928: my $esc_unam=&escape($uname);
1.70 www 1929: if ($servers{$uhom}) {
1.800 albertel 1930: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1931: } else {
1.800 albertel 1932: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1933: }
1934: }
1.191 harris41 1935: }
1.800 albertel 1936: foreach my $server (keys(%servers)) {
1937: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1938: }
1.344 www 1939: }
1940:
1.1172.2.30 raeburn 1941: # ---------------------------------------- Delete unwanted IDs from ids.db file
1942:
1943: sub iddel {
1944: my ($udom,$idshashref,$uhome)=@_;
1945: my %result=();
1946: unless (ref($idshashref) eq 'HASH') {
1947: return %result;
1948: }
1949: my %servers=();
1950: while (my ($id,$uname) = each(%{$idshashref})) {
1951: my $uhom;
1952: if ($uhome) {
1953: $uhom = $uhome;
1954: } else {
1955: $uhom=&homeserver($uname,$udom);
1956: }
1957: if ($uhom ne 'no_host') {
1958: if ($servers{$uhom}) {
1959: $servers{$uhom}.='&'.&escape($id);
1960: } else {
1961: $servers{$uhom}=&escape($id);
1962: }
1963: }
1964: }
1965: foreach my $server (keys(%servers)) {
1966: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1967: }
1968: return %result;
1969: }
1970:
1.1023 raeburn 1971: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1972: sub dump_dom {
1.1165 droeschl 1973: my ($namespace, $udom, $regexp) = @_;
1974:
1975: $udom ||= $env{'user.domain'};
1976:
1977: return () unless $udom;
1978:
1979: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012 raeburn 1980: }
1981:
1.1023 raeburn 1982: # ------------------------------------------ get items from domain db files
1.806 raeburn 1983:
1984: sub get_dom {
1.1172.2.146. .1(raebu 1985:22): my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
1.1172.2.57 raeburn 1986: return if ($udom eq 'public');
1.806 raeburn 1987: my $items='';
1988: foreach my $item (@$storearr) {
1989: $items.=&escape($item).'&';
1990: }
1991: $items=~s/\&$//;
1.860 raeburn 1992: if (!$udom) {
1993: $udom=$env{'user.domain'};
1.1172.2.57 raeburn 1994: return if ($udom eq 'public');
1.860 raeburn 1995: if (defined(&domain($udom,'primary'))) {
1996: $uhome=&domain($udom,'primary');
1997: } else {
1.874 albertel 1998: undef($uhome);
1.860 raeburn 1999: }
2000: } else {
2001: if (!$uhome) {
2002: if (defined(&domain($udom,'primary'))) {
2003: $uhome=&domain($udom,'primary');
2004: }
2005: }
2006: }
2007: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1172.2.139 raeburn 2008: my $rep;
2009: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
2010: # domain information is hosted on this machine
2011: $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
1.1172.2.146. .1(raebu 2012:22): } else {
2013:22): if ($encrypt) {
2014:22): $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
2015:22): } else {
2016:22): $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
2017:22): }
1.1172.2.139 raeburn 2018: }
1.866 raeburn 2019: my %returnhash;
1.875 albertel 2020: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 2021: return %returnhash;
2022: }
1.806 raeburn 2023: my @pairs=split(/\&/,$rep);
2024: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
2025: return @pairs;
2026: }
2027: my $i=0;
2028: foreach my $item (@$storearr) {
2029: $returnhash{$item}=&thaw_unescape($pairs[$i]);
2030: $i++;
2031: }
2032: return %returnhash;
2033: } else {
1.880 banghart 2034: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 2035: }
2036: }
2037:
2038: # -------------------------------------------- put items in domain db files
2039:
2040: sub put_dom {
1.1172.2.146. .1(raebu 2041:22): my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
1.860 raeburn 2042: if (!$udom) {
2043: $udom=$env{'user.domain'};
2044: if (defined(&domain($udom,'primary'))) {
2045: $uhome=&domain($udom,'primary');
2046: } else {
1.874 albertel 2047: undef($uhome);
1.860 raeburn 2048: }
2049: } else {
2050: if (!$uhome) {
2051: if (defined(&domain($udom,'primary'))) {
2052: $uhome=&domain($udom,'primary');
2053: }
2054: }
2055: }
2056: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 2057: my $items='';
2058: foreach my $item (keys(%$storehash)) {
2059: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
2060: }
2061: $items=~s/\&$//;
1.1172.2.146. .1(raebu 2062:22): if ($encrypt) {
2063:22): return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
2064:22): } else {
2065:22): return &reply("putdom:$udom:$namespace:$items",$uhome);
2066:22): }
1.806 raeburn 2067: } else {
1.860 raeburn 2068: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 2069: }
2070: }
2071:
1.1023 raeburn 2072: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 2073: sub newput_dom {
1.1023 raeburn 2074: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 2075: my $result;
2076: if (!$udom) {
2077: $udom=$env{'user.domain'};
2078: }
1.1023 raeburn 2079: if ($udom) {
2080: my $uname = &get_domainconfiguser($udom);
2081: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 2082: }
2083: return $result;
2084: }
2085:
1.1023 raeburn 2086: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 2087: sub del_dom {
1.1023 raeburn 2088: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 2089: if (ref($storearr) eq 'ARRAY') {
2090: if (!$udom) {
2091: $udom=$env{'user.domain'};
2092: }
1.1023 raeburn 2093: if ($udom) {
2094: my $uname = &get_domainconfiguser($udom);
2095: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 2096: }
2097: }
2098: }
2099:
1.1172.2.146. .4(raebu 2100:22): sub store_dom {
2101:22): my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
2102:22): $$storehash{'ip'}=&get_requestor_ip();
2103:22): $$storehash{'host'}=$perlvar{'lonHostID'};
2104:22): my $namevalue='';
2105:22): foreach my $key (keys(%{$storehash})) {
2106:22): $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
2107:22): }
2108:22): $namevalue=~s/\&$//;
2109:22): if (grep { $_ eq $home } current_machine_ids()) {
2110:22): return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
2111:22): } else {
2112:22): if ($namespace eq 'private') {
2113:22): return 'refused';
2114:22): } elsif ($encrypt) {
2115:22): return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
2116:22): } else {
2117:22): return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
2118:22): }
2119:22): }
2120:22): }
2121:22):
2122:22): sub restore_dom {
2123:22): my ($id,$namespace,$dom,$home,$encrypt) = @_;
2124:22): my $answer;
2125:22): if (grep { $_ eq $home } current_machine_ids()) {
2126:22): $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
2127:22): } elsif ($namespace ne 'private') {
2128:22): if ($encrypt) {
2129:22): $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
2130:22): } else {
2131:22): $answer=&reply("restoredom:$dom:$namespace:$id",$home);
2132:22): }
2133:22): }
2134:22): my %returnhash=();
2135:22): unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') ||
2136:22): ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
2137:22): foreach my $line (split(/\&/,$answer)) {
2138:22): my ($name,$value)=split(/\=/,$line);
2139:22): $returnhash{&unescape($name)}=&thaw_unescape($value);
2140:22): }
2141:22): my $version;
2142:22): for ($version=1;$version<=$returnhash{'version'};$version++) {
2143:22): foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
2144:22): $returnhash{$item}=$returnhash{$version.':'.$item};
2145:22): }
2146:22): }
2147:22): }
2148:22): return %returnhash;
2149:22): }
2150:22):
1.1023 raeburn 2151: # ----------------------------------construct domainconfig user for a domain
2152: sub get_domainconfiguser {
2153: my ($udom) = @_;
2154: return $udom.'-domainconfig';
2155: }
2156:
1.837 raeburn 2157: sub retrieve_inst_usertypes {
2158: my ($udom) = @_;
2159: my (%returnhash,@order);
1.989 raeburn 2160: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
2161: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
2162: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1172.2.44 raeburn 2163: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989 raeburn 2164: } else {
2165: if (defined(&domain($udom,'primary'))) {
2166: my $uhome=&domain($udom,'primary');
2167: my $rep=&reply("inst_usertypes:$udom",$uhome);
2168: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1172.2.44 raeburn 2169: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989 raeburn 2170: return (\%returnhash,\@order);
2171: }
2172: my ($hashitems,$orderitems) = split(/:/,$rep);
2173: my @pairs=split(/\&/,$hashitems);
2174: foreach my $item (@pairs) {
2175: my ($key,$value)=split(/=/,$item,2);
2176: $key = &unescape($key);
2177: next if ($key =~ /^error: 2 /);
2178: $returnhash{$key}=&thaw_unescape($value);
2179: }
2180: my @esc_order = split(/\&/,$orderitems);
2181: foreach my $item (@esc_order) {
2182: push(@order,&unescape($item));
2183: }
2184: } else {
1.1172.2.44 raeburn 2185: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837 raeburn 2186: }
1.1172.2.44 raeburn 2187: return (\%returnhash,\@order);
1.837 raeburn 2188: }
2189: }
2190:
1.868 raeburn 2191: sub is_domainimage {
2192: my ($url) = @_;
1.1172.2.142 raeburn 2193: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
1.868 raeburn 2194: if (&domain($1) ne '') {
2195: return '1';
2196: }
2197: }
2198: return;
2199: }
2200:
1.899 raeburn 2201: sub inst_directory_query {
2202: my ($srch) = @_;
2203: my $udom = $srch->{'srchdomain'};
2204: my %results;
2205: my $homeserver = &domain($udom,'primary');
1.909 raeburn 2206: my $outcome;
1.899 raeburn 2207: if ($homeserver ne '') {
1.1172.2.116 raeburn 2208: unless ($homeserver eq $perlvar{'lonHostID'}) {
2209: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2210: my $lcrev = &get_server_loncaparev($udom,$homeserver);
1.1172.2.116 raeburn 2211: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
2212: if (($major eq '' && $minor eq '') || ($major < 2) ||
2213: (($major == 2) && ($minor < 11)) ||
2214: (($major == 2) && ($minor == 11) && ($subver < 3))) {
2215: return;
2216: }
2217: }
2218: }
1.904 albertel 2219: my $queryid=&reply("querysend:instdirsearch:".
2220: &escape($srch->{'srchby'}).':'.
2221: &escape($srch->{'srchterm'}).':'.
2222: &escape($srch->{'srchtype'}),$homeserver);
2223: my $host=&hostname($homeserver);
2224: if ($queryid !~/^\Q$host\E\_/) {
1.1172.2.96 raeburn 2225: &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904 albertel 2226: return;
2227: }
2228: my $response = &get_query_reply($queryid);
2229: my $maxtries = 5;
2230: my $tries = 1;
2231: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2232: $response = &get_query_reply($queryid);
2233: $tries ++;
2234: }
2235:
2236: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 2237: if ($response eq 'unavailable') {
2238: $outcome = $response;
2239: } else {
2240: $outcome = 'ok';
2241: my @matches = split(/\n/,$response);
2242: foreach my $match (@matches) {
2243: my ($key,$value) = split(/=/,$match);
2244: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
2245: }
1.899 raeburn 2246: }
2247: }
2248: }
1.909 raeburn 2249: return ($outcome,%results);
1.899 raeburn 2250: }
2251:
2252: sub usersearch {
2253: my ($srch) = @_;
2254: my $dom = $srch->{'srchdomain'};
2255: my %results;
2256: my %libserv = &all_library();
2257: my $query = 'usersearch';
2258: foreach my $tryserver (keys(%libserv)) {
2259: if (&host_domain($tryserver) eq $dom) {
1.1172.2.116 raeburn 2260: unless ($tryserver eq $perlvar{'lonHostID'}) {
2261: if ($srch->{'srchby'} eq 'email') {
1.1172.2.117 raeburn 2262: my $lcrev = &get_server_loncaparev($dom,$tryserver);
1.1172.2.119 raeburn 2263: my ($major,$minor,$subver) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.(\d+)[\w.\-]+\'?$/);
1.1172.2.116 raeburn 2264: next if (($major eq '' && $minor eq '') || ($major < 2) ||
2265: (($major == 2) && ($minor < 11)) ||
2266: (($major == 2) && ($minor == 11) && ($subver < 3)));
2267: }
2268: }
1.899 raeburn 2269: my $host=&hostname($tryserver);
2270: my $queryid=
1.911 raeburn 2271: &reply("querysend:".&escape($query).':'.
2272: &escape($srch->{'srchby'}).':'.
1.899 raeburn 2273: &escape($srch->{'srchtype'}).':'.
2274: &escape($srch->{'srchterm'}),$tryserver);
2275: if ($queryid !~/^\Q$host\E\_/) {
2276: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 2277: next;
1.899 raeburn 2278: }
2279: my $reply = &get_query_reply($queryid);
2280: my $maxtries = 1;
2281: my $tries = 1;
2282: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2283: $reply = &get_query_reply($queryid);
2284: $tries ++;
2285: }
2286: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2287: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2288: } else {
1.911 raeburn 2289: my @matches;
2290: if ($reply =~ /\n/) {
2291: @matches = split(/\n/,$reply);
2292: } else {
2293: @matches = split(/\&/,$reply);
2294: }
1.899 raeburn 2295: foreach my $match (@matches) {
2296: my ($uname,$udom,%userhash);
1.911 raeburn 2297: foreach my $entry (split(/:/,$match)) {
2298: my ($key,$value) =
2299: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 2300: $userhash{$key} = $value;
2301: if ($key eq 'username') {
2302: $uname = $value;
2303: } elsif ($key eq 'domain') {
2304: $udom = $value;
1.911 raeburn 2305: }
1.899 raeburn 2306: }
2307: $results{$uname.':'.$udom} = \%userhash;
2308: }
2309: }
2310: }
2311: }
2312: return %results;
2313: }
2314:
1.912 raeburn 2315: sub get_instuser {
2316: my ($udom,$uname,$id) = @_;
2317: my $homeserver = &domain($udom,'primary');
2318: my ($outcome,%results);
2319: if ($homeserver ne '') {
2320: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2321: &escape($id).':'.&escape($udom),$homeserver);
2322: my $host=&hostname($homeserver);
2323: if ($queryid !~/^\Q$host\E\_/) {
2324: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2325: return;
2326: }
2327: my $response = &get_query_reply($queryid);
2328: my $maxtries = 5;
2329: my $tries = 1;
2330: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2331: $response = &get_query_reply($queryid);
2332: $tries ++;
2333: }
2334: if (!&error($response) && $response ne 'refused') {
2335: if ($response eq 'unavailable') {
2336: $outcome = $response;
2337: } else {
2338: $outcome = 'ok';
2339: my @matches = split(/\n/,$response);
2340: foreach my $match (@matches) {
2341: my ($key,$value) = split(/=/,$match);
2342: $results{&unescape($key)} = &thaw_unescape($value);
2343: }
2344: }
2345: }
2346: }
2347: my %userinfo;
2348: if (ref($results{$uname}) eq 'HASH') {
2349: %userinfo = %{$results{$uname}};
2350: }
2351: return ($outcome,%userinfo);
2352: }
2353:
1.1172.2.69 raeburn 2354: sub get_multiple_instusers {
2355: my ($udom,$users,$caller) = @_;
2356: my ($outcome,$results);
2357: if (ref($users) eq 'HASH') {
2358: my $count = keys(%{$users});
2359: my $requested = &freeze_escape($users);
2360: my $homeserver = &domain($udom,'primary');
2361: if ($homeserver ne '') {
2362: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2363: my $host=&hostname($homeserver);
2364: if ($queryid !~/^\Q$host\E\_/) {
2365: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2366: ' for host: '.$homeserver.'in domain '.$udom);
2367: return ($outcome,$results);
2368: }
2369: my $response = &get_query_reply($queryid);
2370: my $maxtries = 5;
2371: if ($count > 100) {
2372: $maxtries = 1+int($count/20);
2373: }
2374: my $tries = 1;
2375: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2376: $response = &get_query_reply($queryid);
2377: $tries ++;
2378: }
2379: if ($response eq '') {
2380: $results = {};
2381: foreach my $key (keys(%{$users})) {
2382: my ($uname,$id);
2383: if ($caller eq 'id') {
2384: $id = $key;
2385: } else {
2386: $uname = $key;
2387: }
2388: my ($resp,%info) = &get_instuser($udom,$uname,$id);
2389: $outcome = $resp;
2390: if ($resp eq 'ok') {
2391: %{$results} = (%{$results}, %info);
2392: } else {
2393: last;
2394: }
2395: }
2396: } elsif(!&error($response) && ($response ne 'refused')) {
2397: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2398: $outcome = $response;
2399: } else {
2400: ($outcome,my $userdata) = split(/=/,$response,2);
2401: if ($outcome eq 'ok') {
2402: $results = &thaw_unescape($userdata);
2403: }
2404: }
2405: }
2406: }
2407: }
2408: return ($outcome,$results);
2409: }
2410:
1.912 raeburn 2411: sub inst_rulecheck {
1.923 raeburn 2412: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 2413: my %returnhash;
2414: if ($udom ne '') {
2415: if (ref($rules) eq 'ARRAY') {
2416: @{$rules} = map {&escape($_);} (@{$rules});
2417: my $rulestr = join(':',@{$rules});
2418: my $homeserver=&domain($udom,'primary');
2419: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2420: my $response;
2421: if ($item eq 'username') {
2422: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2423: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 2424: $homeserver));
1.923 raeburn 2425: } elsif ($item eq 'id') {
2426: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2427: ':'.&escape($id).':'.$rulestr,
2428: $homeserver));
1.1172.2.146. .5(raebu 2429:22): } elsif ($item eq 'unamemap') {
2430:22): $response=&unescape(&reply('instunamemapcheck:'.
2431:22): &escape($udom).':'.&escape($uname).
2432:22): ':'.$rulestr,$homeserver));
1.945 raeburn 2433: } elsif ($item eq 'selfcreate') {
2434: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 2435: &escape($udom).':'.&escape($uname).
2436: ':'.$rulestr,$homeserver));
1.923 raeburn 2437: }
1.912 raeburn 2438: if ($response ne 'refused') {
2439: my @pairs=split(/\&/,$response);
2440: foreach my $item (@pairs) {
2441: my ($key,$value)=split(/=/,$item,2);
2442: $key = &unescape($key);
2443: next if ($key =~ /^error: 2 /);
2444: $returnhash{$key}=&thaw_unescape($value);
2445: }
2446: }
2447: }
2448: }
2449: }
2450: return %returnhash;
2451: }
2452:
2453: sub inst_userrules {
1.923 raeburn 2454: my ($udom,$check) = @_;
1.912 raeburn 2455: my (%ruleshash,@ruleorder);
2456: if ($udom ne '') {
2457: my $homeserver=&domain($udom,'primary');
2458: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2459: my $response;
2460: if ($check eq 'id') {
2461: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 2462: $homeserver);
1.943 raeburn 2463: } elsif ($check eq 'email') {
2464: $response=&reply('instemailrules:'.&escape($udom),
2465: $homeserver);
1.1172.2.146. .5(raebu 2466:22): } elsif ($check eq 'unamemap') {
2467:22): $response=&reply('unamemaprules:'.&escape($udom),
2468:22): $homeserver);
1.923 raeburn 2469: } else {
2470: $response=&reply('instuserrules:'.&escape($udom),
2471: $homeserver);
2472: }
1.912 raeburn 2473: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 2474: ($response ne 'unknown_cmd') &&
1.912 raeburn 2475: ($response ne 'no_such_host')) {
2476: my ($hashitems,$orderitems) = split(/:/,$response);
2477: my @pairs=split(/\&/,$hashitems);
2478: foreach my $item (@pairs) {
2479: my ($key,$value)=split(/=/,$item,2);
2480: $key = &unescape($key);
2481: next if ($key =~ /^error: 2 /);
2482: $ruleshash{$key}=&thaw_unescape($value);
2483: }
2484: my @esc_order = split(/\&/,$orderitems);
2485: foreach my $item (@esc_order) {
2486: push(@ruleorder,&unescape($item));
2487: }
2488: }
2489: }
2490: }
2491: return (\%ruleshash,\@ruleorder);
2492: }
2493:
1.976 raeburn 2494: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 2495:
2496: sub get_domain_defaults {
1.1172.2.35 raeburn 2497: my ($domain,$ignore_cache) = @_;
2498: return if (($domain eq '') || ($domain eq 'public'));
1.943 raeburn 2499: my $cachetime = 60*60*24;
1.1172.2.35 raeburn 2500: unless ($ignore_cache) {
2501: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2502: if (defined($cached)) {
2503: if (ref($result) eq 'HASH') {
2504: return %{$result};
2505: }
1.943 raeburn 2506: }
2507: }
2508: my %domdefaults;
2509: my %domconfig =
1.989 raeburn 2510: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 2511: 'requestcourses','inststatus',
1.1172.2.9 raeburn 2512: 'coursedefaults','usersessions',
1.1172.2.146. .18(raeb 2513:-24): 'requestauthor','authordefaults',
2514:-24): 'selfenrollment','coursecategories',
2515:-24): 'autoenroll','helpsettings',
2516:-24): 'wafproxy','ltisec','toolsec',
2517:-24): 'domexttool','exttool'],$domain);
1.1172.2.41 raeburn 2518: my @coursetypes = ('official','unofficial','community','textbook');
1.943 raeburn 2519: if (ref($domconfig{'defaults'}) eq 'HASH') {
2520: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2521: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2522: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 2523: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 2524: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147 raeburn 2525: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1172.2.146. .12(raeb 2526:-23): $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
2527:-23): $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
1.1172.2.91 raeburn 2528: $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
2529: $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
2530: $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.1172.2.146. .5(raebu 2531:22): $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
1.943 raeburn 2532: } else {
2533: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2534: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2535: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2536: }
1.976 raeburn 2537: if (ref($domconfig{'quotas'}) eq 'HASH') {
2538: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2539: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2540: } else {
2541: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1172.2.29 raeburn 2542: }
1.1172.2.146. .23(raeb 2543:-24): my @usertools = ('aboutme','blog','webdav','portfolio','portaccess');
1.976 raeburn 2544: foreach my $item (@usertools) {
2545: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2546: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2547: }
2548: }
1.1172.2.29 raeburn 2549: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2550: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2551: }
1.976 raeburn 2552: }
1.985 raeburn 2553: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1172.2.37 raeburn 2554: foreach my $item ('official','unofficial','community','textbook') {
1.985 raeburn 2555: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2556: }
2557: }
1.1172.2.146. .18(raeb 2558:-24): if (ref($domconfig{'authordefaults'}) eq 'HASH') {
.23(raeb 2559:-24): foreach my $item ('nocodemirror','copyright','sourceavail','domcoordacc','editors','archive') {
.18(raeb 2560:-24): if ($item eq 'editors') {
2561:-24): if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
2562:-24): $domdefaults{$item} = join(',',@{$domconfig{'authordefaults'}{'editors'}});
2563:-24): }
2564:-24): } else {
2565:-24): $domdefaults{$item} = $domconfig{'authordefaults'}{$item};
2566:-24): }
2567:-24): }
2568:-24): }
1.1172.2.9 raeburn 2569: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2570: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2571: }
1.989 raeburn 2572: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1172.2.44 raeburn 2573: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989 raeburn 2574: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2575: }
2576: }
1.1047 raeburn 2577: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1172.2.60 raeburn 2578: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
1.1172.2.144 raeburn 2579: $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
1.1172.2.60 raeburn 2580: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2581: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2582: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2583: }
1.1172.2.146. .25(raeb 2584:-24): if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
2585:-24): $domdefaults{'crseditors'}=join(',',@{$domconfig{'coursedefaults'}{'crseditors'}});
2586:-24): }
1.1172.2.41 raeburn 2587: foreach my $type (@coursetypes) {
2588: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2589: unless ($type eq 'community') {
2590: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2591: }
2592: }
2593: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2594: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2595: }
1.1172.2.146. .14(raeb 2596:-23): if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
2597:-23): $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
2598:-23): }
1.1172.2.60 raeburn 2599: if ($domdefaults{'postsubmit'} eq 'on') {
2600: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2601: $domdefaults{$type.'postsubtimeout'} =
2602: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2603: }
2604: }
1.1172.2.146. .13(raeb 2605:-23): if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
2606:-23): $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
2607:-23): } else {
2608:-23): $domdefaults{$type.'domexttool'} = 1;
2609:-23): }
2610:-23): if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
2611:-23): $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
2612:-23): } else {
2613:-23): $domdefaults{$type.'exttool'} = 0;
2614:-23): }
1.1172.2.30 raeburn 2615: }
1.1172.2.67 raeburn 2616: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2617: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2618: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2619: if (@clonecodes) {
2620: $domdefaults{'canclone'} = join('+',@clonecodes);
2621: }
2622: }
2623: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2624: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2625: }
1.1172.2.103 raeburn 2626: if ($domconfig{'coursedefaults'}{'texengine'}) {
2627: $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
2628: }
1.1172.2.146. .3(raebu 2629:22): if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
2630:22): $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
2631:22): }
1.1047 raeburn 2632: }
1.1073 raeburn 2633: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2634: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2635: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2636: }
2637: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2638: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2639: }
1.1172.2.62 raeburn 2640: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2641: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2642: }
1.1172.2.137 raeburn 2643: if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
1.1172.2.136 raeburn 2644: $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
2645: }
1.1073 raeburn 2646: }
1.1172.2.41 raeburn 2647: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2648: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2649: my @settings = ('types','registered','enroll_dates','access_dates','section',
2650: 'approval','limit');
2651: foreach my $type (@coursetypes) {
2652: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2653: my @mgrdc = ();
2654: foreach my $item (@settings) {
2655: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2656: push(@mgrdc,$item);
2657: }
2658: }
2659: if (@mgrdc) {
2660: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2661: }
2662: }
2663: }
2664: }
2665: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2666: foreach my $type (@coursetypes) {
2667: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2668: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2669: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2670: }
2671: }
2672: }
2673: }
2674: }
1.1172.2.46 raeburn 2675: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2676: $domdefaults{'catauth'} = 'std';
2677: $domdefaults{'catunauth'} = 'std';
2678: if ($domconfig{'coursecategories'}{'auth'}) {
2679: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2680: }
2681: if ($domconfig{'coursecategories'}{'unauth'}) {
2682: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2683: }
2684: }
1.1172.2.76 raeburn 2685: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2686: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
1.1172.2.146 raeburn 2687: $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
1.1172.2.76 raeburn 2688: }
1.1172.2.89 raeburn 2689: if (ref($domconfig{'helpsettings'}) eq 'HASH') {
2690: $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
2691: if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
2692: $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
2693: }
2694: }
1.1172.2.142 raeburn 2695: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
2696: foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
2697: if ($domconfig{'wafproxy'}{$item}) {
2698: $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
2699: }
2700: }
2701: }
1.1172.2.146. .4(raebu 2702:22): if (ref($domconfig{'ltisec'}) eq 'HASH') {
2703:22): if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
2704:22): $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
2705:22): $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
2706:22): $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
2707:22): }
2708:22): if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
2709:22): if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
.13(raeb 2710:-23): $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
2711:-23): }
2712:-23): }
.17(raeb 2713:-24): if (ref($domconfig{'ltisec'}{'suggested'}) eq 'HASH') {
2714:-24): my %suggestions = %{$domconfig{'ltisec'}{'suggested'}};
2715:-24): foreach my $item (keys(%{$domconfig{'ltisec'}{'suggested'}})) {
2716:-24): unless (ref($domconfig{'ltisec'}{'suggested'}{$item}) eq 'HASH') {
2717:-24): delete($suggestions{$item});
2718:-24): }
2719:-24): }
2720:-24): if (keys(%suggestions)) {
2721:-24): $domdefaults{'linkprotsuggested'} = \%suggestions;
2722:-24): }
2723:-24): }
.13(raeb 2724:-23): }
2725:-23): if (ref($domconfig{'toolsec'}) eq 'HASH') {
2726:-23): if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
2727:-23): $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
2728:-23): $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
2729:-23): }
2730:-23): if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
2731:-23): if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
2732:-23): $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
.4(raebu 2733:22): }
2734:22): }
2735:22): }
1.1172.2.23 raeburn 2736: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943 raeburn 2737: return %domdefaults;
2738: }
2739:
1.1172.2.106 raeburn 2740: sub get_dom_cats {
2741: my ($dom) = @_;
2742: return unless (&domain($dom));
2743: my ($cats,$cached)=&is_cached_new('cats',$dom);
2744: unless (defined($cached)) {
2745: my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
2746: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2747: if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
2748: %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
2749: } else {
2750: $cats = {};
2751: }
2752: } else {
2753: $cats = {};
2754: }
2755: &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
2756: }
2757: return $cats;
2758: }
2759:
2760: sub get_dom_instcats {
2761: my ($dom) = @_;
2762: return unless (&domain($dom));
2763: my ($instcats,$cached)=&is_cached_new('instcats',$dom);
2764: unless (defined($cached)) {
2765: my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
2766: my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
2767: if ($totcodes > 0) {
2768: my $caller = 'global';
2769: if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
2770: \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
2771: $instcats = {
1.1172.2.146. .10(raeb 2772:-23): totcodes => $totcodes,
1.1172.2.106 raeburn 2773: codes => \%codes,
2774: codetitles => \@codetitles,
2775: cat_titles => \%cat_titles,
2776: cat_order => \%cat_order,
2777: };
2778: &do_cache_new('instcats',$dom,$instcats,3600);
2779: }
2780: }
2781: }
2782: return $instcats;
2783: }
2784:
2785: sub retrieve_instcodes {
2786: my ($coursecodes,$dom) = @_;
2787: my $totcodes;
2788: my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
2789: foreach my $course (keys(%courses)) {
2790: if (ref($courses{$course}) eq 'HASH') {
2791: if ($courses{$course}{'inst_code'} ne '') {
2792: $$coursecodes{$course} = $courses{$course}{'inst_code'};
2793: $totcodes ++;
2794: }
2795: }
2796: }
2797: return $totcodes;
2798: }
2799:
1.1172.2.113 raeburn 2800: # --------------------------------------------- Get domain config for passwords
2801:
2802: sub get_passwdconf {
2803: my ($dom) = @_;
2804: my (%passwdconf,$gotconf,$lookup);
2805: my ($result,$cached)=&is_cached_new('passwdconf',$dom);
2806: if (defined($cached)) {
2807: if (ref($result) eq 'HASH') {
2808: %passwdconf = %{$result};
2809: $gotconf = 1;
2810: }
2811: }
2812: unless ($gotconf) {
2813: my %domconfig = &get_dom('configuration',['passwords'],$dom);
2814: if (ref($domconfig{'passwords'}) eq 'HASH') {
2815: %passwdconf = %{$domconfig{'passwords'}};
2816: }
2817: my $cachetime = 24*60*60;
2818: &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
2819: }
2820: return %passwdconf;
2821: }
2822:
1.1172.2.146. .1(raebu 2823:22): sub course_portal_url {
2824:22): my ($cnum,$cdom,$r) = @_;
2825:22): my $chome = &homeserver($cnum,$cdom);
2826:22): my $hostname = &hostname($chome);
2827:22): my $protocol = $protocol{$chome};
2828:22): $protocol = 'http' if ($protocol ne 'https');
2829:22): my %domdefaults = &get_domain_defaults($cdom);
2830:22): my $firsturl;
2831:22): if ($domdefaults{'portal_def'}) {
2832:22): $firsturl = $domdefaults{'portal_def'};
2833:22): } else {
2834:22): my $alias = &Apache::lonnet::use_proxy_alias($r,$chome);
2835:22): $hostname = $alias if ($alias ne '');
2836:22): $firsturl = $protocol.'://'.$hostname;
2837:22): }
2838:22): return $firsturl;
2839:22): }
2840:22):
.12(raeb 2841:-23): sub url_prefix {
2842:-23): my ($r,$dom,$home,$context) = @_;
2843:-23): my $prefix;
2844:-23): my %domdefs = &get_domain_defaults($dom);
2845:-23): if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
2846:-23): if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
2847:-23): $prefix = $1;
2848:-23): }
2849:-23): }
2850:-23): if ($prefix eq '') {
2851:-23): my $hostname = &hostname($home);
2852:-23): my $protocol = $protocol{$home};
2853:-23): $protocol = 'http' if ($protocol{$home} ne 'https');
2854:-23): my $alias = &use_proxy_alias($r,$home);
2855:-23): $hostname = $alias if ($alias ne '');
2856:-23): $prefix = $protocol.'://'.$hostname;
2857:-23): }
2858:-23): return $prefix;
2859:-23): }
2860:-23):
1.344 www 2861: # --------------------------------------------------- Assign a key to a student
2862:
2863: sub assign_access_key {
1.364 www 2864: #
2865: # a valid key looks like uname:udom#comments
2866: # comments are being appended
2867: #
1.498 www 2868: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2869: $kdom=
1.620 albertel 2870: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 2871: $knum=
1.620 albertel 2872: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 2873: $cdom=
1.620 albertel 2874: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2875: $cnum=
1.620 albertel 2876: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2877: $udom=$env{'user.name'} unless (defined($udom));
2878: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 2879: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 2880: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 2881: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 2882: # assigned to this person
2883: # - this should not happen,
1.345 www 2884: # unless something went wrong
2885: # the first time around
2886: # ready to assign
1.364 www 2887: $logentry=$1.'; '.$logentry;
1.496 www 2888: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 2889: $kdom,$knum) eq 'ok') {
1.345 www 2890: # key now belongs to user
1.346 www 2891: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 2892: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 2893: &appenv({'environment.'.$envkey => $ckey});
1.345 www 2894: return 'ok';
2895: } else {
2896: return
2897: 'error: Count not permanently assign key, will need to be re-entered later.';
2898: }
2899: } else {
2900: return 'error: Could not assign key, try again later.';
2901: }
1.364 www 2902: } elsif (!$existing{$ckey}) {
1.345 www 2903: # the key does not exist
2904: return 'error: The key does not exist';
2905: } else {
2906: # the key is somebody else's
2907: return 'error: The key is already in use';
2908: }
1.344 www 2909: }
2910:
1.364 www 2911: # ------------------------------------------ put an additional comment on a key
2912:
2913: sub comment_access_key {
2914: #
2915: # a valid key looks like uname:udom#comments
2916: # comments are being appended
2917: #
2918: my ($ckey,$cdom,$cnum,$logentry)=@_;
2919: $cdom=
1.620 albertel 2920: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 2921: $cnum=
1.620 albertel 2922: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 2923: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2924: if ($existing{$ckey}) {
2925: $existing{$ckey}.='; '.$logentry;
2926: # ready to assign
1.367 www 2927: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 2928: $cdom,$cnum) eq 'ok') {
2929: return 'ok';
2930: } else {
2931: return 'error: Count not store comment.';
2932: }
2933: } else {
2934: # the key does not exist
2935: return 'error: The key does not exist';
2936: }
2937: }
2938:
1.344 www 2939: # ------------------------------------------------------ Generate a set of keys
2940:
2941: sub generate_access_keys {
1.364 www 2942: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 2943: $cdom=
1.620 albertel 2944: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2945: $cnum=
1.620 albertel 2946: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 2947: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 2948: unless (($cdom) && ($cnum)) { return 0; }
2949: if ($number>10000) { return 0; }
2950: sleep(2); # make sure don't get same seed twice
2951: srand(time()^($$+($$<<15))); # from "Programming Perl"
2952: my $total=0;
2953: for (my $i=1;$i<=$number;$i++) {
2954: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2955: sprintf("%lx",int(100000*rand)).'-'.
2956: sprintf("%lx",int(100000*rand));
2957: $newkey=~s/1/g/g; # folks mix up 1 and l
2958: $newkey=~s/0/h/g; # and also 0 and O
2959: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2960: if ($existing{$newkey}) {
2961: $i--;
2962: } else {
1.364 www 2963: if (&put('accesskeys',
2964: { $newkey => '# generated '.localtime().
1.620 albertel 2965: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 2966: '; '.$logentry },
2967: $cdom,$cnum) eq 'ok') {
1.344 www 2968: $total++;
2969: }
2970: }
2971: }
1.620 albertel 2972: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 2973: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2974: return $total;
2975: }
2976:
2977: # ------------------------------------------------------- Validate an accesskey
2978:
2979: sub validate_access_key {
2980: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2981: $cdom=
1.620 albertel 2982: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2983: $cnum=
1.620 albertel 2984: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2985: $udom=$env{'user.domain'} unless (defined($udom));
2986: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 2987: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 2988: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 2989: }
2990:
2991: # ------------------------------------- Find the section of student in a course
1.652 albertel 2992: sub devalidate_getsection_cache {
2993: my ($udom,$unam,$courseid)=@_;
2994: my $hashid="$udom:$unam:$courseid";
2995: &devalidate_cache_new('getsection',$hashid);
2996: }
1.298 matthew 2997:
1.815 albertel 2998: sub courseid_to_courseurl {
2999: my ($courseid) = @_;
3000: #already url style courseid
3001: return $courseid if ($courseid =~ m{^/});
3002:
3003: if (exists($env{'course.'.$courseid.'.num'})) {
3004: my $cnum = $env{'course.'.$courseid.'.num'};
3005: my $cdom = $env{'course.'.$courseid.'.domain'};
3006: return "/$cdom/$cnum";
3007: }
3008:
3009: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
3010: if (exists($courseinfo{'num'})) {
3011: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
3012: }
3013:
3014: return undef;
3015: }
3016:
1.298 matthew 3017: sub getsection {
3018: my ($udom,$unam,$courseid)=@_;
1.599 albertel 3019: my $cachetime=1800;
1.551 albertel 3020:
3021: my $hashid="$udom:$unam:$courseid";
1.599 albertel 3022: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 3023: if (defined($cached)) { return $result; }
3024:
1.298 matthew 3025: my %Pending;
3026: my %Expired;
3027: #
3028: # Each role can either have not started yet (pending), be active,
3029: # or have expired.
3030: #
3031: # If there is an active role, we are done.
3032: #
3033: # If there is more than one role which has not started yet,
3034: # choose the one which will start sooner
3035: # If there is one role which has not started yet, return it.
3036: #
3037: # If there is more than one expired role, choose the one which ended last.
3038: # If there is a role which has expired, return it.
3039: #
1.815 albertel 3040: $courseid = &courseid_to_courseurl($courseid);
1.1166 raeburn 3041: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817 raeburn 3042: foreach my $key (keys(%roleshash)) {
1.479 albertel 3043: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 3044: my $section=$1;
3045: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 3046: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 3047: my $now=time;
1.548 albertel 3048: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 3049: $Expired{$end}=$section;
3050: next;
3051: }
1.548 albertel 3052: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 3053: $Pending{$start}=$section;
3054: next;
3055: }
1.599 albertel 3056: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 3057: }
3058: #
3059: # Presumedly there will be few matching roles from the above
3060: # loop and the sorting time will be negligible.
3061: if (scalar(keys(%Pending))) {
3062: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 3063: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 3064: }
3065: if (scalar(keys(%Expired))) {
3066: my @sorted = sort {$a <=> $b} keys(%Expired);
3067: my $time = pop(@sorted);
1.599 albertel 3068: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 3069: }
1.599 albertel 3070: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 3071: }
1.70 www 3072:
1.599 albertel 3073: sub save_cache {
3074: &purge_remembered();
1.722 albertel 3075: #&Apache::loncommon::validate_page();
1.620 albertel 3076: undef(%env);
1.780 albertel 3077: undef($env_loaded);
1.599 albertel 3078: }
1.452 albertel 3079:
1.599 albertel 3080: my $to_remember=-1;
3081: my %remembered;
3082: my %accessed;
3083: my $kicks=0;
3084: my $hits=0;
1.849 albertel 3085: sub make_key {
3086: my ($name,$id) = @_;
1.872 albertel 3087: if (length($id) > 65
3088: && length(&escape($id)) > 200) {
3089: $id=length($id).':'.&Digest::MD5::md5_hex($id);
3090: }
1.849 albertel 3091: return &escape($name.':'.$id);
3092: }
3093:
1.599 albertel 3094: sub devalidate_cache_new {
3095: my ($name,$id,$debug) = @_;
3096: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1172.2.81 raeburn 3097: my $remembered_id=$name.':'.$id;
1.849 albertel 3098: $id=&make_key($name,$id);
1.599 albertel 3099: $memcache->delete($id);
1.1172.2.81 raeburn 3100: delete($remembered{$remembered_id});
3101: delete($accessed{$remembered_id});
1.599 albertel 3102: }
3103:
3104: sub is_cached_new {
3105: my ($name,$id,$debug) = @_;
1.1172.2.81 raeburn 3106: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) for
3107: # keys in %remembered hash, which persists for
3108: # duration of request (no restriction on key length).
3109: if (exists($remembered{$remembered_id})) {
3110: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
3111: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3112: $hits++;
1.1172.2.81 raeburn 3113: return ($remembered{$remembered_id},1);
1.599 albertel 3114: }
1.1172.2.81 raeburn 3115: $id=&make_key($name,$id);
1.599 albertel 3116: my $value = $memcache->get($id);
3117: if (!(defined($value))) {
3118: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 3119: return (undef,undef);
1.416 albertel 3120: }
1.599 albertel 3121: if ($value eq '__undef__') {
3122: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
3123: $value=undef;
3124: }
1.1172.2.81 raeburn 3125: &make_room($remembered_id,$value,$debug);
1.599 albertel 3126: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
3127: return ($value,1);
3128: }
3129:
3130: sub do_cache_new {
3131: my ($name,$id,$value,$time,$debug) = @_;
1.1172.2.81 raeburn 3132: my $remembered_id=$name.':'.$id;
1.849 albertel 3133: $id=&make_key($name,$id);
1.599 albertel 3134: my $setvalue=$value;
3135: if (!defined($setvalue)) {
3136: $setvalue='__undef__';
3137: }
1.623 albertel 3138: if (!defined($time) ) {
3139: $time=600;
3140: }
1.599 albertel 3141: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 3142: my $result = $memcache->set($id,$setvalue,$time);
3143: if (! $result) {
1.872 albertel 3144: &logthis("caching of id -> $id failed");
1.910 albertel 3145: $memcache->disconnect_all();
1.872 albertel 3146: }
1.600 albertel 3147: # need to make a copy of $value
1.1172.2.81 raeburn 3148: &make_room($remembered_id,$value,$debug);
1.599 albertel 3149: return $value;
3150: }
3151:
3152: sub make_room {
1.1172.2.81 raeburn 3153: my ($remembered_id,$value,$debug)=@_;
1.919 albertel 3154:
1.1172.2.81 raeburn 3155: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919 albertel 3156: : $value;
1.599 albertel 3157: if ($to_remember<0) { return; }
1.1172.2.81 raeburn 3158: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 3159: if (scalar(keys(%remembered)) <= $to_remember) { return; }
3160: my $to_kick;
3161: my $max_time=0;
3162: foreach my $other (keys(%accessed)) {
3163: if (&tv_interval($accessed{$other}) > $max_time) {
3164: $to_kick=$other;
3165: $max_time=&tv_interval($accessed{$other});
3166: }
3167: }
3168: delete($remembered{$to_kick});
3169: delete($accessed{$to_kick});
3170: $kicks++;
3171: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 3172: return;
3173: }
3174:
1.599 albertel 3175: sub purge_remembered {
1.604 albertel 3176: #&logthis("Tossing ".scalar(keys(%remembered)));
3177: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 3178: undef(%remembered);
3179: undef(%accessed);
1.428 albertel 3180: }
1.70 www 3181: # ------------------------------------- Read an entry from a user's environment
3182:
3183: sub userenvironment {
3184: my ($udom,$unam,@what)=@_;
1.976 raeburn 3185: my $items;
3186: foreach my $item (@what) {
3187: $items.=&escape($item).'&';
3188: }
3189: $items=~s/\&$//;
1.70 www 3190: my %returnhash=();
1.1009 raeburn 3191: my $uhome = &homeserver($unam,$udom);
3192: unless ($uhome eq 'no_host') {
3193: my @answer=split(/\&/,
3194: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 3195: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
3196: return %returnhash;
3197: }
1.1009 raeburn 3198: my $i;
3199: for ($i=0;$i<=$#what;$i++) {
3200: $returnhash{$what[$i]}=&unescape($answer[$i]);
3201: }
1.70 www 3202: }
3203: return %returnhash;
1.1 albertel 3204: }
3205:
1.617 albertel 3206: # ---------------------------------------------------------- Get a studentphoto
3207: sub studentphoto {
3208: my ($udom,$unam,$ext) = @_;
3209: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 3210: if (defined($env{'request.course.id'})) {
1.708 raeburn 3211: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 3212: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
3213: return(&retrievestudentphoto($udom,$unam,$ext));
3214: } else {
3215: my ($result,$perm_reqd)=
1.707 albertel 3216: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3217: if ($result eq 'ok') {
3218: if (!($perm_reqd eq 'yes')) {
3219: return(&retrievestudentphoto($udom,$unam,$ext));
3220: }
3221: }
3222: }
3223: }
3224: } else {
3225: my ($result,$perm_reqd) =
1.707 albertel 3226: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 3227: if ($result eq 'ok') {
3228: if (!($perm_reqd eq 'yes')) {
3229: return(&retrievestudentphoto($udom,$unam,$ext));
3230: }
3231: }
3232: }
3233: return '/adm/lonKaputt/lonlogo_broken.gif';
3234: }
3235:
3236: sub retrievestudentphoto {
3237: my ($udom,$unam,$ext,$type) = @_;
3238: my $home=&Apache::lonnet::homeserver($unam,$udom);
3239: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
3240: if ($ret eq 'ok') {
3241: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
3242: if ($type eq 'thumbnail') {
3243: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
3244: }
3245: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
3246: return $tokenurl;
3247: } else {
3248: if ($type eq 'thumbnail') {
3249: return '/adm/lonKaputt/genericstudent_tn.gif';
3250: } else {
3251: return '/adm/lonKaputt/lonlogo_broken.gif';
3252: }
1.617 albertel 3253: }
3254: }
3255:
1.263 www 3256: # -------------------------------------------------------------------- New chat
3257:
3258: sub chatsend {
1.724 raeburn 3259: my ($newentry,$anon,$group)=@_;
1.620 albertel 3260: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
3261: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3262: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 3263: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 3264: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 3265: &escape($newentry)).':'.$group,$chome);
1.292 www 3266: }
3267:
3268: # ------------------------------------------ Find current version of a resource
3269:
3270: sub getversion {
3271: my $fname=&clutter(shift);
1.1172.2.10 raeburn 3272: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292 www 3273: return ¤tversion(&filelocation('',$fname));
3274: }
3275:
3276: sub currentversion {
3277: my $fname=shift;
3278: my $author=$fname;
3279: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3280: my ($udom,$uname)=split(/\//,$author);
1.1112 www 3281: my $home=&homeserver($uname,$udom);
1.292 www 3282: if ($home eq 'no_host') {
3283: return -1;
3284: }
1.1112 www 3285: my $answer=&reply("currentversion:$fname",$home);
1.292 www 3286: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3287: return -1;
3288: }
1.1112 www 3289: return $answer;
1.263 www 3290: }
3291:
1.1111 www 3292: #
3293: # Return special version number of resource if set by override, empty otherwise
3294: #
3295: sub usedversion {
3296: my $fname=shift;
3297: unless ($fname) { $fname=$env{'request.uri'}; }
3298: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
3299: if ($urlversion) { return $urlversion; }
3300: return '';
3301: }
3302:
1.1 albertel 3303: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 3304:
1.1 albertel 3305: sub subscribe {
3306: my $fname=shift;
1.761 raeburn 3307: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 3308: $fname=~s/[\n\r]//g;
1.1 albertel 3309: my $author=$fname;
3310: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3311: my ($udom,$uname)=split(/\//,$author);
3312: my $home=homeserver($uname,$udom);
1.335 albertel 3313: if ($home eq 'no_host') {
3314: return 'not_found';
1.1 albertel 3315: }
3316: my $answer=reply("sub:$fname",$home);
1.64 www 3317: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
3318: $answer.=' by '.$home;
3319: }
1.1 albertel 3320: return $answer;
3321: }
3322:
1.8 www 3323: # -------------------------------------------------------------- Replicate file
3324:
3325: sub repcopy {
3326: my $filename=shift;
1.23 www 3327: $filename=~s/\/+/\//g;
1.1142 raeburn 3328: my $londocroot = $perlvar{'lonDocRoot'};
3329: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164 raeburn 3330: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142 raeburn 3331: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
3332: $filename=~m{^/*(uploaded|editupload)/}) {
1.538 albertel 3333: return &repcopy_userfile($filename);
3334: }
1.532 albertel 3335: $filename=~s/[\n\r]//g;
1.8 www 3336: my $transname="$filename.in.transfer";
1.828 www 3337: # FIXME: this should flock
1.607 raeburn 3338: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 3339: my $remoteurl=subscribe($filename);
1.64 www 3340: if ($remoteurl =~ /^con_lost by/) {
3341: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3342: return 'unavailable';
1.8 www 3343: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 3344: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 3345: return 'not_found';
1.64 www 3346: } elsif ($remoteurl =~ /^rejected by/) {
3347: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 3348: return 'forbidden';
1.20 www 3349: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 3350: return 'ok';
1.8 www 3351: } else {
1.290 www 3352: my $author=$filename;
3353: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3354: my ($udom,$uname)=split(/\//,$author);
3355: my $home=homeserver($uname,$udom);
3356: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 3357: my @parts=split(/\//,$filename);
3358: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142 raeburn 3359: if ($path ne "$londocroot/res") {
1.8 www 3360: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 3361: return 'bad_request';
1.8 www 3362: }
3363: my $count;
3364: for ($count=5;$count<$#parts;$count++) {
3365: $path.="/$parts[$count]";
3366: if ((-e $path)!=1) {
3367: mkdir($path,0777);
3368: }
3369: }
3370: my $ua=new LWP::UserAgent;
3371: my $request=new HTTP::Request('GET',"$remoteurl");
3372: my $response=$ua->request($request,$transname);
3373: if ($response->is_error()) {
3374: unlink($transname);
3375: my $message=$response->status_line;
1.672 albertel 3376: &logthis("<font color=\"blue\">WARNING:"
1.12 www 3377: ." LWP get: $message: $filename</font>");
1.607 raeburn 3378: return 'unavailable';
1.8 www 3379: } else {
1.16 www 3380: if ($remoteurl!~/\.meta$/) {
3381: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
3382: my $mresponse=$ua->request($mrequest,$filename.'.meta');
3383: if ($mresponse->is_error()) {
3384: unlink($filename.'.meta');
3385: &logthis(
1.672 albertel 3386: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 3387: }
3388: }
1.8 www 3389: rename($transname,$filename);
1.607 raeburn 3390: return 'ok';
1.8 www 3391: }
1.290 www 3392: }
1.8 www 3393: }
1.330 www 3394: }
3395:
1.1172.2.124 raeburn 3396: # ------------------------------------------------- Unsubscribe from a resource
3397:
3398: sub unsubscribe {
3399: my ($fname) = @_;
3400: my $answer;
3401: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
3402: $fname=~s/[\n\r]//g;
3403: my $author=$fname;
3404: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
3405: my ($udom,$uname)=split(/\//,$author);
3406: my $home=homeserver($uname,$udom);
3407: if ($home eq 'no_host') {
3408: $answer = 'no_host';
3409: } elsif (grep { $_ eq $home } ¤t_machine_ids()) {
3410: $answer = 'home';
3411: } else {
1.1172.2.125 raeburn 3412: $answer = reply("unsub:$fname",$home);
1.1172.2.124 raeburn 3413: }
3414: return $answer;
3415: }
3416:
1.330 www 3417: # ------------------------------------------------ Get server side include body
3418: sub ssi_body {
1.381 albertel 3419: my ($filelink,%form)=@_;
1.606 matthew 3420: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
3421: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
3422: }
1.953 www 3423: my $output='';
3424: my $response;
1.980 raeburn 3425: if ($filelink=~/^https?\:/) {
1.954 raeburn 3426: ($output,$response)=&externalssi($filelink);
1.953 www 3427: } else {
1.1004 droeschl 3428: $filelink .= $filelink=~/\?/ ? '&' : '?';
3429: $filelink .= 'inhibitmenu=yes';
1.953 www 3430: ($output,$response)=&ssi($filelink,%form);
3431: }
1.778 albertel 3432: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 3433: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 3434: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 3435: if (wantarray) {
3436: return ($output, $response);
3437: } else {
3438: return $output;
3439: }
1.8 www 3440: }
3441:
1.15 www 3442: # --------------------------------------------------------- Server Side Include
3443:
1.782 albertel 3444: sub absolute_url {
1.1172.2.145 raeburn 3445: my ($host_name,$unalias,$keep_proto) = @_;
1.782 albertel 3446: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
3447: if ($host_name eq '') {
3448: $host_name = $ENV{'SERVER_NAME'};
3449: }
1.1172.2.145 raeburn 3450: if ($unalias) {
3451: my $alias = &get_proxy_alias();
3452: if ($alias eq $host_name) {
3453: my $lonhost = $perlvar{'lonHostID'};
3454: my $hostname = &hostname($lonhost);
3455: my $lcproto;
3456: if (($keep_proto) || ($hostname eq '')) {
3457: $lcproto = $protocol;
3458: } else {
3459: $lcproto = $protocol{$lonhost};
3460: $lcproto = 'http' if ($lcproto ne 'https');
3461: $lcproto .= '://';
3462: }
3463: unless ($hostname eq '') {
3464: return $lcproto.$hostname;
3465: }
3466: }
3467: }
1.782 albertel 3468: return $protocol.$host_name;
3469: }
3470:
1.942 foxr 3471: #
3472: # Server side include.
3473: # Parameters:
3474: # fn Possibly encrypted resource name/id.
3475: # form Hash that describes how the rendering should be done
3476: # and other things.
1.944 foxr 3477: # Returns:
1.950 raeburn 3478: # Scalar context: The content of the response.
3479: # Array context: 2 element list of the content and the full response object.
1.942 foxr 3480: #
1.15 www 3481: sub ssi {
3482:
1.944 foxr 3483: my ($fn,%form)=@_;
1.1172.2.145 raeburn 3484: my ($host,$request,$response);
3485: $host = &absolute_url('',1);
1.711 albertel 3486:
3487: $form{'no_update_last_known'}=1;
1.895 albertel 3488: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 3489: if (%form) {
1.1172.2.145 raeburn 3490: $request=new HTTP::Request('POST',$host.$fn);
1.1172.2.58 raeburn 3491: $request->content(join('&',map {
3492: my $name = escape($_);
3493: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3494: ? join("&$name=", map {escape($_) } @{$form{$_}})
3495: : &escape($form{$_}) );
3496: } keys(%form)));
1.23 www 3497: } else {
1.1172.2.145 raeburn 3498: $request=new HTTP::Request('GET',$host.$fn);
1.23 www 3499: }
3500:
1.15 www 3501: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1172.2.100 raeburn 3502:
1.1172.2.101 raeburn 3503: if (($env{'request.course.id'}) &&
3504: ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
3505: ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
3506: ($form{'grade_symb'} ne '') &&
3507: (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
3508: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
1.1172.2.100 raeburn 3509: if (LWP::UserAgent->VERSION >= 5.834) {
3510: my $ua=new LWP::UserAgent;
3511: $ua->local_address('127.0.0.1');
3512: $response = $ua->request($request);
3513: } else {
3514: {
3515: require LWP::Protocol::http;
3516: local @LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => '127.0.0.1');
3517: my $ua=new LWP::UserAgent;
3518: $response = $ua->request($request);
3519: @LWP::Protocol::http::EXTRA_SOCK_OPTS = ();
3520: }
3521: }
3522: } else {
3523: my $ua=new LWP::UserAgent;
3524: $response = $ua->request($request);
3525: }
1.944 foxr 3526: if (wantarray) {
1.1172.2.3 raeburn 3527: return ($response->content, $response);
1.944 foxr 3528: } else {
1.1172.2.3 raeburn 3529: return $response->content;
1.942 foxr 3530: }
1.324 www 3531: }
3532:
3533: sub externalssi {
3534: my ($url)=@_;
3535: my $ua=new LWP::UserAgent;
3536: my $request=new HTTP::Request('GET',$url);
3537: my $response=$ua->request($request);
1.954 raeburn 3538: if (wantarray) {
3539: return ($response->content, $response);
3540: } else {
3541: return $response->content;
3542: }
1.15 www 3543: }
1.254 www 3544:
1.1172.2.98 raeburn 3545: # If the local copy of a replicated resource is outdated, trigger a
3546: # connection from the homeserver to flush the delayed queue. If no update
3547: # happens, remove local copies of outdated resource (and corresponding
3548: # metadata file).
3549:
3550: sub remove_stale_resfile {
3551: my ($url) = @_;
3552: my $removed;
3553: if ($url=~m{^/res/($match_domain)/($match_username)/}) {
3554: my $audom = $1;
3555: my $auname = $2;
3556: unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
3557: my $homeserver = &homeserver($auname,$audom);
3558: unless (($homeserver eq 'no_host') ||
3559: (grep { $_ eq $homeserver } ¤t_machine_ids())) {
3560: my $fname = &filelocation('',$url);
3561: if (-e $fname) {
3562: my $hostname = &hostname($homeserver);
3563: if ($hostname) {
1.1172.2.120 raeburn 3564: my $protocol = $protocol{$homeserver};
3565: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.98 raeburn 3566: my $uri = $protocol.'://'.$hostname.'/raw/'.&declutter($url);
1.1172.2.120 raeburn 3567: my $ua=new LWP::UserAgent;
3568: $ua->timeout(5);
1.1172.2.98 raeburn 3569: my $request=new HTTP::Request('HEAD',$uri);
3570: my $response=$ua->request($request);
3571: if ($response->is_success()) {
3572: my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
3573: my $locmodtime = (stat($fname))[9];
3574: if ($locmodtime < $remmodtime) {
3575: my $stale;
3576: my $answer = &reply('pong',$homeserver);
3577: if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
3578: sleep(0.2);
3579: $locmodtime = (stat($fname))[9];
3580: if ($locmodtime < $remmodtime) {
3581: my $posstransfer = $fname.'.in.transfer';
3582: if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
3583: $removed = 1;
3584: } else {
3585: $stale = 1;
3586: }
3587: } else {
3588: $removed = 1;
3589: }
3590: } else {
3591: $stale = 1;
3592: }
3593: if ($stale) {
1.1172.2.124 raeburn 3594: if (unlink($fname)) {
3595: if ($uri!~/\.meta$/) {
3596: if (-e $fname.'.meta') {
3597: unlink($fname.'.meta');
3598: }
3599: }
3600: my $unsubresult = &unsubscribe($fname);
3601: unless ($unsubresult eq 'ok') {
3602: &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
3603: }
3604: $removed = 1;
1.1172.2.98 raeburn 3605: }
3606: }
3607: }
3608: }
3609: }
3610: }
3611: }
3612: }
3613: }
3614: return $removed;
3615: }
3616:
1.492 albertel 3617: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3618:
3619: sub allowuploaded {
3620: my ($srcurl,$url)=@_;
3621: $url=&clutter(&declutter($url));
3622: my $dir=$url;
3623: $dir=~s/\/[^\/]+$//;
3624: my %httpref=();
3625: my $httpurl=&hreflocation('',$url);
3626: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 3627: &Apache::lonnet::appenv(\%httpref);
1.254 www 3628: }
1.477 raeburn 3629:
1.1172.2.13 raeburn 3630: #
3631: # Determine if the current user should be able to edit a particular resource,
3632: # when viewing in course context.
3633: # (a) When viewing resource used to determine if "Edit" item is included in
3634: # Functions.
3635: # (b) When displaying folder contents in course editor, used to determine if
3636: # "Edit" link will be displayed alongside resource.
3637: #
3638: # input: six args -- filename (decluttered), course number, course domain,
3639: # url, symb (if registered) and group (if this is a group
3640: # item -- e.g., bulletin board, group page etc.).
3641: # output: array of five scalars --
3642: # $cfile -- url for file editing if editable on current server
3643: # $home -- homeserver of resource (i.e., for author if published,
3644: # or course if uploaded.).
3645: # $switchserver -- 1 if server switch will be needed.
3646: # $forceedit -- 1 if icon/link should be to go to edit mode
3647: # $forceview -- 1 if icon/link should be to go to view mode
3648: #
3649:
3650: sub can_edit_resource {
3651: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3652: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3653: #
3654: # For aboutme pages user can only edit his/her own.
3655: #
3656: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
3657: my ($sdom,$sname) = ($1,$2);
3658: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3659: $home = $env{'user.home'};
3660: $cfile = $resurl;
3661: if ($env{'form.forceedit'}) {
3662: $forceview = 1;
3663: } else {
3664: $forceedit = 1;
3665: }
3666: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3667: } else {
3668: return;
3669: }
3670: }
3671:
1.1172.2.146. .22(raeb 3672:-24): #
3673:-24): # For /adm/viewcoauthors can only edit if author or co-author who is manager.
3674:-24): #
3675:-24):
3676:-24): if (($resurl eq '/adm/viewcoauthors') && ($cnum ne '') && ($cdom ne '')) {
3677:-24): if (((&allowed('cca',"$cdom/$cnum")) ||
3678:-24): (&allowed('caa',"$cdom/$cnum"))) ||
3679:-24): ((&allowed('vca',"$cdom/$cnum") ||
3680:-24): &allowed('vaa',"$cdom/$cnum")) &&
3681:-24): ($env{"environment.internal.manager./$cdom/$cnum"}))) {
3682:-24): $home = $env{'user.home'};
3683:-24): $cfile = $resurl;
3684:-24): if ($env{'form.forceedit'}) {
3685:-24): $forceview = 1;
3686:-24): } else {
3687:-24): $forceedit = 1;
3688:-24): }
3689:-24): return ($cfile,$home,$switchserver,$forceedit,$forceview);
3690:-24): } else {
3691:-24): return;
3692:-24): }
3693:-24): }
3694:-24):
1.1172.2.13 raeburn 3695: if ($env{'request.course.id'}) {
3696: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3697: if ($group ne '') {
3698: # if this is a group homepage or group bulletin board, check group privs
3699: my $allowed = 0;
3700: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3701: if ((&allowed('mdg',$env{'request.course.id'}.
3702: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3703: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3704: $allowed = 1;
3705: }
3706: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3707: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3708: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3709: $allowed = 1;
3710: }
3711: }
3712: if ($allowed) {
3713: $home=&homeserver($cnum,$cdom);
3714: if ($env{'form.forceedit'}) {
3715: $forceview = 1;
3716: } else {
3717: $forceedit = 1;
3718: }
3719: $cfile = $resurl;
3720: } else {
3721: return;
3722: }
3723: } else {
1.1172.2.15 raeburn 3724: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3725: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3726: return;
3727: }
3728: } elsif (!$crsedit) {
1.1172.2.146. .21(raeb 3729:-24): if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
1.1172.2.13 raeburn 3730: #
3731: # No edit allowed where CC has switched to student role.
3732: #
1.1172.2.146. .21(raeb 3733:-24): return;
3734:-24): } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
3735:-24): ($resurl =~ m{^/res/lib/templates/})) {
3736:-24): return;
3737:-24): }
1.1172.2.13 raeburn 3738: }
3739: }
3740: }
3741:
3742: if ($file ne '') {
3743: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
3744: if (&is_course_upload($file,$cnum,$cdom)) {
3745: $uploaded = 1;
3746: $incourse = 1;
3747: if ($file =~/\.(htm|html|css|js|txt)$/) {
3748: $cfile = &hreflocation('',$file);
3749: if ($env{'form.forceedit'}) {
3750: $forceview = 1;
3751: } else {
3752: $forceedit = 1;
3753: }
3754: }
3755: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3756: $incourse = 1;
3757: if ($env{'form.forceedit'}) {
3758: $forceview = 1;
3759: } else {
3760: $forceedit = 1;
3761: }
3762: $cfile = $resurl;
3763: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3764: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3765: $incourse = 1;
3766: if ($env{'form.forceedit'}) {
3767: $forceview = 1;
3768: } else {
3769: $forceedit = 1;
3770: }
3771: $cfile = $resurl;
3772: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
3773: $incourse = 1;
3774: $cfile = $resurl.'/smpedit';
3775: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
3776: $incourse = 1;
3777: if ($env{'form.forceedit'}) {
3778: $forceview = 1;
3779: } else {
3780: $forceedit = 1;
3781: }
3782: $cfile = $resurl;
1.1172.2.122 raeburn 3783: } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
3784: my ($map,$id,$res) = &decode_symb($symb);
3785: if ($map =~ /\.page$/) {
3786: $incourse = 1;
3787: if ($env{'form.forceedit'}) {
3788: $forceview = 1;
3789: $cfile = $map;
3790: } else {
3791: $forceedit = 1;
3792: $cfile = '/adm/wrapper'.$resurl;
3793: }
3794: }
1.1172.2.146. .1(raebu 3795:22): } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3796:22): $incourse = 1;
3797:22): if ($env{'form.forceedit'}) {
3798:22): $forceview = 1;
3799:22): } else {
3800:22): $forceedit = 1;
3801:22): }
3802:22): $cfile = $resurl;
1.1172.2.15 raeburn 3803: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3804: $incourse = 1;
3805: if ($env{'form.forceedit'}) {
3806: $forceview = 1;
3807: } else {
3808: $forceedit = 1;
3809: }
3810: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3811: }
3812: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3813: my $template = '/res/lib/templates/simpleproblem.problem';
3814: if (&is_on_map($template)) {
3815: $incourse = 1;
3816: $forceview = 1;
3817: $cfile = $template;
3818: }
3819: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1172.2.118 raeburn 3820: $incourse = 1;
3821: if ($env{'form.forceedit'}) {
3822: $forceview = 1;
3823: } else {
3824: $forceedit = 1;
3825: }
3826: $cfile = $resurl;
1.1172.2.146. .1(raebu 3827:22): } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3828:22): $incourse = 1;
3829:22): if ($env{'form.forceedit'}) {
3830:22): $forceview = 1;
3831:22): } else {
3832:22): $forceedit = 1;
3833:22): }
3834:22): $cfile = $resurl;
1.1172.2.13 raeburn 3835: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3836: $incourse = 1;
3837: $forceview = 1;
3838: if ($symb) {
3839: my ($map,$id,$res)=&decode_symb($symb);
3840: $env{'request.symb'} = $symb;
3841: $cfile = &clutter($res);
3842: } else {
3843: $cfile = $env{'form.suppurl'};
1.1172.2.146. .1(raebu 3844:22): my $escfile = &unescape($cfile);
3845:22): if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
3846:22): $cfile = '/adm/wrapper'.$escfile;
3847:22): } else {
3848:22): $escfile =~ s{^http://}{};
3849:22): $cfile = &escape("/adm/wrapper/ext/$escfile");
3850:22): }
1.1172.2.13 raeburn 3851: }
1.1172.2.31 raeburn 3852: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3853: if ($env{'form.forceedit'}) {
3854: $forceview = 1;
3855: } else {
3856: $forceedit = 1;
3857: }
3858: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1172.2.13 raeburn 3859: }
3860: }
3861: if ($uploaded || $incourse) {
3862: $home=&homeserver($cnum,$cdom);
1.1172.2.14 raeburn 3863: } elsif ($file !~ m{/$}) {
1.1172.2.13 raeburn 3864: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3865: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3866: # Check that the user has permission to edit this resource
3867: my $setpriv = 1;
3868: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3869: if (defined($cfudom)) {
3870: $home=&homeserver($cfuname,$cfudom);
3871: $cfile=$file;
3872: }
3873: }
3874: if (($cfile ne '') && (!$incourse || $uploaded) &&
3875: (($home ne '') && ($home ne 'no_host'))) {
3876: my @ids=¤t_machine_ids();
3877: unless (grep(/^\Q$home\E$/,@ids)) {
3878: $switchserver=1;
3879: }
3880: }
3881: }
3882: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3883: }
3884:
3885: sub is_course_upload {
3886: my ($file,$cnum,$cdom) = @_;
3887: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3888: $uploadpath =~ s{^\/}{};
3889: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3890: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
3891: return 1;
3892: }
3893: return;
3894: }
3895:
3896: sub in_course {
3897: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3898: if ($hideprivileged) {
3899: my $skipuser;
1.1172.2.23 raeburn 3900: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3901: my @possdoms = ($cdom);
3902: if ($coursehash{'checkforpriv'}) {
3903: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3904: }
3905: if (&privileged($uname,$udom,\@possdoms)) {
1.1172.2.13 raeburn 3906: $skipuser = 1;
3907: if ($coursehash{'nothideprivileged'}) {
3908: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3909: my $user;
3910: if ($item =~ /:/) {
3911: $user = $item;
3912: } else {
3913: $user = join(':',split(/[\@]/,$item));
3914: }
3915: if ($user eq $uname.':'.$udom) {
3916: undef($skipuser);
3917: last;
3918: }
3919: }
3920: }
3921: if ($skipuser) {
3922: return 0;
3923: }
3924: }
3925: }
3926: $type ||= 'any';
3927: if (!defined($cdom) || !defined($cnum)) {
3928: my $cid = $env{'request.course.id'};
3929: $cdom = $env{'course.'.$cid.'.domain'};
3930: $cnum = $env{'course.'.$cid.'.num'};
3931: }
3932: my $typesref;
3933: if (($type eq 'any') || ($type eq 'all')) {
3934: $typesref = ['active','previous','future'];
3935: } elsif ($type eq 'previous' || $type eq 'future') {
3936: $typesref = [$type];
3937: }
3938: my %roles = &get_my_roles($uname,$udom,'userroles',
3939: $typesref,undef,[$cdom]);
3940: my ($tmp) = keys(%roles);
3941: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3942: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3943: if (@course_roles > 0) {
3944: return 1;
3945: }
3946: return 0;
3947: }
3948:
1.478 albertel 3949: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 3950: # input: action, courseID, current domain, intended
1.637 raeburn 3951: # path to file, source of file, instruction to parse file for objects,
3952: # ref to hash for embedded objects,
3953: # ref to hash for codebase of java objects.
1.1095 raeburn 3954: # reference to scalar to accommodate mime type determined
3955: # from File::MMagic if $parser = parse.
1.637 raeburn 3956: #
1.485 raeburn 3957: # output: url to file (if action was uploaddoc),
3958: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 3959: #
1.478 albertel 3960: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3961: # course.
1.477 raeburn 3962: #
1.478 albertel 3963: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3964: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3965: # course's home server.
1.477 raeburn 3966: #
1.478 albertel 3967: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3968: # be copied from $source (current location) to
3969: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3970: # and will then be copied to
3971: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3972: # course's home server.
1.485 raeburn 3973: #
1.481 raeburn 3974: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 3975: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 3976: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3977: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3978: # in course's home server.
1.637 raeburn 3979: #
1.477 raeburn 3980:
3981: sub process_coursefile {
1.1095 raeburn 3982: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3983: $mimetype)=@_;
1.477 raeburn 3984: my $fetchresult;
1.638 albertel 3985: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 3986: if ($action eq 'propagate') {
1.638 albertel 3987: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3988: $home);
1.481 raeburn 3989: } else {
1.477 raeburn 3990: my $fpath = '';
3991: my $fname = $file;
1.478 albertel 3992: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 3993: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 3994: my $filepath = &build_filepath($fpath);
1.481 raeburn 3995: if ($action eq 'copy') {
3996: if ($source eq '') {
3997: $fetchresult = 'no source file';
3998: return $fetchresult;
3999: } else {
4000: my $destination = $filepath.'/'.$fname;
4001: rename($source,$destination);
4002: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4003: $home);
1.481 raeburn 4004: }
4005: } elsif ($action eq 'uploaddoc') {
1.1172.2.96 raeburn 4006: open(my $fh,'>',$filepath.'/'.$fname);
1.620 albertel 4007: print $fh $env{'form.'.$source};
1.481 raeburn 4008: close($fh);
1.637 raeburn 4009: if ($parser eq 'parse') {
1.1024 raeburn 4010: my $mm = new File::MMagic;
1.1095 raeburn 4011: my $type = $mm->checktype_filename($filepath.'/'.$fname);
4012: if ($type eq 'text/html') {
1.1024 raeburn 4013: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
4014: unless ($parse_result eq 'ok') {
4015: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
4016: }
1.637 raeburn 4017: }
1.1095 raeburn 4018: if (ref($mimetype)) {
4019: $$mimetype = $type;
4020: }
1.637 raeburn 4021: }
1.477 raeburn 4022: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4023: $home);
1.481 raeburn 4024: if ($fetchresult eq 'ok') {
4025: return '/uploaded/'.$fpath.'/'.$fname;
4026: } else {
4027: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4028: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 4029: return '/adm/notfound.html';
4030: }
1.477 raeburn 4031: }
4032: }
1.485 raeburn 4033: unless ( $fetchresult eq 'ok') {
1.477 raeburn 4034: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 4035: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 4036: }
4037: return $fetchresult;
4038: }
4039:
1.637 raeburn 4040: sub build_filepath {
4041: my ($fpath) = @_;
4042: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
4043: unless ($fpath eq '') {
4044: my @parts=split('/',$fpath);
4045: foreach my $part (@parts) {
4046: $filepath.= '/'.$part;
4047: if ((-e $filepath)!=1) {
4048: mkdir($filepath,0777);
4049: }
4050: }
4051: }
4052: return $filepath;
4053: }
4054:
4055: sub store_edited_file {
1.638 albertel 4056: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 4057: my $file = $primary_url;
4058: $file =~ s#^/uploaded/$docudom/$docuname/##;
4059: my $fpath = '';
4060: my $fname = $file;
4061: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
4062: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
4063: my $filepath = &build_filepath($fpath);
1.1172.2.96 raeburn 4064: open(my $fh,'>',$filepath.'/'.$fname);
1.637 raeburn 4065: print $fh $content;
4066: close($fh);
1.638 albertel 4067: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 4068: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 4069: $home);
1.637 raeburn 4070: if ($$fetchresult eq 'ok') {
4071: return '/uploaded/'.$fpath.'/'.$fname;
4072: } else {
1.638 albertel 4073: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
4074: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 4075: return '/adm/notfound.html';
4076: }
4077: }
4078:
1.531 albertel 4079: sub clean_filename {
1.831 albertel 4080: my ($fname,$args)=@_;
1.315 www 4081: # Replace Windows backslashes by forward slashes
1.257 www 4082: $fname=~s/\\/\//g;
1.831 albertel 4083: if (!$args->{'keep_path'}) {
4084: # Get rid of everything but the actual filename
4085: $fname=~s/^.*\/([^\/]+)$/$1/;
4086: }
1.315 www 4087: # Replace spaces by underscores
4088: $fname=~s/\s+/\_/g;
1.1172.2.110 raeburn 4089: # Transliterate non-ascii text to ascii
4090: my $lang = &Apache::lonlocal::current_language();
4091: $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
1.315 www 4092: # Replace all other weird characters by nothing
1.831 albertel 4093: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 4094: # Replace all .\d. sequences with _\d. so they no longer look like version
4095: # numbers
4096: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.1172.2.140 raeburn 4097: # Replace three or more adjacent underscores with one for consistency
4098: # with loncfile::filename_check() so complete url can be extracted by
4099: # lonnet::decode_symb()
4100: $fname=~s/_{3,}/_/g;
1.531 albertel 4101: return $fname;
4102: }
1.1172.2.110 raeburn 4103:
1.1051 raeburn 4104: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
4105: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
4106: # image with the same aspect ratio as the original, but with dimensions which do
4107: # not exceed $resizewidth and $resizeheight.
4108:
1.984 neumanie 4109: sub resizeImage {
1.1051 raeburn 4110: my ($img_path,$resizewidth,$resizeheight) = @_;
4111: my $ima = Image::Magick->new;
4112: my $resized;
4113: if (-e $img_path) {
4114: $ima->Read($img_path);
4115: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
4116: my $width = $ima->Get('width');
4117: my $height = $ima->Get('height');
4118: if ($width > $resizewidth) {
4119: my $factor = $width/$resizewidth;
4120: my $newheight = $height/$factor;
4121: $ima->Scale(width=>$resizewidth,height=>$newheight);
4122: $resized = 1;
4123: }
4124: }
4125: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
4126: my $width = $ima->Get('width');
4127: my $height = $ima->Get('height');
4128: if ($height > $resizeheight) {
4129: my $factor = $height/$resizeheight;
4130: my $newwidth = $width/$factor;
4131: $ima->Scale(width=>$newwidth,height=>$resizeheight);
4132: $resized = 1;
4133: }
4134: }
4135: if ($resized) {
4136: $ima->Write($img_path);
4137: }
4138: }
4139: return;
1.977 amueller 4140: }
4141:
1.608 albertel 4142: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 4143: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093 raeburn 4144: # the desired filename is in $env{"form.$formname.filename"}
1.1090 raeburn 4145: # $context - possible values: coursedoc, existingfile, overwrite,
1.1172.2.146. .13(raeb 4146:-23): # canceloverwrite, scantron, toollogo or ''.
1.1090 raeburn 4147: # if 'coursedoc': upload to the current course
4148: # if 'existingfile': write file to tmp/overwrites directory
4149: # if 'canceloverwrite': delete file written to tmp/overwrites directory
4150: # $context is passed as argument to &finishuserfileupload
1.686 albertel 4151: # $subdir - directory in userfile to store the file into
1.1172.2.109 raeburn 4152: # $parser - instruction to parse file for objects ($parser = parse) or
4153: # if context is 'scantron', $parser is hashref of csv column mapping
4154: # (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3,
4155: # Section => 4, CODE => 5, FirstQuestion => 9 }).
1.858 raeburn 4156: # $allfiles - reference to hash for embedded objects
4157: # $codebase - reference to hash for codebase of java objects
1.1172.2.146. .13(raeb 4158:-23): # $destuname - username for permanent storage of uploaded file
4159:-23): # $destudom - domain for permanaent storage of uploaded file
1.860 raeburn 4160: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
4161: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 4162: # $resizewidth - width (pixels) to which to resize uploaded image
4163: # $resizeheight - height (pixels) to which to resize uploaded image
1.1095 raeburn 4164: # $mimetype - reference to scalar to accommodate mime type determined
1.1152 raeburn 4165: # from File::MMagic.
1.858 raeburn 4166: #
1.686 albertel 4167: # output: url of file in userspace, or error: <message>
4168: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 4169:
1.531 albertel 4170: sub userfileupload {
1.1090 raeburn 4171: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095 raeburn 4172: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 4173: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 4174: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 4175: $fname=&clean_filename($fname);
1.1090 raeburn 4176: # See if there is anything left
1.257 www 4177: unless ($fname) { return 'error: no uploaded file'; }
1.1172.2.110 raeburn 4178: # If filename now begins with a . prepend unix timestamp _ milliseconds
4179: if ($fname =~ /^\./) {
4180: my ($s,$usec) = &gettimeofday();
4181: while (length($usec) < 6) {
4182: $usec = '0'.$usec;
4183: }
4184: $fname = $s.'_'.substr($usec,0,3).$fname;
4185: }
1.1090 raeburn 4186: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
4187: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
4188: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
4189: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 4190: my $now = time;
1.1090 raeburn 4191: my $filepath;
1.1095 raeburn 4192: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090 raeburn 4193: $filepath = 'tmp/helprequests/'.$now;
4194: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
4195: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
4196: '_'.$env{'user.domain'}.'/pending';
4197: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
4198: my ($docuname,$docudom);
1.1172.2.96 raeburn 4199: if ($destudom =~ /^$match_domain$/) {
1.1090 raeburn 4200: $docudom = $destudom;
4201: } else {
4202: $docudom = $env{'user.domain'};
4203: }
1.1172.2.96 raeburn 4204: if ($destuname =~ /^$match_username$/) {
1.1090 raeburn 4205: $docuname = $destuname;
4206: } else {
4207: $docuname = $env{'user.name'};
4208: }
4209: if (exists($env{'form.group'})) {
4210: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4211: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4212: }
4213: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
4214: if ($context eq 'canceloverwrite') {
4215: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
4216: if (-e $tempfile) {
4217: my @info = stat($tempfile);
4218: if ($info[9] eq $env{'form.timestamp'}) {
4219: unlink($tempfile);
4220: }
4221: }
4222: return;
1.523 raeburn 4223: }
4224: }
1.1090 raeburn 4225: # Create the directory if not present
1.741 raeburn 4226: my @parts=split(/\//,$filepath);
4227: my $fullpath = $perlvar{'lonDaemons'};
4228: for (my $i=0;$i<@parts;$i++) {
4229: $fullpath .= '/'.$parts[$i];
4230: if ((-e $fullpath)!=1) {
4231: mkdir($fullpath,0777);
4232: }
4233: }
1.1172.2.96 raeburn 4234: open(my $fh,'>',$fullpath.'/'.$fname);
1.741 raeburn 4235: print $fh $env{'form.'.$formname};
4236: close($fh);
1.1090 raeburn 4237: if ($context eq 'existingfile') {
4238: my @info = stat($fullpath.'/'.$fname);
4239: return ($fullpath.'/'.$fname,$info[9]);
4240: } else {
4241: return $fullpath.'/'.$fname;
4242: }
1.523 raeburn 4243: }
1.995 raeburn 4244: if ($subdir eq 'scantron') {
4245: $fname = 'scantron_orig_'.$fname;
1.1093 raeburn 4246: } else {
1.995 raeburn 4247: $fname="$subdir/$fname";
4248: }
1.1090 raeburn 4249: if ($context eq 'coursedoc') {
1.638 albertel 4250: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4251: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 4252: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 4253: return &finishuserfileupload($docuname,$docudom,
4254: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 4255: $codebase,$thumbwidth,$thumbheight,
1.1095 raeburn 4256: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 4257: } else {
1.1172.2.21 raeburn 4258: if ($env{'form.folder'}) {
4259: $fname=$env{'form.folder'}.'/'.$fname;
4260: }
1.638 albertel 4261: return &process_coursefile('uploaddoc',$docuname,$docudom,
4262: $fname,$formname,$parser,
1.1095 raeburn 4263: $allfiles,$codebase,$mimetype);
1.481 raeburn 4264: }
1.719 banghart 4265: } elsif (defined($destuname)) {
4266: my $docuname=$destuname;
4267: my $docudom=$destudom;
1.860 raeburn 4268: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4269: $parser,$allfiles,$codebase,
1.1051 raeburn 4270: $thumbwidth,$thumbheight,
1.1095 raeburn 4271: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4272: } else {
1.638 albertel 4273: my $docuname=$env{'user.name'};
4274: my $docudom=$env{'user.domain'};
1.1172.2.23 raeburn 4275: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714 raeburn 4276: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
4277: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
4278: }
1.860 raeburn 4279: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
4280: $parser,$allfiles,$codebase,
1.1051 raeburn 4281: $thumbwidth,$thumbheight,
1.1095 raeburn 4282: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 4283: }
1.271 www 4284: }
4285:
4286: sub finishuserfileupload {
1.860 raeburn 4287: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095 raeburn 4288: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 4289: my $path=$docudom.'/'.$docuname.'/';
1.258 www 4290: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 4291:
1.860 raeburn 4292: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 4293: $file=$fname;
4294: if ($fname=~m|/|) {
4295: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
4296: $path.=$fnamepath.'/';
4297: }
1.259 www 4298: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 4299: my $count;
4300: for ($count=4;$count<=$#parts;$count++) {
4301: $filepath.="/$parts[$count]";
4302: if ((-e $filepath)!=1) {
4303: mkdir($filepath,0777);
4304: }
4305: }
1.984 neumanie 4306:
1.258 www 4307: # Save the file
4308: {
1.1172.2.96 raeburn 4309: if (!open(FH,'>',$filepath.'/'.$file)) {
1.701 albertel 4310: &logthis('Failed to create '.$filepath.'/'.$file);
4311: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
4312: return '/adm/notfound.html';
4313: }
1.1090 raeburn 4314: if ($context eq 'overwrite') {
1.1117 foxr 4315: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090 raeburn 4316: my $target = $filepath.'/'.$file;
4317: if (-e $source) {
4318: my @info = stat($source);
4319: if ($info[9] eq $env{'form.timestamp'}) {
4320: unless (&File::Copy::move($source,$target)) {
4321: &logthis('Failed to overwrite '.$filepath.'/'.$file);
4322: return "Moving from $source failed";
4323: }
4324: } else {
4325: return "Temporary file: $source had unexpected date/time for last modification";
4326: }
4327: } else {
4328: return "Temporary file: $source missing";
4329: }
4330: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 4331: &logthis('Failed to write to '.$filepath.'/'.$file);
4332: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
4333: return '/adm/notfound.html';
4334: }
1.570 albertel 4335: close(FH);
1.1051 raeburn 4336: if ($resizewidth && $resizeheight) {
4337: my $mm = new File::MMagic;
4338: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
4339: if ($mime_type =~ m{^image/}) {
4340: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
4341: }
1.977 amueller 4342: }
1.258 www 4343: }
1.1152 raeburn 4344: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
4345: if (ref($mimetype)) {
4346: if ($$mimetype eq '') {
4347: my $mm = new File::MMagic;
4348: my $type = $mm->checktype_filename($filepath.'/'.$file);
4349: $$mimetype = $type;
4350: }
4351: }
4352: }
1.1172.2.109 raeburn 4353: if (($context ne 'scantron') && ($parser eq 'parse')) {
1.1152 raeburn 4354: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024 raeburn 4355: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
4356: $allfiles,$codebase);
4357: unless ($parse_result eq 'ok') {
4358: &logthis('Failed to parse '.$filepath.$file.
4359: ' for embedded media: '.$parse_result);
4360: }
1.637 raeburn 4361: }
1.1172.2.109 raeburn 4362: } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
4363: my $format = $env{'form.scantron_format'};
4364: &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
1.637 raeburn 4365: }
1.860 raeburn 4366: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
4367: my $input = $filepath.'/'.$file;
4368: my $output = $filepath.'/'.'tn-'.$file;
1.1172.2.146. .13(raeb 4369:-23): my $makethumb;
1.860 raeburn 4370: my $thumbsize = $thumbwidth.'x'.$thumbheight;
1.1172.2.146. .13(raeb 4371:-23): if ($context eq 'toollogo') {
4372:-23): my ($fullwidth,$fullheight) = &check_dimensions($input);
4373:-23): if ($fullwidth ne '' && $fullheight ne '') {
4374:-23): if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
4375:-23): $makethumb = 1;
4376:-23): }
4377:-23): }
4378:-23): } else {
4379:-23): $makethumb = 1;
4380:-23): }
4381:-23): if ($makethumb) {
4382:-23): my @args = ('convert','-sample',$thumbsize,$input,$output);
4383:-23): system({$args[0]} @args);
4384:-23): if (-e $filepath.'/'.'tn-'.$file) {
4385:-23): $fetchthumb = 1;
4386:-23): }
1.860 raeburn 4387: }
4388: }
1.858 raeburn 4389:
1.259 www 4390: # Notify homeserver to grep it
4391: #
1.984 neumanie 4392: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 4393: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 4394: if ($fetchresult eq 'ok') {
1.860 raeburn 4395: if ($fetchthumb) {
4396: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
4397: if ($thumbresult ne 'ok') {
4398: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
4399: $docuhome.': '.$thumbresult);
4400: }
4401: }
1.259 www 4402: #
1.258 www 4403: # Return the URL to it
1.494 albertel 4404: return '/uploaded/'.$path.$file;
1.263 www 4405: } else {
1.494 albertel 4406: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
4407: ': '.$fetchresult);
1.263 www 4408: return '/adm/notfound.html';
1.858 raeburn 4409: }
1.493 albertel 4410: }
4411:
1.637 raeburn 4412: sub extract_embedded_items {
1.961 raeburn 4413: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 4414: my @state = ();
1.1164 raeburn 4415: my (%lastids,%related,%shockwave,%flashvars);
1.637 raeburn 4416: my %javafiles = (
4417: codebase => '',
4418: code => '',
4419: archive => ''
4420: );
4421: my %mediafiles = (
4422: src => '',
4423: movie => '',
4424: );
1.648 raeburn 4425: my $p;
4426: if ($content) {
4427: $p = HTML::LCParser->new($content);
4428: } else {
1.961 raeburn 4429: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 4430: }
1.641 albertel 4431: while (my $t=$p->get_token()) {
1.640 albertel 4432: if ($t->[0] eq 'S') {
4433: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 4434: push(@state, $tagname);
1.648 raeburn 4435: if (lc($tagname) eq 'allow') {
4436: &add_filetype($allfiles,$attr->{'src'},'src');
4437: }
1.640 albertel 4438: if (lc($tagname) eq 'img') {
4439: &add_filetype($allfiles,$attr->{'src'},'src');
4440: }
1.886 albertel 4441: if (lc($tagname) eq 'a') {
1.1172.2.24 raeburn 4442: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
4443: &add_filetype($allfiles,$attr->{'href'},'href');
4444: }
1.886 albertel 4445: }
1.645 raeburn 4446: if (lc($tagname) eq 'script') {
1.1164 raeburn 4447: my $src;
1.645 raeburn 4448: if ($attr->{'archive'} =~ /\.jar$/i) {
4449: &add_filetype($allfiles,$attr->{'archive'},'archive');
4450: } else {
1.1164 raeburn 4451: if ($attr->{'src'} ne '') {
4452: $src = $attr->{'src'};
4453: &add_filetype($allfiles,$src,'src');
4454: }
4455: }
4456: my $text = $p->get_trimmed_text();
4457: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
4458: my @swfargs = split(/,/,$1);
4459: foreach my $item (@swfargs) {
4460: $item =~ s/["']//g;
4461: $item =~ s/^\s+//;
4462: $item =~ s/\s+$//;
4463: }
4464: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
4465: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
4466: push(@{$related{$swfargs[0]}},$swfargs[2]);
4467: } else {
4468: $related{$swfargs[0]} = [$swfargs[2]];
4469: }
4470: }
1.645 raeburn 4471: }
4472: }
4473: if (lc($tagname) eq 'link') {
4474: if (lc($attr->{'rel'}) eq 'stylesheet') {
4475: &add_filetype($allfiles,$attr->{'href'},'href');
4476: }
4477: }
1.640 albertel 4478: if (lc($tagname) eq 'object' ||
4479: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
4480: foreach my $item (keys(%javafiles)) {
4481: $javafiles{$item} = '';
4482: }
1.1164 raeburn 4483: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
4484: $lastids{lc($tagname)} = $attr->{'id'};
4485: }
1.640 albertel 4486: }
4487: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
4488: my $name = lc($attr->{'name'});
4489: foreach my $item (keys(%javafiles)) {
4490: if ($name eq $item) {
4491: $javafiles{$item} = $attr->{'value'};
4492: last;
4493: }
4494: }
1.1164 raeburn 4495: my $pathfrom;
1.640 albertel 4496: foreach my $item (keys(%mediafiles)) {
4497: if ($name eq $item) {
1.1164 raeburn 4498: $pathfrom = $attr->{'value'};
4499: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
4500: &add_filetype($allfiles,$pathfrom,$name);
1.640 albertel 4501: last;
4502: }
4503: }
1.1164 raeburn 4504: if ($name eq 'flashvars') {
4505: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
4506: }
4507: if ($pathfrom ne '') {
4508: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
4509: $pathfrom);
4510: }
1.640 albertel 4511: }
4512: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
4513: foreach my $item (keys(%javafiles)) {
4514: if ($attr->{$item}) {
4515: $javafiles{$item} = $attr->{$item};
4516: last;
4517: }
4518: }
4519: foreach my $item (keys(%mediafiles)) {
4520: if ($attr->{$item}) {
4521: &add_filetype($allfiles,$attr->{$item},$item);
4522: last;
4523: }
4524: }
1.1164 raeburn 4525: if (lc($tagname) eq 'embed') {
4526: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
4527: &embedded_dependency($allfiles,\%related,$attr->{'name'},
4528: $attr->{'src'});
4529: }
4530: }
1.640 albertel 4531: }
1.1172.2.35 raeburn 4532: if (lc($tagname) eq 'iframe') {
4533: my $src = $attr->{'src'} ;
4534: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
4535: &add_filetype($allfiles,$src,'src');
4536: } elsif ($src =~ m{^/}) {
4537: if ($env{'request.course.id'}) {
4538: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
4539: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
4540: my $url = &hreflocation('',$fullpath);
4541: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
4542: my $relpath = $1;
4543: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
4544: &add_filetype($allfiles,$1,'src');
4545: }
4546: }
4547: }
4548: }
4549: }
1.1164 raeburn 4550: if ($t->[4] =~ m{/>$}) {
1.1172.2.35 raeburn 4551: pop(@state);
1.1164 raeburn 4552: }
1.640 albertel 4553: } elsif ($t->[0] eq 'E') {
4554: my ($tagname) = ($t->[1]);
4555: if ($javafiles{'codebase'} ne '') {
4556: $javafiles{'codebase'} .= '/';
4557: }
4558: if (lc($tagname) eq 'applet' ||
4559: lc($tagname) eq 'object' ||
4560: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
4561: ) {
4562: foreach my $item (keys(%javafiles)) {
4563: if ($item ne 'codebase' && $javafiles{$item} ne '') {
4564: my $file=$javafiles{'codebase'}.$javafiles{$item};
4565: &add_filetype($allfiles,$file,$item);
4566: }
4567: }
4568: }
4569: pop @state;
4570: }
4571: }
1.1164 raeburn 4572: foreach my $id (sort(keys(%flashvars))) {
4573: if ($shockwave{$id} ne '') {
4574: my @pairs = split(/\&/,$flashvars{$id});
4575: foreach my $pair (@pairs) {
4576: my ($key,$value) = split(/\=/,$pair);
4577: if ($key eq 'thumb') {
4578: &add_filetype($allfiles,$value,$key);
4579: } elsif ($key eq 'content') {
4580: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
4581: my ($ext) = ($value =~ /\.([^.]+)$/);
4582: if ($ext ne '') {
4583: &add_filetype($allfiles,$path.$value,$ext);
4584: }
4585: }
4586: }
4587: }
4588: }
1.637 raeburn 4589: return 'ok';
4590: }
4591:
1.639 albertel 4592: sub add_filetype {
4593: my ($allfiles,$file,$type)=@_;
4594: if (exists($allfiles->{$file})) {
4595: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
4596: push(@{$allfiles->{$file}}, &escape($type));
4597: }
4598: } else {
4599: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 4600: }
4601: }
4602:
1.1164 raeburn 4603: sub embedded_dependency {
4604: my ($allfiles,$related,$identifier,$pathfrom) = @_;
4605: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
4606: if (($identifier ne '') &&
4607: (ref($related->{$identifier}) eq 'ARRAY') &&
4608: ($pathfrom ne '')) {
4609: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
4610: foreach my $dep (@{$related->{$identifier}}) {
4611: &add_filetype($allfiles,$path.$dep,'object');
4612: }
4613: }
4614: }
4615: return;
4616: }
4617:
1.1172.2.146. .13(raeb 4618:-23): sub check_dimensions {
4619:-23): my ($inputfile) = @_;
4620:-23): my ($fullwidth,$fullheight);
4621:-23): if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
4622:-23): my $mm = new File::MMagic;
4623:-23): my $mime_type = $mm->checktype_filename($inputfile);
4624:-23): if ($mime_type =~ m{^image/}) {
4625:-23): if (open(PIPE,"identify $inputfile 2>&1 |")) {
4626:-23): my $imageinfo = <PIPE>;
4627:-23): if (!close(PIPE)) {
4628:-23): &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
4629:-23): }
4630:-23): chomp($imageinfo);
4631:-23): my ($fullsize) =
4632:-23): ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
4633:-23): if ($fullsize) {
4634:-23): ($fullwidth,$fullheight) = split(/x/,$fullsize);
4635:-23): }
4636:-23): }
4637:-23): }
4638:-23): }
4639:-23): return ($fullwidth,$fullheight);
4640:-23): }
4641:-23):
1.1172.2.109 raeburn 4642: sub bubblesheet_converter {
4643: my ($cdom,$fullpath,$config,$format) = @_;
4644: if ((&domain($cdom) ne '') &&
4645: ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
4646: (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
4647: my (%csvcols,%csvoptions);
4648: if (ref($config->{'fields'}) eq 'HASH') {
4649: %csvcols = %{$config->{'fields'}};
4650: }
4651: if (ref($config->{'options'}) eq 'HASH') {
4652: %csvoptions = %{$config->{'options'}};
4653: }
4654: my %csvbynum = reverse(%csvcols);
4655: my %scantronconf = &get_scantron_config($format,$cdom);
4656: if (keys(%scantronconf)) {
4657: my %bynum = (
4658: $scantronconf{CODEstart} => 'CODEstart',
4659: $scantronconf{IDstart} => 'IDstart',
4660: $scantronconf{PaperID} => 'PaperID',
4661: $scantronconf{FirstName} => 'FirstName',
4662: $scantronconf{LastName} => 'LastName',
4663: $scantronconf{Qstart} => 'Qstart',
4664: );
4665: my @ordered;
4666: foreach my $item (sort { $a <=> $b } keys(%bynum)) {
4667: push(@ordered,$bynum{$item});
4668: }
4669: my %mapstart = (
4670: CODEstart => 'CODE',
4671: IDstart => 'ID',
4672: PaperID => 'PaperID',
4673: FirstName => 'FirstName',
4674: LastName => 'LastName',
4675: Qstart => 'FirstQuestion',
4676: );
4677: my %maplength = (
4678: CODEstart => 'CODElength',
4679: IDstart => 'IDlength',
4680: PaperID => 'PaperIDlength',
4681: FirstName => 'FirstNamelength',
4682: LastName => 'LastNamelength',
4683: );
4684: if (open(my $fh,'<',$fullpath)) {
4685: my $output;
4686: my %lettdig = &letter_to_digits();
4687: my %diglett = reverse(%lettdig);
4688: my $numletts = scalar(keys(%lettdig));
4689: my $num = 0;
4690: while (my $line=<$fh>) {
4691: $num ++;
4692: next if (($num == 1) && ($csvoptions{'hdr'} == 1));
4693: $line =~ s{[\r\n]+$}{};
4694: my %found;
1.1172.2.143 raeburn 4695: my @values = split(/,/,$line,-1);
1.1172.2.109 raeburn 4696: my ($qstart,$record);
4697: for (my $i=0; $i<@values; $i++) {
4698: if ((($qstart ne '') && ($i > $qstart)) ||
4699: ($csvbynum{$i} eq 'FirstQuestion')) {
4700: if ($values[$i] eq '') {
4701: $values[$i] = $scantronconf{'Qoff'};
4702: } elsif ($scantronconf{'Qon'} eq 'number') {
4703: if ($values[$i] =~ /^[A-Ja-j]$/) {
4704: $values[$i] = $lettdig{uc($values[$i])};
4705: }
4706: } elsif ($scantronconf{'Qon'} eq 'letter') {
4707: if ($values[$i] =~ /^[0-9]$/) {
4708: $values[$i] = $diglett{$values[$i]};
4709: }
4710: } else {
4711: if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
4712: my $digit;
4713: if ($values[$i] =~ /^[A-Ja-j]$/) {
4714: $digit = $lettdig{uc($values[$i])}-1;
4715: if ($values[$i] eq 'J') {
4716: $digit += $numletts;
4717: }
4718: } elsif ($values[$i] =~ /^[0-9]$/) {
4719: $digit = $values[$i]-1;
4720: if ($values[$i] eq '0') {
4721: $digit += $numletts;
4722: }
4723: }
4724: my $qval='';
4725: for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
4726: if ($j == $digit) {
4727: $qval .= $scantronconf{'Qon'};
4728: } else {
4729: $qval .= $scantronconf{'Qoff'};
4730: }
4731: }
4732: $values[$i] = $qval;
4733: }
4734: }
4735: if (length($values[$i]) > $scantronconf{'Qlength'}) {
4736: $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
4737: }
4738: my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
4739: if ($numblank > 0) {
4740: $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
4741: }
4742: if ($csvbynum{$i} eq 'FirstQuestion') {
4743: $qstart = $i;
4744: $found{$csvbynum{$i}} = $values[$i];
4745: } else {
4746: $found{'FirstQuestion'} .= $values[$i];
4747: }
4748: } elsif (exists($csvbynum{$i})) {
4749: if ($csvoptions{'rem'}) {
4750: $values[$i] =~ s/^\s+//;
4751: }
4752: if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
4753: while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
4754: $values[$i] = '0'.$values[$i];
4755: }
4756: }
4757: $found{$csvbynum{$i}} = $values[$i];
4758: }
4759: }
4760: foreach my $item (@ordered) {
4761: my $currlength = 1+length($record);
4762: my $numspaces = $scantronconf{$item} - $currlength;
4763: if ($numspaces > 0) {
4764: $record .= (' ' x $numspaces);
4765: }
4766: if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
4767: unless ($item eq 'Qstart') {
4768: if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
4769: $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
4770: }
4771: }
4772: $record .= $found{$mapstart{$item}};
4773: }
4774: }
4775: $output .= "$record\n";
4776: }
4777: close($fh);
4778: if ($output) {
4779: if (open(my $fh,'>',$fullpath)) {
4780: print $fh $output;
4781: close($fh);
4782: }
4783: }
4784: }
4785: }
4786: return;
4787: }
4788: }
4789:
4790: sub letter_to_digits {
4791: my %lettdig = (
4792: A => 1,
4793: B => 2,
4794: C => 3,
4795: D => 4,
4796: E => 5,
4797: F => 6,
4798: G => 7,
4799: H => 8,
4800: I => 9,
4801: J => 0,
4802: );
4803: return %lettdig;
4804: }
4805:
4806: sub get_scantron_config {
4807: my ($which,$cdom) = @_;
4808: my @lines = &get_scantronformat_file($cdom);
4809: my %config;
4810: #FIXME probably should move to XML it has already gotten a bit much now
4811: foreach my $line (@lines) {
4812: my ($name,$descrip)=split(/:/,$line);
4813: if ($name ne $which ) { next; }
4814: chomp($line);
4815: my @config=split(/:/,$line);
4816: $config{'name'}=$config[0];
4817: $config{'description'}=$config[1];
4818: $config{'CODElocation'}=$config[2];
4819: $config{'CODEstart'}=$config[3];
4820: $config{'CODElength'}=$config[4];
4821: $config{'IDstart'}=$config[5];
4822: $config{'IDlength'}=$config[6];
4823: $config{'Qstart'}=$config[7];
4824: $config{'Qlength'}=$config[8];
4825: $config{'Qoff'}=$config[9];
4826: $config{'Qon'}=$config[10];
4827: $config{'PaperID'}=$config[11];
4828: $config{'PaperIDlength'}=$config[12];
4829: $config{'FirstName'}=$config[13];
4830: $config{'FirstNamelength'}=$config[14];
4831: $config{'LastName'}=$config[15];
4832: $config{'LastNamelength'}=$config[16];
4833: $config{'BubblesPerRow'}=$config[17];
4834: last;
4835: }
4836: return %config;
4837: }
4838:
4839: sub get_scantronformat_file {
4840: my ($cdom) = @_;
4841: if ($cdom eq '') {
4842: $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
4843: }
4844: my %domconfig = &get_dom('configuration',['scantron'],$cdom);
4845: my $gottab = 0;
4846: my @lines;
4847: if (ref($domconfig{'scantron'}) eq 'HASH') {
4848: if ($domconfig{'scantron'}{'scantronformat'} ne '') {
4849: my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
4850: if ($formatfile ne '-1') {
4851: @lines = split("\n",$formatfile,-1);
4852: $gottab = 1;
4853: }
4854: }
4855: }
4856: if (!$gottab) {
4857: my $confname = $cdom.'-domainconfig';
4858: my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
4859: my $formatfile = &getfile($default);
4860: if ($formatfile ne '-1') {
4861: @lines = split("\n",$formatfile,-1);
4862: $gottab = 1;
4863: }
4864: }
4865: if (!$gottab) {
4866: my @domains = ¤t_machine_domains();
4867: if (grep(/^\Q$cdom\E$/,@domains)) {
4868: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
4869: @lines = <$fh>;
4870: close($fh);
4871: }
4872: } else {
4873: if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
4874: @lines = <$fh>;
4875: close($fh);
4876: }
4877: }
1.1172.2.146. .8(raebu 4878:23): chomp(@lines);
1.1172.2.109 raeburn 4879: }
4880: return @lines;
4881: }
4882:
1.493 albertel 4883: sub removeuploadedurl {
1.984 neumanie 4884: my ($url)=@_;
4885: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 4886: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 4887: }
4888:
4889: sub removeuserfile {
4890: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 4891: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4892: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 4893: if ($result eq 'ok') {
1.798 raeburn 4894: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
4895: my $metafile = $fname.'.meta';
4896: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 4897: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 4898: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4899: my $sqlresult =
1.823 albertel 4900: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4901: 'portfolio_metadata',$group,
4902: 'delete');
1.798 raeburn 4903: }
4904: }
4905: return $result;
1.257 www 4906: }
1.15 www 4907:
1.530 albertel 4908: sub mkdiruserfile {
4909: my ($docuname,$docudom,$dir)=@_;
4910: my $home=&homeserver($docuname,$docudom);
4911: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
4912: }
4913:
1.531 albertel 4914: sub renameuserfile {
4915: my ($docuname,$docudom,$old,$new)=@_;
4916: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4917: my $result = &reply("renameuserfile:$docudom:$docuname:".
4918: &escape("$old").':'.&escape("$new"),$home);
4919: if ($result eq 'ok') {
4920: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4921: my $oldmeta = $old.'.meta';
4922: my $newmeta = $new.'.meta';
4923: my $metaresult =
4924: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 4925: my $url = "/uploaded/$docudom/$docuname/$old";
4926: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4927: my $sqlresult =
1.823 albertel 4928: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4929: 'portfolio_metadata',$group,
4930: 'delete');
1.798 raeburn 4931: }
4932: }
4933: return $result;
1.531 albertel 4934: }
4935:
1.14 www 4936: # ------------------------------------------------------------------------- Log
4937:
4938: sub log {
4939: my ($dom,$nam,$hom,$what)=@_;
1.47 www 4940: return critical("log:$dom:$nam:$what",$hom);
1.157 www 4941: }
4942:
4943: # ------------------------------------------------------------------ Course Log
1.352 www 4944: #
4945: # This routine flushes several buffers of non-mission-critical nature
4946: #
1.157 www 4947:
4948: sub flushcourselogs {
1.352 www 4949: &logthis('Flushing log buffers');
4950: #
4951: # course logs
4952: # This is a log of all transactions in a course, which can be used
4953: # for data mining purposes
4954: #
4955: # It also collects the courseid database, which lists last transaction
4956: # times and course titles for all courseids
4957: #
4958: my %courseidbuffer=();
1.921 raeburn 4959: foreach my $crsid (keys(%courselogs)) {
1.352 www 4960: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 4961: &escape($courselogs{$crsid}),
4962: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 4963: delete $courselogs{$crsid};
4964: } else {
4965: &logthis('Failed to flush log buffer for '.$crsid);
4966: if (length($courselogs{$crsid})>40000) {
1.672 albertel 4967: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 4968: " exceeded maximum size, deleting.</font>");
4969: delete $courselogs{$crsid};
4970: }
1.352 www 4971: }
1.920 raeburn 4972: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 4973: 'description' => $coursedescrbuf{$crsid},
4974: 'inst_code' => $courseinstcodebuf{$crsid},
4975: 'type' => $coursetypebuf{$crsid},
4976: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 4977: };
1.191 harris41 4978: }
1.352 www 4979: #
4980: # Write course id database (reverse lookup) to homeserver of courses
4981: # Is used in pickcourse
4982: #
1.840 albertel 4983: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 4984: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 4985: $courseidbuffer{$crs_home},
4986: $crs_home,'timeonly');
1.352 www 4987: }
4988: #
4989: # File accesses
4990: # Writes to the dynamic metadata of resources to get hit counts, etc.
4991: #
1.449 matthew 4992: foreach my $entry (keys(%accesshash)) {
1.458 matthew 4993: if ($entry =~ /___count$/) {
4994: my ($dom,$name);
1.807 albertel 4995: ($dom,$name,undef)=
1.811 albertel 4996: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 4997: if (! defined($dom) || $dom eq '' ||
4998: ! defined($name) || $name eq '') {
1.620 albertel 4999: my $cid = $env{'request.course.id'};
1.1172.2.142 raeburn 5000: #
5001: # FIXME 11/29/2021
5002: # Typo in rev. 1.458 (2003/12/09)??
5003: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
5004: #
1.1172.2.146. .13(raeb 5005:-23): # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
1.1172.2.142 raeburn 5006: # $dom and $name will always be null, so the &inc() call will default to storing this data
5007: # in a nohist_accesscount.db file for the user rather than the course.
5008: #
5009: # That said there is a lot of noise in the data being stored.
5010: # So counts for prtspool/ and adm/ etc. are recorded.
5011: #
5012: # A review of which items ending '___count' are written to %accesshash should likely be
5013: # made before deciding whether to set these to 'course.' instead of 'request.'
5014: #
5015: # Under the current scheme each user receives a nohist_accesscount.db file listing
5016: # accesses for things which are not published resources, regardless of course, and
5017: # there is not a nohist_accesscount.db file in a course, which might log accesses from
5018: # anyone in the course for things which are not published resources.
5019: #
5020: # For an author, nohist_accesscount.db ends up having records for other items
5021: # mixed up with the legitimate access counts for the author's published resources.
5022: #
1.620 albertel 5023: $dom = $env{'request.'.$cid.'.domain'};
5024: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 5025: }
1.450 matthew 5026: my $value = $accesshash{$entry};
5027: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
5028: my %temphash=($url => $value);
1.449 matthew 5029: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
5030: if ($result eq 'ok') {
5031: delete $accesshash{$entry};
5032: }
5033: } else {
1.811 albertel 5034: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159 www 5035: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450 matthew 5036: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 5037: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
5038: delete $accesshash{$entry};
5039: }
1.185 www 5040: }
1.191 harris41 5041: }
1.352 www 5042: #
5043: # Roles
5044: # Reverse lookup of user roles for course faculty/staff and co-authorship
5045: #
1.800 albertel 5046: foreach my $entry (keys(%userrolehash)) {
1.351 www 5047: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 5048: split(/\:/,$entry);
5049: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 5050: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 5051: $rudom,$runame) eq 'ok') {
5052: delete $userrolehash{$entry};
5053: }
5054: }
1.662 raeburn 5055: #
1.1172.2.90 raeburn 5056: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662 raeburn 5057: #
5058: my %domrolebuffer = ();
1.1000 raeburn 5059: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 5060: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 5061: if ($domrolebuffer{$rudom}) {
5062: $domrolebuffer{$rudom}.='&'.&escape($entry).
5063: '='.&escape($domainrolehash{$entry});
5064: } else {
5065: $domrolebuffer{$rudom}.=&escape($entry).
5066: '='.&escape($domainrolehash{$entry});
5067: }
5068: delete $domainrolehash{$entry};
5069: }
5070: foreach my $dom (keys(%domrolebuffer)) {
1.1172.2.82 raeburn 5071: my %servers;
5072: if (defined(&domain($dom,'primary'))) {
5073: my $primary=&domain($dom,'primary');
5074: my $hostname=&hostname($primary);
5075: $servers{$primary} = $hostname;
5076: } else {
5077: %servers = &get_servers($dom,'library');
5078: }
1.841 albertel 5079: foreach my $tryserver (keys(%servers)) {
1.1172.2.82 raeburn 5080: if (&reply('domroleput:'.$dom.':'.
5081: $domrolebuffer{$dom},$tryserver) eq 'ok') {
5082: last;
5083: } else {
1.841 albertel 5084: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
5085: }
1.662 raeburn 5086: }
5087: }
1.186 www 5088: $dumpcount++;
1.157 www 5089: }
5090:
5091: sub courselog {
5092: my $what=shift;
1.158 www 5093: $what=time.':'.$what;
1.620 albertel 5094: unless ($env{'request.course.id'}) { return ''; }
5095: $coursedombuf{$env{'request.course.id'}}=
5096: $env{'course.'.$env{'request.course.id'}.'.domain'};
5097: $coursenumbuf{$env{'request.course.id'}}=
5098: $env{'course.'.$env{'request.course.id'}.'.num'};
5099: $coursehombuf{$env{'request.course.id'}}=
5100: $env{'course.'.$env{'request.course.id'}.'.home'};
5101: $coursedescrbuf{$env{'request.course.id'}}=
5102: $env{'course.'.$env{'request.course.id'}.'.description'};
5103: $courseinstcodebuf{$env{'request.course.id'}}=
5104: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
5105: $courseownerbuf{$env{'request.course.id'}}=
5106: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 5107: $coursetypebuf{$env{'request.course.id'}}=
5108: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 5109: if (defined $courselogs{$env{'request.course.id'}}) {
5110: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 5111: } else {
1.620 albertel 5112: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 5113: }
1.620 albertel 5114: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 5115: &flushcourselogs();
5116: }
1.158 www 5117: }
5118:
5119: sub courseacclog {
5120: my $fnsymb=shift;
1.620 albertel 5121: unless ($env{'request.course.id'}) { return ''; }
5122: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144 www 5123: if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187 www 5124: $what.=':POST';
1.583 matthew 5125: # FIXME: Probably ought to escape things....
1.800 albertel 5126: foreach my $key (keys(%env)) {
5127: if ($key=~/^form\.(.*)/) {
1.975 raeburn 5128: my $formitem = $1;
5129: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
5130: $what.=':'.$formitem.'='.$env{$key};
5131: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
1.1172.2.131 raeburn 5132: if ($formitem eq 'proctorpassword') {
1.1172.2.132 raeburn 5133: $what.=':'.$formitem.'=' . '*' x length($env{$key});
1.1172.2.131 raeburn 5134: } else {
5135: $what.=':'.$formitem.'='.$env{$key};
5136: }
1.975 raeburn 5137: }
1.158 www 5138: }
1.191 harris41 5139: }
1.583 matthew 5140: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
5141: # FIXME: We should not be depending on a form parameter that someone
5142: # editing lonsearchcat.pm might change in the future.
1.620 albertel 5143: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 5144: $what.= ':POST';
5145: # FIXME: Probably ought to escape things....
5146: foreach my $element ('courseexp','crsfulltext','crsrelated',
5147: 'crsdiscuss') {
1.620 albertel 5148: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 5149: }
5150: }
1.158 www 5151: }
5152: &courselog($what);
1.149 www 5153: }
5154:
1.185 www 5155: sub countacc {
5156: my $url=&declutter(shift);
1.458 matthew 5157: return if (! defined($url) || $url eq '');
1.620 albertel 5158: unless ($env{'request.course.id'}) { return ''; }
1.1158 www 5159: #
5160: # Mark that this url was used in this course
5161: #
1.620 albertel 5162: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158 www 5163: #
5164: # Increase the access count for this resource in this child process
5165: #
1.281 www 5166: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 5167: $accesshash{$key}++;
1.185 www 5168: }
1.349 www 5169:
1.361 www 5170: sub linklog {
5171: my ($from,$to)=@_;
5172: $from=&declutter($from);
5173: $to=&declutter($to);
5174: $accesshash{$from.'___'.$to.'___comefrom'}=1;
5175: $accesshash{$to.'___'.$from.'___goto'}=1;
5176: }
1.1160 www 5177:
5178: sub statslog {
5179: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
5180: if ($users<2) { return; }
5181: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
5182: 'course' => $env{'request.course.id'},
5183: 'sections' => '"all"',
5184: 'num_students' => $users,
5185: 'part' => $part,
5186: 'symb' => $symb,
5187: 'mean_tries' => $av_attempts,
5188: 'deg_of_diff' => $degdiff});
5189: foreach my $key (keys(%dynstore)) {
5190: $accesshash{$key}=$dynstore{$key};
5191: }
5192: }
1.361 www 5193:
1.349 www 5194: sub userrolelog {
5195: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169 droeschl 5196: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350 www 5197: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5198: $userrolehash
5199: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 5200: =$tend.':'.$tstart;
1.662 raeburn 5201: }
1.1169 droeschl 5202: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898 albertel 5203: $userrolehash
5204: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
5205: =$tend.':'.$tstart;
5206: }
1.1172.2.90 raeburn 5207: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662 raeburn 5208: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
5209: $domainrolehash
5210: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
5211: = $tend.':'.$tstart;
5212: }
1.351 www 5213: }
5214:
1.957 raeburn 5215: sub courserolelog {
5216: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1172.2.9 raeburn 5217: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
5218: my $cdom = $1;
5219: my $cnum = $2;
5220: my $sec = $3;
5221: my $namespace = 'rolelog';
5222: my %storehash = (
5223: role => $trole,
5224: start => $tstart,
5225: end => $tend,
5226: selfenroll => $selfenroll,
5227: context => $context,
5228: );
5229: if ($trole eq 'gr') {
5230: $namespace = 'groupslog';
5231: $storehash{'group'} = $sec;
5232: } else {
5233: $storehash{'section'} = $sec;
1.1172.2.146. .16(raeb 5234:-23): my ($curruserdomstr,$newuserdomstr);
5235:-23): if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'})) {
5236:-23): $curruserdomstr = $env{'course.'.$env{'request.course.id'}.'.internal.userdomains'};
5237:-23): } else {
5238:-23): my %courseinfo = &coursedescription($cdom.'/'.$cnum);
5239:-23): $curruserdomstr = $courseinfo{'internal.userdomains'};
5240:-23): }
5241:-23): if ($curruserdomstr ne '') {
5242:-23): my @udoms = split(/,/,$curruserdomstr);
5243:-23): unless (grep(/^\Q$domain\E/,@udoms)) {
5244:-23): push(@udoms,$domain);
5245:-23): $newuserdomstr = join(',',sort(@udoms));
5246:-23): }
5247:-23): } else {
5248:-23): $newuserdomstr = $domain;
5249:-23): }
5250:-23): if ($newuserdomstr ne '') {
5251:-23): my $putresult = &put('environment',{ 'internal.userdomains' => $newuserdomstr },
5252:-23): $cdom,$cnum);
5253:-23): if ($putresult eq 'ok') {
5254:-23): unless (($selfenroll) || ($context eq 'selfenroll')) {
5255:-23): if (($context eq 'createcourse') || ($context eq 'requestcourses') ||
5256:-23): ($context eq 'automated') || ($context eq 'domain')) {
5257:-23): $env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'} = $newuserdomstr;
5258:-23): } elsif ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
5259:-23): &appenv({'course.'.$cdom.'_'.$cnum.'.internal.userdomains' => $newuserdomstr});
5260:-23): }
5261:-23): }
5262:-23): }
5263:-23): }
1.1172.2.9 raeburn 5264: }
5265: &write_log('course',$namespace,\%storehash,$delflag,$username,
5266: $domain,$cnum,$cdom);
5267: if (($trole ne 'st') || ($sec ne '')) {
5268: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957 raeburn 5269: }
5270: }
5271: return;
5272: }
5273:
1.1172.2.9 raeburn 5274: sub domainrolelog {
5275: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5276: if ($area =~ m{^/($match_domain)/$}) {
5277: my $cdom = $1;
5278: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
5279: my $namespace = 'rolelog';
5280: my %storehash = (
5281: role => $trole,
5282: start => $tstart,
5283: end => $tend,
5284: context => $context,
5285: );
5286: &write_log('domain',$namespace,\%storehash,$delflag,$username,
5287: $domain,$domconfiguser,$cdom);
5288: }
5289: return;
5290:
5291: }
5292:
5293: sub coauthorrolelog {
5294: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
5295: if ($area =~ m{^/($match_domain)/($match_username)$}) {
5296: my $audom = $1;
5297: my $auname = $2;
5298: my $namespace = 'rolelog';
5299: my %storehash = (
5300: role => $trole,
5301: start => $tstart,
5302: end => $tend,
5303: context => $context,
5304: );
5305: &write_log('author',$namespace,\%storehash,$delflag,$username,
5306: $domain,$auname,$audom);
5307: }
5308: return;
5309: }
5310:
1.1172.2.146. .23(raeb 5311:-24): sub authorarchivelog {
5312:-24): my ($hashref,$size,$filesdest,$action) = @_;
5313:-24): my $lonprtdir = $Apache::lonnet::perlvar{'lonPrtDir'};
5314:-24): my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
5315:-24): $filesdest =~ s{^\Q$lonprtdir/\E}{};
5316:-24): if ($filesdest =~ m{^($match_username)_($match_domain)_archive_(\d+_\d+_\d+(|[.\w]+))$}) {
5317:-24): my ($auname,$audom,$id) = ($1,$2,$3);
5318:-24): if (ref($hashref) eq 'HASH') {
5319:-24): my $namespace = 'archivelog';
5320:-24): my $dir;
5321:-24): if ($hashref->{dir} =~ m{^\Q$londocroot/priv/$audom/$auname\E(.*)$}) {
5322:-24): $dir = $1;
5323:-24): }
5324:-24): my $delflag = 0;
5325:-24): my %storehash = (
5326:-24): id => $id,
5327:-24): dir => $dir,
5328:-24): files => $hashref->{numfiles},
5329:-24): subdirs => $hashref->{numdirs},
5330:-24): bytes => $hashref->{bytes},
5331:-24): size => $size,
5332:-24): action => $action,
5333:-24): );
5334:-24): if ($action eq 'delete') {
5335:-24): $delflag = 1;
5336:-24): }
5337:-24): &write_log('author',$namespace,\%storehash,$delflag,$auname,
5338:-24): $audom,$auname,$audom);
5339:-24): }
5340:-24): }
5341:-24): return;
5342:-24): }
5343:-24):
1.351 www 5344: sub get_course_adv_roles {
1.948 raeburn 5345: my ($cid,$codes) = @_;
1.620 albertel 5346: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 5347: my %coursehash=&coursedescription($cid);
1.988 raeburn 5348: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 5349: my %nothide=();
1.800 albertel 5350: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 5351: if ($user !~ /:/) {
5352: $nothide{join(':',split(/[\@]/,$user))}=1;
5353: } else {
5354: $nothide{$user}=1;
5355: }
1.470 www 5356: }
1.1172.2.23 raeburn 5357: my @possdoms = ($coursehash{'domain'});
5358: if ($coursehash{'checkforpriv'}) {
5359: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
5360: }
1.351 www 5361: my %returnhash=();
5362: my %dumphash=
5363: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
5364: my $now=time;
1.997 raeburn 5365: my %privileged;
1.1000 raeburn 5366: foreach my $entry (keys(%dumphash)) {
1.800 albertel 5367: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 5368: if (($tstart) && ($tstart<0)) { next; }
5369: if (($tend) && ($tend<$now)) { next; }
5370: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 5371: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 5372: if ($username eq '' || $domain eq '') { next; }
1.1172.2.23 raeburn 5373: if ((&privileged($username,$domain,\@possdoms)) &&
1.997 raeburn 5374: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 5375: if ($role eq 'cr') { next; }
1.948 raeburn 5376: if ($codes) {
5377: if ($section) { $role .= ':'.$section; }
5378: if ($returnhash{$role}) {
5379: $returnhash{$role}.=','.$username.':'.$domain;
5380: } else {
5381: $returnhash{$role}=$username.':'.$domain;
5382: }
1.351 www 5383: } else {
1.988 raeburn 5384: my $key=&plaintext($role,$crstype);
1.973 bisitz 5385: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 5386: if ($returnhash{$key}) {
5387: $returnhash{$key}.=','.$username.':'.$domain;
5388: } else {
5389: $returnhash{$key}=$username.':'.$domain;
5390: }
1.351 www 5391: }
1.948 raeburn 5392: }
1.400 www 5393: return %returnhash;
5394: }
5395:
5396: sub get_my_roles {
1.937 raeburn 5397: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 5398: unless (defined($uname)) { $uname=$env{'user.name'}; }
5399: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 5400: my (%dumphash,%nothide);
1.1086 raeburn 5401: if ($context eq 'userroles') {
1.1166 raeburn 5402: %dumphash = &dump('roles',$udom,$uname);
1.858 raeburn 5403: } else {
1.1172.2.23 raeburn 5404: %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 5405: if ($hidepriv) {
5406: my %coursehash=&coursedescription($udom.'_'.$uname);
5407: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
5408: if ($user !~ /:/) {
5409: $nothide{join(':',split(/[\@]/,$user))} = 1;
5410: } else {
5411: $nothide{$user} = 1;
5412: }
5413: }
5414: }
1.858 raeburn 5415: }
1.400 www 5416: my %returnhash=();
5417: my $now=time;
1.999 raeburn 5418: my %privileged;
1.800 albertel 5419: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 5420: my ($role,$tend,$tstart);
5421: if ($context eq 'userroles') {
1.1149 raeburn 5422: next if ($entry =~ /^rolesdef/);
1.867 raeburn 5423: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
5424: } else {
5425: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
5426: }
1.400 www 5427: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 5428: my $status = 'active';
1.939 raeburn 5429: if (($tend) && ($tend<=$now)) {
1.832 raeburn 5430: $status = 'previous';
5431: }
5432: if (($tstart) && ($now<$tstart)) {
5433: $status = 'future';
5434: }
5435: if (ref($types) eq 'ARRAY') {
5436: if (!grep(/^\Q$status\E$/,@{$types})) {
5437: next;
5438: }
5439: } else {
5440: if ($status ne 'active') {
5441: next;
5442: }
5443: }
1.867 raeburn 5444: my ($rolecode,$username,$domain,$section,$area);
5445: if ($context eq 'userroles') {
1.1172.2.9 raeburn 5446: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867 raeburn 5447: (undef,$domain,$username,$section) = split(/\//,$area);
5448: } else {
5449: ($role,$username,$domain,$section) = split(/\:/,$entry);
5450: }
1.832 raeburn 5451: if (ref($roledoms) eq 'ARRAY') {
5452: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
5453: next;
5454: }
5455: }
5456: if (ref($roles) eq 'ARRAY') {
5457: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 5458: if ($role =~ /^cr\//) {
5459: if (!grep(/^cr$/,@{$roles})) {
5460: next;
5461: }
1.1104 raeburn 5462: } elsif ($role =~ /^gr\//) {
5463: if (!grep(/^gr$/,@{$roles})) {
5464: next;
5465: }
1.922 raeburn 5466: } else {
5467: next;
5468: }
1.832 raeburn 5469: }
1.867 raeburn 5470: }
1.937 raeburn 5471: if ($hidepriv) {
1.1172.2.23 raeburn 5472: my @privroles = ('dc','su');
1.999 raeburn 5473: if ($context eq 'userroles') {
1.1172.2.23 raeburn 5474: next if (grep(/^\Q$role\E$/,@privroles));
1.999 raeburn 5475: } else {
1.1172.2.23 raeburn 5476: my $possdoms = [$domain];
5477: if (ref($roledoms) eq 'ARRAY') {
5478: push(@{$possdoms},@{$roledoms});
1.999 raeburn 5479: }
1.1172.2.23 raeburn 5480: if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999 raeburn 5481: if (!$nothide{$username.':'.$domain}) {
5482: next;
5483: }
5484: }
1.937 raeburn 5485: }
5486: }
1.933 raeburn 5487: if ($withsec) {
5488: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
5489: $tstart.':'.$tend;
5490: } else {
5491: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
5492: }
1.832 raeburn 5493: }
1.373 www 5494: return %returnhash;
1.399 www 5495: }
5496:
1.1172.2.89 raeburn 5497: sub get_all_adhocroles {
5498: my ($dom) = @_;
5499: my @roles_by_num = ();
5500: my %domdefaults = &get_domain_defaults($dom);
5501: my (%description,%access_in_dom,%access_info);
5502: if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
5503: my $count = 0;
5504: my %domcurrent = %{$domdefaults{'adhocroles'}};
5505: my %ordered;
5506: foreach my $role (sort(keys(%domcurrent))) {
5507: my ($order,$desc,$access_in_dom);
5508: if (ref($domcurrent{$role}) eq 'HASH') {
5509: $order = $domcurrent{$role}{'order'};
5510: $desc = $domcurrent{$role}{'desc'};
5511: $access_in_dom{$role} = $domcurrent{$role}{'access'};
5512: $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
5513: }
5514: if ($order eq '') {
5515: $order = $count;
5516: }
5517: $ordered{$order} = $role;
5518: if ($desc ne '') {
5519: $description{$role} = $desc;
5520: } else {
5521: $description{$role}= $role;
5522: }
5523: $count++;
5524: }
5525: foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
5526: push(@roles_by_num,$ordered{$item});
5527: }
5528: }
5529: return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
5530: }
5531:
5532: sub get_my_adhocroles {
5533: my ($cid,$checkreg) = @_;
5534: my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
5535: if ($env{'request.course.id'} eq $cid) {
5536: $cdom = $env{'course.'.$cid.'.domain'};
5537: $cnum = $env{'course.'.$cid.'.num'};
5538: $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
5539: } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
5540: $cdom = $1;
5541: $cnum = $2;
5542: %info = &Apache::lonnet::get('environment',['internal.coursecode'],
5543: $cdom,$cnum);
5544: }
5545: if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
5546: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5547: my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
5548: if ($rosterhash{$user} ne '') {
5549: my $type = (split(/:/,$rosterhash{$user}))[5];
5550: return ([],{}) if ($type eq 'auto');
5551: }
5552: }
5553: if (($cdom ne '') && ($cnum ne '')) {
1.1172.2.90 raeburn 5554: if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1172.2.89 raeburn 5555: my $then=$env{'user.login.time'};
5556: my $update=$env{'user.update.time'};
1.1172.2.90 raeburn 5557: if (!$update) {
5558: $update = $then;
5559: }
5560: my @liveroles;
5561: foreach my $role ('dh','da') {
5562: if ($env{"user.role.$role./$cdom/"}) {
5563: my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
5564: my $limit = $update;
5565: if ($env{'request.role'} eq "$role./$cdom/") {
5566: $limit = $then;
5567: }
5568: my $activerole = 1;
5569: if ($tstart && $tstart>$limit) { $activerole = 0; }
5570: if ($tend && $tend <$limit) { $activerole = 0; }
5571: if ($activerole) {
5572: push(@liveroles,$role);
5573: }
5574: }
5575: }
5576: if (@liveroles) {
1.1172.2.89 raeburn 5577: if (&homeserver($cnum,$cdom) ne 'no_host') {
5578: my ($accessref,$accessinfo,%access_in_dom);
5579: ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
5580: if (ref($roles_by_num) eq 'ARRAY') {
5581: if (@{$roles_by_num}) {
5582: my %settings;
5583: if ($env{'request.course.id'} eq $cid) {
5584: foreach my $envkey (keys(%env)) {
5585: if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
5586: $settings{$1} = $env{$envkey};
5587: }
5588: }
5589: } else {
5590: %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
5591: }
5592: my %setincrs;
5593: if ($settings{'internal.adhocaccess'}) {
5594: map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
5595: }
5596: my @statuses;
5597: if ($env{'environment.inststatus'}) {
5598: @statuses = split(/,/,$env{'environment.inststatus'});
5599: }
5600: my $user = $env{'user.name'}.':'.$env{'user.domain'};
5601: if (ref($accessref) eq 'HASH') {
5602: %access_in_dom = %{$accessref};
5603: }
5604: foreach my $role (@{$roles_by_num}) {
5605: my ($curraccess,@okstatus,@personnel);
5606: if ($setincrs{$role}) {
5607: ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
5608: if ($curraccess eq 'status') {
5609: @okstatus = split(/\&/,$rest);
5610: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5611: @personnel = split(/\&/,$rest);
5612: }
5613: } else {
5614: $curraccess = $access_in_dom{$role};
5615: if (ref($accessinfo) eq 'HASH') {
5616: if ($curraccess eq 'status') {
5617: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5618: @okstatus = @{$accessinfo->{$role}};
5619: }
5620: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5621: if (ref($accessinfo->{$role}) eq 'ARRAY') {
5622: @personnel = @{$accessinfo->{$role}};
5623: }
5624: }
5625: }
5626: }
5627: if ($curraccess eq 'none') {
5628: next;
5629: } elsif ($curraccess eq 'all') {
5630: push(@possroles,$role);
1.1172.2.90 raeburn 5631: } elsif ($curraccess eq 'dh') {
5632: if (grep(/^dh$/,@liveroles)) {
5633: push(@possroles,$role);
5634: } else {
5635: next;
5636: }
5637: } elsif ($curraccess eq 'da') {
5638: if (grep(/^da$/,@liveroles)) {
5639: push(@possroles,$role);
5640: } else {
5641: next;
5642: }
1.1172.2.89 raeburn 5643: } elsif ($curraccess eq 'status') {
5644: if (@okstatus) {
5645: if (!@statuses) {
5646: if (grep(/^default$/,@okstatus)) {
5647: push(@possroles,$role);
5648: }
5649: } else {
5650: foreach my $status (@okstatus) {
5651: if (grep(/^\Q$status\E$/,@statuses)) {
5652: push(@possroles,$role);
5653: last;
5654: }
5655: }
5656: }
5657: }
5658: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
5659: if (grep(/^\Q$user\E$/,@personnel)) {
5660: if ($curraccess eq 'exc') {
5661: push(@possroles,$role);
5662: }
5663: } elsif ($curraccess eq 'inc') {
5664: push(@possroles,$role);
5665: }
5666: }
5667: }
5668: }
5669: }
5670: }
5671: }
5672: }
5673: }
5674: unless (ref($description) eq 'HASH') {
5675: if (ref($roles_by_num) eq 'ARRAY') {
5676: my %desc;
5677: map { $desc{$_} = $_; } (@{$roles_by_num});
5678: $description = \%desc;
5679: } else {
5680: $description = {};
5681: }
5682: }
5683: return (\@possroles,$description);
5684: }
5685:
1.399 www 5686: # ----------------------------------------------------- Frontpage Announcements
5687: #
5688: #
5689:
5690: sub postannounce {
5691: my ($server,$text)=@_;
1.844 albertel 5692: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 5693: unless ($text=~/\w/) { $text=''; }
5694: return &reply('setannounce:'.&escape($text),$server);
5695: }
5696:
5697: sub getannounce {
1.448 albertel 5698:
1.1172.2.96 raeburn 5699: if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 5700: my $announcement='';
1.800 albertel 5701: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 5702: close($fh);
1.399 www 5703: if ($announcement=~/\w/) {
5704: return
5705: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 5706: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 5707: } else {
5708: return '';
5709: }
5710: } else {
5711: return '';
5712: }
1.351 www 5713: }
1.353 www 5714:
5715: # ---------------------------------------------------------- Course ID routines
5716: # Deal with domain's nohist_courseid.db files
5717: #
5718:
5719: sub courseidput {
1.921 raeburn 5720: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 5721: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 5722: my $outcome;
5723: if ($caller eq 'timeonly') {
5724: my $cids = '';
5725: foreach my $item (keys(%$storehash)) {
5726: $cids.=&escape($item).'&';
5727: }
5728: $cids=~s/\&$//;
5729: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
5730: $coursehome);
5731: } else {
5732: my $items = '';
5733: foreach my $item (keys(%$storehash)) {
5734: $items.= &escape($item).'='.
5735: &freeze_escape($$storehash{$item}).'&';
5736: }
5737: $items=~s/\&$//;
5738: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
5739: $coursehome);
1.918 raeburn 5740: }
5741: if ($outcome eq 'unknown_cmd') {
5742: my $what;
5743: foreach my $cid (keys(%$storehash)) {
5744: $what .= &escape($cid).'=';
1.921 raeburn 5745: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 5746: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 5747: }
5748: $what =~ s/\:$/&/;
5749: }
5750: $what =~ s/\&$//;
5751: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
5752: } else {
5753: return $outcome;
5754: }
1.353 www 5755: }
5756:
5757: sub courseiddump {
1.921 raeburn 5758: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 5759: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 5760: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1172.2.38 raeburn 5761: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1172.2.68 raeburn 5762: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918 raeburn 5763: my $as_hash = 1;
5764: my %returnhash;
5765: if (!$domfilter) { $domfilter=''; }
1.845 albertel 5766: my %libserv = &all_library();
5767: foreach my $tryserver (keys(%libserv)) {
5768: if ( ( $hostidflag == 1
5769: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
5770: || (!defined($hostidflag)) ) {
5771:
1.918 raeburn 5772: if (($domfilter eq '') ||
5773: (&host_domain($tryserver) eq $domfilter)) {
1.1172.2.22 raeburn 5774: my $rep;
5775: if (grep { $_ eq $tryserver } ¤t_machine_ids()) {
5776: $rep = &LONCAPA::Lond::dump_course_id_handler(
5777: join(":", (&host_domain($tryserver), $sincefilter,
5778: &escape($descfilter), &escape($instcodefilter),
5779: &escape($ownerfilter), &escape($coursefilter),
5780: &escape($typefilter), &escape($regexp_ok),
5781: $as_hash, &escape($selfenrollonly),
5782: &escape($catfilter), $showhidden, $caller,
5783: &escape($cloner), &escape($cc_clone), $cloneonly,
5784: &escape($createdbefore), &escape($createdafter),
1.1172.2.68 raeburn 5785: &escape($creationcontext),$domcloner,$hasuniquecode,
5786: $reqcrsdom,&escape($reqinstcode))));
1.1172.2.22 raeburn 5787: } else {
5788: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
5789: $sincefilter.':'.&escape($descfilter).':'.
5790: &escape($instcodefilter).':'.&escape($ownerfilter).
5791: ':'.&escape($coursefilter).':'.&escape($typefilter).
5792: ':'.&escape($regexp_ok).':'.$as_hash.':'.
5793: &escape($selfenrollonly).':'.&escape($catfilter).':'.
5794: $showhidden.':'.$caller.':'.&escape($cloner).':'.
5795: &escape($cc_clone).':'.$cloneonly.':'.
5796: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1172.2.68 raeburn 5797: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
5798: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1172.2.22 raeburn 5799: }
5800:
1.918 raeburn 5801: my @pairs=split(/\&/,$rep);
5802: foreach my $item (@pairs) {
5803: my ($key,$value)=split(/\=/,$item,2);
5804: $key = &unescape($key);
5805: next if ($key =~ /^error: 2 /);
5806: my $result = &thaw_unescape($value);
5807: if (ref($result) eq 'HASH') {
5808: $returnhash{$key}=$result;
5809: } else {
1.921 raeburn 5810: my @responses = split(/:/,$value);
5811: my @items = ('description','inst_code','owner','type');
1.918 raeburn 5812: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 5813: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 5814: }
1.1008 raeburn 5815: }
1.353 www 5816: }
5817: }
5818: }
5819: }
5820: return %returnhash;
5821: }
5822:
1.1055 raeburn 5823: sub courselastaccess {
5824: my ($cdom,$cnum,$hostidref) = @_;
5825: my %returnhash;
5826: if ($cdom && $cnum) {
5827: my $chome = &homeserver($cnum,$cdom);
5828: if ($chome ne 'no_host') {
5829: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
5830: &extract_lastaccess(\%returnhash,$rep);
5831: }
5832: } else {
5833: if (!$cdom) { $cdom=''; }
5834: my %libserv = &all_library();
5835: foreach my $tryserver (keys(%libserv)) {
5836: if (ref($hostidref) eq 'ARRAY') {
5837: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
5838: }
5839: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
5840: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
5841: &extract_lastaccess(\%returnhash,$rep);
5842: }
5843: }
5844: }
5845: return %returnhash;
5846: }
5847:
5848: sub extract_lastaccess {
5849: my ($returnhash,$rep) = @_;
5850: if (ref($returnhash) eq 'HASH') {
1.1172.2.146. .26(raeb 5851:-25): unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' ||
1.1055 raeburn 5852: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
5853: $rep eq '') {
5854: my @pairs=split(/\&/,$rep);
5855: foreach my $item (@pairs) {
5856: my ($key,$value)=split(/\=/,$item,2);
5857: $key = &unescape($key);
5858: next if ($key =~ /^error: 2 /);
5859: $returnhash->{$key} = &thaw_unescape($value);
5860: }
5861: }
5862: }
5863: return;
5864: }
5865:
1.658 raeburn 5866: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 5867:
5868: sub dcmailput {
1.685 raeburn 5869: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 5870: my $status = &Apache::lonnet::critical(
1.740 www 5871: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
5872: &escape($message),$server);
1.662 raeburn 5873: return $status;
5874: }
5875:
1.658 raeburn 5876: sub dcmaildump {
5877: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 5878: my %returnhash=();
1.846 albertel 5879:
5880: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 5881: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
5882: &escape($enddate).':';
5883: my @esc_senders=map { &escape($_)} @$senders;
5884: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 5885: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 5886: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 5887: if (($key) && ($value)) {
5888: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 5889: }
5890: }
5891: }
5892: return %returnhash;
5893: }
1.662 raeburn 5894: # ---------------------------------------------------------- Domain roles
5895:
5896: sub get_domain_roles {
5897: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 5898: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 5899: $startdate = '.';
5900: }
1.1018 raeburn 5901: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 5902: $enddate = '.';
5903: }
1.922 raeburn 5904: my $rolelist;
5905: if (ref($roles) eq 'ARRAY') {
1.1172.2.23 raeburn 5906: $rolelist = join('&',@{$roles});
1.922 raeburn 5907: }
1.662 raeburn 5908: my %personnel = ();
1.841 albertel 5909:
5910: my %servers = &get_servers($dom,'library');
5911: foreach my $tryserver (keys(%servers)) {
5912: %{$personnel{$tryserver}}=();
5913: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
5914: &escape($startdate).':'.
5915: &escape($enddate).':'.
5916: &escape($rolelist), $tryserver))) {
5917: my ($key,$value) = split(/\=/,$line,2);
5918: if (($key) && ($value)) {
5919: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
5920: }
5921: }
1.662 raeburn 5922: }
5923: return %personnel;
5924: }
1.658 raeburn 5925:
1.1172.2.89 raeburn 5926: sub get_active_domroles {
5927: my ($dom,$roles) = @_;
5928: return () unless (ref($roles) eq 'ARRAY');
5929: my $now = time;
5930: my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
5931: my %domroles;
5932: foreach my $server (keys(%dompersonnel)) {
5933: foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
5934: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
5935: $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
5936: }
5937: }
5938: return %domroles;
5939: }
5940:
1.1057 www 5941: # ----------------------------------------------------------- Interval timing
1.149 www 5942:
1.1153 www 5943: {
5944: # Caches needed for speedup of navmaps
5945: # We don't want to cache this for very long at all (5 seconds at most)
5946: #
5947: # The user for whom we cache
5948: my $cachedkey='';
5949: # The cached times for this user
5950: my %cachedtimes=();
5951: # When this was last done
1.1172.2.66 raeburn 5952: my $cachedtime='';
1.1153 www 5953:
5954: sub load_all_first_access {
1.1172.2.146. .1(raebu 5955:22): my ($uname,$udom,$ignorecache)=@_;
1.1156 www 5956: if (($cachedkey eq $uname.':'.$udom) &&
1.1172.2.146. .1(raebu 5957:22): (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
5958:22): (!$ignorecache)) {
1.1154 raeburn 5959: return;
5960: }
5961: $cachedtime=time;
5962: $cachedkey=$uname.':'.$udom;
5963: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153 www 5964: }
5965:
1.504 albertel 5966: sub get_first_access {
1.1172.2.146. .1(raebu 5967:22): my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790 albertel 5968: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5969: if ($argsymb) { $symb=$argsymb; }
5970: my ($map,$id,$res)=&decode_symb($symb);
1.1162 raeburn 5971: if ($argmap) { $map = $argmap; }
1.926 albertel 5972: if ($type eq 'course') {
5973: $res='course';
5974: } elsif ($type eq 'map') {
1.588 albertel 5975: $res=&symbread($map);
5976: } else {
5977: $res=$symb;
5978: }
1.1172.2.146. .1(raebu 5979:22): &load_all_first_access($uname,$udom,$ignorecache);
1.1153 www 5980: return $cachedtimes{"$courseid\0$res"};
1.504 albertel 5981: }
5982:
5983: sub set_first_access {
1.1162 raeburn 5984: my ($type,$interval)=@_;
1.790 albertel 5985: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5986: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 5987: if ($type eq 'course') {
5988: $res='course';
5989: } elsif ($type eq 'map') {
1.588 albertel 5990: $res=&symbread($map);
5991: } else {
5992: $res=$symb;
5993: }
1.1153 www 5994: $cachedkey='';
1.1162 raeburn 5995: my $firstaccess=&get_first_access($type,$symb,$map);
1.1172.2.102 raeburn 5996: if ($firstaccess) {
5997: &logthis("First access time already set ($firstaccess) when attempting ".
5998: "to set new value (type: $type, extent: $res) for $uname:$udom ".
5999: "in $courseid");
6000: return 'already_set';
6001: } else {
1.1162 raeburn 6002: my $start = time;
6003: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
6004: $udom,$uname);
6005: if ($putres eq 'ok') {
6006: &put('timerinterval',{"$courseid\0$res"=>$interval},
6007: $udom,$uname);
6008: &appenv(
6009: {
6010: 'course.'.$courseid.'.firstaccess.'.$res => $start,
6011: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
6012: }
6013: );
1.1172.2.97 raeburn 6014: if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
6015: $cachedtimes{"$courseid\0$res"} = $start;
6016: }
1.1172.2.102 raeburn 6017: } elsif ($putres ne 'refused') {
6018: &logthis("Result: $putres when attempting to set first access time ".
6019: "(type: $type, extent: $res) for $uname:$udom in $courseid");
1.1162 raeburn 6020: }
6021: return $putres;
1.505 albertel 6022: }
6023: return 'already_set';
1.504 albertel 6024: }
1.1153 www 6025: }
1.1172.2.32 raeburn 6026:
6027: sub checkout {
6028: my ($symb,$tuname,$tudom,$tcrsid)=@_;
6029: my $now=time;
6030: my $lonhost=$perlvar{'lonHostID'};
1.1172.2.134 raeburn 6031: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6032: my $infostr=&escape(
6033: 'CHECKOUTTOKEN&'.
6034: $tuname.'&'.
6035: $tudom.'&'.
6036: $tcrsid.'&'.
6037: $symb.'&'.
1.1172.2.134 raeburn 6038: $now.'&'.$ip);
1.1172.2.32 raeburn 6039: my $token=&reply('tmpput:'.$infostr,$lonhost);
6040: if ($token=~/^error\:/) {
6041: &logthis("<font color=\"blue\">WARNING: ".
6042: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
6043: "</font>");
6044: return '';
6045: }
6046:
6047: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
6048: $token=~tr/a-z/A-Z/;
6049:
6050: my %infohash=('resource.0.outtoken' => $token,
6051: 'resource.0.checkouttime' => $now,
1.1172.2.134 raeburn 6052: 'resource.0.outremote' => $ip);
1.1172.2.32 raeburn 6053:
6054: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6055: return '';
6056: } else {
6057: &logthis("<font color=\"blue\">WARNING: ".
6058: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
6059: "</font>");
6060: }
6061:
6062: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6063: &escape('Checkout '.$infostr.' - '.
6064: $token)) ne 'ok') {
6065: return '';
6066: } else {
6067: &logthis("<font color=\"blue\">WARNING: ".
6068: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
6069: "</font>");
6070: }
6071: return $token;
6072: }
6073:
6074: # ------------------------------------------------------------ Check in an item
6075:
6076: sub checkin {
6077: my $token=shift;
6078: my $now=time;
6079: my ($ta,$tb,$lonhost)=split(/\*/,$token);
6080: $lonhost=~tr/A-Z/a-z/;
6081: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
6082: $dtoken=~s/\W/\_/g;
1.1172.2.134 raeburn 6083: my $ip = &get_requestor_ip();
1.1172.2.32 raeburn 6084: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
6085: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
6086:
6087: unless (($tuname) && ($tudom)) {
6088: &logthis('Check in '.$token.' ('.$dtoken.') failed');
6089: return '';
6090: }
6091:
6092: unless (&allowed('mgr',$tcrsid)) {
6093: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
6094: $env{'user.name'}.' - '.$env{'user.domain'});
6095: return '';
6096: }
6097:
6098: my %infohash=('resource.0.intoken' => $token,
6099: 'resource.0.checkintime' => $now,
1.1172.2.134 raeburn 6100: 'resource.0.inremote' => $ip);
1.1172.2.32 raeburn 6101:
6102: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
6103: return '';
6104: }
6105:
6106: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
6107: &escape('Checkin - '.$token)) ne 'ok') {
6108: return '';
6109: }
6110:
6111: return ($symb,$tuname,$tudom,$tcrsid);
6112: }
6113:
1.110 www 6114: # --------------------------------------------- Set Expire Date for Spreadsheet
6115:
6116: sub expirespread {
6117: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 6118: my $cid=$env{'request.course.id'};
1.110 www 6119: if ($cid) {
6120: my $now=time;
6121: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 6122: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
6123: $env{'course.'.$cid.'.num'}.
1.110 www 6124: ':nohist_expirationdates:'.
6125: &escape($key).'='.$now,
1.620 albertel 6126: $env{'course.'.$cid.'.home'})
1.110 www 6127: }
6128: return 'ok';
1.14 www 6129: }
6130:
1.109 www 6131: # ----------------------------------------------------- Devalidate Spreadsheets
6132:
6133: sub devalidate {
1.325 www 6134: my ($symb,$uname,$udom)=@_;
1.620 albertel 6135: my $cid=$env{'request.course.id'};
1.109 www 6136: if ($cid) {
1.391 matthew 6137: # delete the stored spreadsheets for
6138: # - the student level sheet of this user in course's homespace
6139: # - the assessment level sheet for this resource
6140: # for this user in user's homespace
1.553 albertel 6141: # - current conditional state info
1.325 www 6142: my $key=$uname.':'.$udom.':';
1.109 www 6143: my $status=
1.299 matthew 6144: &del('nohist_calculatedsheets',
1.391 matthew 6145: [$key.'studentcalc:'],
1.620 albertel 6146: $env{'course.'.$cid.'.domain'},
6147: $env{'course.'.$cid.'.num'})
1.133 albertel 6148: .' '.
6149: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 6150: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 6151: unless ($status eq 'ok ok') {
6152: &logthis('Could not devalidate spreadsheet '.
1.325 www 6153: $uname.' at '.$udom.' for '.
1.109 www 6154: $symb.': '.$status);
1.133 albertel 6155: }
1.553 albertel 6156: &delenv('user.state.'.$cid);
1.109 www 6157: }
6158: }
6159:
1.265 albertel 6160: sub get_scalar {
6161: my ($string,$end) = @_;
6162: my $value;
6163: if ($$string =~ s/^([^&]*?)($end)/$2/) {
6164: $value = $1;
6165: } elsif ($$string =~ s/^([^&]*?)&//) {
6166: $value = $1;
6167: }
6168: return &unescape($value);
6169: }
6170:
6171: sub array2str {
6172: my (@array) = @_;
6173: my $result=&arrayref2str(\@array);
6174: $result=~s/^__ARRAY_REF__//;
6175: $result=~s/__END_ARRAY_REF__$//;
6176: return $result;
6177: }
6178:
1.204 albertel 6179: sub arrayref2str {
6180: my ($arrayref) = @_;
1.265 albertel 6181: my $result='__ARRAY_REF__';
1.204 albertel 6182: foreach my $elem (@$arrayref) {
1.265 albertel 6183: if(ref($elem) eq 'ARRAY') {
6184: $result.=&arrayref2str($elem).'&';
6185: } elsif(ref($elem) eq 'HASH') {
6186: $result.=&hashref2str($elem).'&';
6187: } elsif(ref($elem)) {
6188: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 6189: } else {
6190: $result.=&escape($elem).'&';
6191: }
6192: }
6193: $result=~s/\&$//;
1.265 albertel 6194: $result .= '__END_ARRAY_REF__';
1.204 albertel 6195: return $result;
6196: }
6197:
1.168 albertel 6198: sub hash2str {
1.204 albertel 6199: my (%hash) = @_;
6200: my $result=&hashref2str(\%hash);
1.265 albertel 6201: $result=~s/^__HASH_REF__//;
6202: $result=~s/__END_HASH_REF__$//;
1.204 albertel 6203: return $result;
6204: }
6205:
6206: sub hashref2str {
6207: my ($hashref)=@_;
1.265 albertel 6208: my $result='__HASH_REF__';
1.800 albertel 6209: foreach my $key (sort(keys(%$hashref))) {
6210: if (ref($key) eq 'ARRAY') {
6211: $result.=&arrayref2str($key).'=';
6212: } elsif (ref($key) eq 'HASH') {
6213: $result.=&hashref2str($key).'=';
6214: } elsif (ref($key)) {
1.265 albertel 6215: $result.='=';
1.800 albertel 6216: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 6217: } else {
1.1132 raeburn 6218: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 6219: }
6220:
1.800 albertel 6221: if(ref($hashref->{$key}) eq 'ARRAY') {
6222: $result.=&arrayref2str($hashref->{$key}).'&';
6223: } elsif(ref($hashref->{$key}) eq 'HASH') {
6224: $result.=&hashref2str($hashref->{$key}).'&';
6225: } elsif(ref($hashref->{$key})) {
1.265 albertel 6226: $result.='&';
1.800 albertel 6227: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 6228: } else {
1.800 albertel 6229: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 6230: }
6231: }
1.168 albertel 6232: $result=~s/\&$//;
1.265 albertel 6233: $result .= '__END_HASH_REF__';
1.168 albertel 6234: return $result;
6235: }
6236:
6237: sub str2hash {
1.265 albertel 6238: my ($string)=@_;
6239: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
6240: return %$hash;
6241: }
6242:
6243: sub str2hashref {
1.168 albertel 6244: my ($string) = @_;
1.265 albertel 6245:
6246: my %hash;
6247:
6248: if($string !~ /^__HASH_REF__/) {
6249: if (! ($string eq '' || !defined($string))) {
6250: $hash{'error'}='Not hash reference';
6251: }
6252: return (\%hash, $string);
6253: }
6254:
6255: $string =~ s/^__HASH_REF__//;
6256:
6257: while($string !~ /^__END_HASH_REF__/) {
6258: #key
6259: my $key='';
6260: if($string =~ /^__HASH_REF__/) {
6261: ($key, $string)=&str2hashref($string);
6262: if(defined($key->{'error'})) {
6263: $hash{'error'}='Bad data';
6264: return (\%hash, $string);
6265: }
6266: } elsif($string =~ /^__ARRAY_REF__/) {
6267: ($key, $string)=&str2arrayref($string);
6268: if($key->[0] eq 'Array reference error') {
6269: $hash{'error'}='Bad data';
6270: return (\%hash, $string);
6271: }
6272: } else {
6273: $string =~ s/^(.*?)=//;
1.267 albertel 6274: $key=&unescape($1);
1.265 albertel 6275: }
6276: $string =~ s/^=//;
6277:
6278: #value
6279: my $value='';
6280: if($string =~ /^__HASH_REF__/) {
6281: ($value, $string)=&str2hashref($string);
6282: if(defined($value->{'error'})) {
6283: $hash{'error'}='Bad data';
6284: return (\%hash, $string);
6285: }
6286: } elsif($string =~ /^__ARRAY_REF__/) {
6287: ($value, $string)=&str2arrayref($string);
6288: if($value->[0] eq 'Array reference error') {
6289: $hash{'error'}='Bad data';
6290: return (\%hash, $string);
6291: }
6292: } else {
6293: $value=&get_scalar(\$string,'__END_HASH_REF__');
6294: }
6295: $string =~ s/^&//;
6296:
6297: $hash{$key}=$value;
1.204 albertel 6298: }
1.265 albertel 6299:
6300: $string =~ s/^__END_HASH_REF__//;
6301:
6302: return (\%hash, $string);
1.204 albertel 6303: }
6304:
6305: sub str2array {
1.265 albertel 6306: my ($string)=@_;
6307: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
6308: return @$array;
6309: }
6310:
6311: sub str2arrayref {
1.204 albertel 6312: my ($string) = @_;
1.265 albertel 6313: my @array;
6314:
6315: if($string !~ /^__ARRAY_REF__/) {
6316: if (! ($string eq '' || !defined($string))) {
6317: $array[0]='Array reference error';
6318: }
6319: return (\@array, $string);
6320: }
6321:
6322: $string =~ s/^__ARRAY_REF__//;
6323:
6324: while($string !~ /^__END_ARRAY_REF__/) {
6325: my $value='';
6326: if($string =~ /^__HASH_REF__/) {
6327: ($value, $string)=&str2hashref($string);
6328: if(defined($value->{'error'})) {
6329: $array[0] ='Array reference error';
6330: return (\@array, $string);
6331: }
6332: } elsif($string =~ /^__ARRAY_REF__/) {
6333: ($value, $string)=&str2arrayref($string);
6334: if($value->[0] eq 'Array reference error') {
6335: $array[0] ='Array reference error';
6336: return (\@array, $string);
6337: }
6338: } else {
6339: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
6340: }
6341: $string =~ s/^&//;
6342:
6343: push(@array, $value);
1.191 harris41 6344: }
1.265 albertel 6345:
6346: $string =~ s/^__END_ARRAY_REF__//;
6347:
6348: return (\@array, $string);
1.168 albertel 6349: }
6350:
1.167 albertel 6351: # -------------------------------------------------------------------Temp Store
6352:
1.168 albertel 6353: sub tmpreset {
6354: my ($symb,$namespace,$domain,$stuname) = @_;
6355: if (!$symb) {
6356: $symb=&symbread();
1.620 albertel 6357: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6358: }
6359: $symb=escape($symb);
6360:
1.620 albertel 6361: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 6362: $namespace=~s/\//\_/g;
6363: $namespace=~s/\W//g;
6364:
1.620 albertel 6365: if (!$domain) { $domain=$env{'user.domain'}; }
6366: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6367: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6368: $stuname=&get_requestor_ip();
1.591 albertel 6369: }
1.1117 foxr 6370: my $path=LONCAPA::tempdir();
1.168 albertel 6371: my %hash;
6372: if (tie(%hash,'GDBM_File',
6373: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6374: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 6375: foreach my $key (keys(%hash)) {
1.180 albertel 6376: if ($key=~ /:$symb/) {
1.168 albertel 6377: delete($hash{$key});
6378: }
6379: }
6380: }
6381: }
6382:
1.167 albertel 6383: sub tmpstore {
1.168 albertel 6384: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
6385:
6386: if (!$symb) {
6387: $symb=&symbread();
1.620 albertel 6388: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6389: }
6390: $symb=escape($symb);
6391:
6392: if (!$namespace) {
6393: # I don't think we would ever want to store this for a course.
6394: # it seems this will only be used if we don't have a course.
1.620 albertel 6395: #$namespace=$env{'request.course.id'};
1.168 albertel 6396: #if (!$namespace) {
1.620 albertel 6397: $namespace=$env{'request.state'};
1.168 albertel 6398: #}
6399: }
6400: $namespace=~s/\//\_/g;
6401: $namespace=~s/\W//g;
1.620 albertel 6402: if (!$domain) { $domain=$env{'user.domain'}; }
6403: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6404: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6405: $stuname=&get_requestor_ip();
1.591 albertel 6406: }
1.168 albertel 6407: my $now=time;
6408: my %hash;
1.1117 foxr 6409: my $path=LONCAPA::tempdir();
1.168 albertel 6410: if (tie(%hash,'GDBM_File',
6411: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6412: &GDBM_WRCREAT(),0640)) {
1.168 albertel 6413: $hash{"version:$symb"}++;
6414: my $version=$hash{"version:$symb"};
6415: my $allkeys='';
6416: foreach my $key (keys(%$storehash)) {
6417: $allkeys.=$key.':';
1.591 albertel 6418: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 6419: }
6420: $hash{"$version:$symb:timestamp"}=$now;
6421: $allkeys.='timestamp';
6422: $hash{"$version:keys:$symb"}=$allkeys;
6423: if (untie(%hash)) {
6424: return 'ok';
6425: } else {
6426: return "error:$!";
6427: }
6428: } else {
6429: return "error:$!";
6430: }
6431: }
1.167 albertel 6432:
1.168 albertel 6433: # -----------------------------------------------------------------Temp Restore
1.167 albertel 6434:
1.168 albertel 6435: sub tmprestore {
6436: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 6437:
1.168 albertel 6438: if (!$symb) {
6439: $symb=&symbread();
1.620 albertel 6440: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 6441: }
6442: $symb=escape($symb);
6443:
1.620 albertel 6444: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 6445:
1.620 albertel 6446: if (!$domain) { $domain=$env{'user.domain'}; }
6447: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 6448: if ($domain eq 'public' && $stuname eq 'public') {
1.1172.2.138 raeburn 6449: $stuname=&get_requestor_ip();
1.591 albertel 6450: }
1.168 albertel 6451: my %returnhash;
6452: $namespace=~s/\//\_/g;
6453: $namespace=~s/\W//g;
6454: my %hash;
1.1117 foxr 6455: my $path=LONCAPA::tempdir();
1.168 albertel 6456: if (tie(%hash,'GDBM_File',
6457: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 6458: &GDBM_READER(),0640)) {
1.168 albertel 6459: my $version=$hash{"version:$symb"};
6460: $returnhash{'version'}=$version;
6461: my $scope;
6462: for ($scope=1;$scope<=$version;$scope++) {
6463: my $vkeys=$hash{"$scope:keys:$symb"};
6464: my @keys=split(/:/,$vkeys);
6465: my $key;
6466: $returnhash{"$scope:keys"}=$vkeys;
6467: foreach $key (@keys) {
1.591 albertel 6468: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
6469: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 6470: }
6471: }
1.168 albertel 6472: if (!(untie(%hash))) {
6473: return "error:$!";
6474: }
6475: } else {
6476: return "error:$!";
6477: }
6478: return %returnhash;
1.167 albertel 6479: }
6480:
1.9 www 6481: # ----------------------------------------------------------------------- Store
6482:
6483: sub store {
1.1172.2.64 raeburn 6484: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6485: my $home='';
6486:
1.168 albertel 6487: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6488:
1.213 www 6489: $symb=&symbclean($symb);
1.122 albertel 6490: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 6491:
1.620 albertel 6492: if (!$domain) { $domain=$env{'user.domain'}; }
6493: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 6494:
6495: &devalidate($symb,$stuname,$domain);
1.109 www 6496:
6497: $symb=escape($symb);
1.187 www 6498: if (!$namespace) {
1.620 albertel 6499: unless ($namespace=$env{'request.course.id'}) {
1.187 www 6500: return '';
6501: }
6502: }
1.620 albertel 6503: if (!$home) { $home=$env{'user.home'}; }
1.447 www 6504:
1.1172.2.138 raeburn 6505: $$storehash{'ip'}=&get_requestor_ip();
1.447 www 6506: $$storehash{'host'}=$perlvar{'lonHostID'};
6507:
1.12 www 6508: my $namevalue='';
1.800 albertel 6509: foreach my $key (keys(%$storehash)) {
6510: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 6511: }
1.12 www 6512: $namevalue=~s/\&$//;
1.187 www 6513: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1172.2.64 raeburn 6514: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9 www 6515: }
6516:
1.47 www 6517: # -------------------------------------------------------------- Critical Store
6518:
6519: sub cstore {
1.1172.2.64 raeburn 6520: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 6521: my $home='';
6522:
1.168 albertel 6523: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 6524:
1.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.994 raeburn 7295: }
7296:
1.12 www 7297: # --------------------------------------------------------------- get interface
7298:
7299: sub get {
1.131 albertel 7300: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7301: my $items='';
1.800 albertel 7302: foreach my $item (@$storearr) {
7303: $items.=&escape($item).'&';
1.191 harris41 7304: }
1.12 www 7305: $items=~s/\&$//;
1.620 albertel 7306: if (!$udomain) { $udomain=$env{'user.domain'}; }
7307: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 7308: my $uhome=&homeserver($uname,$udomain);
7309:
1.133 albertel 7310: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7311: my @pairs=split(/\&/,$rep);
1.273 albertel 7312: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
7313: return @pairs;
7314: }
1.15 www 7315: my %returnhash=();
1.42 www 7316: my $i=0;
1.800 albertel 7317: foreach my $item (@$storearr) {
7318: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7319: $i++;
1.191 harris41 7320: }
1.15 www 7321: return %returnhash;
1.27 www 7322: }
7323:
7324: # --------------------------------------------------------------- del interface
7325:
7326: sub del {
1.133 albertel 7327: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 7328: my $items='';
1.800 albertel 7329: foreach my $item (@$storearr) {
7330: $items.=&escape($item).'&';
1.191 harris41 7331: }
1.984 neumanie 7332:
1.27 www 7333: $items=~s/\&$//;
1.620 albertel 7334: if (!$udomain) { $udomain=$env{'user.domain'}; }
7335: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7336: my $uhome=&homeserver($uname,$udomain);
7337: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 7338: }
7339:
7340: # -------------------------------------------------------------- dump interface
7341:
1.1172.2.22 raeburn 7342: sub unserialize {
7343: my ($rep, $escapedkeys) = @_;
7344:
7345: return {} if $rep =~ /^error/;
7346:
7347: my %returnhash=();
7348: foreach my $item (split(/\&/,$rep)) {
7349: my ($key, $value) = split(/=/, $item, 2);
7350: $key = unescape($key) unless $escapedkeys;
7351: next if $key =~ /^error: 2 /;
7352: $returnhash{$key} = &thaw_unescape($value);
7353: }
7354: return \%returnhash;
7355: }
7356:
7357: # see Lond::dump_with_regexp
7358: # if $escapedkeys hash keys won't get unescaped.
1.15 www 7359: sub dump {
1.1172.2.146. .1(raebu 7360:22): my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
1.755 albertel 7361: if (!$udomain) { $udomain=$env{'user.domain'}; }
7362: if (!$uname) { $uname=$env{'user.name'}; }
7363: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 7364:
1.1172.2.55 raeburn 7365: if ($regexp) {
7366: $regexp=&escape($regexp);
7367: } else {
7368: $regexp='.';
7369: }
1.1172.2.22 raeburn 7370: if (grep { $_ eq $uhome } ¤t_machine_ids()) {
7371: # user is hosted on this machine
1.1172.2.55 raeburn 7372: my $reply = LONCAPA::Lond::dump_with_regexp(join(':', ($udomain,
1.1172.2.27 raeburn 7373: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1172.2.22 raeburn 7374: return %{&unserialize($reply, $escapedkeys)};
7375: }
1.1172.2.146. .1(raebu 7376:22): my $rep;
7377:22): if ($encrypt) {
7378:22): $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7379:22): } else {
7380:22): $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
7381:22): }
1.755 albertel 7382: my @pairs=split(/\&/,$rep);
7383: my %returnhash=();
1.1098 foxr 7384: if (!($rep =~ /^error/ )) {
7385: foreach my $item (@pairs) {
7386: my ($key,$value)=split(/=/,$item,2);
1.1172.2.22 raeburn 7387: $key = &unescape($key) unless ($escapedkeys);
1.1098 foxr 7388: next if ($key =~ /^error: 2 /);
7389: $returnhash{$key}=&thaw_unescape($value);
7390: }
1.755 albertel 7391: }
7392: return %returnhash;
1.407 www 7393: }
7394:
1.1098 foxr 7395:
1.717 albertel 7396: # --------------------------------------------------------- dumpstore interface
7397:
7398: sub dumpstore {
7399: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1172.2.22 raeburn 7400: # same as dump but keys must be escaped. They may contain colon separated
7401: # lists of values that may themself contain colons (e.g. symbs).
7402: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 7403: }
7404:
1.407 www 7405: # -------------------------------------------------------------- keys interface
7406:
7407: sub getkeys {
7408: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 7409: if (!$udomain) { $udomain=$env{'user.domain'}; }
7410: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 7411: my $uhome=&homeserver($uname,$udomain);
7412: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
7413: my @keyarray=();
1.800 albertel 7414: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 7415: next if ($key =~ /^error: 2 /);
1.800 albertel 7416: push(@keyarray,&unescape($key));
1.407 www 7417: }
7418: return @keyarray;
1.318 matthew 7419: }
7420:
1.319 matthew 7421: # --------------------------------------------------------------- currentdump
7422: sub currentdump {
1.328 matthew 7423: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 7424: $courseid = $env{'request.course.id'} if (! defined($courseid));
7425: $sdom = $env{'user.domain'} if (! defined($sdom));
7426: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 7427: my $uhome = &homeserver($sname,$sdom);
1.1172.2.50 raeburn 7428: my $rep;
7429:
7430: if (grep { $_ eq $uhome } current_machine_ids()) {
7431: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
7432: $courseid)));
7433: } else {
7434: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
7435: }
7436:
1.318 matthew 7437: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 7438: #
1.318 matthew 7439: my %returnhash=();
1.319 matthew 7440: #
7441: if ($rep eq "unknown_cmd") {
7442: # an old lond will not know currentdump
7443: # Do a dump and make it look like a currentdump
1.822 albertel 7444: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 7445: return if ($tmp[0] =~ /^(error:|no_such_host)/);
7446: my %hash = @tmp;
7447: @tmp=();
1.424 matthew 7448: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 7449: } else {
7450: my @pairs=split(/\&/,$rep);
1.800 albertel 7451: foreach my $pair (@pairs) {
7452: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 7453: my ($symb,$param) = split(/:/,$key);
7454: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 7455: &thaw_unescape($value);
1.319 matthew 7456: }
1.191 harris41 7457: }
1.12 www 7458: return %returnhash;
1.424 matthew 7459: }
7460:
7461: sub convert_dump_to_currentdump{
7462: my %hash = %{shift()};
7463: my %returnhash;
7464: # Code ripped from lond, essentially. The only difference
7465: # here is the unescaping done by lonnet::dump(). Conceivably
7466: # we might run in to problems with parameter names =~ /^v\./
7467: while (my ($key,$value) = each(%hash)) {
7468: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 7469: $symb = &unescape($symb);
7470: $param = &unescape($param);
1.424 matthew 7471: next if ($v eq 'version' || $symb eq 'keys');
7472: next if (exists($returnhash{$symb}) &&
7473: exists($returnhash{$symb}->{$param}) &&
7474: $returnhash{$symb}->{'v.'.$param} > $v);
7475: $returnhash{$symb}->{$param}=$value;
7476: $returnhash{$symb}->{'v.'.$param}=$v;
7477: }
7478: #
7479: # Remove all of the keys in the hashes which keep track of
7480: # the version of the parameter.
7481: while (my ($symb,$param_hash) = each(%returnhash)) {
7482: # use a foreach because we are going to delete from the hash.
7483: foreach my $key (keys(%$param_hash)) {
7484: delete($param_hash->{$key}) if ($key =~ /^v\./);
7485: }
7486: }
7487: return \%returnhash;
1.12 www 7488: }
7489:
1.627 albertel 7490: # ------------------------------------------------------ critical inc interface
7491:
7492: sub cinc {
7493: return &inc(@_,'critical');
7494: }
7495:
1.449 matthew 7496: # --------------------------------------------------------------- inc interface
7497:
7498: sub inc {
1.627 albertel 7499: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 7500: if (!$udomain) { $udomain=$env{'user.domain'}; }
7501: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 7502: my $uhome=&homeserver($uname,$udomain);
7503: my $items='';
7504: if (! ref($store)) {
7505: # got a single value, so use that instead
7506: $items = &escape($store).'=&';
7507: } elsif (ref($store) eq 'SCALAR') {
7508: $items = &escape($$store).'=&';
7509: } elsif (ref($store) eq 'ARRAY') {
7510: $items = join('=&',map {&escape($_);} @{$store});
7511: } elsif (ref($store) eq 'HASH') {
7512: while (my($key,$value) = each(%{$store})) {
7513: $items.= &escape($key).'='.&escape($value).'&';
7514: }
7515: }
7516: $items=~s/\&$//;
1.627 albertel 7517: if ($critical) {
7518: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
7519: } else {
7520: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
7521: }
1.449 matthew 7522: }
7523:
1.12 www 7524: # --------------------------------------------------------------- put interface
7525:
7526: sub put {
1.1172.2.146. .1(raebu 7527:22): my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
1.620 albertel 7528: if (!$udomain) { $udomain=$env{'user.domain'}; }
7529: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7530: my $uhome=&homeserver($uname,$udomain);
1.12 www 7531: my $items='';
1.800 albertel 7532: foreach my $item (keys(%$storehash)) {
7533: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7534: }
1.12 www 7535: $items=~s/\&$//;
1.1172.2.146. .1(raebu 7536:22): if ($encrypt) {
7537:22): return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
7538:22): } else {
7539:22): return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
7540:22): }
1.47 www 7541: }
7542:
1.631 albertel 7543: # ------------------------------------------------------------ newput interface
7544:
7545: sub newput {
7546: my ($namespace,$storehash,$udomain,$uname)=@_;
7547: if (!$udomain) { $udomain=$env{'user.domain'}; }
7548: if (!$uname) { $uname=$env{'user.name'}; }
7549: my $uhome=&homeserver($uname,$udomain);
7550: my $items='';
7551: foreach my $key (keys(%$storehash)) {
7552: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7553: }
7554: $items=~s/\&$//;
7555: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
7556: }
7557:
7558: # --------------------------------------------------------- putstore interface
7559:
1.524 raeburn 7560: sub putstore {
1.1172.2.59 raeburn 7561: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 7562: if (!$udomain) { $udomain=$env{'user.domain'}; }
7563: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 7564: my $uhome=&homeserver($uname,$udomain);
7565: my $items='';
1.715 albertel 7566: foreach my $key (keys(%$storehash)) {
7567: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 7568: }
1.715 albertel 7569: $items=~s/\&$//;
1.716 albertel 7570: my $esc_symb=&escape($symb);
7571: my $esc_v=&escape($version);
1.715 albertel 7572: my $reply =
1.716 albertel 7573: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 7574: $uhome);
1.1172.2.59 raeburn 7575: if (($tolog) && ($reply eq 'ok')) {
7576: my $namevalue='';
7577: foreach my $key (keys(%{$storehash})) {
7578: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
7579: }
1.1172.2.134 raeburn 7580: my $ip = &get_requestor_ip();
1.1172.2.135 raeburn 7581: $namevalue .= 'ip='.&escape($ip).
1.1172.2.59 raeburn 7582: '&host='.&escape($perlvar{'lonHostID'}).
7583: '&version='.$esc_v.
7584: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
7585: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
7586: }
1.715 albertel 7587: if ($reply eq 'unknown_cmd') {
1.716 albertel 7588: # gfall back to way things use to be done
1.715 albertel 7589: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
7590: $uname);
1.524 raeburn 7591: }
1.715 albertel 7592: return $reply;
7593: }
7594:
7595: sub old_putstore {
1.716 albertel 7596: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
7597: if (!$udomain) { $udomain=$env{'user.domain'}; }
7598: if (!$uname) { $uname=$env{'user.name'}; }
7599: my $uhome=&homeserver($uname,$udomain);
7600: my %newstorehash;
1.800 albertel 7601: foreach my $item (keys(%$storehash)) {
7602: my $key = $version.':'.&escape($symb).':'.$item;
7603: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 7604: }
7605: my $items='';
7606: my %allitems = ();
1.800 albertel 7607: foreach my $item (keys(%newstorehash)) {
7608: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 7609: my $key = $1.':keys:'.$2;
7610: $allitems{$key} .= $3.':';
7611: }
1.800 albertel 7612: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 7613: }
1.800 albertel 7614: foreach my $item (keys(%allitems)) {
7615: $allitems{$item} =~ s/\:$//;
7616: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 7617: }
7618: $items=~s/\&$//;
7619: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 7620: }
7621:
1.47 www 7622: # ------------------------------------------------------ critical put interface
7623:
7624: sub cput {
1.134 albertel 7625: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 7626: if (!$udomain) { $udomain=$env{'user.domain'}; }
7627: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 7628: my $uhome=&homeserver($uname,$udomain);
1.47 www 7629: my $items='';
1.800 albertel 7630: foreach my $item (keys(%$storehash)) {
7631: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 7632: }
1.47 www 7633: $items=~s/\&$//;
1.134 albertel 7634: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7635: }
7636:
7637: # -------------------------------------------------------------- eget interface
7638:
7639: sub eget {
1.133 albertel 7640: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 7641: my $items='';
1.800 albertel 7642: foreach my $item (@$storearr) {
7643: $items.=&escape($item).'&';
1.191 harris41 7644: }
1.12 www 7645: $items=~s/\&$//;
1.620 albertel 7646: if (!$udomain) { $udomain=$env{'user.domain'}; }
7647: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 7648: my $uhome=&homeserver($uname,$udomain);
7649: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 7650: my @pairs=split(/\&/,$rep);
7651: my %returnhash=();
1.42 www 7652: my $i=0;
1.800 albertel 7653: foreach my $item (@$storearr) {
7654: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 7655: $i++;
1.191 harris41 7656: }
1.12 www 7657: return %returnhash;
7658: }
7659:
1.667 albertel 7660: # ------------------------------------------------------------ tmpput interface
7661: sub tmpput {
1.802 raeburn 7662: my ($storehash,$server,$context)=@_;
1.667 albertel 7663: my $items='';
1.800 albertel 7664: foreach my $item (keys(%$storehash)) {
7665: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 7666: }
7667: $items=~s/\&$//;
1.802 raeburn 7668: if (defined($context)) {
7669: $items .= ':'.&escape($context);
7670: }
1.667 albertel 7671: return &reply("tmpput:$items",$server);
7672: }
7673:
7674: # ------------------------------------------------------------ tmpget interface
7675: sub tmpget {
1.688 albertel 7676: my ($token,$server)=@_;
7677: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7678: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 7679: my %returnhash;
1.1172.2.85 raeburn 7680: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
7681: return %returnhash;
7682: }
1.667 albertel 7683: foreach my $item (split(/\&/,$rep)) {
7684: my ($key,$value)=split(/=/,$item);
7685: $returnhash{&unescape($key)}=&thaw_unescape($value);
7686: }
7687: return %returnhash;
7688: }
7689:
1.1113 raeburn 7690: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 7691: sub tmpdel {
7692: my ($token,$server)=@_;
7693: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
7694: return &reply("tmpdel:$token",$server);
7695: }
7696:
1.1172.2.13 raeburn 7697: # ------------------------------------------------------------ get_timebased_id
7698:
7699: sub get_timebased_id {
7700: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
7701: $maxtries) = @_;
7702: my ($newid,$error,$dellock);
7703: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
7704: return ('','ok','invalid call to get suffix');
7705: }
7706:
7707: # set defaults for any optional args for which values were not supplied
7708: if ($who eq '') {
7709: $who = $env{'user.name'}.':'.$env{'user.domain'};
7710: }
7711: if (!$locktries) {
7712: $locktries = 3;
7713: }
7714: if (!$maxtries) {
7715: $maxtries = 10;
7716: }
7717:
7718: if (($cdom eq '') || ($cnum eq '')) {
7719: if ($env{'request.course.id'}) {
7720: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7721: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7722: }
7723: if (($cdom eq '') || ($cnum eq '')) {
7724: return ('','ok','call to get suffix not in course context');
7725: }
7726: }
7727:
7728: # construct locking item
7729: my $lockhash = {
7730: $prefix."\0".'locked_'.$keyid => $who,
7731: };
7732: my $tries = 0;
7733:
7734: # attempt to get lock on nohist_$namespace file
7735: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7736: while (($gotlock ne 'ok') && $tries <$locktries) {
7737: $tries ++;
7738: sleep 1;
7739: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
7740: }
7741:
7742: # attempt to get unique identifier, based on current timestamp
7743: if ($gotlock eq 'ok') {
7744: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
7745: my $id = time;
7746: $newid = $id;
1.1172.2.56 raeburn 7747: if ($idtype eq 'addcode') {
7748: $newid .= &sixnum_code();
7749: }
1.1172.2.13 raeburn 7750: my $idtries = 0;
7751: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
7752: if ($idtype eq 'concat') {
7753: $newid = $id.$idtries;
1.1172.2.56 raeburn 7754: } elsif ($idtype eq 'addcode') {
7755: $newid = $newid.&sixnum_code();
1.1172.2.13 raeburn 7756: } else {
7757: $newid ++;
7758: }
7759: $idtries ++;
7760: }
7761: if (!exists($inuse{$prefix."\0".$newid})) {
7762: my %new_item = (
7763: $prefix."\0".$newid => $who,
7764: );
7765: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
7766: $cdom,$cnum);
7767: if ($putresult ne 'ok') {
7768: undef($newid);
7769: $error = 'error saving new item: '.$putresult;
7770: }
7771: } else {
1.1172.2.56 raeburn 7772: undef($newid);
1.1172.2.13 raeburn 7773: $error = ('error: no unique suffix available for the new item ');
7774: }
7775: # remove lock
7776: my @del_lock = ($prefix."\0".'locked_'.$keyid);
7777: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
7778: } else {
7779: $error = "error: could not obtain lockfile\n";
7780: $dellock = 'ok';
1.1172.2.58 raeburn 7781: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
7782: $dellock = 'nolock';
7783: }
1.1172.2.13 raeburn 7784: }
7785: return ($newid,$dellock,$error);
7786: }
7787:
1.1172.2.56 raeburn 7788: sub sixnum_code {
7789: my $code;
7790: for (0..6) {
7791: $code .= int( rand(9) );
7792: }
7793: return $code;
7794: }
7795:
1.765 albertel 7796: # -------------------------------------------------- portfolio access checking
7797:
7798: sub portfolio_access {
1.1172.2.77 raeburn 7799: my ($requrl,$clientip) = @_;
1.765 albertel 7800: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1172.2.77 raeburn 7801: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 7802: if ($result) {
7803: my %setters;
7804: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
1.1172.2.142 raeburn 7805: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
7806: &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
7807: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7808: return 'B';
7809: }
7810: } else {
1.1172.2.142 raeburn 7811: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdo) =
7812: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
7813: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 7814: return 'B';
7815: }
7816: }
7817: }
1.765 albertel 7818: if ($result eq 'ok') {
1.766 albertel 7819: return 'F';
1.765 albertel 7820: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 7821: return 'A';
1.765 albertel 7822: }
1.766 albertel 7823: return '';
1.765 albertel 7824: }
7825:
7826: sub get_portfolio_access {
1.1172.2.146. .23(raeb 7827:-24): my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
1.767 albertel 7828:
7829: if (!ref($access_hash)) {
7830: my $current_perms = &get_portfile_permissions($udom,$unum);
7831: my %access_controls = &get_access_controls($current_perms,$group,
7832: $file_name);
7833: $access_hash = $access_controls{$file_name};
7834: }
7835:
1.1172.2.146. .23(raeb 7836:-24): my $portaccess;
7837:-24): if (ref($portaccess) eq 'SCALAR') {
7838:-24): $portaccess = $$portaccessref;
7839:-24): } else {
7840:-24): $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
7841:-24): }
7842:-24):
7843:-24): my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
1.765 albertel 7844: my $now = time;
7845: if (ref($access_hash) eq 'HASH') {
7846: foreach my $key (keys(%{$access_hash})) {
7847: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1172.2.146. .23(raeb 7848:-24): next if (($scope ne 'ip') && ($portaccess == 0));
1.765 albertel 7849: if ($start > $now) {
7850: next;
7851: }
7852: if ($end && $end<$now) {
7853: next;
7854: }
7855: if ($scope eq 'public') {
7856: $public = $key;
7857: last;
7858: } elsif ($scope eq 'guest') {
7859: $guest = $key;
7860: } elsif ($scope eq 'domains') {
7861: push(@domains,$key);
7862: } elsif ($scope eq 'users') {
7863: push(@users,$key);
7864: } elsif ($scope eq 'course') {
7865: push(@courses,$key);
7866: } elsif ($scope eq 'group') {
7867: push(@groups,$key);
1.1172.2.77 raeburn 7868: } elsif ($scope eq 'ip') {
7869: push(@ips,$key);
1.1172.2.146. .23(raeb 7870:-24): } elsif ($scope eq 'userip') {
7871:-24): push(@userips,$key);
1.765 albertel 7872: }
7873: }
7874: if ($public) {
7875: return 'ok';
1.1172.2.77 raeburn 7876: } elsif (@ips > 0) {
7877: my $allowed;
7878: foreach my $ipkey (@ips) {
7879: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
7880: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
7881: $allowed = 1;
7882: last;
7883: }
7884: }
7885: }
7886: if ($allowed) {
7887: return 'ok';
7888: }
1.1172.2.146. .23(raeb 7889:-24): } elsif (@userips > 0) {
7890:-24): my $allowed;
7891:-24): foreach my $useripkey (@userips) {
7892:-24): if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
7893:-24): if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
7894:-24): $allowed = 1;
7895:-24): last;
7896:-24): }
7897:-24): }
7898:-24): }
7899:-24): if ($allowed) {
7900:-24): return 'ok';
7901:-24): }
1.765 albertel 7902: }
7903: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
7904: if ($guest) {
7905: return $guest;
7906: }
7907: } else {
7908: if (@domains > 0) {
7909: foreach my $domkey (@domains) {
7910: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
7911: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
7912: return 'ok';
7913: }
7914: }
7915: }
7916: }
7917: if (@users > 0) {
7918: foreach my $userkey (@users) {
1.865 raeburn 7919: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
7920: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
7921: if (ref($item) eq 'HASH') {
7922: if (($item->{'uname'} eq $env{'user.name'}) &&
7923: ($item->{'udom'} eq $env{'user.domain'})) {
7924: return 'ok';
7925: }
7926: }
7927: }
7928: }
1.765 albertel 7929: }
7930: }
7931: my %roleshash;
7932: my @courses_and_groups = @courses;
7933: push(@courses_and_groups,@groups);
7934: if (@courses_and_groups > 0) {
7935: my (%allgroups,%allroles);
7936: my ($start,$end,$role,$sec,$group);
7937: foreach my $envkey (%env) {
1.1060 raeburn 7938: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7939: my $cid = $2.'_'.$3;
7940: if ($1 eq 'gr') {
7941: $group = $4;
7942: $allgroups{$cid}{$group} = $env{$envkey};
7943: } else {
7944: if ($4 eq '') {
7945: $sec = 'none';
7946: } else {
7947: $sec = $4;
7948: }
7949: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7950: }
1.811 albertel 7951: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 7952: my $cid = $2.'_'.$3;
7953: if ($4 eq '') {
7954: $sec = 'none';
7955: } else {
7956: $sec = $4;
7957: }
7958: $allroles{$cid}{$1}{$sec} = $env{$envkey};
7959: }
7960: }
7961: if (keys(%allroles) == 0) {
7962: return;
7963: }
7964: foreach my $key (@courses_and_groups) {
7965: my %content = %{$$access_hash{$key}};
7966: my $cnum = $content{'number'};
7967: my $cdom = $content{'domain'};
7968: my $cid = $cdom.'_'.$cnum;
7969: if (!exists($allroles{$cid})) {
7970: next;
7971: }
7972: foreach my $role_id (keys(%{$content{'roles'}})) {
7973: my @sections = @{$content{'roles'}{$role_id}{'section'}};
7974: my @groups = @{$content{'roles'}{$role_id}{'group'}};
7975: my @status = @{$content{'roles'}{$role_id}{'access'}};
7976: my @roles = @{$content{'roles'}{$role_id}{'role'}};
7977: foreach my $role (keys(%{$allroles{$cid}})) {
7978: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
7979: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
7980: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
7981: if (grep/^all$/,@sections) {
7982: return 'ok';
7983: } else {
7984: if (grep/^$sec$/,@sections) {
7985: return 'ok';
7986: }
7987: }
7988: }
7989: }
7990: if (keys(%{$allgroups{$cid}}) == 0) {
7991: if (grep/^none$/,@groups) {
7992: return 'ok';
7993: }
7994: } else {
7995: if (grep/^all$/,@groups) {
7996: return 'ok';
7997: }
7998: foreach my $group (keys(%{$allgroups{$cid}})) {
7999: if (grep/^$group$/,@groups) {
8000: return 'ok';
8001: }
8002: }
8003: }
8004: }
8005: }
8006: }
8007: }
8008: }
8009: if ($guest) {
8010: return $guest;
8011: }
8012: }
8013: }
8014: return;
8015: }
8016:
8017: sub course_group_datechecker {
8018: my ($dates,$now,$status) = @_;
8019: my ($start,$end) = split(/\./,$dates);
8020: if (!$start && !$end) {
8021: return 'ok';
8022: }
8023: if (grep/^active$/,@{$status}) {
8024: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
8025: return 'ok';
8026: }
8027: }
8028: if (grep/^previous$/,@{$status}) {
8029: if ($end > $now ) {
8030: return 'ok';
8031: }
8032: }
8033: if (grep/^future$/,@{$status}) {
8034: if ($start > $now) {
8035: return 'ok';
8036: }
8037: }
8038: return;
8039: }
8040:
8041: sub parse_portfolio_url {
8042: my ($url) = @_;
8043:
8044: my ($type,$udom,$unum,$group,$file_name);
8045:
1.823 albertel 8046: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 8047: $type = 1;
8048: $udom = $1;
8049: $unum = $2;
8050: $file_name = $3;
1.823 albertel 8051: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 8052: $type = 2;
8053: $udom = $1;
8054: $unum = $2;
8055: $group = $3;
8056: $file_name = $3.'/'.$4;
8057: }
8058: if (wantarray) {
8059: return ($type,$udom,$unum,$file_name,$group);
8060: }
8061: return $type;
8062: }
8063:
8064: sub is_portfolio_url {
8065: my ($url) = @_;
8066: return scalar(&parse_portfolio_url($url));
8067: }
8068:
1.798 raeburn 8069: sub is_portfolio_file {
8070: my ($file) = @_;
1.820 raeburn 8071: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 8072: return 1;
8073: }
8074: return;
8075: }
8076:
1.1172.2.146. .13(raeb 8077:-23): sub is_coursetool_logo {
8078:-23): my ($uri) = @_;
8079:-23): if ($env{'request.course.id'}) {
8080:-23): my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
8081:-23): if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
8082:-23): return 1;
8083:-23): }
8084:-23): }
8085:-23): return;
8086:-23): }
8087:-23):
1.976 raeburn 8088: sub usertools_access {
1.1084 raeburn 8089: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 8090: my ($access,%tools);
8091: if ($context eq '') {
8092: $context = 'tools';
8093: }
8094: if ($context eq 'requestcourses') {
8095: %tools = (
8096: official => 1,
8097: unofficial => 1,
1.1006 raeburn 8098: community => 1,
1.1172.2.37 raeburn 8099: textbook => 1,
1.1172.2.146. .13(raeb 8100:-23): lti => 1,
1.985 raeburn 8101: );
1.1172.2.9 raeburn 8102: } elsif ($context eq 'requestauthor') {
8103: %tools = (
8104: requestauthor => 1,
8105: );
1.1172.2.146. .18(raeb 8106:-24): } elsif ($context eq 'authordefaults') {
8107:-24): %tools = (
8108:-24): webdav => 1,
8109:-24): );
1.985 raeburn 8110: } else {
8111: %tools = (
8112: aboutme => 1,
8113: blog => 1,
1.1172.2.146. .24(raeb 8114:-24): webdav => 1,
1.985 raeburn 8115: portfolio => 1,
1.1172.2.146. .22(raeb 8116:-24): portaccess => 1,
.6(raebu 8117:22): timezone => 1,
1.985 raeburn 8118: );
8119: }
1.976 raeburn 8120: return if (!defined($tools{$tool}));
8121:
1.1172.2.35 raeburn 8122: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 8123: $udom = $env{'user.domain'};
8124: $uname = $env{'user.name'};
8125: }
8126:
1.978 raeburn 8127: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
8128: if ($action ne 'reload') {
1.985 raeburn 8129: if ($context eq 'requestcourses') {
8130: return $env{'environment.canrequest.'.$tool};
1.1172.2.9 raeburn 8131: } elsif ($context eq 'requestauthor') {
8132: return $env{'environment.canrequest.author'};
1.1172.2.146. .18(raeb 8133:-24): } elsif ($context eq 'authordefaults') {
8134:-24): if ($tool eq 'webdav') {
8135:-24): return $env{'environment.availabletools.'.$tool};
8136:-24): }
1.985 raeburn 8137: } else {
8138: return $env{'environment.availabletools.'.$tool};
8139: }
8140: }
1.976 raeburn 8141: }
8142:
1.1172.2.9 raeburn 8143: my ($toolstatus,$inststatus,$envkey);
8144: if ($context eq 'requestauthor') {
8145: $envkey = $context;
1.1172.2.146. .18(raeb 8146:-24): } elsif ($context eq 'authordefaults') {
8147:-24): if ($tool eq 'webdav') {
8148:-24): $envkey = 'tools.'.$tool;
8149:-24): }
1.1172.2.9 raeburn 8150: } else {
8151: $envkey = $context.'.'.$tool;
8152: }
1.976 raeburn 8153:
1.985 raeburn 8154: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8155: ($action ne 'reload')) {
1.1172.2.9 raeburn 8156: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 8157: $inststatus = $env{'environment.inststatus'};
8158: } else {
1.1084 raeburn 8159: if (ref($userenvref) eq 'HASH') {
1.1172.2.9 raeburn 8160: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 8161: $inststatus = $userenvref->{'inststatus'};
8162: } else {
1.1172.2.9 raeburn 8163: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
8164: $toolstatus = $userenv{$envkey};
1.1084 raeburn 8165: $inststatus = $userenv{'inststatus'};
8166: }
1.976 raeburn 8167: }
8168:
8169: if ($toolstatus ne '') {
8170: if ($toolstatus) {
8171: $access = 1;
8172: } else {
8173: $access = 0;
8174: }
8175: return $access;
8176: }
8177:
1.1084 raeburn 8178: my ($is_adv,%domdef);
8179: if (ref($is_advref) eq 'HASH') {
8180: $is_adv = $is_advref->{'is_adv'};
8181: } else {
8182: $is_adv = &is_advanced_user($udom,$uname);
8183: }
8184: if (ref($domdefref) eq 'HASH') {
8185: %domdef = %{$domdefref};
8186: } else {
8187: %domdef = &get_domain_defaults($udom);
8188: }
1.976 raeburn 8189: if (ref($domdef{$tool}) eq 'HASH') {
8190: if ($is_adv) {
8191: if ($domdef{$tool}{'_LC_adv'} ne '') {
8192: if ($domdef{$tool}{'_LC_adv'}) {
8193: $access = 1;
8194: } else {
8195: $access = 0;
8196: }
8197: return $access;
8198: }
8199: }
8200: if ($inststatus ne '') {
8201: my ($hasaccess,$hasnoaccess);
8202: foreach my $affiliation (split(/:/,$inststatus)) {
8203: if ($domdef{$tool}{$affiliation} ne '') {
8204: if ($domdef{$tool}{$affiliation}) {
8205: $hasaccess = 1;
8206: } else {
8207: $hasnoaccess = 1;
8208: }
8209: }
8210: }
8211: if ($hasaccess || $hasnoaccess) {
8212: if ($hasaccess) {
8213: $access = 1;
8214: } elsif ($hasnoaccess) {
8215: $access = 0;
8216: }
8217: return $access;
8218: }
8219: } else {
8220: if ($domdef{$tool}{'default'} ne '') {
8221: if ($domdef{$tool}{'default'}) {
8222: $access = 1;
8223: } elsif ($domdef{$tool}{'default'} == 0) {
8224: $access = 0;
8225: }
8226: return $access;
8227: }
8228: }
8229: } else {
1.1172.2.6 raeburn 8230: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 8231: $access = 1;
8232: } else {
8233: $access = 0;
8234: }
1.976 raeburn 8235: return $access;
8236: }
8237: }
8238:
1.1050 raeburn 8239: sub is_course_owner {
8240: my ($cdom,$cnum,$udom,$uname) = @_;
8241: if (($udom eq '') || ($uname eq '')) {
8242: $udom = $env{'user.domain'};
8243: $uname = $env{'user.name'};
8244: }
8245: unless (($udom eq '') || ($uname eq '')) {
8246: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
8247: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
8248: return 1;
8249: } else {
8250: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
8251: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
8252: return 1;
8253: }
8254: }
8255: }
8256: }
8257: return;
8258: }
8259:
1.976 raeburn 8260: sub is_advanced_user {
8261: my ($udom,$uname) = @_;
1.1085 raeburn 8262: if ($udom ne '' && $uname ne '') {
8263: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 8264: if (wantarray) {
8265: return ($env{'user.adv'},$env{'user.author'});
8266: } else {
8267: return $env{'user.adv'};
8268: }
1.1085 raeburn 8269: }
8270: }
1.976 raeburn 8271: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
8272: my %allroles;
1.1128 raeburn 8273: my ($is_adv,$is_author);
1.976 raeburn 8274: foreach my $role (keys(%roleshash)) {
8275: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
8276: my $area = '/'.$tdomain.'/'.$trest;
8277: if ($sec ne '') {
8278: $area .= '/'.$sec;
8279: }
8280: if (($area ne '') && ($trole ne '')) {
8281: my $spec=$trole.'.'.$area;
8282: if ($trole =~ /^cr\//) {
8283: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
8284: } elsif ($trole ne 'gr') {
8285: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
8286: }
1.1128 raeburn 8287: if ($trole eq 'au') {
8288: $is_author = 1;
8289: }
1.976 raeburn 8290: }
8291: }
8292: foreach my $role (keys(%allroles)) {
8293: last if ($is_adv);
8294: foreach my $item (split(/:/,$allroles{$role})) {
8295: if ($item ne '') {
8296: my ($privilege,$restrictions)=split(/&/,$item);
8297: if ($privilege eq 'adv') {
8298: $is_adv = 1;
8299: last;
8300: }
8301: }
8302: }
8303: }
1.1128 raeburn 8304: if (wantarray) {
8305: return ($is_adv,$is_author);
8306: }
1.976 raeburn 8307: return $is_adv;
8308: }
1.798 raeburn 8309:
1.1035 raeburn 8310: sub check_can_request {
1.1172.2.146. .13(raeb 8311:-23): my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
1.1035 raeburn 8312: my $canreq = 0;
1.1172.2.146. .13(raeb 8313:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8314:-23): $uname = $env{'user.name'};
8315:-23): $udom = $env{'user.domain'};
8316:-23): }
1.1035 raeburn 8317: my ($types,$typename) = &Apache::loncommon::course_types();
8318: my @options = ('approval','validate','autolimit');
8319: my $optregex = join('|',@options);
8320: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
8321: foreach my $type (@{$types}) {
1.1172.2.146. .13(raeb 8322:-23): if (&usertools_access($uname,$udom,$type,undef,
8323:-23): 'requestcourses')) {
1.1035 raeburn 8324: $canreq ++;
1.1036 raeburn 8325: if (ref($request_domains) eq 'HASH') {
1.1172.2.146. .13(raeb 8326:-23): push(@{$request_domains->{$type}},$udom);
1.1036 raeburn 8327: }
1.1172.2.146. .13(raeb 8328:-23): if ($dom eq $udom) {
1.1035 raeburn 8329: $can_request->{$type} = 1;
8330: }
8331: }
1.1172.2.146. .13(raeb 8332:-23): if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
8333:-23): ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
1.1035 raeburn 8334: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
8335: if (@curr > 0) {
1.1036 raeburn 8336: foreach my $item (@curr) {
8337: if (ref($request_domains) eq 'HASH') {
8338: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
8339: if ($otherdom ne '') {
8340: if (ref($request_domains->{$type}) eq 'ARRAY') {
8341: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
8342: push(@{$request_domains->{$type}},$otherdom);
8343: }
8344: } else {
8345: push(@{$request_domains->{$type}},$otherdom);
8346: }
8347: }
8348: }
8349: }
1.1172.2.146. .13(raeb 8350:-23): unless ($dom eq $env{'user.domain'}) {
1.1036 raeburn 8351: $canreq ++;
1.1035 raeburn 8352: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
8353: $can_request->{$type} = 1;
8354: }
8355: }
8356: }
8357: }
8358: }
8359: }
8360: return $canreq;
8361: }
8362:
1.341 www 8363: # ---------------------------------------------- Custom access rule evaluation
8364:
8365: sub customaccess {
8366: my ($priv,$uri)=@_;
1.807 albertel 8367: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 8368: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 8369: $udom = &LONCAPA::clean_domain($udom);
8370: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 8371: my $access=0;
1.800 albertel 8372: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 8373: my ($effect,$realm,$role,$type)=split(/\:/,$right);
8374: if ($type eq 'user') {
8375: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 8376: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 8377: if ($tdom) {
8378: if ($tdom ne $env{'user.domain'}) { next; }
8379: }
1.896 albertel 8380: if ($tuname) {
8381: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 8382: }
8383: $access=($effect eq 'allow');
8384: last;
8385: }
8386: } else {
8387: if ($role) {
8388: if ($role ne $urole) { next; }
8389: }
8390: foreach my $scope (split(/\s*\,\s*/,$realm)) {
8391: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
8392: if ($tdom) {
8393: if ($tdom ne $udom) { next; }
8394: }
8395: if ($tcrs) {
8396: if ($tcrs ne $ucrs) { next; }
8397: }
8398: if ($tsec) {
8399: if ($tsec ne $usec) { next; }
8400: }
8401: $access=($effect eq 'allow');
8402: last;
8403: }
8404: if ($realm eq '' && $role eq '') {
8405: $access=($effect eq 'allow');
8406: }
1.402 bowersj2 8407: }
1.341 www 8408: }
8409: return $access;
8410: }
8411:
1.103 harris41 8412: # ------------------------------------------------- Check for a user privilege
1.12 www 8413:
8414: sub allowed {
1.1172.2.146. .1(raebu 8415:22): my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
1.705 albertel 8416: my $ver_orguri=$uri;
1.439 www 8417: $uri=&deversion($uri);
1.152 www 8418: my $orguri=$uri;
1.52 www 8419: $uri=&declutter($uri);
1.809 raeburn 8420:
1.810 raeburn 8421: if ($priv eq 'evb') {
1.1172.2.146. .3(raebu 8422:22): # Evade communication block restrictions for specified role in a course or domain
1.810 raeburn 8423: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
8424: return $1;
8425: } else {
8426: return;
8427: }
8428: }
8429:
1.620 albertel 8430: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 8431: # Free bre access to adm and meta resources
1.1172.2.146. .1(raebu 8432:22): if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$}))
1.769 albertel 8433: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
8434: && ($priv eq 'bre')) {
1.14 www 8435: return 'F';
1.159 www 8436: }
8437:
1.545 banghart 8438: # Free bre access to user's own portfolio contents
1.714 raeburn 8439: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 8440: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 8441: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 8442: my %setters;
1.1172.2.142 raeburn 8443: my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
8444: &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
8445: if (($startblock && $endblock) || ($by_ip)) {
1.814 raeburn 8446: return 'B';
8447: } else {
8448: return 'F';
8449: }
1.545 banghart 8450: }
8451:
1.762 raeburn 8452: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 8453: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
8454: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
8455: if (exists($env{'request.course.id'})) {
8456: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8457: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
8458: if (($domain eq $cdom) && ($name eq $cnum)) {
8459: my $courseprivid=$env{'request.course.id'};
8460: $courseprivid=~s/\_/\//;
8461: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
8462: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
8463: return $1;
1.762 raeburn 8464: } else {
8465: if ($env{'request.course.sec'}) {
8466: $courseprivid.='/'.$env{'request.course.sec'};
8467: }
8468: if ($env{'user.priv.'.$env{'request.role'}.'./'.
8469: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
8470: return $2;
8471: }
1.714 raeburn 8472: }
8473: }
8474: }
8475: }
8476:
1.159 www 8477: # Free bre to public access
8478:
8479: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8480:22): my $copyright;
8481:22): unless ($uri =~ /ext\.tool/) {
8482:22): $copyright=&metadata($uri,'copyright');
8483:22): }
1.620 albertel 8484: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 8485: return 'F';
8486: }
1.238 www 8487: if ($copyright eq 'priv') {
8488: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8489: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 8490: return '';
8491: }
8492: }
8493: if ($copyright eq 'domain') {
8494: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 8495: unless (($env{'user.domain'} eq $1) ||
8496: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 8497: return '';
8498: }
1.262 matthew 8499: }
1.620 albertel 8500: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 8501: # Library role, so allow browsing of resources in this domain.
8502: return 'F';
1.238 www 8503: }
1.341 www 8504: if ($copyright eq 'custom') {
8505: unless (&customaccess($priv,$uri)) { return ''; }
8506: }
1.14 www 8507: }
1.264 matthew 8508: # Domain coordinator is trying to create a course
1.620 albertel 8509: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 8510: # uri is the requested domain in this case.
8511: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 8512: # a role of dc for the domain in question.
1.620 albertel 8513: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 8514: }
1.29 www 8515:
1.52 www 8516: my $thisallowed='';
8517: my $statecond=0;
8518: my $courseprivid='';
8519:
1.1039 raeburn 8520: my $ownaccess;
1.1043 raeburn 8521: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 8522: if (($priv eq 'bro') && ($env{'user.author'})) {
8523: if ($uri eq '') {
8524: $ownaccess = 1;
8525: } else {
8526: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
8527: my $udom = $env{'user.domain'};
8528: my $uname = $env{'user.name'};
8529: if ($uri =~ m{^\Q$udom\E/?$}) {
8530: $ownaccess = 1;
1.1040 raeburn 8531: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 8532: unless ($uri =~ m{\.\./}) {
8533: $ownaccess = 1;
8534: }
8535: } elsif (($udom ne 'public') && ($uname ne 'public')) {
8536: my $now = time;
8537: if ($uri =~ m{^([^/]+)/?$}) {
8538: my $adom = $1;
8539: foreach my $key (keys(%env)) {
1.1042 raeburn 8540: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1172.2.142 raeburn 8541: my ($start,$end) = split(/\./,$env{$key});
8542: if (($now >= $start) && (!$end || $end > $now)) {
1.1039 raeburn 8543: $ownaccess = 1;
8544: last;
8545: }
8546: }
8547: }
8548: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
8549: my $adom = $1;
8550: my $aname = $2;
1.1042 raeburn 8551: foreach my $role ('ca','aa') {
8552: if ($env{"user.role.$role./$adom/$aname"}) {
8553: my ($start,$end) =
1.1172.2.142 raeburn 8554: split(/\./,$env{"user.role.$role./$adom/$aname"});
8555: if (($now >= $start) && (!$end || $end > $now)) {
1.1042 raeburn 8556: $ownaccess = 1;
8557: last;
8558: }
1.1039 raeburn 8559: }
8560: }
8561: }
8562: }
8563: }
8564: }
8565: }
8566:
1.52 www 8567: # Course
8568:
1.620 albertel 8569: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8570: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8571: $thisallowed.=$1;
8572: }
1.52 www 8573: }
1.29 www 8574:
1.52 www 8575: # Domain
8576:
1.620 albertel 8577: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 8578: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 8579: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 8580: $thisallowed.=$1;
8581: }
1.12 www 8582: }
1.52 www 8583:
1.1141 raeburn 8584: # User who is not author or co-author might still be able to edit
8585: # resource of an author in the domain (e.g., if Domain Coordinator).
8586: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
8587: (&allowed('mdc',$env{'request.course.id'}))) {
8588: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
8589: $thisallowed.=$1;
8590: }
8591: }
8592:
1.52 www 8593: # Course: uri itself is a course
1.66 www 8594: my $courseuri=$uri;
8595: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 8596: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 8597:
1.620 albertel 8598: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 8599: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.115 raeburn 8600: if ($priv eq 'mip') {
8601: my $rem = $1;
8602: if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
8603: ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
8604: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
8605: if ($cdom ne '') {
8606: my %passwdconf = &get_passwdconf($cdom);
8607: if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
8608: if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
8609: if (@{$passwdconf{'crsownerchg'}{'by'}}) {
8610: my @inststatuses = split(':',$env{'environment.inststatus'});
8611: unless (@inststatuses) {
8612: @inststatuses = ('default');
8613: }
8614: foreach my $status (@inststatuses) {
8615: if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
8616: $thisallowed.=$rem;
8617: }
8618: }
8619: }
8620: }
8621: }
8622: }
8623: }
8624: } else {
8625: unless (($priv eq 'bro') && (!$ownaccess)) {
8626: $thisallowed.=$1;
8627: }
1.1039 raeburn 8628: }
1.12 www 8629: }
1.29 www 8630:
1.665 albertel 8631: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 8632: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 8633: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 8634: $thisallowed='';
1.671 raeburn 8635: my ($match)=&is_on_map($uri);
8636: if ($match) {
8637: if ($env{'user.priv.'.$env{'request.role'}.'./'}
8638: =~/\Q$priv\E\&([^\:]*)/) {
1.1172.2.65 raeburn 8639: my $value = $1;
1.1172.2.146. .1(raebu 8640:22): my $deeplinkblock;
8641:22): unless ($nodeeplinkcheck) {
8642:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8643:22): }
8644:22): if ($deeplinkblock) {
8645:22): $thisallowed='D';
8646:22): } elsif ($noblockcheck) {
1.1172.2.65 raeburn 8647: $thisallowed.=$value;
1.1162 raeburn 8648: } else {
1.1172.2.126 raeburn 8649: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8650: if (@blockers > 0) {
8651: $thisallowed = 'B';
8652: } else {
8653: $thisallowed.=$value;
8654: }
1.1162 raeburn 8655: }
1.671 raeburn 8656: }
8657: } else {
1.705 albertel 8658: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 8659: if ($refuri) {
8660: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 8661: $thisallowed='F';
1.671 raeburn 8662: } else {
8663: $refuri=&declutter($refuri);
8664: my ($match) = &is_on_map($refuri);
8665: if ($match) {
1.1172.2.146. .1(raebu 8666:22): my $deeplinkblock;
8667:22): unless ($nodeeplinkcheck) {
8668:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8669:22): }
8670:22): if ($deeplinkblock) {
8671:22): $thisallowed='D';
8672:22): } elsif ($noblockcheck) {
1.1162 raeburn 8673: $thisallowed='F';
1.1172.2.65 raeburn 8674: } else {
1.1172.2.127 raeburn 8675: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8676: if (@blockers > 0) {
8677: $thisallowed = 'B';
8678: } else {
8679: $thisallowed='F';
8680: }
1.1162 raeburn 8681: }
1.671 raeburn 8682: }
1.669 raeburn 8683: }
1.671 raeburn 8684: }
8685: }
1.314 www 8686: }
1.492 albertel 8687:
1.766 albertel 8688: if ($priv eq 'bre'
8689: && $thisallowed ne 'F'
8690: && $thisallowed ne '2'
8691: && &is_portfolio_url($uri)) {
1.1172.2.77 raeburn 8692: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 8693: }
8694:
1.52 www 8695: # Full access at system, domain or course-wide level? Exit.
1.29 www 8696: if ($thisallowed=~/F/) {
8697: return 'F';
8698: }
8699:
1.52 www 8700: # If this is generating or modifying users, exit with special codes
1.29 www 8701:
1.1172.2.146. .22(raeb 8702:-24): if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa::vca:vaa:'=~/\:\Q$priv\E\:/) {
1.643 www 8703: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 8704: my ($audom,$auname)=split('/',$uri);
1.643 www 8705: # no author name given, so this just checks on the general right to make a co-author in this domain
8706: unless ($auname) { return $thisallowed; }
8707: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 8708: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
8709: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
8710: ($audom ne $env{'request.role.domain'}))) { return ''; }
1.1172.2.146. .22(raeb 8711:-24): } elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
8712:-24): my ($audom,$auname)=split('/',$uri);
8713:-24): unless ($auname) { return $thisallowed; }
8714:-24): unless (($env{'request.role'} eq "dc./$audom") ||
8715:-24): ($env{'request.role'} eq "ca./$uri")) {
8716:-24): return '';
8717:-24): }
1.642 albertel 8718: }
1.52 www 8719: return $thisallowed;
8720: }
8721: #
1.103 harris41 8722: # Gathered so far: system, domain and course wide privileges
1.52 www 8723: #
8724: # Course: See if uri or referer is an individual resource that is part of
8725: # the course
8726:
1.620 albertel 8727: if ($env{'request.course.id'}) {
1.232 www 8728:
1.1172.2.146. .13(raeb 8729:-23): if ($priv eq 'bre') {
8730:-23): if (&is_coursetool_logo($uri)) {
8731:-23): return 'F';
8732:-23): }
8733:-23): }
8734:-23):
1.1172.2.115 raeburn 8735: # If this is modifying password (internal auth) domains must match for user and user's role.
8736:
8737: if ($priv eq 'mip') {
8738: if ($env{'user.domain'} eq $env{'request.role.domain'}) {
8739: return $thisallowed;
8740: } else {
8741: return '';
8742: }
8743: }
8744:
1.620 albertel 8745: $courseprivid=$env{'request.course.id'};
8746: if ($env{'request.course.sec'}) {
8747: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 8748: }
8749: $courseprivid=~s/\_/\//;
8750: my $checkreferer=1;
1.232 www 8751: my ($match,$cond)=&is_on_map($uri);
8752: if ($match) {
8753: $statecond=$cond;
1.620 albertel 8754: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8755: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8756: my $value = $1;
8757: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8758:22): my $deeplinkblock;
8759:22): unless ($nodeeplinkcheck) {
8760:22): $deeplinkblock = &deeplink_check($priv,$symb,$uri);
8761:22): }
8762:22): if ($deeplinkblock) {
8763:22): $thisallowed = 'D';
8764:22): } elsif ($noblockcheck) {
1.1162 raeburn 8765: $thisallowed.=$value;
1.1172.2.65 raeburn 8766: } else {
1.1172.2.126 raeburn 8767: my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
1.1172.2.65 raeburn 8768: if (@blockers > 0) {
8769: $thisallowed = 'B';
8770: } else {
8771: $thisallowed.=$value;
8772: }
1.1162 raeburn 8773: }
8774: } else {
8775: $thisallowed.=$value;
8776: }
1.52 www 8777: $checkreferer=0;
8778: }
1.29 www 8779: }
1.1172.2.126 raeburn 8780:
1.148 www 8781: if ($checkreferer) {
1.620 albertel 8782: my $refuri=$env{'httpref.'.$orguri};
1.148 www 8783: unless ($refuri) {
1.800 albertel 8784: foreach my $key (keys(%env)) {
8785: if ($key=~/^httpref\..*\*/) {
8786: my $pattern=$key;
1.156 www 8787: $pattern=~s/^httpref\.\/res\///;
1.148 www 8788: $pattern=~s/\*/\[\^\/\]\+/g;
8789: $pattern=~s/\//\\\//g;
1.152 www 8790: if ($orguri=~/$pattern/) {
1.800 albertel 8791: $refuri=$env{$key};
1.148 www 8792: }
8793: }
1.191 harris41 8794: }
1.148 www 8795: }
1.232 www 8796:
1.148 www 8797: if ($refuri) {
1.152 www 8798: $refuri=&declutter($refuri);
1.232 www 8799: my ($match,$cond)=&is_on_map($refuri);
8800: if ($match) {
8801: my $refstatecond=$cond;
1.620 albertel 8802: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 8803: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 8804: my $value = $1;
8805: if ($priv eq 'bre') {
1.1172.2.146. .1(raebu 8806:22): my $deeplinkblock;
8807:22): unless ($nodeeplinkcheck) {
8808:22): $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
8809:22): }
8810:22): if ($deeplinkblock) {
8811:22): $thisallowed = 'D';
8812:22): } elsif ($noblockcheck) {
1.1162 raeburn 8813: $thisallowed.=$value;
1.1172.2.65 raeburn 8814: } else {
1.1172.2.127 raeburn 8815: my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
1.1172.2.65 raeburn 8816: if (@blockers > 0) {
8817: $thisallowed = 'B';
8818: } else {
8819: $thisallowed.=$value;
8820: }
1.1162 raeburn 8821: }
8822: } else {
8823: $thisallowed.=$value;
8824: }
1.53 www 8825: $uri=$refuri;
8826: $statecond=$refstatecond;
1.52 www 8827: }
8828: }
1.148 www 8829: }
1.29 www 8830: }
1.52 www 8831: }
1.29 www 8832:
1.52 www 8833: #
1.103 harris41 8834: # Gathered now: all privileges that could apply, and condition number
1.52 www 8835: #
8836: #
8837: # Full or no access?
8838: #
1.29 www 8839:
1.52 www 8840: if ($thisallowed=~/F/) {
8841: return 'F';
8842: }
1.29 www 8843:
1.52 www 8844: unless ($thisallowed) {
8845: return '';
8846: }
1.29 www 8847:
1.52 www 8848: # Restrictions exist, deal with them
8849: #
8850: # C:according to course preferences
8851: # R:according to resource settings
8852: # L:unless locked
8853: # X:according to user session state
8854: #
8855:
8856: # Possibly locked functionality, check all courses
1.1172.2.142 raeburn 8857: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
1.54 www 8858: # Locks might take effect only after 10 minutes cache expiration for other
1.1172.2.142 raeburn 8859: # courses, and 2 minutes for current course, in which user has st or ta role
8860: # which is neither expired nor a future role (unless current course).
1.52 www 8861:
1.1172.2.142 raeburn 8862: my ($needlockcheck,$now,$crsonly);
1.52 www 8863: if ($thisallowed=~/L/) {
1.1172.2.142 raeburn 8864: $now = time;
8865: if ($priv eq 'bre') {
8866: if ($uri ne '') {
8867: if ($orguri =~ m{^/+res/}) {
8868: if ($uri =~ m{^lib/templates/}) {
8869: if ($env{'request.course.id'}) {
8870: $crsonly = 1;
8871: $needlockcheck = 1;
8872: }
8873: } else {
8874: $needlockcheck = 1;
8875: }
8876: } elsif ($env{'request.course.id'}) {
8877: my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
8878: if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
8879: ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
8880: $crsonly = 1;
8881: }
8882: $needlockcheck = 1;
8883: }
8884: }
8885: } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
8886: $needlockcheck = 1;
8887: }
8888: }
8889: if ($needlockcheck) {
8890: foreach my $envkey (keys(%env)) {
1.54 www 8891: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
8892: my $courseid=$2;
8893: my $roleid=$1.'.'.$2;
1.92 www 8894: $courseid=~s/^\///;
1.1172.2.142 raeburn 8895: unless ($env{'request.role'} eq $roleid) {
8896: my ($start,$end) = split(/\./,$env{$envkey});
8897: next unless (($now >= $start) && (!$end || $end > $now));
8898: }
1.54 www 8899: my $expiretime=600;
1.620 albertel 8900: if ($env{'request.role'} eq $roleid) {
1.54 www 8901: $expiretime=120;
8902: }
8903: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
8904: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 8905: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 8906: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 8907: }
1.620 albertel 8908: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
8909: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
8910: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
8911: &log($env{'user.domain'},$env{'user.name'},
8912: $env{'user.home'},
1.57 www 8913: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 8914: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8915: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8916: return '';
8917: }
8918: }
1.620 albertel 8919: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
8920: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1.1172.2.142 raeburn 8921: if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
1.620 albertel 8922: &log($env{'user.domain'},$env{'user.name'},
8923: $env{'user.home'},
1.57 www 8924: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 8925: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 8926: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 8927: return '';
8928: }
8929: }
8930: }
1.29 www 8931: }
1.52 www 8932: }
1.1172.2.126 raeburn 8933:
1.52 www 8934: #
8935: # Rest of the restrictions depend on selected course
8936: #
8937:
1.620 albertel 8938: unless ($env{'request.course.id'}) {
1.766 albertel 8939: if ($thisallowed eq 'A') {
8940: return 'A';
1.814 raeburn 8941: } elsif ($thisallowed eq 'B') {
8942: return 'B';
1.766 albertel 8943: } else {
8944: return '1';
8945: }
1.52 www 8946: }
1.29 www 8947:
1.52 www 8948: #
8949: # Now user is definitely in a course
8950: #
1.53 www 8951:
8952:
8953: # Course preferences
8954:
8955: if ($thisallowed=~/C/) {
1.620 albertel 8956: my $rolecode=(split(/\./,$env{'request.role'}))[0];
8957: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
8958: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 8959: =~/\Q$rolecode\E/) {
1.1103 raeburn 8960: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8961: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8962: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
8963: $env{'request.course.id'});
8964: }
1.237 www 8965: return '';
8966: }
8967:
1.620 albertel 8968: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 8969: =~/\Q$unamedom\E/) {
1.1103 raeburn 8970: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8971: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
8972: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
8973: $env{'request.course.id'});
8974: }
1.54 www 8975: return '';
8976: }
1.53 www 8977: }
8978:
8979: # Resource preferences
8980:
8981: if ($thisallowed=~/R/) {
1.620 albertel 8982: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 8983: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 8984: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 8985: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
8986: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
8987: }
8988: return '';
1.54 www 8989: }
1.53 www 8990: }
1.30 www 8991:
1.1172.2.146. .1(raebu 8992:22): # Restricted for deeplinked session?
8993:22):
8994:22): if ($env{'request.deeplink.login'}) {
8995:22): if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
8996:22): if (!$symb) { $symb=&symbread($uri,1); }
8997:22): if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
8998:22): return '';
8999:22): }
9000:22): }
9001:22): }
9002:22):
1.246 www 9003: # Restricted by state or randomout?
1.30 www 9004:
1.52 www 9005: if ($thisallowed=~/X/) {
1.620 albertel 9006: if ($env{'acc.randomout'}) {
1.579 albertel 9007: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 9008: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 9009: return '';
9010: }
1.247 www 9011: }
9012: if (&condval($statecond)) {
1.52 www 9013: return '2';
9014: } else {
9015: return '';
9016: }
9017: }
1.30 www 9018:
1.766 albertel 9019: if ($thisallowed eq 'A') {
9020: return 'A';
1.814 raeburn 9021: } elsif ($thisallowed eq 'B') {
9022: return 'B';
1.1172.2.146. .1(raebu 9023:22): } elsif ($thisallowed eq 'D') {
9024:22): return 'D';
1.766 albertel 9025: }
1.52 www 9026: return 'F';
1.232 www 9027: }
1.1162 raeburn 9028:
1.1172.2.13 raeburn 9029: # ------------------------------------------- Check construction space access
9030:
9031: sub constructaccess {
9032: my ($url,$setpriv)=@_;
9033:
9034: # We do not allow editing of previous versions of files
9035: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
9036:
9037: # Get username and domain from URL
9038: my ($ownername,$ownerdomain,$ownerhome);
9039:
9040: ($ownerdomain,$ownername) =
1.1172.2.146. .19(raeb 9041:-24): ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1172.2.13 raeburn 9042:
9043: # The URL does not really point to any authorspace, forget it
9044: unless (($ownername) && ($ownerdomain)) { return ''; }
9045:
9046: # Now we need to see if the user has access to the authorspace of
9047: # $ownername at $ownerdomain
9048:
9049: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
9050: # Real author for this?
9051: $ownerhome = $env{'user.home'};
9052: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
9053: return ($ownername,$ownerdomain,$ownerhome);
9054: }
9055: } else {
9056: # Co-author for this?
9057: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
9058: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
9059: $ownerhome = &homeserver($ownername,$ownerdomain);
9060: return ($ownername,$ownerdomain,$ownerhome);
9061: }
9062: }
9063:
9064: # We don't have any access right now. If we are not possibly going to do anything about this,
9065: # we might as well leave
9066: unless ($setpriv) { return ''; }
9067:
9068: # Backdoor access?
9069: my $allowed=&allowed('eco',$ownerdomain);
9070: # Nope
9071: unless ($allowed) { return ''; }
9072: # Looks like we may have access, but could be locked by the owner of the construction space
9073: if ($allowed eq 'U') {
9074: my %blocked=&get('environment',['domcoord.author'],
9075: $ownerdomain,$ownername);
9076: # Is blocked by owner
9077: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
9078: }
9079: if (($allowed eq 'F') || ($allowed eq 'U')) {
9080: # Grant temporary access
9081: my $then=$env{'user.login.time'};
1.1172.2.16 raeburn 9082: my $update=$env{'user.update.time'};
1.1172.2.13 raeburn 9083: if (!$update) { $update = $then; }
9084: my $refresh=$env{'user.refresh.time'};
9085: if (!$refresh) { $refresh = $update; }
9086: my $now = time;
9087: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
9088: $now,'ca','constructaccess');
9089: $ownerhome = &homeserver($ownername,$ownerdomain);
9090: return($ownername,$ownerdomain,$ownerhome);
9091: }
9092: # No business here
9093: return '';
9094: }
9095:
1.1172.2.66 raeburn 9096: # ----------------------------------------------------------- Content Blocking
9097:
9098: {
9099: # Caches for faster Course Contents display where content blocking
9100: # is in operation (i.e., interval param set) for timed quiz.
9101: #
9102: # User for whom data are being temporarily cached.
9103: my $cacheduser='';
1.1172.2.126 raeburn 9104: # Course for which data are being temporarily cached.
9105: my $cachedcid='';
1.1172.2.66 raeburn 9106: # Cached blockers for this user (a hash of blocking items).
9107: my %cachedblockers=();
9108: # When the data were last cached.
9109: my $cachedlast='';
9110:
9111: sub load_all_blockers {
1.1172.2.128 raeburn 9112: my ($uname,$udom)=@_;
1.1172.2.66 raeburn 9113: if (($uname ne '') && ($udom ne '')) {
9114: if (($cacheduser eq $uname.':'.$udom) &&
1.1172.2.126 raeburn 9115: ($cachedcid eq $env{'request.course.id'}) &&
1.1172.2.128 raeburn 9116: (abs($cachedlast-time)<5)) {
1.1172.2.66 raeburn 9117: return;
9118: }
9119: }
9120: $cachedlast=time;
9121: $cacheduser=$uname.':'.$udom;
1.1172.2.126 raeburn 9122: $cachedcid=$env{'request.course.id'};
1.1172.2.128 raeburn 9123: %cachedblockers = &get_commblock_resources();
1.1172.2.126 raeburn 9124: return;
1.1172.2.66 raeburn 9125: }
9126:
1.1162 raeburn 9127: sub get_comm_blocks {
9128: my ($cdom,$cnum) = @_;
9129: if ($cdom eq '' || $cnum eq '') {
9130: return unless ($env{'request.course.id'});
9131: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9132: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9133: }
9134: my %commblocks;
9135: my $hashid=$cdom.'_'.$cnum;
9136: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
9137: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
9138: %commblocks = %{$blocksref};
9139: } else {
9140: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
9141: my $cachetime = 600;
9142: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
9143: }
9144: return %commblocks;
9145: }
9146:
1.1172.2.66 raeburn 9147: sub get_commblock_resources {
9148: my ($blocks) = @_;
9149: my %blockers = ();
9150: return %blockers unless ($env{'request.course.id'});
1.1172.2.142 raeburn 9151: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9152: if ($env{'request.course.sec'}) {
9153: $courseurl .= '/'.$env{'request.course.sec'};
9154: }
9155: return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1162 raeburn 9156: my %commblocks;
9157: if (ref($blocks) eq 'HASH') {
9158: %commblocks = %{$blocks};
9159: } else {
9160: %commblocks = &get_comm_blocks();
9161: }
1.1172.2.66 raeburn 9162: return %blockers unless (keys(%commblocks) > 0);
1.1163 raeburn 9163: my $navmap = Apache::lonnavmaps::navmap->new();
1.1172.2.66 raeburn 9164: return %blockers unless (ref($navmap));
9165: my $now = time;
1.1162 raeburn 9166: foreach my $block (keys(%commblocks)) {
9167: if ($block =~ /^(\d+)____(\d+)$/) {
9168: my ($start,$end) = ($1,$2);
9169: if ($start <= $now && $end >= $now) {
9170: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9171: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
9172: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1172.2.66 raeburn 9173: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9174: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 9175: }
9176: }
9177: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1172.2.66 raeburn 9178: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9179: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 9180: }
9181: }
9182: }
9183: }
9184: }
9185: } elsif ($block =~ /^firstaccess____(.+)$/) {
9186: my $item = $1;
9187: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
9188: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1172.2.142 raeburn 9189: my (@interval,$mapname);
1.1172.2.66 raeburn 9190: my $type = 'map';
9191: if ($item eq 'course') {
9192: $type = 'course';
9193: @interval=&EXT("resource.0.interval");
9194: } else {
9195: if ($item =~ /___\d+___/) {
9196: $type = 'resource';
9197: @interval=&EXT("resource.0.interval",$item);
1.1162 raeburn 9198: } else {
1.1172.2.142 raeburn 9199: $mapname = &deversion($item);
9200: if (ref($navmap)) {
9201: my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
9202: @interval = ($timelimit,'map');
1.1162 raeburn 9203: }
9204: }
1.1172.2.66 raeburn 9205: }
1.1172.2.146. .1(raebu 9206:22): if ($interval[0] =~ /^(\d+)/) {
9207:22): my $timelimit = $1;
1.1172.2.66 raeburn 9208: my $first_access;
9209: if ($type eq 'resource') {
9210: $first_access=&get_first_access($interval[1],$item);
9211: } elsif ($type eq 'map') {
9212: $first_access=&get_first_access($interval[1],undef,$item);
9213: } else {
9214: $first_access=&get_first_access($interval[1]);
9215: }
9216: if ($first_access) {
1.1172.2.146. .1(raebu 9217:22): my $timesup = $first_access+$timelimit;
1.1172.2.66 raeburn 9218: if ($timesup > $now) {
9219: my $activeblock;
1.1172.2.142 raeburn 9220: if ($type eq 'resource') {
9221: if (ref($navmap)) {
9222: my $res = $navmap->getBySymb($item);
9223: if ($res->answerable()) {
9224: $activeblock = 1;
9225: }
9226: }
9227: } elsif ($type eq 'map') {
9228: my $mapsymb = &symbread($mapname,1);
9229: if (($mapsymb) && (ref($navmap))) {
9230: my $mapres = $navmap->getBySymb($mapsymb);
9231: if (ref($mapres)) {
9232: my $first = $mapres->map_start();
9233: my $finish = $mapres->map_finish();
9234: my $it = $navmap->getIterator($first,$finish,undef,0,0);
9235: if (ref($it)) {
9236: my $res;
9237: while ($res = $it->next(undef,1)) {
9238: next unless (ref($res));
9239: my $symb = $res->symb();
9240: next if (($symb eq $mapsymb) || ($symb eq ''));
9241: @interval=&EXT("resource.0.interval",$symb);
9242: if ($interval[1] eq 'map') {
9243: if ($res->answerable()) {
9244: $activeblock = 1;
9245: last;
9246: }
9247: }
9248: }
9249: }
9250: }
1.1172.2.66 raeburn 9251: }
9252: }
9253: if ($activeblock) {
9254: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
9255: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
9256: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
9257: }
9258: }
9259: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
9260: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
9261: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 9262: }
1.1162 raeburn 9263: }
9264: }
9265: }
9266: }
9267: }
9268: }
9269: }
9270: }
9271: }
1.1172.2.66 raeburn 9272: return %blockers;
1.1162 raeburn 9273: }
9274:
1.1172.2.66 raeburn 9275: sub has_comm_blocking {
1.1172.2.128 raeburn 9276: my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
1.1172.2.66 raeburn 9277: my @blockers;
9278: return unless ($env{'request.course.id'});
9279: return unless ($priv eq 'bre');
9280: return if ($env{'request.state'} eq 'construct');
1.1172.2.142 raeburn 9281: my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
9282: if ($env{'request.course.sec'}) {
9283: $courseurl .= '/'.$env{'request.course.sec'};
9284: }
9285: return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
1.1172.2.128 raeburn 9286: my %blockinfo;
9287: if (ref($blocks) eq 'HASH') {
9288: %blockinfo = &get_commblock_resources($blocks);
9289: } else {
9290: &load_all_blockers($env{'user.name'},$env{'user.domain'});
9291: %blockinfo = %cachedblockers;
9292: }
9293: return unless (keys(%blockinfo) > 0);
1.1172.2.66 raeburn 9294: my (%possibles,@symbs);
9295: if (!$symb) {
1.1172.2.128 raeburn 9296: $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
1.1162 raeburn 9297: }
1.1172.2.66 raeburn 9298: if ($symb) {
9299: @symbs = ($symb);
9300: } elsif (keys(%possibles)) {
9301: @symbs = keys(%possibles);
9302: }
9303: my $noblock;
9304: foreach my $symb (@symbs) {
9305: last if ($noblock);
9306: my ($map,$resid,$resurl)=&decode_symb($symb);
1.1172.2.128 raeburn 9307: foreach my $block (keys(%blockinfo)) {
1.1172.2.66 raeburn 9308: if ($block =~ /^firstaccess____(.+)$/) {
9309: my $item = $1;
1.1172.2.126 raeburn 9310: unless ($blocked) {
9311: if (($item eq $map) || ($item eq $symb)) {
9312: $noblock = 1;
9313: last;
9314: }
1.1172.2.66 raeburn 9315: }
1.1162 raeburn 9316: }
1.1172.2.128 raeburn 9317: if (ref($blockinfo{$block}) eq 'HASH') {
9318: if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
9319: if ($blockinfo{$block}{'resources'}{$symb}) {
1.1172.2.66 raeburn 9320: unless (grep(/^\Q$block\E$/,@blockers)) {
9321: push(@blockers,$block);
9322: }
9323: }
9324: }
1.1172.2.128 raeburn 9325: if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
9326: if ($blockinfo{$block}{'maps'}{$map}) {
1.1172.2.126 raeburn 9327: unless (grep(/^\Q$block\E$/,@blockers)) {
9328: push(@blockers,$block);
9329: }
1.1172.2.66 raeburn 9330: }
9331: }
1.1162 raeburn 9332: }
9333: }
9334: }
1.1172.2.126 raeburn 9335: unless ($noblock) {
9336: return @blockers;
9337: }
9338: return;
1.1172.2.66 raeburn 9339: }
1.1162 raeburn 9340: }
9341:
1.1172.2.146. .1(raebu 9342:22): sub deeplink_check {
9343:22): my ($priv,$symb,$uri) = @_;
9344:22): return unless ($env{'request.course.id'});
9345:22): return unless ($priv eq 'bre');
9346:22): return if ($env{'request.state'} eq 'construct');
9347:22): return if ($env{'request.role.adv'});
9348:22): my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
9349:22): my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
9350:22): my (%possibles,@symbs);
9351:22): if (!$symb) {
9352:22): $symb = &symbread($uri,1,1,1,\%possibles);
9353:22): }
9354:22): if ($symb) {
9355:22): @symbs = ($symb);
9356:22): } elsif (keys(%possibles)) {
9357:22): @symbs = keys(%possibles);
9358:22): }
9359:22):
9360:22): my ($deeplink_symb,$allow);
9361:22): if ($env{'request.deeplink.login'}) {
9362:22): $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
9363:22): }
9364:22): foreach my $symb (@symbs) {
9365:22): last if ($allow);
9366:22): my $deeplink = &EXT("resource.0.deeplink",$symb);
9367:22): if ($deeplink eq '') {
9368:22): $allow = 1;
9369:22): } else {
9370:22): my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
9371:22): if ($state ne 'only') {
9372:22): $allow = 1;
9373:22): } else {
9374:22): my $check_deeplink_entry;
9375:22): if ($protect ne 'none') {
9376:22): my ($acctype,$item) = split(/:/,$protect);
9377:22): if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
9378:22): if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
9379:22): $check_deeplink_entry = 1
9380:22): }
9381:22): } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
9382:22): if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
9383:22): $check_deeplink_entry = 1;
9384:22): }
9385:22): } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
9386:22): if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
9387:22): $check_deeplink_entry = 1;
9388:22): }
9389:22): }
9390:22): }
9391:22): if (($protect eq 'none') || ($check_deeplink_entry)) {
9392:22): if ($scope eq 'res') {
9393:22): if ($symb eq $deeplink_symb) {
9394:22): $allow = 1;
9395:22): }
9396:22): } elsif (($scope eq 'map') || ($scope eq 'rec')) {
9397:22): my ($map_from_symb,$map_from_login);
9398:22): $map_from_symb = &deversion((&decode_symb($symb))[0]);
9399:22): if ($deeplink_symb =~ /\.(page|sequence)$/) {
9400:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
9401:22): } else {
9402:22): $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
9403:22): }
9404:22): if (($map_from_symb) && ($map_from_login)) {
9405:22): if ($map_from_symb eq $map_from_login) {
9406:22): $allow = 1;
9407:22): } elsif ($scope eq 'rec') {
9408:22): my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
9409:22): if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
9410:22): $allow = 1;
9411:22): }
9412:22): }
9413:22): }
9414:22): }
9415:22): }
9416:22): }
9417:22): }
9418:22): }
9419:22): return if ($allow);
9420:22): return 1;
9421:22): }
9422:22):
1.1172.2.66 raeburn 9423: # -------------------------------- Deversion and split uri into path an filename
9424:
1.1133 foxr 9425: #
1.1172.2.66 raeburn 9426: # Removes the version from a URI and
1.1133 foxr 9427: # splits it in to its filename and path to the filename.
9428: # Seems like File::Basename could have done this more clearly.
9429: # Parameters:
9430: # $uri - input URI
9431: # Returns:
9432: # Two element list consisting of
9433: # $pathname - the URI up to and excluding the trailing /
9434: # $filename - The part of the URI following the last /
9435: # NOTE:
9436: # Another realization of this is simply:
9437: # use File::Basename;
9438: # ...
9439: # $uri = shift;
9440: # $filename = basename($uri);
9441: # $path = dirname($uri);
9442: # return ($filename, $path);
9443: #
9444: # The implementation below is probably faster however.
9445: #
1.710 albertel 9446: sub split_uri_for_cond {
9447: my $uri=&deversion(&declutter(shift));
9448: my @uriparts=split(/\//,$uri);
9449: my $filename=pop(@uriparts);
9450: my $pathname=join('/',@uriparts);
9451: return ($pathname,$filename);
9452: }
1.232 www 9453: # --------------------------------------------------- Is a resource on the map?
9454:
9455: sub is_on_map {
1.710 albertel 9456: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 9457: #Trying to find the conditional for the file
1.620 albertel 9458: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 9459: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 9460: if ($match) {
1.289 bowersj2 9461: return (1,$1);
9462: } else {
1.434 www 9463: return (0,0);
1.289 bowersj2 9464: }
1.12 www 9465: }
9466:
1.427 www 9467: # --------------------------------------------------------- Get symb from alias
9468:
9469: sub get_symb_from_alias {
9470: my $symb=shift;
9471: my ($map,$resid,$url)=&decode_symb($symb);
9472: # Already is a symb
9473: if ($url) { return $symb; }
9474: # Must be an alias
9475: my $aliassymb='';
9476: my %bighash;
1.620 albertel 9477: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 9478: &GDBM_READER(),0640)) {
9479: my $rid=$bighash{'mapalias_'.$symb};
9480: if ($rid) {
9481: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 9482: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
9483: $resid,$bighash{'src_'.$rid});
1.427 www 9484: }
9485: untie %bighash;
9486: }
9487: return $aliassymb;
9488: }
9489:
1.12 www 9490: # ----------------------------------------------------------------- Define Role
9491:
9492: sub definerole {
9493: if (allowed('mcr','/')) {
1.1172.2.84 raeburn 9494: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 9495: foreach my $role (split(':',$sysrole)) {
9496: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9497: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
9498: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
9499: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9500: return "refused:s:$crole&$cqual";
9501: }
9502: }
1.191 harris41 9503: }
1.800 albertel 9504: foreach my $role (split(':',$domrole)) {
9505: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9506: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
9507: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
9508: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 9509: return "refused:d:$crole&$cqual";
9510: }
9511: }
1.191 harris41 9512: }
1.800 albertel 9513: foreach my $role (split(':',$courole)) {
9514: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 9515: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
9516: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
9517: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 9518: return "refused:c:$crole&$cqual";
9519: }
9520: }
1.191 harris41 9521: }
1.1172.2.84 raeburn 9522: my $uhome;
9523: if (($uname ne '') && ($udom ne '')) {
9524: $uhome = &homeserver($uname,$udom);
9525: return $uhome if ($uhome eq 'no_host');
9526: } else {
9527: $uname = $env{'user.name'};
9528: $udom = $env{'user.domain'};
9529: $uhome = $env{'user.home'};
9530: }
1.620 albertel 9531: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1172.2.84 raeburn 9532: "$udom:$uname:rolesdef_$rolename=".
1.21 www 9533: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1172.2.84 raeburn 9534: return reply($command,$uhome);
1.12 www 9535: } else {
9536: return 'refused';
9537: }
1.105 harris41 9538: }
9539:
9540: # ---------------- Make a metadata query against the network of library servers
9541:
9542: sub metadata_query {
1.1172.2.33 raeburn 9543: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 9544: my %rhash;
1.845 albertel 9545: my %libserv = &all_library();
1.244 matthew 9546: my @server_list = (defined($server_array) ? @$server_array
9547: : keys(%libserv) );
9548: for my $server (@server_list) {
1.1172.2.33 raeburn 9549: my $domains = '';
9550: if (ref($domains_hash) eq 'HASH') {
9551: $domains = $domains_hash->{$server};
9552: }
1.118 harris41 9553: unless ($custom or $customshow) {
1.1172.2.33 raeburn 9554: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 9555: $rhash{$server}=$reply;
9556: }
9557: else {
9558: my $reply=&reply("querysend:".&escape($query).':'.
1.1172.2.33 raeburn 9559: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 9560: $server);
9561: $rhash{$server}=$reply;
9562: }
1.112 harris41 9563: }
1.118 harris41 9564: return \%rhash;
1.240 www 9565: }
9566:
9567: # ----------------------------------------- Send log queries and wait for reply
9568:
9569: sub log_query {
9570: my ($uname,$udom,$query,%filters)=@_;
9571: my $uhome=&homeserver($uname,$udom);
9572: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 9573: my $uhost=&hostname($uhome);
1.800 albertel 9574: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 9575: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
9576: $uhome);
1.479 albertel 9577: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 9578: return get_query_reply($queryid);
9579: }
9580:
1.818 raeburn 9581: # -------------------------- Update MySQL table for portfolio file
9582:
9583: sub update_portfolio_table {
1.821 raeburn 9584: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 9585: if ($group ne '') {
9586: $file_name =~s /^\Q$group\E//;
9587: }
1.818 raeburn 9588: my $homeserver = &homeserver($uname,$udom);
9589: my $queryid=
1.821 raeburn 9590: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
9591: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 9592: my $reply = &get_query_reply($queryid);
9593: return $reply;
9594: }
9595:
1.899 raeburn 9596: # -------------------------- Update MySQL allusers table
9597:
9598: sub update_allusers_table {
9599: my ($uname,$udom,$names) = @_;
9600: my $homeserver = &homeserver($uname,$udom);
9601: my $queryid=
9602: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
9603: 'lastname='.&escape($names->{'lastname'}).'%%'.
9604: 'firstname='.&escape($names->{'firstname'}).'%%'.
9605: 'middlename='.&escape($names->{'middlename'}).'%%'.
9606: 'generation='.&escape($names->{'generation'}).'%%'.
9607: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
9608: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 9609: return;
1.899 raeburn 9610: }
9611:
1.508 raeburn 9612: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 9613:
9614: sub fetch_enrollment_query {
1.511 raeburn 9615: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1172.2.79 raeburn 9616: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 9617: my $maxtries = 1;
1.508 raeburn 9618: if ($context eq 'automated') {
9619: $homeserver = $perlvar{'lonHostID'};
1.1172.2.79 raeburn 9620: $sleep = 2;
9621: $loopmax = 100;
1.547 raeburn 9622: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 9623: } else {
9624: $homeserver = &homeserver($cnum,$dom);
9625: }
1.838 albertel 9626: my $host=&hostname($homeserver);
1.506 raeburn 9627: my $cmd = '';
1.1000 raeburn 9628: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 9629: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 9630: }
9631: $cmd =~ s/%%$//;
9632: $cmd = &escape($cmd);
9633: my $query = 'fetchenrollment';
1.620 albertel 9634: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 9635: unless ($queryid=~/^\Q$host\E\_/) {
9636: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
9637: return 'error: '.$queryid;
9638: }
1.1172.2.93 raeburn 9639: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9640: my $tries = 1;
9641: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1172.2.79 raeburn 9642: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 9643: $tries ++;
9644: }
1.526 raeburn 9645: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 9646: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 9647: } else {
1.901 albertel 9648: my @responses = split(/:/,$reply);
1.1172.2.82 raeburn 9649: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 9650: foreach my $line (@responses) {
9651: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 9652: $$replyref{$key} = $value;
9653: }
9654: } else {
1.1117 foxr 9655: my $pathname = LONCAPA::tempdir();
1.800 albertel 9656: foreach my $line (@responses) {
9657: my ($key,$value) = split(/=/,$line);
1.506 raeburn 9658: $$replyref{$key} = $value;
9659: if ($value > 0) {
1.800 albertel 9660: foreach my $item (@{$$affiliatesref{$key}}) {
9661: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 9662: my $destname = $pathname.'/'.$filename;
9663: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 9664: if ($xml_classlist =~ /^error/) {
9665: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
9666: } else {
1.1172.2.96 raeburn 9667: if ( open(FILE,">",$destname) ) {
1.506 raeburn 9668: print FILE &unescape($xml_classlist);
9669: close(FILE);
1.526 raeburn 9670: } else {
9671: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 9672: }
9673: }
9674: }
9675: }
9676: }
9677: }
9678: return 'ok';
9679: }
9680: return 'error';
9681: }
9682:
1.242 www 9683: sub get_query_reply {
1.1172.2.79 raeburn 9684: my ($queryid,$sleep,$loopmax) = @_;
9685: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
9686: $sleep = 0.2;
9687: }
9688: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
9689: $loopmax = 100;
9690: }
1.1117 foxr 9691: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 9692: my $reply='';
1.1172.2.79 raeburn 9693: for (1..$loopmax) {
9694: sleep($sleep);
1.240 www 9695: if (-e $replyfile.'.end') {
1.1172.2.96 raeburn 9696: if (open(my $fh,"<",$replyfile)) {
1.904 albertel 9697: $reply = join('',<$fh>);
9698: close($fh);
1.240 www 9699: } else { return 'error: reply_file_error'; }
1.242 www 9700: return &unescape($reply);
9701: }
1.240 www 9702: }
1.242 www 9703: return 'timeout:'.$queryid;
1.240 www 9704: }
9705:
9706: sub courselog_query {
1.241 www 9707: #
9708: # possible filters:
9709: # url: url or symb
9710: # username
9711: # domain
9712: # action: view, submit, grade
9713: # start: timestamp
9714: # end: timestamp
9715: #
1.240 www 9716: my (%filters)=@_;
1.620 albertel 9717: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 9718: if ($filters{'url'}) {
9719: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
9720: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
9721: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
9722: }
1.620 albertel 9723: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
9724: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 9725: return &log_query($cname,$cdom,'courselog',%filters);
9726: }
9727:
9728: sub userlog_query {
1.858 raeburn 9729: #
9730: # possible filters:
9731: # action: log check role
9732: # start: timestamp
9733: # end: timestamp
9734: #
1.240 www 9735: my ($uname,$udom,%filters)=@_;
9736: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 9737: }
9738:
1.506 raeburn 9739: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
9740:
9741: sub auto_run {
1.508 raeburn 9742: my ($cnum,$cdom) = @_;
1.876 raeburn 9743: my $response = 0;
9744: my $settings;
9745: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
9746: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
9747: $settings = $domconfig{'autoenroll'};
9748: if ($settings->{'run'} eq '1') {
9749: $response = 1;
9750: }
9751: } else {
1.934 raeburn 9752: my $homeserver;
9753: if (&is_course($cdom,$cnum)) {
9754: $homeserver = &homeserver($cnum,$cdom);
9755: } else {
9756: $homeserver = &domain($cdom,'primary');
9757: }
9758: if ($homeserver ne 'no_host') {
9759: $response = &reply('autorun:'.$cdom,$homeserver);
9760: }
1.876 raeburn 9761: }
1.506 raeburn 9762: return $response;
9763: }
1.776 albertel 9764:
1.506 raeburn 9765: sub auto_get_sections {
1.508 raeburn 9766: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 9767: my $homeserver;
9768: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9769: $homeserver = &homeserver($cnum,$cdom);
9770: }
9771: if (!defined($homeserver)) {
9772: if ($cdom =~ /^$match_domain$/) {
9773: $homeserver = &domain($cdom,'primary');
9774: }
9775: }
9776: my @secs;
9777: if (defined($homeserver)) {
9778: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
9779: unless ($response eq 'refused') {
9780: @secs = split(/:/,$response);
9781: }
1.506 raeburn 9782: }
9783: return @secs;
9784: }
1.776 albertel 9785:
1.506 raeburn 9786: sub auto_new_course {
1.1099 raeburn 9787: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 9788: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 9789: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 9790: return $response;
9791: }
1.776 albertel 9792:
1.506 raeburn 9793: sub auto_validate_courseID {
1.508 raeburn 9794: my ($cnum,$cdom,$inst_course_id) = @_;
9795: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 9796: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 9797: return $response;
9798: }
1.776 albertel 9799:
1.1007 raeburn 9800: sub auto_validate_instcode {
1.1020 raeburn 9801: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 9802: my ($homeserver,$response);
9803: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9804: $homeserver = &homeserver($cnum,$cdom);
9805: }
9806: if (!defined($homeserver)) {
9807: if ($cdom =~ /^$match_domain$/) {
9808: $homeserver = &domain($cdom,'primary');
9809: }
9810: }
1.1065 raeburn 9811: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
9812: &escape($instcode).':'.&escape($owner),$homeserver));
1.1172.2.19 raeburn 9813: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
9814: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 9815: }
9816:
1.1172.2.141 raeburn 9817: sub auto_validate_inst_crosslist {
9818: my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
9819: my ($homeserver,$response);
9820: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9821: $homeserver = &homeserver($cnum,$cdom);
9822: }
9823: if (!defined($homeserver)) {
9824: if ($cdom =~ /^$match_domain$/) {
9825: $homeserver = &domain($cdom,'primary');
9826: }
9827: }
9828: unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
9829: $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
9830: &escape($instcode).':'.&escape($inst_xlist).':'.
9831: &escape($coowner),$homeserver);
9832: }
9833: return $response;
9834: }
9835:
1.506 raeburn 9836: sub auto_create_password {
1.873 raeburn 9837: my ($cnum,$cdom,$authparam,$udom) = @_;
9838: my ($homeserver,$response);
1.506 raeburn 9839: my $create_passwd = 0;
9840: my $authchk = '';
1.873 raeburn 9841: if ($udom =~ /^$match_domain$/) {
9842: $homeserver = &domain($udom,'primary');
9843: }
9844: if ($homeserver eq '') {
9845: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
9846: $homeserver = &homeserver($cnum,$cdom);
9847: }
9848: }
9849: if ($homeserver eq '') {
9850: $authchk = 'nodomain';
1.506 raeburn 9851: } else {
1.873 raeburn 9852: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
9853: if ($response eq 'refused') {
9854: $authchk = 'refused';
9855: } else {
1.901 albertel 9856: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 9857: }
1.506 raeburn 9858: }
9859: return ($authparam,$create_passwd,$authchk);
9860: }
9861:
1.706 raeburn 9862: sub auto_photo_permission {
9863: my ($cnum,$cdom,$students) = @_;
9864: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 9865: my ($outcome,$perm_reqd,$conditions) =
9866: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 9867: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9868: return (undef,undef);
9869: }
1.706 raeburn 9870: return ($outcome,$perm_reqd,$conditions);
9871: }
9872:
9873: sub auto_checkphotos {
9874: my ($uname,$udom,$pid) = @_;
9875: my $homeserver = &homeserver($uname,$udom);
9876: my ($result,$resulttype);
9877: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 9878: &escape($uname).':'.&escape($pid),
9879: $homeserver));
1.709 albertel 9880: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9881: return (undef,undef);
9882: }
1.706 raeburn 9883: if ($outcome) {
9884: ($result,$resulttype) = split(/:/,$outcome);
9885: }
9886: return ($result,$resulttype);
9887: }
9888:
9889: sub auto_photochoice {
9890: my ($cnum,$cdom) = @_;
9891: my $homeserver = &homeserver($cnum,$cdom);
9892: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 9893: &escape($cdom),
9894: $homeserver)));
1.709 albertel 9895: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
9896: return (undef,undef);
9897: }
1.706 raeburn 9898: return ($update,$comment);
9899: }
9900:
9901: sub auto_photoupdate {
9902: my ($affiliatesref,$dom,$cnum,$photo) = @_;
9903: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 9904: my $host=&hostname($homeserver);
1.706 raeburn 9905: my $cmd = '';
9906: my $maxtries = 1;
1.800 albertel 9907: foreach my $affiliate (keys(%{$affiliatesref})) {
9908: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 9909: }
9910: $cmd =~ s/%%$//;
9911: $cmd = &escape($cmd);
9912: my $query = 'institutionalphotos';
9913: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
9914: unless ($queryid=~/^\Q$host\E\_/) {
9915: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
9916: return 'error: '.$queryid;
9917: }
9918: my $reply = &get_query_reply($queryid);
9919: my $tries = 1;
9920: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
9921: $reply = &get_query_reply($queryid);
9922: $tries ++;
9923: }
9924: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
9925: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
9926: } else {
9927: my @responses = split(/:/,$reply);
9928: my $outcome = shift(@responses);
9929: foreach my $item (@responses) {
9930: my ($key,$value) = split(/=/,$item);
9931: $$photo{$key} = $value;
9932: }
9933: return $outcome;
9934: }
9935: return 'error';
9936: }
9937:
1.521 raeburn 9938: sub auto_instcode_format {
1.793 albertel 9939: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
9940: $cat_order) = @_;
1.521 raeburn 9941: my $courses = '';
1.772 raeburn 9942: my @homeservers;
1.521 raeburn 9943: if ($caller eq 'global') {
1.841 albertel 9944: my %servers = &get_servers($codedom,'library');
9945: foreach my $tryserver (keys(%servers)) {
9946: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9947: push(@homeservers,$tryserver);
9948: }
1.584 raeburn 9949: }
1.1022 raeburn 9950: } elsif ($caller eq 'requests') {
9951: if ($codedom =~ /^$match_domain$/) {
9952: my $chome = &domain($codedom,'primary');
9953: unless ($chome eq 'no_host') {
9954: push(@homeservers,$chome);
9955: }
9956: }
1.521 raeburn 9957: } else {
1.772 raeburn 9958: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 9959: }
1.793 albertel 9960: foreach my $code (keys(%{$instcodes})) {
9961: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 9962: }
9963: chop($courses);
1.772 raeburn 9964: my $ok_response = 0;
9965: my $response;
9966: while (@homeservers > 0 && $ok_response == 0) {
9967: my $server = shift(@homeservers);
9968: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
9969: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
9970: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 9971: split(/:/,$response);
1.772 raeburn 9972: %{$codes} = (%{$codes},&str2hash($codes_str));
9973: push(@{$codetitles},&str2array($codetitles_str));
9974: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
9975: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
9976: $ok_response = 1;
9977: }
9978: }
9979: if ($ok_response) {
1.521 raeburn 9980: return 'ok';
1.772 raeburn 9981: } else {
9982: return $response;
1.521 raeburn 9983: }
9984: }
9985:
1.792 raeburn 9986: sub auto_instcode_defaults {
9987: my ($domain,$returnhash,$code_order) = @_;
9988: my @homeservers;
1.841 albertel 9989:
9990: my %servers = &get_servers($domain,'library');
9991: foreach my $tryserver (keys(%servers)) {
9992: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
9993: push(@homeservers,$tryserver);
9994: }
1.792 raeburn 9995: }
1.841 albertel 9996:
1.792 raeburn 9997: my $response;
1.841 albertel 9998: foreach my $server (@homeservers) {
1.792 raeburn 9999: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 10000: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10001:
10002: foreach my $pair (split(/\&/,$response)) {
10003: my ($name,$value)=split(/\=/,$pair);
10004: if ($name eq 'code_order') {
10005: @{$code_order} = split(/\&/,&unescape($value));
10006: } else {
10007: $returnhash->{&unescape($name)}=&unescape($value);
10008: }
10009: }
10010: return 'ok';
1.792 raeburn 10011: }
1.841 albertel 10012:
10013: return $response;
1.1003 raeburn 10014: }
10015:
10016: sub auto_possible_instcodes {
1.1007 raeburn 10017: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10018: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
10019: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10020: return;
10021: }
1.1003 raeburn 10022: my (@homeservers,$uhome);
10023: if (defined(&domain($domain,'primary'))) {
10024: $uhome=&domain($domain,'primary');
10025: push(@homeservers,&domain($domain,'primary'));
10026: } else {
10027: my %servers = &get_servers($domain,'library');
10028: foreach my $tryserver (keys(%servers)) {
10029: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10030: push(@homeservers,$tryserver);
10031: }
10032: }
10033: }
10034: my $response;
10035: foreach my $server (@homeservers) {
10036: $response=&reply('autopossibleinstcodes:'.$domain,$server);
10037: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 10038: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
10039: split(':',$response);
10040: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10041: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 10042: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 10043: my ($name,$value)=split('=',$item);
10044: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10045: }
10046: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 10047: my ($name,$value)=split('=',$item);
10048: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 10049: }
10050: return 'ok';
10051: }
10052: return $response;
10053: }
1.792 raeburn 10054:
1.1010 raeburn 10055: sub auto_courserequest_checks {
10056: my ($dom) = @_;
1.1020 raeburn 10057: my ($homeserver,%validations);
10058: if ($dom =~ /^$match_domain$/) {
10059: $homeserver = &domain($dom,'primary');
10060: }
10061: unless ($homeserver eq 'no_host') {
10062: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10063: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10064: my @items = split(/&/,$response);
10065: foreach my $item (@items) {
10066: my ($key,$value) = split('=',$item);
10067: $validations{&unescape($key)} = &thaw_unescape($value);
10068: }
10069: }
10070: }
1.1010 raeburn 10071: return %validations;
10072: }
10073:
1.1020 raeburn 10074: sub auto_courserequest_validation {
1.1172.2.43 raeburn 10075: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 10076: my ($homeserver,$response);
10077: if ($dom =~ /^$match_domain$/) {
10078: $homeserver = &domain($dom,'primary');
10079: }
1.1172.2.43 raeburn 10080: unless ($homeserver eq 'no_host') {
10081: my $customdata;
10082: if (ref($custominfo) eq 'HASH') {
10083: $customdata = &freeze_escape($custominfo);
10084: }
1.1020 raeburn 10085: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 10086: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1172.2.43 raeburn 10087: ':'.&escape($instcode).':'.&escape($instseclist).':'.
10088: $customdata,$homeserver));
1.1020 raeburn 10089: }
10090: return $response;
10091: }
10092:
1.777 albertel 10093: sub auto_validate_class_sec {
1.918 raeburn 10094: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 10095: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 10096: my $ownerlist;
10097: if (ref($owners) eq 'ARRAY') {
10098: $ownerlist = join(',',@{$owners});
10099: } else {
10100: $ownerlist = $owners;
10101: }
1.773 raeburn 10102: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 10103: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 10104: return $response;
10105: }
10106:
1.1172.2.141 raeburn 10107: sub auto_instsec_reformat {
10108: my ($cdom,$action,$instsecref) = @_;
10109: return unless(($action eq 'clutter') || ($action eq 'declutter'));
10110: my @homeservers;
10111: if (defined(&domain($cdom,'primary'))) {
10112: push(@homeservers,&domain($cdom,'primary'));
10113: } else {
10114: my %servers = &get_servers($cdom,'library');
10115: foreach my $tryserver (keys(%servers)) {
10116: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10117: push(@homeservers,$tryserver);
10118: }
10119: }
10120: }
10121: my $response;
10122: my %reformatted = %{$instsecref};
10123: foreach my $server (@homeservers) {
10124: if (ref($instsecref) eq 'HASH') {
10125: my $info = &freeze_escape($instsecref);
10126: my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10127: $action.':'.$info,$server);
1.1172.2.146. .26(raeb 10128:-25): next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/);
1.1172.2.141 raeburn 10129: my @items = split(/&/,$response);
10130: foreach my $item (@items) {
10131: my ($key,$value) = split(/=/,$item);
10132: $reformatted{&unescape($key)} = &thaw_unescape($value);
10133: }
10134: }
10135: }
10136: return %reformatted;
10137: }
10138:
1.1172.2.94 raeburn 10139: sub auto_validate_instclasses {
10140: my ($cdom,$cnum,$owners,$classesref) = @_;
10141: my ($homeserver,%validations);
10142: $homeserver = &homeserver($cnum,$cdom);
10143: unless ($homeserver eq 'no_host') {
10144: my $ownerlist;
10145: if (ref($owners) eq 'ARRAY') {
10146: $ownerlist = join(',',@{$owners});
10147: } else {
10148: $ownerlist = $owners;
10149: }
10150: if (ref($classesref) eq 'HASH') {
10151: my $classes = &freeze_escape($classesref);
10152: my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10153: ':'.$cdom.':'.$classes,$homeserver);
10154: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10155: my @items = split(/&/,$response);
10156: foreach my $item (@items) {
10157: my ($key,$value) = split('=',$item);
10158: $validations{&unescape($key)} = &thaw_unescape($value);
10159: }
10160: }
10161: }
10162: }
10163: return %validations;
10164: }
10165:
1.1172.2.38 raeburn 10166: sub auto_crsreq_update {
10167: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1172.2.45 raeburn 10168: $code,$accessstart,$accessend,$inbound) = @_;
1.1172.2.38 raeburn 10169: my ($homeserver,%crsreqresponse);
10170: if ($cdom =~ /^$match_domain$/) {
10171: $homeserver = &domain($cdom,'primary');
10172: }
10173: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10174: my $info;
10175: if (ref($inbound) eq 'HASH') {
10176: $info = &freeze_escape($inbound);
10177: }
10178: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10179: ':'.&escape($action).':'.&escape($ownername).':'.
10180: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1172.2.45 raeburn 10181: &escape($title).':'.&escape($code).':'.
10182: &escape($accessstart).':'.&escape($accessend).':'.$info,$homeserver);
1.1172.2.38 raeburn 10183: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10184: my @items = split(/&/,$response);
10185: foreach my $item (@items) {
10186: my ($key,$value) = split('=',$item);
10187: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10188: }
10189: }
10190: }
10191: return \%crsreqresponse;
10192: }
10193:
1.1172.2.78 raeburn 10194: sub auto_export_grades {
10195: my ($cdom,$cnum,$inforef,$gradesref) = @_;
10196: my ($homeserver,%exportresponse);
10197: if ($cdom =~ /^$match_domain$/) {
10198: $homeserver = &domain($cdom,'primary');
10199: }
10200: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10201: my $info;
10202: if (ref($inforef) eq 'HASH') {
10203: $info = &freeze_escape($inforef);
10204: }
10205: if (ref($gradesref) eq 'HASH') {
10206: my $grades = &freeze_escape($gradesref);
10207: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10208: $info.':'.$grades,$homeserver);
1.1172.2.146. .26(raeb 10209:-25): unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/) {
1.1172.2.78 raeburn 10210: my @items = split(/&/,$response);
10211: foreach my $item (@items) {
10212: my ($key,$value) = split('=',$item);
10213: $exportresponse{&unescape($key)} = &thaw_unescape($value);
10214: }
10215: }
10216: }
10217: }
10218: return \%exportresponse;
10219: }
10220:
1.1172.2.68 raeburn 10221: sub check_instcode_cloning {
10222: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10223: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10224: return;
10225: }
10226: my $canclone;
10227: if (@{$code_order} > 0) {
10228: my $instcoderegexp ='^';
10229: my @clonecodes = split(/\&/,$cloner);
10230: foreach my $item (@{$code_order}) {
10231: if (grep(/^\Q$item\E=/,@clonecodes)) {
10232: foreach my $pair (@clonecodes) {
10233: my ($key,$val) = split(/\=/,$pair,2);
10234: $val = &unescape($val);
10235: if ($key eq $item) {
10236: $instcoderegexp .= '('.$val.')';
10237: last;
10238: }
10239: }
10240: } else {
10241: $instcoderegexp .= $codedefaults->{$item};
10242: }
10243: }
10244: $instcoderegexp .= '$';
10245: my (@from,@to);
10246: eval {
10247: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10248: (@to) = ($clonetocode =~ /$instcoderegexp/);
10249: };
10250: if ((@from > 0) && (@to > 0)) {
10251: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10252: if (!@diffs) {
10253: $canclone = 1;
10254: }
10255: }
10256: }
10257: return $canclone;
10258: }
10259:
10260: sub default_instcode_cloning {
10261: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10262: my (%codedefaults,@code_order,$canclone);
10263: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10264: %codedefaults = %{$codedefaultsref};
10265: @code_order = @{$codeorderref};
10266: } elsif ($clonedom) {
10267: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10268: }
10269: if (($domdefclone) && (@code_order)) {
10270: my @clonecodes = split(/\+/,$domdefclone);
10271: my $instcoderegexp ='^';
10272: foreach my $item (@code_order) {
10273: if (grep(/^\Q$item\E$/,@clonecodes)) {
10274: $instcoderegexp .= '('.$codedefaults{$item}.')';
10275: } else {
10276: $instcoderegexp .= $codedefaults{$item};
10277: }
10278: }
10279: $instcoderegexp .= '$';
10280: my (@from,@to);
10281: eval {
10282: (@from) = ($clonefromcode =~ /$instcoderegexp/);
10283: (@to) = ($clonetocode =~ /$instcoderegexp/);
10284: };
10285: if ((@from > 0) && (@to > 0)) {
10286: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10287: if (!@diffs) {
10288: $canclone = 1;
10289: }
10290: }
10291: }
10292: return $canclone;
10293: }
10294:
1.679 raeburn 10295: # ------------------------------------------------------- Course Group routines
10296:
10297: sub get_coursegroups {
1.809 raeburn 10298: my ($cdom,$cnum,$group,$namespace) = @_;
10299: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 10300: }
10301:
1.679 raeburn 10302: sub modify_coursegroup {
10303: my ($cdom,$cnum,$groupsettings) = @_;
10304: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10305: }
10306:
1.809 raeburn 10307: sub toggle_coursegroup_status {
10308: my ($cdom,$cnum,$group,$action) = @_;
10309: my ($from_namespace,$to_namespace);
10310: if ($action eq 'delete') {
10311: $from_namespace = 'coursegroups';
10312: $to_namespace = 'deleted_groups';
10313: } else {
10314: $from_namespace = 'deleted_groups';
10315: $to_namespace = 'coursegroups';
10316: }
10317: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 10318: if (my $tmp = &error(%curr_group)) {
10319: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10320: return ('read error',$tmp);
10321: } else {
10322: my %savedsettings = %curr_group;
1.809 raeburn 10323: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 10324: my $deloutcome;
10325: if ($result eq 'ok') {
1.809 raeburn 10326: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 10327: } else {
10328: return ('write error',$result);
10329: }
10330: if ($deloutcome eq 'ok') {
10331: return 'ok';
10332: } else {
10333: return ('delete error',$deloutcome);
10334: }
10335: }
10336: }
10337:
1.679 raeburn 10338: sub modify_group_roles {
1.957 raeburn 10339: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 10340: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10341: my $role = 'gr/'.&escape($userprivs);
10342: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 10343: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 10344: if ($result eq 'ok') {
10345: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10346: }
1.679 raeburn 10347: return $result;
10348: }
10349:
10350: sub modify_coursegroup_membership {
10351: my ($cdom,$cnum,$membership) = @_;
10352: my $result = &put('groupmembership',$membership,$cdom,$cnum);
10353: return $result;
10354: }
10355:
1.682 raeburn 10356: sub get_active_groups {
10357: my ($udom,$uname,$cdom,$cnum) = @_;
10358: my $now = time;
10359: my %groups = ();
10360: foreach my $key (keys(%env)) {
1.811 albertel 10361: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 10362: my ($start,$end) = split(/\./,$env{$key});
10363: if (($end!=0) && ($end<$now)) { next; }
10364: if (($start!=0) && ($start>$now)) { next; }
10365: if ($1 eq $cdom && $2 eq $cnum) {
10366: $groups{$3} = $env{$key} ;
10367: }
10368: }
10369: }
10370: return %groups;
10371: }
10372:
1.683 raeburn 10373: sub get_group_membership {
10374: my ($cdom,$cnum,$group) = @_;
10375: return(&dump('groupmembership',$cdom,$cnum,$group));
10376: }
10377:
10378: sub get_users_groups {
10379: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 10380: my @usersgroups;
1.683 raeburn 10381: my $cachetime=1800;
10382:
10383: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 10384: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10385: if (defined($cached)) {
1.734 albertel 10386: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 10387: } else {
10388: $grouplist = '';
1.816 raeburn 10389: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 10390: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 10391: my $access_end = $env{'course.'.$courseid.
10392: '.default_enrollment_end_date'};
10393: my $now = time;
10394: foreach my $key (keys(%roleshash)) {
10395: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10396: my $group = $1;
10397: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10398: my $start = $2;
10399: my $end = $1;
10400: if ($start == -1) { next; } # deleted from group
10401: if (($start!=0) && ($start>$now)) { next; }
10402: if (($end!=0) && ($end<$now)) {
10403: if ($access_end && $access_end < $now) {
10404: if ($access_end - $end < 86400) {
10405: push(@usersgroups,$group);
1.733 raeburn 10406: }
10407: }
1.817 raeburn 10408: next;
1.733 raeburn 10409: }
1.817 raeburn 10410: push(@usersgroups,$group);
1.683 raeburn 10411: }
10412: }
10413: }
1.817 raeburn 10414: @usersgroups = &sort_course_groups($courseid,@usersgroups);
10415: $grouplist = join(':',@usersgroups);
10416: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 10417: }
1.733 raeburn 10418: return @usersgroups;
1.683 raeburn 10419: }
10420:
10421: sub devalidate_getgroups_cache {
10422: my ($udom,$uname,$cdom,$cnum)=@_;
10423: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 10424:
1.683 raeburn 10425: my $hashid="$udom:$uname:$courseid";
10426: &devalidate_cache_new('getgroups',$hashid);
10427: }
10428:
1.12 www 10429: # ------------------------------------------------------------------ Plain Text
10430:
10431: sub plaintext {
1.988 raeburn 10432: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 10433: if ($short =~ m{^cr/}) {
1.758 albertel 10434: return (split('/',$short))[-1];
10435: }
1.742 raeburn 10436: if (!defined($cid)) {
10437: $cid = $env{'request.course.id'};
10438: }
10439: my %rolenames = (
1.1008 raeburn 10440: Course => 'std',
10441: Community => 'alt1',
1.742 raeburn 10442: );
1.1037 raeburn 10443: if ($cid ne '') {
10444: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10445: unless ($forcedefault) {
10446: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
10447: &Apache::lonlocal::mt_escape(\$roletext);
10448: return &Apache::lonlocal::mt($roletext);
10449: }
10450: }
10451: }
10452: if ((defined($type)) && (defined($rolenames{$type})) &&
10453: (defined($rolenames{$type})) &&
10454: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 10455: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 10456: } elsif ($cid ne '') {
10457: my $crstype = $env{'course.'.$cid.'.type'};
10458: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10459: (defined($prp{$short}{$rolenames{$crstype}}))) {
10460: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10461: }
1.742 raeburn 10462: }
1.1037 raeburn 10463: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 10464: }
10465:
10466: # ----------------------------------------------------------------- Assign Role
10467:
10468: sub assignrole {
1.957 raeburn 10469: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10470: $context)=@_;
1.1172.2.146. .22(raeb 10471:-24): my ($mrole,$rolelogcontext);
1.21 www 10472: if ($role =~ /^cr\//) {
1.393 www 10473: my $cwosec=$url;
1.811 albertel 10474: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 10475: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 10476: my $refused = 1;
10477: if ($context eq 'requestcourses') {
10478: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10479: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10480: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10481: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10482: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10483: if ($crsenv{'internal.courseowner'} eq
10484: $env{'user.name'}.':'.$env{'user.domain'}) {
10485: $refused = '';
10486: }
10487: }
10488: }
10489: }
10490: }
10491: if ($refused) {
10492: &logthis('Refused custom assignrole: '.
10493: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10494: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10495: return 'refused';
10496: }
1.104 www 10497: }
1.21 www 10498: $mrole='cr';
1.678 raeburn 10499: } elsif ($role =~ /^gr\//) {
10500: my $cwogrp=$url;
1.811 albertel 10501: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 10502: unless (&allowed('mdg',$cwogrp)) {
10503: &logthis('Refused group assignrole: '.
10504: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10505: $env{'user.name'}.' at '.$env{'user.domain'});
10506: return 'refused';
10507: }
10508: $mrole='gr';
1.21 www 10509: } else {
1.82 www 10510: my $cwosec=$url;
1.811 albertel 10511: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 10512: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10513: my $refused;
10514: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
10515: if (!(&allowed('c'.$role,$url))) {
10516: $refused = 1;
10517: }
10518: } else {
10519: $refused = 1;
10520: }
1.947 raeburn 10521: if ($refused) {
1.1045 raeburn 10522: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10523: if (!$selfenroll && $context eq 'course') {
10524: my %crsenv;
10525: if ($role eq 'cc' || $role eq 'co') {
10526: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10527: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10528: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10529: if ($crsenv{'internal.courseowner'} eq
10530: $env{'user.name'}.':'.$env{'user.domain'}) {
10531: $refused = '';
10532: }
10533: }
10534: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
10535: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10536: if ($crsenv{'internal.courseowner'} eq
10537: $env{'user.name'}.':'.$env{'user.domain'}) {
10538: $refused = '';
10539: }
10540: }
10541: }
10542: }
1.1172.2.146. .13(raeb 10543:-23): } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10544:-23): if ($role eq 'st') {
10545:-23): $refused = '';
10546:-23): } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10547:-23): $refused = '';
10548:-23): }
1.1017 raeburn 10549: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 10550: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 10551: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 10552: my $wrongcc;
10553: if ($cnum =~ /^$match_community$/) {
10554: $wrongcc = 1 if ($role eq 'cc');
10555: } else {
10556: $wrongcc = 1 if ($role eq 'co');
10557: }
10558: unless ($wrongcc) {
10559: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10560: if ($crsenv{'internal.courseowner'} eq
10561: $env{'user.name'}.':'.$env{'user.domain'}) {
10562: $refused = '';
10563: }
1.1017 raeburn 10564: }
10565: }
1.1172.2.9 raeburn 10566: } elsif ($context eq 'requestauthor') {
10567: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
10568: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10569: if ($env{'environment.requestauthor'} eq 'automatic') {
10570: $refused = '';
10571: } else {
10572: my %domdefaults = &get_domain_defaults($udom);
10573: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10574: my $checkbystatus;
10575: if ($env{'user.adv'}) {
10576: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10577: if ($disposition eq 'automatic') {
10578: $refused = '';
10579: } elsif ($disposition eq '') {
10580: $checkbystatus = 1;
10581: }
10582: } else {
10583: $checkbystatus = 1;
10584: }
10585: if ($checkbystatus) {
10586: if ($env{'environment.inststatus'}) {
10587: my @inststatuses = split(/,/,$env{'environment.inststatus'});
10588: foreach my $type (@inststatuses) {
10589: if (($type ne '') &&
10590: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10591: $refused = '';
10592: }
10593: }
10594: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10595: $refused = '';
10596: }
10597: }
10598: }
10599: }
10600: }
1.1172.2.146. .22(raeb 10601:-24): } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10602:-24): if ($url =~ m{^/($match_domain)/($match_username)$}) {
10603:-24): my ($audom,$auname) = ($1,$2);
10604:-24): if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10605:-24): ($env{"environment.internal.manager.$url"})) {
10606:-24): $refused = '';
10607:-24): $rolelogcontext = 'coauthor';
10608:-24): }
10609:-24): }
1.1017 raeburn 10610: }
10611: if ($refused) {
1.947 raeburn 10612: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10613: ' '.$role.' '.$end.' '.$start.' by '.
10614: $env{'user.name'}.' at '.$env{'user.domain'});
10615: return 'refused';
10616: }
1.932 raeburn 10617: }
1.1131 raeburn 10618: } elsif ($role eq 'au') {
10619: if ($url ne '/'.$udom.'/') {
10620: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10621: ' to assign author role for '.$uname.':'.$udom.
10622: ' in domain: '.$url.' refused (wrong domain).');
10623: return 'refused';
10624: }
1.104 www 10625: }
1.21 www 10626: $mrole=$role;
10627: }
1.620 albertel 10628: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 10629: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 10630: if ($end) { $command.='_'.$end; }
1.21 www 10631: if ($start) {
10632: if ($end) {
1.81 www 10633: $command.='_'.$start;
1.21 www 10634: } else {
1.81 www 10635: $command.='_0_'.$start;
1.21 www 10636: }
10637: }
1.739 raeburn 10638: my $origstart = $start;
10639: my $origend = $end;
1.957 raeburn 10640: my $delflag;
1.357 www 10641: # actually delete
10642: if ($deleteflag) {
1.373 www 10643: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 10644: # modify command to delete the role
1.620 albertel 10645: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 10646: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 10647: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 10648: # set start and finish to negative values for userrolelog
10649: $start=-1;
10650: $end=-1;
1.957 raeburn 10651: $delflag = 1;
1.357 www 10652: }
10653: }
10654: # send command
1.349 www 10655: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 10656: # log new user role if status is ok
1.349 www 10657: if ($answer eq 'ok') {
1.663 raeburn 10658: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1172.2.9 raeburn 10659: if (($role eq 'cc') || ($role eq 'in') ||
10660: ($role eq 'ep') || ($role eq 'ad') ||
10661: ($role eq 'ta') || ($role eq 'st') ||
10662: ($role=~/^cr/) || ($role eq 'gr') ||
10663: ($role eq 'co')) {
1.1172.2.13 raeburn 10664: # for course roles, perform group memberships changes triggered by role change.
10665: unless ($role =~ /^gr/) {
10666: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10667: $origstart,$selfenroll,$context);
10668: }
1.1172.2.9 raeburn 10669: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10670: $selfenroll,$context);
10671: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1172.2.90 raeburn 10672: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10673: ($role eq 'da')) {
1.1172.2.9 raeburn 10674: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10675: $context);
10676: } elsif (($role eq 'ca') || ($role eq 'aa')) {
1.1172.2.146. .22(raeb 10677:-24): if ($rolelogcontext eq '') {
10678:-24): $rolelogcontext = $context;
10679:-24): }
1.1172.2.9 raeburn 10680: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
1.1172.2.146. .22(raeb 10681:-24): $rolelogcontext);
1.1172.2.9 raeburn 10682: }
1.1053 raeburn 10683: if ($role eq 'cc') {
10684: &autoupdate_coowners($url,$end,$start,$uname,$udom);
10685: }
1.349 www 10686: }
10687: return $answer;
1.169 harris41 10688: }
10689:
1.1053 raeburn 10690: sub autoupdate_coowners {
10691: my ($url,$end,$start,$uname,$udom) = @_;
10692: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10693: if (($cdom ne '') && ($cnum ne '')) {
10694: my $now = time;
10695: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10696: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10697: my %coursehash = &coursedescription($cdom.'_'.$cnum);
10698: my $instcode = $coursehash{'internal.coursecode'};
1.1172.2.141 raeburn 10699: my $xlists = $coursehash{'internal.crosslistings'};
1.1053 raeburn 10700: if ($instcode ne '') {
1.1056 raeburn 10701: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10702: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 10703: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 10704: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
1.1172.2.141 raeburn 10705: unless ($result eq 'valid') {
10706: if ($xlists ne '') {
10707: foreach my $xlist (split(',',$xlists)) {
10708: my ($inst_crosslist,$lcsec) = split(':',$xlist);
10709: $result =
10710: &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10711: $inst_crosslist,$uname.':'.$udom);
10712: last if ($result eq 'valid');
10713: }
10714: }
10715: }
1.1056 raeburn 10716: if ($result eq 'valid') {
10717: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 10718: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10719: push(@newcoowners,$coowner);
10720: }
10721: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10722: push(@newcoowners,$uname.':'.$udom);
10723: }
10724: @newcoowners = sort(@newcoowners);
10725: } else {
10726: push(@newcoowners,$uname.':'.$udom);
10727: }
1.1172.2.141 raeburn 10728: } elsif ($coursehash{'internal.co-owners'}) {
10729: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10730: unless ($coowner eq $uname.':'.$udom) {
10731: push(@newcoowners,$coowner);
1.1053 raeburn 10732: }
10733: }
1.1172.2.141 raeburn 10734: unless (@newcoowners > 0) {
10735: $delcoowners = 1;
10736: $coowners = '';
10737: }
1.1053 raeburn 10738: }
10739: if (@newcoowners || $delcoowners) {
10740: &store_coowners($cdom,$cnum,$coursehash{'home'},
10741: $delcoowners,@newcoowners);
10742: }
10743: }
10744: }
10745: }
10746: }
10747: }
10748: }
10749:
10750: sub store_coowners {
10751: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10752: my $cid = $cdom.'_'.$cnum;
10753: my ($coowners,$delresult,$putresult);
10754: if (@newcoowners) {
10755: $coowners = join(',',@newcoowners);
10756: my %coownershash = (
10757: 'internal.co-owners' => $coowners,
10758: );
10759: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10760: if ($putresult eq 'ok') {
10761: if ($env{'course.'.$cid.'.num'} eq $cnum) {
10762: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10763: }
10764: }
10765: }
10766: if ($delcoowners) {
10767: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10768: if ($delresult eq 'ok') {
10769: if ($env{'course.'.$cid.'.internal.co-owners'}) {
10770: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10771: }
10772: }
10773: }
10774: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10775: my %crsinfo =
10776: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10777: if (ref($crsinfo{$cid}) eq 'HASH') {
10778: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10779: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10780: }
10781: }
10782: }
10783:
1.169 harris41 10784: # -------------------------------------------------- Modify user authentication
1.197 www 10785: # Overrides without validation
10786:
1.169 harris41 10787: sub modifyuserauth {
10788: my ($udom,$uname,$umode,$upass)=@_;
10789: my $uhome=&homeserver($uname,$udom);
1.1172.2.115 raeburn 10790: my $allowed;
10791: if (&allowed('mau',$udom)) {
10792: $allowed = 1;
10793: } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10794: ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10795: (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10796: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10797: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10798: if (($cdom ne '') && ($cnum ne '')) {
10799: my $is_owner = &is_course_owner($cdom,$cnum);
10800: if ($is_owner) {
10801: $allowed = 1;
10802: }
10803: }
10804: }
10805: unless ($allowed) { return 'refused'; }
1.197 www 10806: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 10807: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10808: ' in domain '.$env{'request.role.domain'});
1.169 harris41 10809: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10810: &escape($upass),$uhome);
1.1172.2.134 raeburn 10811: my $ip = &get_requestor_ip();
1.620 albertel 10812: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 10813: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
1.1172.2.134 raeburn 10814: '(Remote '.$ip.'): '.$reply);
1.197 www 10815: &log($udom,,$uname,$uhome,
1.620 albertel 10816: 'Authentication changed by '.$env{'user.domain'}.', '.
10817: $env{'user.name'}.', '.$umode.
1.1172.2.134 raeburn 10818: '(Remote '.$ip.'): '.$reply);
1.169 harris41 10819: unless ($reply eq 'ok') {
1.197 www 10820: &logthis('Authentication mode error: '.$reply);
1.169 harris41 10821: return 'error: '.$reply;
10822: }
1.170 harris41 10823: return 'ok';
1.80 www 10824: }
10825:
1.81 www 10826: # --------------------------------------------------------------- Modify a user
1.80 www 10827:
1.81 www 10828: sub modifyuser {
1.206 matthew 10829: my ($udom, $uname, $uid,
10830: $umode, $upass, $first,
10831: $middle, $last, $gene,
1.1058 raeburn 10832: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 10833: $udom= &LONCAPA::clean_domain($udom);
10834: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 10835: my $showcandelete = 'none';
10836: if (ref($candelete) eq 'ARRAY') {
10837: if (@{$candelete} > 0) {
10838: $showcandelete = join(', ',@{$candelete});
10839: }
10840: }
1.81 www 10841: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 10842: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 10843: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 10844: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10845: ' desiredhome not specified').
1.620 albertel 10846: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10847: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 10848: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 10849: my $newuser;
10850: if ($uhome eq 'no_host') {
10851: $newuser = 1;
1.1172.2.146. .13(raeb 10852:-23): unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10853:-23): ($umode eq 'lti')) {
10854:-23): return 'error: more information needed to create new user';
10855:-23): }
1.1075 raeburn 10856: }
1.80 www 10857: # ----------------------------------------------------------------- Create User
1.406 albertel 10858: if (($uhome eq 'no_host') &&
1.1172.2.146. .13(raeb 10859:-23): (($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
1.80 www 10860: my $unhome='';
1.844 albertel 10861: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 10862: $unhome = $desiredhome;
1.620 albertel 10863: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10864: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 10865: } else { # load balancing routine for determining $unhome
1.81 www 10866: my $loadm=10000000;
1.841 albertel 10867: my %servers = &get_servers($udom,'library');
10868: foreach my $tryserver (keys(%servers)) {
10869: my $answer=reply('load',$tryserver);
10870: if (($answer=~/\d+/) && ($answer<$loadm)) {
10871: $loadm=$answer;
10872: $unhome=$tryserver;
10873: }
1.80 www 10874: }
10875: }
10876: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 10877: return 'error: unable to find a home server for '.$uname.
10878: ' in domain '.$udom;
1.80 www 10879: }
10880: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10881: &escape($upass),$unhome);
10882: unless ($reply eq 'ok') {
10883: return 'error: '.$reply;
10884: }
1.230 stredwic 10885: $uhome=&homeserver($uname,$udom,'true');
1.80 www 10886: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 10887: return 'error: unable verify users home machine.';
1.80 www 10888: }
1.209 matthew 10889: } # End of creation of new user
1.80 www 10890: # ---------------------------------------------------------------------- Add ID
10891: if ($uid) {
10892: $uid=~tr/A-Z/a-z/;
10893: my %uidhash=&idrget($udom,$uname);
1.196 www 10894: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
10895: && (!$forceid)) {
1.80 www 10896: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 10897: return 'error: user id "'.$uid.'" does not match '.
10898: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 10899: }
10900: } else {
10901: &idput($udom,($uname => $uid));
10902: }
10903: }
10904: # -------------------------------------------------------------- Add names, etc
1.313 matthew 10905: my @tmp=&get('environment',
1.899 raeburn 10906: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 10907: 'permanentemail','inststatus'],
1.134 albertel 10908: $udom,$uname);
1.1075 raeburn 10909: my (%names,%oldnames);
1.313 matthew 10910: if ($tmp[0] =~ m/^error:.*/) {
10911: %names=();
10912: } else {
10913: %names = @tmp;
1.1075 raeburn 10914: %oldnames = %names;
1.313 matthew 10915: }
1.388 www 10916: #
1.1058 raeburn 10917: # If name, email and/or uid are blank (e.g., because an uploaded file
10918: # of users did not contain them), do not overwrite existing values
10919: # unless field is in $candelete array ref.
10920: #
10921:
10922: my @fields = ('firstname','middlename','lastname','generation',
10923: 'permanentemail','id');
10924: my %newvalues;
10925: if (ref($candelete) eq 'ARRAY') {
10926: foreach my $field (@fields) {
10927: if (grep(/^\Q$field\E$/,@{$candelete})) {
10928: if ($field eq 'firstname') {
10929: $names{$field} = $first;
10930: } elsif ($field eq 'middlename') {
10931: $names{$field} = $middle;
10932: } elsif ($field eq 'lastname') {
10933: $names{$field} = $last;
10934: } elsif ($field eq 'generation') {
10935: $names{$field} = $gene;
10936: } elsif ($field eq 'permanentemail') {
10937: $names{$field} = $email;
10938: } elsif ($field eq 'id') {
10939: $names{$field} = $uid;
10940: }
10941: }
10942: }
10943: }
1.388 www 10944: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 10945: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 10946: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 10947: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 10948: if ($email) {
10949: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 10950: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 10951: }
1.899 raeburn 10952: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 10953: if (defined($inststatus)) {
10954: $names{'inststatus'} = '';
10955: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10956: if (ref($usertypes) eq 'HASH') {
10957: my @okstatuses;
10958: foreach my $item (split(/:/,$inststatus)) {
10959: if (defined($usertypes->{$item})) {
10960: push(@okstatuses,$item);
10961: }
10962: }
10963: if (@okstatuses) {
10964: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10965: }
10966: }
10967: }
1.1075 raeburn 10968: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 10969: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 10970: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 10971: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10972: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10973: } else {
10974: $logmsg .= ' during self creation';
10975: }
1.1075 raeburn 10976: my $changed;
10977: if ($newuser) {
10978: $changed = 1;
10979: } else {
10980: foreach my $field (@fields) {
10981: if ($names{$field} ne $oldnames{$field}) {
10982: $changed = 1;
10983: last;
10984: }
10985: }
10986: }
10987: unless ($changed) {
10988: $logmsg = 'No changes in user information needed for: '.$logmsg;
10989: &logthis($logmsg);
10990: return 'ok';
10991: }
10992: my $reply = &put('environment', \%names, $udom,$uname);
10993: if ($reply ne 'ok') {
10994: return 'error: '.$reply;
10995: }
1.1087 raeburn 10996: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10997: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10998: }
1.1075 raeburn 10999: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
11000: &devalidate_cache_new('namescache',$uname.':'.$udom);
11001: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 11002: &logthis($logmsg);
1.134 albertel 11003: return 'ok';
1.80 www 11004: }
11005:
1.81 www 11006: # -------------------------------------------------------------- Modify student
1.80 www 11007:
1.81 www 11008: sub modifystudent {
11009: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 11010: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1172.2.76 raeburn 11011: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 11012: if (!$cid) {
1.620 albertel 11013: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11014: return 'not_in_class';
11015: }
1.80 www 11016: }
11017: # --------------------------------------------------------------- Make the user
1.81 www 11018: my $reply=&modifyuser
1.209 matthew 11019: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 11020: $desiredhome,$email,$inststatus);
1.80 www 11021: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 11022: # This will cause &modify_student_enrollment to get the uid from the
1.1172.2.31 raeburn 11023: # student's environment
1.297 matthew 11024: $uid = undef if (!$forceid);
1.455 albertel 11025: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1172.2.19 raeburn 11026: $gene,$usec,$end,$start,$type,$locktype,
1.1172.2.76 raeburn 11027: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 11028: return $reply;
11029: }
11030:
11031: sub modify_student_enrollment {
1.1172.2.23 raeburn 11032: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1172.2.76 raeburn 11033: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 11034: my ($cdom,$cnum,$chome);
11035: if (!$cid) {
1.620 albertel 11036: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 11037: return 'not_in_class';
11038: }
1.620 albertel 11039: $cdom=$env{'course.'.$cid.'.domain'};
11040: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 11041: } else {
11042: ($cdom,$cnum)=split(/_/,$cid);
11043: }
1.620 albertel 11044: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 11045: if (!$chome) {
1.457 raeburn 11046: $chome=&homeserver($cnum,$cdom);
1.297 matthew 11047: }
1.455 albertel 11048: if (!$chome) { return 'unknown_course'; }
1.297 matthew 11049: # Make sure the user exists
1.81 www 11050: my $uhome=&homeserver($uname,$udom);
11051: if (($uhome eq '') || ($uhome eq 'no_host')) {
11052: return 'error: no such user';
11053: }
1.297 matthew 11054: # Get student data if we were not given enough information
11055: if (!defined($first) || $first eq '' ||
11056: !defined($last) || $last eq '' ||
11057: !defined($uid) || $uid eq '' ||
11058: !defined($middle) || $middle eq '' ||
11059: !defined($gene) || $gene eq '') {
1.294 matthew 11060: # They did not supply us with enough data to enroll the student, so
11061: # we need to pick up more information.
1.297 matthew 11062: my %tmp = &get('environment',
1.294 matthew 11063: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 11064: ,$udom,$uname);
11065:
1.800 albertel 11066: #foreach my $key (keys(%tmp)) {
11067: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 11068: #}
1.294 matthew 11069: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
11070: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11071: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 11072: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 11073: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
11074: }
1.556 albertel 11075: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 11076: my $user = "$uname:$udom";
11077: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 11078: my $reply=cput('classlist',
1.1148 raeburn 11079: {$user =>
1.1172.2.76 raeburn 11080: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 11081: $cdom,$cnum);
1.1148 raeburn 11082: if (($reply eq 'ok') || ($reply eq 'delayed')) {
11083: &devalidate_getsection_cache($udom,$uname,$cid);
11084: } else {
1.81 www 11085: return 'error: '.$reply;
11086: }
1.297 matthew 11087: # Add student role to user
1.83 www 11088: my $uurl='/'.$cid;
1.81 www 11089: $uurl=~s/\_/\//g;
11090: if ($usec) {
11091: $uurl.='/'.$usec;
11092: }
1.1148 raeburn 11093: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11094: $selfenroll,$context);
11095: if ($result ne 'ok') {
11096: if ($old_entry{$user} ne '') {
11097: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11098: } else {
11099: $reply = &del('classlist',[$user],$cdom,$cnum);
11100: }
11101: }
11102: return $result;
1.21 www 11103: }
11104:
1.556 albertel 11105: sub format_name {
11106: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11107: my $name;
11108: if ($first ne 'lastname') {
11109: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11110: } else {
11111: if ($lastname=~/\S/) {
11112: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11113: $name=~s/\s+,/,/;
11114: } else {
11115: $name.= $firstname.' '.$middlename.' '.$generation;
11116: }
11117: }
11118: $name=~s/^\s+//;
11119: $name=~s/\s+$//;
11120: $name=~s/\s+/ /g;
11121: return $name;
11122: }
11123:
1.84 www 11124: # ------------------------------------------------- Write to course preferences
11125:
11126: sub writecoursepref {
11127: my ($courseid,%prefs)=@_;
11128: $courseid=~s/^\///;
11129: $courseid=~s/\_/\//g;
11130: my ($cdomain,$cnum)=split(/\//,$courseid);
11131: my $chome=homeserver($cnum,$cdomain);
11132: if (($chome eq '') || ($chome eq 'no_host')) {
11133: return 'error: no such course';
11134: }
11135: my $cstring='';
1.800 albertel 11136: foreach my $pref (keys(%prefs)) {
11137: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 11138: }
1.84 www 11139: $cstring=~s/\&$//;
11140: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11141: }
11142:
11143: # ---------------------------------------------------------- Make/modify course
11144:
11145: sub createcourse {
1.741 raeburn 11146: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1172.2.146. .1(raebu 11147:22): $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
1.84 www 11148: $url=&declutter($url);
11149: my $cid='';
1.1028 raeburn 11150: if ($context eq 'requestcourses') {
11151: my $can_create = 0;
11152: my ($ownername,$ownerdom) = split(':',$course_owner);
11153: if ($udom eq $ownerdom) {
1.1172.2.146. .1(raebu 11154:22): my $reload;
11155:22): if (($callercontext eq 'auto') &&
11156:22): ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11157:22): $reload = 'reload';
11158:22): }
11159:22): if (&usertools_access($ownername,$ownerdom,$category,$reload,
1.1028 raeburn 11160: $context)) {
11161: $can_create = 1;
11162: }
11163: } else {
11164: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11165: $category);
11166: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11167: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11168: if (@curr > 0) {
11169: my @options = qw(approval validate autolimit);
11170: my $optregex = join('|',@options);
11171: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11172: $can_create = 1;
11173: }
11174: }
11175: }
11176: }
11177: if ($can_create) {
11178: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11179: unless (&allowed('ccc',$udom)) {
11180: return 'refused';
11181: }
1.1017 raeburn 11182: }
11183: } else {
11184: return 'refused';
11185: }
1.1028 raeburn 11186: } elsif (!&allowed('ccc',$udom)) {
11187: return 'refused';
1.84 www 11188: }
1.1011 raeburn 11189: # --------------------------------------------------------------- Get Unique ID
11190: my $uname;
11191: if ($cnum =~ /^$match_courseid$/) {
11192: my $chome=&homeserver($cnum,$udom,'true');
11193: if (($chome eq '') || ($chome eq 'no_host')) {
11194: $uname = $cnum;
11195: } else {
1.1038 raeburn 11196: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11197: }
11198: } else {
1.1038 raeburn 11199: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 11200: }
11201: return $uname if ($uname =~ /^error/);
11202: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 11203: if (!defined($course_server)) {
11204: if (defined(&domain($udom,'primary'))) {
11205: $course_server = &domain($udom,'primary');
11206: } else {
11207: $course_server = $env{'user.home'};
11208: }
11209: }
11210: my %host_servers =
11211: &Apache::lonnet::get_servers($udom,'library');
11212: unless ($host_servers{$course_server}) {
11213: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 11214: }
1.84 www 11215: # ------------------------------------------------------------- Make the course
11216: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 11217: $course_server);
1.84 www 11218: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 11219: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 11220: if (($uhome eq '') || ($uhome eq 'no_host')) {
11221: return 'error: no such course';
11222: }
1.271 www 11223: # ----------------------------------------------------------------- Course made
1.516 raeburn 11224: # log existence
1.1029 raeburn 11225: my $now = time;
1.918 raeburn 11226: my $newcourse = {
11227: $udom.'_'.$uname => {
1.921 raeburn 11228: description => $description,
11229: inst_code => $inst_code,
11230: owner => $course_owner,
11231: type => $crstype,
1.1029 raeburn 11232: creator => $env{'user.name'}.':'.
11233: $env{'user.domain'},
11234: created => $now,
11235: context => $context,
1.918 raeburn 11236: },
11237: };
1.921 raeburn 11238: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 11239: # set toplevel url
1.271 www 11240: my $topurl=$url;
11241: unless ($nonstandard) {
11242: # ------------------------------------------ For standard courses, make top url
11243: my $mapurl=&clutter($url);
1.278 www 11244: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 11245: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 11246: <map>
11247: <resource id="1" type="start"></resource>
11248: <resource id="2" src="$mapurl"></resource>
11249: <resource id="3" type="finish"></resource>
11250: <link index="1" from="1" to="2"></link>
11251: <link index="2" from="2" to="3"></link>
11252: </map>
11253: ENDINITMAP
11254: $topurl=&declutter(
1.638 albertel 11255: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 11256: );
11257: }
11258: # ----------------------------------------------------------- Write preferences
1.84 www 11259: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 11260: ('description' => $description,
11261: 'url' => $topurl,
11262: 'internal.creator' => $env{'user.name'}.':'.
11263: $env{'user.domain'},
11264: 'internal.created' => $now,
11265: 'internal.creationcontext' => $context)
11266: );
1.84 www 11267: return '/'.$udom.'/'.$uname;
11268: }
11269:
1.1011 raeburn 11270: # ------------------------------------------------------------------- Create ID
11271: sub generate_coursenum {
1.1038 raeburn 11272: my ($udom,$crstype) = @_;
1.1011 raeburn 11273: my $domdesc = &domain($udom);
11274: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 11275: my $first;
11276: if ($crstype eq 'Community') {
11277: $first = '0';
11278: } else {
11279: $first = int(1+rand(9));
11280: }
11281: my $uname=$first.
1.1011 raeburn 11282: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11283: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11284: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11285: # ----------------------------------------------- Make sure that does not exist
11286: my $uhome=&homeserver($uname,$udom,'true');
11287: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 11288: if ($crstype eq 'Community') {
11289: $first = '0';
11290: } else {
11291: $first = int(1+rand(9));
11292: }
11293: $uname=$first.
1.1011 raeburn 11294: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11295: substr($$.time,0,5).unpack("H8",pack("I32",time)).
11296: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11297: $uhome=&homeserver($uname,$udom,'true');
11298: unless (($uhome eq '') || ($uhome eq 'no_host')) {
11299: return 'error: unable to generate unique course-ID';
11300: }
11301: }
11302: return $uname;
11303: }
11304:
1.813 albertel 11305: sub is_course {
1.1167 droeschl 11306: my ($cdom, $cnum) = scalar(@_) == 1 ?
11307: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
1.1172.2.95 raeburn 11308: return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11309: my $uhome=&homeserver($cnum,$cdom);
11310: my $iscourse;
11311: if (grep { $_ eq $uhome } current_machine_ids()) {
11312: $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11313: } else {
11314: my $hashid = $cdom.':'.$cnum;
11315: ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11316: unless (defined($cached)) {
11317: my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11318: $cnum,undef,undef,'.');
11319: $iscourse = 0;
11320: if (exists($courses{$cdom.'_'.$cnum})) {
11321: $iscourse = 1;
11322: }
11323: &do_cache_new('iscourse',$hashid,$iscourse,3600);
11324: }
11325: }
11326: return unless($iscourse);
1.1167 droeschl 11327: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 11328: }
11329:
1.1015 raeburn 11330: sub store_userdata {
1.1172.2.146. .27(raeb 11331:-25): my ($storehash,$datakey,$namespace,$udom,$uname,$ip) = @_;
1.1013 raeburn 11332: my $result;
1.1016 raeburn 11333: if ($datakey ne '') {
1.1013 raeburn 11334: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 11335: if ($udom eq '' || $uname eq '') {
11336: $udom = $env{'user.domain'};
11337: $uname = $env{'user.name'};
11338: }
11339: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 11340: if (($uhome eq '') || ($uhome eq 'no_host')) {
11341: $result = 'error: no_host';
11342: } else {
1.1172.2.146. .27(raeb 11343:-25): if ($ip ne '') {
11344:-25): $storehash->{'ip'} = $ip;
11345:-25): } else {
11346:-25): $storehash->{'ip'} = &get_requestor_ip();
11347:-25): }
1.1013 raeburn 11348: $storehash->{'host'} = $perlvar{'lonHostID'};
11349:
11350: my $namevalue='';
11351: foreach my $key (keys(%{$storehash})) {
11352: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11353: }
11354: $namevalue=~s/\&$//;
1.1172.2.26 raeburn 11355: unless ($namespace eq 'courserequests') {
11356: $datakey = &escape($datakey);
11357: }
1.1105 raeburn 11358: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
11359: $namevalue,$uhome);
1.1013 raeburn 11360: }
11361: } else {
11362: $result = 'error: data to store was not a hash reference';
11363: }
11364: } else {
11365: $result= 'error: invalid requestkey';
11366: }
11367: return $result;
11368: }
11369:
1.21 www 11370: # ---------------------------------------------------------- Assign Custom Role
11371:
11372: sub assigncustomrole {
1.957 raeburn 11373: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11374: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 11375: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 11376: }
11377:
11378: # ----------------------------------------------------------------- Revoke Role
11379:
11380: sub revokerole {
1.957 raeburn 11381: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11382: my $now=time;
1.965 raeburn 11383: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 11384: }
11385:
11386: # ---------------------------------------------------------- Revoke Custom Role
11387:
11388: sub revokecustomrole {
1.957 raeburn 11389: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 11390: my $now=time;
1.357 www 11391: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 11392: $deleteflag,$selfenroll,$context);
1.17 www 11393: }
11394:
1.533 banghart 11395: # ------------------------------------------------------------ Disk usage
1.535 albertel 11396: sub diskusage {
1.955 raeburn 11397: my ($udom,$uname,$directorypath,$getpropath)=@_;
11398: $directorypath =~ s/\/$//;
11399: my $listing=&reply('du2:'.&escape($directorypath).':'
11400: .&escape($getpropath).':'.&escape($uname).':'
11401: .&escape($udom),homeserver($uname,$udom));
11402: if ($listing eq 'unknown_cmd') {
11403: if ($getpropath) {
11404: $directorypath = &propath($udom,$uname).'/'.$directorypath;
11405: }
11406: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11407: }
1.514 albertel 11408: return $listing;
1.512 banghart 11409: }
11410:
1.566 banghart 11411: sub is_locked {
1.1096 raeburn 11412: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 11413: my @check;
11414: my $is_locked;
1.1093 raeburn 11415: push (@check,$file_name);
1.613 albertel 11416: my %locked = &get('file_permissions',\@check,
1.620 albertel 11417: $env{'user.domain'},$env{'user.name'});
1.615 albertel 11418: my ($tmp)=keys(%locked);
11419: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 11420:
1.566 banghart 11421: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 11422: $is_locked = 'false';
11423: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 11424: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 11425: $is_locked = 'true';
1.1096 raeburn 11426: if (ref($which) eq 'ARRAY') {
11427: push(@{$which},$entry);
11428: } else {
11429: last;
11430: }
1.745 raeburn 11431: }
11432: }
1.566 banghart 11433: } else {
11434: $is_locked = 'false';
11435: }
1.1093 raeburn 11436: return $is_locked;
1.566 banghart 11437: }
11438:
1.759 albertel 11439: sub declutter_portfile {
11440: my ($file) = @_;
1.833 albertel 11441: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 11442: return $file;
11443: }
11444:
1.559 banghart 11445: # ------------------------------------------------------------- Mark as Read Only
11446:
11447: sub mark_as_readonly {
11448: my ($domain,$user,$files,$what) = @_;
1.613 albertel 11449: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11450: my ($tmp)=keys(%current_permissions);
11451: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 11452: foreach my $file (@{$files}) {
1.759 albertel 11453: $file = &declutter_portfile($file);
1.561 banghart 11454: push(@{$current_permissions{$file}},$what);
1.559 banghart 11455: }
1.613 albertel 11456: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11457: return;
11458: }
11459:
1.572 banghart 11460: # ------------------------------------------------------------Save Selected Files
11461:
11462: sub save_selected_files {
11463: my ($user, $path, @files) = @_;
11464: my $filename = $user."savedfiles";
1.573 banghart 11465: my @other_files = &files_not_in_path($user, $path);
1.1172.2.96 raeburn 11466: open (OUT,'>',LONCAPA::tempdir().$filename);
1.573 banghart 11467: foreach my $file (@files) {
1.620 albertel 11468: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 11469: }
11470: foreach my $file (@other_files) {
1.574 banghart 11471: print (OUT $file."\n");
1.572 banghart 11472: }
1.574 banghart 11473: close (OUT);
1.572 banghart 11474: return 'ok';
11475: }
11476:
1.574 banghart 11477: sub clear_selected_files {
11478: my ($user) = @_;
11479: my $filename = $user."savedfiles";
1.1172.2.96 raeburn 11480: open (OUT,'>',LONCAPA::tempdir().$filename);
1.574 banghart 11481: print (OUT undef);
11482: close (OUT);
11483: return ("ok");
11484: }
11485:
1.572 banghart 11486: sub files_in_path {
11487: my ($user, $path) = @_;
11488: my $filename = $user."savedfiles";
11489: my %return_files;
1.1172.2.96 raeburn 11490: open (IN,'<',LONCAPA::tempdir().$filename);
1.573 banghart 11491: while (my $line_in = <IN>) {
1.574 banghart 11492: chomp ($line_in);
11493: my @paths_and_file = split (m!/!, $line_in);
11494: my $file_part = pop (@paths_and_file);
11495: my $path_part = join ('/', @paths_and_file);
1.573 banghart 11496: $path_part.='/';
11497: my $path_and_file = $path_part.$file_part;
11498: if ($path_part eq $path) {
11499: $return_files{$file_part}= 'selected';
11500: }
11501: }
1.574 banghart 11502: close (IN);
11503: return (\%return_files);
1.572 banghart 11504: }
11505:
11506: # called in portfolio select mode, to show files selected NOT in current directory
11507: sub files_not_in_path {
11508: my ($user, $path) = @_;
11509: my $filename = $user."savedfiles";
11510: my @return_files;
11511: my $path_part;
1.1172.2.96 raeburn 11512: open(IN, '<',LONCAPA::tempdir().$filename);
1.800 albertel 11513: while (my $line = <IN>) {
1.572 banghart 11514: #ok, I know it's clunky, but I want it to work
1.800 albertel 11515: my @paths_and_file = split(m|/|, $line);
11516: my $file_part = pop(@paths_and_file);
11517: chomp($file_part);
11518: my $path_part = join('/', @paths_and_file);
1.572 banghart 11519: $path_part .= '/';
11520: my $path_and_file = $path_part.$file_part;
11521: if ($path_part ne $path) {
1.800 albertel 11522: push(@return_files, ($path_and_file));
1.572 banghart 11523: }
11524: }
1.800 albertel 11525: close(OUT);
1.574 banghart 11526: return (@return_files);
1.572 banghart 11527: }
11528:
1.745 raeburn 11529: #----------------------------------------------Get portfolio file permissions
1.629 banghart 11530:
1.745 raeburn 11531: sub get_portfile_permissions {
11532: my ($domain,$user) = @_;
1.613 albertel 11533: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 11534: my ($tmp)=keys(%current_permissions);
11535: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11536: return \%current_permissions;
11537: }
11538:
11539: #---------------------------------------------Get portfolio file access controls
11540:
1.749 raeburn 11541: sub get_access_controls {
1.745 raeburn 11542: my ($current_permissions,$group,$file) = @_;
1.769 albertel 11543: my %access;
11544: my $real_file = $file;
11545: $file =~ s/\.meta$//;
1.745 raeburn 11546: if (defined($file)) {
1.749 raeburn 11547: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11548: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 11549: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 11550: }
11551: }
1.745 raeburn 11552: } else {
1.749 raeburn 11553: foreach my $key (keys(%{$current_permissions})) {
11554: if ($key =~ /\0accesscontrol$/) {
11555: if (defined($group)) {
11556: if ($key !~ m-^\Q$group\E/-) {
11557: next;
11558: }
11559: }
11560: my ($fullpath) = split(/\0/,$key);
11561: if (ref($$current_permissions{$key}) eq 'HASH') {
11562: foreach my $control (keys(%{$$current_permissions{$key}})) {
11563: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11564: }
11565: }
11566: }
11567: }
11568: }
11569: return %access;
11570: }
11571:
11572: sub modify_access_controls {
11573: my ($file_name,$changes,$domain,$user)=@_;
11574: my ($outcome,$deloutcome);
11575: my %store_permissions;
11576: my %new_values;
11577: my %new_control;
11578: my %translation;
11579: my @deletions = ();
11580: my $now = time;
11581: if (exists($$changes{'activate'})) {
11582: if (ref($$changes{'activate'}) eq 'HASH') {
11583: my @newitems = sort(keys(%{$$changes{'activate'}}));
11584: my $numnew = scalar(@newitems);
11585: for (my $i=0; $i<$numnew; $i++) {
11586: my $newkey = $newitems[$i];
11587: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 11588: if ($newkey =~ /^\d+:/) {
11589: $newkey =~ s/^(\d+)/$newid/;
11590: $translation{$1} = $newid;
11591: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11592: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11593: $translation{$1} = $newid;
11594: }
1.749 raeburn 11595: $new_values{$file_name."\0".$newkey} =
11596: $$changes{'activate'}{$newitems[$i]};
11597: $new_control{$newkey} = $now;
11598: }
11599: }
11600: }
11601: my %todelete;
11602: my %changed_items;
11603: foreach my $action ('delete','update') {
11604: if (exists($$changes{$action})) {
11605: if (ref($$changes{$action}) eq 'HASH') {
11606: foreach my $key (keys(%{$$changes{$action}})) {
11607: my ($itemnum) = ($key =~ /^([^:]+):/);
11608: if ($action eq 'delete') {
11609: $todelete{$itemnum} = 1;
11610: } else {
11611: $changed_items{$itemnum} = $key;
11612: }
11613: }
1.745 raeburn 11614: }
11615: }
1.749 raeburn 11616: }
11617: # get lock on access controls for file.
11618: my $lockhash = {
11619: $file_name."\0".'locked_access_records' => $env{'user.name'}.
11620: ':'.$env{'user.domain'},
11621: };
11622: my $tries = 0;
11623: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11624:
1.1172.2.79 raeburn 11625: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 11626: $tries ++;
1.1172.2.79 raeburn 11627: sleep(0.1);
1.749 raeburn 11628: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11629: }
11630: if ($gotlock eq 'ok') {
11631: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11632: my ($tmp)=keys(%curr_permissions);
11633: if ($tmp=~/^error:/) { undef(%curr_permissions); }
11634: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11635: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11636: if (ref($curr_controls) eq 'HASH') {
11637: foreach my $control_item (keys(%{$curr_controls})) {
11638: my ($itemnum) = ($control_item =~ /^([^:]+):/);
11639: if (defined($todelete{$itemnum})) {
11640: push(@deletions,$file_name."\0".$control_item);
11641: } else {
11642: if (defined($changed_items{$itemnum})) {
11643: $new_control{$changed_items{$itemnum}} = $now;
11644: push(@deletions,$file_name."\0".$control_item);
11645: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11646: } else {
11647: $new_control{$control_item} = $$curr_controls{$control_item};
11648: }
11649: }
1.745 raeburn 11650: }
11651: }
11652: }
1.970 raeburn 11653: my ($group);
11654: if (&is_course($domain,$user)) {
11655: ($group,my $file) = split(/\//,$file_name,2);
11656: }
1.749 raeburn 11657: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11658: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11659: $outcome = &put('file_permissions',\%new_values,$domain,$user);
11660: # remove lock
11661: my @del_lock = ($file_name."\0".'locked_access_records');
11662: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 11663: my $sqlresult =
1.970 raeburn 11664: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 11665: $group);
1.749 raeburn 11666: } else {
11667: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 11668: }
1.749 raeburn 11669: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 11670: }
11671:
1.827 raeburn 11672: sub make_public_indefinitely {
11673: my ($requrl) = @_;
11674: my $now = time;
11675: my $action = 'activate';
11676: my $aclnum = 0;
11677: if (&is_portfolio_url($requrl)) {
11678: my (undef,$udom,$unum,$file_name,$group) =
11679: &parse_portfolio_url($requrl);
11680: my $current_perms = &get_portfile_permissions($udom,$unum);
11681: my %access_controls = &get_access_controls($current_perms,
11682: $group,$file_name);
11683: foreach my $key (keys(%{$access_controls{$file_name}})) {
11684: my ($num,$scope,$end,$start) =
11685: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11686: if ($scope eq 'public') {
11687: if ($start <= $now && $end == 0) {
11688: $action = 'none';
11689: } else {
11690: $action = 'update';
11691: $aclnum = $num;
11692: }
11693: last;
11694: }
11695: }
11696: if ($action eq 'none') {
11697: return 'ok';
11698: } else {
11699: my %changes;
11700: my $newend = 0;
11701: my $newstart = $now;
11702: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
11703: $changes{$action}{$newkey} = {
11704: type => 'public',
11705: time => {
11706: start => $newstart,
11707: end => $newend,
11708: },
11709: };
11710: my ($outcome,$deloutcome,$new_values,$translation) =
11711: &modify_access_controls($file_name,\%changes,$udom,$unum);
11712: return $outcome;
11713: }
11714: } else {
11715: return 'invalid';
11716: }
11717: }
11718:
1.745 raeburn 11719: #------------------------------------------------------Get Marked as Read Only
11720:
11721: sub get_marked_as_readonly {
11722: my ($domain,$user,$what,$group) = @_;
11723: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 11724: my @readonly_files;
1.629 banghart 11725: my $cmp1=$what;
11726: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 11727: while (my ($file_name,$value) = each(%{$current_permissions})) {
11728: if (defined($group)) {
11729: if ($file_name !~ m-^\Q$group\E/-) {
11730: next;
11731: }
11732: }
1.561 banghart 11733: if (ref($value) eq "ARRAY"){
11734: foreach my $stored_what (@{$value}) {
1.629 banghart 11735: my $cmp2=$stored_what;
1.759 albertel 11736: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 11737: $cmp2=join('',@{$stored_what});
1.745 raeburn 11738: }
1.629 banghart 11739: if ($cmp1 eq $cmp2) {
1.561 banghart 11740: push(@readonly_files, $file_name);
1.745 raeburn 11741: last;
1.563 banghart 11742: } elsif (!defined($what)) {
11743: push(@readonly_files, $file_name);
1.745 raeburn 11744: last;
1.561 banghart 11745: }
11746: }
1.745 raeburn 11747: }
1.561 banghart 11748: }
11749: return @readonly_files;
11750: }
1.577 banghart 11751: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 11752:
1.577 banghart 11753: sub get_marked_as_readonly_hash {
1.745 raeburn 11754: my ($current_permissions,$group,$what) = @_;
1.577 banghart 11755: my %readonly_files;
1.745 raeburn 11756: while (my ($file_name,$value) = each(%{$current_permissions})) {
11757: if (defined($group)) {
11758: if ($file_name !~ m-^\Q$group\E/-) {
11759: next;
11760: }
11761: }
1.577 banghart 11762: if (ref($value) eq "ARRAY"){
11763: foreach my $stored_what (@{$value}) {
1.745 raeburn 11764: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 11765: foreach my $lock_descriptor(@{$stored_what}) {
11766: if ($lock_descriptor eq 'graded') {
11767: $readonly_files{$file_name} = 'graded';
11768: } elsif ($lock_descriptor eq 'handback') {
11769: $readonly_files{$file_name} = 'handback';
11770: } else {
11771: if (!exists($readonly_files{$file_name})) {
11772: $readonly_files{$file_name} = 'locked';
11773: }
11774: }
1.745 raeburn 11775: }
1.750 banghart 11776: }
1.577 banghart 11777: }
11778: }
11779: }
11780: return %readonly_files;
11781: }
1.559 banghart 11782: # ------------------------------------------------------------ Unmark as Read Only
11783:
11784: sub unmark_as_readonly {
1.629 banghart 11785: # unmarks $file_name (if $file_name is defined), or all files locked by $what
11786: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 11787: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 11788: $file_name = &declutter_portfile($file_name);
1.634 albertel 11789: my $symb_crs = $what;
11790: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 11791: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 11792: my ($tmp)=keys(%current_permissions);
11793: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 11794: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 11795: foreach my $file (@readonly_files) {
1.759 albertel 11796: my $clean_file = &declutter_portfile($file);
11797: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 11798: my $current_locks = $current_permissions{$file};
1.563 banghart 11799: my @new_locks;
11800: my @del_keys;
11801: if (ref($current_locks) eq "ARRAY"){
11802: foreach my $locker (@{$current_locks}) {
1.632 albertel 11803: my $compare=$locker;
1.749 raeburn 11804: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 11805: $compare=join('',@{$locker});
1.746 raeburn 11806: if ($compare ne $symb_crs) {
11807: push(@new_locks, $locker);
11808: }
1.563 banghart 11809: }
11810: }
1.650 albertel 11811: if (scalar(@new_locks) > 0) {
1.563 banghart 11812: $current_permissions{$file} = \@new_locks;
11813: } else {
11814: push(@del_keys, $file);
1.613 albertel 11815: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 11816: delete($current_permissions{$file});
1.563 banghart 11817: }
11818: }
1.561 banghart 11819: }
1.613 albertel 11820: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 11821: return;
11822: }
1.512 banghart 11823:
1.17 www 11824: # ------------------------------------------------------------ Directory lister
11825:
11826: sub dirlist {
1.955 raeburn 11827: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 11828: $uri=~s/^\///;
11829: $uri=~s/\/$//;
1.253 stredwic 11830: my ($udom, $uname);
1.955 raeburn 11831: if ($getuserdir) {
1.253 stredwic 11832: $udom = $userdomain;
11833: $uname = $username;
1.955 raeburn 11834: } else {
11835: (undef,$udom,$uname)=split(/\//,$uri);
11836: if(defined($userdomain)) {
11837: $udom = $userdomain;
11838: }
11839: if(defined($username)) {
11840: $uname = $username;
11841: }
1.253 stredwic 11842: }
1.955 raeburn 11843: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 11844:
1.955 raeburn 11845: $dirRoot = $perlvar{'lonDocRoot'};
11846: if (defined($getpropath)) {
11847: $dirRoot = &propath($udom,$uname);
1.253 stredwic 11848: $dirRoot =~ s/\/$//;
1.955 raeburn 11849: } elsif (defined($getuserdir)) {
11850: my $subdir=$uname.'__';
11851: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11852: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11853: ."/$udom/$subdir/$uname";
11854: } elsif (defined($alternateRoot)) {
11855: $dirRoot = $alternateRoot;
1.751 banghart 11856: }
1.253 stredwic 11857:
11858: if($udom) {
11859: if($uname) {
1.1135 raeburn 11860: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 11861: if ($uhome eq 'no_host') {
11862: return ([],'no_host');
11863: }
1.955 raeburn 11864: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 11865: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 11866: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 11867: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11868: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 11869: } else {
11870: @listing_results = map { &unescape($_); } split(/:/,$listing);
11871: }
1.605 matthew 11872: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 11873: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 11874: @listing_results = split(/:/,$listing);
11875: } else {
11876: @listing_results = map { &unescape($_); } split(/:/,$listing);
11877: }
1.1135 raeburn 11878: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 11879: ($listing eq 'rejected') || ($listing eq 'refused') ||
11880: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11881: return ([],$listing);
11882: } else {
11883: return (\@listing_results);
1.1135 raeburn 11884: }
1.955 raeburn 11885: } elsif(!$alternateRoot) {
1.1136 raeburn 11886: my (%allusers,%listerror);
1.841 albertel 11887: my %servers = &get_servers($udom,'library');
1.955 raeburn 11888: foreach my $tryserver (keys(%servers)) {
11889: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11890: &escape($udom),$tryserver);
11891: if ($listing eq 'unknown_cmd') {
11892: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11893: $udom, $tryserver);
11894: } else {
11895: @listing_results = map { &unescape($_); } split(/:/,$listing);
11896: }
1.841 albertel 11897: if ($listing eq 'unknown_cmd') {
11898: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11899: $udom, $tryserver);
11900: @listing_results = split(/:/,$listing);
11901: } else {
11902: @listing_results =
11903: map { &unescape($_); } split(/:/,$listing);
11904: }
1.1136 raeburn 11905: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11906: ($listing eq 'rejected') || ($listing eq 'refused') ||
11907: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11908: $listerror{$tryserver} = $listing;
11909: } else {
1.841 albertel 11910: foreach my $line (@listing_results) {
11911: my ($entry) = split(/&/,$line,2);
11912: $allusers{$entry} = 1;
11913: }
11914: }
1.253 stredwic 11915: }
1.1136 raeburn 11916: my @alluserslist=();
1.800 albertel 11917: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 11918: push(@alluserslist,$user.'&user');
1.253 stredwic 11919: }
1.1172.2.80 raeburn 11920: if (!%listerror) {
11921: # no errors
11922: return (\@alluserslist);
11923: } elsif (scalar(keys(%servers)) == 1) {
11924: # one library server, one error
11925: my ($key) = keys(%listerror);
11926: return (\@alluserslist, $listerror{$key});
11927: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11928: # con_lost indicates that we might miss data from at least one
11929: # library server
11930: return (\@alluserslist, 'con_lost');
11931: } else {
11932: # multiple library servers and no con_lost -> data should be
11933: # complete.
11934: return (\@alluserslist);
11935: }
11936:
1.253 stredwic 11937: } else {
1.1136 raeburn 11938: return ([],'missing username');
1.253 stredwic 11939: }
1.955 raeburn 11940: } elsif(!defined($getpropath)) {
1.1136 raeburn 11941: my $path = $perlvar{'lonDocRoot'}.'/res/';
11942: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11943: return (\@all_domains);
1.955 raeburn 11944: } else {
1.1136 raeburn 11945: return ([],'missing domain');
1.275 stredwic 11946: }
11947: }
11948:
11949: # --------------------------------------------- GetFileTimestamp
11950: # This function utilizes dirlist and returns the date stamp for
11951: # when it was last modified. It will also return an error of -1
11952: # if an error occurs
11953:
11954: sub GetFileTimestamp {
1.955 raeburn 11955: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 11956: $studentDomain = &LONCAPA::clean_domain($studentDomain);
11957: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 11958: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11959: undef,$getuserdir);
11960: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11961: return -1;
11962: }
11963: if (ref($fileref) eq 'ARRAY') {
11964: my @stats = split('&',$fileref->[0]);
1.375 matthew 11965: # @stats contains first the filename, then the stat output
11966: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 11967: } else {
11968: return -1;
1.253 stredwic 11969: }
1.26 www 11970: }
11971:
1.712 albertel 11972: sub stat_file {
11973: my ($uri) = @_;
1.787 albertel 11974: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 11975:
1.955 raeburn 11976: my ($udom,$uname,$file);
1.712 albertel 11977: if ($uri =~ m-^/(uploaded|editupload)/-) {
11978: ($udom,$uname,$file) =
1.811 albertel 11979: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 11980: $file = 'userfiles/'.$file;
11981: }
11982: if ($uri =~ m-^/res/-) {
11983: ($udom,$uname) =
1.807 albertel 11984: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 11985: $file = $uri;
11986: }
11987:
11988: if (!$udom || !$uname || !$file) {
11989: # unable to handle the uri
11990: return ();
11991: }
1.956 raeburn 11992: my $getpropath;
11993: if ($file =~ /^userfiles\//) {
11994: $getpropath = 1;
11995: }
1.1136 raeburn 11996: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11997: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11998: return ();
11999: } else {
12000: if (ref($listref) eq 'ARRAY') {
12001: my @stats = split('&',$listref->[0]);
12002: shift(@stats); #filename is first
12003: return @stats;
12004: }
1.712 albertel 12005: }
12006: return ();
12007: }
12008:
1.26 www 12009: # -------------------------------------------------------- Value of a Condition
12010:
1.713 albertel 12011: # gets the value of a specific preevaluated condition
12012: # stored in the string $env{user.state.<cid>}
12013: # or looks up a condition reference in the bighash and if if hasn't
12014: # already been evaluated recurses into docondval to get the value of
12015: # the condition, then memoizing it to
12016: # $env{user.state.<cid>.<condition>}
1.40 www 12017: sub directcondval {
12018: my $number=shift;
1.620 albertel 12019: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 12020: &Apache::lonuserstate::evalstate();
12021: }
1.713 albertel 12022: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12023: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12024: } elsif ($number =~ /^_/) {
12025: my $sub_condition;
12026: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12027: &GDBM_READER(),0640)) {
12028: $sub_condition=$bighash{'conditions'.$number};
12029: untie(%bighash);
12030: }
12031: my $value = &docondval($sub_condition);
1.949 raeburn 12032: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 12033: return $value;
12034: }
1.620 albertel 12035: if ($env{'user.state.'.$env{'request.course.id'}}) {
12036: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 12037: } else {
12038: return 2;
12039: }
12040: }
12041:
1.713 albertel 12042: # get the collection of conditions for this resource
1.26 www 12043: sub condval {
12044: my $condidx=shift;
1.54 www 12045: my $allpathcond='';
1.713 albertel 12046: foreach my $cond (split(/\|/,$condidx)) {
12047: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12048: $allpathcond.=
12049: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12050: }
1.191 harris41 12051: }
1.54 www 12052: $allpathcond=~s/\|$//;
1.713 albertel 12053: return &docondval($allpathcond);
12054: }
12055:
12056: #evaluates an expression of conditions
12057: sub docondval {
12058: my ($allpathcond) = @_;
12059: my $result=0;
12060: if ($env{'request.course.id'}
12061: && defined($allpathcond)) {
12062: my $operand='|';
12063: my @stack;
12064: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12065: if ($chunk eq '(') {
12066: push @stack,($operand,$result);
12067: } elsif ($chunk eq ')') {
12068: my $before=pop @stack;
12069: if (pop @stack eq '&') {
12070: $result=$result>$before?$before:$result;
12071: } else {
12072: $result=$result>$before?$result:$before;
12073: }
12074: } elsif (($chunk eq '&') || ($chunk eq '|')) {
12075: $operand=$chunk;
12076: } else {
12077: my $new=directcondval($chunk);
12078: if ($operand eq '&') {
12079: $result=$result>$new?$new:$result;
12080: } else {
12081: $result=$result>$new?$result:$new;
12082: }
12083: }
12084: }
1.26 www 12085: }
12086: return $result;
1.421 albertel 12087: }
12088:
12089: # ---------------------------------------------------- Devalidate courseresdata
12090:
12091: sub devalidatecourseresdata {
12092: my ($coursenum,$coursedomain)=@_;
12093: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12094: &devalidate_cache_new('courseres',$hashid);
1.28 www 12095: }
12096:
1.763 www 12097:
1.200 www 12098: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 12099: #
12100: # Parameters:
12101: # $coursenum - Number of the course.
12102: # $coursedomain - Domain at which the course was created.
12103: # Returns:
12104: # A hash of the course parameters along (I think) with timestamps
12105: # and version info.
1.877 foxr 12106:
1.624 albertel 12107: sub get_courseresdata {
12108: my ($coursenum,$coursedomain)=@_;
1.200 www 12109: my $coursehom=&homeserver($coursenum,$coursedomain);
12110: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 12111: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 12112: my %dumpreply;
1.417 albertel 12113: unless (defined($cached)) {
1.624 albertel 12114: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 12115: $result=\%dumpreply;
1.251 albertel 12116: my ($tmp) = keys(%dumpreply);
12117: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 12118: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 12119: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12120: return $tmp;
1.416 albertel 12121: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 12122: $result=undef;
1.599 albertel 12123: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 12124: }
12125: }
1.624 albertel 12126: return $result;
12127: }
12128:
1.633 albertel 12129: sub devalidateuserresdata {
12130: my ($uname,$udom)=@_;
12131: my $hashid="$udom:$uname";
12132: &devalidate_cache_new('userres',$hashid);
12133: }
12134:
1.624 albertel 12135: sub get_userresdata {
12136: my ($uname,$udom)=@_;
12137: #most student don\'t have any data set, check if there is some data
12138: if (&EXT_cache_status($udom,$uname)) { return undef; }
12139:
12140: my $hashid="$udom:$uname";
12141: my ($result,$cached)=&is_cached_new('userres',$hashid);
12142: if (!defined($cached)) {
12143: my %resourcedata=&dump('resourcedata',$udom,$uname);
12144: $result=\%resourcedata;
12145: &do_cache_new('userres',$hashid,$result,600);
12146: }
12147: my ($tmp)=keys(%$result);
12148: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12149: return $result;
12150: }
12151: #error 2 occurs when the .db doesn't exist
12152: if ($tmp!~/error: 2 /) {
1.1172.2.71 raeburn 12153: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12154: &logthis("<font color=\"blue\">WARNING:".
12155: " Trying to get resource data for ".
12156: $uname." at ".$udom.": ".
12157: $tmp."</font>");
12158: }
1.624 albertel 12159: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 12160: #&EXT_cache_set($udom,$uname);
12161: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 12162: undef($tmp); # not really an error so don't send it back
1.624 albertel 12163: }
12164: return $tmp;
12165: }
1.879 foxr 12166: #----------------------------------------------- resdata - return resource data
12167: # Purpose:
12168: # Return resource data for either users or for a course.
12169: # Parameters:
12170: # $name - Course/user name.
12171: # $domain - Name of the domain the user/course is registered on.
12172: # $type - Type of thing $name is (must be 'course' or 'user'
12173: # @which - Array of names of resources desired.
12174: # Returns:
12175: # The value of the first reasource in @which that is found in the
12176: # resource hash.
12177: # Exceptional Conditions:
12178: # If the $type passed in is not valid (not the string 'course' or
12179: # 'user', an undefined reference is returned.
12180: # If none of the resources are found, an undef is returned
1.624 albertel 12181: sub resdata {
12182: my ($name,$domain,$type,@which)=@_;
12183: my $result;
12184: if ($type eq 'course') {
12185: $result=&get_courseresdata($name,$domain);
12186: } elsif ($type eq 'user') {
12187: $result=&get_userresdata($name,$domain);
12188: }
12189: if (!ref($result)) { return $result; }
1.251 albertel 12190: foreach my $item (@which) {
1.927 albertel 12191: if (defined($result->{$item->[0]})) {
12192: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 12193: }
1.250 albertel 12194: }
1.291 albertel 12195: return undef;
1.200 www 12196: }
12197:
1.1172.2.146. .1(raebu 12198:22): sub get_domain_lti {
12199:22): my ($cdom,$context) = @_;
.4(raebu 12200:22): my ($name,$cachename,%lti);
.1(raebu 12201:22): if ($context eq 'consumer') {
12202:22): $name = 'ltitools';
12203:22): } elsif ($context eq 'provider') {
12204:22): $name = 'lti';
.4(raebu 12205:22): } elsif ($context eq 'linkprot') {
12206:22): $name = 'ltisec';
.1(raebu 12207:22): } else {
12208:22): return %lti;
12209:22): }
.4(raebu 12210:22): if ($context eq 'linkprot') {
12211:22): $cachename = $context;
12212:22): } else {
12213:22): $cachename = $name;
12214:22): }
12215:22): my ($result,$cached)=&is_cached_new($cachename,$cdom);
.1(raebu 12216:22): if (defined($cached)) {
12217:22): if (ref($result) eq 'HASH') {
12218:22): %lti = %{$result};
12219:22): }
12220:22): } else {
12221:22): my %domconfig = &get_dom('configuration',[$name],$cdom);
12222:22): if (ref($domconfig{$name}) eq 'HASH') {
.4(raebu 12223:22): if ($context eq 'linkprot') {
12224:22): if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12225:22): %lti = %{$domconfig{$name}{'linkprot'}};
12226:22): }
12227:22): } else {
12228:22): %lti = %{$domconfig{$name}};
12229:22): }
.1(raebu 12230:22): }
12231:22): my $cachetime = 24*60*60;
.4(raebu 12232:22): &do_cache_new($cachename,$cdom,\%lti,$cachetime);
.1(raebu 12233:22): }
12234:22): return %lti;
12235:22): }
12236:22):
12237:22): sub get_course_lti {
.13(raeb 12238:-23): my ($cnum,$cdom,$context) = @_;
12239:-23): my ($name,$cachename,%lti);
12240:-23): if ($context eq 'consumer') {
12241:-23): $name = 'ltitools';
12242:-23): $cachename = 'courseltitools';
12243:-23): } elsif ($context eq 'provider') {
12244:-23): $name = 'lti';
12245:-23): $cachename = 'courselti';
12246:-23): } else {
12247:-23): return %lti;
12248:-23): }
.1(raebu 12249:22): my $hashid=$cdom.'_'.$cnum;
.13(raeb 12250:-23): my ($result,$cached)=&is_cached_new($cachename,$hashid);
.1(raebu 12251:22): if (defined($cached)) {
12252:22): if (ref($result) eq 'HASH') {
.13(raeb 12253:-23): %lti = %{$result};
.1(raebu 12254:22): }
12255:22): } else {
.13(raeb 12256:-23): %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
.1(raebu 12257:22): my $cachetime = 24*60*60;
.13(raeb 12258:-23): &do_cache_new($cachename,$hashid,\%lti,$cachetime);
.1(raebu 12259:22): }
.13(raeb 12260:-23): return %lti;
.1(raebu 12261:22): }
12262:22):
.3(raebu 12263:22): sub courselti_itemid {
12264:22): my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12265:22): my ($chome,$itemid);
12266:22): $chome = &homeserver($cnum,$cdom);
12267:22): return if ($chome eq 'no_host');
12268:22): if (ref($params) eq 'HASH') {
12269:22): my $rep;
12270:22): if (grep { $_ eq $chome } current_machine_ids()) {
12271:22): $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12272:22): } else {
12273:22): my $escurl = &escape($url);
12274:22): my $escmethod = &escape($method);
12275:22): my $items = &freeze_escape($params);
12276:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12277:22): }
12278:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12279:22): ($rep eq 'unknown_cmd')) {
12280:22): $itemid = $rep;
12281:22): }
12282:22): }
12283:22): return $itemid;
12284:22): }
12285:22):
12286:22): sub domainlti_itemid {
12287:22): my ($cdom,$url,$method,$params,$context) = @_;
12288:22): my ($primary_id,$itemid);
12289:22): $primary_id = &domain($cdom,'primary');
12290:22): return if ($primary_id eq '');
12291:22): if (ref($params) eq 'HASH') {
12292:22): my $rep;
12293:22): if (grep { $_ eq $primary_id } current_machine_ids()) {
12294:22): $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12295:22): } else {
12296:22): my $cnum = '';
12297:22): my $escurl = &escape($url);
12298:22): my $escmethod = &escape($method);
12299:22): my $items = &freeze_escape($params);
12300:22): $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12301:22): }
12302:22): unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12303:22): ($rep eq 'unknown_cmd')) {
12304:22): $itemid = $rep;
12305:22): }
12306:22): }
12307:22): return $itemid;
12308:22): }
12309:22):
.15(raeb 12310:-23): sub get_ltitools_id {
12311:-23): my ($context,$cdom,$cnum,$title) = @_;
12312:-23): my ($lockhash,$tries,$gotlock,$id,$error);
12313:-23):
12314:-23): # get lock on ltitools db
12315:-23): $lockhash = {
12316:-23): lock => $env{'user.name'}.
12317:-23): ':'.$env{'user.domain'},
12318:-23): };
12319:-23): $tries = 0;
12320:-23): if ($context eq 'domain') {
12321:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12322:-23): } else {
12323:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12324:-23): }
12325:-23): while (($gotlock ne 'ok') && ($tries<10)) {
12326:-23): $tries ++;
12327:-23): sleep (0.1);
12328:-23): if ($context eq 'domain') {
12329:-23): $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12330:-23): } else {
12331:-23): $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12332:-23): }
12333:-23): }
12334:-23): if ($gotlock eq 'ok') {
12335:-23): my %currids;
12336:-23): if ($context eq 'domain') {
12337:-23): %currids = &dump_dom('ltitools',$cdom);
12338:-23): } else {
12339:-23): %currids = &dump('ltitools',$cdom,$cnum);
12340:-23): }
12341:-23): if ($currids{'lock'}) {
12342:-23): delete($currids{'lock'});
12343:-23): if (keys(%currids)) {
12344:-23): my @curr = sort { $a <=> $b } keys(%currids);
12345:-23): if ($curr[-1] =~ /^\d+$/) {
12346:-23): $id = 1 + $curr[-1];
12347:-23): }
12348:-23): } else {
12349:-23): $id = 1;
12350:-23): }
12351:-23): if ($id) {
12352:-23): if ($context eq 'domain') {
12353:-23): unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12354:-23): $error = 'nostore';
12355:-23): }
12356:-23): } else {
12357:-23): unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12358:-23): $error = 'nostore';
12359:-23): }
12360:-23): }
12361:-23): } else {
12362:-23): $error = 'nonumber';
12363:-23): }
12364:-23): }
12365:-23): my $dellockoutcome;
12366:-23): if ($context eq 'domain') {
12367:-23): $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12368:-23): } else {
12369:-23): $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12370:-23): }
12371:-23): } else {
12372:-23): $error = 'nolock';
12373:-23): }
12374:-23): return ($id,$error);
12375:-23): }
12376:-23):
.9(raebu 12377:23): sub count_supptools {
12378:23): my ($cnum,$cdom,$ignorecache,$reload)=@_;
1.1172.2.31 raeburn 12379: my $hashid=$cnum.':'.$cdom;
1.1172.2.146. .9(raebu 12380:23): my ($numexttools,$cached);
1.1172.2.31 raeburn 12381: unless ($ignorecache) {
1.1172.2.146. .9(raebu 12382:23): ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
1.1172.2.31 raeburn 12383: }
12384: unless (defined($cached)) {
12385: my $chome=&homeserver($cnum,$cdom);
1.1172.2.146. .9(raebu 12386:23): $numexttools = 0;
1.1172.2.31 raeburn 12387: unless ($chome eq 'no_host') {
1.1172.2.146. .9(raebu 12388:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12389:23): if (ref($supplemental) eq 'HASH') {
12390:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12391:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12392:23): if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12393:23): $numexttools ++;
12394:23): }
12395:23): }
12396:23): }
12397:23): }
1.1172.2.31 raeburn 12398: }
1.1172.2.146. .9(raebu 12399:23): &do_cache_new('supptools',$hashid,$numexttools,600);
1.1172.2.31 raeburn 12400: }
1.1172.2.146. .9(raebu 12401:23): return $numexttools;
12402:23): }
12403:23):
12404:23): sub has_unhidden_suppfiles {
12405:23): my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12406:23): my $hashid=$cnum.':'.$cdom;
12407:23): my ($showsupp,$cached);
12408:23): unless ($ignorecache) {
12409:23): ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12410:23): }
12411:23): unless (defined($cached)) {
12412:23): my $chome=&homeserver($cnum,$cdom);
12413:23): unless ($chome eq 'no_host') {
12414:23): my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12415:23): if (ref($supplemental) eq 'HASH') {
12416:23): if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12417:23): foreach my $key (keys(%{$supplemental->{'ids'}})) {
12418:23): next if ($key =~ /\.sequence$/);
12419:23): if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12420:23): foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12421:23): unless ($supplemental->{'hidden'}->{$id}) {
12422:23): $showsupp = 1;
12423:23): last;
12424:23): }
12425:23): }
12426:23): }
12427:23): last if ($showsupp);
12428:23): }
12429:23): }
12430:23): }
12431:23): }
12432:23): &do_cache_new('showsupp',$hashid,$showsupp,600);
12433:23): }
12434:23): return $showsupp;
1.1172.2.31 raeburn 12435: }
12436:
1.379 matthew 12437: #
12438: # EXT resource caching routines
12439: #
12440:
1.1172.2.146. .1(raebu 12441:22): {
12442:22): # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12443:22): #
12444:22): # The course for which we cache
12445:22): my $cachedmapkey='';
12446:22): # The cached recursive maps for this course
12447:22): my %cachedmaps=();
12448:22): # When this was last done
12449:22): my $cachedmaptime='';
12450:22):
1.379 matthew 12451: sub clear_EXT_cache_status {
1.383 albertel 12452: &delenv('cache.EXT.');
1.379 matthew 12453: }
12454:
12455: sub EXT_cache_status {
12456: my ($target_domain,$target_user) = @_;
1.383 albertel 12457: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 12458: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 12459: # We know already the user has no data
12460: return 1;
12461: } else {
12462: return 0;
12463: }
12464: }
12465:
12466: sub EXT_cache_set {
12467: my ($target_domain,$target_user) = @_;
1.383 albertel 12468: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 12469: #&appenv({$cachename => time});
1.379 matthew 12470: }
12471:
1.28 www 12472: # --------------------------------------------------------- Value of a Variable
1.58 www 12473: sub EXT {
1.715 albertel 12474:
1.1172.2.28 raeburn 12475: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 12476: unless ($varname) { return ''; }
1.218 albertel 12477: #get real user name/domain, courseid and symb
12478: my $courseid;
1.359 albertel 12479: my $publicuser;
1.427 www 12480: if ($symbparm) {
12481: $symbparm=&get_symb_from_alias($symbparm);
12482: }
1.218 albertel 12483: if (!($uname && $udom)) {
1.790 albertel 12484: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 12485: if (!$symbparm) { $symbparm=$cursymb; }
12486: } else {
1.620 albertel 12487: $courseid=$env{'request.course.id'};
1.218 albertel 12488: }
1.48 www 12489: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12490: my $rest;
1.320 albertel 12491: if (defined($therest[0])) {
1.48 www 12492: $rest=join('.',@therest);
12493: } else {
12494: $rest='';
12495: }
1.320 albertel 12496:
1.57 www 12497: my $qualifierrest=$qualifier;
12498: if ($rest) { $qualifierrest.='.'.$rest; }
12499: my $spacequalifierrest=$space;
12500: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 12501: if ($realm eq 'user') {
1.48 www 12502: # --------------------------------------------------------------- user.resource
12503: if ($space eq 'resource') {
1.651 albertel 12504: if ( (defined($Apache::lonhomework::parsing_a_problem)
12505: || defined($Apache::lonhomework::parsing_a_task))
12506: &&
1.1172.2.142 raeburn 12507: ($symbparm eq &symbread()) ) {
1.744 albertel 12508: # if we are in the middle of processing the resource the
12509: # get the value we are planning on committing
12510: if (defined($Apache::lonhomework::results{$qualifierrest})) {
12511: return $Apache::lonhomework::results{$qualifierrest};
12512: } else {
12513: return $Apache::lonhomework::history{$qualifierrest};
12514: }
1.335 albertel 12515: } else {
1.359 albertel 12516: my %restored;
1.620 albertel 12517: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 12518: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12519: } else {
12520: %restored=&restore($symbparm,$courseid,$udom,$uname);
12521: }
1.335 albertel 12522: return $restored{$qualifierrest};
12523: }
1.48 www 12524: # ----------------------------------------------------------------- user.access
12525: } elsif ($space eq 'access') {
1.218 albertel 12526: # FIXME - not supporting calls for a specific user
1.48 www 12527: return &allowed($qualifier,$rest);
12528: # ------------------------------------------ user.preferences, user.environment
12529: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 12530: if (($uname eq $env{'user.name'}) &&
12531: ($udom eq $env{'user.domain'})) {
12532: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 12533: } else {
1.359 albertel 12534: my %returnhash;
12535: if (!$publicuser) {
12536: %returnhash=&userenvironment($udom,$uname,
12537: $qualifierrest);
12538: }
1.218 albertel 12539: return $returnhash{$qualifierrest};
12540: }
1.48 www 12541: # ----------------------------------------------------------------- user.course
12542: } elsif ($space eq 'course') {
1.218 albertel 12543: # FIXME - not supporting calls for a specific user
1.620 albertel 12544: return $env{join('.',('request.course',$qualifier))};
1.48 www 12545: # ------------------------------------------------------------------- user.role
12546: } elsif ($space eq 'role') {
1.218 albertel 12547: # FIXME - not supporting calls for a specific user
1.620 albertel 12548: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 12549: if ($qualifier eq 'value') {
12550: return $role;
12551: } elsif ($qualifier eq 'extent') {
12552: return $where;
12553: }
12554: # ----------------------------------------------------------------- user.domain
12555: } elsif ($space eq 'domain') {
1.218 albertel 12556: return $udom;
1.48 www 12557: # ------------------------------------------------------------------- user.name
12558: } elsif ($space eq 'name') {
1.218 albertel 12559: return $uname;
1.48 www 12560: # ---------------------------------------------------- Any other user namespace
1.29 www 12561: } else {
1.359 albertel 12562: my %reply;
12563: if (!$publicuser) {
12564: %reply=&get($space,[$qualifierrest],$udom,$uname);
12565: }
12566: return $reply{$qualifierrest};
1.48 www 12567: }
1.236 www 12568: } elsif ($realm eq 'query') {
12569: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 12570: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12571: [$spacequalifierrest]);
1.620 albertel 12572: return $env{'form.'.$spacequalifierrest};
1.236 www 12573: } elsif ($realm eq 'request') {
1.48 www 12574: # ------------------------------------------------------------- request.browser
12575: if ($space eq 'browser') {
1.1145 bisitz 12576: return $env{'browser.'.$qualifier};
1.57 www 12577: # ------------------------------------------------------------ request.filename
12578: } else {
1.620 albertel 12579: return $env{'request.'.$spacequalifierrest};
1.29 www 12580: }
1.28 www 12581: } elsif ($realm eq 'course') {
1.48 www 12582: # ---------------------------------------------------------- course.description
1.620 albertel 12583: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 12584: } elsif ($realm eq 'resource') {
1.165 www 12585:
1.620 albertel 12586: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 12587: if (!$symbparm) { $symbparm=&symbread(); }
12588: }
1.693 albertel 12589:
1.1172.2.30 raeburn 12590: if ($qualifier eq '') {
12591: if ($space eq 'title') {
12592: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12593: return &gettitle($symbparm);
12594: }
1.693 albertel 12595:
1.1172.2.30 raeburn 12596: if ($space eq 'map') {
12597: my ($map) = &decode_symb($symbparm);
12598: return &symbread($map);
12599: }
12600: if ($space eq 'maptitle') {
12601: my ($map) = &decode_symb($symbparm);
12602: return &gettitle($map);
12603: }
12604: if ($space eq 'filename') {
12605: if ($symbparm) {
12606: return &clutter((&decode_symb($symbparm))[2]);
12607: }
12608: return &hreflocation('',$env{'request.filename'});
1.905 albertel 12609: }
1.1172.2.30 raeburn 12610:
12611: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12612: if ($space eq 'visibleparts') {
12613: my $navmap = Apache::lonnavmaps::navmap->new();
12614: my $item;
12615: if (ref($navmap)) {
12616: my $res = $navmap->getBySymb($symbparm);
12617: my $parts = $res->parts();
12618: if (ref($parts) eq 'ARRAY') {
12619: $item = join(',',@{$parts});
12620: }
12621: undef($navmap);
12622: }
12623: return $item;
12624: }
12625: }
12626: }
1.693 albertel 12627:
12628: my ($section, $group, @groups);
1.593 albertel 12629: my ($courselevelm,$courselevel);
1.1172.2.28 raeburn 12630: if (($courseid eq '') && ($cid)) {
12631: $courseid = $cid;
12632: }
12633: if (($symbparm && $courseid) &&
12634: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 12635:
1.218 albertel 12636: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 12637:
1.60 www 12638: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 12639: my $symbp=$symbparm;
1.735 albertel 12640: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 12641:
12642: my $symbparm=$symbp.'.'.$spacequalifierrest;
12643: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12644:
1.620 albertel 12645: if (($env{'user.name'} eq $uname) &&
12646: ($env{'user.domain'} eq $udom)) {
12647: $section=$env{'request.course.sec'};
1.733 raeburn 12648: @groups = split(/:/,$env{'request.course.groups'});
12649: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 12650: } else {
1.539 albertel 12651: if (! defined($usection)) {
1.551 albertel 12652: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 12653: } else {
12654: $section = $usection;
12655: }
1.733 raeburn 12656: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 12657: }
12658:
12659: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12660: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12661: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12662:
1.593 albertel 12663: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 12664: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 12665: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 12666:
1.60 www 12667: # ----------------------------------------------------------- first, check user
1.624 albertel 12668:
12669: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 12670: ([$courselevelr,'resource'],
12671: [$courselevelm,'map' ],
12672: [$courselevel, 'course' ]));
1.931 albertel 12673: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 12674:
1.594 albertel 12675: # ------------------------------------------------ second, check some of course
1.684 raeburn 12676: my $coursereply;
1.691 raeburn 12677: if (@groups > 0) {
12678: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12679: $mapparm,$spacequalifierrest);
1.927 albertel 12680: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 12681: }
1.96 www 12682:
1.684 raeburn 12683: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 12684: $env{'course.'.$courseid.'.domain'},
12685: 'course',
12686: ([$seclevelr, 'resource'],
12687: [$seclevelm, 'map' ],
12688: [$seclevel, 'course' ],
12689: [$courselevelr,'resource']));
12690: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 12691:
1.60 www 12692: # ------------------------------------------------------ third, check map parms
1.218 albertel 12693: my %parmhash=();
12694: my $thisparm='';
12695: if (tie(%parmhash,'GDBM_File',
1.620 albertel 12696: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 12697: &GDBM_READER(),0640)) {
1.218 albertel 12698: $thisparm=$parmhash{$symbparm};
12699: untie(%parmhash);
12700: }
1.927 albertel 12701: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 12702: }
1.594 albertel 12703: # ------------------------------------------ fourth, look in resource metadata
1.71 www 12704:
1.1172.2.146. .13(raeb 12705:-23): my $what = $spacequalifierrest;
12706:-23): $what=~s/\./\_/;
12707:-23): my $filename;
1.282 albertel 12708: if (!$symbparm) { $symbparm=&symbread(); }
12709: if ($symbparm) {
1.409 www 12710: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 12711: } else {
1.620 albertel 12712: $filename=$env{'request.filename'};
1.282 albertel 12713: }
1.1172.2.146. .13(raeb 12714:-23): my $toolsymb;
12715:-23): if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12716:-23): $toolsymb = $symbparm;
12717:-23): }
12718:-23): my $metadata=&metadata($filename,$what,$toolsymb);
1.927 albertel 12719: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1172.2.146. .13(raeb 12720:-23): $metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
1.927 albertel 12721: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 12722:
1.1172.2.146. .13(raeb 12723:-23): # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 12724: if ($symbparm && defined($courseid) &&
1.620 albertel 12725: $courseid eq $env{'request.course.id'}) {
1.624 albertel 12726: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12727: $env{'course.'.$courseid.'.domain'},
12728: 'course',
1.927 albertel 12729: ([$courselevelm,'map' ],
12730: [$courselevel, 'course']));
12731: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 12732: }
1.145 www 12733: # ------------------------------------------------------------------ Cascade up
1.218 albertel 12734: unless ($space eq '0') {
1.336 albertel 12735: my @parts=split(/_/,$space);
12736: my $id=pop(@parts);
12737: my $part=join('_',@parts);
12738: if ($part eq '') { $part='0'; }
1.927 albertel 12739: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 12740: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 12741: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 12742: }
1.395 albertel 12743: if ($recurse) { return undef; }
1.1172.2.146. .13(raeb 12744:-23): my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
1.927 albertel 12745: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 12746: # ---------------------------------------------------- Any other user namespace
12747: } elsif ($realm eq 'environment') {
12748: # ----------------------------------------------------------------- environment
1.620 albertel 12749: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12750: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 12751: } else {
1.770 albertel 12752: if ($uname eq 'anonymous' && $udom eq '') {
12753: return '';
12754: }
1.219 albertel 12755: my %returnhash=&userenvironment($udom,$uname,
12756: $spacequalifierrest);
12757: return $returnhash{$spacequalifierrest};
12758: }
1.28 www 12759: } elsif ($realm eq 'system') {
1.48 www 12760: # ----------------------------------------------------------------- system.time
12761: if ($space eq 'time') {
12762: return time;
12763: }
1.696 albertel 12764: } elsif ($realm eq 'server') {
12765: # ----------------------------------------------------------------- system.time
12766: if ($space eq 'name') {
12767: return $ENV{'SERVER_NAME'};
12768: }
1.1172.2.146. .1(raebu 12769:22): } elsif ($realm eq 'client') {
12770:22): if ($space eq 'remote_addr') {
12771:22): return &get_requestor_ip();
12772:22): }
1.28 www 12773: }
1.48 www 12774: return '';
1.61 www 12775: }
12776:
1.927 albertel 12777: sub get_reply {
12778: my ($reply_value) = @_;
1.940 raeburn 12779: if (ref($reply_value) eq 'ARRAY') {
12780: if (wantarray) {
12781: return @$reply_value;
12782: }
12783: return $reply_value->[0];
12784: } else {
12785: return $reply_value;
1.927 albertel 12786: }
12787: }
12788:
1.691 raeburn 12789: sub check_group_parms {
12790: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
12791: my @groupitems = ();
12792: my $resultitem;
1.927 albertel 12793: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 12794: foreach my $group (@{$groups}) {
12795: foreach my $level (@levels) {
1.927 albertel 12796: my $item = $courseid.'.['.$group.'].'.$level->[0];
12797: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 12798: }
12799: }
12800: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12801: $env{'course.'.$courseid.'.domain'},
12802: 'course',@groupitems);
12803: return $coursereply;
12804: }
12805:
1.1172.2.146. .1(raebu 12806:22): sub get_map_hierarchy {
12807:22): my ($mapname,$courseid) = @_;
12808:22): my @recurseup = ();
12809:22): if ($mapname) {
12810:22): if (($cachedmapkey eq $courseid) &&
12811:22): (abs($cachedmaptime-time)<5)) {
12812:22): if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12813:22): return @{$cachedmaps{$mapname}};
12814:22): }
12815:22): }
12816:22): my $navmap = Apache::lonnavmaps::navmap->new();
12817:22): if (ref($navmap)) {
12818:22): @recurseup = $navmap->recurseup_maps($mapname);
12819:22): undef($navmap);
12820:22): $cachedmaps{$mapname} = \@recurseup;
12821:22): $cachedmaptime=time;
12822:22): $cachedmapkey=$courseid;
12823:22): }
12824:22): }
12825:22): return @recurseup;
12826:22): }
12827:22):
.2(raebu 12828:22): }
.1(raebu 12829:22):
1.691 raeburn 12830: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 12831: my ($courseid,@groups) = @_;
12832: @groups = sort(@groups);
1.691 raeburn 12833: return @groups;
12834: }
12835:
1.395 albertel 12836: sub packages_tab_default {
1.1172.2.146. .13(raeb 12837:-23): my ($uri,$varname,$toolsymb)=@_;
1.395 albertel 12838: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 12839:
12840: my (@extension,@specifics,$do_default);
1.1172.2.146. .13(raeb 12841:-23): foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
1.395 albertel 12842: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 12843: if ($pack_type eq 'default') {
12844: $do_default=1;
12845: } elsif ($pack_type eq 'extension') {
12846: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 12847: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 12848: # only look at packages defaults for packages that this id is
1.738 albertel 12849: push(@specifics,[$package,$pack_type,$pack_part]);
12850: }
12851: }
12852: # first look for a package that matches the requested part id
12853: foreach my $package (@specifics) {
12854: my (undef,$pack_type,$pack_part)=@{$package};
12855: next if ($pack_part ne $part);
12856: if (defined($packagetab{"$pack_type&$name&default"})) {
12857: return $packagetab{"$pack_type&$name&default"};
12858: }
12859: }
12860: # look for any possible matching non extension_ package
12861: foreach my $package (@specifics) {
12862: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 12863: if (defined($packagetab{"$pack_type&$name&default"})) {
12864: return $packagetab{"$pack_type&$name&default"};
12865: }
1.585 albertel 12866: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 12867: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12868: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 12869: }
12870: }
1.738 albertel 12871: # look for any posible extension_ match
12872: foreach my $package (@extension) {
12873: my ($package,$pack_type)=@{$package};
12874: if (defined($packagetab{"$pack_type&$name&default"})) {
12875: return $packagetab{"$pack_type&$name&default"};
12876: }
12877: if (defined($packagetab{$package."&$name&default"})) {
12878: return $packagetab{$package."&$name&default"};
12879: }
12880: }
12881: # look for a global default setting
12882: if ($do_default && defined($packagetab{"default&$name&default"})) {
12883: return $packagetab{"default&$name&default"};
12884: }
1.395 albertel 12885: return undef;
12886: }
12887:
1.334 albertel 12888: sub add_prefix_and_part {
12889: my ($prefix,$part)=@_;
12890: my $keyroot;
12891: if (defined($prefix) && $prefix !~ /^__/) {
12892: # prefix that has a part already
12893: $keyroot=$prefix;
12894: } elsif (defined($prefix)) {
12895: # prefix that is missing a part
12896: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12897: } else {
12898: # no prefix at all
12899: if (defined($part)) { $keyroot='_'.$part; }
12900: }
12901: return $keyroot;
12902: }
12903:
1.71 www 12904: # ---------------------------------------------------------------- Get metadata
12905:
1.599 albertel 12906: my %metaentry;
1.1070 www 12907: my %importedpartids;
1.1172.2.99 raeburn 12908: my %importedrespids;
1.71 www 12909: sub metadata {
1.1172.2.146. .13(raeb 12910:-23): my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
1.71 www 12911: $uri=&declutter($uri);
1.288 albertel 12912: # if it is a non metadata possible uri return quickly
1.529 albertel 12913: if (($uri eq '') ||
12914: (($uri =~ m|^/*adm/|) &&
1.1172.2.146. .1(raebu 12915:22): ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 12916: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 12917: return undef;
12918: }
1.1172.2.49 raeburn 12919: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 12920: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 12921: return undef;
1.288 albertel 12922: }
1.73 www 12923: my $filename=$uri;
12924: $uri=~s/\.meta$//;
1.172 www 12925: #
12926: # Is the metadata already cached?
1.177 www 12927: # Look at timestamp of caching
1.172 www 12928: # Everything is cached by the main uri, libraries are never directly cached
12929: #
1.428 albertel 12930: if (!defined($liburi)) {
1.599 albertel 12931: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 12932: if (defined($cached)) { return $result->{':'.$what}; }
12933: }
1.1172.2.146. .13(raeb 12934:-23):
12935:-23): #
12936:-23): # If the uri is for an external tool the file from
12937:-23): # which metadata should be retrieved depends on whether
12938:-23): # the tool had been configured to be gradable (set in the Course
12939:-23): # Editor or Resource Editor).
12940:-23): #
12941:-23): # If a valid symb has been included as the third arg in the call
12942:-23): # to &metadata() that can be used to retrieve the value of
12943:-23): # parameter_0_gradable set for the resource, and included in the
12944:-23): # uploaded map containing the tool. The value is retrieved via
12945:-23): # &EXT(), if a valid symb is available. Otherwise the value of
12946:-23): # gradable in the exttool_$marker.db file for the tool instance
12947:-23): # is retrieved via &get().
12948:-23): #
12949:-23): # When lonuserstate::traceroute() calls lonnet::EXT() for
12950:-23): # hiddenresource and encrypturl (during course initialization)
12951:-23): # the map-level parameter for resource.0.gradable included in the
12952:-23): # uploaded map containing the tool will not yet have been stored
12953:-23): # in the user_course_parms.db file for the user's session, so in
12954:-23): # this case fall back to retrieving gradable status from the
12955:-23): # exttool_$marker.db file.
12956:-23): #
12957:-23): # In order to avoid an infinite loop, &metadata() will return
12958:-23): # before a call to &EXT(), if the uri is for an external tool
12959:-23): # and the $what for which metadata is being requested is
12960:-23): # parameter_0_gradable or 0_gradable.
12961:-23): #
12962:-23):
12963:-23): if ($uri =~ /ext\.tool$/) {
12964:-23): if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12965:-23): return;
12966:-23): } else {
12967:-23): my ($checked,$use_passback);
12968:-23): if ($toolsymb ne '') {
12969:-23): (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12970:-23): if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12971:-23): $checked = 1;
12972:-23): if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12973:-23): $use_passback = 1;
12974:-23): }
12975:-23): }
12976:-23): }
12977:-23): unless ($checked) {
12978:-23): my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12979:-23): $marker=~s/\D//g;
12980:-23): if ($marker) {
12981:-23): my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12982:-23): $use_passback = $toolsettings{'gradable'};
12983:-23): }
12984:-23): }
12985:-23): if ($use_passback) {
12986:-23): $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12987:-23): } else {
12988:-23): $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12989:-23): }
12990:-23): }
12991:-23): }
12992:-23):
1.428 albertel 12993: {
1.1069 www 12994: # Imported parts would go here
1.1172.2.99 raeburn 12995: my @origfiletagids=();
1.1069 www 12996: my $importedparts=0;
1.1172.2.99 raeburn 12997:
12998: # Imported responseids would go here
12999: my $importedresponses=0;
1.172 www 13000: #
13001: # Is this a recursive call for a library?
13002: #
1.599 albertel 13003: # if (! exists($metacache{$uri})) {
13004: # $metacache{$uri}={};
13005: # }
1.924 albertel 13006: my $cachetime = 60*60;
1.171 www 13007: if ($liburi) {
13008: $liburi=&declutter($liburi);
13009: $filename=$liburi;
1.401 bowersj2 13010: } else {
1.599 albertel 13011: &devalidate_cache_new('meta',$uri);
13012: undef(%metaentry);
1.401 bowersj2 13013: }
1.140 www 13014: my %metathesekeys=();
1.73 www 13015: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 13016: my $metastring;
1.1140 www 13017: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 13018: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 13019: $metastring =
1.929 albertel 13020: &Apache::lonnet::ssi_body($which,
1.924 albertel 13021: ('grade_target' => 'meta'));
13022: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 13023: } elsif (($uri !~ m -^(editupload)/-) &&
13024: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 13025: my $file=&filelocation('',&clutter($filename));
1.599 albertel 13026: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 13027: $metastring=&getfile($file);
1.489 albertel 13028: }
1.208 albertel 13029: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 13030: my $token;
1.140 www 13031: undef %metathesekeys;
1.71 www 13032: while ($token=$parser->get_token) {
1.339 albertel 13033: if ($token->[0] eq 'S') {
13034: if (defined($token->[2]->{'package'})) {
1.172 www 13035: #
13036: # This is a package - get package info
13037: #
1.339 albertel 13038: my $package=$token->[2]->{'package'};
13039: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13040: if (defined($token->[2]->{'id'})) {
13041: $keyroot.='_'.$token->[2]->{'id'};
13042: }
1.599 albertel 13043: if ($metaentry{':packages'}) {
13044: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 13045: } else {
1.599 albertel 13046: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 13047: }
1.736 albertel 13048: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 13049: my $part=$keyroot;
13050: $part=~s/^\_//;
1.736 albertel 13051: if ($pack_entry=~/^\Q$package\E\&/ ||
13052: $pack_entry=~/^\Q$package\E_0\&/) {
13053: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 13054: # ignore package.tab specified default values
13055: # here &package_tab_default() will fetch those
13056: if ($subp eq 'default') { next; }
1.736 albertel 13057: my $value=$packagetab{$pack_entry};
1.432 albertel 13058: my $unikey;
13059: if ($pack =~ /_0$/) {
13060: $unikey='parameter_0_'.$name;
13061: $part=0;
13062: } else {
13063: $unikey='parameter'.$keyroot.'_'.$name;
13064: }
1.339 albertel 13065: if ($subp eq 'display') {
13066: $value.=' [Part: '.$part.']';
13067: }
1.599 albertel 13068: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 13069: $metathesekeys{$unikey}=1;
1.599 albertel 13070: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13071: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 13072: }
1.599 albertel 13073: if (defined($metaentry{':'.$unikey.'.default'})) {
13074: $metaentry{':'.$unikey}=
13075: $metaentry{':'.$unikey.'.default'};
1.356 albertel 13076: }
1.339 albertel 13077: }
13078: }
13079: } else {
1.172 www 13080: #
13081: # This is not a package - some other kind of start tag
1.339 albertel 13082: #
13083: my $entry=$token->[1];
1.1068 www 13084: my $unikey='';
1.175 www 13085:
1.339 albertel 13086: if ($entry eq 'import') {
1.175 www 13087: #
13088: # Importing a library here
1.339 albertel 13089: #
1.1067 www 13090: my $location=$parser->get_text('/import');
13091: my $dir=$filename;
13092: $dir=~s|[^/]*$||;
13093: $location=&filelocation($dir,$location);
1.1172.2.99 raeburn 13094:
13095: my $importid=$token->[2]->{'id'};
1.1068 www 13096: my $importmode=$token->[2]->{'importmode'};
1.1172.2.99 raeburn 13097: #
13098: # Check metadata for imported file to
13099: # see if it contained response items
13100: #
13101: my %currmetaentry = %metaentry;
13102: my $libresponseorder = &metadata($location,'responseorder');
13103: my $origfile;
13104: if ($libresponseorder ne '') {
13105: if ($#origfiletagids<0) {
13106: undef(%importedrespids);
13107: undef(%importedpartids);
13108: }
13109: @{$importedrespids{$importid}} = split(/\s*,\s*/,$libresponseorder);
13110: if (@{$importedrespids{$importid}} > 0) {
13111: $importedresponses = 1;
13112: # We need to get the original file and the imported file to get the response order correct
13113: # Load and inspect original file
13114: if ($#origfiletagids<0) {
13115: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13116: $origfile=&getfile($origfilelocation);
13117: @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13118: }
13119: }
13120: }
13121: # Do not overwrite contents of %metaentry hash for resource itself with
13122: # hash populated for imported library file
13123: %metaentry = %currmetaentry;
13124: undef(%currmetaentry);
1.1068 www 13125: if ($importmode eq 'problem') {
1.1069 www 13126: # Import as problem/response
1.1068 www 13127: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13128: } elsif ($importmode eq 'part') {
13129: # Import as part(s)
1.1069 www 13130: $importedparts=1;
13131: # We need to get the original file and the imported file to get the part order correct
13132: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
1.1172.2.99 raeburn 13133: # Load and inspect original file if we didn't do that already
13134: if ($#origfiletagids<0) {
13135: undef(%importedrespids);
13136: undef(%importedpartids);
13137: if ($origfile eq '') {
13138: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13139: $origfile=&getfile($origfilelocation);
13140: @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13141: }
1.1070 www 13142: }
13143:
1.1069 www 13144: # Load and inspect imported file
13145: my $impfile=&getfile($location);
13146: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13147: if ($#impfilepartids>=0) {
13148: # This problem had parts
1.1070 www 13149: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 13150: } else {
13151: # Importing by turning a single problem into a problem part
13152: # It gets the import-tags ID as part-ID
13153: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 13154: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 13155: }
1.1068 www 13156: } else {
13157: # Normal import
13158: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13159: if (defined($token->[2]->{'id'})) {
13160: $unikey.='_'.$token->[2]->{'id'};
13161: }
1.1067 www 13162: }
13163:
1.339 albertel 13164: if ($depthcount<20) {
1.736 albertel 13165: my $metadata =
1.1172.2.146. .13(raeb 13166:-23): &metadata($uri,'keys',$toolsymb,$location,$unikey,
1.736 albertel 13167: $depthcount+1);
13168: foreach my $meta (split(',',$metadata)) {
13169: $metaentry{':'.$meta}=$metaentry{':'.$meta};
13170: $metathesekeys{$meta}=1;
1.339 albertel 13171: }
1.1068 www 13172:
13173: }
1.1067 www 13174: } else {
13175: #
13176: # Not importing, some other kind of non-package, non-library start tag
13177: #
13178: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13179: if (defined($token->[2]->{'id'})) {
13180: $unikey.='_'.$token->[2]->{'id'};
13181: }
1.339 albertel 13182: if (defined($token->[2]->{'name'})) {
13183: $unikey.='_'.$token->[2]->{'name'};
13184: }
13185: $metathesekeys{$unikey}=1;
1.736 albertel 13186: foreach my $param (@{$token->[3]}) {
13187: $metaentry{':'.$unikey.'.'.$param} =
13188: $token->[2]->{$param};
1.339 albertel 13189: }
13190: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 13191: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 13192: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13193: # only ws inside the tag, and not in default, so use default
13194: # as value
1.599 albertel 13195: $metaentry{':'.$unikey}=$default;
1.908 albertel 13196: } elsif ( $internaltext =~ /\S/ ) {
13197: # something interesting inside the tag
13198: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 13199: } else {
1.908 albertel 13200: # no interesting values, don't set a default
1.339 albertel 13201: }
1.172 www 13202: # end of not-a-package not-a-library import
1.339 albertel 13203: }
1.172 www 13204: # end of not-a-package start tag
1.339 albertel 13205: }
1.172 www 13206: # the next is the end of "start tag"
1.339 albertel 13207: }
13208: }
1.483 albertel 13209: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 13210: $extension = lc($extension);
13211: if ($extension eq 'htm') { $extension='html'; }
13212:
1.737 albertel 13213: foreach my $key (keys(%packagetab)) {
1.483 albertel 13214: #no specific packages #how's our extension
13215: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 13216: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 13217: \%metathesekeys);
13218: }
1.883 albertel 13219:
13220: if (!exists($metaentry{':packages'})
13221: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 13222: foreach my $key (keys(%packagetab)) {
1.483 albertel 13223: #no specific packages well let's get default then
13224: if ($key!~/^default&/) { next; }
1.488 albertel 13225: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 13226: \%metathesekeys);
13227: }
13228: }
1.338 www 13229: # are there custom rights to evaluate
1.599 albertel 13230: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 13231:
1.338 www 13232: #
13233: # Importing a rights file here
1.339 albertel 13234: #
13235: unless ($depthcount) {
1.599 albertel 13236: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 13237: my $dir=$filename;
13238: $dir=~s|[^/]*$||;
13239: $location=&filelocation($dir,$location);
1.736 albertel 13240: my $rights_metadata =
1.1172.2.146. .13(raeb 13241:-23): &metadata($uri,'keys',$toolsymb,$location,'_rights',
1.736 albertel 13242: $depthcount+1);
13243: foreach my $rights (split(',',$rights_metadata)) {
13244: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13245: $metathesekeys{$rights}=1;
1.339 albertel 13246: }
13247: }
13248: }
1.737 albertel 13249: # uniqifiy package listing
13250: my %seen;
13251: my @uniq_packages =
13252: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13253: $metaentry{':packages'} = join(',',@uniq_packages);
13254:
1.1172.2.99 raeburn 13255: if (($importedresponses) || ($importedparts)) {
13256: if ($importedparts) {
1.1070 www 13257: # We had imported parts and need to rebuild partorder
1.1172.2.99 raeburn 13258: $metaentry{':partorder'}='';
13259: $metathesekeys{'partorder'}=1;
13260: }
13261: if ($importedresponses) {
13262: # We had imported responses and need to rebuild responseorder
13263: $metaentry{':responseorder'}='';
13264: $metathesekeys{'responseorder'}=1;
13265: }
13266: for (my $index=0;$index<$#origfiletagids;$index+=2) {
13267: my $origid = $origfiletagids[$index+1];
13268: if ($origfiletagids[$index] eq 'part') {
13269: # Original part, part of the problem
13270: if ($importedparts) {
13271: $metaentry{':partorder'}.=','.$origid;
13272: }
13273: } elsif ($origfiletagids[$index] eq 'import') {
13274: if ($importedparts) {
13275: # We have imported parts at this position
13276: $metaentry{':partorder'}.=','.$importedpartids{$origid};
13277: }
13278: if ($importedresponses) {
13279: # We have imported responses at this position
13280: if (ref($importedrespids{$origid}) eq 'ARRAY') {
13281: $metaentry{':responseorder'}.=','.join(',',map { $origid.'_'.$_ } @{$importedrespids{$origid}});
13282: }
13283: }
13284: } else {
13285: # Original response item, part of the problem
13286: if ($importedresponses) {
13287: $metaentry{':responseorder'}.=','.$origid;
13288: }
13289: }
13290: }
13291: if ($importedparts) {
13292: $metaentry{':partorder'}=~s/^\,//;
13293: }
13294: if ($importedresponses) {
13295: $metaentry{':responseorder'}=~s/^\,//;
13296: }
1.1070 www 13297: }
13298:
1.737 albertel 13299: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 13300: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1172.2.58 raeburn 13301: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 13302: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 13303: # this is the end of "was not already recently cached
1.71 www 13304: }
1.599 albertel 13305: return $metaentry{':'.$what};
1.261 albertel 13306: }
13307:
1.488 albertel 13308: sub metadata_create_package_def {
1.483 albertel 13309: my ($uri,$key,$package,$metathesekeys)=@_;
13310: my ($pack,$name,$subp)=split(/\&/,$key);
13311: if ($subp eq 'default') { next; }
13312:
1.599 albertel 13313: if (defined($metaentry{':packages'})) {
13314: $metaentry{':packages'}.=','.$package;
1.483 albertel 13315: } else {
1.599 albertel 13316: $metaentry{':packages'}=$package;
1.483 albertel 13317: }
13318: my $value=$packagetab{$key};
13319: my $unikey;
13320: $unikey='parameter_0_'.$name;
1.599 albertel 13321: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 13322: $$metathesekeys{$unikey}=1;
1.599 albertel 13323: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13324: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 13325: }
1.599 albertel 13326: if (defined($metaentry{':'.$unikey.'.default'})) {
13327: $metaentry{':'.$unikey}=
13328: $metaentry{':'.$unikey.'.default'};
1.483 albertel 13329: }
13330: }
13331:
1.261 albertel 13332: sub metadata_generate_part0 {
13333: my ($metadata,$metacache,$uri) = @_;
13334: my %allnames;
1.737 albertel 13335: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 13336: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 13337: my $part=$$metacache{':'.$metakey.'.part'};
13338: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 13339: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 13340: $allnames{$name}=$part;
13341: }
13342: }
13343: }
13344: foreach my $name (keys(%allnames)) {
13345: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 13346: my $key=":parameter_0_$name";
1.261 albertel 13347: $$metacache{"$key.part"}='0';
13348: $$metacache{"$key.name"}=$name;
1.428 albertel 13349: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 13350: $allnames{$name}.'_'.$name.
13351: '.type'};
1.428 albertel 13352: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 13353: '.display'};
1.644 www 13354: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 13355: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 13356: $$metacache{"$key.display"}=$olddis;
13357: }
1.71 www 13358: }
13359:
1.764 albertel 13360: # ------------------------------------------------------ Devalidate title cache
13361:
13362: sub devalidate_title_cache {
13363: my ($url)=@_;
13364: if (!$env{'request.course.id'}) { return; }
13365: my $symb=&symbread($url);
13366: if (!$symb) { return; }
13367: my $key=$env{'request.course.id'}."\0".$symb;
13368: &devalidate_cache_new('title',$key);
13369: }
13370:
1.1014 droeschl 13371: # ------------------------------------------------- Get the title of a course
13372:
13373: sub current_course_title {
13374: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13375: }
1.301 www 13376: # ------------------------------------------------- Get the title of a resource
13377:
13378: sub gettitle {
13379: my $urlsymb=shift;
13380: my $symb=&symbread($urlsymb);
1.534 albertel 13381: if ($symb) {
1.620 albertel 13382: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 13383: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 13384: if (defined($cached)) {
13385: return $result;
13386: }
1.534 albertel 13387: my ($map,$resid,$url)=&decode_symb($symb);
13388: my $title='';
1.907 albertel 13389: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13390: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13391: } else {
13392: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13393: &GDBM_READER(),0640)) {
13394: my $mapid=$bighash{'map_pc_'.&clutter($map)};
13395: $title=$bighash{'title_'.$mapid.'.'.$resid};
13396: untie(%bighash);
13397: }
1.534 albertel 13398: }
13399: $title=~s/\&colon\;/\:/gs;
13400: if ($title) {
1.1159 www 13401: # Remember both $symb and $title for dynamic metadata
13402: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 13403: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 13404: # Cache this title and then return it
1.599 albertel 13405: return &do_cache_new('title',$key,$title,600);
1.534 albertel 13406: }
13407: $urlsymb=$url;
13408: }
13409: my $title=&metadata($urlsymb,'title');
13410: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
13411: return $title;
1.301 www 13412: }
1.613 albertel 13413:
1.614 albertel 13414: sub get_slot {
13415: my ($which,$cnum,$cdom)=@_;
13416: if (!$cnum || !$cdom) {
1.790 albertel 13417: (undef,my $courseid)=&whichuser();
1.620 albertel 13418: $cdom=$env{'course.'.$courseid.'.domain'};
13419: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 13420: }
1.703 albertel 13421: my $key=join("\0",'slots',$cdom,$cnum,$which);
13422: my %slotinfo;
13423: if (exists($remembered{$key})) {
13424: $slotinfo{$which} = $remembered{$key};
13425: } else {
13426: %slotinfo=&get('slots',[$which],$cdom,$cnum);
13427: &Apache::lonhomework::showhash(%slotinfo);
13428: my ($tmp)=keys(%slotinfo);
13429: if ($tmp=~/^error:/) { return (); }
13430: $remembered{$key} = $slotinfo{$which};
13431: }
1.616 albertel 13432: if (ref($slotinfo{$which}) eq 'HASH') {
13433: return %{$slotinfo{$which}};
13434: }
13435: return $slotinfo{$which};
1.614 albertel 13436: }
1.1150 raeburn 13437:
13438: sub get_reservable_slots {
13439: my ($cnum,$cdom,$uname,$udom) = @_;
13440: my $now = time;
13441: my $reservable_info;
13442: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13443: if (exists($remembered{$key})) {
13444: $reservable_info = $remembered{$key};
13445: } else {
13446: my %resv;
13447: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13448: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13449: $reservable_info = \%resv;
13450: $remembered{$key} = $reservable_info;
13451: }
13452: return $reservable_info;
13453: }
13454:
13455: sub get_course_slots {
13456: my ($cnum,$cdom) = @_;
13457: my $hashid=$cnum.':'.$cdom;
13458: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13459: if (defined($cached)) {
13460: if (ref($result) eq 'HASH') {
13461: return %{$result};
13462: }
13463: } else {
13464: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13465: my ($tmp) = keys(%slots);
13466: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1172.2.23 raeburn 13467: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 13468: return %slots;
13469: }
13470: }
13471: return;
13472: }
13473:
13474: sub devalidate_slots_cache {
13475: my ($cnum,$cdom)=@_;
13476: my $hashid=$cnum.':'.$cdom;
13477: &devalidate_cache_new('allslots',$hashid);
13478: }
13479:
1.1172.2.8 raeburn 13480: sub get_coursechange {
13481: my ($cdom,$cnum) = @_;
13482: if ($cdom eq '' || $cnum eq '') {
13483: return unless ($env{'request.course.id'});
13484: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13485: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13486: }
13487: my $hashid=$cdom.'_'.$cnum;
13488: my ($change,$cached)=&is_cached_new('crschange',$hashid);
13489: if ((defined($cached)) && ($change ne '')) {
13490: return $change;
13491: } else {
13492: my %crshash;
13493: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13494: if ($crshash{'internal.contentchange'} eq '') {
13495: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13496: if ($change eq '') {
13497: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13498: $change = $crshash{'internal.created'};
13499: }
13500: } else {
13501: $change = $crshash{'internal.contentchange'};
13502: }
13503: my $cachetime = 600;
13504: &do_cache_new('crschange',$hashid,$change,$cachetime);
13505: }
13506: return $change;
13507: }
13508:
13509: sub devalidate_coursechange_cache {
1.1172.2.146. .9(raebu 13510:23): my ($cdom,$cnum)=@_;
13511:23): my $hashid=$cdom.'_'.$cnum;
1.1172.2.8 raeburn 13512: &devalidate_cache_new('crschange',$hashid);
13513: }
13514:
1.1172.2.146. .9(raebu 13515:23): sub get_suppchange {
13516:23): my ($cdom,$cnum) = @_;
13517:23): if ($cdom eq '' || $cnum eq '') {
13518:23): return unless ($env{'request.course.id'});
13519:23): $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13520:23): $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13521:23): }
13522:23): my $hashid=$cdom.'_'.$cnum;
13523:23): my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13524:23): if ((defined($cached)) && ($change ne '')) {
13525:23): return $change;
13526:23): } else {
13527:23): my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13528:23): if ($crshash{'internal.supplementalchange'} eq '') {
13529:23): $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13530:23): if ($change eq '') {
13531:23): %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13532:23): $change = $crshash{'internal.created'};
13533:23): }
13534:23): } else {
13535:23): $change = $crshash{'internal.supplementalchange'};
13536:23): }
13537:23): my $cachetime = 600;
13538:23): &do_cache_new('suppchange',$hashid,$change,$cachetime);
13539:23): }
13540:23): return $change;
13541:23): }
13542:23):
13543:23): sub devalidate_suppchange_cache {
13544:23): my ($cdom,$cnum)=@_;
13545:23): my $hashid=$cdom.'_'.$cnum;
13546:23): &devalidate_cache_new('suppchange',$hashid);
13547:23): }
13548:23):
13549:23): sub update_supp_caches {
13550:23): my ($cdom,$cnum) = @_;
13551:23): my %servers = &internet_dom_servers($cdom);
13552:23): my @ids=¤t_machine_ids();
13553:23): foreach my $server (keys(%servers)) {
13554:23): next if (grep(/^\Q$server\E$/,@ids));
13555:23): my $hashid=$cnum.':'.$cdom;
13556:23): my $cachekey = &escape('showsupp').':'.&escape($hashid);
13557:23): &remote_devalidate_cache($server,[$cachekey]);
13558:23): }
13559:23): &has_unhidden_suppfiles($cnum,$cdom,1,1);
13560:23): &count_supptools($cnum,$cdom,1);
13561:23): my $now = time;
13562:23): if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13563:23): &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13564:23): }
13565:23): &put('environment',{'internal.supplementalchange' => $now},
13566:23): $cdom,$cnum);
13567:23): &Apache::lonnet::appenv(
13568:23): {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13569:23): &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13570:23): }
13571:23):
1.31 www 13572: # ------------------------------------------------- Update symbolic store links
13573:
13574: sub symblist {
13575: my ($mapname,%newhash)=@_;
1.438 www 13576: $mapname=&deversion(&declutter($mapname));
1.31 www 13577: my %hash;
1.620 albertel 13578: if (($env{'request.course.fn'}) && (%newhash)) {
13579: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 13580: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 13581: foreach my $url (keys(%newhash)) {
1.711 albertel 13582: next if ($url eq 'last_known'
13583: && $env{'form.no_update_last_known'});
13584: $hash{declutter($url)}=&encode_symb($mapname,
13585: $newhash{$url}->[1],
13586: $newhash{$url}->[0]);
1.191 harris41 13587: }
1.31 www 13588: if (untie(%hash)) {
13589: return 'ok';
13590: }
13591: }
13592: }
13593: return 'error';
1.212 www 13594: }
13595:
13596: # --------------------------------------------------------------- Verify a symb
13597:
13598: sub symbverify {
1.1172.2.11 raeburn 13599: my ($symb,$thisurl,$encstate)=@_;
1.510 www 13600: my $thisfn=$thisurl;
1.439 www 13601: $thisfn=&declutter($thisfn);
1.215 www 13602: # direct jump to resource in page or to a sequence - will construct own symbs
13603: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13604: # check URL part
1.409 www 13605: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 13606:
1.431 www 13607: unless ($url eq $thisfn) { return 0; }
1.213 www 13608:
1.216 www 13609: $symb=&symbclean($symb);
1.510 www 13610: $thisurl=&deversion($thisurl);
1.439 www 13611: $thisfn=&deversion($thisfn);
1.213 www 13612:
13613: my %bighash;
13614: my $okay=0;
1.431 www 13615:
1.620 albertel 13616: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13617: &GDBM_READER(),0640)) {
1.1032 raeburn 13618: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13619: $thisurl =~ s/\?.+$//;
1.1172.2.13 raeburn 13620: if ($map =~ m{^uploaded/.+\.page$}) {
13621: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13622: $thisurl =~ s{^\Qhttp://https://\E}{https://};
13623: }
13624: }
13625: my $ids;
1.1172.2.122 raeburn 13626: if ($map =~ m{^uploaded/.+\.page$}) {
13627: $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
1.1172.2.13 raeburn 13628: } else {
13629: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 13630: }
1.1102 raeburn 13631: unless ($ids) {
1.1172.2.13 raeburn 13632: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
1.1102 raeburn 13633: $ids=$bighash{$idkey};
1.216 www 13634: }
13635: if ($ids) {
13636: # ------------------------------------------------------------------- Has ID(s)
1.1172.2.13 raeburn 13637: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13638: $symb =~ s/\?.+$//;
13639: }
1.800 albertel 13640: foreach my $id (split(/\,/,$ids)) {
13641: my ($mapid,$resid)=split(/\./,$id);
1.216 www 13642: if (
13643: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1172.2.13 raeburn 13644: eq $symb) {
1.1172.2.11 raeburn 13645: if (ref($encstate)) {
13646: $$encstate = $bighash{'encrypted_'.$id};
13647: }
1.1172.2.13 raeburn 13648: if (($env{'request.role.adv'}) ||
13649: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
1.1101 raeburn 13650: ($thisurl eq '/adm/navmaps')) {
1.1172.2.13 raeburn 13651: $okay=1;
13652: last;
13653: }
13654: }
13655: }
1.216 www 13656: }
1.213 www 13657: untie(%bighash);
13658: }
13659: return $okay;
1.31 www 13660: }
13661:
1.210 www 13662: # --------------------------------------------------------------- Clean-up symb
13663:
13664: sub symbclean {
13665: my $symb=shift;
1.568 albertel 13666: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 13667: # remove version from map
13668: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 13669:
1.210 www 13670: # remove version from URL
13671: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 13672:
1.507 www 13673: # remove wrapper
13674:
1.510 www 13675: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 13676: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 13677: return $symb;
1.409 www 13678: }
13679:
13680: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 13681:
13682: sub encode_symb {
13683: my ($map,$resid,$url)=@_;
13684: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13685: }
1.409 www 13686:
13687: sub decode_symb {
1.568 albertel 13688: my $symb=shift;
13689: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13690: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 13691: return (&fixversion($map),$resid,&fixversion($url));
13692: }
13693:
13694: sub fixversion {
13695: my $fn=shift;
1.609 banghart 13696: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 13697: my %bighash;
13698: my $uri=&clutter($fn);
1.620 albertel 13699: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 13700: # is this cached?
1.599 albertel 13701: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 13702: if (defined($cached)) { return $result; }
13703: # unfortunately not cached, or expired
1.620 albertel 13704: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 13705: &GDBM_READER(),0640)) {
13706: if ($bighash{'version_'.$uri}) {
13707: my $version=$bighash{'version_'.$uri};
1.444 www 13708: unless (($version eq 'mostrecent') ||
13709: ($version==&getversion($uri))) {
1.440 www 13710: $uri=~s/\.(\w+)$/\.$version\.$1/;
13711: }
13712: }
13713: untie %bighash;
1.413 www 13714: }
1.599 albertel 13715: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 13716: }
13717:
13718: sub deversion {
13719: my $url=shift;
13720: $url=~s/\.\d+\.(\w+)$/\.$1/;
13721: return $url;
1.210 www 13722: }
13723:
1.31 www 13724: # ------------------------------------------------------ Return symb list entry
13725:
13726: sub symbread {
1.1172.2.126 raeburn 13727: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
1.1172.2.128 raeburn 13728: $ignoresymbdb,$noenccheck)=@_;
1.1172.2.54 raeburn 13729: my $cache_str='request.symbread.cached.'.$thisfn;
1.1172.2.128 raeburn 13730: if (defined($env{$cache_str})) {
1.1172.2.126 raeburn 13731: unless (ref($possibles) eq 'HASH') {
13732: if ($ignorecachednull) {
13733: return $env{$cache_str} unless ($env{$cache_str} eq '');
13734: } else {
13735: return $env{$cache_str};
13736: }
1.1172.2.66 raeburn 13737: }
13738: }
1.242 www 13739: # no filename provided? try from environment
1.1172.2.54 raeburn 13740: unless ($thisfn) {
1.620 albertel 13741: if ($env{'request.symb'}) {
1.1172.2.128 raeburn 13742: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.1172.2.13 raeburn 13743: }
13744: $thisfn=$env{'request.filename'};
1.44 www 13745: }
1.569 albertel 13746: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 13747: # is that filename actually a symb? Verify, clean, and return
13748: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 13749: if (&symbverify($thisfn,$1)) {
1.1172.2.128 raeburn 13750: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 13751: }
1.242 www 13752: }
1.44 www 13753: $thisfn=declutter($thisfn);
1.31 www 13754: my %hash;
1.37 www 13755: my %bighash;
13756: my $syval='';
1.620 albertel 13757: if (($env{'request.course.fn'}) && ($thisfn)) {
1.1172.2.128 raeburn 13758: unless ($ignoresymbdb) {
1.1172.2.126 raeburn 13759: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13760: &GDBM_READER(),0640)) {
1.1172.2.146. .7(raebu 13761:22): $syval=$hash{$thisfn};
1.1172.2.126 raeburn 13762: untie(%hash);
13763: }
1.1172.2.128 raeburn 13764: if ($syval && $checkforblock) {
13765: my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
1.1172.2.126 raeburn 13766: if (@blockers) {
13767: $syval='';
13768: }
13769: }
1.37 www 13770: }
13771: # ---------------------------------------------------------- There was an entry
13772: if ($syval) {
1.601 albertel 13773: #unless ($syval=~/\_\d+$/) {
1.620 albertel 13774: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 13775: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13776: #return $env{$cache_str}='';
1.601 albertel 13777: #}
13778: #$syval.=$1;
13779: #}
1.37 www 13780: } else {
13781: # ------------------------------------------------------- Was not in symb table
1.620 albertel 13782: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 13783: &GDBM_READER(),0640)) {
1.37 www 13784: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 13785: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 13786: unless ($ids) {
13787: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 13788: }
13789: unless ($ids) {
13790: # alias?
13791: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 13792: }
1.37 www 13793: if ($ids) {
13794: # ------------------------------------------------------------------- Has ID(s)
13795: my @possibilities=split(/\,/,$ids);
1.39 www 13796: if ($#possibilities==0) {
13797: # ----------------------------------------------- There is only one possibility
1.37 www 13798: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 13799: $syval=&encode_symb($bighash{'map_id_'.$mapid},
13800: $resid,$thisfn);
1.1172.2.66 raeburn 13801: if (ref($possibles) eq 'HASH') {
1.1172.2.126 raeburn 13802: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13803: $possibles->{$syval} = 1;
13804: }
1.1172.2.66 raeburn 13805: }
13806: if ($checkforblock) {
1.1172.2.126 raeburn 13807: unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
1.1172.2.127 raeburn 13808: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
1.1172.2.126 raeburn 13809: if (@blockers) {
13810: $syval = '';
13811: untie(%bighash);
13812: return $env{$cache_str}='';
13813: }
1.1172.2.66 raeburn 13814: }
13815: }
13816: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 13817: # ------------------------------------------ There is more than one possibility
13818: my $realpossible=0;
1.800 albertel 13819: foreach my $id (@possibilities) {
13820: my $file=$bighash{'src_'.$id};
1.1172.2.66 raeburn 13821: my $canaccess;
13822: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13823: $canaccess = 1;
13824: } else {
13825: $canaccess = &allowed('bre',$file);
13826: }
13827: if ($canaccess) {
13828: my ($mapid,$resid)=split(/\./,$id);
13829: if ($bighash{'map_type_'.$mapid} ne 'page') {
13830: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13831: $resid,$thisfn);
1.1172.2.126 raeburn 13832: next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
1.1172.2.127 raeburn 13833: next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
1.1172.2.66 raeburn 13834: if ($checkforblock) {
1.1172.2.127 raeburn 13835: my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
1.1172.2.126 raeburn 13836: if (@blockers > 0) {
13837: $syval = '';
13838: } else {
1.1172.2.66 raeburn 13839: $syval = $poss_syval;
13840: $realpossible++;
13841: }
13842: } else {
13843: $syval = $poss_syval;
13844: $realpossible++;
13845: }
1.1172.2.126 raeburn 13846: if ($syval) {
13847: if (ref($possibles) eq 'HASH') {
13848: $possibles->{$syval} = 1;
13849: }
13850: }
1.1172.2.66 raeburn 13851: }
1.39 www 13852: }
1.191 harris41 13853: }
1.39 www 13854: if ($realpossible!=1) { $syval=''; }
1.249 www 13855: } else {
13856: $syval='';
1.37 www 13857: }
13858: }
1.1172.2.66 raeburn 13859: untie(%bighash);
1.481 raeburn 13860: }
1.31 www 13861: }
1.62 www 13862: if ($syval) {
1.1172.2.128 raeburn 13863: return $env{$cache_str}=$syval;
1.62 www 13864: }
1.31 www 13865: }
1.949 raeburn 13866: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 13867: return $env{$cache_str}='';
1.31 www 13868: }
13869:
13870: # ---------------------------------------------------------- Return random seed
13871:
1.32 www 13872: sub numval {
13873: my $txt=shift;
13874: $txt=~tr/A-J/0-9/;
13875: $txt=~tr/a-j/0-9/;
13876: $txt=~tr/K-T/0-9/;
13877: $txt=~tr/k-t/0-9/;
13878: $txt=~tr/U-Z/0-5/;
13879: $txt=~tr/u-z/0-5/;
13880: $txt=~s/\D//g;
1.564 albertel 13881: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 13882: return int($txt);
1.368 albertel 13883: }
13884:
1.484 albertel 13885: sub numval2 {
13886: my $txt=shift;
13887: $txt=~tr/A-J/0-9/;
13888: $txt=~tr/a-j/0-9/;
13889: $txt=~tr/K-T/0-9/;
13890: $txt=~tr/k-t/0-9/;
13891: $txt=~tr/U-Z/0-5/;
13892: $txt=~tr/u-z/0-5/;
13893: $txt=~s/\D//g;
13894: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13895: my $total;
13896: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 13897: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 13898: return int($total);
13899: }
13900:
1.575 albertel 13901: sub numval3 {
13902: use integer;
13903: my $txt=shift;
13904: $txt=~tr/A-J/0-9/;
13905: $txt=~tr/a-j/0-9/;
13906: $txt=~tr/K-T/0-9/;
13907: $txt=~tr/k-t/0-9/;
13908: $txt=~tr/U-Z/0-5/;
13909: $txt=~tr/u-z/0-5/;
13910: $txt=~s/\D//g;
13911: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13912: my $total;
13913: foreach my $val (@txts) { $total+=$val; }
13914: if ($_64bit) { $total=(($total<<32)>>32); }
13915: return $total;
13916: }
13917:
1.675 albertel 13918: sub digest {
13919: my ($data)=@_;
13920: my $digest=&Digest::MD5::md5($data);
13921: my ($a,$b,$c,$d)=unpack("iiii",$digest);
13922: my ($e,$f);
13923: {
13924: use integer;
13925: $e=($a+$b);
13926: $f=($c+$d);
13927: if ($_64bit) {
13928: $e=(($e<<32)>>32);
13929: $f=(($f<<32)>>32);
13930: }
13931: }
13932: if (wantarray) {
13933: return ($e,$f);
13934: } else {
13935: my $g;
13936: {
13937: use integer;
13938: $g=($e+$f);
13939: if ($_64bit) {
13940: $g=(($g<<32)>>32);
13941: }
13942: }
13943: return $g;
13944: }
13945: }
13946:
1.368 albertel 13947: sub latest_rnd_algorithm_id {
1.675 albertel 13948: return '64bit5';
1.366 albertel 13949: }
1.32 www 13950:
1.503 albertel 13951: sub get_rand_alg {
13952: my ($courseid)=@_;
1.790 albertel 13953: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 13954: if ($courseid) {
1.620 albertel 13955: return $env{"course.$courseid.rndseed"};
1.503 albertel 13956: }
13957: return &latest_rnd_algorithm_id();
13958: }
13959:
1.562 albertel 13960: sub validCODE {
13961: my ($CODE)=@_;
13962: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13963: return 0;
13964: }
13965:
1.491 albertel 13966: sub getCODE {
1.620 albertel 13967: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 13968: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13969: defined($Apache::lonhomework::parsing_a_task) ) &&
13970: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 13971: return $Apache::lonhomework::history{'resource.CODE'};
13972: }
13973: return undef;
13974: }
1.1133 foxr 13975: #
13976: # Determines the random seed for a specific context:
13977: #
13978: # parameters:
13979: # symb - in course context the symb for the seed.
13980: # course_id - The course id of the form domain_coursenum.
13981: # domain - Domain for the user.
13982: # course - Course for the user.
13983: # cenv - environment of the course.
13984: #
13985: # NOTE:
13986: # All parameters are picked out of the environment if missing
13987: # or not defined.
13988: # If a symb cannot be determined the current time is used instead.
13989: #
13990: # For a given well defined symb, courside, domain, username,
13991: # and course environment, the seed is reproducible.
13992: #
1.31 www 13993: sub rndseed {
1.1133 foxr 13994: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 13995: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 13996: if (!defined($symb)) {
1.366 albertel 13997: unless ($symb=$wsymb) { return time; }
13998: }
1.1146 foxr 13999: if (!defined $courseid) {
14000: $courseid=$wcourseid;
14001: }
14002: if (!defined $domain) { $domain=$wdomain; }
14003: if (!defined $username) { $username=$wusername }
1.1133 foxr 14004:
14005: my $which;
14006: if (defined($cenv->{'rndseed'})) {
14007: $which = $cenv->{'rndseed'};
14008: } else {
14009: $which =&get_rand_alg($courseid);
14010: }
1.491 albertel 14011: if (defined(&getCODE())) {
1.675 albertel 14012: if ($which eq '64bit5') {
14013: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14014: } elsif ($which eq '64bit4') {
1.575 albertel 14015: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14016: } else {
14017: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14018: }
1.675 albertel 14019: } elsif ($which eq '64bit5') {
14020: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 14021: } elsif ($which eq '64bit4') {
14022: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 14023: } elsif ($which eq '64bit3') {
14024: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 14025: } elsif ($which eq '64bit2') {
14026: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 14027: } elsif ($which eq '64bit') {
14028: return &rndseed_64bit($symb,$courseid,$domain,$username);
14029: }
14030: return &rndseed_32bit($symb,$courseid,$domain,$username);
14031: }
14032:
14033: sub rndseed_32bit {
14034: my ($symb,$courseid,$domain,$username)=@_;
14035: {
14036: use integer;
14037: my $symbchck=unpack("%32C*",$symb) << 27;
14038: my $symbseed=numval($symb) << 22;
14039: my $namechck=unpack("%32C*",$username) << 17;
14040: my $nameseed=numval($username) << 12;
14041: my $domainseed=unpack("%32C*",$domain) << 7;
14042: my $courseseed=unpack("%32C*",$courseid);
14043: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 14044: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14045: #&logthis("rndseed :$num:$symb");
1.564 albertel 14046: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 14047: return $num;
14048: }
14049: }
14050:
14051: sub rndseed_64bit {
14052: my ($symb,$courseid,$domain,$username)=@_;
14053: {
14054: use integer;
14055: my $symbchck=unpack("%32S*",$symb) << 21;
14056: my $symbseed=numval($symb) << 10;
14057: my $namechck=unpack("%32S*",$username);
14058:
14059: my $nameseed=numval($username) << 21;
14060: my $domainseed=unpack("%32S*",$domain) << 10;
14061: my $courseseed=unpack("%32S*",$courseid);
14062:
14063: my $num1=$symbchck+$symbseed+$namechck;
14064: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14065: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14066: #&logthis("rndseed :$num:$symb");
1.564 albertel 14067: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 14068: return "$num1,$num2";
1.155 albertel 14069: }
1.366 albertel 14070: }
14071:
1.443 albertel 14072: sub rndseed_64bit2 {
14073: my ($symb,$courseid,$domain,$username)=@_;
14074: {
14075: use integer;
14076: # strings need to be an even # of cahracters long, it it is odd the
14077: # last characters gets thrown away
14078: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14079: my $symbseed=numval($symb) << 10;
14080: my $namechck=unpack("%32S*",$username.' ');
14081:
14082: my $nameseed=numval($username) << 21;
1.501 albertel 14083: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14084: my $courseseed=unpack("%32S*",$courseid.' ');
14085:
14086: my $num1=$symbchck+$symbseed+$namechck;
14087: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14088: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14089: #&logthis("rndseed :$num:$symb");
1.803 albertel 14090: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 14091: return "$num1,$num2";
14092: }
14093: }
14094:
14095: sub rndseed_64bit3 {
14096: my ($symb,$courseid,$domain,$username)=@_;
14097: {
14098: use integer;
14099: # strings need to be an even # of cahracters long, it it is odd the
14100: # last characters gets thrown away
14101: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14102: my $symbseed=numval2($symb) << 10;
14103: my $namechck=unpack("%32S*",$username.' ');
14104:
14105: my $nameseed=numval2($username) << 21;
1.443 albertel 14106: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14107: my $courseseed=unpack("%32S*",$courseid.' ');
14108:
14109: my $num1=$symbchck+$symbseed+$namechck;
14110: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14111: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14112: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 14113: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14114:
1.503 albertel 14115: return "$num1:$num2";
1.443 albertel 14116: }
14117: }
14118:
1.575 albertel 14119: sub rndseed_64bit4 {
14120: my ($symb,$courseid,$domain,$username)=@_;
14121: {
14122: use integer;
14123: # strings need to be an even # of cahracters long, it it is odd the
14124: # last characters gets thrown away
14125: my $symbchck=unpack("%32S*",$symb.' ') << 21;
14126: my $symbseed=numval3($symb) << 10;
14127: my $namechck=unpack("%32S*",$username.' ');
14128:
14129: my $nameseed=numval3($username) << 21;
14130: my $domainseed=unpack("%32S*",$domain.' ') << 10;
14131: my $courseseed=unpack("%32S*",$courseid.' ');
14132:
14133: my $num1=$symbchck+$symbseed+$namechck;
14134: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 14135: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14136: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 14137: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 14138:
1.575 albertel 14139: return "$num1:$num2";
14140: }
14141: }
14142:
1.675 albertel 14143: sub rndseed_64bit5 {
14144: my ($symb,$courseid,$domain,$username)=@_;
14145: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14146: return "$num1:$num2";
14147: }
14148:
1.366 albertel 14149: sub rndseed_CODE_64bit {
14150: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 14151: {
1.366 albertel 14152: use integer;
1.443 albertel 14153: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 14154: my $symbseed=numval2($symb);
1.491 albertel 14155: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14156: my $CODEseed=numval(&getCODE());
1.443 albertel 14157: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 14158: my $num1=$symbseed+$CODEchck;
14159: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14160: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14161: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 14162: if ($_64bit) { $num1=(($num1<<32)>>32); }
14163: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 14164: return "$num1:$num2";
1.366 albertel 14165: }
14166: }
14167:
1.575 albertel 14168: sub rndseed_CODE_64bit4 {
14169: my ($symb,$courseid,$domain,$username)=@_;
14170: {
14171: use integer;
14172: my $symbchck=unpack("%32S*",$symb.' ') << 16;
14173: my $symbseed=numval3($symb);
14174: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14175: my $CODEseed=numval3(&getCODE());
14176: my $courseseed=unpack("%32S*",$courseid.' ');
14177: my $num1=$symbseed+$CODEchck;
14178: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 14179: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14180: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 14181: if ($_64bit) { $num1=(($num1<<32)>>32); }
14182: if ($_64bit) { $num2=(($num2<<32)>>32); }
14183: return "$num1:$num2";
14184: }
14185: }
14186:
1.675 albertel 14187: sub rndseed_CODE_64bit5 {
14188: my ($symb,$courseid,$domain,$username)=@_;
14189: my $code = &getCODE();
14190: my ($num1,$num2)=&digest("$symb,$courseid,$code");
14191: return "$num1:$num2";
14192: }
14193:
1.366 albertel 14194: sub setup_random_from_rndseed {
14195: my ($rndseed)=@_;
1.503 albertel 14196: if ($rndseed =~/([,:])/) {
1.1172.2.51 raeburn 14197: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14198: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14199: &Math::Random::random_set_seed_from_phrase($rndseed);
14200: } else {
14201: &Math::Random::random_set_seed($num1,$num2);
14202: }
1.366 albertel 14203: } else {
14204: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 14205: }
1.36 albertel 14206: }
14207:
1.474 albertel 14208: sub latest_receipt_algorithm_id {
1.835 albertel 14209: return 'receipt3';
1.474 albertel 14210: }
14211:
1.480 www 14212: sub recunique {
14213: my $fucourseid=shift;
14214: my $unique;
1.835 albertel 14215: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14216: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14217: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 14218: } else {
14219: $unique=$perlvar{'lonReceipt'};
14220: }
14221: return unpack("%32C*",$unique);
14222: }
14223:
14224: sub recprefix {
14225: my $fucourseid=shift;
14226: my $prefix;
1.835 albertel 14227: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14228: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 14229: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 14230: } else {
14231: $prefix=$perlvar{'lonHostID'};
14232: }
14233: return unpack("%32C*",$prefix);
14234: }
14235:
1.76 www 14236: sub ireceipt {
1.474 albertel 14237: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 14238:
14239: my $return =&recprefix($fucourseid).'-';
14240:
14241: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14242: $env{'request.state'} eq 'construct') {
14243: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14244: return $return;
14245: }
14246:
1.76 www 14247: my $cuname=unpack("%32C*",$funame);
14248: my $cudom=unpack("%32C*",$fudom);
14249: my $cucourseid=unpack("%32C*",$fucourseid);
14250: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 14251: my $cunique=&recunique($fucourseid);
1.474 albertel 14252: my $cpart=unpack("%32S*",$part);
1.835 albertel 14253: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14254:
1.790 albertel 14255: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 14256:
14257: $return.= ($cunique%$cuname+
14258: $cunique%$cudom+
14259: $cusymb%$cuname+
14260: $cusymb%$cudom+
14261: $cucourseid%$cuname+
14262: $cucourseid%$cudom+
14263: $cpart%$cuname+
14264: $cpart%$cudom);
14265: } else {
14266: $return.= ($cunique%$cuname+
14267: $cunique%$cudom+
14268: $cusymb%$cuname+
14269: $cusymb%$cudom+
14270: $cucourseid%$cuname+
14271: $cucourseid%$cudom);
14272: }
14273: return $return;
1.76 www 14274: }
14275:
14276: sub receipt {
1.474 albertel 14277: my ($part)=@_;
1.790 albertel 14278: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 14279: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 14280: }
1.260 ng 14281:
1.790 albertel 14282: sub whichuser {
14283: my ($passedsymb)=@_;
14284: my ($symb,$courseid,$domain,$name,$publicuser);
14285: if (defined($env{'form.grade_symb'})) {
14286: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14287: my $allowed=&allowed('vgr',$tmp_courseid);
14288: if (!$allowed &&
14289: exists($env{'request.course.sec'}) &&
14290: $env{'request.course.sec'} !~ /^\s*$/) {
14291: $allowed=&allowed('vgr',$tmp_courseid.
14292: '/'.$env{'request.course.sec'});
14293: }
14294: if ($allowed) {
14295: ($symb)=&get_env_multiple('form.grade_symb');
14296: $courseid=$tmp_courseid;
14297: ($domain)=&get_env_multiple('form.grade_domain');
14298: ($name)=&get_env_multiple('form.grade_username');
14299: return ($symb,$courseid,$domain,$name,$publicuser);
14300: }
14301: }
14302: if (!$passedsymb) {
14303: $symb=&symbread();
14304: } else {
14305: $symb=$passedsymb;
14306: }
14307: $courseid=$env{'request.course.id'};
14308: $domain=$env{'user.domain'};
14309: $name=$env{'user.name'};
14310: if ($name eq 'public' && $domain eq 'public') {
14311: if (!defined($env{'form.username'})) {
14312: $env{'form.username'}.=time.rand(10000000);
14313: }
14314: $name.=$env{'form.username'};
14315: }
14316: return ($symb,$courseid,$domain,$name,$publicuser);
14317:
14318: }
14319:
1.36 albertel 14320: # ------------------------------------------------------------ Serves up a file
1.472 albertel 14321: # returns either the contents of the file or
14322: # -1 if the file doesn't exist
1.481 raeburn 14323: #
14324: # if the target is a file that was uploaded via DOCS,
14325: # a check will be made to see if a current copy exists on the local server,
14326: # if it does this will be served, otherwise a copy will be retrieved from
14327: # the home server for the course and stored in /home/httpd/html/userfiles on
14328: # the local server.
1.472 albertel 14329:
1.36 albertel 14330: sub getfile {
1.538 albertel 14331: my ($file) = @_;
1.609 banghart 14332: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 14333: &repcopy($file);
14334: return &readfile($file);
14335: }
14336:
14337: sub repcopy_userfile {
14338: my ($file)=@_;
1.1142 raeburn 14339: my $londocroot = $perlvar{'lonDocRoot'};
14340: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 14341: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 14342: my ($cdom,$cnum,$filename) =
1.811 albertel 14343: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 14344: my $uri="/uploaded/$cdom/$cnum/$filename";
14345: if (-e "$file") {
1.828 www 14346: # we already have a local copy, check it out
1.538 albertel 14347: my @fileinfo = stat($file);
1.828 www 14348: my $rtncode;
14349: my $info;
1.538 albertel 14350: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 14351: if ($lwpresp ne 'ok') {
1.828 www 14352: # there is no such file anymore, even though we had a local copy
1.482 albertel 14353: if ($rtncode eq '404') {
1.538 albertel 14354: unlink($file);
1.482 albertel 14355: }
14356: return -1;
14357: }
14358: if ($info < $fileinfo[9]) {
1.828 www 14359: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 14360: return 'ok';
1.828 www 14361: } else {
14362: # the file is outdated, get rid of it
14363: unlink($file);
1.482 albertel 14364: }
1.828 www 14365: }
14366: # one way or the other, at this point, we don't have the file
14367: # construct the correct path for the file
14368: my @parts = ($cdom,$cnum);
14369: if ($filename =~ m|^(.+)/[^/]+$|) {
14370: push @parts, split(/\//,$1);
14371: }
14372: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14373: foreach my $part (@parts) {
14374: $path .= '/'.$part;
14375: if (!-e $path) {
14376: mkdir($path,0770);
1.482 albertel 14377: }
14378: }
1.828 www 14379: # now the path exists for sure
14380: # get a user agent
14381: my $ua=new LWP::UserAgent;
14382: my $transferfile=$file.'.in.transfer';
14383: # FIXME: this should flock
14384: if (-e $transferfile) { return 'ok'; }
14385: my $request;
14386: $uri=~s/^\///;
1.980 raeburn 14387: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14388: my $hostname = &hostname($homeserver);
1.980 raeburn 14389: my $protocol = $protocol{$homeserver};
14390: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14391: $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
1.828 www 14392: my $response=$ua->request($request,$transferfile);
14393: # did it work?
14394: if ($response->is_error()) {
14395: unlink($transferfile);
14396: &logthis("Userfile repcopy failed for $uri");
14397: return -1;
14398: }
14399: # worked, rename the transfer file
14400: rename($transferfile,$file);
1.607 raeburn 14401: return 'ok';
1.481 raeburn 14402: }
14403:
1.517 albertel 14404: sub tokenwrapper {
14405: my $uri=shift;
1.980 raeburn 14406: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 14407: $uri=~s|^/||;
1.620 albertel 14408: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 14409: my $token=$1;
1.552 albertel 14410: my (undef,$udom,$uname,$file)=split('/',$uri,4);
14411: if ($udom && $uname && $file) {
14412: $file=~s|(\?\.*)*$||;
1.949 raeburn 14413: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 14414: my $homeserver = &homeserver($uname,$udom);
1.1172.2.120 raeburn 14415: my $hostname = &hostname($homeserver);
1.980 raeburn 14416: my $protocol = $protocol{$homeserver};
14417: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14418: return $protocol.'://'.$hostname.'/'.$uri.
1.517 albertel 14419: (($uri=~/\?/)?'&':'?').'token='.$token.
14420: '&tokenissued='.$perlvar{'lonHostID'};
14421: } else {
14422: return '/adm/notfound.html';
14423: }
14424: }
14425:
1.828 www 14426: # call with reqtype HEAD: get last modification time
14427: # call with reqtype GET: get the file contents
14428: # Do not call this with reqtype GET for large files! It loads everything into memory
14429: #
1.481 raeburn 14430: sub getuploaded {
14431: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14432: $uri=~s/^\///;
1.980 raeburn 14433: my $homeserver = &homeserver($cnum,$cdom);
1.1172.2.120 raeburn 14434: my $hostname = &hostname($homeserver);
1.980 raeburn 14435: my $protocol = $protocol{$homeserver};
14436: $protocol = 'http' if ($protocol ne 'https');
1.1172.2.120 raeburn 14437: $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
1.481 raeburn 14438: my $ua=new LWP::UserAgent;
14439: my $request=new HTTP::Request($reqtype,$uri);
14440: my $response=$ua->request($request);
14441: $$rtncode = $response->code;
1.482 albertel 14442: if (! $response->is_success()) {
14443: return 'failed';
14444: }
14445: if ($reqtype eq 'HEAD') {
1.486 www 14446: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 14447: } elsif ($reqtype eq 'GET') {
14448: $$info = $response->content;
1.472 albertel 14449: }
1.482 albertel 14450: return 'ok';
1.36 albertel 14451: }
14452:
1.481 raeburn 14453: sub readfile {
14454: my $file = shift;
14455: if ( (! -e $file ) || ($file eq '') ) { return -1; };
14456: my $fh;
1.1172.2.96 raeburn 14457: open($fh,"<",$file);
1.481 raeburn 14458: my $a='';
1.800 albertel 14459: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 14460: return $a;
14461: }
14462:
1.36 albertel 14463: sub filelocation {
1.590 banghart 14464: my ($dir,$file) = @_;
14465: my $location;
14466: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 14467:
14468: if ($file =~ m-^/adm/-) {
14469: $file=~s-^/adm/wrapper/-/-;
14470: $file=~s-^/adm/coursedocs/showdoc/-/-;
14471: }
1.882 albertel 14472:
1.1139 www 14473: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 14474: $location = $file;
1.609 banghart 14475: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 14476: my ($udom,$uname,$filename)=
1.811 albertel 14477: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 14478: my $home=&homeserver($uname,$udom);
14479: my $is_me=0;
14480: my @ids=¤t_machine_ids();
14481: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14482: if ($is_me) {
1.1117 foxr 14483: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 14484: } else {
14485: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14486: $udom.'/'.$uname.'/'.$filename;
14487: }
1.882 albertel 14488: } elsif ($file =~ m-^/adm/-) {
14489: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 14490: } else {
14491: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 14492: $file=~s:^/(res|priv)/:/:;
14493: my $space=$1;
1.590 banghart 14494: if ( !( $file =~ m:^/:) ) {
14495: $location = $dir. '/'.$file;
14496: } else {
1.1142 raeburn 14497: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 14498: }
1.59 albertel 14499: }
1.590 banghart 14500: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 14501: while ($location=~m{/\.\./}) {
14502: if ($location =~ m{/[^/]+/\.\./}) {
14503: $location=~ s{/[^/]+/\.\./}{/}g;
14504: } else {
14505: $location=~ s{/\.\./}{/}g;
14506: }
14507: } #remove dir/..
1.590 banghart 14508: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14509: return $location;
1.46 www 14510: }
1.36 albertel 14511:
1.46 www 14512: sub hreflocation {
14513: my ($dir,$file)=@_;
1.980 raeburn 14514: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 14515: $file=filelocation($dir,$file);
1.700 albertel 14516: } elsif ($file=~m-^/adm/-) {
14517: $file=~s-^/adm/wrapper/-/-;
14518: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 14519: }
14520: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14521: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14522: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 14523: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14524: {/uploaded/$1/$2/}x;
1.46 www 14525: }
1.913 albertel 14526: if ($file=~ m{^/userfiles/}) {
14527: $file =~ s{^/userfiles/}{/uploaded/};
14528: }
1.462 albertel 14529: return $file;
1.465 albertel 14530: }
14531:
1.1139 www 14532:
14533:
14534:
14535:
1.465 albertel 14536: sub current_machine_domains {
1.853 albertel 14537: return &machine_domains(&hostname($perlvar{'lonHostID'}));
14538: }
14539:
14540: sub machine_domains {
14541: my ($hostname) = @_;
1.465 albertel 14542: my @domains;
1.838 albertel 14543: my %hostname = &all_hostnames();
1.465 albertel 14544: while( my($id, $name) = each(%hostname)) {
1.467 matthew 14545: # &logthis("-$id-$name-$hostname-");
1.465 albertel 14546: if ($hostname eq $name) {
1.844 albertel 14547: push(@domains,&host_domain($id));
1.465 albertel 14548: }
14549: }
14550: return @domains;
14551: }
14552:
14553: sub current_machine_ids {
1.853 albertel 14554: return &machine_ids(&hostname($perlvar{'lonHostID'}));
14555: }
14556:
14557: sub machine_ids {
14558: my ($hostname) = @_;
14559: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 14560: my @ids;
1.888 albertel 14561: my %name_to_host = &all_names();
1.889 albertel 14562: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14563: return @{ $name_to_host{$hostname} };
14564: }
14565: return;
1.31 www 14566: }
14567:
1.824 raeburn 14568: sub additional_machine_domains {
14569: my @domains;
1.1172.2.142 raeburn 14570: if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14571: if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14572: while( my $line = <$fh>) {
14573: chomp($line);
14574: $line =~ s/\s//g;
14575: push(@domains,$line);
14576: }
14577: close($fh);
14578: }
1.824 raeburn 14579: }
14580: return @domains;
14581: }
14582:
14583: sub default_login_domain {
14584: my $domain = $perlvar{'lonDefDomain'};
14585: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14586: foreach my $posdom (¤t_machine_domains(),
14587: &additional_machine_domains()) {
14588: if (lc($posdom) eq lc($testdomain)) {
14589: $domain=$posdom;
14590: last;
14591: }
14592: }
14593: return $domain;
14594: }
14595:
1.1172.2.106 raeburn 14596: sub shared_institution {
1.1172.2.136 raeburn 14597: my ($dom,$lonhost) = @_;
14598: if ($lonhost eq '') {
14599: $lonhost = $perlvar{'lonHostID'};
14600: }
1.1172.2.106 raeburn 14601: my $same_intdom;
1.1172.2.136 raeburn 14602: my $hostintdom = &internet_dom($lonhost);
1.1172.2.106 raeburn 14603: if ($hostintdom ne '') {
14604: my %iphost = &get_iphost();
14605: my $primary_id = &domain($dom,'primary');
14606: my $primary_ip = &get_host_ip($primary_id);
14607: if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14608: foreach my $id (@{$iphost{$primary_ip}}) {
14609: my $intdom = &internet_dom($id);
14610: if ($intdom eq $hostintdom) {
14611: $same_intdom = 1;
14612: last;
14613: }
14614: }
14615: }
14616: }
14617: return $same_intdom;
14618: }
14619:
1.1172.2.120 raeburn 14620: sub uses_sts {
14621: my ($ignore_cache) = @_;
14622: my $lonhost = $perlvar{'lonHostID'};
14623: my $hostname = &hostname($lonhost);
14624: my $sts_on;
14625: if ($protocol{$lonhost} eq 'https') {
14626: my $cachetime = 12*3600;
14627: if (!$ignore_cache) {
14628: ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14629: if (defined($cached)) {
14630: return $sts_on;
14631: }
14632: }
1.1172.2.121 raeburn 14633: my $ua=new LWP::UserAgent;
1.1172.2.120 raeburn 14634: my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14635: my $request=new HTTP::Request('HEAD',$url);
1.1172.2.121 raeburn 14636: my $response=$ua->request($request);
1.1172.2.120 raeburn 14637: if ($response->is_success) {
14638: my $has_sts = $response->header('Strict-Transport-Security');
14639: if ($has_sts eq '') {
14640: $sts_on = 0;
14641: } else {
14642: if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14643: my $maxage = $1;
14644: if ($maxage) {
14645: $sts_on = 1;
14646: } else {
14647: $sts_on = 0;
14648: }
14649: } else {
14650: $sts_on = 0;
14651: }
14652: }
14653: return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14654: }
14655: }
14656: return;
14657: }
14658:
1.1172.2.142 raeburn 14659: sub waf_allssl {
14660: my ($host_name) = @_;
14661: my $alias = &get_proxy_alias();
14662: if ($host_name eq '') {
14663: $host_name = $ENV{'SERVER_NAME'};
14664: }
14665: if (($host_name ne '') && ($alias eq $host_name)) {
14666: my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14667: my %defdomdefaults = &get_domain_defaults($serverhomedom);
14668: if ($defdomdefaults{'waf_sslopt'}) {
14669: return $defdomdefaults{'waf_sslopt'};
14670: }
14671: }
14672: return;
14673: }
14674:
1.1172.2.134 raeburn 14675: sub get_requestor_ip {
14676: my ($r,$nolookup,$noproxy) = @_;
14677: my $from_ip;
14678: if (ref($r)) {
1.1172.2.142 raeburn 14679: if ($r->can('useragent_ip')) {
14680: if ($noproxy && $r->can('client_ip')) {
14681: $from_ip = $r->client_ip();
14682: } else {
14683: $from_ip = $r->useragent_ip();
14684: }
14685: } elsif ($r->connection->can('remote_ip')) {
14686: $from_ip = $r->connection->remote_ip();
14687: } else {
14688: $from_ip = $r->get_remote_host($nolookup);
14689: }
1.1172.2.134 raeburn 14690: } else {
14691: $from_ip = $ENV{'REMOTE_ADDR'};
14692: }
1.1172.2.142 raeburn 14693: return $from_ip if ($noproxy);
14694: # Who controls proxy settings for server
14695: my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14696: my $proxyinfo = &get_proxy_settings($dom_in_use);
14697: if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14698: if ($proxyinfo->{'vpnint'}) {
14699: if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14700: return $from_ip;
14701: }
14702: }
14703: if ($proxyinfo->{'trusted'}) {
14704: if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14705: my $ipheader = $proxyinfo->{'ipheader'};
14706: my ($ip,$xfor);
14707: if (ref($r)) {
14708: if ($ipheader) {
14709: $ip = $r->headers_in->{$ipheader};
14710: }
14711: $xfor = $r->headers_in->{'X-Forwarded-For'};
14712: } else {
14713: if ($ipheader) {
14714: $ip = $ENV{'HTTP_'.uc($ipheader)};
14715: }
14716: $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14717: }
14718: if (($ip eq '') && ($xfor ne '')) {
14719: foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14720: unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14721: $ip = $poss_ip;
14722: last;
14723: }
14724: }
14725: }
14726: if ($ip ne '') {
14727: return $ip;
14728: }
14729: }
14730: }
14731: }
1.1172.2.134 raeburn 14732: return $from_ip;
14733: }
14734:
1.1172.2.142 raeburn 14735: sub get_proxy_settings {
14736: my ($dom_in_use) = @_;
14737: my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14738: my $proxyinfo = {
14739: ipheader => $domdefaults{'waf_ipheader'},
14740: trusted => $domdefaults{'waf_trusted'},
14741: vpnint => $domdefaults{'waf_vpnint'},
14742: vpnext => $domdefaults{'waf_vpnext'},
14743: sslopt => $domdefaults{'waf_sslopt'},
14744: };
14745: return $proxyinfo;
14746: }
14747:
14748: sub ip_match {
14749: my ($ip,$pattern_str) = @_;
14750: $ip=Net::CIDR::cidrvalidate($ip);
14751: if ($ip) {
14752: return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14753: }
14754: return;
14755: }
14756:
14757: sub get_proxy_alias {
14758: my ($lonid) = @_;
14759: if ($lonid eq '') {
14760: $lonid = $perlvar{'lonHostID'};
14761: }
14762: if (!defined(&hostname($lonid))) {
14763: return;
14764: }
14765: if ($lonid ne '') {
14766: my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14767: if ($cached) {
14768: return $alias;
14769: }
14770: my $dom = &Apache::lonnet::host_domain($lonid);
14771: if ($dom ne '') {
14772: my $cachetime = 60*60*24;
14773: my %domconfig =
14774: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14775: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14776: if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14777: $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14778: }
14779: }
14780: return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14781: }
14782: }
14783: return;
14784: }
14785:
14786: sub use_proxy_alias {
14787: my ($r,$lonid) = @_;
14788: my $alias = &get_proxy_alias($lonid);
14789: if ($alias) {
14790: my $dom = &host_domain($lonid);
14791: if ($dom ne '') {
14792: my $proxyinfo = &get_proxy_settings($dom);
14793: my ($vpnint,$remote_ip);
14794: if (ref($proxyinfo) eq 'HASH') {
14795: $vpnint = $proxyinfo->{'vpnint'};
14796: if ($vpnint) {
14797: $remote_ip = &get_requestor_ip($r,1,1);
14798: }
14799: }
14800: unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14801: return $alias;
14802: }
14803: }
14804: }
14805: return;
14806: }
14807:
14808: sub alias_sso {
14809: my ($lonid) = @_;
14810: if ($lonid eq '') {
14811: $lonid = $perlvar{'lonHostID'};
14812: }
14813: if (!defined(&hostname($lonid))) {
14814: return;
14815: }
14816: if ($lonid ne '') {
14817: my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14818: if ($cached) {
14819: return $use_alias;
14820: }
14821: my $dom = &Apache::lonnet::host_domain($lonid);
14822: if ($dom ne '') {
14823: my $cachetime = 60*60*24;
14824: my %domconfig =
14825: &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14826: if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14827: if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14828: $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14829: }
14830: }
14831: return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14832: }
14833: }
14834: return;
14835: }
14836:
14837: sub get_saml_landing {
14838: my ($lonid) = @_;
14839: if ($lonid eq '') {
14840: my $defdom = &default_login_domain();
14841: my @hosts = ¤t_machine_ids();
14842: if (@hosts > 1) {
14843: foreach my $hostid (@hosts) {
14844: if (&host_domain($hostid) eq $defdom) {
14845: $lonid = $hostid;
14846: last;
14847: }
14848: }
14849: } else {
14850: $lonid = $perlvar{'lonHostID'};
14851: }
14852: if ($lonid) {
14853: unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14854: return;
14855: }
14856: } else {
14857: return;
14858: }
14859: } elsif (!defined(&hostname($lonid))) {
14860: return;
14861: }
14862: my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14863: if ($cached) {
14864: return $landing;
14865: }
14866: my $dom = &Apache::lonnet::host_domain($lonid);
14867: if ($dom ne '') {
14868: my $cachetime = 60*60*24;
14869: my %domconfig =
14870: &Apache::lonnet::get_dom('configuration',['login'],$dom);
14871: if (ref($domconfig{'login'}) eq 'HASH') {
14872: if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14873: if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14874: $landing = 1;
14875: }
14876: }
14877: }
14878: return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14879: }
14880: return;
14881: }
14882:
1.31 www 14883: # ------------------------------------------------------------- Declutters URLs
14884:
14885: sub declutter {
14886: my $thisfn=shift;
1.569 albertel 14887: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1172.2.49 raeburn 14888: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14889: $thisfn=~s{^/home/httpd/html}{};
14890: }
1.31 www 14891: $thisfn=~s/^\///;
1.697 albertel 14892: $thisfn=~s|^adm/wrapper/||;
14893: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 14894: $thisfn=~s/^res\///;
1.1172 bisitz 14895: $thisfn=~s/^priv\///;
1.1032 raeburn 14896: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14897: $thisfn=~s/\?.+$//;
14898: }
1.268 www 14899: return $thisfn;
14900: }
14901:
14902: # ------------------------------------------------------------- Clutter up URLs
14903:
14904: sub clutter {
14905: my $thisfn='/'.&declutter(shift);
1.887 albertel 14906: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 14907: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 14908: $thisfn='/res'.$thisfn;
14909: }
1.1031 raeburn 14910: if ($thisfn !~m|^/adm|) {
14911: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 14912: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 14913: } else {
14914: my ($ext) = ($thisfn =~ /\.(\w+)$/);
14915: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 14916: if ($embstyle eq 'ssi'
14917: || ($embstyle eq 'hdn')
14918: || ($embstyle eq 'rat')
14919: || ($embstyle eq 'prv')
14920: || ($embstyle eq 'ign')) {
14921: #do nothing with these
14922: } elsif (($embstyle eq 'img')
1.695 albertel 14923: || ($embstyle eq 'emb')
14924: || ($embstyle eq 'wrp')) {
14925: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 14926: } elsif ($embstyle eq 'unk'
14927: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 14928: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 14929: } else {
1.718 www 14930: # &logthis("Got a blank emb style");
1.695 albertel 14931: }
1.694 albertel 14932: }
1.1172.2.146. .1(raebu 14933:22): } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14934:22): $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 14935: }
1.31 www 14936: return $thisfn;
1.12 www 14937: }
14938:
1.787 albertel 14939: sub clutter_with_no_wrapper {
14940: my $uri = &clutter(shift);
14941: if ($uri =~ m-^/adm/-) {
14942: $uri =~ s-^/adm/wrapper/-/-;
14943: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
14944: }
14945: return $uri;
14946: }
14947:
1.557 albertel 14948: sub freeze_escape {
14949: my ($value)=@_;
14950: if (ref($value)) {
14951: $value=&nfreeze($value);
14952: return '__FROZEN__'.&escape($value);
14953: }
14954: return &escape($value);
14955: }
14956:
1.11 www 14957:
1.557 albertel 14958: sub thaw_unescape {
14959: my ($value)=@_;
14960: if ($value =~ /^__FROZEN__/) {
14961: substr($value,0,10,undef);
14962: $value=&unescape($value);
14963: return &thaw($value);
14964: }
14965: return &unescape($value);
14966: }
14967:
1.436 albertel 14968: sub correct_line_ends {
14969: my ($result)=@_;
14970: $$result =~s/\r\n/\n/mg;
14971: $$result =~s/\r/\n/mg;
1.415 albertel 14972: }
1.1 albertel 14973: # ================================================================ Main Program
14974:
1.184 www 14975: sub goodbye {
1.204 albertel 14976: &logthis("Starting Shut down");
1.443 albertel 14977: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 14978: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 14979: #converted
1.599 albertel 14980: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 14981: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14982: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14983: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 14984: #1.1 only
1.870 albertel 14985: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14986: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14987: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14988: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14989: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 14990: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14991: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 14992: &flushcourselogs();
14993: &logthis("Shutting down");
14994: }
14995:
1.852 albertel 14996: sub get_dns {
1.1172.2.17 raeburn 14997: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 14998: if (!$ignore_cache) {
14999: my ($content,$cached)=
15000: &Apache::lonnet::is_cached_new('dns',$url);
15001: if ($cached) {
1.1172.2.17 raeburn 15002: &$func($content,$hashref);
1.869 albertel 15003: return;
15004: }
15005: }
15006:
15007: my %alldns;
1.1172.2.96 raeburn 15008: if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15009: foreach my $dns (<$config>) {
15010: next if ($dns !~ /^\^(\S*)/x);
15011: my $line = $1;
15012: my ($host,$protocol) = split(/:/,$line);
15013: if ($protocol ne 'https') {
15014: $protocol = 'http';
15015: }
15016: $alldns{$host} = $protocol;
1.979 raeburn 15017: }
1.1172.2.96 raeburn 15018: close($config);
1.869 albertel 15019: }
15020: while (%alldns) {
1.1172.2.52 raeburn 15021: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.1172.2.142 raeburn 15022: my @content;
15023: if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15024: my $command = (split('/',$url))[3];
15025: my ($dir,$file) = &parse_getdns_url($command,$url);
15026: delete($alldns{$dns});
15027: next if (($dir eq '') || ($file eq ''));
15028: if (open(my $config,'<',"$dir/$file")) {
15029: @content = <$config>;
15030: close($config);
15031: }
15032: } else {
15033: my $ua=new LWP::UserAgent;
15034: $ua->timeout(30);
15035: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15036: my $response=$ua->request($request);
15037: delete($alldns{$dns});
15038: next if ($response->is_error());
15039: @content = split("\n",$response->content);
15040: }
1.1172.2.17 raeburn 15041: unless ($nocache) {
1.1172.2.23 raeburn 15042: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1172.2.17 raeburn 15043: }
15044: &$func(\@content,$hashref);
1.869 albertel 15045: return;
1.852 albertel 15046: }
1.871 albertel 15047: my $which = (split('/',$url))[3];
15048: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
1.1172.2.105 raeburn 15049: if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15050: my @content = <$config>;
15051: &$func(\@content,$hashref);
15052: }
1.1172.2.17 raeburn 15053: return;
15054: }
15055:
15056: # ------------------------------------------------------Get DNS checksums file
15057: sub parse_dns_checksums_tab {
15058: my ($lines,$hashref) = @_;
1.1172.2.53 raeburn 15059: my $lonhost = $perlvar{'lonHostID'};
15060: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1172.2.17 raeburn 15061: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1172.2.53 raeburn 15062: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15063: my $webconfdir = '/etc/httpd/conf';
15064: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15065: $webconfdir = '/etc/apache2';
15066: } elsif ($distro =~ /^sles(\d+)$/) {
15067: if ($1 >= 10) {
15068: $webconfdir = '/etc/apache2';
15069: }
15070: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15071: if ($1 >= 10.0) {
15072: $webconfdir = '/etc/apache2';
15073: }
15074: }
1.1172.2.17 raeburn 15075: my ($release,$timestamp) = split(/\-/,$loncaparev);
15076: my (%chksum,%revnum);
15077: if (ref($lines) eq 'ARRAY') {
15078: chomp(@{$lines});
1.1172.2.34 raeburn 15079: my $version = shift(@{$lines});
15080: if ($version eq $release) {
1.1172.2.17 raeburn 15081: foreach my $line (@{$lines}) {
1.1172.2.34 raeburn 15082: my ($file,$version,$shasum) = split(/,/,$line);
1.1172.2.53 raeburn 15083: if ($file =~ m{^/etc/httpd/conf}) {
15084: if ($webconfdir eq '/etc/apache2') {
15085: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15086: }
15087: }
1.1172.2.34 raeburn 15088: $chksum{$file} = $shasum;
15089: $revnum{$file} = $version;
1.1172.2.17 raeburn 15090: }
15091: if (ref($hashref) eq 'HASH') {
15092: %{$hashref} = (
15093: sums => \%chksum,
15094: versions => \%revnum,
15095: );
15096: }
15097: }
15098: }
1.869 albertel 15099: return;
1.852 albertel 15100: }
1.1172.2.17 raeburn 15101:
15102: sub fetch_dns_checksums {
15103: my %checksums;
1.1172.2.34 raeburn 15104: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1172.2.48 raeburn 15105: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1172.2.34 raeburn 15106: my ($release,$timestamp) = split(/\-/,$loncaparev);
15107: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1172.2.17 raeburn 15108: \%checksums);
15109: return \%checksums;
15110: }
15111:
1.1172.2.142 raeburn 15112: sub parse_getdns_url {
15113: my ($command,$url) = @_;
15114: my $dir = $perlvar{'lonTabDir'};
15115: my $file;
15116: if ($command eq 'hosts') {
15117: $file = 'dns_hosts.tab';
15118: } elsif ($command eq 'domain') {
15119: $file = 'dns_domain.tab';
15120: } elsif ($command eq 'checksums') {
15121: my $version = (split('/',$url))[4];
15122: $file = "dns_checksums/$version.tab",
15123: }
15124: return ($dir,$file);
15125: }
15126:
1.327 albertel 15127: # ------------------------------------------------------------ Read domain file
15128: {
1.852 albertel 15129: my $loaded;
1.846 albertel 15130: my %domain;
15131:
1.852 albertel 15132: sub parse_domain_tab {
15133: my ($lines) = @_;
15134: foreach my $line (@$lines) {
15135: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 15136:
1.846 albertel 15137: chomp($line);
1.852 albertel 15138: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 15139: my %this_domain;
15140: foreach my $field ('description', 'auth_def', 'auth_arg_def',
15141: 'lang_def', 'city', 'longi', 'lati',
15142: 'primary') {
15143: $this_domain{$field} = shift(@elements);
15144: }
15145: $domain{$name} = \%this_domain;
1.852 albertel 15146: }
15147: }
1.864 albertel 15148:
15149: sub reset_domain_info {
15150: undef($loaded);
15151: undef(%domain);
15152: }
15153:
1.852 albertel 15154: sub load_domain_tab {
1.1172.2.70 raeburn 15155: my ($ignore_cache,$nocache) = @_;
15156: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 15157: my $fh;
1.1172.2.96 raeburn 15158: if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
1.852 albertel 15159: my @lines = <$fh>;
15160: &parse_domain_tab(\@lines);
1.448 albertel 15161: }
1.852 albertel 15162: close($fh);
15163: $loaded = 1;
1.327 albertel 15164: }
1.846 albertel 15165:
15166: sub domain {
1.852 albertel 15167: &load_domain_tab() if (!$loaded);
15168:
1.846 albertel 15169: my ($name,$what) = @_;
15170: return if ( !exists($domain{$name}) );
15171:
15172: if (!$what) {
15173: return $domain{$name}{'description'};
15174: }
15175: return $domain{$name}{$what};
15176: }
1.974 raeburn 15177:
15178: sub domain_info {
15179: &load_domain_tab() if (!$loaded);
15180: return %domain;
15181: }
15182:
1.327 albertel 15183: }
15184:
15185:
1.1 albertel 15186: # ------------------------------------------------------------- Read hosts file
15187: {
1.838 albertel 15188: my %hostname;
1.844 albertel 15189: my %hostdom;
1.845 albertel 15190: my %libserv;
1.852 albertel 15191: my $loaded;
1.888 albertel 15192: my %name_to_host;
1.1074 raeburn 15193: my %internetdom;
1.1107 raeburn 15194: my %LC_dns_serv;
1.852 albertel 15195:
15196: sub parse_hosts_tab {
15197: my ($file) = @_;
15198: foreach my $configline (@$file) {
15199: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 15200: chomp($configline);
15201: if ($configline =~ /^\^/) {
15202: if ($configline =~ /^\^([\w.\-]+)/) {
15203: $LC_dns_serv{$1} = 1;
15204: }
15205: next;
15206: }
1.1074 raeburn 15207: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 15208: $name=~s/\s//g;
15209: if ($id && $domain && $role && $name) {
1.1172.2.96 raeburn 15210: if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15211: my $curr = $hostname{$id};
15212: my $skip;
15213: if (ref($name_to_host{$curr}) eq 'ARRAY') {
15214: if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15215: $skip = 1;
15216: } else {
15217: @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15218: }
15219: }
15220: unless ($skip) {
15221: push(@{$name_to_host{$name}},$id);
15222: }
15223: } else {
15224: push(@{$name_to_host{$name}},$id);
15225: }
1.852 albertel 15226: $hostname{$id}=$name;
15227: $hostdom{$id}=$domain;
15228: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 15229: if (defined($protocol)) {
15230: if ($protocol eq 'https') {
15231: $protocol{$id} = $protocol;
15232: } else {
15233: $protocol{$id} = 'http';
15234: }
1.968 raeburn 15235: } else {
1.969 raeburn 15236: $protocol{$id} = 'http';
1.968 raeburn 15237: }
1.1074 raeburn 15238: if (defined($intdom)) {
15239: $internetdom{$id} = $intdom;
15240: }
1.852 albertel 15241: }
15242: }
15243: }
1.864 albertel 15244:
15245: sub reset_hosts_info {
1.897 albertel 15246: &purge_remembered();
1.864 albertel 15247: &reset_domain_info();
15248: &reset_hosts_ip_info();
1.892 albertel 15249: undef(%name_to_host);
1.864 albertel 15250: undef(%hostname);
15251: undef(%hostdom);
15252: undef(%libserv);
15253: undef($loaded);
15254: }
1.1 albertel 15255:
1.852 albertel 15256: sub load_hosts_tab {
1.1172.2.70 raeburn 15257: my ($ignore_cache,$nocache) = @_;
15258: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.1172.2.96 raeburn 15259: open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
1.852 albertel 15260: my @config = <$config>;
15261: &parse_hosts_tab(\@config);
15262: close($config);
15263: $loaded=1;
1.1 albertel 15264: }
1.852 albertel 15265:
1.838 albertel 15266: sub hostname {
1.852 albertel 15267: &load_hosts_tab() if (!$loaded);
15268:
1.838 albertel 15269: my ($lonid) = @_;
15270: return $hostname{$lonid};
15271: }
1.845 albertel 15272:
1.838 albertel 15273: sub all_hostnames {
1.852 albertel 15274: &load_hosts_tab() if (!$loaded);
15275:
1.838 albertel 15276: return %hostname;
15277: }
1.845 albertel 15278:
1.888 albertel 15279: sub all_names {
1.1172.2.70 raeburn 15280: my ($ignore_cache,$nocache) = @_;
15281: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 15282:
15283: return %name_to_host;
15284: }
15285:
1.974 raeburn 15286: sub all_host_domain {
15287: &load_hosts_tab() if (!$loaded);
15288: return %hostdom;
15289: }
15290:
1.845 albertel 15291: sub is_library {
1.852 albertel 15292: &load_hosts_tab() if (!$loaded);
15293:
1.845 albertel 15294: return exists($libserv{$_[0]});
15295: }
15296:
15297: sub all_library {
1.852 albertel 15298: &load_hosts_tab() if (!$loaded);
15299:
1.845 albertel 15300: return %libserv;
15301: }
15302:
1.1062 droeschl 15303: sub unique_library {
15304: #2x reverse removes all hostnames that appear more than once
15305: my %unique = reverse &all_library();
15306: return reverse %unique;
15307: }
15308:
1.841 albertel 15309: sub get_servers {
1.852 albertel 15310: &load_hosts_tab() if (!$loaded);
15311:
1.841 albertel 15312: my ($domain,$type) = @_;
15313: my %possible_hosts = ($type eq 'library') ? %libserv
15314: : %hostname;
15315: my %result;
1.842 albertel 15316: if (ref($domain) eq 'ARRAY') {
15317: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 15318: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 15319: $result{$host} = $hostname;
15320: }
15321: }
15322: } else {
15323: while ( my ($host,$hostname) = each(%possible_hosts)) {
15324: if ($hostdom{$host} eq $domain) {
15325: $result{$host} = $hostname;
15326: }
1.841 albertel 15327: }
15328: }
15329: return %result;
15330: }
1.845 albertel 15331:
1.1062 droeschl 15332: sub get_unique_servers {
15333: my %unique = reverse &get_servers(@_);
15334: return reverse %unique;
15335: }
15336:
1.844 albertel 15337: sub host_domain {
1.852 albertel 15338: &load_hosts_tab() if (!$loaded);
15339:
1.844 albertel 15340: my ($lonid) = @_;
15341: return $hostdom{$lonid};
15342: }
15343:
1.841 albertel 15344: sub all_domains {
1.852 albertel 15345: &load_hosts_tab() if (!$loaded);
15346:
1.841 albertel 15347: my %seen;
15348: my @uniq = grep(!$seen{$_}++, values(%hostdom));
15349: return @uniq;
15350: }
1.1074 raeburn 15351:
15352: sub internet_dom {
15353: &load_hosts_tab() if (!$loaded);
15354:
15355: my ($lonid) = @_;
15356: return $internetdom{$lonid};
15357: }
1.1107 raeburn 15358:
15359: sub is_LC_dns {
15360: &load_hosts_tab() if (!$loaded);
15361:
15362: my ($hostname) = @_;
15363: return exists($LC_dns_serv{$hostname});
15364: }
15365:
1.1 albertel 15366: }
15367:
1.847 albertel 15368: {
15369: my %iphost;
1.856 albertel 15370: my %name_to_ip;
15371: my %lonid_to_ip;
1.869 albertel 15372:
1.847 albertel 15373: sub get_hosts_from_ip {
15374: my ($ip) = @_;
15375: my %iphosts = &get_iphost();
15376: if (ref($iphosts{$ip})) {
15377: return @{$iphosts{$ip}};
15378: }
15379: return;
1.839 albertel 15380: }
1.864 albertel 15381:
15382: sub reset_hosts_ip_info {
15383: undef(%iphost);
15384: undef(%name_to_ip);
15385: undef(%lonid_to_ip);
15386: }
1.856 albertel 15387:
15388: sub get_host_ip {
15389: my ($lonid) = @_;
15390: if (exists($lonid_to_ip{$lonid})) {
15391: return $lonid_to_ip{$lonid};
15392: }
15393: my $name=&hostname($lonid);
15394: my $ip = gethostbyname($name);
15395: return if (!$ip || length($ip) ne 4);
15396: $ip=inet_ntoa($ip);
15397: $name_to_ip{$name} = $ip;
15398: $lonid_to_ip{$lonid} = $ip;
15399: return $ip;
15400: }
1.847 albertel 15401:
15402: sub get_iphost {
1.1172.2.70 raeburn 15403: my ($ignore_cache,$nocache) = @_;
1.894 albertel 15404:
1.869 albertel 15405: if (!$ignore_cache) {
15406: if (%iphost) {
15407: return %iphost;
15408: }
15409: my ($ip_info,$cached)=
15410: &Apache::lonnet::is_cached_new('iphost','iphost');
15411: if ($cached) {
15412: %iphost = %{$ip_info->[0]};
15413: %name_to_ip = %{$ip_info->[1]};
15414: %lonid_to_ip = %{$ip_info->[2]};
15415: return %iphost;
15416: }
15417: }
1.894 albertel 15418:
15419: # get yesterday's info for fallback
15420: my %old_name_to_ip;
15421: my ($ip_info,$cached)=
15422: &Apache::lonnet::is_cached_new('iphost','iphost');
15423: if ($cached) {
15424: %old_name_to_ip = %{$ip_info->[1]};
15425: }
15426:
1.1172.2.70 raeburn 15427: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 15428: foreach my $name (keys(%name_to_host)) {
1.847 albertel 15429: my $ip;
15430: if (!exists($name_to_ip{$name})) {
15431: $ip = gethostbyname($name);
15432: if (!$ip || length($ip) ne 4) {
1.894 albertel 15433: if (defined($old_name_to_ip{$name})) {
15434: $ip = $old_name_to_ip{$name};
15435: &logthis("Can't find $name defaulting to old $ip");
15436: } else {
15437: &logthis("Name $name no IP found");
15438: next;
15439: }
15440: } else {
15441: $ip=inet_ntoa($ip);
1.847 albertel 15442: }
15443: $name_to_ip{$name} = $ip;
15444: } else {
15445: $ip = $name_to_ip{$name};
1.653 albertel 15446: }
1.888 albertel 15447: foreach my $id (@{ $name_to_host{$name} }) {
15448: $lonid_to_ip{$id} = $ip;
15449: }
15450: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 15451: }
1.1172.2.70 raeburn 15452: unless ($nocache) {
15453: &do_cache_new('iphost','iphost',
15454: [\%iphost,\%name_to_ip,\%lonid_to_ip],
15455: 48*60*60);
15456: }
1.869 albertel 15457:
1.847 albertel 15458: return %iphost;
1.598 albertel 15459: }
15460:
1.992 raeburn 15461: #
15462: # Given a DNS returns the loncapa host name for that DNS
15463: #
15464: sub host_from_dns {
15465: my ($dns) = @_;
15466: my @hosts;
15467: my $ip;
15468:
1.993 raeburn 15469: if (exists($name_to_ip{$dns})) {
1.992 raeburn 15470: $ip = $name_to_ip{$dns};
15471: }
15472: if (!$ip) {
15473: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15474: if (length($ip) == 4) {
15475: $ip = &IO::Socket::inet_ntoa($ip);
15476: }
15477: }
15478: if ($ip) {
15479: @hosts = get_hosts_from_ip($ip);
15480: return $hosts[0];
15481: }
15482: return undef;
1.986 foxr 15483: }
1.992 raeburn 15484:
1.1074 raeburn 15485: sub get_internet_names {
15486: my ($lonid) = @_;
15487: return if ($lonid eq '');
15488: my ($idnref,$cached)=
15489: &Apache::lonnet::is_cached_new('internetnames',$lonid);
15490: if ($cached) {
15491: return $idnref;
15492: }
15493: my $ip = &get_host_ip($lonid);
15494: my @hosts = &get_hosts_from_ip($ip);
15495: my %iphost = &get_iphost();
15496: my (@idns,%seen);
15497: foreach my $id (@hosts) {
15498: my $dom = &host_domain($id);
15499: my $prim_id = &domain($dom,'primary');
15500: my $prim_ip = &get_host_ip($prim_id);
15501: next if ($seen{$prim_ip});
15502: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15503: foreach my $id (@{$iphost{$prim_ip}}) {
15504: my $intdom = &internet_dom($id);
15505: unless (grep(/^\Q$intdom\E$/,@idns)) {
15506: push(@idns,$intdom);
15507: }
15508: }
15509: }
15510: $seen{$prim_ip} = 1;
15511: }
1.1172.2.23 raeburn 15512: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 15513: }
15514:
1.986 foxr 15515: }
15516:
1.1079 raeburn 15517: sub all_loncaparevs {
1.1172.2.39 raeburn 15518: 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 15519: }
15520:
1.1172.2.27 raeburn 15521: # ------------------------------------------------------- Read loncaparev table
15522: {
15523: sub load_loncaparevs {
15524: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
1.1172.2.96 raeburn 15525: if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.1172.2.27 raeburn 15526: while (my $configline=<$config>) {
15527: chomp($configline);
15528: my ($hostid,$loncaparev)=split(/:/,$configline);
15529: $loncaparevs{$hostid}=$loncaparev;
15530: }
15531: close($config);
15532: }
15533: }
15534: }
15535: }
15536:
15537: # ----------------------------------------------------- Read serverhostID table
15538: {
15539: sub load_serverhomeIDs {
15540: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1.1172.2.96 raeburn 15541: if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.1172.2.27 raeburn 15542: while (my $configline=<$config>) {
15543: chomp($configline);
15544: my ($name,$id)=split(/:/,$configline);
15545: $serverhomeIDs{$name}=$id;
15546: }
15547: close($config);
15548: }
15549: }
15550: }
15551: }
15552:
15553:
1.862 albertel 15554: BEGIN {
15555:
15556: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15557: unless ($readit) {
15558: {
15559: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15560: %perlvar = (%perlvar,%{$configvars});
15561: }
15562:
15563:
1.1 albertel 15564: # ------------------------------------------------------ Read spare server file
15565: {
1.1172.2.96 raeburn 15566: open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 15567:
15568: while (my $configline=<$config>) {
15569: chomp($configline);
1.284 matthew 15570: if ($configline) {
1.784 albertel 15571: my ($host,$type) = split(':',$configline,2);
1.785 albertel 15572: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 15573: push(@{ $spareid{$type} }, $host);
1.1 albertel 15574: }
15575: }
1.448 albertel 15576: close($config);
1.1 albertel 15577: }
1.11 www 15578: # ------------------------------------------------------------ Read permissions
15579: {
1.1172.2.96 raeburn 15580: open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
1.11 www 15581:
15582: while (my $configline=<$config>) {
1.448 albertel 15583: chomp($configline);
15584: if ($configline) {
15585: my ($role,$perm)=split(/ /,$configline);
15586: if ($perm ne '') { $pr{$role}=$perm; }
15587: }
1.11 www 15588: }
1.448 albertel 15589: close($config);
1.11 www 15590: }
15591:
15592: # -------------------------------------------- Read plain texts for permissions
15593: {
1.1172.2.96 raeburn 15594: open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 15595:
15596: while (my $configline=<$config>) {
1.448 albertel 15597: chomp($configline);
15598: if ($configline) {
1.742 raeburn 15599: my ($short,@plain)=split(/:/,$configline);
15600: %{$prp{$short}} = ();
15601: if (@plain > 0) {
15602: $prp{$short}{'std'} = $plain[0];
15603: for (my $i=1; $i<@plain; $i++) {
15604: $prp{$short}{'alt'.$i} = $plain[$i];
15605: }
15606: }
1.448 albertel 15607: }
1.135 www 15608: }
1.448 albertel 15609: close($config);
1.135 www 15610: }
15611:
15612: # ---------------------------------------------------------- Read package table
15613: {
1.1172.2.96 raeburn 15614: open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
1.135 www 15615:
15616: while (my $configline=<$config>) {
1.483 albertel 15617: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 15618: chomp($configline);
15619: my ($short,$plain)=split(/:/,$configline);
15620: my ($pack,$name)=split(/\&/,$short);
15621: if ($plain ne '') {
15622: $packagetab{$pack.'&'.$name.'&name'}=$name;
15623: $packagetab{$short}=$plain;
15624: }
1.11 www 15625: }
1.448 albertel 15626: close($config);
1.329 matthew 15627: }
15628:
1.1172.2.27 raeburn 15629: # --------------------------------------------------------- Read loncaparev table
1.1073 raeburn 15630:
1.1172.2.27 raeburn 15631: &load_loncaparevs();
15632:
15633: # ------------------------------------------------------- Read serverhostID table
15634:
15635: &load_serverhomeIDs();
1.1074 raeburn 15636:
1.1172.2.27 raeburn 15637: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 15638: {
15639: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15640: if (-e $file) {
15641: my $parser = HTML::LCParser->new($file);
15642: while (my $token = $parser->get_token()) {
15643: if ($token->[0] eq 'S') {
15644: my $item = $token->[1];
15645: my $name = $token->[2]{'name'};
15646: my $value = $token->[2]{'value'};
15647: if ($item ne '' && $name ne '' && $value ne '') {
15648: my $release = $parser->get_text();
15649: $release =~ s/(^\s*|\s*$ )//gx;
15650: $needsrelease{$item.':'.$name.':'.$value} = $release;
15651: }
15652: }
15653: }
15654: }
1.1073 raeburn 15655: }
15656:
1.1138 raeburn 15657: # ---------------------------------------------------------- Read managers table
15658: {
15659: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
1.1172.2.96 raeburn 15660: if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
1.1138 raeburn 15661: while (my $configline=<$config>) {
15662: chomp($configline);
15663: next if ($configline =~ /^\#/);
15664: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15665: $managerstab{$configline} = 1;
15666: }
15667: }
15668: close($config);
15669: }
15670: }
15671: }
15672:
1.329 matthew 15673: # ------------- set up temporary directory
15674: {
1.1117 foxr 15675: $tmpdir = LONCAPA::tempdir();
1.329 matthew 15676:
1.11 www 15677: }
15678:
1.1172.2.104 raeburn 15679: # ------------- set default texengine (domain default overrides this)
15680: {
15681: $deftex = LONCAPA::texengine();
15682: }
15683:
1.1172.2.114 raeburn 15684: # ------------- set default minimum length for passwords for internal auth users
15685: {
15686: $passwdmin = LONCAPA::passwd_min();
15687: }
15688:
1.794 albertel 15689: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
15690: 'compress_threshold'=> 20_000,
15691: });
1.185 www 15692:
1.281 www 15693: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 15694: $dumpcount=0;
1.958 www 15695: $locknum=0;
1.22 www 15696:
1.163 harris41 15697: &logtouch();
1.672 albertel 15698: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 15699: $readit=1;
1.564 albertel 15700: {
15701: use integer;
15702: my $test=(2**32)+1;
1.568 albertel 15703: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 15704: &logthis(" Detected 64bit platform ($_64bit)");
15705: }
1.195 www 15706: }
1.1 albertel 15707: }
1.179 www 15708:
1.1 albertel 15709: 1;
1.191 harris41 15710: __END__
15711:
1.243 albertel 15712: =pod
15713:
1.191 harris41 15714: =head1 NAME
15715:
1.243 albertel 15716: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 15717:
15718: =head1 SYNOPSIS
15719:
1.243 albertel 15720: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 15721:
15722: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15723:
1.243 albertel 15724: Common parameters:
15725:
15726: =over 4
15727:
15728: =item *
15729:
15730: $uname : an internal username (if $cname expecting a course Id specifically)
15731:
15732: =item *
15733:
15734: $udom : a domain (if $cdom expecting a course's domain specifically)
15735:
15736: =item *
15737:
15738: $symb : a resource instance identifier
15739:
15740: =item *
15741:
15742: $namespace : the name of a .db file that contains the data needed or
15743: being set.
15744:
15745: =back
15746:
1.394 bowersj2 15747: =head1 OVERVIEW
1.191 harris41 15748:
1.394 bowersj2 15749: lonnet provides subroutines which interact with the
15750: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15751: about classes, users, and resources.
1.243 albertel 15752:
15753: For many of these objects you can also use this to store data about
15754: them or modify them in various ways.
1.191 harris41 15755:
1.394 bowersj2 15756: =head2 Symbs
1.191 harris41 15757:
1.394 bowersj2 15758: To identify a specific instance of a resource, LON-CAPA uses symbols
15759: or "symbs"X<symb>. These identifiers are built from the URL of the
15760: map, the resource number of the resource in the map, and the URL of
15761: the resource itself. The latter is somewhat redundant, but might help
15762: if maps change.
15763:
15764: An example is
15765:
15766: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15767:
15768: The respective map entry is
15769:
15770: <resource id="19" src="/res/msu/korte/tests/part12.problem"
15771: title="Problem 2">
15772: </resource>
15773:
15774: Symbs are used by the random number generator, as well as to store and
15775: restore data specific to a certain instance of for example a problem.
15776:
15777: =head2 Storing And Retrieving Data
15778:
15779: X<store()>X<cstore()>X<restore()>Three of the most important functions
15780: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15781: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15782: is is the non-critical message twin of cstore. These functions are for
15783: handlers to store a perl hash to a user's permanent data space in an
15784: easy manner, and to retrieve it again on another call. It is expected
15785: that a handler would use this once at the beginning to retrieve data,
15786: and then again once at the end to send only the new data back.
15787:
15788: The data is stored in the user's data directory on the user's
15789: homeserver under the ID of the course.
15790:
15791: The hash that is returned by restore will have all of the previous
15792: value for all of the elements of the hash.
15793:
15794: Example:
15795:
15796: #creating a hash
15797: my %hash;
15798: $hash{'foo'}='bar';
15799:
15800: #storing it
15801: &Apache::lonnet::cstore(\%hash);
15802:
15803: #changing a value
15804: $hash{'foo'}='notbar';
15805:
15806: #adding a new value
15807: $hash{'bar'}='foo';
15808: &Apache::lonnet::cstore(\%hash);
15809:
15810: #retrieving the hash
15811: my %history=&Apache::lonnet::restore();
15812:
15813: #print the hash
15814: foreach my $key (sort(keys(%history))) {
15815: print("\%history{$key} = $history{$key}");
15816: }
15817:
15818: Will print out:
1.191 harris41 15819:
1.394 bowersj2 15820: %history{1:foo} = bar
15821: %history{1:keys} = foo:timestamp
15822: %history{1:timestamp} = 990455579
15823: %history{2:bar} = foo
15824: %history{2:foo} = notbar
15825: %history{2:keys} = foo:bar:timestamp
15826: %history{2:timestamp} = 990455580
15827: %history{bar} = foo
15828: %history{foo} = notbar
15829: %history{timestamp} = 990455580
15830: %history{version} = 2
15831:
15832: Note that the special hash entries C<keys>, C<version> and
15833: C<timestamp> were added to the hash. C<version> will be equal to the
15834: total number of versions of the data that have been stored. The
15835: C<timestamp> attribute will be the UNIX time the hash was
15836: stored. C<keys> is available in every historical section to list which
15837: keys were added or changed at a specific historical revision of a
15838: hash.
15839:
15840: B<Warning>: do not store the hash that restore returns directly. This
15841: will cause a mess since it will restore the historical keys as if the
15842: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 15843:
1.394 bowersj2 15844: Calling convention:
1.191 harris41 15845:
1.1172.2.27 raeburn 15846: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1172.2.64 raeburn 15847: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 15848:
1.394 bowersj2 15849: For more detailed information, see lonnet specific documentation.
1.191 harris41 15850:
1.394 bowersj2 15851: =head1 RETURN MESSAGES
1.191 harris41 15852:
1.394 bowersj2 15853: =over 4
1.191 harris41 15854:
1.394 bowersj2 15855: =item * B<con_lost>: unable to contact remote host
1.191 harris41 15856:
1.394 bowersj2 15857: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15858: when the connection is brought back up
1.191 harris41 15859:
1.394 bowersj2 15860: =item * B<con_failed>: unable to contact remote host and unable to save message
15861: for later delivery
1.191 harris41 15862:
1.967 bisitz 15863: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 15864:
1.394 bowersj2 15865: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 15866: that was requested
1.191 harris41 15867:
1.243 albertel 15868: =back
1.191 harris41 15869:
1.243 albertel 15870: =head1 PUBLIC SUBROUTINES
1.191 harris41 15871:
1.243 albertel 15872: =head2 Session Environment Functions
1.191 harris41 15873:
1.243 albertel 15874: =over 4
1.191 harris41 15875:
1.394 bowersj2 15876: =item *
15877: X<appenv()>
1.949 raeburn 15878: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 15879: the user envirnoment file, and will be restored for each access this
1.620 albertel 15880: user makes during this session, also modifies the %env for the current
1.949 raeburn 15881: process. Optional rolesarrayref - if defined contains a reference to an array
15882: of roles which are exempt from the restriction on modifying user.role entries
15883: in the user's environment.db and in %env.
1.191 harris41 15884:
15885: =item *
1.394 bowersj2 15886: X<delenv()>
1.987 raeburn 15887: B<delenv($delthis,$regexp)>: removes all items from the session
15888: environment file that begin with $delthis. If the
15889: optional second arg - $regexp - is true, $delthis is treated as a
15890: regular expression, otherwise \Q$delthis\E is used.
15891: The values are also deleted from the current processes %env.
1.191 harris41 15892:
1.795 albertel 15893: =item * get_env_multiple($name)
15894:
15895: gets $name from the %env hash, it seemlessly handles the cases where multiple
15896: values may be defined and end up as an array ref.
15897:
15898: returns an array of values
15899:
1.243 albertel 15900: =back
15901:
15902: =head2 User Information
1.191 harris41 15903:
1.243 albertel 15904: =over 4
1.191 harris41 15905:
15906: =item *
1.394 bowersj2 15907: X<queryauthenticate()>
15908: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 15909: authentication scheme
15910:
15911: =item *
1.394 bowersj2 15912: X<authenticate()>
1.1073 raeburn 15913: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 15914: authenticate user from domain's lib servers (first use the current
15915: one). C<$upass> should be the users password.
1.1073 raeburn 15916: $checkdefauth is optional (value is 1 if a check should be made to
15917: authenticate user using default authentication method, and allow
15918: account creation if username does not have account in the domain).
15919: $clientcancheckhost is optional (value is 1 if checking whether the
15920: server can host will occur on the client side in lonauth.pm).
1.191 harris41 15921:
15922: =item *
1.394 bowersj2 15923: X<homeserver()>
15924: B<homeserver($uname,$udom)>: find the server which has
15925: the user's directory and files (there must be only one), this caches
15926: the answer, and also caches if there is a borken connection.
1.191 harris41 15927:
15928: =item *
1.394 bowersj2 15929: X<idget()>
15930: B<idget($udom,@ids)>: find the usernames behind a list of IDs
15931: (IDs are a unique resource in a domain, there must be only 1 ID per
15932: username, and only 1 username per ID in a specific domain) (returns
15933: hash: id=>name,id=>name)
1.191 harris41 15934:
15935: =item *
1.394 bowersj2 15936: X<idrget()>
15937: B<idrget($udom,@unames)>: find the IDs behind a list of
15938: usernames (returns hash: name=>id,name=>id)
1.191 harris41 15939:
15940: =item *
1.394 bowersj2 15941: X<idput()>
15942: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 15943:
15944: =item *
1.394 bowersj2 15945: X<rolesinit()>
1.1169 droeschl 15946: B<rolesinit($udom,$username)>: get user privileges.
15947: returns user role, first access and timer interval hashes
1.243 albertel 15948:
15949: =item *
1.1171 droeschl 15950: X<privileged()>
15951: B<privileged($username,$domain)>: returns a true if user has a
15952: privileged and active role (i.e. su or dc), false otherwise.
15953:
15954: =item *
1.551 albertel 15955: X<getsection()>
15956: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 15957: course $cname, return section name/number or '' for "not in course"
15958: and '-1' for "no section"
15959:
15960: =item *
1.394 bowersj2 15961: X<userenvironment()>
15962: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 15963: passed in @what from the requested user's environment, returns a hash
15964:
1.858 raeburn 15965: =item *
15966: X<userlog_query()>
1.859 albertel 15967: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15968: activity.log file. %filters defines filters applied when parsing the
15969: log file. These can be start or end timestamps, or the type of action
15970: - log to look for Login or Logout events, check for Checkin or
15971: Checkout, role for role selection. The response is in the form
15972: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
15973: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 15974:
1.243 albertel 15975: =back
15976:
15977: =head2 User Roles
15978:
15979: =over 4
15980:
15981: =item *
15982:
1.1172.2.65 raeburn 15983: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
15984: returns codes for allowed actions.
15985:
15986: The first argument is required, all others are optional.
15987:
15988: $priv is the privilege being checked.
15989: $uri contains additional information about what is being checked for access (e.g.,
15990: URL, course ID etc.).
15991: $symb is the unique resource instance identifier in a course; if needed,
15992: but not provided, it will be retrieved via a call to &symbread().
15993: $role is the role for which a priv is being checked (only used if priv is evb).
15994: $clientip is the user's IP address (only used when checking for access to portfolio
15995: files).
15996: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
15997: prevents recursive calls to &allowed.
15998:
1.243 albertel 15999: F: full access
16000: U,I,K: authentication modes (cxx only)
16001: '': forbidden
16002: 1: user needs to choose course
16003: 2: browse allowed
1.766 albertel 16004: A: passphrase authentication needed
1.1172.2.65 raeburn 16005: B: access temporarily blocked because of a blocking event in a course.
1.1172.2.146. .1(raebu 16006:22): D: access blocked because access is required via session initiated via deep-link
1.243 albertel 16007:
16008: =item *
16009:
1.1172.2.13 raeburn 16010: constructaccess($url,$setpriv) : check for access to construction space URL
16011:
16012: See if the owner domain and name in the URL match those in the
16013: expected environment. If so, return three element list
16014: ($ownername,$ownerdomain,$ownerhome).
16015:
16016: Otherwise return the null string.
16017:
16018: If second argument 'setpriv' is true, it assigns the privileges,
16019: and returns the same three element list, unless the owner has
16020: blocked "ad hoc" Domain Coordinator access to the Author Space,
16021: in which case the null string is returned.
16022:
16023: =item *
16024:
1.1172.2.84 raeburn 16025: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16026: define a custom role rolename set privileges in format of lonTabs/roles.tab
16027: for system, domain, and course level. $uname and $udom are optional (current
16028: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 16029:
16030: =item *
16031:
1.988 raeburn 16032: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
16033: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 16034: $type is Course (default) or Community.
1.988 raeburn 16035: If $forcedefault evaluates to true, text returned will be default
16036: text for $type. Otherwise, if this is a course, the text returned
16037: will be a custom name for the role (if defined in the course's
16038: environment). If no custom name is defined the default is returned.
16039:
1.832 raeburn 16040: =item *
16041:
1.1172.2.23 raeburn 16042: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 16043: All arguments are optional. Returns a hash of a roles, either for
16044: co-author/assistant author roles for a user's Construction Space
1.906 albertel 16045: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 16046: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16047: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16048: For each key, value is set to colon-separated start and end times for
16049: the role. If no username and domain are specified, will default to
1.934 raeburn 16050: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 16051: of role statuses (active, future or previous), roles
16052: (e.g., cc,in, st etc.) and domains of the roles which can be used
16053: to restrict the list of roles reported. If no array ref is
16054: provided for types, will default to return only active roles.
1.834 albertel 16055:
1.1172.2.13 raeburn 16056: =item *
16057:
16058: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16059: user: $uname:$udom has a role in the course: $cdom_$cnum.
16060:
16061: Additional optional arguments are: $type (if role checking is to be restricted
16062: to certain user status types -- previous (expired roles), active (currently
16063: available roles) or future (roles available in the future), and
16064: $hideprivileged -- if true will not report course roles for users who
1.1172.2.23 raeburn 16065: have active Domain Coordinator role in course's domain or in additional
16066: domains (specified in 'Domains to check for privileged users' in course
16067: environment -- set via: Course Settings -> Classlists and staff listing).
16068:
16069: =item *
16070:
16071: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16072: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16073: $possdomains and $possroles are optional array refs -- to domains to check and
16074: roles to check. If $possdomains is not specified, a dump will be done of the
16075: users' roles.db to check for a dc or su role in any domain. This can be
16076: time consuming if &privileged is called repeatedly (e.g., when displaying a
16077: classlist), so in such cases, supplying a $possdomains array is preferred, as
16078: this then allows &privileged_by_domain() to be used, which caches the identity
16079: of privileged users, eliminating the need for repeated calls to &dump().
16080:
16081: =item *
16082:
16083: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16084: where the outer hash keys are domains specified in the $possdomains array ref,
16085: next inner hash keys are privileged roles specified in the $roles array ref,
16086: and the innermost hash contains key = value pairs for username:domain = end:start
16087: for active or future "privileged" users with that role in that domain. To avoid
16088: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16089: innerhash are cached using priv_$role and $dom as the identifiers.
1.1172.2.13 raeburn 16090:
1.243 albertel 16091: =back
16092:
16093: =head2 User Modification
16094:
16095: =over 4
16096:
16097: =item *
16098:
1.957 raeburn 16099: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 16100: user for the level given by URL. Optional start and end dates (leave empty
16101: string or zero for "no date")
1.191 harris41 16102:
16103: =item *
16104:
1.243 albertel 16105: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16106: change a users, password, possible return values are: ok,
16107: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16108: refused
1.191 harris41 16109:
16110: =item *
16111:
1.243 albertel 16112: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 16113:
16114: =item *
16115:
1.1058 raeburn 16116: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16117: $forceid,$desiredhome,$email,$inststatus,$candelete) :
16118:
16119: will update user information (firstname,middlename,lastname,generation,
16120: permanentemail), and if forceid is true, student/employee ID also.
16121: A user's institutional affiliation(s) can also be updated.
16122: User information fields will not be overwritten with empty entries
16123: unless the field is included in the $candelete array reference.
16124: This array is included when a single user is modified via "Manage Users",
16125: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 16126:
16127: =item *
16128:
1.286 matthew 16129: modifystudent
16130:
1.957 raeburn 16131: modify a student's enrollment and identification information.
1.1172.2.31 raeburn 16132: The course id is resolved based on the current user's environment.
16133: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 16134: associated with a course.
16135:
1.297 matthew 16136: This call is essentially a wrapper for lonnet::modifyuser and
16137: lonnet::modify_student_enrollment
1.286 matthew 16138:
16139: Inputs:
16140:
16141: =over 4
16142:
1.957 raeburn 16143: =item B<$udom> Student's loncapa domain
1.286 matthew 16144:
1.957 raeburn 16145: =item B<$uname> Student's loncapa login name
1.286 matthew 16146:
1.964 bisitz 16147: =item B<$uid> Student/Employee ID
1.286 matthew 16148:
1.957 raeburn 16149: =item B<$umode> Student's authentication mode
1.286 matthew 16150:
1.957 raeburn 16151: =item B<$upass> Student's password
1.286 matthew 16152:
1.957 raeburn 16153: =item B<$first> Student's first name
1.286 matthew 16154:
1.957 raeburn 16155: =item B<$middle> Student's middle name
1.286 matthew 16156:
1.957 raeburn 16157: =item B<$last> Student's last name
1.286 matthew 16158:
1.957 raeburn 16159: =item B<$gene> Student's generation
1.286 matthew 16160:
1.957 raeburn 16161: =item B<$usec> Student's section in course
1.286 matthew 16162:
16163: =item B<$end> Unix time of the roles expiration
16164:
16165: =item B<$start> Unix time of the roles start date
16166:
16167: =item B<$forceid> If defined, allow $uid to be changed
16168:
16169: =item B<$desiredhome> server to use as home server for student
16170:
1.957 raeburn 16171: =item B<$email> Student's permanent e-mail address
16172:
16173: =item B<$type> Type of enrollment (auto or manual)
16174:
1.963 raeburn 16175: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
16176:
16177: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 16178:
1.963 raeburn 16179: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 16180:
1.963 raeburn 16181: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 16182:
1.1172.2.19 raeburn 16183: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16184:
16185: =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 16186:
1.286 matthew 16187: =back
1.297 matthew 16188:
16189: =item *
16190:
16191: modify_student_enrollment
16192:
1.1172.2.31 raeburn 16193: Change a student's enrollment status in a class. The environment variable
1.297 matthew 16194: 'role.request.course' must be defined for this function to proceed.
16195:
16196: Inputs:
16197:
16198: =over 4
16199:
1.1172.2.31 raeburn 16200: =item $udom, student's domain
1.297 matthew 16201:
1.1172.2.31 raeburn 16202: =item $uname, student's name
1.297 matthew 16203:
1.1172.2.31 raeburn 16204: =item $uid, student's user id
1.297 matthew 16205:
1.1172.2.31 raeburn 16206: =item $first, student's first name
1.297 matthew 16207:
16208: =item $middle
16209:
16210: =item $last
16211:
16212: =item $gene
16213:
16214: =item $usec
16215:
16216: =item $end
16217:
16218: =item $start
16219:
1.957 raeburn 16220: =item $type
16221:
16222: =item $locktype
16223:
16224: =item $cid
16225:
16226: =item $selfenroll
16227:
16228: =item $context
16229:
1.1172.2.19 raeburn 16230: =item $credits, number of credits student will earn from this class
16231:
1.1172.2.76 raeburn 16232: =item $instsec, institutional course section code for student
16233:
1.297 matthew 16234: =back
16235:
1.191 harris41 16236:
16237: =item *
16238:
1.243 albertel 16239: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16240: custom role; give a custom role to a user for the level given by URL. Specify
16241: name and domain of role author, and role name
1.191 harris41 16242:
16243: =item *
16244:
1.243 albertel 16245: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 16246:
16247: =item *
16248:
1.243 albertel 16249: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16250:
16251: =back
16252:
16253: =head2 Course Infomation
16254:
16255: =over 4
1.191 harris41 16256:
16257: =item *
16258:
1.1118 foxr 16259: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 16260: specified course id, including all environment settings for the
16261: course, the description of the course will be in the hash under the
16262: key 'description'
1.191 harris41 16263:
1.1118 foxr 16264: $options is an optional parameter that if supplied is a hash reference that controls
16265: what how this function works. It has the following key/values:
16266:
16267: =over 4
16268:
16269: =item freshen_cache
16270:
16271: If defined, and the environment cache for the course is valid, it is
16272: returned in the returned hash.
16273:
16274: =item one_time
16275:
16276: If defined, the last cache time is set to _now_
16277:
16278: =item user
16279:
16280: If defined, the supplied username is used instead of the current user.
16281:
16282:
16283: =back
16284:
1.191 harris41 16285: =item *
16286:
1.624 albertel 16287: resdata($name,$domain,$type,@which) : request for current parameter
16288: setting for a specific $type, where $type is either 'course' or 'user',
16289: @what should be a list of parameters to ask about. This routine caches
1.1172.2.31 raeburn 16290: answers for 10 minutes.
1.243 albertel 16291:
1.877 foxr 16292: =item *
16293:
16294: get_courseresdata($courseid, $domain) : dump the entire course resource
16295: data base, returning a hash that is keyed by the resource name and has
16296: values that are the resource value. I believe that the timestamps and
16297: versions are also returned.
16298:
1.243 albertel 16299: =back
16300:
16301: =head2 Course Modification
16302:
16303: =over 4
1.191 harris41 16304:
16305: =item *
16306:
1.243 albertel 16307: writecoursepref($courseid,%prefs) : write preferences (environment
16308: database) for a course
1.191 harris41 16309:
16310: =item *
16311:
1.1011 raeburn 16312: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16313:
16314: =item *
16315:
1.1038 raeburn 16316: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 16317:
1.1167 droeschl 16318: =item *
16319:
16320: is_course($courseid), is_course($cdom, $cnum)
16321:
16322: Accepts either a combined $courseid (in the form of domain_courseid) or the
16323: two component version $cdom, $cnum. It checks if the specified course exists.
16324:
16325: Returns:
16326: undef if the course doesn't exist, otherwise
16327: in scalar context the combined courseid.
16328: in list context the two components of the course identifier, domain and
16329: courseid.
16330:
1.243 albertel 16331: =back
16332:
1.1172.2.109 raeburn 16333: =head2 Bubblesheet Configuration
16334:
16335: =over 4
16336:
16337: =item *
16338:
16339: get_scantron_config($which)
16340:
16341: $which - the name of the configuration to parse from the file.
16342:
16343: Parses and returns the bubblesheet configuration line selected as a
16344: hash of configuration file fields.
16345:
16346:
16347: Returns:
16348: If the named configuration is not in the file, an empty
16349: hash is returned.
16350:
16351: a hash with the fields
16352: name - internal name for the this configuration setup
16353: description - text to display to operator that describes this config
16354: CODElocation - if 0 or the string 'none'
16355: - no CODE exists for this config
16356: if -1 || the string 'letter'
16357: - a CODE exists for this config and is
16358: a string of letters
16359: Unsupported value (but planned for future support)
16360: if a positive integer
16361: - The CODE exists as the first n items from
16362: the question section of the form
16363: if the string 'number'
16364: - The CODE exists for this config and is
16365: a string of numbers
16366: CODEstart - (only matter if a CODE exists) column in the line where
16367: the CODE starts
16368: CODElength - length of the CODE
16369: IDstart - column where the student/employee ID starts
16370: IDlength - length of the student/employee ID info
16371: Qstart - column where the information from the bubbled
16372: 'questions' start
16373: Qlength - number of columns comprising a single bubble line from
16374: the sheet. (usually either 1 or 10)
16375: Qon - either a single character representing the character used
16376: to signal a bubble was chosen in the positional setup, or
16377: the string 'letter' if the letter of the chosen bubble is
16378: in the final, or 'number' if a number representing the
16379: chosen bubble is in the file (1->A 0->J)
16380: Qoff - the character used to represent that a bubble was
16381: left blank
16382: PaperID - if the scanning process generates a unique number for each
16383: sheet scanned the column that this ID number starts in
16384: PaperIDlength - number of columns that comprise the unique ID number
16385: for the sheet of paper
16386: FirstName - column that the first name starts in
16387: FirstNameLength - number of columns that the first name spans
16388: LastName - column that the last name starts in
16389: LastNameLength - number of columns that the last name spans
16390: BubblesPerRow - number of bubbles available in each row used to
16391: bubble an answer. (If not specified, 10 assumed).
16392:
16393:
16394: =item *
16395:
16396: get_scantronformat_file($cdom)
16397:
16398: $cdom - the course's domain (optional); if not supplied, uses
16399: domain for current $env{'request.course.id'}.
16400:
16401: Returns an array containing lines from the scantron format file for
16402: the domain of the course.
16403:
16404: If a url for a custom.tab file is listed in domain's configuration.db,
16405: lines are from this file.
16406:
16407: Otherwise, if a default.tab has been published in RES space by the
16408: domainconfig user, lines are from this file.
16409:
16410: Otherwise, fall back to getting lines from the legacy file on the
16411: local server: /home/httpd/lonTabs/default_scantronformat.tab
16412:
16413: =back
16414:
1.243 albertel 16415: =head2 Resource Subroutines
16416:
16417: =over 4
1.191 harris41 16418:
16419: =item *
16420:
1.243 albertel 16421: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 16422:
16423: =item *
16424:
1.243 albertel 16425: repcopy($filename) : subscribes to the requested file, and attempts to
16426: replicate from the owning library server, Might return
1.607 raeburn 16427: 'unavailable', 'not_found', 'forbidden', 'ok', or
16428: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 16429: resource. Expects the local filesystem pathname
16430: (/home/httpd/html/res/....)
16431:
16432: =back
16433:
16434: =head2 Resource Information
16435:
16436: =over 4
1.191 harris41 16437:
16438: =item *
16439:
1.1172.2.28 raeburn 16440: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
16441: and returns the value of a variety of different possible values,
16442: $varname should be a request string, and the other parameters can be
16443: used to specify who and what one is asking about. Ordinarily, $cid
16444: does not need to be specified, as it is retrived from
16445: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16446: within lonuserstate::loadmap() when initializing a course, before
16447: $env{'request.course.id'} has been set, so it needs to be provided
16448: in that one case.
1.243 albertel 16449:
16450: Possible values for $varname are environment.lastname (or other item
16451: from the envirnment hash), user.name (or someother aspect about the
16452: user), resource.0.maxtries (or some other part and parameter of a
16453: resource)
1.204 albertel 16454:
16455: =item *
16456:
1.243 albertel 16457: directcondval($number) : get current value of a condition; reads from a state
16458: string
1.204 albertel 16459:
16460: =item *
16461:
1.243 albertel 16462: condval($condidx) : value of condition index based on state
1.204 albertel 16463:
16464: =item *
16465:
1.1172.2.146. .13(raeb 16466:-23): metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
1.243 albertel 16467: resource's metadata, $what should be either a specific key, or either
16468: 'keys' (to get a list of possible keys) or 'packages' to get a list of
1.1172.2.146. .13(raeb 16469:-23): packages that this resource currently uses, the last 3 arguments are
16470:-23): only used internally for recursive metadata.
16471:-23):
16472:-23): the toolsymb is only used where the uri is for an external tool (for which
16473:-23): the uri as well as the symb are guaranteed to be unique).
1.243 albertel 16474:
16475: this function automatically caches all requests
1.191 harris41 16476:
16477: =item *
16478:
1.243 albertel 16479: metadata_query($query,$custom,$customshow) : make a metadata query against the
16480: network of library servers; returns file handle of where SQL and regex results
16481: will be stored for query
1.191 harris41 16482:
16483: =item *
16484:
1.1172.2.66 raeburn 16485: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
16486: return symbolic list entry (all arguments optional).
16487:
16488: Args: filename is the filename (including path) for the file for which a symb
16489: is required; donotrecurse, if true will prevent calls to allowed() being made
16490: to check access status if more than one resource was found in the bighash
16491: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
16492: a randompick); ignorecachednull, if true will prevent a symb of '' being
16493: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16494: cause possible symbs to be checked to determine if they are subject to content
16495: blocking, if so they will not be included as possible symbs; possibles is a
16496: ref to a hash, which, as a side effect, will be populated with all possible
16497: symbs (content blocking not tested).
16498:
1.243 albertel 16499: returns the data handle
1.191 harris41 16500:
16501: =item *
16502:
1.1172.2.17 raeburn 16503: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
1.1172.2.11 raeburn 16504: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 16505: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1172.2.11 raeburn 16506: on failure, user must be in a course, as it assumes the existence of
16507: the course initial hash, and uses $env('request.course.id'}. The third
16508: arg is an optional reference to a scalar. If this arg is passed in the
16509: call to symbverify, it will be set to 1 if the symb has been set to be
16510: encrypted; otherwise it will be null.
1.243 albertel 16511:
1.191 harris41 16512: =item *
16513:
1.243 albertel 16514: symbclean($symb) : removes versions numbers from a symb, returns the
16515: cleaned symb
1.191 harris41 16516:
16517: =item *
16518:
1.243 albertel 16519: is_on_map($uri) : checks if the $uri is somewhere on the current
16520: course map, user must be in a course for it to work.
1.191 harris41 16521:
16522: =item *
16523:
1.243 albertel 16524: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 16525:
16526: =item *
16527:
1.243 albertel 16528: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16529: a random seed, all arguments are optional, if they aren't sent it uses the
16530: environment to derive them. Note: if symb isn't sent and it can't get one
16531: from &symbread it will use the current time as its return value
1.191 harris41 16532:
16533: =item *
16534:
1.243 albertel 16535: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16536: unfakeable, receipt
1.191 harris41 16537:
16538: =item *
16539:
1.620 albertel 16540: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 16541:
16542: =item *
16543:
1.243 albertel 16544: countacc($url) : count the number of accesses to a given URL
1.191 harris41 16545:
16546: =item *
16547:
1.243 albertel 16548: 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 16549:
16550: =item *
16551:
1.243 albertel 16552: 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 16553:
16554: =item *
16555:
1.243 albertel 16556: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 16557:
16558: =item *
16559:
1.243 albertel 16560: devalidate($symb) : devalidate temporary spreadsheet calculations,
16561: forcing spreadsheet to reevaluate the resource scores next time.
16562:
1.1172.2.13 raeburn 16563: =item *
16564:
16565: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16566: when viewing in course context.
16567:
16568: input: six args -- filename (decluttered), course number, course domain,
16569: url, symb (if registered) and group (if this is a
16570: group item -- e.g., bulletin board, group page etc.).
16571:
16572: output: array of five scalars --
16573: $cfile -- url for file editing if editable on current server
16574: $home -- homeserver of resource (i.e., for author if published,
16575: or course if uploaded.).
16576: $switchserver -- 1 if server switch will be needed.
16577: $forceedit -- 1 if icon/link should be to go to edit mode
16578: $forceview -- 1 if icon/link should be to go to view mode
16579:
16580: =item *
16581:
16582: is_course_upload($file,$cnum,$cdom)
16583:
16584: Used in course context to determine if current file was uploaded to
16585: the course (i.e., would be found in /userfiles/docs on the course's
16586: homeserver.
16587:
16588: input: 3 args -- filename (decluttered), course number and course domain.
16589: output: boolean -- 1 if file was uploaded.
16590:
1.243 albertel 16591: =back
16592:
16593: =head2 Storing/Retreiving Data
16594:
16595: =over 4
1.191 harris41 16596:
16597: =item *
16598:
1.1172.2.64 raeburn 16599: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16600: permanently for this url; hashref needs to be given and should be a \%hashname;
16601: the remaining args aren't required and if they aren't passed or are '' they will
16602: be derived from the env (with the exception of $laststore, which is an
16603: optional arg used when a user's submission is stored in grading).
16604: $laststore is $version=$timestamp, where $version is the most recent version
16605: number retrieved for the corresponding $symb in the $namespace db file, and
16606: $timestamp is the timestamp for that transaction (UNIX time).
16607: $laststore is currently only passed when cstore() is called by
16608: structuretags::finalize_storage().
1.191 harris41 16609:
16610: =item *
16611:
1.1172.2.64 raeburn 16612: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16613: but uses critical subroutine
1.191 harris41 16614:
16615: =item *
16616:
1.243 albertel 16617: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16618: all args are optional
1.191 harris41 16619:
16620: =item *
16621:
1.717 albertel 16622: dumpstore($namespace,$udom,$uname,$regexp,$range) :
16623: dumps the complete (or key matching regexp) namespace into a hash
16624: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16625: normally &store()ed into
16626:
16627: $range should be either an integer '100' (give me the first 100
16628: matching records)
16629: or be two integers sperated by a - with no spaces
16630: '30-50' (give me the 30th through the 50th matching
16631: records)
16632:
16633:
16634: =item *
16635:
1.1172.2.59 raeburn 16636: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 16637: replaces a &store() version of data with a replacement set of data
16638: for a particular resource in a namespace passed in the $storehash hash
1.1172.2.59 raeburn 16639: reference. If $tolog is true, the transaction is logged in the courselog
16640: with an action=PUTSTORE.
1.717 albertel 16641:
16642: =item *
16643:
1.243 albertel 16644: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16645: works very similar to store/cstore, but all data is stored in a
16646: temporary location and can be reset using tmpreset, $storehash should
16647: be a hash reference, returns nothing on success
1.191 harris41 16648:
16649: =item *
16650:
1.243 albertel 16651: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16652: similar to restore, but all data is stored in a temporary location and
16653: can be reset using tmpreset. Returns a hash of values on success,
16654: error string otherwise.
1.191 harris41 16655:
16656: =item *
16657:
1.243 albertel 16658: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16659: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 16660:
16661: =item *
16662:
1.243 albertel 16663: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16664: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 16665:
16666: =item *
16667:
1.243 albertel 16668: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16669: namesp ($udom and $uname are optional)
1.191 harris41 16670:
16671: =item *
16672:
1.702 albertel 16673: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 16674: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 16675: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 16676:
1.702 albertel 16677: $range should be either an integer '100' (give me the first 100
16678: matching records)
16679: or be two integers sperated by a - with no spaces
16680: '30-50' (give me the 30th through the 50th matching
16681: records)
1.449 matthew 16682: =item *
16683:
16684: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16685: $store can be a scalar, an array reference, or if the amount to be
16686: incremented is > 1, a hash reference.
16687:
16688: ($udom and $uname are optional)
1.191 harris41 16689:
16690: =item *
16691:
1.243 albertel 16692: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16693: ($udom and $uname are optional)
1.191 harris41 16694:
16695: =item *
16696:
1.243 albertel 16697: cput($namespace,$storehash,$udom,$uname) : critical put
16698: ($udom and $uname are optional)
1.191 harris41 16699:
16700: =item *
16701:
1.748 albertel 16702: newput($namespace,$storehash,$udom,$uname) :
16703:
16704: Attempts to store the items in the $storehash, but only if they don't
16705: currently exist, if this succeeds you can be certain that you have
16706: successfully created a new key value pair in the $namespace db.
16707:
16708:
16709: Args:
16710: $namespace: name of database to store values to
16711: $storehash: hashref to store to the db
16712: $udom: (optional) domain of user containing the db
16713: $uname: (optional) name of user caontaining the db
16714:
16715: Returns:
16716: 'ok' -> succeeded in storing all keys of $storehash
16717: 'key_exists: <key>' -> failed to anything out of $storehash, as at
16718: least <key> already existed in the db (other
16719: requested keys may also already exist)
1.967 bisitz 16720: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 16721: 'con_lost' -> unable to contact request server
16722: 'refused' -> action was not allowed by remote machine
16723:
16724:
16725: =item *
16726:
1.243 albertel 16727: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16728: reference filled in from namesp (encrypts the return communication)
16729: ($udom and $uname are optional)
1.191 harris41 16730:
16731: =item *
16732:
1.243 albertel 16733: log($udom,$name,$home,$message) : write to permanent log for user; use
16734: critical subroutine
16735:
1.806 raeburn 16736: =item *
16737:
1.860 raeburn 16738: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16739: array reference filled in from namespace found in domain level on either
16740: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 16741:
16742: =item *
16743:
1.860 raeburn 16744: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
16745: domain level either on specified domain server ($uhome) or primary domain
16746: server ($udom and $uhome are optional)
1.806 raeburn 16747:
1.943 raeburn 16748: =item *
16749:
1.1172.2.35 raeburn 16750: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
16751: for: authentication, language, quotas, timezone, date locale, and portal URL in
16752: the target domain.
16753:
16754: May also include additional key => value pairs for the following groups:
16755:
16756: =over
16757:
16758: =item
16759: disk quotas (MB allocated by default to portfolios and authoring spaces).
16760:
16761: =over
16762:
16763: =item defaultquota, authorquota
16764:
16765: =back
16766:
16767: =item
16768: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16769: portfolio for users).
16770:
16771: =over
16772:
16773: =item
16774: aboutme, blog, webdav, portfolio
16775:
16776: =back
16777:
16778: =item
16779: requestcourses: ability to request courses, and how requests are processed.
16780:
16781: =over
16782:
16783: =item
1.1172.2.37 raeburn 16784: official, unofficial, community, textbook
1.1172.2.35 raeburn 16785:
16786: =back
16787:
16788: =item
16789: inststatus: types of institutional affiliation, and order in which they are displayed.
16790:
16791: =over
16792:
16793: =item
1.1172.2.44 raeburn 16794: inststatustypes, inststatusorder, inststatusguest
1.1172.2.35 raeburn 16795:
16796: =back
16797:
16798: =item
16799: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16800: for course's uploaded content.
16801:
16802: =over
16803:
16804: =item
1.1172.2.68 raeburn 16805: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
16806: communityquota, textbookquota
1.1172.2.35 raeburn 16807:
16808: =back
16809:
16810: =item
16811: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16812: on your servers.
16813:
16814: =over
16815:
16816: =item
16817: remotesessions, hostedsessions
16818:
16819: =back
16820:
16821: =back
16822:
16823: In cases where a domain coordinator has never used the "Set Domain Configuration"
16824: utility to create a configuration.db file on a domain's primary library server
16825: only the following domain defaults: auth_def, auth_arg_def, lang_def
16826: -- corresponding values are authentication type (internal, krb4, krb5,
16827: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
16828: will be available. Values are retrieved from cache (if current), unless the
16829: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16830: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16831:
16832: Typical usage:
1.943 raeburn 16833:
1.1172.2.35 raeburn 16834: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 16835:
1.243 albertel 16836: =back
16837:
16838: =head2 Network Status Functions
16839:
16840: =over 4
1.191 harris41 16841:
16842: =item *
16843:
1.1137 raeburn 16844: dirlist() : return directory list based on URI (first arg).
16845:
16846: Inputs: 1 required, 5 optional.
16847:
16848: =over
16849:
16850: =item
16851: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16852:
16853: =item
16854: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
16855:
16856: =item
16857: $username - username of user/course to be listed. Extracted from $uri if absent.
16858:
16859: =item
16860: $getpropath - boolean: 1 if prepend path using &propath().
16861:
16862: =item
16863: $getuserdir - boolean: 1 if prepend path for "userfiles".
16864:
16865: =item
16866: $alternateRoot - path to prepend in place of path from $uri.
16867:
16868: =back
16869:
16870: Returns: Array of up to two items.
16871:
16872: =over
16873:
16874: a reference to an array of files/subdirectories
16875:
16876: =over
16877:
16878: Each element in the array of files/subdirectories is a & separated list of
16879: item name and the result of running stat on the item. If dirlist was requested
16880: for a file instead of a directory, the item name will be ''. For a directory
16881: listing, if the item is a metadata file, the element will end &N&M
16882: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16883: default copyright set (1).
16884:
16885: =back
16886:
16887: a scalar containing error condition (if encountered).
16888:
16889: =over
16890:
16891: =item
16892: no_host (no homeserver identified for $username:$domain).
16893:
16894: =item
16895: no_such_host (server contacted for listing not identified as valid host).
16896:
16897: =item
16898: con_lost (connection to remote server failed).
16899:
16900: =item
16901: refused (invalid $username:$domain received on lond side).
16902:
16903: =item
16904: no_such_dir (directory at specified path on lond side does not exist).
16905:
16906: =item
16907: empty (directory at specified path on lond side is empty).
16908:
16909: =over
16910:
16911: This is currently not encountered because the &ls3, &ls2,
16912: &ls (_handler) routines on the lond side do not filter out
16913: . and .. from a directory listing.
16914:
16915: =back
16916:
16917: =back
16918:
16919: =back
1.191 harris41 16920:
16921: =item *
16922:
1.243 albertel 16923: spareserver() : find server with least workload from spare.tab
16924:
1.986 foxr 16925:
16926: =item *
16927:
16928: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16929: if there is no corresponding loncapa host.
16930:
1.243 albertel 16931: =back
16932:
1.986 foxr 16933:
1.243 albertel 16934: =head2 Apache Request
16935:
16936: =over 4
1.191 harris41 16937:
16938: =item *
16939:
1.243 albertel 16940: ssi($url,%hash) : server side include, does a complete request cycle on url to
16941: localhost, posts hash
16942:
16943: =back
16944:
16945: =head2 Data to String to Data
16946:
16947: =over 4
1.191 harris41 16948:
16949: =item *
16950:
1.243 albertel 16951: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16952: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 16953:
16954: =item *
16955:
1.243 albertel 16956: hashref2str($hashref) : convert a hashref into a string complete with
16957: escaping and '=' and '&' separators, supports elements that are
16958: arrayrefs and hashrefs
1.191 harris41 16959:
16960: =item *
16961:
1.243 albertel 16962: arrayref2str($arrayref) : convert an arrayref into a string complete
16963: with escaping and '&' separators, supports elements that are arrayrefs
16964: and hashrefs
1.191 harris41 16965:
16966: =item *
16967:
1.243 albertel 16968: str2hash($string) : convert string to hash using unescaping and
16969: splitting on '=' and '&', supports elements that are arrayrefs and
16970: hashrefs
1.191 harris41 16971:
16972: =item *
16973:
1.243 albertel 16974: str2array($string) : convert string to hash using unescaping and
16975: splitting on '&', supports elements that are arrayrefs and hashrefs
16976:
16977: =back
16978:
16979: =head2 Logging Routines
16980:
16981:
16982: These routines allow one to make log messages in the lonnet.log and
16983: lonnet.perm logfiles.
1.191 harris41 16984:
1.1119 foxr 16985: =over 4
16986:
1.191 harris41 16987: =item *
16988:
1.243 albertel 16989: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 16990:
16991: =item *
16992:
1.243 albertel 16993: logthis() : append message to the normal lonnet.log file, it gets
16994: preiodically rolled over and deleted.
1.191 harris41 16995:
16996: =item *
16997:
1.243 albertel 16998: logperm() : append a permanent message to lonnet.perm.log, this log
16999: file never gets deleted by any automated portion of the system, only
17000: messages of critical importance should go in here.
17001:
1.1119 foxr 17002:
1.243 albertel 17003: =back
17004:
17005: =head2 General File Helper Routines
17006:
17007: =over 4
1.191 harris41 17008:
17009: =item *
17010:
1.481 raeburn 17011: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17012: (a) files in /uploaded
17013: (i) If a local copy of the file exists -
17014: compares modification date of local copy with last-modified date for
17015: definitive version stored on home server for course. If local copy is
17016: stale, requests a new version from the home server and stores it.
17017: If the original has been removed from the home server, then local copy
17018: is unlinked.
17019: (ii) If local copy does not exist -
17020: requests the file from the home server and stores it.
17021:
17022: If $caller is 'uploadrep':
17023: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17024: for request for files originally uploaded via DOCS.
17025: - returns 'ok' if fresh local copy now available, -1 otherwise.
17026:
17027: Otherwise:
17028: This indicates a call from the content generation phase of the request.
17029: - returns the entire contents of the file or -1.
17030:
17031: (b) files in /res
17032: - returns the entire contents of a file or -1;
17033: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 17034:
1.712 albertel 17035:
17036: =item *
17037:
17038: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17039: reference
17040:
17041: returns either a stat() list of data about the file or an empty list
17042: if the file doesn't exist or couldn't find out about it (connection
17043: problems or user unknown)
17044:
1.191 harris41 17045: =item *
17046:
1.243 albertel 17047: filelocation($dir,$file) : returns file system location of a file
17048: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17049: directory that relative $file lookups are to looked in ($dir of /a/dir
17050: and a file of ../bob will become /a/bob)
1.191 harris41 17051:
17052: =item *
17053:
17054: hreflocation($dir,$file) : returns file system location or a URL; same as
17055: filelocation except for hrefs
17056:
17057: =item *
17058:
1.1172.2.49 raeburn 17059: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17060: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 17061:
1.243 albertel 17062: =back
17063:
1.608 albertel 17064: =head2 Usererfile file routines (/uploaded*)
17065:
17066: =over 4
17067:
17068: =item *
17069:
17070: userfileupload(): main rotine for putting a file in a user or course's
17071: filespace, arguments are,
17072:
1.620 albertel 17073: formname - required - this is the name of the element in $env where the
1.608 albertel 17074: filename, and the contents of the file to create/modifed exist
1.620 albertel 17075: the filename is in $env{'form.'.$formname.'.filename'} and the
17076: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 17077: context - if coursedoc, store the file in the course of the active role
17078: of the current user;
17079: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17080: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 17081: subdir - required - subdirectory to put the file in under ../userfiles/
17082: if undefined, it will be placed in "unknown"
17083:
17084: (This routine calls clean_filename() to remove any dangerous
17085: characters from the filename, and then calls finuserfileupload() to
17086: complete the transaction)
17087:
17088: returns either the url of the uploaded file (/uploaded/....) if successful
17089: and /adm/notfound.html if unsuccessful
17090:
17091: =item *
17092:
17093: clean_filename(): routine for cleaing a filename up for storage in
17094: userfile space, argument is:
17095:
17096: filename - proposed filename
17097:
17098: returns: the new clean filename
17099:
17100: =item *
17101:
1.1090 raeburn 17102: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 17103: userspace, probably shouldn't be called directly
17104:
17105: docuname: username or courseid of destination for the file
17106: docudom: domain of user/course of destination for the file
17107: formname: same as for userfileupload()
1.1090 raeburn 17108: fname: filename (including subdirectories) for the file
17109: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
1.1172.2.109 raeburn 17110: if hashref, and context is scantron, will convert csv format to standard format
1.1090 raeburn 17111: allfiles: reference to hash used to store objects found by parser
17112: codebase: reference to hash used for codebases of java objects found by parser
17113: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17114: thumbheight: height (pixels) of thumbnail to be created for uploaded image
17115: resizewidth: width to be used to resize image using resizeImage from ImageMagick
17116: resizeheight: height to be used to resize image using resizeImage from ImageMagick
17117: context: if 'overwrite', will move the uploaded file from its temporary location to
17118: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 17119: mimetype: reference to scalar to accommodate mime type determined
17120: from File::MMagic if $parser = parse.
1.608 albertel 17121:
17122: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 17123: and /adm/notfound.html if unsuccessful (or an error message if context
17124: was 'overwrite').
17125:
1.608 albertel 17126:
17127: =item *
17128:
17129: renameuserfile(): renames an existing userfile to a new name
17130:
17131: Args:
17132: docuname: username or courseid of destination for the file
17133: docudom: domain of user/course of destination for the file
17134: old: current file name (including any subdirs under userfiles)
17135: new: desired file name (including any subdirs under userfiles)
17136:
17137: =item *
17138:
17139: mkdiruserfile(): creates a directory is a userfiles dir
17140:
17141: Args:
17142: docuname: username or courseid of destination for the file
17143: docudom: domain of user/course of destination for the file
17144: dir: dir to create (including any subdirs under userfiles)
17145:
17146: =item *
17147:
17148: removeuserfile(): removes a file that exists in userfiles
17149:
17150: Args:
17151: docuname: username or courseid of destination for the file
17152: docudom: domain of user/course of destination for the file
17153: fname: filname to delete (including any subdirs under userfiles)
17154:
17155: =item *
17156:
17157: removeuploadedurl(): convience function for removeuserfile()
17158:
17159: Args:
17160: url: a full /uploaded/... url to delete
17161:
1.747 albertel 17162: =item *
17163:
17164: get_portfile_permissions():
17165: Args:
17166: domain: domain of user or course contain the portfolio files
17167: user: name of user or num of course contain the portfolio files
17168: Returns:
17169: hashref of a dump of the proper file_permissions.db
17170:
17171:
17172: =item *
17173:
17174: get_access_controls():
17175:
17176: Args:
17177: current_permissions: the hash ref returned from get_portfile_permissions()
17178: group: (optional) the group you want the files associated with
17179: file: (optional) the file you want access info on
17180:
17181: Returns:
1.749 raeburn 17182: a hash (keys are file names) of hashes containing
17183: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17184: values are XML containing access control settings (see below)
1.747 albertel 17185:
17186: Internal notes:
17187:
1.749 raeburn 17188: access controls are stored in file_permissions.db as key=value pairs.
17189: key -> path to file/file_name\0uniqueID:scope_end_start
17190: where scope -> public,guest,course,group,domains or users.
17191: end -> UNIX time for end of access (0 -> no end date)
17192: start -> UNIX time for start of access
17193:
17194: value -> XML description of access control
17195: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17196: <start></start>
17197: <end></end>
17198:
17199: <password></password> for scope type = guest
17200:
17201: <domain></domain> for scope type = course or group
17202: <number></number>
17203: <roles id="">
17204: <role></role>
17205: <access></access>
17206: <section></section>
17207: <group></group>
17208: </roles>
17209:
17210: <dom></dom> for scope type = domains
17211:
17212: <users> for scope type = users
17213: <user>
17214: <uname></uname>
17215: <udom></udom>
17216: </user>
17217: </users>
17218: </scope>
17219:
17220: Access data is also aggregated for each file in an additional key=value pair:
17221: key -> path to file/file_name\0accesscontrol
17222: value -> reference to hash
17223: hash contains key = value pairs
17224: where key = uniqueID:scope_end_start
17225: value = UNIX time record was last updated
17226:
17227: Used to improve speed of look-ups of access controls for each file.
17228:
17229: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17230:
1.1172.2.13 raeburn 17231: =item *
17232:
1.749 raeburn 17233: modify_access_controls():
17234:
17235: Modifies access controls for a portfolio file
17236: Args
17237: 1. file name
17238: 2. reference to hash of required changes,
17239: 3. domain
17240: 4. username
17241: where domain,username are the domain of the portfolio owner
17242: (either a user or a course)
17243:
17244: Returns:
17245: 1. result of additions or updates ('ok' or 'error', with error message).
17246: 2. result of deletions ('ok' or 'error', with error message).
17247: 3. reference to hash of any new or updated access controls.
17248: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17249: key = integer (inbound ID)
1.1172.2.13 raeburn 17250: value = uniqueID
17251:
17252: =item *
17253:
17254: get_timebased_id():
17255:
17256: Attempts to get a unique timestamp-based suffix for use with items added to a
17257: course via the Course Editor (e.g., folders, composite pages,
17258: group bulletin boards).
17259:
17260: Args: (first three required; six others optional)
17261:
17262: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17263: docssequence, or name of group
17264:
17265: 2. keyid (alphanumeric): name of temporary locking key in hash,
17266: e.g., num, boardids
17267:
17268: 3. namespace: name of gdbm file used to store suffixes already assigned;
17269: file will be named nohist_namespace.db
17270:
17271: 4. cdom: domain of course; default is current course domain from %env
17272:
17273: 5. cnum: course number; default is current course number from %env
17274:
17275: 6. idtype: set to concat if an additional digit is to be appended to the
17276: unix timestamp to form the suffix, if the plain timestamp is already
17277: in use. Default is to not do this, but simply increment the unix
17278: timestamp by 1 until a unique key is obtained.
17279:
17280: 7. who: holder of locking key; defaults to user:domain for user.
17281:
17282: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
17283: retrying); default is 3.
17284:
17285: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
17286:
17287: Returns:
17288:
17289: 1. suffix obtained (numeric)
17290:
17291: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17292:
17293: 3. error: contains (localized) error message if an error occurred.
17294:
1.747 albertel 17295:
1.608 albertel 17296: =back
17297:
1.243 albertel 17298: =head2 HTTP Helper Routines
17299:
17300: =over 4
17301:
1.191 harris41 17302: =item *
17303:
17304: escape() : unpack non-word characters into CGI-compatible hex codes
17305:
17306: =item *
17307:
17308: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17309:
1.243 albertel 17310: =back
17311:
17312: =head1 PRIVATE SUBROUTINES
17313:
17314: =head2 Underlying communication routines (Shouldn't call)
17315:
17316: =over 4
17317:
17318: =item *
17319:
17320: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17321:
17322: =item *
17323:
17324: reply() : uses subreply to send a message to remote machine, logs all failures
17325:
17326: =item *
17327:
17328: critical() : passes a critical message to another server; if cannot
17329: get through then place message in connection buffer directory and
17330: returns con_delayed, if incapable of saving message, returns
17331: con_failed
17332:
17333: =item *
17334:
17335: reconlonc() : tries to reconnect lonc client processes.
17336:
17337: =back
17338:
17339: =head2 Resource Access Logging
17340:
17341: =over 4
17342:
17343: =item *
17344:
17345: flushcourselogs() : flush (save) buffer logs and access logs
17346:
17347: =item *
17348:
17349: courselog($what) : save message for course in hash
17350:
17351: =item *
17352:
17353: courseacclog($what) : save message for course using &courselog(). Perform
17354: special processing for specific resource types (problems, exams, quizzes, etc).
17355:
1.191 harris41 17356: =item *
17357:
17358: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17359: as a PerlChildExitHandler
1.243 albertel 17360:
17361: =back
17362:
17363: =head2 Other
17364:
17365: =over 4
17366:
17367: =item *
17368:
17369: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 17370:
17371: =back
17372:
17373: =cut
1.877 foxr 17374:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>